PostGIS  3.0.6dev-r@@SVN_REVISION@@
bytebuffer.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright 2015 Nicklas Avén <nicklas.aven@jordogskog.no>
22  *
23  **********************************************************************/
24 
25 
26 
27 #include "liblwgeom_internal.h"
28 #include "bytebuffer.h"
29 
30 
35 void
37 {
38  if ( size < BYTEBUFFER_STATICSIZE )
39  {
40  s->capacity = BYTEBUFFER_STATICSIZE;
41  s->buf_start = s->buf_static;
42  }
43  else
44  {
45  s->buf_start = lwalloc(size);
46  s->capacity = size;
47  }
48  s->readcursor = s->writecursor = s->buf_start;
49  memset(s->buf_start, 0, s->capacity);
50 }
51 
55 void
57 {
58  if ( s->buf_start != s->buf_static )
59  {
60  lwfree(s->buf_start);
61  s->buf_start = NULL;
62  }
63 
64  return;
65 }
66 
71 static inline void
72 bytebuffer_makeroom(bytebuffer_t *s, size_t size_to_add)
73 {
74  LWDEBUGF(2,"Entered bytebuffer_makeroom with space need of %d", size_to_add);
75  size_t current_write_size = (s->writecursor - s->buf_start);
76  size_t capacity = s->capacity;
77  size_t required_size = current_write_size + size_to_add;
78 
79  LWDEBUGF(2,"capacity = %d and required size = %d",capacity ,required_size);
80  while (capacity < required_size)
81  capacity *= 2;
82 
83  if ( capacity > s->capacity )
84  {
85  size_t current_read_size = (s->readcursor - s->buf_start);
86  LWDEBUGF(4,"We need to realloc more memory. New capacity is %d", capacity);
87  if ( s->buf_start == s->buf_static )
88  {
89  s->buf_start = lwalloc(capacity);
90  memcpy(s->buf_start, s->buf_static, s->capacity);
91  }
92  else
93  {
94  s->buf_start = lwrealloc(s->buf_start, capacity);
95  }
96  s->capacity = capacity;
97  s->writecursor = s->buf_start + current_write_size;
98  s->readcursor = s->buf_start + current_read_size;
99  }
100  return;
101 }
102 
104 uint8_t*
105 bytebuffer_get_buffer_copy(const bytebuffer_t *s, size_t *buffer_length)
106 {
107  size_t bufsz = bytebuffer_getlength(s);
108  uint8_t *buf = lwalloc(bufsz);
109  memcpy(buf, s->buf_start, bufsz);
110  if ( buffer_length )
111  *buffer_length = bufsz;
112  return buf;
113 }
114 
116 const uint8_t*
117 bytebuffer_get_buffer(const bytebuffer_t *s, size_t *buffer_length)
118 {
119  if ( buffer_length )
120  *buffer_length = bytebuffer_getlength(s);
121  return s->buf_start;
122 }
123 
127 void
129 {
130  LWDEBUGF(2,"Entered bytebuffer_append_byte with value %d", val);
132  *(s->writecursor)=val;
133  s->writecursor += 1;
134  return;
135 }
136 
140 void
142 {
143  LWDEBUG(2,"bytebuffer_append_bytebuffer");
144  size_t size = bytebuffer_getlength(write_from);
145  bytebuffer_makeroom(write_to, size);
146  memcpy(write_to->writecursor, write_from->buf_start, size);
147  write_to->writecursor += size;
148  return;
149 }
150 
154 void
156 {
157  bytebuffer_makeroom(b, 16);
159  return;
160 }
161 
165 void
167 {
168  bytebuffer_makeroom(b, 16);
170  return;
171 }
172 
176 size_t
178 {
179  return (size_t)(s->writecursor - s->buf_start);
180 }
181 
182 /* Unused functions */
183 #if 0
184 
189 bytebuffer_create(void)
190 {
191  LWDEBUG(2,"Entered bytebuffer_create");
192  return bytebuffer_create_with_size(BYTEBUFFER_STARTSIZE);
193 }
194 
199 bytebuffer_create_with_size(size_t size)
200 {
201  LWDEBUGF(2,"Entered bytebuffer_create_with_size %d", size);
202  bytebuffer_t *s;
203 
204  s = lwalloc(sizeof(bytebuffer_t));
205  if ( size < BYTEBUFFER_STATICSIZE )
206  {
207  s->capacity = BYTEBUFFER_STATICSIZE;
208  s->buf_start = s->buf_static;
209  }
210  else
211  {
212  s->buf_start = lwalloc(size);
213  s->capacity = size;
214  }
215  s->readcursor = s->writecursor = s->buf_start;
216  memset(s->buf_start,0,s->capacity);
217  LWDEBUGF(4,"We create a buffer on %p of %d bytes", s->buf_start, s->capacity);
218  return s;
219 }
220 
224 void
225 bytebuffer_destroy(bytebuffer_t *s)
226 {
228  if ( s )
229  lwfree(s);
230 
231  return;
232 }
233 
237 void
238 bytebuffer_reset_reading(bytebuffer_t *s)
239 {
240  s->readcursor = s->buf_start;
241 }
242 
248 void
249 bytebuffer_clear(bytebuffer_t *s)
250 {
251  s->readcursor = s->writecursor = s->buf_start;
252 }
253 
257 void
258 bytebuffer_append_bulk(bytebuffer_t *s, void * start, size_t size)
259 {
260  LWDEBUGF(2,"bytebuffer_append_bulk with size %d",size);
261  bytebuffer_makeroom(s, size);
262  memcpy(s->writecursor, start, size);
263  s->writecursor += size;
264  return;
265 }
266 
267 /*
268 * Writes Integer to the buffer
269 */
270 void
271 bytebuffer_append_int(bytebuffer_t *buf, const int val, int swap)
272 {
273  LWDEBUGF(2,"Entered bytebuffer_append_int with value %d, swap = %d", val, swap);
274 
275  LWDEBUGF(4,"buf_start = %p and write_cursor=%p", buf->buf_start,buf->writecursor);
276  char *iptr = (char*)(&val);
277  int i = 0;
278 
279  if ( sizeof(int) != WKB_INT_SIZE )
280  {
281  lwerror("Machine int size is not %d bytes!", WKB_INT_SIZE);
282  }
283 
285  /* Machine/request arch mismatch, so flip byte order */
286  if ( swap)
287  {
288  LWDEBUG(4,"Ok, let's do the swaping thing");
289  for ( i = 0; i < WKB_INT_SIZE; i++ )
290  {
291  *(buf->writecursor) = iptr[WKB_INT_SIZE - 1 - i];
292  buf->writecursor += 1;
293  }
294  }
295  /* If machine arch and requested arch match, don't flip byte order */
296  else
297  {
298  LWDEBUG(4,"Ok, let's do the memcopying thing");
299  memcpy(buf->writecursor, iptr, WKB_INT_SIZE);
300  buf->writecursor += WKB_INT_SIZE;
301  }
302 
303  LWDEBUGF(4,"buf_start = %p and write_cursor=%p", buf->buf_start,buf->writecursor);
304  return;
305 
306  }
307 
308 
309 
310 
311 
315 void
316 bytebuffer_append_double(bytebuffer_t *buf, const double val, int swap)
317 {
318  LWDEBUGF(2,"Entered bytebuffer_append_double with value %lf swap = %d", val, swap);
319 
320  LWDEBUGF(4,"buf_start = %p and write_cursor=%p", buf->buf_start,buf->writecursor);
321  char *dptr = (char*)(&val);
322  int i = 0;
323 
324  if ( sizeof(double) != WKB_DOUBLE_SIZE )
325  {
326  lwerror("Machine double size is not %d bytes!", WKB_DOUBLE_SIZE);
327  }
328 
330 
331  /* Machine/request arch mismatch, so flip byte order */
332  if ( swap )
333  {
334  LWDEBUG(4,"Ok, let's do the swapping thing");
335  for ( i = 0; i < WKB_DOUBLE_SIZE; i++ )
336  {
337  *(buf->writecursor) = dptr[WKB_DOUBLE_SIZE - 1 - i];
338  buf->writecursor += 1;
339  }
340  }
341  /* If machine arch and requested arch match, don't flip byte order */
342  else
343  {
344  LWDEBUG(4,"Ok, let's do the memcopying thing");
345  memcpy(buf->writecursor, dptr, WKB_DOUBLE_SIZE);
347  }
348 
349  LWDEBUG(4,"Return from bytebuffer_append_double");
350  return;
351 
352  }
353 
357 int64_t
358 bytebuffer_read_varint(bytebuffer_t *b)
359 {
360  size_t size;
361  int64_t val = varint_s64_decode(b->readcursor, b->buf_start + b->capacity, &size);
362  b->readcursor += size;
363  return val;
364 }
365 
369 uint64_t
370 bytebuffer_read_uvarint(bytebuffer_t *b)
371 {
372  size_t size;
373  uint64_t val = varint_u64_decode(b->readcursor, b->buf_start + b->capacity, &size);
374  b->readcursor += size;
375  return val;
376 }
377 
378 
384 bytebuffer_merge(bytebuffer_t **buff_array, int nbuffers)
385 {
386  size_t total_size = 0, current_size, acc_size = 0;
387  int i;
388  for ( i = 0; i < nbuffers; i++ )
389  {
390  total_size += bytebuffer_getlength(buff_array[i]);
391  }
392 
393  bytebuffer_t *res = bytebuffer_create_with_size(total_size);
394  for ( i = 0; i < nbuffers; i++)
395  {
396  current_size = bytebuffer_getlength(buff_array[i]);
397  memcpy(res->buf_start+acc_size, buff_array[i]->buf_start, current_size);
398  acc_size += current_size;
399  }
400  res->writecursor = res->buf_start + total_size;
401  res->readcursor = res->buf_start;
402  return res;
403 }
404 
405 #endif
void bytebuffer_destroy_buffer(bytebuffer_t *s)
Free the bytebuffer_t and all memory managed within it.
Definition: bytebuffer.c:56
void bytebuffer_append_bytebuffer(bytebuffer_t *write_to, bytebuffer_t *write_from)
Writes a uint8_t value to the buffer.
Definition: bytebuffer.c:141
size_t bytebuffer_getlength(const bytebuffer_t *s)
Returns the length of the current buffer.
Definition: bytebuffer.c:177
void bytebuffer_append_byte(bytebuffer_t *s, const uint8_t val)
Writes a uint8_t value to the buffer.
Definition: bytebuffer.c:128
void bytebuffer_append_uvarint(bytebuffer_t *b, const uint64_t val)
Writes a unsigned varInt to the buffer.
Definition: bytebuffer.c:166
void bytebuffer_append_varint(bytebuffer_t *b, const int64_t val)
Writes a signed varInt to the buffer.
Definition: bytebuffer.c:155
uint8_t * bytebuffer_get_buffer_copy(const bytebuffer_t *s, size_t *buffer_length)
Returns a copy of the internal buffer.
Definition: bytebuffer.c:105
void bytebuffer_init_with_size(bytebuffer_t *s, size_t size)
Allocate just the internal buffer of an existing bytebuffer_t struct.
Definition: bytebuffer.c:36
static void bytebuffer_makeroom(bytebuffer_t *s, size_t size_to_add)
If necessary, expand the bytebuffer_t internal buffer to accomodate the specified additional size.
Definition: bytebuffer.c:72
const uint8_t * bytebuffer_get_buffer(const bytebuffer_t *s, size_t *buffer_length)
Returns a read-only reference to the internal buffer.
Definition: bytebuffer.c:117
#define BYTEBUFFER_STATICSIZE
Definition: bytebuffer.h:36
#define BYTEBUFFER_STARTSIZE
Definition: bytebuffer.h:35
char * s
Definition: cu_in_wkt.c:23
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:235
void lwfree(void *mem)
Definition: lwutil.c:242
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define WKB_INT_SIZE
#define WKB_DOUBLE_SIZE
Well-Known Binary (WKB) Output Variant Types.
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
tuple res
Definition: window.py:79
size_t capacity
Definition: bytebuffer.h:40
uint8_t * readcursor
Definition: bytebuffer.h:43
uint8_t * writecursor
Definition: bytebuffer.h:42
uint8_t * buf_start
Definition: bytebuffer.h:41
size_t varint_s64_encode_buf(int64_t val, uint8_t *buf)
Definition: varint.c:89
size_t varint_u64_encode_buf(uint64_t val, uint8_t *buf)
Definition: varint.c:76
int64_t varint_s64_decode(const uint8_t *the_start, const uint8_t *the_end, size_t *size)
Definition: varint.c:102
uint64_t varint_u64_decode(const uint8_t *the_start, const uint8_t *the_end, size_t *size)
Definition: varint.c:109