From 7c5a77c651bcde37005e6b6e209747edcc6c9361 Mon Sep 17 00:00:00 2001 From: Jose Fonseca Date: Tue, 29 Mar 2016 12:13:23 +0100 Subject: cli: Do CRC check of the compressed Brotli file. Just in case. --- cli/cli_repack.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'cli') diff --git a/cli/cli_repack.cpp b/cli/cli_repack.cpp index e259661d..2e17cdeb 100644 --- a/cli/cli_repack.cpp +++ b/cli/cli_repack.cpp @@ -28,10 +28,12 @@ #include #include +#include #include "cli.hpp" #include +#include // for crc32 #include "trace_file.hpp" #include "trace_ostream.hpp" @@ -80,9 +82,12 @@ private: bool eof = false; public: + uLong crc; + BrotliTraceIn(trace::File *s) : stream(s) { + crc = crc32(0L, Z_NULL, 0); } ~BrotliTraceIn() { @@ -101,6 +106,7 @@ public: eof = true; return nullptr; } + crc32(crc, reinterpret_cast(buf), *bytes_read); return buf; } }; @@ -158,6 +164,22 @@ repack_brotli(trace::File *inFile, const char *outFileName, int quality) } fclose(fout); + std::unique_ptr outFileIn(trace::File::createBrotli()); + if (!outFileIn->open(outFileName)) { + std::cerr << "error: failed to open " << outFileName << " for reading\n"; + return EXIT_FAILURE; + } + BrotliTraceIn outIn(outFileIn.get()); + size_t bytes_read; + do { + outIn.Read(65536, &bytes_read); + } while (bytes_read > 0); + + if (in.crc != outIn.crc) { + std::cerr << "error: CRC mismatch reading " << outFileName << "\n"; + return EXIT_FAILURE; + } + return EXIT_SUCCESS; } -- cgit v1.2.3