10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi> 30Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 40Sstevel@tonic-gate * All rights reserved 50Sstevel@tonic-gate * Functions for manipulating fifo buffers (that can grow if needed). 60Sstevel@tonic-gate * 70Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software 80Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this 90Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is 100Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be 110Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell". 120Sstevel@tonic-gate */ 130Sstevel@tonic-gate 14*5087Sjp161948 /* $OpenBSD: buffer.c,v 1.31 2006/08/03 03:34:41 deraadt Exp $ */ 150Sstevel@tonic-gate 160Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 170Sstevel@tonic-gate 18*5087Sjp161948 #include "includes.h" 19*5087Sjp161948 200Sstevel@tonic-gate #include "xmalloc.h" 210Sstevel@tonic-gate #include "buffer.h" 220Sstevel@tonic-gate #include "log.h" 230Sstevel@tonic-gate 24*5087Sjp161948 #define BUFFER_MAX_CHUNK 0x100000 25*5087Sjp161948 #define BUFFER_MAX_LEN 0xa00000 26*5087Sjp161948 #define BUFFER_ALLOCSZ 0x008000 27*5087Sjp161948 280Sstevel@tonic-gate /* Initializes the buffer structure. */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate void 310Sstevel@tonic-gate buffer_init(Buffer *buffer) 320Sstevel@tonic-gate { 330Sstevel@tonic-gate const u_int len = 4096; 340Sstevel@tonic-gate 350Sstevel@tonic-gate buffer->alloc = 0; 360Sstevel@tonic-gate buffer->buf = xmalloc(len); 370Sstevel@tonic-gate buffer->alloc = len; 380Sstevel@tonic-gate buffer->offset = 0; 390Sstevel@tonic-gate buffer->end = 0; 400Sstevel@tonic-gate } 410Sstevel@tonic-gate 420Sstevel@tonic-gate /* Frees any memory used for the buffer. */ 430Sstevel@tonic-gate 440Sstevel@tonic-gate void 450Sstevel@tonic-gate buffer_free(Buffer *buffer) 460Sstevel@tonic-gate { 470Sstevel@tonic-gate if (buffer->alloc > 0) { 480Sstevel@tonic-gate memset(buffer->buf, 0, buffer->alloc); 492757Sjp161948 buffer->alloc = 0; 500Sstevel@tonic-gate xfree(buffer->buf); 510Sstevel@tonic-gate } 520Sstevel@tonic-gate } 530Sstevel@tonic-gate 540Sstevel@tonic-gate /* 550Sstevel@tonic-gate * Clears any data from the buffer, making it empty. This does not actually 560Sstevel@tonic-gate * zero the memory. 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate 590Sstevel@tonic-gate void 600Sstevel@tonic-gate buffer_clear(Buffer *buffer) 610Sstevel@tonic-gate { 620Sstevel@tonic-gate buffer->offset = 0; 630Sstevel@tonic-gate buffer->end = 0; 640Sstevel@tonic-gate } 650Sstevel@tonic-gate 660Sstevel@tonic-gate /* Appends data to the buffer, expanding it if necessary. */ 670Sstevel@tonic-gate 680Sstevel@tonic-gate void 690Sstevel@tonic-gate buffer_append(Buffer *buffer, const void *data, u_int len) 700Sstevel@tonic-gate { 710Sstevel@tonic-gate void *p; 720Sstevel@tonic-gate p = buffer_append_space(buffer, len); 730Sstevel@tonic-gate memcpy(p, data, len); 740Sstevel@tonic-gate } 750Sstevel@tonic-gate 76*5087Sjp161948 static int 77*5087Sjp161948 buffer_compact(Buffer *buffer) 78*5087Sjp161948 { 79*5087Sjp161948 /* 80*5087Sjp161948 * If the buffer is quite empty, but all data is at the end, move the 81*5087Sjp161948 * data to the beginning. 82*5087Sjp161948 */ 83*5087Sjp161948 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) { 84*5087Sjp161948 memmove(buffer->buf, buffer->buf + buffer->offset, 85*5087Sjp161948 buffer->end - buffer->offset); 86*5087Sjp161948 buffer->end -= buffer->offset; 87*5087Sjp161948 buffer->offset = 0; 88*5087Sjp161948 return (1); 89*5087Sjp161948 } 90*5087Sjp161948 return (0); 91*5087Sjp161948 } 92*5087Sjp161948 930Sstevel@tonic-gate /* 940Sstevel@tonic-gate * Appends space to the buffer, expanding the buffer if necessary. This does 950Sstevel@tonic-gate * not actually copy the data into the buffer, but instead returns a pointer 960Sstevel@tonic-gate * to the allocated region. 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate 990Sstevel@tonic-gate void * 1000Sstevel@tonic-gate buffer_append_space(Buffer *buffer, u_int len) 1010Sstevel@tonic-gate { 1020Sstevel@tonic-gate u_int newlen; 1030Sstevel@tonic-gate void *p; 1040Sstevel@tonic-gate 1052757Sjp161948 if (len > BUFFER_MAX_CHUNK) 1060Sstevel@tonic-gate fatal("buffer_append_space: len %u not supported", len); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate /* If the buffer is empty, start using it from the beginning. */ 1090Sstevel@tonic-gate if (buffer->offset == buffer->end) { 1100Sstevel@tonic-gate buffer->offset = 0; 1110Sstevel@tonic-gate buffer->end = 0; 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate restart: 1140Sstevel@tonic-gate /* If there is enough space to store all data, store it now. */ 1150Sstevel@tonic-gate if (buffer->end + len < buffer->alloc) { 1160Sstevel@tonic-gate p = buffer->buf + buffer->end; 1170Sstevel@tonic-gate buffer->end += len; 1180Sstevel@tonic-gate return p; 1190Sstevel@tonic-gate } 120*5087Sjp161948 121*5087Sjp161948 /* Compact data back to the start of the buffer if necessary */ 122*5087Sjp161948 if (buffer_compact(buffer)) 1230Sstevel@tonic-gate goto restart; 124*5087Sjp161948 1250Sstevel@tonic-gate /* Increase the size of the buffer and retry. */ 126*5087Sjp161948 newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ); 1272757Sjp161948 if (newlen > BUFFER_MAX_LEN) 1280Sstevel@tonic-gate fatal("buffer_append_space: alloc %u not supported", 1290Sstevel@tonic-gate newlen); 1300Sstevel@tonic-gate buffer->buf = xrealloc(buffer->buf, newlen); 1310Sstevel@tonic-gate buffer->alloc = newlen; 1320Sstevel@tonic-gate goto restart; 1330Sstevel@tonic-gate /* NOTREACHED */ 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate 136*5087Sjp161948 /* 137*5087Sjp161948 * Check whether an allocation of 'len' will fit in the buffer 138*5087Sjp161948 * This must follow the same math as buffer_append_space 139*5087Sjp161948 */ 140*5087Sjp161948 int 141*5087Sjp161948 buffer_check_alloc(Buffer *buffer, u_int len) 142*5087Sjp161948 { 143*5087Sjp161948 if (buffer->offset == buffer->end) { 144*5087Sjp161948 buffer->offset = 0; 145*5087Sjp161948 buffer->end = 0; 146*5087Sjp161948 } 147*5087Sjp161948 restart: 148*5087Sjp161948 if (buffer->end + len < buffer->alloc) 149*5087Sjp161948 return (1); 150*5087Sjp161948 if (buffer_compact(buffer)) 151*5087Sjp161948 goto restart; 152*5087Sjp161948 if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN) 153*5087Sjp161948 return (1); 154*5087Sjp161948 return (0); 155*5087Sjp161948 } 156*5087Sjp161948 1570Sstevel@tonic-gate /* Returns the number of bytes of data in the buffer. */ 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate u_int 1600Sstevel@tonic-gate buffer_len(Buffer *buffer) 1610Sstevel@tonic-gate { 1620Sstevel@tonic-gate return buffer->end - buffer->offset; 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /* Gets data from the beginning of the buffer. */ 1660Sstevel@tonic-gate 1672757Sjp161948 int 1682757Sjp161948 buffer_get_ret(Buffer *buffer, void *buf, u_int len) 1692757Sjp161948 { 1702757Sjp161948 if (len > buffer->end - buffer->offset) { 1712757Sjp161948 error("buffer_get_ret: trying to get more bytes %d than in buffer %d", 1722757Sjp161948 len, buffer->end - buffer->offset); 1732757Sjp161948 return (-1); 1742757Sjp161948 } 1752757Sjp161948 memcpy(buf, buffer->buf + buffer->offset, len); 1762757Sjp161948 buffer->offset += len; 1772757Sjp161948 return (0); 1782757Sjp161948 } 1792757Sjp161948 1800Sstevel@tonic-gate void 1810Sstevel@tonic-gate buffer_get(Buffer *buffer, void *buf, u_int len) 1820Sstevel@tonic-gate { 1832757Sjp161948 if (buffer_get_ret(buffer, buf, len) == -1) 1842757Sjp161948 fatal("buffer_get: buffer error"); 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate /* Consumes the given number of bytes from the beginning of the buffer. */ 1880Sstevel@tonic-gate 1892757Sjp161948 int 1902757Sjp161948 buffer_consume_ret(Buffer *buffer, u_int bytes) 1912757Sjp161948 { 1922757Sjp161948 if (bytes > buffer->end - buffer->offset) { 1932757Sjp161948 error("buffer_consume_ret: trying to get more bytes than in buffer"); 1942757Sjp161948 return (-1); 1952757Sjp161948 } 1962757Sjp161948 buffer->offset += bytes; 1972757Sjp161948 return (0); 1982757Sjp161948 } 1992757Sjp161948 2000Sstevel@tonic-gate void 2010Sstevel@tonic-gate buffer_consume(Buffer *buffer, u_int bytes) 2020Sstevel@tonic-gate { 2032757Sjp161948 if (buffer_consume_ret(buffer, bytes) == -1) 2042757Sjp161948 fatal("buffer_consume: buffer error"); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* Consumes the given number of bytes from the end of the buffer. */ 2080Sstevel@tonic-gate 2092757Sjp161948 int 2102757Sjp161948 buffer_consume_end_ret(Buffer *buffer, u_int bytes) 2112757Sjp161948 { 2122757Sjp161948 if (bytes > buffer->end - buffer->offset) 2132757Sjp161948 return (-1); 2142757Sjp161948 buffer->end -= bytes; 2152757Sjp161948 return (0); 2162757Sjp161948 } 2172757Sjp161948 2180Sstevel@tonic-gate void 2190Sstevel@tonic-gate buffer_consume_end(Buffer *buffer, u_int bytes) 2200Sstevel@tonic-gate { 2212757Sjp161948 if (buffer_consume_end_ret(buffer, bytes) == -1) 2220Sstevel@tonic-gate fatal("buffer_consume_end: trying to get more bytes than in buffer"); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* Returns a pointer to the first used byte in the buffer. */ 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate void * 2280Sstevel@tonic-gate buffer_ptr(Buffer *buffer) 2290Sstevel@tonic-gate { 2300Sstevel@tonic-gate return buffer->buf + buffer->offset; 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate /* Dumps the contents of the buffer to stderr. */ 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate void 2360Sstevel@tonic-gate buffer_dump(Buffer *buffer) 2370Sstevel@tonic-gate { 2382757Sjp161948 u_int i; 2390Sstevel@tonic-gate u_char *ucp = buffer->buf; 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate for (i = buffer->offset; i < buffer->end; i++) { 2420Sstevel@tonic-gate fprintf(stderr, "%02x", ucp[i]); 2430Sstevel@tonic-gate if ((i-buffer->offset)%16==15) 2440Sstevel@tonic-gate fprintf(stderr, "\r\n"); 2450Sstevel@tonic-gate else if ((i-buffer->offset)%2==1) 2460Sstevel@tonic-gate fprintf(stderr, " "); 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate fprintf(stderr, "\r\n"); 2490Sstevel@tonic-gate } 250