Added Brotli compress/decompress utilities and makefiles
diff --git a/brotli/dec/Makefile b/brotli/dec/Makefile
new file mode 100644
index 0000000..f1e39b9
--- /dev/null
+++ b/brotli/dec/Makefile
@@ -0,0 +1,10 @@
+#brotli/dec
+
+include ../../shared.mk
+
+OBJS = bit_reader.o decode.o huffman.o safe_malloc.o streams.o
+
+all : $(OBJS)
+
+clean :
+	rm -f $(OBJS)
diff --git a/brotli/dec/decode.c b/brotli/dec/decode.c
index a8b4ba3..6f08c5a 100644
--- a/brotli/dec/decode.c
+++ b/brotli/dec/decode.c
@@ -14,6 +14,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include "./bit_reader.h"
 #include "./context.h"
 #include "./decode.h"
@@ -372,8 +373,8 @@
   } else {
     *copy_dist = 0;
   }
-  insert_code = (kInsertRangeLut[range_idx] << 3) + ((code >> 3) & 7);
-  copy_code = (kCopyRangeLut[range_idx] << 3) + (code & 7);
+  insert_code = kInsertRangeLut[range_idx] + ((code >> 3) & 7);
+  copy_code = kCopyRangeLut[range_idx] + (code & 7);
   *insert_len = kInsertLengthPrefixCode[insert_code].offset;
   insert_extra_bits = kInsertLengthPrefixCode[insert_code].nbits;
   if (insert_extra_bits > 0) {
@@ -471,17 +472,11 @@
     return 1;
   }
 
-  if (*num_htrees == context_map_size) {
-    int i;
-    for (i = 0; i < context_map_size; ++i) {
-      (*context_map)[i] = i;
-    }
-    return 1;
-  }
   {
     HuffmanTree tree_index_htree;
     int use_rle_for_zeros = BrotliReadBits(br, 1);
     int max_run_length_prefix = 0;
+    int i;
     if (use_rle_for_zeros) {
       max_run_length_prefix = BrotliReadBits(br, 4) + 1;
     }
@@ -489,39 +484,26 @@
                          &tree_index_htree, br)) {
       return 0;
     }
-    if (use_rle_for_zeros) {
-      int i;
-      for (i = 0; i < context_map_size;) {
-        int code;
-        if (!BrotliReadMoreInput(br)) {
-          printf("[DecodeContextMap] Unexpected end of input.\n");
-          ok = 0;
-          goto End;
-        }
-        code = ReadSymbol(&tree_index_htree, br);
-        if (code == 0) {
+    for (i = 0; i < context_map_size;) {
+      int code;
+      if (!BrotliReadMoreInput(br)) {
+        printf("[DecodeContextMap] Unexpected end of input.\n");
+        ok = 0;
+        goto End;
+      }
+      code = ReadSymbol(&tree_index_htree, br);
+      if (code == 0) {
+        (*context_map)[i] = 0;
+        ++i;
+      } else if (code <= max_run_length_prefix) {
+        int reps = 1 + (1 << code) + BrotliReadBits(br, code);
+        while (--reps) {
           (*context_map)[i] = 0;
           ++i;
-        } else if (code <= max_run_length_prefix) {
-          int reps = 1 + (1 << code) + BrotliReadBits(br, code);
-          while (--reps) {
-            (*context_map)[i] = 0;
-            ++i;
-          }
-        } else {
-          (*context_map)[i] = code - max_run_length_prefix;
-          ++i;
         }
-      }
-    } else {
-      int i;
-      for (i = 0; i < context_map_size; ++i) {
-        if (!BrotliReadMoreInput(br)) {
-          printf("[DecodeContextMap] Unexpected end of input.\n");
-          ok = 0;
-          goto End;
-        }
-        (*context_map)[i] = ReadSymbol(&tree_index_htree, br);
+      } else {
+        (*context_map)[i] = code - max_run_length_prefix;
+        ++i;
       }
     }
    End:
@@ -640,6 +622,7 @@
   int input_size_bits = 0;
   int input_end = 0;
   int window_bits = 0;
+  size_t max_backward_distance;
   size_t ringbuffer_size;
   size_t ringbuffer_mask;
   uint8_t* ringbuffer;
@@ -678,6 +661,7 @@
   } else {
     window_bits = 16;
   }
+  max_backward_distance = (1 << window_bits) - 16;
 
   ringbuffer_size = 1 << window_bits;
   ringbuffer_mask = ringbuffer_size - 1;
@@ -812,6 +796,7 @@
       int copy_length;
       int distance_code;
       int distance;
+      size_t max_distance;
       uint8_t context;
       int j;
       const uint8_t* copy_src;
@@ -899,44 +884,56 @@
         dist_rb[dist_rb_idx & 3] = distance;
         ++dist_rb_idx;
       }
-
       BROTLI_LOG_UINT(distance);
 
-      if (pos < (size_t)distance || pos + copy_length > meta_block_end_pos) {
+      max_distance = max_backward_distance;
+      if (pos < max_distance) {
+        max_distance = pos;
+      }
+
+      if ((size_t)distance > max_distance) {
         printf("Invalid backward reference. pos: %ld distance: %d "
                "len: %d end: %lu\n", pos, distance, copy_length,
                (unsigned long)meta_block_end_pos);
         ok = 0;
         goto End;
-      }
+      } else {
+        if (pos + copy_length > meta_block_end_pos) {
+          printf("Invalid backward reference. pos: %zu distance: %d "
+                 "len: %d end: %zu\n", pos, distance, copy_length,
+                 meta_block_end_pos);
+          ok = 0;
+          goto End;
+        }
 
-      copy_src = &ringbuffer[(pos - distance) & ringbuffer_mask];
-      copy_dst = &ringbuffer[pos & ringbuffer_mask];
+        copy_src = &ringbuffer[(pos - distance) & ringbuffer_mask];
+        copy_dst = &ringbuffer[pos & ringbuffer_mask];
 
 #if (defined(__x86_64__) || defined(_M_X64))
-      if (copy_src + copy_length <= ringbuffer_end &&
-          copy_dst + copy_length < ringbuffer_end) {
-        if (copy_length <= 16 && distance >= 8) {
-          UNALIGNED_COPY64(copy_dst, copy_src);
-          UNALIGNED_COPY64(copy_dst + 8, copy_src + 8);
-        } else {
-          IncrementalCopyFastPath(copy_dst, copy_src, copy_length);
+        if (copy_src + copy_length <= ringbuffer_end &&
+            copy_dst + copy_length < ringbuffer_end) {
+          if (copy_length <= 16 && distance >= 8) {
+            UNALIGNED_COPY64(copy_dst, copy_src);
+            UNALIGNED_COPY64(copy_dst + 8, copy_src + 8);
+          } else {
+            IncrementalCopyFastPath(copy_dst, copy_src, copy_length);
+          }
+          pos += copy_length;
+          copy_length = 0;
         }
-        pos += copy_length;
-        copy_length = 0;
-      }
 #endif
 
-      for (j = 0; j < copy_length; ++j) {
-        ringbuffer[pos & ringbuffer_mask] =
-            ringbuffer[(pos - distance) & ringbuffer_mask];
-        if ((pos & ringbuffer_mask) == ringbuffer_mask) {
-          if (BrotliWrite(output, ringbuffer, ringbuffer_size) < 0) {
-            ok = 0;
-            goto End;
+        for (j = 0; j < copy_length; ++j) {
+          ringbuffer[pos & ringbuffer_mask] =
+              ringbuffer[(pos - distance) & ringbuffer_mask];
+          if ((pos & ringbuffer_mask) == ringbuffer_mask) {
+            if (BrotliWrite(output, ringbuffer, ringbuffer_size) < 0) {
+              ok = 0;
+              goto End;
+            }
           }
+          ++pos;
         }
-        ++pos;
       }
 
       // When we get here, we must have inserted at least one literal and made
diff --git a/brotli/dec/prefix.h b/brotli/dec/prefix.h
index dda01b1..500bd10 100644
--- a/brotli/dec/prefix.h
+++ b/brotli/dec/prefix.h
@@ -53,16 +53,12 @@
   {326, 8}, { 582,  9}, {1094, 10}, {2118, 24},
 };
 
-static const int kInsertAndCopyRangeLut[9] = {
-  0, 1, 4, 2, 3, 6, 5, 7, 8,
-};
-
 static const int kInsertRangeLut[9] = {
-  0, 0, 1, 1, 0, 2, 1, 2, 2,
+  0, 0, 8, 8, 0, 16, 8, 16, 16,
 };
 
 static const int kCopyRangeLut[9] = {
-  0, 1, 0, 1, 2, 0, 2, 1, 2,
+  0, 8, 0, 8, 16, 0, 16, 8, 16,
 };
 
 #endif  // BROTLI_DEC_PREFIX_H_
diff --git a/brotli/enc/Makefile b/brotli/enc/Makefile
new file mode 100644
index 0000000..c7041dc
--- /dev/null
+++ b/brotli/enc/Makefile
@@ -0,0 +1,11 @@
+#brotli/enc
+
+include ../../shared.mk
+
+OBJS = backward_references.o block_splitter.o encode.o entropy_encode.o histogram.o literal_cost.o prefix.o
+
+all : $(OBJS)
+
+clean :
+	rm -f $(OBJS) $(SO)
+
diff --git a/brotli/enc/backward_references.cc b/brotli/enc/backward_references.cc
index 5675633..71554fe 100644
--- a/brotli/enc/backward_references.cc
+++ b/brotli/enc/backward_references.cc
@@ -47,27 +47,30 @@
 
   while (i + 2 < i_end) {
     size_t best_len = 0;
+    size_t best_len_code = 0;
     size_t best_dist = 0;
     double best_score = 0;
-    const size_t max_distance = std::min(i + i_diff, max_backward_limit);
+    size_t max_distance = std::min(i + i_diff, max_backward_limit);
     hasher->set_insert_length(insert_length);
     bool match_found = hasher->FindLongestMatch(
         ringbuffer, literal_cost, ringbuffer_mask,
         i + i_diff, i_end - i, max_distance,
-        &best_len, &best_dist, &best_score);
+        &best_len, &best_len_code, &best_dist, &best_score);
     if (match_found) {
       // Found a match. Let's look for something even better ahead.
       int delayed_backward_references_in_row = 0;
       while (i + 4 < i_end &&
              delayed_backward_references_in_row < 4) {
         size_t best_len_2 = 0;
+        size_t best_len_code_2 = 0;
         size_t best_dist_2 = 0;
         double best_score_2 = 0;
+        max_distance = std::min(i + i_diff + 1, max_backward_limit);
         hasher->Store(ringbuffer + i, i + i_diff);
         match_found = hasher->FindLongestMatch(
             ringbuffer, literal_cost, ringbuffer_mask,
             i + i_diff + 1, i_end - i - 1, max_distance,
-            &best_len_2, &best_dist_2, &best_score_2);
+            &best_len_2, &best_len_code_2, &best_dist_2, &best_score_2);
         double cost_diff_lazy = 0;
         if (best_len >= 4) {
           cost_diff_lazy +=
@@ -96,6 +99,7 @@
           ++insert_length;
           ++delayed_backward_references_in_row;
           best_len = best_len_2;
+          best_len_code = best_len_code_2;
           best_dist = best_dist_2;
           best_score = best_score_2;
           i++;
@@ -106,6 +110,7 @@
       Command cmd;
       cmd.insert_length_ = insert_length;
       cmd.copy_length_ = best_len;
+      cmd.copy_length_code_ = best_len_code;
       cmd.copy_distance_ = best_dist;
       commands->push_back(cmd);
       hasher->set_last_distance(best_dist);
diff --git a/brotli/enc/command.h b/brotli/enc/command.h
index 8a539d0..7a9f481 100644
--- a/brotli/enc/command.h
+++ b/brotli/enc/command.h
@@ -24,13 +24,14 @@
 // Command holds a sequence of literals and a backward reference copy.
 class Command {
  public:
-  Command() : insert_length_(0), copy_length_(0),
+  Command() : insert_length_(0), copy_length_(0), copy_length_code_(0),
               copy_distance_(0), distance_code_(0),
               distance_prefix_(0), command_prefix_(0),
               distance_extra_bits_(0), distance_extra_bits_value_(0) {}
 
   uint32_t insert_length_;
   uint32_t copy_length_;
+  uint32_t copy_length_code_;
   uint32_t copy_distance_;
   // Values <= 16 are short codes, values > 16 are distances shifted by 16.
   uint32_t distance_code_;
diff --git a/brotli/enc/encode.cc b/brotli/enc/encode.cc
index bb3e3b8..88e1c4c 100644
--- a/brotli/enc/encode.cc
+++ b/brotli/enc/encode.cc
@@ -34,6 +34,18 @@
 
 namespace brotli {
 
+static const int kWindowBits = 22;
+// To make decoding faster, we allow the decoder to write 16 bytes ahead in
+// its ringbuffer, therefore the encoder has to decrease max distance by this
+// amount.
+static const int kDecoderRingBufferWriteAheadSlack = 16;
+static const int kMaxBackwardDistance =
+    (1 << kWindowBits) - kDecoderRingBufferWriteAheadSlack;
+
+static const int kMetaBlockSizeBits = 21;
+static const int kRingBufferBits = 23;
+static const int kRingBufferMask = (1 << kRingBufferBits) - 1;
+
 template<int kSize>
 double Entropy(const std::vector<Histogram<kSize> >& histograms) {
   double retval = 0;
@@ -264,7 +276,7 @@
   uint64_t insert_extra_bits_val =
       cmd.insert_length_ - InsertLengthOffset(code);
   int copy_extra_bits = CopyLengthExtraBits(code);
-  uint64_t copy_extra_bits_val = cmd.copy_length_ - CopyLengthOffset(code);
+  uint64_t copy_extra_bits_val = cmd.copy_length_code_ - CopyLengthOffset(code);
   if (insert_extra_bits > 0) {
     WriteBits(insert_extra_bits, insert_extra_bits_val, storage_ix, storage);
   }
@@ -325,8 +337,8 @@
   for (int i = 0; i < cmds->size(); ++i) {
     Command* cmd = &(*cmds)[i];
     cmd->command_prefix_ = CommandPrefix(cmd->insert_length_,
-                                         cmd->copy_length_);
-    if (cmd->copy_length_ > 0) {
+                                         cmd->copy_length_code_);
+    if (cmd->copy_length_code_ > 0) {
       PrefixEncodeCopyDistance(cmd->distance_code_,
                                num_direct_distance_codes,
                                distance_postfix_bits,
@@ -454,7 +466,7 @@
                       int* storage_ix, uint8_t* storage) {
   WriteBits(8, num_clusters - 1, storage_ix, storage);
 
-  if (num_clusters == 1 || num_clusters == context_map.size()) {
+  if (num_clusters == 1) {
     return;
   }
 
@@ -737,10 +749,10 @@
     }
     if (*pos < end_pos && cmd.distance_prefix_ != 0xffff) {
       MoveAndEncode(distance_split_code, &distance_it, storage_ix, storage);
-      int histogram_index = distance_it.type_;
       int context = (distance_it.type_ << 2) +
-          ((cmd.copy_length_ > 4) ? 3 : cmd.copy_length_ - 2);
-      histogram_index = mb.distance_context_map[context];
+          ((cmd.copy_length_code_ > 4) ? 3 : cmd.copy_length_code_ - 2);
+      int histogram_index = mb.distance_context_map[context];
+      size_t max_distance = std::min(*pos, (size_t)kMaxBackwardDistance);
       EncodeCopyDistance(cmd, distance_codes[histogram_index],
                          storage_ix, storage);
     }
@@ -748,32 +760,21 @@
   }
 }
 
-static const int kWindowBits = 22;
-// To make decoding faster, we allow the decoder to write 16 bytes ahead in
-// its ringbuffer, therefore the encoder has to decrease max distance by this
-// amount.
-static const int kDecoderRingBufferWriteAheadSlack = 16;
-static const int kMaxBackwardDistance =
-    (1 << kWindowBits) - kDecoderRingBufferWriteAheadSlack;
-
-static const int kMetaBlockSizeBits = 21;
-static const int kRingBufferBits = 23;
-static const int kRingBufferMask = (1 << kRingBufferBits) - 1;
-
 BrotliCompressor::BrotliCompressor()
-    : hasher_(new Hasher),
+    : window_bits_(kWindowBits),
+      hasher_(new Hasher),
       dist_ringbuffer_idx_(0),
       input_pos_(0),
       ringbuffer_(kRingBufferBits, kMetaBlockSizeBits),
       literal_cost_(1 << kRingBufferBits),
       storage_ix_(0),
       storage_(new uint8_t[2 << kMetaBlockSizeBits]) {
-    dist_ringbuffer_[0] = 4;
-    dist_ringbuffer_[1] = 11;
-    dist_ringbuffer_[2] = 15;
-    dist_ringbuffer_[3] = 16;
-    storage_[0] = 0;
-  }
+  dist_ringbuffer_[0] = 4;
+  dist_ringbuffer_[1] = 11;
+  dist_ringbuffer_[2] = 15;
+  dist_ringbuffer_[3] = 16;
+  storage_[0] = 0;
+}
 
 BrotliCompressor::~BrotliCompressor() {
   delete hasher_;
@@ -784,8 +785,12 @@
   // Don't encode input size.
   WriteBits(3, 0, &storage_ix_, storage_);
   // Encode window size.
-  WriteBits(1, 1, &storage_ix_, storage_);
-  WriteBits(3, kWindowBits - 17, &storage_ix_, storage_);
+  if (window_bits_ == 16) {
+    WriteBits(1, 0, &storage_ix_, storage_);
+  } else {
+    WriteBits(1, 1, &storage_ix_, storage_);
+    WriteBits(3, window_bits_ - 17, &storage_ix_, storage_);
+  }
 }
 
 void BrotliCompressor::WriteMetaBlock(const size_t input_size,
diff --git a/brotli/enc/encode.h b/brotli/enc/encode.h
index d2fb18e..60d150b 100644
--- a/brotli/enc/encode.h
+++ b/brotli/enc/encode.h
@@ -49,6 +49,7 @@
 
 
  private:
+  int window_bits_;
   Hasher* hasher_;
   int dist_ringbuffer_[4];
   size_t dist_ringbuffer_idx_;
diff --git a/brotli/enc/hash.h b/brotli/enc/hash.h
index 9b9bbab..c11e3a5 100644
--- a/brotli/enc/hash.h
+++ b/brotli/enc/hash.h
@@ -147,6 +147,7 @@
                         uint32_t max_length,
                         const uint32_t max_backward,
                         size_t * __restrict best_len_out,
+                        size_t * __restrict best_len_code_out,
                         size_t * __restrict best_distance_out,
                         double * __restrict best_score_out) {
     const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
@@ -227,6 +228,7 @@
           best_len = len;
           best_ix = backward;
           *best_len_out = best_len;
+          *best_len_code_out = best_len;
           *best_distance_out = best_ix;
           *best_score_out = best_score;
           match_found = true;
@@ -234,7 +236,7 @@
       }
     }
     const uint32_t key = Hash3Bytes(&data[cur_ix_masked], kBucketBits);
-    const uint32_t * __restrict const bucket = &buckets_[key][0];
+    const int * __restrict const bucket = &buckets_[key][0];
     const int down = (num_[key] > kBlockSize) ? (num_[key] - kBlockSize) : 0;
     int stop = int(cur_ix) - 64;
     if (stop < 0) { stop = 0; }
@@ -259,44 +261,50 @@
         best_len = len;
         best_ix = backward;
         *best_len_out = best_len;
+        *best_len_code_out = best_len;
         *best_distance_out = best_ix;
         match_found = true;
       }
     }
     for (int i = num_[key] - 1; i >= down; --i) {
-      size_t prev_ix = bucket[i & kBlockMask];
-      const size_t backward = cur_ix - prev_ix;
-      if (PREDICT_FALSE(backward > max_backward)) {
-        break;
-      }
-      prev_ix &= ring_buffer_mask;
-      if (data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
+      int prev_ix = bucket[i & kBlockMask];
+      if (prev_ix < 0) {
         continue;
-      }
-      const size_t len =
-          FindMatchLengthWithLimit(&data[prev_ix], &data[cur_ix_masked],
-                                   max_length);
-      if (len >= 3) {
-        // Comparing for >= 3 does not change the semantics, but just saves for
-        // a few unnecessary binary logarithms in backward reference score,
-        // since we are not interested in such short matches.
-        const double score = BackwardReferenceScore(average_cost_,
-                                                    start_cost4,
-                                                    start_cost3,
-                                                    start_cost2,
-                                                    len, backward,
-                                                    last_distance1_,
-                                                    last_distance2_,
-                                                    last_distance3_,
-                                                    last_distance4_);
-        if (best_score < score) {
-          best_score = score;
-          best_len = len;
-          best_ix = backward;
-          *best_len_out = best_len;
-          *best_distance_out = best_ix;
-          *best_score_out = best_score;
-          match_found = true;
+      } else {
+        const size_t backward = cur_ix - prev_ix;
+        if (PREDICT_FALSE(backward > max_backward)) {
+          break;
+        }
+        prev_ix &= ring_buffer_mask;
+        if (data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
+          continue;
+        }
+        const size_t len =
+            FindMatchLengthWithLimit(&data[prev_ix], &data[cur_ix_masked],
+                                     max_length);
+        if (len >= 3) {
+          // Comparing for >= 3 does not change the semantics, but just saves
+          // for a few unnecessary binary logarithms in backward reference
+          // score, since we are not interested in such short matches.
+          const double score = BackwardReferenceScore(average_cost_,
+                                                      start_cost4,
+                                                      start_cost3,
+                                                      start_cost2,
+                                                      len, backward,
+                                                      last_distance1_,
+                                                      last_distance2_,
+                                                      last_distance3_,
+                                                      last_distance4_);
+          if (best_score < score) {
+            best_score = score;
+            best_len = len;
+            best_ix = backward;
+            *best_len_out = best_len;
+            *best_len_code_out = best_len;
+            *best_distance_out = best_ix;
+            *best_score_out = best_score;
+            match_found = true;
+          }
         }
       }
     }
@@ -333,7 +341,7 @@
   uint16_t num_[kBucketSize];
 
   // Buckets containing kBlockSize of backward references.
-  uint32_t buckets_[kBucketSize][kBlockSize];
+  int buckets_[kBucketSize][kBlockSize];
 
   int last_distance1_;
   int last_distance2_;
diff --git a/brotli/enc/histogram.cc b/brotli/enc/histogram.cc
index fcffd1f..910b987 100644
--- a/brotli/enc/histogram.cc
+++ b/brotli/enc/histogram.cc
@@ -59,7 +59,7 @@
     if (cmd.copy_length_ > 0 && cmd.distance_prefix_ != 0xffff) {
       dist_it.Next();
       int context = (dist_it.type_ << kDistanceContextBits) +
-          ((cmd.copy_length_ > 4) ? 3 : cmd.copy_length_ - 2);
+          ((cmd.copy_length_code_ > 4) ? 3 : cmd.copy_length_code_ - 2);
       (*copy_dist_histograms)[context].Add(cmd.distance_prefix_);
     }
   }
diff --git a/shared.mk b/shared.mk
new file mode 100644
index 0000000..ecb6edf
--- /dev/null
+++ b/shared.mk
@@ -0,0 +1,10 @@
+IDIRS=-I../brotli/dec/ -I../brotli/enc/ -I../
+
+GFLAGS=-no-canonical-prefixes -fno-omit-frame-pointer -fno-tree-vrp -m64
+
+CPP = g++
+LFLAGS =
+CPPFLAGS = -c $(IDIRS) -std=c++0x $(GFLAGS)
+
+%.o : %.c
+	$(CPP) $(CPPFLAGS) $< -o $@
diff --git a/woff2/Makefile b/woff2/Makefile
new file mode 100644
index 0000000..971feac
--- /dev/null
+++ b/woff2/Makefile
@@ -0,0 +1,28 @@
+#Converter makefile
+
+include ../shared.mk
+
+OUROBJ = font.o glyph.o normalize.o transform.o woff2.o
+
+BROTLI = ../brotli
+ENCOBJ = $(BROTLI)/enc/*.o
+DECOBJ = $(BROTLI)/dec/*.o
+
+OBJS = $(OUROBJ)
+EXECUTABLES=woff2_compress woff2_decompress
+
+EXE_OBJS=$(patsubst %, %.o, $(EXECUTABLES))
+
+all : $(OBJS) $(EXECUTABLES)
+
+$(EXECUTABLES) : $(EXE_OBJS) deps
+	$(CPP) $(LFLAGS) $(OBJS) $(ENCOBJ) $(DECOBJ) $@.o -o $@
+
+deps :
+	make -C $(BROTLI)/dec
+	make -C $(BROTLI)/enc
+
+clean :
+	rm -f $(OBJS) $(EXE_OBJS) $(EXECUTABLES)
+	make -C $(BROTLI)/dec clean
+	make -C $(BROTLI)/enc clean
diff --git a/woff2/file.h b/woff2/file.h
new file mode 100644
index 0000000..f93fdee
--- /dev/null
+++ b/woff2/file.h
@@ -0,0 +1,40 @@
+// 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
+
+#ifndef BROTLI_WOFF2_FILE_H_
+#define BROTLI_WOFF2_FILE_H_
+
+#include <fstream>
+#include <iterator>
+
+namespace woff2 {
+
+inline std::string GetFileContent(std::string filename) {
+  std::ifstream ifs(filename.c_str(), std::ios::binary);
+  return std::string(
+    std::istreambuf_iterator<char>(ifs.rdbuf()),
+    std::istreambuf_iterator<char>());
+}
+
+inline void SetFileContents(std::string filename, std::string content) {
+  std::ofstream ofs(filename.c_str(), std::ios::binary);
+  std::copy(content.begin(),
+            content.end(),
+            std::ostream_iterator<char>(ofs));
+}
+
+} // namespace woff2
+#endif   //  BROTLI_WOFF2_FILE_H_
diff --git a/woff2/font.cc b/woff2/font.cc
new file mode 100644
index 0000000..2733708
--- /dev/null
+++ b/woff2/font.cc
@@ -0,0 +1,176 @@
+// 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
+
+#include "./font.h"
+
+#include <algorithm>
+
+#include "./ots.h"
+#include "./port.h"
+#include "./store_bytes.h"
+
+namespace woff2 {
+
+Font::Table* Font::FindTable(uint32_t tag) {
+  std::map<uint32_t, Font::Table>::iterator it = tables.find(tag);
+  return it == tables.end() ? 0 : &it->second;
+}
+
+const Font::Table* Font::FindTable(uint32_t tag) const {
+  std::map<uint32_t, Font::Table>::const_iterator it = tables.find(tag);
+  return it == tables.end() ? 0 : &it->second;
+}
+
+bool ReadFont(const uint8_t* data, size_t len, Font* font) {
+  ots::Buffer file(data, len);
+
+  // We don't care about the search_range, entry_selector and range_shift
+  // fields, they will always be computed upon writing the font.
+  if (!file.ReadU32(&font->flavor) ||
+      !file.ReadU16(&font->num_tables) ||
+      !file.Skip(6)) {
+    return OTS_FAILURE();
+  }
+
+  std::map<uint32_t, uint32_t> intervals;
+  for (uint16_t i = 0; i < font->num_tables; ++i) {
+    Font::Table table;
+    if (!file.ReadU32(&table.tag) ||
+        !file.ReadU32(&table.checksum) ||
+        !file.ReadU32(&table.offset) ||
+        !file.ReadU32(&table.length)) {
+      return OTS_FAILURE();
+    }
+    if ((table.offset & 3) != 0 ||
+        table.length > len ||
+        len - table.length < table.offset) {
+      return OTS_FAILURE();
+    }
+    intervals[table.offset] = table.length;
+    table.data = data + table.offset;
+    if (font->tables.find(table.tag) != font->tables.end()) {
+      return OTS_FAILURE();
+    }
+    font->tables[table.tag] = table;
+  }
+
+  // Check that tables are non-overlapping.
+  uint32_t last_offset = 12UL + 16UL * font->num_tables;
+  for (const auto& i : intervals) {
+    if (i.first < last_offset || i.first + i.second < i.first) {
+      return OTS_FAILURE();
+    }
+    last_offset = i.first + i.second;
+  }
+  return true;
+}
+
+size_t FontFileSize(const Font& font) {
+  size_t max_offset = 12ULL + 16ULL * font.num_tables;
+  for (const auto& i : font.tables) {
+    const Font::Table& table = i.second;
+    size_t padding_size = (4 - (table.length & 3)) & 3;
+    size_t end_offset = (padding_size + table.offset) + table.length;
+    max_offset = std::max(max_offset, end_offset);
+  }
+  return max_offset;
+}
+
+bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size) {
+  if (dst_size < 12ULL + 16ULL * font.num_tables) {
+    return OTS_FAILURE();
+  }
+  size_t offset = 0;
+  StoreU32(font.flavor, &offset, dst);
+  Store16(font.num_tables, &offset, dst);
+  uint16_t max_pow2 = font.num_tables ? Log2Floor(font.num_tables) : 0;
+  uint16_t search_range = max_pow2 ? 1 << (max_pow2 + 4) : 0;
+  uint16_t range_shift = (font.num_tables << 4) - search_range;
+  Store16(search_range, &offset, dst);
+  Store16(max_pow2, &offset, dst);
+  Store16(range_shift, &offset, dst);
+  for (const auto& i : font.tables) {
+    const Font::Table& table = i.second;
+    StoreU32(table.tag, &offset, dst);
+    StoreU32(table.checksum, &offset, dst);
+    StoreU32(table.offset, &offset, dst);
+    StoreU32(table.length, &offset, dst);
+    if (table.offset + table.length < table.offset ||
+        dst_size < table.offset + table.length) {
+      return OTS_FAILURE();
+    }
+    memcpy(dst + table.offset, table.data, table.length);
+    size_t padding_size = (4 - (table.length & 3)) & 3;
+    if (table.offset + table.length + padding_size < padding_size ||
+        dst_size < table.offset + table.length + padding_size) {
+      return OTS_FAILURE();
+    }
+    memset(dst + table.offset + table.length, 0, padding_size);
+  }
+  return true;
+}
+
+int NumGlyphs(const Font& font) {
+  const Font::Table* head_table = font.FindTable(kHeadTableTag);
+  const Font::Table* loca_table = font.FindTable(kLocaTableTag);
+  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;
+}
+
+bool GetGlyphData(const Font& font, int glyph_index,
+                  const uint8_t** glyph_data, size_t* glyph_size) {
+  if (glyph_index < 0) {
+    return OTS_FAILURE();
+  }
+  const Font::Table* head_table = font.FindTable(kHeadTableTag);
+  const Font::Table* loca_table = font.FindTable(kLocaTableTag);
+  const Font::Table* glyf_table = font.FindTable(kGlyfTableTag);
+  if (head_table == NULL || loca_table == NULL || glyf_table == NULL ||
+      head_table->length < 52) {
+    return OTS_FAILURE();
+  }
+  int index_fmt = head_table->data[51];
+  ots::Buffer loca_buf(loca_table->data, loca_table->length);
+  if (index_fmt == 0) {
+    uint16_t offset1, offset2;
+    if (!loca_buf.Skip(2 * glyph_index) ||
+        !loca_buf.ReadU16(&offset1) ||
+        !loca_buf.ReadU16(&offset2) ||
+        offset2 < offset1 ||
+        2 * offset2 > glyf_table->length) {
+      return OTS_FAILURE();
+    }
+    *glyph_data = glyf_table->data + 2 * offset1;
+    *glyph_size = 2 * (offset2 - offset1);
+  } else {
+    uint32_t offset1, offset2;
+    if (!loca_buf.Skip(4 * glyph_index) ||
+        !loca_buf.ReadU32(&offset1) ||
+        !loca_buf.ReadU32(&offset2) ||
+        offset2 < offset1 ||
+        offset2 > glyf_table->length) {
+      return OTS_FAILURE();
+    }
+    *glyph_data = glyf_table->data + offset1;
+    *glyph_size = offset2 - offset1;
+  }
+  return true;
+}
+
+} // namespace woff2
diff --git a/woff2/font.h b/woff2/font.h
new file mode 100644
index 0000000..21fd634
--- /dev/null
+++ b/woff2/font.h
@@ -0,0 +1,81 @@
+// 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.
+
+#ifndef BROTLI_WOFF2_FONT_H_
+#define BROTLI_WOFF2_FONT_H_
+
+#include <stddef.h>
+#include <inttypes.h>
+#include <map>
+#include <vector>
+
+namespace woff2 {
+
+// Tags of popular tables.
+static const uint32_t kGlyfTableTag = 0x676c7966;
+static const uint32_t kHeadTableTag = 0x68656164;
+static const uint32_t kLocaTableTag = 0x6c6f6361;
+
+// Represents an sfnt font file. Only the table directory is parsed, for the
+// table data we only store a raw pointer, therefore a font object is valid only
+// as long the data from which it was parsed is around.
+struct Font {
+  uint32_t flavor;
+  uint16_t num_tables;
+
+  struct Table {
+    uint32_t tag;
+    uint32_t checksum;
+    uint32_t offset;
+    uint32_t length;
+    const uint8_t* data;
+
+    // Buffer used to mutate the data before writing out.
+    std::vector<uint8_t> buffer;
+  };
+  std::map<uint32_t, Table> tables;
+
+  Table* FindTable(uint32_t tag);
+  const Table* FindTable(uint32_t tag) const;
+};
+
+// Parses the font from the given data. Returns false on parsing failure or
+// buffer overflow. The font is valid only so long the input data pointer is
+// valid.
+bool ReadFont(const uint8_t* data, size_t len, Font* font);
+
+// Returns the file size of the font.
+size_t FontFileSize(const Font& font);
+
+// Writes the font into the specified dst buffer. The dst_size should be the
+// same as returned by FontFileSize(). Returns false upon buffer overflow (which
+// should not happen if dst_size was computed by FontFileSize()).
+bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size);
+
+// Returns the number of glyphs in the font.
+// NOTE: Currently this works only for TrueType-flavored fonts, will return
+// zero for CFF-flavored fonts.
+int NumGlyphs(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,
+                  const uint8_t** glyph_data, size_t* glyph_size);
+
+} // namespace woff2
+
+#endif  // BROTLI_WOFF2_FONT_H_
diff --git a/woff2/glyph.cc b/woff2/glyph.cc
new file mode 100644
index 0000000..4752e09
--- /dev/null
+++ b/woff2/glyph.cc
@@ -0,0 +1,380 @@
+// 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
+
+#include "./glyph.h"
+
+#include <stdlib.h>
+#include <limits>
+#include "./ots.h"
+#include "./store_bytes.h"
+
+namespace woff2 {
+
+static const int32_t kFLAG_ONCURVE = 1;
+static const int32_t kFLAG_XSHORT = 1 << 1;
+static const int32_t kFLAG_YSHORT = 1 << 2;
+static const int32_t kFLAG_REPEAT = 1 << 3;
+static const int32_t kFLAG_XREPEATSIGN = 1 << 4;
+static const int32_t kFLAG_YREPEATSIGN = 1 << 5;
+static const int32_t kFLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0;
+static const int32_t kFLAG_WE_HAVE_A_SCALE = 1 << 3;
+static const int32_t kFLAG_MORE_COMPONENTS = 1 << 5;
+static const int32_t kFLAG_WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6;
+static const int32_t kFLAG_WE_HAVE_A_TWO_BY_TWO = 1 << 7;
+static const int32_t kFLAG_WE_HAVE_INSTRUCTIONS = 1 << 8;
+
+bool ReadCompositeGlyphData(ots::Buffer* buffer, Glyph* glyph) {
+  glyph->have_instructions = false;
+  glyph->composite_data = buffer->buffer() + buffer->offset();
+  size_t start_offset = buffer->offset();
+  uint16_t flags = kFLAG_MORE_COMPONENTS;
+  while (flags & kFLAG_MORE_COMPONENTS) {
+    if (!buffer->ReadU16(&flags)) {
+      return OTS_FAILURE();
+    }
+    glyph->have_instructions |= (flags & kFLAG_WE_HAVE_INSTRUCTIONS) != 0;
+    size_t arg_size = 2;  // glyph index
+    if (flags & kFLAG_ARG_1_AND_2_ARE_WORDS) {
+      arg_size += 4;
+    } else {
+      arg_size += 2;
+    }
+    if (flags & kFLAG_WE_HAVE_A_SCALE) {
+      arg_size += 2;
+    } else if (flags & kFLAG_WE_HAVE_AN_X_AND_Y_SCALE) {
+      arg_size += 4;
+    } else if (flags & kFLAG_WE_HAVE_A_TWO_BY_TWO) {
+      arg_size += 8;
+    }
+    if (!buffer->Skip(arg_size)) {
+      return OTS_FAILURE();
+    }
+  }
+  if (buffer->offset() - start_offset > std::numeric_limits<uint32_t>::max()) {
+    return OTS_FAILURE();
+  }
+  glyph->composite_data_size = buffer->offset() - start_offset;
+  return true;
+}
+
+bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) {
+  ots::Buffer buffer(data, len);
+
+  int16_t num_contours;
+  if (!buffer.ReadS16(&num_contours)) {
+    return OTS_FAILURE();
+  }
+
+  if (num_contours == 0) {
+    // Empty glyph.
+    return true;
+  }
+
+  // Read the bounding box.
+  if (!buffer.ReadS16(&glyph->x_min) ||
+      !buffer.ReadS16(&glyph->y_min) ||
+      !buffer.ReadS16(&glyph->x_max) ||
+      !buffer.ReadS16(&glyph->y_max)) {
+    return OTS_FAILURE();
+  }
+
+  if (num_contours > 0) {
+    // Simple glyph.
+    glyph->contours.resize(num_contours);
+
+    // Read the number of points per contour.
+    uint16_t last_point_index = 0;
+    for (int i = 0; i < num_contours; ++i) {
+      uint16_t point_index;
+      if (!buffer.ReadU16(&point_index)) {
+        return OTS_FAILURE();
+      }
+      uint16_t num_points = point_index - last_point_index + (i == 0 ? 1 : 0);
+      glyph->contours[i].resize(num_points);
+      last_point_index = point_index;
+    }
+
+    // Read the instructions.
+    if (!buffer.ReadU16(&glyph->instructions_size)) {
+      return OTS_FAILURE();
+    }
+    glyph->instructions_data = data + buffer.offset();
+    if (!buffer.Skip(glyph->instructions_size)) {
+      return OTS_FAILURE();
+    }
+
+    // Read the run-length coded flags.
+    std::vector<std::vector<uint8_t> > flags(num_contours);
+    uint8_t flag = 0;
+    uint8_t flag_repeat = 0;
+    for (int i = 0; i < num_contours; ++i) {
+      flags[i].resize(glyph->contours[i].size());
+      for (int j = 0; j < glyph->contours[i].size(); ++j) {
+        if (flag_repeat == 0) {
+          if (!buffer.ReadU8(&flag)) {
+            return OTS_FAILURE();
+          }
+          if (flag & kFLAG_REPEAT) {
+            if (!buffer.ReadU8(&flag_repeat)) {
+              return OTS_FAILURE();
+            }
+          }
+        } else {
+          flag_repeat--;
+        }
+        flags[i][j] = flag;
+        glyph->contours[i][j].on_curve = flag & kFLAG_ONCURVE;
+      }
+    }
+
+    // Read the x coordinates.
+    int prev_x = 0;
+    for (int i = 0; i < num_contours; ++i) {
+      for (int j = 0; j < glyph->contours[i].size(); ++j) {
+        uint8_t flag = flags[i][j];
+        if (flag & kFLAG_XSHORT) {
+          // single byte x-delta coord value
+          uint8_t x_delta;
+          if (!buffer.ReadU8(&x_delta)) {
+            return OTS_FAILURE();
+          }
+          int sign = (flag & kFLAG_XREPEATSIGN) ? 1 : -1;
+          glyph->contours[i][j].x = prev_x + sign * x_delta;
+        } else {
+          // double byte x-delta coord value
+          int16_t x_delta = 0;
+          if (!(flag & kFLAG_XREPEATSIGN)) {
+            if (!buffer.ReadS16(&x_delta)) {
+              return OTS_FAILURE();
+            }
+          }
+          glyph->contours[i][j].x = prev_x + x_delta;
+        }
+        prev_x = glyph->contours[i][j].x;
+      }
+    }
+
+    // Read the y coordinates.
+    int prev_y = 0;
+    for (int i = 0; i < num_contours; ++i) {
+      for (int j = 0; j < glyph->contours[i].size(); ++j) {
+        uint8_t flag = flags[i][j];
+        if (flag & kFLAG_YSHORT) {
+          // single byte y-delta coord value
+          uint8_t y_delta;
+          if (!buffer.ReadU8(&y_delta)) {
+            return OTS_FAILURE();
+          }
+          int sign = (flag & kFLAG_YREPEATSIGN) ? 1 : -1;
+          glyph->contours[i][j].y = prev_y + sign * y_delta;
+        } else {
+          // double byte y-delta coord value
+          int16_t y_delta = 0;
+          if (!(flag & kFLAG_YREPEATSIGN)) {
+            if (!buffer.ReadS16(&y_delta)) {
+              return OTS_FAILURE();
+            }
+          }
+          glyph->contours[i][j].y = prev_y + y_delta;
+        }
+        prev_y = glyph->contours[i][j].y;
+      }
+    }
+  } else if (num_contours == -1) {
+    // Composite glyph.
+    if (!ReadCompositeGlyphData(&buffer, glyph)) {
+      return OTS_FAILURE();
+    }
+    // Read the instructions.
+    if (glyph->have_instructions) {
+      if (!buffer.ReadU16(&glyph->instructions_size)) {
+        return OTS_FAILURE();
+      }
+      glyph->instructions_data = data + buffer.offset();
+      if (!buffer.Skip(glyph->instructions_size)) {
+        return OTS_FAILURE();
+      }
+    } else {
+      glyph->instructions_size = 0;
+    }
+  } else {
+    return OTS_FAILURE();
+  }
+  return true;
+}
+
+namespace {
+
+void StoreBbox(const Glyph& glyph, size_t* offset, uint8_t* dst) {
+  Store16(glyph.x_min, offset, dst);
+  Store16(glyph.y_min, offset, dst);
+  Store16(glyph.x_max, offset, dst);
+  Store16(glyph.y_max, offset, dst);
+}
+
+void StoreInstructions(const Glyph& glyph, size_t* offset, uint8_t* dst) {
+  Store16(glyph.instructions_size, offset, dst);
+  StoreBytes(glyph.instructions_data, glyph.instructions_size, offset, dst);
+}
+
+bool StoreEndPtsOfContours(const Glyph& glyph, size_t* offset, uint8_t* dst) {
+  int end_point = -1;
+  for (const auto& contour : glyph.contours) {
+    end_point += contour.size();
+    if (contour.size() > std::numeric_limits<uint16_t>::max() ||
+        end_point > std::numeric_limits<uint16_t>::max()) {
+      return OTS_FAILURE();
+    }
+    Store16(end_point, offset, dst);
+  }
+  return true;
+}
+
+bool StorePoints(const Glyph& glyph, size_t* offset,
+                 uint8_t* dst, size_t dst_size) {
+  int last_flag = -1;
+  int repeat_count = 0;
+  int last_x = 0;
+  int last_y = 0;
+  size_t x_bytes = 0;
+  size_t y_bytes = 0;
+
+  // Store the flags and calculate the total size of the x and y coordinates.
+  for (const auto& contour : glyph.contours) {
+    for (const auto& point : contour) {
+      int flag = point.on_curve ? kFLAG_ONCURVE : 0;
+      int dx = point.x - last_x;
+      int dy = point.y - last_y;
+      if (dx == 0) {
+        flag |= kFLAG_XREPEATSIGN;
+      } else if (dx > -256 && dx < 256) {
+        flag |= kFLAG_XSHORT | (dx > 0 ? kFLAG_XREPEATSIGN : 0);
+        x_bytes += 1;
+      } else {
+        x_bytes += 2;
+      }
+      if (dy == 0) {
+        flag |= kFLAG_YREPEATSIGN;
+      } else if (dy > -256 && dy < 256) {
+        flag |= kFLAG_YSHORT | (dy > 0 ? kFLAG_YREPEATSIGN : 0);
+        y_bytes += 1;
+      } else {
+        y_bytes += 2;
+      }
+      if (flag == last_flag && repeat_count != 255) {
+        dst[*offset - 1] |= kFLAG_REPEAT;
+        repeat_count++;
+      } else {
+        if (repeat_count != 0) {
+          if (*offset >= dst_size) {
+            return OTS_FAILURE();
+          }
+          dst[(*offset)++] = repeat_count;
+        }
+        if (*offset >= dst_size) {
+          return OTS_FAILURE();
+        }
+        dst[(*offset)++] = flag;
+        repeat_count = 0;
+      }
+      last_x = point.x;
+      last_y = point.y;
+      last_flag = flag;
+    }
+  }
+  if (repeat_count != 0) {
+    if (*offset >= dst_size) {
+      return OTS_FAILURE();
+    }
+    dst[(*offset)++] = repeat_count;
+  }
+
+  if (*offset + x_bytes + y_bytes > dst_size) {
+    return OTS_FAILURE();
+  }
+
+  // Store the x and y coordinates.
+  size_t x_offset = *offset;
+  size_t y_offset = *offset + x_bytes;
+  last_x = 0;
+  last_y = 0;
+  for (const auto& contour : glyph.contours) {
+    for (const auto& point : contour) {
+      int dx = point.x - last_x;
+      int dy = point.y - last_y;
+      if (dx == 0) {
+        // pass
+      } else if (dx > -256 && dx < 256) {
+        dst[x_offset++] = std::abs(dx);
+      } else {
+        Store16(dx, &x_offset, dst);
+      }
+      if (dy == 0) {
+        // pass
+      } else if (dy > -256 && dy < 256) {
+        dst[y_offset++] = std::abs(dy);
+      } else {
+        Store16(dy, &y_offset, dst);
+      }
+      last_x += dx;
+      last_y += dy;
+    }
+  }
+  *offset = y_offset;
+  return true;
+}
+
+}  // namespace
+
+bool StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) {
+  size_t offset = 0;
+  if (glyph.composite_data_size > 0) {
+    // Composite glyph.
+    if (*dst_size < ((10ULL + glyph.composite_data_size) +
+                     ((glyph.have_instructions ? 2ULL : 0) +
+                      glyph.instructions_size))) {
+      return OTS_FAILURE();
+    }
+    Store16(-1, &offset, dst);
+    StoreBbox(glyph, &offset, dst);
+    StoreBytes(glyph.composite_data, glyph.composite_data_size, &offset, dst);
+    if (glyph.have_instructions) {
+      StoreInstructions(glyph, &offset, dst);
+    }
+  } else if (glyph.contours.size() > 0) {
+    // Simple glyph.
+    if (glyph.contours.size() > std::numeric_limits<int16_t>::max()) {
+      return OTS_FAILURE();
+    }
+    if (*dst_size < ((12ULL + 2 * glyph.contours.size()) +
+                     glyph.instructions_size)) {
+      return OTS_FAILURE();
+    }
+    Store16(glyph.contours.size(), &offset, dst);
+    StoreBbox(glyph, &offset, dst);
+    if (!StoreEndPtsOfContours(glyph, &offset, dst)) {
+      return OTS_FAILURE();
+    }
+    StoreInstructions(glyph, &offset, dst);
+    if (!StorePoints(glyph, &offset, dst, *dst_size)) {
+      return OTS_FAILURE();
+    }
+  }
+  *dst_size = offset;
+  return true;
+}
+
+} // namespace woff2
diff --git a/woff2/glyph.h b/woff2/glyph.h
new file mode 100644
index 0000000..2e249f6
--- /dev/null
+++ b/woff2/glyph.h
@@ -0,0 +1,71 @@
+// 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.
+
+#ifndef BROTLI_WOFF2_GLYPH_H_
+#define BROTLI_WOFF2_GLYPH_H_
+
+#include <stddef.h>
+#include <inttypes.h>
+#include <vector>
+
+namespace woff2 {
+
+// Represents a parsed simple or composite glyph. The composite glyph data and
+// instructions are un-parsed and we keep only pointers to the raw data,
+// therefore the glyph is valid only so long the data from which it was parsed
+// is around.
+class Glyph {
+ public:
+  Glyph() : instructions_size(0), composite_data_size(0) {}
+
+  // Bounding box.
+  int16_t x_min;
+  int16_t x_max;
+  int16_t y_min;
+  int16_t y_max;
+
+  // Instructions.
+  uint16_t instructions_size;
+  const uint8_t* instructions_data;
+
+  // Data model for simple glyphs.
+  struct Point {
+    int x;
+    int y;
+    bool on_curve;
+  };
+  std::vector<std::vector<Point> > contours;
+
+  // Data for composite glyphs.
+  const uint8_t* composite_data;
+  uint32_t composite_data_size;
+  bool have_instructions;
+};
+
+// Parses the glyph from the given data. Returns false on parsing failure or
+// buffer overflow. The glyph is valid only so long the input data pointer is
+// valid.
+bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph);
+
+// Stores the glyph into the specified dst buffer. The *dst_size is the buffer
+// size on entry and is set to the actual (unpadded) stored size on exit.
+// Returns false on buffer overflow.
+bool StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size);
+
+} // namespace woff2
+
+#endif  // BROTLI_WOFF2_GLYPH_H_
diff --git a/woff2/normalize.cc b/woff2/normalize.cc
new file mode 100644
index 0000000..ef9f158
--- /dev/null
+++ b/woff2/normalize.cc
@@ -0,0 +1,194 @@
+// 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
+
+#include "./normalize.h"
+
+#include <inttypes.h>
+#include <stddef.h>
+
+#include "./ots.h"
+#include "./port.h"
+#include "./font.h"
+#include "./glyph.h"
+#include "./round.h"
+#include "./store_bytes.h"
+
+namespace woff2 {
+
+namespace {
+
+void StoreLoca(int index_fmt, uint32_t value, size_t* offset, uint8_t* dst) {
+  if (index_fmt == 0) {
+    Store16(value >> 1, offset, dst);
+  } else {
+    StoreU32(value, offset, dst);
+  }
+}
+
+void NormalizeSimpleGlyphBoundingBox(Glyph* glyph) {
+  if (glyph->contours.empty() || glyph->contours[0].empty()) {
+    return;
+  }
+  int16_t x_min = glyph->contours[0][0].x;
+  int16_t y_min = glyph->contours[0][0].y;
+  int16_t x_max = x_min;
+  int16_t y_max = y_min;
+  for (const auto& contour : glyph->contours) {
+    for (const auto& point : contour) {
+      if (point.x < x_min) x_min = point.x;
+      if (point.x > x_max) x_max = point.x;
+      if (point.y < y_min) y_min = point.y;
+      if (point.y > y_max) y_max = point.y;
+    }
+  }
+  glyph->x_min = x_min;
+  glyph->y_min = y_min;
+  glyph->x_max = x_max;
+  glyph->y_max = y_max;
+}
+
+}  // namespace
+
+bool NormalizeGlyphs(Font* font) {
+  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 || loca_table == NULL || glyf_table == NULL) {
+    return OTS_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);
+  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;
+  size_t loca_offset = 0;
+
+  for (int i = 0; i < num_glyphs; ++i) {
+    StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst);
+    Glyph glyph;
+    const uint8_t* glyph_data;
+    size_t glyph_size;
+    if (!GetGlyphData(*font, i, &glyph_data, &glyph_size) ||
+        (glyph_size > 0 && !ReadGlyph(glyph_data, glyph_size, &glyph))) {
+      return OTS_FAILURE();
+    }
+    NormalizeSimpleGlyphBoundingBox(&glyph);
+    size_t glyf_dst_size = glyf_table->buffer.size() - glyf_offset;
+    if (!StoreGlyph(glyph, glyf_dst + glyf_offset, &glyf_dst_size)) {
+      return OTS_FAILURE();
+    }
+    glyf_dst_size = Round4(glyf_dst_size);
+    if (glyf_dst_size > std::numeric_limits<uint32_t>::max() ||
+        glyf_offset + static_cast<uint32_t>(glyf_dst_size) < glyf_offset ||
+        (index_fmt == 0 && glyf_offset + glyf_dst_size >= (1UL << 17))) {
+      return OTS_FAILURE();
+    }
+    glyf_offset += glyf_dst_size;
+  }
+  StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst);
+
+  glyf_table->buffer.resize(glyf_offset);
+  glyf_table->data = &glyf_table->buffer[0];
+  glyf_table->length = glyf_offset;
+  loca_table->data = &loca_table->buffer[0];
+
+  return true;
+}
+
+bool NormalizeOffsets(Font* font) {
+  uint32_t offset = 12 + 16 * font->num_tables;
+  for (auto& i : font->tables) {
+    i.second.offset = offset;
+    offset += Round4(i.second.length);
+  }
+  return true;
+}
+
+namespace {
+
+uint32_t ComputeChecksum(const uint8_t* buf, size_t size) {
+  uint32_t checksum = 0;
+  for (size_t i = 0; i < size; i += 4) {
+    checksum += ((buf[i] << 24) |
+                 (buf[i + 1] << 16) |
+                 (buf[i + 2] << 8) |
+                 buf[i + 3]);
+  }
+  return checksum;
+}
+
+uint32_t ComputeHeaderChecksum(const Font& font) {
+  uint32_t checksum = font.flavor;
+  uint16_t max_pow2 = font.num_tables ? Log2Floor(font.num_tables) : 0;
+  uint16_t search_range = max_pow2 ? 1 << (max_pow2 + 4) : 0;
+  uint16_t range_shift = (font.num_tables << 4) - search_range;
+  checksum += (font.num_tables << 16 | search_range);
+  checksum += (max_pow2 << 16 | range_shift);
+  for (const auto& i : font.tables) {
+    checksum += i.second.tag;
+    checksum += i.second.checksum;
+    checksum += i.second.offset;
+    checksum += i.second.length;
+  }
+  return checksum;
+}
+
+}  // namespace
+
+bool FixChecksums(Font* font) {
+  Font::Table* head_table = font->FindTable(kHeadTableTag);
+  if (head_table == NULL || head_table->length < 12) {
+    return OTS_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;
+  for (auto& i : font->tables) {
+    Font::Table* table = &i.second;
+    table->checksum = ComputeChecksum(table->data, table->length);
+    file_checksum += table->checksum;
+  }
+  file_checksum += ComputeHeaderChecksum(*font);
+  offset = 8;
+  StoreU32(0xb1b0afba - file_checksum, &offset, head_buf);
+  return true;
+}
+
+bool NormalizeFont(Font* font) {
+  return (NormalizeGlyphs(font) &&
+          NormalizeOffsets(font) &&
+          FixChecksums(font));
+}
+
+} // namespace woff2
diff --git a/woff2/normalize.h b/woff2/normalize.h
new file mode 100644
index 0000000..b3d8331
--- /dev/null
+++ b/woff2/normalize.h
@@ -0,0 +1,45 @@
+// 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.
+
+#ifndef BROTLI_WOFF2_NORMALIZE_H_
+#define BROTLI_WOFF2_NORMALIZE_H_
+
+namespace woff2 {
+
+struct Font;
+
+// Changes the offset fields of the table headers so that the data for the
+// tables will be written in order of increasing tag values, without any gaps
+// other than the 4-byte padding.
+bool NormalizeOffsets(Font* font);
+
+// Changes the checksum fields of the table headers and the checksum field of
+// the head table so that it matches the current data.
+bool FixChecksums(Font* font);
+
+// Parses each of the glyphs in the font and writes them again to the glyf
+// table in normalized form, as defined by the StoreGlyph() function. Changes
+// the loca table accordigly.
+bool NormalizeGlyphs(Font* font);
+
+// Performs all of the normalization steps above.
+bool NormalizeFont(Font* font);
+
+} // namespace woff2
+
+#endif  // BROTLI_WOFF2_NORMALIZE_H_
diff --git a/woff2/ots.h b/woff2/ots.h
new file mode 100644
index 0000000..65b0d8c
--- /dev/null
+++ b/woff2/ots.h
@@ -0,0 +1,170 @@
+// 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.
+
+#ifndef BROTLI_WOFF2_OTS_H_
+#define BROTLI_WOFF2_OTS_H_
+
+#include <cstdlib>
+#include <cstring>
+#include <limits>
+
+namespace ots {
+
+#if defined(_WIN32)
+#include <stdlib.h>
+typedef signed char int8_t;
+typedef unsigned char uint8_t;
+typedef short int16_t;
+typedef unsigned short uint16_t;
+typedef int int32_t;
+typedef unsigned int uint32_t;
+typedef __int64 int64_t;
+typedef unsigned __int64 uint64_t;
+#define ntohl(x) _byteswap_ulong (x)
+#define ntohs(x) _byteswap_ushort (x)
+#define htonl(x) _byteswap_ulong (x)
+#define htons(x) _byteswap_ushort (x)
+#else
+#include <arpa/inet.h>
+#include <stdint.h>
+#endif
+
+#if defined(_MSC_VER) || !defined(OTS_DEBUG)
+#define OTS_FAILURE() false
+#else
+#define OTS_FAILURE() ots::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);
+  return false;
+}
+#endif
+
+// -----------------------------------------------------------------------------
+// Buffer helper class
+//
+// This class perform some trival buffer operations while checking for
+// out-of-bounds errors. As a family they return false if anything is amiss,
+// updating the current offset otherwise.
+// -----------------------------------------------------------------------------
+class Buffer {
+ public:
+  Buffer(const uint8_t *buffer, size_t len)
+      : buffer_(buffer),
+        length_(len),
+        offset_(0) { }
+
+  bool Skip(size_t n_bytes) {
+    return Read(NULL, n_bytes);
+  }
+
+  bool Read(uint8_t *buffer, size_t n_bytes) {
+    if (n_bytes > 1024 * 1024 * 1024) {
+      return OTS_FAILURE();
+    }
+    if ((offset_ + n_bytes > length_) ||
+        (offset_ > length_ - n_bytes)) {
+      return OTS_FAILURE();
+    }
+    if (buffer) {
+      std::memcpy(buffer, buffer_ + offset_, n_bytes);
+    }
+    offset_ += n_bytes;
+    return true;
+  }
+
+  inline bool ReadU8(uint8_t *value) {
+    if (offset_ + 1 > length_) {
+      return OTS_FAILURE();
+    }
+    *value = buffer_[offset_];
+    ++offset_;
+    return true;
+  }
+
+  bool ReadU16(uint16_t *value) {
+    if (offset_ + 2 > length_) {
+      return OTS_FAILURE();
+    }
+    std::memcpy(value, buffer_ + offset_, sizeof(uint16_t));
+    *value = ntohs(*value);
+    offset_ += 2;
+    return true;
+  }
+
+  bool ReadS16(int16_t *value) {
+    return ReadU16(reinterpret_cast<uint16_t*>(value));
+  }
+
+  bool ReadU24(uint32_t *value) {
+    if (offset_ + 3 > length_) {
+      return OTS_FAILURE();
+    }
+    *value = static_cast<uint32_t>(buffer_[offset_]) << 16 |
+        static_cast<uint32_t>(buffer_[offset_ + 1]) << 8 |
+        static_cast<uint32_t>(buffer_[offset_ + 2]);
+    offset_ += 3;
+    return true;
+  }
+
+  bool ReadU32(uint32_t *value) {
+    if (offset_ + 4 > length_) {
+      return OTS_FAILURE();
+    }
+    std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
+    *value = ntohl(*value);
+    offset_ += 4;
+    return true;
+  }
+
+  bool ReadS32(int32_t *value) {
+    return ReadU32(reinterpret_cast<uint32_t*>(value));
+  }
+
+  bool ReadTag(uint32_t *value) {
+    if (offset_ + 4 > length_) {
+      return OTS_FAILURE();
+    }
+    std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
+    offset_ += 4;
+    return true;
+  }
+
+  bool ReadR64(uint64_t *value) {
+    if (offset_ + 8 > length_) {
+      return OTS_FAILURE();
+    }
+    std::memcpy(value, buffer_ + offset_, sizeof(uint64_t));
+    offset_ += 8;
+    return true;
+  }
+
+  const uint8_t *buffer() const { return buffer_; }
+  size_t offset() const { return offset_; }
+  size_t length() const { return length_; }
+
+  void set_offset(size_t newoffset) { offset_ = newoffset; }
+
+ private:
+  const uint8_t * const buffer_;
+  const size_t length_;
+  size_t offset_;
+};
+
+}  // namespace ots
+
+#endif  // BROTLI_WOFF2_OTS_H_
diff --git a/woff2/port.h b/woff2/port.h
new file mode 100644
index 0000000..e7a2708
--- /dev/null
+++ b/woff2/port.h
@@ -0,0 +1,46 @@
+// 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
+
+#ifndef BROTLI_WOFF2_PORT_H_
+#define BROTLI_WOFF2_PORT_H_
+
+namespace woff2 {
+
+typedef unsigned int       uint32;
+
+inline int Log2Floor(uint32 n) {
+#if defined(__GNUC__)
+  return n == 0 ? -1 : 31 ^ __builtin_clz(n);
+#else
+  if (n == 0)
+    return -1;
+  int log = 0;
+  uint32 value = n;
+  for (int i = 4; i >= 0; --i) {
+    int shift = (1 << i);
+    uint32 x = value >> shift;
+    if (x != 0) {
+      value = x;
+      log += shift;
+    }
+  }
+  assert(value == 1);
+  return log;
+#endif
+}
+
+} // namespace woff2
+#endif  // BROTLI_WOFF2_PORT_H_
diff --git a/woff2/round.h b/woff2/round.h
new file mode 100644
index 0000000..4d88862
--- /dev/null
+++ b/woff2/round.h
@@ -0,0 +1,33 @@
+// 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
+
+#ifndef BROTLI_WOFF2_ROUND_H_
+#define BROTLI_WOFF2_ROUND_H_
+
+namespace woff2 {
+
+// Round a value up to the nearest multiple of 4. Don't round the value in the
+// case that rounding up overflows.
+template<typename T> T Round4(T value) {
+  if (std::numeric_limits<T>::max() - value < 3) {
+    return value;
+  }
+  return (value + 3) & ~3;
+}
+
+} // namespace woff2
+
+#endif  // BROTLI_WOFF2_ROUND_H_
diff --git a/woff2/store_bytes.h b/woff2/store_bytes.h
new file mode 100644
index 0000000..37054b2
--- /dev/null
+++ b/woff2/store_bytes.h
@@ -0,0 +1,61 @@
+// 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.
+
+#ifndef BROTLI_WOFF2_STORE_BYTES_H_
+#define BROTLI_WOFF2_STORE_BYTES_H_
+
+#include <inttypes.h>
+#include <stddef.h>
+#include <string.h>
+
+namespace woff2 {
+
+inline size_t StoreU32(uint8_t* dst, size_t offset, uint32_t x) {
+  dst[offset] = x >> 24;
+  dst[offset + 1] = x >> 16;
+  dst[offset + 2] = x >> 8;
+  dst[offset + 3] = x;
+  return offset + 4;
+}
+
+inline size_t Store16(uint8_t* dst, size_t offset, int x) {
+  dst[offset] = x >> 8;
+  dst[offset + 1] = x;
+  return offset + 2;
+}
+
+inline void StoreU32(uint32_t val, size_t* offset, uint8_t* dst) {
+  dst[(*offset)++] = val >> 24;
+  dst[(*offset)++] = val >> 16;
+  dst[(*offset)++] = val >> 8;
+  dst[(*offset)++] = val;
+}
+
+inline void Store16(int val, size_t* offset, uint8_t* dst) {
+  dst[(*offset)++] = val >> 8;
+  dst[(*offset)++] = val;
+}
+
+inline void StoreBytes(const uint8_t* data, size_t len,
+                       size_t* offset, uint8_t* dst) {
+  memcpy(&dst[*offset], data, len);
+  *offset += len;
+}
+
+} // namespace woff2
+
+#endif  // BROTLI_WOFF2_STORE_BYTES_H_
diff --git a/woff2/transform.cc b/woff2/transform.cc
new file mode 100644
index 0000000..a218ed1
--- /dev/null
+++ b/woff2/transform.cc
@@ -0,0 +1,263 @@
+// 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.
+
+#include "./transform.h"
+
+#include <complex>  // for std::abs
+
+#include "./ots.h"
+#include "./font.h"
+#include "./glyph.h"
+
+namespace woff2 {
+
+namespace {
+
+const int FLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0;
+const int FLAG_WE_HAVE_INSTRUCTIONS = 1 << 8;
+
+void WriteBytes(std::vector<uint8_t>* out, const uint8_t* data, size_t len) {
+  if (len == 0) return;
+  size_t offset = out->size();
+  out->resize(offset + len);
+  memcpy(&(*out)[offset], data, len);
+}
+
+void WriteBytes(std::vector<uint8_t>* out, const std::vector<uint8_t>& in) {
+  for (int i = 0; i < in.size(); ++i) {
+    out->push_back(in[i]);
+  }
+}
+
+void WriteUShort(std::vector<uint8_t>* out, int value) {
+  out->push_back(value >> 8);
+  out->push_back(value & 255);
+}
+
+void WriteLong(std::vector<uint8_t>* out, int value) {
+  out->push_back((value >> 24) & 255);
+  out->push_back((value >> 16) & 255);
+  out->push_back((value >> 8) & 255);
+  out->push_back(value & 255);
+}
+
+void Write255UShort(std::vector<uint8_t>* out, int value) {
+  if (value < 253) {
+    out->push_back(value);
+  } else if (value < 506) {
+    out->push_back(255);
+    out->push_back(value - 253);
+  } else if (value < 762) {
+    out->push_back(254);
+    out->push_back(value - 506);
+  } else {
+    out->push_back(253);
+    out->push_back(value >> 8);
+    out->push_back(value & 0xff);
+  }
+}
+
+// Glyf table preprocessing, based on
+// GlyfEncoder.java
+// but only the "sbbox" and "cbbox" options are supported.
+class GlyfEncoder {
+ public:
+  explicit GlyfEncoder(int num_glyphs)
+      : sbbox_(false), cbbox_(true), n_glyphs_(num_glyphs) {
+    bbox_bitmap_.resize(((num_glyphs + 31) >> 5) << 2);
+  }
+
+  bool Encode(int glyph_id, const Glyph& glyph) {
+    if (glyph.composite_data_size > 0) {
+      WriteCompositeGlyph(glyph_id, glyph);
+    } else if (glyph.contours.size() > 0) {
+      WriteSimpleGlyph(glyph_id, glyph);
+    } else {
+      WriteUShort(&n_contour_stream_, 0);
+    }
+    return true;
+  }
+
+  void GetTransformedGlyfBytes(std::vector<uint8_t>* result) {
+    WriteLong(result, 0);  // version
+    WriteUShort(result, n_glyphs_);
+    WriteUShort(result, 0);  // index_format, will be set later
+    WriteLong(result, n_contour_stream_.size());
+    WriteLong(result, n_points_stream_.size());
+    WriteLong(result, flag_byte_stream_.size());
+    WriteLong(result, glyph_stream_.size());
+    WriteLong(result, composite_stream_.size());
+    WriteLong(result, bbox_bitmap_.size() + bbox_stream_.size());
+    WriteLong(result, instruction_stream_.size());
+    WriteBytes(result, n_contour_stream_);
+    WriteBytes(result, n_points_stream_);
+    WriteBytes(result, flag_byte_stream_);
+    WriteBytes(result, glyph_stream_);
+    WriteBytes(result, composite_stream_);
+    WriteBytes(result, bbox_bitmap_);
+    WriteBytes(result, bbox_stream_);
+    WriteBytes(result, instruction_stream_);
+  }
+
+ private:
+  void WriteInstructions(const Glyph& glyph) {
+    Write255UShort(&glyph_stream_, glyph.instructions_size);
+    WriteBytes(&instruction_stream_,
+               glyph.instructions_data, glyph.instructions_size);
+  }
+
+  void WriteSimpleGlyph(int glyph_id, const Glyph& glyph) {
+    int num_contours = glyph.contours.size();
+    WriteUShort(&n_contour_stream_, num_contours);
+    if (sbbox_) {
+      WriteBbox(glyph_id, glyph);
+    }
+    // TODO: check that bbox matches, write bbox if not
+    for (int i = 0; i < num_contours; i++) {
+      Write255UShort(&n_points_stream_, glyph.contours[i].size());
+    }
+    int lastX = 0;
+    int lastY = 0;
+    for (int i = 0; i < num_contours; i++) {
+      int num_points = glyph.contours[i].size();
+      for (int j = 0; j < num_points; j++) {
+        int x = glyph.contours[i][j].x;
+        int y = glyph.contours[i][j].y;
+        int dx = x - lastX;
+        int dy = y - lastY;
+        WriteTriplet(glyph.contours[i][j].on_curve, dx, dy);
+        lastX = x;
+        lastY = y;
+      }
+    }
+    if (num_contours > 0) {
+      WriteInstructions(glyph);
+    }
+  }
+
+  void WriteCompositeGlyph(int glyph_id, const Glyph& glyph) {
+    WriteUShort(&n_contour_stream_, -1);
+    if (cbbox_) {
+      WriteBbox(glyph_id, glyph);
+    }
+    WriteBytes(&composite_stream_,
+               glyph.composite_data,
+               glyph.composite_data_size);
+    if (glyph.have_instructions) {
+      WriteInstructions(glyph);
+    }
+  }
+
+  void WriteBbox(int glyph_id, const Glyph& glyph) {
+    bbox_bitmap_[glyph_id >> 3] |= 0x80 >> (glyph_id & 7);
+    WriteUShort(&bbox_stream_, glyph.x_min);
+    WriteUShort(&bbox_stream_, glyph.y_min);
+    WriteUShort(&bbox_stream_, glyph.x_max);
+    WriteUShort(&bbox_stream_, glyph.y_max);
+  }
+
+  void WriteTriplet(bool on_curve, int x, int y) {
+    int abs_x = std::abs(x);
+    int abs_y = std::abs(y);
+    int on_curve_bit = on_curve ? 0 : 128;
+    int x_sign_bit = (x < 0) ? 0 : 1;
+    int y_sign_bit = (y < 0) ? 0 : 1;
+    int xy_sign_bits = x_sign_bit + 2 * y_sign_bit;
+    if (x == 0 && abs_y < 1280) {
+      flag_byte_stream_.push_back(on_curve_bit +
+                                  ((abs_y & 0xf00) >> 7) + y_sign_bit);
+      glyph_stream_.push_back(abs_y & 0xff);
+    } else if (y == 0 && abs_x < 1280) {
+      flag_byte_stream_.push_back(on_curve_bit + 10 +
+                                  ((abs_x & 0xf00) >> 7) + x_sign_bit);
+      glyph_stream_.push_back(abs_x & 0xff);
+    } else if (abs_x < 65 && abs_y < 65) {
+      flag_byte_stream_.push_back(on_curve_bit + 20 +
+                                  ((abs_x - 1) & 0x30) +
+                                  (((abs_y - 1) & 0x30) >> 2) +
+                                  xy_sign_bits);
+      glyph_stream_.push_back((((abs_x - 1) & 0xf) << 4) | ((abs_y - 1) & 0xf));
+    } else if (abs_x < 769 && abs_y < 769) {
+      flag_byte_stream_.push_back(on_curve_bit + 84 +
+                                  12 * (((abs_x - 1) & 0x300) >> 8) +
+                                  (((abs_y - 1) & 0x300) >> 6) + xy_sign_bits);
+      glyph_stream_.push_back((abs_x - 1) & 0xff);
+      glyph_stream_.push_back((abs_y - 1) & 0xff);
+    } else if (abs_x < 4096 && abs_y < 4096) {
+      flag_byte_stream_.push_back(on_curve_bit + 120 + xy_sign_bits);
+      glyph_stream_.push_back(abs_x >> 4);
+      glyph_stream_.push_back(((abs_x & 0xf) << 4) | (abs_y >> 8));
+      glyph_stream_.push_back(abs_y & 0xff);
+    } else {
+      flag_byte_stream_.push_back(on_curve_bit + 124 + xy_sign_bits);
+      glyph_stream_.push_back(abs_x >> 8);
+      glyph_stream_.push_back(abs_x & 0xff);
+      glyph_stream_.push_back(abs_y >> 8);
+      glyph_stream_.push_back(abs_y & 0xff);
+    }
+  }
+
+  std::vector<uint8_t> n_contour_stream_;
+  std::vector<uint8_t> n_points_stream_;
+  std::vector<uint8_t> flag_byte_stream_;
+  std::vector<uint8_t> composite_stream_;
+  std::vector<uint8_t> bbox_bitmap_;
+  std::vector<uint8_t> bbox_stream_;
+  std::vector<uint8_t> glyph_stream_;
+  std::vector<uint8_t> instruction_stream_;
+  bool sbbox_;
+  bool cbbox_;
+  int n_glyphs_;
+};
+
+}  // namespace
+
+bool TransformGlyfAndLocaTables(Font* font) {
+  Font::Table* transformed_glyf = &font->tables[kGlyfTableTag ^ 0x80808080];
+  Font::Table* transformed_loca = &font->tables[kLocaTableTag ^ 0x80808080];
+
+  int num_glyphs = NumGlyphs(*font);
+  GlyfEncoder encoder(num_glyphs);
+  for (int i = 0; i < num_glyphs; ++i) {
+    Glyph glyph;
+    const uint8_t* glyph_data;
+    size_t glyph_size;
+    if (!GetGlyphData(*font, i, &glyph_data, &glyph_size) ||
+        (glyph_size > 0 && !ReadGlyph(glyph_data, glyph_size, &glyph))) {
+      return OTS_FAILURE();
+    }
+    encoder.Encode(i, glyph);
+  }
+  encoder.GetTransformedGlyfBytes(&transformed_glyf->buffer);
+
+  const Font::Table* head_table = font->FindTable(kHeadTableTag);
+  if (head_table == NULL || head_table->length < 52) {
+    return OTS_FAILURE();
+  }
+  transformed_glyf->buffer[7] = head_table->data[51];  // index_format
+
+  transformed_glyf->tag = kGlyfTableTag ^ 0x80808080;
+  transformed_glyf->length = transformed_glyf->buffer.size();
+  transformed_glyf->data = transformed_glyf->buffer.data();
+
+  transformed_loca->tag = kLocaTableTag ^ 0x80808080;
+  transformed_loca->length = 0;
+  transformed_loca->data = NULL;
+
+  return true;
+}
+
+} // namespace woff2
diff --git a/woff2/transform.h b/woff2/transform.h
new file mode 100644
index 0000000..dd63e73
--- /dev/null
+++ b/woff2/transform.h
@@ -0,0 +1,31 @@
+// 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.
+
+#ifndef BROTLI_WOFF2_TRANSFORM_H_
+#define BROTLI_WOFF2_TRANSFORM_H_
+
+#include "./font.h"
+
+namespace woff2 {
+
+// Adds the transformed versions of the glyf and loca tables to the font. The
+// transformed loca table has zero length. The tag of the transformed tables is
+// derived from the original tag by flipping the MSBs of every byte.
+bool TransformGlyfAndLocaTables(Font* font);
+
+} // namespace woff2
+
+#endif  // BROTLI_WOFF2_TRANSFORM_H_
diff --git a/woff2/woff2.cc b/woff2/woff2.cc
new file mode 100644
index 0000000..43e0861
--- /dev/null
+++ b/woff2/woff2.cc
@@ -0,0 +1,1313 @@
+// 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 converting WOFF2 format font files to their TTF versions.
+
+#include "./woff2.h"
+
+#include <stdlib.h>
+#include <complex>
+#include <cstring>
+#include <limits>
+#include <string>
+#include <vector>
+
+#include "./ots.h"
+#include "./decode.h"
+#include "./encode.h"
+#include "./font.h"
+#include "./normalize.h"
+#include "./round.h"
+#include "./store_bytes.h"
+#include "./transform.h"
+
+namespace woff2 {
+
+namespace {
+
+using std::string;
+using std::vector;
+
+
+
+// simple glyph flags
+const int kGlyfOnCurve = 1 << 0;
+const int kGlyfXShort = 1 << 1;
+const int kGlyfYShort = 1 << 2;
+const int kGlyfRepeat = 1 << 3;
+const int kGlyfThisXIsSame = 1 << 4;
+const int kGlyfThisYIsSame = 1 << 5;
+
+// composite glyph flags
+const int FLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0;
+const int FLAG_ARGS_ARE_XY_VALUES = 1 << 1;
+const int FLAG_ROUND_XY_TO_GRID = 1 << 2;
+const int FLAG_WE_HAVE_A_SCALE = 1 << 3;
+const int FLAG_RESERVED = 1 << 4;
+const int FLAG_MORE_COMPONENTS = 1 << 5;
+const int FLAG_WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6;
+const int FLAG_WE_HAVE_A_TWO_BY_TWO = 1 << 7;
+const int FLAG_WE_HAVE_INSTRUCTIONS = 1 << 8;
+const int FLAG_USE_MY_METRICS = 1 << 9;
+const int FLAG_OVERLAP_COMPOUND = 1 << 10;
+const int FLAG_SCALED_COMPONENT_OFFSET = 1 << 11;
+const int FLAG_UNSCALED_COMPONENT_OFFSET = 1 << 12;
+
+const size_t kSfntHeaderSize = 12;
+const size_t kSfntEntrySize = 16;
+const size_t kCheckSumAdjustmentOffset = 8;
+
+const size_t kEndPtsOfContoursOffset = 10;
+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)
+
+const uint32_t kWoff2Signature = 0x774f4632;  // "wOF2"
+
+const unsigned int kWoff2FlagsContinueStream = 1 << 4;
+const unsigned int kWoff2FlagsTransform = 1 << 5;
+
+const size_t kWoff2HeaderSize = 44;
+const size_t kWoff2EntrySize = 20;
+
+const size_t kLzmaHeaderSize = 13;
+
+// Compression type values common to both short and long formats
+const uint32_t kCompressionTypeMask = 0xf;
+const uint32_t kCompressionTypeNone = 0;
+const uint32_t kCompressionTypeGzip = 1;
+const uint32_t kCompressionTypeLzma = 2;
+const uint32_t kCompressionTypeBrotli = 3;
+const uint32_t kCompressionTypeLzham = 4;
+
+// This is a special value for the short format only, as described in
+// "Design for compressed header format" in draft doc.
+const uint32_t kShortFlagsContinue = 3;
+
+struct Point {
+  int x;
+  int y;
+  bool on_curve;
+};
+
+struct Table {
+  uint32_t tag;
+  uint32_t flags;
+  uint32_t src_offset;
+  uint32_t src_length;
+
+  uint32_t transform_length;
+
+  uint32_t dst_offset;
+  uint32_t dst_length;
+  const uint8_t* dst_data;
+};
+
+// Based on section 6.1.1 of MicroType Express draft spec
+bool Read255UShort(ots::Buffer* buf, unsigned int* value) {
+  static const int kWordCode = 253;
+  static const int kOneMoreByteCode2 = 254;
+  static const int kOneMoreByteCode1 = 255;
+  static const int kLowestUCode = 253;
+  uint8_t code = 0;
+  if (!buf->ReadU8(&code)) {
+    return OTS_FAILURE();
+  }
+  if (code == kWordCode) {
+    uint16_t result = 0;
+    if (!buf->ReadU16(&result)) {
+      return OTS_FAILURE();
+    }
+    *value = result;
+    return true;
+  } else if (code == kOneMoreByteCode1) {
+    uint8_t result = 0;
+    if (!buf->ReadU8(&result)) {
+      return OTS_FAILURE();
+    }
+    *value = result + kLowestUCode;
+    return true;
+  } else if (code == kOneMoreByteCode2) {
+    uint8_t result = 0;
+    if (!buf->ReadU8(&result)) {
+      return OTS_FAILURE();
+    }
+    *value = result + kLowestUCode * 2;
+    return true;
+  } else {
+    *value = code;
+    return true;
+  }
+}
+
+bool ReadBase128(ots::Buffer* buf, uint32_t* value) {
+  uint32_t result = 0;
+  for (size_t i = 0; i < 5; ++i) {
+    uint8_t code = 0;
+    if (!buf->ReadU8(&code)) {
+      return OTS_FAILURE();
+    }
+    // If any of the top seven bits are set then we're about to overflow.
+    if (result & 0xe0000000) {
+      return OTS_FAILURE();
+    }
+    result = (result << 7) | (code & 0x7f);
+    if ((code & 0x80) == 0) {
+      *value = result;
+      return true;
+    }
+  }
+  // Make sure not to exceed the size bound
+  return OTS_FAILURE();
+}
+
+size_t Base128Size(size_t n) {
+  size_t size = 1;
+  for (; n >= 128; n >>= 7) ++size;
+  return size;
+}
+
+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;
+    if (i < size - 1) {
+      b |= 0x80;
+    }
+    dst[(*offset)++] = b;
+  }
+}
+
+int WithSign(int flag, int baseval) {
+  // Precondition: 0 <= baseval < 65536 (to avoid integer overflow)
+  return (flag & 1) ? baseval : -baseval;
+}
+
+bool TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size,
+    unsigned int n_points, std::vector<Point>* result,
+    size_t* in_bytes_consumed) {
+  int x = 0;
+  int y = 0;
+
+  if (n_points > in_size) {
+    return OTS_FAILURE();
+  }
+  unsigned int triplet_index = 0;
+
+  for (unsigned int i = 0; i < n_points; ++i) {
+    uint8_t flag = flags_in[i];
+    bool on_curve = !(flag >> 7);
+    flag &= 0x7f;
+    unsigned int n_data_bytes;
+    if (flag < 84) {
+      n_data_bytes = 1;
+    } else if (flag < 120) {
+      n_data_bytes = 2;
+    } else if (flag < 124) {
+      n_data_bytes = 3;
+    } else {
+      n_data_bytes = 4;
+    }
+    if (triplet_index + n_data_bytes > in_size ||
+        triplet_index + n_data_bytes < triplet_index) {
+      return OTS_FAILURE();
+    }
+    int dx, dy;
+    if (flag < 10) {
+      dx = 0;
+      dy = WithSign(flag, ((flag & 14) << 7) + in[triplet_index]);
+    } else if (flag < 20) {
+      dx = WithSign(flag, (((flag - 10) & 14) << 7) + in[triplet_index]);
+      dy = 0;
+    } else if (flag < 84) {
+      int b0 = flag - 20;
+      int b1 = in[triplet_index];
+      dx = WithSign(flag, 1 + (b0 & 0x30) + (b1 >> 4));
+      dy = WithSign(flag >> 1, 1 + ((b0 & 0x0c) << 2) + (b1 & 0x0f));
+    } else if (flag < 120) {
+      int b0 = flag - 84;
+      dx = WithSign(flag, 1 + ((b0 / 12) << 8) + in[triplet_index]);
+      dy = WithSign(flag >> 1,
+                    1 + (((b0 % 12) >> 2) << 8) + in[triplet_index + 1]);
+    } else if (flag < 124) {
+      int b2 = in[triplet_index + 1];
+      dx = WithSign(flag, (in[triplet_index] << 4) + (b2 >> 4));
+      dy = WithSign(flag >> 1, ((b2 & 0x0f) << 8) + in[triplet_index + 2]);
+    } else {
+      dx = WithSign(flag, (in[triplet_index] << 8) + in[triplet_index + 1]);
+      dy = WithSign(flag >> 1,
+          (in[triplet_index + 2] << 8) + in[triplet_index + 3]);
+    }
+    triplet_index += n_data_bytes;
+    // Possible overflow but coordinate values are not security sensitive
+    x += dx;
+    y += dy;
+    result->push_back(Point());
+    Point& back = result->back();
+    back.x = x;
+    back.y = y;
+    back.on_curve = on_curve;
+  }
+  *in_bytes_consumed = triplet_index;
+  return true;
+}
+
+// This function stores just the point data. On entry, dst points to the
+// beginning of a simple glyph. Returns true on success.
+bool StorePoints(const std::vector<Point>& points,
+    unsigned int n_contours, unsigned int instruction_length,
+    uint8_t* dst, size_t dst_size, size_t* glyph_size) {
+  // I believe that n_contours < 65536, in which case this is safe. However, a
+  // comment and/or an assert would be good.
+  unsigned int flag_offset = kEndPtsOfContoursOffset + 2 * n_contours + 2 +
+    instruction_length;
+  int last_flag = -1;
+  int repeat_count = 0;
+  int last_x = 0;
+  int last_y = 0;
+  unsigned int x_bytes = 0;
+  unsigned int y_bytes = 0;
+
+  for (unsigned int i = 0; i < points.size(); ++i) {
+    const Point& point = points[i];
+    int flag = point.on_curve ? kGlyfOnCurve : 0;
+    int dx = point.x - last_x;
+    int dy = point.y - last_y;
+    if (dx == 0) {
+      flag |= kGlyfThisXIsSame;
+    } else if (dx > -256 && dx < 256) {
+      flag |= kGlyfXShort | (dx > 0 ? kGlyfThisXIsSame : 0);
+      x_bytes += 1;
+    } else {
+      x_bytes += 2;
+    }
+    if (dy == 0) {
+      flag |= kGlyfThisYIsSame;
+    } else if (dy > -256 && dy < 256) {
+      flag |= kGlyfYShort | (dy > 0 ? kGlyfThisYIsSame : 0);
+      y_bytes += 1;
+    } else {
+      y_bytes += 2;
+    }
+
+    if (flag == last_flag && repeat_count != 255) {
+      dst[flag_offset - 1] |= kGlyfRepeat;
+      repeat_count++;
+    } else {
+      if (repeat_count != 0) {
+        if (flag_offset >= dst_size) {
+          return OTS_FAILURE();
+        }
+        dst[flag_offset++] = repeat_count;
+      }
+      if (flag_offset >= dst_size) {
+        return OTS_FAILURE();
+      }
+      dst[flag_offset++] = flag;
+      repeat_count = 0;
+    }
+    last_x = point.x;
+    last_y = point.y;
+    last_flag = flag;
+  }
+
+  if (repeat_count != 0) {
+    if (flag_offset >= dst_size) {
+      return OTS_FAILURE();
+    }
+    dst[flag_offset++] = repeat_count;
+  }
+  unsigned int xy_bytes = x_bytes + y_bytes;
+  if (xy_bytes < x_bytes ||
+      flag_offset + xy_bytes < flag_offset ||
+      flag_offset + xy_bytes > dst_size) {
+    return OTS_FAILURE();
+  }
+
+  int x_offset = flag_offset;
+  int y_offset = flag_offset + x_bytes;
+  last_x = 0;
+  last_y = 0;
+  for (unsigned int i = 0; i < points.size(); ++i) {
+    int dx = points[i].x - last_x;
+    if (dx == 0) {
+      // pass
+    } else if (dx > -256 && dx < 256) {
+      dst[x_offset++] = std::abs(dx);
+    } else {
+      // will always fit for valid input, but overflow is harmless
+      x_offset = Store16(dst, x_offset, dx);
+    }
+    last_x += dx;
+    int dy = points[i].y - last_y;
+    if (dy == 0) {
+      // pass
+    } else if (dy > -256 && dy < 256) {
+      dst[y_offset++] = std::abs(dy);
+    } else {
+      y_offset = Store16(dst, y_offset, dy);
+    }
+    last_y += dy;
+  }
+  *glyph_size = y_offset;
+  return true;
+}
+
+// Compute the bounding box of the coordinates, and store into a glyf buffer.
+// A precondition is that there are at least 10 bytes available.
+void ComputeBbox(const std::vector<Point>& points, uint8_t* dst) {
+  int x_min = 0;
+  int y_min = 0;
+  int x_max = 0;
+  int y_max = 0;
+
+  for (unsigned int i = 0; i < points.size(); ++i) {
+    int x = points[i].x;
+    int y = points[i].y;
+    if (i == 0 || x < x_min) x_min = x;
+    if (i == 0 || x > x_max) x_max = x;
+    if (i == 0 || y < y_min) y_min = y;
+    if (i == 0 || y > y_max) y_max = y;
+  }
+  size_t offset = 2;
+  offset = Store16(dst, offset, x_min);
+  offset = Store16(dst, offset, y_min);
+  offset = Store16(dst, offset, x_max);
+  offset = Store16(dst, offset, y_max);
+}
+
+// Process entire bbox stream. This is done as a separate pass to allow for
+// composite bbox computations (an optional more aggressive transform).
+bool ProcessBboxStream(ots::Buffer* bbox_stream, unsigned int n_glyphs,
+    const std::vector<uint32_t>& loca_values, uint8_t* glyf_buf,
+    size_t glyf_buf_length) {
+  const uint8_t* buf = bbox_stream->buffer();
+  if (n_glyphs >= 65536 || loca_values.size() != n_glyphs + 1) {
+    return OTS_FAILURE();
+  }
+  // Safe because n_glyphs is bounded
+  unsigned int bitmap_length = ((n_glyphs + 31) >> 5) << 2;
+  if (!bbox_stream->Skip(bitmap_length)) {
+    return OTS_FAILURE();
+  }
+  for (unsigned int i = 0; i < n_glyphs; ++i) {
+    if (buf[i >> 3] & (0x80 >> (i & 7))) {
+      uint32_t loca_offset = loca_values[i];
+      if (loca_values[i + 1] - loca_offset < kEndPtsOfContoursOffset) {
+        return OTS_FAILURE();
+      }
+      if (glyf_buf_length < 2 + 10 ||
+          loca_offset > glyf_buf_length - 2 - 10) {
+        return OTS_FAILURE();
+      }
+      if (!bbox_stream->Read(glyf_buf + loca_offset + 2, 8)) {
+        return OTS_FAILURE();
+      }
+    }
+  }
+  return true;
+}
+
+bool ProcessComposite(ots::Buffer* composite_stream, uint8_t* dst,
+    size_t dst_size, size_t* glyph_size, bool* have_instructions) {
+  size_t start_offset = composite_stream->offset();
+  bool we_have_instructions = false;
+
+  uint16_t flags = FLAG_MORE_COMPONENTS;
+  while (flags & FLAG_MORE_COMPONENTS) {
+    if (!composite_stream->ReadU16(&flags)) {
+      return OTS_FAILURE();
+    }
+    we_have_instructions |= (flags & FLAG_WE_HAVE_INSTRUCTIONS) != 0;
+    size_t arg_size = 2;  // glyph index
+    if (flags & FLAG_ARG_1_AND_2_ARE_WORDS) {
+      arg_size += 4;
+    } else {
+      arg_size += 2;
+    }
+    if (flags & FLAG_WE_HAVE_A_SCALE) {
+      arg_size += 2;
+    } else if (flags & FLAG_WE_HAVE_AN_X_AND_Y_SCALE) {
+      arg_size += 4;
+    } else if (flags & FLAG_WE_HAVE_A_TWO_BY_TWO) {
+      arg_size += 8;
+    }
+    if (!composite_stream->Skip(arg_size)) {
+      return OTS_FAILURE();
+    }
+  }
+  size_t composite_glyph_size = composite_stream->offset() - start_offset;
+  if (composite_glyph_size + kCompositeGlyphBegin > dst_size) {
+    return OTS_FAILURE();
+  }
+  Store16(dst, 0, 0xffff);  // nContours = -1 for composite glyph
+  std::memcpy(dst + kCompositeGlyphBegin,
+      composite_stream->buffer() + start_offset,
+      composite_glyph_size);
+  *glyph_size = kCompositeGlyphBegin + composite_glyph_size;
+  *have_instructions = we_have_instructions;
+  return true;
+}
+
+// Build TrueType loca table
+bool StoreLoca(const std::vector<uint32_t>& loca_values, int index_format,
+    uint8_t* dst, size_t dst_size) {
+  const uint64_t loca_size = loca_values.size();
+  const uint64_t offset_size = index_format ? 4 : 2;
+  if ((loca_size << 2) >> 2 != loca_size) {
+    return OTS_FAILURE();
+  }
+  if (offset_size * loca_size > dst_size) {
+    return OTS_FAILURE();
+  }
+  size_t offset = 0;
+  for (size_t i = 0; i < loca_values.size(); ++i) {
+    uint32_t value = loca_values[i];
+    if (index_format) {
+      offset = StoreU32(dst, offset, value);
+    } else {
+      offset = Store16(dst, offset, value >> 1);
+    }
+  }
+  return true;
+}
+
+// Reconstruct entire glyf table based on transformed original
+bool ReconstructGlyf(const uint8_t* data, size_t data_size,
+    uint8_t* dst, size_t dst_size,
+    uint8_t* loca_buf, size_t loca_size) {
+  static const int kNumSubStreams = 7;
+  ots::Buffer file(data, data_size);
+  uint32_t version;
+  std::vector<std::pair<const uint8_t*, size_t> > substreams(kNumSubStreams);
+
+  if (!file.ReadU32(&version)) {
+    return OTS_FAILURE();
+  }
+  uint16_t num_glyphs;
+  uint16_t index_format;
+  if (!file.ReadU16(&num_glyphs) ||
+      !file.ReadU16(&index_format)) {
+    return OTS_FAILURE();
+  }
+  unsigned int offset = (2 + kNumSubStreams) * 4;
+  if (offset > data_size) {
+    return OTS_FAILURE();
+  }
+  // Invariant from here on: data_size >= offset
+  for (int i = 0; i < kNumSubStreams; ++i) {
+    uint32_t substream_size;
+    if (!file.ReadU32(&substream_size)) {
+      return OTS_FAILURE();
+    }
+    if (substream_size > data_size - offset) {
+      return OTS_FAILURE();
+    }
+    substreams[i] = std::make_pair(data + offset, substream_size);
+    offset += substream_size;
+  }
+  ots::Buffer n_contour_stream(substreams[0].first, substreams[0].second);
+  ots::Buffer n_points_stream(substreams[1].first, substreams[1].second);
+  ots::Buffer flag_stream(substreams[2].first, substreams[2].second);
+  ots::Buffer glyph_stream(substreams[3].first, substreams[3].second);
+  ots::Buffer composite_stream(substreams[4].first, substreams[4].second);
+  ots::Buffer bbox_stream(substreams[5].first, substreams[5].second);
+  ots::Buffer instruction_stream(substreams[6].first, substreams[6].second);
+
+  std::vector<uint32_t> loca_values(num_glyphs + 1);
+  std::vector<unsigned int> n_points_vec;
+  std::vector<Point> points;
+  uint32_t loca_offset = 0;
+  for (unsigned int i = 0; i < num_glyphs; ++i) {
+    size_t glyph_size = 0;
+    uint16_t n_contours = 0;
+    if (!n_contour_stream.ReadU16(&n_contours)) {
+      return OTS_FAILURE();
+    }
+    uint8_t* glyf_dst = dst + loca_offset;
+    size_t glyf_dst_size = dst_size - loca_offset;
+    if (n_contours == 0xffff) {
+      // composite glyph
+      bool have_instructions = false;
+      unsigned int instruction_size = 0;
+      if (!ProcessComposite(&composite_stream, glyf_dst, glyf_dst_size,
+            &glyph_size, &have_instructions)) {
+        return OTS_FAILURE();
+      }
+      if (have_instructions) {
+        if (!Read255UShort(&glyph_stream, &instruction_size)) {
+          return OTS_FAILURE();
+        }
+        if (instruction_size + 2 > glyf_dst_size - glyph_size) {
+          return OTS_FAILURE();
+        }
+        Store16(glyf_dst, glyph_size, instruction_size);
+        if (!instruction_stream.Read(glyf_dst + glyph_size + 2,
+              instruction_size)) {
+          return OTS_FAILURE();
+        }
+        glyph_size += instruction_size + 2;
+      }
+    } else if (n_contours > 0) {
+      // simple glyph
+      n_points_vec.clear();
+      points.clear();
+      unsigned int total_n_points = 0;
+      unsigned int n_points_contour;
+      for (unsigned int j = 0; j < n_contours; ++j) {
+        if (!Read255UShort(&n_points_stream, &n_points_contour)) {
+          return OTS_FAILURE();
+        }
+        n_points_vec.push_back(n_points_contour);
+        if (total_n_points + n_points_contour < total_n_points) {
+          return OTS_FAILURE();
+        }
+        total_n_points += n_points_contour;
+      }
+      unsigned int flag_size = total_n_points;
+      if (flag_size > flag_stream.length() - flag_stream.offset()) {
+        return OTS_FAILURE();
+      }
+      const uint8_t* flags_buf = flag_stream.buffer() + flag_stream.offset();
+      const uint8_t* triplet_buf = glyph_stream.buffer() +
+        glyph_stream.offset();
+      size_t triplet_size = glyph_stream.length() - glyph_stream.offset();
+      size_t triplet_bytes_consumed = 0;
+      if (!TripletDecode(flags_buf, triplet_buf, triplet_size, total_n_points,
+            &points, &triplet_bytes_consumed)) {
+        return OTS_FAILURE();
+      }
+      const uint32_t header_and_endpts_contours_size =
+          kEndPtsOfContoursOffset + 2 * n_contours;
+      if (glyf_dst_size < header_and_endpts_contours_size) {
+        return OTS_FAILURE();
+      }
+      Store16(glyf_dst, 0, n_contours);
+      ComputeBbox(points, glyf_dst);
+      size_t offset = kEndPtsOfContoursOffset;
+      int end_point = -1;
+      for (unsigned int contour_ix = 0; contour_ix < n_contours; ++contour_ix) {
+        end_point += n_points_vec[contour_ix];
+        if (end_point >= 65536) {
+          return OTS_FAILURE();
+        }
+        offset = Store16(glyf_dst, offset, end_point);
+      }
+      if (!flag_stream.Skip(flag_size)) {
+        return OTS_FAILURE();
+      }
+      if (!glyph_stream.Skip(triplet_bytes_consumed)) {
+        return OTS_FAILURE();
+      }
+      unsigned int instruction_size;
+      if (!Read255UShort(&glyph_stream, &instruction_size)) {
+        return OTS_FAILURE();
+      }
+      if (glyf_dst_size - header_and_endpts_contours_size <
+          instruction_size + 2) {
+        return OTS_FAILURE();
+      }
+      uint8_t* instruction_dst = glyf_dst + header_and_endpts_contours_size;
+      Store16(instruction_dst, 0, instruction_size);
+      if (!instruction_stream.Read(instruction_dst + 2, instruction_size)) {
+        return OTS_FAILURE();
+      }
+      if (!StorePoints(points, n_contours, instruction_size,
+            glyf_dst, glyf_dst_size, &glyph_size)) {
+        return OTS_FAILURE();
+      }
+    } else {
+      glyph_size = 0;
+    }
+    loca_values[i] = loca_offset;
+    if (glyph_size + 3 < glyph_size) {
+      return OTS_FAILURE();
+    }
+    glyph_size = Round4(glyph_size);
+    if (glyph_size > dst_size - loca_offset) {
+      // This shouldn't happen, but this test defensively maintains the
+      // invariant that loca_offset <= dst_size.
+      return OTS_FAILURE();
+    }
+    loca_offset += glyph_size;
+  }
+  loca_values[num_glyphs] = loca_offset;
+  if (!ProcessBboxStream(&bbox_stream, num_glyphs, loca_values,
+          dst, dst_size)) {
+    return OTS_FAILURE();
+  }
+  return StoreLoca(loca_values, index_format, loca_buf, loca_size);
+}
+
+// This is linear search, but could be changed to binary because we
+// do have a guarantee that the tables are sorted by tag. But the total
+// cpu time is expected to be very small in any case.
+const Table* FindTable(const std::vector<Table>& tables, uint32_t tag) {
+  size_t n_tables = tables.size();
+  for (size_t i = 0; i < n_tables; ++i) {
+    if (tables[i].tag == tag) {
+      return &tables[i];
+    }
+  }
+  return NULL;
+}
+
+bool ReconstructTransformed(const std::vector<Table>& tables, uint32_t tag,
+    const uint8_t* transformed_buf, size_t transformed_size,
+    uint8_t* dst, size_t dst_length) {
+  if (tag == TAG('g', 'l', 'y', 'f')) {
+    const Table* glyf_table = FindTable(tables, tag);
+    const Table* loca_table = FindTable(tables, TAG('l', 'o', 'c', 'a'));
+    if (glyf_table == NULL || loca_table == NULL) {
+      return OTS_FAILURE();
+    }
+    if (static_cast<uint64_t>(glyf_table->dst_offset + glyf_table->dst_length) >
+        dst_length) {
+      return OTS_FAILURE();
+    }
+    if (static_cast<uint64_t>(loca_table->dst_offset + loca_table->dst_length) >
+        dst_length) {
+      return OTS_FAILURE();
+    }
+    return ReconstructGlyf(transformed_buf, transformed_size,
+        dst + glyf_table->dst_offset, glyf_table->dst_length,
+        dst + loca_table->dst_offset, loca_table->dst_length);
+  } else if (tag == TAG('l', 'o', 'c', 'a')) {
+    // processing was already done by glyf table, but validate
+    if (!FindTable(tables, TAG('g', 'l', 'y', 'f'))) {
+      return OTS_FAILURE();
+    }
+  } else {
+    // transform for the tag is not known
+    return OTS_FAILURE();
+  }
+  return true;
+}
+
+uint32_t ComputeChecksum(const uint8_t* buf, size_t size) {
+  uint32_t checksum = 0;
+  for (size_t i = 0; i < size; i += 4) {
+    // We assume the addition is mod 2^32, which is valid because unsigned
+    checksum += (buf[i] << 24) | (buf[i + 1] << 16) |
+      (buf[i + 2] << 8) | buf[i + 3];
+  }
+  return checksum;
+}
+
+bool FixChecksums(const std::vector<Table>& tables, uint8_t* dst) {
+  const Table* head_table = FindTable(tables, TAG('h', 'e', 'a', 'd'));
+  if (head_table == NULL ||
+      head_table->dst_length < kCheckSumAdjustmentOffset + 4) {
+    return OTS_FAILURE();
+  }
+  size_t adjustment_offset = head_table->dst_offset + kCheckSumAdjustmentOffset;
+  StoreU32(dst, adjustment_offset, 0);
+  size_t n_tables = tables.size();
+  uint32_t file_checksum = 0;
+  for (size_t i = 0; i < n_tables; ++i) {
+    const Table* table = &tables[i];
+    size_t table_length = table->dst_length;
+    uint8_t* table_data = dst + table->dst_offset;
+    uint32_t checksum = ComputeChecksum(table_data, table_length);
+    StoreU32(dst, kSfntHeaderSize + i * kSfntEntrySize + 4, checksum);
+    file_checksum += checksum;
+  }
+  file_checksum += ComputeChecksum(dst,
+      kSfntHeaderSize + kSfntEntrySize * n_tables);
+  uint32_t checksum_adjustment = 0xb1b0afba - file_checksum;
+  StoreU32(dst, adjustment_offset, checksum_adjustment);
+  return true;
+}
+
+bool Woff2Compress(const uint8_t* data, const size_t len,
+                   uint32_t compression_type,
+                   uint8_t* result, uint32_t* result_len) {
+  if (compression_type == kCompressionTypeBrotli) {
+    size_t compressed_len = *result_len;
+    
+    brotli::BrotliCompressBuffer(len, data, &compressed_len, result);
+    *result_len = compressed_len;
+    return true;
+  }
+  return false;
+}
+
+bool Woff2Uncompress(uint8_t* dst_buf, size_t dst_size,
+    const uint8_t* src_buf, size_t src_size, uint32_t compression_type) {
+  if (compression_type == kCompressionTypeBrotli) {
+    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;
+  }
+  // Unknown compression type
+  return OTS_FAILURE();
+}
+
+bool ReadLongDirectory(ots::Buffer* file, std::vector<Table>* tables,
+    size_t num_tables) {
+  for (size_t i = 0; i < num_tables; ++i) {
+    Table* table = &(*tables)[i];
+    if (!file->ReadU32(&table->tag) ||
+        !file->ReadU32(&table->flags) ||
+        !file->ReadU32(&table->src_length) ||
+        !file->ReadU32(&table->transform_length) ||
+        !file->ReadU32(&table->dst_length)) {
+      return OTS_FAILURE();
+    }
+  }
+  return true;
+}
+
+const uint32_t known_tags[29] = {
+  TAG('c', 'm', 'a', 'p'),  // 0
+  TAG('h', 'e', 'a', 'd'),  // 1
+  TAG('h', 'h', 'e', 'a'),  // 2
+  TAG('h', 'm', 't', 'x'),  // 3
+  TAG('m', 'a', 'x', 'p'),  // 4
+  TAG('n', 'a', 'm', 'e'),  // 5
+  TAG('O', 'S', '/', '2'),  // 6
+  TAG('p', 'o', 's', 't'),  // 7
+  TAG('c', 'v', 't', ' '),  // 8
+  TAG('f', 'p', 'g', 'm'),  // 9
+  TAG('g', 'l', 'y', 'f'),  // 10
+  TAG('l', 'o', 'c', 'a'),  // 11
+  TAG('p', 'r', 'e', 'p'),  // 12
+  TAG('C', 'F', 'F', ' '),  // 13
+  TAG('V', 'O', 'R', 'G'),  // 14
+  TAG('E', 'B', 'D', 'T'),  // 15
+  TAG('E', 'B', 'L', 'C'),  // 16
+  TAG('g', 'a', 's', 'p'),  // 17
+  TAG('h', 'd', 'm', 'x'),  // 18
+  TAG('k', 'e', 'r', 'n'),  // 19
+  TAG('L', 'T', 'S', 'H'),  // 20
+  TAG('P', 'C', 'L', 'T'),  // 21
+  TAG('V', 'D', 'M', 'X'),  // 22
+  TAG('v', 'h', 'e', 'a'),  // 23
+  TAG('v', 'm', 't', 'x'),  // 24
+  TAG('B', 'A', 'S', 'E'),  // 25
+  TAG('G', 'D', 'E', 'F'),  // 26
+  TAG('G', 'P', 'O', 'S'),  // 27
+  TAG('G', 'S', 'U', 'B'),  // 28
+};
+
+int KnownTableIndex(uint32_t tag) {
+  for (int i = 0; i < 29; ++i) {
+    if (tag == known_tags[i]) return i;
+  }
+  return 31;
+}
+
+bool ReadShortDirectory(ots::Buffer* file, std::vector<Table>* tables,
+    size_t num_tables) {
+  uint32_t last_compression_type = 0;
+  for (size_t i = 0; i < num_tables; ++i) {
+    Table* table = &(*tables)[i];
+    uint8_t flag_byte;
+    if (!file->ReadU8(&flag_byte)) {
+      return OTS_FAILURE();
+    }
+    uint32_t tag;
+    if ((flag_byte & 0x1f) == 0x1f) {
+      if (!file->ReadU32(&tag)) {
+        return OTS_FAILURE();
+      }
+    } else {
+      if ((flag_byte & 0x1f) >= (sizeof(known_tags) / sizeof(known_tags[0]))) {
+        return OTS_FAILURE();
+      }
+      tag = known_tags[flag_byte & 0x1f];
+    }
+    uint32_t flags = flag_byte >> 6;
+    if (flags == kShortFlagsContinue) {
+      flags = last_compression_type | kWoff2FlagsContinueStream;
+    } else {
+      if (flags == kCompressionTypeNone ||
+          flags == kCompressionTypeGzip ||
+          flags == kCompressionTypeLzma) {
+        last_compression_type = flags;
+      } else {
+        return OTS_FAILURE();
+      }
+    }
+    if ((flag_byte & 0x20) != 0) {
+      flags |= kWoff2FlagsTransform;
+    }
+    uint32_t dst_length;
+    if (!ReadBase128(file, &dst_length)) {
+      return OTS_FAILURE();
+    }
+    uint32_t transform_length = dst_length;
+    if ((flags & kWoff2FlagsTransform) != 0) {
+      if (!ReadBase128(file, &transform_length)) {
+        return OTS_FAILURE();
+      }
+    }
+    uint32_t src_length = transform_length;
+    if ((flag_byte >> 6) == 1 || (flag_byte >> 6) == 2) {
+      if (!ReadBase128(file, &src_length)) {
+        return OTS_FAILURE();
+      }
+    } else if ((flag_byte >> 6) == kShortFlagsContinue) {
+      // The compressed data for this table is in a previuos table, so we set
+      // the src_length to zero.
+      src_length = 0;
+    }
+    table->tag = tag;
+    table->flags = flags;
+    table->src_length = src_length;
+    table->transform_length = transform_length;
+    table->dst_length = dst_length;
+  }
+  return true;
+}
+
+}  // namespace
+
+size_t ComputeWOFF2FinalSize(const uint8_t* data, size_t length) {
+  ots::Buffer file(data, length);
+  uint32_t total_length;
+
+  if (!file.Skip(16) ||
+      !file.ReadU32(&total_length)) {
+    return 0;
+  }
+  return total_length;
+}
+
+bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length,
+                       const uint8_t* data, size_t length) {
+  ots::Buffer file(data, length);
+
+  uint32_t signature;
+  uint32_t flavor;
+  if (!file.ReadU32(&signature) || signature != kWoff2Signature ||
+      !file.ReadU32(&flavor)) {
+    return OTS_FAILURE();
+  }
+
+  // TODO(user): Should call IsValidVersionTag() here.
+
+  uint32_t reported_length;
+  if (!file.ReadU32(&reported_length) || length != reported_length) {
+    return OTS_FAILURE();
+  }
+  uint16_t num_tables;
+  if (!file.ReadU16(&num_tables) || !num_tables) {
+    return OTS_FAILURE();
+  }
+  // These reserved bits will be always zero in the final format, but they
+  // temporarily indicate the use of brotli, so that we can evaluate gzip, lzma
+  // and brotli side-by-side.
+  uint16_t reserved;
+  if (!file.ReadU16(&reserved)) {
+    return OTS_FAILURE();
+  }
+  // We don't care about these fields of the header:
+  //   uint32_t total_sfnt_size
+  //   uint16_t major_version, minor_version
+  //   uint32_t meta_offset, meta_length, meta_orig_length
+  //   uint32_t priv_offset, priv_length
+  if (!file.Skip(28)) {
+    return OTS_FAILURE();
+  }
+  std::vector<Table> tables(num_tables);
+  // Note: change below to ReadLongDirectory to enable long format.
+  if (!ReadShortDirectory(&file, &tables, num_tables)) {
+    return OTS_FAILURE();
+  }
+  uint64_t src_offset = file.offset();
+  uint64_t dst_offset = kSfntHeaderSize +
+      kSfntEntrySize * static_cast<uint64_t>(num_tables);
+  uint64_t uncompressed_sum = 0;
+  for (uint16_t i = 0; i < num_tables; ++i) {
+    Table* table = &tables[i];
+    table->src_offset = src_offset;
+    src_offset += table->src_length;
+    if (src_offset > std::numeric_limits<uint32_t>::max()) {
+      return OTS_FAILURE();
+    }
+    src_offset = Round4(src_offset);  // TODO: reconsider
+    table->dst_offset = dst_offset;
+    dst_offset += table->dst_length;
+    if (dst_offset > std::numeric_limits<uint32_t>::max()) {
+      return OTS_FAILURE();
+    }
+    dst_offset = Round4(dst_offset);
+    if ((table->flags & kCompressionTypeMask) != kCompressionTypeNone) {
+      uncompressed_sum += table->src_length;
+      if (uncompressed_sum > std::numeric_limits<uint32_t>::max()) {
+        return OTS_FAILURE();
+      }
+    }
+  }
+  // Enforce same 30M limit on uncompressed tables as OTS
+  if (uncompressed_sum > 30 * 1024 * 1024) {
+    return OTS_FAILURE();
+  }
+  if (src_offset > length || dst_offset > result_length) {
+    return OTS_FAILURE();
+  }
+
+  const uint32_t sfnt_header_and_table_directory_size = 12 + 16 * num_tables;
+  if (sfnt_header_and_table_directory_size > result_length) {
+    return OTS_FAILURE();
+  }
+
+  // Start building the font
+  size_t offset = 0;
+  offset = StoreU32(result, offset, flavor);
+  offset = Store16(result, offset, num_tables);
+  unsigned max_pow2 = 0;
+  while (1u << (max_pow2 + 1) <= num_tables) {
+    max_pow2++;
+  }
+  const uint16_t output_search_range = (1u << max_pow2) << 4;
+  offset = Store16(result, offset, output_search_range);
+  offset = Store16(result, offset, max_pow2);
+  offset = Store16(result, offset, (num_tables << 4) - output_search_range);
+  for (uint16_t i = 0; i < num_tables; ++i) {
+    const Table* table = &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);
+    offset = StoreU32(result, offset, table->dst_length);
+  }
+  std::vector<uint8_t> uncompressed_buf;
+  bool continue_valid = false;
+  const uint8_t* transform_buf = NULL;
+  for (uint16_t i = 0; i < num_tables; ++i) {
+    const Table* table = &tables[i];
+    uint32_t flags = table->flags;
+    const uint8_t* src_buf = data + table->src_offset;
+    uint32_t compression_type = flags & kCompressionTypeMask;
+    if (compression_type == kCompressionTypeLzma && reserved > 0) {
+      compression_type = kCompressionTypeLzma + reserved;
+    }
+    size_t transform_length = table->transform_length;
+    if ((flags & kWoff2FlagsContinueStream) != 0) {
+      if (!continue_valid) {
+        return OTS_FAILURE();
+      }
+    } else if (compression_type == kCompressionTypeNone) {
+      if (transform_length != table->src_length) {
+        return OTS_FAILURE();
+      }
+      transform_buf = src_buf;
+      continue_valid = false;
+    } else if ((flags & kWoff2FlagsContinueStream) == 0) {
+      uint64_t total_size = transform_length;
+      for (uint16_t j = i + 1; j < num_tables; ++j) {
+        if ((tables[j].flags & kWoff2FlagsContinueStream) == 0) {
+          break;
+        }
+        total_size += tables[j].transform_length;
+        if (total_size > std::numeric_limits<uint32_t>::max()) {
+          return OTS_FAILURE();
+        }
+      }
+      uncompressed_buf.resize(total_size);
+      if (!Woff2Uncompress(&uncompressed_buf[0], total_size,
+          src_buf, table->src_length, compression_type)) {
+        return OTS_FAILURE();
+      }
+      transform_buf = &uncompressed_buf[0];
+      continue_valid = true;
+    } else {
+      return OTS_FAILURE();
+    }
+
+    if ((flags & kWoff2FlagsTransform) == 0) {
+      if (transform_length != table->dst_length) {
+        return OTS_FAILURE();
+      }
+      if (static_cast<uint64_t>(table->dst_offset + transform_length) >
+          result_length) {
+        return OTS_FAILURE();
+      }
+      std::memcpy(result + table->dst_offset, transform_buf,
+          transform_length);
+    } else {
+      if (!ReconstructTransformed(tables, table->tag,
+            transform_buf, transform_length, result, result_length)) {
+        return OTS_FAILURE();
+      }
+    }
+    if (continue_valid) {
+      transform_buf += transform_length;
+      if (transform_buf > uncompressed_buf.data() + uncompressed_buf.size()) {
+        return OTS_FAILURE();
+      }
+    }
+  }
+
+  return FixChecksums(tables, result);
+}
+
+void StoreTableEntry(const Table& table, size_t* offset, uint8_t* dst) {
+  uint8_t flag_byte = KnownTableIndex(table.tag);
+  if ((table.flags & kWoff2FlagsTransform) != 0) {
+    flag_byte |= 0x20;
+  }
+  if ((table.flags & kWoff2FlagsContinueStream) != 0) {
+    flag_byte |= 0xc0;
+  } else {
+    flag_byte |= ((table.flags & 3) << 6);
+  }
+  dst[(*offset)++] = flag_byte;
+  if ((flag_byte & 0x1f) == 0x1f) {
+    StoreU32(table.tag, offset, dst);
+  }
+  StoreBase128(table.src_length, offset, dst);
+  if ((flag_byte & 0x20) != 0) {
+    StoreBase128(table.transform_length, offset, dst);
+  }
+  if ((flag_byte & 0xc0) == 0x40 || (flag_byte & 0xc0) == 0x80) {
+    StoreBase128(table.dst_length, offset, dst);
+  }
+}
+
+size_t TableEntrySize(const Table& table) {
+  size_t size = KnownTableIndex(table.tag) < 31 ? 1 : 5;
+  size += Base128Size(table.src_length);
+  if ((table.flags & kWoff2FlagsTransform) != 0) {
+    size += Base128Size(table.transform_length);
+  }
+  if ((table.flags & kWoff2FlagsContinueStream) == 0 &&
+      ((table.flags & 3) == kCompressionTypeGzip ||
+       (table.flags & 3) == kCompressionTypeLzma)) {
+    size += Base128Size(table.dst_length);
+  }
+  return size;
+}
+
+size_t ComputeWoff2Length(const std::vector<Table>& tables) {
+  size_t size = 44;  // header size
+  for (const auto& table : tables) {
+    size += TableEntrySize(table);
+  }
+  for (const auto& table : tables) {
+    size += table.dst_length;
+    size = Round4(size);
+  }
+  return size;
+}
+
+size_t ComputeTTFLength(const std::vector<Table>& tables) {
+  size_t size = 12 + 16 * tables.size();  // sfnt header
+  for (const auto& table : tables) {
+    size += Round4(table.src_length);
+  }
+  return size;
+}
+
+size_t ComputeTotalTransformLength(const Font& font) {
+  size_t total = 0;
+  for (const auto& i : font.tables) {
+    const Font::Table& table = i.second;
+    if (table.tag & 0x80808080 || !font.FindTable(table.tag ^ 0x80808080)) {
+      // Count transformed tables and non-transformed tables that do not have
+      // transformed versions.
+      total += table.length;
+    }
+  }
+  return total;
+}
+
+struct Woff2ConvertOptions {
+  uint32_t compression_type;
+  bool continue_streams;
+  bool keep_dsig;
+  bool transform_glyf;
+
+  Woff2ConvertOptions()
+      : compression_type(kCompressionTypeBrotli),
+        continue_streams(true),
+        keep_dsig(true),
+        transform_glyf(true) {}
+
+
+};
+
+size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length) {
+  // 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;
+}
+
+bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
+                       uint8_t *result, size_t *result_length) {
+
+  Woff2ConvertOptions options;
+
+  Font font;
+  if (!ReadFont(data, length, &font)) {
+    fprintf(stderr, "Parsing of the input font failed.\n");
+    return false;
+  }
+
+  if (!NormalizeFont(&font)) {
+    fprintf(stderr, "Font normalization failed.\n");
+    return false;
+  }
+
+  if (!options.keep_dsig) {
+    font.tables.erase(TAG('D', 'S', 'I', 'G'));
+  }
+
+  if (options.transform_glyf &&
+      !TransformGlyfAndLocaTables(&font)) {
+    fprintf(stderr, "Font transformation failed.\n");
+    return false;
+  }
+
+  const Font::Table* head_table = font.FindTable(kHeadTableTag);
+  if (head_table == NULL) {
+    fprintf(stderr, "Missing head table.\n");
+    return false;
+  }
+
+  // Although the compressed size of each table in the final woff2 file won't
+  // be larger than its transform_length, we have to allocate a large enough
+  // buffer for the compressor, since the compressor can potentially increase
+  // 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;
+  std::vector<uint8_t> compression_buf(compression_buffer_size);
+  size_t compression_buf_offset = 0;
+  uint32_t total_compressed_length = compression_buffer_size;
+
+  if (options.continue_streams) {
+    // Collect all transformed data into one place.
+    std::vector<uint8_t> transform_buf(total_transform_length);
+    size_t transform_offset = 0;
+    for (const auto& i : font.tables) {
+      if (i.second.tag & 0x80808080) continue;
+      const Font::Table* table = font.FindTable(i.second.tag ^ 0x80808080);
+      if (table == NULL) table = &i.second;
+      StoreBytes(table->data, table->length,
+                 &transform_offset, &transform_buf[0]);
+    }
+    // Compress all transformed data in one stream.
+    if (!Woff2Compress(transform_buf.data(), total_transform_length,
+                       options.compression_type,
+                       &compression_buf[0],
+                       &total_compressed_length)) {
+      fprintf(stderr, "Compression of combined table failed.\n");
+      return false;
+    }
+  }
+
+  std::vector<Table> tables;
+  for (const auto& i : font.tables) {
+    const Font::Table& src_table = i.second;
+    if (src_table.tag & 0x80808080) {
+      // This is a transformed table, we will write it together with the
+      // original version.
+      continue;
+    }
+    Table table;
+    table.tag = src_table.tag;
+    table.flags = std::min(options.compression_type, kCompressionTypeLzma);
+    table.src_length = src_table.length;
+    table.transform_length = src_table.length;
+    const uint8_t* transformed_data = src_table.data;
+    const Font::Table* transformed_table =
+        font.FindTable(src_table.tag ^ 0x80808080);
+    if (transformed_table != NULL) {
+      table.flags |= kWoff2FlagsTransform;
+      table.transform_length = transformed_table->length;
+      transformed_data = transformed_table->data;
+    }
+    if (options.continue_streams) {
+      if (tables.empty()) {
+        table.dst_length = total_compressed_length;
+        table.dst_data = &compression_buf[0];
+      } else {
+        table.dst_length = 0;
+        table.dst_data = NULL;
+        table.flags |= kWoff2FlagsContinueStream;
+      }
+    } else {
+      table.dst_length = table.transform_length;
+      table.dst_data = transformed_data;
+      if (options.compression_type != kCompressionTypeNone) {
+        uint32_t compressed_length =
+            compression_buf.size() - compression_buf_offset;
+        if (!Woff2Compress(transformed_data, table.transform_length,
+                           options.compression_type,
+                           &compression_buf[compression_buf_offset],
+                           &compressed_length)) {
+          fprintf(stderr, "Compression of table %x failed.\n", src_table.tag);
+          return false;
+        }
+        if (compressed_length >= table.transform_length) {
+          table.flags &= (~3);  // no compression
+        } else {
+          table.dst_length = compressed_length;
+          table.dst_data = &compression_buf[compression_buf_offset];
+          compression_buf_offset += table.dst_length;
+        }
+      }
+    }
+    tables.push_back(table);
+  }
+
+  size_t woff2_length = ComputeWoff2Length(tables);
+  if (woff2_length > *result_length) {
+    fprintf(stderr, "Result allocation was too small (%zd vs %zd bytes).\n",
+           *result_length, woff2_length);
+    return false;
+  }
+  *result_length = woff2_length;
+  uint16_t reserved =
+      (options.compression_type > kCompressionTypeLzma) ?
+      options.compression_type - kCompressionTypeLzma : 0;
+
+  size_t offset = 0;
+  StoreU32(kWoff2Signature, &offset, result);
+  StoreU32(font.flavor, &offset, result);
+  StoreU32(woff2_length, &offset, result);
+  Store16(tables.size(), &offset, result);
+  Store16(reserved, &offset, result);
+  StoreU32(ComputeTTFLength(tables), &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
+  StoreU32(0, &offset, result);  // privOffset
+  StoreU32(0, &offset, result);  // privLength
+  for (const auto& table : tables) {
+    StoreTableEntry(table, &offset, result);
+  }
+  for (const auto& table : tables) {
+    StoreBytes(table.dst_data, table.dst_length, &offset, result);
+    offset = Round4(offset);
+  }
+  if (*result_length != offset) {
+    fprintf(stderr, "Mismatch between computed and actual length "
+            "(%zd vs %zd)\n", *result_length, offset);
+    return false;
+  }
+  return true;
+}
+
+} // namespace woff2
diff --git a/woff2/woff2.h b/woff2/woff2.h
new file mode 100644
index 0000000..aba5080
--- /dev/null
+++ b/woff2/woff2.h
@@ -0,0 +1,50 @@
+// 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 converting WOFF2 format font files to their TTF versions.
+
+#ifndef BROTLI_WOFF2_WOFF2_H_
+#define BROTLI_WOFF2_WOFF2_H_
+
+#include <stddef.h>
+#include <inttypes.h>
+#include <string>
+
+namespace woff2 {
+
+using std::string;
+
+// 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.
+bool ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
+                       const uint8_t *data, size_t length);
+
+// Returns an upper bound on the size of the compressed file.
+size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length);
+
+// 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);
+
+
+
+} // namespace woff2
+
+#endif  // BROTLI_WOFF2_WOFF2_H_
diff --git a/woff2/woff2_compress.cc b/woff2/woff2_compress.cc
new file mode 100644
index 0000000..778369b
--- /dev/null
+++ b/woff2/woff2_compress.cc
@@ -0,0 +1,52 @@
+// 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.
+
+#include <string>
+
+#include "file.h"
+#include "./woff2.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(".")) + ".woff2";
+  fprintf(stdout, "Processing %s => %s\n",
+    filename.c_str(), outfilename.c_str());
+  string input = woff2::GetFileContent(filename);
+
+  const uint8_t* input_data = reinterpret_cast<const uint8_t*>(input.data());
+  size_t output_size = woff2::MaxWOFF2CompressedSize(input_data, input.size());
+  string output(output_size, 0);
+  uint8_t* output_data = reinterpret_cast<uint8_t*>(&output[0]);
+
+  if (!woff2::ConvertTTFToWOFF2(input_data, input.size(),
+                                output_data, &output_size)) {
+    fprintf(stderr, "Compression failed.\n");
+    return 1;
+  }
+  output.resize(output_size);
+
+  woff2::SetFileContents(outfilename, output);
+
+  return 0;
+}
diff --git a/woff2/woff2_decompress.cc b/woff2/woff2_decompress.cc
new file mode 100644
index 0000000..c083793
--- /dev/null
+++ b/woff2/woff2_decompress.cc
@@ -0,0 +1,54 @@
+// 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.
+
+#include <string>
+
+
+#include "file.h"
+#include "./woff2.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";
+  fprintf(stdout, "Processing %s => %s\n",
+    filename.c_str(), outfilename.c_str());
+  string input = woff2::GetFileContent(filename);
+
+  size_t decompressed_size = woff2::ComputeWOFF2FinalSize(
+      reinterpret_cast<const uint8_t*>(input.data()), input.size());
+  string output(decompressed_size, 0);
+  const bool ok = woff2::ConvertWOFF2ToTTF(
+      reinterpret_cast<uint8_t*>(&output[0]), decompressed_size,
+      reinterpret_cast<const uint8_t*>(input.data()), input.size());
+
+  if (!ok) {
+    fprintf(stderr, "Decompression failed\n");
+    return 1;
+  }
+
+  woff2::SetFileContents(outfilename, output);
+
+  return 0;
+}
+