Remove uncompressed blocks from deflate lists.
am: b8325c22bb

Change-Id: I26105f1c5c43b57fdfc5e18c9d3ff6ae68c675e5
diff --git a/src/include/puffin/puffer.h b/src/include/puffin/puffer.h
index 9d47ef8..28e21ae 100644
--- a/src/include/puffin/puffer.h
+++ b/src/include/puffin/puffer.h
@@ -22,8 +22,13 @@
   Puffer();
   ~Puffer();
 
-  // Creates a puffed buffer from a deflate buffer. If |deflates| is not null,
-  // it will be populated with the location of subblocks in the input data.
+  // Creates a puffed buffer from a deflate buffer.
+  //
+  // If |deflates| is not null, it will be populated with the location of the
+  // subblocks in the input data. In addition, the uncompressed deflate blocks
+  // will be ignored and will not be added to the |deflates|. For this case to
+  // happen correctly, the |pw| should write into an empty/null buffer,
+  // otherwise the created puff stream, will not match the deflate stream.
   bool PuffDeflate(BitReaderInterface* br,
                    PuffWriterInterface* pw,
                    std::vector<BitExtent>* deflates) const;
diff --git a/src/puffer.cc b/src/puffer.cc
index 72817f5..a667912 100644
--- a/src/puffer.cc
+++ b/src/puffer.cc
@@ -88,10 +88,9 @@
         pd.type = PuffData::Type::kEndOfBlock;
         TEST_AND_RETURN_FALSE(pw->Insert(pd));
 
-        if (deflates != nullptr) {
-          deflates->emplace_back(start_bit_offset,
-                                 br->OffsetInBits() - start_bit_offset);
-        }
+        // There is no need to insert the location of uncompressed deflates
+        // because we do not want the uncompressed blocks when trying to find
+        // the bit-addressed location of deflates. They better be ignored.
 
         // continue the loop. Do not read any literal/length/distance.
         continue;
diff --git a/src/puffin_unittest.cc b/src/puffin_unittest.cc
index 28d797a..6e3b72b 100644
--- a/src/puffin_unittest.cc
+++ b/src/puffin_unittest.cc
@@ -263,6 +263,31 @@
   CheckSample(kRaw5, kDeflate, kPuff);
 }
 
+TEST_F(PuffinTest, NoIgnoreUncompressedBlocksTest) {
+  Buffer uncomp_deflate = {0x01, 0x05, 0x00, 0xFA, 0xFF,
+                           0x01, 0x02, 0x03, 0x04, 0x05};
+  BufferBitReader bit_reader(uncomp_deflate.data(), uncomp_deflate.size());
+  Buffer puff_buffer(11);  // Same size as |uncomp_puff| below.
+  BufferPuffWriter puff_writer(puff_buffer.data(), puff_buffer.size());
+  vector<BitExtent> deflates;
+  EXPECT_TRUE(puffer_.PuffDeflate(&bit_reader, &puff_writer, nullptr));
+  Buffer uncomp_puff = {0x00, 0x00, 0x80, 0x04, 0x01, 0x02,
+                        0x03, 0x04, 0x05, 0xFF, 0x81};
+  EXPECT_EQ(puff_writer.Size(), uncomp_puff.size());
+  EXPECT_EQ(puff_buffer, uncomp_puff);
+}
+
+TEST_F(PuffinTest, IgnoreUncompressedBlocksTest) {
+  Buffer uncomp_deflate = {0x01, 0x05, 0x00, 0xFA, 0xFF,
+                           0x01, 0x02, 0x03, 0x04, 0x05};
+  BufferBitReader bit_reader(uncomp_deflate.data(), uncomp_deflate.size());
+  BufferPuffWriter puff_writer(nullptr, 0);
+  vector<BitExtent> deflates;
+  EXPECT_TRUE(puffer_.PuffDeflate(&bit_reader, &puff_writer, &deflates));
+  vector<BitExtent> empty;
+  EXPECT_EQ(deflates, empty);
+}
+
 namespace {
 // It is actuall the content of the copyright header.
 const Buffer kDynamicHTRaw = {
@@ -559,11 +584,4 @@
       /*ignore_deflate_size=*/false));
 }
 
-// TODO(ahassani): add tests for:
-//   TestPatchingEmptyTo9
-//   TestPatchingNoDeflateTo9
-
-// TODO(ahassani): Change tests data if you decided to compress the header of
-// the patch.
-
 }  // namespace puffin
diff --git a/src/utils.cc b/src/utils.cc
index fdf5aa5..7eae38e 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -164,6 +164,9 @@
 
     // Find all the subblocks.
     BufferBitReader bit_reader(deflate_buffer.data(), deflate.length);
+    // The uncompressed blocks will be ignored since we are passing a null
+    // buffered puff writer and a valid deflate locations output array. This
+    // should not happen in the puffdiff or anywhere else by default.
     BufferPuffWriter puff_writer(nullptr, 0);
     vector<BitExtent> subblocks;
     TEST_AND_RETURN_FALSE(