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