[zlib bench] Add check option modifier --check-binary

Per CL:3591994 if the --check option is given, compress the input file
and output the length and crc32 of the compressed data.

Add check option modifier: --check-binary, which writes the compressed
data to the output as well.

Also normalize the check output compression details: use the base-name
of the input file, instead of its full-path name.

Bug: 798943,1316541
Change-Id: Ia27f09b1ad583bda8e9c82c62a5b24b619f6713a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3616433
Commit-Queue: Noel Gordon <noel@chromium.org>
Auto-Submit: Noel Gordon <noel@chromium.org>
Reviewed-by: Adenilson Cavalcanti <cavalcantii@chromium.org>
Commit-Queue: Adenilson Cavalcanti <cavalcantii@chromium.org>
Cr-Commit-Position: refs/heads/main@{#998055}
NOKEYCHECK=True
GitOrigin-RevId: 5ae6f05e848868027ac1a78f191c4a43919e8c94
diff --git a/contrib/bench/zlib_bench.cc b/contrib/bench/zlib_bench.cc
index fcb2848..3d99646 100644
--- a/contrib/bench/zlib_bench.cc
+++ b/contrib/bench/zlib_bench.cc
@@ -45,6 +45,7 @@
   Data(size_t s) { data.reset(new (std::nothrow) char[size = s]); }
   std::unique_ptr<char[]> data;
   size_t size;
+  std::string name;
 };
 
 Data read_file_data_or_exit(const char* name) {
@@ -66,6 +67,7 @@
     exit(1);
   }
 
+  data.name = std::string(name);
   return data;
 }
 
@@ -193,10 +195,15 @@
   exit(3);
 }
 
-void check_file(const Data& file, zlib_wrapper type) {
+void check_file(const Data& file, zlib_wrapper type, int mode) {
+  printf("%s %d %s%s\n", zlib_wrapper_name(type), zlib_compression_level,
+    zlib_level_strategy_name(zlib_compression_level), file.name.c_str());
+
+  // Compress the file data.
   std::string compressed;
   zlib_compress(type, file.data.get(), file.size, &compressed, true);
 
+  // Output compressed data integrity check: the data crc32.
   unsigned long check = crc32_z(0, Z_NULL, 0);
   const Bytef* data = (const Bytef*)compressed.data();
   static_assert(sizeof(z_size_t) == sizeof(size_t), "z_size_t size");
@@ -205,6 +212,7 @@
   const size_t compressed_length = compressed.size();
   printf("data crc32 %.8lx length %zu\n", check, compressed_length);
 
+  // Output gzip or zlib DEFLATE stream internal check data.
   if (type == kWrapperGZIP) {
     uint32_t prev_word, last_word;
     data += compressed_length - 8;
@@ -218,31 +226,38 @@
     last_word = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
     printf("zlib adler %.8x\n", last_word);
   }
+
+  if (mode == 2)  // --check-binary: output compressed data.
+    fwrite(compressed.data(), compressed_length, 1, stdout);
+
+  if (fflush(stdout), ferror(stdout))
+    error_exit("check file: error writing output", 3);
 }
 
 void zlib_file(const char* name, zlib_wrapper type, int width, int check) {
   /*
    * Read the file data.
    */
-  const auto file = read_file_data_or_exit(name);
+  struct Data file = read_file_data_or_exit(name);
   const int length = static_cast<int>(file.size);
   const char* data = file.data.get();
 
   /*
+   * Compress file: report output data checks and return.
+   */
+  if (check) {
+    file.name = file.name.substr(file.name.find_last_of("/\\") + 1);
+    check_file(file, type, check);
+    return;
+  }
+
+  /*
    * Report compression strategy and file name.
    */
   const char* strategy = zlib_level_strategy_name(zlib_compression_level);
   printf("%s%-40s :\n", strategy, name);
 
   /*
-   * Compress file: report output data checks.
-   */
-  if (check) {
-    check_file(file, type);
-    return;
-  }
-
-  /*
    * Chop the data into blocks.
    */
   const int block_size = 1 << 20;
@@ -377,6 +392,8 @@
       zlib_strategy = Z_RLE;
     } else if (get_option(argc, argv, "--check")) {
       file_check = 1;
+    } else if (get_option(argc, argv, "--check-binary")) {
+      file_check = 2;
     } else if (get_option(argc, argv, "--field")) {
       get_field_width(argc, argv, size_field_width);
     } else {