Code Coverage: vinyl-cache/lib/libvinyl/vsb.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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 507377804
#define KASSERT(e, m)           assert(e)
46 3771493
#define SBMALLOC(size)          malloc(size)
47 3145580
#define SBFREE(buf)             free(buf)
48
49 46173
#define rndup2(x, y)    (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
50
51
/*
52
 * Predicates
53
 */
54 5104854
#define VSB_ISDYNAMIC(s)        ((s)->s_flags & VSB_DYNAMIC)
55
#define VSB_ISDYNSTRUCT(s)      ((s)->s_flags & VSB_DYNSTRUCT)
56 29889930
#define VSB_HASROOM(s)          ((s)->s_len < (s)->s_size - 1L)
57 114030680
#define VSB_FREESPACE(s)        ((s)->s_size - ((s)->s_len + 1L))
58 2759575
#define VSB_CANEXTEND(s)        ((s)->s_flags & VSB_AUTOEXTEND)
59
60
/*
61
 * Set / clear flags
62
 */
63 7320024
#define VSB_SETFLAG(s, f)       do { (s)->s_flags |= (f); } while (0)
64 2647468
#define VSB_CLEARFLAG(s, f)     do { (s)->s_flags &= ~(f); } while (0)
65
66 4392006
#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 4438179
#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
_assert_VSB_integrity(const char *fun, const struct vsb *s)
82 95029679
{
83
84 95029679
        (void)fun;
85 95029679
        (void)s;
86 95029679
        KASSERT(s != NULL,
87 95029679
            ("%s called with a NULL vsb pointer", fun));
88 95029679
        KASSERT(s->magic == VSB_MAGIC,
89 95029679
            ("%s called with a bogus vsb pointer", fun));
90 95029679
        KASSERT(s->s_buf != NULL,
91 95029679
            ("%s called with uninitialized or corrupt vsb", fun));
92 95029679
        KASSERT(s->s_len < s->s_size,
93 95029679
            ("wrote past end of vsb (%d >= %d)", s->s_len, s->s_size));
94 95029679
}
95
96
static void
97
_assert_VSB_state(const char *fun, const struct vsb *s, int state)
98 89370874
{
99
100 89370874
        (void)fun;
101 89370874
        (void)s;
102 89370874
        (void)state;
103 89370874
        KASSERT((s->s_flags & VSB_FINISHED) == state,
104 89370874
            ("%s called with %sfinished or corrupt vsb", fun,
105 89370874
            (state ? "un" : "")));
106 89370874
}
107 95068180
#define assert_VSB_integrity(s) _assert_VSB_integrity(__func__, (s))
108 89399410
#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
VSB_extendsize(ssize_t size)
121 4438179
{
122 4438179
        ssize_t newsize;
123
124 4438179
        if (size < (int)VSB_MAXEXTENDSIZE) {
125 4392006
                newsize = VSB_MINEXTENDSIZE;
126 10462041
                while (newsize < size)
127 6070035
                        newsize *= 2;
128 4392006
        } else {
129 46173
                newsize = rndup2(size, VSB_MAXEXTENDINCR);
130 46173
        }
131 4438179
        KASSERT(newsize >= size, ("%s: %d < %d\n", __func__, newsize, size));
132 4438179
        return (newsize);
133 4438179
}
134
135
/*
136
 * Extend an vsb.
137
 */
138
static ssize_t
139
VSB_extend(struct vsb *s, ssize_t addlen)
140 2552466
{
141 2552466
        char *newbuf;
142 2552466
        ssize_t newsize;
143
144 2552466
        if (!VSB_CANEXTEND(s))
145 39
                return (-1);
146 2552427
        newsize = VSB_extendsize(s->s_size + addlen);
147 2552427
        if (VSB_ISDYNAMIC(s))
148 2552426
                newbuf = realloc(s->s_buf, newsize);
149 1
        else
150 1
                newbuf = SBMALLOC(newsize);
151 2552427
        if (newbuf == NULL)
152 0
                return (-1);
153 2552427
        if (!VSB_ISDYNAMIC(s)) {
154 0
                vmemcpy(newbuf, s->s_buf, s->s_size);
155 0
                VSB_SETFLAG(s, VSB_DYNAMIC);
156 0
        }
157 2552427
        s->s_buf = newbuf;
158 2552427
        s->s_size = newsize;
159 2552427
        return (0);
160 2552427
}
161
162
static void
163
_vsb_indent(struct vsb *s)
164 78414812
{
165 78414812
        if (s->s_indent == 0 || s->s_error != 0 ||
166 78414812
            (s->s_len > 0 && s->s_buf[s->s_len - 1] != '\n'))
167 78406470
                return;
168 8342
        if (VSB_FREESPACE(s) <= s->s_indent &&
169 8342
            VSB_extend(s, s->s_indent) < 0) {
170 0
                s->s_error = ENOMEM;
171 0
                return;
172 0
        }
173 8342
        memset(s->s_buf + s->s_len, ' ', s->s_indent);
174 8342
        s->s_len += s->s_indent;
175 8342
}
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
VSB_newbuf(struct vsb *s, char *buf, int length, int flags)
184 1951935
{
185
186 1951935
        memset(s, 0, sizeof(*s));
187 1951935
        s->magic = VSB_MAGIC;
188 1951935
        s->s_flags = flags;
189 1951935
        s->s_size = length;
190 1951935
        s->s_buf = buf;
191
192 1951935
        if ((s->s_flags & VSB_AUTOEXTEND) == 0) {
193 66183
                KASSERT(s->s_size > 1,
194 66183
                    ("attempt to create a too small vsb"));
195 66183
        }
196
197 1951935
        if (s->s_buf != NULL)
198 66187
                return (s);
199
200 1885748
        if ((flags & VSB_AUTOEXTEND) != 0)
201 1885757
                s->s_size = VSB_extendsize(s->s_size);
202
203 1885748
        s->s_buf = SBMALLOC(s->s_size);
204 1885748
        if (s->s_buf == NULL)
205 0
                return (NULL);
206 1885748
        VSB_SETFLAG(s, VSB_DYNAMIC);
207 1885748
        return (s);
208 1885748
}
209
210
struct vsb *
211
VSB_init(struct vsb *s, void *buf, ssize_t length)
212 66187
{
213 66187
        AN(s);
214 66187
        AN(buf);
215
216 66187
        KASSERT(length >= 0,
217 66187
            ("attempt to create an vsb of negative length (%zd)", length));
218 66187
        return (VSB_newbuf(s, buf, length, VSB_FIXEDLEN));
219 66187
}
220
221
/*
222
 * Allocate a dynamic vsb
223
 */
224
struct vsb *
225
VSB_new_auto(void)
226 1885744
{
227 1885744
        struct vsb *s;
228
229 1885744
        s = SBMALLOC(sizeof(*s));
230 1885744
        if (s == NULL)
231 0
                return (NULL);
232 1885744
        if (VSB_newbuf(s, NULL, 0, VSB_AUTOEXTEND) == NULL) {
233 0
                SBFREE(s);
234 0
                return (NULL);
235 0
        }
236 1885744
        VSB_SETFLAG(s, VSB_DYNSTRUCT);
237 1885744
        return (s);
238 1885744
}
239
240
/*
241
 * Clear an vsb and reset its position.
242
 */
243
void
244
VSB_clear(struct vsb *s)
245 2647468
{
246
247 2647468
        assert_VSB_integrity(s);
248
        /* don't care if it's finished or not */
249
250 2647468
        VSB_CLEARFLAG(s, VSB_FINISHED);
251 2647468
        s->s_error = 0;
252 2647468
        s->s_len = 0;
253 2647468
        s->s_indent = 0;
254 2647468
}
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
VSB_put_byte(struct vsb *s, int c)
263 59947206
{
264
265 59947206
        assert_VSB_integrity(s);
266 59947206
        assert_VSB_state(s, 0);
267
268 59947206
        if (s->s_error != 0)
269 0
                return;
270 59947206
        _vsb_indent(s);
271 59947206
        if (VSB_FREESPACE(s) <= 0) {
272 603917
                if (VSB_extend(s, 1) < 0)
273 0
                        s->s_error = ENOMEM;
274 603917
                if (s->s_error != 0)
275 0
                        return;
276 603917
        }
277 59947206
        s->s_buf[s->s_len++] = (char)c;
278 59947206
}
279
280
/*
281
 * Append a byte string to an vsb.
282
 */
283
int
284
VSB_bcat(struct vsb *s, const void *buf, ssize_t len)
285 3779867
{
286 3779867
        assert_VSB_integrity(s);
287 3779867
        assert_VSB_state(s, 0);
288
289 3779867
        assert(len >= 0);
290 3779867
        if (s->s_error != 0)
291 0
                return (-1);
292 3779867
        if (len == 0)
293 244455
                return (0);
294 3535412
        _vsb_indent(s);
295 3535412
        if (len > VSB_FREESPACE(s)) {
296 696409
                if (VSB_extend(s, len - VSB_FREESPACE(s)) < 0)
297 8
                        s->s_error = ENOMEM;
298 696409
                if (s->s_error != 0)
299 8
                        return (-1);
300 696409
        }
301 3535404
        vmemcpy(s->s_buf + s->s_len, buf, len);
302 3535404
        s->s_len += len;
303 3535404
        return (0);
304 3535412
}
305
306
/*
307
 * Append a string to an vsb.
308
 */
309
int
310
VSB_cat(struct vsb *s, const char *str)
311 3427735
{
312 3427735
        const char *nl;
313 3427735
        size_t l;
314
315 3427735
        assert_VSB_integrity(s);
316 3427735
        assert_VSB_state(s, 0);
317 3427735
        KASSERT(str != NULL,
318 3427735
            ("%s called with a NULL str pointer", __func__));
319
320 3427735
        if (s->s_error != 0)
321 5
                return (-1);
322
323 3428873
        while (s->s_indent > 0 && (nl = strchr(str, '\n')) != NULL) {
324 1143
                l = (nl - str) + 1;
325 1143
                if (VSB_bcat(s, str, l) < 0)
326 0
                        return (-1);
327 1143
                str += l;
328 1143
        }
329
330 3427730
        l = vstrlen(str);
331 3427730
        return (VSB_bcat(s, str, l));
332 3427730
}
333
334
/*
335
 * Format the given argument list and append the resulting string to an vsb.
336
 */
337
int
338
VSB_vprintf(struct vsb *s, const char *fmt, va_list ap)
339 14944965
{
340 14944965
        va_list ap_copy;
341 14944965
        int len;
342
343 14944965
        assert_VSB_integrity(s);
344 14944965
        assert_VSB_state(s, 0);
345
346 14944965
        KASSERT(fmt != NULL,
347 14944965
            ("%s called with a NULL format string", __func__));
348
349 14944965
        if (s->s_error != 0)
350 0
                return (-1);
351 14944965
        _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 16197061
        do {
365 16197061
                va_copy(ap_copy, ap);
366 16197061
                len = vsnprintf(&s->s_buf[s->s_len], VSB_FREESPACE(s) + 1,
367 16197061
                    fmt, ap_copy);
368 16197061
                va_end(ap_copy);
369 16197061
                if (len < 0) {
370 0
                        s->s_error = errno;
371 0
                        return (-1);
372 0
                }
373 16197061
        } while (len > VSB_FREESPACE(s) &&
374 16197061
            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 14944965
        s->s_len += vmin_t(ssize_t, len, VSB_FREESPACE(s));
386 14944965
        if (!VSB_HASROOM(s) && !VSB_CANEXTEND(s))
387 31
                s->s_error = ENOMEM;
388
389 14944965
        KASSERT(s->s_len < s->s_size,
390 14944965
            ("wrote past end of vsb (%d >= %d)", s->s_len, s->s_size));
391
392 14944965
        if (s->s_error != 0)
393 31
                return (-1);
394 14944934
        return (0);
395 14944965
}
396
397
/*
398
 * Format the given arguments and append the resulting string to an vsb.
399
 */
400
int
401
VSB_printf(struct vsb *s, const char *fmt, ...)
402 7955343
{
403 7955343
        va_list ap;
404 7955343
        int result;
405
406 7955343
        va_start(ap, fmt);
407 7955343
        result = VSB_vprintf(s, fmt, ap);
408 7955343
        va_end(ap);
409 7955343
        return (result);
410 7955343
}
411
412
/*
413
 * Append a character to an vsb.
414
 */
415
int
416
VSB_putc(struct vsb *s, int c)
417 59946564
{
418
419 59946564
        VSB_put_byte(s, c);
420 59946564
        if (s->s_error != 0)
421 0
                return (-1);
422 59946564
        return (0);
423 59946564
}
424
425
/*
426
 * Check if an vsb has an error.
427
 */
428
int
429
VSB_error(const struct vsb *s)
430 1556
{
431
432 1556
        return (s->s_error);
433 1556
}
434
435
/*
436
 * Finish off an vsb.
437
 */
438
int
439
VSB_finish(struct vsb *s)
440 3548532
{
441
442 3548532
        assert_VSB_integrity(s);
443 3548532
        assert_VSB_state(s, 0);
444
445 3548532
        s->s_buf[s->s_len] = '\0';
446 3548532
        VSB_SETFLAG(s, VSB_FINISHED);
447 3548532
        errno = s->s_error;
448 3548532
        if (s->s_error)
449 39
                return (-1);
450 3548493
        return (0);
451 3548532
}
452
453
/*
454
 * Return a pointer to the vsb data.
455
 */
456
char *
457
VSB_data(const struct vsb *s)
458 3665896
{
459
460 3665896
        assert_VSB_integrity(s);
461 3665896
        assert_VSB_state(s, VSB_FINISHED);
462
463 3665896
        return (s->s_buf);
464 3665896
}
465
466
/*
467
 * Return the length of the vsb data.
468
 */
469
ssize_t
470
VSB_len(const struct vsb *s)
471 1380479
{
472
473 1380479
        assert_VSB_integrity(s);
474
        /* don't care if it's finished or not */
475
476 1380479
        if (s->s_error != 0)
477 0
                return (-1);
478 1380479
        return (s->s_len);
479 1380479
}
480
481
void
482
VSB_fini(struct vsb *s)
483 65121
{
484
485 65121
        assert_VSB_integrity(s);
486 65121
        assert(!VSB_ISDYNAMIC(s));
487 65121
        assert(!VSB_ISDYNSTRUCT(s));
488 65121
        memset(s, 0, sizeof(*s));
489 65121
}
490
491
void
492
VSB_destroy(struct vsb **s)
493 1572790
{
494
495 1572790
        AN(s);
496 1572790
        assert_VSB_integrity(*s);
497 1572790
        assert(VSB_ISDYNAMIC(*s));
498 1572790
        assert(VSB_ISDYNSTRUCT(*s));
499 1572790
        SBFREE((*s)->s_buf);
500 1572790
        memset(*s, 0, sizeof(**s));
501 1572790
        SBFREE(*s);
502 1572790
        *s = NULL;
503 1572790
}
504
505
/*
506
 * Quote a string
507
 */
508
509
static void
510
vsb_quote_hex(struct vsb *s, const uint8_t *u, size_t len)
511 4206
{
512 4206
        const uint8_t *w;
513
514 4206
        VSB_cat(s, "0x");
515 10160
        for (w = u; w < u + len; w++)
516 9548
                if (*w != 0x00)
517 3594
                        break;
518 4206
        if (w == u + len && len > 4) {
519 17
                VSB_cat(s, "0...0");
520 4189
        } else {
521 717499
                for (w = u; w < u + len; w++)
522 713310
                        VSB_printf(s, "%02x", *w);
523 4189
        }
524 4206
}
525
526
void
527
VSB_quote_pfx(struct vsb *s, const char *pfx, const void *v, int len, int how)
528 362705
{
529 362705
        const uint8_t *p = v;
530 362705
        const uint8_t *q;
531 362705
        int quote = 0;
532 362705
        int nl;
533
534 362705
        nl = how &
535 362705
            (VSB_QUOTE_JSON|VSB_QUOTE_HEX|VSB_QUOTE_CSTR|VSB_QUOTE_UNSAFE);
536 362705
        AZ(nl & (nl - 1)); // Only one bit can be set
537
538 362705
        if (how & VSB_QUOTE_ESCHEX)
539 188874
                AZ(how & (VSB_QUOTE_JSON|VSB_QUOTE_HEX));
540
541 362705
        if (how & VSB_QUOTE_UNSAFE)
542 145936
                how |= VSB_QUOTE_NONL;
543
544 362705
        assert(p != NULL);
545 362705
        if (how & VSB_QUOTE_ABBREVIATE) {
546 7
                assert (len > 5);
547 7
                if (vstrlen(v) < (unsigned)len) {
548 2
                        len = vstrlen(v);
549 2
                        how &= ~VSB_QUOTE_ABBREVIATE;
550 5
                } else {
551 5
                        len -= 5;
552 5
                }
553 7
        }
554 362705
        if (len == -1)
555 161068
                len = vstrlen(v);
556
557 362705
        if (len == 0 && (how & VSB_QUOTE_CSTR)) {
558 209
                VSB_printf(s, "%s\"\"", pfx);
559 209
                if ((how & VSB_QUOTE_NONL))
560 0
                        VSB_putc(s, '\n');
561 209
        }
562
563 362705
        if (len == 0)
564 8822
                return;
565
566 353883
        VSB_cat(s, pfx);
567
568 353883
        if (how & VSB_QUOTE_HEX) {
569 4206
                vsb_quote_hex(s, v, len);
570 4206
                if (how & VSB_QUOTE_NONL)
571 1
                        VSB_putc(s, '\n');
572 4206
                return;
573 4206
        }
574
575 349677
        if (how & VSB_QUOTE_CSTR)
576 152152
                VSB_putc(s, '"');
577
578 8566752
        for (q = p; q < p + len; q++) {
579 8310362
                if (
580 8310362
                    *q < 0x20 ||
581 8310362
                    *q == '"' ||
582 8310362
                    *q == '\\' ||
583 8310362
                    (*q == '?' && (how & VSB_QUOTE_CSTR)) ||
584 8310362
                    (*q > 0x7e && !(how & VSB_QUOTE_JSON))
585 8310362
                ) {
586 93287
                        quote++;
587 93287
                        break;
588 93287
                }
589 8310362
        }
590
591 349677
        if (!quote) {
592 256384
                VSB_bcat(s, p, len);
593 256384
                if (how & VSB_QUOTE_CSTR)
594 110036
                        VSB_putc(s, '"');
595 256384
                if (how & VSB_QUOTE_ABBREVIATE)
596 1
                        VSB_cat(s, "[...]");
597 256384
                if ((how & VSB_QUOTE_NONL) &&
598 256384
                    p[len-1] != '\n')
599 89359
                        (void)VSB_putc(s, '\n');
600 256384
                return;
601 256384
        }
602
603 93293
        nl = 0;
604 21414471
        for (q = p; q < p + len; q++) {
605 21321764
                if (nl)
606 195217
                        VSB_cat(s, pfx);
607 21321764
                nl = 0;
608 21321764
                switch (*q) {
609 34407
                case '?':
610
                        /* Avoid C Trigraph insanity */
611 34407
                        if (how & VSB_QUOTE_CSTR && !(how & VSB_QUOTE_JSON))
612 13114
                                (void)VSB_putc(s, '\\');
613 34407
                        (void)VSB_putc(s, *q);
614 34407
                        break;
615 26983
                case '\\':
616 223288
                case '"':
617 223288
                        if (!(how & VSB_QUOTE_UNSAFE))
618 146341
                                (void)VSB_putc(s, '\\');
619 223288
                        (void)VSB_putc(s, *q);
620 223288
                        break;
621 848719
                case '\n':
622 848719
                        if (how & VSB_QUOTE_CSTR) {
623 606714
                                VSB_printf(s, "\\n\"\n%s\"", pfx);
624 606714
                        } else if (how & VSB_QUOTE_JSON) {
625 1752
                                VSB_cat(s, "\\n");
626 240253
                        } else if (how & VSB_QUOTE_NONL) {
627 239608
                                VSB_putc(s, *q);
628 239608
                                nl = 1;
629 239608
                        } else {
630 645
                                VSB_cat(s, "\\n");
631 645
                        }
632 848719
                        break;
633 100816
                case '\r':
634 100816
                        VSB_cat(s, "\\r");
635 100816
                        break;
636 299284
                case '\t':
637 299284
                        VSB_cat(s, "\\t");
638 299284
                        break;
639 19830213
                default:
640 19830213
                        if (0x20 <= *q && *q <= 0x7e)
641 19804129
                                VSB_putc(s, *q);
642 26084
                        else if (*q > 0x7e && (how & VSB_QUOTE_JSON))
643 4
                                VSB_putc(s, *q);
644 26080
                        else if (how & VSB_QUOTE_JSON)
645 7
                                VSB_printf(s, "\\u%04x", *q);
646 26073
                        else if (how & VSB_QUOTE_ESCHEX)
647 26478
                                VSB_printf(s, "\\x%02x", *q);
648 18446744073709551211
                        else
649 18446744073709551211
                                VSB_printf(s, "\\%03o", *q);
650 19830213
                        break;
651 21321764
                }
652 21321764
        }
653 92707
        if (how & VSB_QUOTE_CSTR)
654 42116
                VSB_putc(s, '"');
655 92707
        if (how & VSB_QUOTE_ABBREVIATE)
656 4
                VSB_cat(s, "[...]");
657 92707
        if ((how & VSB_QUOTE_NONL) && !nl)
658 4870
                VSB_putc(s, '\n');
659 92707
}
660
661
void
662
VSB_quote(struct vsb *s, const void *v, int len, int how)
663 212659
{
664 212659
        VSB_quote_pfx(s, "", v, len, how);
665 212659
}
666
667
/*
668
 * Indentation
669
 */
670
671
void
672
VSB_indent(struct vsb *s, int i)
673 2912
{
674
675 2912
        assert_VSB_integrity(s);
676 2912
        if (s->s_indent + i < 0)
677 0
                s->s_error = EINVAL;
678 2912
        else
679 2912
                s->s_indent += i;
680 2912
}
681
682
int
683
VSB_tofile(const struct vsb *s, int fd)
684 85209
{
685 85209
        const char *p;
686 85209
        ssize_t r;
687 85209
        size_t sz;
688
689 85209
        assert_VSB_integrity(s);
690 85209
        assert_VSB_state(s, VSB_FINISHED);
691 85209
        assert(s->s_len >= 0);
692 85209
        r = 0;
693 85209
        p = s->s_buf;
694 85209
        sz = (typeof(sz))s->s_len;
695 170389
        while (sz > 0) {
696 85195
                r = write(fd, p, sz);
697 85195
                if (r < 0)
698 15
                        return (-1);
699 85180
                assert((typeof(sz))r <= sz);
700 85180
                p += r;
701 85180
                sz -= r;
702 85180
        }
703 85194
        return (0);
704 85209
}