forked from rachanon/stdbWeb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
464 lines
13 KiB
464 lines
13 KiB
9 years ago
|
/*! base64x-1.1.6 (c) 2012-2015 Kenji Urushima | kjur.github.com/jsrsasign/license
|
||
|
*/
|
||
|
/*
|
||
|
* base64x.js - Base64url and supplementary functions for Tom Wu's base64.js library
|
||
|
*
|
||
|
* version: 1.1.6 (2015-Nov-11)
|
||
|
*
|
||
|
* Copyright (c) 2012-2015 Kenji Urushima (kenji.urushima@gmail.com)
|
||
|
*
|
||
|
* This software is licensed under the terms of the MIT License.
|
||
|
* http://kjur.github.com/jsjws/license/
|
||
|
*
|
||
|
* The above copyright and license notice shall be
|
||
|
* included in all copies or substantial portions of the Software.
|
||
|
*
|
||
|
* DEPENDS ON:
|
||
|
* - base64.js - Tom Wu's Base64 library
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* @fileOverview
|
||
|
* @name base64x-1.1.js
|
||
|
* @author Kenji Urushima kenji.urushima@gmail.com
|
||
|
* @version asn1 1.1.6 (2015-Nov-11)
|
||
|
* @since jsrsasign 2.1
|
||
|
* @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* Base64URL and supplementary functions for Tom Wu's base64.js library.<br/>
|
||
|
* This class is just provide information about global functions
|
||
|
* defined in 'base64x.js'. The 'base64x.js' script file provides
|
||
|
* global functions for converting following data each other.
|
||
|
* <ul>
|
||
|
* <li>(ASCII) String</li>
|
||
|
* <li>UTF8 String including CJK, Latin and other characters</li>
|
||
|
* <li>byte array</li>
|
||
|
* <li>hexadecimal encoded String</li>
|
||
|
* <li>Full URIComponent encoded String (such like "%69%94")</li>
|
||
|
* <li>Base64 encoded String</li>
|
||
|
* <li>Base64URL encoded String</li>
|
||
|
* </ul>
|
||
|
* All functions in 'base64x.js' are defined in {@link _global_} and not
|
||
|
* in this class.
|
||
|
*
|
||
|
* @class Base64URL and supplementary functions for Tom Wu's base64.js library
|
||
|
* @author Kenji Urushima
|
||
|
* @version 1.1 (07 May 2012)
|
||
|
* @requires base64.js
|
||
|
* @see <a href="http://kjur.github.com/jsjws/">'jwjws'(JWS JavaScript Library) home page http://kjur.github.com/jsjws/</a>
|
||
|
* @see <a href="http://kjur.github.com/jsrsasigns/">'jwrsasign'(RSA Sign JavaScript Library) home page http://kjur.github.com/jsrsasign/</a>
|
||
|
*/
|
||
|
function Base64x() {
|
||
|
}
|
||
|
|
||
|
// ==== string / byte array ================================
|
||
|
/**
|
||
|
* convert a string to an array of character codes
|
||
|
* @param {String} s
|
||
|
* @return {Array of Numbers}
|
||
|
*/
|
||
|
function stoBA(s) {
|
||
|
var a = new Array();
|
||
|
for (var i = 0; i < s.length; i++) {
|
||
|
a[i] = s.charCodeAt(i);
|
||
|
}
|
||
|
return a;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* convert an array of character codes to a string
|
||
|
* @param {Array of Numbers} a array of character codes
|
||
|
* @return {String} s
|
||
|
*/
|
||
|
function BAtos(a) {
|
||
|
var s = "";
|
||
|
for (var i = 0; i < a.length; i++) {
|
||
|
s = s + String.fromCharCode(a[i]);
|
||
|
}
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
// ==== byte array / hex ================================
|
||
|
/**
|
||
|
* convert an array of bytes(Number) to hexadecimal string.<br/>
|
||
|
* @param {Array of Numbers} a array of bytes
|
||
|
* @return {String} hexadecimal string
|
||
|
*/
|
||
|
function BAtohex(a) {
|
||
|
var s = "";
|
||
|
for (var i = 0; i < a.length; i++) {
|
||
|
var hex1 = a[i].toString(16);
|
||
|
if (hex1.length == 1) hex1 = "0" + hex1;
|
||
|
s = s + hex1;
|
||
|
}
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
// ==== string / hex ================================
|
||
|
/**
|
||
|
* convert a ASCII string to a hexadecimal string of ASCII codes.<br/>
|
||
|
* NOTE: This can't be used for non ASCII characters.
|
||
|
* @param {s} s ASCII string
|
||
|
* @return {String} hexadecimal string
|
||
|
*/
|
||
|
function stohex(s) {
|
||
|
return BAtohex(stoBA(s));
|
||
|
}
|
||
|
|
||
|
// ==== string / base64 ================================
|
||
|
/**
|
||
|
* convert a ASCII string to a Base64 encoded string.<br/>
|
||
|
* NOTE: This can't be used for non ASCII characters.
|
||
|
* @param {s} s ASCII string
|
||
|
* @return {String} Base64 encoded string
|
||
|
*/
|
||
|
function stob64(s) {
|
||
|
return hex2b64(stohex(s));
|
||
|
}
|
||
|
|
||
|
// ==== string / base64url ================================
|
||
|
/**
|
||
|
* convert a ASCII string to a Base64URL encoded string.<br/>
|
||
|
* NOTE: This can't be used for non ASCII characters.
|
||
|
* @param {s} s ASCII string
|
||
|
* @return {String} Base64URL encoded string
|
||
|
*/
|
||
|
function stob64u(s) {
|
||
|
return b64tob64u(hex2b64(stohex(s)));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* convert a Base64URL encoded string to a ASCII string.<br/>
|
||
|
* NOTE: This can't be used for Base64URL encoded non ASCII characters.
|
||
|
* @param {s} s Base64URL encoded string
|
||
|
* @return {String} ASCII string
|
||
|
*/
|
||
|
function b64utos(s) {
|
||
|
return BAtos(b64toBA(b64utob64(s)));
|
||
|
}
|
||
|
|
||
|
// ==== base64 / base64url ================================
|
||
|
/**
|
||
|
* convert a Base64 encoded string to a Base64URL encoded string.<br/>
|
||
|
* Example: "ab+c3f/==" → "ab-c3f_"
|
||
|
* @param {String} s Base64 encoded string
|
||
|
* @return {String} Base64URL encoded string
|
||
|
*/
|
||
|
function b64tob64u(s) {
|
||
|
s = s.replace(/\=/g, "");
|
||
|
s = s.replace(/\+/g, "-");
|
||
|
s = s.replace(/\//g, "_");
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* convert a Base64URL encoded string to a Base64 encoded string.<br/>
|
||
|
* Example: "ab-c3f_" → "ab+c3f/=="
|
||
|
* @param {String} s Base64URL encoded string
|
||
|
* @return {String} Base64 encoded string
|
||
|
*/
|
||
|
function b64utob64(s) {
|
||
|
if (s.length % 4 == 2) s = s + "==";
|
||
|
else if (s.length % 4 == 3) s = s + "=";
|
||
|
s = s.replace(/-/g, "+");
|
||
|
s = s.replace(/_/g, "/");
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
// ==== hex / base64url ================================
|
||
|
/**
|
||
|
* convert a hexadecimal string to a Base64URL encoded string.<br/>
|
||
|
* @param {String} s hexadecimal string
|
||
|
* @return {String} Base64URL encoded string
|
||
|
* @description
|
||
|
* convert a hexadecimal string to a Base64URL encoded string.
|
||
|
* NOTE: If leading "0" is omitted and odd number length for
|
||
|
* hexadecimal leading "0" is automatically added.
|
||
|
*/
|
||
|
function hextob64u(s) {
|
||
|
if (s.length % 2 == 1) s = "0" + s;
|
||
|
return b64tob64u(hex2b64(s));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* convert a Base64URL encoded string to a hexadecimal string.<br/>
|
||
|
* @param {String} s Base64URL encoded string
|
||
|
* @return {String} hexadecimal string
|
||
|
*/
|
||
|
function b64utohex(s) {
|
||
|
return b64tohex(b64utob64(s));
|
||
|
}
|
||
|
|
||
|
var utf8tob64u, b64utoutf8;
|
||
|
|
||
|
if (typeof Buffer === 'function')
|
||
|
{
|
||
|
utf8tob64u = function (s)
|
||
|
{
|
||
|
return b64tob64u(new Buffer(s, 'utf8').toString('base64'));
|
||
|
};
|
||
|
|
||
|
b64utoutf8 = function (s)
|
||
|
{
|
||
|
return new Buffer(b64utob64(s), 'base64').toString('utf8');
|
||
|
};
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// ==== utf8 / base64url ================================
|
||
|
/**
|
||
|
* convert a UTF-8 encoded string including CJK or Latin to a Base64URL encoded string.<br/>
|
||
|
* @param {String} s UTF-8 encoded string
|
||
|
* @return {String} Base64URL encoded string
|
||
|
* @since 1.1
|
||
|
*/
|
||
|
utf8tob64u = function (s)
|
||
|
{
|
||
|
return hextob64u(uricmptohex(encodeURIComponentAll(s)));
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* convert a Base64URL encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
|
||
|
* @param {String} s Base64URL encoded string
|
||
|
* @return {String} UTF-8 encoded string
|
||
|
* @since 1.1
|
||
|
*/
|
||
|
b64utoutf8 = function (s)
|
||
|
{
|
||
|
return decodeURIComponent(hextouricmp(b64utohex(s)));
|
||
|
};
|
||
|
}
|
||
|
|
||
|
// ==== utf8 / base64url ================================
|
||
|
/**
|
||
|
* convert a UTF-8 encoded string including CJK or Latin to a Base64 encoded string.<br/>
|
||
|
* @param {String} s UTF-8 encoded string
|
||
|
* @return {String} Base64 encoded string
|
||
|
* @since 1.1.1
|
||
|
*/
|
||
|
function utf8tob64(s) {
|
||
|
return hex2b64(uricmptohex(encodeURIComponentAll(s)));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* convert a Base64 encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
|
||
|
* @param {String} s Base64 encoded string
|
||
|
* @return {String} UTF-8 encoded string
|
||
|
* @since 1.1.1
|
||
|
*/
|
||
|
function b64toutf8(s) {
|
||
|
return decodeURIComponent(hextouricmp(b64tohex(s)));
|
||
|
}
|
||
|
|
||
|
// ==== utf8 / hex ================================
|
||
|
/**
|
||
|
* convert a UTF-8 encoded string including CJK or Latin to a hexadecimal encoded string.<br/>
|
||
|
* @param {String} s UTF-8 encoded string
|
||
|
* @return {String} hexadecimal encoded string
|
||
|
* @since 1.1.1
|
||
|
*/
|
||
|
function utf8tohex(s) {
|
||
|
return uricmptohex(encodeURIComponentAll(s));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* convert a hexadecimal encoded string to a UTF-8 encoded string including CJK or Latin.<br/>
|
||
|
* Note that when input is improper hexadecimal string as UTF-8 string, this function returns
|
||
|
* 'null'.
|
||
|
* @param {String} s hexadecimal encoded string
|
||
|
* @return {String} UTF-8 encoded string or null
|
||
|
* @since 1.1.1
|
||
|
*/
|
||
|
function hextoutf8(s) {
|
||
|
return decodeURIComponent(hextouricmp(s));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* convert a hexadecimal encoded string to raw string including non printable characters.<br/>
|
||
|
* @param {String} s hexadecimal encoded string
|
||
|
* @return {String} raw string
|
||
|
* @since 1.1.2
|
||
|
* @example
|
||
|
* hextorstr("610061") → "a\x00a"
|
||
|
*/
|
||
|
function hextorstr(sHex) {
|
||
|
var s = "";
|
||
|
for (var i = 0; i < sHex.length - 1; i += 2) {
|
||
|
s += String.fromCharCode(parseInt(sHex.substr(i, 2), 16));
|
||
|
}
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* convert a raw string including non printable characters to hexadecimal encoded string.<br/>
|
||
|
* @param {String} s raw string
|
||
|
* @return {String} hexadecimal encoded string
|
||
|
* @since 1.1.2
|
||
|
* @example
|
||
|
* rstrtohex("a\x00a") → "610061"
|
||
|
*/
|
||
|
function rstrtohex(s) {
|
||
|
var result = "";
|
||
|
for (var i = 0; i < s.length; i++) {
|
||
|
result += ("0" + s.charCodeAt(i).toString(16)).slice(-2);
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
// ==== hex / b64nl =======================================
|
||
|
|
||
|
/*
|
||
|
* since base64x 1.1.3
|
||
|
*/
|
||
|
function hextob64(s) {
|
||
|
return hex2b64(s);
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* since base64x 1.1.3
|
||
|
*/
|
||
|
function hextob64nl(s) {
|
||
|
var b64 = hextob64(s);
|
||
|
var b64nl = b64.replace(/(.{64})/g, "$1\r\n");
|
||
|
b64nl = b64nl.replace(/\r\n$/, '');
|
||
|
return b64nl;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* since base64x 1.1.3
|
||
|
*/
|
||
|
function b64nltohex(s) {
|
||
|
var b64 = s.replace(/[^0-9A-Za-z\/+=]*/g, '');
|
||
|
var hex = b64tohex(b64);
|
||
|
return hex;
|
||
|
}
|
||
|
|
||
|
// ==== URIComponent / hex ================================
|
||
|
/**
|
||
|
* convert a URLComponent string such like "%67%68" to a hexadecimal string.<br/>
|
||
|
* @param {String} s URIComponent string such like "%67%68"
|
||
|
* @return {String} hexadecimal string
|
||
|
* @since 1.1
|
||
|
*/
|
||
|
function uricmptohex(s) {
|
||
|
return s.replace(/%/g, "");
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* convert a hexadecimal string to a URLComponent string such like "%67%68".<br/>
|
||
|
* @param {String} s hexadecimal string
|
||
|
* @return {String} URIComponent string such like "%67%68"
|
||
|
* @since 1.1
|
||
|
*/
|
||
|
function hextouricmp(s) {
|
||
|
return s.replace(/(..)/g, "%$1");
|
||
|
}
|
||
|
|
||
|
// ==== URIComponent ================================
|
||
|
/**
|
||
|
* convert UTFa hexadecimal string to a URLComponent string such like "%67%68".<br/>
|
||
|
* Note that these "<code>0-9A-Za-z!'()*-._~</code>" characters will not
|
||
|
* converted to "%xx" format by builtin 'encodeURIComponent()' function.
|
||
|
* However this 'encodeURIComponentAll()' function will convert
|
||
|
* all of characters into "%xx" format.
|
||
|
* @param {String} s hexadecimal string
|
||
|
* @return {String} URIComponent string such like "%67%68"
|
||
|
* @since 1.1
|
||
|
*/
|
||
|
function encodeURIComponentAll(u8) {
|
||
|
var s = encodeURIComponent(u8);
|
||
|
var s2 = "";
|
||
|
for (var i = 0; i < s.length; i++) {
|
||
|
if (s[i] == "%") {
|
||
|
s2 = s2 + s.substr(i, 3);
|
||
|
i = i + 2;
|
||
|
} else {
|
||
|
s2 = s2 + "%" + stohex(s[i]);
|
||
|
}
|
||
|
}
|
||
|
return s2;
|
||
|
}
|
||
|
|
||
|
// ==== new lines ================================
|
||
|
/**
|
||
|
* convert all DOS new line("\r\n") to UNIX new line("\n") in
|
||
|
* a String "s".
|
||
|
* @param {String} s string
|
||
|
* @return {String} converted string
|
||
|
*/
|
||
|
function newline_toUnix(s) {
|
||
|
s = s.replace(/\r\n/mg, "\n");
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* convert all UNIX new line("\r\n") to DOS new line("\n") in
|
||
|
* a String "s".
|
||
|
* @param {String} s string
|
||
|
* @return {String} converted string
|
||
|
*/
|
||
|
function newline_toDos(s) {
|
||
|
s = s.replace(/\r\n/mg, "\n");
|
||
|
s = s.replace(/\n/mg, "\r\n");
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
// ==== others ================================
|
||
|
|
||
|
/**
|
||
|
* convert string of integer array to hexadecimal string.<br/>
|
||
|
* @param {String} s string of integer array
|
||
|
* @return {String} hexadecimal string
|
||
|
* @since base64x 1.1.6 jsrsasign 5.0.2
|
||
|
* @throws "malformed integer array string: *" for wrong input
|
||
|
* @description
|
||
|
* This function converts a string of JavaScript integer array to
|
||
|
* a hexadecimal string. Each integer value shall be in a range
|
||
|
* from 0 to 255 otherwise it raise exception. Input string can
|
||
|
* have extra space or newline string so that they will be ignored.
|
||
|
*
|
||
|
* @example
|
||
|
* intarystrtohex(" [123, 34, 101, 34, 58] ")
|
||
|
* -> 7b2265223a (i.e. `{"e":` as string)
|
||
|
*/
|
||
|
function intarystrtohex(s) {
|
||
|
s = s.replace(/^\s*\[\s*/, '');
|
||
|
s = s.replace(/\s*\]\s*$/, '');
|
||
|
s = s.replace(/\s*/g, '');
|
||
|
try {
|
||
|
var hex = s.split(/,/).map(function(element, index, array) {
|
||
|
var i = parseInt(element);
|
||
|
if (i < 0 || 255 < i) throw "integer not in range 0-255";
|
||
|
var hI = ("00" + i.toString(16)).slice(-2);
|
||
|
return hI;
|
||
|
}).join('');
|
||
|
return hex;
|
||
|
} catch(ex) {
|
||
|
throw "malformed integer array string: " + ex;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* find index of string where two string differs
|
||
|
* @param {String} s1 string to compare
|
||
|
* @param {String} s2 string to compare
|
||
|
* @return {Number} string index of where character differs. Return -1 if same.
|
||
|
* @since jsrsasign 4.9.0 base64x 1.1.5
|
||
|
* @example
|
||
|
* strdiffidx("abcdefg", "abcd4fg") -> 4
|
||
|
* strdiffidx("abcdefg", "abcdefg") -> -1
|
||
|
* strdiffidx("abcdefg", "abcdef") -> 6
|
||
|
* strdiffidx("abcdefgh", "abcdef") -> 6
|
||
|
*/
|
||
|
var strdiffidx = function(s1, s2) {
|
||
|
var n = s1.length;
|
||
|
if (s1.length > s2.length) n = s2.length;
|
||
|
for (var i = 0; i < n; i++) {
|
||
|
if (s1.charCodeAt(i) != s2.charCodeAt(i)) return i;
|
||
|
}
|
||
|
if (s1.length != s2.length) return n;
|
||
|
return -1; // same
|
||
|
};
|