Lev Walkin | d922184 | 2017-08-22 01:44:56 -0700 | [diff] [blame] | 1 | #include <sys/types.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdarg.h> |
| 4 | #include <string.h> |
| 5 | #include <stdio.h> |
| 6 | #include <assert.h> |
| 7 | |
| 8 | #include "asn1_buffer.h" |
| 9 | |
| 10 | /* |
| 11 | * Create and destroy the buffer. |
| 12 | */ |
| 13 | abuf * |
| 14 | abuf_new() { |
| 15 | abuf *ab = calloc(1, sizeof(abuf)); |
| 16 | assert(ab); |
| 17 | ab->length = 0; |
| 18 | ab->size = 32; |
| 19 | ab->buffer = calloc(1, ab->size); |
| 20 | assert(ab->buffer); |
| 21 | return ab; |
| 22 | } |
| 23 | |
| 24 | void abuf_free(abuf *ab) { |
| 25 | if(ab) { |
| 26 | union { |
| 27 | const char *c_buf; |
| 28 | char *nc_buf; |
| 29 | } const_cast; |
| 30 | const_cast.c_buf = ab->buffer; |
| 31 | free(const_cast.nc_buf); |
| 32 | free(ab); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * Erase contents of the buffer (without destroying it). |
| 38 | */ |
| 39 | void |
| 40 | abuf_clear(abuf *ab) { |
| 41 | union { |
| 42 | const char *c_buf; |
| 43 | char *nc_buf; |
| 44 | } const_cast; |
| 45 | if(!ab->buffer) { |
| 46 | ab->size = 32; |
| 47 | ab->buffer = calloc(1, ab->size); |
| 48 | assert(ab->buffer); |
| 49 | } |
| 50 | const_cast.c_buf = ab->buffer; |
| 51 | ab->length = 0; |
| 52 | const_cast.nc_buf[0] = '\0'; |
| 53 | } |
| 54 | |
| 55 | static void |
Lev Walkin | e3204e7 | 2017-08-23 06:22:53 -0700 | [diff] [blame] | 56 | abuf_resize_by(abuf *ab, size_t size) { |
Lev Walkin | d922184 | 2017-08-22 01:44:56 -0700 | [diff] [blame] | 57 | union { |
| 58 | const char *c_buf; |
| 59 | char *nc_buf; |
| 60 | } const_cast; |
| 61 | const_cast.c_buf = ab->buffer; |
| 62 | |
Lev Walkin | e3204e7 | 2017-08-23 06:22:53 -0700 | [diff] [blame] | 63 | assert(!ab->buffer || ab->buffer[ab->length] == '\0'); |
Lev Walkin | d922184 | 2017-08-22 01:44:56 -0700 | [diff] [blame] | 64 | |
Lev Walkin | e3204e7 | 2017-08-23 06:22:53 -0700 | [diff] [blame] | 65 | size_t new_size = ab->length + size; |
Lev Walkin | d922184 | 2017-08-22 01:44:56 -0700 | [diff] [blame] | 66 | char *p = realloc(const_cast.nc_buf, new_size); |
| 67 | assert(p); |
Lev Walkin | e3204e7 | 2017-08-23 06:22:53 -0700 | [diff] [blame] | 68 | if(!ab->buffer) { |
| 69 | assert(ab->length == 0); |
| 70 | *p = '\0'; |
| 71 | } |
Lev Walkin | d922184 | 2017-08-22 01:44:56 -0700 | [diff] [blame] | 72 | ab->buffer = p; |
| 73 | assert(ab->buffer[ab->length] == '\0'); |
| 74 | ab->size = new_size; |
| 75 | } |
| 76 | |
Lev Walkin | e3204e7 | 2017-08-23 06:22:53 -0700 | [diff] [blame] | 77 | void abuf_add_bytes(abuf *ab, const char *str, size_t size) { |
| 78 | abuf_resize_by(ab, size + 1); |
| 79 | union { |
| 80 | const char *c_buf; |
| 81 | char *nc_buf; |
| 82 | } const_cast; |
| 83 | const_cast.c_buf = ab->buffer; |
| 84 | memcpy(&const_cast.nc_buf[ab->length], str, size); |
| 85 | ab->length += size; |
| 86 | const_cast.nc_buf[ab->length] = '\0'; |
| 87 | } |
| 88 | |
Lev Walkin | d922184 | 2017-08-22 01:44:56 -0700 | [diff] [blame] | 89 | void abuf_str(abuf *ab, const char *str) { |
Lev Walkin | e3204e7 | 2017-08-23 06:22:53 -0700 | [diff] [blame] | 90 | abuf_add_bytes(ab, str, strlen(str)); |
Lev Walkin | d922184 | 2017-08-22 01:44:56 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void abuf_buf(abuf *ab, const abuf *buf) { |
Lev Walkin | e3204e7 | 2017-08-23 06:22:53 -0700 | [diff] [blame] | 94 | abuf_add_bytes(ab, buf->buffer, buf->length); |
Lev Walkin | d922184 | 2017-08-22 01:44:56 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Lev Walkin | e3204e7 | 2017-08-23 06:22:53 -0700 | [diff] [blame] | 97 | int abuf_printf(abuf *ab, const char *fmt, ...) { |
Lev Walkin | d922184 | 2017-08-22 01:44:56 -0700 | [diff] [blame] | 98 | va_list ap; |
| 99 | |
| 100 | for(;;) { |
| 101 | union { |
| 102 | const char *c_buf; |
| 103 | char *nc_buf; |
| 104 | } const_cast; |
| 105 | const_cast.c_buf = ab->buffer; |
| 106 | va_start(ap, fmt); |
| 107 | int ret = vsnprintf(&const_cast.nc_buf[ab->length], |
| 108 | ab->size - ab->length, fmt, ap); |
| 109 | va_end(ap); |
| 110 | assert(ret >= 0); |
| 111 | if((size_t)ret < ab->size - ab->length) { |
| 112 | ab->length += ret; |
| 113 | assert(ab->buffer[ab->length] == '\0'); |
Lev Walkin | e3204e7 | 2017-08-23 06:22:53 -0700 | [diff] [blame] | 114 | return ret; |
Lev Walkin | d922184 | 2017-08-22 01:44:56 -0700 | [diff] [blame] | 115 | } |
| 116 | const_cast.nc_buf[ab->length] = '\0'; /* Restore order */ |
| 117 | abuf_resize_by(ab, ret + 1); |
| 118 | } |
| 119 | } |
| 120 | |
Lev Walkin | e3204e7 | 2017-08-23 06:22:53 -0700 | [diff] [blame] | 121 | int abuf_vprintf(abuf *ab, const char *fmt, va_list ap) { |
| 122 | int ret; |
| 123 | char *str = 0; |
| 124 | |
| 125 | ret = vasprintf(&str, fmt, ap); |
| 126 | assert(ret >= 0); |
| 127 | assert(str != NULL); |
| 128 | |
| 129 | abuf_add_bytes(ab, str, ret); |
| 130 | |
| 131 | free(str); |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |