static unsigned char * gif_read_tbid(fz_context *ctx, struct info *info, unsigned char *dest, unsigned char *p, unsigned char *end) { fz_stream *stm, *lzwstm = NULL; unsigned int mincodesize, y; fz_buffer *compressed = NULL, *uncompressed = NULL; const unsigned char *ct; unsigned char *sp; if (end - p < 1) fz_throw(ctx, FZ_ERROR_GENERIC, "premature end in table based image data in gif image"); mincodesize = *p; fz_var(compressed); fz_var(lzwstm); fz_var(uncompressed); fz_try(ctx) { compressed = fz_new_buffer(ctx, 0); p = gif_read_subblocks(ctx, info, p + 1, end, compressed); stm = fz_open_buffer(ctx, compressed); lzwstm = fz_open_lzwd(ctx, stm, 0, mincodesize + 1, 1, 0); uncompressed = fz_read_all(ctx, lzwstm, info->width * info->height); sp = uncompressed->data; if (info->has_lct) ct = info->lct; else if (info->has_gct) ct = info->gct; else ct = dct; if (info->image_interlaced) { for (y = 0; y < info->image_height; y += 8, sp += info->image_width) gif_read_line(ctx, info, dest, ct, y, sp); for (y = 4; y < info->image_height; y += 8, sp += info->image_width) gif_read_line(ctx, info, dest, ct, y, sp); for (y = 2; y < info->image_height; y += 4, sp += info->image_width) gif_read_line(ctx, info, dest, ct, y, sp); for (y = 1; y < info->image_height; y += 2, sp += info->image_width) gif_read_line(ctx, info, dest, ct, y, sp); } else for (y = 0; y < info->image_height; y++, sp += info->image_width) gif_read_line(ctx, info, dest, ct, y, sp); } fz_always(ctx) { fz_drop_buffer(ctx, uncompressed); fz_drop_buffer(ctx, compressed); fz_drop_stream(ctx, lzwstm); } fz_catch(ctx) { fz_rethrow(ctx); } return p; }
static const unsigned char * gif_read_tbid(fz_context *ctx, struct info *info, unsigned char *dest, const unsigned char *p, const unsigned char *end) { fz_stream *stm, *lzwstm = NULL; unsigned int mincodesize, y; fz_buffer *compressed = NULL, *uncompressed = NULL; const unsigned char *ct; unsigned char *sp; int ct_entries; if (end - p < 1) fz_throw(ctx, FZ_ERROR_GENERIC, "premature end in table based image data in gif image"); mincodesize = *p; /* if there is no overlap, avoid pasting image data, just consume it */ if (info->image_top >= info->height || info->image_left >= info->width) { p = gif_read_subblocks(ctx, info, p + 1, end, NULL); return p; } fz_var(compressed); fz_var(lzwstm); fz_var(uncompressed); fz_try(ctx) { compressed = fz_new_buffer(ctx, 0); p = gif_read_subblocks(ctx, info, p + 1, end, compressed); stm = fz_open_buffer(ctx, compressed); lzwstm = fz_open_lzwd(ctx, stm, 0, mincodesize + 1, 1, 1); uncompressed = fz_read_all(ctx, lzwstm, 0); if (uncompressed->len < info->image_width * info->image_height) fz_throw(ctx, FZ_ERROR_GENERIC, "premature end in compressed table based image data in gif image"); if (info->has_lct) { ct = info->lct; ct_entries = info->lct_entries; } else if (info->has_gct) { ct = info->gct; ct_entries = info->gct_entries; } else { ct = dct; ct_entries = 256; } sp = uncompressed->data; if (info->image_interlaced) { for (y = 0; y < info->image_height; y += 8, sp += info->image_width) gif_read_line(ctx, info, dest, ct_entries, ct, y, sp); for (y = 4; y < info->image_height; y += 8, sp += info->image_width) gif_read_line(ctx, info, dest, ct_entries, ct, y, sp); for (y = 2; y < info->image_height; y += 4, sp += info->image_width) gif_read_line(ctx, info, dest, ct_entries, ct, y, sp); for (y = 1; y < info->image_height; y += 2, sp += info->image_width) gif_read_line(ctx, info, dest, ct_entries, ct, y, sp); } else for (y = 0; y < info->image_height; y++, sp += info->image_width) gif_read_line(ctx, info, dest, ct_entries, ct, y, sp); } fz_always(ctx) { fz_drop_buffer(ctx, uncompressed); fz_drop_buffer(ctx, compressed); fz_drop_stream(ctx, lzwstm); } fz_catch(ctx) { fz_rethrow(ctx); } return p; }