2019-07-27 19:02:24 -03:00
|
|
|
"use strict";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { arrayify } from "@ethersproject/bytes";
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2019-07-27 19:02:24 -03:00
|
|
|
///////////////////////////////
|
2019-08-25 02:39:20 -04:00
|
|
|
export var UnicodeNormalizationForm;
|
2019-07-27 19:02:24 -03:00
|
|
|
(function (UnicodeNormalizationForm) {
|
|
|
|
UnicodeNormalizationForm["current"] = "";
|
|
|
|
UnicodeNormalizationForm["NFC"] = "NFC";
|
|
|
|
UnicodeNormalizationForm["NFD"] = "NFD";
|
|
|
|
UnicodeNormalizationForm["NFKC"] = "NFKC";
|
|
|
|
UnicodeNormalizationForm["NFKD"] = "NFKD";
|
2019-08-25 02:39:20 -04:00
|
|
|
})(UnicodeNormalizationForm || (UnicodeNormalizationForm = {}));
|
2019-07-27 19:02:24 -03:00
|
|
|
;
|
2020-01-20 19:43:50 -05:00
|
|
|
export var Utf8ErrorReason;
|
|
|
|
(function (Utf8ErrorReason) {
|
|
|
|
// A continuation byte was present where there was nothing to continue
|
|
|
|
// - offset = the index the codepoint began in
|
|
|
|
Utf8ErrorReason["UNEXPECTED_CONTINUE"] = "unexpected continuation byte";
|
|
|
|
// An invalid (non-continuation) byte to start a UTF-8 codepoint was found
|
|
|
|
// - offset = the index the codepoint began in
|
|
|
|
Utf8ErrorReason["BAD_PREFIX"] = "bad codepoint prefix";
|
|
|
|
// The string is too short to process the expected codepoint
|
|
|
|
// - offset = the index the codepoint began in
|
|
|
|
Utf8ErrorReason["OVERRUN"] = "string overrun";
|
|
|
|
// A missing continuation byte was expected but not found
|
|
|
|
// - offset = the index the continuation byte was expected at
|
|
|
|
Utf8ErrorReason["MISSING_CONTINUE"] = "missing continuation byte";
|
|
|
|
// The computed code point is outside the range for UTF-8
|
|
|
|
// - offset = start of this codepoint
|
|
|
|
// - badCodepoint = the computed codepoint; outside the UTF-8 range
|
|
|
|
Utf8ErrorReason["OUT_OF_RANGE"] = "out of UTF-8 range";
|
|
|
|
// UTF-8 strings may not contain UTF-16 surrogate pairs
|
|
|
|
// - offset = start of this codepoint
|
|
|
|
// - badCodepoint = the computed codepoint; inside the UTF-16 surrogate range
|
|
|
|
Utf8ErrorReason["UTF16_SURROGATE"] = "UTF-16 surrogate";
|
|
|
|
// The string is an overlong reperesentation
|
|
|
|
// - offset = start of this codepoint
|
|
|
|
// - badCodepoint = the computed codepoint; already bounds checked
|
|
|
|
Utf8ErrorReason["OVERLONG"] = "overlong representation";
|
|
|
|
})(Utf8ErrorReason || (Utf8ErrorReason = {}));
|
|
|
|
;
|
|
|
|
function errorFunc(reason, offset, bytes, output, badCodepoint) {
|
|
|
|
return logger.throwArgumentError(`invalid codepoint at offset ${offset}; ${reason}`, "bytes", bytes);
|
|
|
|
}
|
|
|
|
function ignoreFunc(reason, offset, bytes, output, badCodepoint) {
|
|
|
|
// If there is an invalid prefix (including stray continuation), skip any additional continuation bytes
|
|
|
|
if (reason === Utf8ErrorReason.BAD_PREFIX || reason === Utf8ErrorReason.UNEXPECTED_CONTINUE) {
|
|
|
|
let i = 0;
|
|
|
|
for (let o = offset + 1; o < bytes.length; o++) {
|
|
|
|
if (bytes[o] >> 6 !== 0x02) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
// This byte runs us past the end of the string, so just jump to the end
|
|
|
|
// (but the first byte was read already read and therefore skipped)
|
|
|
|
if (reason === Utf8ErrorReason.OVERRUN) {
|
|
|
|
return bytes.length - offset - 1;
|
|
|
|
}
|
|
|
|
// Nothing to skip
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
function replaceFunc(reason, offset, bytes, output, badCodepoint) {
|
|
|
|
// Overlong representations are otherwise "valid" code points; just non-deistingtished
|
|
|
|
if (reason === Utf8ErrorReason.OVERLONG) {
|
|
|
|
output.push(badCodepoint);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// Put the replacement character into the output
|
|
|
|
output.push(0xfffd);
|
|
|
|
// Otherwise, process as if ignoring errors
|
|
|
|
return ignoreFunc(reason, offset, bytes, output, badCodepoint);
|
|
|
|
}
|
|
|
|
// Common error handing strategies
|
|
|
|
export const Utf8ErrorFuncs = Object.freeze({
|
|
|
|
error: errorFunc,
|
|
|
|
ignore: ignoreFunc,
|
|
|
|
replace: replaceFunc
|
|
|
|
});
|
2019-07-27 19:02:24 -03:00
|
|
|
// http://stackoverflow.com/questions/13356493/decode-utf-8-with-javascript#13691499
|
2020-01-20 19:43:50 -05:00
|
|
|
function getUtf8CodePoints(bytes, onError) {
|
|
|
|
if (onError == null) {
|
|
|
|
onError = Utf8ErrorFuncs.error;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
bytes = arrayify(bytes);
|
2019-11-20 18:57:38 +09:00
|
|
|
const result = [];
|
2019-08-25 02:39:20 -04:00
|
|
|
let i = 0;
|
2019-07-27 19:02:24 -03:00
|
|
|
// Invalid bytes are ignored
|
|
|
|
while (i < bytes.length) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const c = bytes[i++];
|
2019-07-27 19:02:24 -03:00
|
|
|
// 0xxx xxxx
|
|
|
|
if (c >> 7 === 0) {
|
|
|
|
result.push(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Multibyte; how many bytes left for this character?
|
2019-08-25 02:39:20 -04:00
|
|
|
let extraLength = null;
|
|
|
|
let overlongMask = null;
|
2019-07-27 19:02:24 -03:00
|
|
|
// 110x xxxx 10xx xxxx
|
|
|
|
if ((c & 0xe0) === 0xc0) {
|
|
|
|
extraLength = 1;
|
|
|
|
overlongMask = 0x7f;
|
|
|
|
// 1110 xxxx 10xx xxxx 10xx xxxx
|
|
|
|
}
|
|
|
|
else if ((c & 0xf0) === 0xe0) {
|
|
|
|
extraLength = 2;
|
|
|
|
overlongMask = 0x7ff;
|
|
|
|
// 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx
|
|
|
|
}
|
|
|
|
else if ((c & 0xf8) === 0xf0) {
|
|
|
|
extraLength = 3;
|
|
|
|
overlongMask = 0xffff;
|
|
|
|
}
|
|
|
|
else {
|
2020-01-20 19:43:50 -05:00
|
|
|
if ((c & 0xc0) === 0x80) {
|
|
|
|
i += onError(Utf8ErrorReason.UNEXPECTED_CONTINUE, i - 1, bytes, result);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i += onError(Utf8ErrorReason.BAD_PREFIX, i - 1, bytes, result);
|
2019-07-27 19:02:24 -03:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Do we have enough bytes in our data?
|
2020-01-20 19:43:50 -05:00
|
|
|
if (i - 1 + extraLength >= bytes.length) {
|
|
|
|
i += onError(Utf8ErrorReason.OVERRUN, i - 1, bytes, result);
|
2019-07-27 19:02:24 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Remove the length prefix from the char
|
2019-08-25 02:39:20 -04:00
|
|
|
let res = c & ((1 << (8 - extraLength - 1)) - 1);
|
|
|
|
for (let j = 0; j < extraLength; j++) {
|
|
|
|
let nextChar = bytes[i];
|
2019-07-27 19:02:24 -03:00
|
|
|
// Invalid continuation byte
|
|
|
|
if ((nextChar & 0xc0) != 0x80) {
|
2020-01-20 19:43:50 -05:00
|
|
|
i += onError(Utf8ErrorReason.MISSING_CONTINUE, i, bytes, result);
|
2019-07-27 19:02:24 -03:00
|
|
|
res = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
res = (res << 6) | (nextChar & 0x3f);
|
|
|
|
i++;
|
|
|
|
}
|
2020-01-20 19:43:50 -05:00
|
|
|
// See above loop for invalid contimuation byte
|
2019-07-27 19:02:24 -03:00
|
|
|
if (res === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Maximum code point
|
|
|
|
if (res > 0x10ffff) {
|
2020-01-20 19:43:50 -05:00
|
|
|
i += onError(Utf8ErrorReason.OUT_OF_RANGE, i - 1 - extraLength, bytes, result, res);
|
2019-07-27 19:02:24 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Reserved for UTF-16 surrogate halves
|
|
|
|
if (res >= 0xd800 && res <= 0xdfff) {
|
2020-01-20 19:43:50 -05:00
|
|
|
i += onError(Utf8ErrorReason.UTF16_SURROGATE, i - 1 - extraLength, bytes, result, res);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Check for overlong sequences (more bytes than needed)
|
|
|
|
if (res <= overlongMask) {
|
|
|
|
i += onError(Utf8ErrorReason.OVERLONG, i - 1 - extraLength, bytes, result, res);
|
2019-07-27 19:02:24 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result.push(res);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// http://stackoverflow.com/questions/18729405/how-to-convert-utf8-string-to-byte-array
|
2019-08-25 02:39:20 -04:00
|
|
|
export function toUtf8Bytes(str, form = UnicodeNormalizationForm.current) {
|
2019-07-27 19:02:24 -03:00
|
|
|
if (form != UnicodeNormalizationForm.current) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.checkNormalize();
|
2019-07-27 19:02:24 -03:00
|
|
|
str = str.normalize(form);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let result = [];
|
|
|
|
for (let i = 0; i < str.length; i++) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const c = str.charCodeAt(i);
|
2019-07-27 19:02:24 -03:00
|
|
|
if (c < 0x80) {
|
|
|
|
result.push(c);
|
|
|
|
}
|
|
|
|
else if (c < 0x800) {
|
|
|
|
result.push((c >> 6) | 0xc0);
|
|
|
|
result.push((c & 0x3f) | 0x80);
|
|
|
|
}
|
|
|
|
else if ((c & 0xfc00) == 0xd800) {
|
|
|
|
i++;
|
2019-11-20 18:57:38 +09:00
|
|
|
const c2 = str.charCodeAt(i);
|
2019-07-27 19:02:24 -03:00
|
|
|
if (i >= str.length || (c2 & 0xfc00) !== 0xdc00) {
|
|
|
|
throw new Error("invalid utf-8 string");
|
|
|
|
}
|
|
|
|
// Surrogate Pair
|
2019-11-20 18:57:38 +09:00
|
|
|
const pair = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff);
|
|
|
|
result.push((pair >> 18) | 0xf0);
|
|
|
|
result.push(((pair >> 12) & 0x3f) | 0x80);
|
|
|
|
result.push(((pair >> 6) & 0x3f) | 0x80);
|
|
|
|
result.push((pair & 0x3f) | 0x80);
|
2019-07-27 19:02:24 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.push((c >> 12) | 0xe0);
|
|
|
|
result.push(((c >> 6) & 0x3f) | 0x80);
|
|
|
|
result.push((c & 0x3f) | 0x80);
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
return arrayify(result);
|
2019-07-27 19:02:24 -03:00
|
|
|
}
|
|
|
|
;
|
|
|
|
function escapeChar(value) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const hex = ("0000" + value.toString(16));
|
2019-07-27 19:02:24 -03:00
|
|
|
return "\\u" + hex.substring(hex.length - 4);
|
|
|
|
}
|
2020-01-20 19:43:50 -05:00
|
|
|
export function _toEscapedUtf8String(bytes, onError) {
|
|
|
|
return '"' + getUtf8CodePoints(bytes, onError).map((codePoint) => {
|
2019-07-27 19:02:24 -03:00
|
|
|
if (codePoint < 256) {
|
|
|
|
switch (codePoint) {
|
|
|
|
case 8: return "\\b";
|
|
|
|
case 9: return "\\t";
|
|
|
|
case 10: return "\\n";
|
|
|
|
case 13: return "\\r";
|
|
|
|
case 34: return "\\\"";
|
|
|
|
case 92: return "\\\\";
|
|
|
|
}
|
|
|
|
if (codePoint >= 32 && codePoint < 127) {
|
|
|
|
return String.fromCharCode(codePoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (codePoint <= 0xffff) {
|
|
|
|
return escapeChar(codePoint);
|
|
|
|
}
|
|
|
|
codePoint -= 0x10000;
|
|
|
|
return escapeChar(((codePoint >> 10) & 0x3ff) + 0xd800) + escapeChar((codePoint & 0x3ff) + 0xdc00);
|
|
|
|
}).join("") + '"';
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function _toUtf8String(codePoints) {
|
|
|
|
return codePoints.map((codePoint) => {
|
2019-07-27 19:02:24 -03:00
|
|
|
if (codePoint <= 0xffff) {
|
|
|
|
return String.fromCharCode(codePoint);
|
|
|
|
}
|
|
|
|
codePoint -= 0x10000;
|
|
|
|
return String.fromCharCode((((codePoint >> 10) & 0x3ff) + 0xd800), ((codePoint & 0x3ff) + 0xdc00));
|
|
|
|
}).join("");
|
|
|
|
}
|
2020-01-20 19:43:50 -05:00
|
|
|
export function toUtf8String(bytes, onError) {
|
|
|
|
return _toUtf8String(getUtf8CodePoints(bytes, onError));
|
2019-08-02 02:10:58 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export function toUtf8CodePoints(str, form = UnicodeNormalizationForm.current) {
|
2019-07-27 19:02:24 -03:00
|
|
|
return getUtf8CodePoints(toUtf8Bytes(str, form));
|
|
|
|
}
|