static boolean fill_input_buffer(j_decompress_ptr cinfo) { struct jpeg_source_mgr *src = cinfo->src; fz_dctd *state = JZ_DCT_STATE_FROM_CINFO(cinfo); fz_stream *curr_stm = state->curr_stm; fz_context *ctx = curr_stm->ctx; curr_stm->rp = curr_stm->wp; fz_try(ctx) { src->bytes_in_buffer = fz_available(curr_stm, 1); } fz_catch(ctx) { fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); return 0; } src->next_input_byte = curr_stm->rp; if (src->bytes_in_buffer == 0) { static unsigned char eoi[2] = { 0xFF, JPEG_EOI }; fz_warn(state->ctx, "premature end of file in jpeg"); src->next_input_byte = eoi; src->bytes_in_buffer = 2; } return 1; }
static int next_null(fz_stream *stm, int max) { struct null_filter *state = stm->state; int n; if (state->remain == 0) return EOF; fz_seek(state->chain, state->offset, 0); n = fz_available(state->chain, max); if (n > state->remain) n = state->remain; if (n > sizeof(state->buffer)) n = sizeof(state->buffer); memcpy(state->buffer, state->chain->rp, n); stm->rp = state->buffer; stm->wp = stm->rp + n; if (n == 0) return EOF; state->chain->rp += n; state->remain -= n; state->offset += n; stm->pos += n; return *stm->rp++; }
static int next_concat(fz_stream *stm, int max) { struct concat_filter *state = (struct concat_filter *)stm->state; int n; while (state->current < state->count) { /* Read the next block of underlying data. */ if (stm->wp == state->chain[state->current]->wp) state->chain[state->current]->rp = stm->wp; n = fz_available(state->chain[state->current], max); if (n) { stm->rp = state->chain[state->current]->rp; stm->wp = state->chain[state->current]->wp; stm->pos += n; return *stm->rp++; } else { if (state->chain[state->current]->error) { stm->error = 1; break; } state->current++; fz_close(state->chain[state->current-1]); if (state->pad) { stm->rp = &state->ws_buf; stm->wp = stm->rp + 1; stm->pos++; return 32; } } } stm->rp = stm->wp; return EOF; }
static int next_leech(fz_context *ctx, fz_stream *stm, size_t max) { fz_leech *state = stm->state; fz_buffer *buffer = state->buffer; size_t n = fz_available(ctx, state->chain, max); if (n > max) n = max; while (buffer->cap < buffer->len + n) { fz_grow_buffer(ctx, state->buffer); } memcpy(buffer->data + buffer->len, state->chain->rp, n); stm->rp = buffer->data + buffer->len; stm->wp = buffer->data + buffer->len + n; state->chain->rp += n; buffer->len += n; if (n == 0) return EOF; return *stm->rp++; }
int fz_read(fz_stream *stm, unsigned char *buf, int len) { int count, n; count = 0; do { n = fz_available(stm, len); if (n > len) n = len; if (n == 0) break; memcpy(buf, stm->rp, n); stm->rp += n; buf += n; count += n; len -= n; } while (len > 0); return count; }
static int next_flated(fz_stream *stm, int required) { fz_flate *state = stm->state; fz_stream *chain = state->chain; z_streamp zp = &state->z; int code; unsigned char *outbuf = state->buffer; int outlen = sizeof(state->buffer); if (stm->eof) return EOF; zp->next_out = outbuf; zp->avail_out = outlen; while (zp->avail_out > 0) { zp->avail_in = fz_available(chain, 1); zp->next_in = chain->rp; code = inflate(zp, Z_SYNC_FLUSH); chain->rp = chain->wp - zp->avail_in; if (code == Z_STREAM_END) { break; } else if (code == Z_BUF_ERROR) { fz_warn(stm->ctx, "premature end of data in flate filter"); break; } else if (code == Z_DATA_ERROR && zp->avail_in == 0) { fz_warn(stm->ctx, "ignoring zlib error: %s", zp->msg); break; } else if (code == Z_DATA_ERROR && !strcmp(zp->msg, "incorrect data check")) { fz_warn(stm->ctx, "ignoring zlib error: %s", zp->msg); chain->rp = chain->wp; break; } else if (code != Z_OK) { fz_throw(stm->ctx, FZ_ERROR_GENERIC, "zlib error: %s", zp->msg); } } stm->rp = state->buffer; stm->wp = state->buffer + outlen - zp->avail_out; stm->pos += outlen - zp->avail_out; if (stm->rp == stm->wp) { stm->eof = 1; return EOF; } return *stm->rp++; }