comparison src/ch/ethz/ssh2/crypto/Base64.java @ 307:071eccdff8ea ganymed

fix java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:16:58 -0700
parents 91a31873c42a
children
comparison
equal deleted inserted replaced
305:d2b303406d63 307:071eccdff8ea
7 import java.io.CharArrayWriter; 7 import java.io.CharArrayWriter;
8 import java.io.IOException; 8 import java.io.IOException;
9 9
10 /** 10 /**
11 * Basic Base64 Support. 11 * Basic Base64 Support.
12 * 12 *
13 * @author Christian Plattner 13 * @author Christian Plattner
14 * @version 2.50, 03/15/10 14 * @version 2.50, 03/15/10
15 */ 15 */
16 public class Base64 16 public class Base64 {
17 { 17 static final char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
18 static final char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
19
20 public static char[] encode(byte[] content)
21 {
22 CharArrayWriter cw = new CharArrayWriter((4 * content.length) / 3);
23 18
24 int idx = 0; 19 public static char[] encode(byte[] content) {
25 20 CharArrayWriter cw = new CharArrayWriter((4 * content.length) / 3);
26 int x = 0; 21 int idx = 0;
22 int x = 0;
27 23
28 for (int i = 0; i < content.length; i++) 24 for (int i = 0; i < content.length; i++) {
29 { 25 if (idx == 0)
30 if (idx == 0) 26 x = (content[i] & 0xff) << 16;
31 x = (content[i] & 0xff) << 16; 27 else if (idx == 1)
32 else if (idx == 1) 28 x = x | ((content[i] & 0xff) << 8);
33 x = x | ((content[i] & 0xff) << 8); 29 else
34 else 30 x = x | (content[i] & 0xff);
35 x = x | (content[i] & 0xff);
36 31
37 idx++; 32 idx++;
38 33
39 if (idx == 3) 34 if (idx == 3) {
40 { 35 cw.write(alphabet[x >> 18]);
41 cw.write(alphabet[x >> 18]); 36 cw.write(alphabet[(x >> 12) & 0x3f]);
42 cw.write(alphabet[(x >> 12) & 0x3f]); 37 cw.write(alphabet[(x >> 6) & 0x3f]);
43 cw.write(alphabet[(x >> 6) & 0x3f]); 38 cw.write(alphabet[x & 0x3f]);
44 cw.write(alphabet[x & 0x3f]); 39 idx = 0;
40 }
41 }
45 42
46 idx = 0; 43 if (idx == 1) {
47 } 44 cw.write(alphabet[x >> 18]);
48 } 45 cw.write(alphabet[(x >> 12) & 0x3f]);
46 cw.write('=');
47 cw.write('=');
48 }
49 49
50 if (idx == 1) 50 if (idx == 2) {
51 { 51 cw.write(alphabet[x >> 18]);
52 cw.write(alphabet[x >> 18]); 52 cw.write(alphabet[(x >> 12) & 0x3f]);
53 cw.write(alphabet[(x >> 12) & 0x3f]); 53 cw.write(alphabet[(x >> 6) & 0x3f]);
54 cw.write('='); 54 cw.write('=');
55 cw.write('='); 55 }
56 }
57 56
58 if (idx == 2) 57 return cw.toCharArray();
59 { 58 }
60 cw.write(alphabet[x >> 18]);
61 cw.write(alphabet[(x >> 12) & 0x3f]);
62 cw.write(alphabet[(x >> 6) & 0x3f]);
63 cw.write('=');
64 }
65 59
66 return cw.toCharArray(); 60 public static byte[] decode(char[] message) throws IOException {
67 } 61 byte buff[] = new byte[4];
62 byte dest[] = new byte[message.length];
63 int bpos = 0;
64 int destpos = 0;
68 65
69 public static byte[] decode(char[] message) throws IOException 66 for (int i = 0; i < message.length; i++) {
70 { 67 int c = message[i];
71 byte buff[] = new byte[4];
72 byte dest[] = new byte[message.length];
73 68
74 int bpos = 0; 69 if ((c == '\n') || (c == '\r') || (c == ' ') || (c == '\t'))
75 int destpos = 0; 70 continue;
76 71
77 for (int i = 0; i < message.length; i++) 72 if ((c >= 'A') && (c <= 'Z')) {
78 { 73 buff[bpos++] = (byte)(c - 'A');
79 int c = message[i]; 74 }
75 else if ((c >= 'a') && (c <= 'z')) {
76 buff[bpos++] = (byte)((c - 'a') + 26);
77 }
78 else if ((c >= '0') && (c <= '9')) {
79 buff[bpos++] = (byte)((c - '0') + 52);
80 }
81 else if (c == '+') {
82 buff[bpos++] = 62;
83 }
84 else if (c == '/') {
85 buff[bpos++] = 63;
86 }
87 else if (c == '=') {
88 buff[bpos++] = 64;
89 }
90 else {
91 throw new IOException("Illegal char in base64 code.");
92 }
80 93
81 if ((c == '\n') || (c == '\r') || (c == ' ') || (c == '\t')) 94 if (bpos == 4) {
82 continue; 95 bpos = 0;
83 96
84 if ((c >= 'A') && (c <= 'Z')) 97 if (buff[0] == 64)
85 { 98 break;
86 buff[bpos++] = (byte) (c - 'A');
87 }
88 else if ((c >= 'a') && (c <= 'z'))
89 {
90 buff[bpos++] = (byte) ((c - 'a') + 26);
91 }
92 else if ((c >= '0') && (c <= '9'))
93 {
94 buff[bpos++] = (byte) ((c - '0') + 52);
95 }
96 else if (c == '+')
97 {
98 buff[bpos++] = 62;
99 }
100 else if (c == '/')
101 {
102 buff[bpos++] = 63;
103 }
104 else if (c == '=')
105 {
106 buff[bpos++] = 64;
107 }
108 else
109 {
110 throw new IOException("Illegal char in base64 code.");
111 }
112 99
113 if (bpos == 4) 100 if (buff[1] == 64)
114 { 101 throw new IOException("Unexpected '=' in base64 code.");
115 bpos = 0;
116 102
117 if (buff[0] == 64) 103 if (buff[2] == 64) {
118 break; 104 int v = (((buff[0] & 0x3f) << 6) | ((buff[1] & 0x3f)));
105 dest[destpos++] = (byte)(v >> 4);
106 break;
107 }
108 else if (buff[3] == 64) {
109 int v = (((buff[0] & 0x3f) << 12) | ((buff[1] & 0x3f) << 6) | ((buff[2] & 0x3f)));
110 dest[destpos++] = (byte)(v >> 10);
111 dest[destpos++] = (byte)(v >> 2);
112 break;
113 }
114 else {
115 int v = (((buff[0] & 0x3f) << 18) | ((buff[1] & 0x3f) << 12) | ((buff[2] & 0x3f) << 6) | ((buff[3] & 0x3f)));
116 dest[destpos++] = (byte)(v >> 16);
117 dest[destpos++] = (byte)(v >> 8);
118 dest[destpos++] = (byte)(v);
119 }
120 }
121 }
119 122
120 if (buff[1] == 64) 123 byte[] res = new byte[destpos];
121 throw new IOException("Unexpected '=' in base64 code."); 124 System.arraycopy(dest, 0, res, 0, destpos);
122 125 return res;
123 if (buff[2] == 64) 126 }
124 {
125 int v = (((buff[0] & 0x3f) << 6) | ((buff[1] & 0x3f)));
126 dest[destpos++] = (byte) (v >> 4);
127 break;
128 }
129 else if (buff[3] == 64)
130 {
131 int v = (((buff[0] & 0x3f) << 12) | ((buff[1] & 0x3f) << 6) | ((buff[2] & 0x3f)));
132 dest[destpos++] = (byte) (v >> 10);
133 dest[destpos++] = (byte) (v >> 2);
134 break;
135 }
136 else
137 {
138 int v = (((buff[0] & 0x3f) << 18) | ((buff[1] & 0x3f) << 12) | ((buff[2] & 0x3f) << 6) | ((buff[3] & 0x3f)));
139 dest[destpos++] = (byte) (v >> 16);
140 dest[destpos++] = (byte) (v >> 8);
141 dest[destpos++] = (byte) (v);
142 }
143 }
144 }
145
146 byte[] res = new byte[destpos];
147 System.arraycopy(dest, 0, res, 0, destpos);
148
149 return res;
150 }
151 } 127 }