diff options
Diffstat (limited to 'c/tools/brotli.c')
-rw-r--r-- | c/tools/brotli.c | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/c/tools/brotli.c b/c/tools/brotli.c index ce05b64..838539a 100644 --- a/c/tools/brotli.c +++ b/c/tools/brotli.c @@ -86,10 +86,10 @@ typedef struct { /* Parameters */ int quality; int lgwin; + int verbosity; BROTLI_BOOL force_overwrite; BROTLI_BOOL junk_source; BROTLI_BOOL copy_stat; - BROTLI_BOOL verbose; BROTLI_BOOL write_to_stdout; BROTLI_BOOL test_integrity; BROTLI_BOOL decompress; @@ -121,6 +121,12 @@ typedef struct { const uint8_t* next_in; size_t available_out; uint8_t* next_out; + + /* Reporting */ + /* size_t would be large enough, + until 4GiB+ files are compressed / decompressed on 32-bit CPUs. */ + size_t total_in; + size_t total_out; } Context; /* Parse up to 5 decimal digits. */ @@ -279,11 +285,11 @@ static Command ParseParams(Context* params) { command = COMMAND_TEST_INTEGRITY; continue; } else if (c == 'v') { - if (params->verbose) { + if (params->verbosity > 0) { fprintf(stderr, "argument --verbose / -v already set\n"); return COMMAND_INVALID; } - params->verbose = BROTLI_TRUE; + params->verbosity = 1; continue; } else if (c == 'V') { /* Don't parse further. */ @@ -415,11 +421,11 @@ static Command ParseParams(Context* params) { command_set = BROTLI_TRUE; command = COMMAND_TEST_INTEGRITY; } else if (strcmp("verbose", arg) == 0) { - if (params->verbose) { + if (params->verbosity > 0) { fprintf(stderr, "argument --verbose / -v already set\n"); return COMMAND_INVALID; } - params->verbose = BROTLI_TRUE; + params->verbosity = 1; } else if (strcmp("version", arg) == 0) { /* Don't parse further. */ return COMMAND_VERSION; @@ -787,6 +793,8 @@ static void InitializeBuffers(Context* context) { context->next_in = NULL; context->available_out = kFileBufferSize; context->next_out = context->output; + context->total_in = 0; + context->total_out = 0; } static BROTLI_BOOL HasMoreInput(Context* context) { @@ -796,6 +804,7 @@ static BROTLI_BOOL HasMoreInput(Context* context) { static BROTLI_BOOL ProvideInput(Context* context) { context->available_in = fread(context->input, 1, kFileBufferSize, context->fin); + context->total_in += context->available_in; context->next_in = context->input; if (ferror(context->fin)) { fprintf(stderr, "failed to read input [%s]: %s\n", @@ -808,6 +817,7 @@ static BROTLI_BOOL ProvideInput(Context* context) { /* Internal: should be used only in Provide-/Flush-Output. */ static BROTLI_BOOL WriteOutput(Context* context) { size_t out_size = (size_t)(context->next_out - context->output); + context->total_out += out_size; if (out_size == 0) return BROTLI_TRUE; if (context->test_integrity) return BROTLI_TRUE; @@ -833,6 +843,25 @@ static BROTLI_BOOL FlushOutput(Context* context) { return BROTLI_TRUE; } +static void PrintBytes(size_t value) { + if (value < 1024) { + fprintf(stderr, "%d B", (int)value); + } else if (value < 1048576) { + fprintf(stderr, "%0.3f KiB", (double)value / 1024.0); + } else if (value < 1073741824) { + fprintf(stderr, "%0.3f MiB", (double)value / 1048576.0); + } else { + fprintf(stderr, "%0.3f GiB", (double)value / 1073741824.0); + } +} + +static void PrintFileProcessingProgress(Context* context) { + fprintf(stderr, "[%s]: ", PrintablePath(context->current_input_path)); + PrintBytes(context->total_in); + fprintf(stderr, " -> "); + PrintBytes(context->total_out); +} + static BROTLI_BOOL DecompressFile(Context* context, BrotliDecoderState* s) { BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; InitializeBuffers(context); @@ -853,6 +882,11 @@ static BROTLI_BOOL DecompressFile(Context* context, BrotliDecoderState* s) { PrintablePath(context->current_input_path)); return BROTLI_FALSE; } + if (context->verbosity > 0) { + fprintf(stderr, "Decompressed "); + PrintFileProcessingProgress(context); + fprintf(stderr, "\n"); + } return BROTLI_TRUE; } else { fprintf(stderr, "corrupt input [%s]\n", @@ -915,7 +949,13 @@ static BROTLI_BOOL CompressFile(Context* context, BrotliEncoderState* s) { } if (BrotliEncoderIsFinished(s)) { - return FlushOutput(context); + if (!FlushOutput(context)) return BROTLI_FALSE; + if (context->verbosity > 0) { + fprintf(stderr, "Compressed "); + PrintFileProcessingProgress(context); + fprintf(stderr, "\n"); + } + return BROTLI_TRUE; } } } @@ -979,11 +1019,11 @@ int main(int argc, char** argv) { context.quality = 11; context.lgwin = -1; + context.verbosity = 0; context.force_overwrite = BROTLI_FALSE; context.junk_source = BROTLI_FALSE; context.copy_stat = BROTLI_TRUE; context.test_integrity = BROTLI_FALSE; - context.verbose = BROTLI_FALSE; context.write_to_stdout = BROTLI_FALSE; context.decompress = BROTLI_FALSE; context.large_window = BROTLI_FALSE; |