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 53 im_io_new_bufchain(pIMCTX) {
248 io_glue *ig;
249 53 io_ex_bchain *ieb = mymalloc(sizeof(io_ex_bchain));
250  
251 53 im_log((aIMCTX, 1, "io_new_bufchain()\n"));
252  
253 53 ig = mymalloc(sizeof(io_glue));
254 53 memset(ig, 0, sizeof(*ig));
255 53 i_io_init(aIMCTX, ig, BUFCHAIN, &bufchain_vtable);
256  
257 53 ieb->offset = 0;
258 53 ieb->length = 0;
259 53 ieb->cpos = 0;
260 53 ieb->gpos = 0;
261 53 ieb->tfill = 0;
262
263 53 ieb->head = io_blink_new();
264 53 ieb->cp = ieb->head;
265 53 ieb->tail = ieb->head;
266
267 53 ig->exdata = ieb;
268  
269 53 im_context_refinc(aIMCTX, "im_io_new_bufchain");
270  
271 53 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 199 im_io_new_buffer(pIMCTX, const char *data, size_t len, i_io_closebufp_t closecb, void *closedata) {
294 io_buffer *ig;
295
296 199 im_log((aIMCTX, 1, "io_new_buffer(data %p, len %ld, closecb %p, closedata %p)\n", data, (long)len, closecb, closedata));
297  
298 199 ig = mymalloc(sizeof(io_buffer));
299 199 memset(ig, 0, sizeof(*ig));
300 199 i_io_init(aIMCTX, &ig->base, BUFFER, &buffer_vtable);
301 199 ig->data = data;
302 199 ig->len = len;
303 199 ig->closecb = closecb;
304 199 ig->closedata = closedata;
305  
306 199 ig->cpos = 0;
307
308 199 im_context_refinc(aIMCTX, "im_io_new_bufchain");
309  
310 199 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 44 io_slurp(io_glue *ig, unsigned char **c) {
448 ssize_t rc;
449 io_ex_bchain *ieb;
450 unsigned char *cc;
451 44 io_type inn = ig->type;
452
453 44 ▶ if ( inn != BUFCHAIN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( inn != BUFCHAIN ) { 0 44 44
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 44 ieb = ig->exdata;
459 44 cc = *c = mymalloc( ieb->length );
460
461 44 bufchain_seek(ig, 0, SEEK_SET);
462
463 44 rc = bufchain_read(ig, cc, ieb->length);
464  
465 44 ▶ if (rc != ieb->length) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != ieb->length) { 0 44 44
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 44 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 742 io_glue_destroy(io_glue *ig) {
490 742 dIMCTXio(ig);
491 742 im_log((aIMCTX, 1, "io_glue_DESTROY(ig %p)\n", ig));
492  
493 742 ▶ if (ig->vtbl->destroycb)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->vtbl->destroycb) 742 0 742
494 742 ig->vtbl->destroycb(ig);
495  
496 742 ▶ if (ig->buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->buffer) 654 88 742
497 654 myfree(ig->buffer);
498
499 742 myfree(ig);
500  
501 742 im_context_refdec(aIMCTX, "io_glue_destroy");
502 742 }
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 20075 i_io_getc_imp(io_glue *ig) {
517 20075 ▶ if (ig->write_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->write_ptr) 1 20074 20075
518 1 return EOF;
519
520 20074 ▶ if (ig->error || ig->buf_eof)
Branch: 2 branches
BranchTrueFalseTotal
if (ig->error || ig->buf_eof) 20074 0 20074
if (ig->error || ig->buf_eof) 4 20070 20074
521 4 return EOF;
522
523 20070 ▶ if (!ig->buffered) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffered) { 20004 66 20070
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 66 ▶ if (!ig->buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffer) 59 7 66
540 59 i_io_setup_buffer(ig);
541
542 66 ▶ 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 61 66
if (!ig->read_ptr || ig->read_ptr == ig->read_end) { 5 0 5
543 66 ▶ if (!i_io_read_fill(ig, 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!i_io_read_fill(ig, 1)) 4 62 66
544 4 return EOF;
545 }
546
547 62 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 631339 i_io_read(io_glue *ig, void *buf, size_t size) {
739 631339 unsigned char *pbuf = buf;
740 631339 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 631339 ▶ if (ig->write_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->write_ptr) { 1 631338 631339
745 IOL_DEB(fprintf(IOL_DEBs, "i_io_read() => -1 (write_ptr set)\n"));
746 1 return -1;
747 }
748  
749 631338 ▶ if (!ig->buffer && ig->buffered)
Branch: 2 branches
BranchTrueFalseTotal
if (!ig->buffer && ig->buffered) 157 631181 631338
if (!ig->buffer && ig->buffered) 156 1 157
750 156 i_io_setup_buffer(ig);
751  
752 631338 ▶ if (ig->read_ptr && ig->read_ptr < ig->read_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (ig->read_ptr && ig->read_ptr < ig->read_end) { 628492 2846 631338
if (ig->read_ptr && ig->read_ptr < ig->read_end) { 628439 53 628492
753 628439 size_t alloc = ig->read_end - ig->read_ptr;
754
755 628439 ▶ if (alloc > size)
Branch: 1 branch
BranchTrueFalseTotal
if (alloc > size) 627551 888 628439
756 627551 alloc = size;
757  
758 628439 memcpy(pbuf, ig->read_ptr, alloc);
759 628439 ig->read_ptr += alloc;
760 628439 pbuf += alloc;
761 628439 size -= alloc;
762 628439 read_total += alloc;
763 }
764  
765 631338 ▶ if (size > 0 && !(ig->error || ig->buf_eof)) {
Branch: 3 branches
BranchTrueFalseTotal
if (size > 0 && !(ig->error || ig->buf_eof)) { 3544 627794 631338
if (size > 0 && !(ig->error || ig->buf_eof)) { 3544 0 3544
if (size > 0 && !(ig->error || ig->buf_eof)) { 3540 4 3544
766 3545 ▶ if (!ig->buffered || size > ig->buf_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ig->buffered || size > ig->buf_size) { 3539 1 3540
if (!ig->buffered || size > ig->buf_size) { 5 3534 3539
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 3534 ▶ if (i_io_read_fill(ig, size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_read_fill(ig, size)) { 3481 53 3534
787 3481 size_t alloc = ig->read_end - ig->read_ptr;
788 3481 ▶ if (alloc > size)
Branch: 1 branch
BranchTrueFalseTotal
if (alloc > size) 3447 34 3481
789 3447 alloc = size;
790
791 3481 memcpy(pbuf, ig->read_ptr, alloc);
792 3481 ig->read_ptr += alloc;
793 3481 pbuf += alloc;
794 3481 size -= alloc;
795 3481 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 631334 ▶ if (!read_total && ig->error)
Branch: 2 branches
BranchTrueFalseTotal
if (!read_total && ig->error) 19 631315 631334
if (!read_total && ig->error) 0 19 19
807 0 read_total = -1;
808  
809 IOL_DEB(fprintf(IOL_DEBs, "i_io_read() => %d\n", (int)read_total));
810  
811 631334 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 501868 i_io_write(io_glue *ig, const void *buf, size_t size) {
828 501868 const unsigned char *pbuf = buf;
829 501868 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 501868 ▶ if (!ig->buffered) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffered) { 1937 499931 501868
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 499931 ▶ if (ig->read_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->read_ptr) { 1 499930 499931
854 IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => -1 (read_ptr set)\n"));
855 1 return -1;
856 }
857  
858 499930 ▶ if (ig->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->error) { 1 499929 499930
859 IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => -1 (error)\n"));
860 1 return -1;
861 }
862  
863 499929 ▶ if (!ig->buffer)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->buffer) 231 499698 499929
864 231 i_io_setup_buffer(ig);
865  
866 499929 ▶ if (!ig->write_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->write_ptr) 236 499693 499929
867 236 i_io_start_write(ig);
868  
869 499929 ▶ 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) { 499929 0 499929
if (ig->write_ptr && ig->write_ptr + size <= ig->write_end) { 499236 693 499929
870 499236 size_t alloc = ig->write_end - ig->write_ptr;
871 499236 ▶ if (alloc > size)
Branch: 1 branch
BranchTrueFalseTotal
if (alloc > size) 499121 115 499236
872 499121 alloc = size;
873 499236 memcpy(ig->write_ptr, pbuf, alloc);
874 499236 write_count += alloc;
875 499236 size -= alloc;
876 499236 pbuf += alloc;
877 499236 ig->write_ptr += alloc;
878 }
879  
880 499929 ▶ if (size) {
Branch: 1 branch
BranchTrueFalseTotal
if (size) { 693 499236 499929
881 693 ▶ if (!i_io_flush(ig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!i_io_flush(ig)) { 1 692 693
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 692 i_io_start_write(ig);
887
888 692 ▶ if (size > ig->buf_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > ig->buf_size) { 105 587 692
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 587 memcpy(ig->write_ptr, pbuf, size);
905 587 write_count += size;
906 587 ig->write_ptr += size;
907 }
908 }
909  
910 IOL_DEB(fprintf(IOL_DEBs, "i_io_write() => %d\n", (int)write_count));
911  
912 499927 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 933 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 933 ▶ if (ig->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->error) { 1 932 933
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 932 ▶ if (!ig->write_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!ig->write_ptr) 1 931 932
978 1 return 1;
979  
980 931 bufp = ig->buffer;
981 1738 ▶ while (bufp < ig->write_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
while (bufp < ig->write_ptr) { 816 922 1738
982 816 ssize_t rc = i_io_raw_write(ig, bufp, ig->write_ptr - bufp);
983 816 ▶ if (rc <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc <= 0) { 9 807 816
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 807 bufp += rc;
990 }
991  
992 922 ig->write_ptr = ig->write_end = NULL;
993  
994 IOL_DEB(fprintf(IOL_DEBs, "i_io_flush() => 1\n", ig));
995  
996 922 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 282 i_io_close(io_glue *ig) {
1012 282 int result = 0;
1013  
1014 IOL_DEB(fprintf(IOL_DEBs, "i_io_close(%p)\n", ig));
1015 282 ▶ if (ig->error)
Branch: 1 branch
BranchTrueFalseTotal
if (ig->error) 2 280 282
1016 2 result = -1;
1017  
1018 282 ▶ if (ig->write_ptr && !i_io_flush(ig))
Branch: 2 branches
BranchTrueFalseTotal
if (ig->write_ptr && !i_io_flush(ig)) 229 53 282
if (ig->write_ptr && !i_io_flush(ig)) 6 223 229
1019 6 result = -1;
1020  
1021 282 ▶ if (i_io_raw_close(ig))
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_raw_close(ig)) 13 269 282
1022 13 result = -1;
1023  
1024 IOL_DEB(fprintf(IOL_DEBs, "i_io_close() => %d\n", result));
1025  
1026 282 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 742 i_io_init(pIMCTX, io_glue *ig, int type, const i_io_glue_vtable_t *vtbl) {
1077 742 ig->type = type;
1078 742 ig->exdata = NULL;
1079  
1080 742 ig->vtbl = vtbl;
1081 742 ig->context = aIMCTX;
1082  
1083 742 ig->buffer = NULL;
1084 742 ig->read_ptr = NULL;
1085 742 ig->read_end = NULL;
1086 742 ig->write_ptr = NULL;
1087 742 ig->write_end = NULL;
1088 742 ig->buf_size = IO_BUF_SIZE;
1089 742 ig->buf_eof = 0;
1090 742 ig->error = 0;
1091 742 ig->buffered = 1;
1092 742 }
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 654 i_io_setup_buffer(io_glue *ig) {
1208 654 ig->buffer = mymalloc(ig->buf_size);
1209 654 }
1210  
1211 static void
1212 933 i_io_start_write(io_glue *ig) {
1213 933 ig->write_ptr = ig->buffer;
1214 933 ig->write_end = ig->buffer + ig->buf_size;
1215 933 }
1216  
1217 static int
1218 3813 i_io_read_fill(io_glue *ig, ssize_t needed) {
1219 3813 unsigned char *buf_end = ig->buffer + ig->buf_size;
1220 3813 unsigned char *buf_start = ig->buffer;
1221 3813 unsigned char *work = ig->buffer;
1222 ssize_t rc;
1223 3813 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 3813 ▶ if (ig->error || ig->buf_eof)
Branch: 2 branches
BranchTrueFalseTotal
if (ig->error || ig->buf_eof) 3813 0 3813
if (ig->error || ig->buf_eof) 0 3813 3813
1229 0 return 0;
1230  
1231 3813 ▶ if (needed > (ssize_t)ig->buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (needed > (ssize_t)ig->buf_size) 1 3812 3813
1232 1 needed = ig->buf_size;
1233  
1234 3813 ▶ if (ig->read_ptr && ig->read_ptr < ig->read_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (ig->read_ptr && ig->read_ptr < ig->read_end) { 707 3106 3813
if (ig->read_ptr && ig->read_ptr < ig->read_end) { 5 702 707
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 3808 work = ig->buffer;
1251 }
1252  
1253 /* there should always be buffer space the first time around, but
1254 avoid a compiler warning here */
1255 3813 rc = -1;
1256 3861 ▶ 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) { 3860 1 3861
while (work < buf_end && (rc = i_io_raw_read(ig, work, buf_end - work)) > 0) { 3746 114 3860
1257 3746 work += rc;
1258 3746 good = 1;
1259 3746 ▶ if (needed < rc)
Branch: 1 branch
BranchTrueFalseTotal
if (needed < rc) 3698 48 3746
1260 3698 break;
1261  
1262 48 needed -= rc;
1263 }
1264  
1265 3813 ▶ if (rc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { 8 3805 3813
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 3805 ▶ else if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (rc == 0) { 106 3699 3805
1271 106 ig->buf_eof = 1;
1272 IOL_DEB(fprintf(IOL_DEBs, " i_io_read_fill -> rc 0, setting eof\n"));
1273 }
1274  
1275 3813 ▶ if (good) {
Branch: 1 branch
BranchTrueFalseTotal
if (good) { 3744 69 3813
1276 3744 ig->read_ptr = buf_start;
1277 3744 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 3813 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 21838 buffer_read(io_glue *igo, void *buf, size_t count) {
1522 21838 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 21838 ▶ if ( ig->cpos+count > ig->len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ig->cpos+count > ig->len ) { 781 21057 21838
1527 781 dIMCTXio(igo);
1528 781 im_log((aIMCTX, 1,"buffer_read: short read: cpos=%ld, len=%ld, count=%ld\n", (long)ig->cpos, (long)ig->len, (long)count));
1529 781 count = ig->len - ig->cpos;
1530 }
1531
1532 21838 memcpy(buf, ig->data+ig->cpos, count);
1533 21838 ig->cpos += count;
1534 IOL_DEB( fprintf(IOL_DEBs, "buffer_read: count = %ld\n", (long)count) );
1535 21838 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 199 buffer_destroy(io_glue *igo) {
1648 199 io_buffer *ig = (io_buffer *)igo;
1649  
1650 199 ▶ if (ig->closecb) {
Branch: 1 branch
BranchTrueFalseTotal
if (ig->closecb) { 199 0 199
1651 199 dIMCTXio(igo);
1652 199 im_log((aIMCTX, 1,"calling close callback %p for io_buffer\n",
1653 ig->closecb));
1654 199 ig->closecb(ig->closedata);
1655 }
1656 199 }
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 186 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 186 ib = mymalloc(sizeof(io_blink));
1678  
1679 186 ib->next = NULL;
1680 186 ib->prev = NULL;
1681 186 ib->len = BBSIZ;
1682  
1683 186 memset(&ib->buf, 0, ib->len);
1684 186 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 133 io_bchain_advance(io_ex_bchain *ieb) {
1703 133 ▶ if (ieb->cp->next == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ieb->cp->next == NULL) { 133 0 133
1704 133 ieb->tail = io_blink_new();
1705 133 ieb->tail->prev = ieb->cp;
1706 133 ieb->cp->next = ieb->tail;
1707  
1708 133 ieb->tfill = 0; /* Only set this if we added a new slice */
1709 }
1710 133 ieb->cp = ieb->cp->next;
1711 133 ieb->cpos = 0;
1712 133 }
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 53 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 53 cp = ieb->head;
1731
1732 239 ▶ while(cp) {
Branch: 1 branch
BranchTrueFalseTotal
while(cp) { 186 53 239
1733 186 io_blink *t = cp->next;
1734 186 myfree(cp);
1735 186 cp = t;
1736 }
1737 53 }
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 46 bufchain_read(io_glue *ig, void *buf, size_t count) {
1856 46 io_ex_bchain *ieb = ig->exdata;
1857 46 size_t scount = count;
1858 46 char *cbuf = buf;
1859 size_t sk;
1860 46 dIMCTXio(ig);
1861  
1862 46 im_log((aIMCTX, 1, "bufchain_read(ig %p, buf %p, count %ld)\n", ig, buf, (long)count));
1863  
1864 225 ▶ while( scount ) {
Branch: 1 branch
BranchTrueFalseTotal
while( scount ) { 180 45 225
1865 180 ▶ 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; 28 152 180
1866 180 ▶ if (clen == ieb->cpos) {
Branch: 1 branch
BranchTrueFalseTotal
if (clen == ieb->cpos) { 134 46 180
1867 134 ▶ if (ieb->cp == ieb->tail) break; /* EOF */
Branch: 1 branch
BranchTrueFalseTotal
if (ieb->cp == ieb->tail) break; /* EOF */ 1 133 134
1868 133 ieb->cp = ieb->cp->next;
1869 133 ieb->cpos = 0;
1870 133 ▶ 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; 19 114 133
1871 }
1872  
1873 179 sk = clen - ieb->cpos;
1874 179 sk = sk > scount ? scount : sk;
1875  
1876 179 memcpy(&cbuf[count-scount], &ieb->cp->buf[ieb->cpos], sk);
1877 179 scount -= sk;
1878 179 ieb->cpos += sk;
1879 179 ieb->gpos += sk;
1880 }
1881  
1882 46 im_log((aIMCTX, 1, "bufchain_read: returning %ld\n", (long)(count-scount)));
1883 46 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 254 bufchain_write(io_glue *ig, const void *buf, size_t count) {
1905 254 char *cbuf = (char *)buf;
1906 254 io_ex_bchain *ieb = ig->exdata;
1907 254 size_t ocount = count;
1908 size_t sk;
1909 254 dIMCTXio(ig);
1910  
1911 254 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 629 ▶ while(count) {
Branch: 1 branch
BranchTrueFalseTotal
while(count) { 375 254 629
1916 375 im_log((aIMCTX, 2, "bufchain_write: - looping - count = %ld\n", (long)count));
1917 375 ▶ if ((off_t)ieb->cp->len == ieb->cpos) {
Branch: 1 branch
BranchTrueFalseTotal
if ((off_t)ieb->cp->len == ieb->cpos) { 133 242 375
1918 133 im_log((aIMCTX, 1, "bufchain_write: cp->len == ieb->cpos = %ld - advancing chain\n", (long) ieb->cpos));
1919 133 io_bchain_advance(ieb);
1920 }
1921  
1922 375 sk = ieb->cp->len - ieb->cpos;
1923 375 sk = sk > count ? count : sk;
1924 375 memcpy(&ieb->cp->buf[ieb->cpos], &cbuf[ocount-count], sk);
1925  
1926 375 ▶ if (ieb->cp == ieb->tail) {
Branch: 1 branch
BranchTrueFalseTotal
if (ieb->cp == ieb->tail) { 374 1 375
1927 374 int extend = ieb->cpos + sk - ieb->tfill;
1928 374 im_log((aIMCTX, 2, "bufchain_write: extending tail by %d\n", extend));
1929 374 ▶ if (extend > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (extend > 0) { 374 0 374
1930 374 ieb->length += extend;
1931 374 ieb->tfill += extend;
1932 }
1933 }
1934  
1935 375 ieb->cpos += sk;
1936 375 ieb->gpos += sk;
1937 375 count -= sk;
1938 }
1939 254 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 44 bufchain_close(io_glue *ig) {
1962 44 dIMCTXio(ig);
1963 44 im_log((aIMCTX, 1, "bufchain_close(ig %p)\n",ig));
1964 IOL_DEB( fprintf(IOL_DEBs, "bufchain_close(ig %p)\n", ig) );
1965  
1966 44 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 49 bufchain_seek(io_glue *ig, off_t offset, int whence) {
1986 49 io_ex_bchain *ieb = ig->exdata;
1987 int wrlen;
1988  
1989 49 ▶ 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 46 49
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 49 dIMCTXio(ig);
1992  
1993 49 im_log((aIMCTX, 1, "bufchain_seek(ig %p, offset %ld, whence %d)\n", ig, (long)offset, whence));
1994  
1995 49 ▶ if (scount < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scount < 0) { 0 49 49
1996 0 i_push_error(0, "invalid whence supplied or seek before start of file");
1997 0 return (off_t)-1;
1998 }
1999  
2000 49 ieb->cp = ieb->head;
2001 49 ieb->cpos = 0;
2002 49 ieb->gpos = 0;
2003
2004 53 ▶ while( scount ) {
Branch: 1 branch
BranchTrueFalseTotal
while( scount ) { 4 49 53
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 49 wrlen = scount;
2022  
2023 49 ▶ if (wrlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (wrlen > 0) { 0 49 49
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 49 im_log((aIMCTX, 2, "bufchain_seek: returning ieb->gpos = %ld\n", (long)ieb->gpos));
2043 49 return ieb->gpos;
2044 }
2045  
2046 static
2047 void
2048 53 bufchain_destroy(io_glue *ig) {
2049 53 io_ex_bchain *ieb = ig->exdata;
2050  
2051 53 io_destroy_bufchain(ieb);
2052  
2053 53 myfree(ieb);
2054 53 }
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 »