diff options
author | ksakamoto <ksakamoto@chromium.org> | 2015-04-07 01:18:42 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-07 08:19:13 +0000 |
commit | 3a672fe050438bc787af3f9bb652048291558c6c (patch) | |
tree | df4723114239a6a8124f490dece5eebbd7fdf47d /third_party/ots | |
parent | 9318af7ff5e16a28a67c9d1f1844e3f32725069c (diff) | |
download | chromium_src-3a672fe050438bc787af3f9bb652048291558c6c.zip chromium_src-3a672fe050438bc787af3f9bb652048291558c6c.tar.gz chromium_src-3a672fe050438bc787af3f9bb652048291558c6c.tar.bz2 |
Update OTS to revision 6d2e08b
This brings in a bugfix of checksum calculation that caused assertion
failure on debug builds.
BUG=464780
Review URL: https://codereview.chromium.org/1062093002
Cr-Commit-Position: refs/heads/master@{#324023}
Diffstat (limited to 'third_party/ots')
-rw-r--r-- | third_party/ots/README.chromium | 3 | ||||
-rw-r--r-- | third_party/ots/include/opentype-sanitiser.h | 64 | ||||
-rw-r--r-- | third_party/ots/src/cmap.cc | 5 | ||||
-rw-r--r-- | third_party/ots/src/os2.cc | 119 | ||||
-rw-r--r-- | third_party/ots/src/os2.h | 2 | ||||
-rw-r--r-- | third_party/ots/src/ots.cc | 9 | ||||
-rw-r--r-- | third_party/ots/src/woff2.cc | 36 | ||||
-rw-r--r-- | third_party/ots/test/ot-sanitise.cc | 14 | ||||
-rw-r--r-- | third_party/ots/test/side-by-side.cc | 4 |
9 files changed, 125 insertions, 131 deletions
diff --git a/third_party/ots/README.chromium b/third_party/ots/README.chromium index ffe584d..befc3aa 100644 --- a/third_party/ots/README.chromium +++ b/third_party/ots/README.chromium @@ -1,8 +1,9 @@ Name: OTS URL: https://github.com/khaledhosny/ots.git -Version: 4d011721c2eadd4e649326f8d164423db060d85e +Version: 6d2e08bdc56d6739d48f5e59594669616f6b8b9d Security Critical: yes License: BSD Local Modifications: - src/woff2.cc: Changed include path to brotli/dec/decode.h. +- BUILD.gn: Added. diff --git a/third_party/ots/include/opentype-sanitiser.h b/third_party/ots/include/opentype-sanitiser.h index 1c445ae..285a438 100644 --- a/third_party/ots/include/opentype-sanitiser.h +++ b/third_party/ots/include/opentype-sanitiser.h @@ -29,6 +29,9 @@ typedef unsigned __int64 uint64_t; #include <cstddef> #include <cstring> +#define OTS_TAG(c1,c2,c3,c4) ((uint32_t)((((uint8_t)(c1))<<24)|(((uint8_t)(c2))<<16)|(((uint8_t)(c3))<<8)|((uint8_t)(c4)))) +#define OTS_UNTAG(tag) ((uint8_t)((tag)>>24)), ((uint8_t)((tag)>>16)), ((uint8_t)((tag)>>8)), ((uint8_t)(tag)) + namespace ots { // ----------------------------------------------------------------------------- @@ -37,9 +40,7 @@ namespace ots { // ----------------------------------------------------------------------------- class OTSStream { public: - OTSStream() { - ResetChecksum(); - } + OTSStream() : chksum_(0) {} virtual ~OTSStream() {} @@ -51,20 +52,15 @@ class OTSStream { const size_t orig_length = length; size_t offset = 0; - if (chksum_buffer_offset_) { - const size_t l = - std::min(length, static_cast<size_t>(4) - chksum_buffer_offset_); - std::memcpy(chksum_buffer_ + chksum_buffer_offset_, data, l); - chksum_buffer_offset_ += l; - offset += l; - length -= l; - } - if (chksum_buffer_offset_ == 4) { - uint32_t tmp; - std::memcpy(&tmp, chksum_buffer_, 4); + size_t chksum_offset = Tell() & 3; + if (chksum_offset) { + const size_t l = std::min(length, static_cast<size_t>(4) - chksum_offset); + uint32_t tmp = 0; + std::memcpy(reinterpret_cast<uint8_t *>(&tmp) + chksum_offset, data, l); chksum_ += ntohl(tmp); - chksum_buffer_offset_ = 0; + length -= l; + offset += l; } while (length >= 4) { @@ -77,11 +73,11 @@ class OTSStream { } if (length) { - if (chksum_buffer_offset_ != 0) return false; // not reached if (length > 4) return false; // not reached - std::memcpy(chksum_buffer_, - reinterpret_cast<const uint8_t*>(data) + offset, length); - chksum_buffer_offset_ = length; + uint32_t tmp = 0; + std::memcpy(&tmp, + reinterpret_cast<const uint8_t*>(data) + offset, length); + chksum_ += ntohl(tmp); } return WriteRaw(data, orig_length); @@ -142,41 +138,16 @@ class OTSStream { } void ResetChecksum() { + assert((Tell() & 3) == 0); chksum_ = 0; - chksum_buffer_offset_ = 0; } uint32_t chksum() const { - assert(chksum_buffer_offset_ == 0); return chksum_; } - struct ChecksumState { - uint32_t chksum; - uint8_t chksum_buffer[4]; - unsigned chksum_buffer_offset; - }; - - ChecksumState SaveChecksumState() const { - ChecksumState s; - s.chksum = chksum_; - s.chksum_buffer_offset = chksum_buffer_offset_; - std::memcpy(s.chksum_buffer, chksum_buffer_, 4); - - return s; - } - - void RestoreChecksum(const ChecksumState &s) { - assert(chksum_buffer_offset_ == 0); - chksum_ += s.chksum; - chksum_buffer_offset_ = s.chksum_buffer_offset; - std::memcpy(chksum_buffer_, s.chksum_buffer, 4); - } - protected: uint32_t chksum_; - uint8_t chksum_buffer_[4]; - unsigned chksum_buffer_offset_; }; #ifdef __GCC__ @@ -218,9 +189,6 @@ class OTSContext { virtual TableAction GetTableAction(uint32_t tag) { return ots::TABLE_ACTION_DEFAULT; } }; -// For backward compatibility - remove once Chrome switches over to the new API. -bool Process(OTSStream *output, const uint8_t *input, size_t length); - } // namespace ots #endif // OPENTYPE_SANITISER_H_ diff --git a/third_party/ots/src/cmap.cc b/third_party/ots/src/cmap.cc index b1bf5fd..a238300 100644 --- a/third_party/ots/src/cmap.cc +++ b/third_party/ots/src/cmap.cc @@ -1023,10 +1023,6 @@ bool ots_cmap_serialise(OTSStream *out, OpenTypeFile *file) { } const off_t table_end = out->Tell(); - // We might have hanging bytes from the above's checksum which the OTSStream - // then merges into the table of offsets. - OTSStream::ChecksumState saved_checksum = out->SaveChecksumState(); - out->ResetChecksum(); // Now seek back and write the table of offsets if (!out->Seek(record_offset)) { @@ -1092,7 +1088,6 @@ bool ots_cmap_serialise(OTSStream *out, OpenTypeFile *file) { if (!out->Seek(table_end)) { return OTS_FAILURE(); } - out->RestoreChecksum(saved_checksum); return true; } diff --git a/third_party/ots/src/os2.cc b/third_party/ots/src/os2.cc index 915877e..3c0d1fd 100644 --- a/third_party/ots/src/os2.cc +++ b/third_party/ots/src/os2.cc @@ -2,8 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "os2.h" +#include <string> +#include "os2.h" #include "head.h" // OS/2 - OS/2 and Windows Metrics @@ -35,26 +36,29 @@ bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { !table.ReadS16(&os2->strikeout_size) || !table.ReadS16(&os2->strikeout_position) || !table.ReadS16(&os2->family_class)) { - return OTS_FAILURE_MSG("Failed toi read basic os2 elements"); + return OTS_FAILURE_MSG("Error reading basic table elements"); } - if (os2->version > 4) { - return OTS_FAILURE_MSG("os2 version too high %d", os2->version); + if (os2->version > 5) { + return OTS_FAILURE_MSG("Unsupported table version: %u", os2->version); } - // Some linux fonts (e.g., Kedage-t.ttf and LucidaSansDemiOblique.ttf) have - // weird weight/width classes. Overwrite them with FW_NORMAL/1/9. - if (os2->weight_class < 100 || - os2->weight_class > 900 || - os2->weight_class % 100) { - OTS_WARNING("bad weight: %u", os2->weight_class); - os2->weight_class = 400; // FW_NORMAL + // Follow WPF Font Selection Model's advice. + if (1 <= os2->weight_class && os2->weight_class <= 9) { + OTS_WARNING("Bad usWeightClass: %u, changing it to: %u", os2->weight_class, os2->weight_class * 100); + os2->weight_class *= 100; } + // Ditto. + if (os2->weight_class > 999) { + OTS_WARNING("Bad usWeightClass: %u, changing it to: %d", os2->weight_class, 999); + os2->weight_class = 999; + } + if (os2->width_class < 1) { - OTS_WARNING("bad width: %u", os2->width_class); + OTS_WARNING("Bad usWidthClass: %u, changing it to: %d", os2->width_class, 1); os2->width_class = 1; } else if (os2->width_class > 9) { - OTS_WARNING("bad width: %u", os2->width_class); + OTS_WARNING("Bad usWidthClass: %u, changing it to: %d", os2->width_class, 9); os2->width_class = 9; } @@ -73,30 +77,34 @@ bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { // mask reserved bits. use only 0..3, 8, 9 bits. os2->type &= 0x30f; - if (os2->subscript_x_size < 0) { - OTS_WARNING("bad subscript_x_size: %d", os2->subscript_x_size); - os2->subscript_x_size = 0; - } - if (os2->subscript_y_size < 0) { - OTS_WARNING("bad subscript_y_size: %d", os2->subscript_y_size); - os2->subscript_y_size = 0; - } - if (os2->superscript_x_size < 0) { - OTS_WARNING("bad superscript_x_size: %d", os2->superscript_x_size); - os2->superscript_x_size = 0; - } - if (os2->superscript_y_size < 0) { - OTS_WARNING("bad superscript_y_size: %d", os2->superscript_y_size); - os2->superscript_y_size = 0; - } - if (os2->strikeout_size < 0) { - OTS_WARNING("bad strikeout_size: %d", os2->strikeout_size); - os2->strikeout_size = 0; - } - +#define SET_TO_ZERO(a, b) \ + if (os2->b < 0) { \ + OTS_WARNING("Bad " a ": %d, setting it to zero", os2->b); \ + os2->b = 0; \ + } + + SET_TO_ZERO("ySubscriptXSize", subscript_x_size); + SET_TO_ZERO("ySubscriptYSize", subscript_y_size); + SET_TO_ZERO("ySuperscriptXSize", superscript_x_size); + SET_TO_ZERO("ySuperscriptYSize", superscript_y_size); + SET_TO_ZERO("yStrikeoutSize", strikeout_size); +#undef SET_TO_ZERO + + static std::string panose_strings[10] = { + "bFamilyType", + "bSerifStyle", + "bWeight", + "bProportion", + "bContrast", + "bStrokeVariation", + "bArmStyle", + "bLetterform", + "bMidline", + "bXHeight", + }; for (unsigned i = 0; i < 10; ++i) { if (!table.ReadU8(&os2->panose[i])) { - return OTS_FAILURE_MSG("Failed to read panose in os2 table"); + return OTS_FAILURE_MSG("Error reading PANOSE %s", panose_strings[i].c_str()); } } @@ -113,7 +121,7 @@ bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { !table.ReadS16(&os2->typo_linegap) || !table.ReadU16(&os2->win_ascent) || !table.ReadU16(&os2->win_descent)) { - return OTS_FAILURE_MSG("Failed to read more basic os2 fields"); + return OTS_FAILURE_MSG("Error reading more basic table fields"); } // If bit 6 is set, then bits 0 and 5 must be clear. @@ -124,7 +132,7 @@ bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { // the settings of bits 0 and 1 must be reflected in the macStyle bits // in the 'head' table. if (!file->head) { - return OTS_FAILURE_MSG("Head table missing from font as needed by os2 table"); + return OTS_FAILURE_MSG("Needed head table is missing from the font"); } if ((os2->selection & 0x1) && !(file->head->mac_style & 0x2)) { @@ -148,7 +156,7 @@ bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { if ((os2->version < 4) && (os2->selection & 0x300)) { // bit 8 and 9 must be unset in OS/2 table versions less than 4. - return OTS_FAILURE_MSG("OS2 version %d incompatible with selection %d", os2->version, os2->selection); + return OTS_FAILURE_MSG("Version %d incompatible with selection %d", os2->version, os2->selection); } // mask reserved bits. use only 0..9 bits. @@ -198,7 +206,7 @@ bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { !table.ReadU16(&os2->default_char) || !table.ReadU16(&os2->break_char) || !table.ReadU16(&os2->max_context)) { - return OTS_FAILURE_MSG("Failed to read os2 version 2 information"); + return OTS_FAILURE_MSG("Failed to read version 2-specific fields"); } if (os2->x_height < 0) { @@ -210,6 +218,26 @@ bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { os2->cap_height = 0; } + if (os2->version < 5) { + // http://www.microsoft.com/typography/otspec/os2ver4.htm + return true; + } + + if (!table.ReadU16(&os2->lower_optical_pointsize) || + !table.ReadU16(&os2->upper_optical_pointsize)) { + return OTS_FAILURE_MSG("Failed to read version 5-specific fields"); + } + + if (os2->lower_optical_pointsize > 0xFFFE) { + OTS_WARNING("'usLowerOpticalPointSize' is bigger than 0xFFFE: %d", os2->lower_optical_pointsize); + os2->lower_optical_pointsize = 0xFFFE; + } + + if (os2->upper_optical_pointsize < 2) { + OTS_WARNING("'usUpperOpticalPointSize' is lower than 2: %d", os2->upper_optical_pointsize); + os2->upper_optical_pointsize = 2; + } + return true; } @@ -258,7 +286,7 @@ bool ots_os2_serialise(OTSStream *out, OpenTypeFile *file) { !out->WriteS16(os2->typo_linegap) || !out->WriteU16(os2->win_ascent) || !out->WriteU16(os2->win_descent)) { - return OTS_FAILURE_MSG("Failed to write os2 version 1 information"); + return OTS_FAILURE_MSG("Failed to write version 1-specific fields"); } if (os2->version < 1) { @@ -279,7 +307,16 @@ bool ots_os2_serialise(OTSStream *out, OpenTypeFile *file) { !out->WriteU16(os2->default_char) || !out->WriteU16(os2->break_char) || !out->WriteU16(os2->max_context)) { - return OTS_FAILURE_MSG("Failed to write os2 version 2 information"); + return OTS_FAILURE_MSG("Failed to write version 2-specific fields"); + } + + if (os2->version < 2) { + return true; + } + + if (!out->WriteU16(os2->lower_optical_pointsize) || + !out->WriteU16(os2->upper_optical_pointsize)) { + return OTS_FAILURE_MSG("Failed to write version 5-specific fields"); } return true; diff --git a/third_party/ots/src/os2.h b/third_party/ots/src/os2.h index 9e0fc34..01511c5 100644 --- a/third_party/ots/src/os2.h +++ b/third_party/ots/src/os2.h @@ -47,6 +47,8 @@ struct OpenTypeOS2 { uint16_t default_char; uint16_t break_char; uint16_t max_context; + uint16_t lower_optical_pointsize; + uint16_t upper_optical_pointsize; }; } // namespace ots diff --git a/third_party/ots/src/ots.cc b/third_party/ots/src/ots.cc index 5ba8dd9..28d0285d 100644 --- a/third_party/ots/src/ots.cc +++ b/third_party/ots/src/ots.cc @@ -24,6 +24,7 @@ namespace { // Generate a message with or without a table tag, when 'header' is the OpenTypeFile pointer #define OTS_FAILURE_MSG_TAG(msg_,tag_) OTS_FAILURE_MSG_TAG_(header, msg_, tag_) #define OTS_FAILURE_MSG_HDR(msg_) OTS_FAILURE_MSG_(header, msg_) +#define OTS_WARNING_MSG_HDR(msg_) OTS_WARNING_MSG_(header, msg_) struct OpenTypeTable { @@ -472,7 +473,7 @@ bool ProcessGeneric(ots::OpenTypeFile *header, uint32_t signature, const uint32_t this_tag = ntohl(tables[i].tag); const uint32_t prev_tag = ntohl(tables[i - 1].tag); if (this_tag <= prev_tag) { - return OTS_FAILURE_MSG_HDR("table directory not correctly ordered"); + OTS_WARNING_MSG_HDR("Table directory is not correctly ordered"); } } @@ -818,10 +819,4 @@ bool OTSContext::Process(OTSStream *output, return result; } -// For backward compatibility -bool Process(OTSStream *output, const uint8_t *data, size_t length) { - static OTSContext context; - return context.Process(output, data, length); -} - } // namespace ots diff --git a/third_party/ots/src/woff2.cc b/third_party/ots/src/woff2.cc index b244aec..5a79598 100644 --- a/third_party/ots/src/woff2.cc +++ b/third_party/ots/src/woff2.cc @@ -45,7 +45,7 @@ const size_t kCompositeGlyphBegin = 10; // Note that the byte order is big-endian, not the same as ots.cc #define TAG(a, b, c, d) ((a << 24) | (b << 16) | (c << 8) | d) -#define CHR(t) (t >> 24), (t >> 16), (t >> 8), (t >> 0) +#define UNTAG(t) (t >> 24), (t >> 16), (t >> 8), (t >> 0) const unsigned int kWoff2FlagsTransform = 1 << 5; @@ -772,17 +772,6 @@ bool FixChecksums(const std::vector<Table>& tables, uint8_t* dst) { return true; } -bool Woff2Uncompress(uint8_t* dst_buf, size_t dst_size, - const uint8_t* src_buf, size_t src_size) { - size_t uncompressed_size = dst_size; - int ok = BrotliDecompressBuffer(src_size, src_buf, - &uncompressed_size, dst_buf); - if (!ok || uncompressed_size != dst_size) { - return OTS_FAILURE(); - } - return true; -} - bool ReadTableDirectory(ots::OpenTypeFile* file, ots::Buffer* buffer, std::vector<Table>* tables, size_t num_tables) { @@ -812,12 +801,16 @@ bool ReadTableDirectory(ots::OpenTypeFile* file, } uint32_t dst_length; if (!ReadBase128(buffer, &dst_length)) { - return OTS_FAILURE_MSG("Failed to read 'origLength' for table '%c%c%c%c'", CHR(tag)); + return OTS_FAILURE_MSG("Failed to read 'origLength' for table '%c%c%c%c'", UNTAG(tag)); } uint32_t transform_length = dst_length; if ((flags & kWoff2FlagsTransform) != 0) { if (!ReadBase128(buffer, &transform_length)) { - return OTS_FAILURE_MSG("Failed to read 'transformLength' for table '%c%c%c%c'", CHR(tag)); + return OTS_FAILURE_MSG("Failed to read 'transformLength' for table '%c%c%c%c'", UNTAG(tag)); + } + + if (tag == TAG('l', 'o', 'c', 'a') && transform_length != 0) { + return OTS_FAILURE_MSG("The 'transformLength' of 'loca' table must be zero: %d", transform_length); } } // Disallow huge numbers (> 1GB) for sanity. @@ -1020,13 +1013,16 @@ bool ConvertWOFF2ToSFNT(ots::OpenTypeFile* file, if (total_size > 30 * 1024 * 1024) { return OTS_FAILURE(); } - const size_t total_size_size_t = static_cast<size_t>(total_size); - uncompressed_buf.resize(total_size_size_t); - const uint8_t* src_buf = data + compressed_offset; - if (!Woff2Uncompress(&uncompressed_buf[0], total_size_size_t, - src_buf, compressed_length)) { + size_t uncompressed_size = static_cast<size_t>(total_size); + uncompressed_buf.resize(uncompressed_size); + const uint8_t* compressed_buf = data + compressed_offset; + if (!BrotliDecompressBuffer(compressed_length, compressed_buf, + &uncompressed_size, &uncompressed_buf[0])) { return OTS_FAILURE_MSG("Failed to uncompress font data"); } + if (uncompressed_size != static_cast<size_t>(total_size)) { + return OTS_FAILURE_MSG("Decompressed font data size does not match the sum of 'origLength' and 'transformLength'"); + } transform_buf = &uncompressed_buf[0]; for (uint16_t i = 0; i < num_tables; ++i) { @@ -1047,7 +1043,7 @@ bool ConvertWOFF2ToSFNT(ots::OpenTypeFile* file, } else { if (!ReconstructTransformed(file, tables, table->tag, transform_buf, transform_length, result, result_length)) { - return OTS_FAILURE_MSG("Failed to reconstruct '%c%c%c%c' table", CHR(table->tag)); + return OTS_FAILURE_MSG("Failed to reconstruct '%c%c%c%c' table", UNTAG(table->tag)); } } diff --git a/third_party/ots/test/ot-sanitise.cc b/third_party/ots/test/ot-sanitise.cc index 2d4526a..3204ce4 100644 --- a/third_party/ots/test/ot-sanitise.cc +++ b/third_party/ots/test/ot-sanitise.cc @@ -49,18 +49,18 @@ class Context: public ots::OTSContext { } virtual ots::TableAction GetTableAction(uint32_t tag) { -#define TAG(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d)) switch (tag) { - case TAG('S','i','l','f'): - case TAG('S','i','l','l'): - case TAG('G','l','o','c'): - case TAG('G','l','a','t'): - case TAG('F','e','a','t'): + case OTS_TAG('S','i','l','f'): + case OTS_TAG('S','i','l','l'): + case OTS_TAG('G','l','o','c'): + case OTS_TAG('G','l','a','t'): + case OTS_TAG('F','e','a','t'): + case OTS_TAG('C','B','D','T'): + case OTS_TAG('C','B','L','C'): return ots::TABLE_ACTION_PASSTHRU; default: return ots::TABLE_ACTION_DEFAULT; } -#undef TAG } }; diff --git a/third_party/ots/test/side-by-side.cc b/third_party/ots/test/side-by-side.cc index 9034a7c..8565bfe 100644 --- a/third_party/ots/test/side-by-side.cc +++ b/third_party/ots/test/side-by-side.cc @@ -20,7 +20,7 @@ namespace { void DumpBitmap(const FT_Bitmap *bitmap) { - for (int i = 0; i < bitmap->rows * bitmap->width; ++i) { + for (unsigned int i = 0; i < bitmap->rows * bitmap->width; ++i) { if (bitmap->buffer[i] > 192) { std::fprintf(stderr, "#"); } else if (bitmap->buffer[i] > 128) { @@ -44,7 +44,7 @@ int CompareBitmaps(const FT_Bitmap *orig, const FT_Bitmap *trans) { if (orig->width == trans->width && orig->rows == trans->rows) { - for (int i = 0; i < orig->rows * orig->width; ++i) { + for (unsigned int i = 0; i < orig->rows * orig->width; ++i) { if (orig->buffer[i] != trans->buffer[i]) { std::fprintf(stderr, "bitmap data doesn't match!\n"); ret = 1; |