Reland the change again

The change to factor out the diff generator was accidentally reverted.
Reland the change to call diff algorithms in one place.

Bug: 197361113
Test: TH
Change-Id: If54d7c977f74205a32e99e67898f8d1f6de91b89
diff --git a/payload_generator/delta_diff_utils.cc b/payload_generator/delta_diff_utils.cc
index 46bf70e..356d369 100644
--- a/payload_generator/delta_diff_utils.cc
+++ b/payload_generator/delta_diff_utils.cc
@@ -30,6 +30,7 @@
 
 #include <algorithm>
 #include <functional>
+#include <limits>
 #include <list>
 #include <map>
 #include <memory>
@@ -365,10 +366,10 @@
   InstallOperation& operation = aop->op;
   if (IsDiffOperationBetter(operation,
                             data_blob->size(),
-                            zucchini_delta.size(),
+                            compressed_delta.size(),
                             src_extents_.size())) {
     operation.set_type(InstallOperation::ZUCCHINI);
-    *data_blob = std::move(zucchini_delta);
+    *data_blob = std::move(compressed_delta);
   }
 
   return true;
@@ -1027,24 +1028,6 @@
   uint64_t blocks_to_read = utils::BlocksInExtents(old_extents);
   uint64_t blocks_to_write = utils::BlocksInExtents(new_extents);
 
-  // Disable bsdiff, and puffdiff when the data is too big.
-  bool bsdiff_allowed =
-      version.OperationAllowed(InstallOperation::SOURCE_BSDIFF);
-  if (bsdiff_allowed &&
-      blocks_to_read * kBlockSize > kMaxBsdiffDestinationSize) {
-    LOG(INFO) << "bsdiff ignored, data too big: " << blocks_to_read * kBlockSize
-              << " bytes";
-    bsdiff_allowed = false;
-  }
-
-  bool puffdiff_allowed = version.OperationAllowed(InstallOperation::PUFFDIFF);
-  if (puffdiff_allowed &&
-      blocks_to_read * kBlockSize > kMaxPuffdiffDestinationSize) {
-    LOG(INFO) << "puffdiff ignored, data too big: "
-              << blocks_to_read * kBlockSize << " bytes";
-    puffdiff_allowed = false;
-  }
-
   const vector<Extent>& src_extents = old_extents;
   const vector<Extent>& dst_extents = new_extents;
   // All operations have dst_extents.
@@ -1085,91 +1068,16 @@
                    operation, data_blob.size(), 0, src_extents.size())) {
       // No point in trying diff if zero blob size diff operation is
       // still worse than replace.
-      if (bsdiff_allowed) {
-        base::FilePath patch;
-        TEST_AND_RETURN_FALSE(base::CreateTemporaryFile(&patch));
-        ScopedPathUnlinker unlinker(patch.value());
 
-        std::unique_ptr<bsdiff::PatchWriterInterface> bsdiff_patch_writer;
-        InstallOperation::Type operation_type = InstallOperation::SOURCE_BSDIFF;
-        if (version.OperationAllowed(InstallOperation::BROTLI_BSDIFF)) {
-          bsdiff_patch_writer =
-              bsdiff::CreateBSDF2PatchWriter(patch.value(),
-                                             bsdiff::CompressorType::kBrotli,
-                                             kBrotliCompressionQuality);
-          operation_type = InstallOperation::BROTLI_BSDIFF;
-        } else {
-          bsdiff_patch_writer = bsdiff::CreateBsdiffPatchWriter(patch.value());
-        }
-
-        brillo::Blob bsdiff_delta;
-        TEST_AND_RETURN_FALSE(0 == bsdiff::bsdiff(old_data.data(),
-                                                  old_data.size(),
-                                                  new_data.data(),
-                                                  new_data.size(),
-                                                  bsdiff_patch_writer.get(),
-                                                  nullptr));
-
-        TEST_AND_RETURN_FALSE(utils::ReadFile(patch.value(), &bsdiff_delta));
-
-        CHECK_GT(bsdiff_delta.size(), static_cast<brillo::Blob::size_type>(0));
-        if (IsDiffOperationBetter(operation,
-                                  data_blob.size(),
-                                  bsdiff_delta.size(),
-                                  src_extents.size())) {
-          if (config.enable_vabc_xor) {
-            StoreExtents(src_extents, operation.mutable_src_extents());
-            PopulateXorOps(&aop, bsdiff_delta);
-          }
-          operation.set_type(operation_type);
-          data_blob = std::move(bsdiff_delta);
-        }
-      }
-      if (puffdiff_allowed) {
-        // Find all deflate positions inside the given extents and then put all
-        // deflates together because we have already read all the extents into
-        // one buffer.
-        vector<puffin::BitExtent> src_deflates;
-        TEST_AND_RETURN_FALSE(deflate_utils::FindAndCompactDeflates(
-            src_extents, old_deflates, &src_deflates));
-
-        vector<puffin::BitExtent> dst_deflates;
-        TEST_AND_RETURN_FALSE(deflate_utils::FindAndCompactDeflates(
-            dst_extents, new_deflates, &dst_deflates));
-
-        puffin::RemoveEqualBitExtents(
-            old_data, new_data, &src_deflates, &dst_deflates);
-
-        // See crbug.com/915559.
-        if (version.minor <= kPuffdiffMinorPayloadVersion) {
-          TEST_AND_RETURN_FALSE(puffin::RemoveDeflatesWithBadDistanceCaches(
-              old_data, &src_deflates));
-
-          TEST_AND_RETURN_FALSE(puffin::RemoveDeflatesWithBadDistanceCaches(
-              new_data, &dst_deflates));
-        }
-
-        // Only Puffdiff if both files have at least one deflate left.
-        if (!src_deflates.empty() && !dst_deflates.empty()) {
-          brillo::Blob puffdiff_delta;
-          ScopedTempFile temp_file("puffdiff-delta.XXXXXX");
-          // Perform PuffDiff operation.
-          TEST_AND_RETURN_FALSE(puffin::PuffDiff(old_data,
-                                                 new_data,
-                                                 src_deflates,
-                                                 dst_deflates,
-                                                 temp_file.path(),
-                                                 &puffdiff_delta));
-          TEST_AND_RETURN_FALSE(puffdiff_delta.size() > 0);
-          if (IsDiffOperationBetter(operation,
-                                    data_blob.size(),
-                                    puffdiff_delta.size(),
-                                    src_extents.size())) {
-            operation.set_type(InstallOperation::PUFFDIFF);
-            data_blob = std::move(puffdiff_delta);
-          }
-        }
-      }
+      BestDiffGenerator best_diff_generator(old_data,
+                                            new_data,
+                                            src_extents,
+                                            dst_extents,
+                                            old_deflates,
+                                            new_deflates,
+                                            config);
+      TEST_AND_RETURN_FALSE(
+          best_diff_generator.GenerateBestDiffOperation(&aop, &data_blob));
     }
   }