aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Kliuchnikov <eustas@google.com>2016-10-19 16:27:57 +0200
committerEugene Kliuchnikov <eustas@google.com>2016-10-20 10:28:44 +0200
commitb1db6f149a9779fe5182bfdfd555fa6d3ecc246d (patch)
tree58fde6c0a413a5cbd257e8c1aca4cb591f78c8f0
parent74147a1a41c21c1718bbac002f3f07aa6c70659e (diff)
downloadbrotli-b1db6f149a9779fe5182bfdfd555fa6d3ecc246d.zip
brotli-b1db6f149a9779fe5182bfdfd555fa6d3ecc246d.tar.gz
brotli-b1db6f149a9779fe5182bfdfd555fa6d3ecc246d.tar.bz2
Fix `-Wcast-align` warnings
-rw-r--r--dec/decode.c30
-rw-r--r--dec/state.c14
-rw-r--r--dec/state.h4
3 files changed, 25 insertions, 23 deletions
diff --git a/dec/decode.c b/dec/decode.c
index 56f79d1..9c74733 100644
--- a/dec/decode.c
+++ b/dec/decode.c
@@ -851,37 +851,38 @@ static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength(
static BROTLI_NOINLINE void InverseMoveToFrontTransform(
uint8_t* v, uint32_t v_len, BrotliDecoderState* state) {
/* Reinitialize elements that could have been changed. */
- uint32_t i = 4;
+ uint32_t i = 1;
uint32_t upper_bound = state->mtf_upper_bound;
- uint8_t* mtf = &state->mtf[4]; /* Make mtf[-1] addressable. */
+ uint32_t* mtf = &state->mtf[1]; /* Make mtf[-1] addressable. */
+ uint8_t* mtf_u8 = (uint8_t*)mtf;
/* Load endian-aware constant. */
const uint8_t b0123[4] = {0, 1, 2, 3};
uint32_t pattern;
memcpy(&pattern, &b0123, 4);
/* Initialize list using 4 consequent values pattern. */
- *(uint32_t*)mtf = pattern;
+ mtf[0] = pattern;
do {
pattern += 0x04040404; /* Advance all 4 values by 4. */
- *(uint32_t*)(mtf + i) = pattern;
- i += 4;
+ mtf[i] = pattern;
+ i++;
} while (i <= upper_bound);
/* Transform the input. */
upper_bound = 0;
for (i = 0; i < v_len; ++i) {
int index = v[i];
- uint8_t value = mtf[index];
+ uint8_t value = mtf_u8[index];
upper_bound |= v[i];
v[i] = value;
- mtf[-1] = value;
+ mtf_u8[-1] = value;
do {
index--;
- mtf[index + 1] = mtf[index];
+ mtf_u8[index + 1] = mtf_u8[index];
} while (index >= 0);
}
/* Remember amount of elements to be reinitialized. */
- state->mtf_upper_bound = upper_bound;
+ state->mtf_upper_bound = upper_bound >> 2;
}
/* Decodes a series of Huffman table using ReadHuffmanCode function. */
@@ -2142,24 +2143,23 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
{
uint32_t num_distance_codes = s->num_direct_distance_codes +
((2 * BROTLI_MAX_DISTANCE_BITS) << s->distance_postfix_bits);
+ BROTLI_BOOL allocation_success = BROTLI_TRUE;
result = DecodeContextMap(
s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS,
&s->num_dist_htrees, &s->dist_context_map, s);
if (result != BROTLI_DECODER_SUCCESS) {
break;
}
- BrotliDecoderHuffmanTreeGroupInit(
+ allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS,
s->num_literal_htrees);
- BrotliDecoderHuffmanTreeGroupInit(
+ allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS,
s->num_block_types[1]);
- BrotliDecoderHuffmanTreeGroupInit(
+ allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
s, &s->distance_hgroup, num_distance_codes,
s->num_dist_htrees);
- if (s->literal_hgroup.codes == 0 ||
- s->insert_copy_hgroup.codes == 0 ||
- s->distance_hgroup.codes == 0) {
+ if (!allocation_success) {
return SaveErrorCode(s,
BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS));
}
diff --git a/dec/state.c b/dec/state.c
index 6521874..0fd0a86 100644
--- a/dec/state.c
+++ b/dec/state.c
@@ -97,7 +97,7 @@ void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s,
/* Make small negative indexes addressable. */
s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
- s->mtf_upper_bound = 255;
+ s->mtf_upper_bound = 63;
}
void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
@@ -148,22 +148,24 @@ void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
BROTLI_FREE(s, s->block_type_trees);
}
-void BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
+BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) {
/* Pack two allocations into one */
const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];
const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
- char* p = (char*)BROTLI_ALLOC(s, code_size + htree_size);
+ /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
+ HuffmanCode** p = (HuffmanCode**)BROTLI_ALLOC(s, code_size + htree_size);
group->alphabet_size = (uint16_t)alphabet_size;
group->num_htrees = (uint16_t)ntrees;
- group->codes = (HuffmanCode*)p;
- group->htrees = (HuffmanCode**)(p + code_size);
+ group->htrees = (HuffmanCode**)p;
+ group->codes = (HuffmanCode*)(&p[ntrees]);
+ return !!p;
}
void BrotliDecoderHuffmanTreeGroupRelease(
BrotliDecoderState* s, HuffmanTreeGroup* group) {
- BROTLI_FREE(s, group->codes);
+ BROTLI_FREE(s, group->htrees);
group->htrees = NULL;
}
diff --git a/dec/state.h b/dec/state.h
index de97840..d96f447 100644
--- a/dec/state.h
+++ b/dec/state.h
@@ -192,7 +192,7 @@ struct BrotliDecoderStateStruct {
/* For InverseMoveToFrontTransform */
uint32_t mtf_upper_bound;
- uint8_t mtf[256 + 4];
+ uint32_t mtf[64 + 1];
/* For custom dictionaries */
const uint8_t* custom_dict;
@@ -235,7 +235,7 @@ BROTLI_INTERNAL void BrotliDecoderStateCleanup(BrotliDecoderState* s);
BROTLI_INTERNAL void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s);
BROTLI_INTERNAL void BrotliDecoderStateCleanupAfterMetablock(
BrotliDecoderState* s);
-BROTLI_INTERNAL void BrotliDecoderHuffmanTreeGroupInit(
+BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(
BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size,
uint32_t ntrees);
BROTLI_INTERNAL void BrotliDecoderHuffmanTreeGroupRelease(