diff options
Diffstat (limited to 'pdf/pdf_cmap_parse.c')
-rw-r--r-- | pdf/pdf_cmap_parse.c | 134 |
1 files changed, 61 insertions, 73 deletions
diff --git a/pdf/pdf_cmap_parse.c b/pdf/pdf_cmap_parse.c index 93dd97c9..b78a36ec 100644 --- a/pdf/pdf_cmap_parse.c +++ b/pdf/pdf_cmap_parse.c @@ -62,48 +62,42 @@ pdf_lex_cmap(fz_stream *file, pdf_lexbuf *buf) } static void -pdf_parse_cmap_name(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) +pdf_parse_cmap_name(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf) { - pdf_lexbuf buf; int tok; - buf.size = PDF_LEXBUF_SMALL; - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: Lost debugging: "syntaxerror in cmap" */ if (tok == PDF_TOK_NAME) - fz_strlcpy(cmap->cmap_name, buf.scratch, sizeof(cmap->cmap_name)); + fz_strlcpy(cmap->cmap_name, buf->scratch, sizeof(cmap->cmap_name)); else fz_warn(ctx, "expected name after CMapName in cmap"); } static void -pdf_parse_wmode(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) +pdf_parse_wmode(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf) { - pdf_lexbuf buf; int tok; - buf.size = PDF_LEXBUF_SMALL; - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: Lost debugging: "syntaxerror in cmap" */ if (tok == PDF_TOK_INT) - pdf_set_cmap_wmode(ctx, cmap, buf.i); + pdf_set_cmap_wmode(ctx, cmap, buf->i); else fz_warn(ctx, "expected integer after WMode in cmap"); } static void -pdf_parse_codespace_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) +pdf_parse_codespace_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf) { - pdf_lexbuf buf; int tok; int lo, hi; - buf.size = PDF_LEXBUF_SMALL; while (1) { - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: Lost debugging: "syntaxerror in cmap" */ if (tok == TOK_END_CODESPACE_RANGE) @@ -111,13 +105,13 @@ pdf_parse_codespace_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) else if (tok == PDF_TOK_STRING) { - lo = pdf_code_from_string(buf.scratch, buf.len); - tok = pdf_lex_cmap(file, &buf); + lo = pdf_code_from_string(buf->scratch, buf->len); + tok = pdf_lex_cmap(file, buf); /* RJW: Lost debugging: "syntaxerror in cmap" */ if (tok == PDF_TOK_STRING) { - hi = pdf_code_from_string(buf.scratch, buf.len); - pdf_add_codespace(ctx, cmap, lo, hi, buf.len); + hi = pdf_code_from_string(buf->scratch, buf->len); + pdf_add_codespace(ctx, cmap, lo, hi, buf->len); } else break; } @@ -129,16 +123,14 @@ pdf_parse_codespace_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) } static void -pdf_parse_cid_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) +pdf_parse_cid_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf) { - pdf_lexbuf buf; int tok; int lo, hi, dst; - buf.size = PDF_LEXBUF_SMALL; while (1) { - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: Lost debugging: "syntaxerror in cmap" */ if (tok == TOK_END_CID_RANGE) @@ -147,37 +139,35 @@ pdf_parse_cid_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) else if (tok != PDF_TOK_STRING) fz_throw(ctx, "expected string or endcidrange"); - lo = pdf_code_from_string(buf.scratch, buf.len); + lo = pdf_code_from_string(buf->scratch, buf->len); - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: Lost debugging: "syntaxerror in cmap" */ if (tok != PDF_TOK_STRING) fz_throw(ctx, "expected string"); - hi = pdf_code_from_string(buf.scratch, buf.len); + hi = pdf_code_from_string(buf->scratch, buf->len); - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: Lost debugging: "syntaxerror in cmap" */ if (tok != PDF_TOK_INT) fz_throw(ctx, "expected integer"); - dst = buf.i; + dst = buf->i; pdf_map_range_to_range(ctx, cmap, lo, hi, dst); } } static void -pdf_parse_cid_char(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) +pdf_parse_cid_char(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf) { - pdf_lexbuf buf; int tok; int src, dst; - buf.size = PDF_LEXBUF_SMALL; while (1) { - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: "syntaxerror in cmap" */ if (tok == TOK_END_CID_CHAR) @@ -186,32 +176,30 @@ pdf_parse_cid_char(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) else if (tok != PDF_TOK_STRING) fz_throw(ctx, "expected string or endcidchar"); - src = pdf_code_from_string(buf.scratch, buf.len); + src = pdf_code_from_string(buf->scratch, buf->len); - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: "syntaxerror in cmap" */ if (tok != PDF_TOK_INT) fz_throw(ctx, "expected integer"); - dst = buf.i; + dst = buf->i; pdf_map_range_to_range(ctx, cmap, src, src, dst); } } static void -pdf_parse_bf_range_array(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, int lo, int hi) +pdf_parse_bf_range_array(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf, int lo, int hi) { - pdf_lexbuf buf; int tok; int dst[256]; int i; - buf.size = PDF_LEXBUF_SMALL; while (1) { - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: "syntaxerror in cmap" */ if (tok == PDF_TOK_CLOSE_ARRAY) @@ -221,12 +209,12 @@ pdf_parse_bf_range_array(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, int l else if (tok != PDF_TOK_STRING) fz_throw(ctx, "expected string or ]"); - if (buf.len / 2) + if (buf->len / 2) { - for (i = 0; i < buf.len / 2; i++) - dst[i] = pdf_code_from_string(&buf.scratch[i * 2], 2); + for (i = 0; i < buf->len / 2; i++) + dst[i] = pdf_code_from_string(&buf->scratch[i * 2], 2); - pdf_map_one_to_many(ctx, cmap, lo, dst, buf.len / 2); + pdf_map_one_to_many(ctx, cmap, lo, dst, buf->len / 2); } lo ++; @@ -234,16 +222,14 @@ pdf_parse_bf_range_array(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, int l } static void -pdf_parse_bf_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) +pdf_parse_bf_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf) { - pdf_lexbuf buf; int tok; int lo, hi, dst; - buf.size = PDF_LEXBUF_SMALL; while (1) { - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: "syntaxerror in cmap" */ if (tok == TOK_END_BF_RANGE) @@ -252,23 +238,23 @@ pdf_parse_bf_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) else if (tok != PDF_TOK_STRING) fz_throw(ctx, "expected string or endbfrange"); - lo = pdf_code_from_string(buf.scratch, buf.len); + lo = pdf_code_from_string(buf->scratch, buf->len); - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: "syntaxerror in cmap" */ if (tok != PDF_TOK_STRING) fz_throw(ctx, "expected string"); - hi = pdf_code_from_string(buf.scratch, buf.len); + hi = pdf_code_from_string(buf->scratch, buf->len); - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: "syntaxerror in cmap" */ if (tok == PDF_TOK_STRING) { - if (buf.len == 2) + if (buf->len == 2) { - dst = pdf_code_from_string(buf.scratch, buf.len); + dst = pdf_code_from_string(buf->scratch, buf->len); pdf_map_range_to_range(ctx, cmap, lo, hi, dst); } else @@ -276,10 +262,10 @@ pdf_parse_bf_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) int dststr[256]; int i; - if (buf.len / 2) + if (buf->len / 2) { - for (i = 0; i < buf.len / 2; i++) - dststr[i] = pdf_code_from_string(&buf.scratch[i * 2], 2); + for (i = 0; i < buf->len / 2; i++) + dststr[i] = pdf_code_from_string(&buf->scratch[i * 2], 2); while (lo <= hi) { @@ -293,7 +279,7 @@ pdf_parse_bf_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) else if (tok == PDF_TOK_OPEN_ARRAY) { - pdf_parse_bf_range_array(ctx, cmap, file, lo, hi); + pdf_parse_bf_range_array(ctx, cmap, file, buf, lo, hi); /* RJW: "cannot map bfrange" */ } @@ -305,18 +291,16 @@ pdf_parse_bf_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) } static void -pdf_parse_bf_char(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) +pdf_parse_bf_char(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf) { - pdf_lexbuf buf; int tok; int dst[256]; int src; int i; - buf.size = PDF_LEXBUF_SMALL; while (1) { - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: "syntaxerror in cmap" */ if (tok == TOK_END_BF_CHAR) @@ -325,18 +309,18 @@ pdf_parse_bf_char(fz_context *ctx, pdf_cmap *cmap, fz_stream *file) else if (tok != PDF_TOK_STRING) fz_throw(ctx, "expected string or endbfchar"); - src = pdf_code_from_string(buf.scratch, buf.len); + src = pdf_code_from_string(buf->scratch, buf->len); - tok = pdf_lex_cmap(file, &buf); + tok = pdf_lex_cmap(file, buf); /* RJW: "syntaxerror in cmap" */ /* Note: does not handle /dstName */ if (tok != PDF_TOK_STRING) fz_throw(ctx, "expected string"); - if (buf.len / 2) + if (buf->len / 2) { - for (i = 0; i < buf.len / 2; i++) - dst[i] = pdf_code_from_string(&buf.scratch[i * 2], 2); + for (i = 0; i < buf->len / 2; i++) + dst[i] = pdf_code_from_string(&buf->scratch[i * 2], 2); pdf_map_one_to_many(ctx, cmap, src, dst, i); } } @@ -351,7 +335,7 @@ pdf_load_cmap(fz_context *ctx, fz_stream *file) int tok; const char *where; - buf.size = PDF_LEXBUF_SMALL; + pdf_lexbuf_init(ctx, &buf, PDF_LEXBUF_SMALL); cmap = pdf_new_cmap(ctx); strcpy(key, ".notdef"); @@ -373,12 +357,12 @@ pdf_load_cmap(fz_context *ctx, fz_stream *file) if (!strcmp(buf.scratch, "CMapName")) { where = " after CMapName"; - pdf_parse_cmap_name(ctx, cmap, file); + pdf_parse_cmap_name(ctx, cmap, file, &buf); } else if (!strcmp(buf.scratch, "WMode")) { where = " after WMode"; - pdf_parse_wmode(ctx, cmap, file); + pdf_parse_wmode(ctx, cmap, file, &buf); } else fz_strlcpy(key, buf.scratch, sizeof key); @@ -392,31 +376,31 @@ pdf_load_cmap(fz_context *ctx, fz_stream *file) else if (tok == TOK_BEGIN_CODESPACE_RANGE) { where = " codespacerange"; - pdf_parse_codespace_range(ctx, cmap, file); + pdf_parse_codespace_range(ctx, cmap, file, &buf); } else if (tok == TOK_BEGIN_BF_CHAR) { where = " bfchar"; - pdf_parse_bf_char(ctx, cmap, file); + pdf_parse_bf_char(ctx, cmap, file, &buf); } else if (tok == TOK_BEGIN_CID_CHAR) { where = " cidchar"; - pdf_parse_cid_char(ctx, cmap, file); + pdf_parse_cid_char(ctx, cmap, file, &buf); } else if (tok == TOK_BEGIN_BF_RANGE) { where = " bfrange"; - pdf_parse_bf_range(ctx, cmap, file); + pdf_parse_bf_range(ctx, cmap, file, &buf); } else if (tok == TOK_BEGIN_CID_RANGE) { where = "cidrange"; - pdf_parse_cid_range(ctx, cmap, file); + pdf_parse_cid_range(ctx, cmap, file, &buf); } /* ignore everything else */ @@ -424,6 +408,10 @@ pdf_load_cmap(fz_context *ctx, fz_stream *file) pdf_sort_cmap(ctx, cmap); } + fz_always(ctx) + { + pdf_lexbuf_fin(&buf); + } fz_catch(ctx) { pdf_drop_cmap(ctx, cmap); |