Mercurial > dropbear
comparison buffer.c @ 1057:16584026a1f0 nocircbuffer
allocate buffer and data in a single allocation
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 01 Mar 2015 21:16:09 +0800 |
parents | a5b785c12340 |
children | 2fa71c3b2827 aaf576b27a10 |
comparison
equal
deleted
inserted
replaced
1056:a2bfd4374878 | 1057:16584026a1f0 |
---|---|
44 | 44 |
45 if (size > BUF_MAX_SIZE) { | 45 if (size > BUF_MAX_SIZE) { |
46 dropbear_exit("buf->size too big"); | 46 dropbear_exit("buf->size too big"); |
47 } | 47 } |
48 | 48 |
49 buf = (buffer*)m_malloc(sizeof(buffer)); | 49 buf = (buffer*)m_malloc(sizeof(buffer)+size); |
50 | 50 |
51 if (size > 0) { | 51 if (size > 0) { |
52 buf->data = (unsigned char*)m_malloc(size); | 52 buf->data = (unsigned char*)buf + sizeof(buffer); |
53 } else { | 53 } else { |
54 buf->data = NULL; | 54 buf->data = NULL; |
55 } | 55 } |
56 | 56 |
57 buf->size = size; | 57 buf->size = size; |
58 buf->pos = 0; | |
59 buf->len = 0; | |
60 | 58 |
61 return buf; | 59 return buf; |
62 | 60 |
63 } | 61 } |
64 | 62 |
65 /* free the buffer's data and the buffer itself */ | 63 /* free the buffer's data and the buffer itself */ |
66 void buf_free(buffer* buf) { | 64 void buf_free(buffer* buf) { |
67 | 65 |
68 m_free(buf->data) | |
69 m_free(buf); | 66 m_free(buf); |
70 } | 67 } |
71 | 68 |
72 /* overwrite the contents of the buffer to clear it */ | 69 /* overwrite the contents of the buffer to clear it */ |
73 void buf_burn(buffer* buf) { | 70 void buf_burn(buffer* buf) { |
76 | 73 |
77 } | 74 } |
78 | 75 |
79 /* resize a buffer, pos and len will be repositioned if required when | 76 /* resize a buffer, pos and len will be repositioned if required when |
80 * downsizing */ | 77 * downsizing */ |
81 void buf_resize(buffer *buf, unsigned int newsize) { | 78 buffer* buf_resize(buffer *buf, unsigned int newsize) { |
82 | 79 |
83 if (newsize > BUF_MAX_SIZE) { | 80 if (newsize > BUF_MAX_SIZE) { |
84 dropbear_exit("buf->size too big"); | 81 dropbear_exit("buf->size too big"); |
85 } | 82 } |
86 | 83 |
87 buf->data = m_realloc(buf->data, newsize); | 84 buf = m_realloc(buf, sizeof(buffer)+newsize); |
85 buf->data = (unsigned char*)buf + sizeof(buffer); | |
88 buf->size = newsize; | 86 buf->size = newsize; |
89 buf->len = MIN(newsize, buf->len); | 87 buf->len = MIN(newsize, buf->len); |
90 buf->pos = MIN(newsize, buf->pos); | 88 buf->pos = MIN(newsize, buf->pos); |
91 | 89 return buf; |
92 } | 90 } |
93 | 91 |
94 /* Create a copy of buf, allocating required memory etc. */ | 92 /* Create a copy of buf, allocating required memory etc. */ |
95 /* The new buffer is sized the same as the length of the source buffer. */ | 93 /* The new buffer is sized the same as the length of the source buffer. */ |
96 buffer* buf_newcopy(buffer* buf) { | 94 buffer* buf_newcopy(buffer* buf) { |
225 return ret; | 223 return ret; |
226 } | 224 } |
227 | 225 |
228 /* Return a string as a newly allocated buffer */ | 226 /* Return a string as a newly allocated buffer */ |
229 buffer * buf_getstringbuf(buffer *buf) { | 227 buffer * buf_getstringbuf(buffer *buf) { |
230 buffer *ret; | 228 buffer *ret = NULL; |
231 unsigned char* str; | 229 unsigned int len = buf_getint(buf); |
232 unsigned int len; | 230 if (len > MAX_STRING_LEN) { |
233 str = buf_getstring(buf, &len); | 231 dropbear_exit("String too long"); |
234 ret = m_malloc(sizeof(*ret)); | 232 } |
235 ret->data = str; | 233 ret = buf_new(len); |
236 ret->len = len; | 234 memcpy(buf_getwriteptr(ret, len), buf_getptr(buf, len), len); |
237 ret->size = len; | 235 buf_incrpos(buf, len); |
238 ret->pos = 0; | 236 buf_incrlen(ret, len); |
239 return ret; | 237 return ret; |
240 } | 238 } |
241 | 239 |
242 /* Just increment the buffer position the same as if we'd used buf_getstring, | 240 /* Just increment the buffer position the same as if we'd used buf_getstring, |
243 * but don't bother copying/malloc()ing for it */ | 241 * but don't bother copying/malloc()ing for it */ |