View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2005, 2009
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Roberto Pineiro, IBM, roberto.pineiro@us.ibm.com
12   * @author : Chung-hao Tan, IBM, chungtan@us.ibm.com
13   * 
14   * 
15   * Change History
16   * Flag       Date        Prog         Description
17   *------------------------------------------------------------------------------- 
18   * 17970      2005-08-11  pineiro5     Logon from z/OS not possible
19   * 1565892    2006-11-28  lupusalex    Make SBLIM client JSR48 compliant
20   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
21   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
22   */
23  
24  package org.metricshub.wbem.sblim.cimclient.internal.http;
25  
26  /*-
27   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
28   * WBEM Java Client
29   * ჻჻჻჻჻჻
30   * Copyright 2023 - 2025 MetricsHub
31   * ჻჻჻჻჻჻
32   * Licensed under the Apache License, Version 2.0 (the "License");
33   * you may not use this file except in compliance with the License.
34   * You may obtain a copy of the License at
35   *
36   *      http://www.apache.org/licenses/LICENSE-2.0
37   *
38   * Unless required by applicable law or agreed to in writing, software
39   * distributed under the License is distributed on an "AS IS" BASIS,
40   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41   * See the License for the specific language governing permissions and
42   * limitations under the License.
43   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
44   */
45  
46  import java.io.UnsupportedEncodingException;
47  
48  /**
49   * Class BASE64Encoder implements the BASE64 encoding algorithm
50   *
51   */
52  public final class BASE64Encoder {
53  	private static byte BASE64_ALPHABET[] = {
54  		65,
55  		66,
56  		67,
57  		68,
58  		69,
59  		70,
60  		71,
61  		72,
62  		73,
63  		74,
64  		75,
65  		76,
66  		77,
67  		78,
68  		79,
69  		80,
70  		81,
71  		82,
72  		83,
73  		84,
74  		85,
75  		86,
76  		87,
77  		88,
78  		89,
79  		90,
80  		97,
81  		98,
82  		99,
83  		100,
84  		101,
85  		102,
86  		103,
87  		104,
88  		105,
89  		106,
90  		107,
91  		108,
92  		109,
93  		110,
94  		111,
95  		112,
96  		113,
97  		114,
98  		115,
99  		116,
100 		117,
101 		118,
102 		119,
103 		120,
104 		121,
105 		122,
106 		48,
107 		49,
108 		50,
109 		51,
110 		52,
111 		53,
112 		54,
113 		55,
114 		56,
115 		57,
116 		43,
117 		47
118 	};
119 
120 	private BASE64Encoder() {
121 		// no instances
122 	}
123 
124 	/**
125 	 * Encodes a given byte array
126 	 *
127 	 * @param pPlain
128 	 *            The raw bytes
129 	 * @return The encoded bytes
130 	 */
131 	public static String encode(byte[] pPlain) {
132 		byte encoded[];
133 		int totalBits = pPlain.length << 3;
134 		int remainder = totalBits % 24;
135 		int totalEncoded = totalBits / 24;
136 
137 		int size = totalEncoded;
138 		if (remainder > 0) size++;
139 
140 		encoded = new byte[size << 2];
141 
142 		short highBits = 0;
143 		short lowBits = 0;
144 		short byte1 = 0;
145 		short byte2 = 0;
146 		short byte3 = 0;
147 		int dest = 0;
148 		int source = 0;
149 		for (int i = 0; i < totalEncoded; i++) {
150 			byte1 = (short) (pPlain[source++] & 0xFF);
151 			byte2 = (short) (pPlain[source++] & 0xFF);
152 			byte3 = (short) (pPlain[source++] & 0xFF);
153 
154 			highBits = (short) (byte1 & 0x03);
155 			lowBits = (short) (byte2 & 0x0F);
156 
157 			short val1 = (short) (byte1 >> 2);
158 			short val2 = (short) (byte2 >> 4);
159 			short val3 = (short) (byte3 >> 6);
160 
161 			encoded[dest++] = BASE64_ALPHABET[val1];
162 			encoded[dest++] = BASE64_ALPHABET[val2 | (highBits << 4)];
163 			encoded[dest++] = BASE64_ALPHABET[(lowBits << 2) | val3];
164 			encoded[dest++] = BASE64_ALPHABET[byte3 & 0x3f];
165 		}
166 
167 		if (remainder == 8) {
168 			byte1 = (short) (pPlain[source] & 0xFF);
169 			highBits = (short) (byte1 & 0x03);
170 			short val1 = (short) (byte1 >> 2);
171 			encoded[dest++] = BASE64_ALPHABET[val1];
172 			encoded[dest++] = BASE64_ALPHABET[highBits << 4];
173 			encoded[dest++] = (byte) 61; // '='
174 			encoded[dest++] = (byte) 61; // '='
175 		} else if (remainder == 16) {
176 			byte1 = (short) (pPlain[source++] & 0xFF);
177 			byte2 = (short) (pPlain[source++] & 0xFF);
178 			highBits = (short) (byte1 & 0x03);
179 			lowBits = (short) (byte2 & 0x0F);
180 
181 			short val1 = (short) (byte1 >> 2);
182 			short val2 = (short) (byte2 >> 4);
183 
184 			encoded[dest++] = BASE64_ALPHABET[val1];
185 			encoded[dest++] = BASE64_ALPHABET[val2 | (highBits << 4)];
186 			encoded[dest++] = BASE64_ALPHABET[lowBits << 2];
187 			encoded[dest++] = (byte) 61; // '='
188 		}
189 		String res;
190 		try {
191 			res = new String(encoded, "ASCII");
192 		} catch (UnsupportedEncodingException e) {
193 			try {
194 				res = new String(encoded, "UTF-8");
195 			} catch (UnsupportedEncodingException e1) {
196 				res = new String(encoded);
197 			}
198 		}
199 		return res;
200 	}
201 }