Bring suffixInPSL() from upstream (#3851)

This commit is contained in:
Manish Jethani 2021-08-26 18:20:49 +05:30 committed by GitHub
parent 5eac054ac7
commit 498f90966d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 9 deletions

View File

@ -64,12 +64,13 @@ export default (function() {
*/ */
// i32 / i8 // i32 / i8
const HOSTNAME_SLOT = 0; // jshint ignore:line const HOSTNAME_SLOT = 0; // jshint ignore:line
const LABEL_INDICES_SLOT = 256; // -- / 256 const LABEL_INDICES_SLOT = 256; // -- / 256 (256/2 => 128 labels max)
const RULES_PTR_SLOT = 100; // 100 / 400 const RULES_PTR_SLOT = 100; // 100 / 400 (400-256=144 => 144>128)
const CHARDATA_PTR_SLOT = 101; // 101 / 404 const SUFFIX_NOT_FOUND_SLOT = 399; // -- / 399 (safe, see above)
const EMPTY_STRING = ''; const CHARDATA_PTR_SLOT = 101; // 101 / 404
const SELFIE_MAGIC = 2; const EMPTY_STRING = '';
const SELFIE_MAGIC = 2;
let wasmMemory; let wasmMemory;
let pslBuffer32; let pslBuffer32;
@ -110,7 +111,7 @@ const allocateBuffers = function(byteLength) {
pslBuffer8 = new Uint8Array(pslByteLength); pslBuffer8 = new Uint8Array(pslByteLength);
pslBuffer32 = new Uint32Array(pslBuffer8.buffer); pslBuffer32 = new Uint32Array(pslBuffer8.buffer);
} }
hostnameArg = ''; hostnameArg = EMPTY_STRING;
pslBuffer8[LABEL_INDICES_SLOT] = 0; pslBuffer8[LABEL_INDICES_SLOT] = 0;
}; };
@ -328,7 +329,7 @@ const setHostnameArg = function(hostname) {
const buf = pslBuffer8; const buf = pslBuffer8;
if ( hostname === hostnameArg ) { return buf[LABEL_INDICES_SLOT]; } if ( hostname === hostnameArg ) { return buf[LABEL_INDICES_SLOT]; }
if ( hostname === null || hostname.length === 0 ) { if ( hostname === null || hostname.length === 0 ) {
hostnameArg = ''; hostnameArg = EMPTY_STRING;
return (buf[LABEL_INDICES_SLOT] = 0); return (buf[LABEL_INDICES_SLOT] = 0);
} }
hostname = hostname.toLowerCase(); hostname = hostname.toLowerCase();
@ -403,6 +404,7 @@ const getPublicSuffixPosJS = function() {
// 2. If no rules match, the prevailing rule is "*". // 2. If no rules match, the prevailing rule is "*".
if ( iFound === 0 ) { if ( iFound === 0 ) {
if ( buf8[iCandidates + 1 << 2] !== 0x2A /* '*' */ ) { break; } if ( buf8[iCandidates + 1 << 2] !== 0x2A /* '*' */ ) { break; }
buf8[SUFFIX_NOT_FOUND_SLOT] = 1;
iFound = iCandidates; iFound = iCandidates;
} }
iNode = iFound; iNode = iFound;
@ -471,6 +473,24 @@ const getDomain = function(hostname) {
/******************************************************************************/ /******************************************************************************/
const suffixInPSL = function(hostname) {
if ( pslBuffer32 === undefined ) { return false; }
const hostnameLen = setHostnameArg(hostname);
const buf8 = pslBuffer8;
if ( hostnameLen === 0 || buf8[0] === 0x2E /* '.' */ ) {
return false;
}
buf8[SUFFIX_NOT_FOUND_SLOT] = 0;
const cursorPos = getPublicSuffixPos();
return cursorPos !== -1 &&
buf8[cursorPos + 1] === 0 &&
buf8[SUFFIX_NOT_FOUND_SLOT] !== 1;
};
/******************************************************************************/
const toSelfie = function(encoder) { const toSelfie = function(encoder) {
if ( pslBuffer8 === undefined ) { return ''; } if ( pslBuffer8 === undefined ) { return ''; }
if ( encoder instanceof Object ) { if ( encoder instanceof Object ) {
@ -514,7 +534,7 @@ const fromSelfie = function(selfie, decoder) {
} }
// Important! // Important!
hostnameArg = ''; hostnameArg = EMPTY_STRING;
pslBuffer8[LABEL_INDICES_SLOT] = 0; pslBuffer8[LABEL_INDICES_SLOT] = 0;
fireChangedEvent(); fireChangedEvent();
@ -609,6 +629,7 @@ return ({
version: '2.0', version: '2.0',
parse, parse,
getDomain, getDomain,
suffixInPSL,
getPublicSuffix, getPublicSuffix,
toSelfie, fromSelfie, toSelfie, fromSelfie,
disableWASM, enableWASM, disableWASM, enableWASM,