-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.c
471 lines (397 loc) · 10.5 KB
/
stream.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "stream.h"
/* the macros below are copied from gnulib and are licensed under lgpl */
/* True if the arithmetic type T is signed. */
#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
/* Minimum and maximum values for integer types and expressions. These
macros have undefined behavior if T is signed and has padding bits.
If this is a problem for you, please let us know how to fix it for
your host. */
/* The maximum and minimum values for the integer type T. */
#define TYPE_MINIMUM(t) \
((t) (! TYPE_SIGNED (t) \
? (t) 0 \
: TYPE_SIGNED_MAGNITUDE (t) \
? ~ (t) 0 \
: ~ TYPE_MAXIMUM (t)))
#define TYPE_MAXIMUM(t) \
((t) (! TYPE_SIGNED (t) \
? (t) -1 \
: ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1)))
#define OFF_T_MAX TYPE_MAXIMUM (off_t)
typedef enum {
RAZOR2_STREAM_NONE = 0,
RAZOR2_STREAM_FILE,
RAZOR2_STREAM_MEM,
RAZOR2_STREAM_PIPE
} razor2_stream_type_t;
typedef struct {
char fn[PATH_MAX+1];
int fd;
} razor2_stream_file_t;
typedef struct {
char *buf;
size_t len;
size_t pos;
} razor2_stream_mem_t;
typedef ssize_t(*razor2_stream_read_t)(void *, size_t, razor2_stream_t *);
typedef int(*razor2_stream_seek_t)(razor2_stream_t *, off_t, int);
typedef off_t(*razor2_stream_tell_t)(razor2_stream_t *);
typedef int(*razor2_stream_close_t)(razor2_stream_t *);
struct _razor2_stream {
razor2_stream_type_t type; /* stream type */
int eof; /* end of file */
int err; /* error number */
razor2_stream_read_t read;
razor2_stream_seek_t seek;
razor2_stream_tell_t tell;
razor2_stream_close_t close;
union {
razor2_stream_file_t file;
razor2_stream_mem_t mem;
} data;
};
static ssize_t razor2_stream_file_read (void *, size_t, razor2_stream_t *);
static int razor2_stream_file_seek (razor2_stream_t *, off_t, int);
static off_t razor2_stream_file_tell (razor2_stream_t *);
static int razor2_stream_file_close (razor2_stream_t *);
static ssize_t razor2_stream_mem_read (void *, size_t, razor2_stream_t *);
static int razor2_stream_mem_seek (razor2_stream_t *, off_t, int);
static off_t razor2_stream_mem_tell (razor2_stream_t *);
static int razor2_stream_mem_close (razor2_stream_t *);
static int razor2_stream_pipe_close (razor2_stream_t *);
int
razor2_stream_file (razor2_stream_t **stm, const char *fn)
{
int err;
razor2_stream_t *ptr;
assert (stm);
assert (fn);
if (! (ptr = calloc (1, sizeof (razor2_stream_t))))
goto error;
if (! (realpath (fn, ptr->data.file.fn)))
goto error;
if ((ptr->data.file.fd = open (ptr->data.file.fn, O_RDONLY)) < 0)
goto error;
ptr->type = RAZOR2_STREAM_FILE;
ptr->read = &razor2_stream_file_read;
ptr->seek = &razor2_stream_file_seek;
ptr->tell = &razor2_stream_file_tell;
ptr->close = &razor2_stream_file_close;
*stm = ptr;
return (0);
error:
err = errno;
if (ptr)
free (ptr);
return (err);
}
int
razor2_stream_mem (razor2_stream_t **stm, const char *str, size_t len)
{
razor2_stream_t *ptr;
assert (stm);
assert (str);
if (! (ptr = calloc (1, sizeof (razor2_stream_t)))) {
return (errno);
}
ptr->type = RAZOR2_STREAM_MEM;
ptr->read = &razor2_stream_mem_read;
ptr->seek = &razor2_stream_mem_seek;
ptr->tell = &razor2_stream_mem_tell;
ptr->close = &razor2_stream_mem_close;
ptr->data.mem.buf = (char *)str;
ptr->data.mem.len = len;
*stm = ptr;
return (0);
}
/* pipe functionality is emulated by reading everything from the file
descriptor and storing it in memory to make it seekable. doing that here
saves us a lot of work in the upper layers */
#define RAZOR2_BLOCK_SIZE 4096
int
razor2_stream_pipe (razor2_stream_t **stm, int fd)
{
int err, loop;
char *alloc_str, *str;
size_t alloc_len, len, pos;
ssize_t cnt;
assert (stm);
str = NULL;
len = 0;
pos = 0;
for (loop = 1; loop;) {
again:
/* allocate more memory if needed */
if (pos >= len) {
/* calculate if amount of memory is withing limits */
if (len > (SIZE_MAX - (RAZOR2_BLOCK_SIZE + 1))) {
err = ENOMEM;
goto error;
}
alloc_len = len + RAZOR2_BLOCK_SIZE;
/* allocate memory */
alloc_str = realloc (str, alloc_len + 1);
if (! alloc_str) {
err = errno;
goto error;
}
str = alloc_str;
len = alloc_len;
/* zero out newly allocated memory */
memset (str + pos, '\0', (len - pos) + 1);
goto again;
}
err = errno;
errno = 0;
cnt = read (fd, str+pos, len-pos);
if (cnt > 0) {
pos += (size_t) cnt;
}
if (pos < len) {
if (errno && errno != EINTR) {
err = errno;
goto error;
} else {
/* end of file */
loop = 0;
}
}
errno = err;
}
if ((err = razor2_stream_mem (stm, str, pos)) == 0) {
(*stm)->type = RAZOR2_STREAM_PIPE;
(*stm)->close = &razor2_stream_pipe_close;
return (0);
}
error:
if (str)
free (str);
return (err);
}
#undef RAZOR2_BLOCK_SIZE
static ssize_t
razor2_stream_file_read (void *buf, size_t len, razor2_stream_t *stm)
{
assert (buf);
assert (stm);
assert (stm->type == RAZOR2_STREAM_FILE);
int err, loop;
size_t pos;
ssize_t cnt;
err = errno;
for (loop = 1, cnt = 0, pos = 0; loop && pos < len; ) {
errno = 0;
cnt = read (stm->data.file.fd, buf + pos, len - pos);
/* increase position */
if (cnt > 0) {
pos += (size_t)cnt;
}
/* evaluate status */
if (pos < len) {
if (errno == 0) {
stm->eof = 1;
loop = 0;
} else if (errno != EINTR) {
stm->err = errno;
loop = 0;
}
}
}
cnt = len - pos;
if (stm->err) {
if (cnt < 1) {
cnt = -1;
}
} else {
errno = err;
}
return (cnt);
}
static ssize_t
razor2_stream_mem_read (void *buf, size_t len, razor2_stream_t *stm)
{
assert (buf);
assert (stm);
assert (stm->type == RAZOR2_STREAM_MEM ||
stm->type == RAZOR2_STREAM_PIPE);
/* end of stream */
if (stm->data.mem.pos >= stm->data.mem.len) {
stm->eof = 1;
return (0);
}
if (len > (stm->data.mem.len - stm->data.mem.pos))
len = stm->data.mem.len - stm->data.mem.pos;
memcpy (buf, stm->data.mem.buf + stm->data.mem.pos, len);
stm->data.mem.pos += len;
if (stm->data.mem.pos >= stm->data.mem.len)
stm->eof = 1;
return ((ssize_t) len);
}
ssize_t
razor2_stream_read (void *buf, size_t len, razor2_stream_t *stm)
{
assert (stm && stm->read);
return stm->read (buf, len, stm);
}
static int
razor2_stream_file_seek (razor2_stream_t *stm, off_t off, int src)
{
off_t ret;
assert (stm);
assert (stm->type == RAZOR2_STREAM_FILE);
ret = lseek (stm->data.file.fd, off, src);
if (ret != 0)
stm->err = errno;
return (ret);
}
static int
razor2_stream_mem_seek (razor2_stream_t *stm, off_t off, int src)
{
size_t max;
assert (stm);
assert (stm->type == RAZOR2_STREAM_MEM ||
stm->type == RAZOR2_STREAM_PIPE);
switch (src) {
/* position is n bytes from start */
case SEEK_SET:
if (off < 0 || (uintmax_t)off >= (uintmax_t)stm->data.mem.len)
goto beyond;
stm->data.mem.pos = (size_t)off;
break;
/* position is n bytes from current offset */
case SEEK_CUR:
if (off > 0) {
max = stm->data.mem.len - stm->data.mem.pos;
if ((uintmax_t)off >= (uintmax_t)max)
goto beyond;
stm->data.mem.pos += (size_t)off;
} else if (off < 0) {
if ((uintmax_t)(off * -1) > (uintmax_t)stm->data.mem.pos)
goto limit;
stm->data.mem.pos -= (size_t)(off * -1);
}
break;
/* position is n bytes from end */
case SEEK_END:
if (off > 0 || (uintmax_t)(off * -1) > (uintmax_t)stm->data.mem.len)
goto limit;
stm->data.mem.pos = stm->data.mem.len - (size_t)(off * -1);
break;
default:
errno = EINVAL;
stm->err = errno;
return (-1);
}
return (0);
beyond:
errno = EFBIG;
stm->err = errno;
return (-1);
limit:
errno = EINVAL;
stm->err = errno;
return (-1);
}
/* move stream offset */
int
razor2_stream_seek (razor2_stream_t *stm, off_t off, int src)
{
assert (stm && stm->seek);
return stm->seek (stm, off, src);
}
static off_t
razor2_stream_file_tell (razor2_stream_t *stm)
{
assert (stm);
assert (stm->type == RAZOR2_STREAM_FILE);
return lseek (stm->data.file.fd, 0, SEEK_CUR);
}
static off_t
razor2_stream_mem_tell (razor2_stream_t *stm)
{
assert (stm);
assert (stm->type == RAZOR2_STREAM_MEM ||
stm->type == RAZOR2_STREAM_PIPE);
if ((uintmax_t)stm->data.mem.pos > (uintmax_t)OFF_T_MAX) {
errno = EOVERFLOW;
stm->err = errno;
return (-1);
}
return (off_t)stm->data.mem.pos;
}
/* return stream offset */
off_t
razor2_stream_tell (razor2_stream_t *stm)
{
assert (stm && stm->tell);
return stm->tell (stm);
}
int razor2_stream_eof (razor2_stream_t *);
int razor2_stream_error (razor2_stream_t *);
static int
razor2_stream_file_close (razor2_stream_t *stm)
{
int err;
assert (stm);
assert (stm->type == RAZOR2_STREAM_FILE);
err = 0;
if (stm && stm->type == RAZOR2_STREAM_FILE) {
if (stm->data.file.fd >= 0 && (err = close (stm->data.file.fd)) != 0) {
stm->err = errno;
return (-1);
}
free (stm);
}
return (0);
}
static int
razor2_stream_mem_close (razor2_stream_t *stm)
{
assert (stm);
assert (stm->type == RAZOR2_STREAM_MEM);
if (stm && stm->type == RAZOR2_STREAM_MEM) {
free (stm);
}
return (0);
}
static int
razor2_stream_pipe_close (razor2_stream_t *stm)
{
assert (stm);
assert (stm->type == RAZOR2_STREAM_PIPE);
if (stm && stm->type == RAZOR2_STREAM_PIPE) {
if (stm->data.mem.buf)
free (stm->data.mem.buf);
free (stm);
}
return (0);
}
int
razor2_stream_close (razor2_stream_t *stm)
{
assert (stm && stm->close);
return stm->close (stm);
}
int
razor2_stream_error (razor2_stream_t *stm)
{
assert (stm);
if (stm && stm->err)
return (stm->err);
return (0);
}
int
razor2_stream_eof (razor2_stream_t *stm)
{
assert (stm);
return stm->eof ? 1 : 0;
}