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