| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ots.h" | 5 #include "ots.h" |
| 6 | 6 |
| 7 #include <sys/types.h> | 7 #include <sys/types.h> |
| 8 #include <zlib.h> | 8 #include <zlib.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 for (unsigned i = 0; i < 4; ++i) { | 39 for (unsigned i = 0; i < 4; ++i) { |
| 40 const uint32_t check = tag_value & 0xff; | 40 const uint32_t check = tag_value & 0xff; |
| 41 if (check < 32 || check > 126) { | 41 if (check < 32 || check > 126) { |
| 42 return false; // non-ASCII character found. | 42 return false; // non-ASCII character found. |
| 43 } | 43 } |
| 44 tag_value >>= 8; | 44 tag_value >>= 8; |
| 45 } | 45 } |
| 46 return true; | 46 return true; |
| 47 } | 47 } |
| 48 | 48 |
| 49 uint32_t Tag(const char *tag_str) { | |
| 50 uint32_t ret; | |
| 51 std::memcpy(&ret, tag_str, 4); | |
| 52 return ret; | |
| 53 } | |
| 54 | |
| 55 struct OutputTable { | |
| 56 uint32_t tag; | |
| 57 size_t offset; | |
| 58 size_t length; | |
| 59 uint32_t chksum; | |
| 60 | |
| 61 bool operator<(const OutputTable& other) const { | |
| 62 return ntohl(tag) < ntohl(other.tag); | |
| 63 } | |
| 64 }; | |
| 65 | |
| 66 struct Arena { | 49 struct Arena { |
| 67 public: | 50 public: |
| 68 ~Arena() { | 51 ~Arena() { |
| 69 for (std::vector<uint8_t*>::iterator | 52 for (std::vector<uint8_t*>::iterator |
| 70 i = hunks_.begin(); i != hunks_.end(); ++i) { | 53 i = hunks_.begin(); i != hunks_.end(); ++i) { |
| 71 delete[] *i; | 54 delete[] *i; |
| 72 } | 55 } |
| 73 } | 56 } |
| 74 | 57 |
| 75 uint8_t* Allocate(size_t length) { | 58 uint8_t* Allocate(size_t length) { |
| 76 uint8_t* p = new uint8_t[length]; | 59 uint8_t* p = new uint8_t[length]; |
| 77 hunks_.push_back(p); | 60 hunks_.push_back(p); |
| 78 return p; | 61 return p; |
| 79 } | 62 } |
| 80 | 63 |
| 81 private: | 64 private: |
| 82 std::vector<uint8_t*> hunks_; | 65 std::vector<uint8_t*> hunks_; |
| 83 }; | 66 }; |
| 84 | 67 |
| 85 const struct { | 68 const struct { |
| 86 const char* tag; | 69 uint32_t tag; |
| 87 bool (*parse)(ots::OpenTypeFile *otf, const uint8_t *data, size_t length); | 70 bool (*parse)(ots::Font *font, const uint8_t *data, size_t length); |
| 88 bool (*serialise)(ots::OTSStream *out, ots::OpenTypeFile *file); | 71 bool (*serialise)(ots::OTSStream *out, ots::Font *font); |
| 89 bool (*should_serialise)(ots::OpenTypeFile *file); | 72 bool (*should_serialise)(ots::Font *font); |
| 90 void (*free)(ots::OpenTypeFile *file); | 73 void (*reuse)(ots::Font *font, ots::Font *other); |
| 74 void (*free)(ots::Font *font); |
| 91 bool required; | 75 bool required; |
| 92 } table_parsers[] = { | 76 } table_parsers[] = { |
| 93 { "maxp", ots::ots_maxp_parse, ots::ots_maxp_serialise, | 77 { OTS_TAG('m','a','x','p'), ots::ots_maxp_parse, ots::ots_maxp_serialise, |
| 94 ots::ots_maxp_should_serialise, ots::ots_maxp_free, true }, | 78 ots::ots_maxp_should_serialise, ots::ots_maxp_reuse, ots::ots_maxp_free, |
| 95 { "head", ots::ots_head_parse, ots::ots_head_serialise, | 79 true }, |
| 96 ots::ots_head_should_serialise, ots::ots_head_free, true }, | 80 { OTS_TAG('h','e','a','d'), ots::ots_head_parse, ots::ots_head_serialise, |
| 97 { "OS/2", ots::ots_os2_parse, ots::ots_os2_serialise, | 81 ots::ots_head_should_serialise, ots::ots_head_reuse, ots::ots_head_free, |
| 98 ots::ots_os2_should_serialise, ots::ots_os2_free, true }, | 82 true }, |
| 99 { "cmap", ots::ots_cmap_parse, ots::ots_cmap_serialise, | 83 { OTS_TAG('O','S','/','2'), ots::ots_os2_parse, ots::ots_os2_serialise, |
| 100 ots::ots_cmap_should_serialise, ots::ots_cmap_free, true }, | 84 ots::ots_os2_should_serialise, ots::ots_os2_reuse, ots::ots_os2_free, |
| 101 { "hhea", ots::ots_hhea_parse, ots::ots_hhea_serialise, | 85 true }, |
| 102 ots::ots_hhea_should_serialise, ots::ots_hhea_free, true }, | 86 { OTS_TAG('c','m','a','p'), ots::ots_cmap_parse, ots::ots_cmap_serialise, |
| 103 { "hmtx", ots::ots_hmtx_parse, ots::ots_hmtx_serialise, | 87 ots::ots_cmap_should_serialise, ots::ots_cmap_reuse, ots::ots_cmap_free, |
| 104 ots::ots_hmtx_should_serialise, ots::ots_hmtx_free, true }, | 88 true }, |
| 105 { "name", ots::ots_name_parse, ots::ots_name_serialise, | 89 { OTS_TAG('h','h','e','a'), ots::ots_hhea_parse, ots::ots_hhea_serialise, |
| 106 ots::ots_name_should_serialise, ots::ots_name_free, true }, | 90 ots::ots_hhea_should_serialise, ots::ots_hhea_reuse, ots::ots_hhea_free, |
| 107 { "post", ots::ots_post_parse, ots::ots_post_serialise, | 91 true }, |
| 108 ots::ots_post_should_serialise, ots::ots_post_free, true }, | 92 { OTS_TAG('h','m','t','x'), ots::ots_hmtx_parse, ots::ots_hmtx_serialise, |
| 109 { "loca", ots::ots_loca_parse, ots::ots_loca_serialise, | 93 ots::ots_hmtx_should_serialise, ots::ots_hmtx_reuse, ots::ots_hmtx_free, |
| 110 ots::ots_loca_should_serialise, ots::ots_loca_free, false }, | 94 true }, |
| 111 { "glyf", ots::ots_glyf_parse, ots::ots_glyf_serialise, | 95 { OTS_TAG('n','a','m','e'), ots::ots_name_parse, ots::ots_name_serialise, |
| 112 ots::ots_glyf_should_serialise, ots::ots_glyf_free, false }, | 96 ots::ots_name_should_serialise, ots::ots_name_reuse, ots::ots_name_free, |
| 113 { "CFF ", ots::ots_cff_parse, ots::ots_cff_serialise, | 97 true }, |
| 114 ots::ots_cff_should_serialise, ots::ots_cff_free, false }, | 98 { OTS_TAG('p','o','s','t'), ots::ots_post_parse, ots::ots_post_serialise, |
| 115 { "VDMX", ots::ots_vdmx_parse, ots::ots_vdmx_serialise, | 99 ots::ots_post_should_serialise, ots::ots_post_reuse, ots::ots_post_free, |
| 116 ots::ots_vdmx_should_serialise, ots::ots_vdmx_free, false }, | 100 true }, |
| 117 { "hdmx", ots::ots_hdmx_parse, ots::ots_hdmx_serialise, | 101 { OTS_TAG('l','o','c','a'), ots::ots_loca_parse, ots::ots_loca_serialise, |
| 118 ots::ots_hdmx_should_serialise, ots::ots_hdmx_free, false }, | 102 ots::ots_loca_should_serialise, ots::ots_loca_reuse, ots::ots_loca_free, |
| 119 { "gasp", ots::ots_gasp_parse, ots::ots_gasp_serialise, | 103 false }, |
| 120 ots::ots_gasp_should_serialise, ots::ots_gasp_free, false }, | 104 { OTS_TAG('g','l','y','f'), ots::ots_glyf_parse, ots::ots_glyf_serialise, |
| 121 { "cvt ", ots::ots_cvt_parse, ots::ots_cvt_serialise, | 105 ots::ots_glyf_should_serialise, ots::ots_glyf_reuse, ots::ots_glyf_free, |
| 122 ots::ots_cvt_should_serialise, ots::ots_cvt_free, false }, | 106 false }, |
| 123 { "fpgm", ots::ots_fpgm_parse, ots::ots_fpgm_serialise, | 107 { OTS_TAG('C','F','F',' '), ots::ots_cff_parse, ots::ots_cff_serialise, |
| 124 ots::ots_fpgm_should_serialise, ots::ots_fpgm_free, false }, | 108 ots::ots_cff_should_serialise, ots::ots_cff_reuse, ots::ots_cff_free, |
| 125 { "prep", ots::ots_prep_parse, ots::ots_prep_serialise, | 109 false }, |
| 126 ots::ots_prep_should_serialise, ots::ots_prep_free, false }, | 110 { OTS_TAG('V','D','M','X'), ots::ots_vdmx_parse, ots::ots_vdmx_serialise, |
| 127 { "LTSH", ots::ots_ltsh_parse, ots::ots_ltsh_serialise, | 111 ots::ots_vdmx_should_serialise, ots::ots_vdmx_reuse, ots::ots_vdmx_free, |
| 128 ots::ots_ltsh_should_serialise, ots::ots_ltsh_free, false }, | 112 false }, |
| 129 { "VORG", ots::ots_vorg_parse, ots::ots_vorg_serialise, | 113 { OTS_TAG('h','d','m','x'), ots::ots_hdmx_parse, ots::ots_hdmx_serialise, |
| 130 ots::ots_vorg_should_serialise, ots::ots_vorg_free, false }, | 114 ots::ots_hdmx_should_serialise, ots::ots_hdmx_reuse, ots::ots_hdmx_free, |
| 131 { "kern", ots::ots_kern_parse, ots::ots_kern_serialise, | 115 false }, |
| 132 ots::ots_kern_should_serialise, ots::ots_kern_free, false }, | 116 { OTS_TAG('g','a','s','p'), ots::ots_gasp_parse, ots::ots_gasp_serialise, |
| 117 ots::ots_gasp_should_serialise, ots::ots_gasp_reuse, ots::ots_gasp_free, |
| 118 false }, |
| 119 { OTS_TAG('c','v','t',' '), ots::ots_cvt_parse, ots::ots_cvt_serialise, |
| 120 ots::ots_cvt_should_serialise, ots::ots_cvt_reuse, ots::ots_cvt_free, |
| 121 false }, |
| 122 { OTS_TAG('f','p','g','m'), ots::ots_fpgm_parse, ots::ots_fpgm_serialise, |
| 123 ots::ots_fpgm_should_serialise, ots::ots_fpgm_reuse, ots::ots_fpgm_free, |
| 124 false }, |
| 125 { OTS_TAG('p','r','e','p'), ots::ots_prep_parse, ots::ots_prep_serialise, |
| 126 ots::ots_prep_should_serialise, ots::ots_prep_reuse, ots::ots_prep_free, |
| 127 false }, |
| 128 { OTS_TAG('L','T','S','H'), ots::ots_ltsh_parse, ots::ots_ltsh_serialise, |
| 129 ots::ots_ltsh_should_serialise, ots::ots_ltsh_reuse, ots::ots_ltsh_free, |
| 130 false }, |
| 131 { OTS_TAG('V','O','R','G'), ots::ots_vorg_parse, ots::ots_vorg_serialise, |
| 132 ots::ots_vorg_should_serialise, ots::ots_vorg_reuse, ots::ots_vorg_free, |
| 133 false }, |
| 134 { OTS_TAG('k','e','r','n'), ots::ots_kern_parse, ots::ots_kern_serialise, |
| 135 ots::ots_kern_should_serialise, ots::ots_kern_reuse, ots::ots_kern_free, |
| 136 false }, |
| 133 // We need to parse GDEF table in advance of parsing GSUB/GPOS tables | 137 // We need to parse GDEF table in advance of parsing GSUB/GPOS tables |
| 134 // because they could refer GDEF table. | 138 // because they could refer GDEF table. |
| 135 { "GDEF", ots::ots_gdef_parse, ots::ots_gdef_serialise, | 139 { OTS_TAG('G','D','E','F'), ots::ots_gdef_parse, ots::ots_gdef_serialise, |
| 136 ots::ots_gdef_should_serialise, ots::ots_gdef_free, false }, | 140 ots::ots_gdef_should_serialise, ots::ots_gdef_reuse, ots::ots_gdef_free, |
| 137 { "GPOS", ots::ots_gpos_parse, ots::ots_gpos_serialise, | 141 false }, |
| 138 ots::ots_gpos_should_serialise, ots::ots_gpos_free, false }, | 142 { OTS_TAG('G','P','O','S'), ots::ots_gpos_parse, ots::ots_gpos_serialise, |
| 139 { "GSUB", ots::ots_gsub_parse, ots::ots_gsub_serialise, | 143 ots::ots_gpos_should_serialise, ots::ots_gpos_reuse, ots::ots_gpos_free, |
| 140 ots::ots_gsub_should_serialise, ots::ots_gsub_free, false }, | 144 false }, |
| 141 { "vhea", ots::ots_vhea_parse, ots::ots_vhea_serialise, | 145 { OTS_TAG('G','S','U','B'), ots::ots_gsub_parse, ots::ots_gsub_serialise, |
| 142 ots::ots_vhea_should_serialise, ots::ots_vhea_free, false }, | 146 ots::ots_gsub_should_serialise, ots::ots_gsub_reuse, ots::ots_gsub_free, |
| 143 { "vmtx", ots::ots_vmtx_parse, ots::ots_vmtx_serialise, | 147 false }, |
| 144 ots::ots_vmtx_should_serialise, ots::ots_vmtx_free, false }, | 148 { OTS_TAG('v','h','e','a'), ots::ots_vhea_parse, ots::ots_vhea_serialise, |
| 145 { "MATH", ots::ots_math_parse, ots::ots_math_serialise, | 149 ots::ots_vhea_should_serialise, ots::ots_vhea_reuse, ots::ots_vhea_free, |
| 146 ots::ots_math_should_serialise, ots::ots_math_free, false }, | 150 false }, |
| 147 { 0, NULL, NULL, NULL, NULL, false }, | 151 { OTS_TAG('v','m','t','x'), ots::ots_vmtx_parse, ots::ots_vmtx_serialise, |
| 152 ots::ots_vmtx_should_serialise, ots::ots_vmtx_reuse, ots::ots_vmtx_free, |
| 153 false }, |
| 154 { OTS_TAG('M','A','T','H'), ots::ots_math_parse, ots::ots_math_serialise, |
| 155 ots::ots_math_should_serialise, ots::ots_math_reuse, ots::ots_math_free, |
| 156 false }, |
| 157 { 0, NULL, NULL, NULL, NULL, NULL, false }, |
| 148 }; | 158 }; |
| 149 | 159 |
| 150 bool ProcessGeneric(ots::OpenTypeFile *header, | 160 bool ProcessGeneric(ots::OpenTypeFile *header, |
| 161 ots::Font *font, |
| 151 uint32_t signature, | 162 uint32_t signature, |
| 152 ots::OTSStream *output, | 163 ots::OTSStream *output, |
| 153 const uint8_t *data, size_t length, | 164 const uint8_t *data, size_t length, |
| 154 const std::vector<OpenTypeTable>& tables, | 165 const std::vector<OpenTypeTable>& tables, |
| 155 ots::Buffer& file); | 166 ots::Buffer& file); |
| 156 | 167 |
| 157 bool ProcessTTF(ots::OpenTypeFile *header, | 168 bool ProcessTTF(ots::OpenTypeFile *header, |
| 158 ots::OTSStream *output, const uint8_t *data, size_t length) { | 169 ots::Font *font, |
| 159 ots::Buffer file(data, length); | 170 ots::OTSStream *output, const uint8_t *data, size_t length, |
| 171 uint32_t offset = 0) { |
| 172 ots::Buffer file(data + offset, length - offset); |
| 173 |
| 174 if (offset > length) { |
| 175 return OTS_FAILURE_MSG_HDR("offset beyond end of file"); |
| 176 } |
| 160 | 177 |
| 161 // we disallow all files > 1GB in size for sanity. | 178 // we disallow all files > 1GB in size for sanity. |
| 162 if (length > 1024 * 1024 * 1024) { | 179 if (length > 1024 * 1024 * 1024) { |
| 163 return OTS_FAILURE_MSG_HDR("file exceeds 1GB"); | 180 return OTS_FAILURE_MSG_HDR("file exceeds 1GB"); |
| 164 } | 181 } |
| 165 | 182 |
| 166 if (!file.ReadTag(&header->version)) { | 183 if (!file.ReadU32(&font->version)) { |
| 167 return OTS_FAILURE_MSG_HDR("error reading version tag"); | 184 return OTS_FAILURE_MSG_HDR("error reading version tag"); |
| 168 } | 185 } |
| 169 if (!ots::IsValidVersionTag(header->version)) { | 186 if (!ots::IsValidVersionTag(font->version)) { |
| 170 return OTS_FAILURE_MSG_HDR("invalid version tag"); | 187 return OTS_FAILURE_MSG_HDR("invalid version tag"); |
| 171 } | 188 } |
| 172 | 189 |
| 173 if (!file.ReadU16(&header->num_tables) || | 190 if (!file.ReadU16(&font->num_tables) || |
| 174 !file.ReadU16(&header->search_range) || | 191 !file.ReadU16(&font->search_range) || |
| 175 !file.ReadU16(&header->entry_selector) || | 192 !file.ReadU16(&font->entry_selector) || |
| 176 !file.ReadU16(&header->range_shift)) { | 193 !file.ReadU16(&font->range_shift)) { |
| 177 return OTS_FAILURE_MSG_HDR("error reading table directory search header"); | 194 return OTS_FAILURE_MSG_HDR("error reading table directory search header"); |
| 178 } | 195 } |
| 179 | 196 |
| 180 // search_range is (Maximum power of 2 <= numTables) x 16. Thus, to avoid | 197 // search_range is (Maximum power of 2 <= numTables) x 16. Thus, to avoid |
| 181 // overflow num_tables is, at most, 2^16 / 16 = 2^12 | 198 // overflow num_tables is, at most, 2^16 / 16 = 2^12 |
| 182 if (header->num_tables >= 4096 || header->num_tables < 1) { | 199 if (font->num_tables >= 4096 || font->num_tables < 1) { |
| 183 return OTS_FAILURE_MSG_HDR("excessive (or zero) number of tables"); | 200 return OTS_FAILURE_MSG_HDR("excessive (or zero) number of tables"); |
| 184 } | 201 } |
| 185 | 202 |
| 186 unsigned max_pow2 = 0; | 203 unsigned max_pow2 = 0; |
| 187 while (1u << (max_pow2 + 1) <= header->num_tables) { | 204 while (1u << (max_pow2 + 1) <= font->num_tables) { |
| 188 max_pow2++; | 205 max_pow2++; |
| 189 } | 206 } |
| 190 const uint16_t expected_search_range = (1u << max_pow2) << 4; | 207 const uint16_t expected_search_range = (1u << max_pow2) << 4; |
| 191 | 208 |
| 192 // Don't call ots_failure() here since ~25% of fonts (250+ fonts) in | 209 // Don't call ots_failure() here since ~25% of fonts (250+ fonts) in |
| 193 // http://www.princexml.com/fonts/ have unexpected search_range value. | 210 // http://www.princexml.com/fonts/ have unexpected search_range value. |
| 194 if (header->search_range != expected_search_range) { | 211 if (font->search_range != expected_search_range) { |
| 195 OTS_WARNING_MSG_HDR("bad search range"); | 212 OTS_WARNING_MSG_HDR("bad search range"); |
| 196 header->search_range = expected_search_range; // Fix the value. | 213 font->search_range = expected_search_range; // Fix the value. |
| 197 } | 214 } |
| 198 | 215 |
| 199 // entry_selector is Log2(maximum power of 2 <= numTables) | 216 // entry_selector is Log2(maximum power of 2 <= numTables) |
| 200 if (header->entry_selector != max_pow2) { | 217 if (font->entry_selector != max_pow2) { |
| 201 return OTS_FAILURE_MSG_HDR("incorrect entrySelector for table directory"); | 218 return OTS_FAILURE_MSG_HDR("incorrect entrySelector for table directory"); |
| 202 } | 219 } |
| 203 | 220 |
| 204 // range_shift is NumTables x 16-searchRange. We know that 16*num_tables | 221 // range_shift is NumTables x 16-searchRange. We know that 16*num_tables |
| 205 // doesn't over flow because we range checked it above. Also, we know that | 222 // doesn't over flow because we range checked it above. Also, we know that |
| 206 // it's > header->search_range by construction of search_range. | 223 // it's > font->search_range by construction of search_range. |
| 207 const uint16_t expected_range_shift = | 224 const uint16_t expected_range_shift = |
| 208 16 * header->num_tables - header->search_range; | 225 16 * font->num_tables - font->search_range; |
| 209 if (header->range_shift != expected_range_shift) { | 226 if (font->range_shift != expected_range_shift) { |
| 210 OTS_WARNING_MSG_HDR("bad range shift"); | 227 OTS_WARNING_MSG_HDR("bad range shift"); |
| 211 header->range_shift = expected_range_shift; // the same as above. | 228 font->range_shift = expected_range_shift; // the same as above. |
| 212 } | 229 } |
| 213 | 230 |
| 214 // Next up is the list of tables. | 231 // Next up is the list of tables. |
| 215 std::vector<OpenTypeTable> tables; | 232 std::vector<OpenTypeTable> tables; |
| 216 | 233 |
| 217 for (unsigned i = 0; i < header->num_tables; ++i) { | 234 for (unsigned i = 0; i < font->num_tables; ++i) { |
| 218 OpenTypeTable table; | 235 OpenTypeTable table; |
| 219 if (!file.ReadTag(&table.tag) || | 236 if (!file.ReadU32(&table.tag) || |
| 220 !file.ReadU32(&table.chksum) || | 237 !file.ReadU32(&table.chksum) || |
| 221 !file.ReadU32(&table.offset) || | 238 !file.ReadU32(&table.offset) || |
| 222 !file.ReadU32(&table.length)) { | 239 !file.ReadU32(&table.length)) { |
| 223 return OTS_FAILURE_MSG_HDR("error reading table directory"); | 240 return OTS_FAILURE_MSG_HDR("error reading table directory"); |
| 224 } | 241 } |
| 225 | 242 |
| 226 table.uncompressed_length = table.length; | 243 table.uncompressed_length = table.length; |
| 227 tables.push_back(table); | 244 tables.push_back(table); |
| 228 } | 245 } |
| 229 | 246 |
| 230 return ProcessGeneric(header, header->version, output, data, length, | 247 return ProcessGeneric(header, font, font->version, output, data, length, |
| 231 tables, file); | 248 tables, file); |
| 232 } | 249 } |
| 233 | 250 |
| 251 bool ProcessTTC(ots::OpenTypeFile *header, |
| 252 ots::OTSStream *output, |
| 253 const uint8_t *data, |
| 254 size_t length, |
| 255 uint32_t index) { |
| 256 ots::Buffer file(data, length); |
| 257 |
| 258 // we disallow all files > 1GB in size for sanity. |
| 259 if (length > 1024 * 1024 * 1024) { |
| 260 return OTS_FAILURE_MSG_HDR("file exceeds 1GB"); |
| 261 } |
| 262 |
| 263 uint32_t ttc_tag; |
| 264 if (!file.ReadU32(&ttc_tag)) { |
| 265 return OTS_FAILURE_MSG_HDR("Error reading TTC tag"); |
| 266 } |
| 267 if (ttc_tag != OTS_TAG('t','t','c','f')) { |
| 268 return OTS_FAILURE_MSG_HDR("Invalid TTC tag"); |
| 269 } |
| 270 |
| 271 uint32_t ttc_version; |
| 272 if (!file.ReadU32(&ttc_version)) { |
| 273 return OTS_FAILURE_MSG_HDR("Error reading TTC version"); |
| 274 } |
| 275 if (ttc_version != 0x00010000 && ttc_version != 0x00020000) { |
| 276 return OTS_FAILURE_MSG_HDR("Invalid TTC version"); |
| 277 } |
| 278 |
| 279 uint32_t num_fonts; |
| 280 if (!file.ReadU32(&num_fonts)) { |
| 281 return OTS_FAILURE_MSG_HDR("Error reading number of TTC fonts"); |
| 282 } |
| 283 // Limit the allowed number of subfonts to have same memory allocation. |
| 284 if (num_fonts > 0x10000) { |
| 285 return OTS_FAILURE_MSG_HDR("Too many fonts in TTC"); |
| 286 } |
| 287 |
| 288 std::vector<uint32_t> offsets(num_fonts); |
| 289 for (unsigned i = 0; i < num_fonts; i++) { |
| 290 if (!file.ReadU32(&offsets[i])) { |
| 291 return OTS_FAILURE_MSG_HDR("Error reading offset to OffsetTable"); |
| 292 } |
| 293 } |
| 294 |
| 295 if (ttc_version == 0x00020000) { |
| 296 // We don't care about these fields of the header: |
| 297 // uint32_t dsig_tag, dsig_length, dsig_offset |
| 298 if (!file.Skip(3 * 4)) { |
| 299 return OTS_FAILURE_MSG_HDR("Error reading DSIG offset and length in TTC fo
nt"); |
| 300 } |
| 301 } |
| 302 |
| 303 if (index == static_cast<uint32_t>(-1)) { |
| 304 if (!output->WriteU32(ttc_tag) || |
| 305 !output->WriteU32(0x00010000) || |
| 306 !output->WriteU32(num_fonts) || |
| 307 !output->Seek((3 + num_fonts) * 4)) { |
| 308 return OTS_FAILURE_MSG_HDR("Error writing output"); |
| 309 } |
| 310 |
| 311 // Keep references to the fonts processed in the loop below, as we need |
| 312 // them for reused tables. |
| 313 std::vector<ots::Font> fonts(num_fonts, ots::Font(header)); |
| 314 |
| 315 for (unsigned i = 0; i < num_fonts; i++) { |
| 316 uint32_t out_offset = output->Tell(); |
| 317 if (!output->Seek((3 + i) * 4) || |
| 318 !output->WriteU32(out_offset) || |
| 319 !output->Seek(out_offset)) { |
| 320 return OTS_FAILURE_MSG_HDR("Error writing output"); |
| 321 } |
| 322 if (!ProcessTTF(header, &fonts[i], output, data, length, offsets[i])) { |
| 323 return false; |
| 324 } |
| 325 } |
| 326 |
| 327 return true; |
| 328 } else { |
| 329 if (index >= num_fonts) { |
| 330 return OTS_FAILURE_MSG_HDR("Requested font index is bigger than the number
of fonts in the TTC file"); |
| 331 } |
| 332 |
| 333 ots::Font font(header); |
| 334 return ProcessTTF(header, &font, output, data, length, offsets[index]); |
| 335 } |
| 336 } |
| 337 |
| 234 bool ProcessWOFF(ots::OpenTypeFile *header, | 338 bool ProcessWOFF(ots::OpenTypeFile *header, |
| 339 ots::Font *font, |
| 235 ots::OTSStream *output, const uint8_t *data, size_t length) { | 340 ots::OTSStream *output, const uint8_t *data, size_t length) { |
| 236 ots::Buffer file(data, length); | 341 ots::Buffer file(data, length); |
| 237 | 342 |
| 238 // we disallow all files > 1GB in size for sanity. | 343 // we disallow all files > 1GB in size for sanity. |
| 239 if (length > 1024 * 1024 * 1024) { | 344 if (length > 1024 * 1024 * 1024) { |
| 240 return OTS_FAILURE_MSG_HDR("file exceeds 1GB"); | 345 return OTS_FAILURE_MSG_HDR("file exceeds 1GB"); |
| 241 } | 346 } |
| 242 | 347 |
| 243 uint32_t woff_tag; | 348 uint32_t woff_tag; |
| 244 if (!file.ReadTag(&woff_tag)) { | 349 if (!file.ReadU32(&woff_tag)) { |
| 245 return OTS_FAILURE_MSG_HDR("error reading WOFF marker"); | 350 return OTS_FAILURE_MSG_HDR("error reading WOFF marker"); |
| 246 } | 351 } |
| 247 | 352 |
| 248 if (woff_tag != Tag("wOFF")) { | 353 if (woff_tag != OTS_TAG('w','O','F','F')) { |
| 249 return OTS_FAILURE_MSG_HDR("invalid WOFF marker"); | 354 return OTS_FAILURE_MSG_HDR("invalid WOFF marker"); |
| 250 } | 355 } |
| 251 | 356 |
| 252 if (!file.ReadTag(&header->version)) { | 357 if (!file.ReadU32(&font->version)) { |
| 253 return OTS_FAILURE_MSG_HDR("error reading version tag"); | 358 return OTS_FAILURE_MSG_HDR("error reading version tag"); |
| 254 } | 359 } |
| 255 if (!ots::IsValidVersionTag(header->version)) { | 360 if (!ots::IsValidVersionTag(font->version)) { |
| 256 return OTS_FAILURE_MSG_HDR("invalid version tag"); | 361 return OTS_FAILURE_MSG_HDR("invalid version tag"); |
| 257 } | 362 } |
| 258 | 363 |
| 259 header->search_range = 0; | 364 font->search_range = 0; |
| 260 header->entry_selector = 0; | 365 font->entry_selector = 0; |
| 261 header->range_shift = 0; | 366 font->range_shift = 0; |
| 262 | 367 |
| 263 uint32_t reported_length; | 368 uint32_t reported_length; |
| 264 if (!file.ReadU32(&reported_length) || length != reported_length) { | 369 if (!file.ReadU32(&reported_length) || length != reported_length) { |
| 265 return OTS_FAILURE_MSG_HDR("incorrect file size in WOFF header"); | 370 return OTS_FAILURE_MSG_HDR("incorrect file size in WOFF header"); |
| 266 } | 371 } |
| 267 | 372 |
| 268 if (!file.ReadU16(&header->num_tables) || !header->num_tables) { | 373 if (!file.ReadU16(&font->num_tables) || !font->num_tables) { |
| 269 return OTS_FAILURE_MSG_HDR("error reading number of tables"); | 374 return OTS_FAILURE_MSG_HDR("error reading number of tables"); |
| 270 } | 375 } |
| 271 | 376 |
| 272 uint16_t reserved_value; | 377 uint16_t reserved_value; |
| 273 if (!file.ReadU16(&reserved_value) || reserved_value) { | 378 if (!file.ReadU16(&reserved_value) || reserved_value) { |
| 274 return OTS_FAILURE_MSG_HDR("error in reserved field of WOFF header"); | 379 return OTS_FAILURE_MSG_HDR("error in reserved field of WOFF header"); |
| 275 } | 380 } |
| 276 | 381 |
| 277 uint32_t reported_total_sfnt_size; | 382 uint32_t reported_total_sfnt_size; |
| 278 if (!file.ReadU32(&reported_total_sfnt_size)) { | 383 if (!file.ReadU32(&reported_total_sfnt_size)) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 return OTS_FAILURE_MSG_HDR("Invalid private block offset or length"); | 417 return OTS_FAILURE_MSG_HDR("Invalid private block offset or length"); |
| 313 } | 418 } |
| 314 } | 419 } |
| 315 | 420 |
| 316 // Next up is the list of tables. | 421 // Next up is the list of tables. |
| 317 std::vector<OpenTypeTable> tables; | 422 std::vector<OpenTypeTable> tables; |
| 318 | 423 |
| 319 uint32_t first_index = 0; | 424 uint32_t first_index = 0; |
| 320 uint32_t last_index = 0; | 425 uint32_t last_index = 0; |
| 321 // Size of sfnt header plus size of table records. | 426 // Size of sfnt header plus size of table records. |
| 322 uint64_t total_sfnt_size = 12 + 16 * header->num_tables; | 427 uint64_t total_sfnt_size = 12 + 16 * font->num_tables; |
| 323 for (unsigned i = 0; i < header->num_tables; ++i) { | 428 for (unsigned i = 0; i < font->num_tables; ++i) { |
| 324 OpenTypeTable table; | 429 OpenTypeTable table; |
| 325 if (!file.ReadTag(&table.tag) || | 430 if (!file.ReadU32(&table.tag) || |
| 326 !file.ReadU32(&table.offset) || | 431 !file.ReadU32(&table.offset) || |
| 327 !file.ReadU32(&table.length) || | 432 !file.ReadU32(&table.length) || |
| 328 !file.ReadU32(&table.uncompressed_length) || | 433 !file.ReadU32(&table.uncompressed_length) || |
| 329 !file.ReadU32(&table.chksum)) { | 434 !file.ReadU32(&table.chksum)) { |
| 330 return OTS_FAILURE_MSG_HDR("error reading table directory"); | 435 return OTS_FAILURE_MSG_HDR("error reading table directory"); |
| 331 } | 436 } |
| 332 | 437 |
| 333 total_sfnt_size += ots::Round4(table.uncompressed_length); | 438 total_sfnt_size += ots::Round4(table.uncompressed_length); |
| 334 if (total_sfnt_size > std::numeric_limits<uint32_t>::max()) { | 439 if (total_sfnt_size > std::numeric_limits<uint32_t>::max()) { |
| 335 return OTS_FAILURE_MSG_HDR("sfnt size overflow"); | 440 return OTS_FAILURE_MSG_HDR("sfnt size overflow"); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 block_end = ots::Round4(static_cast<uint64_t>(priv_offset) + | 484 block_end = ots::Round4(static_cast<uint64_t>(priv_offset) + |
| 380 static_cast<uint64_t>(priv_length)); | 485 static_cast<uint64_t>(priv_length)); |
| 381 if (block_end > std::numeric_limits<uint32_t>::max()) { | 486 if (block_end > std::numeric_limits<uint32_t>::max()) { |
| 382 return OTS_FAILURE_MSG_HDR("Invalid private block length"); | 487 return OTS_FAILURE_MSG_HDR("Invalid private block length"); |
| 383 } | 488 } |
| 384 } | 489 } |
| 385 if (block_end != ots::Round4(length)) { | 490 if (block_end != ots::Round4(length)) { |
| 386 return OTS_FAILURE_MSG_HDR("File length mismatch (trailing junk?)"); | 491 return OTS_FAILURE_MSG_HDR("File length mismatch (trailing junk?)"); |
| 387 } | 492 } |
| 388 | 493 |
| 389 return ProcessGeneric(header, woff_tag, output, data, length, tables, file); | 494 return ProcessGeneric(header, font, woff_tag, output, data, length, tables, fi
le); |
| 390 } | 495 } |
| 391 | 496 |
| 392 bool ProcessWOFF2(ots::OpenTypeFile *header, | 497 bool ProcessWOFF2(ots::OpenTypeFile *header, |
| 498 ots::Font *font, |
| 393 ots::OTSStream *output, const uint8_t *data, size_t length) { | 499 ots::OTSStream *output, const uint8_t *data, size_t length) { |
| 394 size_t decompressed_size = ots::ComputeWOFF2FinalSize(data, length); | 500 size_t decompressed_size = ots::ComputeWOFF2FinalSize(data, length); |
| 501 |
| 395 if (decompressed_size == 0) { | 502 if (decompressed_size == 0) { |
| 396 return OTS_FAILURE_MSG_HDR("Size of decompressed WOFF 2.0 is set to 0"); | 503 return OTS_FAILURE_MSG_HDR("Size of decompressed WOFF 2.0 is set to 0"); |
| 397 } | 504 } |
| 398 // decompressed font must be <= 30MB | 505 // decompressed font must be <= 30MB |
| 399 if (decompressed_size > 30 * 1024 * 1024) { | 506 if (decompressed_size > 30 * 1024 * 1024) { |
| 400 return OTS_FAILURE_MSG_HDR("Size of decompressed WOFF 2.0 font exceeds 30MB"
); | 507 return OTS_FAILURE_MSG_HDR("Size of decompressed WOFF 2.0 font exceeds 30MB"
); |
| 401 } | 508 } |
| 402 | 509 |
| 403 std::vector<uint8_t> decompressed_buffer(decompressed_size); | 510 std::vector<uint8_t> decompressed_buffer(decompressed_size); |
| 404 if (!ots::ConvertWOFF2ToSFNT(header, &decompressed_buffer[0], decompressed_siz
e, | 511 if (!ots::ConvertWOFF2ToSFNT(font, &decompressed_buffer[0], decompressed_size, |
| 405 data, length)) { | 512 data, length)) { |
| 406 return OTS_FAILURE_MSG_HDR("Failed to convert WOFF 2.0 font to SFNT"); | 513 return OTS_FAILURE_MSG_HDR("Failed to convert WOFF 2.0 font to SFNT"); |
| 407 } | 514 } |
| 408 return ProcessTTF(header, output, &decompressed_buffer[0], decompressed_size); | 515 return ProcessTTF(header, font, output, &decompressed_buffer[0], decompressed_
size); |
| 409 } | 516 } |
| 410 | 517 |
| 411 ots::TableAction GetTableAction(ots::OpenTypeFile *header, uint32_t tag) { | 518 ots::TableAction GetTableAction(ots::OpenTypeFile *header, uint32_t tag) { |
| 412 ots::TableAction action = ots::TABLE_ACTION_DEFAULT; | 519 ots::TableAction action = ots::TABLE_ACTION_DEFAULT; |
| 413 | 520 |
| 414 action = header->context->GetTableAction(htonl(tag)); | 521 action = header->context->GetTableAction(htonl(tag)); |
| 415 | 522 |
| 416 if (action == ots::TABLE_ACTION_DEFAULT) { | 523 if (action == ots::TABLE_ACTION_DEFAULT) { |
| 417 action = ots::TABLE_ACTION_DROP; | 524 action = ots::TABLE_ACTION_DROP; |
| 418 | 525 |
| 419 for (unsigned i = 0; ; ++i) { | 526 for (unsigned i = 0; ; ++i) { |
| 420 if (table_parsers[i].parse == NULL) break; | 527 if (table_parsers[i].parse == NULL) break; |
| 421 | 528 |
| 422 if (Tag(table_parsers[i].tag) == tag) { | 529 if (table_parsers[i].tag == tag) { |
| 423 action = ots::TABLE_ACTION_SANITIZE; | 530 action = ots::TABLE_ACTION_SANITIZE; |
| 424 break; | 531 break; |
| 425 } | 532 } |
| 426 } | 533 } |
| 427 } | 534 } |
| 428 | 535 |
| 429 assert(action != ots::TABLE_ACTION_DEFAULT); // Should never return this. | 536 assert(action != ots::TABLE_ACTION_DEFAULT); // Should never return this. |
| 430 return action; | 537 return action; |
| 431 } | 538 } |
| 432 | 539 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 447 } | 554 } |
| 448 } else { | 555 } else { |
| 449 // Uncompressed table. We can process directly from memory. | 556 // Uncompressed table. We can process directly from memory. |
| 450 *table_data = data + table.offset; | 557 *table_data = data + table.offset; |
| 451 *table_length = table.length; | 558 *table_length = table.length; |
| 452 } | 559 } |
| 453 | 560 |
| 454 return true; | 561 return true; |
| 455 } | 562 } |
| 456 | 563 |
| 457 bool ProcessGeneric(ots::OpenTypeFile *header, uint32_t signature, | 564 bool ProcessGeneric(ots::OpenTypeFile *header, |
| 565 ots::Font *font, |
| 566 uint32_t signature, |
| 458 ots::OTSStream *output, | 567 ots::OTSStream *output, |
| 459 const uint8_t *data, size_t length, | 568 const uint8_t *data, size_t length, |
| 460 const std::vector<OpenTypeTable>& tables, | 569 const std::vector<OpenTypeTable>& tables, |
| 461 ots::Buffer& file) { | 570 ots::Buffer& file) { |
| 462 const size_t data_offset = file.offset(); | 571 const size_t data_offset = file.offset(); |
| 463 | 572 |
| 464 uint32_t uncompressed_sum = 0; | 573 uint32_t uncompressed_sum = 0; |
| 465 | 574 |
| 466 for (unsigned i = 0; i < header->num_tables; ++i) { | 575 for (unsigned i = 0; i < font->num_tables; ++i) { |
| 467 // the tables must be sorted by tag (when taken as big-endian numbers). | 576 // the tables must be sorted by tag (when taken as big-endian numbers). |
| 468 // This also remove the possibility of duplicate tables. | 577 // This also remove the possibility of duplicate tables. |
| 469 if (i) { | 578 if (i) { |
| 470 const uint32_t this_tag = ntohl(tables[i].tag); | 579 const uint32_t this_tag = tables[i].tag; |
| 471 const uint32_t prev_tag = ntohl(tables[i - 1].tag); | 580 const uint32_t prev_tag = tables[i - 1].tag; |
| 472 if (this_tag <= prev_tag) { | 581 if (this_tag <= prev_tag) { |
| 473 OTS_WARNING_MSG_HDR("Table directory is not correctly ordered"); | 582 OTS_WARNING_MSG_HDR("Table directory is not correctly ordered"); |
| 474 } | 583 } |
| 475 } | 584 } |
| 476 | 585 |
| 477 // all tag names must be built from printable ASCII characters | 586 // all tag names must be built from printable ASCII characters |
| 478 if (!CheckTag(tables[i].tag)) { | 587 if (!CheckTag(tables[i].tag)) { |
| 479 return OTS_FAILURE_MSG_TAG("invalid table tag", &tables[i].tag); | 588 return OTS_FAILURE_MSG_TAG("invalid table tag", tables[i].tag); |
| 480 } | 589 } |
| 481 | 590 |
| 482 // tables must be 4-byte aligned | 591 // tables must be 4-byte aligned |
| 483 if (tables[i].offset & 3) { | 592 if (tables[i].offset & 3) { |
| 484 return OTS_FAILURE_MSG_TAG("misaligned table", &tables[i].tag); | 593 return OTS_FAILURE_MSG_TAG("misaligned table", tables[i].tag); |
| 485 } | 594 } |
| 486 | 595 |
| 487 // and must be within the file | 596 // and must be within the file |
| 488 if (tables[i].offset < data_offset || tables[i].offset >= length) { | 597 if (tables[i].offset < data_offset || tables[i].offset >= length) { |
| 489 return OTS_FAILURE_MSG_TAG("invalid table offset", &tables[i].tag); | 598 return OTS_FAILURE_MSG_TAG("invalid table offset", tables[i].tag); |
| 490 } | 599 } |
| 491 // disallow all tables with a zero length | 600 // disallow all tables with a zero length |
| 492 if (tables[i].length < 1) { | 601 if (tables[i].length < 1) { |
| 493 // Note: malayalam.ttf has zero length CVT table... | 602 // Note: malayalam.ttf has zero length CVT table... |
| 494 return OTS_FAILURE_MSG_TAG("zero-length table", &tables[i].tag); | 603 return OTS_FAILURE_MSG_TAG("zero-length table", tables[i].tag); |
| 495 } | 604 } |
| 496 // disallow all tables with a length > 1GB | 605 // disallow all tables with a length > 1GB |
| 497 if (tables[i].length > 1024 * 1024 * 1024) { | 606 if (tables[i].length > 1024 * 1024 * 1024) { |
| 498 return OTS_FAILURE_MSG_TAG("table length exceeds 1GB", &tables[i].tag); | 607 return OTS_FAILURE_MSG_TAG("table length exceeds 1GB", tables[i].tag); |
| 499 } | 608 } |
| 500 // disallow tables where the uncompressed size is < the compressed size. | 609 // disallow tables where the uncompressed size is < the compressed size. |
| 501 if (tables[i].uncompressed_length < tables[i].length) { | 610 if (tables[i].uncompressed_length < tables[i].length) { |
| 502 return OTS_FAILURE_MSG_TAG("invalid compressed table", &tables[i].tag); | 611 return OTS_FAILURE_MSG_TAG("invalid compressed table", tables[i].tag); |
| 503 } | 612 } |
| 504 if (tables[i].uncompressed_length > tables[i].length) { | 613 if (tables[i].uncompressed_length > tables[i].length) { |
| 505 // We'll probably be decompressing this table. | 614 // We'll probably be decompressing this table. |
| 506 | 615 |
| 507 // disallow all tables which uncompress to > 30 MB | 616 // disallow all tables which uncompress to > 30 MB |
| 508 if (tables[i].uncompressed_length > 30 * 1024 * 1024) { | 617 if (tables[i].uncompressed_length > 30 * 1024 * 1024) { |
| 509 return OTS_FAILURE_MSG_TAG("uncompressed length exceeds 30MB", &tables[i
].tag); | 618 return OTS_FAILURE_MSG_TAG("uncompressed length exceeds 30MB", tables[i]
.tag); |
| 510 } | 619 } |
| 511 if (uncompressed_sum + tables[i].uncompressed_length < uncompressed_sum) { | 620 if (uncompressed_sum + tables[i].uncompressed_length < uncompressed_sum) { |
| 512 return OTS_FAILURE_MSG_TAG("overflow of uncompressed sum", &tables[i].ta
g); | 621 return OTS_FAILURE_MSG_TAG("overflow of uncompressed sum", tables[i].tag
); |
| 513 } | 622 } |
| 514 | 623 |
| 515 uncompressed_sum += tables[i].uncompressed_length; | 624 uncompressed_sum += tables[i].uncompressed_length; |
| 516 } | 625 } |
| 517 // since we required that the file be < 1GB in length, and that the table | 626 // since we required that the file be < 1GB in length, and that the table |
| 518 // length is < 1GB, the following addtion doesn't overflow | 627 // length is < 1GB, the following addtion doesn't overflow |
| 519 uint32_t end_byte = tables[i].offset + tables[i].length; | 628 uint32_t end_byte = tables[i].offset + tables[i].length; |
| 520 // Tables in the WOFF file must be aligned 4-byte boundary. | 629 // Tables in the WOFF file must be aligned 4-byte boundary. |
| 521 if (signature == Tag("wOFF")) { | 630 if (signature == OTS_TAG('w','O','F','F')) { |
| 522 end_byte = ots::Round4(end_byte); | 631 end_byte = ots::Round4(end_byte); |
| 523 } | 632 } |
| 524 if (!end_byte || end_byte > length) { | 633 if (!end_byte || end_byte > length) { |
| 525 return OTS_FAILURE_MSG_TAG("table overruns end of file", &tables[i].tag); | 634 return OTS_FAILURE_MSG_TAG("table overruns end of file", tables[i].tag); |
| 526 } | 635 } |
| 527 } | 636 } |
| 528 | 637 |
| 529 // All decompressed tables uncompressed must be <= 30MB. | 638 // All decompressed tables uncompressed must be <= 30MB. |
| 530 if (uncompressed_sum > 30 * 1024 * 1024) { | 639 if (uncompressed_sum > 30 * 1024 * 1024) { |
| 531 return OTS_FAILURE_MSG_HDR("uncompressed sum exceeds 30MB"); | 640 return OTS_FAILURE_MSG_HDR("uncompressed sum exceeds 30MB"); |
| 532 } | 641 } |
| 533 | 642 |
| 534 std::map<uint32_t, OpenTypeTable> table_map; | 643 std::map<uint32_t, OpenTypeTable> table_map; |
| 535 for (unsigned i = 0; i < header->num_tables; ++i) { | 644 for (unsigned i = 0; i < font->num_tables; ++i) { |
| 536 table_map[tables[i].tag] = tables[i]; | 645 table_map[tables[i].tag] = tables[i]; |
| 537 } | 646 } |
| 538 | 647 |
| 539 // check that the tables are not overlapping. | 648 // check that the tables are not overlapping. |
| 540 std::vector<std::pair<uint32_t, uint8_t> > overlap_checker; | 649 std::vector<std::pair<uint32_t, uint8_t> > overlap_checker; |
| 541 for (unsigned i = 0; i < header->num_tables; ++i) { | 650 for (unsigned i = 0; i < font->num_tables; ++i) { |
| 542 overlap_checker.push_back( | 651 overlap_checker.push_back( |
| 543 std::make_pair(tables[i].offset, static_cast<uint8_t>(1) /* start */)); | 652 std::make_pair(tables[i].offset, static_cast<uint8_t>(1) /* start */)); |
| 544 overlap_checker.push_back( | 653 overlap_checker.push_back( |
| 545 std::make_pair(tables[i].offset + tables[i].length, | 654 std::make_pair(tables[i].offset + tables[i].length, |
| 546 static_cast<uint8_t>(0) /* end */)); | 655 static_cast<uint8_t>(0) /* end */)); |
| 547 } | 656 } |
| 548 std::sort(overlap_checker.begin(), overlap_checker.end()); | 657 std::sort(overlap_checker.begin(), overlap_checker.end()); |
| 549 int overlap_count = 0; | 658 int overlap_count = 0; |
| 550 for (unsigned i = 0; i < overlap_checker.size(); ++i) { | 659 for (unsigned i = 0; i < overlap_checker.size(); ++i) { |
| 551 overlap_count += (overlap_checker[i].second ? 1 : -1); | 660 overlap_count += (overlap_checker[i].second ? 1 : -1); |
| 552 if (overlap_count > 1) { | 661 if (overlap_count > 1) { |
| 553 return OTS_FAILURE_MSG_HDR("overlapping tables"); | 662 return OTS_FAILURE_MSG_HDR("overlapping tables"); |
| 554 } | 663 } |
| 555 } | 664 } |
| 556 | 665 |
| 557 Arena arena; | 666 Arena arena; |
| 558 | 667 |
| 559 for (unsigned i = 0; ; ++i) { | 668 for (unsigned i = 0; ; ++i) { |
| 560 if (table_parsers[i].parse == NULL) break; | 669 if (table_parsers[i].parse == NULL) break; |
| 561 | 670 |
| 562 uint32_t tag = Tag(table_parsers[i].tag); | 671 uint32_t tag = table_parsers[i].tag; |
| 563 const std::map<uint32_t, OpenTypeTable>::const_iterator it = table_map.find(
tag); | 672 const std::map<uint32_t, OpenTypeTable>::const_iterator it = table_map.find(
tag); |
| 564 | 673 |
| 565 ots::TableAction action = GetTableAction(header, tag); | 674 ots::TableAction action = GetTableAction(header, tag); |
| 566 if (it == table_map.end()) { | 675 if (it == table_map.end()) { |
| 567 if (table_parsers[i].required && action == ots::TABLE_ACTION_SANITIZE) { | 676 if (table_parsers[i].required && action == ots::TABLE_ACTION_SANITIZE) { |
| 568 return OTS_FAILURE_MSG_TAG("missing required table", table_parsers[i].ta
g); | 677 return OTS_FAILURE_MSG_TAG("missing required table", table_parsers[i].ta
g); |
| 569 } | 678 } |
| 570 continue; | 679 continue; |
| 571 } | 680 } |
| 572 | 681 |
| 573 const uint8_t* table_data; | 682 uint32_t input_offset = it->second.offset; |
| 574 size_t table_length; | 683 const ots::TableMap::const_iterator ot = header->tables.find(input_offset); |
| 684 if (ot == header->tables.end()) { |
| 685 const uint8_t* table_data; |
| 686 size_t table_length; |
| 575 | 687 |
| 576 if (!GetTableData(data, it->second, &arena, &table_length, &table_data)) { | 688 if (!GetTableData(data, it->second, &arena, &table_length, &table_data)) { |
| 577 return OTS_FAILURE_MSG_TAG("uncompress failed", table_parsers[i].tag); | 689 return OTS_FAILURE_MSG_TAG("uncompress failed", table_parsers[i].tag); |
| 578 } | 690 } |
| 579 | 691 |
| 580 if (action == ots::TABLE_ACTION_SANITIZE && | 692 if (action == ots::TABLE_ACTION_SANITIZE && |
| 581 !table_parsers[i].parse(header, table_data, table_length)) { | 693 !table_parsers[i].parse(font, table_data, table_length)) { |
| 582 // TODO: parsers should generate specific messages detailing the failure; | 694 // TODO: parsers should generate specific messages detailing the failure
; |
| 583 // once those are all added, we won't need a generic failure message here | 695 // once those are all added, we won't need a generic failure message her
e |
| 584 return OTS_FAILURE_MSG_TAG("failed to parse table", table_parsers[i].tag); | 696 return OTS_FAILURE_MSG_TAG("failed to parse table", table_parsers[i].tag
); |
| 697 } |
| 698 } else if (action == ots::TABLE_ACTION_SANITIZE) { |
| 699 table_parsers[i].reuse(font, ot->second.first); |
| 585 } | 700 } |
| 586 } | 701 } |
| 587 | 702 |
| 588 if (header->cff) { | 703 if (font->cff) { |
| 589 // font with PostScript glyph | 704 // font with PostScript glyph |
| 590 if (header->version != Tag("OTTO")) { | 705 if (font->version != OTS_TAG('O','T','T','O')) { |
| 591 return OTS_FAILURE_MSG_HDR("wrong font version for PostScript glyph data")
; | 706 return OTS_FAILURE_MSG_HDR("wrong font version for PostScript glyph data")
; |
| 592 } | 707 } |
| 593 if (header->glyf || header->loca) { | 708 if (font->glyf || font->loca) { |
| 594 // mixing outline formats is not recommended | 709 // mixing outline formats is not recommended |
| 595 return OTS_FAILURE_MSG_HDR("font contains both PS and TT glyphs"); | 710 return OTS_FAILURE_MSG_HDR("font contains both PS and TT glyphs"); |
| 596 } | 711 } |
| 597 } else { | 712 } else { |
| 598 if (!header->glyf || !header->loca) { | 713 if (!font->glyf || !font->loca) { |
| 599 // No TrueType glyph found. | 714 // No TrueType glyph found. |
| 600 #define PASSTHRU_TABLE(TAG) (table_map.find(Tag(TAG)) != table_map.end() && \ | 715 #define PASSTHRU_TABLE(tag_) (table_map.find(tag_) != table_map.end() && \ |
| 601 GetTableAction(header, Tag(TAG)) == ots::TABLE_ACTI
ON_PASSTHRU) | 716 GetTableAction(header, tag_) == ots::TABLE_ACTION_
PASSTHRU) |
| 602 // We don't sanitise bitmap table, but don't reject bitmap-only fonts if | 717 // We don't sanitise bitmap table, but don't reject bitmap-only fonts if |
| 603 // we keep the tables. | 718 // we keep the tables. |
| 604 if (!PASSTHRU_TABLE("CBDT") || !PASSTHRU_TABLE("CBLC")) { | 719 if (!PASSTHRU_TABLE(OTS_TAG('C','B','D','T')) || |
| 720 !PASSTHRU_TABLE(OTS_TAG('C','B','L','C'))) { |
| 605 return OTS_FAILURE_MSG_HDR("no supported glyph shapes table(s) present")
; | 721 return OTS_FAILURE_MSG_HDR("no supported glyph shapes table(s) present")
; |
| 606 } | 722 } |
| 607 #undef PASSTHRU_TABLE | 723 #undef PASSTHRU_TABLE |
| 608 } | 724 } |
| 609 } | 725 } |
| 610 | 726 |
| 611 uint16_t num_output_tables = 0; | 727 uint16_t num_output_tables = 0; |
| 612 for (unsigned i = 0; ; ++i) { | |
| 613 if (table_parsers[i].parse == NULL) { | |
| 614 break; | |
| 615 } | |
| 616 | |
| 617 if (table_parsers[i].should_serialise(header)) { | |
| 618 num_output_tables++; | |
| 619 } | |
| 620 } | |
| 621 | |
| 622 for (std::map<uint32_t, OpenTypeTable>::const_iterator it = table_map.begin(); | 728 for (std::map<uint32_t, OpenTypeTable>::const_iterator it = table_map.begin(); |
| 623 it != table_map.end(); ++it) { | 729 it != table_map.end(); ++it) { |
| 624 ots::TableAction action = GetTableAction(header, it->first); | 730 ots::TableAction action = GetTableAction(header, it->first); |
| 625 if (action == ots::TABLE_ACTION_PASSTHRU) { | 731 if (action == ots::TABLE_ACTION_PASSTHRU) { |
| 626 num_output_tables++; | 732 num_output_tables++; |
| 733 } else { |
| 734 for (unsigned i = 0; table_parsers[i].parse != NULL; ++i) { |
| 735 if (table_parsers[i].tag == it->first && |
| 736 table_parsers[i].should_serialise(font)) { |
| 737 num_output_tables++; |
| 738 break; |
| 739 } |
| 740 } |
| 627 } | 741 } |
| 628 } | 742 } |
| 629 | 743 |
| 630 uint16_t max_pow2 = 0; | 744 uint16_t max_pow2 = 0; |
| 631 while (1u << (max_pow2 + 1) <= num_output_tables) { | 745 while (1u << (max_pow2 + 1) <= num_output_tables) { |
| 632 max_pow2++; | 746 max_pow2++; |
| 633 } | 747 } |
| 634 const uint16_t output_search_range = (1u << max_pow2) << 4; | 748 const uint16_t output_search_range = (1u << max_pow2) << 4; |
| 635 | 749 |
| 636 // most of the errors here are highly unlikely - they'd only occur if the | 750 // most of the errors here are highly unlikely - they'd only occur if the |
| 637 // output stream returns a failure, e.g. lack of space to write | 751 // output stream returns a failure, e.g. lack of space to write |
| 638 output->ResetChecksum(); | 752 output->ResetChecksum(); |
| 639 if (!output->WriteTag(header->version) || | 753 if (!output->WriteU32(font->version) || |
| 640 !output->WriteU16(num_output_tables) || | 754 !output->WriteU16(num_output_tables) || |
| 641 !output->WriteU16(output_search_range) || | 755 !output->WriteU16(output_search_range) || |
| 642 !output->WriteU16(max_pow2) || | 756 !output->WriteU16(max_pow2) || |
| 643 !output->WriteU16((num_output_tables << 4) - output_search_range)) { | 757 !output->WriteU16((num_output_tables << 4) - output_search_range)) { |
| 644 return OTS_FAILURE_MSG_HDR("error writing output"); | 758 return OTS_FAILURE_MSG_HDR("error writing output"); |
| 645 } | 759 } |
| 646 const uint32_t offset_table_chksum = output->chksum(); | 760 const uint32_t offset_table_chksum = output->chksum(); |
| 647 | 761 |
| 648 const size_t table_record_offset = output->Tell(); | 762 const size_t table_record_offset = output->Tell(); |
| 649 if (!output->Pad(16 * num_output_tables)) { | 763 if (!output->Pad(16 * num_output_tables)) { |
| 650 return OTS_FAILURE_MSG_HDR("error writing output"); | 764 return OTS_FAILURE_MSG_HDR("error writing output"); |
| 651 } | 765 } |
| 652 | 766 |
| 653 std::vector<OutputTable> out_tables; | 767 std::vector<ots::OutputTable> out_tables; |
| 654 | 768 |
| 655 size_t head_table_offset = 0; | 769 size_t head_table_offset = 0; |
| 656 for (unsigned i = 0; ; ++i) { | |
| 657 if (table_parsers[i].parse == NULL) { | |
| 658 break; | |
| 659 } | |
| 660 | |
| 661 if (!table_parsers[i].should_serialise(header)) { | |
| 662 continue; | |
| 663 } | |
| 664 | |
| 665 OutputTable out; | |
| 666 uint32_t tag = Tag(table_parsers[i].tag); | |
| 667 out.tag = tag; | |
| 668 out.offset = output->Tell(); | |
| 669 | |
| 670 output->ResetChecksum(); | |
| 671 if (tag == Tag("head")) { | |
| 672 head_table_offset = out.offset; | |
| 673 } | |
| 674 if (!table_parsers[i].serialise(output, header)) { | |
| 675 return OTS_FAILURE_MSG_TAG("failed to serialize table", table_parsers[i].t
ag); | |
| 676 } | |
| 677 | |
| 678 const size_t end_offset = output->Tell(); | |
| 679 if (end_offset <= out.offset) { | |
| 680 // paranoid check. |end_offset| is supposed to be greater than the offset, | |
| 681 // as long as the Tell() interface is implemented correctly. | |
| 682 return OTS_FAILURE_MSG_HDR("error writing output"); | |
| 683 } | |
| 684 out.length = end_offset - out.offset; | |
| 685 | |
| 686 // align tables to four bytes | |
| 687 if (!output->Pad((4 - (end_offset & 3)) % 4)) { | |
| 688 return OTS_FAILURE_MSG_HDR("error writing output"); | |
| 689 } | |
| 690 out.chksum = output->chksum(); | |
| 691 out_tables.push_back(out); | |
| 692 } | |
| 693 | |
| 694 for (std::map<uint32_t, OpenTypeTable>::const_iterator it = table_map.begin(); | 770 for (std::map<uint32_t, OpenTypeTable>::const_iterator it = table_map.begin(); |
| 695 it != table_map.end(); ++it) { | 771 it != table_map.end(); ++it) { |
| 696 ots::TableAction action = GetTableAction(header, it->first); | 772 uint32_t input_offset = it->second.offset; |
| 697 if (action == ots::TABLE_ACTION_PASSTHRU) { | 773 const ots::TableMap::const_iterator ot = header->tables.find(input_offset); |
| 698 OutputTable out; | 774 if (ot != header->tables.end()) { |
| 699 out.tag = it->second.tag; | 775 ots::OutputTable out = ot->second.second; |
| 776 if (out.tag == OTS_TAG('h','e','a','d')) { |
| 777 head_table_offset = out.offset; |
| 778 } |
| 779 out_tables.push_back(out); |
| 780 } else { |
| 781 ots::OutputTable out; |
| 782 out.tag = it->first; |
| 700 out.offset = output->Tell(); | 783 out.offset = output->Tell(); |
| 701 | 784 |
| 702 output->ResetChecksum(); | 785 if (out.tag == OTS_TAG('h','e','a','d')) { |
| 703 if (it->second.tag == Tag("head")) { | |
| 704 head_table_offset = out.offset; | 786 head_table_offset = out.offset; |
| 705 } | 787 } |
| 706 | 788 |
| 707 const uint8_t* table_data; | 789 ots::TableAction action = GetTableAction(header, it->first); |
| 708 size_t table_length; | 790 if (action == ots::TABLE_ACTION_PASSTHRU) { |
| 791 output->ResetChecksum(); |
| 792 const uint8_t* table_data; |
| 793 size_t table_length; |
| 709 | 794 |
| 710 if (!GetTableData(data, it->second, &arena, &table_length, &table_data)) { | 795 if (!GetTableData(data, it->second, &arena, &table_length, &table_data))
{ |
| 711 return OTS_FAILURE_MSG_HDR("Failed to uncompress table"); | 796 return OTS_FAILURE_MSG_HDR("Failed to uncompress table"); |
| 797 } |
| 798 |
| 799 if (!output->Write(table_data, table_length)) { |
| 800 return OTS_FAILURE_MSG_HDR("Failed to serialize table"); |
| 801 } |
| 802 |
| 803 const size_t end_offset = output->Tell(); |
| 804 if (end_offset <= out.offset) { |
| 805 // paranoid check. |end_offset| is supposed to be greater than the off
set, |
| 806 // as long as the Tell() interface is implemented correctly. |
| 807 return OTS_FAILURE_MSG_HDR("error writing output"); |
| 808 } |
| 809 out.length = end_offset - out.offset; |
| 810 |
| 811 // align tables to four bytes |
| 812 if (!output->Pad((4 - (end_offset & 3)) % 4)) { |
| 813 return OTS_FAILURE_MSG_HDR("error writing output"); |
| 814 } |
| 815 out.chksum = output->chksum(); |
| 816 out_tables.push_back(out); |
| 817 header->tables[input_offset] = std::make_pair(font, out); |
| 818 } else { |
| 819 for (unsigned i = 0; table_parsers[i].parse != NULL; ++i) { |
| 820 if (table_parsers[i].tag == it->first && |
| 821 table_parsers[i].should_serialise(font)) { |
| 822 output->ResetChecksum(); |
| 823 if (!table_parsers[i].serialise(output, font)) { |
| 824 return OTS_FAILURE_MSG_TAG("failed to serialize table", table_pars
ers[i].tag); |
| 825 } |
| 826 |
| 827 const size_t end_offset = output->Tell(); |
| 828 if (end_offset <= out.offset) { |
| 829 // paranoid check. |end_offset| is supposed to be greater than the
offset, |
| 830 // as long as the Tell() interface is implemented correctly. |
| 831 return OTS_FAILURE_MSG_HDR("error writing output"); |
| 832 } |
| 833 out.length = end_offset - out.offset; |
| 834 |
| 835 // align tables to four bytes |
| 836 if (!output->Pad((4 - (end_offset & 3)) % 4)) { |
| 837 return OTS_FAILURE_MSG_HDR("error writing output"); |
| 838 } |
| 839 out.chksum = output->chksum(); |
| 840 out_tables.push_back(out); |
| 841 header->tables[input_offset] = std::make_pair(font, out); |
| 842 |
| 843 break; |
| 844 } |
| 845 } |
| 712 } | 846 } |
| 713 | |
| 714 if (!output->Write(table_data, table_length)) { | |
| 715 return OTS_FAILURE_MSG_HDR("Failed to serialize table"); | |
| 716 } | |
| 717 | |
| 718 const size_t end_offset = output->Tell(); | |
| 719 if (end_offset <= out.offset) { | |
| 720 // paranoid check. |end_offset| is supposed to be greater than the offse
t, | |
| 721 // as long as the Tell() interface is implemented correctly. | |
| 722 return OTS_FAILURE_MSG_HDR("error writing output"); | |
| 723 } | |
| 724 out.length = end_offset - out.offset; | |
| 725 | |
| 726 // align tables to four bytes | |
| 727 if (!output->Pad((4 - (end_offset & 3)) % 4)) { | |
| 728 return OTS_FAILURE_MSG_HDR("error writing output"); | |
| 729 } | |
| 730 out.chksum = output->chksum(); | |
| 731 out_tables.push_back(out); | |
| 732 } | 847 } |
| 733 } | 848 } |
| 734 | 849 |
| 735 const size_t end_of_file = output->Tell(); | 850 const size_t end_of_file = output->Tell(); |
| 736 | 851 |
| 737 // Need to sort the output tables for inclusion in the file | 852 // Need to sort the output tables for inclusion in the file |
| 738 std::sort(out_tables.begin(), out_tables.end()); | 853 std::sort(out_tables.begin(), out_tables.end()); |
| 739 if (!output->Seek(table_record_offset)) { | 854 if (!output->Seek(table_record_offset)) { |
| 740 return OTS_FAILURE_MSG_HDR("error writing output"); | 855 return OTS_FAILURE_MSG_HDR("error writing output"); |
| 741 } | 856 } |
| 742 | 857 |
| 743 output->ResetChecksum(); | 858 output->ResetChecksum(); |
| 744 uint32_t tables_chksum = 0; | 859 uint32_t tables_chksum = 0; |
| 745 for (unsigned i = 0; i < out_tables.size(); ++i) { | 860 for (unsigned i = 0; i < out_tables.size(); ++i) { |
| 746 if (!output->WriteTag(out_tables[i].tag) || | 861 if (!output->WriteU32(out_tables[i].tag) || |
| 747 !output->WriteU32(out_tables[i].chksum) || | 862 !output->WriteU32(out_tables[i].chksum) || |
| 748 !output->WriteU32(out_tables[i].offset) || | 863 !output->WriteU32(out_tables[i].offset) || |
| 749 !output->WriteU32(out_tables[i].length)) { | 864 !output->WriteU32(out_tables[i].length)) { |
| 750 return OTS_FAILURE_MSG_HDR("error writing output"); | 865 return OTS_FAILURE_MSG_HDR("error writing output"); |
| 751 } | 866 } |
| 752 tables_chksum += out_tables[i].chksum; | 867 tables_chksum += out_tables[i].chksum; |
| 753 } | 868 } |
| 754 const uint32_t table_record_chksum = output->chksum(); | 869 const uint32_t table_record_chksum = output->chksum(); |
| 755 | 870 |
| 756 // http://www.microsoft.com/typography/otspec/otff.htm | 871 // http://www.microsoft.com/typography/otspec/otff.htm |
| (...skipping 17 matching lines...) Expand all Loading... |
| 774 } | 889 } |
| 775 | 890 |
| 776 return true; | 891 return true; |
| 777 } | 892 } |
| 778 | 893 |
| 779 } // namespace | 894 } // namespace |
| 780 | 895 |
| 781 namespace ots { | 896 namespace ots { |
| 782 | 897 |
| 783 bool IsValidVersionTag(uint32_t tag) { | 898 bool IsValidVersionTag(uint32_t tag) { |
| 784 return tag == Tag("\x00\x01\x00\x00") || | 899 return tag == 0x000010000 || |
| 785 // OpenType fonts with CFF data have 'OTTO' tag. | 900 // OpenType fonts with CFF data have 'OTTO' tag. |
| 786 tag == Tag("OTTO") || | 901 tag == OTS_TAG('O','T','T','O') || |
| 787 // Older Mac fonts might have 'true' or 'typ1' tag. | 902 // Older Mac fonts might have 'true' or 'typ1' tag. |
| 788 tag == Tag("true") || | 903 tag == OTS_TAG('t','r','u','e') || |
| 789 tag == Tag("typ1"); | 904 tag == OTS_TAG('t','y','p','1'); |
| 790 } | 905 } |
| 791 | 906 |
| 792 bool OTSContext::Process(OTSStream *output, | 907 bool OTSContext::Process(OTSStream *output, |
| 793 const uint8_t *data, | 908 const uint8_t *data, |
| 794 size_t length) { | 909 size_t length, |
| 910 uint32_t index) { |
| 795 OpenTypeFile header; | 911 OpenTypeFile header; |
| 796 | 912 Font font(&header); |
| 797 header.context = this; | 913 header.context = this; |
| 798 | 914 |
| 799 if (length < 4) { | 915 if (length < 4) { |
| 800 return OTS_FAILURE_MSG_(&header, "file less than 4 bytes"); | 916 return OTS_FAILURE_MSG_(&header, "file less than 4 bytes"); |
| 801 } | 917 } |
| 802 | 918 |
| 803 bool result; | 919 bool result; |
| 804 if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == 'F') { | 920 if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == 'F') { |
| 805 result = ProcessWOFF(&header, output, data, length); | 921 result = ProcessWOFF(&header, &font, output, data, length); |
| 806 } else if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == '2
') { | 922 } else if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == '2
') { |
| 807 result = ProcessWOFF2(&header, output, data, length); | 923 result = ProcessWOFF2(&header, &font, output, data, length); |
| 924 } else if (data[0] == 't' && data[1] == 't' && data[2] == 'c' && data[3] == 'f
') { |
| 925 result = ProcessTTC(&header, output, data, length, index); |
| 808 } else { | 926 } else { |
| 809 result = ProcessTTF(&header, output, data, length); | 927 result = ProcessTTF(&header, &font, output, data, length); |
| 810 } | 928 } |
| 811 | 929 |
| 812 for (unsigned i = 0; ; ++i) { | |
| 813 if (table_parsers[i].parse == NULL) break; | |
| 814 table_parsers[i].free(&header); | |
| 815 } | |
| 816 return result; | 930 return result; |
| 817 } | 931 } |
| 818 | 932 |
| 819 } // namespace ots | 933 } // namespace ots |
| OLD | NEW |