View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.codec.binary;
19  
20  import java.util.Objects;
21  
22  import org.apache.commons.codec.CodecPolicy;
23  
24  /**
25   * Provides Base16 encoding and decoding.
26   *
27   * <p>
28   * This class is thread-safe.
29   * </p>
30   * <p>
31   * This implementation strictly follows RFC 4648, and as such unlike the {@link Base32} and {@link Base64} implementations, it does not ignore invalid alphabet
32   * characters or whitespace, neither does it offer chunking or padding characters.
33   * </p>
34   * <p>
35   * The only additional feature above those specified in RFC 4648 is support for working with a lower-case alphabet in addition to the default upper-case
36   * alphabet.
37   * </p>
38   *
39   * @see <a href="https://tools.ietf.org/html/rfc4648#section-8">RFC 4648 - 8. Base 16 Encoding</a>
40   * @since 1.15
41   */
42  public class Base16 extends BaseNCodec {
43  
44      /**
45       * BASE16 characters are 4 bits in length. They are formed by taking an 8-bit group, which is converted into two BASE16 characters.
46       */
47      private static final int BITS_PER_ENCODED_BYTE = 4;
48      private static final int BYTES_PER_ENCODED_BLOCK = 2;
49      private static final int BYTES_PER_UNENCODED_BLOCK = 1;
50  
51      /**
52       * This array is a lookup table that translates Unicode characters drawn from the "Base16 Alphabet" (as specified in Table 5 of RFC 4648) into their 4-bit
53       * positive integer equivalents. Characters that are not in the Base16 alphabet but fall within the bounds of the array are translated to -1.
54       */
55      // @formatter:off
56      private static final byte[] UPPER_CASE_DECODE_TABLE = {
57              //  0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
58              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00-0f
59              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-1f
60              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20-2f
61               0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, // 30-3f 0-9
62              -1, 10, 11, 12, 13, 14, 15                                      // 40-46 A-F
63      };
64      // @formatter:on
65  
66      /**
67       * This array is a lookup table that translates 4-bit positive integer index values into their "Base16 Alphabet" equivalents as specified in Table 5 of RFC
68       * 4648.
69       */
70      private static final byte[] UPPER_CASE_ENCODE_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
71  
72      /**
73       * This array is a lookup table that translates Unicode characters drawn from the a lower-case "Base16 Alphabet" into their 4-bit positive integer
74       * equivalents. Characters that are not in the Base16 alphabet but fall within the bounds of the array are translated to -1.
75       */
76      // @formatter:off
77      private static final byte[] LOWER_CASE_DECODE_TABLE = {
78              //  0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
79              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00-0f
80              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-1f
81              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20-2f
82               0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, // 30-3f 0-9
83              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 40-4f
84              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 50-5f
85              -1, 10, 11, 12, 13, 14, 15                                      // 60-66 a-f
86      };
87      // @formatter:on
88  
89      /**
90       * This array is a lookup table that translates 4-bit positive integer index values into their "Base16 Alphabet" lower-case equivalents.
91       */
92      private static final byte[] LOWER_CASE_ENCODE_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
93  
94      /** Mask used to extract 4 bits, used when decoding character. */
95      private static final int MASK_4BITS = 0x0f;
96  
97      /**
98       * Decode table to use.
99       */
100     private final byte[] decodeTable;
101 
102     /**
103      * Encode table to use.
104      */
105     private final byte[] encodeTable;
106 
107     /**
108      * Constructs a Base16 codec used for decoding and encoding.
109      */
110     public Base16() {
111         this(false);
112     }
113 
114     /**
115      * Constructs a Base16 codec used for decoding and encoding.
116      *
117      * @param lowerCase if {@code true} then use a lower-case Base16 alphabet.
118      */
119     public Base16(final boolean lowerCase) {
120         this(lowerCase, DECODING_POLICY_DEFAULT);
121     }
122 
123     /**
124      * Constructs a Base16 codec used for decoding and encoding.
125      *
126      * @param lowerCase      if {@code true} then use a lower-case Base16 alphabet.
127      * @param encodeTable    the encode table.
128      * @param decodingPolicy Decoding policy.
129      */
130     private Base16(final boolean lowerCase, final byte[] encodeTable, final CodecPolicy decodingPolicy) {
131         super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, 0, 0, PAD_DEFAULT, decodingPolicy);
132         Objects.requireNonNull(encodeTable, "encodeTable");
133         this.encodeTable = encodeTable;
134         this.decodeTable = encodeTable == LOWER_CASE_ENCODE_TABLE ? LOWER_CASE_DECODE_TABLE : UPPER_CASE_DECODE_TABLE;
135     }
136 
137     /**
138      * Constructs a Base16 codec used for decoding and encoding.
139      *
140      * @param lowerCase      if {@code true} then use a lower-case Base16 alphabet.
141      * @param decodingPolicy Decoding policy.
142      */
143     public Base16(final boolean lowerCase, final CodecPolicy decodingPolicy) {
144         this(lowerCase, lowerCase ? LOWER_CASE_ENCODE_TABLE : UPPER_CASE_ENCODE_TABLE, decodingPolicy);
145     }
146 
147     @Override
148     void decode(final byte[] data, int offset, final int length, final Context context) {
149         if (context.eof || length < 0) {
150             context.eof = true;
151             if (context.ibitWorkArea != 0) {
152                 validateTrailingCharacter();
153             }
154             return;
155         }
156         final int dataLen = Math.min(data.length - offset, length);
157         final int availableChars = (context.ibitWorkArea != 0 ? 1 : 0) + dataLen;
158         // small optimization to short-cut the rest of this method when it is fed byte-by-byte
159         if (availableChars == 1 && availableChars == dataLen) {
160             // store 1/2 byte for next invocation of decode, we offset by +1 as empty-value is 0
161             context.ibitWorkArea = decodeOctet(data[offset]) + 1;
162             return;
163         }
164         // we must have an even number of chars to decode
165         final int charsToProcess = availableChars % BYTES_PER_ENCODED_BLOCK == 0 ? availableChars : availableChars - 1;
166         final int end = offset + dataLen;
167         final byte[] buffer = ensureBufferSize(charsToProcess / BYTES_PER_ENCODED_BLOCK, context);
168         int result;
169         if (dataLen < availableChars) {
170             // we have 1/2 byte from previous invocation to decode
171             result = context.ibitWorkArea - 1 << BITS_PER_ENCODED_BYTE;
172             result |= decodeOctet(data[offset++]);
173             buffer[context.pos++] = (byte) result;
174             // reset to empty-value for next invocation!
175             context.ibitWorkArea = 0;
176         }
177         final int loopEnd = end - 1;
178         while (offset < loopEnd) {
179             result = decodeOctet(data[offset++]) << BITS_PER_ENCODED_BYTE;
180             result |= decodeOctet(data[offset++]);
181             buffer[context.pos++] = (byte) result;
182         }
183         // we have one char of a hex-pair left over
184         if (offset < end) {
185             // store 1/2 byte for next invocation of decode, we offset by +1 as empty-value is 0
186             context.ibitWorkArea = decodeOctet(data[offset]) + 1;
187         }
188     }
189 
190     private int decodeOctet(final byte octet) {
191         int decoded = -1;
192         if ((octet & 0xff) < decodeTable.length) {
193             decoded = decodeTable[octet];
194         }
195         if (decoded == -1) {
196             throw new IllegalArgumentException("Invalid octet in encoded value: " + (int) octet);
197         }
198         return decoded;
199     }
200 
201     @Override
202     void encode(final byte[] data, final int offset, final int length, final Context context) {
203         if (context.eof) {
204             return;
205         }
206         if (length < 0) {
207             context.eof = true;
208             return;
209         }
210         final int size = length * BYTES_PER_ENCODED_BLOCK;
211         if (size < 0) {
212             throw new IllegalArgumentException("Input length exceeds maximum size for encoded data: " + length);
213         }
214         final byte[] buffer = ensureBufferSize(size, context);
215         final int end = offset + length;
216         for (int i = offset; i < end; i++) {
217             final int value = data[i];
218             final int high = value >> BITS_PER_ENCODED_BYTE & MASK_4BITS;
219             final int low = value & MASK_4BITS;
220             buffer[context.pos++] = encodeTable[high];
221             buffer[context.pos++] = encodeTable[low];
222         }
223     }
224 
225     /**
226      * Returns whether or not the {@code octet} is in the Base16 alphabet.
227      *
228      * @param octet The value to test.
229      * @return {@code true} if the value is defined in the Base16 alphabet {@code false} otherwise.
230      */
231     @Override
232     public boolean isInAlphabet(final byte octet) {
233         return (octet & 0xff) < decodeTable.length && decodeTable[octet] != -1;
234     }
235 
236     /**
237      * Validates whether decoding allows an entire final trailing character that cannot be used for a complete byte.
238      *
239      * @throws IllegalArgumentException if strict decoding is enabled
240      */
241     private void validateTrailingCharacter() {
242         if (isStrictDecoding()) {
243             throw new IllegalArgumentException("Strict decoding: Last encoded character is a valid base 16 alphabet character but not a possible encoding. " +
244                     "Decoding requires at least two characters to create one byte.");
245         }
246     }
247 }