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 libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $XConsortium: Buffer.C /main/5 1996/10/04 11:25:25 drk $
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
51 // If necessary in the future, we can rework Buffer class to provide
52 // global buffer by default and private buffers on request. DJB
54 char *Buffer::f_start;
56 int Buffer::f_reference_count;
58 // /////////////////////////////////////////////////////////////////
60 // /////////////////////////////////////////////////////////////////
63 Buffer::init (u_int initial_size)
65 f_start = new char [initial_size];
66 f_end = f_start + initial_size;
69 Buffer::Buffer (u_int initial_size)
71 // printf ("Constructing a %d byte buffer\n", initial_size);
75 f_end_of_data = f_point;
80 // /////////////////////////////////////////////////////////////////
82 // /////////////////////////////////////////////////////////////////
88 if (f_reference_count == 0)
99 // /////////////////////////////////////////////////////////////////
100 // check_space - make sure the is space for additional data
101 // /////////////////////////////////////////////////////////////////
104 Buffer::check_space (u_int size)
106 assert (f_start != NULL);
107 assert (f_point != NULL);
108 assert (f_end != NULL);
109 assert (f_end_of_data != NULL);
110 if (f_point + size > f_end)
112 // Allocate new, larger space.
113 char *new_block = new char [f_point + size - f_start];
114 // Copy bytes in use up to (but not including) point.
115 if (f_point - f_start > 0)
116 memcpy (new_block, f_start, f_point - f_start);
117 // Compute the point in the new buffer.
118 f_point = new_block + (f_point - f_start);
120 // Update start and end relative to new buffer.
122 f_end = f_start + size;
127 // /////////////////////////////////////////////////////////////////
128 // read/write for integers
129 // /////////////////////////////////////////////////////////////////
132 static u_int g_ordering = 0x01020304;
134 static u_int g_ordering = 0x01020304;
136 static const unsigned char *const g_byte_pos =
137 (const unsigned char *const) &g_ordering;
139 static char *const g_intbuf = (char *) &g_int;
142 Buffer::write (const int integer)
144 char *inbuf = (char *) &integer;
146 // Convert bytes from native to MSB first ordering.
148 for (unsigned int i = 0; i < sizeof (int); i++)
149 if (g_byte_pos[i] != 0)
150 g_intbuf[g_byte_pos[i]-1] = inbuf[i];
152 write (g_intbuf, 4, 1);
156 Buffer::read (int *integer)
160 // Convert from MSB first ordering to native.
161 for (unsigned int i = 0; i < sizeof (int); i++)
162 if (g_byte_pos[i] != 0)
163 ((char *) integer)[i] = f_point[g_byte_pos[i]-1];
166 assert (f_point <= f_end_of_data);
170 // /////////////////////////////////////////////////////////////////
171 // read/write for strings
172 // /////////////////////////////////////////////////////////////////
175 Buffer::write (const char *string)
177 int length = strlen (string);
179 write (string, sizeof (char), length + 1);
182 // NOTE: The caller is responsible for deleting the string that is
183 // returned by this function!
186 Buffer::read (char **string)
190 ON_DEBUG(printf ("Reading a %d byte string\n", length));
191 *string = new char [length + 1];
192 read (*string, sizeof (char), length + 1);
193 ON_DEBUG(printf ("String read is actually %ld bytes\n", (long)strlen(*string)));
194 assert (length == strlen (*string));
195 ON_DEBUG(printf ("Read string <%s>\n", *string));
200 Buffer::read (char *const string)
204 read (string, sizeof (char), length + 1);
205 assert (length == strlen (string));
210 // /////////////////////////////////////////////////////////////////
211 // read/write for bytes
212 // /////////////////////////////////////////////////////////////////
215 Buffer::write (const char *bytes, u_int size, u_int length)
217 ON_DEBUG(printf ("Writing %d bytes to buffer @ %p\n", size * length, f_point));
218 int num_bytes = size * length;
219 check_space (num_bytes);
220 for (unsigned int i = 0; i < size * length; i++)
221 ON_DEBUG(printf ("%02X ", (unsigned char) bytes[i]));
222 ON_DEBUG(printf ("\n"));
223 memcpy (f_point, bytes, num_bytes);
224 f_point += num_bytes;
225 f_end_of_data = f_point;
229 Buffer::read (char **bytes, u_int size, u_int length)
231 int num_bytes = size * length;
232 ON_DEBUG(printf ("Reading %d bytes from buffer @ %p\n", num_bytes, f_point));
233 assert (f_point + num_bytes <= f_end_of_data);
235 for (unsigned int i = 0; i < size * length; i++)
236 ON_DEBUG(printf ("%02X ", (unsigned int) (*bytes)[i]));
237 ON_DEBUG(printf ("\n"));
238 f_point += num_bytes;
242 Buffer::read (char *const bytes, u_int size, u_int length)
245 read (&b, size, length);
246 memcpy (bytes, b, size * length);