-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiolayer.c
2024 lines (1535 loc) · 44.5 KB
/
iolayer.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
#define IMAGER_NO_CONTEXT
#include "imager.h"
#include "iolayer.h"
#include "imerror.h"
#include "log.h"
#include <stdlib.h>
#include <stdio.h>
#ifdef _MSC_VER
#include <io.h>
#endif
#include <string.h>
#include <errno.h>
#include "imageri.h"
#define IOL_DEB(x)
#define IOL_DEBs stderr
#define IO_BUF_SIZE 8192
char *io_type_names[] = { "FDSEEK", "FDNOSEEK", "BUFFER", "CBSEEK", "CBNOSEEK", "BUFCHAIN" };
typedef struct io_blink {
char buf[BBSIZ];
/* size_t cnt; */
size_t len; /* How large is this buffer = BBZIS for now */
struct io_blink *next;
struct io_blink *prev;
} io_blink;
typedef struct {
i_io_glue_t base;
int fd;
} io_fdseek;
typedef struct {
i_io_glue_t base;
const char *data;
size_t len;
i_io_closebufp_t closecb; /* free memory mapped segment or decrement refcount */
void *closedata;
off_t cpos;
} io_buffer;
typedef struct {
i_io_glue_t base;
void *p; /* Callback data */
i_io_readl_t readcb;
i_io_writel_t writecb;
i_io_seekl_t seekcb;
i_io_closel_t closecb;
i_io_destroyl_t destroycb;
} io_cb;
typedef struct {
off_t offset; /* Offset of the source - not used */
off_t length; /* Total length of chain in bytes */
io_blink *head; /* Start of chain */
io_blink *tail; /* End of chain */
off_t tfill; /* End of stream in last link */
io_blink *cp; /* Current element of list */
off_t cpos; /* Offset within the current */
off_t gpos; /* Global position in stream */
} io_ex_bchain;
/* turn current offset, file length, whence and offset into a new offset */
#define calc_seek_offset(curr_off, length, offset, whence) \
(((whence) == SEEK_SET) ? (offset) : \
((whence) == SEEK_CUR) ? (curr_off) + (offset) : \
((whence) == SEEK_END) ? (length) + (offset) : -1)
/*
=head1 NAME
iolayer.c - encapsulates different source of data into a single framework.
=head1 SYNOPSIS
io_glue *ig = io_new_fd( fileno(stdin) );
method = io_reqmeth( IOL_NOSEEK | IOL_MMAP ); // not implemented yet
switch (method) {
case IOL_NOSEEK:
code that uses ig->readcb()
to read data goes here.
break;
case IOL_MMAP:
code that uses ig->readcb()
to read data goes here.
break;
}
io_glue_destroy(ig);
// and much more
=head1 DESCRIPTION
iolayer.c implements the basic functions to create and destroy io_glue
objects for Imager. The typical usage pattern for data sources is:
1. Create the source (io_new_fd)
2. Define how you want to get data from it (io_reqmeth)
3. read from it using the interface requested (ig->readdb, ig->mmapcb)
4. Close the source, which
shouldn't really close the underlying source. (io_glue DESTROY)
=head1 FUNCTION REFERENCE
Some of these functions are internal.
=over
=cut
*/
static void
i_io_init(pIMCTX, io_glue *ig, int type, i_io_readp_t readcb,
i_io_writep_t writecb, i_io_seekp_t seekcb);
static ssize_t fd_read(io_glue *ig, void *buf, size_t count);
static ssize_t fd_write(io_glue *ig, const void *buf, size_t count);
static off_t fd_seek(io_glue *ig, off_t offset, int whence);
static int fd_close(io_glue *ig);
static ssize_t fd_size(io_glue *ig);
static const char *my_strerror(int err);
static void i_io_setup_buffer(io_glue *ig);
static void
i_io_start_write(io_glue *ig);
static int
i_io_read_fill(io_glue *ig, ssize_t needed);
static void
dump_data(unsigned char *start, unsigned char *end, int bias);
static ssize_t realseek_read(io_glue *igo, void *buf, size_t count);
static ssize_t realseek_write(io_glue *igo, const void *buf, size_t count);
static int realseek_close(io_glue *igo);
static off_t realseek_seek(io_glue *igo, off_t offset, int whence);
static void realseek_destroy(io_glue *igo);
static ssize_t buffer_read(io_glue *igo, void *buf, size_t count);
static ssize_t buffer_write(io_glue *ig, const void *buf, size_t count);
static int buffer_close(io_glue *ig);
static off_t buffer_seek(io_glue *igo, off_t offset, int whence);
static void buffer_destroy(io_glue *igo);
static io_blink*io_blink_new(void);
static void io_bchain_advance(io_ex_bchain *ieb);
static void io_destroy_bufchain(io_ex_bchain *ieb);
static ssize_t bufchain_read(io_glue *ig, void *buf, size_t count);
static ssize_t bufchain_write(io_glue *ig, const void *buf, size_t count);
static int bufchain_close(io_glue *ig);
static off_t bufchain_seek(io_glue *ig, off_t offset, int whence);
static void bufchain_destroy(io_glue *ig);
/*
* Methods for setting up data source
*/
/*
=item im_io_new_bufchain(ctx)
X<im_io_new_bufchain API>X<i_io_new_bufchain API>
=order 10
=category I/O Layers
Returns a new io_glue object that has the 'empty' source and but can
be written to and read from later (like a pseudo file).
Also callable as C<io_new_bufchain()>.
=cut
*/
io_glue *
im_io_new_bufchain(pIMCTX) {
io_glue *ig;
io_ex_bchain *ieb = mymalloc(sizeof(io_ex_bchain));
im_log((aIMCTX, 1, "io_new_bufchain()\n"));
ig = mymalloc(sizeof(io_glue));
memset(ig, 0, sizeof(*ig));
i_io_init(aIMCTX, ig, BUFCHAIN, bufchain_read, bufchain_write, bufchain_seek);
ieb->offset = 0;
ieb->length = 0;
ieb->cpos = 0;
ieb->gpos = 0;
ieb->tfill = 0;
ieb->head = io_blink_new();
ieb->cp = ieb->head;
ieb->tail = ieb->head;
ig->exdata = ieb;
ig->closecb = bufchain_close;
ig->destroycb = bufchain_destroy;
im_context_refinc(aIMCTX, "im_io_new_bufchain");
return ig;
}
/*
=item im_io_new_buffer(ctx, data, length)
X<im_io_new_buffer API>X<io_new_buffer API>
=order 10
=category I/O Layers
Returns a new io_glue object that has the source defined as reading
from specified buffer. Note that the buffer is not copied.
ctx - an Imager context object
data - buffer to read from
length - length of buffer
Also callable as C<io_new_buffer(data, length>.
=cut
*/
io_glue *
im_io_new_buffer(pIMCTX, const char *data, size_t len, i_io_closebufp_t closecb, void *closedata) {
io_buffer *ig;
im_log((aIMCTX, 1, "io_new_buffer(data %p, len %ld, closecb %p, closedata %p)\n", data, (long)len, closecb, closedata));
ig = mymalloc(sizeof(io_buffer));
memset(ig, 0, sizeof(*ig));
i_io_init(aIMCTX, &ig->base, BUFFER, buffer_read, buffer_write, buffer_seek);
ig->data = data;
ig->len = len;
ig->closecb = closecb;
ig->closedata = closedata;
ig->cpos = 0;
ig->base.closecb = buffer_close;
ig->base.destroycb = buffer_destroy;
im_context_refinc(aIMCTX, "im_io_new_bufchain");
return (io_glue *)ig;
}
/*
=item im_io_new_fd(ctx, file)
X<io_new_fd API>X<im_io_new_fd API>
=order 10
=category I/O Layers
Returns a new io_glue object that has the source defined as reading
from specified file descriptor. Note that the interface to receiving
data from the io_glue callbacks hasn't been done yet.
ctx - and Imager context object
file - file descriptor to read/write from
Also callable as C<io_new_fd(file)>.
=cut
*/
io_glue *
im_io_new_fd(pIMCTX, int fd) {
io_fdseek *ig;
im_log((aIMCTX, 1, "io_new_fd(fd %d)\n", fd));
ig = mymalloc(sizeof(io_fdseek));
memset(ig, 0, sizeof(*ig));
i_io_init(aIMCTX, &ig->base, FDSEEK, fd_read, fd_write, fd_seek);
ig->fd = fd;
ig->base.closecb = fd_close;
ig->base.sizecb = fd_size;
ig->base.destroycb = NULL;
im_context_refinc(aIMCTX, "im_io_new_bufchain");
im_log((aIMCTX, 1, "(%p) <- io_new_fd\n", ig));
return (io_glue *)ig;
}
/*
=item im_io_new_cb(ctx, p, read_cb, write_cb, seek_cb, close_cb, destroy_cb)
X<im_io_new_cb API>X<io_new_cb API>
=category I/O Layers
=order 10
Create a new I/O layer object that calls your supplied callbacks.
In general the callbacks should behave like the corresponding POSIX
primitives.
=over
=item *
C<read_cb>(p, buffer, length) should read up to C<length> bytes into
C<buffer> and return the number of bytes read. At end of file, return
0. On error, return -1.
=item *
C<write_cb>(p, buffer, length) should write up to C<length> bytes from
C<buffer> and return the number of bytes written. A return value <= 0
will be treated as an error.
=item *
C<seekcb>(p, offset, whence) should seek and return the new offset.
=item *
C<close_cb>(p) should return 0 on success, -1 on failure.
=item *
C<destroy_cb>(p) should release any memory specific to your callback
handlers.
=back
Also callable as C<io_new_cb(p, readcb, writecb, seekcb, closecb,
destroycb)>.
=cut
*/
io_glue *
im_io_new_cb(pIMCTX, void *p, i_io_readl_t readcb, i_io_writel_t writecb,
i_io_seekl_t seekcb, i_io_closel_t closecb,
i_io_destroyl_t destroycb) {
io_cb *ig;
im_log((aIMCTX, 1, "io_new_cb(p %p, readcb %p, writecb %p, seekcb %p, closecb %p, "
"destroycb %p)\n", p, readcb, writecb, seekcb, closecb, destroycb));
ig = mymalloc(sizeof(io_cb));
memset(ig, 0, sizeof(*ig));
i_io_init(aIMCTX, &ig->base, CBSEEK, realseek_read, realseek_write, realseek_seek);
im_log((aIMCTX, 1, "(%p) <- io_new_cb\n", ig));
ig->base.closecb = realseek_close;
ig->base.destroycb = realseek_destroy;
ig->p = p;
ig->readcb = readcb;
ig->writecb = writecb;
ig->seekcb = seekcb;
ig->closecb = closecb;
ig->destroycb = destroycb;
im_context_refinc(aIMCTX, "im_io_new_bufchain");
return (io_glue *)ig;
}
/*
=item io_slurp(ig, c)
X<io_slurp API>
=category I/O Layers
Takes the source that the io_glue is bound to and allocates space for
a return buffer and returns the entire content in a single buffer.
Note: This only works for io_glue objects created by
io_new_bufchain(). It is useful for saving to scalars and such.
ig - io_glue object
c - pointer to a pointer to where data should be copied to
char *data;
size_t size = io_slurp(ig, &data);
... do something with the data ...
myfree(data);
io_slurp() will abort the program if the supplied I/O layer is not
from io_new_bufchain().
=cut
*/
size_t
io_slurp(io_glue *ig, unsigned char **c) {
ssize_t rc;
io_ex_bchain *ieb;
unsigned char *cc;
io_type inn = ig->type;
if ( inn != BUFCHAIN ) {
dIMCTXio(ig);
im_fatal(aIMCTX, 0, "io_slurp: called on a source that is not from a bufchain\n");
}
ieb = ig->exdata;
cc = *c = mymalloc( ieb->length );
bufchain_seek(ig, 0, SEEK_SET);
rc = bufchain_read(ig, cc, ieb->length);
if (rc != ieb->length) {
dIMCTXio(ig);
im_fatal(aIMCTX,1, "io_slurp: bufchain_read returned an incomplete read: rc = %ld, request was %ld\n",
(long)rc, (long)ieb->length);
}
return rc;
}
/*
=item io_glue_destroy(ig)
X<io_glue_destroy API>
=category I/O Layers
=order 90
=synopsis io_glue_destroy(ig);
Destroy an io_glue objects. Should clean up all related buffers.
ig - io_glue object to destroy.
=cut
*/
void
io_glue_destroy(io_glue *ig) {
dIMCTXio(ig);
im_log((aIMCTX, 1, "io_glue_DESTROY(ig %p)\n", ig));
if (ig->destroycb)
ig->destroycb(ig);
if (ig->buffer)
myfree(ig->buffer);
myfree(ig);
im_context_refdec(aIMCTX, "io_glue_destroy");
}
/*
=item i_io_getc(ig)
=category I/O Layers
A macro to read a single byte from a buffered I/O glue object.
Returns EOF on failure, or a byte.
=cut
*/
int
i_io_getc_imp(io_glue *ig) {
if (ig->write_ptr)
return EOF;
if (ig->error || ig->buf_eof)
return EOF;
if (!ig->buffered) {
unsigned char buf;
ssize_t rc = i_io_raw_read(ig, &buf, 1);
if (rc > 0) {
return buf;
}
else if (rc == 0) {
ig->buf_eof = 1;
return EOF;
}
else {
ig->error = 1;
return EOF;
}
}
if (!ig->buffer)
i_io_setup_buffer(ig);
if (!ig->read_ptr || ig->read_ptr == ig->read_end) {
if (!i_io_read_fill(ig, 1))
return EOF;
}
return *(ig->read_ptr++);
}
/*
=item i_io_peekc(ig)
=category I/O Layers
Read the next character from the stream without advancing the stream.
On error or end of file, return EOF.
For unbuffered streams a single character buffer will be setup.
=cut
*/
int
i_io_peekc_imp(io_glue *ig) {
if (ig->write_ptr)
return EOF;
if (!ig->buffer)
i_io_setup_buffer(ig);
if (!ig->buffered) {
ssize_t rc = i_io_raw_read(ig, ig->buffer, 1);
if (rc > 0) {
ig->read_ptr = ig->buffer;
ig->read_end = ig->buffer + 1;
return *(ig->buffer);
}
else if (rc == 0) {
ig->buf_eof = 1;
return EOF;
}
else {
ig->error = 1;
return EOF;
}
}
if (!ig->read_ptr || ig->read_ptr == ig->read_end) {
if (ig->error || ig->buf_eof)
return EOF;
if (!i_io_read_fill(ig, 1))
return EOF;
}
return *(ig->read_ptr);
}
/*
=item i_io_peekn(ig, buffer, size)
=category I/O Layers
=synopsis ssize_t count = i_io_peekn(ig, buffer, sizeof(buffer));
Buffer at least C<size> (at most C<< ig->buf_size >> bytes of data
from the stream and return C<size> bytes of it to the caller in
C<buffer>.
This ignores the buffered state of the stream, and will always setup
buffering if needed.
If no C<type> parameter is provided to Imager::read() or
Imager::read_multi(), Imager will call C<i_io_peekn()> when probing
for the file format.
Returns -1 on error, 0 if there is no data before EOF, or the number
of bytes read into C<buffer>.
=cut
*/
ssize_t
i_io_peekn(io_glue *ig, void *buf, size_t size) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn(%p, %p, %d)\n", ig, buf, (int)size));
if (size == 0) {
dIMCTXio(ig);
i_push_error(0, "peekn size must be positive");
IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() => -1 (zero size)\n"));
return -1;
}
if (ig->write_ptr) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() => -1 (write_ptr set)\n"));
return -1;
}
if (!ig->buffer)
i_io_setup_buffer(ig);
if ((!ig->read_ptr || size > ig->read_end - ig->read_ptr)
&& !(ig->buf_eof || ig->error)) {
i_io_read_fill(ig, size);
}
if (ig->read_ptr && ig->read_end != ig->read_ptr) {
if (size > ig->read_end - ig->read_ptr)
size = ig->read_end - ig->read_ptr;
if (size)
memcpy(buf, ig->read_ptr, size);
}
else if (ig->buf_eof) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() => 0 (eof)\n"));
return 0;
}
else if (ig->error) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() => -1 (error)\n"));
return -1;
}
else {
IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() - size 0 but not eof or error!\n"));
return -1;
}
IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() => %d\n", (int)size));
return size;
}
/*
=item i_io_putc(ig, c)
=category I/O Layers
Write a single character to the stream.
On success return c, on error returns EOF
=cut
*/
int
i_io_putc_imp(io_glue *ig, int c) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_putc_imp(%p, %d)\n", ig, c));
if (!ig->buffered) {
char buf = c;
ssize_t write_result;
int result = c;
if (ig->error)
return EOF;
write_result = i_io_raw_write(ig, &buf, 1);
if (write_result != 1) {
ig->error = 1;
result = EOF;
IOL_DEB(fprintf(IOL_DEBs, " unbuffered putc() failed, setting error mode\n"));
}
IOL_DEB(fprintf(IOL_DEBs, " unbuffered: result %d\n", result));
return result;
}
if (ig->read_ptr)
return EOF;
if (ig->error)
return EOF;
if (!ig->buffer)
i_io_setup_buffer(ig);
if (ig->write_ptr && ig->write_ptr == ig->write_end) {
if (!i_io_flush(ig))
return EOF;
}
i_io_start_write(ig);
*(ig->write_ptr)++ = c;
return (unsigned char)c;
}
/*
=item i_io_read(io, buffer, size)
=category I/O Layers
Read up to C<size> bytes from the stream C<io> into C<buffer>.
Returns the number of bytes read. Returns 0 on end of file. Returns
-1 on error.
=cut
*/
ssize_t
i_io_read(io_glue *ig, void *buf, size_t size) {
unsigned char *pbuf = buf;
ssize_t read_total = 0;
IOL_DEB(fprintf(IOL_DEBs, "i_io_read(%p, %p, %u)\n", ig, buf, (unsigned)size));
if (ig->write_ptr) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_read() => -1 (write_ptr set)\n"));
return -1;
}
if (!ig->buffer && ig->buffered)
i_io_setup_buffer(ig);
if (ig->read_ptr && ig->read_ptr < ig->read_end) {
size_t alloc = ig->read_end - ig->read_ptr;
if (alloc > size)
alloc = size;
memcpy(pbuf, ig->read_ptr, alloc);
ig->read_ptr += alloc;
pbuf += alloc;
size -= alloc;
read_total += alloc;
}
if (size > 0 && !(ig->error || ig->buf_eof)) {
if (!ig->buffered || size > ig->buf_size) {
ssize_t rc;
while (size > 0 && (rc = i_io_raw_read(ig, pbuf, size)) > 0) {
size -= rc;
pbuf += rc;
read_total += rc;
}
IOL_DEB(fprintf(IOL_DEBs, "i_io_read() => %d (raw read)\n", (int)read_total));
if (rc < 0)
ig->error = 1;
else if (rc == 0)
ig->buf_eof = 1;
if (!read_total)
return rc;
}
else {
if (i_io_read_fill(ig, size)) {
size_t alloc = ig->read_end - ig->read_ptr;
if (alloc > size)
alloc = size;
memcpy(pbuf, ig->read_ptr, alloc);
ig->read_ptr += alloc;
pbuf += alloc;
size -= alloc;
read_total += alloc;
}
else {
if (!read_total && ig->error) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_read() => -1 (fill failure)\n"));
return -1;
}
}
}
}
if (!read_total && ig->error)
read_total = -1;
IOL_DEB(fprintf(IOL_DEBs, "i_io_read() => %d\n", (int)read_total));
return read_total;
}
/*
=item i_io_write(io, buffer, size)
=category I/O Layers
=synopsis ssize_t result = i_io_write(io, buffer, size)
Write to the given I/O stream.
Returns the number of bytes written.
=cut
*/
ssize_t
i_io_write(io_glue *ig, const void *buf, size_t size) {
const unsigned char *pbuf = buf;
size_t write_count = 0;
IOL_DEB(fprintf(IOL_DEBs, "i_io_write(%p, %p, %u)\n", ig, buf, (unsigned)size));
if (!ig->buffered) {
ssize_t result;
if (ig->error) {
IOL_DEB(fprintf(IOL_DEBs, " unbuffered, error state\n"));
return -1;
}
result = i_io_raw_write(ig, buf, size);
if (result != size) {
ig->error = 1;
IOL_DEB(fprintf(IOL_DEBs, " unbuffered, setting error flag\n"));
}
IOL_DEB(fprintf(IOL_DEBs, " unbuffered, result: %d\n", (int)result));
return result;
}
if (ig->read_ptr) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => -1 (read_ptr set)\n"));
return -1;
}
if (ig->error) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => -1 (error)\n"));
return -1;
}
if (!ig->buffer)
i_io_setup_buffer(ig);
if (!ig->write_ptr)
i_io_start_write(ig);
if (ig->write_ptr && ig->write_ptr + size <= ig->write_end) {
size_t alloc = ig->write_end - ig->write_ptr;
if (alloc > size)
alloc = size;
memcpy(ig->write_ptr, pbuf, alloc);
write_count += alloc;
size -= alloc;
pbuf += alloc;
ig->write_ptr += alloc;
}
if (size) {
if (!i_io_flush(ig)) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => %d (i_io_flush failure)\n", (int)write_count));
return write_count ? write_count : -1;
}
i_io_start_write(ig);
if (size > ig->buf_size) {
ssize_t rc;
while (size > 0 && (rc = i_io_raw_write(ig, pbuf, size)) > 0) {
write_count += rc;
pbuf += rc;
size -= rc;
}
if (rc <= 0) {
ig->error = 1;
if (!write_count) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => -1 (direct write failure)\n"));
return -1;
}
}
}
else {
memcpy(ig->write_ptr, pbuf, size);
write_count += size;
ig->write_ptr += size;
}
}
IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => %d\n", (int)write_count));
return write_count;
}
/*
=item i_io_seek(io, offset, whence)
=category I/O Layers
Seek within the stream.
Acts like perl's seek.
=cut
*/
off_t
i_io_seek(io_glue *ig, off_t offset, int whence) {
off_t new_off;
IOL_DEB(fprintf(IOL_DEBs, "i_io_seek(%p, %ld, %d)\n", ig, (long)offset, whence));
if (ig->write_ptr && ig->write_ptr != ig->write_end) {
if (!i_io_flush(ig))
return (off_t)(-1);
}
if (whence == SEEK_CUR && ig->read_ptr && ig->read_ptr != ig->read_end)
offset -= ig->read_end - ig->read_ptr;
ig->read_ptr = ig->read_end = NULL;
ig->write_ptr = ig->write_end = NULL;
ig->error = 0;
ig->buf_eof = 0;
new_off = i_io_raw_seek(ig, offset, whence);
if (new_off < 0)
ig->error = 1;
IOL_DEB(fprintf(IOL_DEBs, "i_io_seek() => %ld\n", (long)new_off));
return new_off;
}
/*
=item i_io_flush(io)
=category I/O Layers
Flush any buffered output.
Returns true on success,
=cut
*/
int
i_io_flush(io_glue *ig) {
unsigned char *bufp;
IOL_DEB(fprintf(IOL_DEBs, "i_io_flush(%p)\n", ig));
if (ig->error) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_flush() => 0 (error set)\n", ig));
return 0;
}
/* nothing to do */
if (!ig->write_ptr)
return 1;
bufp = ig->buffer;
while (bufp < ig->write_ptr) {
ssize_t rc = i_io_raw_write(ig, bufp, ig->write_ptr - bufp);
if (rc <= 0) {
IOL_DEB(fprintf(IOL_DEBs, "i_io_flush() => 0 (write error)\n", ig));
ig->error = 1;
return 0;
}
bufp += rc;
}
ig->write_ptr = ig->write_end = NULL;
IOL_DEB(fprintf(IOL_DEBs, "i_io_flush() => 1\n", ig));
return 1;
}
/*
=item i_io_close(io)
=category I/O Layers
Flush any pending output and perform the close action for the stream.
Returns 0 on success.
=cut
*/
int
i_io_close(io_glue *ig) {
int result = 0;
IOL_DEB(fprintf(IOL_DEBs, "i_io_close(%p)\n", ig));
if (ig->error)
result = -1;
if (ig->write_ptr && !i_io_flush(ig))
result = -1;
if (i_io_raw_close(ig))
result = -1;
IOL_DEB(fprintf(IOL_DEBs, "i_io_close() => %d\n", result));
return result;
}
/*
=item i_io_gets(ig, buffer, size, end_of_line)
=category I/O Layers
=synopsis char buffer[BUFSIZ]
=synopsis ssize_t len = i_io_gets(buffer, sizeof(buffer), '\n');
Read up to C<size>-1 bytes from the stream C<ig> into C<buffer>.
If the byte C<end_of_line> is seen then no further bytes will be read.
Returns the number of bytes read.
Always C<NUL> terminates the buffer.
=cut
*/
ssize_t
i_io_gets(io_glue *ig, char *buffer, size_t size, int eol) {
ssize_t read_count = 0;
if (size < 2)
return 0;
--size; /* room for nul */
while (size > 0) {
int byte = i_io_getc(ig);
if (byte == EOF)
break;
*buffer++ = byte;
++read_count;
if (byte == eol)
break;
--size;
}
*buffer++ = '\0';
return read_count;
}
/*