Skip to content

Commit

Permalink
Removed TAG() macro and revived Tag() function for big endian platform.
Browse files Browse the repository at this point in the history
BUG=124629
TEST=ran test_{un,}malicious_fonts.sh
Review URL: https://chromiumcodereview.appspot.com/10273021
  • Loading branch information
bashi@chromium.org committed May 15, 2012
1 parent 1d2b263 commit e14e0fa
Showing 1 changed file with 42 additions and 43 deletions.
85 changes: 42 additions & 43 deletions src/ots.cc
Expand Up @@ -49,6 +49,12 @@ bool CheckTag(uint32_t tag_value) {
return true;
}

uint32_t Tag(const char *tag_str) {
uint32_t ret;
std::memcpy(&ret, tag_str, 4);
return ret;
}

struct OutputTable {
uint32_t tag;
size_t offset;
Expand Down Expand Up @@ -81,85 +87,77 @@ struct Arena {
std::vector<uint8_t*> hunks_;
};

// Use a macro instead of a function because gcc 4.4.3 creates static
// initializers in that case.
#if defined(__BIG_ENDIAN__) || defined(_BIG_ENDIAN)
#define TAG(d, c, b, a) (a | (b << 8) | (c << 16) | (d << 24))
#else
#define TAG(a, b, c, d) (a | (b << 8) | (c << 16) | (d << 24))
#endif

const struct {
uint32_t tag;
const char* tag;
bool (*parse)(ots::OpenTypeFile *otf, const uint8_t *data, size_t length);
bool (*serialise)(ots::OTSStream *out, ots::OpenTypeFile *file);
bool (*should_serialise)(ots::OpenTypeFile *file);
void (*free)(ots::OpenTypeFile *file);
bool required;
} table_parsers[] = {
{ TAG('m', 'a', 'x', 'p'), ots::ots_maxp_parse, ots::ots_maxp_serialise,
{ "maxp", ots::ots_maxp_parse, ots::ots_maxp_serialise,
ots::ots_maxp_should_serialise, ots::ots_maxp_free, true },
{ TAG('h', 'e', 'a', 'd'), ots::ots_head_parse, ots::ots_head_serialise,
{ "head", ots::ots_head_parse, ots::ots_head_serialise,
ots::ots_head_should_serialise, ots::ots_head_free, true },
{ TAG('O', 'S', '/', '2'), ots::ots_os2_parse, ots::ots_os2_serialise,
{ "OS/2", ots::ots_os2_parse, ots::ots_os2_serialise,
ots::ots_os2_should_serialise, ots::ots_os2_free, true },
{ TAG('c', 'm', 'a', 'p'), ots::ots_cmap_parse, ots::ots_cmap_serialise,
{ "cmap", ots::ots_cmap_parse, ots::ots_cmap_serialise,
ots::ots_cmap_should_serialise, ots::ots_cmap_free, true },
{ TAG('h', 'h', 'e', 'a'), ots::ots_hhea_parse, ots::ots_hhea_serialise,
{ "hhea", ots::ots_hhea_parse, ots::ots_hhea_serialise,
ots::ots_hhea_should_serialise, ots::ots_hhea_free, true },
{ TAG('h', 'm', 't', 'x'), ots::ots_hmtx_parse, ots::ots_hmtx_serialise,
{ "hmtx", ots::ots_hmtx_parse, ots::ots_hmtx_serialise,
ots::ots_hmtx_should_serialise, ots::ots_hmtx_free, true },
{ TAG('n', 'a', 'm', 'e'), ots::ots_name_parse, ots::ots_name_serialise,
{ "name", ots::ots_name_parse, ots::ots_name_serialise,
ots::ots_name_should_serialise, ots::ots_name_free, true },
{ TAG('p', 'o', 's', 't'), ots::ots_post_parse, ots::ots_post_serialise,
{ "post", ots::ots_post_parse, ots::ots_post_serialise,
ots::ots_post_should_serialise, ots::ots_post_free, true },
{ TAG('l', 'o', 'c', 'a'), ots::ots_loca_parse, ots::ots_loca_serialise,
{ "loca", ots::ots_loca_parse, ots::ots_loca_serialise,
ots::ots_loca_should_serialise, ots::ots_loca_free, false },
{ TAG('g', 'l', 'y', 'f'), ots::ots_glyf_parse, ots::ots_glyf_serialise,
{ "glyf", ots::ots_glyf_parse, ots::ots_glyf_serialise,
ots::ots_glyf_should_serialise, ots::ots_glyf_free, false },
{ TAG('C', 'F', 'F', ' '), ots::ots_cff_parse, ots::ots_cff_serialise,
{ "CFF ", ots::ots_cff_parse, ots::ots_cff_serialise,
ots::ots_cff_should_serialise, ots::ots_cff_free, false },
{ TAG('V', 'D', 'M', 'X'), ots::ots_vdmx_parse, ots::ots_vdmx_serialise,
{ "VDMX", ots::ots_vdmx_parse, ots::ots_vdmx_serialise,
ots::ots_vdmx_should_serialise, ots::ots_vdmx_free, false },
{ TAG('h', 'd', 'm', 'x'), ots::ots_hdmx_parse, ots::ots_hdmx_serialise,
{ "hdmx", ots::ots_hdmx_parse, ots::ots_hdmx_serialise,
ots::ots_hdmx_should_serialise, ots::ots_hdmx_free, false },
{ TAG('g', 'a', 's', 'p'), ots::ots_gasp_parse, ots::ots_gasp_serialise,
{ "gasp", ots::ots_gasp_parse, ots::ots_gasp_serialise,
ots::ots_gasp_should_serialise, ots::ots_gasp_free, false },
{ TAG('c', 'v', 't', ' '), ots::ots_cvt_parse, ots::ots_cvt_serialise,
{ "cvt ", ots::ots_cvt_parse, ots::ots_cvt_serialise,
ots::ots_cvt_should_serialise, ots::ots_cvt_free, false },
{ TAG('f', 'p', 'g', 'm'), ots::ots_fpgm_parse, ots::ots_fpgm_serialise,
{ "fpgm", ots::ots_fpgm_parse, ots::ots_fpgm_serialise,
ots::ots_fpgm_should_serialise, ots::ots_fpgm_free, false },
{ TAG('p', 'r', 'e', 'p'), ots::ots_prep_parse, ots::ots_prep_serialise,
{ "prep", ots::ots_prep_parse, ots::ots_prep_serialise,
ots::ots_prep_should_serialise, ots::ots_prep_free, false },
{ TAG('L', 'T', 'S', 'H'), ots::ots_ltsh_parse, ots::ots_ltsh_serialise,
{ "LTSH", ots::ots_ltsh_parse, ots::ots_ltsh_serialise,
ots::ots_ltsh_should_serialise, ots::ots_ltsh_free, false },
{ TAG('V', 'O', 'R', 'G'), ots::ots_vorg_parse, ots::ots_vorg_serialise,
{ "VORG", ots::ots_vorg_parse, ots::ots_vorg_serialise,
ots::ots_vorg_should_serialise, ots::ots_vorg_free, false },
{ TAG('k', 'e', 'r', 'n'), ots::ots_kern_parse, ots::ots_kern_serialise,
{ "kern", ots::ots_kern_parse, ots::ots_kern_serialise,
ots::ots_kern_should_serialise, ots::ots_kern_free, false },
// We need to parse GDEF table in advance of parsing GSUB/GPOS tables
// because they could refer GDEF table.
{ TAG('G', 'D', 'E', 'F'), ots::ots_gdef_parse, ots::ots_gdef_serialise,
{ "GDEF", ots::ots_gdef_parse, ots::ots_gdef_serialise,
ots::ots_gdef_should_serialise, ots::ots_gdef_free, false },
{ TAG('G', 'P', 'O', 'S'), ots::ots_gpos_parse, ots::ots_gpos_serialise,
{ "GPOS", ots::ots_gpos_parse, ots::ots_gpos_serialise,
ots::ots_gpos_should_serialise, ots::ots_gpos_free, false },
{ TAG('G', 'S', 'U', 'B'), ots::ots_gsub_parse, ots::ots_gsub_serialise,
{ "GSUB", ots::ots_gsub_parse, ots::ots_gsub_serialise,
ots::ots_gsub_should_serialise, ots::ots_gsub_free, false },
{ TAG('v', 'h', 'e', 'a'), ots::ots_vhea_parse, ots::ots_vhea_serialise,
{ "vhea", ots::ots_vhea_parse, ots::ots_vhea_serialise,
ots::ots_vhea_should_serialise, ots::ots_vhea_free, false },
{ TAG('v', 'm', 't', 'x'), ots::ots_vmtx_parse, ots::ots_vmtx_serialise,
{ "vmtx", ots::ots_vmtx_parse, ots::ots_vmtx_serialise,
ots::ots_vmtx_should_serialise, ots::ots_vmtx_free, false },
// TODO(bashi): Support mort, base, and jstf tables.
{ 0, NULL, NULL, NULL, NULL, false },
};

bool IsValidVersionTag(uint32_t tag) {
return tag == TAG('\x00', '\x01', '\x00', '\x00') ||
return tag == Tag("\x00\x01\x00\x00") ||
// OpenType fonts with CFF data have 'OTTO' tag.
tag == TAG('O', 'T', 'T', 'O') ||
tag == Tag("OTTO") ||
// Older Mac fonts might have 'true' or 'typ1' tag.
tag == TAG('t', 'r', 'u', 'e') ||
tag == TAG('t', 'y', 'p', '1');
tag == Tag("true") ||
tag == Tag("typ1");
}

bool ProcessGeneric(ots::OpenTypeFile *header, ots::OTSStream *output,
Expand Down Expand Up @@ -257,7 +255,7 @@ bool ProcessWOFF(ots::OpenTypeFile *header,
return OTS_FAILURE();
}

if (woff_tag != TAG('w', 'O', 'F', 'F')) {
if (woff_tag != Tag("wOFF")) {
return OTS_FAILURE();
}

Expand Down Expand Up @@ -505,7 +503,7 @@ bool ProcessGeneric(ots::OpenTypeFile *header, ots::OTSStream *output,
if (table_parsers[i].parse == NULL) break;

const std::map<uint32_t, OpenTypeTable>::const_iterator it
= table_map.find(table_parsers[i].tag);
= table_map.find(Tag(table_parsers[i].tag));

if (it == table_map.end()) {
if (table_parsers[i].required) {
Expand Down Expand Up @@ -540,7 +538,7 @@ bool ProcessGeneric(ots::OpenTypeFile *header, ots::OTSStream *output,

if (header->cff) {
// font with PostScript glyph
if (header->version != TAG('O', 'T', 'T', 'O')) {
if (header->version != Tag("OTTO")) {
return OTS_FAILURE();
}
if (header->glyf || header->loca) {
Expand Down Expand Up @@ -600,11 +598,12 @@ bool ProcessGeneric(ots::OpenTypeFile *header, ots::OTSStream *output,
}

OutputTable out;
out.tag = table_parsers[i].tag;
uint32_t tag = Tag(table_parsers[i].tag);
out.tag = tag;
out.offset = output->Tell();

output->ResetChecksum();
if (table_parsers[i].tag == TAG('h', 'e', 'a', 'd')) {
if (tag == Tag("head")) {
head_table_offset = out.offset;
}
if (!table_parsers[i].serialise(output, header)) {
Expand Down

0 comments on commit e14e0fa

Please sign in to comment.