Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/modules/brotli/dec/state.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2015 Google Inc. All Rights Reserved.
2
3
   Distributed under MIT license.
4
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
*/
6
7
#include "./state.h"
8
9
#include <stdlib.h>  /* free, malloc */
10
11
#include <brotli/types.h>
12
#include "./huffman.h"
13
14
#if defined(__cplusplus) || defined(c_plusplus)
15
extern "C" {
16
#endif
17
18
BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
19
0
    brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
20
0
  if (!alloc_func) {
21
0
    s->alloc_func = BrotliDefaultAllocFunc;
22
0
    s->free_func = BrotliDefaultFreeFunc;
23
0
    s->memory_manager_opaque = 0;
24
0
  } else {
25
0
    s->alloc_func = alloc_func;
26
0
    s->free_func = free_func;
27
0
    s->memory_manager_opaque = opaque;
28
0
  }
29
0
30
0
  s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */
31
0
32
0
  BrotliInitBitReader(&s->br);
33
0
  s->state = BROTLI_STATE_UNINITED;
34
0
  s->large_window = 0;
35
0
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
36
0
  s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
37
0
  s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
38
0
  s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
39
0
  s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
40
0
  s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
41
0
  s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
42
0
43
0
  s->buffer_length = 0;
44
0
  s->loop_counter = 0;
45
0
  s->pos = 0;
46
0
  s->rb_roundtrips = 0;
47
0
  s->partial_pos_out = 0;
48
0
49
0
  s->block_type_trees = NULL;
50
0
  s->block_len_trees = NULL;
51
0
  s->ringbuffer = NULL;
52
0
  s->ringbuffer_size = 0;
53
0
  s->new_ringbuffer_size = 0;
54
0
  s->ringbuffer_mask = 0;
55
0
56
0
  s->context_map = NULL;
57
0
  s->context_modes = NULL;
58
0
  s->dist_context_map = NULL;
59
0
  s->context_map_slice = NULL;
60
0
  s->dist_context_map_slice = NULL;
61
0
62
0
  s->sub_loop_counter = 0;
63
0
64
0
  s->literal_hgroup.codes = NULL;
65
0
  s->literal_hgroup.htrees = NULL;
66
0
  s->insert_copy_hgroup.codes = NULL;
67
0
  s->insert_copy_hgroup.htrees = NULL;
68
0
  s->distance_hgroup.codes = NULL;
69
0
  s->distance_hgroup.htrees = NULL;
70
0
71
0
  s->is_last_metablock = 0;
72
0
  s->is_uncompressed = 0;
73
0
  s->is_metadata = 0;
74
0
  s->should_wrap_ringbuffer = 0;
75
0
  s->canny_ringbuffer_allocation = 1;
76
0
77
0
  s->window_bits = 0;
78
0
  s->max_distance = 0;
79
0
  s->dist_rb[0] = 16;
80
0
  s->dist_rb[1] = 15;
81
0
  s->dist_rb[2] = 11;
82
0
  s->dist_rb[3] = 4;
83
0
  s->dist_rb_idx = 0;
84
0
  s->block_type_trees = NULL;
85
0
  s->block_len_trees = NULL;
86
0
87
0
  /* Make small negative indexes addressable. */
88
0
  s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
89
0
90
0
  s->mtf_upper_bound = 63;
91
0
92
0
  s->dictionary = BrotliGetDictionary();
93
0
  s->transforms = BrotliGetTransforms();
94
0
95
0
  return BROTLI_TRUE;
96
0
}
97
98
0
void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
99
0
  s->meta_block_remaining_len = 0;
100
0
  s->block_length[0] = 1U << 24;
101
0
  s->block_length[1] = 1U << 24;
102
0
  s->block_length[2] = 1U << 24;
103
0
  s->num_block_types[0] = 1;
104
0
  s->num_block_types[1] = 1;
105
0
  s->num_block_types[2] = 1;
106
0
  s->block_type_rb[0] = 1;
107
0
  s->block_type_rb[1] = 0;
108
0
  s->block_type_rb[2] = 1;
109
0
  s->block_type_rb[3] = 0;
110
0
  s->block_type_rb[4] = 1;
111
0
  s->block_type_rb[5] = 0;
112
0
  s->context_map = NULL;
113
0
  s->context_modes = NULL;
114
0
  s->dist_context_map = NULL;
115
0
  s->context_map_slice = NULL;
116
0
  s->literal_htree = NULL;
117
0
  s->dist_context_map_slice = NULL;
118
0
  s->dist_htree_index = 0;
119
0
  s->context_lookup = NULL;
120
0
  s->literal_hgroup.codes = NULL;
121
0
  s->literal_hgroup.htrees = NULL;
122
0
  s->insert_copy_hgroup.codes = NULL;
123
0
  s->insert_copy_hgroup.htrees = NULL;
124
0
  s->distance_hgroup.codes = NULL;
125
0
  s->distance_hgroup.htrees = NULL;
126
0
}
127
128
0
void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
129
0
  BROTLI_DECODER_FREE(s, s->context_modes);
130
0
  BROTLI_DECODER_FREE(s, s->context_map);
131
0
  BROTLI_DECODER_FREE(s, s->dist_context_map);
132
0
  BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees);
133
0
  BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees);
134
0
  BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees);
135
0
}
136
137
0
void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
138
0
  BrotliDecoderStateCleanupAfterMetablock(s);
139
0
140
0
  BROTLI_DECODER_FREE(s, s->ringbuffer);
141
0
  BROTLI_DECODER_FREE(s, s->block_type_trees);
142
0
}
143
144
BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
145
    HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t max_symbol,
146
0
    uint32_t ntrees) {
147
0
  /* Pack two allocations into one */
148
0
  const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];
149
0
  const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
150
0
  const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
151
0
  /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
152
0
  HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,
153
0
      code_size + htree_size);
154
0
  group->alphabet_size = (uint16_t)alphabet_size;
155
0
  group->max_symbol = (uint16_t)max_symbol;
156
0
  group->num_htrees = (uint16_t)ntrees;
157
0
  group->htrees = p;
158
0
  group->codes = (HuffmanCode*)(&p[ntrees]);
159
0
  return !!p;
160
0
}
161
162
#if defined(__cplusplus) || defined(c_plusplus)
163
}  /* extern "C" */
164
#endif