1 /*! base64x-1.1.2 (c) 2013 Kenji Urushima | kjur.github.com/jsjws/license
  2  */
  3 /*
  4  * base64x.js - Base64url and supplementary functions for Tom Wu's base64.js library
  5  *
  6  * version: 1.1.2 (2013 Jul 21)
  7  *
  8  * Copyright (c) 2012-2013 Kenji Urushima (kenji.urushima@gmail.com)
  9  *
 10  * This software is licensed under the terms of the MIT License.
 11  * http://kjur.github.com/jsjws/license/
 12  *
 13  * The above copyright and license notice shall be 
 14  * included in all copies or substantial portions of the Software.
 15  *
 16  * DEPENDS ON:
 17  *   - base64.js - Tom Wu's Base64 library
 18  */
 19 
 20 /**
 21  * Base64URL and supplementary functions for Tom Wu's base64.js library.<br/>
 22  * This class is just provide information about global functions
 23  * defined in 'base64x.js'. The 'base64x.js' script file provides
 24  * global functions for converting following data each other.
 25  * <ul>
 26  * <li>(ASCII) String</li>
 27  * <li>UTF8 String including CJK, Latin and other characters</li>
 28  * <li>byte array</li>
 29  * <li>hexadecimal encoded String</li>
 30  * <li>Full URIComponent encoded String (such like "%69%94")</li>
 31  * <li>Base64 encoded String</li>
 32  * <li>Base64URL encoded String</li>
 33  * </ul>
 34  * All functions in 'base64x.js' are defined in {@link global__} and not
 35  * in this class.
 36  * 
 37  * @class Base64URL and supplementary functions for Tom Wu's base64.js library
 38  * @author Kenji Urushima
 39  * @version 1.1 (07 May 2012)
 40  * @requires base64.js
 41  * @see <a href="http://kjur.github.com/jsjws/">'jwjws'(JWS JavaScript Library) home page http://kjur.github.com/jsjws/</a>
 42  * @see <a href="http://kjur.github.com/jsrsasigns/">'jwrsasign'(RSA Sign JavaScript Library) home page http://kjur.github.com/jsrsasign/</a>
 43  */
 44 function Base64x() {
 45 }
 46 
 47 // ==== string / byte array ================================
 48 /**
 49  * convert a string to an array of character codes
 50  * @param {String} s
 51  * @return {Array of Numbers} 
 52  */
 53 function stoBA(s) {
 54     var a = new Array();
 55     for (var i = 0; i < s.length; i++) {
 56 	a[i] = s.charCodeAt(i);
 57     }
 58     return a;
 59 }
 60 
 61 /**
 62  * convert an array of character codes to a string
 63  * @param {Array of Numbers} a array of character codes
 64  * @return {String} s
 65  */
 66 function BAtos(a) {
 67     var s = "";
 68     for (var i = 0; i < a.length; i++) {
 69 	s = s + String.fromCharCode(a[i]);
 70     }
 71     return s;
 72 }
 73 
 74 // ==== byte array / hex ================================
 75 /**
 76  * convert an array of bytes(Number) to hexadecimal string.<br/>
 77  * @param {Array of Numbers} a array of bytes
 78  * @return {String} hexadecimal string
 79  */
 80 function BAtohex(a) {
 81     var s = "";
 82     for (var i = 0; i < a.length; i++) {
 83 	var hex1 = a[i].toString(16);
 84 	if (hex1.length == 1) hex1 = "0" + hex1;
 85 	s = s + hex1;
 86     }
 87     return s;
 88 }
 89 
 90 // ==== string / hex ================================
 91 /**
 92  * convert a ASCII string to a hexadecimal string of ASCII codes.<br/>
 93  * NOTE: This can't be used for non ASCII characters.
 94  * @param {s} s ASCII string
 95  * @return {String} hexadecimal string
 96  */
 97 function stohex(s) {
 98     return BAtohex(stoBA(s));
 99 }
100 
101 // ==== string / base64 ================================
102 /**
103  * convert a ASCII string to a Base64 encoded string.<br/>
104  * NOTE: This can't be used for non ASCII characters.
105  * @param {s} s ASCII string
106  * @return {String} Base64 encoded string
107  */
108 function stob64(s) {
109     return hex2b64(stohex(s));
110 }
111 
112 // ==== string / base64url ================================
113 /**
114  * convert a ASCII string to a Base64URL encoded string.<br/>
115  * NOTE: This can't be used for non ASCII characters.
116  * @param {s} s ASCII string
117  * @return {String} Base64URL encoded string
118  */
119 function stob64u(s) {
120     return b64tob64u(hex2b64(stohex(s)));
121 }
122 
123 /**
124  * convert a Base64URL encoded string to a ASCII string.<br/>
125  * NOTE: This can't be used for Base64URL encoded non ASCII characters.
126  * @param {s} s Base64URL encoded string
127  * @return {String} ASCII string
128  */
129 function b64utos(s) {
130     return BAtos(b64toBA(b64utob64(s)));
131 }
132 
133 // ==== base64 / base64url ================================
134 /**
135  * convert a Base64 encoded string to a Base64URL encoded string.<br/>
136  * Example: "ab+c3f/==" → "ab-c3f_"
137  * @param {String} s Base64 encoded string
138  * @return {String} Base64URL encoded string
139  */
140 function b64tob64u(s) {
141     s = s.replace(/\=/g, "");
142     s = s.replace(/\+/g, "-");
143     s = s.replace(/\//g, "_");
144     return s;
145 }
146 
147 /**
148  * convert a Base64URL encoded string to a Base64 encoded string.<br/>
149  * Example: "ab-c3f_" → "ab+c3f/=="
150  * @param {String} s Base64URL encoded string
151  * @return {String} Base64 encoded string
152  */
153 function b64utob64(s) {
154     if (s.length % 4 == 2) s = s + "==";
155     else if (s.length % 4 == 3) s = s + "=";
156     s = s.replace(/-/g, "+");
157     s = s.replace(/_/g, "/");
158     return s;
159 }
160 
161 // ==== hex / base64url ================================
162 /**
163  * convert a hexadecimal string to a Base64URL encoded string.<br/>
164  * @param {String} s hexadecimal string
165  * @return {String} Base64URL encoded string
166  */
167 function hextob64u(s) {
168     return b64tob64u(hex2b64(s));
169 }
170 
171 /**
172  * convert a Base64URL encoded string to a hexadecimal string.<br/>
173  * @param {String} s Base64URL encoded string
174  * @return {String} hexadecimal string
175  */
176 function b64utohex(s) {
177     return b64tohex(b64utob64(s));
178 }
179 
180 var utf8tob64u, b64utoutf8;
181 
182 if (typeof Buffer === 'function')
183 {
184   utf8tob64u = function (s)
185   {
186     return b64tob64u(new Buffer(s, 'utf8').toString('base64'));
187   };
188 
189   b64utoutf8 = function (s)
190   {
191     return new Buffer(b64utob64(s), 'base64').toString('utf8');
192   };
193 }
194 else
195 {
196 // ==== utf8 / base64url ================================
197 /**
198  * convert a UTF-8 encoded string including CJK or Latin to a Base64URL encoded string.<br/>
199  * @param {String} s UTF-8 encoded string
200  * @return {String} Base64URL encoded string
201  * @since 1.1
202  */
203   utf8tob64u = function (s)
204   {
205     return hextob64u(uricmptohex(encodeURIComponentAll(s)));
206   };
207 
208 /**
209  * convert a Base64URL encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
210  * @param {String} s Base64URL encoded string
211  * @return {String} UTF-8 encoded string
212  * @since 1.1
213  */
214   b64utoutf8 = function (s)
215   {
216     return decodeURIComponent(hextouricmp(b64utohex(s)));
217   };
218 }
219 
220 // ==== utf8 / base64url ================================
221 /**
222  * convert a UTF-8 encoded string including CJK or Latin to a Base64 encoded string.<br/>
223  * @param {String} s UTF-8 encoded string
224  * @return {String} Base64 encoded string
225  * @since 1.1.1
226  */
227 function utf8tob64(s) {
228   return hex2b64(uricmptohex(encodeURIComponentAll(s)));
229 }
230 
231 /**
232  * convert a Base64 encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
233  * @param {String} s Base64 encoded string
234  * @return {String} UTF-8 encoded string
235  * @since 1.1.1
236  */
237 function b64toutf8(s) {
238   return decodeURIComponent(hextouricmp(b64tohex(s)));
239 }
240 
241 // ==== utf8 / hex ================================
242 /**
243  * convert a UTF-8 encoded string including CJK or Latin to a hexadecimal encoded string.<br/>
244  * @param {String} s UTF-8 encoded string
245  * @return {String} hexadecimal encoded string
246  * @since 1.1.1
247  */
248 function utf8tohex(s) {
249   return uricmptohex(encodeURIComponentAll(s));
250 }
251 
252 /**
253  * convert a hexadecimal encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
254  * Note that when input is improper hexadecimal string as UTF-8 string, this function returns
255  * 'null'.
256  * @param {String} s hexadecimal encoded string
257  * @return {String} UTF-8 encoded string or null
258  * @since 1.1.1
259  */
260 function hextoutf8(s) {
261   return decodeURIComponent(hextouricmp(s));
262 }
263 
264 /**
265  * convert a hexadecimal encoded string to raw string including non printable characters.<br/>
266  * @param {String} s hexadecimal encoded string
267  * @return {String} raw string
268  * @since 1.1.2
269  * @example
270  * hextorstr("610061") → "a\x00a"
271  */
272 function hextorstr(sHex) {
273     var s = "";
274     for (var i = 0; i < sHex.length - 1; i += 2) {
275 	s += String.fromCharCode(parseInt(sHex.substr(i, 2), 16));
276     }
277     return s;
278 }
279 
280 /**
281  * convert a raw string including non printable characters to hexadecimal encoded string.<br/>
282  * @param {String} s raw string
283  * @return {String} hexadecimal encoded string
284  * @since 1.1.2
285  * @example
286  * rstrtohex("a\x00a") → "610061"
287  */
288 function rstrtohex(s) {
289     var result = "";
290     for (var i = 0; i < s.length; i++) {
291 	result += ("0" + s.charCodeAt(i).toString(16)).slice(-2);
292     }
293     return result;
294 }
295 
296 // ==== URIComponent / hex ================================
297 /**
298  * convert a URLComponent string such like "%67%68" to a hexadecimal string.<br/>
299  * @param {String} s URIComponent string such like "%67%68"
300  * @return {String} hexadecimal string
301  * @since 1.1
302  */
303 function uricmptohex(s) {
304   return s.replace(/%/g, "");
305 }
306 
307 /**
308  * convert a hexadecimal string to a URLComponent string such like "%67%68".<br/>
309  * @param {String} s hexadecimal string
310  * @return {String} URIComponent string such like "%67%68"
311  * @since 1.1
312  */
313 function hextouricmp(s) {
314   return s.replace(/(..)/g, "%$1");
315 }
316 
317 // ==== URIComponent ================================
318 /**
319  * convert UTFa hexadecimal string to a URLComponent string such like "%67%68".<br/>
320  * Note that these "<code>0-9A-Za-z!'()*-._~</code>" characters will not
321  * converted to "%xx" format by builtin 'encodeURIComponent()' function.
322  * However this 'encodeURIComponentAll()' function will convert 
323  * all of characters into "%xx" format.
324  * @param {String} s hexadecimal string
325  * @return {String} URIComponent string such like "%67%68"
326  * @since 1.1
327  */
328 function encodeURIComponentAll(u8) {
329   var s = encodeURIComponent(u8);
330   var s2 = "";
331   for (var i = 0; i < s.length; i++) {
332     if (s[i] == "%") {
333       s2 = s2 + s.substr(i, 3);
334       i = i + 2;
335     } else {
336       s2 = s2 + "%" + stohex(s[i]);
337     }
338   }
339   return s2;
340 }
341 
342 // ==== new lines ================================
343 /**
344  * convert all DOS new line("\r\n") to UNIX new line("\n") in 
345  * a String "s".
346  * @param {String} s string 
347  * @return {String} converted string
348  */
349 function newline_toUnix(s) {
350     s = s.replace(/\r\n/mg, "\n");
351     return s;
352 }
353 
354 /**
355  * convert all UNIX new line("\r\n") to DOS new line("\n") in 
356  * a String "s".
357  * @param {String} s string 
358  * @return {String} converted string
359  */
360 function newline_toDos(s) {
361     s = s.replace(/\r\n/mg, "\n");
362     s = s.replace(/\n/mg, "\r\n");
363     return s;
364 }
365 
366