static SquashStatus squash_brotli_decompress_buffer (SquashCodec* codec, size_t* decompressed_size, uint8_t decompressed[SQUASH_ARRAY_PARAM(*decompressed_size)], size_t compressed_size, const uint8_t compressed[SQUASH_ARRAY_PARAM(compressed_size)], SquashOptions* options) { BrotliResult res; size_t total_out = 0; size_t available_in = compressed_size; const uint8_t* next_in = compressed; size_t available_out = *decompressed_size; uint8_t* next_out = decompressed; BrotliState* s = BrotliCreateState(squash_brotli_malloc, squash_brotli_free, squash_codec_get_context (codec)); try { res = BrotliDecompressStream (&available_in, &next_in, &available_out, &next_out, &total_out, s); } catch (const std::bad_alloc& e) { (void) e; BrotliDestroyState(s); return squash_error (SQUASH_MEMORY); } catch (...) { BrotliDestroyState(s); return squash_error (SQUASH_FAILED); } BrotliDestroyState(s); *decompressed_size = total_out; return squash_brotli_status_to_squash_status (res); }
static void squash_brotli_stream_destroy (void* stream) { SquashBrotliStream* s = (SquashBrotliStream*) stream; if (((SquashStream*) stream)->stream_type == SQUASH_STREAM_COMPRESS) { BrotliEncoderDestroyInstance(s->ctx.encoder); } else if (((SquashStream*) stream)->stream_type == SQUASH_STREAM_DECOMPRESS) { BrotliDestroyState(s->ctx.decoder); } else { squash_assert_unreachable(); } squash_stream_destroy (stream); }
static void squash_brotli_stream_destroy (void* stream) { SquashBrotliStream* s = (SquashBrotliStream*) stream; if (((SquashStream*) stream)->stream_type == SQUASH_STREAM_COMPRESS) { delete s->compressor; } else if (((SquashStream*) stream)->stream_type == SQUASH_STREAM_DECOMPRESS) { BrotliDestroyState(s->decompressor); } else { squash_assert_unreachable(); } squash_stream_destroy (stream); }
NS_IMETHODIMP nsJARInputStream::Close() { if (mMode == MODE_INFLATE) { inflateEnd(&mZs); } #ifdef MOZ_JAR_BROTLI if (mMode == MODE_BROTLI) { BrotliDestroyState(mBrotliState); } #endif mMode = MODE_CLOSED; mFd = nullptr; return NS_OK; }
static int Decompress(FILE* fin, FILE* fout, const char* dictionary_path) { /* Dictionary should be kept during first rounds of decompression. */ uint8_t* dictionary = NULL; uint8_t* input; uint8_t* output; size_t total_out; size_t available_in; const uint8_t* next_in; size_t available_out = kFileBufferSize; uint8_t* next_out; BrotliResult result = BROTLI_RESULT_ERROR; BrotliState* s = BrotliCreateState(NULL, NULL, NULL); if (!s) { fprintf(stderr, "out of memory\n"); return 0; } if (dictionary_path != NULL) { size_t dictionary_size = 0; dictionary = ReadDictionary(dictionary_path, &dictionary_size); BrotliSetCustomDictionary(dictionary_size, dictionary, s); } input = (uint8_t*)malloc(kFileBufferSize); output = (uint8_t*)malloc(kFileBufferSize); if (!input || !output) { fprintf(stderr, "out of memory\n"); goto end; } next_out = output; result = BROTLI_RESULT_NEEDS_MORE_INPUT; while (1) { if (result == BROTLI_RESULT_NEEDS_MORE_INPUT) { if (feof(fin)) { break; } available_in = fread(input, 1, kFileBufferSize, fin); next_in = input; if (ferror(fin)) { break; } } else if (result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) { fwrite(output, 1, kFileBufferSize, fout); if (ferror(fout)) { break; } available_out = kFileBufferSize; next_out = output; } else { break; /* Error or success. */ } result = BrotliDecompressStream(&available_in, &next_in, &available_out, &next_out, &total_out, s); } if (next_out != output) { fwrite(output, 1, (size_t)(next_out - output), fout); } if ((result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) || ferror(fout)) { fprintf(stderr, "failed to write output\n"); } else if (result != BROTLI_RESULT_SUCCESS) { /* Error or needs more input. */ fprintf(stderr, "corrupt input\n"); } end: free(dictionary); free(input); free(output); BrotliDestroyState(s); return (result == BROTLI_RESULT_SUCCESS) ? 1 : 0; }
/** Decompresses a Brotli compressed source buffer. Extracts decompressed data to its original form. If the compressed source data specified by Source is successfully decompressed into Destination, then EFI_SUCCESS is returned. If the compressed source data specified by Source is not in a valid compressed data format, then EFI_INVALID_PARAMETER is returned. @param Source The source buffer containing the compressed data. @param SourceSize The size of source buffer. @param Destination The destination buffer to store the decompressed data. @param DestSize The destination buffer size. @param BuffInfo The pointer to the BROTLI_BUFF instance. @retval EFI_SUCCESS Decompression completed successfully, and the uncompressed buffer is returned in Destination. @retval EFI_INVALID_PARAMETER The source buffer specified by Source is corrupted (not in a valid compressed format). **/ EFI_STATUS BrotliDecompress ( IN CONST VOID* Source, IN UINTN SourceSize, IN OUT VOID* Destination, IN OUT UINTN DestSize, IN VOID * BuffInfo ) { UINT8 * Input; UINT8 * Output; const UINT8 * NextIn; UINT8 * NextOut; size_t TotalOut; size_t AvailableIn; size_t AvailableOut; BrotliResult Result; BrotliState * BroState; VOID * Temp; TotalOut = 0; AvailableOut = FILE_BUFFER_SIZE; Result = BROTLI_RESULT_ERROR; BroState = BrotliCreateState(BrAlloc, BrFree, BuffInfo); Temp = Destination; if (BroState == NULL) { return EFI_INVALID_PARAMETER; } Input = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE); Output = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE); if ((Input==NULL) || (Output==NULL)) { BrFree(BuffInfo, Input); BrFree(BuffInfo, Output); BrotliDestroyState(BroState); return EFI_INVALID_PARAMETER; } NextOut = Output; Result = BROTLI_RESULT_NEEDS_MORE_INPUT; while (1) { if (Result == BROTLI_RESULT_NEEDS_MORE_INPUT) { if (SourceSize == 0) { break; } if (SourceSize >= FILE_BUFFER_SIZE) { AvailableIn = FILE_BUFFER_SIZE; }else{ AvailableIn = SourceSize; } CopyMem(Input, Source, AvailableIn); Source = (VOID *)((UINT8 *)Source + AvailableIn); SourceSize -= AvailableIn; NextIn = Input; } else if (Result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) { CopyMem(Temp, Output, FILE_BUFFER_SIZE); AvailableOut = FILE_BUFFER_SIZE; Temp = (VOID *)((UINT8 *)Temp +FILE_BUFFER_SIZE); NextOut = Output; } else { break; /* Error or success. */ } Result = BrotliDecompressStream( &AvailableIn, &NextIn, &AvailableOut, &NextOut, &TotalOut, BroState ); } if (NextOut != Output) { CopyMem(Temp, Output, (size_t)(NextOut - Output)); } DestSize = TotalOut; BrFree(BuffInfo, Input); BrFree(BuffInfo, Output); BrotliDestroyState(BroState); return (Result == BROTLI_RESULT_SUCCESS) ? EFI_SUCCESS : EFI_INVALID_PARAMETER; }