View Javadoc
1   /*
2    * Copyright 2015 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    *   http://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  
17  /*
18   * Copyright 2014 Twitter, Inc.
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *     http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package io.netty.handler.codec.http2;
33  
34  import io.netty.util.AsciiString;
35  import io.netty.util.internal.ConstantTimeUtils;
36  import io.netty.util.internal.PlatformDependent;
37  
38  final class HpackUtil {
39      /**
40       * Compare two {@link CharSequence} objects without leaking timing information.
41       * <p>
42       * The {@code int} return type is intentional and is designed to allow cascading of constant time operations:
43       * <pre>
44       *     String s1 = "foo";
45       *     String s2 = "foo";
46       *     String s3 = "foo";
47       *     String s4 = "goo";
48       *     boolean equals = (equalsConstantTime(s1, s2) & equalsConstantTime(s3, s4)) != 0;
49       * </pre>
50       * @param s1 the first value.
51       * @param s2 the second value.
52       * @return {@code 0} if not equal. {@code 1} if equal.
53       */
54      static int equalsConstantTime(CharSequence s1, CharSequence s2) {
55          if (s1 instanceof AsciiString && s2 instanceof AsciiString) {
56              if (s1.length() != s2.length()) {
57                  return 0;
58              }
59              AsciiString s1Ascii = (AsciiString) s1;
60              AsciiString s2Ascii = (AsciiString) s2;
61              return PlatformDependent.equalsConstantTime(s1Ascii.array(), s1Ascii.arrayOffset(),
62                                                          s2Ascii.array(), s2Ascii.arrayOffset(), s1.length());
63          }
64  
65          return ConstantTimeUtils.equalsConstantTime(s1, s2);
66      }
67  
68      // Section 6.2. Literal Header Field Representation
69      enum IndexType {
70          INCREMENTAL, // Section 6.2.1. Literal Header Field with Incremental Indexing
71          NONE,        // Section 6.2.2. Literal Header Field without Indexing
72          NEVER        // Section 6.2.3. Literal Header Field never Indexed
73      }
74  
75      // Appendix B: Huffman Codes
76      // http://tools.ietf.org/html/rfc7541#appendix-B
77      static final int[] HUFFMAN_CODES = {
78              0x1ff8,
79              0x7fffd8,
80              0xfffffe2,
81              0xfffffe3,
82              0xfffffe4,
83              0xfffffe5,
84              0xfffffe6,
85              0xfffffe7,
86              0xfffffe8,
87              0xffffea,
88              0x3ffffffc,
89              0xfffffe9,
90              0xfffffea,
91              0x3ffffffd,
92              0xfffffeb,
93              0xfffffec,
94              0xfffffed,
95              0xfffffee,
96              0xfffffef,
97              0xffffff0,
98              0xffffff1,
99              0xffffff2,
100             0x3ffffffe,
101             0xffffff3,
102             0xffffff4,
103             0xffffff5,
104             0xffffff6,
105             0xffffff7,
106             0xffffff8,
107             0xffffff9,
108             0xffffffa,
109             0xffffffb,
110             0x14,
111             0x3f8,
112             0x3f9,
113             0xffa,
114             0x1ff9,
115             0x15,
116             0xf8,
117             0x7fa,
118             0x3fa,
119             0x3fb,
120             0xf9,
121             0x7fb,
122             0xfa,
123             0x16,
124             0x17,
125             0x18,
126             0x0,
127             0x1,
128             0x2,
129             0x19,
130             0x1a,
131             0x1b,
132             0x1c,
133             0x1d,
134             0x1e,
135             0x1f,
136             0x5c,
137             0xfb,
138             0x7ffc,
139             0x20,
140             0xffb,
141             0x3fc,
142             0x1ffa,
143             0x21,
144             0x5d,
145             0x5e,
146             0x5f,
147             0x60,
148             0x61,
149             0x62,
150             0x63,
151             0x64,
152             0x65,
153             0x66,
154             0x67,
155             0x68,
156             0x69,
157             0x6a,
158             0x6b,
159             0x6c,
160             0x6d,
161             0x6e,
162             0x6f,
163             0x70,
164             0x71,
165             0x72,
166             0xfc,
167             0x73,
168             0xfd,
169             0x1ffb,
170             0x7fff0,
171             0x1ffc,
172             0x3ffc,
173             0x22,
174             0x7ffd,
175             0x3,
176             0x23,
177             0x4,
178             0x24,
179             0x5,
180             0x25,
181             0x26,
182             0x27,
183             0x6,
184             0x74,
185             0x75,
186             0x28,
187             0x29,
188             0x2a,
189             0x7,
190             0x2b,
191             0x76,
192             0x2c,
193             0x8,
194             0x9,
195             0x2d,
196             0x77,
197             0x78,
198             0x79,
199             0x7a,
200             0x7b,
201             0x7ffe,
202             0x7fc,
203             0x3ffd,
204             0x1ffd,
205             0xffffffc,
206             0xfffe6,
207             0x3fffd2,
208             0xfffe7,
209             0xfffe8,
210             0x3fffd3,
211             0x3fffd4,
212             0x3fffd5,
213             0x7fffd9,
214             0x3fffd6,
215             0x7fffda,
216             0x7fffdb,
217             0x7fffdc,
218             0x7fffdd,
219             0x7fffde,
220             0xffffeb,
221             0x7fffdf,
222             0xffffec,
223             0xffffed,
224             0x3fffd7,
225             0x7fffe0,
226             0xffffee,
227             0x7fffe1,
228             0x7fffe2,
229             0x7fffe3,
230             0x7fffe4,
231             0x1fffdc,
232             0x3fffd8,
233             0x7fffe5,
234             0x3fffd9,
235             0x7fffe6,
236             0x7fffe7,
237             0xffffef,
238             0x3fffda,
239             0x1fffdd,
240             0xfffe9,
241             0x3fffdb,
242             0x3fffdc,
243             0x7fffe8,
244             0x7fffe9,
245             0x1fffde,
246             0x7fffea,
247             0x3fffdd,
248             0x3fffde,
249             0xfffff0,
250             0x1fffdf,
251             0x3fffdf,
252             0x7fffeb,
253             0x7fffec,
254             0x1fffe0,
255             0x1fffe1,
256             0x3fffe0,
257             0x1fffe2,
258             0x7fffed,
259             0x3fffe1,
260             0x7fffee,
261             0x7fffef,
262             0xfffea,
263             0x3fffe2,
264             0x3fffe3,
265             0x3fffe4,
266             0x7ffff0,
267             0x3fffe5,
268             0x3fffe6,
269             0x7ffff1,
270             0x3ffffe0,
271             0x3ffffe1,
272             0xfffeb,
273             0x7fff1,
274             0x3fffe7,
275             0x7ffff2,
276             0x3fffe8,
277             0x1ffffec,
278             0x3ffffe2,
279             0x3ffffe3,
280             0x3ffffe4,
281             0x7ffffde,
282             0x7ffffdf,
283             0x3ffffe5,
284             0xfffff1,
285             0x1ffffed,
286             0x7fff2,
287             0x1fffe3,
288             0x3ffffe6,
289             0x7ffffe0,
290             0x7ffffe1,
291             0x3ffffe7,
292             0x7ffffe2,
293             0xfffff2,
294             0x1fffe4,
295             0x1fffe5,
296             0x3ffffe8,
297             0x3ffffe9,
298             0xffffffd,
299             0x7ffffe3,
300             0x7ffffe4,
301             0x7ffffe5,
302             0xfffec,
303             0xfffff3,
304             0xfffed,
305             0x1fffe6,
306             0x3fffe9,
307             0x1fffe7,
308             0x1fffe8,
309             0x7ffff3,
310             0x3fffea,
311             0x3fffeb,
312             0x1ffffee,
313             0x1ffffef,
314             0xfffff4,
315             0xfffff5,
316             0x3ffffea,
317             0x7ffff4,
318             0x3ffffeb,
319             0x7ffffe6,
320             0x3ffffec,
321             0x3ffffed,
322             0x7ffffe7,
323             0x7ffffe8,
324             0x7ffffe9,
325             0x7ffffea,
326             0x7ffffeb,
327             0xffffffe,
328             0x7ffffec,
329             0x7ffffed,
330             0x7ffffee,
331             0x7ffffef,
332             0x7fffff0,
333             0x3ffffee,
334             0x3fffffff // EOS
335     };
336 
337     static final byte[] HUFFMAN_CODE_LENGTHS = {
338             13, 23, 28, 28, 28, 28, 28, 28, 28, 24, 30, 28, 28, 30, 28, 28,
339             28, 28, 28, 28, 28, 28, 30, 28, 28, 28, 28, 28, 28, 28, 28, 28,
340             6, 10, 10, 12, 13, 6, 8, 11, 10, 10, 8, 11, 8, 6, 6, 6,
341             5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 8, 15, 6, 12, 10,
342             13, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
343             7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 8, 13, 19, 13, 14, 6,
344             15, 5, 6, 5, 6, 5, 6, 6, 6, 5, 7, 7, 6, 6, 6, 5,
345             6, 7, 6, 5, 5, 6, 7, 7, 7, 7, 7, 15, 11, 14, 13, 28,
346             20, 22, 20, 20, 22, 22, 22, 23, 22, 23, 23, 23, 23, 23, 24, 23,
347             24, 24, 22, 23, 24, 23, 23, 23, 23, 21, 22, 23, 22, 23, 23, 24,
348             22, 21, 20, 22, 22, 23, 23, 21, 23, 22, 22, 24, 21, 22, 23, 23,
349             21, 21, 22, 21, 23, 22, 23, 23, 20, 22, 22, 22, 23, 22, 22, 23,
350             26, 26, 20, 19, 22, 23, 22, 25, 26, 26, 26, 27, 27, 26, 24, 25,
351             19, 21, 26, 27, 27, 26, 27, 24, 21, 21, 26, 26, 28, 27, 27, 27,
352             20, 24, 20, 21, 22, 21, 21, 23, 22, 22, 25, 25, 24, 24, 26, 23,
353             26, 27, 26, 26, 27, 27, 27, 27, 27, 28, 27, 27, 27, 27, 27, 26,
354             30 // EOS
355     };
356 
357     static final int HUFFMAN_EOS = 256;
358 
359     private HpackUtil() {
360         // utility class
361     }
362 }