comparison buffer.c @ 1069:2fa71c3b2827 pam

merge pam branch up to date
author Matt Johnston <matt@ucc.asn.au>
date Mon, 16 Mar 2015 21:34:05 +0800
parents bae0b34bc059 16584026a1f0
children
comparison
equal deleted inserted replaced
1068:9a6395ddb1b6 1069:2fa71c3b2827
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) {
97 95
98 buffer* ret; 96 buffer* ret;
99 97
100 ret = buf_new(buf->len); 98 ret = buf_new(buf->len);
101 ret->len = buf->len; 99 ret->len = buf->len;
102 memcpy(ret->data, buf->data, buf->len); 100 if (buf->len > 0) {
101 memcpy(ret->data, buf->data, buf->len);
102 }
103 return ret; 103 return ret;
104 } 104 }
105 105
106 /* Set the length of the buffer */ 106 /* Set the length of the buffer */
107 void buf_setlen(buffer* buf, unsigned int len) { 107 void buf_setlen(buffer* buf, unsigned int len) {
125 dropbear_exit("Bad buf_setpos"); 125 dropbear_exit("Bad buf_setpos");
126 } 126 }
127 buf->pos = pos; 127 buf->pos = pos;
128 } 128 }
129 129
130 /* increment the postion by incr, increasing the buffer length if required */ 130 /* increment the position by incr, increasing the buffer length if required */
131 void buf_incrwritepos(buffer* buf, unsigned int incr) { 131 void buf_incrwritepos(buffer* buf, unsigned int incr) {
132 if (incr > BUF_MAX_INCR || buf->pos + incr > buf->size) { 132 if (incr > BUF_MAX_INCR || buf->pos + incr > buf->size) {
133 dropbear_exit("Bad buf_incrwritepos"); 133 dropbear_exit("Bad buf_incrwritepos");
134 } 134 }
135 buf->pos += incr; 135 buf->pos += incr;
228 return ret; 228 return ret;
229 } 229 }
230 230
231 /* Return a string as a newly allocated buffer */ 231 /* Return a string as a newly allocated buffer */
232 buffer * buf_getstringbuf(buffer *buf) { 232 buffer * buf_getstringbuf(buffer *buf) {
233 buffer *ret; 233 buffer *ret = NULL;
234 unsigned char* str; 234 unsigned int len = buf_getint(buf);
235 unsigned int len; 235 if (len > MAX_STRING_LEN) {
236 str = buf_getstring(buf, &len); 236 dropbear_exit("String too long");
237 ret = m_malloc(sizeof(*ret)); 237 }
238 ret->data = str; 238 ret = buf_new(len);
239 ret->len = len; 239 memcpy(buf_getwriteptr(ret, len), buf_getptr(buf, len), len);
240 ret->size = len; 240 buf_incrpos(buf, len);
241 ret->pos = 0; 241 buf_incrlen(ret, len);
242 return ret; 242 return ret;
243 } 243 }
244 244
245 /* Just increment the buffer position the same as if we'd used buf_getstring, 245 /* Just increment the buffer position the same as if we'd used buf_getstring,
246 * but don't bother copying/malloc()ing for it */ 246 * but don't bother copying/malloc()ing for it */