-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaaac.cpp
279 lines (227 loc) · 7.13 KB
/
aaac.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/************************************************************************
* DICOMLIB
* Copyright 2003 Sunnybrook and Women's College Health Science Center
* Implemented by Trevor Morgan ([email protected])
*
* See LICENSE.txt for copyright and licensing info.
*************************************************************************/
#include "aaac.hpp"
#include <algorithm>
#include <functional>
using namespace std;
/************************************************************************
*
* Presentation Context Accept
*
************************************************************************/
namespace dicom
{
namespace primitive
{
#ifndef _MSC_VER
/*
difference of opinion here between gcc and msvc as to whether constant static members
need to be instantiated seperately from their declaration..
(Are we sure about this?)
*/
const BYTE PresentationContextAccept::ItemType_;
const BYTE PresentationContextAccept::Reserved1_;
const BYTE PresentationContextAccept::Reserved2_;
// const BYTE PresentationContextAccept::Reserved3_;
const BYTE PresentationContextAccept::Reserved4_;
const BYTE AAssociateAC::ItemType_;
const BYTE AAssociateAC::Reserved1_;
const UINT16 AAssociateAC::ProtocolVersion_;
const UINT16 AAssociateAC::Reserved2_;
#endif
namespace
{
/*
These globals are only safe if we NEVER EVER actually trust the data on them.
(i.e. only use them for reading reserved data from byte stream that we don't
check the value on.)
Using them for anything else may cause thread-related data corruption.
*/
BYTE tmpBYTE;
UINT16 tmpUINT16;
UINT32 tmpUINT32;
}
PresentationContextAccept::PresentationContextAccept()
:TrnSyntax_(UID(""))
{}
void PresentationContextAccept::Write(Network::Socket& socket)
{
socket << ItemType_;
socket << Reserved1_;
socket << UINT16(Size()-4);
socket << PresentationContextID_;
socket << Reserved2_;
socket << Result_;
socket << Reserved4_;
TrnSyntax_.Write(socket);
}
UINT32 PresentationContextAccept::Read(Network::Socket& socket)
{
UINT32 byteread=0;
BYTE b;
socket >> b;
byteread+=sizeof(b);
EnforceItemType(b,ItemType_);
byteread+=ReadDynamic(socket);
return byteread;
}
/*!
See Section 8, table 9-18 for desciption of the following fields.
*/
UINT32 PresentationContextAccept::ReadDynamic(Network::Socket &socket)
{
UINT32 byteread=0;
socket >> tmpBYTE; //Reserved
byteread+=sizeof(tmpBYTE);
UINT16 length;
socket >> length; //number of bytes from here to the end of the TransferSyntax item
byteread+=sizeof(length);
//As far as I can see this is redundant apart from as an error check.
socket >> PresentationContextID_;
socket >> tmpBYTE; //Reserved
socket >> Result_; //between 0 and 4
socket >> tmpBYTE; //Reserved
byteread+=sizeof(PresentationContextID_)+sizeof(tmpBYTE)+sizeof(Result_)+sizeof(tmpBYTE);
byteread+=TrnSyntax_.Read(socket);
Enforce(Size()-4==length);
return byteread;
}
UINT16 PresentationContextAccept::Size()
{
return TrnSyntax_.Size() + 8;
}
/************************************************************************
*
* AAssociateAC Packet
*
************************************************************************/
AAssociateAC::AAssociateAC()
:AppContext_(UID(""))
{
std::fill(Reserved3_,Reserved3_+32,0);
}
AAssociateAC::AAssociateAC(const std::string& CallingApp, const std::string& CalledApp)
:CalledAppTitle_(CalledApp)
,CallingAppTitle_(CallingApp)
,AppContext_(UID(""))//not at all happy about this...
{
std::fill(Reserved3_,Reserved3_+32,0);
}
//!I feel this should be happening in the constructor...
void AAssociateAC::SetUserInformation(UserInformation &User)
{
UserInfo_ = User;
}
void AAssociateAC::Write(Network::Socket &socket)
{
socket << ItemType_;
socket << Reserved1_;
socket << UINT32(Size()-6);
socket << ProtocolVersion_;
socket << Reserved2_;
char StringBuffer[16];
std::fill(StringBuffer,StringBuffer+16,' ');
std::copy(CalledAppTitle_.begin(),CalledAppTitle_.end(),StringBuffer);
socket.Sendn(StringBuffer,16);
std::fill(StringBuffer,StringBuffer+16,' ');
std::copy(CallingAppTitle_.begin(),CallingAppTitle_.end(),StringBuffer);
socket.Sendn(StringBuffer,16);
socket.Sendn(Reserved3_,32);
AppContext_.Write(socket);
for_each(PresContextAccepts_.begin(),PresContextAccepts_.end(),
WriteToSocket(socket));
UserInfo_.Write(socket);
}
UINT32 AAssociateAC::Read(Network::Socket &socket)
{
UINT32 byteread=0;
BYTE b;
socket >> b;
byteread+=sizeof(b);
EnforceItemType(b,ItemType_);
byteread+=ReadDynamic(socket);
return byteread;
}
UINT32 AAssociateAC::ReadDynamic(Network::Socket &socket)
{
UINT32 byteread=0;
UINT32 tmp_read=0;
BYTE TempByte;
PresentationContextAccept PresContextAccept;//should be inside loop.
socket >> tmpBYTE;//Reserved1_;
byteread+=sizeof(tmpBYTE);
UINT32 length;
socket >> length;
socket >> tmpUINT16;
/*
verify that (tmpUINT16 bitand 0x01) is true; - see part 8, table 9-17
*/
socket >> tmpUINT16;
byteread+=sizeof(length)+2*sizeof(tmpUINT16);
CalledAppTitle_.assign(16,' ');
socket.Read(CalledAppTitle_);
byteread+=16;
StripTrailingWhitespace(CalledAppTitle_);
CallingAppTitle_.assign(16,' ');
socket.Read(CallingAppTitle_);
byteread+=16;
StripTrailingWhitespace(CallingAppTitle_);
socket.Readn(Reserved3_,32);
byteread+=32;
int BytesLeftToRead = length - sizeof(UINT16) - sizeof(UINT16) - 16 - 16 - 32;
while(BytesLeftToRead > 0)
{
socket >> TempByte;
BytesLeftToRead-=sizeof(TempByte);
byteread+=sizeof(TempByte);
switch(TempByte)
{
case 0x50: // user information
tmp_read=UserInfo_.ReadDynamic(socket);
byteread+=tmp_read;
BytesLeftToRead = BytesLeftToRead - tmp_read;//UserInfo_.Size();
break;
case 0x21:
tmp_read=PresContextAccept.ReadDynamic(socket);
BytesLeftToRead = BytesLeftToRead - tmp_read;//PresContextAccept.Size();
byteread+=tmp_read;
PresContextAccepts_.push_back(PresContextAccept);
break;
case 0x10:
tmp_read=AppContext_.ReadDynamic(socket);
byteread+=tmp_read;
BytesLeftToRead = BytesLeftToRead - tmp_read;//AppContext_.Size();
break;
default:
throw BadItemType(TempByte,0);
}
}
//This line is not enforcible because it may not be true-Sam 28Nov2007
//Enforce(length==Size()-6);
//This if will never happen because of the while loop above. -Sam 28Nov2007
//if(BytesLeftToRead)
// throw dicom::exception("non-zero remaining bytes in AAssociateAC::ReadDynamic()");
return byteread;
}
UINT32 AAssociateAC::Size()
{
UINT32 length;
length = sizeof(UINT16) + sizeof(UINT16) + 16 + 16 + 32;
length += AppContext_.Size();
unsigned int Index = 0;
Index = 0;
while(Index < PresContextAccepts_.size())
{
length += PresContextAccepts_[Index].Size();
++Index;
}
length += UserInfo_.Size();
return (length + sizeof(BYTE) + sizeof(BYTE) + sizeof(UINT32) );
}
}// namespace primitive
}//namespace dicom