Make BsdiffPatchWriter into an Interface.

Currently, all bsdiff() functions take a filename for the patch, which
is called with a temporary file in all cases. To help expose an
interface that allows to write bsdiff patches in different formats (for
example, changing the compressor and header format) we expose the
PatchWriterInterface class in the public interface so callers can use
a different one or define their own to help experimenting with bsdiff
enconding improvements.

Bug: 34220646
Test: make bsdiff and update_engine; ran bsdiff_unittest

Change-Id: Ie450b2790137665bc033cb36d037171090b18a4b
diff --git a/bsdiff.cc b/bsdiff.cc
index 0735ea1..7c9a2e2 100644
--- a/bsdiff.cc
+++ b/bsdiff.cc
@@ -118,12 +118,17 @@
 	return ret;
 }
 
-// Generate bsdiff patch from |old_buf| to |new_buf|, save the patch file to
-// |patch_filename|. Returns 0 on success.
-// |I_cache| can be used to cache the suffix array if the same |old_buf| is used
-// repeatedly, pass nullptr if not needed.
+// TODO(deymo): Deprecate this version of the interface and move all callers
+// to the underlying version using PatchWriterInterface instead. This allows
+// more flexible options including different encodings.
 int bsdiff(const u_char* old_buf, off_t oldsize, const u_char* new_buf,
            off_t newsize, const char* patch_filename, saidx_t** I_cache) {
+	BsdiffPatchWriter patch(patch_filename);
+	return bsdiff(old_buf, oldsize, new_buf, newsize, &patch, I_cache);
+}
+
+int bsdiff(const u_char* old_buf, off_t oldsize, const u_char* new_buf,
+           off_t newsize, PatchWriterInterface* patch, saidx_t** I_cache) {
 	saidx_t *I;
 	off_t scan,pos=0,len;
 	off_t lastscan,lastpos,lastoffset;
@@ -131,7 +136,6 @@
 	off_t s,Sf,lenf,Sb,lenb;
 	off_t overlap,Ss,lens;
 	off_t i;
-	BsdiffPatchWriter patch(old_buf, oldsize, new_buf, newsize);
 
 	if (I_cache && *I_cache) {
 		I = *I_cache;
@@ -144,8 +148,8 @@
 			*I_cache = I;
 	}
 
-	/* Create the patch file */
-	if (!patch.Open(patch_filename))
+	/* Initialize the patch file */
+	if (!patch->InitializeBuffers(old_buf, oldsize, new_buf, newsize))
 		return 1;
 
 	/* Compute the differences, writing ctrl as we go */
@@ -224,9 +228,10 @@
 				lenb-=lens;
 			};
 
-			if (!patch.AddControlEntry(ControlEntry(lenf,
-			                                        (scan - lenb) - (lastscan + lenf),
-			                                        (pos - lenb) - (lastpos + lenf))))
+			if (!patch->AddControlEntry(
+			        ControlEntry(lenf,
+			                     (scan - lenb) - (lastscan + lenf),
+			                     (pos - lenb) - (lastpos + lenf))))
 				errx(1, "Writing a control entry");
 
 			lastscan=scan-lenb;
@@ -234,7 +239,7 @@
 			lastoffset=pos-scan;
 		};
 	};
-	if (!patch.Close())
+	if (!patch->Close())
 		errx(1, "Closing the patch file");
 
 	if (I_cache == nullptr)