1 /*! base64x-1.1.6 (c) 2012-2015 Kenji Urushima | kjur.github.com/jsrsasign/license
  2  */
  3 /*
  4  * base64x.js - Base64url and supplementary functions for Tom Wu's base64.js library
  5  *
  6  * version: 1.1.6 (2015-Nov-11)
  7  *
  8  * Copyright (c) 2012-2015 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  * @fileOverview
 22  * @name base64x-1.1.js
 23  * @author Kenji Urushima kenji.urushima@gmail.com
 24  * @version asn1 1.1.6 (2015-Nov-11)
 25  * @since jsrsasign 2.1
 26  * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
 27  */
 28 
 29 /**
 30  * Base64URL and supplementary functions for Tom Wu's base64.js library.<br/>
 31  * This class is just provide information about global functions
 32  * defined in 'base64x.js'. The 'base64x.js' script file provides
 33  * global functions for converting following data each other.
 34  * <ul>
 35  * <li>(ASCII) String</li>
 36  * <li>UTF8 String including CJK, Latin and other characters</li>
 37  * <li>byte array</li>
 38  * <li>hexadecimal encoded String</li>
 39  * <li>Full URIComponent encoded String (such like "%69%94")</li>
 40  * <li>Base64 encoded String</li>
 41  * <li>Base64URL encoded String</li>
 42  * </ul>
 43  * All functions in 'base64x.js' are defined in {@link global__} and not
 44  * in this class.
 45  * 
 46  * @class Base64URL and supplementary functions for Tom Wu's base64.js library
 47  * @author Kenji Urushima
 48  * @version 1.1 (07 May 2012)
 49  * @requires base64.js
 50  * @see <a href="http://kjur.github.com/jsjws/">'jwjws'(JWS JavaScript Library) home page http://kjur.github.com/jsjws/</a>
 51  * @see <a href="http://kjur.github.com/jsrsasigns/">'jwrsasign'(RSA Sign JavaScript Library) home page http://kjur.github.com/jsrsasign/</a>
 52  */
 53 function Base64x() {
 54 }
 55 
 56 // ==== string / byte array ================================
 57 /**
 58  * convert a string to an array of character codes
 59  * @param {String} s
 60  * @return {Array of Numbers} 
 61  */
 62 function stoBA(s) {
 63     var a = new Array();
 64     for (var i = 0; i < s.length; i++) {
 65 	a[i] = s.charCodeAt(i);
 66     }
 67     return a;
 68 }
 69 
 70 /**
 71  * convert an array of character codes to a string
 72  * @param {Array of Numbers} a array of character codes
 73  * @return {String} s
 74  */
 75 function BAtos(a) {
 76     var s = "";
 77     for (var i = 0; i < a.length; i++) {
 78 	s = s + String.fromCharCode(a[i]);
 79     }
 80     return s;
 81 }
 82 
 83 // ==== byte array / hex ================================
 84 /**
 85  * convert an array of bytes(Number) to hexadecimal string.<br/>
 86  * @param {Array of Numbers} a array of bytes
 87  * @return {String} hexadecimal string
 88  */
 89 function BAtohex(a) {
 90     var s = "";
 91     for (var i = 0; i < a.length; i++) {
 92 	var hex1 = a[i].toString(16);
 93 	if (hex1.length == 1) hex1 = "0" + hex1;
 94 	s = s + hex1;
 95     }
 96     return s;
 97 }
 98 
 99 // ==== string / hex ================================
100 /**
101  * convert a ASCII string to a hexadecimal string of ASCII codes.<br/>
102  * NOTE: This can't be used for non ASCII characters.
103  * @param {s} s ASCII string
104  * @return {String} hexadecimal string
105  */
106 function stohex(s) {
107     return BAtohex(stoBA(s));
108 }
109 
110 // ==== string / base64 ================================
111 /**
112  * convert a ASCII string to a Base64 encoded string.<br/>
113  * NOTE: This can't be used for non ASCII characters.
114  * @param {s} s ASCII string
115  * @return {String} Base64 encoded string
116  */
117 function stob64(s) {
118     return hex2b64(stohex(s));
119 }
120 
121 // ==== string / base64url ================================
122 /**
123  * convert a ASCII string to a Base64URL encoded string.<br/>
124  * NOTE: This can't be used for non ASCII characters.
125  * @param {s} s ASCII string
126  * @return {String} Base64URL encoded string
127  */
128 function stob64u(s) {
129     return b64tob64u(hex2b64(stohex(s)));
130 }
131 
132 /**
133  * convert a Base64URL encoded string to a ASCII string.<br/>
134  * NOTE: This can't be used for Base64URL encoded non ASCII characters.
135  * @param {s} s Base64URL encoded string
136  * @return {String} ASCII string
137  */
138 function b64utos(s) {
139     return BAtos(b64toBA(b64utob64(s)));
140 }
141 
142 // ==== base64 / base64url ================================
143 /**
144  * convert a Base64 encoded string to a Base64URL encoded string.<br/>
145  * Example: "ab+c3f/==" → "ab-c3f_"
146  * @param {String} s Base64 encoded string
147  * @return {String} Base64URL encoded string
148  */
149 function b64tob64u(s) {
150     s = s.replace(/\=/g, "");
151     s = s.replace(/\+/g, "-");
152     s = s.replace(/\//g, "_");
153     return s;
154 }
155 
156 /**
157  * convert a Base64URL encoded string to a Base64 encoded string.<br/>
158  * Example: "ab-c3f_" → "ab+c3f/=="
159  * @param {String} s Base64URL encoded string
160  * @return {String} Base64 encoded string
161  */
162 function b64utob64(s) {
163     if (s.length % 4 == 2) s = s + "==";
164     else if (s.length % 4 == 3) s = s + "=";
165     s = s.replace(/-/g, "+");
166     s = s.replace(/_/g, "/");
167     return s;
168 }
169 
170 // ==== hex / base64url ================================
171 /**
172  * convert a hexadecimal string to a Base64URL encoded string.<br/>
173  * @param {String} s hexadecimal string
174  * @return {String} Base64URL encoded string
175  * @description
176  * convert a hexadecimal string to a Base64URL encoded string.
177  * NOTE: If leading "0" is omitted and odd number length for
178  * hexadecimal leading "0" is automatically added.
179  */
180 function hextob64u(s) {
181     if (s.length % 2 == 1) s = "0" + s;
182     return b64tob64u(hex2b64(s));
183 }
184 
185 /**
186  * convert a Base64URL encoded string to a hexadecimal string.<br/>
187  * @param {String} s Base64URL encoded string
188  * @return {String} hexadecimal string
189  */
190 function b64utohex(s) {
191     return b64tohex(b64utob64(s));
192 }
193 
194 var utf8tob64u, b64utoutf8;
195 
196 if (typeof Buffer === 'function')
197 {
198   utf8tob64u = function (s)
199   {
200     return b64tob64u(new Buffer(s, 'utf8').toString('base64'));
201   };
202 
203   b64utoutf8 = function (s)
204   {
205     return new Buffer(b64utob64(s), 'base64').toString('utf8');
206   };
207 }
208 else
209 {
210 // ==== utf8 / base64url ================================
211 /**
212  * convert a UTF-8 encoded string including CJK or Latin to a Base64URL encoded string.<br/>
213  * @param {String} s UTF-8 encoded string
214  * @return {String} Base64URL encoded string
215  * @since 1.1
216  */
217   utf8tob64u = function (s)
218   {
219     return hextob64u(uricmptohex(encodeURIComponentAll(s)));
220   };
221 
222 /**
223  * convert a Base64URL encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
224  * @param {String} s Base64URL encoded string
225  * @return {String} UTF-8 encoded string
226  * @since 1.1
227  */
228   b64utoutf8 = function (s)
229   {
230     return decodeURIComponent(hextouricmp(b64utohex(s)));
231   };
232 }
233 
234 // ==== utf8 / base64url ================================
235 /**
236  * convert a UTF-8 encoded string including CJK or Latin to a Base64 encoded string.<br/>
237  * @param {String} s UTF-8 encoded string
238  * @return {String} Base64 encoded string
239  * @since 1.1.1
240  */
241 function utf8tob64(s) {
242   return hex2b64(uricmptohex(encodeURIComponentAll(s)));
243 }
244 
245 /**
246  * convert a Base64 encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
247  * @param {String} s Base64 encoded string
248  * @return {String} UTF-8 encoded string
249  * @since 1.1.1
250  */
251 function b64toutf8(s) {
252   return decodeURIComponent(hextouricmp(b64tohex(s)));
253 }
254 
255 // ==== utf8 / hex ================================
256 /**
257  * convert a UTF-8 encoded string including CJK or Latin to a hexadecimal encoded string.<br/>
258  * @param {String} s UTF-8 encoded string
259  * @return {String} hexadecimal encoded string
260  * @since 1.1.1
261  */
262 function utf8tohex(s) {
263   return uricmptohex(encodeURIComponentAll(s));
264 }
265 
266 /**
267  * convert a hexadecimal encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
268  * Note that when input is improper hexadecimal string as UTF-8 string, this function returns
269  * 'null'.
270  * @param {String} s hexadecimal encoded string
271  * @return {String} UTF-8 encoded string or null
272  * @since 1.1.1
273  */
274 function hextoutf8(s) {
275   return decodeURIComponent(hextouricmp(s));
276 }
277 
278 /**
279  * convert a hexadecimal encoded string to raw string including non printable characters.<br/>
280  * @param {String} s hexadecimal encoded string
281  * @return {String} raw string
282  * @since 1.1.2
283  * @example
284  * hextorstr("610061") → "a\x00a"
285  */
286 function hextorstr(sHex) {
287     var s = "";
288     for (var i = 0; i < sHex.length - 1; i += 2) {
289         s += String.fromCharCode(parseInt(sHex.substr(i, 2), 16));
290     }
291     return s;
292 }
293 
294 /**
295  * convert a raw string including non printable characters to hexadecimal encoded string.<br/>
296  * @param {String} s raw string
297  * @return {String} hexadecimal encoded string
298  * @since 1.1.2
299  * @example
300  * rstrtohex("a\x00a") → "610061"
301  */
302 function rstrtohex(s) {
303     var result = "";
304     for (var i = 0; i < s.length; i++) {
305         result += ("0" + s.charCodeAt(i).toString(16)).slice(-2);
306     }
307     return result;
308 }
309 
310 // ==== hex / b64nl =======================================
311 
312 /*
313  * since base64x 1.1.3
314  */
315 function hextob64(s) {
316     return hex2b64(s);
317 }
318 
319 /*
320  * since base64x 1.1.3
321  */
322 function hextob64nl(s) {
323     var b64 = hextob64(s);
324     var b64nl = b64.replace(/(.{64})/g, "$1\r\n");
325     b64nl = b64nl.replace(/\r\n$/, '');
326     return b64nl;
327 }
328 
329 /*
330  * since base64x 1.1.3
331  */
332 function b64nltohex(s) {
333     var b64 = s.replace(/[^0-9A-Za-z\/+=]*/g, '');
334     var hex = b64tohex(b64);
335     return hex;
336 } 
337 
338 // ==== URIComponent / hex ================================
339 /**
340  * convert a URLComponent string such like "%67%68" to a hexadecimal string.<br/>
341  * @param {String} s URIComponent string such like "%67%68"
342  * @return {String} hexadecimal string
343  * @since 1.1
344  */
345 function uricmptohex(s) {
346   return s.replace(/%/g, "");
347 }
348 
349 /**
350  * convert a hexadecimal string to a URLComponent string such like "%67%68".<br/>
351  * @param {String} s hexadecimal string
352  * @return {String} URIComponent string such like "%67%68"
353  * @since 1.1
354  */
355 function hextouricmp(s) {
356   return s.replace(/(..)/g, "%$1");
357 }
358 
359 // ==== URIComponent ================================
360 /**
361  * convert UTFa hexadecimal string to a URLComponent string such like "%67%68".<br/>
362  * Note that these "<code>0-9A-Za-z!'()*-._~</code>" characters will not
363  * converted to "%xx" format by builtin 'encodeURIComponent()' function.
364  * However this 'encodeURIComponentAll()' function will convert 
365  * all of characters into "%xx" format.
366  * @param {String} s hexadecimal string
367  * @return {String} URIComponent string such like "%67%68"
368  * @since 1.1
369  */
370 function encodeURIComponentAll(u8) {
371   var s = encodeURIComponent(u8);
372   var s2 = "";
373   for (var i = 0; i < s.length; i++) {
374     if (s[i] == "%") {
375       s2 = s2 + s.substr(i, 3);
376       i = i + 2;
377     } else {
378       s2 = s2 + "%" + stohex(s[i]);
379     }
380   }
381   return s2;
382 }
383 
384 // ==== new lines ================================
385 /**
386  * convert all DOS new line("\r\n") to UNIX new line("\n") in 
387  * a String "s".
388  * @param {String} s string 
389  * @return {String} converted string
390  */
391 function newline_toUnix(s) {
392     s = s.replace(/\r\n/mg, "\n");
393     return s;
394 }
395 
396 /**
397  * convert all UNIX new line("\r\n") to DOS new line("\n") in 
398  * a String "s".
399  * @param {String} s string 
400  * @return {String} converted string
401  */
402 function newline_toDos(s) {
403     s = s.replace(/\r\n/mg, "\n");
404     s = s.replace(/\n/mg, "\r\n");
405     return s;
406 }
407 
408 // ==== others ================================
409 
410 /**
411  * convert string of integer array to hexadecimal string.<br/>
412  * @param {String} s string of integer array
413  * @return {String} hexadecimal string
414  * @since base64x 1.1.6 jsrsasign 5.0.2
415  * @throws "malformed integer array string: *" for wrong input
416  * @description
417  * This function converts a string of JavaScript integer array to
418  * a hexadecimal string. Each integer value shall be in a range 
419  * from 0 to 255 otherwise it raise exception. Input string can
420  * have extra space or newline string so that they will be ignored.
421  * 
422  * @example
423  * intarystrtohex(" [123, 34, 101, 34, 58] ")
424  * -> 7b2265223a (i.e. `{"e":` as string)
425  */
426 function intarystrtohex(s) {
427   s = s.replace(/^\s*\[\s*/, '');
428   s = s.replace(/\s*\]\s*$/, '');
429   s = s.replace(/\s*/g, '');
430   try {
431     var hex = s.split(/,/).map(function(element, index, array) {
432       var i = parseInt(element);
433       if (i < 0 || 255 < i) throw "integer not in range 0-255";
434       var hI = ("00" + i.toString(16)).slice(-2);
435       return hI;
436     }).join('');
437     return hex;
438   } catch(ex) {
439     throw "malformed integer array string: " + ex;
440   }
441 }
442 
443 /**
444  * find index of string where two string differs
445  * @param {String} s1 string to compare
446  * @param {String} s2 string to compare
447  * @return {Number} string index of where character differs. Return -1 if same.
448  * @since jsrsasign 4.9.0 base64x 1.1.5
449  * @example
450  * strdiffidx("abcdefg", "abcd4fg") -> 4
451  * strdiffidx("abcdefg", "abcdefg") -> -1
452  * strdiffidx("abcdefg", "abcdef") -> 6
453  * strdiffidx("abcdefgh", "abcdef") -> 6
454  */
455 var strdiffidx = function(s1, s2) {
456     var n = s1.length;
457     if (s1.length > s2.length) n = s2.length;
458     for (var i = 0; i < n; i++) {
459 	if (s1.charCodeAt(i) != s2.charCodeAt(i)) return i;
460     }
461     if (s1.length != s2.length) return n;
462     return -1; // same
463 };
464