Path: blob/master/node_modules/@adiwajshing/baileys/lib/WABinary/index.js
2593 views
"use strict";1var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {2if (k2 === undefined) k2 = k;3Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });4}) : (function(o, m, k, k2) {5if (k2 === undefined) k2 = k;6o[k2] = m[k];7}));8var __exportStar = (this && this.__exportStar) || function(m, exports) {9for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);10};11Object.defineProperty(exports, "__esModule", { value: true });12exports.Binary = exports.getBinaryNodeMessages = exports.reduceBinaryNodeToDictionary = exports.assertNodeErrorFree = exports.getBinaryNodeChildUInt = exports.getBinaryNodeChildBuffer = exports.getBinaryNodeChild = exports.getAllBinaryNodeChildren = exports.getBinaryNodeChildren = exports.encodeBinaryNode = exports.decodeBinaryNode = void 0;13const Constants_1 = require("../../WABinary/Constants");14const jid_utils_1 = require("./jid-utils");15const Binary_1 = require("../../WABinary/Binary");16const boom_1 = require("@hapi/boom");17const WAProto_1 = require("../../WAProto");18const LIST1 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '.', '�', '�', '�', '�'];19const LIST2 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];20function k(data, uint) {21let arr = [];22for (let a = 0; a < uint; a++) {23arr.push(exports.decodeBinaryNode(data));24}25return arr;26}27function x(data, t, r, a) {28const arr = new Array(2 * a - r);29for (let n = 0; n < arr.length - 1; n += 2) {30var s = data.readUint8();31(arr[n] = t[s >>> 4]), (arr[n + 1] = t[15 & s]);32}33if (r) {34arr[arr.length - 1] = t[data.readUint8() >>> 4];35}36return arr.join('');37}38function D(e, t, r) {39var a = e.length % 2 == 1;40r.writeUint8(t);41var i = Math.ceil(e.length / 2);42a && (i |= 128), r.writeUint8(i);43for (var n = 0, s = 0; s < e.length; s++) {44var o = e.charCodeAt(s), l = null;45if ((48 <= o && o <= 57 ? (l = o - 48) : 255 === t ? (45 === o ? (l = 10) : 46 === o && (l = 11)) : 251 === t && 65 <= o && o <= 70 && (l = o - 55), null == l))46throw new Error(`Cannot nibble encode ${o}`);47s % 2 == 0 ? ((n = l << 4), s === e.length - 1 && ((n |= 15), r.writeUint8(n))) : ((n |= l), r.writeUint8(n));48}49}50function N(e, t) {51if (e < 256)52t.writeUint8(252), t.writeUint8(e);53else if (e < 1048576)54t.writeUint8(253), t.writeUint8((e >>> 16) & 255), t.writeUint8((e >>> 8) & 255), t.writeUint8(255 & e);55else {56if (!(e < 4294967296))57throw new Error(`Binary with length ${e} is too big for WAP protocol`);58t.writeUint8(254), t.writeUint32(e);59}60}61function R(e, t) {62var w = null;63if ('' === e)64return t.writeUint8(252), void t.writeUint8(0);65var b = Constants_1.SINGLE_BYTE_TOKEN_MAP;66var r = b.get(e);67var c = [236, 237, 238, 239];68if (null == r) {69if (null == w) {70w = [];71for (var a = 0; a < Constants_1.DICTIONARIES_MAP.length; ++a)72w.push(Constants_1.DICTIONARIES_MAP[a]);73}74for (var n = 0; n < w.length; ++n) {75var s = w[n].get(e);76if (null != s)77return t.writeUint8(c[n]), void t.writeUint8(s);78}79var o = Binary_1.numUtf8Bytes(e);80if (o < 128) {81if (!/[^0-9.-]+?/.exec(e))82return void D(e, 255, t);83if (!/[^0-9A-F]+?/.exec(e))84return void D(e, 251, t);85}86N(o, t), t.writeString(e);87}88else89t.writeUint8(r + 1);90}91function M(e, t) {92var p = 248;93var f = 249;94if (void 0 === e.tag)95return t.writeUint8(p), void t.writeUint8(0);96var r = 1;97e.attrs && (r += 2 * Object.keys(e.attrs).length),98e.content && r++,99r < 256 ? (t.writeUint8(p), t.writeUint8(r)) : r < 65536 && (t.writeUint8(f), t.writeUint16(r)),100O(e.tag, t),101e.attrs &&102Object.keys(e.attrs).forEach((r) => {103R(r, t), O(e.attrs[r], t);104});105var a = e.content;106if (Array.isArray(a)) {107a.length < 256 ? (t.writeUint8(p), t.writeUint8(a.length)) : a.length < 65536 && (t.writeUint8(f), t.writeUint16(a.length));108for (var i = 0; i < a.length; i++)109M(a[i], t);110}111else112a && O(a, t);113}114function L(data, t) {115const n = data.readUint8();116if (n === 0) {117return null;118}119if (n === 248) {120return k(data, data.readUint8());121}122if (n === 249) {123return k(data, data.readUint16());124}125if (n === 252) {126return t ? data.readString(data.readUint8()) : data.readByteArray(data.readUint8());127}128if (n === 253) {129const size = ((15 & data.readUint8()) << 16) + (data.readUint8() << 8) + data.readUint8();130return t ? data.readString(size) : data.readByteArray(size);131}132if (n === 254) {133return t ? data.readString(data.readUint32()) : data.readByteArray(data.readUint32());134}135if (n === 250) {136const user = L(data, true);137if (null != user && 'string' != typeof user)138throw new Error(`Decode string got invalid value ${String(t)}, string expected`);139const server = decodeStanzaString(data);140return jid_utils_1.jidEncode(user, server);141}142if (n === 247) {143const agent = data.readUint8();144const device = data.readUint8();145const user = decodeStanzaString(data);146return jid_utils_1.jidEncode(user, 's.whatsapp.net', device, agent);147}148if (n === 255) {149const number = data.readUint8();150return x(data, LIST1, number >>> 7, 127 & number);151}152if (n === 251) {153const number = data.readUint8();154return x(data, LIST2, number >>> 7, 127 & number);155}156if (n <= 0 || n >= 240) {157throw new Error('Unable to decode WAP buffer');158}159if (n >= 236 && n <= 239) {160const dict = Constants_1.DICTIONARIES[n - 236];161if (!dict) {162throw new Error(`Missing WAP dictionary ${n - 236}`);163}164const index = data.readUint8();165const value = dict[index];166if (!value) {167throw new Error(`Invalid value index ${index} in dict ${n - 236}`);168}169return value;170}171const singleToken = Constants_1.SINGLE_BYTE_TOKEN[n - 1];172if (!singleToken)173throw new Error(`Undefined token with index ${n}`);174return singleToken;175}176function O(e, t) {177if (null == e)178t.writeUint8(0);179else if (typeof e === 'object' && !(e instanceof Uint8Array) && !Buffer.isBuffer(e) && !Array.isArray(e))180M(e, t);181else if ('string' == typeof e) {182const jid = jid_utils_1.jidDecode(e);183if (jid) {184if (typeof jid.agent !== 'undefined' || typeof jid.device !== 'undefined') {185var { user: a, agent: i, device: n } = jid;186t.writeUint8(247), t.writeUint8(i || 0), t.writeUint8(n || 0), O(a, t);187}188else {189var { user: s, server: l } = jid;190t.writeUint8(250), null != s ? O(s, t) : t.writeUint8(0), O(l, t);191}192}193else {194R(e, t);195}196}197else {198if (!(e instanceof Uint8Array))199throw new Error('Invalid payload type ' + typeof e);200N(e.length, t), t.writeByteArray(e);201}202}203function decodeStanzaString(data) {204// G205const t = L(data, true);206if (typeof t != 'string') {207throw new Error(`Decode string got invalid value ${String(t)}, string expected`);208}209return t;210}211function bufferToUInt(e, t) {212let a = 0;213for (let i = 0; i < t; i++)214a = 256 * a + e[i];215return a;216}217const decodeBinaryNode = (data) => {218//U219let r = data.readUint8();220let t = r === 248 ? data.readUint8() : data.readUint16();221if (!t) {222throw new Error('Failed to decode node, list cannot be empty');223}224const a = {};225const n = decodeStanzaString(data);226for (t -= 1; t > 1;) {227const s = decodeStanzaString(data);228const l = L(data, true);229a[s] = l;230t -= 2;231}232let i = null;2331 === t && jid_utils_1.jidDecode(i = L(data, !1)) && (i = String(i));234return {235tag: n,236attrs: a,237content: i238};239};240exports.decodeBinaryNode = decodeBinaryNode;241const encodeBinaryNode = (node) => {242const data = new Binary_1.Binary();243O(node, data);244const dataArr = data.readByteArray();245const result = new Uint8Array(1 + dataArr.length);246result[0] = 0;247result.set(dataArr, 1);248return result;249};250exports.encodeBinaryNode = encodeBinaryNode;251// some extra useful utilities252const getBinaryNodeChildren = ({ content }, childTag) => {253if (Array.isArray(content)) {254return content.filter(item => item.tag == childTag);255}256return [];257};258exports.getBinaryNodeChildren = getBinaryNodeChildren;259const getAllBinaryNodeChildren = ({ content }) => {260if (Array.isArray(content)) {261return content;262}263return [];264};265exports.getAllBinaryNodeChildren = getAllBinaryNodeChildren;266const getBinaryNodeChild = ({ content }, childTag) => {267if (Array.isArray(content)) {268return content.find(item => item.tag == childTag);269}270};271exports.getBinaryNodeChild = getBinaryNodeChild;272const getBinaryNodeChildBuffer = (node, childTag) => {273var _a;274const child = (_a = exports.getBinaryNodeChild(node, childTag)) === null || _a === void 0 ? void 0 : _a.content;275if (Buffer.isBuffer(child) || child instanceof Uint8Array) {276return child;277}278};279exports.getBinaryNodeChildBuffer = getBinaryNodeChildBuffer;280const getBinaryNodeChildUInt = (node, childTag, length) => {281const buff = exports.getBinaryNodeChildBuffer(node, childTag);282if (buff)283return bufferToUInt(buff, length);284};285exports.getBinaryNodeChildUInt = getBinaryNodeChildUInt;286const assertNodeErrorFree = (node) => {287const errNode = exports.getBinaryNodeChild(node, 'error');288if (errNode) {289throw new boom_1.Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });290}291};292exports.assertNodeErrorFree = assertNodeErrorFree;293const reduceBinaryNodeToDictionary = (node, tag) => {294const nodes = exports.getBinaryNodeChildren(node, tag);295const dict = nodes.reduce((dict, { attrs }) => {296dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value;297return dict;298}, {});299return dict;300};301exports.reduceBinaryNodeToDictionary = reduceBinaryNodeToDictionary;302const getBinaryNodeMessages = ({ content }) => {303const msgs = [];304if (Array.isArray(content)) {305for (const item of content) {306if (item.tag === 'message') {307msgs.push(WAProto_1.proto.WebMessageInfo.decode(item.content));308}309}310}311return msgs;312};313exports.getBinaryNodeMessages = getBinaryNodeMessages;314__exportStar(require("./generic-utils"), exports);315__exportStar(require("./jid-utils"), exports);316var Binary_2 = require("../../WABinary/Binary");317Object.defineProperty(exports, "Binary", { enumerable: true, get: function () { return Binary_2.Binary; } });318__exportStar(require("./types"), exports);319__exportStar(require("./Legacy"), exports);320321322