iolayer.c

filter: Statementstmt 80.7% 584 / 723 Branchbran 74.3% 290 / 390 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.5% 874 / 1113 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« rotate.im ↑ Summary img16.c »
1 #define IMAGER_NO_CONTEXT
2 #include "imager.h"
3 #include "iolayer.h"
4 #include "imerror.h"
5 #include "log.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #ifdef _MSC_VER
9 #include <io.h>
10 #endif
11 #include <string.h>
12 #include <errno.h>
13 #include "imageri.h"
14 #include <sys/stat.h>
15  
16 #ifdef IMAGER_POSIX_MMAP
17 #include <sys/mman.h>
18 #endif
19 #ifdef WIN32
20 #include <windows.h>
21 #include <io.h> /* _get_osfhandle */
22 #endif
23  
24 #define IOL_DEB(x)
25 #define IOL_DEBs stderr
26  
27 #define IO_BUF_SIZE 8192
28  
29 char *io_type_names[] = { "FDSEEK", "FDNOSEEK", "BUFFER", "CBSEEK", "CBNOSEEK", "BUFCHAIN" };
30  
31 typedef struct io_blink {
32 char buf[BBSIZ];
33 /* size_t cnt; */
34 size_t len; /* How large is this buffer = BBZIS for now */
35 struct io_blink *next;
36 struct io_blink *prev;
37 } io_blink;
38  
39  
40 typedef struct {
41 i_io_glue_t base;
42 int fd;
43 off_t size;
44 int size_set;
45 #if defined(IMAGER_POSIX_MMAP) || defined(WIN32)
46 void *mapped;
47 size_t map_size;
48 #endif
49 } io_fdseek;
50  
51 typedef struct {
52 i_io_glue_t base;
53 const char *data;
54 size_t len;
55 i_io_closebufp_t closecb; /* free memory mapped segment or decrement refcount */
56 void *closedata;
57 off_t cpos;
58 } io_buffer;
59  
60 typedef struct {
61 i_io_glue_t base;
62 void *p; /* Callback data */
63 i_io_readl_t readcb;
64 i_io_writel_t writecb;
65 i_io_seekl_t seekcb;
66 i_io_closel_t closecb;
67 i_io_destroyl_t destroycb;
68 i_io_sizel_t sizecb;
69 off_t size;
70 } io_cb;
71  
72 typedef struct {
73 off_t offset; /* Offset of the source - not used */
74 off_t length; /* Total length of chain in bytes */
75 io_blink *head; /* Start of chain */
76 io_blink *tail; /* End of chain */
77 off_t tfill; /* End of stream in last link */
78 io_blink *cp; /* Current element of list */
79 off_t cpos; /* Offset within the current */
80 off_t gpos; /* Global position in stream */
81 } io_ex_bchain;
82  
83 /* turn current offset, file length, whence and offset into a new offset */
84 #define calc_seek_offset(curr_off, length, offset, whence) \
85 (((whence) == SEEK_SET) ? (offset) : \
86 ((whence) == SEEK_CUR) ? (curr_off) + (offset) : \
87 ((whence) == SEEK_END) ? (off_t)(length) + (offset) : -1)
88  
89 /*
90 =head1 NAME
91  
92 iolayer.c - encapsulates different source of data into a single framework.
93  
94 =head1 SYNOPSIS
95  
96 io_glue *ig = io_new_fd( fileno(stdin) );
97 method = io_reqmeth( IOL_NOSEEK | IOL_MMAP ); // not implemented yet
98  
99 switch (method) {
100 case IOL_NOSEEK:
101 code that uses ig->readcb()
102 to read data goes here.
103 break;
104 case IOL_MMAP:
105 code that uses ig->readcb()
106 to read data goes here.
107 break;
108 }
109  
110 io_glue_destroy(ig);
111 // and much more
112  
113 =head1 DESCRIPTION
114  
115 iolayer.c implements the basic functions to create and destroy io_glue
116 objects for Imager. The typical usage pattern for data sources is:
117  
118 1. Create the source (io_new_fd)
119 2. Define how you want to get data from it
120 3. read from it using the interface requested (ig->readdb, ig->mmapcb)
121 4. Close the source, which
122 shouldn't really close the underlying source. (io_glue DESTROY)
123  
124 =head1 FUNCTION REFERENCE
125  
126 Some of these functions are internal.
127  
128 =over
129  
130 =cut
131 */
132  
133 static void
134 i_io_init(pIMCTX, io_glue *ig, int type, const i_io_glue_vtable_t *vtbl);
135  
136 static ssize_t fd_read(io_glue *ig, void *buf, size_t count);
137 static ssize_t fd_write(io_glue *ig, const void *buf, size_t count);
138 static off_t fd_seek(io_glue *ig, off_t offset, int whence);
139 static int fd_close(io_glue *ig);
140 static off_t fd_size(io_glue *ig);
141 static int fd_mmap(io_glue *ig, const void **pdata, size_t *psize);
142 static int fd_munmap(io_glue *ig);
143 static void fd_destroy(io_glue *igo);
144 static const char *my_strerror(int err);
145 static void i_io_setup_buffer(io_glue *ig);
146 static void
147 i_io_start_write(io_glue *ig);
148 static int
149 i_io_read_fill(io_glue *ig, ssize_t needed);
150 static void
151 dump_data(unsigned char *start, unsigned char *end, int bias);
152 static ssize_t realseek_read(io_glue *igo, void *buf, size_t count);
153 static ssize_t realseek_write(io_glue *igo, const void *buf, size_t count);
154 static int realseek_close(io_glue *igo);
155 static off_t realseek_seek(io_glue *igo, off_t offset, int whence);
156 static void realseek_destroy(io_glue *igo);
157 static off_t realseek_size(io_glue *igo);
158 static ssize_t buffer_read(io_glue *igo, void *buf, size_t count);
159 static ssize_t buffer_write(io_glue *ig, const void *buf, size_t count);
160 static int buffer_close(io_glue *ig);
161 static off_t buffer_seek(io_glue *igo, off_t offset, int whence);
162 static off_t buffer_size(io_glue *ig);
163 static int buffer_mmap(io_glue *ig, const void **pdata, size_t *psize);
164 static int buffer_munmap(io_glue *ig);
165 static void buffer_destroy(io_glue *igo);
166 static io_blink*io_blink_new(void);
167 static void io_bchain_advance(io_ex_bchain *ieb);
168 static void io_destroy_bufchain(io_ex_bchain *ieb);
169 static ssize_t bufchain_read(io_glue *ig, void *buf, size_t count);
170 static ssize_t bufchain_write(io_glue *ig, const void *buf, size_t count);
171 static int bufchain_close(io_glue *ig);
172 static off_t bufchain_seek(io_glue *ig, off_t offset, int whence);
173 static off_t bufchain_size(io_glue *ig);
174 static void bufchain_destroy(io_glue *ig);
175  
176 static const i_io_glue_vtable_t
177 bufchain_vtable =
178 {
179 bufchain_read,
180 bufchain_write,
181 bufchain_seek,
182 bufchain_close,
183 bufchain_size,
184 bufchain_destroy,
185 NULL,
186 NULL
187 };
188  
189 static const i_io_glue_vtable_t
190 buffer_vtable =
191 {
192 buffer_read,
193 buffer_write,
194 buffer_seek,
195 buffer_close,
196 buffer_size,
197 buffer_destroy,
198 buffer_mmap,
199 buffer_munmap
200 };
201  
202 static const i_io_glue_vtable_t
203 fd_vtable =
204 {
205 fd_read,
206 fd_write,
207 fd_seek,
208 fd_close,
209 fd_size,
210 fd_destroy, /* destroy */
211 fd_mmap,
212 fd_munmap
213 };
214  
215 static const i_io_glue_vtable_t
216 callback_vtable =
217 {
218 realseek_read,
219 realseek_write,
220 realseek_seek,
221 realseek_close,
222 realseek_size,
223 realseek_destroy,
224 NULL,
225 NULL
226 };
227  
228 /*
229 * Methods for setting up data source
230 */
231  
232 /*
233 =item im_io_new_bufchain(ctx)
234 X<im_io_new_bufchain API>X<i_io_new_bufchain API>
235 =order 10
236 =category I/O Layers
237  
238 Returns a new io_glue object that has the 'empty' source and but can
239 be written to and read from later (like a pseudo file).
240  
241 Also callable as C<io_new_bufchain()>.
242  
243 =cut
244 */
245  
246 io_glue *
247 56 im_io_new_bufchain(pIMCTX) {
248 io_glue *ig;
249 56 io_ex_bchain *ieb = mymalloc(sizeof(io_ex_bchain));
250  
251 56 im_log((aIMCTX, 1, "io_new_bufchain()\n"));
252  
253 56 ig = mymalloc(sizeof(io_glue));
254 56 memset(ig, 0, sizeof(*ig));
255 56 i_io_init(aIMCTX, ig, BUFCHAIN, &bufchain_vtable);
256  
257 56 ieb->offset = 0;
258 56 ieb->length = 0;
259 56 ieb->cpos = 0;
260 56 ieb->gpos = 0;
261 56 ieb->tfill = 0;
262
263 56 ieb->head = io_blink_new();
264 56 ieb->cp = ieb->head;
265 56 ieb->tail = ieb->head;
266
267 56 ig->exdata = ieb;
268  
269 56 im_context_refinc(aIMCTX, "im_io_new_bufchain");
270  
271 56 return ig;
272 }
273  
274 /*
275 =item im_io_new_buffer(ctx, data, length)
276 X<im_io_new_buffer API>X<io_new_buffer API>
277 =order 10
278 =category I/O Layers
279  
280 Returns a new io_glue object that has the source defined as reading
281 from specified buffer. Note that the buffer is not copied.
282  
283 ctx - an Imager context object
284 data - buffer to read from
285 length - length of buffer
286  
287 Also callable as C<io_new_buffer(data, length>.
288  
289 =cut
290 */
291  
292 io_glue *
293 204 im_io_new_buffer(pIMCTX, const char *data, size_t len, i_io_closebufp_t closecb, void *closedata) {
294 io_buffer *ig;
295
296 204 im_log((aIMCTX, 1, "io_new_buffer(data %p, len %ld, closecb %p, closedata %p)\n", data, (long)len, closecb, closedata));
297  
298 204 ig = mymalloc(sizeof(io_buffer));
299 204 memset(ig, 0, sizeof(*ig));
300 204 i_io_init(aIMCTX, &ig->base, BUFFER, &buffer_vtable);
301 204 ig->data = data;
302 204 ig->len = len;
303 204 ig->closecb = closecb;
304 204 ig->closedata = closedata;
305  
306 204 ig->cpos = 0;
307
308 204 im_context_refinc(aIMCTX, "im_io_new_bufchain");
309  
310 204 return (io_glue *)ig;
311 }
312  
313  
314 /*
315 =item im_io_new_fd(ctx, file)
316 X<io_new_fd API>X<im_io_new_fd API>
317 =order 10
318 =category I/O Layers
319  
320 Returns a new io_glue object that has the source defined as reading
321 from specified file descriptor. Note that the interface to receiving
322 data from the io_glue callbacks hasn't been done yet.
323  
324 ctx - and Imager context object
325 file - file descriptor to read/write from
326  
327 Also callable as C<io_new_fd(file)>.
328  
329 =cut
330 */
331  
332 io_glue *
333 358 im_io_new_fd(pIMCTX, int fd) {
334 io_fdseek *ig;
335  
336 358 im_log((aIMCTX, 1, "io_new_fd(fd %d)\n", fd));
337  
338 358 ig = mymalloc(sizeof(io_fdseek));
339 358 memset(ig, 0, sizeof(*ig));
340 358 i_io_init(aIMCTX, &ig->base, FDSEEK, &fd_vtable);
341 358 ig->fd = fd;
342  
343 358 im_context_refinc(aIMCTX, "im_io_new_bufchain");
344  
345 358 im_log((aIMCTX, 1, "(%p) <- io_new_fd\n", ig));
346 358 return (io_glue *)ig;
347 }
348  
349 /*
350 =item im_io_new_cb8(ctx, p, read_cb, write_cb, seek_cb, close_cb, destroy_cb, size_cb)
351 X<im_io_new_cb API>X<io_new_cb API>
352 =category I/O Layers
353 =order 10
354  
355 Create a new I/O layer object that calls your supplied callbacks.
356  
357 In general the callbacks should behave like the corresponding POSIX
358 primitives.
359  
360 =over
361  
362 =item *
363  
364 C<read_cb>(p, buffer, length) should read up to C<length> bytes into
365 C<buffer> and return the number of bytes read. At end of file, return
366 0. On error, return -1.
367  
368 =item *
369  
370 C<write_cb>(p, buffer, length) should write up to C<length> bytes from
371 C<buffer> and return the number of bytes written. A return value <= 0
372 will be treated as an error.
373  
374 =item *
375  
376 C<seekcb>(p, offset, whence) should seek and return the new offset.
377  
378 =item *
379  
380 C<close_cb>(p) should return 0 on success, -1 on failure.
381  
382 =item *
383  
384 C<destroy_cb>(p) should release any memory specific to your callback
385 handlers.
386  
387 =back
388  
389 Also callable as C<io_new_cb(p, readcb, writecb, seekcb, closecb,
390 destroycb)>.
391  
392 =cut
393 */
394  
395 io_glue *
396 132 im_io_new_cb8(pIMCTX, void *p, i_io_readl_t readcb, i_io_writel_t writecb,
397 i_io_seekl_t seekcb, i_io_closel_t closecb,
398 i_io_destroyl_t destroycb, i_io_sizel_t sizecb) {
399 io_cb *ig;
400  
401 132 im_log((aIMCTX, 1, "io_new_cb(p %p, readcb %p, writecb %p, seekcb %p, closecb %p, "
402 "destroycb %p, seekcb %p)\n", p, readcb, writecb, seekcb, closecb, destroycb, seekcb));
403 132 ig = mymalloc(sizeof(io_cb));
404 132 memset(ig, 0, sizeof(*ig));
405 132 i_io_init(aIMCTX, &ig->base, CBSEEK, &callback_vtable);
406 132 im_log((aIMCTX, 1, "(%p) <- io_new_cb8\n", ig));
407  
408 132 ig->p = p;
409 132 ig->readcb = readcb;
410 132 ig->writecb = writecb;
411 132 ig->seekcb = seekcb;
412 132 ig->closecb = closecb;
413 132 ig->destroycb = destroycb;
414 132 ig->sizecb = sizecb;
415 132 ig->size = -1;
416
417 132 im_context_refinc(aIMCTX, "im_io_new_cb8");
418  
419 132 return (io_glue *)ig;
420 }
421  
422 /*
423 =item io_slurp(ig, c)
424 X<io_slurp API>
425 =category I/O Layers
426  
427 Takes the source that the io_glue is bound to and allocates space for
428 a return buffer and returns the entire content in a single buffer.
429 Note: This only works for io_glue objects created by
430 io_new_bufchain(). It is useful for saving to scalars and such.
431  
432 ig - io_glue object
433 c - pointer to a pointer to where data should be copied to
434  
435 char *data;
436 size_t size = io_slurp(ig, &data);
437 ... do something with the data ...
438 myfree(data);
439  
440 io_slurp() will abort the program if the supplied I/O layer is not
441 from io_new_bufchain().
442  
443 =cut
444 */
445  
446 size_t
447 47 io_slurp(io_glue *ig, unsigned char **c) {
448 ssize_t rc;
449 io_ex_bchain *ieb;
450 unsigned char *cc;
451 47 io_type inn = ig->type;
452
453 47 ▶ if ( inn != BUFCHAIN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( inn != BUFCHAIN ) { 0 47 47
454 0 dIMCTXio(ig);
455 0 im_fatal(aIMCTX, 0, "io_slurp: called on a source that is not from a bufchain\n");
456 }
457  
458 47 ieb = ig->exdata;
459 47 cc = *c = mymalloc( ieb->length );
460
461 47 bufchain_seek(ig, 0, SEEK_SET);
462
463 47 rc = bufchain_read(ig, cc, ieb->length);
464  
465 47 ▶ if (rc != ieb->length) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != ieb->length) { 0 47 47
466 0 dIMCTXio(ig);
467 0 im_fatal(aIMCTX,1, "io_slurp: bufchain_read returned an incomplete read: rc = %ld, request was %ld\n",
468 0 (long)rc, (long)ieb->length);
469 }
470  
471 47 return rc;
472 }
473  
474 /*
475 =item io_glue_destroy(ig)
476 X<io_glue_destroy API>
477 =category I/O Layers
478 =order 90
479 =synopsis io_glue_destroy(ig);
480  
481 Destroy an io_glue objects. Should clean up all related buffers.
482  
483 ig - io_glue object to destroy.
484  
485 =cut
486 */
487  
488 void
489 750 io_glue_destroy(io_glue *ig) {
490 750 dIMCTXio(ig);
491 750 im_log((aIMCTX, 1, "io_glue_DESTROY(ig %p)\n", ig));
492  
493 750 ▶ if (ig->vtbl->destroycb)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->vtbl->destroycb) 750 0 750
494 750 ig->vtbl->destroycb(ig);
495  
496 750 ▶ if (ig->buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->buffer) 662 88 750
497 662 myfree(ig->buffer);
498
499 750 myfree(ig);
500  
501 750 im_context_refdec(aIMCTX, "io_glue_destroy");
502 750 }
503  
504 /*
505 =item i_io_getc(ig)
506 =category I/O Layers
507  
508 A macro to read a single byte from a buffered I/O glue object.
509  
510 Returns EOF on failure, or a byte.
511  
512 =cut
513 */
514  
515 int
516 20079 i_io_getc_imp(io_glue *ig) {
517 20079 ▶ if (ig->write_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->write_ptr) 1 20078 20079
518 1 return EOF;
519
520 20078 ▶ if (ig->error || ig->buf_eof)
Branch: 2 branches
BranchTrueFalseTotal
if (ig->error || ig->buf_eof) 20078 0 20078
if (ig->error || ig->buf_eof) 4 20074 20078
521 4 return EOF;
522
523 20074 ▶ if (!ig->buffered) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffered) { 20004 70 20074
524 unsigned char buf;
525 20004 ssize_t rc = i_io_raw_read(ig, &buf, 1);
526 20004 ▶ if (rc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc > 0) { 20002 2 20004
527 20002 return buf;
528 }
529 2 ▶ else if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (rc == 0) { 1 1 2
530 1 ig->buf_eof = 1;
531 1 return EOF;
532 }
533 else {
534 1 ig->error = 1;
535 1 return EOF;
536 }
537 }
538  
539 70 ▶ if (!ig->buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffer) 63 7 70
540 63 i_io_setup_buffer(ig);
541
542 70 ▶ if (!ig->read_ptr || ig->read_ptr == ig->read_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ig->read_ptr || ig->read_ptr == ig->read_end) { 5 65 70
if (!ig->read_ptr || ig->read_ptr == ig->read_end) { 5 0 5
543 70 ▶ if (!i_io_read_fill(ig, 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!i_io_read_fill(ig, 1)) 4 66 70
544 4 return EOF;
545 }
546
547 66 return *(ig->read_ptr++);
548 }
549  
550 /*
551 =item i_io_peekc(ig)
552 =category I/O Layers
553  
554 Read the next character from the stream without advancing the stream.
555  
556 On error or end of file, return EOF.
557  
558 For unbuffered streams a single character buffer will be setup.
559  
560 =cut
561 */
562  
563 int
564 23 i_io_peekc_imp(io_glue *ig) {
565 23 ▶ if (ig->write_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->write_ptr) 1 22 23
566 1 return EOF;
567  
568 22 ▶ if (!ig->buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffer) 6 16 22
569 6 i_io_setup_buffer(ig);
570  
571 22 ▶ if (!ig->buffered) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffered) { 5 17 22
572 5 ssize_t rc = i_io_raw_read(ig, ig->buffer, 1);
573 5 ▶ if (rc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc > 0) { 1 4 5
574 1 ig->read_ptr = ig->buffer;
575 1 ig->read_end = ig->buffer + 1;
576 1 return *(ig->buffer);
577 }
578 4 ▶ else if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (rc == 0) { 2 2 4
579 2 ig->buf_eof = 1;
580 2 return EOF;
581 }
582 else {
583 2 ig->error = 1;
584 2 return EOF;
585 }
586 }
587  
588 17 ▶ if (!ig->read_ptr || ig->read_ptr == ig->read_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ig->read_ptr || ig->read_ptr == ig->read_end) { 12 5 17
if (!ig->read_ptr || ig->read_ptr == ig->read_end) { 12 0 12
589 17 ▶ if (ig->error || ig->buf_eof)
Branch: 2 branches
BranchTrueFalseTotal
if (ig->error || ig->buf_eof) 16 1 17
if (ig->error || ig->buf_eof) 5 11 16
590 6 return EOF;
591
592 11 ▶ if (!i_io_read_fill(ig, 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!i_io_read_fill(ig, 1)) 10 1 11
593 10 return EOF;
594 }
595  
596 1 return *(ig->read_ptr);
597 }
598  
599 /*
600 =item i_io_peekn(ig, buffer, size)
601 =category I/O Layers
602 =synopsis ssize_t count = i_io_peekn(ig, buffer, sizeof(buffer));
603  
604 Buffer at least C<size> (at most C<< ig->buf_size >> bytes of data
605 from the stream and return C<size> bytes of it to the caller in
606 C<buffer>.
607  
608 This ignores the buffered state of the stream, and will always setup
609 buffering if needed.
610  
611 If no C<type> parameter is provided to Imager::read() or
612 Imager::read_multi(), Imager will call C<i_io_peekn()> when probing
613 for the file format.
614  
615 Returns -1 on error, 0 if there is no data before EOF, or the number
616 of bytes read into C<buffer>.
617  
618 =cut
619 */
620  
621 ssize_t
622 210 i_io_peekn(io_glue *ig, void *buf, size_t size) {
623 IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn(%p, %p, %d)\n", ig, buf, (int)size));
624  
625 210 ▶ if (size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (size == 0) { 1 209 210
626 1 dIMCTXio(ig);
627 1 i_push_error(0, "peekn size must be positive");
628 IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() => -1 (zero size)\n"));
629 1 return -1;
630 }
631  
632 209 ▶ if (ig->write_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->write_ptr) { 1 208 209
633 IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() => -1 (write_ptr set)\n"));
634 1 return -1;
635 }
636  
637 208 ▶ if (!ig->buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffer) 197 11 208
638 197 i_io_setup_buffer(ig);
639  
640 208 ▶ if ((!ig->read_ptr || (ssize_t)size > ig->read_end - ig->read_ptr)
Branch: 2 branches
BranchTrueFalseTotal
if ((!ig->read_ptr || (ssize_t)size > ig->read_end - ig->read_ptr) 11 197 208
if ((!ig->read_ptr || (ssize_t)size > ig->read_end - ig->read_ptr) 8 3 11
641 205 ▶ && !(ig->buf_eof || ig->error)) {
Branch: 2 branches
BranchTrueFalseTotal
&& !(ig->buf_eof || ig->error)) { 204 1 205
&& !(ig->buf_eof || ig->error)) { 202 2 204
642 202 i_io_read_fill(ig, size);
643 }
644  
645 208 ▶ if (ig->read_ptr && ig->read_end != ig->read_ptr) {
Branch: 2 branches
BranchTrueFalseTotal
if (ig->read_ptr && ig->read_end != ig->read_ptr) { 206 2 208
if (ig->read_ptr && ig->read_end != ig->read_ptr) { 204 2 206
646 204 ▶ if ((ssize_t)size > ig->read_end - ig->read_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if ((ssize_t)size > ig->read_end - ig->read_ptr) 13 191 204
647 13 size = ig->read_end - ig->read_ptr;
648  
649 204 ▶ if (size)
Branch: 1 branch
BranchTrueFalseTotal
if (size) 204 0 204
650 204 memcpy(buf, ig->read_ptr, size);
651 }
652 4 ▶ else if (ig->buf_eof) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ig->buf_eof) { 1 3 4
653 IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() => 0 (eof)\n"));
654 1 return 0;
655 }
656 3 ▶ else if (ig->error) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ig->error) { 3 0 3
657 IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() => -1 (error)\n"));
658 3 return -1;
659 }
660 else {
661 IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() - size 0 but not eof or error!\n"));
662 0 return -1;
663 }
664  
665 IOL_DEB(fprintf(IOL_DEBs, "i_io_peekn() => %d\n", (int)size));
666  
667 204 return size;
668 }
669  
670 /*
671 =item i_io_putc(ig, c)
672 =category I/O Layers
673  
674 Write a single character to the stream.
675  
676 On success return c, on error returns EOF
677  
678 =cut
679 */
680  
681 int
682 12 i_io_putc_imp(io_glue *ig, int c) {
683 IOL_DEB(fprintf(IOL_DEBs, "i_io_putc_imp(%p, %d)\n", ig, c));
684  
685 12 ▶ if (!ig->buffered) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffered) { 3 9 12
686 3 char buf = c;
687 ssize_t write_result;
688 3 int result = c;
689  
690 3 ▶ if (ig->error)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->error) 1 2 3
691 1 return EOF;
692  
693 2 write_result = i_io_raw_write(ig, &buf, 1);
694 2 ▶ if (write_result != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (write_result != 1) { 1 1 2
695 1 ig->error = 1;
696 1 result = EOF;
697 IOL_DEB(fprintf(IOL_DEBs, " unbuffered putc() failed, setting error mode\n"));
698 }
699 IOL_DEB(fprintf(IOL_DEBs, " unbuffered: result %d\n", result));
700  
701 2 return result;
702 }
703  
704 9 ▶ if (ig->read_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->read_ptr) 1 8 9
705 1 return EOF;
706  
707 8 ▶ if (ig->error)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->error) 2 6 8
708 2 return EOF;
709  
710 6 ▶ if (!ig->buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffer) 5 1 6
711 5 i_io_setup_buffer(ig);
712  
713 6 ▶ if (ig->write_ptr && ig->write_ptr == ig->write_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (ig->write_ptr && ig->write_ptr == ig->write_end) { 1 5 6
if (ig->write_ptr && ig->write_ptr == ig->write_end) { 1 0 1
714 1 ▶ if (!i_io_flush(ig))
Branch: 1 branch
BranchTrueFalseTotal
if (!i_io_flush(ig)) 1 0 1
715 1 return EOF;
716 }
717  
718 5 i_io_start_write(ig);
719  
720 5 *(ig->write_ptr)++ = c;
721  
722 5 return (unsigned char)c;
723 }
724  
725 /*
726 =item i_io_read(io, buffer, size)
727 =category I/O Layers
728  
729 Read up to C<size> bytes from the stream C<io> into C<buffer>.
730  
731 Returns the number of bytes read. Returns 0 on end of file. Returns
732 -1 on error.
733  
734 =cut
735 */
736  
737 ssize_t
738 631641 i_io_read(io_glue *ig, void *buf, size_t size) {
739 631641 unsigned char *pbuf = buf;
740 631641 ssize_t read_total = 0;
741  
742 IOL_DEB(fprintf(IOL_DEBs, "i_io_read(%p, %p, %u)\n", ig, buf, (unsigned)size));
743  
744 631641 ▶ if (ig->write_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->write_ptr) { 1 631640 631641
745 IOL_DEB(fprintf(IOL_DEBs, "i_io_read() => -1 (write_ptr set)\n"));
746 1 return -1;
747 }
748  
749 631640 ▶ if (!ig->buffer && ig->buffered)
Branch: 2 branches
BranchTrueFalseTotal
if (!ig->buffer && ig->buffered) 158 631482 631640
if (!ig->buffer && ig->buffered) 157 1 158
750 157 i_io_setup_buffer(ig);
751  
752 631640 ▶ if (ig->read_ptr && ig->read_ptr < ig->read_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (ig->read_ptr && ig->read_ptr < ig->read_end) { 628793 2847 631640
if (ig->read_ptr && ig->read_ptr < ig->read_end) { 628739 54 628793
753 628739 size_t alloc = ig->read_end - ig->read_ptr;
754
755 628739 ▶ if (alloc > size)
Branch: 1 branch
BranchTrueFalseTotal
if (alloc > size) 627828 911 628739
756 627828 alloc = size;
757  
758 628739 memcpy(pbuf, ig->read_ptr, alloc);
759 628739 ig->read_ptr += alloc;
760 628739 pbuf += alloc;
761 628739 size -= alloc;
762 628739 read_total += alloc;
763 }
764  
765 631640 ▶ if (size > 0 && !(ig->error || ig->buf_eof)) {
Branch: 3 branches
BranchTrueFalseTotal
if (size > 0 && !(ig->error || ig->buf_eof)) { 3567 628073 631640
if (size > 0 && !(ig->error || ig->buf_eof)) { 3567 0 3567
if (size > 0 && !(ig->error || ig->buf_eof)) { 3562 5 3567
766 3567 ▶ if (!ig->buffered || size > ig->buf_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ig->buffered || size > ig->buf_size) { 3561 1 3562
if (!ig->buffered || size > ig->buf_size) { 5 3556 3561
767 ssize_t rc;
768
769 10 ▶ while (size > 0 && (rc = i_io_raw_read(ig, pbuf, size)) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (size > 0 && (rc = i_io_raw_read(ig, pbuf, size)) > 0) { 7 3 10
while (size > 0 && (rc = i_io_raw_read(ig, pbuf, size)) > 0) { 4 3 7
770 4 size -= rc;
771 4 pbuf += rc;
772 4 read_total += rc;
773 }
774
775 IOL_DEB(fprintf(IOL_DEBs, "i_io_read() => %d (raw read)\n", (int)read_total));
776  
777 6 ▶ if (rc < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) 1 5 6
778 1 ig->error = 1;
779 5 ▶ else if (rc == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (rc == 0) 2 3 5
780 2 ig->buf_eof = 1;
781  
782 6 ▶ if (!read_total)
Branch: 1 branch
BranchTrueFalseTotal
if (!read_total) 1 5 6
783 1 return rc;
784 }
785 else {
786 3556 ▶ if (i_io_read_fill(ig, size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_read_fill(ig, size)) { 3503 53 3556
787 3503 size_t alloc = ig->read_end - ig->read_ptr;
788 3503 ▶ if (alloc > size)
Branch: 1 branch
BranchTrueFalseTotal
if (alloc > size) 3468 35 3503
789 3468 alloc = size;
790
791 3503 memcpy(pbuf, ig->read_ptr, alloc);
792 3503 ig->read_ptr += alloc;
793 3503 pbuf += alloc;
794 3503 size -= alloc;
795 3503 read_total += alloc;
796 }
797 else {
798 53 ▶ if (!read_total && ig->error) {
Branch: 2 branches
BranchTrueFalseTotal
if (!read_total && ig->error) { 18 35 53
if (!read_total && ig->error) { 3 15 18
799 IOL_DEB(fprintf(IOL_DEBs, "i_io_read() => -1 (fill failure)\n"));
800 3 return -1;
801 }
802 }
803 }
804 }
805  
806 631636 ▶ if (!read_total && ig->error)
Branch: 2 branches
BranchTrueFalseTotal
if (!read_total && ig->error) 20 631616 631636
if (!read_total && ig->error) 0 20 20
807 0 read_total = -1;
808  
809 IOL_DEB(fprintf(IOL_DEBs, "i_io_read() => %d\n", (int)read_total));
810  
811 631636 return read_total;
812 }
813  
814 /*
815 =item i_io_write(io, buffer, size)
816 =category I/O Layers
817 =synopsis ssize_t result = i_io_write(io, buffer, size)
818  
819 Write to the given I/O stream.
820  
821 Returns the number of bytes written.
822  
823 =cut
824 */
825  
826 ssize_t
827 504101 i_io_write(io_glue *ig, const void *buf, size_t size) {
828 504101 const unsigned char *pbuf = buf;
829 504101 size_t write_count = 0;
830  
831 IOL_DEB(fprintf(IOL_DEBs, "i_io_write(%p, %p, %u)\n", ig, buf, (unsigned)size));
832  
833 504101 ▶ if (!ig->buffered) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffered) { 1937 502164 504101
834 ssize_t result;
835  
836 1937 ▶ if (ig->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->error) { 1 1936 1937
837 IOL_DEB(fprintf(IOL_DEBs, " unbuffered, error state\n"));
838 1 return -1;
839 }
840  
841 1936 result = i_io_raw_write(ig, buf, size);
842  
843 1936 ▶ if (result != (ssize_t)size) {
Branch: 1 branch
BranchTrueFalseTotal
if (result != (ssize_t)size) { 42 1894 1936
844 42 ig->error = 1;
845 IOL_DEB(fprintf(IOL_DEBs, " unbuffered, setting error flag\n"));
846 }
847  
848 IOL_DEB(fprintf(IOL_DEBs, " unbuffered, result: %d\n", (int)result));
849  
850 1936 return result;
851 }
852  
853 502164 ▶ if (ig->read_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->read_ptr) { 1 502163 502164
854 IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => -1 (read_ptr set)\n"));
855 1 return -1;
856 }
857  
858 502163 ▶ if (ig->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->error) { 1 502162 502163
859 IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => -1 (error)\n"));
860 1 return -1;
861 }
862  
863 502162 ▶ if (!ig->buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffer) 234 501928 502162
864 234 i_io_setup_buffer(ig);
865  
866 502162 ▶ if (!ig->write_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->write_ptr) 239 501923 502162
867 239 i_io_start_write(ig);
868  
869 502162 ▶ if (ig->write_ptr && ig->write_ptr + size <= ig->write_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (ig->write_ptr && ig->write_ptr + size <= ig->write_end) { 502162 0 502162
if (ig->write_ptr && ig->write_ptr + size <= ig->write_end) { 501447 715 502162
870 501447 size_t alloc = ig->write_end - ig->write_ptr;
871 501447 ▶ if (alloc > size)
Branch: 1 branch
BranchTrueFalseTotal
if (alloc > size) 501332 115 501447
872 501332 alloc = size;
873 501447 memcpy(ig->write_ptr, pbuf, alloc);
874 501447 write_count += alloc;
875 501447 size -= alloc;
876 501447 pbuf += alloc;
877 501447 ig->write_ptr += alloc;
878 }
879  
880 502162 ▶ if (size) {
Branch: 1 branch
BranchTrueFalseTotal
if (size) { 715 501447 502162
881 715 ▶ if (!i_io_flush(ig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!i_io_flush(ig)) { 1 714 715
882 IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => %d (i_io_flush failure)\n", (int)write_count));
883 1 ▶ return write_count ? (ssize_t)write_count : -1;
Branch: 1 branch
BranchTrueFalseTotal
return write_count ? (ssize_t)write_count : -1; 0 1 1
884 }
885  
886 714 i_io_start_write(ig);
887
888 714 ▶ if (size > ig->buf_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > ig->buf_size) { 105 609 714
889 ssize_t rc;
890 209 ▶ while (size > 0 && (rc = i_io_raw_write(ig, pbuf, size)) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (size > 0 && (rc = i_io_raw_write(ig, pbuf, size)) > 0) { 105 104 209
while (size > 0 && (rc = i_io_raw_write(ig, pbuf, size)) > 0) { 104 1 105
891 104 write_count += rc;
892 104 pbuf += rc;
893 104 size -= rc;
894 }
895 105 ▶ if (rc <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) { 1 104 105
896 1 ig->error = 1;
897 1 ▶ if (!write_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (!write_count) { 1 0 1
898 IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => -1 (direct write failure)\n"));
899 1 return -1;
900 }
901 }
902 }
903 else {
904 609 memcpy(ig->write_ptr, pbuf, size);
905 609 write_count += size;
906 609 ig->write_ptr += size;
907 }
908 }
909  
910 IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => %d\n", (int)write_count));
911  
912 502160 return write_count;
913 }
914  
915 /*
916 =item i_io_seek(io, offset, whence)
917 =category I/O Layers
918  
919 Seek within the stream.
920  
921 Acts like perl's seek.
922  
923 =cut
924 */
925  
926 off_t
927 2704 i_io_seek(io_glue *ig, off_t offset, int whence) {
928 off_t new_off;
929  
930 IOL_DEB(fprintf(IOL_DEBs, "i_io_seek(%p, %ld, %d)\n", ig, (long)offset, whence));
931  
932 2704 ▶ if (ig->write_ptr && ig->write_ptr != ig->write_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (ig->write_ptr && ig->write_ptr != ig->write_end) { 8 2696 2704
if (ig->write_ptr && ig->write_ptr != ig->write_end) { 8 0 8
933 8 ▶ if (!i_io_flush(ig))
Branch: 1 branch
BranchTrueFalseTotal
if (!i_io_flush(ig)) 1 7 8
934 1 return (off_t)(-1);
935 }
936  
937 2703 ▶ if (whence == SEEK_CUR && ig->read_ptr && ig->read_ptr != ig->read_end)
Branch: 3 branches
BranchTrueFalseTotal
if (whence == SEEK_CUR && ig->read_ptr && ig->read_ptr != ig->read_end) 1 2702 2703
if (whence == SEEK_CUR && ig->read_ptr && ig->read_ptr != ig->read_end) 1 0 1
if (whence == SEEK_CUR && ig->read_ptr && ig->read_ptr != ig->read_end) 1 0 1
938 1 offset -= ig->read_end - ig->read_ptr;
939  
940 2703 ig->read_ptr = ig->read_end = NULL;
941 2703 ig->write_ptr = ig->write_end = NULL;
942 2703 ig->error = 0;
943 2703 ig->buf_eof = 0;
944
945 2703 new_off = i_io_raw_seek(ig, offset, whence);
946 2703 ▶ if (new_off < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (new_off < 0) 5 2698 2703
947 5 ig->error = 1;
948  
949 IOL_DEB(fprintf(IOL_DEBs, "i_io_seek() => %ld\n", (long)new_off));
950  
951 2703 return new_off;
952 }
953  
954 /*
955 =item i_io_flush(io)
956 =category I/O Layers
957  
958 Flush any buffered output.
959  
960 Returns true on success,
961  
962 =cut
963 */
964  
965 int
966 958 i_io_flush(io_glue *ig) {
967 unsigned char *bufp;
968  
969 IOL_DEB(fprintf(IOL_DEBs, "i_io_flush(%p)\n", ig));
970  
971 958 ▶ if (ig->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->error) { 1 957 958
972 IOL_DEB(fprintf(IOL_DEBs, "i_io_flush() => 0 (error set)\n", ig));
973 1 return 0;
974 }
975  
976 /* nothing to do */
977 957 ▶ if (!ig->write_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->write_ptr) 1 956 957
978 1 return 1;
979  
980 956 bufp = ig->buffer;
981 1788 ▶ while (bufp < ig->write_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
while (bufp < ig->write_ptr) { 841 947 1788
982 841 ssize_t rc = i_io_raw_write(ig, bufp, ig->write_ptr - bufp);
983 841 ▶ if (rc <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) { 9 832 841
984 IOL_DEB(fprintf(IOL_DEBs, "i_io_flush() => 0 (write error)\n", ig));
985 9 ig->error = 1;
986 9 return 0;
987 }
988
989 832 bufp += rc;
990 }
991  
992 947 ig->write_ptr = ig->write_end = NULL;
993  
994 IOL_DEB(fprintf(IOL_DEBs, "i_io_flush() => 1\n", ig));
995  
996 947 return 1;
997 }
998  
999 /*
1000 =item i_io_close(io)
1001 =category I/O Layers
1002  
1003 Flush any pending output and perform the close action for the stream.
1004  
1005 Returns 0 on success.
1006  
1007 =cut
1008 */
1009  
1010 int
1011 285 i_io_close(io_glue *ig) {
1012 285 int result = 0;
1013  
1014 IOL_DEB(fprintf(IOL_DEBs, "i_io_close(%p)\n", ig));
1015 285 ▶ if (ig->error)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->error) 2 283 285
1016 2 result = -1;
1017  
1018 285 ▶ if (ig->write_ptr && !i_io_flush(ig))
Branch: 2 branches
BranchTrueFalseTotal
if (ig->write_ptr && !i_io_flush(ig)) 232 53 285
if (ig->write_ptr && !i_io_flush(ig)) 6 226 232
1019 6 result = -1;
1020  
1021 285 ▶ if (i_io_raw_close(ig))
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_raw_close(ig)) 13 272 285
1022 13 result = -1;
1023  
1024 IOL_DEB(fprintf(IOL_DEBs, "i_io_close() => %d\n", result));
1025  
1026 285 return result;
1027 }
1028  
1029 /*
1030 =item i_io_gets(ig, buffer, size, end_of_line)
1031 =category I/O Layers
1032 =synopsis char buffer[BUFSIZ]
1033 =synopsis ssize_t len = i_io_gets(buffer, sizeof(buffer), '\n');
1034  
1035 Read up to C<size>-1 bytes from the stream C<ig> into C<buffer>.
1036  
1037 If the byte C<end_of_line> is seen then no further bytes will be read.
1038  
1039 Returns the number of bytes read.
1040  
1041 Always C<NUL> terminates the buffer.
1042  
1043 =cut
1044 */
1045  
1046 ssize_t
1047 7 i_io_gets(io_glue *ig, char *buffer, size_t size, int eol) {
1048 7 ssize_t read_count = 0;
1049 7 ▶ if (size < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (size < 2) 0 7 7
1050 0 return 0;
1051 7 --size; /* room for nul */
1052 36 ▶ while (size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (size > 0) { 35 1 36
1053 35 ▶ int byte = i_io_getc(ig);
Branch: 1 branch
BranchTrueFalseTotal
int byte = i_io_getc(ig); 32 3 35
1054 35 ▶ if (byte == EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (byte == EOF) 1 34 35
1055 1 break;
1056 34 *buffer++ = byte;
1057 34 ++read_count;
1058 34 ▶ if (byte == eol)
Branch: 1 branch
BranchTrueFalseTotal
if (byte == eol) 5 29 34
1059 5 break;
1060 29 --size;
1061 }
1062 7 *buffer++ = '\0';
1063  
1064 7 return read_count;
1065 }
1066  
1067 /*
1068 =item i_io_init(ig, type, vtbl)
1069  
1070 Do common initialization for io_glue objects.
1071  
1072 =cut
1073 */
1074  
1075 static void
1076 750 i_io_init(pIMCTX, io_glue *ig, int type, const i_io_glue_vtable_t *vtbl) {
1077 750 ig->type = type;
1078 750 ig->exdata = NULL;
1079  
1080 750 ig->vtbl = vtbl;
1081 750 ig->context = aIMCTX;
1082  
1083 750 ig->buffer = NULL;
1084 750 ig->read_ptr = NULL;
1085 750 ig->read_end = NULL;
1086 750 ig->write_ptr = NULL;
1087 750 ig->write_end = NULL;
1088 750 ig->buf_size = IO_BUF_SIZE;
1089 750 ig->buf_eof = 0;
1090 750 ig->error = 0;
1091 750 ig->buffered = 1;
1092 750 }
1093  
1094 /*
1095 =item i_io_set_buffered(io, buffered)
1096 =category I/O Layers
1097  
1098 Set the buffering mode of the stream.
1099  
1100 If you switch buffering off on a stream with buffering on:
1101  
1102 =over
1103  
1104 =item *
1105  
1106 any buffered output will be flushed.
1107  
1108 =item *
1109  
1110 any existing buffered input will be consumed before reads become
1111 unbuffered.
1112  
1113 =back
1114  
1115 Returns true on success. This may fail if any buffered output cannot
1116 be flushed.
1117  
1118 =cut
1119 */
1120  
1121 int
1122 52 i_io_set_buffered(io_glue *ig, int buffered) {
1123 52 ▶ if (!buffered && ig->write_ptr) {
Branch: 2 branches
BranchTrueFalseTotal
if (!buffered && ig->write_ptr) { 51 1 52
if (!buffered && ig->write_ptr) { 0 51 51
1124 0 ▶ if (!i_io_flush(ig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!i_io_flush(ig)) { 0 0 0
1125 0 ig->error = 1;
1126 0 return 0;
1127 }
1128 }
1129 52 ig->buffered = buffered;
1130  
1131 52 return 1;
1132 }
1133  
1134 /*
1135 =item i_io_dump(ig)
1136  
1137 Dump the base fields of an io_glue object to stdout.
1138  
1139 =cut
1140 */
1141 void
1142 0 i_io_dump(io_glue *ig, int flags) {
1143 0 fprintf(IOL_DEBs, "ig %p:\n", ig);
1144 0 fprintf(IOL_DEBs, " type: %d\n", ig->type);
1145 0 fprintf(IOL_DEBs, " exdata: %p\n", ig->exdata);
1146 0 ▶ if (flags & I_IO_DUMP_CALLBACKS) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & I_IO_DUMP_CALLBACKS) { 0 0 0
1147 0 fprintf(IOL_DEBs, " vtable: %p\n", ig->vtbl);
1148 0 fprintf(IOL_DEBs, " readcb: %p\n", ig->vtbl->readcb);
1149 0 fprintf(IOL_DEBs, " writecb: %p\n", ig->vtbl->writecb);
1150 0 fprintf(IOL_DEBs, " seekcb: %p\n", ig->vtbl->seekcb);
1151 0 fprintf(IOL_DEBs, " closecb: %p\n", ig->vtbl->closecb);
1152 0 fprintf(IOL_DEBs, " sizecb: %p\n", ig->vtbl->sizecb);
1153 }
1154 0 ▶ if (flags & I_IO_DUMP_BUFFER) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & I_IO_DUMP_BUFFER) { 0 0 0
1155 0 fprintf(IOL_DEBs, " buffer: %p\n", ig->buffer);
1156 0 fprintf(IOL_DEBs, " read_ptr: %p\n", ig->read_ptr);
1157 0 ▶ if (ig->read_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->read_ptr) { 0 0 0
1158 0 fprintf(IOL_DEBs, " ");
1159 0 dump_data(ig->read_ptr, ig->read_end, 0);
1160 0 putc('\n', IOL_DEBs);
1161 }
1162 0 fprintf(IOL_DEBs, " read_end: %p\n", ig->read_end);
1163 0 fprintf(IOL_DEBs, " write_ptr: %p\n", ig->write_ptr);
1164 0 ▶ if (ig->write_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->write_ptr) { 0 0 0
1165 0 fprintf(IOL_DEBs, " ");
1166 0 dump_data(ig->buffer, ig->write_ptr, 1);
1167 0 putc('\n', IOL_DEBs);
1168 }
1169 0 fprintf(IOL_DEBs, " write_end: %p\n", ig->write_end);
1170 0 fprintf(IOL_DEBs, " buf_size: %u\n", (unsigned)(ig->buf_size));
1171 }
1172 0 ▶ if (flags & I_IO_DUMP_STATUS) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & I_IO_DUMP_STATUS) { 0 0 0
1173 0 fprintf(IOL_DEBs, " buf_eof: %d\n", ig->buf_eof);
1174 0 fprintf(IOL_DEBs, " error: %d\n", ig->error);
1175 0 fprintf(IOL_DEBs, " buffered: %d\n", ig->buffered);
1176 }
1177 0 }
1178  
1179 /*
1180 =back
1181  
1182 =head1 INTERNAL FUNCTIONS
1183  
1184 =over
1185  
1186 =item my_strerror
1187  
1188 Calls strerror() and ensures we don't return NULL.
1189  
1190 On some platforms it's possible for strerror() to return NULL, this
1191 wrapper ensures we only get non-NULL values.
1192  
1193 =cut
1194 */
1195  
1196 static
1197 3 const char *my_strerror(int err) {
1198 3 const char *result = strerror(err);
1199
1200 3 ▶ if (!result)
Branch: 1 branch
BranchTrueFalseTotal
if (!result) 0 3 3
1201 0 result = "Unknown error";
1202
1203 3 return result;
1204 }
1205  
1206 static void
1207 662 i_io_setup_buffer(io_glue *ig) {
1208 662 ig->buffer = mymalloc(ig->buf_size);
1209 662 }
1210  
1211 static void
1212 958 i_io_start_write(io_glue *ig) {
1213 958 ig->write_ptr = ig->buffer;
1214 958 ig->write_end = ig->buffer + ig->buf_size;
1215 958 }
1216  
1217 static int
1218 3839 i_io_read_fill(io_glue *ig, ssize_t needed) {
1219 3839 unsigned char *buf_end = ig->buffer + ig->buf_size;
1220 3839 unsigned char *buf_start = ig->buffer;
1221 3839 unsigned char *work = ig->buffer;
1222 ssize_t rc;
1223 3839 int good = 0;
1224  
1225 IOL_DEB(fprintf(IOL_DEBs, "i_io_read_fill(%p, %d)\n", ig, (int)needed));
1226  
1227 /* these conditions may be unused, callers should also be checking them */
1228 3839 ▶ if (ig->error || ig->buf_eof)
Branch: 2 branches
BranchTrueFalseTotal
if (ig->error || ig->buf_eof) 3839 0 3839
if (ig->error || ig->buf_eof) 0 3839 3839
1229 0 return 0;
1230  
1231 3839 ▶ if (needed > (ssize_t)ig->buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (needed > (ssize_t)ig->buf_size) 1 3838 3839
1232 1 needed = ig->buf_size;
1233  
1234 3839 ▶ if (ig->read_ptr && ig->read_ptr < ig->read_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (ig->read_ptr && ig->read_ptr < ig->read_end) { 728 3111 3839
if (ig->read_ptr && ig->read_ptr < ig->read_end) { 5 723 728
1235 5 ssize_t kept = ig->read_end - ig->read_ptr;
1236  
1237 5 ▶ if (needed < kept) {
Branch: 1 branch
BranchTrueFalseTotal
if (needed < kept) { 0 5 5
1238 IOL_DEB(fprintf(IOL_DEBs, "i_io_read_fill(%u) -> 1 (already have enough)\n", (unsigned)needed));
1239 0 return 1;
1240 }
1241  
1242 5 ▶ if (ig->read_ptr != ig->buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->read_ptr != ig->buffer) 1 4 5
1243 1 memmove(ig->buffer, ig->read_ptr, kept);
1244  
1245 5 good = 1; /* we have *something* available to read */
1246 5 work = buf_start + kept;
1247 5 needed -= kept;
1248 }
1249 else {
1250 3834 work = ig->buffer;
1251 }
1252  
1253 /* there should always be buffer space the first time around, but
1254 avoid a compiler warning here */
1255 3839 rc = -1;
1256 3888 ▶ while (work < buf_end && (rc = i_io_raw_read(ig, work, buf_end - work)) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (work < buf_end && (rc = i_io_raw_read(ig, work, buf_end - work)) > 0) { 3887 1 3888
while (work < buf_end && (rc = i_io_raw_read(ig, work, buf_end - work)) > 0) { 3772 115 3887
1257 3772 work += rc;
1258 3772 good = 1;
1259 3772 ▶ if (needed < rc)
Branch: 1 branch
BranchTrueFalseTotal
if (needed < rc) 3723 49 3772
1260 3723 break;
1261  
1262 49 needed -= rc;
1263 }
1264  
1265 3839 ▶ if (rc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { 8 3831 3839
1266 8 ig->error = 1;
1267 IOL_DEB(fprintf(IOL_DEBs, " i_io_read_fill -> rc %d, setting error\n",
1268 (int)rc));
1269 }
1270 3831 ▶ else if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (rc == 0) { 107 3724 3831
1271 107 ig->buf_eof = 1;
1272 IOL_DEB(fprintf(IOL_DEBs, " i_io_read_fill -> rc 0, setting eof\n"));
1273 }
1274  
1275 3839 ▶ if (good) {
Branch: 1 branch
BranchTrueFalseTotal
if (good) { 3770 69 3839
1276 3770 ig->read_ptr = buf_start;
1277 3770 ig->read_end = work;
1278 }
1279
1280 IOL_DEB(fprintf(IOL_DEBs, "i_io_read_fill => %d, %u buffered\n", good,
1281 (unsigned)(ig->read_end - ig->read_ptr)));
1282 3839 return good;
1283 }
1284  
1285 /*
1286 =item dump_data(start, end, bias)
1287  
1288 Hex dump the data between C<start> and C<end>.
1289  
1290 If there is more than a pleasing amount of data, either dump the
1291 beginning (C<bias == 0>) or dump the end C(<bias != 0>) of the range.
1292  
1293 =cut
1294 */
1295  
1296 static void
1297 0 dump_data(unsigned char *start, unsigned char *end, int bias) {
1298 unsigned char *p;
1299 0 size_t count = end - start;
1300  
1301 0 ▶ if (start == end) {
Branch: 1 branch
BranchTrueFalseTotal
if (start == end) { 0 0 0
1302 0 fprintf(IOL_DEBs, "(empty)");
1303 0 return;
1304 }
1305  
1306 0 ▶ if (count > 15) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 15) { 0 0 0
1307 0 ▶ if (bias) {
Branch: 1 branch
BranchTrueFalseTotal
if (bias) { 0 0 0
1308 0 fprintf(IOL_DEBs, "... ");
1309 0 start = end - 14;
1310 }
1311 else {
1312 0 end = start + 14;
1313 }
1314
1315 0 ▶ for (p = start; p < end; ++p) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = start; p < end; ++p) { 0 0 0
1316 0 fprintf(IOL_DEBs, " %02x", *p);
1317 }
1318 0 putc(' ', IOL_DEBs);
1319 0 putc('<', IOL_DEBs);
1320 0 ▶ for (p = start; p < end; ++p) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = start; p < end; ++p) { 0 0 0
1321 0 ▶ if (*p < ' ' || *p > '~')
Branch: 2 branches
BranchTrueFalseTotal
if (*p < ' ' || *p > '~') 0 0 0
if (*p < ' ' || *p > '~') 0 0 0
1322 0 putc('.', IOL_DEBs);
1323 else
1324 0 putc(*p, IOL_DEBs);
1325 }
1326 0 putc('>', IOL_DEBs);
1327 0 ▶ if (!bias)
Branch: 1 branch
BranchTrueFalseTotal
if (!bias) 0 0 0
1328 0 fprintf(IOL_DEBs, " ...");
1329 }
1330 else {
1331 0 ▶ for (p = start; p < end; ++p) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = start; p < end; ++p) { 0 0 0
1332 0 fprintf(IOL_DEBs, " %02x", *p);
1333 }
1334 0 putc(' ', IOL_DEBs);
1335 0 ▶ for (p = start; p < end; ++p) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = start; p < end; ++p) { 0 0 0
1336 0 ▶ if (*p < ' ' || *p > '~')
Branch: 2 branches
BranchTrueFalseTotal
if (*p < ' ' || *p > '~') 0 0 0
if (*p < ' ' || *p > '~') 0 0 0
1337 0 putc('.', IOL_DEBs);
1338 else
1339 0 putc(*p, IOL_DEBs);
1340 }
1341 }
1342 }
1343  
1344 /*
1345 =item realseek_read(ig, buf, count)
1346  
1347 Does the reading from a source that can be seeked on
1348  
1349 ig - io_glue object
1350 buf - buffer to return data in
1351 count - number of bytes to read into buffer max
1352  
1353 =cut
1354 */
1355  
1356 static
1357 ssize_t
1358 160 realseek_read(io_glue *igo, void *buf, size_t count) {
1359 160 io_cb *ig = (io_cb *)igo;
1360 160 void *p = ig->p;
1361 160 ssize_t rc = 0;
1362  
1363 IOL_DEB( fprintf(IOL_DEBs, "realseek_read: buf = %p, count = %u\n",
1364 buf, (unsigned)count) );
1365 160 ▶ if (!ig->readcb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->readcb) { 0 160 160
1366 0 errno = EINVAL;
1367 0 return -1;
1368 }
1369 160 rc = ig->readcb(p,buf,count);
1370  
1371 IOL_DEB( fprintf(IOL_DEBs, "realseek_read: rc = %d\n", (int)rc) );
1372  
1373 160 return rc;
1374 }
1375  
1376  
1377 /*
1378 =item realseek_write(ig, buf, count)
1379  
1380 Does the writing to a 'source' that can be seeked on
1381  
1382 ig - io_glue object
1383 buf - buffer that contains data
1384 count - number of bytes to write
1385  
1386 =cut
1387 */
1388  
1389 static
1390 ssize_t
1391 2080 realseek_write(io_glue *igo, const void *buf, size_t count) {
1392 2080 io_cb *ig = (io_cb *)igo;
1393 2080 void *p = ig->p;
1394 2080 ssize_t rc = 0;
1395 2080 ssize_t bc = 0;
1396 2080 char *cbuf = (char*)buf;
1397
1398 IOL_DEB( fprintf(IOL_DEBs, "realseek_write: ig = %p, buf = %p, "
1399 "count = %u\n", ig, buf, (unsigned)count) );
1400  
1401 2080 ▶ if (!ig->writecb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->writecb) { 0 2080 2080
1402 0 errno = EINVAL;
1403 0 return -1;
1404 }
1405  
1406 /* Is this a good idea? Would it be better to handle differently?
1407 skip handling? */
1408 4104 ▶ while( (ssize_t)count != bc && (rc = ig->writecb(p, cbuf+bc, count-bc)) > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
while( (ssize_t)count != bc && (rc = ig->writecb(p, cbuf+bc, count-bc)) > 0 ) { 2080 2024 4104
while( (ssize_t)count != bc && (rc = ig->writecb(p, cbuf+bc, count-bc)) > 0 ) { 2024 56 2080
1409 2024 bc+=rc;
1410 }
1411  
1412 IOL_DEB( fprintf(IOL_DEBs, "realseek_write: rc = %d, bc = %u\n", (int)rc, (unsigned)bc) );
1413 2080 ▶ return rc < 0 ? rc : bc;
Branch: 1 branch
BranchTrueFalseTotal
return rc < 0 ? rc : bc; 55 2025 2080
1414 }
1415  
1416  
1417 /*
1418 =item realseek_close(ig)
1419  
1420 Closes a source that can be seeked on. Not sure if this should be an
1421 actual close or not. Does nothing for now. Should be fixed.
1422  
1423 ig - data source
1424  
1425 =cut */
1426  
1427 static
1428 int
1429 38 realseek_close(io_glue *igo) {
1430 38 io_cb *ig = (io_cb *)igo;
1431 38 dIMCTXio(igo);
1432  
1433 IOL_DEB(fprintf(IOL_DEBs, "realseek_close(%p)\n", ig));
1434 38 im_log((aIMCTX,1, "realseek_close(ig %p)\n", ig));
1435 38 ▶ if (ig->closecb)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->closecb) 38 0 38
1436 38 return ig->closecb(ig->p);
1437 else
1438 0 return 0;
1439 }
1440  
1441  
1442 /*
1443 =item realseek_seek(ig, offset, whence)
1444  
1445 Implements seeking for a source that is seekable, the purpose of having this is to be able to
1446 have an offset into a file that is different from what the underlying library thinks.
1447  
1448 ig - data source
1449 offset - offset into stream
1450 whence - whence argument a la lseek
1451  
1452 =cut
1453 */
1454  
1455 static
1456 off_t
1457 13 realseek_seek(io_glue *igo, off_t offset, int whence) {
1458 13 io_cb *ig = (io_cb *)igo;
1459 13 void *p = ig->p;
1460 off_t rc;
1461 IOL_DEB( fprintf(IOL_DEBs, "realseek_seek(ig %p, offset %ld, whence %d)\n", ig, (long) offset, whence) );
1462 13 ▶ if (!ig->seekcb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->seekcb) { 0 13 13
1463 0 errno = ESPIPE;
1464 0 return -1;
1465 }
1466 13 rc = ig->seekcb(p, offset, whence);
1467  
1468 IOL_DEB( fprintf(IOL_DEBs, "realseek_seek: rc %ld\n", (long) rc) );
1469 13 return rc;
1470 /* FIXME: How about implementing this offset handling stuff? */
1471 }
1472  
1473 static
1474 void
1475 132 realseek_destroy(io_glue *igo) {
1476 132 io_cb *ig = (io_cb *)igo;
1477  
1478 132 ▶ if (ig->destroycb)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->destroycb) 132 0 132
1479 132 ig->destroycb(ig->p);
1480 132 }
1481  
1482 static off_t
1483 3 realseek_size(io_glue *igo) {
1484 3 io_cb * const ig = (io_cb *)igo;
1485 3 void *p = ig->p;
1486  
1487 3 off_t result = (off_t)-1;
1488 3 ▶ if (ig->sizecb) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->sizecb) { 3 0 3
1489 3 result = ig->sizecb(p);
1490 }
1491 3 ▶ if (result == (off_t)-1 && ig->seekcb) {
Branch: 2 branches
BranchTrueFalseTotal
if (result == (off_t)-1 && ig->seekcb) { 1 2 3
if (result == (off_t)-1 && ig->seekcb) { 1 0 1
1492 /* try to figure it out */
1493 1 const off_t orig_pos = realseek_seek(igo, 0, SEEK_CUR);
1494 1 ▶ if (orig_pos != (off_t)-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (orig_pos != (off_t)-1) { 1 0 1
1495 1 result = realseek_seek(igo, 0, SEEK_END);
1496 1 realseek_seek(igo, orig_pos, SEEK_SET);
1497 }
1498 }
1499  
1500 3 return result;
1501 }
1502  
1503 /*
1504 * Callbacks for sources that are a fixed size buffer
1505 */
1506  
1507 /*
1508 =item buffer_read(ig, buf, count)
1509  
1510 Does the reading from a buffer source
1511  
1512 ig - io_glue object
1513 buf - buffer to return data in
1514 count - number of bytes to read into buffer max
1515  
1516 =cut
1517 */
1518  
1519 static
1520 ssize_t
1521 21865 buffer_read(io_glue *igo, void *buf, size_t count) {
1522 21865 io_buffer *ig = (io_buffer *)igo;
1523  
1524 IOL_DEB( fprintf(IOL_DEBs, "buffer_read: ig->cpos = %ld, buf = %p, count = %u\n", (long) ig->cpos, buf, (unsigned)count) );
1525  
1526 21865 ▶ if ( ig->cpos+count > ig->len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ig->cpos+count > ig->len ) { 785 21080 21865
1527 785 dIMCTXio(igo);
1528 785 im_log((aIMCTX, 1,"buffer_read: short read: cpos=%ld, len=%ld, count=%ld\n", (long)ig->cpos, (long)ig->len, (long)count));
1529 785 count = ig->len - ig->cpos;
1530 }
1531
1532 21865 memcpy(buf, ig->data+ig->cpos, count);
1533 21865 ig->cpos += count;
1534 IOL_DEB( fprintf(IOL_DEBs, "buffer_read: count = %ld\n", (long)count) );
1535 21865 return count;
1536 }
1537  
1538  
1539 /*
1540 =item buffer_write(ig, buf, count)
1541  
1542 Does nothing, returns -1
1543  
1544 ig - io_glue object
1545 buf - buffer that contains data
1546 count - number of bytes to write
1547  
1548 =cut
1549 */
1550  
1551 static
1552 ssize_t
1553 0 buffer_write(io_glue *ig, const void *buf, size_t count) {
1554 0 dIMCTXio(ig);
1555 (void)buf;
1556 (void)count;
1557 0 im_log((aIMCTX, 1, "buffer_write called, this method should never be called.\n"));
1558 0 return -1;
1559 }
1560  
1561  
1562 /*
1563 =item buffer_close(ig)
1564  
1565 Closes a source that can be seeked on. Not sure if this should be an actual close
1566 or not. Does nothing for now. Should be fixed.
1567  
1568 ig - data source
1569  
1570 =cut
1571 */
1572  
1573 static
1574 int
1575 29 buffer_close(io_glue *ig) {
1576 29 dIMCTXio(ig);
1577 29 im_log((aIMCTX, 1, "buffer_close(ig %p)\n", ig));
1578  
1579 29 return 0;
1580 }
1581  
1582  
1583 /*
1584 =item buffer_seek(ig, offset, whence)
1585  
1586 Implements seeking for a buffer source.
1587  
1588 ig - data source
1589 offset - offset into stream
1590 whence - whence argument a la lseek
1591  
1592 =cut
1593 */
1594  
1595 static
1596 off_t
1597 1355 buffer_seek(io_glue *igo, off_t offset, int whence) {
1598 1355 io_buffer *ig = (io_buffer *)igo;
1599 1355 off_t reqpos =
1600 1355 ▶ calc_seek_offset(ig->cpos, ig->len, offset, whence);
Branch: 3 branches
BranchTrueFalseTotal
calc_seek_offset(ig->cpos, ig->len, offset, whence); 4 1351 1355
calc_seek_offset(ig->cpos, ig->len, offset, whence); 3 1 4
calc_seek_offset(ig->cpos, ig->len, offset, whence); 1 0 1
1601
1602 1355 ▶ if (reqpos > (off_t)ig->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (reqpos > (off_t)ig->len) { 3 1352 1355
1603 3 dIMCTXio(igo);
1604 3 im_log((aIMCTX, 1, "seeking out of readable range\n"));
1605 3 return (off_t)-1;
1606 }
1607 1352 ▶ if (reqpos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (reqpos < 0) { 1 1351 1352
1608 1 dIMCTXio(igo);
1609 1 i_push_error(0, "seek before beginning of file");
1610 1 return (off_t)-1;
1611 }
1612
1613 1351 ig->cpos = reqpos;
1614 IOL_DEB( fprintf(IOL_DEBs, "buffer_seek(ig %p, offset %ld, whence %d)\n", ig, (long) offset, whence) );
1615  
1616 1351 return reqpos;
1617 /* FIXME: How about implementing this offset handling stuff? */
1618 }
1619  
1620 static off_t
1621 1 buffer_size(io_glue *igo) {
1622 1 io_buffer *ig = (io_buffer *)igo;
1623  
1624 1 return ig->len;
1625 }
1626  
1627 static int
1628 0 buffer_mmap(io_glue *igo, const void **pdata, size_t *psize) {
1629 0 io_buffer *ig = (io_buffer *)igo;
1630  
1631 /* already mapped */
1632 0 *pdata = ig->data;
1633 0 *psize = ig->len;
1634  
1635 0 return 1;
1636 }
1637  
1638 static int
1639 0 buffer_munmap(io_glue *igo) {
1640 (void)igo;
1641 /* nothing to do */
1642 0 return 1;
1643 }
1644  
1645 static
1646 void
1647 204 buffer_destroy(io_glue *igo) {
1648 204 io_buffer *ig = (io_buffer *)igo;
1649  
1650 204 ▶ if (ig->closecb) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->closecb) { 204 0 204
1651 204 dIMCTXio(igo);
1652 204 im_log((aIMCTX, 1,"calling close callback %p for io_buffer\n",
1653 ig->closecb));
1654 204 ig->closecb(ig->closedata);
1655 }
1656 204 }
1657  
1658  
1659  
1660 /*
1661 * Callbacks for sources that are a chain of variable sized buffers
1662 */
1663  
1664  
1665  
1666 /* Helper functions for buffer chains */
1667  
1668 static
1669 io_blink*
1670 199 io_blink_new(void) {
1671 io_blink *ib;
1672  
1673 #if 0
1674 im_log((aIMCTX, 1, "io_blink_new()\n"));
1675 #endif
1676  
1677 199 ib = mymalloc(sizeof(io_blink));
1678  
1679 199 ib->next = NULL;
1680 199 ib->prev = NULL;
1681 199 ib->len = BBSIZ;
1682  
1683 199 memset(&ib->buf, 0, ib->len);
1684 199 return ib;
1685 }
1686  
1687  
1688  
1689 /*
1690 =item io_bchain_advance(ieb)
1691  
1692 Advances the buffer chain to the next link - extending if
1693 necessary. Also adjusts the cpos and tfill counters as needed.
1694  
1695 ieb - buffer chain object
1696  
1697 =cut
1698 */
1699  
1700 static
1701 void
1702 143 io_bchain_advance(io_ex_bchain *ieb) {
1703 143 ▶ if (ieb->cp->next == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ieb->cp->next == NULL) { 143 0 143
1704 143 ieb->tail = io_blink_new();
1705 143 ieb->tail->prev = ieb->cp;
1706 143 ieb->cp->next = ieb->tail;
1707  
1708 143 ieb->tfill = 0; /* Only set this if we added a new slice */
1709 }
1710 143 ieb->cp = ieb->cp->next;
1711 143 ieb->cpos = 0;
1712 143 }
1713  
1714  
1715  
1716 /*
1717 =item io_bchain_destroy()
1718  
1719 frees all resources used by a buffer chain.
1720  
1721 =cut
1722 */
1723  
1724 static void
1725 56 io_destroy_bufchain(io_ex_bchain *ieb) {
1726 io_blink *cp;
1727 #if 0
1728 mm_log((1, "io_destroy_bufchain(ieb %p)\n", ieb));
1729 #endif
1730 56 cp = ieb->head;
1731
1732 255 ▶ while(cp) {
Branch: 1 branch
BranchTrueFalseTotal
while(cp) { 199 56 255
1733 199 io_blink *t = cp->next;
1734 199 myfree(cp);
1735 199 cp = t;
1736 }
1737 56 }
1738  
1739  
1740  
1741  
1742 /*
1743  
1744 static
1745 void
1746 bufchain_dump(io_ex_bchain *ieb) {
1747 mm_log((1, " buf_chain_dump(ieb %p)\n"));
1748 mm_log((1, " buf_chain_dump: ieb->offset = %d\n", ieb->offset));
1749 mm_log((1, " buf_chain_dump: ieb->length = %d\n", ieb->length));
1750 mm_log((1, " buf_chain_dump: ieb->head = %p\n", ieb->head ));
1751 mm_log((1, " buf_chain_dump: ieb->tail = %p\n", ieb->tail ));
1752 mm_log((1, " buf_chain_dump: ieb->tfill = %d\n", ieb->tfill ));
1753 mm_log((1, " buf_chain_dump: ieb->cp = %p\n", ieb->cp ));
1754 mm_log((1, " buf_chain_dump: ieb->cpos = %d\n", ieb->cpos ));
1755 mm_log((1, " buf_chain_dump: ieb->gpos = %d\n", ieb->gpos ));
1756 }
1757 */
1758  
1759 /*
1760 * TRUE if lengths are NOT equal
1761 */
1762  
1763 /*
1764 static
1765 void
1766 chainlencert( io_glue *ig ) {
1767 int clen;
1768 int cfl = 0;
1769 size_t csize = 0;
1770 size_t cpos = 0;
1771 io_ex_bchain *ieb = ig->exdata;
1772 io_blink *cp = ieb->head;
1773
1774  
1775 if (ieb->gpos > ieb->length) mm_log((1, "BBAR : ieb->gpos = %d, ieb->length = %d\n", ieb->gpos, ieb->length));
1776  
1777 while(cp) {
1778 clen = (cp == ieb->tail) ? ieb->tfill : cp->len;
1779 if (ieb->head == cp && cp->prev) mm_log((1, "Head of chain has a non null prev\n"));
1780 if (ieb->tail == cp && cp->next) mm_log((1, "Tail of chain has a non null next\n"));
1781
1782 if (ieb->head != cp && !cp->prev) mm_log((1, "Middle of chain has a null prev\n"));
1783 if (ieb->tail != cp && !cp->next) mm_log((1, "Middle of chain has a null next\n"));
1784
1785 if (cp->prev && cp->prev->next != cp) mm_log((1, "%p = cp->prev->next != cp\n", cp->prev->next));
1786 if (cp->next && cp->next->prev != cp) mm_log((1, "%p cp->next->prev != cp\n", cp->next->prev));
1787  
1788 if (cp == ieb->cp) {
1789 cfl = 1;
1790 cpos += ieb->cpos;
1791 }
1792  
1793 if (!cfl) cpos += clen;
1794  
1795 csize += clen;
1796 cp = cp->next;
1797 }
1798 if (( csize != ieb->length )) mm_log((1, "BAR : csize = %d, ieb->length = %d\n", csize, ieb->length));
1799 if (( cpos != ieb->gpos )) mm_log((1, "BAR : cpos = %d, ieb->gpos = %d\n", cpos, ieb->gpos ));
1800 }
1801  
1802  
1803 static
1804 void
1805 chaincert( io_glue *ig) {
1806 size_t csize = 0;
1807 io_ex_bchain *ieb = ig->exdata;
1808 io_blink *cp = ieb->head;
1809
1810 mm_log((1, "Chain verification.\n"));
1811  
1812 mm_log((1, " buf_chain_dump: ieb->offset = %d\n", ieb->offset));
1813 mm_log((1, " buf_chain_dump: ieb->length = %d\n", ieb->length));
1814 mm_log((1, " buf_chain_dump: ieb->head = %p\n", ieb->head ));
1815 mm_log((1, " buf_chain_dump: ieb->tail = %p\n", ieb->tail ));
1816 mm_log((1, " buf_chain_dump: ieb->tfill = %d\n", ieb->tfill ));
1817 mm_log((1, " buf_chain_dump: ieb->cp = %p\n", ieb->cp ));
1818 mm_log((1, " buf_chain_dump: ieb->cpos = %d\n", ieb->cpos ));
1819 mm_log((1, " buf_chain_dump: ieb->gpos = %d\n", ieb->gpos ));
1820  
1821 while(cp) {
1822 int clen = cp == ieb->tail ? ieb->tfill : cp->len;
1823 mm_log((1, "link: %p <- %p -> %p\n", cp->prev, cp, cp->next));
1824 if (ieb->head == cp && cp->prev) mm_log((1, "Head of chain has a non null prev\n"));
1825 if (ieb->tail == cp && cp->next) mm_log((1, "Tail of chain has a non null next\n"));
1826
1827 if (ieb->head != cp && !cp->prev) mm_log((1, "Middle of chain has a null prev\n"));
1828 if (ieb->tail != cp && !cp->next) mm_log((1, "Middle of chain has a null next\n"));
1829
1830 if (cp->prev && cp->prev->next != cp) mm_log((1, "%p = cp->prev->next != cp\n", cp->prev->next));
1831 if (cp->next && cp->next->prev != cp) mm_log((1, "%p cp->next->prev != cp\n", cp->next->prev));
1832  
1833 csize += clen;
1834 cp = cp->next;
1835 }
1836  
1837 mm_log((1, "csize = %d %s ieb->length = %d\n", csize, csize == ieb->length ? "==" : "!=", ieb->length));
1838 }
1839 */
1840  
1841 /*
1842 =item bufchain_read(ig, buf, count)
1843  
1844 Does the reading from a source that can be seeked on
1845  
1846 ig - io_glue object
1847 buf - buffer to return data in
1848 count - number of bytes to read into buffer max
1849  
1850 =cut
1851 */
1852  
1853 static
1854 ssize_t
1855 49 bufchain_read(io_glue *ig, void *buf, size_t count) {
1856 49 io_ex_bchain *ieb = ig->exdata;
1857 49 size_t scount = count;
1858 49 char *cbuf = buf;
1859 size_t sk;
1860 49 dIMCTXio(ig);
1861  
1862 49 im_log((aIMCTX, 1, "bufchain_read(ig %p, buf %p, count %ld)\n", ig, buf, (long)count));
1863  
1864 241 ▶ while( scount ) {
Branch: 1 branch
BranchTrueFalseTotal
while( scount ) { 193 48 241
1865 193 ▶ int clen = (ieb->cp == ieb->tail) ? ieb->tfill : (off_t)ieb->cp->len;
Branch: 1 branch
BranchTrueFalseTotal
int clen = (ieb->cp == ieb->tail) ? ieb->tfill : (off_t)ieb->cp->len; 29 164 193
1866 193 ▶ if (clen == ieb->cpos) {
Branch: 1 branch
BranchTrueFalseTotal
if (clen == ieb->cpos) { 144 49 193
1867 144 ▶ if (ieb->cp == ieb->tail) break; /* EOF */
Branch: 1 branch
BranchTrueFalseTotal
if (ieb->cp == ieb->tail) break; /* EOF */ 1 143 144
1868 143 ieb->cp = ieb->cp->next;
1869 143 ieb->cpos = 0;
1870 143 ▶ clen = (ieb->cp == ieb->tail) ? ieb->tfill : (off_t)ieb->cp->len;
Branch: 1 branch
BranchTrueFalseTotal
clen = (ieb->cp == ieb->tail) ? ieb->tfill : (off_t)ieb->cp->len; 21 122 143
1871 }
1872  
1873 192 sk = clen - ieb->cpos;
1874 192 sk = sk > scount ? scount : sk;
1875  
1876 192 memcpy(&cbuf[count-scount], &ieb->cp->buf[ieb->cpos], sk);
1877 192 scount -= sk;
1878 192 ieb->cpos += sk;
1879 192 ieb->gpos += sk;
1880 }
1881  
1882 49 im_log((aIMCTX, 1, "bufchain_read: returning %ld\n", (long)(count-scount)));
1883 49 return count-scount;
1884 }
1885  
1886  
1887  
1888  
1889  
1890 /*
1891 =item bufchain_write(ig, buf, count)
1892  
1893 Does the writing to a 'source' that can be seeked on
1894  
1895 ig - io_glue object
1896 buf - buffer that contains data
1897 count - number of bytes to write
1898  
1899 =cut
1900 */
1901  
1902 static
1903 ssize_t
1904 279 bufchain_write(io_glue *ig, const void *buf, size_t count) {
1905 279 char *cbuf = (char *)buf;
1906 279 io_ex_bchain *ieb = ig->exdata;
1907 279 size_t ocount = count;
1908 size_t sk;
1909 279 dIMCTXio(ig);
1910  
1911 279 im_log((aIMCTX, 1, "bufchain_write: ig = %p, buf = %p, count = %ld\n", ig, buf, (long)count));
1912  
1913 IOL_DEB( fprintf(IOL_DEBs, "bufchain_write: ig = %p, ieb->cpos = %ld, buf = %p, count = %ld\n", ig, (long) ieb->cpos, buf, (long)count) );
1914
1915 689 ▶ while(count) {
Branch: 1 branch
BranchTrueFalseTotal
while(count) { 410 279 689
1916 410 im_log((aIMCTX, 2, "bufchain_write: - looping - count = %ld\n", (long)count));
1917 410 ▶ if ((off_t)ieb->cp->len == ieb->cpos) {
Branch: 1 branch
BranchTrueFalseTotal
if ((off_t)ieb->cp->len == ieb->cpos) { 143 267 410
1918 143 im_log((aIMCTX, 1, "bufchain_write: cp->len == ieb->cpos = %ld - advancing chain\n", (long) ieb->cpos));
1919 143 io_bchain_advance(ieb);
1920 }
1921  
1922 410 sk = ieb->cp->len - ieb->cpos;
1923 410 sk = sk > count ? count : sk;
1924 410 memcpy(&ieb->cp->buf[ieb->cpos], &cbuf[ocount-count], sk);
1925  
1926 410 ▶ if (ieb->cp == ieb->tail) {
Branch: 1 branch
BranchTrueFalseTotal
if (ieb->cp == ieb->tail) { 409 1 410
1927 409 int extend = ieb->cpos + sk - ieb->tfill;
1928 409 im_log((aIMCTX, 2, "bufchain_write: extending tail by %d\n", extend));
1929 409 ▶ if (extend > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (extend > 0) { 409 0 409
1930 409 ieb->length += extend;
1931 409 ieb->tfill += extend;
1932 }
1933 }
1934  
1935 410 ieb->cpos += sk;
1936 410 ieb->gpos += sk;
1937 410 count -= sk;
1938 }
1939 279 return ocount;
1940 }
1941  
1942 static off_t
1943 1 bufchain_size(io_glue *ig) {
1944 1 io_ex_bchain *ieb = ig->exdata;
1945 1 return ieb->length;
1946 }
1947  
1948 /*
1949 =item bufchain_close(ig)
1950  
1951 Closes a source that can be seeked on. Not sure if this should be an actual close
1952 or not. Does nothing for now. Should be fixed.
1953  
1954 ig - data source
1955  
1956 =cut
1957 */
1958  
1959 static
1960 int
1961 47 bufchain_close(io_glue *ig) {
1962 47 dIMCTXio(ig);
1963 47 im_log((aIMCTX, 1, "bufchain_close(ig %p)\n",ig));
1964 IOL_DEB( fprintf(IOL_DEBs, "bufchain_close(ig %p)\n", ig) );
1965  
1966 47 return 0;
1967 }
1968  
1969  
1970 /*
1971 =item bufchain_seek(ig, offset, whence)
1972  
1973 Implements seeking for a source that is seekable, the purpose of having this is to be able to
1974 have an offset into a file that is different from what the underlying library thinks.
1975  
1976 ig - data source
1977 offset - offset into stream
1978 whence - whence argument a la lseek
1979  
1980 =cut
1981 */
1982  
1983 static
1984 off_t
1985 52 bufchain_seek(io_glue *ig, off_t offset, int whence) {
1986 52 io_ex_bchain *ieb = ig->exdata;
1987 int wrlen;
1988  
1989 52 ▶ off_t scount = calc_seek_offset(ieb->gpos, ieb->length, offset, whence);
Branch: 3 branches
BranchTrueFalseTotal
off_t scount = calc_seek_offset(ieb->gpos, ieb->length, offset, whence); 3 49 52
off_t scount = calc_seek_offset(ieb->gpos, ieb->length, offset, whence); 2 1 3
off_t scount = calc_seek_offset(ieb->gpos, ieb->length, offset, whence); 1 0 1
1990 off_t sk;
1991 52 dIMCTXio(ig);
1992  
1993 52 im_log((aIMCTX, 1, "bufchain_seek(ig %p, offset %ld, whence %d)\n", ig, (long)offset, whence));
1994  
1995 52 ▶ if (scount < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scount < 0) { 0 52 52
1996 0 i_push_error(0, "invalid whence supplied or seek before start of file");
1997 0 return (off_t)-1;
1998 }
1999  
2000 52 ieb->cp = ieb->head;
2001 52 ieb->cpos = 0;
2002 52 ieb->gpos = 0;
2003
2004 56 ▶ while( scount ) {
Branch: 1 branch
BranchTrueFalseTotal
while( scount ) { 4 52 56
2005 4 ▶ int clen = (ieb->cp == ieb->tail) ? ieb->tfill : (off_t)ieb->cp->len;
Branch: 1 branch
BranchTrueFalseTotal
int clen = (ieb->cp == ieb->tail) ? ieb->tfill : (off_t)ieb->cp->len; 3 1 4
2006 4 ▶ if (clen == ieb->cpos) {
Branch: 1 branch
BranchTrueFalseTotal
if (clen == ieb->cpos) { 0 4 4
2007 0 ▶ if (ieb->cp == ieb->tail) break; /* EOF */
Branch: 1 branch
BranchTrueFalseTotal
if (ieb->cp == ieb->tail) break; /* EOF */ 0 0 0
2008 0 ieb->cp = ieb->cp->next;
2009 0 ieb->cpos = 0;
2010 0 ▶ clen = (ieb->cp == ieb->tail) ? ieb->tfill : (off_t)ieb->cp->len;
Branch: 1 branch
BranchTrueFalseTotal
clen = (ieb->cp == ieb->tail) ? ieb->tfill : (off_t)ieb->cp->len; 0 0 0
2011 }
2012
2013 4 sk = clen - ieb->cpos;
2014 4 sk = sk > scount ? scount : sk;
2015
2016 4 scount -= sk;
2017 4 ieb->cpos += sk;
2018 4 ieb->gpos += sk;
2019 }
2020
2021 52 wrlen = scount;
2022  
2023 52 ▶ if (wrlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (wrlen > 0) { 0 52 52
2024 /*
2025 * extending file - get ieb into consistent state and then
2026 * call write which will get it to the correct position
2027 */
2028 char TB[BBSIZ];
2029 0 memset(TB, 0, BBSIZ);
2030 0 ieb->gpos = ieb->length;
2031 0 ieb->cpos = ieb->tfill;
2032
2033 0 ▶ while(wrlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while(wrlen > 0) { 0 0 0
2034 0 ssize_t rc, wl = i_min(wrlen, BBSIZ);
2035 0 im_log((aIMCTX, 1, "bufchain_seek: wrlen = %d, wl = %ld\n", wrlen, (long)wl));
2036 0 rc = bufchain_write( ig, TB, wl );
2037 0 ▶ if (rc != wl) im_fatal(aIMCTX, 0, "bufchain_seek: Unable to extend file\n");
Branch: 1 branch
BranchTrueFalseTotal
if (rc != wl) im_fatal(aIMCTX, 0, "bufchain_seek: Unable to extend file\n"); 0 0 0
2038 0 wrlen -= rc;
2039 }
2040 }
2041  
2042 52 im_log((aIMCTX, 2, "bufchain_seek: returning ieb->gpos = %ld\n", (long)ieb->gpos));
2043 52 return ieb->gpos;
2044 }
2045  
2046 static
2047 void
2048 56 bufchain_destroy(io_glue *ig) {
2049 56 io_ex_bchain *ieb = ig->exdata;
2050  
2051 56 io_destroy_bufchain(ieb);
2052  
2053 56 myfree(ieb);
2054 56 }
2055  
2056 /*
2057 =item fd_read(ig, buf, count)
2058  
2059 Read callback for file descriptor IO objects.
2060  
2061 =cut
2062 */
2063 1896 static ssize_t fd_read(io_glue *igo, void *buf, size_t count) {
2064 1896 io_fdseek *ig = (io_fdseek *)igo;
2065 ssize_t result;
2066 #ifdef _MSC_VER
2067 result = _read(ig->fd, buf, count);
2068 #else
2069 1896 result = read(ig->fd, buf, count);
2070 #endif
2071  
2072 IOL_DEB(fprintf(IOL_DEBs, "fd_read(%p, %p, %u) => %d\n", ig, buf,
2073 (unsigned)count, (int)result));
2074  
2075 /* 0 is valid - means EOF */
2076 1896 ▶ if (result < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (result < 0) { 1 1895 1896
2077 1 dIMCTXio(igo);
2078 1 im_push_errorf(aIMCTX, 0, "read() failure: %s (%d)", my_strerror(errno), errno);
2079 }
2080  
2081 1896 return result;
2082 }
2083  
2084 532 static ssize_t fd_write(io_glue *igo, const void *buf, size_t count) {
2085 532 io_fdseek *ig = (io_fdseek *)igo;
2086 ssize_t result;
2087 #ifdef _MSC_VER
2088 result = _write(ig->fd, buf, count);
2089 #else
2090 532 result = write(ig->fd, buf, count);
2091 #endif
2092  
2093 IOL_DEB(fprintf(IOL_DEBs, "fd_write(%p, %p, %u) => %d\n", ig, buf,
2094 (unsigned)count, (int)result));
2095  
2096 532 ▶ if (result <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (result <= 0) { 1 531 532
2097 1 dIMCTXio(igo);
2098 1 im_push_errorf(aIMCTX, errno, "write() failure: %s (%d)", my_strerror(errno), errno);
2099 }
2100  
2101 532 return result;
2102 }
2103  
2104 1344 static off_t fd_seek(io_glue *igo, off_t offset, int whence) {
2105 1344 io_fdseek *ig = (io_fdseek *)igo;
2106 off_t result;
2107 #ifdef _MSC_VER
2108 result = _lseek(ig->fd, offset, whence);
2109 #else
2110 1344 result = lseek(ig->fd, offset, whence);
2111 #endif
2112  
2113 1344 ▶ if (result == (off_t)-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (result == (off_t)-1) { 1 1343 1344
2114 1 dIMCTXio(igo);
2115 1 im_push_errorf(aIMCTX, errno, "lseek() failure: %s (%d)", my_strerror(errno), errno);
2116 }
2117  
2118 1344 return result;
2119 }
2120  
2121 173 static int fd_close(io_glue *ig) {
2122 (void)ig;
2123 /* no, we don't close it */
2124 173 return 0;
2125 }
2126  
2127 static off_t
2128 1 fd_size(io_glue *igo) {
2129 1 io_fdseek *ig = (io_fdseek *)igo;
2130  
2131 1 ▶ if (!ig->size_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->size_set) { 1 0 1
2132 struct stat st;
2133 1 ▶ if (fstat(ig->fd, &st) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fstat(ig->fd, &st) == 0) { 1 0 1
2134 1 ig->size = st.st_size;
2135 }
2136 else {
2137 0 ig->size = -1;
2138 }
2139 1 ig->size_set = 1;
2140 }
2141
2142 1 return ig->size;
2143 }
2144  
2145 static int
2146 0 fd_mmap(io_glue *igo, const void **pdata, size_t *psize) {
2147 #if defined(IMAGER_POSIX_MMAP) || defined(WIN32)
2148 0 io_fdseek *ig = (io_fdseek *)igo;
2149  
2150 0 ▶ if (!ig->mapped) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->mapped) { 0 0 0
2151 0 dIMCTXio(igo);
2152 void *data;
2153  
2154 0 ▶ if (!ig->size_set)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->size_set) 0 0 0
2155 0 fd_size(igo);
2156 0 ▶ if (ig->size <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->size <= 0) 0 0 0
2157 0 return 0;
2158 #if IMAGER_PTR_SIZE == 4
2159 if (ig->size > im_ssize_t_max) {
2160 im_push_error(aIMCTX, 0, "file too large to mmap");
2161 return 0;
2162 }
2163 #endif
2164 0 ▶ if ((size_t)ig->size > aIMCTX->max_mmap_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)ig->size > aIMCTX->max_mmap_size) { 0 0 0
2165 0 im_push_error(aIMCTX, 0, "file larger than max mmap size");
2166 0 return 0;
2167 }
2168 # if defined(IMAGER_POSIX_MMAP)
2169 0 data = mmap(NULL, ig->size, PROT_READ, MAP_SHARED, ig->fd, 0);
2170 0 ▶ if (data == (void *)-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (data == (void *)-1) { 0 0 0
2171 0 return 0;
2172 }
2173 # else
2174 HANDLE h_mapping =
2175 CreateFileMapping((HANDLE)_get_osfhandle(ig->fd), NULL, PAGE_READONLY,
2176 0, 0, NULL);
2177 if (h_mapping == NULL) {
2178 im_push_errorf(aIMCTX, 0, "CreateFileMapping failure %lu",
2179 (unsigned long)GetLastError());
2180 return 0;
2181 }
2182 data = MapViewOfFile(h_mapping, FILE_MAP_READ, 0, 0, 0);
2183 CloseHandle(h_mapping);
2184 if (data == NULL) {
2185 im_push_errorf(aIMCTX, 0, "MapViewOfFile failure %lu",
2186 (unsigned long)GetLastError());
2187 return 0;
2188 }
2189 # endif
2190 0 ig->mapped = data;
2191 0 ig->map_size = ig->size;
2192 0 im_log((aIMCTX, 2, "mapped io %p to address %p size %zu\n",
2193 (void *)ig, data, ig->map_size));
2194 }
2195
2196 0 *pdata = ig->mapped;
2197 0 *psize = ig->size;
2198  
2199 0 return 1;
2200 #else
2201 (void)igo;
2202 *pdata = NULL;
2203 *psize = 0;
2204  
2205 return 0;
2206 #endif
2207 }
2208  
2209 static int
2210 0 fd_munmap(io_glue *igo) {
2211 #if defined(IMAGER_POSIX_MMAP) || defined(WIN32)
2212 0 io_fdseek *ig = (io_fdseek *)igo;
2213  
2214 0 ▶ if (ig->mapped) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->mapped) { 0 0 0
2215 0 dIMCTXio(igo);
2216 # ifdef IMAGER_POSIX_MMAP
2217 0 munmap(ig->mapped, ig->size);
2218 # else
2219 UnmapViewOfFile(ig->mapped);
2220 # endif
2221 0 im_log((aIMCTX, 2, "unmapped io %p from address %p\n",
2222 (void *)ig, ig->mapped));
2223 0 ig->mapped = NULL;
2224 0 return 1;
2225 }
2226 0 return 0;
2227 #else
2228 (void)igo;
2229
2230 return 0;
2231 #endif
2232 }
2233  
2234 static void
2235 358 fd_destroy(io_glue *igo) {
2236 #if defined(IMAGER_POSIX_MMAP)
2237 358 io_fdseek *ig = (io_fdseek *)igo;
2238  
2239 358 ▶ if (ig->mapped) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->mapped) { 0 358 358
2240 0 fd_munmap(igo);
2241 }
2242 #else
2243 (void)igo;
2244 #endif
2245
2246 358 }
2247  
2248 /*
2249 =item im_io_get_max_mmap_size()
2250  
2251 size_t size = im_io_get_max_mmap_size(aIMCTX);
2252 size_t size = i_io_get_max_mmap_size();
2253  
2254 Fetch the maximum address space than can be mapped by i_io_mmap() for
2255 files.
2256  
2257 =cut
2258 */
2259  
2260 size_t
2261 2 im_io_get_max_mmap_size(pIMCTX) {
2262 2 return aIMCTX->max_mmap_size;
2263 }
2264  
2265 /*
2266 =item im_io_set_max_mmap_size()
2267  
2268 int ok size = im_io_set_max_mmap_size(aIMCTX, new_size);
2269 int ok size = i_io_set_max_mmap_size(new_size);
2270  
2271 Set the maximum address space than can be mapped by i_io_mmap() for
2272 files.
2273  
2274 =cut
2275 */
2276  
2277 int
2278 2 im_io_set_max_mmap_size(pIMCTX, size_t new_size) {
2279 2 im_clear_error(aIMCTX);
2280  
2281 2 ▶ if (new_size > im_size_t_max / 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_size > im_size_t_max / 2) { 0 2 2
2282 0 im_push_error(aIMCTX, 0, "set_max_map_size: new size too large (> half address space)");
2283 0 return 0;
2284 }
2285 2 aIMCTX->max_mmap_size = new_size;
2286  
2287 2 return 1;
2288 }
2289  
2290  
2291 /*
2292 =back
2293  
2294 =head1 AUTHOR
2295  
2296 Arnar M. Hrafnkelsson <addi@umich.edu>
2297  
2298 =head1 SEE ALSO
2299  
2300 Imager(3)
2301  
2302 =cut
2303 */
2304  
2305 /*
2306  
2307 Document the I/O macros.
2308  
2309 =item i_io_type()
2310 =category I/O Layers
2311  
2312 Return the type of an I/O layer object.
2313  
2314 int type = i_io_type(io);
2315  
2316 =cut
2317 =item i_io_raw_read()
2318 =category I/O Layers
2319  
2320 Raw read from an I/O layer object. This does not mix well with the
2321 buffer I/O functions.
2322  
2323 char buf[SIZE];
2324 ssize_t rd_size = i_io_raw_read(io, buf, SIZE);
2325  
2326 =cut
2327 =item i_io_raw_write()
2328 =category I/O Layers
2329  
2330 Raw write to an I/O layer object. This does not mix well with the
2331 buffer I/O functions.
2332  
2333 char data[SIZE] = "...";
2334 ssize_t wr_size = i_io_raw_write(io, buf, SIZE);
2335  
2336 =cut
2337 =item i_io_raw_seek()
2338 =category I/O Layers
2339  
2340 Raw seek in an I/O layer object. This does not mix well with the
2341 buffer I/O functions.
2342  
2343 off_t pos = i_io_raw_seek(io, offset, seek_type);
2344  
2345 =cut
2346 =item i_io_raw_close()
2347 =category I/O Layers
2348  
2349 Raw close an I/O layer object. This will not flush any buffered data.
2350  
2351 int result = i_io_raw_close(io)
2352  
2353 =cut
2354 =item i_io_is_buffered()
2355 =category I/O Layers
2356  
2357 Returns true if the I/O layer object is buffered.
2358  
2359 =cut
2360 =item i_io_mmap()
2361 =category I/O Layers
2362  
2363 If the I/O object allows it, return the address and size of the data.
2364 For C<fd> objects this will attempt to mmap() the file into memory.
2365  
2366 The memory may not be written to.
2367  
2368 const void *p;
2369 size_t size;
2370 if (i_io_mmap(io, &p, &size)) {
2371 i_io_munmap(p);
2372 }
2373  
2374 =cut
2375 =item i_io_munmap()
2376 =category I/O Layers
2377  
2378 Remove the memory mapping created by i_io_mmap().
2379  
2380 i_io_munmap(io);
2381  
2382 =cut
2383 =item i_io_getc(io)
2384 =category I/O Layers
2385  
2386 Fetch a single character from a buffered I/O stream.
2387  
2388 =cut
2389 =item i_io_nextc()
2390 =category I/O Layers
2391  
2392 Currently broken.
2393  
2394 =cut
2395 =item i_io_putc()
2396 =category I/O Layers
2397  
2398 Write a single character to a buffered I/O stream.
2399  
2400 =cut
2401 =item i_io_eof()
2402 =category I/O Layers
2403  
2404 Return true if the stream is at end of file.
2405  
2406 =cut
2407 =item i_io_error()
2408 =category I/O Layers
2409  
2410 Return true if the stream is in an error state. i_io_seek() will
2411 clear the error state.
2412  
2413 =cut
2414  
2415 */
« rotate.im ↑ Summary img16.c »