summaryrefslogtreecommitdiff
path: root/src/buffer.c
blob: c30caf2a85fc8ff1e2c253588b8827a2b0982f42 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "buffer.h"

#include <unistd.h>

#include "byte.h"
#include "error.h"
#include "str.h"

/**
	@file buffer.c
	@author djb
	@brief input/output routines
*/

void buffer_init(buffer *s, ssize_t (*op)(), int fd, char *buf, size_t len)
{
  s->x = buf;
  s->fd = fd;
  s->op = op;
  s->p = 0;
  s->n = len;
}

ssize_t buffer_0_read(int fd, char *buf, size_t len)
{
  if (buffer_flush(buffer_1) == -1) return -1;
  return read(fd, buf, len);
}

char buffer_0_space[BUFFER_INSIZE];
static buffer it0 = BUFFER_INIT(buffer_0_read, 0, buffer_0_space, sizeof(buffer_0_space));
buffer *buffer_0 = &it0;

char buffer_1_space[BUFFER_OUTSIZE];
static buffer it1 = BUFFER_INIT(write, 1, buffer_1_space, sizeof(buffer_1_space));
buffer *buffer_1 = &it1;

char buffer_2_space[BUFFER_OUTSIZE];
static buffer it2 = BUFFER_INIT(write, 2, buffer_2_space, sizeof(buffer_2_space));
buffer *buffer_2 = &it2;

char buffer_0_small[BUFFER_SMALL];
static buffer is0 = BUFFER_INIT(buffer_0_read, 0, buffer_0_small, sizeof(buffer_0_small));
buffer *buffer_0small = &is0;

char buffer_1_small[BUFFER_SMALL];
static buffer is1 = BUFFER_INIT(write, 1, buffer_1_small, sizeof(buffer_1_small));
buffer *buffer_1small = &is1;

char buffer_2_small[BUFFER_SMALL];
static buffer is2 = BUFFER_INIT(write, 2, buffer_2_small, sizeof(buffer_2_small));
buffer *buffer_2small = &is2;

ssize_t buffer_unixread(int fd, char *buf, size_t len)
{
  return read(fd, buf, len);
}

ssize_t buffer_unixwrite(int fd, char *buf, size_t len)
{
  return write(fd, buf, len);
}

int buffer_copy(buffer *bout, buffer *bin)
{
  int n;
  char *x;

  for (;;) {
    n = buffer_feed(bin);
    if (n < 0) return -2;
    if (!n) return 0;
    x = buffer_PEEK(bin);
    if (buffer_put(bout, x, n) == -1) return -3;
    buffer_SEEK(bin, n);
  }
}

static int oneread(ssize_t (*op)(), int fd, char *buf, size_t len)
{
  int r;

  for (;;) {
    r = op(fd, buf, len);
    if (r == -1)
      if (errno == EINTR) continue;
    return r;
  }
}

static int getthis(buffer *s, char *buf, size_t len)
{
  if (len > s->p) len = s->p;
  s->p -= len;
  byte_copy(buf, len, s->x + s->n);
  s->n += len;
  return len;
}

int buffer_feed(buffer *s)
{
  int r;

  if (s->p) return s->p;
  r = oneread(s->op, s->fd, s->x, s->n);
  if (r <= 0) return r;
  s->p = r;
  s->n -= r;
  if (s->n > 0) byte_copyr(s->x + s->n, r, s->x);
  return r;
}

int buffer_bget(buffer *s, char *buf, size_t len)
{
  int r;

  if (s->p > 0) return getthis(s, buf, len);
  if (s->n <= len) return oneread(s->op, s->fd, buf, s->n);
  r = buffer_feed(s);
  if (r <= 0) return r;
  return getthis(s, buf, len);
}

int buffer_get(buffer *s, char *buf, size_t len)
{
  int r;

  if (s->p > 0) return getthis(s, buf, len);
  if (s->n <= len) return oneread(s->op, s->fd, buf, len);
  r = buffer_feed(s);
  if (r <= 0) return r;
  return getthis(s, buf, len);
}

char *buffer_peek(buffer *s)
{
  return s->x + s->n;
}

void buffer_seek(buffer *s, size_t len)
{
  s->n += len;
  s->p -= len;
}

static int allwrite(ssize_t (*op)(), int fd, const char *buf, size_t len)
{
  int w;

  while (len) {
    w = op(fd, buf, len);
    if (w == -1) {
      if (errno == EINTR) continue;
      return -1; /* note that some data may have been written */
    }
    if (w == 0) /* luser's fault */
      ;
    buf += w;
    len -= w;
  }
  return 0;
}

int buffer_flush(buffer *s)
{
  int p;

  p = s->p;
  if (!p) return 0;
  s->p = 0;
  return allwrite(s->op, s->fd, s->x, p);
}

int buffer_putalign(buffer *s, const char *buf, size_t len)
{
  unsigned int n;

  while (len > (n = s->n - s->p)) {
    byte_copy(s->x + s->p, n, buf);
    s->p += n;
    buf += n;
    len -= n;
    if (buffer_flush(s) == -1) return -1;
  }
  /* now len <= s->n - s->p */
  byte_copy(s->x + s->p, len, buf);
  s->p += len;
  return 0;
}

int buffer_put(buffer *s, const char *buf, size_t len)
{
  unsigned int n;

  n = s->n;
  if (len > n - s->p) {
    if (buffer_flush(s) == -1) return -1;
    /* now s->p == 0 */
    if (n < BUFFER_OUTSIZE) n = BUFFER_OUTSIZE;
    while (len > s->n) {
      if (n > len) n = len;
      if (allwrite(s->op, s->fd, buf, n) == -1) return -1;
      buf += n;
      len -= n;
    }
  }
  /* now len <= s->n - s->p */
  byte_copy(s->x + s->p, len, buf);
  s->p += len;
  return 0;
}

int buffer_putflush(buffer *s, const char *buf, size_t len)
{
  if (buffer_flush(s) == -1) return -1;
  return allwrite(s->op, s->fd, buf, len);
}

int buffer_putsalign(buffer *s, const char *buf)
{
  return buffer_putalign(s, buf, str_len(buf));
}

int buffer_puts(buffer *s, const char *buf)
{
  return buffer_put(s, buf, str_len(buf));
}

int buffer_putsflush(buffer *s, const char *buf)
{
  return buffer_putflush(s, buf, str_len(buf));
}