2015-06-10 23:59:01 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function utf8ToBinaryString(str) {
|
|
|
|
var escstr = encodeURIComponent(str);
|
|
|
|
// replaces any uri escape sequence, such as %0A,
|
|
|
|
// with binary escape, such as 0x0A
|
|
|
|
var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) {
|
|
|
|
return String.fromCharCode('0x' + p1);
|
|
|
|
});
|
|
|
|
|
|
|
|
return binstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function utf8ToBuffer(str) {
|
|
|
|
var binstr = utf8ToBinaryString(str);
|
|
|
|
var buf = binaryStringToBuffer(binstr);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
function utf8ToBase64(str) {
|
|
|
|
var binstr = utf8ToBinaryString(str);
|
|
|
|
return btoa(binstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function binaryStringToUtf8(binstr) {
|
|
|
|
var escstr = binstr.replace(/(.)/g, function (m, p) {
|
|
|
|
var code = p.charCodeAt(p).toString(16).toUpperCase();
|
|
|
|
if (code.length < 2) {
|
|
|
|
code = '0' + code;
|
|
|
|
}
|
|
|
|
return '%' + code;
|
|
|
|
});
|
|
|
|
|
|
|
|
return decodeURIComponent(escstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function bufferToUtf8(buf) {
|
2015-06-14 03:36:57 +00:00
|
|
|
var binstr = bufferToBinaryString(buf);
|
2015-06-10 23:59:01 +00:00
|
|
|
|
|
|
|
return binaryStringToUtf8(binstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function base64ToUtf8(b64) {
|
|
|
|
var binstr = atob(b64);
|
|
|
|
|
|
|
|
return binaryStringToUtf8(binstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function bufferToBinaryString(buf) {
|
|
|
|
var binstr = Array.prototype.map.call(buf, function (ch) {
|
2015-06-11 01:28:03 +00:00
|
|
|
return String.fromCharCode(ch);
|
2015-06-10 23:59:01 +00:00
|
|
|
}).join('');
|
|
|
|
|
|
|
|
return binstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function bufferToBase64(arr) {
|
|
|
|
var binstr = bufferToBinaryString(arr);
|
|
|
|
return btoa(binstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function binaryStringToBuffer(binstr) {
|
|
|
|
var buf;
|
|
|
|
|
2015-06-27 08:11:59 +00:00
|
|
|
if ('undefined' !== typeof Uint8Array) {
|
2015-06-10 23:59:01 +00:00
|
|
|
buf = new Uint8Array(binstr.length);
|
|
|
|
} else {
|
|
|
|
buf = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
Array.prototype.forEach.call(binstr, function (ch, i) {
|
|
|
|
buf[i] = ch.charCodeAt(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
function base64ToBuffer(base64) {
|
|
|
|
var binstr = atob(base64);
|
|
|
|
var buf = binaryStringToBuffer(binstr);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.Unibabel = {
|
|
|
|
utf8ToBinaryString: utf8ToBinaryString
|
|
|
|
, utf8ToBuffer: utf8ToBuffer
|
|
|
|
, utf8ToBase64: utf8ToBase64
|
|
|
|
, binaryStringToUtf8: binaryStringToUtf8
|
|
|
|
, bufferToUtf8: bufferToUtf8
|
|
|
|
, base64ToUtf8: base64ToUtf8
|
|
|
|
, bufferToBinaryString: bufferToBinaryString
|
|
|
|
, bufferToBase64: bufferToBase64
|
|
|
|
, binaryStringToBuffer: binaryStringToBuffer
|
|
|
|
, base64ToBuffer: base64ToBuffer
|
|
|
|
|
|
|
|
// compat
|
|
|
|
, strToUtf8Arr: utf8ToBuffer
|
|
|
|
, utf8ArrToStr: bufferToUtf8
|
|
|
|
, arrToBase64: bufferToBase64
|
|
|
|
, base64ToArr: base64ToBuffer
|
|
|
|
};
|
|
|
|
|
|
|
|
}());
|