123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- /*! base64x-1.1.5 (c) 2012-2015 Kenji Urushima | kjur.github.com/jsjws/license
- */
- /*
- * base64x.js - Base64url and supplementary functions for Tom Wu's base64.js library
- *
- * version: 1.1.5 (2015-Sep-13)
- *
- * 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.5 (2015-Sep-13)
- * @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 ================================
- /**
- * 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
- };
|