From 42ff67d458b2682d18f57843eb73e94600511dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Br=C3=BCns?= Date: Thu, 17 May 2018 02:35:32 +0200 Subject: [PATCH] GfxFont: Reject invalid names from encoding table Some PDF files contain invalid glyph names in the Encoding /Differences array. Putting these e.g. in a Type42 /Encoding array when converting it to postscript creates an invalid file, rejected by e.g. ghostscript. If the encoding already has a name for a codepoint we keep it, otherwise (i.e. the name is empty or .notdef) we create a name. --- poppler/GfxFont.cc | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/poppler/GfxFont.cc b/poppler/GfxFont.cc index 7b4d4b54..b9f150f3 100644 --- a/poppler/GfxFont.cc +++ b/poppler/GfxFont.cc @@ -944,6 +944,25 @@ static GBool testForNumericNames(Dict *fontDict, GBool hex) { return numeric; } +// Return gTrue if the name is valid, according to +// https://github.com/adobe-type-tools/agl-specification +static GBool glyphNameIsValid(const char *name) +{ + // Valid characters: [a-zA-Z0-9_.] + // The AGL spec has more requirements, but this breaks some documents + while (*name != '\0') { + if (islower(*name) || isdigit(*name) || + isupper(*name) || (*name == '_') || + (*name == '.')) { + name++; + } else { + return gFalse; + } + } + + return gTrue; +} + Gfx8BitFont::Gfx8BitFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA, GfxFontType typeA, Ref embFontIDA, Dict *fontDict): GfxFont(tagA, idA, nameA, typeA, embFontIDA) { @@ -1221,12 +1240,23 @@ Gfx8BitFont::Gfx8BitFont(XRef *xref, const char *tagA, Ref idA, GooString *nameA if (obj3.isInt()) { code = obj3.getInt(); } else if (obj3.isName()) { - if (code >= 0 && code < 256) { + if (glyphNameIsValid(obj3.getName()) && code >= 0 && code < 256) { if (encFree[code]) { gfree(enc[code]); } enc[code] = copyString(obj3.getName()); encFree[code] = gTrue; + } else if (code >= 0 && code < 256) { + // if there is no name yet, we create one + if (!enc[code] || !strcmp(enc[code], ".notdef")) { + if (encFree[code]) { + gfree(enc[code]); + } + char namebuf[8] = "uni0000"; + snprintf(namebuf, 8, "uni%04x", code); + enc[code] = copyString(namebuf); + encFree[code] = gTrue; + } } ++code; } else { -- 2.16.3