Path: blob/master/node_modules/@adiwajshing/baileys/WABinary/Binary.js
2593 views
const { hexAt, hexLongIsNegative, hexLongToHex, negateHexLong, NUM_HEX_IN_LONG } = require("./HexHelper");1const { inflateSync } = require("zlib")23var l = "",4d = 0;56const i = 65533,7n = new Uint8Array(10),8s = new Uint8Array(0);910function u(e) {11if (e === l) return d;12for (var t = e.length, r = 0, a = 0; a < t; a++) {13var i = e.charCodeAt(a);14if (i < 128) r++;15else if (i < 2048) r += 2;16else if (i < 55296 || (57344 <= i && i <= 65535)) r += 3;17else if (55296 <= i && i < 56320 && a + 1 !== t) {18var n = e.charCodeAt(a + 1);1956320 <= n && n < 57344 ? (a++, (r += 4)) : (r += 3);20} else r += 3;21}22return (l = e), (d = r);23}24function c(e, t, r) {25var a = t >> 21;26if (e) {27var i = Boolean(2097151 & t || r);28return 0 === a || (-1 === a && i);29}30return 0 === a;31}32function p(e, t, r, a, i = undefined) {33return e.readWithViewParser(t, r, a, i);34}35function f(e, t, r, a = undefined, i = undefined) {36return e.readWithBytesParser(t, r, a, i);37}38function h(e, t, r, a) {39return a ? e.getInt8(t) : e.getUint8(t);40}41function _(e, t, r, a) {42return e.getUint16(t, a);43}44function m(e, t, r, a) {45return e.getInt32(t, a);46}47function g(e, t, r, a) {48return e.getUint32(t, a);49}50function v(e, t, r, a, i) {51return a(e.getInt32(i ? t + 4 : t, i), e.getInt32(i ? t : t + 4, i));52}53function y(e, t, r, a) {54return e.getFloat32(t, a);55}56function E(e, t, r, a) {57return e.getFloat64(t, a);58}59function S(e, t, r, a) {60for (var i = Math.min(a, 10), n = 0, s = 128; n < i && 128 & s; )61s = e[t + n++];62if (10 === n && s > 1) throw new Error("ParseError: varint exceeds 64 bits");63return 128 & s ? n + 1 : n;64}65function T(e, t, r, a) {66var i = 0,67n = 0,68s = r;6910 === r && (n = 1 & e[t + --s]);70for (var o = s - 1; o >= 0; o--)71(i = (i << 7) | (n >>> 25)), (n = (n << 7) | (127 & e[t + o]));72return a(i, n);73}74function A(e, t, r) {75var a = t + e.byteOffset,76i = e.buffer;77return 0 === a && r === i.byteLength ? i : i.slice(a, a + r);78}79function b(e, t, r) {80return e.subarray(t, t + r);81}82function C(e, t, r) {83for (var a = t + r, n = [], s = null, o = t; o < a; o++) {84n.length > 5e3 &&85(s || (s = []), s.push(String.fromCharCode.apply(String, n)), (n = []));86var l = 0 | e[o];87if (0 == (128 & l)) n.push(l);88else if (192 == (224 & l)) {89var d = H(e, o + 1, a);90if (d) {91o++;92var u = ((31 & l) << 6) | (63 & d);93u >= 128 ? n.push(u) : n.push(i);94} else n.push(i);95} else if (224 == (240 & l)) {96var c = H(e, o + 1, a),97p = H(e, o + 2, a);98if (c && p) {99o += 2;100var f = ((15 & l) << 12) | ((63 & c) << 6) | (63 & p);101f >= 2048 && !(55296 <= f && f < 57344) ? n.push(f) : n.push(i);102} else c ? (o++, n.push(i)) : n.push(i);103} else if (240 == (248 & l)) {104var h = H(e, o + 1, a),105_ = H(e, o + 2, a),106m = H(e, o + 3, a);107if (h && _ && m) {108o += 3;109var g = ((7 & l) << 18) | ((63 & h) << 12) | ((63 & _) << 6) | (63 & m);110if (g >= 65536 && g <= 1114111) {111var v = g - 65536;112n.push(55296 | (v >> 10), 56320 | (1023 & v));113} else n.push(i);114} else h && _ ? ((o += 2), n.push(i)) : h ? (o++, n.push(i)) : n.push(i);115} else n.push(i);116}117var y = String.fromCharCode.apply(String, n);118return s ? (s.push(y), s.join("")) : y;119}120function P(e, t, r, a, i) {121return e.writeToView(t, r, a, i);122}123function O(e, t, r, a, i = undefined) {124return e.writeToBytes(t, r, a, i);125}126function M(e, t, r, a) {127e[t] = a;128}129function w(e, t, r, a, i) {130e.setUint16(t, a, i);131}132function I(e, t, r, a, i) {133e.setInt16(t, a, i);134}135function R(e, t, r, a, i) {136e.setUint32(t, a, i);137}138function D(e, t, r, a, i) {139e.setInt32(t, a, i);140}141function N(e, t, r, a, i) {142var n = a < 0,143s = n ? -a : a,144o = Math.floor(s / 4294967296),145l = s - 4294967296 * o;146n && ((o = ~o), 0 === l ? o++ : (l = -l)),147e.setUint32(i ? t + 4 : t, o, i),148e.setUint32(i ? t : t + 4, l, i);149}150function L(e, t, r, a, i) {151e.setFloat32(t, a, i);152}153function k(e, t, r, a, i) {154e.setFloat64(t, a, i);155}156function U(e, t, r, a, i) {157for (var n = a, s = i, o = t + r - 1, l = t; l < o; l++)158(e[l] = 128 | (127 & s)), (s = (n << 25) | (s >>> 7)), (n >>>= 7);159e[o] = s;160}161function G(e, t, r, a) {162for (var i = t, n = a.length, s = 0; s < n; s++) {163var o = a.charCodeAt(s);164if (o < 128) e[i++] = o;165else if (o < 2048) (e[i++] = 192 | (o >> 6)), (e[i++] = 128 | (63 & o));166else if (o < 55296 || 57344 <= o)167(e[i++] = 224 | (o >> 12)),168(e[i++] = 128 | ((o >> 6) & 63)),169(e[i++] = 128 | (63 & o));170else if (55296 <= o && o < 56320 && s + 1 !== n) {171var l = a.charCodeAt(s + 1);172if (56320 <= l && l < 57344) {173s++;174var d = 65536 + (((1023 & o) << 10) | (1023 & l));175(e[i++] = 240 | (d >> 18)),176(e[i++] = 128 | ((d >> 12) & 63)),177(e[i++] = 128 | ((d >> 6) & 63)),178(e[i++] = 128 | (63 & d));179} else (e[i++] = 239), (e[i++] = 191), (e[i++] = 189);180} else (e[i++] = 239), (e[i++] = 191), (e[i++] = 189);181}182}183function F(e, t, r, i, n) {184for (185var s = hexLongIsNegative(i),186o = hexLongToHex(i),187l = 0,188d = 0,189u = 0;190u < 16;191u++192)193(l = (l << 4) | (d >>> 28)), (d = (d << 4) | hexAt(o, u));194s && ((l = ~l), 0 === d ? l++ : (d = -d)),195e.setUint32(n ? t + 4 : t, l, n),196e.setUint32(n ? t : t + 4, d, n);197}198function x(e, t, r, a) {199for (var i = 0; i < r; i++) e[t + i] = a[i];200}201function B(e, t) {202var r, a;203for (e ? ((r = 5), (a = e >>> 3)) : ((r = 1), (a = t >>> 7)); a; )204r++, (a >>>= 7);205return r;206}207function Y(e, t, r, a) {208if ("number" != typeof e || e != e || Math.floor(e) !== e || e < t || e >= r) {209console.trace('here')210throw new TypeError(211"string" == typeof e212? `WriteError: string "${e}" is not a valid ${a}`213: `WriteError: ${String(e)} is not a valid ${a}`214);215}216217}218function K(e, t, r) {219var a =2204294967296 * (t >= 0 || e ? t : 4294967296 + t) +221(r >= 0 ? r : 4294967296 + r);222if (!c(e, t, r))223throw new Error(`ReadError: integer exceeded 53 bits (${a})`);224return a;225}226function j(e, t) {227return K(!0, e, t);228}229function W(e, t) {230return K(!1, e, t);231}232function H(e, t, r) {233if (t >= r) return 0;234var a = 0 | e[t];235return 128 == (192 & a) ? a : 0;236}237238module.exports.numUtf8Bytes = u;239module.exports.longFitsInDouble = c;240module.exports.parseInt64OrThrow = j;241module.exports.parseUint64OrThrow = W;242243class Binary {244/** @type {Uint8Array} */245buffer;246readEndIndex;247writeIndex;248bytesTrashed = 0;249earliestIndex = 0;250readIndex = 0;251/** @type {DataView} */252view = null;253littleEndian = false;254hiddenReads = 0;255hiddenWrites = 0;256257constructor(data = new Uint8Array(0), littleEndian = false) {258if (data instanceof ArrayBuffer) {259this.buffer = new Uint8Array(data);260this.readEndIndex = data.byteLength;261this.writeIndex = data.byteLength;262}263264if (data instanceof Uint8Array) {265this.buffer = data;266this.readEndIndex = data.length;267this.writeIndex = data.length;268}269270this.littleEndian = littleEndian;271}272273size() {274return this.readEndIndex - this.readIndex;275}276277peek(e, t = undefined) {278this.hiddenReads++;279280const r = this.readIndex;281const a = this.bytesTrashed;282283try {284return e(this, t);285} finally {286this.hiddenReads--, (this.readIndex = r - (this.bytesTrashed - a));287}288}289290advance(e) {291this.shiftReadOrThrow(e);292}293294readWithViewParser(e, t, r, a) {295return t(this.getView(), this.shiftReadOrThrow(e), e, r, a);296}297298readWithBytesParser(e, t, r, a) {299return t(this.buffer, this.shiftReadOrThrow(e), e, r, a);300}301302readUint8() {303//return this.readWithViewParser(1, h, false)304return p(this, 1, h, !1);305}306readInt8() {307return p(this, 1, h, !0);308}309readUint16(e = this.littleEndian) {310return p(this, 2, _, e);311}312readInt32(e = this.littleEndian) {313return p(this, 4, m, e);314}315readUint32(e = this.littleEndian) {316return p(this, 4, g, e);317}318readInt64(e = this.littleEndian) {319return p(this, 8, v, j, e);320}321readUint64(e = this.littleEndian) {322return p(this, 8, v, W, e);323}324readLong(e, t = this.littleEndian) {325return p(this, 8, v, e, t);326}327readFloat32(e = this.littleEndian) {328return p(this, 4, y, e);329}330readFloat64(e = this.littleEndian) {331return p(this, 8, E, e);332}333readVarInt(e) {334var t = f(this, 0, S, this.size());335return f(this, t, T, e);336}337readBuffer(e = this.size()) {338return 0 === e ? new ArrayBuffer(0) : f(this, e, A);339}340readByteArray(e = this.size()) {341return 0 === e ? new Uint8Array(0) : f(this, e, b);342}343readBinary(e = this.size(), t = this.littleEndian) {344if (0 === e) return new Binary(void 0, t);345var r = f(this, e, b);346return new Binary(r, t);347}348indexOf(e) {349if (0 === e.length) return 0;350for (351var t = this.buffer,352r = this.readEndIndex,353a = this.readIndex,354i = 0,355n = a,356s = a;357s < r;358s++359)360if (t[s] === e[i]) {361if ((0 === i && (n = s), ++i === e.byteLength))362return s - a - e.byteLength + 1;363} else i > 0 && ((i = 0), (s = n));364return -1;3651;366}367readString(e) {368return f(this, e, C);369}370ensureCapacity(e) {371this.maybeReallocate(this.readIndex + e);372}373ensureAdditionalCapacity(e) {374this.maybeReallocate(this.writeIndex + e);375}376writeToView(e, t, r, a) {377var i = this.shiftWriteMaybeReallocate(e);378return t(this.getView(), i, e, r, a);379}380writeToBytes(e, t, r, a) {381var i = this.shiftWriteMaybeReallocate(e);382return t(this.buffer, i, e, r, a);383}384write(...e) {385for (var t = 0; t < e.length; t++) {386var r = e[t];387"string" == typeof r388? this.writeString(r)389: "number" == typeof r390? this.writeUint8(r)391: r instanceof Binary392? this.writeBinary(r)393: r instanceof ArrayBuffer394? this.writeBuffer(r)395: r instanceof Uint8Array && this.writeByteArray(r);396}397}398writeUint8(e) {399Y(e, 0, 256, "uint8"), O(this, 1, M, e, !1);400}401writeInt8(e) {402Y(e, -128, 128, "signed int8"), O(this, 1, M, e, !0);403}404writeUint16(e, t = this.littleEndian) {405Y(e, 0, 65536, "uint16"), P(this, 2, w, e, t);406}407writeInt16(e, t = this.littleEndian) {408Y(e, -32768, 32768, "signed int16"), P(this, 2, I, e, t);409}410writeUint32(e, t = this.littleEndian) {411Y(e, 0, 4294967296, "uint32"), P(this, 4, R, e, t);412}413writeInt32(e, t = this.littleEndian) {414Y(e, -2147483648, 2147483648, "signed int32"), P(this, 4, D, e, t);415}416writeUint64(e, t = this.littleEndian) {417Y(e, 0, 0x10000000000000000, "uint64"), P(this, 8, N, e, t);418}419writeInt64(e, t = this.littleEndian) {420Y(e, -0x8000000000000000, 0x8000000000000000, "signed int64"),421P(this, 8, N, e, t);422}423writeFloat32(e, t = this.littleEndian) {424P(this, 4, L, e, t);425}426writeFloat64(e, t = this.littleEndian) {427P(this, 8, k, e, t);428}429writeVarInt(e) {430Y(e, -0x8000000000000000, 0x8000000000000000, "varint (signed int64)");431var t = e < 0,432r = t ? -e : e,433a = Math.floor(r / 4294967296),434i = r - 4294967296 * a;435t && ((a = ~a), 0 === i ? a++ : (i = -i)), O(this, B(a, i), U, a, i);436}437writeVarIntFromHexLong(e) {438for (439var t = hexLongIsNegative(e),440r = t ? negateHexLong(e) : e,441i = hexLongToHex(r),442n = 0,443s = 0,444o = 0;445o < NUM_HEX_IN_LONG;446o++447)448(n = (n << 4) | (s >>> 28)), (s = (s << 4) | hexAt(i, o));449t && ((n = ~n), 0 === s ? n++ : (s = -s)), O(this, B(n, s), U, n, s);450}451writeBinary(e) {452var t = e.peek((e) => e.readByteArray());453if (t.length) {454var r = this.shiftWriteMaybeReallocate(t.length);455this.buffer.set(t, r);456}457}458writeBuffer(e) {459this.writeByteArray(new Uint8Array(e));460}461writeByteArray(e) {462var t = this.shiftWriteMaybeReallocate(e.length);463this.buffer.set(e, t);464}465writeBufferView(e) {466this.writeByteArray(new Uint8Array(e.buffer, e.byteOffset, e.byteLength));467}468writeString(e) {469O(this, u(e), G, e);470}471writeHexLong(e, t = this.littleEndian) {472P(this, 8, F, e, t);473}474writeBytes(...e) {475for (var t = 0; t < e.length; t++) Y(e[t], 0, 256, "byte");476O(this, e.length, x, e);477}478writeAtomically(e, t) {479this.hiddenWrites++;480var r = this.writeIndex,481a = this.bytesTrashed;482try {483var i = e(this, t);484return (r = this.writeIndex), (a = this.bytesTrashed), i;485} finally {486this.hiddenWrites--, (this.writeIndex = r - (this.bytesTrashed - a));487}488}489490writeWithVarIntLength(e, t) {491var r = this.writeIndex,492a = this.writeAtomically(e, t),493i = this.writeIndex;494this.writeVarInt(i - r);495for (var s = this.writeIndex - i, o = this.buffer, l = 0; l < s; l++)496n[l] = o[i + l];497for (var d = i - 1; d >= r; d--) o[d + s] = o[d];498for (var u = 0; u < s; u++) o[r + u] = n[u];499return a;500}501502static build(...e) {503let t = 0;504let r = 0;505for (t = 0, r = 0; r < e.length; r++) {506let a = e[r];507"string" == typeof a508? (t += u(a))509: "number" == typeof a510? t++511: a instanceof Binary512? (t += a.size())513: a instanceof ArrayBuffer514? (t += a.byteLength)515: a instanceof Uint8Array && (t += a.length);516}517518var i = new Binary();519return i.ensureCapacity(t), i.write.apply(i, arguments), i;520}521522getView() {523return (524this.view ||525(this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset))526);527}528529shiftReadOrThrow(e) {530if (e < 0)531throw new Error("ReadError: given negative number of bytes to read");532var t = this.readIndex,533r = t + e;534if (r > this.readEndIndex)535throw new Error(536t === this.readEndIndex537? "ReadError: tried to read from depleted binary"538: "ReadError: tried to read beyond end of binary"539);540return (541(this.readIndex = r), this.hiddenReads || (this.earliestIndex = r), t542);543}544545maybeReallocate(e) {546const t = this.buffer;547if (e <= t.length) {548return e;549}550551const r = this.earliestIndex;552const a = e - r;553const i = Math.max(a, 2 * (t.length - r), 64);554const n = new Uint8Array(i);555return (556r557? (n.set(t.subarray(r)),558(this.bytesTrashed += r),559(this.readIndex -= r),560(this.readEndIndex -= r),561(this.writeIndex -= r),562(this.earliestIndex = 0))563: n.set(t),564(this.buffer = n),565(this.view = null),566a567);568}569570shiftWriteMaybeReallocate(e) {571const t = this.maybeReallocate(this.writeIndex + e);572const r = this.writeIndex;573return (574(this.writeIndex = t), this.hiddenWrites || (this.readEndIndex = t), r575);576}577decompressed = () => {578if (2 & this.readUint8()) {579const result = inflateSync(this.readByteArray())580return new Binary(result)581}582return this583}584}585586module.exports.Binary = Binary587588