2 * Miscellaneous I/O functions
6 #include "fitz-stream.h"
8 int fz_tell(fz_stream
*stm
)
10 if (stm
->mode
== FZ_SREAD
)
15 int fz_seek(fz_stream
*stm
, int offset
, int whence
)
17 if (stm
->mode
== FZ_SREAD
)
18 return fz_rseek(stm
, offset
, whence
);
19 return fz_wseek(stm
, offset
, whence
);
23 * Read a line terminated by LF or CR or CRLF.
26 int fz_readline(fz_stream
*stm
, char *mem
, int n
)
54 struct fz_linkedbuf_s
* next
;
57 typedef struct fz_linkedbuf_s fz_linkedbuf
;
59 fz_error
*fz_newlinkedbuf(fz_linkedbuf
**bufp
, int len
, unsigned char **data
)
63 buf
= *bufp
= fz_malloc(sizeof(fz_linkedbuf
) + len
);
64 if (!buf
) return fz_outofmem
;
67 *data
= (unsigned char*)buf
+ sizeof(fz_linkedbuf
);
71 fz_error
*fz_growlinkedbuf(fz_linkedbuf
*buf
, int len
, unsigned char **data
)
76 error
= fz_newlinkedbuf(&newbuf
, len
, data
);
77 if (error
) return error
;
84 void fz_droplinkedbuf(fz_linkedbuf
*buf
)
94 int fz_linkedbuflen(fz_linkedbuf
*buf
)
104 fz_error
*fz_linearizelinkedbuf(fz_linkedbuf
*buf
, int len
, unsigned char **datap
)
106 unsigned char *data
, *bufdata
;
108 int buflen
= fz_linkedbuflen(buf
);
109 assert(len
<= buflen
);
110 data
= *datap
= fz_malloc(len
);
111 if (!data
) return fz_outofmem
;
115 bufdata
= (unsigned char*)buf
+ sizeof(fz_linkedbuf
);
116 tocopy
= MIN(len
, buf
->len
);
117 memmove(data
, bufdata
, tocopy
);
126 * Utility function to consume all the contents of an input stream into
127 * a freshly allocated buffer; realloced and trimmed to size.
130 enum { MINCHUNKSIZE
= 1024 * 8 };
131 enum { MAXCHUNKSIZE
= MINCHUNKSIZE
* 10 };
133 int fz_readall(fz_buffer
**bufp
, fz_stream
*stm
)
141 int chunksize
= MINCHUNKSIZE
;
146 error
= fz_newlinkedbuf(&buf
, chunksize
, &data
);
152 n
= fz_read(stm
, data
, chunksize
);
163 if (chunksize
< MAXCHUNKSIZE
)
164 chunksize
= chunksize
+ MINCHUNKSIZE
;
165 error
= fz_growlinkedbuf(buf
, chunksize
, &data
);
168 fz_droplinkedbuf(buf
);
173 error
= fz_linearizelinkedbuf(buf
, totallen
, &data
);
174 fz_droplinkedbuf(buf
);
178 real
= *bufp
= fz_malloc(sizeof(fz_buffer
));
189 real
->wp
= data
+ totallen
;
190 real
->ep
= data
+ totallen
;