-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.c
1296 lines (1156 loc) · 28.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 2003-2006, Maxime Henrion <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <zlib.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "misc.h"
#include "stream.h"
/*
* Simple stream API to make my life easier. If the fgetln() and
* funopen() functions were standard and if funopen() wasn't using
* wrong types for the function pointers, I could have just used
* stdio, but life sucks.
*
* For now, streams are always block-buffered.
*/
/*
* Flags passed to the flush methods.
*
* STREAM_FLUSH_CLOSING is passed during the last flush call before
* closing a stream. This allows the zlib filter to emit the EOF
* marker as appropriate. In all other cases, STREAM_FLUSH_NORMAL
* should be passed.
*
* These flags are completely unused in the default flush method,
* but they are very important for the flush method of the zlib
* filter.
*/
typedef enum {
STREAM_FLUSH_NORMAL,
STREAM_FLUSH_CLOSING
} stream_flush_t;
/*
* This is because buf_new() will always allocate size + 1 bytes,
* so our buffer sizes will still be power of 2 values.
*/
#define STREAM_BUFSIZ 1023
struct buf {
char *buf;
size_t size;
size_t in;
size_t off;
};
struct stream {
void *cookie;
int fd;
int buf;
struct buf *rdbuf;
struct buf *wrbuf;
stream_readfn_t *readfn;
stream_writefn_t *writefn;
stream_closefn_t *closefn;
int eof;
struct stream_filter *filter;
void *fdata;
#ifdef DEBUG
struct stream *log;
#endif
};
typedef int stream_filter_initfn_t(struct stream *, void *);
typedef void stream_filter_finifn_t(struct stream *);
typedef int stream_filter_flushfn_t(struct stream *, struct buf *,
stream_flush_t);
typedef ssize_t stream_filter_fillfn_t(struct stream *, struct buf *);
struct stream_filter {
stream_filter_t id;
stream_filter_initfn_t *initfn;
stream_filter_finifn_t *finifn;
stream_filter_fillfn_t *fillfn;
stream_filter_flushfn_t *flushfn;
};
/* Low-level buffer API. */
#define buf_avail(buf) ((buf)->size - (buf)->off - (buf)->in)
#define buf_count(buf) ((buf)->in)
#define buf_size(buf) ((buf)->size)
static void buf_more(struct buf *, size_t);
static void buf_less(struct buf *, size_t);
static void buf_grow(struct buf *, size_t);
/* Internal stream functions. */
static ssize_t stream_fill(struct stream *);
static ssize_t stream_fill_default(struct stream *, struct buf *);
static int stream_flush_int(struct stream *, stream_flush_t);
static int stream_flush_default(struct stream *, struct buf *,
stream_flush_t);
/* Filters specific functions. */
static struct stream_filter *stream_filter_lookup(stream_filter_t);
static int stream_filter_init(struct stream *, void *);
static void stream_filter_fini(struct stream *);
/* The zlib stream filter declarations. */
#define ZFILTER_EOF 1 /* Got Z_STREAM_END. */
struct zfilter {
int flags;
struct buf *rdbuf;
struct buf *wrbuf;
z_stream *rdstate;
z_stream *wrstate;
};
static int zfilter_init(struct stream *, void *);
static void zfilter_fini(struct stream *);
static ssize_t zfilter_fill(struct stream *, struct buf *);
static int zfilter_flush(struct stream *, struct buf *,
stream_flush_t);
/* The MD5 stream filter. */
struct md5filter {
MD5_CTX ctx;
char *md5;
char lastc;
#define PRINT 1
#define WS 2
#define STRING 3
#define SEEN 4
int state;
};
static int md5filter_init(struct stream *, void *);
static void md5filter_fini(struct stream *);
static ssize_t md5filter_fill(struct stream *, struct buf *);
static int md5filter_flush(struct stream *, struct buf *,
stream_flush_t);
static int md5rcsfilter_flush(struct stream *, struct buf *,
stream_flush_t);
/* The available stream filters. */
struct stream_filter stream_filters[] = {
{
STREAM_FILTER_NULL,
NULL,
NULL,
stream_fill_default,
stream_flush_default
},
{
STREAM_FILTER_ZLIB,
zfilter_init,
zfilter_fini,
zfilter_fill,
zfilter_flush
},
{
STREAM_FILTER_MD5,
md5filter_init,
md5filter_fini,
md5filter_fill,
md5filter_flush
},
{
STREAM_FILTER_MD5RCS,
md5filter_init,
md5filter_fini,
md5filter_fill,
md5rcsfilter_flush
}
};
/* Create a new buffer. */
struct buf *
buf_new(size_t size)
{
struct buf *buf;
buf = xmalloc(sizeof(struct buf));
/*
* We keep one spare byte so that stream_getln() can put a '\0'
* there in case the stream doesn't have an ending newline.
*/
buf->buf = xmalloc(size + 1);
memset(buf->buf, 0, size + 1);
buf->size = size;
buf->in = 0;
buf->off = 0;
return (buf);
}
/*
* Grow the size of the buffer. If "need" is 0, bump its size to the
* next power of 2 value. Otherwise, bump it to the next power of 2
* value bigger than "need".
*/
static void
buf_grow(struct buf *buf, size_t need)
{
if (need == 0)
buf->size = buf->size * 2 + 1; /* Account for the spare byte. */
else {
assert(need > buf->size);
while (buf->size < need)
buf->size = buf->size * 2 + 1;
}
buf->buf = xrealloc(buf->buf, buf->size + 1);
}
/* Make more room in the buffer if needed. */
static void
buf_prewrite(struct buf *buf)
{
if (buf_count(buf) == buf_size(buf))
buf_grow(buf, 0);
if (buf_count(buf) > 0 && buf_avail(buf) == 0) {
memmove(buf->buf, buf->buf + buf->off, buf_count(buf));
buf->off = 0;
}
}
/* Account for "n" bytes being added in the buffer. */
static void
buf_more(struct buf *buf, size_t n)
{
assert(n <= buf_avail(buf));
buf->in += n;
}
/* Account for "n" bytes having been read in the buffer. */
static void
buf_less(struct buf *buf, size_t n)
{
assert(n <= buf_count(buf));
buf->in -= n;
if (buf->in == 0)
buf->off = 0;
else
buf->off += n;
}
/* Free a buffer. */
void
buf_free(struct buf *buf)
{
free(buf->buf);
free(buf);
}
static struct stream *
stream_new(stream_readfn_t *readfn, stream_writefn_t *writefn,
stream_closefn_t *closefn)
{
struct stream *stream;
stream = xmalloc(sizeof(struct stream));
if (readfn == NULL && writefn == NULL) {
errno = EINVAL;
return (NULL);
}
if (readfn != NULL)
stream->rdbuf = buf_new(STREAM_BUFSIZ);
else
stream->rdbuf = NULL;
if (writefn != NULL)
stream->wrbuf = buf_new(STREAM_BUFSIZ);
else
stream->wrbuf = NULL;
stream->cookie = NULL;
stream->fd = -1;
stream->buf = 0;
stream->readfn = readfn;
stream->writefn = writefn;
stream->closefn = closefn;
stream->filter = stream_filter_lookup(STREAM_FILTER_NULL);
stream->fdata = NULL;
stream->eof = 0;
#ifdef DEBUG
stream->log = NULL;
#endif
return (stream);
}
/* Create a new stream associated with a void *. */
struct stream *
stream_open(void *cookie, stream_readfn_t *readfn, stream_writefn_t *writefn,
stream_closefn_t *closefn)
{
struct stream *stream;
stream = stream_new(readfn, writefn, closefn);
stream->cookie = cookie;
return (stream);
}
/* Associate a file descriptor with a stream. */
struct stream *
stream_open_fd(int fd, stream_readfn_t *readfn, stream_writefn_t *writefn,
stream_closefn_t *closefn)
{
struct stream *stream;
stream = stream_new(readfn, writefn, closefn);
stream->cookie = &stream->fd;
stream->fd = fd;
return (stream);
}
/* Associate a buf with a stream. */
struct stream *
stream_open_buf(struct buf *b)
{
struct stream *stream;
stream = stream_new(stream_read_buf, stream_append_buf, stream_close_buf);
stream->cookie = b;
stream->buf = 1;
b->in = 0;
return (stream);
}
/*
* Truncate a buffer, just decrease offset pointer.
* XXX: this can be dangerous if not used correctly.
*/
void
stream_truncate_buf(struct buf *b, off_t off)
{
b->off += off;
}
/* Like open() but returns a stream. */
struct stream *
stream_open_file(const char *path, int flags, ...)
{
struct stream *stream;
stream_readfn_t *readfn;
stream_writefn_t *writefn;
va_list ap;
mode_t mode;
int fd;
va_start(ap, flags);
if (flags & O_CREAT) {
/*
* GCC says I should not be using mode_t here since it's
* promoted to an int when passed through `...'.
*/
mode = va_arg(ap, int);
fd = open(path, flags, mode);
} else
fd = open(path, flags);
va_end(ap);
if (fd == -1)
return (NULL);
flags &= O_ACCMODE;
if (flags == O_RDONLY) {
readfn = stream_read_fd;
writefn = NULL;
} else if (flags == O_WRONLY) {
readfn = NULL;
writefn = stream_write_fd;
} else if (flags == O_RDWR) {
assert(flags == O_RDWR);
readfn = stream_read_fd;
writefn = stream_write_fd;
} else {
errno = EINVAL;
close(fd);
return (NULL);
}
stream = stream_open_fd(fd, readfn, writefn, stream_close_fd);
if (stream == NULL)
close(fd);
return (stream);
}
/* Return the file descriptor associated with this stream, or -1. */
int
stream_fileno(struct stream *stream)
{
return (stream->fd);
}
/* Convenience read function for character buffers. */
ssize_t
stream_read_buf(void *cookie, void *buf, size_t size)
{
struct buf *b;
size_t avail;
/* Use in to be read offset. */
b = (struct buf *)cookie;
/* Just return what we have if the request is to large. */
avail = b->off - b->in;
if (avail < size) {
memcpy(buf, b->buf + b->in, avail);
b->in += avail;
return (avail);
}
memcpy(buf, b->buf + b->in, size);
b->in += size;
return (size);
}
/* Convenience write function for appending character buffers. */
ssize_t
stream_append_buf(void *cookie, const void *buf, size_t size)
{
struct buf *b;
size_t avail;
/* Use off to be write offset. */
b = (struct buf *)cookie;
avail = b->size - b->off;
if (size > avail)
buf_grow(b, b->size + size);
memcpy(b->buf + b->off, buf, size);
b->off += size;
b->buf[b->off] = '\0';
return (size);
}
/* Convenience close function for freeing character buffers. */
int
stream_close_buf(void *cookie)
{
void *data;
data = cookie;
/* Basically a NOP. */
return (0);
}
/* Convenience read function for file descriptors. */
ssize_t
stream_read_fd(void *cookie, void *buf, size_t size)
{
ssize_t nbytes;
int fd;
fd = *(int *)cookie;
nbytes = read(fd, buf, size);
return (nbytes);
}
/* Convenience write function for file descriptors. */
ssize_t
stream_write_fd(void *cookie, const void *buf, size_t size)
{
ssize_t nbytes;
int fd;
fd = *(int *)cookie;
nbytes = write(fd, buf, size);
return (nbytes);
}
/* Convenience close function for file descriptors. */
int
stream_close_fd(void *cookie)
{
int fd, ret;
fd = *(int *)cookie;
ret = close(fd);
return (ret);
}
/* Read some bytes from the stream. */
ssize_t
stream_read(struct stream *stream, void *buf, size_t size)
{
struct buf *rdbuf;
ssize_t ret;
size_t n;
rdbuf = stream->rdbuf;
if (buf_count(rdbuf) == 0) {
ret = stream_fill(stream);
if (ret <= 0)
return (-1);
}
n = min(size, buf_count(rdbuf));
memcpy(buf, rdbuf->buf + rdbuf->off, n);
buf_less(rdbuf, n);
return (n);
}
/*
* Read a line from the stream and return a pointer to it.
*
* If "len" is non-NULL, the length of the string will be put into it.
* The pointer is only valid until the next stream API call. The line
* can be modified by the caller, provided he doesn't write before or
* after it.
*
* This is somewhat similar to the BSD fgetln() function, except that
* "len" can be NULL here. In that case the string is terminated by
* overwriting the '\n' character with a NUL character. If it's the
* last line in the stream and it has no ending newline, we can still
* add '\0' after it, because we keep one spare byte in the buffers.
*
* However, be warned that one can't handle binary lines properly
* without knowing the size of the string since those can contain
* NUL characters.
*/
char *
stream_getln(struct stream *stream, size_t *len)
{
struct buf *buf;
char *cp, *line;
ssize_t n;
size_t done, size;
buf = stream->rdbuf;
if (buf_count(buf) == 0) {
n = stream_fill(stream);
if (n <= 0)
return (NULL);
}
cp = memchr(buf->buf + buf->off, '\n', buf_count(buf));
for (done = buf_count(buf); cp == NULL; done += n) {
n = stream_fill(stream);
if (n < 0)
return (NULL);
if (n == 0)
/* Last line of the stream. */
cp = buf->buf + buf->off + buf->in - 1;
else
cp = memchr(buf->buf + buf->off + done, '\n',
buf_count(buf) - done);
}
line = buf->buf + buf->off;
assert(cp >= line);
size = cp - line + 1;
buf_less(buf, size);
if (len != NULL) {
*len = size;
} else {
/* Terminate the string when len == NULL. */
if (line[size - 1] == '\n')
line[size - 1] = '\0';
else
line[size] = '\0';
}
return (line);
}
/* Write some bytes to a stream. */
ssize_t
stream_write(struct stream *stream, const void *src, size_t nbytes)
{
struct buf *buf;
int error;
buf = stream->wrbuf;
if (nbytes > buf_size(buf))
buf_grow(buf, nbytes);
if (nbytes > buf_avail(buf)) {
error = stream_flush_int(stream, STREAM_FLUSH_NORMAL);
if (error)
return (-1);
}
memcpy(buf->buf + buf->off + buf->in, src, nbytes);
buf_more(buf, nbytes);
return (nbytes);
}
#ifdef DEBUG
int
stream_log(struct stream *stream, const char *path)
{
struct stream *log;
assert(stream->log == NULL);
log = stream_open_file(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (log == NULL)
return (-1);
stream->log = log;
return (0);
}
#endif
/* Formatted output to a stream. */
int
stream_printf(struct stream *stream, const char *fmt, ...)
{
struct buf *buf;
va_list ap;
int error, ret;
buf = stream->wrbuf;
again:
va_start(ap, fmt);
ret = vsnprintf(buf->buf + buf->off + buf->in, buf_avail(buf), fmt, ap);
va_end(ap);
if (ret < 0)
return (ret);
if ((unsigned)ret >= buf_avail(buf)) {
if ((unsigned)ret >= buf_size(buf))
buf_grow(buf, ret + 1);
if ((unsigned)ret >= buf_avail(buf)) {
error = stream_flush_int(stream, STREAM_FLUSH_NORMAL);
if (error)
return (-1);
}
goto again;
}
buf_more(buf, ret);
return (ret);
}
/* Flush the entire write buffer of the stream. */
int
stream_flush(struct stream *stream)
{
int error;
error = stream_flush_int(stream, STREAM_FLUSH_NORMAL);
return (error);
}
/* Internal flush API. */
static int
stream_flush_int(struct stream *stream, stream_flush_t how)
{
struct buf *buf;
int error;
buf = stream->wrbuf;
#ifdef DEBUG
if (stream->log != NULL)
stream_write(stream->log, buf->buf + buf->off, buf_count(buf));
#endif
error = (*stream->filter->flushfn)(stream, buf, how);
return (error);
}
/* The default flush method. */
static int
stream_flush_default(struct stream *stream, struct buf *buf,
stream_flush_t __unused how)
{
ssize_t n;
while (buf_count(buf) > 0) {
do {
n = (*stream->writefn)(stream->cookie,
buf->buf + buf->off, buf_count(buf));
} while (n == -1 && errno == EINTR);
if (n <= 0)
return (-1);
buf_less(buf, n);
}
return (0);
}
/* Flush the write buffer and call fsync() on the file descriptor. */
int
stream_sync(struct stream *stream)
{
int error;
if (stream->fd == -1) {
errno = EINVAL;
return (-1);
}
error = stream_flush_int(stream, STREAM_FLUSH_NORMAL);
if (error)
return (-1);
error = fsync(stream->fd);
return (error);
}
/* Like truncate() but on a stream. */
int
stream_truncate(struct stream *stream, off_t size)
{
int error;
if (stream->fd == -1) {
errno = EINVAL;
return (-1);
}
error = stream_flush_int(stream, STREAM_FLUSH_NORMAL);
if (error)
return (-1);
error = ftruncate(stream->fd, size);
return (error);
}
/* Like stream_truncate() except the off_t parameter is an offset. */
int
stream_truncate_rel(struct stream *stream, off_t off)
{
struct stat sb;
int error;
if (stream->buf) {
stream_truncate_buf(stream->cookie, off);
return (0);
}
if (stream->fd == -1) {
errno = EINVAL;
return (-1);
}
error = stream_flush_int(stream, STREAM_FLUSH_NORMAL);
if (error)
return (-1);
error = fstat(stream->fd, &sb);
if (error)
return (-1);
error = stream_truncate(stream, sb.st_size + off);
return (error);
}
/* Rewind the stream. */
int
stream_rewind(struct stream *stream)
{
int error;
if (stream->fd == -1) {
errno = EINVAL;
return (-1);
}
if (stream->rdbuf != NULL)
buf_less(stream->rdbuf, buf_count(stream->rdbuf));
if (stream->wrbuf != NULL) {
error = stream_flush_int(stream, STREAM_FLUSH_NORMAL);
if (error)
return (error);
}
error = lseek(stream->fd, 0, SEEK_SET);
return (error);
}
/* Return EOF status. */
int
stream_eof(struct stream *stream)
{
return (stream->eof);
}
/* Close a stream and free any resources held by it. */
int
stream_close(struct stream *stream)
{
int error;
if (stream == NULL)
return (0);
error = 0;
if (stream->wrbuf != NULL)
error = stream_flush_int(stream, STREAM_FLUSH_CLOSING);
stream_filter_fini(stream);
if (stream->closefn != NULL)
/*
* We might overwrite a previous error from stream_flush(),
* but we have no choice, because wether it had worked or
* not, we need to close the file descriptor.
*/
error = (*stream->closefn)(stream->cookie);
if (stream->rdbuf != NULL)
buf_free(stream->rdbuf);
if (stream->wrbuf != NULL)
buf_free(stream->wrbuf);
#ifdef DEBUG
if (stream->log != NULL)
stream_close(stream->log);
#endif
free(stream);
return (error);
}
/* The default fill method. */
static ssize_t
stream_fill_default(struct stream *stream, struct buf *buf)
{
ssize_t n;
if (stream->eof)
return (0);
assert(buf_avail(buf) > 0);
n = (*stream->readfn)(stream->cookie, buf->buf + buf->off + buf->in,
buf_avail(buf));
if (n < 0)
return (-1);
if (n == 0) {
stream->eof = 1;
return (0);
}
buf_more(buf, n);
return (n);
}
/*
* Refill the read buffer. This function is not permitted to return
* without having made more bytes available, unless there was an error.
* Moreover, stream_fill() returns the number of bytes added.
*/
static ssize_t
stream_fill(struct stream *stream)
{
struct stream_filter *filter;
struct buf *buf;
#ifndef NDEBUG
size_t oldcount;
#endif
ssize_t n;
filter = stream->filter;
buf = stream->rdbuf;
buf_prewrite(buf);
#ifndef NDEBUG
oldcount = buf_count(buf);
#endif
n = (*filter->fillfn)(stream, buf);
#ifdef DEBUG
if (stream->log != NULL && n > 0)
stream_write(stream->log, buf->buf + buf->off, n);
#endif
assert((n > 0 && n == (signed)(buf_count(buf) - oldcount)) ||
(n <= 0 && buf_count(buf) == oldcount));
return (n);
}
/*
* Lookup a stream filter.
*
* We are not supposed to get passed an invalid filter id, since
* filter ids are an enum type and we don't have invalid filter
* ids in the enum :-). Thus, we are not checking for out of
* bounds access here. If it happens, it's the caller's fault
* anyway.
*/
static struct stream_filter *
stream_filter_lookup(stream_filter_t id)
{
struct stream_filter *filter;
filter = stream_filters;
while (filter->id != id)
filter++;
return (filter);
}
static int
stream_filter_init(struct stream *stream, void *data)
{
struct stream_filter *filter;
int error;
filter = stream->filter;
if (filter->initfn == NULL)
return (0);
error = (*filter->initfn)(stream, data);
return (error);
}
static void
stream_filter_fini(struct stream *stream)
{
struct stream_filter *filter;
filter = stream->filter;
if (filter->finifn != NULL)
(*filter->finifn)(stream);
}
/*
* Start a filter on a stream.
*/
int
stream_filter_start(struct stream *stream, stream_filter_t id, void *data)
{
struct stream_filter *filter;
int error;
filter = stream->filter;
if (id == filter->id)
return (0);
stream_filter_fini(stream);
stream->filter = stream_filter_lookup(id);
stream->fdata = NULL;
error = stream_filter_init(stream, data);
return (error);
}
/* Stop a filter, this is equivalent to setting the null filter. */
void
stream_filter_stop(struct stream *stream)
{
stream_filter_start(stream, STREAM_FILTER_NULL, NULL);
}
/* The zlib stream filter implementation. */
/* Take no chances with zlib... */
static void *
zfilter_alloc(void __unused *opaque, unsigned int items, unsigned int size)
{
return (xmalloc(items * size));
}
static void
zfilter_free(void __unused *opaque, void *ptr)
{
free(ptr);
}
static int
zfilter_init(struct stream *stream, void __unused *data)
{
struct zfilter *zf;
struct buf *buf;
z_stream *state;
int rv;
zf = xmalloc(sizeof(struct zfilter));
memset(zf, 0, sizeof(struct zfilter));
if (stream->rdbuf != NULL) {
state = xmalloc(sizeof(z_stream));
state->zalloc = zfilter_alloc;
state->zfree = zfilter_free;
state->opaque = Z_NULL;
rv = inflateInit(state);
if (rv != Z_OK)
errx(1, "inflateInit: %s", state->msg);
buf = buf_new(buf_size(stream->rdbuf));
zf->rdbuf = stream->rdbuf;
stream->rdbuf = buf;
zf->rdstate = state;
}
if (stream->wrbuf != NULL) {
state = xmalloc(sizeof(z_stream));
state->zalloc = zfilter_alloc;
state->zfree = zfilter_free;
state->opaque = Z_NULL;
rv = deflateInit(state, Z_DEFAULT_COMPRESSION);
if (rv != Z_OK)
errx(1, "deflateInit: %s", state->msg);
buf = buf_new(buf_size(stream->wrbuf));
zf->wrbuf = stream->wrbuf;
stream->wrbuf = buf;
zf->wrstate = state;
}
stream->fdata = zf;
return (0);
}