-
Notifications
You must be signed in to change notification settings - Fork 33
/
BASE64Decoder.java
133 lines (117 loc) · 4.17 KB
/
BASE64Decoder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright 2021, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.util;
import java.io.OutputStream;
import java.io.InputStream;
/**
* This class implements a BASE64 Character decoder as specified in RFC1521.
*
* This RFC is part of the MIME specification which is published by the Internet Engineering Task Force (IETF). Unlike
* some other encoding schemes there is nothing in this encoding that tells the decoder where a buffer starts or stops,
* so to use it you will need to isolate your encoded data into a single chunk and then feed them this decoder. The
* simplest way to do that is to read all of the encoded data into a string and then use:
*
* <pre>
* byte mydata[];
* BASE64Decoder base64 = new BASE64Decoder();
*
* mydata = base64.decodeBuffer(bufferString);
* </pre>
*
* This will decode the String in <i>bufferString</i> and give you an array of bytes in the array <i>myData</i>.
*
* @see CharacterEncoder
* @see BASE64Decoder
*/
public class BASE64Decoder extends CharacterDecoder {
/** This class has 4 bytes per atom */
@Override
protected int bytesPerAtom() {
return (4);
}
/** Any multiple of 4 will do, 72 might be common */
@Override
protected int bytesPerLine() {
return (72);
}
private static final byte pem_convert_array[] = new byte[256];
static {
char pem_array[] = Base64PemCharArray.getPemArray();
for (int i = 0; i < 255; i++) {
pem_convert_array[i] = -1;
}
for (int i = 0; i < pem_array.length; i++) {
pem_convert_array[pem_array[i]] = (byte) i;
}
}
byte decode_buffer[] = new byte[4];
/**
* Decode one BASE64 atom into 1, 2, or 3 bytes of data.
*/
@Override
protected void decodeAtom(InputStream inStream, OutputStream outStream, int rem) throws java.io.IOException {
int i;
byte a = -1, b = -1, c = -1, d = -1;
if (rem < 2) {
throw new java.io.IOException();
}
do {
i = inStream.read();
if (i == -1) {
throw new CEStreamExhausted();
}
} while (i == '\n' || i == '\r');
decode_buffer[0] = (byte) i;
i = readFully(inStream, decode_buffer, 1, rem - 1);
if (i == -1) {
throw new CEStreamExhausted();
}
if (rem > 3 && decode_buffer[3] == '=') {
rem = 3;
}
if (rem > 2 && decode_buffer[2] == '=') {
rem = 2;
}
switch (rem) {
case 4:
d = pem_convert_array[decode_buffer[3] & 0xff];
// NOBREAK
case 3:
c = pem_convert_array[decode_buffer[2] & 0xff];
// NOBREAK
case 2:
b = pem_convert_array[decode_buffer[1] & 0xff];
a = pem_convert_array[decode_buffer[0] & 0xff];
break;
}
switch (rem) {
case 2:
outStream.write((byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)));
break;
case 3:
outStream.write((byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)));
outStream.write((byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)));
break;
case 4:
outStream.write((byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)));
outStream.write((byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)));
outStream.write((byte) (((c << 6) & 0xc0) | (d & 0x3f)));
break;
}
return;
}
}