import { ERR_BUFFER_OUT_OF_BOUNDS, ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE,} from "../_errors.ts";import { encodings } from "../internal_binding/string_decoder.ts";import { indexOfBuffer } from "../internal_binding/buffer.ts";import { asciiToBytes, base64ToBytes, base64UrlToBytes, hexToBytes, utf16leToBytes,} from "../internal_binding/_utils.ts";
const utf8Encoder = new TextEncoder();
const float32Array = new Float32Array(1);const uInt8Float32Array = new Uint8Array(float32Array.buffer);const float64Array = new Float64Array(1);const uInt8Float64Array = new Uint8Array(float64Array.buffer);
float32Array[0] = -1; export const bigEndian = uInt8Float32Array[3] === 0;
export function readUInt48LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 5]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 6); }
return first + buf[++offset] * 2 ** 8 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 24 + (buf[++offset] + last * 2 ** 8) * 2 ** 32;}
export function readUInt40LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 4]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 5); }
return first + buf[++offset] * 2 ** 8 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 24 + last * 2 ** 32;}
export function readUInt24LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 2]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 3); }
return first + buf[++offset] * 2 ** 8 + last * 2 ** 16;}
export function readUInt48BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 5]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 6); }
return (first * 2 ** 8 + buf[++offset]) * 2 ** 32 + buf[++offset] * 2 ** 24 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 8 + last;}
export function readUInt40BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 4]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 5); }
return first * 2 ** 32 + buf[++offset] * 2 ** 24 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 8 + last;}
export function readUInt24BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 2]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 3); }
return first * 2 ** 16 + buf[++offset] * 2 ** 8 + last;}
export function readUInt16BE(offset = 0) { validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 1]; if (first === undefined || last === undefined) { boundsError(offset, this.length - 2); }
return first * 2 ** 8 + last;}
export function readUInt32BE(offset = 0) { validateNumber(offset, "offset"); const first = this[offset]; const last = this[offset + 3]; if (first === undefined || last === undefined) { boundsError(offset, this.length - 4); }
return first * 2 ** 24 + this[++offset] * 2 ** 16 + this[++offset] * 2 ** 8 + last;}
export function readDoubleBackwards(buffer, offset = 0) { validateNumber(offset, "offset"); const first = buffer[offset]; const last = buffer[offset + 7]; if (first === undefined || last === undefined) { boundsError(offset, buffer.length - 8); }
uInt8Float64Array[7] = first; uInt8Float64Array[6] = buffer[++offset]; uInt8Float64Array[5] = buffer[++offset]; uInt8Float64Array[4] = buffer[++offset]; uInt8Float64Array[3] = buffer[++offset]; uInt8Float64Array[2] = buffer[++offset]; uInt8Float64Array[1] = buffer[++offset]; uInt8Float64Array[0] = last; return float64Array[0];}
export function readDoubleForwards(buffer, offset = 0) { validateNumber(offset, "offset"); const first = buffer[offset]; const last = buffer[offset + 7]; if (first === undefined || last === undefined) { boundsError(offset, buffer.length - 8); }
uInt8Float64Array[0] = first; uInt8Float64Array[1] = buffer[++offset]; uInt8Float64Array[2] = buffer[++offset]; uInt8Float64Array[3] = buffer[++offset]; uInt8Float64Array[4] = buffer[++offset]; uInt8Float64Array[5] = buffer[++offset]; uInt8Float64Array[6] = buffer[++offset]; uInt8Float64Array[7] = last; return float64Array[0];}
export function writeDoubleForwards(buffer, val, offset = 0) { val = +val; checkBounds(buffer, offset, 7);
float64Array[0] = val; buffer[offset++] = uInt8Float64Array[0]; buffer[offset++] = uInt8Float64Array[1]; buffer[offset++] = uInt8Float64Array[2]; buffer[offset++] = uInt8Float64Array[3]; buffer[offset++] = uInt8Float64Array[4]; buffer[offset++] = uInt8Float64Array[5]; buffer[offset++] = uInt8Float64Array[6]; buffer[offset++] = uInt8Float64Array[7]; return offset;}
export function writeDoubleBackwards(buffer, val, offset = 0) { val = +val; checkBounds(buffer, offset, 7);
float64Array[0] = val; buffer[offset++] = uInt8Float64Array[7]; buffer[offset++] = uInt8Float64Array[6]; buffer[offset++] = uInt8Float64Array[5]; buffer[offset++] = uInt8Float64Array[4]; buffer[offset++] = uInt8Float64Array[3]; buffer[offset++] = uInt8Float64Array[2]; buffer[offset++] = uInt8Float64Array[1]; buffer[offset++] = uInt8Float64Array[0]; return offset;}
export function readFloatBackwards(buffer, offset = 0) { validateNumber(offset, "offset"); const first = buffer[offset]; const last = buffer[offset + 3]; if (first === undefined || last === undefined) { boundsError(offset, buffer.length - 4); }
uInt8Float32Array[3] = first; uInt8Float32Array[2] = buffer[++offset]; uInt8Float32Array[1] = buffer[++offset]; uInt8Float32Array[0] = last; return float32Array[0];}
export function readFloatForwards(buffer, offset = 0) { validateNumber(offset, "offset"); const first = buffer[offset]; const last = buffer[offset + 3]; if (first === undefined || last === undefined) { boundsError(offset, buffer.length - 4); }
uInt8Float32Array[0] = first; uInt8Float32Array[1] = buffer[++offset]; uInt8Float32Array[2] = buffer[++offset]; uInt8Float32Array[3] = last; return float32Array[0];}
export function writeFloatForwards(buffer, val, offset = 0) { val = +val; checkBounds(buffer, offset, 3);
float32Array[0] = val; buffer[offset++] = uInt8Float32Array[0]; buffer[offset++] = uInt8Float32Array[1]; buffer[offset++] = uInt8Float32Array[2]; buffer[offset++] = uInt8Float32Array[3]; return offset;}
export function writeFloatBackwards(buffer, val, offset = 0) { val = +val; checkBounds(buffer, offset, 3);
float32Array[0] = val; buffer[offset++] = uInt8Float32Array[3]; buffer[offset++] = uInt8Float32Array[2]; buffer[offset++] = uInt8Float32Array[1]; buffer[offset++] = uInt8Float32Array[0]; return offset;}
export function readInt24LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 2]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 3); }
const val = first + buf[++offset] * 2 ** 8 + last * 2 ** 16; return val | (val & 2 ** 23) * 0x1fe;}
export function readInt40LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 4]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 5); }
return (last | (last & 2 ** 7) * 0x1fffffe) * 2 ** 32 + first + buf[++offset] * 2 ** 8 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 24;}
export function readInt48LE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 5]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 6); }
const val = buf[offset + 4] + last * 2 ** 8; return (val | (val & 2 ** 15) * 0x1fffe) * 2 ** 32 + first + buf[++offset] * 2 ** 8 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 24;}
export function readInt24BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 2]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 3); }
const val = first * 2 ** 16 + buf[++offset] * 2 ** 8 + last; return val | (val & 2 ** 23) * 0x1fe;}
export function readInt48BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 5]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 6); }
const val = buf[++offset] + first * 2 ** 8; return (val | (val & 2 ** 15) * 0x1fffe) * 2 ** 32 + buf[++offset] * 2 ** 24 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 8 + last;}
export function readInt40BE(buf, offset = 0) { validateNumber(offset, "offset"); const first = buf[offset]; const last = buf[offset + 4]; if (first === undefined || last === undefined) { boundsError(offset, buf.length - 5); }
return (first | (first & 2 ** 7) * 0x1fffffe) * 2 ** 32 + buf[++offset] * 2 ** 24 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 8 + last;}
export function byteLengthUtf8(str) { return utf8Encoder.encode(str).length;}
function base64ByteLength(str, bytes) { if (str.charCodeAt(bytes - 1) === 0x3D) { bytes--; } if (bytes > 1 && str.charCodeAt(bytes - 1) === 0x3D) { bytes--; }
return (bytes * 3) >>> 2;}
export const encodingsMap = Object.create(null);for (let i = 0; i < encodings.length; ++i) { encodingsMap[encodings[i]] = i;}
export const encodingOps = { ascii: { byteLength: (string) => string.length, encoding: "ascii", encodingVal: encodingsMap.ascii, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, asciiToBytes(val), byteOffset, encodingsMap.ascii, dir, ), slice: (buf, start, end) => buf.asciiSlice(start, end), write: (buf, string, offset, len) => buf.asciiWrite(string, offset, len), }, base64: { byteLength: (string) => base64ByteLength(string, string.length), encoding: "base64", encodingVal: encodingsMap.base64, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, base64ToBytes(val), byteOffset, encodingsMap.base64, dir, ), slice: (buf, start, end) => buf.base64Slice(start, end), write: (buf, string, offset, len) => buf.base64Write(string, offset, len), }, base64url: { byteLength: (string) => base64ByteLength(string, string.length), encoding: "base64url", encodingVal: encodingsMap.base64url, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, base64UrlToBytes(val), byteOffset, encodingsMap.base64url, dir, ), slice: (buf, start, end) => buf.base64urlSlice(start, end), write: (buf, string, offset, len) => buf.base64urlWrite(string, offset, len), }, hex: { byteLength: (string) => string.length >>> 1, encoding: "hex", encodingVal: encodingsMap.hex, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, hexToBytes(val), byteOffset, encodingsMap.hex, dir, ), slice: (buf, start, end) => buf.hexSlice(start, end), write: (buf, string, offset, len) => buf.hexWrite(string, offset, len), }, latin1: { byteLength: (string) => string.length, encoding: "latin1", encodingVal: encodingsMap.latin1, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, asciiToBytes(val), byteOffset, encodingsMap.latin1, dir, ), slice: (buf, start, end) => buf.latin1Slice(start, end), write: (buf, string, offset, len) => buf.latin1Write(string, offset, len), }, ucs2: { byteLength: (string) => string.length * 2, encoding: "ucs2", encodingVal: encodingsMap.utf16le, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, utf16leToBytes(val), byteOffset, encodingsMap.utf16le, dir, ), slice: (buf, start, end) => buf.ucs2Slice(start, end), write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len), }, utf8: { byteLength: byteLengthUtf8, encoding: "utf8", encodingVal: encodingsMap.utf8, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, utf8Encoder.encode(val), byteOffset, encodingsMap.utf8, dir, ), slice: (buf, start, end) => buf.utf8Slice(start, end), write: (buf, string, offset, len) => buf.utf8Write(string, offset, len), }, utf16le: { byteLength: (string) => string.length * 2, encoding: "utf16le", encodingVal: encodingsMap.utf16le, indexOf: (buf, val, byteOffset, dir) => indexOfBuffer( buf, utf16leToBytes(val), byteOffset, encodingsMap.utf16le, dir, ), slice: (buf, start, end) => buf.ucs2Slice(start, end), write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len), },};
export function getEncodingOps(encoding) { encoding = String(encoding).toLowerCase(); switch (encoding.length) { case 4: if (encoding === "utf8") return encodingOps.utf8; if (encoding === "ucs2") return encodingOps.ucs2; break; case 5: if (encoding === "utf-8") return encodingOps.utf8; if (encoding === "ascii") return encodingOps.ascii; if (encoding === "ucs-2") return encodingOps.ucs2; break; case 7: if (encoding === "utf16le") { return encodingOps.utf16le; } break; case 8: if (encoding === "utf-16le") { return encodingOps.utf16le; } break; case 6: if (encoding === "latin1" || encoding === "binary") { return encodingOps.latin1; } if (encoding === "base64") return encodingOps.base64; case 3: if (encoding === "hex") { return encodingOps.hex; } break; case 9: if (encoding === "base64url") { return encodingOps.base64url; } break; }}
export function _copyActual( source, target, targetStart, sourceStart, sourceEnd,) { if (sourceEnd - sourceStart > target.length - targetStart) { sourceEnd = sourceStart + target.length - targetStart; }
let nb = sourceEnd - sourceStart; const sourceLen = source.length - sourceStart; if (nb > sourceLen) { nb = sourceLen; }
if (sourceStart !== 0 || sourceEnd < source.length) { source = new Uint8Array(source.buffer, source.byteOffset + sourceStart, nb); }
target.set(source, targetStart);
return nb;}
export function boundsError(value, length, type) { if (Math.floor(value) !== value) { validateNumber(value, type); throw new ERR_OUT_OF_RANGE(type || "offset", "an integer", value); }
if (length < 0) { throw new ERR_BUFFER_OUT_OF_BOUNDS(); }
throw new ERR_OUT_OF_RANGE( type || "offset", `>= ${type ? 1 : 0} and <= ${length}`, value, );}
export function validateNumber(value, name) { if (typeof value !== "number") { throw new ERR_INVALID_ARG_TYPE(name, "number", value); }}
function checkBounds(buf, offset, byteLength) { validateNumber(offset, "offset"); if (buf[offset] === undefined || buf[offset + byteLength] === undefined) { boundsError(offset, buf.length - (byteLength + 1)); }}
function checkInt(value, min, max, buf, offset, byteLength) { if (value > max || value < min) { const n = typeof min === "bigint" ? "n" : ""; let range; if (byteLength > 3) { if (min === 0 || min === 0n) { range = `>= 0${n} and < 2${n} ** ${(byteLength + 1) * 8}${n}`; } else { range = `>= -(2${n} ** ${(byteLength + 1) * 8 - 1}${n}) and ` + `< 2${n} ** ${(byteLength + 1) * 8 - 1}${n}`; } } else { range = `>= ${min}${n} and <= ${max}${n}`; } throw new ERR_OUT_OF_RANGE("value", range, value); } checkBounds(buf, offset, byteLength);}
export function toInteger(n, defaultVal) { n = +n; if ( !Number.isNaN(n) && n >= Number.MIN_SAFE_INTEGER && n <= Number.MAX_SAFE_INTEGER ) { return ((n % 1) === 0 ? n : Math.floor(n)); } return defaultVal;}
export function writeU_Int8(buf, value, offset, min, max) { value = +value; validateNumber(offset, "offset"); if (value > max || value < min) { throw new ERR_OUT_OF_RANGE("value", `>= ${min} and <= ${max}`, value); } if (buf[offset] === undefined) { boundsError(offset, buf.length - 1); }
buf[offset] = value; return offset + 1;}
export function writeU_Int16BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 1);
buf[offset++] = value >>> 8; buf[offset++] = value; return offset;}
export function _writeUInt32LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 3);
buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; return offset;}
export function writeU_Int16LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 1);
buf[offset++] = value; buf[offset++] = value >>> 8; return offset;}
export function _writeUInt32BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 3);
buf[offset + 3] = value; value = value >>> 8; buf[offset + 2] = value; value = value >>> 8; buf[offset + 1] = value; value = value >>> 8; buf[offset] = value; return offset + 4;}
export function writeU_Int48BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 5);
const newVal = Math.floor(value * 2 ** -32); buf[offset++] = newVal >>> 8; buf[offset++] = newVal; buf[offset + 3] = value; value = value >>> 8; buf[offset + 2] = value; value = value >>> 8; buf[offset + 1] = value; value = value >>> 8; buf[offset] = value; return offset + 4;}
export function writeU_Int40BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 4);
buf[offset++] = Math.floor(value * 2 ** -32); buf[offset + 3] = value; value = value >>> 8; buf[offset + 2] = value; value = value >>> 8; buf[offset + 1] = value; value = value >>> 8; buf[offset] = value; return offset + 4;}
export function writeU_Int32BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 3);
buf[offset + 3] = value; value = value >>> 8; buf[offset + 2] = value; value = value >>> 8; buf[offset + 1] = value; value = value >>> 8; buf[offset] = value; return offset + 4;}
export function writeU_Int24BE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 2);
buf[offset + 2] = value; value = value >>> 8; buf[offset + 1] = value; value = value >>> 8; buf[offset] = value; return offset + 3;}
export function validateOffset( value, name, min = 0, max = Number.MAX_SAFE_INTEGER,) { if (typeof value !== "number") { throw new ERR_INVALID_ARG_TYPE(name, "number", value); } if (!Number.isInteger(value)) { throw new ERR_OUT_OF_RANGE(name, "an integer", value); } if (value < min || value > max) { throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value); }}
export function writeU_Int48LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 5);
const newVal = Math.floor(value * 2 ** -32); buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; buf[offset++] = newVal; buf[offset++] = newVal >>> 8; return offset;}
export function writeU_Int40LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 4);
const newVal = value; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; buf[offset++] = Math.floor(newVal * 2 ** -32); return offset;}
export function writeU_Int32LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 3);
buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; return offset;}
export function writeU_Int24LE(buf, value, offset, min, max) { value = +value; checkInt(value, min, max, buf, offset, 2);
buf[offset++] = value; value = value >>> 8; buf[offset++] = value; value = value >>> 8; buf[offset++] = value; return offset;}