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 7134
VLU_New(vlu_f *func, void *priv, unsigned bufsize)
56
{
57
        struct vlu *l;
58
59 7134
        if (bufsize == 0)
60 7114
                bufsize = BUFSIZ;
61 7134
        ALLOC_OBJ(l, LINEUP_MAGIC);
62 7134
        if (l != NULL) {
63 7134
                l->func = func;
64 7134
                l->priv = priv;
65 7134
                l->bufl = bufsize - 1;
66 7134
                l->buf = malloc(l->bufl + 1L);
67 7134
                if (l->buf == NULL) {
68 0
                        FREE_OBJ(l);
69 0
                        l = NULL;
70
                }
71
        }
72 7134
        return (l);
73
}
74
75
void
76 7134
VLU_Destroy(struct vlu **lp)
77
{
78
        struct vlu *l;
79
80 7134
        AN(lp);
81 7134
        l = *lp;
82 7134
        *lp = NULL;
83 7134
        CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
84 7134
        free(l->buf);
85 7134
        FREE_OBJ(l);
86 7134
}
87
88
static int
89 8281
LineUpProcess(struct vlu *l)
90
{
91
        char *p, *q;
92
        int i;
93
94 8281
        l->buf[l->bufp] = '\0';
95 20031
        for (p = l->buf; *p != '\0'; p = q) {
96
                /* Find first CR or NL */
97 619197
                for (q = p; *q != '\0'; q++) {
98 618445
                        if (*q == '\n' || *q == '\r')
99
                                break;
100
                }
101 12502
                if (*q == '\0')
102 752
                        break;
103 11750
                *q++ = '\0';
104 11750
                i = l->func(l->priv, p);
105 11750
                if (i != 0)
106 0
                        return (i);
107
        }
108 8281
        if (*p != '\0') {
109 752
                q = strchr(p, '\0');
110 752
                assert(q != NULL);
111 752
                l->bufp = (unsigned)(q - p);
112 752
                memmove(l->buf, p, l->bufp);
113 752
                l->buf[l->bufp] = '\0';
114
        } else
115 7529
                l->bufp = 0;
116 8281
        return (0);
117
}
118
119
int
120 13918
VLU_Fd(struct vlu *l, int fd)
121
{
122
        int i;
123
124 13918
        CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
125 13918
        i = read(fd, l->buf + l->bufp, l->bufl - l->bufp);
126 13918
        if (i <= 0)
127 5718
                return (-1);
128 8200
        l->bufp += i;
129 8200
        return (LineUpProcess(l));
130
}
131
132
int
133 5702
VLU_File(int fd, vlu_f *func, void *priv, unsigned bufsize)
134
{
135
        struct vlu *vlu;
136
        int i;
137
138 5702
        vlu = VLU_New(func, priv, bufsize);
139 5702
        AN(vlu);
140
        do {
141 10429
                i = VLU_Fd(vlu, fd);
142 10429
        } while (i == 0);
143 5702
        VLU_Destroy(&vlu);
144 5702
        return (i);
145
}
146
147
int
148 81
VLU_Feed(struct vlu *l, const char *ptr, int len)
149
{
150 81
        int i = 0, ll;
151
152 81
        CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
153 81
        AN(ptr);
154 81
        assert(len > 0);
155 243
        while (len > 0) {
156 81
                ll = len;
157 81
                if (ll > l->bufl - l->bufp)
158 0
                        ll = l->bufl - l->bufp;
159 81
                memcpy(l->buf + l->bufp, ptr, ll);
160 81
                len -= ll;
161 81
                ptr += ll;
162 81
                l->bufp += ll;
163 81
                i = LineUpProcess(l);
164 81
                if (i)
165 0
                        return (i);
166
        }
167 81
        return (i);
168
}