| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <proton/error.h> |
| 23 |
#include <proton/object.h> |
| 24 |
|
| 25 |
#ifndef __cplusplus |
| 26 |
#include <stdbool.h> |
| 27 |
#endif |
| 28 |
#include <stddef.h> |
| 29 |
#include <string.h> |
| 30 |
#include <stdio.h> |
| 31 |
|
| 32 |
#include "buffer.h" |
| 33 |
#include "memory.h" |
| 34 |
#include "util.h" |
| 35 |
|
| 36 |
struct pn_buffer_t { |
| 37 |
size_t capacity; |
| 38 |
size_t start; |
| 39 |
size_t size; |
| 40 |
char *bytes; |
| 41 |
}; |
| 42 |
|
| 43 |
PN_STRUCT_CLASSDEF(pn_buffer) |
| 44 |
|
| 45 |
pn_buffer_t *pn_buffer(size_t capacity) |
| 46 |
{ |
| 47 |
pn_buffer_t *buf = (pn_buffer_t *) pni_mem_allocate(PN_CLASSCLASS(pn_buffer), sizeof(pn_buffer_t)); |
| 48 |
if (buf != NULL) { |
| 49 |
buf->capacity = capacity; |
| 50 |
buf->start = 0; |
| 51 |
buf->size = 0; |
| 52 |
if (capacity > 0) { |
| 53 |
buf->bytes = (char *) pni_mem_suballocate(PN_CLASSCLASS(pn_buffer), buf, capacity); |
| 54 |
if (buf->bytes == NULL) { |
| 55 |
pni_mem_deallocate(PN_CLASSCLASS(pn_buffer), buf); |
| 56 |
buf = NULL; |
| 57 |
} |
| 58 |
} |
| 59 |
else { |
| 60 |
buf->bytes = NULL; |
| 61 |
} |
| 62 |
} |
| 63 |
return buf; |
| 64 |
} |
| 65 |
|
| 66 |
void pn_buffer_free(pn_buffer_t *buf) |
| 67 |
{ |
| 68 |
if (buf) { |
| 69 |
pni_mem_subdeallocate(PN_CLASSCLASS(pn_buffer), buf, buf->bytes); |
| 70 |
pni_mem_deallocate(PN_CLASSCLASS(pn_buffer), buf); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
size_t pn_buffer_size(pn_buffer_t *buf) |
| 75 |
{ |
| 76 |
return buf->size; |
| 77 |
} |
| 78 |
|
| 79 |
size_t pn_buffer_capacity(pn_buffer_t *buf) |
| 80 |
{ |
| 81 |
return buf->capacity; |
| 82 |
} |
| 83 |
|
| 84 |
size_t pn_buffer_available(pn_buffer_t *buf) |
| 85 |
{ |
| 86 |
return buf->capacity - buf->size; |
| 87 |
} |
| 88 |
|
| 89 |
static size_t pni_buffer_head(pn_buffer_t *buf) |
| 90 |
{ |
| 91 |
return buf->start; |
| 92 |
} |
| 93 |
|
| 94 |
static size_t pni_buffer_tail(pn_buffer_t *buf) |
| 95 |
{ |
| 96 |
size_t tail = buf->start + buf->size; |
| 97 |
if (tail >= buf->capacity) |
| 98 |
tail -= buf->capacity; |
| 99 |
return tail; |
| 100 |
} |
| 101 |
|
| 102 |
static bool pni_buffer_wrapped(pn_buffer_t *buf) |
| 103 |
{ |
| 104 |
return buf->size && pni_buffer_head(buf) >= pni_buffer_tail(buf); |
| 105 |
} |
| 106 |
|
| 107 |
static size_t pni_buffer_tail_space(pn_buffer_t *buf) |
| 108 |
{ |
| 109 |
if (pni_buffer_wrapped(buf)) { |
| 110 |
return pn_buffer_available(buf); |
| 111 |
} else { |
| 112 |
return buf->capacity - pni_buffer_tail(buf); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
static size_t pni_buffer_head_space(pn_buffer_t *buf) |
| 117 |
{ |
| 118 |
if (pni_buffer_wrapped(buf)) { |
| 119 |
return pn_buffer_available(buf); |
| 120 |
} else { |
| 121 |
return pni_buffer_head(buf); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
static size_t pni_buffer_head_size(pn_buffer_t *buf) |
| 126 |
{ |
| 127 |
if (pni_buffer_wrapped(buf)) { |
| 128 |
return buf->capacity - pni_buffer_head(buf); |
| 129 |
} else { |
| 130 |
return pni_buffer_tail(buf) - pni_buffer_head(buf); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
static size_t pni_buffer_tail_size(pn_buffer_t *buf) |
| 135 |
{ |
| 136 |
if (pni_buffer_wrapped(buf)) { |
| 137 |
return pni_buffer_tail(buf); |
| 138 |
} else { |
| 139 |
return 0; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
int pn_buffer_ensure(pn_buffer_t *buf, size_t size) |
| 144 |
{ |
| 145 |
size_t old_capacity = buf->capacity; |
| 146 |
size_t old_head = pni_buffer_head(buf); |
| 147 |
bool wrapped = pni_buffer_wrapped(buf); |
| 148 |
|
| 149 |
while (pn_buffer_available(buf) < size) { |
| 150 |
buf->capacity = 2*(buf->capacity ? buf->capacity : 16); |
| 151 |
} |
| 152 |
|
| 153 |
if (buf->capacity != old_capacity) { |
| 154 |
char* new_bytes = (char *) pni_mem_subreallocate(PN_CLASSCLASS(pn_buffer), buf, buf->bytes, buf->capacity); |
| 155 |
if (new_bytes) { |
| 156 |
buf->bytes = new_bytes; |
| 157 |
|
| 158 |
if (wrapped) { |
| 159 |
size_t n = old_capacity - old_head; |
| 160 |
memmove(buf->bytes + buf->capacity - n, buf->bytes + old_head, n); |
| 161 |
buf->start = buf->capacity - n; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
int pn_buffer_append(pn_buffer_t *buf, const char *bytes, size_t size) |
| 170 |
{ |
| 171 |
if (!size) return 0; |
| 172 |
int err = pn_buffer_ensure(buf, size); |
| 173 |
if (err) return err; |
| 174 |
|
| 175 |
size_t tail = pni_buffer_tail(buf); |
| 176 |
size_t tail_space = pni_buffer_tail_space(buf); |
| 177 |
size_t n = pn_min(tail_space, size); |
| 178 |
|
| 179 |
// Heuristic check for a strange usage in messenger |
| 180 |
if (bytes!=buf->bytes+tail) { |
| 181 |
memcpy(buf->bytes + tail, bytes, n); |
| 182 |
memcpy(buf->bytes, bytes + n, size - n); |
| 183 |
} |
| 184 |
buf->size += size; |
| 185 |
|
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
int pn_buffer_prepend(pn_buffer_t *buf, const char *bytes, size_t size) |
| 190 |
{ |
| 191 |
int err = pn_buffer_ensure(buf, size); |
| 192 |
if (err) return err; |
| 193 |
|
| 194 |
size_t head = pni_buffer_head(buf); |
| 195 |
size_t head_space = pni_buffer_head_space(buf); |
| 196 |
size_t n = pn_min(head_space, size); |
| 197 |
|
| 198 |
memcpy(buf->bytes + head - n, bytes + size - n, n); |
| 199 |
memcpy(buf->bytes + buf->capacity - (size - n), bytes, size - n); |
| 200 |
|
| 201 |
if (buf->start >= size) { |
| 202 |
buf->start -= size; |
| 203 |
} else { |
| 204 |
buf->start = buf->capacity - (size - buf->start); |
| 205 |
} |
| 206 |
|
| 207 |
buf->size += size; |
| 208 |
|
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
static size_t pni_buffer_index(pn_buffer_t *buf, size_t index) |
| 213 |
{ |
| 214 |
size_t result = buf->start + index; |
| 215 |
if (result >= buf->capacity) result -= buf->capacity; |
| 216 |
return result; |
| 217 |
} |
| 218 |
|
| 219 |
size_t pn_buffer_get(pn_buffer_t *buf, size_t offset, size_t size, char *dst) |
| 220 |
{ |
| 221 |
size = pn_min(size, buf->size); |
| 222 |
size_t start = pni_buffer_index(buf, offset); |
| 223 |
size_t stop = pni_buffer_index(buf, offset + size); |
| 224 |
|
| 225 |
if (size == 0) return 0; |
| 226 |
|
| 227 |
size_t sz1; |
| 228 |
size_t sz2; |
| 229 |
|
| 230 |
if (start >= stop) { |
| 231 |
sz1 = buf->capacity - start; |
| 232 |
sz2 = stop; |
| 233 |
} else { |
| 234 |
sz1 = stop - start; |
| 235 |
sz2 = 0; |
| 236 |
} |
| 237 |
|
| 238 |
memcpy(dst, buf->bytes + start, sz1); |
| 239 |
memcpy(dst + sz1, buf->bytes, sz2); |
| 240 |
|
| 241 |
return sz1 + sz2; |
| 242 |
} |
| 243 |
|
| 244 |
int pn_buffer_trim(pn_buffer_t *buf, size_t left, size_t right) |
| 245 |
{ |
| 246 |
if (left + right > buf->size) return PN_ARG_ERR; |
| 247 |
|
| 248 |
// In special case where we trim everything just clear buffer |
| 249 |
if (left + right == buf->size) { |
| 250 |
pn_buffer_clear(buf); |
| 251 |
return 0; |
| 252 |
} |
| 253 |
buf->start += left; |
| 254 |
if (buf->start >= buf->capacity) |
| 255 |
buf->start -= buf->capacity; |
| 256 |
|
| 257 |
buf->size -= left + right; |
| 258 |
|
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
void pn_buffer_clear(pn_buffer_t *buf) |
| 263 |
{ |
| 264 |
buf->start = 0; |
| 265 |
buf->size = 0; |
| 266 |
} |
| 267 |
|
| 268 |
static void pn_buffer_rotate (pn_buffer_t *buf, size_t sz) { |
| 269 |
if (sz == 0) return; |
| 270 |
|
| 271 |
unsigned c = 0, v = 0; |
| 272 |
for (; c < buf->capacity; v++) { |
| 273 |
unsigned t = v, tp = v + sz; |
| 274 |
char tmp = buf->bytes[v]; |
| 275 |
c++; |
| 276 |
while (tp != v) { |
| 277 |
buf->bytes[t] = buf->bytes[tp]; |
| 278 |
t = tp; |
| 279 |
tp += sz; |
| 280 |
if (tp >= buf->capacity) tp -= buf->capacity; |
| 281 |
c++; |
| 282 |
} |
| 283 |
buf->bytes[t] = tmp; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
int pn_buffer_defrag(pn_buffer_t *buf) |
| 288 |
{ |
| 289 |
pn_buffer_rotate(buf, buf->start); |
| 290 |
buf->start = 0; |
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
pn_bytes_t pn_buffer_bytes(pn_buffer_t *buf) |
| 295 |
{ |
| 296 |
if (!buf) { |
| 297 |
return (pn_bytes_t){0, NULL}; |
| 298 |
} |
| 299 |
pn_buffer_defrag(buf); |
| 300 |
return (pn_bytes_t){.size=buf->size, .start=buf->bytes}; |
| 301 |
} |
| 302 |
|
| 303 |
pn_rwbytes_t pn_buffer_memory(pn_buffer_t *buf) |
| 304 |
{ |
| 305 |
if (!buf) { |
| 306 |
return (pn_rwbytes_t){0, NULL}; |
| 307 |
} |
| 308 |
pn_buffer_defrag(buf); |
| 309 |
return (pn_rwbytes_t){.size=buf->size, .start=buf->bytes}; |
| 310 |
} |
| 311 |
|
| 312 |
pn_rwbytes_t pn_buffer_free_memory(pn_buffer_t *buf) |
| 313 |
{ |
| 314 |
if (!buf) { |
| 315 |
return (pn_rwbytes_t){0, NULL}; |
| 316 |
} |
| 317 |
size_t free_size = buf->capacity-buf->size; |
| 318 |
if (buf->start == 0) { |
| 319 |
return (pn_rwbytes_t){.size=free_size, .start=buf->bytes}; |
| 320 |
} |
| 321 |
// If free memory in one single blob don't need to defragment |
| 322 |
if (buf->start+buf->size > buf->capacity) { |
| 323 |
return (pn_rwbytes_t){.size=free_size, .start=buf->bytes+buf->start-free_size}; |
| 324 |
} |
| 325 |
pn_buffer_defrag(buf); |
| 326 |
return (pn_rwbytes_t){.size=free_size, .start=buf->bytes+buf->start+buf->size}; |
| 327 |
} |
| 328 |
|
| 329 |
int pn_buffer_quote(pn_buffer_t *buf, pn_string_t *str, size_t n) |
| 330 |
{ |
| 331 |
size_t hsize = pni_buffer_head_size(buf); |
| 332 |
size_t tsize = pni_buffer_tail_size(buf); |
| 333 |
if (hsize >= n) { |
| 334 |
pn_quote(str, buf->bytes + pni_buffer_head(buf), n); |
| 335 |
return 0; |
| 336 |
} |
| 337 |
pn_quote(str, buf->bytes + pni_buffer_head(buf), hsize); |
| 338 |
pn_quote(str, buf->bytes, pn_min(tsize, n-hsize)); |
| 339 |
return 0; |
| 340 |
} |