-
Notifications
You must be signed in to change notification settings - Fork 0
/
linebuf.c
120 lines (99 loc) · 2.17 KB
/
linebuf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Read arbitrary length data in optimal chunks and split to lines
* as separated by CRLF or LF.
*
* For example:
* lb = linebuf_create();
*
* while (linebuf_fill_from_tls(lb, tclient) > 0)
* while ((s = linebuf_read(lb)) != NULL)
* puts(s);
*
* linebuf_free(lb);
*/
#include "linebuf.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <tls.h>
#define READ_BUFFER_ALLOC 4096
#define READ_BUFFER_CHUNK 512
struct linebuf
{
size_t alloc; /* memory allocated for the buffer */
size_t chunk; /* read chunk size */
size_t sz; /* bytes currently in a line */
size_t shrink; /* shrink buffer by n bytes from beginning */
char *buf; /* line data */
};
static void linebuf_make_space(struct linebuf *);
struct linebuf *
linebuf_create()
{
struct linebuf *line;
line = calloc(1, sizeof(struct linebuf));
if (line == NULL)
err(1, "calloc");
line->alloc = READ_BUFFER_ALLOC;
line->chunk = READ_BUFFER_CHUNK;
line->sz = 0;
line->shrink = 0;
if ((line->buf = malloc(line->alloc)) == NULL)
err(1, "init_tls_line_buffer");
return line;
}
void
linebuf_free(struct linebuf *line)
{
if (line->buf != NULL)
free(line->buf);
free(line);
}
static void
linebuf_make_space(struct linebuf *line)
{
if (line->sz >= line->alloc - line->chunk) {
line->alloc *= 2;
line->buf = realloc(line->buf, line->alloc);
}
}
int
linebuf_fill_from_tls(struct linebuf *line, struct tls *tclient)
{
int n;
linebuf_make_space(line);
do {
n = tls_read(tclient, &line->buf[line->sz], line->chunk);
} while (n == TLS_WANT_POLLIN);
if (n < 0)
errx(1, "TLS read failed: %s", tls_error(tclient));
if (n == 0)
return 0;
line->sz += n;
return n;
}
char *
linebuf_read(struct linebuf *line)
{
size_t i;
if (line->shrink > 0) {
line->sz -= line->shrink;
if (line->sz >= 1)
memmove(line->buf, &line->buf[line->shrink], line->sz);
else
line->sz = 0;
line->shrink = 0;
line->buf[line->sz] = '\0';
}
for (i = 0; i < line->sz; i++) {
if (line->buf[i] != '\n')
continue;
if (i > 0 && line->buf[i-1] == '\r')
line->buf[i-1] = '\0';
line->buf[i] = '\0';
line->shrink = (i + 1);
return line->buf;
}
return NULL;
}