diff --git a/src/woff2_common.h b/src/woff2_common.h index c03d5d5..159300a 100644 --- a/src/woff2_common.h +++ b/src/woff2_common.h @@ -43,6 +43,10 @@ struct Table { uint32_t dst_offset; uint32_t dst_length; const uint8_t* dst_data; + + bool operator<(const Table& other) const { + return tag < other.tag; + } }; } // namespace woff2 diff --git a/src/woff2_dec.cc b/src/woff2_dec.cc index f95b570..e2bf6fb 100644 --- a/src/woff2_dec.cc +++ b/src/woff2_dec.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include "./buffer.h" @@ -824,8 +825,13 @@ bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length, offset = Store16(result, offset, output_search_range); offset = Store16(result, offset, max_pow2); offset = Store16(result, offset, (num_tables << 4) - output_search_range); + + // sort tags in the table directory in ascending alphabetical order + std::vector sorted_tables(tables); + std::sort(sorted_tables.begin(), sorted_tables.end()); + for (uint16_t i = 0; i < num_tables; ++i) { - const Table* table = &tables[i]; + const Table* table = &sorted_tables[i]; offset = StoreU32(result, offset, table->tag); offset = StoreU32(result, offset, 0); // checksum, to fill in later offset = StoreU32(result, offset, table->dst_offset); @@ -889,7 +895,7 @@ bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length, } } - return FixChecksums(tables, result); + return FixChecksums(sorted_tables, result); } } // namespace woff2