9
#define REFTABLE_ALLOW_BANNED_ALLOCATORS
10
#include "basics.h"
11
#include "reftable-basics.h"
12
+#include "reftable-error.h"
13
14
static void *(*reftable_malloc_ptr)(size_t sz);
15
static void *(*reftable_realloc_ptr)(void *, size_t);
70
reftable_free_ptr = free;
71
}
72
73
+void reftable_buf_init(struct reftable_buf *buf)
74
+{
75
+ struct reftable_buf empty = REFTABLE_BUF_INIT;
76
+ *buf = empty;
77
+}
78
+
79
+void reftable_buf_release(struct reftable_buf *buf)
80
+{
81
+ reftable_free(buf->buf);
82
+ reftable_buf_init(buf);
83
+}
84
+
85
+void reftable_buf_reset(struct reftable_buf *buf)
86
+{
87
+ if (buf->alloc) {
88
+ buf->len = 0;
89
+ buf->buf[0] = '\0';
90
+ }
91
+}
92
+
93
+int reftable_buf_setlen(struct reftable_buf *buf, size_t len)
94
+{
95
+ if (len > buf->len)
96
+ return -1;
97
+ if (len == buf->len)
98
+ return 0;
99
+ buf->buf[len] = '\0';
100
+ buf->len = len;
101
+ return 0;
102
+}
103
+
104
+int reftable_buf_cmp(const struct reftable_buf *a, const struct reftable_buf *b)
105
+{
106
+ size_t len = a->len < b->len ? a->len : b->len;
107
+ if (len) {
108
+ int cmp = memcmp(a->buf, b->buf, len);
109
+ if (cmp)
110
+ return cmp;
111
+ }
112
+ return a->len < b->len ? -1 : a->len != b->len;
113
+}
114
+
115
+int reftable_buf_add(struct reftable_buf *buf, const void *data, size_t len)
116
+{
117
+ size_t newlen = buf->len + len;
118
+
119
+ if (newlen + 1 > buf->alloc) {
120
+ char *reallocated = buf->buf;
121
+ REFTABLE_ALLOC_GROW(reallocated, newlen + 1, buf->alloc);
122
+ if (!reallocated)
123
+ return REFTABLE_OUT_OF_MEMORY_ERROR;
124
+ buf->buf = reallocated;
125
+ }
126
+
127
+ memcpy(buf->buf + buf->len, data, len);
128
+ buf->buf[newlen] = '\0';
129
+ buf->len = newlen;
130
+
131
+ return 0;
132
+}
133
+
134
+int reftable_buf_addstr(struct reftable_buf *buf, const char *s)
135
+{
136
+ return reftable_buf_add(buf, s, strlen(s));
137
+}
138
+
139
+char *reftable_buf_detach(struct reftable_buf *buf)
140
+{
141
+ char *result = buf->buf;
142
+ reftable_buf_init(buf);
143
+ return result;
144
+}
145
+
146
void put_be24(uint8_t *out, uint32_t i)
147
{
148
out[0] = (uint8_t)((i >> 16) & 0xff);