Skip to content

Commit

Permalink
verbose error report in CLI
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 546833411
  • Loading branch information
eustas committed Jul 10, 2023
1 parent 70e7b1a commit 2e6164d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion c/dec/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -2831,7 +2831,7 @@ BrotliDecoderErrorCode BrotliDecoderGetErrorCode(const BrotliDecoderState* s) {
const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c) {
switch (c) {
#define BROTLI_ERROR_CODE_CASE_(PREFIX, NAME, CODE) \
case BROTLI_DECODER ## PREFIX ## NAME: return #NAME;
case BROTLI_DECODER ## PREFIX ## NAME: return #PREFIX #NAME;
#define BROTLI_NOTHING_
BROTLI_DECODER_ERROR_CODES_LIST(BROTLI_ERROR_CODE_CASE_, BROTLI_NOTHING_)
#undef BROTLI_ERROR_CODE_CASE_
Expand Down
29 changes: 28 additions & 1 deletion c/tools/brotli.c
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,22 @@ static void PrintFileProcessingProgress(Context* context) {
fprintf(stderr, " in %1.2f sec", (double)(context->end_time - context->start_time) / CLOCKS_PER_SEC);
}

static const char* PrettyDecoderErrorString(BrotliDecoderErrorCode code) {
/* "_ERROR_domain_" is in only added in newer versions. If CLI is linked
against older shared library, return error string as is; result might be
a bit confusing, e.g. "RESERVED" instead of "FORMAT_RESERVED" */
const char* prefix = "_ERROR_";
size_t prefix_len = strlen(prefix);
const char* error_str = BrotliDecoderErrorString(code);
size_t error_len = strlen(error_str);
if (error_len > prefix_len) {
if (strncmp(error_str, prefix, prefix_len) == 0) {
error_str += prefix_len;
}
}
return error_str;
}

static BROTLI_BOOL DecompressFile(Context* context, BrotliDecoderState* s) {
BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
InitializeBuffers(context);
Expand All @@ -996,6 +1012,9 @@ static BROTLI_BOOL DecompressFile(Context* context, BrotliDecoderState* s) {
if (!HasMoreInput(context)) {
fprintf(stderr, "corrupt input [%s]\n",
PrintablePath(context->current_input_path));
if (context->verbosity > 0) {
fprintf(stderr, "reason: truncated input\n");
}
return BROTLI_FALSE;
}
if (!ProvideInput(context)) return BROTLI_FALSE;
Expand All @@ -1008,6 +1027,9 @@ static BROTLI_BOOL DecompressFile(Context* context, BrotliDecoderState* s) {
if (has_more_input) {
fprintf(stderr, "corrupt input [%s]\n",
PrintablePath(context->current_input_path));
if (context->verbosity > 0) {
fprintf(stderr, "reason: extra input\n");
}
return BROTLI_FALSE;
}
if (context->verbosity > 0) {
Expand All @@ -1017,9 +1039,14 @@ static BROTLI_BOOL DecompressFile(Context* context, BrotliDecoderState* s) {
fprintf(stderr, "\n");
}
return BROTLI_TRUE;
} else {
} else { /* result == BROTLI_DECODER_RESULT_ERROR */
fprintf(stderr, "corrupt input [%s]\n",
PrintablePath(context->current_input_path));
if (context->verbosity > 0) {
BrotliDecoderErrorCode error = BrotliDecoderGetErrorCode(s);
const char* error_str = PrettyDecoderErrorString(error);
fprintf(stderr, "reason: %s (%d)\n", error_str, error);
}
return BROTLI_FALSE;
}

Expand Down

0 comments on commit 2e6164d

Please sign in to comment.