/src/dcmtk/oflog/libsrc/sockbuff.cc
Line | Count | Source |
1 | | // Module: Log4CPLUS |
2 | | // File: socketbuffer.cxx |
3 | | // Created: 5/2003 |
4 | | // Author: Tad E. Smith |
5 | | // |
6 | | // |
7 | | // Copyright 2003-2010 Tad E. Smith |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | // you may not use this file except in compliance with the License. |
11 | | // You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software |
16 | | // distributed under the License is distributed on an "AS IS" BASIS, |
17 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | // See the License for the specific language governing permissions and |
19 | | // limitations under the License. |
20 | | |
21 | | #include <cstring> |
22 | | #include <limits> |
23 | | #include "dcmtk/oflog/helpers/sockbuff.h" |
24 | | #include "dcmtk/oflog/helpers/loglog.h" |
25 | | |
26 | | #if !defined(_WIN32) |
27 | | # include <netdb.h> |
28 | | #else |
29 | | # include "dcmtk/oflog/config/windowsh.h" |
30 | | #endif |
31 | | |
32 | | #if defined (DCMTK_LOG4CPLUS_HAVE_NETINET_IN_H) |
33 | | #include <netinet/in.h> |
34 | | #endif |
35 | | |
36 | | // helper methods to fix old-style casts warnings |
37 | | BEGIN_EXTERN_C |
38 | 0 | static unsigned short OFntohs(unsigned short us) { return ntohs(us); } |
39 | 0 | static unsigned short OFhtons(unsigned short us) { return htons(us); } |
40 | | END_EXTERN_C |
41 | | |
42 | | |
43 | | namespace dcmtk { |
44 | | namespace log4cplus { namespace helpers { |
45 | | |
46 | | |
47 | | ////////////////////////////////////////////////////////////////////////////// |
48 | | // SocketBuffer ctors and dtor |
49 | | ////////////////////////////////////////////////////////////////////////////// |
50 | | |
51 | | SocketBuffer::SocketBuffer(size_t maxsize_) |
52 | 0 | : maxsize(maxsize_), |
53 | 0 | size(0), |
54 | 0 | pos(0), |
55 | 0 | buffer(new char[maxsize]) |
56 | 0 | { |
57 | 0 | } |
58 | | |
59 | | |
60 | | SocketBuffer::~SocketBuffer() |
61 | 0 | { |
62 | 0 | delete [] buffer; |
63 | 0 | } |
64 | | |
65 | | |
66 | | ////////////////////////////////////////////////////////////////////////////// |
67 | | // SocketBuffer methods |
68 | | ////////////////////////////////////////////////////////////////////////////// |
69 | | |
70 | | unsigned char |
71 | | SocketBuffer::readByte() |
72 | 0 | { |
73 | 0 | if(pos >= maxsize) { |
74 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::readByte()- end of buffer reached")); |
75 | 0 | return 0; |
76 | 0 | } |
77 | 0 | else if((pos + sizeof(unsigned char)) > maxsize) { |
78 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::readByte()- Attempt to read beyond end of buffer")); |
79 | 0 | return 0; |
80 | 0 | } |
81 | | |
82 | 0 | unsigned char ret = OFstatic_cast(unsigned char, buffer[pos]); |
83 | 0 | pos += sizeof(unsigned char); |
84 | |
|
85 | 0 | return ret; |
86 | 0 | } |
87 | | |
88 | | |
89 | | |
90 | | unsigned short |
91 | | SocketBuffer::readShort() |
92 | 0 | { |
93 | 0 | if(pos >= maxsize) { |
94 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::readShort()- end of buffer reached")); |
95 | 0 | return 0; |
96 | 0 | } |
97 | 0 | else if((pos + sizeof(unsigned short)) > maxsize) { |
98 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::readShort()- Attempt to read beyond end of buffer")); |
99 | 0 | return 0; |
100 | 0 | } |
101 | | |
102 | 0 | unsigned short ret; |
103 | 0 | memcpy(&ret, buffer + pos, sizeof(ret)); |
104 | 0 | ret = OFntohs(ret); |
105 | 0 | pos += sizeof(unsigned short); |
106 | |
|
107 | 0 | return ret; |
108 | 0 | } |
109 | | |
110 | | |
111 | | |
112 | | unsigned int |
113 | | SocketBuffer::readInt() |
114 | 0 | { |
115 | 0 | if(pos >= maxsize) { |
116 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::readInt()- end of buffer reached")); |
117 | 0 | return 0; |
118 | 0 | } |
119 | 0 | else if((pos + sizeof(unsigned int)) > maxsize) { |
120 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::readInt()- Attempt to read beyond end of buffer")); |
121 | 0 | return 0; |
122 | 0 | } |
123 | | |
124 | 0 | unsigned int ret; |
125 | 0 | memcpy (&ret, buffer + pos, sizeof(ret)); |
126 | 0 | ret = ntohl(ret); |
127 | 0 | pos += sizeof(unsigned int); |
128 | | |
129 | 0 | return ret; |
130 | 0 | } |
131 | | |
132 | | |
133 | | tstring |
134 | | SocketBuffer::readString(unsigned char sizeOfChar) |
135 | 0 | { |
136 | 0 | size_t strlen = readInt(); |
137 | 0 | size_t bufferLen = strlen * sizeOfChar; |
138 | |
|
139 | 0 | if(strlen == 0) { |
140 | 0 | return tstring(); |
141 | 0 | } |
142 | 0 | if(pos > maxsize) { |
143 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::readString()- end of buffer reached")); |
144 | 0 | return tstring(); |
145 | 0 | } |
146 | | |
147 | 0 | if((pos + bufferLen) > maxsize) { |
148 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::readString()- Attempt to read beyond end of buffer")); |
149 | 0 | bufferLen = (maxsize - 1) - pos; |
150 | 0 | strlen = bufferLen / sizeOfChar; |
151 | 0 | } |
152 | |
|
153 | 0 | #ifndef DCMTK_OFLOG_UNICODE |
154 | 0 | if(sizeOfChar == 1) { |
155 | 0 | tstring ret(&buffer[pos], strlen); |
156 | 0 | pos += strlen; |
157 | 0 | return ret; |
158 | 0 | } |
159 | 0 | else if(sizeOfChar == 2) { |
160 | 0 | tstring ret; |
161 | 0 | for(tstring::size_type i=0; i<strlen; ++i) { |
162 | 0 | unsigned short tmp = readShort(); |
163 | 0 | ret += OFstatic_cast(char, tmp < 256 ? tmp : ' '); |
164 | 0 | } |
165 | 0 | return ret; |
166 | 0 | } |
167 | 0 | else { |
168 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::readString()- Invalid sizeOfChar!!!!")); |
169 | 0 | } |
170 | | |
171 | | #else /* DCMTK_OFLOG_UNICODE */ |
172 | | if(sizeOfChar == 1) { |
173 | | STD_NAMESPACE string ret(&buffer[pos], strlen); |
174 | | pos += strlen; |
175 | | return towstring(ret); |
176 | | } |
177 | | else if(sizeOfChar == 2) { |
178 | | tstring ret; |
179 | | for(tstring::size_type i=0; i<strlen; ++i) { |
180 | | ret += OFstatic_cast(tchar, readShort()); |
181 | | } |
182 | | return ret; |
183 | | } |
184 | | else { |
185 | | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::readString()- Invalid sizeOfChar!!!!")); |
186 | | } |
187 | | #endif |
188 | | |
189 | 0 | return tstring(); |
190 | 0 | } |
191 | | |
192 | | |
193 | | |
194 | | void |
195 | | SocketBuffer::appendByte(unsigned char val) |
196 | 0 | { |
197 | 0 | if((pos + sizeof(unsigned char)) > maxsize) { |
198 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::appendByte()- Attempt to write beyond end of buffer")); |
199 | 0 | return; |
200 | 0 | } |
201 | | |
202 | 0 | buffer[pos] = OFstatic_cast(char, val); |
203 | 0 | pos += sizeof(unsigned char); |
204 | 0 | size = pos; |
205 | 0 | } |
206 | | |
207 | | |
208 | | |
209 | | void |
210 | | SocketBuffer::appendShort(unsigned short val) |
211 | 0 | { |
212 | 0 | if((pos + sizeof(unsigned short)) > maxsize) { |
213 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::appendShort()- Attempt to write beyond end of buffer")); |
214 | 0 | return; |
215 | 0 | } |
216 | | |
217 | 0 | unsigned short s = OFhtons(val); |
218 | 0 | memcpy(buffer + pos, &s, sizeof (s)); |
219 | 0 | pos += sizeof(s); |
220 | 0 | size = pos; |
221 | 0 | } |
222 | | |
223 | | |
224 | | |
225 | | void |
226 | | SocketBuffer::appendInt(unsigned int val) |
227 | 0 | { |
228 | 0 | if((pos + sizeof(unsigned int)) > maxsize) { |
229 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::appendInt()- Attempt to write beyond end of buffer")); |
230 | 0 | return; |
231 | 0 | } |
232 | | |
233 | 0 | int i = htonl(val); |
234 | 0 | memcpy(buffer + pos, &i, sizeof (i)); |
235 | 0 | pos += sizeof(i); |
236 | 0 | size = pos; |
237 | 0 | } |
238 | | |
239 | | |
240 | | |
241 | | void |
242 | | SocketBuffer::appendString(const tstring& str) |
243 | 0 | { |
244 | 0 | size_t const strlen = str.length(); |
245 | 0 | static size_t const sizeOfChar = sizeof (tchar) == 1 ? 1 : 2; |
246 | |
|
247 | 0 | if((pos + sizeof(unsigned int) + strlen * sizeOfChar) > maxsize) |
248 | 0 | { |
249 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::appendString()-") |
250 | 0 | DCMTK_LOG4CPLUS_TEXT(" Attempt to write beyond end of buffer")); |
251 | 0 | return; |
252 | 0 | } |
253 | | |
254 | 0 | appendInt(OFstatic_cast(unsigned, strlen)); |
255 | 0 | #ifndef DCMTK_OFLOG_UNICODE |
256 | 0 | memcpy(&buffer[pos], str.data(), strlen); |
257 | 0 | pos += strlen; |
258 | 0 | size = pos; |
259 | | #else |
260 | | for(tstring::size_type i=0; i<str.length(); ++i) { |
261 | | appendShort(OFstatic_cast(unsigned short, str[i])); |
262 | | } |
263 | | #endif |
264 | 0 | } |
265 | | |
266 | | |
267 | | |
268 | | void |
269 | | SocketBuffer::appendBuffer(const SocketBuffer& buf) |
270 | 0 | { |
271 | 0 | if((pos + buf.getSize()) > maxsize) { |
272 | 0 | getLogLog().error(DCMTK_LOG4CPLUS_TEXT("SocketBuffer::appendBuffer()- Attempt to write beyond end of buffer")); |
273 | 0 | return; |
274 | 0 | } |
275 | | |
276 | 0 | memcpy(&buffer[pos], buf.buffer, buf.getSize()); |
277 | 0 | pos += buf.getSize(); |
278 | 0 | size = pos; |
279 | 0 | } |
280 | | |
281 | | |
282 | | } } // namespace log4cplus { namespace helpers { |
283 | | } // end namespace dcmtk |