Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / utility / buffer.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * $TOG: buffer.C /main/9 1998/04/17 11:50:56 mgreess $
25  *
26  * Copyright (c) 1992 HaL Computer Systems, Inc.  All rights reserved.
27  * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
28  * States.  Use of a copyright notice is precautionary only and does not
29  * imply publication or disclosure.
30  * 
31  * This software contains confidential information and trade secrets of HaL
32  * Computer Systems, Inc.  Use, disclosure, or reproduction is prohibited
33  * without the prior express written permission of HaL Computer Systems, Inc.
34  * 
35  *                         RESTRICTED RIGHTS LEGEND
36  * Use, duplication, or disclosure by the Government is subject to
37  * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
38  * Technical Data and Computer Software clause at DFARS 252.227-7013.
39  *                        HaL Computer Systems, Inc.
40  *                  1315 Dell Avenue, Campbell, CA  95008
41  * 
42  */
43
44
45 #include "utility/buffer.h"
46
47 /***********************************************************/
48 // Constructor
49 /***********************************************************/
50 /*
51 buffer::buffer(const int sz)
52 {
53    _alloc(sz);
54 }
55 */
56
57 buffer::buffer(buffer& original): v_swap_order(original.v_swap_order)
58 {
59    _alloc(original.v_bufsz);
60    memcpy(v_aptr, original.v_aptr, original.v_bufsz);
61 }
62
63 void buffer::_alloc(const int sz)
64 {
65    int void_ptr_size = sizeof(voidPtr);
66
67    v_bufsz = sz;
68    if ( sz > 0 ) {
69
70       v_base = ::new char[void_ptr_size+sz];
71
72       memset(v_base, (char)0, void_ptr_size+sz);
73
74       v_align_offset = int(void_ptr_size - long(v_base) % void_ptr_size);
75       if (v_align_offset != void_ptr_size)
76         v_base += v_align_offset;
77       else
78         v_align_offset = 0;
79
80       v_allocated = 1;
81
82    } else {
83       v_base = 0;
84       v_allocated = 0;
85    }
86    v_aptr = v_eptr = v_base;
87 }
88
89
90 /***********************************************************/
91 // Destructor
92 /***********************************************************/
93 buffer::~buffer()
94 {
95    if ( v_allocated == 1 ) {
96      free((char*)(v_base-v_align_offset));
97    }
98 }
99
100 void buffer::reset()
101 {
102    v_eptr = v_base;
103    v_aptr = v_base;
104    memset(v_base, (char)0, v_bufsz);
105 }
106    
107 /*
108 void buffer::set_chunk(char* ptr, const int sz)
109 {
110    bufsz = sz;
111    eptr = aptr = base = ptr;
112    allocated = 0;
113 }
114
115 void buffer::set_content_sz(const int sz)
116 {
117    eptr = base + sz;
118 }
119 */
120
121 /***********************************************************/
122 // Expand buffer chunk to newsz.
123 /***********************************************************/
124 int buffer::expand_chunk(const int newsz)
125 {
126    if ( v_allocated == 0 )
127       throw(stringException("expand a buffer with reference memory"));
128
129    int cursz = buf_sz();
130
131    if ( newsz <= cursz ) {
132       v_bufsz = newsz;
133       return 0;
134    }
135
136    int void_ptr_size = sizeof(voidPtr);
137
138 //MESSAGE(cerr, "real expand");
139
140    //char* x = (char*)malloc(void_ptr_size+newsz);
141    char* x = ::new char [void_ptr_size+newsz];
142
143    int delta = int(void_ptr_size - long(x) % void_ptr_size);
144    x += delta ;
145
146    memcpy(x, v_base, cursz);
147
148    memset(x+cursz, char(0), newsz - cursz);
149
150    v_aptr -= long(v_base);
151    v_eptr -= long(v_base);
152
153    //free((char*)(v_base-v_align_offset));
154    delete (char*)(v_base-v_align_offset);
155    v_align_offset = delta;
156
157    v_base = x;
158    v_aptr += long(v_base);
159    v_eptr += long(v_base);
160    v_bufsz = newsz;
161
162    return 0;
163 }
164
165 #if defined(linux)
166 #define CASTBNDEXCEPT (boundaryException*)
167 #else
168 #define CASTBNDEXCEPT
169 #endif
170
171 /***********************************************************/
172 // Get sz chars to the array x. x is supposed allocated
173 /***********************************************************/
174 buffer& buffer::get(char *x, int sz)
175 {
176    if ( sz + v_aptr > v_eptr ) {
177       MESSAGE(cerr, "buffer::get(): underflow");
178       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sz + v_aptr) ));
179    }
180
181    memcpy(x, v_aptr, sz);
182    v_aptr += sz;
183    return *this;
184 }
185
186 /***********************************************************/
187 // skip i chars.
188 /***********************************************************/
189 buffer& buffer::skip(int i)
190 {
191    if ( i + v_aptr > v_eptr ) 
192       MESSAGE(cerr, "buffer::skip(): underflow");
193       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(i + v_aptr) ));
194
195    v_aptr += i;
196    return *this;
197 }
198
199
200 /***********************************************************/
201 // Get a char to y.
202 /***********************************************************/
203 buffer& buffer::get(char& y)
204 {
205    if ( v_aptr > v_eptr) {
206       MESSAGE(cerr, "buffer::get(char&): underflow");
207       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(1+v_aptr)));
208    }
209
210    y = *v_aptr;
211    v_aptr++;
212    return *this;
213 }
214
215 buffer& buffer::getusc(int& y)
216 {
217    y = *(unsigned char*)v_aptr;
218    v_aptr++;
219    return *this;
220 }
221
222 /***********************************************************/
223 // Get an integer to y. option can be ASCII or BINARY.
224 /***********************************************************/
225 buffer& buffer::get(int& y)
226 {
227    unsigned int x;
228    get(x);
229    y = (int)x;
230    return *this;
231 }
232
233 buffer& buffer::get(unsigned int& y)
234 {
235    if ( v_aptr + sizeof(y) > v_eptr ) {
236       MESSAGE(cerr, "buffer::get(int&): underflow");
237       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(unsigned)+v_aptr)));
238    }
239
240    get((char*)&y, sizeof(y));
241
242 #ifdef PORTABLE_DB
243    if ( v_swap_order == true )
244       ORDER_SWAP_UINT(y);
245 #endif
246
247    return *this;
248 }
249
250 /***********************************************************/
251 // Get a long to y. option can be ASCII or BINARY.
252 /***********************************************************/
253 buffer& buffer::get(long& y)
254 {
255 //MESSAGE(cerr, "WARNING: buffer::get(long& y) +++++++++++++++++++++++");
256    if ( v_aptr + sizeof(y) > v_eptr ) {
257       MESSAGE(cerr, "buffer::get(long&): underflow");
258       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(long)+v_aptr)));
259    }
260
261    get((char*)&y, sizeof(y));
262
263 #ifdef PORTABLE_DB
264    if ( v_swap_order == true )
265       ORDER_SWAP_LONG(y);
266 #endif
267
268    return *this;
269 }
270
271 /***********************************************************/
272 // Get a float to y. 
273 /***********************************************************/
274 buffer& buffer::get(float& y)
275 {
276    if ( v_aptr + sizeof(y) > v_eptr ) {
277       MESSAGE(cerr, "buffer::get(float &): underflow");
278       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(float)+v_aptr)));
279    }
280
281    get((char*)&y, sizeof(y));
282
283 #ifdef PORTABLE_DB
284    if ( v_swap_order == true )
285       ORDER_SWAP_FLOAT(y);
286 #endif
287
288    return *this;
289 }
290
291 buffer& buffer::get(unsigned short& y)
292 {
293    if ( v_aptr + sizeof(y) > v_eptr ) {
294       MESSAGE(cerr, "buffer::get(float &): underflow");
295       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(float)+v_aptr)));
296    }
297
298    get((char*)&y, sizeof(y));
299
300 #ifdef PORTABLE_DB
301    if ( v_swap_order == true )
302       ORDER_SWAP_USHORT(y);
303 #endif
304
305    return *this;
306 }
307
308 /***********************************************************/
309 // Put a char to buffer.
310 /***********************************************************/
311 buffer& buffer::put(const char content, Boolean exp_buf) 
312 {
313    //return put((char*)&content, sizeof(content));
314
315    if ( v_bufsz == content_sz() ) 
316    {
317       if ( exp_buf == true )
318         expand_chunk(v_bufsz + 10);
319       else {
320          MESSAGE( cerr, "buffer::put(const char): overflow");
321          throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, 1) );
322       }
323    }
324
325    *v_eptr = content;
326    v_eptr++;
327    return *this;
328 }
329
330 buffer& buffer::put(const unsigned char content, Boolean exp_buf) 
331 {
332    return put((char*)&content, sizeof(content), exp_buf);
333 }
334
335 /***********************************************************/
336 // Put a unsigned int to buffer.
337 /***********************************************************/
338 buffer& buffer::put(const int content, Boolean exp_buf)
339 {
340    return put((unsigned int)content, exp_buf);
341 }
342
343 buffer& buffer::put(const unsigned int content, Boolean exp_buf)
344 {
345 #ifdef PORTABLE_DB
346    if ( v_swap_order == true )
347       ORDER_SWAP_UINT(content);
348 #endif
349
350    return put((char*)&content, sizeof(content), exp_buf);
351 }
352
353 /***********************************************************/
354 // Put a long to buffer.
355 /***********************************************************/
356 buffer& buffer::put(const long content, Boolean exp_buf)
357 {
358 //MESSAGE(cerr, "WARNING: buffer::put(long& y) =====================");
359 #ifdef PORTABLE_DB
360    if ( v_swap_order == true )
361       ORDER_SWAP_LONG(content);
362 #endif
363
364    return put((char*)&content, sizeof(content), exp_buf);
365 }
366
367 /***********************************************************/
368 // Put a unsigned short to buffer.
369 /***********************************************************/
370 buffer& buffer::put(const unsigned short content, Boolean exp_buf)
371 {
372 #ifdef PORTABLE_DB
373    if ( v_swap_order == true )
374       ORDER_SWAP_USHORT(content);
375 #endif
376
377    return put((char*)&content, sizeof(content), exp_buf);
378 }
379
380 /***********************************************************/
381 // Put a float to buffer.
382 /***********************************************************/
383 buffer& buffer::put(const float content, Boolean exp_buf)
384 {
385 #ifdef PORTABLE_DB
386    if ( v_swap_order == true )
387       ORDER_SWAP_FLOAT(content);
388 #endif
389
390    return put((char*)&content, sizeof(content), exp_buf);
391 }
392
393 /***********************************************************/
394 // Put sz chars to buffer.
395 /***********************************************************/
396 buffer& buffer::put(const char* content, int sz, Boolean exp_buf) 
397 {
398    if ( sz > v_bufsz - content_sz() ) {
399       if ( exp_buf == true ) 
400          expand_chunk(v_bufsz + sz);
401       else {
402          MESSAGE( cerr, "buffer::put(char*, int): overflow");
403          throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, sz) );
404       } 
405    } 
406       
407    //memcpy(v_eptr, content, sz);
408
409 //debug(cerr, int(v_base));
410 //debug(cerr, int(v_eptr));
411
412    for ( int i=0; i<sz; i++ ) {
413      v_eptr[i] = content[i];
414 //debug(cerr, int(v_eptr[i]));
415    }
416
417    v_eptr += sz;
418    return *this;
419 }
420
421 Boolean operator ==(buffer&x, buffer& y)
422 {
423    if ( x.content_sz() != y.content_sz() ) {
424       debug(cerr, x.content_sz());
425       debug(cerr, y.content_sz());
426    }
427
428    unsigned char* x_buf = (unsigned char*)x.get_base();
429    unsigned char* y_buf = (unsigned char*)y.get_base();
430
431    for ( int i=0; i<x.content_sz(); i++ ) {
432        if ( x_buf[i] != y_buf[i] ) {
433           debug(cerr, i);
434           debug(cerr, x_buf[i]);
435           debug(cerr, y_buf[i]);
436 #ifndef __osf__
437           debug(cerr, hex(x_buf[i]));
438           debug(cerr, hex(y_buf[i]));
439 #endif
440           //return false;
441        }
442    }
443
444    return true;
445 }
446
447 /***********************************************************/
448 // show content
449 /***********************************************************/
450
451 ostream& operator<<(ostream& s, buffer& b)
452 {
453
454    //debug(s, b.bufsz);
455    //debug(s, b.content_sz());
456    //debug(s, b.base);
457    //debug(s, b.eptr);
458    //debug(s, b.aptr);
459
460    int x = b.v_eptr - b.v_base ;
461
462
463    for ( int i = 0; i < x; i++ ) {
464       s << b.v_base[i];
465
466 /*
467       if ( isprint(b.v_base[i]) )
468          cout << b.v_base[i];
469       else
470          cout << int(b.v_base[i]);
471 */
472    }
473
474 MESSAGE(cerr, "buffer=");
475    for ( i = 0; i < x; i++ ) {
476       cout << int(b.v_base[i]) << " ";
477    }
478 cout << endl;
479
480    return s;
481 }
482
483 /*
484 void buffer::cdrIn(buffer& buf)
485 {
486    buf.get(v_bufsz);
487    buf.get(v_base, v_bufsz);
488 }
489
490 void buffer::cdrOut(buffer& buf)
491 {
492    buf.put(v_bufsz);
493    buf.put(v_base, v_bufsz);
494 }
495 */
496