varnish-cache/lib/libvmod_blob/wb.h
1
/*-
2
 * Copyright 2015-2016 UPLEX - Nils Goroll Systemoptimierung
3
 * All rights reserved.
4
 *
5
 * Author: 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
 */
27
28
struct wb_s {
29
        struct ws       *ws; // varnish workspace
30
        char            *w;  // current write position
31
};
32
33
/* return one byte less for the final zero byte */
34
static inline const char*
35 1082
wb_end(const struct wb_s *wb) {
36 1082
        return wb->ws->r - 1;
37
}
38
39
/* return the write position */
40
static inline char*
41 1612
wb_buf(const struct wb_s *wb) {
42 1612
        return wb->w;
43
}
44
45
/* return one byte less for the final zero byte */
46
static inline ssize_t
47 1260
wb_space(const struct wb_s *wb) {
48 1260
        ssize_t f = wb->ws->r - wb->w;
49 1260
        assert(f > 0);
50 1260
        return f - 1;
51
}
52
53
static inline ssize_t
54 0
wb_len(const struct wb_s *wb) {
55 0
        ssize_t l = wb->w - wb->ws->f;
56 0
        assert(l >= 0);
57 0
        return l;
58
}
59
60
static inline void
61 1082
wb_advance(struct wb_s *wb, ssize_t l) {
62 1082
        wb->w += l;                     // final byte
63 1082
        assert(wb->w < wb_end(wb));
64 1082
}
65
66
char *wb_create(struct ws *ws, struct wb_s *wb);
67
void wb_reset(struct wb_s *wb);
68
char *wb_finish(struct wb_s *wb, ssize_t *l);