static void showstream(int num, int gen) { fz_error error; fz_stream *stm; unsigned char buf[2048]; int n; showcolumn = 0; if (showdecode) error = pdf_openstream(&stm, xref, num, gen); else error = pdf_openrawstream(&stm, xref, num, gen); if (error) die(error); while (1) { error = fz_read(&n, stm, buf, sizeof buf); if (error) die(error); if (n == 0) break; if (showbinary) fwrite(buf, 1, n, stdout); else showsafe(buf, n); } fz_dropstream(stm); }
/* * Load raw (compressed but decrypted) contents of a stream into buf. */ fz_error * pdf_loadrawstream(fz_buffer **bufp, pdf_xref *xref, int oid, int gen) { fz_error *error; fz_stream *stm; error = pdf_openrawstream(&stm, xref, oid, gen); if (error) return fz_rethrow(error, "cannot open raw stream (%d)", oid); error = fz_readall(bufp, stm, 0); fz_dropstream(stm); if (error) return fz_rethrow(error, "cannot load stream into buffer (%d)", oid); return fz_okay; }
static fz_error * writestream(fz_stream *out, pdf_xref *xref, pdf_crypt *encrypt, int oid, int gen) { fz_error *error; fz_stream *dststm; fz_stream *srcstm; unsigned char buf[4096]; fz_filter *ef; int n; fz_print(out, "stream\n"); if (encrypt) { error = pdf_cryptstream(&ef, encrypt, oid, gen); if (error) return error; error = fz_openrfilter(&dststm, ef, out); fz_dropfilter(ef); if (error) return error; } else { dststm = fz_keepstream(out); } error = pdf_openrawstream(&srcstm, xref, oid, gen); if (error) goto cleanupdst; while (1) { n = fz_read(srcstm, buf, sizeof buf); if (n == 0) break; if (n < 0) { error = fz_ioerror(srcstm); goto cleanupsrc; } n = fz_write(dststm, buf, n); if (n < 0) { error = fz_ioerror(dststm); goto cleanupsrc; } } fz_dropstream(srcstm); fz_dropstream(dststm); fz_print(out, "endstream\n"); return nil; cleanupsrc: fz_dropstream(srcstm); cleanupdst: fz_dropstream(dststm); return error; }