Mercurial > dropbear
comparison buffer.c @ 641:2b1bb792cd4d dropbear-tfm
- Update tfm changes to current default tip
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 21 Nov 2011 19:52:28 +0800 |
parents | 76097ec1a29a a98a2138364a |
children | 33fd2f3499d2 |
comparison
equal
deleted
inserted
replaced
640:76097ec1a29a | 641:2b1bb792cd4d |
---|---|
106 } | 106 } |
107 | 107 |
108 /* Set the length of the buffer */ | 108 /* Set the length of the buffer */ |
109 void buf_setlen(buffer* buf, unsigned int len) { | 109 void buf_setlen(buffer* buf, unsigned int len) { |
110 if (len > buf->size) { | 110 if (len > buf->size) { |
111 dropbear_exit("bad buf_setlen"); | 111 dropbear_exit("Bad buf_setlen"); |
112 } | 112 } |
113 buf->len = len; | 113 buf->len = len; |
114 } | 114 } |
115 | 115 |
116 /* Increment the length of the buffer */ | 116 /* Increment the length of the buffer */ |
117 void buf_incrlen(buffer* buf, unsigned int incr) { | 117 void buf_incrlen(buffer* buf, unsigned int incr) { |
118 if (incr > BUF_MAX_INCR || buf->len + incr > buf->size) { | 118 if (incr > BUF_MAX_INCR || buf->len + incr > buf->size) { |
119 dropbear_exit("bad buf_incrlen"); | 119 dropbear_exit("Bad buf_incrlen"); |
120 } | 120 } |
121 buf->len += incr; | 121 buf->len += incr; |
122 } | 122 } |
123 /* Set the position of the buffer */ | 123 /* Set the position of the buffer */ |
124 void buf_setpos(buffer* buf, unsigned int pos) { | 124 void buf_setpos(buffer* buf, unsigned int pos) { |
125 | 125 |
126 if (pos > buf->len) { | 126 if (pos > buf->len) { |
127 dropbear_exit("bad buf_setpos"); | 127 dropbear_exit("Bad buf_setpos"); |
128 } | 128 } |
129 buf->pos = pos; | 129 buf->pos = pos; |
130 } | 130 } |
131 | 131 |
132 /* increment the postion by incr, increasing the buffer length if required */ | 132 /* increment the postion by incr, increasing the buffer length if required */ |
133 void buf_incrwritepos(buffer* buf, unsigned int incr) { | 133 void buf_incrwritepos(buffer* buf, unsigned int incr) { |
134 if (incr > BUF_MAX_INCR || buf->pos + incr > buf->size) { | 134 if (incr > BUF_MAX_INCR || buf->pos + incr > buf->size) { |
135 dropbear_exit("bad buf_incrwritepos"); | 135 dropbear_exit("Bad buf_incrwritepos"); |
136 } | 136 } |
137 buf->pos += incr; | 137 buf->pos += incr; |
138 if (buf->pos > buf->len) { | 138 if (buf->pos > buf->len) { |
139 buf->len = buf->pos; | 139 buf->len = buf->pos; |
140 } | 140 } |
144 * decrement the pos*/ | 144 * decrement the pos*/ |
145 void buf_incrpos(buffer* buf, int incr) { | 145 void buf_incrpos(buffer* buf, int incr) { |
146 if (incr > BUF_MAX_INCR || | 146 if (incr > BUF_MAX_INCR || |
147 (unsigned int)((int)buf->pos + incr) > buf->len | 147 (unsigned int)((int)buf->pos + incr) > buf->len |
148 || ((int)buf->pos + incr) < 0) { | 148 || ((int)buf->pos + incr) < 0) { |
149 dropbear_exit("bad buf_incrpos"); | 149 dropbear_exit("Bad buf_incrpos"); |
150 } | 150 } |
151 buf->pos += incr; | 151 buf->pos += incr; |
152 } | 152 } |
153 | 153 |
154 /* Get a byte from the buffer and increment the pos */ | 154 /* Get a byte from the buffer and increment the pos */ |
155 unsigned char buf_getbyte(buffer* buf) { | 155 unsigned char buf_getbyte(buffer* buf) { |
156 | 156 |
157 /* This check is really just ==, but the >= allows us to check for the | 157 /* This check is really just ==, but the >= allows us to check for the |
158 * bad case of pos > len, which should _never_ happen. */ | 158 * bad case of pos > len, which should _never_ happen. */ |
159 if (buf->pos >= buf->len) { | 159 if (buf->pos >= buf->len) { |
160 dropbear_exit("bad buf_getbyte"); | 160 dropbear_exit("Bad buf_getbyte"); |
161 } | 161 } |
162 return buf->data[buf->pos++]; | 162 return buf->data[buf->pos++]; |
163 } | 163 } |
164 | 164 |
165 /* Get a bool from the buffer and increment the pos */ | 165 /* Get a bool from the buffer and increment the pos */ |
185 /* returns an in-place pointer to the buffer, checking that | 185 /* returns an in-place pointer to the buffer, checking that |
186 * the next len bytes from that position can be used */ | 186 * the next len bytes from that position can be used */ |
187 unsigned char* buf_getptr(buffer* buf, unsigned int len) { | 187 unsigned char* buf_getptr(buffer* buf, unsigned int len) { |
188 | 188 |
189 if (buf->pos + len > buf->len) { | 189 if (buf->pos + len > buf->len) { |
190 dropbear_exit("bad buf_getptr"); | 190 dropbear_exit("Bad buf_getptr"); |
191 } | 191 } |
192 return &buf->data[buf->pos]; | 192 return &buf->data[buf->pos]; |
193 } | 193 } |
194 | 194 |
195 /* like buf_getptr, but checks against total size, not used length. | 195 /* like buf_getptr, but checks against total size, not used length. |
196 * This allows writing past the used length, but not past the size */ | 196 * This allows writing past the used length, but not past the size */ |
197 unsigned char* buf_getwriteptr(buffer* buf, unsigned int len) { | 197 unsigned char* buf_getwriteptr(buffer* buf, unsigned int len) { |
198 | 198 |
199 if (buf->pos + len > buf->size) { | 199 if (buf->pos + len > buf->size) { |
200 dropbear_exit("bad buf_getwriteptr"); | 200 dropbear_exit("Bad buf_getwriteptr"); |
201 } | 201 } |
202 return &buf->data[buf->pos]; | 202 return &buf->data[buf->pos]; |
203 } | 203 } |
204 | 204 |
205 /* Return a null-terminated string, it is malloced, so must be free()ed | 205 /* Return a null-terminated string, it is malloced, so must be free()ed |
209 | 209 |
210 unsigned int len; | 210 unsigned int len; |
211 unsigned char* ret; | 211 unsigned char* ret; |
212 len = buf_getint(buf); | 212 len = buf_getint(buf); |
213 if (len > MAX_STRING_LEN) { | 213 if (len > MAX_STRING_LEN) { |
214 dropbear_exit("string too long"); | 214 dropbear_exit("String too long"); |
215 } | 215 } |
216 | 216 |
217 if (retlen != NULL) { | 217 if (retlen != NULL) { |
218 *retlen = len; | 218 *retlen = len; |
219 } | 219 } |
220 ret = m_malloc(len+1); | 220 ret = m_malloc(len+1); |
221 memcpy(ret, buf_getptr(buf, len), len); | 221 memcpy(ret, buf_getptr(buf, len), len); |
222 buf_incrpos(buf, len); | 222 buf_incrpos(buf, len); |
223 ret[len] = '\0'; | 223 ret[len] = '\0'; |
224 | 224 |
225 return ret; | |
226 } | |
227 | |
228 /* Return a string as a newly allocated buffer */ | |
229 buffer * buf_getstringbuf(buffer *buf) { | |
230 buffer *ret; | |
231 unsigned char* str; | |
232 unsigned int len; | |
233 str = buf_getstring(buf, &len); | |
234 ret = m_malloc(sizeof(*ret)); | |
235 ret->data = str; | |
236 ret->len = len; | |
237 ret->size = len; | |
238 ret->pos = 0; | |
225 return ret; | 239 return ret; |
226 } | 240 } |
227 | 241 |
228 /* Just increment the buffer position the same as if we'd used buf_getstring, | 242 /* Just increment the buffer position the same as if we'd used buf_getstring, |
229 * but don't bother copying/malloc()ing for it */ | 243 * but don't bother copying/malloc()ing for it */ |