Skip to content

Commit

Permalink
Use Hedley for lots of platform-specific bits.
Browse files Browse the repository at this point in the history
  • Loading branch information
nemequ committed Jul 23, 2016
1 parent 67a2fd8 commit b9d66ea
Show file tree
Hide file tree
Showing 84 changed files with 736 additions and 827 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@
[submodule "plugins/lzfse/lzfse"]
path = plugins/lzfse/lzfse
url = https://github.com/lzfse/lzfse.git
[submodule "squash/hedley"]
path = squash/hedley
url = https://github.com/nemequ/hedley.git
32 changes: 16 additions & 16 deletions plugins/brieflz/squash-brieflz.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ SquashStatus squash_plugin_init_codec (SquashCodec* codec, SquashCodecImpl* impl
static size_t
squash_brieflz_get_max_compressed_size (SquashCodec* codec, size_t uncompressed_size) {
#if ULONG_MAX < SIZE_MAX
if (SQUASH_UNLIKELY(ULONG_MAX < uncompressed_size))
if (HEDLEY_UNLIKELY(ULONG_MAX < uncompressed_size))
return (squash_error (SQUASH_RANGE), 0);
#endif

const unsigned long r = blz_max_packed_size ((unsigned long) uncompressed_size);

#if SIZE_MAX < ULONG_MAX
if (SQUASH_UNLIKELY(SIZE_MAX < r))
if (HEDLEY_UNLIKELY(SIZE_MAX < r))
return (squash_error (SQUASH_RANGE), 0);
#endif

Expand All @@ -59,27 +59,27 @@ squash_brieflz_get_max_compressed_size (SquashCodec* codec, size_t uncompressed_
static SquashStatus
squash_brieflz_decompress_buffer (SquashCodec* codec,
size_t* decompressed_size,
uint8_t decompressed[SQUASH_ARRAY_PARAM(*decompressed_size)],
uint8_t decompressed[HEDLEY_ARRAY_PARAM(*decompressed_size)],
size_t compressed_size,
const uint8_t compressed[SQUASH_ARRAY_PARAM(compressed_size)],
const uint8_t compressed[HEDLEY_ARRAY_PARAM(compressed_size)],
SquashOptions* options) {
const uint8_t *src = compressed;
unsigned long size;

#if ULONG_MAX < SIZE_MAX
if (SQUASH_UNLIKELY(ULONG_MAX < compressed_size) ||
SQUASH_UNLIKELY(ULONG_MAX < *decompressed_size))
if (HEDLEY_UNLIKELY(ULONG_MAX < compressed_size) ||
HEDLEY_UNLIKELY(ULONG_MAX < *decompressed_size))
return squash_error (SQUASH_RANGE);
#endif

size = blz_depack_safe (src, (unsigned long) compressed_size,
decompressed, (unsigned long) *decompressed_size);

if (SQUASH_UNLIKELY(size != *decompressed_size))
if (HEDLEY_UNLIKELY(size != *decompressed_size))
return squash_error (SQUASH_FAILED);

#if SIZE_MAX < ULONG_MAX
if (SQUASH_UNLIKELY(SIZE_MAX < size))
if (HEDLEY_UNLIKELY(SIZE_MAX < size))
return squash_error (SQUASH_RANGE);
#endif

Expand All @@ -91,28 +91,28 @@ squash_brieflz_decompress_buffer (SquashCodec* codec,
static SquashStatus
squash_brieflz_compress_buffer (SquashCodec* codec,
size_t* compressed_size,
uint8_t compressed[SQUASH_ARRAY_PARAM(*compressed_size)],
uint8_t compressed[HEDLEY_ARRAY_PARAM(*compressed_size)],
size_t uncompressed_size,
const uint8_t uncompressed[SQUASH_ARRAY_PARAM(uncompressed_size)],
const uint8_t uncompressed[HEDLEY_ARRAY_PARAM(uncompressed_size)],
SquashOptions* options) {
uint8_t *dst = compressed;
void *workmem = NULL;
unsigned long size;

#if ULONG_MAX < SIZE_MAX
if (SQUASH_UNLIKELY(ULONG_MAX < uncompressed_size) ||
SQUASH_UNLIKELY(ULONG_MAX < *compressed_size))
if (HEDLEY_UNLIKELY(ULONG_MAX < uncompressed_size) ||
HEDLEY_UNLIKELY(ULONG_MAX < *compressed_size))
return squash_error (SQUASH_RANGE);
#endif

if (SQUASH_UNLIKELY((unsigned long) *compressed_size
if (HEDLEY_UNLIKELY((unsigned long) *compressed_size
< squash_brieflz_get_max_compressed_size (codec, uncompressed_size))) {
return squash_error (SQUASH_BUFFER_FULL);
}

workmem = squash_malloc (blz_workmem_size ((unsigned long) uncompressed_size));

if (SQUASH_UNLIKELY(workmem == NULL)) {
if (HEDLEY_UNLIKELY(workmem == NULL)) {
return squash_error (SQUASH_MEMORY);
}

Expand All @@ -123,7 +123,7 @@ squash_brieflz_compress_buffer (SquashCodec* codec,
squash_free (workmem);

#if SIZE_MAX < ULONG_MAX
if (SQUASH_UNLIKELY(SIZE_MAX < size))
if (HEDLEY_UNLIKELY(SIZE_MAX < size))
return squash_error (SQUASH_RANGE);
#endif

Expand All @@ -136,7 +136,7 @@ SquashStatus
squash_plugin_init_codec (SquashCodec* codec, SquashCodecImpl* impl) {
const char* name = squash_codec_get_name (codec);

if (SQUASH_LIKELY(strcmp ("brieflz", name) == 0)) {
if (HEDLEY_LIKELY(strcmp ("brieflz", name) == 0)) {
impl->info = SQUASH_CODEC_INFO_WRAP_SIZE;
/* impl->get_uncompressed_size = squash_brieflz_get_uncompressed_size; */
impl->get_max_compressed_size = squash_brieflz_get_max_compressed_size;
Expand Down
28 changes: 14 additions & 14 deletions plugins/brotli/squash-brotli.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ squash_brotli_stream_init (SquashBrotliStream* s,
} else if (stream_type == SQUASH_STREAM_DECOMPRESS) {
s->ctx.decoder = BrotliCreateState(squash_brotli_malloc, squash_brotli_free, NULL);
} else {
squash_assert_unreachable();
HEDLEY_UNREACHABLE();
}
}

Expand All @@ -150,7 +150,7 @@ squash_brotli_stream_destroy (void* stream) {
} else if (((SquashStream*) stream)->stream_type == SQUASH_STREAM_DECOMPRESS) {
BrotliDestroyState(s->ctx.decoder);
} else {
squash_assert_unreachable();
HEDLEY_UNREACHABLE();
}

squash_stream_destroy (stream);
Expand All @@ -172,7 +172,7 @@ squash_brotli_encoder_operation_from_squash_operation (const SquashOperation ope
return BROTLI_OPERATION_FINISH;
case SQUASH_OPERATION_TERMINATE:
default:
squash_assert_unreachable ();
HEDLEY_UNREACHABLE ();
}
}

Expand All @@ -188,7 +188,7 @@ squash_brotli_process_stream (SquashStream* stream, SquashOperation operation) {
&(stream->avail_out), &(stream->next_out),
NULL);

if (SQUASH_UNLIKELY(be_ret != 1))
if (HEDLEY_UNLIKELY(be_ret != 1))
return squash_error (SQUASH_FAILED);
else if (stream->avail_in != 0 || BrotliEncoderHasMoreOutput(s->ctx.encoder))
return SQUASH_PROCESSING;
Expand All @@ -212,13 +212,13 @@ squash_brotli_process_stream (SquashStream* stream, SquashOperation operation) {
return SQUASH_FAILED;
}

if (SQUASH_UNLIKELY(bd_ret != BROTLI_RESULT_SUCCESS))
if (HEDLEY_UNLIKELY(bd_ret != BROTLI_RESULT_SUCCESS))
return squash_error (SQUASH_FAILED);
} else {
squash_assert_unreachable ();
HEDLEY_UNREACHABLE ();
}

squash_assert_unreachable ();
HEDLEY_UNREACHABLE ();
}

static size_t
Expand All @@ -229,9 +229,9 @@ squash_brotli_get_max_compressed_size (SquashCodec* codec, size_t uncompressed_s
static SquashStatus
squash_brotli_compress_buffer (SquashCodec* codec,
size_t* compressed_size,
uint8_t compressed[SQUASH_ARRAY_PARAM(*compressed_size)],
uint8_t compressed[HEDLEY_ARRAY_PARAM(*compressed_size)],
size_t uncompressed_size,
const uint8_t uncompressed[SQUASH_ARRAY_PARAM(uncompressed_size)],
const uint8_t uncompressed[HEDLEY_ARRAY_PARAM(uncompressed_size)],
SquashOptions* options) {
const int quality = squash_options_get_int_at (options, codec, SQUASH_BROTLI_OPT_LEVEL);
const int lgwin = squash_options_get_int_at (options, codec, SQUASH_BROTLI_OPT_WINDOW_SIZE);
Expand All @@ -240,26 +240,26 @@ squash_brotli_compress_buffer (SquashCodec* codec,

const int res = BrotliEncoderCompress (quality, lgwin, mode, uncompressed_size, uncompressed, compressed_size, compressed);

return SQUASH_LIKELY(res == 1) ? SQUASH_OK : squash_error (SQUASH_BUFFER_FULL);
return HEDLEY_LIKELY(res == 1) ? SQUASH_OK : squash_error (SQUASH_BUFFER_FULL);
}

static SquashStatus
squash_brotli_decompress_buffer (SquashCodec* codec,
size_t* decompressed_size,
uint8_t decompressed[SQUASH_ARRAY_PARAM(*decompressed_size)],
uint8_t decompressed[HEDLEY_ARRAY_PARAM(*decompressed_size)],
size_t compressed_size,
const uint8_t compressed[SQUASH_ARRAY_PARAM(compressed_size)],
const uint8_t compressed[HEDLEY_ARRAY_PARAM(compressed_size)],
SquashOptions* options) {
const BrotliResult res = BrotliDecompressBuffer(compressed_size, compressed, decompressed_size, decompressed);

return SQUASH_LIKELY(res == BROTLI_RESULT_SUCCESS) ? SQUASH_OK : squash_error (SQUASH_BUFFER_FULL);
return HEDLEY_LIKELY(res == BROTLI_RESULT_SUCCESS) ? SQUASH_OK : squash_error (SQUASH_BUFFER_FULL);
}

SquashStatus
squash_plugin_init_codec (SquashCodec* codec, SquashCodecImpl* impl) {
const char* name = squash_codec_get_name (codec);

if (SQUASH_LIKELY(strcmp ("brotli", name) == 0)) {
if (HEDLEY_LIKELY(strcmp ("brotli", name) == 0)) {
impl->info = SQUASH_CODEC_INFO_CAN_FLUSH;
impl->options = squash_brotli_options;
impl->get_max_compressed_size = squash_brotli_get_max_compressed_size;
Expand Down
36 changes: 18 additions & 18 deletions plugins/bsc/squash-bsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ squash_bsc_get_max_compressed_size (SquashCodec* codec, size_t uncompressed_size
static size_t
squash_bsc_get_uncompressed_size (SquashCodec* codec,
size_t compressed_size,
const uint8_t compressed[SQUASH_ARRAY_PARAM(compressed_size)]) {
const uint8_t compressed[HEDLEY_ARRAY_PARAM(compressed_size)]) {
int p_block_size, p_data_size;

#if INT_MAX < SIZE_MAX
if (SQUASH_UNLIKELY(INT_MAX < compressed_size))
if (HEDLEY_UNLIKELY(INT_MAX < compressed_size))
return (squash_error (SQUASH_RANGE), 0);
#endif

Expand All @@ -126,7 +126,7 @@ squash_bsc_get_uncompressed_size (SquashCodec* codec,
return 0;
} else {
#if SIZE_MAX < INT_MAX
if (SQUASH_UNLIKELY(SIZE_MAX < p_data_size))
if (HEDLEY_UNLIKELY(SIZE_MAX < p_data_size))
return (squash_error (SQUASH_RANGE), 0);
#endif
return (size_t) p_data_size;
Expand All @@ -146,9 +146,9 @@ squash_bsc_options_get_features (SquashCodec* codec,
static SquashStatus
squash_bsc_compress_buffer_unsafe (SquashCodec* codec,
size_t* compressed_size,
uint8_t compressed[SQUASH_ARRAY_PARAM(*compressed_size)],
uint8_t compressed[HEDLEY_ARRAY_PARAM(*compressed_size)],
size_t uncompressed_size,
const uint8_t uncompressed[SQUASH_ARRAY_PARAM(uncompressed_size)],
const uint8_t uncompressed[HEDLEY_ARRAY_PARAM(uncompressed_size)],
SquashOptions* options) {
const int lzp_hash_size = squash_options_get_int_at (options, codec, SQUASH_BSC_OPT_LZP_HASH_SIZE);
const int lzp_min_len = squash_options_get_int_at (options, codec, SQUASH_BSC_OPT_LZP_MIN_LEN);
Expand All @@ -157,22 +157,22 @@ squash_bsc_compress_buffer_unsafe (SquashCodec* codec,
const int features = squash_bsc_options_get_features (codec, options);

#if INT_MAX < SIZE_MAX
if (SQUASH_UNLIKELY(INT_MAX < uncompressed_size))
if (HEDLEY_UNLIKELY(INT_MAX < uncompressed_size))
return squash_error (SQUASH_RANGE);
#endif

if (SQUASH_UNLIKELY(*compressed_size < (uncompressed_size + LIBBSC_HEADER_SIZE)))
if (HEDLEY_UNLIKELY(*compressed_size < (uncompressed_size + LIBBSC_HEADER_SIZE)))
return squash_error (SQUASH_BUFFER_FULL);

const int res = bsc_compress (uncompressed, compressed, (int) uncompressed_size,
lzp_hash_size, lzp_min_len, block_sorter, coder, features);

if (SQUASH_UNLIKELY(res < 0)) {
if (HEDLEY_UNLIKELY(res < 0)) {
return squash_error (SQUASH_FAILED);
}

#if SIZE_MAX < INT_MAX
if (SQUASH_UNLIKELY(SIZE_MAX < res))
if (HEDLEY_UNLIKELY(SIZE_MAX < res))
return squash_error (SQUASH_RANGE);
#endif

Expand All @@ -184,13 +184,13 @@ squash_bsc_compress_buffer_unsafe (SquashCodec* codec,
static SquashStatus
squash_bsc_decompress_buffer (SquashCodec* codec,
size_t* decompressed_size,
uint8_t decompressed[SQUASH_ARRAY_PARAM(*decompressed_size)],
uint8_t decompressed[HEDLEY_ARRAY_PARAM(*decompressed_size)],
size_t compressed_size,
const uint8_t compressed[SQUASH_ARRAY_PARAM(compressed_size)],
const uint8_t compressed[HEDLEY_ARRAY_PARAM(compressed_size)],
SquashOptions* options) {
#if INT_MAX < SIZE_MAX
if (SQUASH_UNLIKELY(INT_MAX < compressed_size) ||
SQUASH_UNLIKELY(INT_MAX < *decompressed_size))
if (HEDLEY_UNLIKELY(INT_MAX < compressed_size) ||
HEDLEY_UNLIKELY(INT_MAX < *decompressed_size))
return squash_error (SQUASH_RANGE);
#endif

Expand All @@ -200,18 +200,18 @@ squash_bsc_decompress_buffer (SquashCodec* codec,

int res = bsc_block_info (compressed, (int) compressed_size, &p_block_size, &p_data_size, LIBBSC_DEFAULT_FEATURES);

if (SQUASH_UNLIKELY(p_block_size != (int) compressed_size))
if (HEDLEY_UNLIKELY(p_block_size != (int) compressed_size))
return squash_error (SQUASH_FAILED);
if (SQUASH_UNLIKELY(p_data_size > (int) *decompressed_size))
if (HEDLEY_UNLIKELY(p_data_size > (int) *decompressed_size))
return squash_error (SQUASH_BUFFER_FULL);

res = bsc_decompress (compressed, p_block_size, decompressed, p_data_size, features);

if (SQUASH_UNLIKELY(res < 0))
if (HEDLEY_UNLIKELY(res < 0))
return squash_error (SQUASH_FAILED);

#if SIZE_MAX < INT_MAX
if (SQUASH_UNLIKELY(SIZE_MAX < p_data_size))
if (HEDLEY_UNLIKELY(SIZE_MAX < p_data_size))
return squash_error (SQUASH_RANGE);
#endif

Expand All @@ -226,7 +226,7 @@ squash_plugin_init_codec (SquashCodec* codec, SquashCodecImpl* impl) {

const char* name = squash_codec_get_name (codec);

if (SQUASH_LIKELY(strcmp ("bsc", name) == 0)) {
if (HEDLEY_LIKELY(strcmp ("bsc", name) == 0)) {
impl->options = squash_bsc_options;
impl->get_uncompressed_size = squash_bsc_get_uncompressed_size;
impl->get_max_compressed_size = squash_bsc_get_max_compressed_size;
Expand Down
6 changes: 3 additions & 3 deletions plugins/bzip2/squash-bzip2.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ squash_bz2_stream_new (SquashCodec* codec, SquashStreamType stream_type, SquashO
0,
squash_options_get_bool_at (options, codec, SQUASH_BZ2_OPT_SMALL));
} else {
squash_assert_unreachable();
HEDLEY_UNREACHABLE();
}

if (bz2_e != BZ_OK) {
Expand Down Expand Up @@ -266,11 +266,11 @@ squash_bz2_process_stream (SquashStream* stream, SquashOperation operation) {
case SQUASH_OPERATION_FINISH:
return squash_bz2_finish_stream (stream);
case SQUASH_OPERATION_TERMINATE:
squash_assert_unreachable ();
HEDLEY_UNREACHABLE ();
break;
}

squash_assert_unreachable();
HEDLEY_UNREACHABLE();
}

static size_t
Expand Down
16 changes: 8 additions & 8 deletions plugins/copy/squash-copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ squash_copy_get_max_compressed_size (SquashCodec* codec, size_t uncompressed_siz
static size_t
squash_copy_get_uncompressed_size (SquashCodec* codec,
size_t compressed_size,
const uint8_t compressed[SQUASH_ARRAY_PARAM(compressed_size)]) {
const uint8_t compressed[HEDLEY_ARRAY_PARAM(compressed_size)]) {
return compressed_size;
}

Expand Down Expand Up @@ -108,11 +108,11 @@ squash_copy_process_stream (SquashStream* stream, SquashOperation operation) {
static SquashStatus
squash_copy_compress_buffer (SquashCodec* codec,
size_t* compressed_size,
uint8_t compressed[SQUASH_ARRAY_PARAM(*compressed_size)],
uint8_t compressed[HEDLEY_ARRAY_PARAM(*compressed_size)],
size_t uncompressed_size,
const uint8_t uncompressed[SQUASH_ARRAY_PARAM(uncompressed_size)],
const uint8_t uncompressed[HEDLEY_ARRAY_PARAM(uncompressed_size)],
SquashOptions* options) {
if (SQUASH_UNLIKELY(*compressed_size < uncompressed_size))
if (HEDLEY_UNLIKELY(*compressed_size < uncompressed_size))
return squash_error (SQUASH_BUFFER_FULL);

memcpy (compressed, uncompressed, uncompressed_size);
Expand All @@ -124,11 +124,11 @@ squash_copy_compress_buffer (SquashCodec* codec,
static SquashStatus
squash_copy_decompress_buffer (SquashCodec* codec,
size_t* decompressed_size,
uint8_t decompressed[SQUASH_ARRAY_PARAM(*decompressed_size)],
uint8_t decompressed[HEDLEY_ARRAY_PARAM(*decompressed_size)],
size_t compressed_size,
const uint8_t compressed[SQUASH_ARRAY_PARAM(compressed_size)],
const uint8_t compressed[HEDLEY_ARRAY_PARAM(compressed_size)],
SquashOptions* options) {
if (SQUASH_UNLIKELY(*decompressed_size < compressed_size))
if (HEDLEY_UNLIKELY(*decompressed_size < compressed_size))
return squash_error (SQUASH_BUFFER_FULL);

memcpy (decompressed, compressed, compressed_size);
Expand All @@ -141,7 +141,7 @@ SquashStatus
squash_plugin_init_codec (SquashCodec* codec, SquashCodecImpl* impl) {
const char* name = squash_codec_get_name (codec);

if (SQUASH_LIKELY(strcmp ("copy", name) == 0)) {
if (HEDLEY_LIKELY(strcmp ("copy", name) == 0)) {
impl->info = SQUASH_CODEC_INFO_CAN_FLUSH;
impl->get_uncompressed_size = squash_copy_get_uncompressed_size;
impl->get_max_compressed_size = squash_copy_get_max_compressed_size;
Expand Down
Loading

0 comments on commit b9d66ea

Please sign in to comment.