Merge from Chromium at DEPS revision db3f05efe0f9

This commit was generated by merge_to_master.py.

Change-Id: Ibdfd7c22c87e88c364f51632ce079d66e5f90d4e
diff --git a/brotli/dec/types.h b/brotli/dec/types.h
index bc09f8b..2f79b2a 100644
--- a/brotli/dec/types.h
+++ b/brotli/dec/types.h
@@ -22,10 +22,11 @@
 
 #ifndef _MSC_VER
 #include <inttypes.h>
-#ifdef __STRICT_ANSI__
-#define BROTLI_INLINE
-#else  /* __STRICT_ANSI__ */
+#if defined(__cplusplus) || !defined(__STRICT_ANSI__) \
+    || __STDC_VERSION__ >= 199901L
 #define BROTLI_INLINE inline
+#else
+#define BROTLI_INLINE
 #endif
 #else
 typedef signed   char int8_t;
diff --git a/woff2/buffer.h b/woff2/buffer.h
index 5111c65..3595e84 100644
--- a/woff2/buffer.h
+++ b/woff2/buffer.h
@@ -37,6 +37,7 @@
 #include <stdint.h>
 #endif
 
+#include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <limits>
@@ -49,8 +50,8 @@
 #define FONT_COMPRESSION_FAILURE() \
   util::compression::font::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
 inline bool Failure(const char *f, int l, const char *fn) {
-  std::fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn);
-  std::fflush(stderr);
+  fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn);
+  fflush(stderr);
   return false;
 }
 #endif
diff --git a/woff2/font.cc b/woff2/font.cc
index c0fb206..a7e5607 100644
--- a/woff2/font.cc
+++ b/woff2/font.cc
@@ -130,8 +130,17 @@
   if (head_table == NULL || loca_table == NULL || head_table->length < 52) {
     return 0;
   }
-  int index_fmt = head_table->data[51];
-  return (loca_table->length / (index_fmt == 0 ? 2 : 4)) - 1;
+  int index_fmt = IndexFormat(font);
+  int num_glyphs = (loca_table->length / (index_fmt == 0 ? 2 : 4)) - 1;
+  return num_glyphs;
+}
+
+int IndexFormat(const Font& font) {
+  const Font::Table* head_table = font.FindTable(kHeadTableTag);
+  if (head_table == NULL) {
+    return 0;
+  }
+  return head_table->data[51];
 }
 
 bool GetGlyphData(const Font& font, int glyph_index,
@@ -146,7 +155,9 @@
       head_table->length < 52) {
     return FONT_COMPRESSION_FAILURE();
   }
-  int index_fmt = head_table->data[51];
+
+  int index_fmt = IndexFormat(font);
+
   Buffer loca_buf(loca_table->data, loca_table->length);
   if (index_fmt == 0) {
     uint16_t offset1, offset2;
diff --git a/woff2/font.h b/woff2/font.h
index 01da720..08c414c 100644
--- a/woff2/font.h
+++ b/woff2/font.h
@@ -66,6 +66,9 @@
 // zero for CFF-flavored fonts.
 int NumGlyphs(const Font& font);
 
+// Returns the index format of the font
+int IndexFormat(const Font& font);
+
 // Sets *glyph_data and *glyph_size to point to the location of the glyph data
 // with the given index. Returns false if the glyph is not found.
 bool GetGlyphData(const Font& font, int glyph_index,
diff --git a/woff2/normalize.cc b/woff2/normalize.cc
index 0812b00..a816feb 100644
--- a/woff2/normalize.cc
+++ b/woff2/normalize.cc
@@ -63,29 +63,16 @@
 
 }  // namespace
 
-bool NormalizeGlyphs(Font* font) {
-  Font::Table* head_table = font->FindTable(kHeadTableTag);
+namespace {
+
+bool WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) {
   Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
   Font::Table* loca_table = font->FindTable(kLocaTableTag);
-  if (head_table == NULL || loca_table == NULL || glyf_table == NULL) {
-    return FONT_COMPRESSION_FAILURE();
-  }
-  int index_fmt = head_table->data[51];
-  int num_glyphs = NumGlyphs(*font);
 
-  // We need to allocate a bit more than its original length for the normalized
-  // glyf table, since it can happen that the glyphs in the original table are
-  // 2-byte aligned, while in the normalized table they are 4-byte aligned.
-  // That gives a maximum of 2 bytes increase per glyph. However, there is no
-  // theoretical guarantee that the total size of the flags plus the coordinates
-  // is the smallest possible in the normalized version, so we have to allow
-  // some general overhead.
-  // TODO(user) Figure out some more precise upper bound on the size of
-  // the overhead.
-  size_t max_normalized_glyf_size = 1.1 * glyf_table->length + 2 * num_glyphs;
+  int glyph_sz = index_fmt == 0 ? 2 : 4;
+  loca_table->buffer.resize(Round4(num_glyphs + 1) * glyph_sz);
+  loca_table->length = (num_glyphs + 1) * glyph_sz;
 
-  glyf_table->buffer.resize(max_normalized_glyf_size);
-  loca_table->buffer.resize(Round4(loca_table->length));
   uint8_t* glyf_dst = &glyf_table->buffer[0];
   uint8_t* loca_dst = &loca_table->buffer[0];
   uint32_t glyf_offset = 0;
@@ -113,6 +100,10 @@
     }
     glyf_offset += glyf_dst_size;
   }
+  if (glyf_offset == 0) {
+    return false;
+  }
+
   StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst);
 
   glyf_table->buffer.resize(glyf_offset);
@@ -123,6 +114,74 @@
   return true;
 }
 
+}  // namespace
+
+namespace {
+
+bool MakeEditableBuffer(Font* font, int tableTag) {
+  Font::Table* table = font->FindTable(tableTag);
+  if (table == NULL) {
+    return FONT_COMPRESSION_FAILURE();
+  }
+  int sz = Round4(table->length);
+  table->buffer.resize(sz);
+  uint8_t* buf = &table->buffer[0];
+  memcpy(buf, table->data, sz);
+  table->data = buf;
+  return true;
+}
+
+}  // namespace
+
+bool NormalizeGlyphs(Font* font) {
+  Font::Table* cff_table = font->FindTable(kCffTableTag);
+  Font::Table* head_table = font->FindTable(kHeadTableTag);
+  Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
+  Font::Table* loca_table = font->FindTable(kLocaTableTag);
+  if (head_table == NULL) {
+    return FONT_COMPRESSION_FAILURE();
+  }
+  // CFF, no loca, no glyf is OK for CFF. If so, don't normalize.
+  if (cff_table != NULL && loca_table == NULL && glyf_table == NULL) {
+    return true;
+  }
+  if (loca_table == NULL || glyf_table == NULL) {
+    return FONT_COMPRESSION_FAILURE();
+  }
+  int index_fmt = head_table->data[51];
+  int num_glyphs = NumGlyphs(*font);
+
+  // We need to allocate a bit more than its original length for the normalized
+  // glyf table, since it can happen that the glyphs in the original table are
+  // 2-byte aligned, while in the normalized table they are 4-byte aligned.
+  // That gives a maximum of 2 bytes increase per glyph. However, there is no
+  // theoretical guarantee that the total size of the flags plus the coordinates
+  // is the smallest possible in the normalized version, so we have to allow
+  // some general overhead.
+  // TODO(user) Figure out some more precise upper bound on the size of
+  // the overhead.
+  size_t max_normalized_glyf_size = 1.1 * glyf_table->length + 2 * num_glyphs;
+
+  glyf_table->buffer.resize(max_normalized_glyf_size);
+
+  // if we can't write a loca using short's (index_fmt 0)
+  // try again using longs (index_fmt 1)
+  if (!WriteNormalizedLoca(index_fmt, num_glyphs, font)) {
+    if (index_fmt != 0) {
+      return FONT_COMPRESSION_FAILURE();
+    }
+
+    // Rewrite loca with 4-byte entries & update head to match
+    index_fmt = 1;
+    if (!WriteNormalizedLoca(index_fmt, num_glyphs, font)) {
+      return FONT_COMPRESSION_FAILURE();
+    }
+    head_table->buffer[51] = 1;
+  }
+
+  return true;
+}
+
 bool NormalizeOffsets(Font* font) {
   uint32_t offset = 12 + 16 * font->num_tables;
   for (auto& i : font->tables) {
@@ -168,10 +227,7 @@
   if (head_table == NULL || head_table->length < 12) {
     return FONT_COMPRESSION_FAILURE();
   }
-  head_table->buffer.resize(Round4(head_table->length));
   uint8_t* head_buf = &head_table->buffer[0];
-  memcpy(head_buf, head_table->data, Round4(head_table->length));
-  head_table->data = head_buf;
   size_t offset = 8;
   StoreU32(0, &offset, head_buf);
   uint32_t file_checksum = 0;
@@ -187,7 +243,8 @@
 }
 
 bool NormalizeFont(Font* font) {
-  return (RemoveDigitalSignature(font) &&
+  return (MakeEditableBuffer(font, kHeadTableTag) &&
+          RemoveDigitalSignature(font) &&
           NormalizeGlyphs(font) &&
           NormalizeOffsets(font) &&
           FixChecksums(font));
diff --git a/woff2/round.h b/woff2/round.h
index cd6e5aa..8fd0f47 100644
--- a/woff2/round.h
+++ b/woff2/round.h
@@ -17,6 +17,8 @@
 #ifndef WOFF2_ROUND_H_
 #define WOFF2_ROUND_H_
 
+#include <limits.h>
+
 namespace woff2 {
 
 // Round a value up to the nearest multiple of 4. Don't round the value in the
diff --git a/woff2/table_tags.h b/woff2/table_tags.h
index f1b219c..c9b09bb 100644
--- a/woff2/table_tags.h
+++ b/woff2/table_tags.h
@@ -26,6 +26,7 @@
 static const uint32_t kHeadTableTag = 0x68656164;
 static const uint32_t kLocaTableTag = 0x6c6f6361;
 static const uint32_t kDsigTableTag = 0x44534947;
+static const uint32_t kCffTableTag = 0x43464620;
 
 extern const uint32_t kKnownTags[];
 
diff --git a/woff2/transform.cc b/woff2/transform.cc
index bb3a27a..44a4781 100644
--- a/woff2/transform.cc
+++ b/woff2/transform.cc
@@ -227,6 +227,12 @@
 }  // namespace
 
 bool TransformGlyfAndLocaTables(Font* font) {
+  // no transform for CFF
+  if (font->FindTable(kCffTableTag) != NULL
+      && font->FindTable(kGlyfTableTag) == NULL
+      && font->FindTable(kLocaTableTag) == NULL) {
+    return true;
+  }
   Font::Table* transformed_glyf = &font->tables[kGlyfTableTag ^ 0x80808080];
   Font::Table* transformed_loca = &font->tables[kLocaTableTag ^ 0x80808080];
 
diff --git a/woff2/woff2_enc.cc b/woff2/woff2_enc.cc
index b8afd92..6150935 100644
--- a/woff2/woff2_enc.cc
+++ b/woff2/woff2_enc.cc
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 //
-// Library for converting WOFF2 format font files to their TTF versions.
+// Library for converting TTF format font files to their WOFF2 versions.
 
 #include "./woff2_enc.h"
 
@@ -53,7 +53,7 @@
 void StoreBase128(size_t len, size_t* offset, uint8_t* dst) {
   size_t size = Base128Size(len);
   for (int i = 0; i < size; ++i) {
-    int b = (int)(len >> (7 * (size - i - 1))) & 0x7f;
+    int b = static_cast<int>((len >> (7 * (size - i - 1))) & 0x7f);
     if (i < size - 1) {
       b |= 0x80;
     }
@@ -96,6 +96,9 @@
 void StoreTableEntry(const Table& table, size_t* offset, uint8_t* dst) {
   uint8_t flag_byte = KnownTableIndex(table.tag);
   dst[(*offset)++] = flag_byte;
+  // The index here is treated as a set of flag bytes because
+  // bits 6 and 7 of the byte are reserved for future use as flags.
+  // 0x3f or 63 means an arbitrary table tag.
   if ((flag_byte & 0x3f) == 0x3f) {
     StoreU32(table.tag, offset, dst);
   }
@@ -106,7 +109,8 @@
 }
 
 size_t TableEntrySize(const Table& table) {
-  size_t size = KnownTableIndex(table.tag) < 31 ? 1 : 5;
+  uint8_t flag_byte = KnownTableIndex(table.tag);
+  size_t size = ((flag_byte & 0x3f) != 0x3f) ? 1 : 5;
   size += Base128Size(table.src_length);
   if ((table.flags & kWoff2FlagsTransform) != 0) {
      size += Base128Size(table.transform_length);
@@ -114,7 +118,8 @@
   return size;
 }
 
-size_t ComputeWoff2Length(const std::vector<Table>& tables) {
+size_t ComputeWoff2Length(const std::vector<Table>& tables,
+                          size_t extended_metadata_length) {
   size_t size = kWoff2HeaderSize;
   for (const auto& table : tables) {
     size += TableEntrySize(table);
@@ -123,6 +128,7 @@
     size += table.dst_length;
     size = Round4(size);
   }
+  size += extended_metadata_length;
   return size;
 }
 
@@ -150,15 +156,30 @@
 }  // namespace
 
 size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length) {
+  return MaxWOFF2CompressedSize(data, length, "");
+}
+
+size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length,
+    const string& extended_metadata) {
   // Except for the header size, which is 32 bytes larger in woff2 format,
   // all other parts should be smaller (table header in short format,
   // transformations and compression). Just to be sure, we will give some
   // headroom anyway.
-  return length + 1024;
+  return length + 1024 + extended_metadata.length();
+}
+
+uint32_t CompressedBufferSize(uint32_t original_size) {
+  return 1.2 * original_size + 10240;
 }
 
 bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
                        uint8_t *result, size_t *result_length) {
+  return ConvertTTFToWOFF2(data, length, result, result_length, "");
+}
+
+bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
+                       uint8_t *result, size_t *result_length,
+                       const string& extended_metadata) {
   Font font;
   if (!ReadFont(data, length, &font)) {
     fprintf(stderr, "Parsing of the input font failed.\n");
@@ -187,7 +208,7 @@
   // the size. If the compressor overflows this, it should return false and
   // then this function will also return false.
   size_t total_transform_length = ComputeTotalTransformLength(font);
-  size_t compression_buffer_size = 1.2 * total_transform_length + 10240;
+  size_t compression_buffer_size = CompressedBufferSize(total_transform_length);
   std::vector<uint8_t> compression_buf(compression_buffer_size);
   uint32_t total_compressed_length = compression_buffer_size;
 
@@ -209,6 +230,23 @@
     return false;
   }
 
+  // Compress the extended metadata
+  uint32_t compressed_metadata_buf_length =
+    CompressedBufferSize(extended_metadata.length());
+  std::vector<uint8_t> compressed_metadata_buf(compressed_metadata_buf_length);
+
+  if (extended_metadata.length() > 0) {
+    if (!Woff2Compress((const uint8_t*)extended_metadata.data(),
+                       extended_metadata.length(),
+                       compressed_metadata_buf.data(),
+                       &compressed_metadata_buf_length)) {
+      fprintf(stderr, "Compression of extended metadata failed.\n");
+      return false;
+    }
+  } else {
+    compressed_metadata_buf_length = 0;
+  }
+
   std::vector<Table> tables;
   for (const auto& i : font.tables) {
     const Font::Table& src_table = i.second;
@@ -241,7 +279,8 @@
     tables.push_back(table);
   }
 
-  size_t woff2_length = ComputeWoff2Length(tables);
+  size_t woff2_length =
+    ComputeWoff2Length(tables, compressed_metadata_buf_length);
   if (woff2_length > *result_length) {
     fprintf(stderr, "Result allocation was too small (%zd vs %zd bytes).\n",
            *result_length, woff2_length);
@@ -258,9 +297,16 @@
   StoreU32(ComputeTTFLength(tables), &offset, result);
   StoreU32(total_compressed_length, &offset, result);
   StoreBytes(head_table->data + 4, 4, &offset, result);  // font revision
-  StoreU32(0, &offset, result);  // metaOffset
-  StoreU32(0, &offset, result);  // metaLength
-  StoreU32(0, &offset, result);  // metaOrigLength
+  if (compressed_metadata_buf_length > 0) {
+    StoreU32(woff2_length - compressed_metadata_buf_length,
+             &offset, result);  // metaOffset
+    StoreU32(compressed_metadata_buf_length, &offset, result);  // metaLength
+    StoreU32(extended_metadata.length(), &offset, result);  // metaOrigLength
+  } else {
+    StoreU32(0, &offset, result);  // metaOffset
+    StoreU32(0, &offset, result);  // metaLength
+    StoreU32(0, &offset, result);  // metaOrigLength
+  }
   StoreU32(0, &offset, result);  // privOffset
   StoreU32(0, &offset, result);  // privLength
   for (const auto& table : tables) {
@@ -270,6 +316,9 @@
     StoreBytes(table.dst_data, table.dst_length, &offset, result);
     offset = Round4(offset);
   }
+  StoreBytes(compressed_metadata_buf.data(), compressed_metadata_buf_length,
+             &offset, result);
+
   if (*result_length != offset) {
     fprintf(stderr, "Mismatch between computed and actual length "
             "(%zd vs %zd)\n", *result_length, offset);
diff --git a/woff2/woff2_enc.h b/woff2/woff2_enc.h
index 18adc0c..d6eb4db 100644
--- a/woff2/woff2_enc.h
+++ b/woff2/woff2_enc.h
@@ -19,11 +19,17 @@
 
 #include <stddef.h>
 #include <inttypes.h>
+#include <string>
+
+using std::string;
+
 
 namespace woff2 {
 
 // Returns an upper bound on the size of the compressed file.
 size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length);
+size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length,
+                              const string& extended_metadata);
 
 // Compresses the font into the target buffer. *result_length should be at least
 // the value returned by MaxWOFF2CompressedSize(), upon return, it is set to the
@@ -31,6 +37,10 @@
 bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
                        uint8_t *result, size_t *result_length);
 
+bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
+                       uint8_t *result, size_t *result_length,
+                       const string& extended_metadata);
+
 } // namespace woff2
 
 #endif  // WOFF2_WOFF2_ENC_H_