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