File: bytebuffer.c

package info (click to toggle)
postgis 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 70,052 kB
  • sloc: ansic: 162,204; sql: 93,950; xml: 53,121; cpp: 12,646; perl: 5,658; sh: 5,369; makefile: 3,434; python: 1,205; yacc: 447; lex: 151; pascal: 58
file content (416 lines) | stat: -rw-r--r-- 9,856 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
 * http://postgis.net
 *
 * PostGIS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * PostGIS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PostGIS.  If not, see <http://www.gnu.org/licenses/>.
 *
 **********************************************************************
 *
 * Copyright 2015 Nicklas Avén <nicklas.aven@jordogskog.no>
 *
 **********************************************************************/



#include "liblwgeom_internal.h"
#include "bytebuffer.h"


/**
* Allocate just the internal buffer of an existing bytebuffer_t
* struct. Useful for allocating short-lived bytebuffers off the stack.
*/
void
bytebuffer_init_with_size(bytebuffer_t *s, size_t size)
{
	if ( size < BYTEBUFFER_STATICSIZE )
	{
		s->capacity = BYTEBUFFER_STATICSIZE;
		s->buf_start = s->buf_static;
	}
	else
	{
		s->buf_start = lwalloc(size);
		s->capacity = size;
	}
	s->readcursor = s->writecursor = s->buf_start;
	memset(s->buf_start, 0, s->capacity);
}

/**
* Free the bytebuffer_t and all memory managed within it.
*/
void
bytebuffer_destroy_buffer(bytebuffer_t *s)
{
	if ( s->buf_start != s->buf_static )
	{
		lwfree(s->buf_start);
		s->buf_start = NULL;
	}

	return;
}

/**
* If necessary, expand the bytebuffer_t internal buffer to accommodate the
* specified additional size.
*/
static inline void
bytebuffer_makeroom(bytebuffer_t *s, size_t size_to_add)
{
	LWDEBUGF(2,"Entered bytebuffer_makeroom with space need of %d", size_to_add);
	size_t current_write_size = (s->writecursor - s->buf_start);
	size_t capacity = s->capacity;
	size_t required_size = current_write_size + size_to_add;

	LWDEBUGF(2,"capacity = %d and required size = %d",capacity ,required_size);
	while (capacity < required_size)
		capacity *= 2;

	if ( capacity > s->capacity )
	{
		size_t current_read_size = (s->readcursor - s->buf_start);
		LWDEBUGF(4,"We need to realloc more memory. New capacity is %d", capacity);
		if ( s->buf_start == s->buf_static )
		{
			s->buf_start = lwalloc(capacity);
			memcpy(s->buf_start, s->buf_static, s->capacity);
		}
		else
		{
			s->buf_start = lwrealloc(s->buf_start, capacity);
		}
		s->capacity = capacity;
		s->writecursor = s->buf_start + current_write_size;
		s->readcursor = s->buf_start + current_read_size;
	}
	return;
}

/** Returns a copy of the internal buffer */
lwvarlena_t *
bytebuffer_get_buffer_varlena(const bytebuffer_t *s)
{
	size_t bufsz = bytebuffer_getlength(s);
	lwvarlena_t *v = lwalloc(bufsz + LWVARHDRSZ);
	memcpy(v->data, s->buf_start, bufsz);
	LWSIZE_SET(v->size, bufsz + LWVARHDRSZ);
	return v;
}

/** Returns a read-only reference to the internal buffer */
const uint8_t*
bytebuffer_get_buffer(const bytebuffer_t *s, size_t *buffer_length)
{
	if ( buffer_length )
		*buffer_length = bytebuffer_getlength(s);
	return s->buf_start;
}

/**
* Writes a uint8_t value to the buffer
*/
void
bytebuffer_append_byte(bytebuffer_t *s, const uint8_t val)
{
	LWDEBUGF(2,"Entered bytebuffer_append_byte with value %d", val);
	bytebuffer_makeroom(s, 1);
	*(s->writecursor)=val;
	s->writecursor += 1;
	return;
}

/**
* Writes a uint8_t value to the buffer
*/
void
bytebuffer_append_bytebuffer(bytebuffer_t *write_to,bytebuffer_t *write_from )
{
	LWDEBUG(2,"bytebuffer_append_bytebuffer");
	size_t size = bytebuffer_getlength(write_from);
	bytebuffer_makeroom(write_to, size);
	memcpy(write_to->writecursor, write_from->buf_start, size);
	write_to->writecursor += size;
	return;
}

/**
* Writes a signed varInt to the buffer
*/
void
bytebuffer_append_varint(bytebuffer_t *b, const int64_t val)
{
	bytebuffer_makeroom(b, 16);
	b->writecursor += varint_s64_encode_buf(val, b->writecursor);
	return;
}

/**
* Writes a unsigned varInt to the buffer
*/
void
bytebuffer_append_uvarint(bytebuffer_t *b, const uint64_t val)
{
	bytebuffer_makeroom(b, 16);
	b->writecursor += varint_u64_encode_buf(val, b->writecursor);
	return;
}

/**
 * Returns the length of the current buffer
 */
size_t
bytebuffer_getlength(const bytebuffer_t *s)
{
	return (size_t)(s->writecursor - s->buf_start);
}

/* Unused functions */
#if 0

/** Returns a copy of the internal buffer */
uint8_t*
bytebuffer_get_buffer_copy(const bytebuffer_t *s, size_t *buffer_length)
{
	size_t bufsz = bytebuffer_getlength(s);
	uint8_t *buf = lwalloc(bufsz);
	memcpy(buf, s->buf_start, bufsz);
	if ( buffer_length )
		*buffer_length = bufsz;
	return buf;
}

/**
* Allocate a new bytebuffer_t. Use bytebuffer_destroy to free.
*/
bytebuffer_t*
bytebuffer_create(void)
{
	LWDEBUG(2,"Entered bytebuffer_create");
	return bytebuffer_create_with_size(BYTEBUFFER_STARTSIZE);
}

 /**
* Allocate a new bytebuffer_t. Use bytebuffer_destroy to free.
*/
bytebuffer_t*
bytebuffer_create_with_size(size_t size)
{
	LWDEBUGF(2,"Entered bytebuffer_create_with_size %d", size);
	bytebuffer_t *s;

	s = lwalloc(sizeof(bytebuffer_t));
	if ( size < BYTEBUFFER_STATICSIZE )
	{
		s->capacity = BYTEBUFFER_STATICSIZE;
		s->buf_start = s->buf_static;
	}
	else
	{
		s->buf_start = lwalloc(size);
		s->capacity = size;
	}
	s->readcursor = s->writecursor = s->buf_start;
	memset(s->buf_start,0,s->capacity);
	LWDEBUGF(4,"We create a buffer on %p of %d bytes", s->buf_start, s->capacity);
	return s;
}

/**
* Free the bytebuffer_t and all memory managed within it.
*/
void
bytebuffer_destroy(bytebuffer_t *s)
{
	bytebuffer_destroy_buffer(s);
	if ( s )
		lwfree(s);

	return;
}

/**
* Set the read cursor to the beginning
*/
void
bytebuffer_reset_reading(bytebuffer_t *s)
{
	s->readcursor = s->buf_start;
}

 /**
* Reset the bytebuffer_t. Useful for starting a fresh string
* without the expense of freeing and re-allocating a new
* bytebuffer_t.
*/
void
bytebuffer_clear(bytebuffer_t *s)
{
	s->readcursor = s->writecursor = s->buf_start;
}

/**
* Writes a uint8_t value to the buffer
*/
void
bytebuffer_append_bulk(bytebuffer_t *s, void * start, size_t size)
{
	LWDEBUGF(2,"bytebuffer_append_bulk with size %d",size);
	bytebuffer_makeroom(s, size);
	memcpy(s->writecursor, start, size);
	s->writecursor += size;
	return;
}

/*
* Writes Integer to the buffer
*/
void
bytebuffer_append_int(bytebuffer_t *buf, const int val, int swap)
{
	LWDEBUGF(2,"Entered bytebuffer_append_int with value %d, swap = %d", val, swap);

	LWDEBUGF(4,"buf_start = %p and write_cursor=%p", buf->buf_start,buf->writecursor);
	char *iptr = (char*)(&val);
	int i = 0;

	if ( sizeof(int) != WKB_INT_SIZE )
	{
		lwerror("Machine int size is not %d bytes!", WKB_INT_SIZE);
	}

	bytebuffer_makeroom(buf, WKB_INT_SIZE);
	/* Machine/request arch mismatch, so flip byte order */
	if ( swap)
	{
		LWDEBUG(4,"Ok, let's do the swapping thing");
		for ( i = 0; i < WKB_INT_SIZE; i++ )
		{
			*(buf->writecursor) = iptr[WKB_INT_SIZE - 1 - i];
			buf->writecursor += 1;
		}
	}
	/* If machine arch and requested arch match, don't flip byte order */
	else
	{
		LWDEBUG(4,"Ok, let's do the memcopying thing");
		memcpy(buf->writecursor, iptr, WKB_INT_SIZE);
		buf->writecursor += WKB_INT_SIZE;
	}

	LWDEBUGF(4,"buf_start = %p and write_cursor=%p", buf->buf_start,buf->writecursor);
	return;

 }





 /**
* Writes a float64 to the buffer
*/
void
bytebuffer_append_double(bytebuffer_t *buf, const double val, int swap)
{
	LWDEBUGF(2,"Entered bytebuffer_append_double with value %lf swap = %d", val, swap);

	LWDEBUGF(4,"buf_start = %p and write_cursor=%p", buf->buf_start,buf->writecursor);
	char *dptr = (char*)(&val);
	int i = 0;

	if ( sizeof(double) != WKB_DOUBLE_SIZE )
	{
		lwerror("Machine double size is not %d bytes!", WKB_DOUBLE_SIZE);
	}

	bytebuffer_makeroom(buf, WKB_DOUBLE_SIZE);

	/* Machine/request arch mismatch, so flip byte order */
	if ( swap )
	{
		LWDEBUG(4,"Ok, let's do the swapping thing");
		for ( i = 0; i < WKB_DOUBLE_SIZE; i++ )
		{
			*(buf->writecursor) = dptr[WKB_DOUBLE_SIZE - 1 - i];
			buf->writecursor += 1;
		}
	}
	/* If machine arch and requested arch match, don't flip byte order */
	else
	{
		LWDEBUG(4,"Ok, let's do the memcopying thing");
		memcpy(buf->writecursor, dptr, WKB_DOUBLE_SIZE);
		buf->writecursor += WKB_DOUBLE_SIZE;
	}

	LWDEBUG(4,"Return from bytebuffer_append_double");
	return;

 }

 /**
* Reads a signed varInt from the buffer
*/
int64_t
bytebuffer_read_varint(bytebuffer_t *b)
{
	size_t size;
	int64_t val = varint_s64_decode(b->readcursor, b->buf_start + b->capacity, &size);
	b->readcursor += size;
	return val;
}

 /**
* Reads a unsigned varInt from the buffer
*/
uint64_t
bytebuffer_read_uvarint(bytebuffer_t *b)
{
	size_t size;
	uint64_t val = varint_u64_decode(b->readcursor, b->buf_start + b->capacity, &size);
	b->readcursor += size;
	return val;
}


 /**
* Returns a new bytebuffer were both ingoing bytebuffers is merged.
* Caller is responsible for freeing both incoming bytebuffers and resulting bytebuffer
*/
bytebuffer_t*
bytebuffer_merge(bytebuffer_t **buff_array, int nbuffers)
{
	size_t total_size = 0, current_size, acc_size = 0;
	int i;
	for ( i = 0; i < nbuffers; i++ )
	{
		total_size += bytebuffer_getlength(buff_array[i]);
	}

	bytebuffer_t *res = bytebuffer_create_with_size(total_size);
	for ( i = 0; i < nbuffers; i++)
	{
		current_size = bytebuffer_getlength(buff_array[i]);
		memcpy(res->buf_start+acc_size, buff_array[i]->buf_start, current_size);
		acc_size += current_size;
	}
	res->writecursor = res->buf_start + total_size;
	res->readcursor = res->buf_start;
	return res;
}

#endif