Esempio n. 1
0
static void
squash_brotli_stream_init (SquashBrotliStream* s,
                           SquashCodec* codec,
                           SquashStreamType stream_type,
                           SquashOptions* options,
                           SquashDestroyNotify destroy_notify) {
  SquashStream* stream = (SquashStream*) s;
  squash_stream_init (stream, codec, stream_type, (SquashOptions*) options, destroy_notify);

  s->finished = false;
  if (stream_type == SQUASH_STREAM_COMPRESS) {
    brotli::BrotliParams params;
    params.quality = squash_options_get_int_at (stream->options, stream->codec, SQUASH_BROTLI_OPT_LEVEL);
    params.mode = (brotli::BrotliParams::Mode) squash_options_get_int_at (stream->options, stream->codec, SQUASH_BROTLI_OPT_MODE);
    s->compressor = new brotli::BrotliCompressor (params);
    s->remaining_block_in = s->compressor->input_block_size();
    s->remaining_out = 0;
    s->next_out = NULL;
    s->should_flush = false;
    s->should_seal = false;
  } else if (stream_type == SQUASH_STREAM_DECOMPRESS) {
    s->decompressor = BrotliCreateState(squash_brotli_malloc, squash_brotli_free, squash_codec_get_context (codec));
  } else {
    squash_assert_unreachable();
  }
}
Esempio n. 2
0
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);
}
Esempio n. 3
0
static void
squash_bz2_stream_init (SquashBZ2Stream* stream,
                        SquashCodec* codec,
                        SquashStreamType stream_type,
                        SquashOptions* options,
                        SquashDestroyNotify destroy_notify) {
  squash_stream_init ((SquashStream*) stream, codec, stream_type, (SquashOptions*) options, destroy_notify);

  bz_stream tmp   = { 0, };
  tmp.bzalloc     = squash_bz2_malloc;
  tmp.bzfree      = squash_bz2_free;
  tmp.opaque      = squash_codec_get_context (codec);
  stream->stream  = tmp;
}