13 char *io_type_names[] = { "FDSEEK", "FDNOSEEK", "BUFFER", "CBSEEK", "CBNOSEEK", "BUFCHAIN" };
19 iolayer.c - encapsulates different source of data into a single framework.
23 io_glue *ig = io_new_fd( fileno(stdin) );
24 method = io_reqmeth( IOL_NOSEEK | IOL_MMAP ); // not implemented yet
25 io_glue_commit_types(ig); // always assume IOL_SEEK for now
28 code that uses ig->readcb()
29 to read data goes here.
32 code that uses ig->readcb()
33 to read data goes here.
42 iolayer.c implements the basic functions to create and destroy io_glue
43 objects for Imager. The typical usage pattern for data sources is:
45 1. Create the source (io_new_fd)
46 2. Define how you want to get data from it (io_reqmeth)
47 3. read from it using the interface requested (ig->readdb, ig->mmapcb)
48 4. Close the source, which
49 shouldn't really close the underlying source. (io_glue DESTROY)
51 =head1 FUNCTION REFERENCE
53 Some of these functions are internal.
64 * Callbacks for sources that cannot seek
67 /* fakeseek_read: read method for when emulating a seekable source
70 fakeseek_read(io_glue *ig, void *buf, size_t count) {
71 io_ex_fseek *exdata = ig->exdata;
79 * Callbacks for sources that can seek
83 =item realseek_read(ig, buf, count)
85 Does the reading from a source that can be seeked on
88 buf - buffer to return data in
89 count - number of bytes to read into buffer max
96 realseek_read(io_glue *ig, void *buf, size_t count) {
97 io_ex_rseek *ier = ig->exdata;
98 int fd = (int)ig->source.cb.p;
103 IOL_DEB( printf("realseek_read: fd = %d, ier->cpos = %ld, buf = 0x%p, count = %d\n", fd, (long) ier->cpos, buf, count) );
104 /* Is this a good idea? Would it be better to handle differently? skip handling? */
105 while( count!=bc && (rc = ig->source.cb.readcb(fd,cbuf+bc,count-bc))>0 ) bc+=rc;
108 IOL_DEB( printf("realseek_read: rc = %d, bc = %d\n", rc, bc) );
114 =item realseek_write(ig, buf, count)
116 Does the writing to a 'source' that can be seeked on
119 buf - buffer that contains data
120 count - number of bytes to write
127 realseek_write(io_glue *ig, const void *buf, size_t count) {
128 io_ex_rseek *ier = ig->exdata;
129 int fd = (int)ig->source.cb.p;
132 char *cbuf = (char*)buf;
134 IOL_DEB( printf("realseek_write: fd = %d, ier->cpos = %ld, buf = 0x%p, count = %d\n", fd, (long) ier->cpos, buf, count) );
135 /* Is this a good idea? Would it be better to handle differently? skip handling? */
137 while( count!=bc && (rc = ig->source.cb.writecb(fd,cbuf+bc,count-bc))>0 ) bc+=rc;
140 IOL_DEB( printf("realseek_write: rc = %d, bc = %d\n", rc, bc) );
146 =item realseek_close(ig)
148 Closes a source that can be seeked on. Not sure if this should be an actual close
149 or not. Does nothing for now. Should be fixed.
158 realseek_close(io_glue *ig) {
159 mm_log((1, "realseek_close(ig %p)\n", ig));
160 /* FIXME: Do stuff here */
164 /* realseek_seek(ig, offset, whence)
166 Implements seeking for a source that is seekable, the purpose of having this is to be able to
167 have an offset into a file that is different from what the underlying library thinks.
170 offset - offset into stream
171 whence - whence argument a la lseek
178 realseek_seek(io_glue *ig, off_t offset, int whence) {
179 /* io_ex_rseek *ier = ig->exdata; Needed later */
180 int fd = (int)ig->source.cb.p;
182 IOL_DEB( printf("realseek_seek(ig 0x%p, offset %ld, whence %d)\n", ig, (long) offset, whence) );
183 rc = lseek(fd, offset, whence);
185 IOL_DEB( printf("realseek_seek: rc %ld\n", (long) rc) );
187 /* FIXME: How about implementing this offset handling stuff? */
197 * Callbacks for sources that are a chain of variable sized buffers
202 /* Helper functions for buffer chains */
209 mm_log((1, "io_blink_new()\n"));
211 ib = mymalloc(sizeof(io_blink));
217 memset(&ib->buf, 0, ib->len);
224 =item io_bchain_advance(ieb)
226 Advances the buffer chain to the next link - extending if
227 necessary. Also adjusts the cpos and tfill counters as needed.
229 ieb - buffer chain object
236 io_bchain_advance(io_ex_bchain *ieb) {
237 if (ieb->cp->next == NULL) {
238 ieb->tail = io_blink_new();
239 ieb->tail->prev = ieb->cp;
240 ieb->cp->next = ieb->tail;
242 ieb->tfill = 0; /* Only set this if we added a new slice */
244 ieb->cp = ieb->cp->next;
253 bufchain_dump(io_ex_bchain *ieb) {
254 mm_log((1, " buf_chain_dump(ieb %p)\n"));
255 mm_log((1, " buf_chain_dump: ieb->offset = %d\n", ieb->offset));
256 mm_log((1, " buf_chain_dump: ieb->length = %d\n", ieb->length));
257 mm_log((1, " buf_chain_dump: ieb->head = %p\n", ieb->head ));
258 mm_log((1, " buf_chain_dump: ieb->tail = %p\n", ieb->tail ));
259 mm_log((1, " buf_chain_dump: ieb->tfill = %d\n", ieb->tfill ));
260 mm_log((1, " buf_chain_dump: ieb->cp = %p\n", ieb->cp ));
261 mm_log((1, " buf_chain_dump: ieb->cpos = %d\n", ieb->cpos ));
262 mm_log((1, " buf_chain_dump: ieb->gpos = %d\n", ieb->gpos ));
267 * TRUE if lengths are NOT equal
273 chainlencert( io_glue *ig ) {
278 io_ex_bchain *ieb = ig->exdata;
279 io_blink *cp = ieb->head;
282 if (ieb->gpos > ieb->length) mm_log((1, "BBAR : ieb->gpos = %d, ieb->length = %d\n", ieb->gpos, ieb->length));
285 clen = (cp == ieb->tail) ? ieb->tfill : cp->len;
286 if (ieb->head == cp && cp->prev) mm_log((1, "Head of chain has a non null prev\n"));
287 if (ieb->tail == cp && cp->next) mm_log((1, "Tail of chain has a non null next\n"));
289 if (ieb->head != cp && !cp->prev) mm_log((1, "Middle of chain has a null prev\n"));
290 if (ieb->tail != cp && !cp->next) mm_log((1, "Middle of chain has a null next\n"));
292 if (cp->prev && cp->prev->next != cp) mm_log((1, "%p = cp->prev->next != cp\n", cp->prev->next));
293 if (cp->next && cp->next->prev != cp) mm_log((1, "%p cp->next->prev != cp\n", cp->next->prev));
300 if (!cfl) cpos += clen;
305 if (( csize != ieb->length )) mm_log((1, "BAR : csize = %d, ieb->length = %d\n", csize, ieb->length));
306 if (( cpos != ieb->gpos )) mm_log((1, "BAR : cpos = %d, ieb->gpos = %d\n", cpos, ieb->gpos ));
312 chaincert( io_glue *ig) {
314 io_ex_bchain *ieb = ig->exdata;
315 io_blink *cp = ieb->head;
317 mm_log((1, "Chain verification.\n"));
319 mm_log((1, " buf_chain_dump: ieb->offset = %d\n", ieb->offset));
320 mm_log((1, " buf_chain_dump: ieb->length = %d\n", ieb->length));
321 mm_log((1, " buf_chain_dump: ieb->head = %p\n", ieb->head ));
322 mm_log((1, " buf_chain_dump: ieb->tail = %p\n", ieb->tail ));
323 mm_log((1, " buf_chain_dump: ieb->tfill = %d\n", ieb->tfill ));
324 mm_log((1, " buf_chain_dump: ieb->cp = %p\n", ieb->cp ));
325 mm_log((1, " buf_chain_dump: ieb->cpos = %d\n", ieb->cpos ));
326 mm_log((1, " buf_chain_dump: ieb->gpos = %d\n", ieb->gpos ));
329 int clen = cp == ieb->tail ? ieb->tfill : cp->len;
330 mm_log((1, "link: %p <- %p -> %p\n", cp->prev, cp, cp->next));
331 if (ieb->head == cp && cp->prev) mm_log((1, "Head of chain has a non null prev\n"));
332 if (ieb->tail == cp && cp->next) mm_log((1, "Tail of chain has a non null next\n"));
334 if (ieb->head != cp && !cp->prev) mm_log((1, "Middle of chain has a null prev\n"));
335 if (ieb->tail != cp && !cp->next) mm_log((1, "Middle of chain has a null next\n"));
337 if (cp->prev && cp->prev->next != cp) mm_log((1, "%p = cp->prev->next != cp\n", cp->prev->next));
338 if (cp->next && cp->next->prev != cp) mm_log((1, "%p cp->next->prev != cp\n", cp->next->prev));
344 mm_log((1, "csize = %d %s ieb->length = %d\n", csize, csize == ieb->length ? "==" : "!=", ieb->length));
359 =item bufchain_read(ig, buf, count)
361 Does the reading from a source that can be seeked on
364 buf - buffer to return data in
365 count - number of bytes to read into buffer max
372 bufchain_read(io_glue *ig, void *buf, size_t count) {
373 io_ex_bchain *ieb = ig->exdata;
374 size_t scount = count;
378 mm_log((1, "bufchain_read(ig %p, buf %p, count %ld)\n", ig, buf, count));
379 IOL_DEB( printf("bufchain_read: fd = %d, ier->cpos = %ld, buf = 0x%p, count = %d\n", fd, (long) ier->cpos, buf, count) );
382 int clen = (ieb->cp == ieb->tail) ? ieb->tfill : ieb->cp->len;
383 if (clen == ieb->cpos) {
384 if (ieb->cp == ieb->tail) break; /* EOF */
385 ieb->cp = ieb->cp->next;
387 clen = (ieb->cp == ieb->tail) ? ieb->tfill : ieb->cp->len;
390 sk = clen - ieb->cpos;
391 sk = sk > scount ? scount : sk;
393 memcpy(&cbuf[count-scount], &ieb->cp->buf[ieb->cpos], sk);
399 mm_log((1, "bufchain_read: returning %d\n", count-scount));
408 =item bufchain_write(ig, buf, count)
410 Does the writing to a 'source' that can be seeked on
413 buf - buffer that contains data
414 count - number of bytes to write
421 bufchain_write(io_glue *ig, const void *buf, size_t count) {
422 char *cbuf = (char *)buf;
423 io_ex_bchain *ieb = ig->exdata;
424 size_t ocount = count;
427 mm_log((1, "bufchain_write: ig = %p, buf = 0x%p, count = %d\n", ig, buf, count));
429 IOL_DEB( printf("bufchain_write: ig = %p, ieb->cpos = %ld, buf = 0x%p, count = %d\n", ig, (long) ieb->cpos, buf, count) );
432 mm_log((2, "bufchain_write: - looping - count = %d\n", count));
433 if (ieb->cp->len == ieb->cpos) {
434 mm_log((1, "bufchain_write: cp->len == ieb->cpos = %d - advancing chain\n", (long) ieb->cpos));
435 io_bchain_advance(ieb);
438 sk = ieb->cp->len - ieb->cpos;
439 sk = sk > count ? count : sk;
440 memcpy(&ieb->cp->buf[ieb->cpos], &cbuf[ocount-count], sk);
442 if (ieb->cp == ieb->tail) {
443 int extend = ieb->cpos + sk - ieb->tfill;
444 mm_log((2, "bufchain_write: extending tail by %d\n", extend));
446 ieb->length += extend;
447 ieb->tfill += extend;
459 =item bufchain_close(ig)
461 Closes a source that can be seeked on. Not sure if this should be an actual close
462 or not. Does nothing for now. Should be fixed.
471 bufchain_close(io_glue *ig) {
472 mm_log((1, "bufchain_close(ig %p)\n",ig));
473 IOL_DEB( printf("bufchain_close(ig 0x%p)\n", ig) );
474 /* FIXME: Commit a seek point here */
479 /* bufchain_seek(ig, offset, whence)
481 Implements seeking for a source that is seekable, the purpose of having this is to be able to
482 have an offset into a file that is different from what the underlying library thinks.
485 offset - offset into stream
486 whence - whence argument a la lseek
493 bufchain_seek(io_glue *ig, off_t offset, int whence) {
494 io_ex_bchain *ieb = ig->exdata;
499 off_t scount = offset;
502 mm_log((1, "bufchain_seek(ig %p, offset %ld, whence %d)\n", ig, offset, whence));
505 case SEEK_SET: /* SEEK_SET = 0, From the top */
511 int clen = (ieb->cp == ieb->tail) ? ieb->tfill : ieb->cp->len;
512 if (clen == ieb->cpos) {
513 if (ieb->cp == ieb->tail) break; /* EOF */
514 ieb->cp = ieb->cp->next;
516 clen = (ieb->cp == ieb->tail) ? ieb->tfill : ieb->cp->len;
519 sk = clen - ieb->cpos;
520 sk = sk > scount ? scount : sk;
531 * extending file - get ieb into consistent state and then
532 * call write which will get it to the correct position
535 memset(TB, 0, BBSIZ);
536 ieb->gpos = ieb->length;
537 ieb->cpos = ieb->tfill;
540 ssize_t rc, wl = min(wrlen, BBSIZ);
541 mm_log((1, "bufchain_seek: wrlen = %d, wl = %d\n", wrlen, wl));
542 rc = bufchain_write( ig, TB, wl );
543 if (rc != wl) m_fatal(0, "bufchain_seek: Unable to extend file\n");
551 m_fatal(123, "SEEK_CUR IS NOT IMPLEMENTED\n");
559 while(cof < 0 && ib->prev) {
565 case SEEK_END: /* SEEK_END = 2 */
566 if (cof>0) m_fatal(0, "bufchain_seek: SEEK_END + %d : Extending files via seek not supported!\n", cof);
569 ieb->cpos = ieb->tfill;
575 while(cof<0 && ib->prev) {
580 if (cof<0) m_fatal(0, "bufchain_seek: Tried to seek before start of file\n");
581 ieb->gpos = ieb->length+offset;
586 m_fatal(0, "bufchain_seek: Unhandled seek request: whence = %d\n", whence );
589 mm_log((2, "bufchain_seek: returning ieb->gpos = %d\n", ieb->gpos));
599 * Methods for setting up data source
603 =item io_obj_setp_buffer(io, p, len)
605 Sets an io_object for reading from a buffer source
607 io - io object that describes a source
608 p - pointer to buffer
609 len - length of buffer
615 io_obj_setp_buffer(io_obj *io, void *p, size_t len) {
616 io->buffer.type = BUFFER;
617 io->buffer.c = (char*) p;
618 io->buffer.len = len;
623 =item io_obj_setp_cbuf(io, p)
625 Sets an io_object for reading/writing from a buffer source
627 io - io object that describes a source
628 p - pointer to buffer
629 len - length of buffer
635 io_obj_setp_bufchain(io_obj *io) {
641 =item io_obj_setp_cb(io, p, readcb, writecb, seekcb)
643 Sets an io_object for reading from a source that uses callbacks
645 io - io object that describes a source
646 p - pointer to data for callbacks
647 readcb - read callback to read from source
648 writecb - write callback to write to source
649 seekcb - seek callback to seek on source
655 io_obj_setp_cb(io_obj *io, void *p, readl readcb, writel writecb, seekl seekcb) {
656 io->cb.type = CBSEEK;
658 io->cb.readcb = readcb;
659 io->cb.writecb = writecb;
660 io->cb.seekcb = seekcb;
664 =item io_glue_commit_types(ig)
666 Creates buffers and initializes structures to read with the chosen interface.
674 io_glue_commit_types(io_glue *ig) {
675 io_type inn = ig->source.type;
677 mm_log((1, "io_glue_commit_types(ig 0x%p)\n", ig));
678 mm_log((1, "io_glue_commit_types: source type %d (%s)\n", inn, io_type_names[inn]));
683 io_ex_bchain *ieb = mymalloc(sizeof(io_ex_bchain));
691 ieb->head = io_blink_new();
693 ieb->tail = ieb->head;
696 ig->readcb = bufchain_read;
697 ig->writecb = bufchain_write;
698 ig->seekcb = bufchain_seek;
699 ig->closecb = bufchain_close;
705 io_ex_rseek *ier = mymalloc(sizeof(io_ex_rseek));
711 ig->readcb = realseek_read;
712 ig->writecb = realseek_write;
713 ig->seekcb = realseek_seek;
714 ig->closecb = realseek_close;
720 =item io_glue_gettypes(ig, reqmeth)
722 Returns a set of compatible interfaces to read data with.
725 reqmeth - request mask
727 The request mask is a bit mask (of something that hasn't been implemented yet)
728 of interfaces that it would like to read data from the source which the ig
735 io_glue_gettypes(io_glue *ig, int reqmeth) {
740 /* FIXME: Implement this function! */
741 /* if (ig->source.type =
742 if (reqmeth & IO_BUFF) */
748 =item io_new_bufchain()
750 returns a new io_glue object that has the 'empty' source and but can
751 be written to and read from later (like a pseudo file).
758 io_glue *ig = mymalloc(sizeof(io_glue));
759 io_obj_setp_bufchain(&ig->source);
767 returns a new io_glue object that has the source defined as reading
768 from specified filedescriptor. Note that the the interface to recieving
769 data from the io_glue callbacks hasn't been done yet.
771 fd - file descriptor to read/write from
778 io_glue *ig = mymalloc(sizeof(io_glue));
780 io_obj_setp_cb(&ig->source, (void*)fd, _read, _write, _lseek);
782 io_obj_setp_cb(&ig->source, (void*)fd, read, write, lseek);
792 Takes the source that the io_glue is bound to and allocates space
793 for a return buffer and returns the entire content in a single buffer.
794 Note: This only works for io_glue objects that contain a bufchain. It
795 is usefull for saving to scalars and such.
798 c - pointer to a pointer to where data should be copied to
804 io_slurp(io_glue *ig, unsigned char **c) {
809 io_type inn = ig->source.type;
811 if ( inn != BUFCHAIN ) {
812 m_fatal(0, "io_slurp: called on a source that is not from a bufchain\n");
816 cc = *c = mymalloc( ieb->length );
820 bufchain_seek(ig, 0, SEEK_SET);
822 rc = bufchain_read(ig, cc, ieb->length);
824 if (rc != ieb->length)
825 m_fatal(1, "io_slurp: bufchain_read returned an incomplete read: rc = %d, request was %d\n", rc, ieb->length);
832 =item io_glue_DESTROY(ig)
834 A destructor method for io_glue objects. Should clean up all related buffers.
835 Might leave us with a dangling pointer issue on some buffers.
837 ig - io_glue object to destroy.
843 io_glue_DESTROY(io_glue *ig) {
845 /* FIXME: Handle extradata and such */