Package: Base64

Base64

nameinstructionbranchcomplexitylinemethod
Base64()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
b64decode(char)
M: 48 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
b64encode(int)
M: 48 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
decode(String)
M: 166 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%
encode(byte[])
M: 194 C: 0
0%
M: 6 C: 0
0%
M: 5 C: 0
0%
M: 32 C: 0
0%
M: 1 C: 0
0%
main(String[])
M: 123 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 29 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (C) 2008 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ is free software: you can redistribute it and/or modify
10: *        it under the terms of the GNU General Public License as published by
11: *        the Free Software Foundation, either version 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ is distributed in the hope that it will be useful,
15: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
16: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: package org.overture.util;
25:
26: import java.io.BufferedReader;
27: import java.io.InputStreamReader;
28: import java.nio.charset.Charset;
29: import java.util.regex.Matcher;
30: import java.util.regex.Pattern;
31:
32: /**
33: * A class to manage base64 encoding and decoding.
34: */
35:
36: public class Base64
37: {
38:         /**
39:          * Return the 6-bit value corresponding to a base64 encoded char. If the character is the base64 padding character,
40:          * '=', -1 is returned.
41:          *
42:          * @param c
43:          * The character to decode
44:          * @return The decoded 6-bit value, or -1 for padding.
45:          */
46:
47:         private static int b64decode(char c)
48:         {
49:                 int i = c;
50:
51:•                if (i >= 'A' && i <= 'Z')
52:                 {
53:                         return i - 'A';
54:                 }
55:
56:•                if (i >= 'a' && i <= 'z')
57:                 {
58:                         return i - 'a' + 26;
59:                 }
60:
61:•                if (i >= '0' && i <= '9')
62:                 {
63:                         return i - '0' + 52;
64:                 }
65:
66:•                if (i == '+')
67:                 {
68:                         return 62;
69:                 }
70:
71:•                if (i == '/')
72:                 {
73:                         return 63;
74:                 }
75:
76:                 return -1; // padding '='
77:         }
78:
79:         /**
80:          * Encode a 6-bit quantity as a base64 character.
81:          *
82:          * @param b
83:          * The 6-bit quantity. return The character.
84:          */
85:
86:         private static char b64encode(int b)
87:         {
88:•                if (b >= 0 && b <= 25)
89:                 {
90:                         return (char) ('A' + b);
91:                 }
92:
93:•                if (b >= 26 && b <= 51)
94:                 {
95:                         return (char) ('a' + b - 26);
96:                 }
97:
98:•                if (b >= 52 && b <= 61)
99:                 {
100:                         return (char) ('0' + b - 52);
101:                 }
102:
103:•                if (b == 62)
104:                 {
105:                         return '+';
106:                 }
107:
108:•                if (b == 63)
109:                 {
110:                         return '/';
111:                 }
112:
113:                 return '?';
114:         }
115:
116:         /**
117:          * Base64 decode a string into a byte array.
118:          *
119:          * @param text
120:          * The encoded base64 text.
121:          * @return a byte array of the decoded data.
122:          * @throws Exception
123:          */
124:
125:         public static byte[] decode(String text) throws Exception
126:         {
127:•                if (text.length() % 4 != 0)
128:                 {
129:                         throw new Exception("Base64 not a multiple of 4 bytes");
130:                 }
131:
132:                 byte[] result = new byte[text.length() / 4 * 3];
133:                 int p = 0;
134:
135:•                for (int i = 0; i < text.length();)
136:                 {
137:•                        if (text.charAt(i) == '\n')
138:                         {
139:                                 continue;
140:                         }
141:
142:                         int b1 = b64decode(text.charAt(i++));
143:                         int b2 = b64decode(text.charAt(i++));
144:                         int b3 = b64decode(text.charAt(i++));
145:                         int b4 = b64decode(text.charAt(i++));
146:
147:•                        if (b4 >= 0)
148:                         {
149:                                 int three = b4 | b3 << 6 | b2 << 12 | b1 << 18;
150:                                 result[p++] = (byte) ((three & 0xff0000) >> 16);
151:                                 result[p++] = (byte) ((three & 0xff00) >> 8);
152:                                 result[p++] = (byte) (three & 0xff);
153:•                        } else if (b3 >= 0)
154:                         {
155:                                 int two = b3 << 6 | b2 << 12 | b1 << 18;
156:                                 result[p++] = (byte) ((two & 0xff0000) >> 16);
157:                                 result[p++] = (byte) ((two & 0xff00) >> 8);
158:                         } else
159:                         {
160:                                 int one = b2 << 12 | b1 << 18;
161:                                 result[p++] = (byte) ((one & 0xff0000) >> 16);
162:                         }
163:                 }
164:
165:                 byte[] output = new byte[p];
166:                 System.arraycopy(result, 0, output, 0, p);
167:
168:                 return output;
169:         }
170:
171:         /**
172:          * Base64 encode a byte array.
173:          *
174:          * @param data
175:          * the data to encode.
176:          * @return a StringBuffer containing the encoded lines.
177:          */
178:
179:         public static StringBuffer encode(byte[] data)
180:         {
181:                 int rem = data.length % 3;
182:                 int num = data.length / 3;
183:                 StringBuffer result = new StringBuffer();
184:                 int p = 0;
185:                 // int c=0;
186:
187:•                for (int i = 0; i < num; i++)
188:                 {
189:                         int b1 = (data[p] & 0xfc) >> 2;
190:                         int b2 = (data[p] & 0x03) << 4 | (data[p + 1] & 0xf0) >> 4;
191:                         int b3 = (data[p + 1] & 0x0f) << 2 | (data[p + 2] & 0xc0) >> 6;
192:                         int b4 = data[p + 2] & 0x3f;
193:
194:                         result.append(b64encode(b1));
195:                         result.append(b64encode(b2));
196:                         result.append(b64encode(b3));
197:                         result.append(b64encode(b4));
198:
199:                         p += 3;
200:                         // c += 4;
201:                 }
202:
203:•                switch (rem)
204:                 {
205:                         case 0:
206:                                 break;
207:
208:                         case 1:
209:                         {
210:                                 int b1 = (data[p] & 0xfc) >> 2;
211:                                 int b2 = (data[p] & 0x03) << 4;
212:
213:                                 result.append(b64encode(b1));
214:                                 result.append(b64encode(b2));
215:                                 result.append('=');
216:                                 result.append('=');
217:                                 break;
218:                         }
219:
220:                         case 2:
221:                         {
222:                                 int b1 = (data[p] & 0xfc) >> 2;
223:                                 int b2 = (data[p] & 0x03) << 4 | (data[p + 1] & 0xf0) >> 4;
224:                                 int b3 = (data[p + 1] & 0x0f) << 2;
225:
226:                                 result.append(b64encode(b1));
227:                                 result.append(b64encode(b2));
228:                                 result.append(b64encode(b3));
229:                                 result.append('=');
230:                                 break;
231:                         }
232:                 }
233:
234:                 return result;
235:         }
236:
237:         public static void main(String[] args) throws Exception
238:         {
239:                 BufferedReader bir = new BufferedReader(new InputStreamReader(System.in));
240:                 String charset = Charset.defaultCharset().name();
241:                 System.out.println("Default charset = " + charset);
242:                 Pattern pattern = Pattern.compile("(\\w+)\\s*?(.*)?$");
243:
244:                 while (true)
245:                 {
246:                         System.out.print("> ");
247:                         String line = bir.readLine();
248:                         Matcher m = pattern.matcher(line);
249:
250:•                        if (!m.matches())
251:                         {
252:                                 System.out.println("[encode|decode] <string>");
253:                                 System.out.println("charset <name>");
254:                                 System.out.println("quit");
255:                                 continue;
256:                         }
257:
258:                         String cmd = m.group(1);
259:•                        String data = m.groupCount() == 2 ? m.group(2) : "";
260:
261:•                        if (cmd.equals("decode"))
262:                         {
263:                                 try
264:                                 {
265:                                         System.out.println(new String(decode(data)));
266:                                 } catch (Exception e)
267:                                 {
268:                                         System.out.println("Oops! " + e.getMessage());
269:                                 }
270:•                        } else if (cmd.equals("encode"))
271:                         {
272:                                 System.out.println(encode(data.getBytes(charset)));
273:•                        } else if (cmd.equals("charset"))
274:                         {
275:                                 charset = data;
276:                                 System.out.println("Charset now " + charset);
277:•                        } else if (cmd.equals("quit"))
278:                         {
279:                                 break;
280:                         }
281:                 }
282:
283:                 bir.close();
284:         }
285: }