* limitations under the License.
*/
-var Base64 = {
- _b64: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
- encodeString: function(str) {
- return this.encode(this._utf8_encode(str));
- },
- encode: function(data) {
- var output = '';
- var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
- var i = 0;
-
- while (i < data.length) {
-
- chr1 = data[i++];
- chr2 = data[i++];
- chr3 = data[i++];
-
- enc1 = chr1 >> 2;
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
- enc4 = chr3 & 63;
-
- if (isNaN(chr2)) {
- enc3 = enc4 = 64;
- } else if (isNaN(chr3)) {
- enc4 = 64;
- }
-
- output += this._b64.charAt(enc1) + this._b64.charAt(enc2) +
- this._b64.charAt(enc3) + this._b64.charAt(enc4);
-
- }
-
- return output;
- },
- decodeString: function(data) {
- return this._utf8_decode(this.decode(data));
- },
- decode: function(data) {
- var output = [];
- var chr1, chr2, chr3;
- var enc1, enc2, enc3, enc4;
- var i = 0;
-
- data = data.replace(/[^A-Za-z0-9\+\/\=]/g, '');
-
- while (i < data.length) {
-
- enc1 = this._b64.indexOf(data.charAt(i++));
- enc2 = this._b64.indexOf(data.charAt(i++));
- enc3 = this._b64.indexOf(data.charAt(i++));
- enc4 = this._b64.indexOf(data.charAt(i++));
-
- chr1 = (enc1 << 2) | (enc2 >> 4);
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
- chr3 = ((enc3 & 3) << 6) | enc4;
-
- output.push(chr1);
-
- if (enc3 !== 64) {
- output.push(chr2);
- }
- if (enc4 !== 64) {
- output.push(chr3);
- }
-
- }
-
- return output;
- },
- getCodePoint: function(string, position) {
- var highWord = string.charCodeAt(position);
-
- if ((highWord & 0xFC00) === 0xD800) {
- if (position + 1 >= string.length) {
- return undefined; //the string is corrupted
- }
-
- var lowWord = string.charCodeAt(position + 1);
- if ((lowWord & 0xFC00) === 0xDC00) {
- return ((highWord & 0x03FF) << 10) | (lowWord & 0x03FF) + 0x10000;
- } else {
- return undefined; //the string is corrupted
- }
- }
- return highWord;
- },
- _utf8_encode: function(str) {
- str = str.replace(/\r\n/g, '\n');
- var utfarray = [];
-
- //TODO: use for( var c of str ) in future versions
- for (var offset = 0; offset < str.length; offset++) {
- var code = this.getCodePoint(str, offset);
-
- if (code <= 0x7F) {
- utfarray.push(code);
- }
- else if (code <= 0x7FF) {
- utfarray.push( 0xC0 | (code >> 6), 0x80 | (code & 0x3F) );
- }
- else if (code <= 0xFFFF) {
- utfarray.push( 0xE0 | (code >> 12), 0x80 | ((code >> 6) & 0x3F), 0x80 | (code & 0x3F) );
- }
- else {
- utfarray.push( 0xF0 | (code >> 18), 0x80 | ((code >> 12) & 0x3F),
- 0x80 | ((code >> 6) & 0x3F), 0x80 | (code & 0x3F) );
- offset++; //there is a UTF16 surrogate pair in str, so jump two elements
- }
+function base64_encode(data) {
+ var _b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
+ var output = '';
+ var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
+ var i = 0;
+
+ while (i < data.length) {
+ chr1 = data[i++];
+ chr2 = data[i++];
+ chr3 = data[i++];
+
+ enc1 = chr1 >> 2;
+ enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
+ enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
+ enc4 = chr3 & 63;
+
+ if (isNaN(chr2)) {
+ enc3 = enc4 = 64;
+ } else if (isNaN(chr3)) {
+ enc4 = 64;
}
- return utfarray;
- },
-
- /*
- * This function validates read characters. Non-standard UTF-8 characters are substituted with
- * a replacement symbol.
- *
- * Used validation check cases are described in http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt,
- * by Markus Kuhn, distributed under CC-BY 4.0 license (https://creativecommons.org/licenses/by/4.0/legalcode).
- */
-
- _utf8_decode: function(utfarray) {
- var str = '';
- var i = 0, c = 0, c1 = 0, c2 = 0, c3 = 0, charCode = 0;
- var INVALID_CHARACTER = String.fromCharCode(0xFFFD);
-
- while (i < utfarray.length) {
- c = utfarray[i];
-
- if (c < 128) {
- str += String.fromCharCode(c);
- i++;
- } else if ((c >= 194) && (c < 224) && (utfarray[i + 1] & 0x80)) {
- c1 = utfarray[i + 1];
- charCode = ((c & 31) << 6) | (c1 & 63);
- /*
- * Below condition is true, if the sequence could be encoded in less than 2 bytes.
- * Such a byte series is invalid in terms of UTF-8.
- * This and similar, longer, sequences will be refered to as "overlong sequence".
- */
- if (!(charCode & 0xFF80)) {
- str += INVALID_CHARACTER;
- } else {
- str += String.fromCharCode(charCode);
- }
-
- i += 2;
- } else if ((c >= 224) && (c < 240) && (utfarray[i + 1] & 0x80) && (utfarray[i + 2] & 0x80)) {
- c1 = utfarray[i + 1];
- c2 = utfarray[i + 2];
- charCode = ((c & 15) << 12) | ((c1 & 63) << 6) | (c2 & 63);
-
- if (!(charCode & 0xF800) //overlong sequence test
- /*
- * Below test checks, if the character is an UTF-16 surrogate halve,
- * UTF-16 surrogate halves are invalid Unicode codepoints.
- */
- || (0xD800 <= charCode && charCode <=0xDFFF)) {
- str += INVALID_CHARACTER;
- } else {
- str += String.fromCharCode(charCode);
- }
-
- i += 3;
- } else if ((c >= 240) && (c < 245) & (utfarray[i + 1] & 0x80) && (utfarray[i + 2] & 0x80) && (utfarray[i + 3] & 0x80)) {
- c1 = utfarray[i + 1];
- c2 = utfarray[i + 2];
- c3 = utfarray[i + 3];
- charCode = ((c & 7) << 18) | ((c1 & 63) << 12) | ((c2 & 63) << 6) | (c3 & 63);
-
- if (!(charCode & 0x1F0000)) { //overlong sequence test
- str += INVALID_CHARACTER;
- } else {
- str += String.fromCharCode(charCode);
- }
- i += 4;
- /*
- * Below condition is true if a continuation byte appeared without a proper leading byte
- */
- } else if ((c & 0x80) && (~c & 0x40)) {
- str += INVALID_CHARACTER;
- i++;
- } else {
- /*
- * One or more continuation bytes are missing
- * OR 'c' is a prohibited byte in terms of UTF-8 standard.
- */
- str += INVALID_CHARACTER;
-
- /*
- * All following continuation bytes are skipped.
- */
- do {
- i++;
- } while((utfarray[i] & 0x80) && (~utfarray[i] & 0x40));
- }
- }
-
- return str;
+ output += _b64.charAt(enc1) + _b64.charAt(enc2) +
+ _b64.charAt(enc3) + _b64.charAt(enc4);
}
-};
+
+ return output;
+}