2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
24 * $TOG: buffer.C /main/9 1998/04/17 11:50:56 mgreess $
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.
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.
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
45 #include "utility/buffer.h"
47 /***********************************************************/
49 /***********************************************************/
51 buffer::buffer(const int sz)
57 buffer::buffer(buffer& original): v_swap_order(original.v_swap_order)
59 _alloc(original.v_bufsz);
60 memcpy(v_aptr, original.v_aptr, original.v_bufsz);
63 void buffer::_alloc(const int sz)
65 int void_ptr_size = sizeof(voidPtr);
70 v_base = ::new char[void_ptr_size+sz];
72 memset(v_base, (char)0, void_ptr_size+sz);
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;
86 v_aptr = v_eptr = v_base;
90 /***********************************************************/
92 /***********************************************************/
95 if ( v_allocated == 1 ) {
96 free((char*)(v_base-v_align_offset));
104 memset(v_base, (char)0, v_bufsz);
108 void buffer::set_chunk(char* ptr, const int sz)
111 eptr = aptr = base = ptr;
115 void buffer::set_content_sz(const int sz)
121 /***********************************************************/
122 // Expand buffer chunk to newsz.
123 /***********************************************************/
124 int buffer::expand_chunk(const int newsz)
126 if ( v_allocated == 0 )
127 throw(stringException("expand a buffer with reference memory"));
129 int cursz = buf_sz();
131 if ( newsz <= cursz ) {
136 int void_ptr_size = sizeof(voidPtr);
138 //MESSAGE(cerr, "real expand");
140 //char* x = (char*)malloc(void_ptr_size+newsz);
141 char* x = ::new char [void_ptr_size+newsz];
143 int delta = int(void_ptr_size - long(x) % void_ptr_size);
146 memcpy(x, v_base, cursz);
148 memset(x+cursz, char(0), newsz - cursz);
150 v_aptr -= long(v_base);
151 v_eptr -= long(v_base);
153 //free((char*)(v_base-v_align_offset));
154 delete (char*)(v_base-v_align_offset);
155 v_align_offset = delta;
158 v_aptr += long(v_base);
159 v_eptr += long(v_base);
166 #define CASTBNDEXCEPT (boundaryException*)
168 #define CASTBNDEXCEPT
171 /***********************************************************/
172 // Get sz chars to the array x. x is supposed allocated
173 /***********************************************************/
174 buffer& buffer::get(char *x, int sz)
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) ));
181 memcpy(x, v_aptr, sz);
186 /***********************************************************/
188 /***********************************************************/
189 buffer& buffer::skip(int i)
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) ));
200 /***********************************************************/
202 /***********************************************************/
203 buffer& buffer::get(char& y)
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)));
215 buffer& buffer::getusc(int& y)
217 y = *(unsigned char*)v_aptr;
222 /***********************************************************/
223 // Get an integer to y. option can be ASCII or BINARY.
224 /***********************************************************/
225 buffer& buffer::get(int& y)
233 buffer& buffer::get(unsigned int& y)
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)));
240 get((char*)&y, sizeof(y));
243 if ( v_swap_order == true )
250 /***********************************************************/
251 // Get a long to y. option can be ASCII or BINARY.
252 /***********************************************************/
253 buffer& buffer::get(long& y)
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)));
261 get((char*)&y, sizeof(y));
264 if ( v_swap_order == true )
271 /***********************************************************/
273 /***********************************************************/
274 buffer& buffer::get(float& y)
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)));
281 get((char*)&y, sizeof(y));
284 if ( v_swap_order == true )
291 buffer& buffer::get(unsigned short& y)
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)));
298 get((char*)&y, sizeof(y));
301 if ( v_swap_order == true )
302 ORDER_SWAP_USHORT(y);
308 /***********************************************************/
309 // Put a char to buffer.
310 /***********************************************************/
311 buffer& buffer::put(const char content, Boolean exp_buf)
313 //return put((char*)&content, sizeof(content));
315 if ( v_bufsz == content_sz() )
317 if ( exp_buf == true )
318 expand_chunk(v_bufsz + 10);
320 MESSAGE( cerr, "buffer::put(const char): overflow");
321 throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, 1) );
330 buffer& buffer::put(const unsigned char content, Boolean exp_buf)
332 return put((char*)&content, sizeof(content), exp_buf);
335 /***********************************************************/
336 // Put a unsigned int to buffer.
337 /***********************************************************/
338 buffer& buffer::put(const int content, Boolean exp_buf)
340 return put((unsigned int)content, exp_buf);
343 buffer& buffer::put(const unsigned int content, Boolean exp_buf)
346 if ( v_swap_order == true )
347 ORDER_SWAP_UINT(content);
350 return put((char*)&content, sizeof(content), exp_buf);
353 /***********************************************************/
354 // Put a long to buffer.
355 /***********************************************************/
356 buffer& buffer::put(const long content, Boolean exp_buf)
358 //MESSAGE(cerr, "WARNING: buffer::put(long& y) =====================");
360 if ( v_swap_order == true )
361 ORDER_SWAP_LONG(content);
364 return put((char*)&content, sizeof(content), exp_buf);
367 /***********************************************************/
368 // Put a unsigned short to buffer.
369 /***********************************************************/
370 buffer& buffer::put(const unsigned short content, Boolean exp_buf)
373 if ( v_swap_order == true )
374 ORDER_SWAP_USHORT(content);
377 return put((char*)&content, sizeof(content), exp_buf);
380 /***********************************************************/
381 // Put a float to buffer.
382 /***********************************************************/
383 buffer& buffer::put(const float content, Boolean exp_buf)
386 if ( v_swap_order == true )
387 ORDER_SWAP_FLOAT(content);
390 return put((char*)&content, sizeof(content), exp_buf);
393 /***********************************************************/
394 // Put sz chars to buffer.
395 /***********************************************************/
396 buffer& buffer::put(const char* content, int sz, Boolean exp_buf)
398 if ( sz > v_bufsz - content_sz() ) {
399 if ( exp_buf == true )
400 expand_chunk(v_bufsz + sz);
402 MESSAGE( cerr, "buffer::put(char*, int): overflow");
403 throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, sz) );
407 //memcpy(v_eptr, content, sz);
409 //debug(cerr, int(v_base));
410 //debug(cerr, int(v_eptr));
412 for ( int i=0; i<sz; i++ ) {
413 v_eptr[i] = content[i];
414 //debug(cerr, int(v_eptr[i]));
421 Boolean operator ==(buffer&x, buffer& y)
423 if ( x.content_sz() != y.content_sz() ) {
424 debug(cerr, x.content_sz());
425 debug(cerr, y.content_sz());
428 unsigned char* x_buf = (unsigned char*)x.get_base();
429 unsigned char* y_buf = (unsigned char*)y.get_base();
431 for ( int i=0; i<x.content_sz(); i++ ) {
432 if ( x_buf[i] != y_buf[i] ) {
434 debug(cerr, x_buf[i]);
435 debug(cerr, y_buf[i]);
437 debug(cerr, hex(x_buf[i]));
438 debug(cerr, hex(y_buf[i]));
447 /***********************************************************/
449 /***********************************************************/
451 ostream& operator<<(ostream& s, buffer& b)
455 //debug(s, b.content_sz());
460 int x = b.v_eptr - b.v_base ;
463 for ( int i = 0; i < x; i++ ) {
467 if ( isprint(b.v_base[i]) )
470 cout << int(b.v_base[i]);
474 MESSAGE(cerr, "buffer=");
475 for ( i = 0; i < x; i++ ) {
476 cout << int(b.v_base[i]) << " ";
484 void buffer::cdrIn(buffer& buf)
487 buf.get(v_base, v_bufsz);
490 void buffer::cdrOut(buffer& buf)
493 buf.put(v_base, v_bufsz);