Exemplo n.º 1
0
static void
close_dctd(fz_context *ctx, void *state_)
{
	fz_dctd *state = (fz_dctd *)state_;

	if (setjmp(state->jb))
	{
		fz_warn(ctx, "jpeg error: %s", state->msg);
		goto skip;
	}

	if (state->init)
		jpeg_finish_decompress(&state->cinfo);

skip:
	if (state->cinfo.src)
		state->curr_stm->rp = state->curr_stm->wp - state->cinfo.src->bytes_in_buffer;
	if (state->init)
		jpeg_destroy_decompress(&state->cinfo);

	fz_free(ctx, state->scanline);
	fz_close(state->chain);
	fz_close(state->jpegtables);
	fz_free(ctx, state);
}
Exemplo n.º 2
0
/*
 * Load uncompressed contents of a stream into buf.
 */
fz_error
pdf_load_stream(fz_buffer **bufp, pdf_xref *xref, int num, int gen)
{
	fz_error error;
	fz_stream *stm;
	fz_obj *dict, *obj;
	int i, len;

	error = pdf_open_stream(&stm, xref, num, gen);
	if (error)
		return fz_rethrow(error, "cannot open stream (%d %d R)", num, gen);

	error = pdf_load_object(&dict, xref, num, gen);
	if (error)
		return fz_rethrow(error, "cannot load stream dictionary (%d %d R)", num, gen);

	len = fz_to_int(fz_dict_gets(dict, "Length"));
	obj = fz_dict_gets(dict, "Filter");
	len = pdf_guess_filter_length(len, fz_to_name(obj));
	for (i = 0; i < fz_array_len(obj); i++)
		len = pdf_guess_filter_length(len, fz_to_name(fz_array_get(obj, i)));

	fz_drop_obj(dict);

	error = fz_read_all(bufp, stm, len);
	if (error)
	{
		fz_close(stm);
		return fz_rethrow(error, "cannot read raw stream (%d %d R)", num, gen);
	}

	fz_close(stm);
	return fz_okay;
}
Exemplo n.º 3
0
static void
close_dctd(fz_context *ctx, void *state_)
{
    fz_dctd *state = (fz_dctd *)state_;

    if (setjmp(state->jb))
    {
        fz_warn(ctx, "jpeg error: %s", state->msg);
        goto skip;
    }

    /* We call jpeg_abort rather than the more usual
     * jpeg_finish_decompress here. This has the same effect,
     * but doesn't spew warnings if we didn't read enough data etc.
     */
    if (state->init)
        jpeg_abort((j_common_ptr)&state->cinfo);

skip:
    if (state->cinfo.src)
        state->curr_stm->rp = state->curr_stm->wp - state->cinfo.src->bytes_in_buffer;
    if (state->init)
        jpeg_destroy_decompress(&state->cinfo);

    fz_dct_mem_term(state);

    fz_free(ctx, state->scanline);
    fz_close(state->chain);
    fz_close(state->jpegtables);
    fz_free(ctx, state);
}
Exemplo n.º 4
0
/*
 * Load raw (compressed but decrypted) contents of a stream into buf.
 */
fz_error
pdf_load_raw_stream(fz_buffer **bufp, pdf_xref *xref, int num, int gen)
{
	fz_error error;
	fz_stream *stm;
	fz_obj *dict;
	int len;

	error = pdf_load_object(&dict, xref, num, gen);
	if (error)
		return fz_rethrow(error, "cannot load stream dictionary (%d %d R)", num, gen);

	len = fz_to_int(fz_dict_gets(dict, "Length"));

	fz_drop_obj(dict);

	error = pdf_open_raw_stream(&stm, xref, num, gen);
	if (error)
		return fz_rethrow(error, "cannot open raw stream (%d %d R)", num, gen);

	error = fz_read_all(bufp, stm, len);
	if (error)
	{
		fz_close(stm);
		return fz_rethrow(error, "cannot read raw stream (%d %d R)", num, gen);
	}

	fz_close(stm);
	return fz_okay;
}
Exemplo n.º 5
0
/* Default: color_transform = -1 (unset), l2factor = 0, jpegtables = NULL */
fz_stream *
fz_open_dctd(fz_stream *chain, int color_transform, int l2factor, fz_stream *jpegtables)
{
	fz_context *ctx = chain->ctx;
	fz_dctd *state = NULL;

	fz_var(state);

	fz_try(ctx)
	{
		state = fz_malloc_struct(chain->ctx, fz_dctd);
		state->ctx = ctx;
		state->chain = chain;
		state->jpegtables = jpegtables;
		state->curr_stm = chain;
		state->color_transform = color_transform;
		state->init = 0;
		state->l2factor = l2factor;
	}
	fz_catch(ctx)
	{
		fz_free(ctx, state);
		fz_close(chain);
		fz_close(jpegtables);
		fz_rethrow(ctx);
	}

	return fz_new_stream(ctx, state, read_dctd, close_dctd);
}
Exemplo n.º 6
0
static fz_error
pdf_repairobjstm(pdf_xref *xref, int num, int gen)
{
	fz_error error;
	fz_obj *obj;
	fz_stream *stm;
	int tok;
	int i, n, count;
	char buf[256];

	error = pdf_loadobject(&obj, xref, num, gen);
	if (error)
		return fz_rethrow(error, "cannot load object stream object (%d %d R)", num, gen);

	count = fz_toint(fz_dictgets(obj, "N"));

	fz_dropobj(obj);

	error = pdf_openstream(&stm, xref, num, gen);
	if (error)
		return fz_rethrow(error, "cannot open object stream object (%d %d R)", num, gen);

	for (i = 0; i < count; i++)
	{
		error = pdf_lex(&tok, stm, buf, sizeof buf, &n);
		if (error || tok != PDF_TINT)
		{
			fz_close(stm);
			return fz_rethrow(error, "corrupt object stream (%d %d R)", num, gen);
		}

		n = atoi(buf);
		if (n >= xref->len)
			pdf_resizexref(xref, n + 1);

		xref->table[n].ofs = num;
		xref->table[n].gen = i;
		xref->table[n].stmofs = 0;
		xref->table[n].obj = nil;
		xref->table[n].type = 'o';

		error = pdf_lex(&tok, stm, buf, sizeof buf, &n);
		if (error || tok != PDF_TINT)
		{
			fz_close(stm);
			return fz_rethrow(error, "corrupt object stream (%d %d R)", num, gen);
		}
	}

	fz_close(stm);
	return fz_okay;
}
Exemplo n.º 7
0
fz_stream *
fz_open_flated(fz_stream *chain, int window_bits)
{
	fz_flate *state = NULL;
	int code = Z_OK;
	fz_context *ctx = chain->ctx;

	fz_var(code);
	fz_var(state);

	fz_try(ctx)
	{
		state = fz_malloc_struct(ctx, fz_flate);
		state->chain = chain;

		state->z.zalloc = zalloc;
		state->z.zfree = zfree;
		state->z.opaque = ctx;
		state->z.next_in = NULL;
		state->z.avail_in = 0;

		code = inflateInit2(&state->z, window_bits);
		if (code != Z_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "zlib error: inflateInit: %s", state->z.msg);
	}
	fz_catch(ctx)
	{
		if (state && code == Z_OK)
			inflateEnd(&state->z);
		fz_free(ctx, state);
		fz_close(chain);
		fz_rethrow(ctx);
	}
	return fz_new_stream(ctx, state, next_flated, close_flated, rebind_flated);
}
Exemplo n.º 8
0
fz_stream *
fz_open_resized_dctd(fz_stream *chain, int color_transform, int factor)
{
	fz_context *ctx = chain->ctx;
	fz_dctd *state = NULL;

	fz_var(state);

	fz_try(ctx)
	{
		state = fz_malloc_struct(chain->ctx, fz_dctd);
		state->ctx = ctx;
		state->chain = chain;
		state->color_transform = color_transform;
		state->init = 0;
		state->factor = factor;
	}
	fz_catch(ctx)
	{
		fz_free(ctx, state);
		fz_close(chain);
		fz_rethrow(ctx);
	}

	return fz_new_stream(ctx, state, read_dctd, close_dctd);
}
Exemplo n.º 9
0
static void
close_lzwd(fz_context *ctx, void *state_)
{
	fz_lzwd *lzw = (fz_lzwd *)state_;
	fz_close(lzw->chain);
	fz_free(ctx, lzw);
}
Exemplo n.º 10
0
static float
pdf_extract_font_size(pdf_xref *xref, char *appearance, char **font_name)
{
	fz_context *ctx = xref->ctx;
	fz_stream *stream = fz_open_memory(ctx, appearance, strlen(appearance));
	float font_size = 0;
	int tok, len;

	*font_name = NULL;
	do
	{
		fz_error error = pdf_lex(&tok, stream, xref->scratch, sizeof(xref->scratch), &len);
		if (error || tok == PDF_TOK_EOF)
		{
			fz_free(ctx, *font_name);
			*font_name = NULL;
			break;
		}
		if (tok == PDF_TOK_NAME)
		{
			fz_free(ctx, *font_name);
			*font_name = fz_strdup(ctx, xref->scratch);
		}
		else if (tok == PDF_TOK_REAL || tok == PDF_TOK_INT)
		{
			font_size = fz_atof(xref->scratch);
		}
	} while (tok != PDF_TOK_KEYWORD || strcmp(xref->scratch, "Tf") != 0);
	fz_close(stream);
	return font_size;
}
Exemplo n.º 11
0
cbz_document *
cbz_open_document(fz_context *ctx, const char *filename)
{
	fz_stream *file;
	cbz_document *doc;

	file = fz_open_file(ctx, filename);
	if (!file)
		fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));

	fz_try(ctx)
	{
		doc = cbz_open_document_with_stream(ctx, file);
	}
	fz_always(ctx)
	{
		fz_close(file);
	}
	fz_catch(ctx)
	{
		fz_rethrow(ctx);
	}

	return doc;
}
Exemplo n.º 12
0
fz_buffer *
pdf_load_raw_renumbered_stream(pdf_document *xref, int num, int gen, int orig_num, int orig_gen)
{
	fz_stream *stm;
	pdf_obj *dict;
	int len;
	fz_buffer *buf;

	if (num > 0 && num < xref->len && xref->table[num].stm_buf)
		return fz_keep_buffer(xref->ctx, xref->table[num].stm_buf);

	dict = pdf_load_object(xref, num, gen);
	/* RJW: "cannot load stream dictionary (%d %d R)", num, gen */

	len = pdf_to_int(pdf_dict_gets(dict, "Length"));

	pdf_drop_obj(dict);

	stm = pdf_open_raw_renumbered_stream(xref, num, gen, orig_num, orig_gen);
	/* RJW: "cannot open raw stream (%d %d R)", num, gen */

	buf = fz_read_all(stm, len);
	/* RJW: "cannot read raw stream (%d %d R)", num, gen */

	fz_close(stm);
	return buf;
}
Exemplo n.º 13
0
static void
closelzwd(fz_stream *stm)
{
	fz_lzwd *lzw = stm->state;
	fz_close(lzw->chain);
	fz_free(lzw);
}
Exemplo n.º 14
0
static void
closenull(fz_stream *stm)
{
    struct nullfilter *state = stm->state;
    fz_close(state->chain);
    fz_free(state);
}
Exemplo n.º 15
0
/*
 * Build a filter for reading raw stream data.
 * This is a null filter to constrain reading to the
 * stream length, followed by a decryption filter.
 */
static fz_stream *
pdf_open_raw_filter(fz_stream *chain, pdf_document *xref, pdf_obj *stmobj, int num, int gen)
{
    int hascrypt;
    int len;
    fz_context *ctx = chain->ctx;

    /* don't close chain when we close this filter */
    fz_keep_stream(chain);

    len = pdf_to_int(pdf_dict_gets(stmobj, "Length"));
    chain = fz_open_null(chain, len);

    fz_try(ctx)
    {
        hascrypt = pdf_stream_has_crypt(ctx, stmobj);
        if (xref->crypt && !hascrypt)
            chain = pdf_open_crypt(chain, xref->crypt, num, gen);
    }
    fz_catch(ctx)
    {
        fz_close(chain);
        fz_rethrow(ctx);
    }

    return chain;
}
Exemplo n.º 16
0
void
xps_free_context(xps_context *ctx)
{
	xps_font_cache *font, *next;
	int i;

	if (ctx->file)
		fz_close(ctx->file);

	for (i = 0; i < ctx->zip_count; i++)
		fz_free(ctx->ctx, ctx->zip_table[i].name);
	fz_free(ctx->ctx, ctx->zip_table);

	font = ctx->font_table;
	while (font)
	{
		next = font->next;
		fz_drop_font(ctx->ctx, font->font);
		fz_free(ctx->ctx, font->name);
		fz_free(ctx->ctx, font);
		font = next;
	}

	xps_free_page_list(ctx);

	fz_free(ctx->ctx, ctx->start_part);
	fz_free(ctx->ctx, ctx->directory);
	fz_free(ctx->ctx, ctx);
}
Exemplo n.º 17
0
static void
closeahxd(fz_stream *stm)
{
    fz_ahxd *state = stm->state;
    fz_close(state->chain);
    fz_free(state);
}
Exemplo n.º 18
0
int
xps_open_file(fz_context *ctx, xps_context **ctxp, char *filename)
{
	char buf[2048];
	fz_stream *file;
	char *p;
	int code;

	if (strstr(filename, "/_rels/.rels") || strstr(filename, "\\_rels\\.rels"))
	{
		fz_strlcpy(buf, filename, sizeof buf);
		p = strstr(buf, "/_rels/.rels");
		if (!p)
			p = strstr(buf, "\\_rels\\.rels");
		*p = 0;
		return xps_open_directory(ctx, ctxp, buf);
	}

	file = fz_open_file(ctx, filename);
	if (!file)
		return fz_error_make(ctx, "cannot open file '%s': %s", filename, strerror(errno));

	code = xps_open_stream(ctxp, file);
	fz_close(file);
	if (code)
		return fz_error_note(ctx, code, "cannot load document '%s'", filename);
	return fz_okay;
}
Exemplo n.º 19
0
HRESULT MuPDFDoc::InitDocument(unsigned char *buffer, int bufferLen, const char *mimeType)
{
	fz_stream *stream = OpenStream(buffer, bufferLen);
	if (!stream)
	{
		return E_OUTOFMEMORY;
	}
	else
	{
		fz_try(m_context)
		{
			m_document = fz_open_document_with_stream(m_context, mimeType, stream);
			m_outline = fz_load_outline(m_document);
			//AlertsInit();
		}
		fz_always(m_context)
		{
			fz_close(stream);
		}
		fz_catch(m_context)
		{
			return E_INVALIDARG;
		}
		return S_OK;
	}
}
Exemplo n.º 20
0
static void
fz_decode_tiff_flate(struct tiff *tiff, fz_stream *chain, unsigned char *wp, int wlen)
{
	fz_stream *stm = fz_open_flated(chain);
	fz_read(stm, wp, wlen);
	fz_close(stm);
}
Exemplo n.º 21
0
static fz_colorspace *
load_indexed(pdf_document *doc, pdf_obj *array)
{
	fz_context *ctx = doc->ctx;
	pdf_obj *baseobj = pdf_array_get(array, 1);
	pdf_obj *highobj = pdf_array_get(array, 2);
	pdf_obj *lookupobj = pdf_array_get(array, 3);
	fz_colorspace *base = NULL;
	fz_colorspace *cs;
	int i, n, high;
	unsigned char *lookup = NULL;

	fz_var(base);
	fz_var(lookup);

	fz_try(ctx)
	{
		base = pdf_load_colorspace(doc, baseobj);

		high = pdf_to_int(highobj);
		high = fz_clampi(high, 0, 255);
		n = base->n * (high + 1);
		lookup = fz_malloc_array(ctx, 1, n);

		if (pdf_is_string(lookupobj) && pdf_to_str_len(lookupobj) >= n)
		{
			unsigned char *buf = (unsigned char *) pdf_to_str_buf(lookupobj);
			for (i = 0; i < n; i++)
				lookup[i] = buf[i];
		}
		else if (pdf_is_indirect(lookupobj))
		{
			fz_stream *file = NULL;

			fz_var(file);

			fz_try(ctx)
			{
				file = pdf_open_stream(doc, pdf_to_num(lookupobj), pdf_to_gen(lookupobj));
				i = fz_read(file, lookup, n);
				if (i < n)
					memset(lookup+i, 0, n-i);
			}
			fz_always(ctx)
			{
				fz_close(file);
			}
			fz_catch(ctx)
			{
				fz_rethrow_message(ctx, "cannot open colorspace lookup table (%d 0 R)", pdf_to_num(lookupobj));
			}
		}
		else
		{
			fz_rethrow_message(ctx, "cannot parse colorspace lookup table");
		}

		cs = fz_new_indexed_colorspace(ctx, base, high, lookup);
	}
Exemplo n.º 22
0
static void
fz_mesh_type5_process(fz_context *ctx, fz_shade *shade, const fz_matrix *ctm, fz_mesh_processor *painter)
{
	fz_stream *stream = fz_open_compressed_buffer(ctx, shade->buffer);
	fz_vertex *buf = NULL;
	fz_vertex *ref = NULL;
	int first;
	int ncomp;
	int i, k;
	int vprow = shade->u.m.vprow;
	int bpcoord = shade->u.m.bpcoord;
	int bpcomp = shade->u.m.bpcomp;
	float x0 = shade->u.m.x0;
	float x1 = shade->u.m.x1;
	float y0 = shade->u.m.y0;
	float y1 = shade->u.m.y1;
	float *c0 = shade->u.m.c0;
	float *c1 = shade->u.m.c1;

	fz_var(buf);
	fz_var(ref);

	fz_try(ctx)
	{
		ref = fz_malloc_array(ctx, vprow, sizeof(fz_vertex));
		buf = fz_malloc_array(ctx, vprow, sizeof(fz_vertex));
		first = 1;

		ncomp = (shade->use_function > 0 ? 1 : shade->colorspace->n);
		while (!fz_is_eof_bits(stream))
		{
			for (i = 0; i < vprow; i++)
			{
				buf[i].p.x = read_sample(stream, bpcoord, x0, x1);
				buf[i].p.y = read_sample(stream, bpcoord, y0, y1);
				fz_transform_point(&buf[i].p, ctm);
				for (k = 0; k < ncomp; k++)
					buf[i].c[k] = read_sample(stream, bpcomp, c0[k], c1[k]);
			}

			if (!first)
				for (i = 0; i < vprow - 1; i++)
					paint_quad(painter, &ref[i], &ref[i+1], &buf[i+1], &buf[i]);

			SWAP(ref,buf);
			first = 0;
		}
	}
	fz_always(ctx)
	{
		fz_free(ctx, ref);
		fz_free(ctx, buf);
		fz_close(stream);
	}
	fz_catch(ctx)
	{
		fz_rethrow(ctx);
	}
}
Exemplo n.º 23
0
static void
close_null(fz_context *ctx, void *state_)
{
	struct null_filter *state = (struct null_filter *)state_;
	fz_stream *chain = state->chain;
	fz_free(ctx, state);
	fz_close(chain);
}
Exemplo n.º 24
0
static void
close_ahxd(fz_context *ctx, void *state_)
{
	fz_ahxd *state = (fz_ahxd *)state_;
	fz_stream *chain = state->chain;
	fz_free(ctx, state);
	fz_close(chain);
}
Exemplo n.º 25
0
static int
xps_decode_tiff_uncompressed(struct tiff *tiff, fz_stream *stm, byte *wp, int wlen)
{
	int n = fz_read(stm, wp, wlen);
	fz_close(stm);
	if (n < 0)
		return fz_error_note(tiff->ctx, n, "cannot read uncompressed strip");
	return fz_okay;
}
Exemplo n.º 26
0
static int
xps_decode_tiff_jpeg(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen)
{
	fz_stream *stm = fz_open_dctd(chain, NULL);
	int n = fz_read(stm, wp, wlen);
	fz_close(stm);
	if (n < 0)
		return fz_error_note(chain->ctx, n, "cannot read jpeg strip");
	return fz_okay;
}
Exemplo n.º 27
0
void dump_stream(int i, FILE *fout)
{
	fz_stream *stm = pdf_open_stream(doc, i, 0);
	static unsigned char buf[8192];
	while (1) {
		int n = fz_read(stm, buf, sizeof buf);
		if (n == 0) break;
		fwrite(buf, 1, n, fout);
	}
	fz_close(stm);
}
Exemplo n.º 28
0
static void
closedctd(fz_stream *stm)
{
	fz_dctd *state = stm->state;
	if (state->init)
		jpeg_finish_decompress(&state->cinfo);
	state->chain->rp = state->chain->wp - state->cinfo.src->bytes_in_buffer;
	jpeg_destroy_decompress(&state->cinfo);
	fz_free(state->scanline);
	fz_close(state->chain);
	fz_free(state);
}
Exemplo n.º 29
0
static fz_error
parseTTFs(char *path, pdf_xref *xref)
{
	fz_error err;
	fz_stream *file = fz_open_file(xref->ctx, path);
	if (!file)
		return fz_error_make(xref->ctx, "fonterror : %s not found", path);

	err = parseTTF(file, 0, 0, path, xref);
	fz_close(file);
	return err;
}
Exemplo n.º 30
0
static void
close_jbig2d(fz_stream *stm)
{
	fz_jbig2d *state = stm->state;
	if (state->page)
		jbig2_release_page(state->ctx, state->page);
	if (state->gctx)
		jbig2_global_ctx_free(state->gctx);
	jbig2_ctx_free(state->ctx);
	fz_close(state->chain);
	fz_free(state);
}