Spaces:
Sleeping
Sleeping
File size: 7,735 Bytes
05a77ff |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
"use strict";
const whatwgEncoding = require("whatwg-encoding");
// https://html.spec.whatwg.org/#encoding-sniffing-algorithm
module.exports = (uint8Array, { transportLayerEncodingLabel, defaultEncoding = "windows-1252" } = {}) => {
let encoding = whatwgEncoding.getBOMEncoding(uint8Array);
if (encoding === null && transportLayerEncodingLabel !== undefined) {
encoding = whatwgEncoding.labelToName(transportLayerEncodingLabel);
}
if (encoding === null) {
encoding = prescanMetaCharset(uint8Array);
}
if (encoding === null) {
encoding = defaultEncoding;
}
return encoding;
};
// https://html.spec.whatwg.org/multipage/syntax.html#prescan-a-byte-stream-to-determine-its-encoding
function prescanMetaCharset(uint8Array) {
const l = Math.min(uint8Array.byteLength, 1024);
for (let i = 0; i < l; i++) {
let c = uint8Array[i];
if (c === 0x3C) {
// "<"
const c1 = uint8Array[i + 1];
const c2 = uint8Array[i + 2];
const c3 = uint8Array[i + 3];
const c4 = uint8Array[i + 4];
const c5 = uint8Array[i + 5];
// !-- (comment start)
if (c1 === 0x21 && c2 === 0x2D && c3 === 0x2D) {
i += 4;
for (; i < l; i++) {
c = uint8Array[i];
const cMinus1 = uint8Array[i - 1];
const cMinus2 = uint8Array[i - 2];
// --> (comment end)
if (c === 0x3E && cMinus1 === 0x2D && cMinus2 === 0x2D) {
break;
}
}
} else if ((c1 === 0x4D || c1 === 0x6D) &&
(c2 === 0x45 || c2 === 0x65) &&
(c3 === 0x54 || c3 === 0x74) &&
(c4 === 0x41 || c4 === 0x61) &&
(isSpaceCharacter(c5) || c5 === 0x2F)) {
// "meta" + space or /
i += 6;
const attributeList = new Set();
let gotPragma = false;
let needPragma = null;
let charset = null;
let attrRes;
do {
attrRes = getAttribute(uint8Array, i, l);
if (attrRes.attr && !attributeList.has(attrRes.attr.name)) {
attributeList.add(attrRes.attr.name);
if (attrRes.attr.name === "http-equiv") {
gotPragma = attrRes.attr.value === "content-type";
} else if (attrRes.attr.name === "content" && !charset) {
charset = extractCharacterEncodingFromMeta(attrRes.attr.value);
if (charset !== null) {
needPragma = true;
}
} else if (attrRes.attr.name === "charset") {
charset = whatwgEncoding.labelToName(attrRes.attr.value);
needPragma = false;
}
}
i = attrRes.i;
} while (attrRes.attr);
if (needPragma === null) {
continue;
}
if (needPragma === true && gotPragma === false) {
continue;
}
if (charset === null) {
continue;
}
if (charset === "UTF-16LE" || charset === "UTF-16BE") {
charset = "UTF-8";
}
if (charset === "x-user-defined") {
charset = "windows-1252";
}
return charset;
} else if ((c1 >= 0x41 && c1 <= 0x5A) || (c1 >= 0x61 && c1 <= 0x7A)) {
// a-z or A-Z
for (i += 2; i < l; i++) {
c = uint8Array[i];
// space or >
if (isSpaceCharacter(c) || c === 0x3E) {
break;
}
}
let attrRes;
do {
attrRes = getAttribute(uint8Array, i, l);
i = attrRes.i;
} while (attrRes.attr);
} else if (c1 === 0x21 || c1 === 0x2F || c1 === 0x3F) {
// ! or / or ?
for (i += 2; i < l; i++) {
c = uint8Array[i];
// >
if (c === 0x3E) {
break;
}
}
}
}
}
return null;
}
// https://html.spec.whatwg.org/multipage/syntax.html#concept-get-attributes-when-sniffing
function getAttribute(uint8Array, i, l) {
for (; i < l; i++) {
let c = uint8Array[i];
// space or /
if (isSpaceCharacter(c) || c === 0x2F) {
continue;
}
// ">"
if (c === 0x3E) {
break;
}
let name = "";
let value = "";
nameLoop:for (; i < l; i++) {
c = uint8Array[i];
// "="
if (c === 0x3D && name !== "") {
i++;
break;
}
// space
if (isSpaceCharacter(c)) {
for (i++; i < l; i++) {
c = uint8Array[i];
// space
if (isSpaceCharacter(c)) {
continue;
}
// not "="
if (c !== 0x3D) {
return { attr: { name, value }, i };
}
i++;
break nameLoop;
}
break;
}
// / or >
if (c === 0x2F || c === 0x3E) {
return { attr: { name, value }, i };
}
// A-Z
if (c >= 0x41 && c <= 0x5A) {
name += String.fromCharCode(c + 0x20); // lowercase
} else {
name += String.fromCharCode(c);
}
}
c = uint8Array[i];
// space
if (isSpaceCharacter(c)) {
for (i++; i < l; i++) {
c = uint8Array[i];
// space
if (isSpaceCharacter(c)) {
continue;
} else {
break;
}
}
}
// " or '
if (c === 0x22 || c === 0x27) {
const quote = c;
for (i++; i < l; i++) {
c = uint8Array[i];
if (c === quote) {
i++;
return { attr: { name, value }, i };
}
// A-Z
if (c >= 0x41 && c <= 0x5A) {
value += String.fromCharCode(c + 0x20); // lowercase
} else {
value += String.fromCharCode(c);
}
}
}
// >
if (c === 0x3E) {
return { attr: { name, value }, i };
}
// A-Z
if (c >= 0x41 && c <= 0x5A) {
value += String.fromCharCode(c + 0x20); // lowercase
} else {
value += String.fromCharCode(c);
}
for (i++; i < l; i++) {
c = uint8Array[i];
// space or >
if (isSpaceCharacter(c) || c === 0x3E) {
return { attr: { name, value }, i };
}
// A-Z
if (c >= 0x41 && c <= 0x5A) {
value += String.fromCharCode(c + 0x20); // lowercase
} else {
value += String.fromCharCode(c);
}
}
}
return { i };
}
function extractCharacterEncodingFromMeta(string) {
let position = 0;
while (true) {
const indexOfCharset = string.substring(position).search(/charset/ui);
if (indexOfCharset === -1) {
return null;
}
let subPosition = position + indexOfCharset + "charset".length;
while (isSpaceCharacter(string[subPosition].charCodeAt(0))) {
++subPosition;
}
if (string[subPosition] !== "=") {
position = subPosition - 1;
continue;
}
++subPosition;
while (isSpaceCharacter(string[subPosition].charCodeAt(0))) {
++subPosition;
}
position = subPosition;
break;
}
if (string[position] === "\"" || string[position] === "'") {
const nextIndex = string.indexOf(string[position], position + 1);
if (nextIndex !== -1) {
return whatwgEncoding.labelToName(string.substring(position + 1, nextIndex));
}
// It is an unmatched quotation mark
return null;
}
if (string.length === position + 1) {
return null;
}
const indexOfASCIIWhitespaceOrSemicolon = string.substring(position + 1).search(/\x09|\x0A|\x0C|\x0D|\x20|;/u);
const end = indexOfASCIIWhitespaceOrSemicolon === -1 ?
string.length :
position + indexOfASCIIWhitespaceOrSemicolon + 1;
return whatwgEncoding.labelToName(string.substring(position, end));
}
function isSpaceCharacter(c) {
return c === 0x09 || c === 0x0A || c === 0x0C || c === 0x0D || c === 0x20;
}
|