comparison buffer.c @ 1683:41bf8f216644

merge rsa-sha256
author Matt Johnston <matt@ucc.asn.au>
date Tue, 26 May 2020 00:24:02 +0800
parents d5cdc60db08e
children 1051e4eea25a
comparison
equal deleted inserted replaced
1673:e0871128e61f 1683:41bf8f216644
226 226
227 return ret; 227 return ret;
228 } 228 }
229 229
230 /* Return a string as a newly allocated buffer */ 230 /* Return a string as a newly allocated buffer */
231 buffer * buf_getstringbuf(buffer *buf) { 231 static buffer * buf_getstringbuf_int(buffer *buf, int incllen) {
232 buffer *ret = NULL; 232 buffer *ret = NULL;
233 unsigned int len = buf_getint(buf); 233 unsigned int len = buf_getint(buf);
234 int extra = 0;
234 if (len > MAX_STRING_LEN) { 235 if (len > MAX_STRING_LEN) {
235 dropbear_exit("String too long"); 236 dropbear_exit("String too long");
236 } 237 }
237 ret = buf_new(len); 238 if (incllen) {
239 extra = 4;
240 }
241 ret = buf_new(len+extra);
242 if (incllen) {
243 buf_putint(ret, len);
244 }
238 memcpy(buf_getwriteptr(ret, len), buf_getptr(buf, len), len); 245 memcpy(buf_getwriteptr(ret, len), buf_getptr(buf, len), len);
239 buf_incrpos(buf, len); 246 buf_incrpos(buf, len);
240 buf_incrlen(ret, len); 247 buf_incrlen(ret, len);
248 buf_setpos(ret, 0);
241 return ret; 249 return ret;
250 }
251
252 /* Return a string as a newly allocated buffer */
253 buffer * buf_getstringbuf(buffer *buf) {
254 return buf_getstringbuf_int(buf, 0);
255 }
256
257 /* Returns a string in a new buffer, including the length */
258 buffer * buf_getbuf(buffer *buf) {
259 return buf_getstringbuf_int(buf, 1);
242 } 260 }
243 261
244 /* Just increment the buffer position the same as if we'd used buf_getstring, 262 /* Just increment the buffer position the same as if we'd used buf_getstring,
245 * but don't bother copying/malloc()ing for it */ 263 * but don't bother copying/malloc()ing for it */
246 void buf_eatstring(buffer *buf) { 264 void buf_eatstring(buffer *buf) {