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 "config.h"
30
31
#include <string.h>
32
33
#include "cache/cache.h"
34
#include "wb.h"
35
36
char *
37 676
wb_create(struct ws *ws, struct wb_s *wb)
38
{
39 676
        if (WS_Reserve(ws, 0) == 0) {
40 1
                WS_Release(ws, 0);
41 1
                wb->w = NULL;
42 1
                wb->ws = NULL;
43 1
                return NULL;
44
        }
45 675
        wb->w = ws->f;
46 675
        wb->ws = ws;
47
48 675
        return wb->w;
49
}
50
51
void
52 104
wb_reset(struct wb_s *wb)
53
{
54 104
        WS_Release(wb->ws, 0);
55 104
        memset(wb, 0, sizeof(*wb));
56 104
}
57
58
/*
59
 * release varnish workspace
60
 *
61
 * return start of buffer
62
 */
63
char *
64 380
wb_finish(struct wb_s *wb, ssize_t *l)
65
{
66 380
        char *r = wb->ws->f;
67 380
        assert(wb->ws->r - wb->w > 0);
68 380
        if (l)
69 0
                *l = wb_len(wb);
70
71 380
        *wb->w = '\0';
72 380
        wb->w++;
73
74
        /* amount of space used */
75 380
        WS_ReleaseP(wb->ws, wb->w);
76
77 380
        return r;
78
}