Bug 1440658 - Update woff2 library to latest upstream code (currently at 1bccf208bca986e53a647dfe4811322adb06ecf8, release 1.0.2). r=fredw
authorJonathan Kew <jkew@mozilla.com>
Fri, 23 Feb 2018 14:32:23 +0000
changeset 405061 a35f1b78fd2ac5743dc8242f13b137a2d590b490
parent 405060 ff135d4dc797776720a4aa61436ba9b3e55d33de
child 405062 78db99b1350b06b24a4db39500a2307bfd31d486
push id33502
push userarchaeopteryx@coole-files.de
push dateSat, 24 Feb 2018 00:59:26 +0000
treeherdermozilla-central@1056e048072c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfredw
bugs1440658
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1440658 - Update woff2 library to latest upstream code (currently at 1bccf208bca986e53a647dfe4811322adb06ecf8, release 1.0.2). r=fredw
gfx/ots/src/ots.cc
modules/woff2/README.mozilla
modules/woff2/include/woff2/decode.h
modules/woff2/include/woff2/encode.h
modules/woff2/include/woff2/output.h
modules/woff2/moz.build
modules/woff2/redefine-unique_ptr.patch
modules/woff2/src/buffer.h
modules/woff2/src/convert_woff2ttf_fuzzer.cc
modules/woff2/src/convert_woff2ttf_fuzzer_new_entry.cc
modules/woff2/src/file.h
modules/woff2/src/font.cc
modules/woff2/src/font.h
modules/woff2/src/glyph.cc
modules/woff2/src/glyph.h
modules/woff2/src/normalize.cc
modules/woff2/src/normalize.h
modules/woff2/src/port.h
modules/woff2/src/round.h
modules/woff2/src/store_bytes.h
modules/woff2/src/table_tags.cc
modules/woff2/src/table_tags.h
modules/woff2/src/transform.cc
modules/woff2/src/transform.h
modules/woff2/src/variable_length.cc
modules/woff2/src/variable_length.h
modules/woff2/src/woff2_common.cc
modules/woff2/src/woff2_common.h
modules/woff2/src/woff2_compress.cc
modules/woff2/src/woff2_dec.cc
modules/woff2/src/woff2_dec.h
modules/woff2/src/woff2_decompress.cc
modules/woff2/src/woff2_enc.cc
modules/woff2/src/woff2_enc.h
modules/woff2/src/woff2_info.cc
modules/woff2/src/woff2_out.cc
modules/woff2/src/woff2_out.h
modules/woff2/update.sh
--- a/gfx/ots/src/ots.cc
+++ b/gfx/ots/src/ots.cc
@@ -9,17 +9,17 @@
 
 #include <algorithm>
 #include <cstdlib>
 #include <cstring>
 #include <limits>
 #include <map>
 #include <vector>
 
-#include "woff2_dec.h"
+#include "woff2/decode.h"
 
 // The OpenType Font File
 // http://www.microsoft.com/typography/otspec/cmap.htm
 
 #include "cff.h"
 #include "cmap.h"
 #include "cvt.h"
 #include "fpgm.h"
--- a/modules/woff2/README.mozilla
+++ b/modules/woff2/README.mozilla
@@ -1,18 +1,14 @@
 This is the woff2 library from
 https://github.com/google/woff2.
 
 Upstream code can be viewed at
-  https://github.com/google/woff2/tree/master/src
+  https://github.com/google/woff2/tree/master
 
 and cloned by
   git clone https://github.com/google/woff2
 
 The in-tree copy is updated by running
   sh update.sh
 from within the modules/woff2 directory.
 
-Current version: [commit e580ebc30a54becf69a75f6e6d6008536ae0c0b4].
-
-redefine-unique_ptr.patch redefines the class std::unique_ptr to workaround a
-build issue with missing C++11 features.
-See https://bugzilla.mozilla.org/show_bug.cgi?id=1227058
+Current version: [commit 1bccf208bca986e53a647dfe4811322adb06ecf8].
new file mode 100644
--- /dev/null
+++ b/modules/woff2/include/woff2/decode.h
@@ -0,0 +1,36 @@
+/* Copyright 2014 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Library for converting WOFF2 format font files to their TTF versions. */
+
+#ifndef WOFF2_WOFF2_DEC_H_
+#define WOFF2_WOFF2_DEC_H_
+
+#include <stddef.h>
+#include <inttypes.h>
+#include <woff2/output.h>
+
+namespace woff2 {
+
+// Compute the size of the final uncompressed font, or 0 on error.
+size_t ComputeWOFF2FinalSize(const uint8_t *data, size_t length);
+
+// Decompresses the font into the target buffer. The result_length should
+// be the same as determined by ComputeFinalSize(). Returns true on successful
+// decompression.
+// DEPRECATED; please prefer the version that takes a WOFF2Out*
+bool ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
+                       const uint8_t *data, size_t length);
+
+// Decompresses the font into out. Returns true on success.
+// Works even if WOFF2Header totalSfntSize is wrong.
+// Please prefer this API.
+bool ConvertWOFF2ToTTF(const uint8_t *data, size_t length,
+                       WOFF2Out* out);
+
+} // namespace woff2
+
+#endif  // WOFF2_WOFF2_DEC_H_
new file mode 100644
--- /dev/null
+++ b/modules/woff2/include/woff2/encode.h
@@ -0,0 +1,43 @@
+/* Copyright 2014 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Library for converting WOFF2 format font files to their TTF versions. */
+
+#ifndef WOFF2_WOFF2_ENC_H_
+#define WOFF2_WOFF2_ENC_H_
+
+#include <stddef.h>
+#include <inttypes.h>
+#include <string>
+
+namespace woff2 {
+
+struct WOFF2Params {
+  WOFF2Params() : extended_metadata(""), brotli_quality(11),
+                  allow_transforms(true) {}
+
+  std::string extended_metadata;
+  int brotli_quality;
+  bool allow_transforms;
+};
+
+// 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 std::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
+// actual compressed size. Returns true on successful compression.
+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 WOFF2Params& params);
+
+} // namespace woff2
+
+#endif  // WOFF2_WOFF2_ENC_H_
new file mode 100644
--- /dev/null
+++ b/modules/woff2/include/woff2/output.h
@@ -0,0 +1,86 @@
+/* Copyright 2016 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Output buffer for WOFF2 decompression. */
+
+#ifndef WOFF2_WOFF2_OUT_H_
+#define WOFF2_WOFF2_OUT_H_
+
+#include <algorithm>
+#include <cstring>
+#include <memory>
+#include <string>
+
+namespace woff2 {
+
+// Suggested max size for output.
+const size_t kDefaultMaxSize = 30 * 1024 * 1024;
+
+/**
+ * Output interface for the woff2 decoding.
+ *
+ * Writes to arbitrary offsets are supported to facilitate updating offset
+ * table and checksums after tables are ready. Reading the current size is
+ * supported so a 'loca' table can be built up while writing glyphs.
+ *
+ * By default limits size to kDefaultMaxSize.
+ */
+class WOFF2Out {
+ public:
+  virtual ~WOFF2Out(void) {}
+
+  // Append n bytes of data from buf.
+  // Return true if all written, false otherwise.
+  virtual bool Write(const void *buf, size_t n) = 0;
+
+  // Write n bytes of data from buf at offset.
+  // Return true if all written, false otherwise.
+  virtual bool Write(const void *buf, size_t offset, size_t n) = 0;
+
+  virtual size_t Size() = 0;
+};
+
+/**
+ * Expanding memory block for woff2 out. By default limited to kDefaultMaxSize.
+ */
+class WOFF2StringOut : public WOFF2Out {
+ public:
+  // Create a writer that writes its data to buf.
+  // buf->size() will grow to at most max_size
+  // buf may be sized (e.g. using EstimateWOFF2FinalSize) or empty.
+  explicit WOFF2StringOut(std::string* buf);
+
+  bool Write(const void *buf, size_t n) override;
+  bool Write(const void *buf, size_t offset, size_t n) override;
+  size_t Size() override { return offset_; }
+  size_t MaxSize() { return max_size_; }
+  void SetMaxSize(size_t max_size);
+ private:
+  std::string* buf_;
+  size_t max_size_;
+  size_t offset_;
+};
+
+/**
+ * Fixed memory block for woff2 out.
+ */
+class WOFF2MemoryOut : public WOFF2Out {
+ public:
+  // Create a writer that writes its data to buf.
+  WOFF2MemoryOut(uint8_t* buf, size_t buf_size);
+
+  bool Write(const void *buf, size_t n) override;
+  bool Write(const void *buf, size_t offset, size_t n) override;
+  size_t Size() override { return offset_; }
+ private:
+  uint8_t* buf_;
+  size_t buf_size_;
+  size_t offset_;
+};
+
+} // namespace woff2
+
+#endif  // WOFF2_WOFF2_OUT_H_
--- a/modules/woff2/moz.build
+++ b/modules/woff2/moz.build
@@ -10,12 +10,18 @@ with Files('**'):
 UNIFIED_SOURCES += [
     'src/table_tags.cc',
     'src/variable_length.cc',
     'src/woff2_common.cc',
     'src/woff2_dec.cc',
     'src/woff2_out.cc',
 ]
 
+EXPORTS.woff2 += [
+    'include/woff2/decode.h',
+    'include/woff2/encode.h',
+    'include/woff2/output.h',
+]
+
 # We allow warnings for third-party code that can be updated from upstream.
 AllowCompilerWarnings()
 
 Library('woff2')
deleted file mode 100644
--- a/modules/woff2/redefine-unique_ptr.patch
+++ /dev/null
@@ -1,29 +0,0 @@
-diff --git a/modules/woff2/src/woff2_dec.cc b/modules/woff2/src/woff2_dec.cc
---- a/modules/woff2/src/woff2_dec.cc
-+++ b/modules/woff2/src/woff2_dec.cc
-@@ -22,16 +22,25 @@
- #include <cstring>
- #include <limits>
- #include <string>
- #include <vector>
- #include <map>
- #include <memory>
- #include <utility>
- 
-+#include "mozilla/UniquePtr.h"
-+namespace std
-+{
-+  using mozilla::DefaultDelete;
-+  using mozilla::UniquePtr;
-+  #define default_delete DefaultDelete
-+  #define unique_ptr UniquePtr
-+}
-+
- #include "./brotli/decode.h"
- #include "./buffer.h"
- #include "./port.h"
- #include "./round.h"
- #include "./store_bytes.h"
- #include "./table_tags.h"
- #include "./variable_length.h"
- #include "./woff2_common.h"
--- a/modules/woff2/src/buffer.h
+++ b/modules/woff2/src/buffer.h
@@ -1,24 +1,16 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// The parts of ots.h & opentype-sanitiser.h that we need, taken from the
-// https://code.google.com/p/ots/ project.
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* The parts of ots.h & opentype-sanitiser.h that we need, taken from the
+   https://code.google.com/p/ots/ project. */
 
 #ifndef WOFF2_BUFFER_H_
 #define WOFF2_BUFFER_H_
 
 #if defined(_WIN32)
 #include <stdlib.h>
 typedef signed char int8_t;
 typedef unsigned char uint8_t;
--- a/modules/woff2/src/convert_woff2ttf_fuzzer.cc
+++ b/modules/woff2/src/convert_woff2ttf_fuzzer.cc
@@ -1,12 +1,12 @@
 #include <stddef.h>
 #include <stdint.h>
 
-#include "woff2_dec.h"
+#include <woff2/decode.h>
 
 // Entry point for LibFuzzer.
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
   std::string buf;
   woff2::WOFF2StringOut out(&buf);
   out.SetMaxSize(30 * 1024 * 1024);
   woff2::ConvertWOFF2ToTTF(data, size, &out);
   return 0;
--- a/modules/woff2/src/convert_woff2ttf_fuzzer_new_entry.cc
+++ b/modules/woff2/src/convert_woff2ttf_fuzzer_new_entry.cc
@@ -1,10 +1,10 @@
 #include <string>
-#include "woff2_dec.h"
+#include <woff2/decode.h>
 
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t data_size) {
   // Decode using newer entry pattern.
   // Same pattern as woff2_decompress.
   std::string output(std::min(woff2::ComputeWOFF2FinalSize(data, data_size),
                               woff2::kDefaultMaxSize), 0);
   woff2::WOFF2StringOut out(&output);
   woff2::ConvertWOFF2ToTTF(data, data_size, &out);
--- a/modules/woff2/src/file.h
+++ b/modules/woff2/src/file.h
@@ -1,23 +1,15 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// File IO helpers.
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* File IO helpers. */
 
 #ifndef WOFF2_FILE_H_
 #define WOFF2_FILE_H_
 
 #include <fstream>
 #include <iterator>
 
 namespace woff2 {
--- a/modules/woff2/src/font.cc
+++ b/modules/woff2/src/font.cc
@@ -1,23 +1,15 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Font management utilities
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Font management utilities */
 
 #include "./font.h"
 
 #include <algorithm>
 
 #include "./buffer.h"
 #include "./port.h"
 #include "./store_bytes.h"
@@ -126,16 +118,19 @@ bool ReadCollectionFont(Buffer* file, co
 
   for (auto& entry : font->tables) {
     Font::Table& table = entry.second;
 
     if (all_tables->find(table.offset) == all_tables->end()) {
       (*all_tables)[table.offset] = font->FindTable(table.tag);
     } else {
       table.reuse_of = (*all_tables)[table.offset];
+      if (table.tag != table.reuse_of->tag) {
+        return FONT_COMPRESSION_FAILURE();
+      }
     }
 
   }
   return true;
 }
 
 bool ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,
                             FontCollection* font_collection) {
--- a/modules/woff2/src/font.h
+++ b/modules/woff2/src/font.h
@@ -1,24 +1,16 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Data model for a font file in sfnt format, reading and writing functions and
-// accessors for the glyph data.
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Data model for a font file in sfnt format, reading and writing functions and
+   accessors for the glyph data. */
 
 #ifndef WOFF2_FONT_H_
 #define WOFF2_FONT_H_
 
 #include <stddef.h>
 #include <inttypes.h>
 #include <map>
 #include <vector>
--- a/modules/woff2/src/glyph.cc
+++ b/modules/woff2/src/glyph.cc
@@ -1,23 +1,15 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Glyph manipulation
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Glyph manipulation */
 
 #include "./glyph.h"
 
 #include <stdlib.h>
 #include <limits>
 #include "./buffer.h"
 #include "./store_bytes.h"
 
--- a/modules/woff2/src/glyph.h
+++ b/modules/woff2/src/glyph.h
@@ -1,24 +1,16 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Data model and I/O for glyph data within sfnt format files for the purpose of
-// performing the preprocessing step of the WOFF 2.0 conversion.
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Data model and I/O for glyph data within sfnt format files for the purpose of
+   performing the preprocessing step of the WOFF 2.0 conversion. */
 
 #ifndef WOFF2_GLYPH_H_
 #define WOFF2_GLYPH_H_
 
 #include <stddef.h>
 #include <inttypes.h>
 #include <vector>
 
--- a/modules/woff2/src/normalize.cc
+++ b/modules/woff2/src/normalize.cc
@@ -1,23 +1,15 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Glyph normalization
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Glyph normalization */
 
 #include "./normalize.h"
 
 #include <inttypes.h>
 #include <stddef.h>
 
 #include "./buffer.h"
 #include "./port.h"
@@ -99,17 +91,20 @@ bool MakeEditableBuffer(Font* font, int 
     return FONT_COMPRESSION_FAILURE();
   }
   if (table->IsReused()) {
     return true;
   }
   int sz = Round4(table->length);
   table->buffer.resize(sz);
   uint8_t* buf = &table->buffer[0];
-  memcpy(buf, table->data, sz);
+  memcpy(buf, table->data, table->length);
+  if (PREDICT_FALSE(sz > table->length)) {
+    memset(buf + table->length, 0, sz - table->length);
+  }
   table->data = buf;
   return true;
 }
 
 }  // namespace
 
 bool NormalizeGlyphs(Font* font) {
   Font::Table* head_table = font->FindTable(kHeadTableTag);
--- a/modules/woff2/src/normalize.h
+++ b/modules/woff2/src/normalize.h
@@ -1,25 +1,17 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Functions for normalizing fonts. Since the WOFF 2.0 decoder creates font
-// files in normalized form, the WOFF 2.0 conversion is guaranteed to be
-// lossless (in a bitwise sense) only for normalized font files.
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Functions for normalizing fonts. Since the WOFF 2.0 decoder creates font
+   files in normalized form, the WOFF 2.0 conversion is guaranteed to be
+   lossless (in a bitwise sense) only for normalized font files. */
 
 #ifndef WOFF2_NORMALIZE_H_
 #define WOFF2_NORMALIZE_H_
 
 namespace woff2 {
 
 struct Font;
 struct FontCollection;
--- a/modules/woff2/src/port.h
+++ b/modules/woff2/src/port.h
@@ -1,23 +1,15 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Helper function for bit twiddling and macros for branch prediction.
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Helper function for bit twiddling and macros for branch prediction. */
 
 #ifndef WOFF2_PORT_H_
 #define WOFF2_PORT_H_
 
 #include <assert.h>
 
 namespace woff2 {
 
--- a/modules/woff2/src/round.h
+++ b/modules/woff2/src/round.h
@@ -1,23 +1,15 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Helper for rounding
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Helper for rounding */
 
 #ifndef WOFF2_ROUND_H_
 #define WOFF2_ROUND_H_
 
 #include <limits>
 
 namespace woff2 {
 
--- a/modules/woff2/src/store_bytes.h
+++ b/modules/woff2/src/store_bytes.h
@@ -1,24 +1,16 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Helper functions for storing integer values into byte streams.
-// No bounds checking is performed, that is the responsibility of the caller.
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Helper functions for storing integer values into byte streams.
+   No bounds checking is performed, that is the responsibility of the caller. */
 
 #ifndef WOFF2_STORE_BYTES_H_
 #define WOFF2_STORE_BYTES_H_
 
 #include <inttypes.h>
 #include <stddef.h>
 #include <string.h>
 
--- a/modules/woff2/src/table_tags.cc
+++ b/modules/woff2/src/table_tags.cc
@@ -1,23 +1,15 @@
-// Copyright 2014 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Font table tags
+/* Copyright 2014 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Font table tags */
 
 #include "./table_tags.h"
 
 namespace woff2 {
 
 // 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)
 
--- a/modules/woff2/src/table_tags.h
+++ b/modules/woff2/src/table_tags.h
@@ -1,23 +1,15 @@
-// Copyright 2014 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Font table tags
+/* Copyright 2014 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Font table tags */
 
 #ifndef WOFF2_TABLE_TAGS_H_
 #define WOFF2_TABLE_TAGS_H_
 
 #include <inttypes.h>
 
 namespace woff2 {
 
--- a/modules/woff2/src/transform.cc
+++ b/modules/woff2/src/transform.cc
@@ -1,23 +1,15 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Library for preprocessing fonts as part of the WOFF 2.0 conversion.
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Library for preprocessing fonts as part of the WOFF 2.0 conversion. */
 
 #include "./transform.h"
 
 #include <complex>  // for std::abs
 
 #include "./buffer.h"
 #include "./font.h"
 #include "./glyph.h"
--- a/modules/woff2/src/transform.h
+++ b/modules/woff2/src/transform.h
@@ -1,23 +1,15 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Library for preprocessing fonts as part of the WOFF 2.0 conversion.
+/* Copyright 2014 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Library for preprocessing fonts as part of the WOFF 2.0 conversion. */
 
 #ifndef WOFF2_TRANSFORM_H_
 #define WOFF2_TRANSFORM_H_
 
 #include "./font.h"
 
 namespace woff2 {
 
--- a/modules/woff2/src/variable_length.cc
+++ b/modules/woff2/src/variable_length.cc
@@ -1,23 +1,15 @@
-// Copyright 2015 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Helper functions for woff2 variable length types: 255UInt16 and UIntBase128
+/* Copyright 2015 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Helper functions for woff2 variable length types: 255UInt16 and UIntBase128 */
 
 #include "./variable_length.h"
 
 namespace woff2 {
 
 size_t Size255UShort(uint16_t value) {
   size_t result = 3;
   if (value < 253) {
--- a/modules/woff2/src/variable_length.h
+++ b/modules/woff2/src/variable_length.h
@@ -1,23 +1,15 @@
-// Copyright 2015 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Helper functions for woff2 variable length types: 255UInt16 and UIntBase128
+/* Copyright 2015 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Helper functions for woff2 variable length types: 255UInt16 and UIntBase128 */
 
 #ifndef WOFF2_VARIABLE_LENGTH_H_
 #define WOFF2_VARIABLE_LENGTH_H_
 
 #include <inttypes.h>
 #include <vector>
 #include "./buffer.h"
 
--- a/modules/woff2/src/woff2_common.cc
+++ b/modules/woff2/src/woff2_common.cc
@@ -1,23 +1,15 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Helpers common across multiple parts of woff2
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Helpers common across multiple parts of woff2 */
 
 #include <algorithm>
 
 #include "./woff2_common.h"
 
 #include "./port.h"
 
 namespace woff2 {
--- a/modules/woff2/src/woff2_common.h
+++ b/modules/woff2/src/woff2_common.h
@@ -1,23 +1,15 @@
-// Copyright 2014 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Common definition for WOFF2 encoding/decoding
+/* Copyright 2014 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Common definition for WOFF2 encoding/decoding */
 
 #ifndef WOFF2_WOFF2_COMMON_H_
 #define WOFF2_WOFF2_COMMON_H_
 
 #include <stddef.h>
 #include <inttypes.h>
 
 #include <string>
--- a/modules/woff2/src/woff2_compress.cc
+++ b/modules/woff2/src/woff2_compress.cc
@@ -1,28 +1,20 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// A commandline tool for compressing ttf format files to woff2.
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* A commandline tool for compressing ttf format files to woff2. */
 
 #include <string>
 
 #include "file.h"
-#include "./woff2_enc.h"
+#include <woff2/encode.h>
 
 
 int main(int argc, char **argv) {
   using std::string;
 
   if (argc != 2) {
     fprintf(stderr, "One argument, the input filename, must be provided.\n");
     return 1;
--- a/modules/woff2/src/woff2_dec.cc
+++ b/modules/woff2/src/woff2_dec.cc
@@ -1,47 +1,30 @@
-// Copyright 2014 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// 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.
+/* Copyright 2014 Google Inc. All Rights Reserved.
 
-#include "./woff2_dec.h"
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Library for converting WOFF2 format font files to their TTF versions. */
+
+#include <woff2/decode.h>
 
 #include <stdlib.h>
 #include <algorithm>
 #include <complex>
 #include <cstring>
 #include <limits>
 #include <string>
 #include <vector>
 #include <map>
 #include <memory>
 #include <utility>
 
-#include "mozilla/UniquePtr.h"
-namespace std
-{
-  using mozilla::DefaultDelete;
-  using mozilla::UniquePtr;
-  #define default_delete DefaultDelete
-  #define unique_ptr UniquePtr
-}
-
-#include "./brotli/decode.h"
+#include <brotli/decode.h>
 #include "./buffer.h"
 #include "./port.h"
 #include "./round.h"
 #include "./store_bytes.h"
 #include "./table_tags.h"
 #include "./variable_length.h"
 #include "./woff2_common.h"
 
@@ -415,16 +398,24 @@ bool ReconstructGlyf(const uint8_t* data
   if (PREDICT_FALSE(!file.ReadU32(&version))) {
     return FONT_COMPRESSION_FAILURE();
   }
   if (PREDICT_FALSE(!file.ReadU16(&info->num_glyphs) ||
       !file.ReadU16(&info->index_format))) {
     return FONT_COMPRESSION_FAILURE();
   }
 
+  // https://dev.w3.org/webfonts/WOFF2/spec/#conform-mustRejectLoca
+  // dst_length here is origLength in the spec
+  uint32_t expected_loca_dst_length = (info->index_format ? 4 : 2)
+    * (static_cast<uint32_t>(info->num_glyphs) + 1);
+  if (PREDICT_FALSE(loca_table->dst_length != expected_loca_dst_length)) {
+    return FONT_COMPRESSION_FAILURE();
+  }
+
   unsigned int offset = (2 + kNumSubStreams) * 4;
   if (PREDICT_FALSE(offset > glyf_table->transform_length)) {
     return FONT_COMPRESSION_FAILURE();
   }
   // Invariant from here on: data_size >= offset
   for (int i = 0; i < kNumSubStreams; ++i) {
     uint32_t substream_size;
     if (PREDICT_FALSE(!file.ReadU32(&substream_size))) {
@@ -596,16 +587,24 @@ bool ReconstructGlyf(const uint8_t* data
         return FONT_COMPRESSION_FAILURE();
       }
       glyph_size += instruction_size;
 
       if (PREDICT_FALSE(!StorePoints(total_n_points, points.get(), n_contours,
             instruction_size, glyph_buf.get(), glyph_buf_size, &glyph_size))) {
         return FONT_COMPRESSION_FAILURE();
       }
+    } else {
+      // n_contours == 0; empty glyph. Must NOT have a bbox.
+      if (PREDICT_FALSE(have_bbox)) {
+#ifdef FONT_COMPRESSION_BIN
+        fprintf(stderr, "Empty glyph has a bbox\n");
+#endif
+        return FONT_COMPRESSION_FAILURE();
+      }
     }
 
     loca_values[i] = out->Size() - glyf_start;
     if (PREDICT_FALSE(!out->Write(glyph_buf.get(), glyph_size))) {
       return FONT_COMPRESSION_FAILURE();
     }
 
     // TODO(user) Old code aligned glyphs ... but do we actually need to?
@@ -673,16 +672,24 @@ bool ReconstructTransformedHmtx(const ui
     return FONT_COMPRESSION_FAILURE();
   }
 
   std::vector<uint16_t> advance_widths;
   std::vector<int16_t> lsbs;
   bool has_proportional_lsbs = (hmtx_flags & 1) == 0;
   bool has_monospace_lsbs = (hmtx_flags & 2) == 0;
 
+  // Bits 2-7 are reserved and MUST be zero.
+  if ((hmtx_flags & 0xFC) != 0) {
+#ifdef FONT_COMPRESSION_BIN
+    fprintf(stderr, "Illegal hmtx flags; bits 2-7 must be 0\n");
+#endif
+    return FONT_COMPRESSION_FAILURE();
+  }
+
   // you say you transformed but there is little evidence of it
   if (has_proportional_lsbs && has_monospace_lsbs) {
     return FONT_COMPRESSION_FAILURE();
   }
 
   assert(x_mins.size() == num_glyphs);
 
   // num_glyphs 0 is OK if there is no 'glyf' but cannot then xform 'hmtx'.
@@ -880,21 +887,36 @@ bool ReconstructFont(uint8_t* transforme
                      size_t font_index,
                      WOFF2Out* out) {
   size_t dest_offset = out->Size();
   uint8_t table_entry[12];
   WOFF2FontInfo* info = &metadata->font_infos[font_index];
   std::vector<Table*> tables = Tables(hdr, font_index);
 
   // 'glyf' without 'loca' doesn't make sense
-  if (PREDICT_FALSE(static_cast<bool>(FindTable(&tables, kGlyfTableTag)) !=
-                    static_cast<bool>(FindTable(&tables, kLocaTableTag)))) {
+  const Table* glyf_table = FindTable(&tables, kGlyfTableTag);
+  const Table* loca_table = FindTable(&tables, kLocaTableTag);
+  if (PREDICT_FALSE(static_cast<bool>(glyf_table) !=
+                    static_cast<bool>(loca_table))) {
+#ifdef FONT_COMPRESSION_BIN
+      fprintf(stderr, "Cannot have just one of glyf/loca\n");
+#endif
     return FONT_COMPRESSION_FAILURE();
   }
 
+  if (glyf_table != NULL) {
+    if (PREDICT_FALSE((glyf_table->flags & kWoff2FlagsTransform)
+                      != (loca_table->flags & kWoff2FlagsTransform))) {
+#ifdef FONT_COMPRESSION_BIN
+      fprintf(stderr, "Cannot transform just one of glyf/loca\n");
+#endif
+      return FONT_COMPRESSION_FAILURE();
+    }
+  }
+
   uint32_t font_checksum = metadata->header_checksum;
   if (hdr->header_version) {
     font_checksum = hdr->ttc_fonts[font_index].header_checksum;
   }
 
   uint32_t loca_checksum = 0;
   for (size_t i = 0; i < tables.size(); i++) {
     Table& table = *tables[i];
@@ -903,17 +925,17 @@ bool ReconstructFont(uint8_t* transforme
     bool reused = metadata->checksums.find(checksum_key)
                != metadata->checksums.end();
     if (PREDICT_FALSE(font_index == 0 && reused)) {
       return FONT_COMPRESSION_FAILURE();
     }
 
     // TODO(user) a collection with optimized hmtx that reused glyf/loca
     // would fail. We don't optimize hmtx for collections yet.
-    if (PREDICT_FALSE(static_cast<uint64_t>(table.src_offset + table.src_length)
+    if (PREDICT_FALSE(static_cast<uint64_t>(table.src_offset) + table.src_length
         > transformed_buf_size)) {
       return FONT_COMPRESSION_FAILURE();
     }
 
     if (table.tag == kHheaTableTag) {
       if (!ReadNumHMetrics(transformed_buf + table.src_offset,
           table.src_length, &info->num_hmetrics)) {
         return FONT_COMPRESSION_FAILURE();
@@ -1104,42 +1126,47 @@ bool ReadWOFF2Header(const uint8_t* data
         return FONT_COMPRESSION_FAILURE();
       }
       if (PREDICT_FALSE(!file.ReadU32(&ttc_font.flavor))) {
         return FONT_COMPRESSION_FAILURE();
       }
 
       ttc_font.table_indices.resize(num_tables);
 
-      const Table* glyf_table = NULL;
-      const Table* loca_table = NULL;
+
+      unsigned int glyf_idx = 0;
+      unsigned int loca_idx = 0;
 
       for (uint32_t j = 0; j < num_tables; j++) {
         unsigned int table_idx;
         if (PREDICT_FALSE(!Read255UShort(&file, &table_idx)) ||
             table_idx >= hdr->tables.size()) {
           return FONT_COMPRESSION_FAILURE();
         }
         ttc_font.table_indices[j] = table_idx;
 
         const Table& table = hdr->tables[table_idx];
         if (table.tag == kLocaTableTag) {
-          loca_table = &table;
+          loca_idx = table_idx;
         }
         if (table.tag == kGlyfTableTag) {
-          glyf_table = &table;
+          glyf_idx = table_idx;
         }
 
       }
 
-      if (PREDICT_FALSE((glyf_table == NULL) != (loca_table == NULL))) {
+      // if we have both glyf and loca make sure they are consecutive
+      // if we have just one we'll reject the font elsewhere
+      if (glyf_idx > 0 || loca_idx > 0) {
+        if (PREDICT_FALSE(glyf_idx > loca_idx || loca_idx - glyf_idx != 1)) {
 #ifdef FONT_COMPRESSION_BIN
-        fprintf(stderr, "Cannot have just one of glyf/loca\n");
+        fprintf(stderr, "TTC font %d has non-consecutive glyf/loca\n", i);
 #endif
-        return FONT_COMPRESSION_FAILURE();
+          return FONT_COMPRESSION_FAILURE();
+        }
       }
     }
   }
 
   const uint64_t first_table_offset = ComputeOffsetToFirstTable(*hdr);
 
   hdr->compressed_offset = file.offset();
   if (PREDICT_FALSE(hdr->compressed_offset >
deleted file mode 100644
--- a/modules/woff2/src/woff2_dec.h
+++ /dev/null
@@ -1,44 +0,0 @@
-// Copyright 2014 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// 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.
-
-#ifndef WOFF2_WOFF2_DEC_H_
-#define WOFF2_WOFF2_DEC_H_
-
-#include <stddef.h>
-#include <inttypes.h>
-#include "./woff2_out.h"
-
-namespace woff2 {
-
-// Compute the size of the final uncompressed font, or 0 on error.
-size_t ComputeWOFF2FinalSize(const uint8_t *data, size_t length);
-
-// Decompresses the font into the target buffer. The result_length should
-// be the same as determined by ComputeFinalSize(). Returns true on successful
-// decompression.
-// DEPRECATED; please prefer the version that takes a WOFF2Out*
-bool ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
-                       const uint8_t *data, size_t length);
-
-// Decompresses the font into out. Returns true on success.
-// Works even if WOFF2Header totalSfntSize is wrong.
-// Please prefer this API.
-bool ConvertWOFF2ToTTF(const uint8_t *data, size_t length,
-                       WOFF2Out* out);
-
-} // namespace woff2
-
-#endif  // WOFF2_WOFF2_DEC_H_
--- a/modules/woff2/src/woff2_decompress.cc
+++ b/modules/woff2/src/woff2_decompress.cc
@@ -1,42 +1,35 @@
-// Copyright 2013 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// A very simple commandline tool for decompressing woff2 format files to true
-// type font files.
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* A very simple commandline tool for decompressing woff2 format files to true
+   type font files. */
 
 #include <string>
 
 #include "./file.h"
-#include "./woff2_dec.h"
+#include <woff2/decode.h>
 
 
 int main(int argc, char **argv) {
   using std::string;
 
   if (argc != 2) {
     fprintf(stderr, "One argument, the input filename, must be provided.\n");
     return 1;
   }
 
   string filename(argv[1]);
   string outfilename = filename.substr(0, filename.find_last_of(".")) + ".ttf";
 
+  // Note: update woff2_dec_fuzzer_new_entry.cc if this pattern changes.
   string input = woff2::GetFileContent(filename);
   const uint8_t* raw_input = reinterpret_cast<const uint8_t*>(input.data());
   string output(std::min(woff2::ComputeWOFF2FinalSize(raw_input, input.size()),
                          woff2::kDefaultMaxSize), 0);
   woff2::WOFF2StringOut out(&output);
 
   const bool ok = woff2::ConvertWOFF2ToTTF(raw_input, input.size(), &out);
 
--- a/modules/woff2/src/woff2_enc.cc
+++ b/modules/woff2/src/woff2_enc.cc
@@ -1,34 +1,26 @@
-// Copyright 2014 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Library for converting TTF format font files to their WOFF2 versions.
+/* Copyright 2014 Google Inc. All Rights Reserved.
 
-#include "./woff2_enc.h"
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* Library for converting TTF format font files to their WOFF2 versions. */
+
+#include <woff2/encode.h>
 
 #include <stdlib.h>
 #include <complex>
 #include <cstring>
 #include <limits>
 #include <string>
 #include <vector>
 
-#include "./brotli/encode.h"
+#include <brotli/encode.h>
 #include "./buffer.h"
 #include "./font.h"
 #include "./normalize.h"
 #include "./round.h"
 #include "./store_bytes.h"
 #include "./table_tags.h"
 #include "./transform.h"
 #include "./variable_length.h"
@@ -431,17 +423,17 @@ bool ConvertTTFToWOFF2(const uint8_t *da
         // for reused tables, only the original has an updated offset
         uint32_t table_offset =
           table.IsReused() ? table.reuse_of->offset : table.offset;
         uint32_t table_length =
           table.IsReused() ? table.reuse_of->length : table.length;
         std::pair<uint32_t, uint32_t> tag_offset(table.tag, table_offset);
         if (index_by_tag_offset.find(tag_offset) == index_by_tag_offset.end()) {
 #ifdef FONT_COMPRESSION_BIN
-          fprintf(stderr, "Missing table index for offset 0x%08x\n",
+fprintf(stderr, "Missing table index for offset 0x%08x\n",
                   table_offset);
 #endif
           return FONT_COMPRESSION_FAILURE();
         }
         uint16_t index = index_by_tag_offset[tag_offset];
         Store255UShort(index, &offset, result);
 
       }
deleted file mode 100644
--- a/modules/woff2/src/woff2_enc.h
+++ /dev/null
@@ -1,54 +0,0 @@
-// Copyright 2014 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// 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.
-
-#ifndef WOFF2_WOFF2_ENC_H_
-#define WOFF2_WOFF2_ENC_H_
-
-#include <stddef.h>
-#include <inttypes.h>
-#include <string>
-
-using std::string;
-
-
-namespace woff2 {
-
-struct WOFF2Params {
-  WOFF2Params() : extended_metadata(""), brotli_quality(11),
-                  allow_transforms(true) {}
-
-  string extended_metadata;
-  int brotli_quality;
-  bool allow_transforms;
-};
-
-// 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
-// actual compressed size. Returns true on successful compression.
-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 WOFF2Params& params);
-
-} // namespace woff2
-
-#endif  // WOFF2_WOFF2_ENC_H_
new file mode 100644
--- /dev/null
+++ b/modules/woff2/src/woff2_info.cc
@@ -0,0 +1,144 @@
+/* Copyright 2014 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+/* A commandline tool for dumping info about a woff2 file. */
+
+#include <string>
+
+#include "file.h"
+#include "./woff2_common.h"
+#include "./buffer.h"
+#include "./font.h"
+#include "./table_tags.h"
+#include "./variable_length.h"
+
+std::string PrintTag(int tag) {
+  if (tag & 0x80808080) {
+    return std::string("_xfm");  // print _xfm for xform tables (else garbage)
+  }
+  char printable[] = {
+    static_cast<char>((tag >> 24) & 0xFF),
+    static_cast<char>((tag >> 16) & 0xFF),
+    static_cast<char>((tag >> 8) & 0xFF),
+    static_cast<char>(tag & 0xFF)
+  };
+  return std::string(printable, 4);
+}
+
+int main(int argc, char **argv) {
+  using std::string;
+
+  if (argc != 2) {
+    fprintf(stderr, "One argument, the input filename, must be provided.\n");
+    return 1;
+  }
+
+  string filename(argv[1]);
+  string outfilename = filename.substr(0, filename.find_last_of(".")) + ".woff2";
+  fprintf(stdout, "Processing %s => %s\n",
+    filename.c_str(), outfilename.c_str());
+  string input = woff2::GetFileContent(filename);
+
+  woff2::Buffer file(reinterpret_cast<const uint8_t*>(input.data()),
+    input.size());
+
+  printf("WOFF2Header\n");
+  uint32_t signature, flavor, length, totalSfntSize, totalCompressedSize;
+  uint32_t metaOffset, metaLength, metaOrigLength, privOffset, privLength;
+  uint16_t num_tables, reserved, major, minor;
+  if (!file.ReadU32(&signature)) return 1;
+  if (!file.ReadU32(&flavor)) return 1;
+  if (!file.ReadU32(&length)) return 1;
+  if (!file.ReadU16(&num_tables)) return 1;
+  if (!file.ReadU16(&reserved)) return 1;
+  if (!file.ReadU32(&totalSfntSize)) return 1;
+  if (!file.ReadU32(&totalCompressedSize)) return 1;
+  if (!file.ReadU16(&major)) return 1;
+  if (!file.ReadU16(&minor)) return 1;
+  if (!file.ReadU32(&metaOffset)) return 1;
+  if (!file.ReadU32(&metaLength)) return 1;
+  if (!file.ReadU32(&metaOrigLength)) return 1;
+  if (!file.ReadU32(&privOffset)) return 1;
+  if (!file.ReadU32(&privLength)) return 1;
+
+  if (signature != 0x774F4632) {
+    printf("Invalid signature: %08x\n", signature);
+    return 1;
+  }
+  printf("signature           0x%08x\n", signature);
+  printf("flavor              0x%08x\n", flavor);
+  printf("length              %d\n", length);
+  printf("numTables           %d\n", num_tables);
+  printf("reserved            %d\n", reserved);
+  printf("totalSfntSize       %d\n", totalSfntSize);
+  printf("totalCompressedSize %d\n", totalCompressedSize);
+  printf("majorVersion        %d\n", major);
+  printf("minorVersion        %d\n", minor);
+  printf("metaOffset          %d\n", metaOffset);
+  printf("metaLength          %d\n", metaLength);
+  printf("metaOrigLength      %d\n", metaOrigLength);
+  printf("privOffset          %d\n", privOffset);
+  printf("privLength          %d\n", privLength);
+
+  std::vector<uint32_t> table_tags;
+  printf("TableDirectory starts at +%zu\n", file.offset());
+  printf("Entry offset flags tag  origLength txLength\n");
+  for (auto i = 0; i < num_tables; i++) {
+    size_t offset = file.offset();
+    uint8_t flags;
+    uint32_t tag, origLength, transformLength;
+    if (!file.ReadU8(&flags)) return 1;
+    if ((flags & 0x3f) == 0x3f) {
+      if (!file.ReadU32(&tag)) return 1;
+    } else {
+      tag = woff2::kKnownTags[flags & 0x3f];
+    }
+    table_tags.push_back(tag);
+    if (!ReadBase128(&file, &origLength)) return 1;
+
+    printf("%5d %6zu  0x%02x %s %10d", i, offset, flags,
+        PrintTag(tag).c_str(), origLength);
+
+    uint8_t xform_version = (flags >> 6) & 0x3;
+    if (tag == woff2::kGlyfTableTag || tag == woff2::kLocaTableTag) {
+      if (xform_version == 0) {
+        if (!ReadBase128(&file, &transformLength)) return 1;
+        printf(" %8d", transformLength);
+      }
+    } else if (xform_version > 0) {
+      if (!ReadBase128(&file, &transformLength)) return 1;
+      printf(" %8d", transformLength);
+    }
+    printf("\n");
+  }
+
+  // Collection header
+  if (flavor == woff2::kTtcFontFlavor) {
+    uint32_t version, numFonts;
+    if (!file.ReadU32(&version)) return 1;
+    if (!woff2::Read255UShort(&file, &numFonts)) return 1;
+    printf("CollectionHeader 0x%08x %d fonts\n", version, numFonts);
+
+    for (auto i = 0; i < numFonts; i++) {
+      uint32_t numTables, flavor;
+      if (!woff2::Read255UShort(&file, &numTables)) return 1;
+      if (!file.ReadU32(&flavor)) return 1;
+      printf("CollectionFontEntry %d flavor 0x%08x %d tables\n", i, flavor,
+          numTables);
+      for (auto j = 0; j < numTables; j++) {
+        uint32_t table_idx;
+        if (!woff2::Read255UShort(&file, &table_idx)) return 1;
+        if (table_idx >= table_tags.size()) return 1;
+        printf("  %d %s (idx %d)\n", j,
+            PrintTag(table_tags[table_idx]).c_str(), table_idx);
+      }
+    }
+  }
+
+  printf("TableDirectory ends at +%zu\n", file.offset());
+
+  return 0;
+}
--- a/modules/woff2/src/woff2_out.cc
+++ b/modules/woff2/src/woff2_out.cc
@@ -1,25 +1,19 @@
-// Copyright 2014 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Output buffer for WOFF2 decompression.
+/* Copyright 2014 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
 
-#include "./woff2_out.h"
+/* Output buffer for WOFF2 decompression. */
+
+#include <woff2/output.h>
+
+using std::string;
 
 namespace woff2 {
 
 WOFF2StringOut::WOFF2StringOut(string* buf)
   : buf_(buf),
     max_size_(kDefaultMaxSize),
     offset_(0) {}
 
deleted file mode 100644
--- a/modules/woff2/src/woff2_out.h
+++ /dev/null
@@ -1,114 +0,0 @@
-// Copyright 2016 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Output buffer for WOFF2 decompression.
-
-// Copyright 2016 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-// Output buffer for WOFF2 decompression.
-
-#ifndef WOFF2_WOFF2_OUT_H_
-#define WOFF2_WOFF2_OUT_H_
-
-#include <algorithm>
-#include <cstring>
-#include <memory>
-#include <string>
-#include "./port.h"
-
-namespace woff2 {
-
-// Suggested max size for output.
-const size_t kDefaultMaxSize = 30 * 1024 * 1024;
-
-using std::string;
-
-
-/**
- * Output interface for the woff2 decoding.
- *
- * Writes to arbitrary offsets are supported to facilitate updating offset
- * table and checksums after tables are ready. Reading the current size is
- * supported so a 'loca' table can be built up while writing glyphs.
- *
- * By default limits size to kDefaultMaxSize.
- */
-class WOFF2Out {
- public:
-  virtual ~WOFF2Out(void) {}
-
-  // Append n bytes of data from buf.
-  // Return true if all written, false otherwise.
-  virtual bool Write(const void *buf, size_t n) = 0;
-
-  // Write n bytes of data from buf at offset.
-  // Return true if all written, false otherwise.
-  virtual bool Write(const void *buf, size_t offset, size_t n) = 0;
-
-  virtual size_t Size() = 0;
-};
-
-/**
- * Expanding memory block for woff2 out. By default limited to kDefaultMaxSize.
- */
-class WOFF2StringOut : public WOFF2Out {
- public:
-  // Create a writer that writes its data to buf.
-  // buf->size() will grow to at most max_size
-  // buf may be sized (e.g. using EstimateWOFF2FinalSize) or empty.
-  explicit WOFF2StringOut(string* buf);
-
-  bool Write(const void *buf, size_t n) override;
-  bool Write(const void *buf, size_t offset, size_t n) override;
-  size_t Size() override { return offset_; }
-  size_t MaxSize() { return max_size_; }
-  void SetMaxSize(size_t max_size);
- private:
-  string* buf_;
-  size_t max_size_;
-  size_t offset_;
-};
-
-/**
- * Fixed memory block for woff2 out.
- */
-class WOFF2MemoryOut : public WOFF2Out {
- public:
-  // Create a writer that writes its data to buf.
-  WOFF2MemoryOut(uint8_t* buf, size_t buf_size);
-
-  bool Write(const void *buf, size_t n) override;
-  bool Write(const void *buf, size_t offset, size_t n) override;
-  size_t Size() override { return offset_; }
- private:
-  uint8_t* buf_;
-  size_t buf_size_;
-  size_t offset_;
-};
-
-} // namespace woff2
-
-#endif  // WOFF2_WOFF2_OUT_H_
--- a/modules/woff2/update.sh
+++ b/modules/woff2/update.sh
@@ -6,17 +6,19 @@
 MY_TEMP_DIR=`mktemp -d -t woff2_update.XXXXXX` || exit 1
 
 git clone https://github.com/google/woff2 ${MY_TEMP_DIR}/woff2
 
 COMMIT=`(cd ${MY_TEMP_DIR}/woff2 && git log | head -n 1)`
 perl -p -i -e "s/\[commit [0-9a-f]{40}\]/[${COMMIT}]/" README.mozilla;
 
 rm -rf src
+rm -rf include
 mv ${MY_TEMP_DIR}/woff2/src src
-patch -p3 < redefine-unique_ptr.patch
+mv ${MY_TEMP_DIR}/woff2/include include
 rm -rf ${MY_TEMP_DIR}
 hg add src
+hg add include
 
 echo "###"
-echo "### Updated woff2/src to $COMMIT."
+echo "### Updated woff2 to $COMMIT."
 echo "### Remember to verify and commit the changes to source control!"
 echo "###"