View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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   * Written by Robert Harder and released to the public domain, as explained at
18   * http://creativecommons.org/licenses/publicdomain
19   */
20  package org.jboss.netty.handler.codec.base64;
21  
22  /**
23   * Enumeration of supported Base64 dialects.
24   * <p>
25   * The internal lookup tables in this class has been derived from
26   * <a href="http://iharder.sourceforge.net/current/java/base64/">Robert Harder's Public Domain Base64 Encoder/Decoder</a>.
27   *
28   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
29   * @author Robert Harder (rob@iharder.net)
30   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
31   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
32   */
33  public enum Base64Dialect {
34      /**
35       * Standard Base64 encoding as described in the Section 3 of
36       * <a href="http://www.faqs.org/rfcs/rfc3548.html">RFC3548</a>.
37       */
38      STANDARD(new byte[] {
39              (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
40              (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
41              (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O',
42              (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
43              (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y',
44              (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
45              (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i',
46              (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
47              (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's',
48              (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
49              (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2',
50              (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
51              (byte) '8', (byte) '9', (byte) '+', (byte) '/' },
52              new byte[] {
53              -9, -9, -9, -9, -9, -9,
54              -9, -9, -9, // Decimal  0 -  8
55              -5, -5, // Whitespace: Tab and Linefeed
56              -9, -9, // Decimal 11 - 12
57              -5, // Whitespace: Carriage Return
58              -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26
59              -9, -9, -9, -9, -9, // Decimal 27 - 31
60              -5, // Whitespace: Space
61              -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42
62              62, // Plus sign at decimal 43
63              -9, -9, -9, // Decimal 44 - 46
64              63, // Slash at decimal 47
65              52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero through nine
66              -9, -9, -9, // Decimal 58 - 60
67              -1, // Equals sign at decimal 61
68              -9, -9, -9, // Decimal 62 - 64
69               0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' through 'N'
70              14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' through 'Z'
71              -9, -9, -9, -9, -9, -9, // Decimal 91 - 96
72              26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' through 'm'
73              39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' through 'z'
74              -9, -9, -9, -9, // Decimal 123 - 126
75           /* -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 127 - 139
76              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 140 - 152
77              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 153 - 165
78              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 166 - 178
79              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 179 - 191
80              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 192 - 204
81              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 205 - 217
82              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 218 - 230
83              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 231 - 243
84              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9         // Decimal 244 - 255 */
85              }, true),
86      /**
87       * Base64-like encoding that is URL-safe as described in the Section 4 of
88       * <a href="http://www.faqs.org/rfcs/rfc3548.html">RFC3548</a>.  It is
89       * important to note that data encoded this way is <em>not</em> officially
90       * valid Base64, or at the very least should not be called Base64 without
91       * also specifying that is was encoded using the URL-safe dialect.
92       */
93      URL_SAFE(new byte[] {
94              (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
95              (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
96              (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O',
97              (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
98              (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y',
99              (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
100             (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i',
101             (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
102             (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's',
103             (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
104             (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2',
105             (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
106             (byte) '8', (byte) '9', (byte) '-', (byte) '_' },
107             new byte[] {
108             -9, -9, -9, -9, -9, -9,
109             -9, -9, -9, // Decimal  0 -  8
110             -5, -5, // Whitespace: Tab and Linefeed
111             -9, -9, // Decimal 11 - 12
112             -5, // Whitespace: Carriage Return
113             -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26
114             -9, -9, -9, -9, -9, // Decimal 27 - 31
115             -5, // Whitespace: Space
116             -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42
117             -9, // Plus sign at decimal 43
118             -9, // Decimal 44
119             62, // Minus sign at decimal 45
120             -9, // Decimal 46
121             -9, // Slash at decimal 47
122             52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero through nine
123             -9, -9, -9, // Decimal 58 - 60
124             -1, // Equals sign at decimal 61
125             -9, -9, -9, // Decimal 62 - 64
126              0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' through 'N'
127             14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' through 'Z'
128             -9, -9, -9, -9, // Decimal 91 - 94
129             63, // Underscore at decimal 95
130             -9, // Decimal 96
131             26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' through 'm'
132             39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' through 'z'
133             -9, -9, -9, -9, // Decimal 123 - 126
134           /*-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 127 - 139
135             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 140 - 152
136             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 153 - 165
137             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 166 - 178
138             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 179 - 191
139             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 192 - 204
140             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 205 - 217
141             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 218 - 230
142             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 231 - 243
143             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9         // Decimal 244 - 255 */
144             }, false),
145     /**
146      * Special "ordered" dialect of Base64 described in
147      * <a href="http://www.faqs.org/qa/rfcc-1940.html">RFC1940</a>.
148      */
149     ORDERED(new byte[] {
150             (byte) '-', (byte) '0', (byte) '1', (byte) '2', (byte) '3',
151             (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8',
152             (byte) '9', (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D',
153             (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I',
154             (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
155             (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S',
156             (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X',
157             (byte) 'Y', (byte) 'Z', (byte) '_', (byte) 'a', (byte) 'b',
158             (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', (byte) 'g',
159             (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l',
160             (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q',
161             (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v',
162             (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z' },
163             new byte[] {
164             -9, -9, -9, -9, -9, -9,
165             -9, -9, -9, // Decimal  0 -  8
166             -5, -5, // Whitespace: Tab and Linefeed
167             -9, -9, // Decimal 11 - 12
168             -5, // Whitespace: Carriage Return
169             -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26
170             -9, -9, -9, -9, -9, // Decimal 27 - 31
171             -5, // Whitespace: Space
172             -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42
173             -9, // Plus sign at decimal 43
174             -9, // Decimal 44
175              0, // Minus sign at decimal 45
176             -9, // Decimal 46
177             -9, // Slash at decimal 47
178              1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // Numbers zero through nine
179             -9, -9, -9, // Decimal 58 - 60
180             -1, // Equals sign at decimal 61
181             -9, -9, -9, // Decimal 62 - 64
182             11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, // Letters 'A' through 'M'
183             24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, // Letters 'N' through 'Z'
184             -9, -9, -9, -9, // Decimal 91 - 94
185             37, // Underscore at decimal 95
186             -9, // Decimal 96
187             38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, // Letters 'a' through 'm'
188             51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, // Letters 'n' through 'z'
189             -9, -9, -9, -9, // Decimal 123 - 126
190          /* -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 127 - 139
191             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 140 - 152
192             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 153 - 165
193             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 166 - 178
194             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 179 - 191
195             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 192 - 204
196             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 205 - 217
197             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 218 - 230
198             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 231 - 243
199             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9         // Decimal 244 - 255 */
200             }, true);
201 
202     final byte[] alphabet;
203     final byte[] decodabet;
204     final boolean breakLinesByDefault;
205 
206     private Base64Dialect(byte[] alphabet, byte[] decodabet, boolean breakLinesByDefault) {
207         this.alphabet = alphabet;
208         this.decodabet = decodabet;
209         this.breakLinesByDefault = breakLinesByDefault;
210     }
211 }