varnish-cache/lib/libvarnish/vlu.c
1
/*-
2
 * Copyright (c) 2005-2008 Poul-Henning Kamp <phk@FreeBSD.org>
3
 * All rights reserved.
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
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
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * Functions for assembling a bytestream into text-lines and calling
29
 * a function on each.
30
 */
31
32
#include "config.h"
33
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
#include <unistd.h>
38
39
#include "vdef.h"
40
41
#include "vas.h"        // XXX Flexelint "not used" - but req'ed for assert()
42
#include "miniobj.h"
43
44
#include "vlu.h"
45
46
struct vlu {
47
        unsigned        magic;
48
#define LINEUP_MAGIC    0x8286661
49
        char            *buf;
50
        unsigned        bufl;
51
        unsigned        bufp;
52
        void            *priv;
53
        vlu_f   *func;
54
};
55
56
struct vlu *
57 321800
VLU_New(vlu_f *func, void *priv, unsigned bufsize)
58
{
59
        struct vlu *l;
60
61 321800
        if (bufsize == 0)
62 321480
                bufsize = BUFSIZ;
63 321800
        ALLOC_OBJ(l, LINEUP_MAGIC);
64 321800
        if (l != NULL) {
65 321800
                l->func = func;
66 321800
                l->priv = priv;
67 321800
                l->bufl = bufsize - 1;
68 321800
                l->buf = malloc(l->bufl + 1L);
69 321800
                if (l->buf == NULL) {
70 0
                        FREE_OBJ(l);
71 0
                        l = NULL;
72 0
                }
73 321800
        }
74 321800
        return (l);
75
}
76
77
void
78 125960
VLU_Reset(struct vlu *l)
79
{
80 125960
        CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
81 125960
        l->bufp = 0;
82 125960
}
83
84
void
85 320120
VLU_Destroy(struct vlu **lp)
86
{
87
        struct vlu *l;
88
89 320120
        TAKE_OBJ_NOTNULL(l, lp, LINEUP_MAGIC);
90 320120
        free(l->buf);
91 320120
        FREE_OBJ(l);
92 320120
}
93
94
static int
95 335817
LineUpProcess(struct vlu *l)
96
{
97
        char *p, *q;
98
        int i;
99
100 335817
        l->buf[l->bufp] = '\0';
101 3092256
        for (p = l->buf; *p != '\0'; p = q) {
102
                /* Find first CR or NL */
103 121644809
                for (q = p; *q != '\0'; q++) {
104 121586207
                        if (*q == '\n' || *q == '\r')
105 2756439
                                break;
106 118829768
                }
107 2815020
                if (*q == '\0')
108 58581
                        break;
109 2756439
                *q++ = '\0';
110 2756439
                i = l->func(l->priv, p);
111 2756439
                if (i != 0)
112 0
                        return (i);
113 2756439
        }
114 335817
        if (*p != '\0') {
115 58581
                q = strchr(p, '\0');
116 58581
                assert(q != NULL);
117 58581
                l->bufp = (unsigned)(q - p);
118 58581
                memmove(l->buf, p, l->bufp);
119 58581
                l->buf[l->bufp] = '\0';
120 58581
        } else
121 277236
                l->bufp = 0;
122 335817
        return (0);
123 335817
}
124
125
int
126 2263915
VLU_Fd(struct vlu *l, int fd)
127
{
128
        int i;
129
130 2263915
        CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
131 2263918
        i = read(fd, l->buf + l->bufp, l->bufl - l->bufp);
132 2263918
        if (i == 0)
133 1929330
                return (-2);
134 334604
        if (i < 0)
135 0
                return (-1);
136 334604
        l->bufp += i;
137 334604
        return (LineUpProcess(l));
138 2263932
}
139
140
int
141 136240
VLU_File(int fd, vlu_f *func, void *priv, unsigned bufsize)
142
{
143
        struct vlu *vlu;
144
        int i;
145
146 136240
        vlu = VLU_New(func, priv, bufsize);
147 136240
        AN(vlu);
148 136240
        do {
149 147400
                i = VLU_Fd(vlu, fd);
150 147400
        } while (i == 0);
151 136240
        VLU_Destroy(&vlu);
152 136240
        return (i);
153
}
154
155
int
156 1213
VLU_Feed(struct vlu *l, const char *ptr, int len)
157
{
158 1213
        int i = 0;
159
        unsigned u;
160
161 1213
        CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
162 1213
        AN(ptr);
163 1213
        assert(len > 0);
164 2426
        while (len > 0) {
165 1213
                u = len;
166 1213
                if (u > l->bufl - l->bufp)
167 0
                        u = l->bufl - l->bufp;
168 1213
                memcpy(l->buf + l->bufp, ptr, u);
169 1213
                len -= u;
170 1213
                ptr += u;
171 1213
                l->bufp += u;
172 1213
                i = LineUpProcess(l);
173 1213
                if (i)
174 0
                        return (i);
175
        }
176 1213
        return (i);
177 1213
}