View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.compression;
17  
18  /**
19   * Constants for both the {@link Bzip2Encoder} and the {@link Bzip2Decoder}.
20   */
21  final class Bzip2Constants {
22  
23      /**
24       * Magic number of Bzip2 stream.
25       */
26      static final int MAGIC_NUMBER = 'B' << 16 | 'Z' << 8 | 'h';
27  
28      /**
29       * Block header magic number. Equals to BCD (pi).
30       */
31      static final int BLOCK_HEADER_MAGIC_1 = 0x314159;
32      static final int BLOCK_HEADER_MAGIC_2 = 0x265359;
33  
34      /**
35       * End of stream magic number. Equals to BCD sqrt(pi).
36       */
37      static final int END_OF_STREAM_MAGIC_1 = 0x177245;
38      static final int END_OF_STREAM_MAGIC_2 = 0x385090;
39  
40      /**
41       * Base block size.
42       */
43      static final int BASE_BLOCK_SIZE = 100000;
44  
45      /**
46       * Minimum and maximum size of one block.
47       * Must be multiplied by {@link Bzip2Constants#BASE_BLOCK_SIZE}.
48       */
49      static final int MIN_BLOCK_SIZE = 1;
50      static final int MAX_BLOCK_SIZE = 9;
51  
52      static final int MAX_BLOCK_LENGTH = MAX_BLOCK_SIZE * BASE_BLOCK_SIZE;
53  
54      /**
55       * Maximum possible Huffman alphabet size.
56       */
57      static final int HUFFMAN_MAX_ALPHABET_SIZE = 258;
58  
59      /**
60       * The longest Huffman code length created by the encoder.
61       */
62      static final int HUFFMAN_ENCODE_MAX_CODE_LENGTH = 20;
63  
64      /**
65       * The longest Huffman code length accepted by the decoder.
66       */
67      static final int HUFFMAN_DECODE_MAX_CODE_LENGTH = 23;
68  
69      /**
70       * Huffman symbols used for run-length encoding.
71       */
72      static final int HUFFMAN_SYMBOL_RUNA = 0;
73      static final int HUFFMAN_SYMBOL_RUNB = 1;
74  
75      /**
76       * Huffman symbols range size for Huffman used map.
77       */
78      static final int HUFFMAN_SYMBOL_RANGE_SIZE = 16;
79  
80      /**
81       * Maximum length of zero-terminated bit runs of MTF'ed Huffman table.
82       */
83      static final int HUFFMAN_SELECTOR_LIST_MAX_LENGTH = 6;
84  
85      /**
86       * Number of symbols decoded after which a new Huffman table is selected.
87       */
88      static final int HUFFMAN_GROUP_RUN_LENGTH = 50;
89  
90      /**
91       * Maximum possible number of Huffman table selectors.
92       */
93      static final int MAX_SELECTORS = 2 + 900000 / HUFFMAN_GROUP_RUN_LENGTH; // 18002
94  
95      /**
96       * Minimum number of alternative Huffman tables.
97       */
98      static final int HUFFMAN_MINIMUM_TABLES = 2;
99  
100     /**
101      * Maximum number of alternative Huffman tables.
102      */
103     static final int HUFFMAN_MAXIMUM_TABLES = 6;
104 
105     private Bzip2Constants() { }
106 }