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