]> git.ipfire.org Git - thirdparty/cups.git/blame - scripting/java/src/com/easysw/cups/Base64Coder.java
Add missing files for test.
[thirdparty/cups.git] / scripting / java / src / com / easysw / cups / Base64Coder.java
CommitLineData
b86bc4cf 1package com.easysw.cups;
2
3/**
4* A Base64 Encoder/Decoder.
5*
6* <p>
7* This class is used to encode and decode data in Base64 format as described in RFC 1521.
8*
9* <p>
10* This is "Open Source" software and released under the <a href="http://www.gnu.org/licenses/lgpl.html">GNU/LGPL</a> license.<br>
11* It is provided "as is" without warranty of any kind.<br>
12* Copyright 2003: Christian d'Heureuse, Inventec Informatik AG, Switzerland.<br>
13* Home page: <a href="http://www.source-code.biz">www.source-code.biz</a><br>
14*
15* <p>
16* Version history:<br>
17* 2003-07-22 Christian d'Heureuse (chdh): Module created.<br>
18* 2005-08-11 chdh: Lincense changed from GPL to LGPL.<br>
19* 2006-11-21 chdh:<br>
20* &nbsp; Method encode(String) renamed to encodeString(String).<br>
21* &nbsp; Method decode(String) renamed to decodeString(String).<br>
22* &nbsp; New method encode(byte[],int) added.<br>
23* &nbsp; New method decode(String) added.<br>
24*/
25
26public class Base64Coder {
27
28// Mapping table from 6-bit nibbles to Base64 characters.
29private static char[] map1 = new char[64];
30 static {
31 int i=0;
32 for (char c='A'; c<='Z'; c++) map1[i++] = c;
33 for (char c='a'; c<='z'; c++) map1[i++] = c;
34 for (char c='0'; c<='9'; c++) map1[i++] = c;
35 map1[i++] = '+'; map1[i++] = '/'; }
36
37// Mapping table from Base64 characters to 6-bit nibbles.
38private static byte[] map2 = new byte[128];
39 static {
40 for (int i=0; i<map2.length; i++) map2[i] = -1;
41 for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
42
43/**
44* Encodes a string into Base64 format.
45* No blanks or line breaks are inserted.
46* @param s a String to be encoded.
47* @return A String with the Base64 encoded data.
48*/
49public static String encodeString (String s) {
50 return new String(encode(s.getBytes())); }
51
52/**
53* Encodes a byte array into Base64 format.
54* No blanks or line breaks are inserted.
55* @param in an array containing the data bytes to be encoded.
56* @return A character array with the Base64 encoded data.
57*/
58public static char[] encode (byte[] in) {
59 return encode(in,in.length); }
60
61/**
62* Encodes a byte array into Base64 format.
63* No blanks or line breaks are inserted.
64* @param in an array containing the data bytes to be encoded.
65* @param iLen number of bytes to process in <code>in</code>.
66* @return A character array with the Base64 encoded data.
67*/
68public static char[] encode (byte[] in, int iLen) {
69 int oDataLen = (iLen*4+2)/3; // output length without padding
70 int oLen = ((iLen+2)/3)*4; // output length including padding
71 char[] out = new char[oLen];
72 int ip = 0;
73 int op = 0;
74 while (ip < iLen) {
75 int i0 = in[ip++] & 0xff;
76 int i1 = ip < iLen ? in[ip++] & 0xff : 0;
77 int i2 = ip < iLen ? in[ip++] & 0xff : 0;
78 int o0 = i0 >>> 2;
79 int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
80 int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
81 int o3 = i2 & 0x3F;
82 out[op++] = map1[o0];
83 out[op++] = map1[o1];
84 out[op] = op < oDataLen ? map1[o2] : '='; op++;
85 out[op] = op < oDataLen ? map1[o3] : '='; op++; }
86 return out; }
87
88/**
89* Decodes a string from Base64 format.
90* @param s a Base64 String to be decoded.
91* @return A String containing the decoded data.
92* @throws IllegalArgumentException if the input is not valid Base64 encoded data.
93*/
94public static String decodeString (String s) {
95 return new String(decode(s)); }
96
97/**
98* Decodes a byte array from Base64 format.
99* @param s a Base64 String to be decoded.
100* @return An array containing the decoded data bytes.
101* @throws IllegalArgumentException if the input is not valid Base64 encoded data.
102*/
103public static byte[] decode (String s) {
104 return decode(s.toCharArray()); }
105
106/**
107* Decodes a byte array from Base64 format.
108* No blanks or line breaks are allowed within the Base64 encoded data.
109* @param in a character array containing the Base64 encoded data.
110* @return An array containing the decoded data bytes.
111* @throws IllegalArgumentException if the input is not valid Base64 encoded data.
112*/
113public static byte[] decode (char[] in) {
114 int iLen = in.length;
115 if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
116 while (iLen > 0 && in[iLen-1] == '=') iLen--;
117 int oLen = (iLen*3) / 4;
118 byte[] out = new byte[oLen];
119 int ip = 0;
120 int op = 0;
121 while (ip < iLen) {
122 int i0 = in[ip++];
123 int i1 = in[ip++];
124 int i2 = ip < iLen ? in[ip++] : 'A';
125 int i3 = ip < iLen ? in[ip++] : 'A';
126 if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
127 throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
128 int b0 = map2[i0];
129 int b1 = map2[i1];
130 int b2 = map2[i2];
131 int b3 = map2[i3];
132 if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
133 throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
134 int o0 = ( b0 <<2) | (b1>>>4);
135 int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
136 int o2 = ((b2 & 3)<<6) | b3;
137 out[op++] = (byte)o0;
138 if (op<oLen) out[op++] = (byte)o1;
139 if (op<oLen) out[op++] = (byte)o2; }
140 return out; }
141
142// Dummy constructor.
143private Base64Coder() {}
144
145} // end class Base64Coder