state.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package brotli
  2. import "io"
  3. /* Copyright 2015 Google Inc. All Rights Reserved.
  4. Distributed under MIT license.
  5. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  6. */
  7. /* Brotli state for partial streaming decoding. */
  8. const (
  9. stateUninited = iota
  10. stateLargeWindowBits
  11. stateInitialize
  12. stateMetablockBegin
  13. stateMetablockHeader
  14. stateMetablockHeader2
  15. stateContextModes
  16. stateCommandBegin
  17. stateCommandInner
  18. stateCommandPostDecodeLiterals
  19. stateCommandPostWrapCopy
  20. stateUncompressed
  21. stateMetadata
  22. stateCommandInnerWrite
  23. stateMetablockDone
  24. stateCommandPostWrite1
  25. stateCommandPostWrite2
  26. stateHuffmanCode0
  27. stateHuffmanCode1
  28. stateHuffmanCode2
  29. stateHuffmanCode3
  30. stateContextMap1
  31. stateContextMap2
  32. stateTreeGroup
  33. stateDone
  34. )
  35. const (
  36. stateMetablockHeaderNone = iota
  37. stateMetablockHeaderEmpty
  38. stateMetablockHeaderNibbles
  39. stateMetablockHeaderSize
  40. stateMetablockHeaderUncompressed
  41. stateMetablockHeaderReserved
  42. stateMetablockHeaderBytes
  43. stateMetablockHeaderMetadata
  44. )
  45. const (
  46. stateUncompressedNone = iota
  47. stateUncompressedWrite
  48. )
  49. const (
  50. stateTreeGroupNone = iota
  51. stateTreeGroupLoop
  52. )
  53. const (
  54. stateContextMapNone = iota
  55. stateContextMapReadPrefix
  56. stateContextMapHuffman
  57. stateContextMapDecode
  58. stateContextMapTransform
  59. )
  60. const (
  61. stateHuffmanNone = iota
  62. stateHuffmanSimpleSize
  63. stateHuffmanSimpleRead
  64. stateHuffmanSimpleBuild
  65. stateHuffmanComplex
  66. stateHuffmanLengthSymbols
  67. )
  68. const (
  69. stateDecodeUint8None = iota
  70. stateDecodeUint8Short
  71. stateDecodeUint8Long
  72. )
  73. const (
  74. stateReadBlockLengthNone = iota
  75. stateReadBlockLengthSuffix
  76. )
  77. type Reader struct {
  78. src io.Reader
  79. buf []byte // scratch space for reading from src
  80. in []byte // current chunk to decode; usually aliases buf
  81. state int
  82. loop_counter int
  83. br bitReader
  84. buffer struct {
  85. u64 uint64
  86. u8 [8]byte
  87. }
  88. buffer_length uint32
  89. pos int
  90. max_backward_distance int
  91. max_distance int
  92. ringbuffer_size int
  93. ringbuffer_mask int
  94. dist_rb_idx int
  95. dist_rb [4]int
  96. error_code int
  97. sub_loop_counter uint32
  98. ringbuffer []byte
  99. ringbuffer_end []byte
  100. htree_command []huffmanCode
  101. context_lookup []byte
  102. context_map_slice []byte
  103. dist_context_map_slice []byte
  104. literal_hgroup huffmanTreeGroup
  105. insert_copy_hgroup huffmanTreeGroup
  106. distance_hgroup huffmanTreeGroup
  107. block_type_trees []huffmanCode
  108. block_len_trees []huffmanCode
  109. trivial_literal_context int
  110. distance_context int
  111. meta_block_remaining_len int
  112. block_length_index uint32
  113. block_length [3]uint32
  114. num_block_types [3]uint32
  115. block_type_rb [6]uint32
  116. distance_postfix_bits uint32
  117. num_direct_distance_codes uint32
  118. distance_postfix_mask int
  119. num_dist_htrees uint32
  120. dist_context_map []byte
  121. literal_htree []huffmanCode
  122. dist_htree_index byte
  123. repeat_code_len uint32
  124. prev_code_len uint32
  125. copy_length int
  126. distance_code int
  127. rb_roundtrips uint
  128. partial_pos_out uint
  129. symbol uint32
  130. repeat uint32
  131. space uint32
  132. table [32]huffmanCode
  133. symbol_lists symbolList
  134. symbols_lists_array [huffmanMaxCodeLength + 1 + numCommandSymbols]uint16
  135. next_symbol [32]int
  136. code_length_code_lengths [codeLengthCodes]byte
  137. code_length_histo [16]uint16
  138. htree_index int
  139. next []huffmanCode
  140. context_index uint32
  141. max_run_length_prefix uint32
  142. code uint32
  143. context_map_table [huffmanMaxSize272]huffmanCode
  144. substate_metablock_header int
  145. substate_tree_group int
  146. substate_context_map int
  147. substate_uncompressed int
  148. substate_huffman int
  149. substate_decode_uint8 int
  150. substate_read_block_length int
  151. is_last_metablock uint
  152. is_uncompressed uint
  153. is_metadata uint
  154. should_wrap_ringbuffer uint
  155. canny_ringbuffer_allocation uint
  156. large_window bool
  157. size_nibbles uint
  158. window_bits uint32
  159. new_ringbuffer_size int
  160. num_literal_htrees uint32
  161. context_map []byte
  162. context_modes []byte
  163. dictionary *dictionary
  164. transforms *transforms
  165. trivial_literal_contexts [8]uint32
  166. }
  167. func decoderStateInit(s *Reader) bool {
  168. s.error_code = 0 /* BROTLI_DECODER_NO_ERROR */
  169. initBitReader(&s.br)
  170. s.state = stateUninited
  171. s.large_window = false
  172. s.substate_metablock_header = stateMetablockHeaderNone
  173. s.substate_tree_group = stateTreeGroupNone
  174. s.substate_context_map = stateContextMapNone
  175. s.substate_uncompressed = stateUncompressedNone
  176. s.substate_huffman = stateHuffmanNone
  177. s.substate_decode_uint8 = stateDecodeUint8None
  178. s.substate_read_block_length = stateReadBlockLengthNone
  179. s.buffer_length = 0
  180. s.loop_counter = 0
  181. s.pos = 0
  182. s.rb_roundtrips = 0
  183. s.partial_pos_out = 0
  184. s.block_type_trees = nil
  185. s.block_len_trees = nil
  186. s.ringbuffer_size = 0
  187. s.new_ringbuffer_size = 0
  188. s.ringbuffer_mask = 0
  189. s.context_map = nil
  190. s.context_modes = nil
  191. s.dist_context_map = nil
  192. s.context_map_slice = nil
  193. s.dist_context_map_slice = nil
  194. s.sub_loop_counter = 0
  195. s.literal_hgroup.codes = nil
  196. s.literal_hgroup.htrees = nil
  197. s.insert_copy_hgroup.codes = nil
  198. s.insert_copy_hgroup.htrees = nil
  199. s.distance_hgroup.codes = nil
  200. s.distance_hgroup.htrees = nil
  201. s.is_last_metablock = 0
  202. s.is_uncompressed = 0
  203. s.is_metadata = 0
  204. s.should_wrap_ringbuffer = 0
  205. s.canny_ringbuffer_allocation = 1
  206. s.window_bits = 0
  207. s.max_distance = 0
  208. s.dist_rb[0] = 16
  209. s.dist_rb[1] = 15
  210. s.dist_rb[2] = 11
  211. s.dist_rb[3] = 4
  212. s.dist_rb_idx = 0
  213. s.block_type_trees = nil
  214. s.block_len_trees = nil
  215. s.symbol_lists.storage = s.symbols_lists_array[:]
  216. s.symbol_lists.offset = huffmanMaxCodeLength + 1
  217. s.dictionary = getDictionary()
  218. s.transforms = getTransforms()
  219. return true
  220. }
  221. func decoderStateMetablockBegin(s *Reader) {
  222. s.meta_block_remaining_len = 0
  223. s.block_length[0] = 1 << 24
  224. s.block_length[1] = 1 << 24
  225. s.block_length[2] = 1 << 24
  226. s.num_block_types[0] = 1
  227. s.num_block_types[1] = 1
  228. s.num_block_types[2] = 1
  229. s.block_type_rb[0] = 1
  230. s.block_type_rb[1] = 0
  231. s.block_type_rb[2] = 1
  232. s.block_type_rb[3] = 0
  233. s.block_type_rb[4] = 1
  234. s.block_type_rb[5] = 0
  235. s.context_map = nil
  236. s.context_modes = nil
  237. s.dist_context_map = nil
  238. s.context_map_slice = nil
  239. s.literal_htree = nil
  240. s.dist_context_map_slice = nil
  241. s.dist_htree_index = 0
  242. s.context_lookup = nil
  243. s.literal_hgroup.codes = nil
  244. s.literal_hgroup.htrees = nil
  245. s.insert_copy_hgroup.codes = nil
  246. s.insert_copy_hgroup.htrees = nil
  247. s.distance_hgroup.codes = nil
  248. s.distance_hgroup.htrees = nil
  249. }
  250. func decoderStateCleanupAfterMetablock(s *Reader) {
  251. s.context_modes = nil
  252. s.context_map = nil
  253. s.dist_context_map = nil
  254. s.literal_hgroup.htrees = nil
  255. s.insert_copy_hgroup.htrees = nil
  256. s.distance_hgroup.htrees = nil
  257. }
  258. func decoderHuffmanTreeGroupInit(s *Reader, group *huffmanTreeGroup, alphabet_size uint32, max_symbol uint32, ntrees uint32) bool {
  259. var max_table_size uint = uint(kMaxHuffmanTableSize[(alphabet_size+31)>>5])
  260. group.alphabet_size = uint16(alphabet_size)
  261. group.max_symbol = uint16(max_symbol)
  262. group.num_htrees = uint16(ntrees)
  263. group.htrees = make([][]huffmanCode, ntrees)
  264. group.codes = make([]huffmanCode, (uint(ntrees) * max_table_size))
  265. return !(group.codes == nil)
  266. }