1 #define _POSIX_C_SOURCE 200809L
9 void tls_buffer_init(struct tls_buffer *buffer, size_t initial) {
16 buffer->buffer = malloc(initial);
18 memset(buffer->buffer, 0, initial);
19 buffer->size = initial;
21 buffer->error = errno;
26 void tls_buffer_compact(struct tls_buffer *b) {
29 if (b->error || b->size == b->len) {
34 new = realloc(b->buffer, b->len);
45 /* makes sure there's at least need bytes free */
46 void tls_buffer_expand(struct tls_buffer *buf, size_t need) {
53 want = buf->len + need;
55 if (want <= buf->size) {
60 char *new = realloc(buf->buffer, want);
64 memset(buf->buffer + buf->len, 0, buf->size - buf->len);
70 void tls_buffer_free(struct tls_buffer *buffer) {
71 /* TODO only zero out memory if it's "secure" buffer */
72 if (buffer->buffer && buffer->len) {
73 memset(buffer->buffer, 0, buffer->len);
76 tls_buffer_init(buffer, 0);
79 void tls_buffer_set(struct tls_buffer *buffer, int ch) {
80 /* TODO only zero out memory if it's "secure" buffer */
81 if (buffer->buffer && buffer->size) {
82 memset(buffer->buffer, ch, buffer->size);
86 void tls_buffer_append(struct tls_buffer *buffer, const unsigned char *bytes, size_t n) {
87 tls_buffer_expand(buffer, n);
89 if (buffer->error || !bytes) {
93 memcpy(buffer->buffer + buffer->len, bytes, n);
97 void tls_buffer_append16(struct tls_buffer *buffer, uint16_t n) {
98 tls_buffer_expand(buffer, 2);
104 buffer->buffer[buffer->len++] = (n >> 8) & 0xff;
105 buffer->buffer[buffer->len++] = n & 0xff;
108 void tls_buffer_append24(struct tls_buffer *buffer, uint32_t n) {
109 tls_buffer_expand(buffer, 3);
115 buffer->buffer[buffer->len++] = (n >> 16) & 0xff;
116 buffer->buffer[buffer->len++] = (n >> 8) & 0xff;
117 buffer->buffer[buffer->len++] = n & 0xff;
120 void tls_buffer_append_byte(struct tls_buffer *buffer, uint8_t n) {
121 tls_buffer_expand(buffer, 1);
127 buffer->buffer[buffer->len++] = n & 0xff;
130 void tls_buffer_write16(struct tls_buffer *buffer, uint16_t n, size_t at) {
131 if (at+2 > buffer->size) {
132 tls_buffer_expand(buffer, at+2 - buffer->size);
139 buffer->buffer[at] = (n >> 8) & 0xff;
140 buffer->buffer[at+1] = n & 0xff;
142 if (at+2 > buffer->len) {
143 buffer->len = at + 2;
147 uint16_t tls_buffer_read16(struct tls_buffer *buffer, size_t at) {
150 if (buffer->error || at+2 < buffer->len) {
154 res = (buffer->buffer[at] << 8) + buffer->buffer[at+1];
158 uint64_t tls_buffer_readbe(struct tls_buffer *buffer, int bytes, size_t at) {
162 if (buffer->error || at+bytes < buffer->len) {
166 for (i=0; i<bytes; i++) {
168 res += buffer->buffer[at+i];
174 void tls_buffer_writebe(struct tls_buffer *buffer, int bytes, size_t at, uint64_t val) {
176 if (at+bytes > buffer->size) {
177 tls_buffer_expand(buffer, at+bytes - buffer->size);
184 for (i=bytes-1; i>=0; i--) {
185 buffer->buffer[at+i] = val & 0xff;
193 void tls_buffer_shift(struct tls_buffer *buffer, size_t n) {
198 if (buffer->len < n) {
203 memmove(buffer->buffer, buffer->buffer + n, buffer->len - n);
204 memset(buffer->buffer + buffer->len - n, 0, n);
206 fprintf(stderr, "memmove(%p, %p, %zu)\n", buffer->buffer,
207 buffer->buffer+n, buffer->len - n);
208 fprintf(stderr, "memset(%p, %d, %zu)\n", buffer->buffer+n,
216 /* make room at the beginning */
217 void tls_buffer_unshift(struct tls_buffer *buffer, size_t n) {
218 tls_buffer_expand(buffer, n);
224 memmove(buffer->buffer + n, buffer->buffer, n);
225 memset(buffer->buffer, 0, n);
229 uint32_t tls_buffer_next3(struct tls_buffer_reader *rb) {
232 r = tls_buffer_readbe(rb, 3, rb->cursor);
237 uint16_t tls_buffer_next2(struct tls_buffer_reader *rb) {
240 r = tls_buffer_readbe(rb, 2, rb->cursor);
245 uint8_t tls_buffer_next(struct tls_buffer_reader *rb) {
248 r = tls_buffer_readbe(rb, 2, rb->cursor);
253 void tls_buffer_nextn(struct tls_buffer_reader *rb, unsigned char *b, size_t n) {
254 tls_buffer_read(rb, b, n, rb->cursor);