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);
165 #define CASTBNDEXCEPT
167 /***********************************************************/
168 // Get sz chars to the array x. x is supposed allocated
169 /***********************************************************/
170 buffer& buffer::get(char *x, int sz)
172 if ( sz + v_aptr > v_eptr ) {
173 MESSAGE(cerr, "buffer::get(): underflow");
174 throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sz + v_aptr) ));
177 memcpy(x, v_aptr, sz);
182 /***********************************************************/
184 /***********************************************************/
185 buffer& buffer::skip(int i)
187 if ( i + v_aptr > v_eptr )
188 MESSAGE(cerr, "buffer::skip(): underflow");
189 throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(i + v_aptr) ));
196 /***********************************************************/
198 /***********************************************************/
199 buffer& buffer::get(char& y)
201 if ( v_aptr > v_eptr) {
202 MESSAGE(cerr, "buffer::get(char&): underflow");
203 throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(1+v_aptr)));
211 buffer& buffer::getusc(int& y)
213 y = *(unsigned char*)v_aptr;
218 /***********************************************************/
219 // Get an integer to y. option can be ASCII or BINARY.
220 /***********************************************************/
221 buffer& buffer::get(int& y)
229 buffer& buffer::get(unsigned int& y)
231 if ( v_aptr + sizeof(y) > v_eptr ) {
232 MESSAGE(cerr, "buffer::get(int&): underflow");
233 throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(unsigned)+v_aptr)));
236 get((char*)&y, sizeof(y));
239 if ( v_swap_order == true )
246 /***********************************************************/
247 // Get a long to y. option can be ASCII or BINARY.
248 /***********************************************************/
249 buffer& buffer::get(long& y)
251 //MESSAGE(cerr, "WARNING: buffer::get(long& y) +++++++++++++++++++++++");
252 if ( v_aptr + sizeof(y) > v_eptr ) {
253 MESSAGE(cerr, "buffer::get(long&): underflow");
254 throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(long)+v_aptr)));
257 get((char*)&y, sizeof(y));
260 if ( v_swap_order == true )
267 /***********************************************************/
269 /***********************************************************/
270 buffer& buffer::get(float& y)
272 if ( v_aptr + sizeof(y) > v_eptr ) {
273 MESSAGE(cerr, "buffer::get(float &): underflow");
274 throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(float)+v_aptr)));
277 get((char*)&y, sizeof(y));
280 if ( v_swap_order == true )
287 buffer& buffer::get(unsigned short& y)
289 if ( v_aptr + sizeof(y) > v_eptr ) {
290 MESSAGE(cerr, "buffer::get(float &): underflow");
291 throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(float)+v_aptr)));
294 get((char*)&y, sizeof(y));
297 if ( v_swap_order == true )
298 ORDER_SWAP_USHORT(y);
304 /***********************************************************/
305 // Put a char to buffer.
306 /***********************************************************/
307 buffer& buffer::put(const char content, Boolean exp_buf)
309 //return put((char*)&content, sizeof(content));
311 if ( (int) v_bufsz == content_sz() )
313 if ( exp_buf == true )
314 expand_chunk(v_bufsz + 10);
316 MESSAGE( cerr, "buffer::put(const char): overflow");
317 throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, 1) );
326 buffer& buffer::put(const unsigned char content, Boolean exp_buf)
328 return put((char*)&content, sizeof(content), exp_buf);
331 /***********************************************************/
332 // Put a unsigned int to buffer.
333 /***********************************************************/
334 buffer& buffer::put(const int content, Boolean exp_buf)
336 return put((unsigned int)content, exp_buf);
339 buffer& buffer::put(const unsigned int content, Boolean exp_buf)
342 if ( v_swap_order == true )
343 ORDER_SWAP_UINT(content);
346 return put((char*)&content, sizeof(content), exp_buf);
349 /***********************************************************/
350 // Put a long to buffer.
351 /***********************************************************/
352 buffer& buffer::put(const long content, Boolean exp_buf)
354 //MESSAGE(cerr, "WARNING: buffer::put(long& y) =====================");
356 if ( v_swap_order == true )
357 ORDER_SWAP_LONG(content);
360 return put((char*)&content, sizeof(content), exp_buf);
363 /***********************************************************/
364 // Put a unsigned short to buffer.
365 /***********************************************************/
366 buffer& buffer::put(const unsigned short content, Boolean exp_buf)
369 if ( v_swap_order == true )
370 ORDER_SWAP_USHORT(content);
373 return put((char*)&content, sizeof(content), exp_buf);
376 /***********************************************************/
377 // Put a float to buffer.
378 /***********************************************************/
379 buffer& buffer::put(const float content, Boolean exp_buf)
382 if ( v_swap_order == true )
383 ORDER_SWAP_FLOAT(content);
386 return put((char*)&content, sizeof(content), exp_buf);
389 /***********************************************************/
390 // Put sz chars to buffer.
391 /***********************************************************/
392 buffer& buffer::put(const char* content, int sz, Boolean exp_buf)
394 if ( sz > (int)(v_bufsz - content_sz()) ) {
395 if ( exp_buf == true )
396 expand_chunk(v_bufsz + sz);
398 MESSAGE( cerr, "buffer::put(char*, int): overflow");
399 throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, sz) );
403 //memcpy(v_eptr, content, sz);
405 //debug(cerr, int(v_base));
406 //debug(cerr, int(v_eptr));
408 for ( int i=0; i<sz; i++ ) {
409 v_eptr[i] = content[i];
410 //debug(cerr, int(v_eptr[i]));
417 Boolean operator ==(buffer&x, buffer& y)
419 if ( x.content_sz() != y.content_sz() ) {
420 debug(cerr, x.content_sz());
421 debug(cerr, y.content_sz());
424 unsigned char* x_buf = (unsigned char*)x.get_base();
425 unsigned char* y_buf = (unsigned char*)y.get_base();
427 for ( int i=0; i<x.content_sz(); i++ ) {
428 if ( x_buf[i] != y_buf[i] ) {
430 debug(cerr, x_buf[i]);
431 debug(cerr, y_buf[i]);
439 /***********************************************************/
441 /***********************************************************/
443 ostream& operator<<(ostream& s, buffer& b)
447 //debug(s, b.content_sz());
452 int x = b.v_eptr - b.v_base ;
455 for ( i = 0; i < x; i++ ) {
459 if ( isprint(b.v_base[i]) )
462 cout << int(b.v_base[i]);
466 MESSAGE(cerr, "buffer=");
467 for ( i = 0; i < x; i++ ) {
468 cout << int(b.v_base[i]) << " ";
476 void buffer::cdrIn(buffer& buf)
479 buf.get(v_base, v_bufsz);
482 void buffer::cdrOut(buffer& buf)
485 buf.put(v_base, v_bufsz);