1 /*
2  * Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include "yajl_buf.h"
18 
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #define YAJL_BUF_INIT_SIZE 2048
24 
25 struct yajl_buf_t {
26     size_t len;
27     size_t used;
28     unsigned char * data;
29     yajl_alloc_funcs * alloc;
30 };
31 
32 static
yajl_buf_ensure_available(yajl_buf buf,size_t want)33 void yajl_buf_ensure_available(yajl_buf buf, size_t want)
34 {
35     size_t need;
36 
37     assert(buf != NULL);
38 
39     /* first call */
40     if (buf->data == NULL) {
41         buf->len = YAJL_BUF_INIT_SIZE;
42         buf->data = (unsigned char *) YA_MALLOC(buf->alloc, buf->len);
43         buf->data[0] = 0;
44     }
45 
46     need = buf->len;
47 
48     while (want >= (need - buf->used)) need <<= 1;
49 
50     if (need != buf->len) {
51         buf->data = (unsigned char *) YA_REALLOC(buf->alloc, buf->data, need);
52         buf->len = need;
53     }
54 }
55 
yajl_buf_alloc(yajl_alloc_funcs * alloc)56 yajl_buf yajl_buf_alloc(yajl_alloc_funcs * alloc)
57 {
58     yajl_buf b = YA_MALLOC(alloc, sizeof(struct yajl_buf_t));
59     memset((void *) b, 0, sizeof(struct yajl_buf_t));
60     b->alloc = alloc;
61     return b;
62 }
63 
yajl_buf_free(yajl_buf buf)64 void yajl_buf_free(yajl_buf buf)
65 {
66     assert(buf != NULL);
67     if (buf->data) YA_FREE(buf->alloc, buf->data);
68     YA_FREE(buf->alloc, buf);
69 }
70 
yajl_buf_append(yajl_buf buf,const void * data,size_t len)71 void yajl_buf_append(yajl_buf buf, const void * data, size_t len)
72 {
73     yajl_buf_ensure_available(buf, len);
74     if (len > 0) {
75         assert(data != NULL);
76         memcpy(buf->data + buf->used, data, len);
77         buf->used += len;
78         buf->data[buf->used] = 0;
79     }
80 }
81 
yajl_buf_clear(yajl_buf buf)82 void yajl_buf_clear(yajl_buf buf)
83 {
84     buf->used = 0;
85     if (buf->data) buf->data[buf->used] = 0;
86 }
87 
yajl_buf_data(yajl_buf buf)88 const unsigned char * yajl_buf_data(yajl_buf buf)
89 {
90     return buf->data;
91 }
92 
yajl_buf_len(yajl_buf buf)93 size_t yajl_buf_len(yajl_buf buf)
94 {
95     return buf->used;
96 }
97 
98 void
yajl_buf_truncate(yajl_buf buf,size_t len)99 yajl_buf_truncate(yajl_buf buf, size_t len)
100 {
101     assert(len <= buf->used);
102     buf->used = len;
103 }
104