| | varnish-cache/lib/libvarnish/vsb.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2000-2008 Poul-Henning Kamp <phk@FreeBSD.org> |
| 2 |
|
* Copyright (c) 2000-2008 Dag-Erling Coïdan Smørgrav |
| 3 |
|
* All rights reserved. |
| 4 |
|
* |
| 5 |
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD |
| 6 |
|
* |
| 7 |
|
* Redistribution and use in source and binary forms, with or without |
| 8 |
|
* modification, are permitted provided that the following conditions |
| 9 |
|
* are met: |
| 10 |
|
* 1. Redistributions of source code must retain the above copyright |
| 11 |
|
* notice, this list of conditions and the following disclaimer |
| 12 |
|
* in this position and unchanged. |
| 13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
|
* notice, this list of conditions and the following disclaimer in the |
| 15 |
|
* documentation and/or other materials provided with the distribution. |
| 16 |
|
* |
| 17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
|
* SUCH DAMAGE. |
| 28 |
|
__FBSDID("$FreeBSD: head/sys/kern/subr_vsb.c 222004 2011-05-17 06:36:32Z phk $") |
| 29 |
|
*/ |
| 30 |
|
|
| 31 |
|
#include "config.h" |
| 32 |
|
|
| 33 |
|
#include <ctype.h> |
| 34 |
|
#include <stdarg.h> |
| 35 |
|
#include <stdio.h> |
| 36 |
|
#include <stdint.h> |
| 37 |
|
#include <stdlib.h> |
| 38 |
|
#include <string.h> |
| 39 |
|
#include <unistd.h> |
| 40 |
|
|
| 41 |
|
#include "vdef.h" |
| 42 |
|
#include "vas.h" // XXX Flexelint "not used" - but req'ed for assert() |
| 43 |
|
#include "vsb.h" |
| 44 |
|
|
| 45 |
|
#define KASSERT(e, m) assert(e) |
| 46 |
|
#define SBMALLOC(size) malloc(size) |
| 47 |
|
#define SBFREE(buf) free(buf) |
| 48 |
|
|
| 49 |
|
#define rndup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */ |
| 50 |
|
|
| 51 |
|
/* |
| 52 |
|
* Predicates |
| 53 |
|
*/ |
| 54 |
|
#define VSB_ISDYNAMIC(s) ((s)->s_flags & VSB_DYNAMIC) |
| 55 |
|
#define VSB_ISDYNSTRUCT(s) ((s)->s_flags & VSB_DYNSTRUCT) |
| 56 |
|
#define VSB_HASROOM(s) ((s)->s_len < (s)->s_size - 1L) |
| 57 |
|
#define VSB_FREESPACE(s) ((s)->s_size - ((s)->s_len + 1L)) |
| 58 |
|
#define VSB_CANEXTEND(s) ((s)->s_flags & VSB_AUTOEXTEND) |
| 59 |
|
|
| 60 |
|
/* |
| 61 |
|
* Set / clear flags |
| 62 |
|
*/ |
| 63 |
|
#define VSB_SETFLAG(s, f) do { (s)->s_flags |= (f); } while (0) |
| 64 |
|
#define VSB_CLEARFLAG(s, f) do { (s)->s_flags &= ~(f); } while (0) |
| 65 |
|
|
| 66 |
|
#define VSB_MINEXTENDSIZE 16 /* Should be power of 2. */ |
| 67 |
|
|
| 68 |
|
#ifdef PAGE_SIZE |
| 69 |
|
#define VSB_MAXEXTENDSIZE PAGE_SIZE |
| 70 |
|
#define VSB_MAXEXTENDINCR PAGE_SIZE |
| 71 |
|
#else |
| 72 |
|
#define VSB_MAXEXTENDSIZE 4096 |
| 73 |
|
#define VSB_MAXEXTENDINCR 4096 |
| 74 |
|
#endif |
| 75 |
|
|
| 76 |
|
/* |
| 77 |
|
* Debugging support |
| 78 |
|
*/ |
| 79 |
|
#if !defined(NDEBUG) |
| 80 |
|
static void |
| 81 |
74374598 |
_assert_VSB_integrity(const char *fun, const struct vsb *s) |
| 82 |
|
{ |
| 83 |
|
|
| 84 |
74374598 |
(void)fun; |
| 85 |
74374598 |
(void)s; |
| 86 |
74374598 |
KASSERT(s != NULL, |
| 87 |
|
("%s called with a NULL vsb pointer", fun)); |
| 88 |
74374598 |
KASSERT(s->magic == VSB_MAGIC, |
| 89 |
|
("%s called with a bogus vsb pointer", fun)); |
| 90 |
74374598 |
KASSERT(s->s_buf != NULL, |
| 91 |
|
("%s called with uninitialized or corrupt vsb", fun)); |
| 92 |
74374598 |
KASSERT(s->s_len < s->s_size, |
| 93 |
|
("wrote past end of vsb (%d >= %d)", s->s_len, s->s_size)); |
| 94 |
74374598 |
} |
| 95 |
|
|
| 96 |
|
static void |
| 97 |
69831316 |
_assert_VSB_state(const char *fun, const struct vsb *s, int state) |
| 98 |
|
{ |
| 99 |
|
|
| 100 |
69831316 |
(void)fun; |
| 101 |
69831316 |
(void)s; |
| 102 |
69831316 |
(void)state; |
| 103 |
69831316 |
KASSERT((s->s_flags & VSB_FINISHED) == state, |
| 104 |
|
("%s called with %sfinished or corrupt vsb", fun, |
| 105 |
|
(state ? "un" : ""))); |
| 106 |
69831316 |
} |
| 107 |
|
#define assert_VSB_integrity(s) _assert_VSB_integrity(__func__, (s)) |
| 108 |
|
#define assert_VSB_state(s, i) _assert_VSB_state(__func__, (s), (i)) |
| 109 |
|
#else |
| 110 |
|
#define assert_VSB_integrity(s) do { } while (0) |
| 111 |
|
#define assert_VSB_state(s, i) do { } while (0) |
| 112 |
|
#endif |
| 113 |
|
|
| 114 |
|
#ifdef CTASSERT |
| 115 |
|
CTASSERT(powerof2(VSB_MAXEXTENDSIZE)); |
| 116 |
|
CTASSERT(powerof2(VSB_MAXEXTENDINCR)); |
| 117 |
|
#endif |
| 118 |
|
|
| 119 |
|
static ssize_t |
| 120 |
3364181 |
VSB_extendsize(ssize_t size) |
| 121 |
|
{ |
| 122 |
|
ssize_t newsize; |
| 123 |
|
|
| 124 |
3364181 |
if (size < (int)VSB_MAXEXTENDSIZE) { |
| 125 |
3326708 |
newsize = VSB_MINEXTENDSIZE; |
| 126 |
7958917 |
while (newsize < size) |
| 127 |
4632209 |
newsize *= 2; |
| 128 |
3326708 |
} else { |
| 129 |
37473 |
newsize = rndup2(size, VSB_MAXEXTENDINCR); |
| 130 |
|
} |
| 131 |
3364181 |
KASSERT(newsize >= size, ("%s: %d < %d\n", __func__, newsize, size)); |
| 132 |
3364181 |
return (newsize); |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
/* |
| 136 |
|
* Extend an vsb. |
| 137 |
|
*/ |
| 138 |
|
static ssize_t |
| 139 |
1929015 |
VSB_extend(struct vsb *s, ssize_t addlen) |
| 140 |
|
{ |
| 141 |
|
char *newbuf; |
| 142 |
|
ssize_t newsize; |
| 143 |
|
|
| 144 |
1929015 |
if (!VSB_CANEXTEND(s)) |
| 145 |
33 |
return (-1); |
| 146 |
1928982 |
newsize = VSB_extendsize(s->s_size + addlen); |
| 147 |
1928982 |
if (VSB_ISDYNAMIC(s)) |
| 148 |
1928982 |
newbuf = realloc(s->s_buf, newsize); |
| 149 |
|
else |
| 150 |
0 |
newbuf = SBMALLOC(newsize); |
| 151 |
1928982 |
if (newbuf == NULL) |
| 152 |
0 |
return (-1); |
| 153 |
1928982 |
if (!VSB_ISDYNAMIC(s)) { |
| 154 |
0 |
memcpy(newbuf, s->s_buf, s->s_size); |
| 155 |
0 |
VSB_SETFLAG(s, VSB_DYNAMIC); |
| 156 |
0 |
} |
| 157 |
1928982 |
s->s_buf = newbuf; |
| 158 |
1928982 |
s->s_size = newsize; |
| 159 |
1928982 |
return (0); |
| 160 |
1929015 |
} |
| 161 |
|
|
| 162 |
|
static void |
| 163 |
61277750 |
_vsb_indent(struct vsb *s) |
| 164 |
|
{ |
| 165 |
61379067 |
if (s->s_indent == 0 || s->s_error != 0 || |
| 166 |
101317 |
(s->s_len > 0 && s->s_buf[s->s_len - 1] != '\n')) |
| 167 |
61270685 |
return; |
| 168 |
7065 |
if (VSB_FREESPACE(s) <= s->s_indent && |
| 169 |
9 |
VSB_extend(s, s->s_indent) < 0) { |
| 170 |
0 |
s->s_error = ENOMEM; |
| 171 |
0 |
return; |
| 172 |
|
} |
| 173 |
7065 |
memset(s->s_buf + s->s_len, ' ', s->s_indent); |
| 174 |
7065 |
s->s_len += s->s_indent; |
| 175 |
61277750 |
} |
| 176 |
|
|
| 177 |
|
/* |
| 178 |
|
* Initialize the internals of an vsb. |
| 179 |
|
* If buf is non-NULL, it points to a static or already-allocated string |
| 180 |
|
* big enough to hold at least length characters. |
| 181 |
|
*/ |
| 182 |
|
static struct vsb * |
| 183 |
1466660 |
VSB_newbuf(struct vsb *s, char *buf, int length, int flags) |
| 184 |
|
{ |
| 185 |
|
|
| 186 |
1466660 |
memset(s, 0, sizeof(*s)); |
| 187 |
1466660 |
s->magic = VSB_MAGIC; |
| 188 |
1466660 |
s->s_flags = flags; |
| 189 |
1466660 |
s->s_size = length; |
| 190 |
1466660 |
s->s_buf = buf; |
| 191 |
|
|
| 192 |
1466660 |
if ((s->s_flags & VSB_AUTOEXTEND) == 0) { |
| 193 |
31460 |
KASSERT(s->s_size > 1, |
| 194 |
|
("attempt to create a too small vsb")); |
| 195 |
31460 |
} |
| 196 |
|
|
| 197 |
1466660 |
if (s->s_buf != NULL) |
| 198 |
31461 |
return (s); |
| 199 |
|
|
| 200 |
1435199 |
if ((flags & VSB_AUTOEXTEND) != 0) |
| 201 |
1435199 |
s->s_size = VSB_extendsize(s->s_size); |
| 202 |
|
|
| 203 |
1435199 |
s->s_buf = SBMALLOC(s->s_size); |
| 204 |
1435199 |
if (s->s_buf == NULL) |
| 205 |
0 |
return (NULL); |
| 206 |
1435199 |
VSB_SETFLAG(s, VSB_DYNAMIC); |
| 207 |
1435199 |
return (s); |
| 208 |
1466660 |
} |
| 209 |
|
|
| 210 |
|
struct vsb * |
| 211 |
31459 |
VSB_init(struct vsb *s, void *buf, ssize_t length) |
| 212 |
|
{ |
| 213 |
31459 |
AN(s); |
| 214 |
31459 |
AN(buf); |
| 215 |
|
|
| 216 |
31459 |
KASSERT(length >= 0, |
| 217 |
|
("attempt to create an vsb of negative length (%zd)", length)); |
| 218 |
31459 |
return (VSB_newbuf(s, buf, length, VSB_FIXEDLEN)); |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
/* |
| 222 |
|
* Allocate a dynamic vsb |
| 223 |
|
*/ |
| 224 |
|
struct vsb * |
| 225 |
1435199 |
VSB_new_auto(void) |
| 226 |
|
{ |
| 227 |
|
struct vsb *s; |
| 228 |
|
|
| 229 |
1435199 |
s = SBMALLOC(sizeof(*s)); |
| 230 |
1435199 |
if (s == NULL) |
| 231 |
0 |
return (NULL); |
| 232 |
1435199 |
if (VSB_newbuf(s, NULL, 0, VSB_AUTOEXTEND) == NULL) { |
| 233 |
0 |
SBFREE(s); |
| 234 |
0 |
return (NULL); |
| 235 |
|
} |
| 236 |
1435199 |
VSB_SETFLAG(s, VSB_DYNSTRUCT); |
| 237 |
1435199 |
return (s); |
| 238 |
1435199 |
} |
| 239 |
|
|
| 240 |
|
/* |
| 241 |
|
* Clear an vsb and reset its position. |
| 242 |
|
*/ |
| 243 |
|
void |
| 244 |
2218588 |
VSB_clear(struct vsb *s) |
| 245 |
|
{ |
| 246 |
|
|
| 247 |
2218588 |
assert_VSB_integrity(s); |
| 248 |
|
/* don't care if it's finished or not */ |
| 249 |
|
|
| 250 |
2218588 |
VSB_CLEARFLAG(s, VSB_FINISHED); |
| 251 |
2218588 |
s->s_error = 0; |
| 252 |
2218588 |
s->s_len = 0; |
| 253 |
2218588 |
s->s_indent = 0; |
| 254 |
2218588 |
} |
| 255 |
|
|
| 256 |
|
/* |
| 257 |
|
* Append a byte to an vsb. This is the core function for appending |
| 258 |
|
* to an vsb and is the main place that deals with extending the |
| 259 |
|
* buffer and marking overflow. |
| 260 |
|
*/ |
| 261 |
|
static void |
| 262 |
46985468 |
VSB_put_byte(struct vsb *s, int c) |
| 263 |
|
{ |
| 264 |
|
|
| 265 |
46985468 |
assert_VSB_integrity(s); |
| 266 |
46985468 |
assert_VSB_state(s, 0); |
| 267 |
|
|
| 268 |
46985468 |
if (s->s_error != 0) |
| 269 |
0 |
return; |
| 270 |
46985468 |
_vsb_indent(s); |
| 271 |
46985468 |
if (VSB_FREESPACE(s) <= 0) { |
| 272 |
443508 |
if (VSB_extend(s, 1) < 0) |
| 273 |
0 |
s->s_error = ENOMEM; |
| 274 |
443508 |
if (s->s_error != 0) |
| 275 |
0 |
return; |
| 276 |
443508 |
} |
| 277 |
46985468 |
s->s_buf[s->s_len++] = (char)c; |
| 278 |
46985468 |
} |
| 279 |
|
|
| 280 |
|
/* |
| 281 |
|
* Append a byte string to an vsb. |
| 282 |
|
*/ |
| 283 |
|
int |
| 284 |
2861836 |
VSB_bcat(struct vsb *s, const void *buf, ssize_t len) |
| 285 |
|
{ |
| 286 |
2861836 |
assert_VSB_integrity(s); |
| 287 |
2861836 |
assert_VSB_state(s, 0); |
| 288 |
|
|
| 289 |
2861836 |
assert(len >= 0); |
| 290 |
2861836 |
if (s->s_error != 0) |
| 291 |
0 |
return (-1); |
| 292 |
2861836 |
if (len == 0) |
| 293 |
163887 |
return (0); |
| 294 |
2697949 |
_vsb_indent(s); |
| 295 |
2697949 |
if (len > VSB_FREESPACE(s)) { |
| 296 |
516516 |
if (VSB_extend(s, len - VSB_FREESPACE(s)) < 0) |
| 297 |
8 |
s->s_error = ENOMEM; |
| 298 |
516516 |
if (s->s_error != 0) |
| 299 |
8 |
return (-1); |
| 300 |
516508 |
} |
| 301 |
2697941 |
memcpy(s->s_buf + s->s_len, buf, len); |
| 302 |
2697941 |
s->s_len += len; |
| 303 |
2697941 |
return (0); |
| 304 |
2861836 |
} |
| 305 |
|
|
| 306 |
|
/* |
| 307 |
|
* Append a string to an vsb. |
| 308 |
|
*/ |
| 309 |
|
int |
| 310 |
2631039 |
VSB_cat(struct vsb *s, const char *str) |
| 311 |
|
{ |
| 312 |
|
const char *nl; |
| 313 |
|
size_t l; |
| 314 |
|
|
| 315 |
2631039 |
assert_VSB_integrity(s); |
| 316 |
2631039 |
assert_VSB_state(s, 0); |
| 317 |
2631039 |
KASSERT(str != NULL, |
| 318 |
|
("%s called with a NULL str pointer", __func__)); |
| 319 |
|
|
| 320 |
2631039 |
if (s->s_error != 0) |
| 321 |
5 |
return (-1); |
| 322 |
|
|
| 323 |
2632021 |
while (s->s_indent > 0 && (nl = strchr(str, '\n')) != NULL) { |
| 324 |
987 |
l = (nl - str) + 1; |
| 325 |
987 |
if (VSB_bcat(s, str, l) < 0) |
| 326 |
0 |
return (-1); |
| 327 |
987 |
str += l; |
| 328 |
|
} |
| 329 |
|
|
| 330 |
2631034 |
l = strlen(str); |
| 331 |
2631034 |
return (VSB_bcat(s, str, l)); |
| 332 |
2631039 |
} |
| 333 |
|
|
| 334 |
|
/* |
| 335 |
|
* Format the given argument list and append the resulting string to an vsb. |
| 336 |
|
*/ |
| 337 |
|
int |
| 338 |
11599934 |
VSB_vprintf(struct vsb *s, const char *fmt, va_list ap) |
| 339 |
|
{ |
| 340 |
|
va_list ap_copy; |
| 341 |
|
int len; |
| 342 |
|
|
| 343 |
11599934 |
assert_VSB_integrity(s); |
| 344 |
11599934 |
assert_VSB_state(s, 0); |
| 345 |
|
|
| 346 |
11599934 |
KASSERT(fmt != NULL, |
| 347 |
|
("%s called with a NULL format string", __func__)); |
| 348 |
|
|
| 349 |
11599934 |
if (s->s_error != 0) |
| 350 |
0 |
return (-1); |
| 351 |
11599934 |
_vsb_indent(s); |
| 352 |
|
|
| 353 |
|
/* |
| 354 |
|
* For the moment, there is no way to get vsnprintf(3) to hand |
| 355 |
|
* back a character at a time, to push everything into |
| 356 |
|
* VSB_putc_func() as was done for the kernel. |
| 357 |
|
* |
| 358 |
|
* In userspace, while drains are useful, there's generally |
| 359 |
|
* not a problem attempting to malloc(3) on out of space. So |
| 360 |
|
* expand a userland vsb if there is not enough room for the |
| 361 |
|
* data produced by VSB_[v]printf(3). |
| 362 |
|
*/ |
| 363 |
|
|
| 364 |
11599934 |
do { |
| 365 |
12568889 |
va_copy(ap_copy, ap); |
| 366 |
25137778 |
len = vsnprintf(&s->s_buf[s->s_len], VSB_FREESPACE(s) + 1, |
| 367 |
12568889 |
fmt, ap_copy); |
| 368 |
12568889 |
va_end(ap_copy); |
| 369 |
12568889 |
if (len < 0) { |
| 370 |
0 |
s->s_error = errno; |
| 371 |
0 |
return (-1); |
| 372 |
|
} |
| 373 |
13537874 |
} while (len > VSB_FREESPACE(s) && |
| 374 |
968985 |
VSB_extend(s, len - VSB_FREESPACE(s)) == 0); |
| 375 |
|
|
| 376 |
|
/* |
| 377 |
|
* s->s_len is the length of the string, without the terminating nul. |
| 378 |
|
* When updating s->s_len, we must subtract 1 from the length that |
| 379 |
|
* we passed into vsnprintf() because that length includes the |
| 380 |
|
* terminating nul. |
| 381 |
|
* |
| 382 |
|
* vsnprintf() returns the amount that would have been copied, |
| 383 |
|
* given sufficient space, so don't over-increment s_len. |
| 384 |
|
*/ |
| 385 |
11599934 |
s->s_len += vmin_t(ssize_t, len, VSB_FREESPACE(s)); |
| 386 |
11599934 |
if (!VSB_HASROOM(s) && !VSB_CANEXTEND(s)) |
| 387 |
25 |
s->s_error = ENOMEM; |
| 388 |
|
|
| 389 |
11599934 |
KASSERT(s->s_len < s->s_size, |
| 390 |
|
("wrote past end of vsb (%d >= %d)", s->s_len, s->s_size)); |
| 391 |
|
|
| 392 |
11599934 |
if (s->s_error != 0) |
| 393 |
23 |
return (-1); |
| 394 |
11599911 |
return (0); |
| 395 |
11599934 |
} |
| 396 |
|
|
| 397 |
|
/* |
| 398 |
|
* Format the given arguments and append the resulting string to an vsb. |
| 399 |
|
*/ |
| 400 |
|
int |
| 401 |
6212403 |
VSB_printf(struct vsb *s, const char *fmt, ...) |
| 402 |
|
{ |
| 403 |
|
va_list ap; |
| 404 |
|
int result; |
| 405 |
|
|
| 406 |
6212403 |
va_start(ap, fmt); |
| 407 |
6212403 |
result = VSB_vprintf(s, fmt, ap); |
| 408 |
6212403 |
va_end(ap); |
| 409 |
6212403 |
return (result); |
| 410 |
|
} |
| 411 |
|
|
| 412 |
|
/* |
| 413 |
|
* Append a character to an vsb. |
| 414 |
|
*/ |
| 415 |
|
int |
| 416 |
46985509 |
VSB_putc(struct vsb *s, int c) |
| 417 |
|
{ |
| 418 |
|
|
| 419 |
46985509 |
VSB_put_byte(s, c); |
| 420 |
46985509 |
if (s->s_error != 0) |
| 421 |
0 |
return (-1); |
| 422 |
46985509 |
return (0); |
| 423 |
46985509 |
} |
| 424 |
|
|
| 425 |
|
/* |
| 426 |
|
* Check if an vsb has an error. |
| 427 |
|
*/ |
| 428 |
|
int |
| 429 |
1510 |
VSB_error(const struct vsb *s) |
| 430 |
|
{ |
| 431 |
|
|
| 432 |
1510 |
return (s->s_error); |
| 433 |
|
} |
| 434 |
|
|
| 435 |
|
/* |
| 436 |
|
* Finish off an vsb. |
| 437 |
|
*/ |
| 438 |
|
int |
| 439 |
2819887 |
VSB_finish(struct vsb *s) |
| 440 |
|
{ |
| 441 |
|
|
| 442 |
2819887 |
assert_VSB_integrity(s); |
| 443 |
2819887 |
assert_VSB_state(s, 0); |
| 444 |
|
|
| 445 |
2819887 |
s->s_buf[s->s_len] = '\0'; |
| 446 |
2819887 |
VSB_SETFLAG(s, VSB_FINISHED); |
| 447 |
2819887 |
errno = s->s_error; |
| 448 |
2819887 |
if (s->s_error) |
| 449 |
33 |
return (-1); |
| 450 |
2819854 |
return (0); |
| 451 |
2819887 |
} |
| 452 |
|
|
| 453 |
|
/* |
| 454 |
|
* Return a pointer to the vsb data. |
| 455 |
|
*/ |
| 456 |
|
char * |
| 457 |
2874728 |
VSB_data(const struct vsb *s) |
| 458 |
|
{ |
| 459 |
|
|
| 460 |
2874728 |
assert_VSB_integrity(s); |
| 461 |
2874728 |
assert_VSB_state(s, VSB_FINISHED); |
| 462 |
|
|
| 463 |
2874728 |
return (s->s_buf); |
| 464 |
|
} |
| 465 |
|
|
| 466 |
|
/* |
| 467 |
|
* Return the length of the vsb data. |
| 468 |
|
*/ |
| 469 |
|
ssize_t |
| 470 |
1103670 |
VSB_len(const struct vsb *s) |
| 471 |
|
{ |
| 472 |
|
|
| 473 |
1103670 |
assert_VSB_integrity(s); |
| 474 |
|
/* don't care if it's finished or not */ |
| 475 |
|
|
| 476 |
1103670 |
if (s->s_error != 0) |
| 477 |
0 |
return (-1); |
| 478 |
1103670 |
return (s->s_len); |
| 479 |
1103670 |
} |
| 480 |
|
|
| 481 |
|
void |
| 482 |
30522 |
VSB_fini(struct vsb *s) |
| 483 |
|
{ |
| 484 |
|
|
| 485 |
30522 |
assert_VSB_integrity(s); |
| 486 |
30522 |
assert(!VSB_ISDYNAMIC(s)); |
| 487 |
30522 |
assert(!VSB_ISDYNSTRUCT(s)); |
| 488 |
30522 |
memset(s, 0, sizeof(*s)); |
| 489 |
30522 |
} |
| 490 |
|
|
| 491 |
|
void |
| 492 |
1193703 |
VSB_destroy(struct vsb **s) |
| 493 |
|
{ |
| 494 |
|
|
| 495 |
1193703 |
AN(s); |
| 496 |
1193703 |
assert_VSB_integrity(*s); |
| 497 |
1193703 |
assert(VSB_ISDYNAMIC(*s)); |
| 498 |
1193703 |
assert(VSB_ISDYNSTRUCT(*s)); |
| 499 |
1193703 |
SBFREE((*s)->s_buf); |
| 500 |
1193703 |
memset(*s, 0, sizeof(**s)); |
| 501 |
1193703 |
SBFREE(*s); |
| 502 |
1193703 |
*s = NULL; |
| 503 |
1193703 |
} |
| 504 |
|
|
| 505 |
|
/* |
| 506 |
|
* Quote a string |
| 507 |
|
*/ |
| 508 |
|
|
| 509 |
|
static void |
| 510 |
4077 |
vsb_quote_hex(struct vsb *s, const uint8_t *u, size_t len) |
| 511 |
|
{ |
| 512 |
|
const uint8_t *w; |
| 513 |
|
|
| 514 |
4077 |
VSB_cat(s, "0x"); |
| 515 |
9839 |
for (w = u; w < u + len; w++) |
| 516 |
9251 |
if (*w != 0x00) |
| 517 |
3489 |
break; |
| 518 |
4077 |
if (w == u + len && len > 4) { |
| 519 |
18 |
VSB_cat(s, "0...0"); |
| 520 |
18 |
} else { |
| 521 |
672068 |
for (w = u; w < u + len; w++) |
| 522 |
668009 |
VSB_printf(s, "%02x", *w); |
| 523 |
|
} |
| 524 |
4077 |
} |
| 525 |
|
|
| 526 |
|
void |
| 527 |
253549 |
VSB_quote_pfx(struct vsb *s, const char *pfx, const void *v, int len, int how) |
| 528 |
|
{ |
| 529 |
253549 |
const uint8_t *p = v; |
| 530 |
|
const uint8_t *q; |
| 531 |
253549 |
int quote = 0; |
| 532 |
|
int nl; |
| 533 |
|
|
| 534 |
253549 |
nl = how & |
| 535 |
|
(VSB_QUOTE_JSON|VSB_QUOTE_HEX|VSB_QUOTE_CSTR|VSB_QUOTE_UNSAFE); |
| 536 |
253549 |
AZ(nl & (nl - 1)); // Only one bit can be set |
| 537 |
|
|
| 538 |
253549 |
if (how & VSB_QUOTE_ESCHEX) |
| 539 |
120000 |
AZ(how & (VSB_QUOTE_JSON|VSB_QUOTE_HEX)); |
| 540 |
|
|
| 541 |
253549 |
if (how & VSB_QUOTE_UNSAFE) |
| 542 |
119686 |
how |= VSB_QUOTE_NONL; |
| 543 |
|
|
| 544 |
253549 |
assert(p != NULL); |
| 545 |
253549 |
if (len == -1) |
| 546 |
121547 |
len = strlen(v); |
| 547 |
|
|
| 548 |
253549 |
if (len == 0 && (how & VSB_QUOTE_CSTR)) { |
| 549 |
204 |
VSB_printf(s, "%s\"\"", pfx); |
| 550 |
204 |
if ((how & VSB_QUOTE_NONL)) |
| 551 |
0 |
VSB_putc(s, '\n'); |
| 552 |
204 |
} |
| 553 |
|
|
| 554 |
253549 |
if (len == 0) |
| 555 |
8233 |
return; |
| 556 |
|
|
| 557 |
245316 |
VSB_cat(s, pfx); |
| 558 |
|
|
| 559 |
245316 |
if (how & VSB_QUOTE_HEX) { |
| 560 |
4077 |
vsb_quote_hex(s, v, len); |
| 561 |
4077 |
if (how & VSB_QUOTE_NONL) |
| 562 |
1 |
VSB_putc(s, '\n'); |
| 563 |
4077 |
return; |
| 564 |
|
} |
| 565 |
|
|
| 566 |
241239 |
if (how & VSB_QUOTE_CSTR) |
| 567 |
113317 |
VSB_putc(s, '"'); |
| 568 |
|
|
| 569 |
5072715 |
for (q = p; q < p + len; q++) { |
| 570 |
|
if ( |
| 571 |
4903996 |
*q < 0x20 || |
| 572 |
4838307 |
*q == '"' || |
| 573 |
4837443 |
*q == '\\' || |
| 574 |
4837223 |
(*q == '?' && (how & VSB_QUOTE_CSTR)) || |
| 575 |
4831476 |
(*q > 0x7e && !(how & VSB_QUOTE_JSON)) |
| 576 |
|
) { |
| 577 |
72388 |
quote++; |
| 578 |
72388 |
break; |
| 579 |
|
} |
| 580 |
4831476 |
} |
| 581 |
|
|
| 582 |
241239 |
if (!quote) { |
| 583 |
168851 |
VSB_bcat(s, p, len); |
| 584 |
168851 |
if ((how & VSB_QUOTE_NONL) && |
| 585 |
77524 |
p[len-1] != '\n') |
| 586 |
77524 |
(void)VSB_putc(s, '\n'); |
| 587 |
168851 |
if (how & VSB_QUOTE_CSTR) |
| 588 |
76979 |
VSB_putc(s, '"'); |
| 589 |
168851 |
return; |
| 590 |
|
} |
| 591 |
|
|
| 592 |
72388 |
nl = 0; |
| 593 |
16022394 |
for (q = p; q < p + len; q++) { |
| 594 |
15950006 |
if (nl) |
| 595 |
165315 |
VSB_cat(s, pfx); |
| 596 |
15950006 |
nl = 0; |
| 597 |
15950006 |
switch (*q) { |
| 598 |
|
case '?': |
| 599 |
|
/* Avoid C Trigraph insanity */ |
| 600 |
24040 |
if (how & VSB_QUOTE_CSTR && !(how & VSB_QUOTE_JSON)) |
| 601 |
8132 |
(void)VSB_putc(s, '\\'); |
| 602 |
24040 |
(void)VSB_putc(s, *q); |
| 603 |
24040 |
break; |
| 604 |
|
case '\\': |
| 605 |
|
case '"': |
| 606 |
160845 |
if (!(how & VSB_QUOTE_UNSAFE)) |
| 607 |
106130 |
(void)VSB_putc(s, '\\'); |
| 608 |
160845 |
(void)VSB_putc(s, *q); |
| 609 |
160845 |
break; |
| 610 |
|
case '\n': |
| 611 |
663780 |
if (how & VSB_QUOTE_CSTR) { |
| 612 |
463973 |
VSB_printf(s, "\\n\"\n%s\"", pfx); |
| 613 |
663780 |
} else if (how & VSB_QUOTE_JSON) { |
| 614 |
1510 |
VSB_cat(s, "\\n"); |
| 615 |
199807 |
} else if (how & VSB_QUOTE_NONL) { |
| 616 |
198284 |
VSB_putc(s, *q); |
| 617 |
198284 |
nl = 1; |
| 618 |
198284 |
} else { |
| 619 |
13 |
VSB_cat(s, "\\n"); |
| 620 |
|
} |
| 621 |
663780 |
break; |
| 622 |
|
case '\r': |
| 623 |
79895 |
VSB_cat(s, "\\r"); |
| 624 |
79895 |
break; |
| 625 |
|
case '\t': |
| 626 |
220195 |
VSB_cat(s, "\\t"); |
| 627 |
220195 |
break; |
| 628 |
|
default: |
| 629 |
14801251 |
if (0x20 <= *q && *q <= 0x7e) |
| 630 |
14792129 |
VSB_putc(s, *q); |
| 631 |
9122 |
else if (*q > 0x7e && (how & VSB_QUOTE_JSON)) |
| 632 |
4 |
VSB_putc(s, *q); |
| 633 |
9118 |
else if (how & VSB_QUOTE_JSON) |
| 634 |
7 |
VSB_printf(s, "\\u%04x", *q); |
| 635 |
9111 |
else if (how & VSB_QUOTE_ESCHEX) |
| 636 |
9047 |
VSB_printf(s, "\\x%02x", *q); |
| 637 |
|
else |
| 638 |
64 |
VSB_printf(s, "\\%03o", *q); |
| 639 |
14801251 |
break; |
| 640 |
|
} |
| 641 |
15950006 |
} |
| 642 |
72388 |
if (how & VSB_QUOTE_CSTR) |
| 643 |
36338 |
VSB_putc(s, '"'); |
| 644 |
72388 |
if ((how & VSB_QUOTE_NONL) && !nl) |
| 645 |
2674 |
VSB_putc(s, '\n'); |
| 646 |
253549 |
} |
| 647 |
|
|
| 648 |
|
void |
| 649 |
129774 |
VSB_quote(struct vsb *s, const void *v, int len, int how) |
| 650 |
|
{ |
| 651 |
129774 |
VSB_quote_pfx(s, "", v, len, how); |
| 652 |
129774 |
} |
| 653 |
|
|
| 654 |
|
/* |
| 655 |
|
* Indentation |
| 656 |
|
*/ |
| 657 |
|
|
| 658 |
|
void |
| 659 |
2436 |
VSB_indent(struct vsb *s, int i) |
| 660 |
|
{ |
| 661 |
|
|
| 662 |
2436 |
assert_VSB_integrity(s); |
| 663 |
2436 |
if (s->s_indent + i < 0) |
| 664 |
0 |
s->s_error = EINVAL; |
| 665 |
|
else |
| 666 |
2436 |
s->s_indent += i; |
| 667 |
2436 |
} |
| 668 |
|
|
| 669 |
|
int |
| 670 |
71499 |
VSB_tofile(const struct vsb *s, int fd) |
| 671 |
|
{ |
| 672 |
|
const char *p; |
| 673 |
|
ssize_t r; |
| 674 |
|
size_t sz; |
| 675 |
|
|
| 676 |
71499 |
assert_VSB_integrity(s); |
| 677 |
71499 |
assert_VSB_state(s, VSB_FINISHED); |
| 678 |
71499 |
assert(s->s_len >= 0); |
| 679 |
71499 |
r = 0; |
| 680 |
71499 |
p = s->s_buf; |
| 681 |
71499 |
sz = (typeof(sz))s->s_len; |
| 682 |
142978 |
while (sz > 0) { |
| 683 |
71486 |
r = write(fd, p, sz); |
| 684 |
71486 |
if (r < 0) |
| 685 |
7 |
return (-1); |
| 686 |
71479 |
assert((typeof(sz))r <= sz); |
| 687 |
71479 |
p += r; |
| 688 |
71479 |
sz -= r; |
| 689 |
|
} |
| 690 |
71492 |
return (0); |
| 691 |
71499 |
} |