varnish-cache/lib/libvmod_blob/wb.c
1
/*-
2
 * Copyright 2015 UPLEX - Nils Goroll Systemoptimierung
3
 * All rights reserved.
4
 *
5
 * Authors: Nils Goroll <nils.goroll@uplex.de>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions are met:
9
 * 1. Redistributions of source code must retain the above copyright notice,
10
 *    this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright notice,
12
 *    this list of conditions and the following disclaimer in the documentation
13
 *    and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
16
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
 * DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
19
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 *
26
 * write buffer: utility functions to append-write on a varnish workspace
27
 */
28
29
#include <string.h>
30
31
#include "cache/cache.h"
32
#include "wb.h"
33
34
char *
35 631
wb_create(struct ws *ws, struct wb_s *wb)
36
{
37 631
        if (WS_Reserve(ws, 0) == 0) {
38 1
                WS_Release(ws, 0);
39 1
                wb->w = NULL;
40 1
                wb->ws = NULL;
41 1
                return NULL;
42
        }
43 630
        wb->w = ws->f;
44 630
        wb->ws = ws;
45
46 630
        return wb->w;
47
}
48
49
void
50 89
wb_reset(struct wb_s *wb)
51
{
52 89
        WS_Release(wb->ws, 0);
53 89
        memset(wb, 0, sizeof(*wb));
54 89
}
55
56
/*
57
 * release varnish workspace
58
 *
59
 * return start of buffer
60
 */
61
char *
62 365
wb_finish(struct wb_s *wb, ssize_t *l)
63
{
64 365
        char *r = wb->ws->f;
65 365
        assert(wb->ws->r - wb->w > 0);
66 365
        if (l)
67 0
                *l = wb_len(wb);
68
69 365
        *wb->w = '\0';
70 365
        wb->w++;
71
72
        /* amount of space used */
73 365
        WS_ReleaseP(wb->ws, wb->w);
74
75 365
        return r;
76
}