Path: blob/master/node_modules/@adiwajshing/baileys/lib/WABinary/Legacy/index.js
2594 views
"use strict";1Object.defineProperty(exports, "__esModule", { value: true });2exports.decodeBinaryNodeLegacy = exports.encodeBinaryNodeLegacy = exports.isLegacyBinaryNode = void 0;3const constants_1 = require("./constants");4const isLegacyBinaryNode = (buffer) => {5switch (buffer[0]) {6case constants_1.Tags.LIST_EMPTY:7case constants_1.Tags.LIST_8:8case constants_1.Tags.LIST_16:9return true;10default:11return false;12}13};14exports.isLegacyBinaryNode = isLegacyBinaryNode;15function decode(buffer, indexRef) {16const checkEOS = (length) => {17if (indexRef.index + length > buffer.length) {18throw new Error('end of stream');19}20};21const next = () => {22const value = buffer[indexRef.index];23indexRef.index += 1;24return value;25};26const readByte = () => {27checkEOS(1);28return next();29};30const readStringFromChars = (length) => {31checkEOS(length);32const value = buffer.slice(indexRef.index, indexRef.index + length);33indexRef.index += length;34return value.toString('utf-8');35};36const readBytes = (n) => {37checkEOS(n);38const value = buffer.slice(indexRef.index, indexRef.index + n);39indexRef.index += n;40return value;41};42const readInt = (n, littleEndian = false) => {43checkEOS(n);44let val = 0;45for (let i = 0; i < n; i++) {46const shift = littleEndian ? i : n - 1 - i;47val |= next() << (shift * 8);48}49return val;50};51const readInt20 = () => {52checkEOS(3);53return ((next() & 15) << 16) + (next() << 8) + next();54};55const unpackHex = (value) => {56if (value >= 0 && value < 16) {57return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10;58}59throw new Error('invalid hex: ' + value);60};61const unpackNibble = (value) => {62if (value >= 0 && value <= 9) {63return '0'.charCodeAt(0) + value;64}65switch (value) {66case 10:67return '-'.charCodeAt(0);68case 11:69return '.'.charCodeAt(0);70case 15:71return '\0'.charCodeAt(0);72default:73throw new Error('invalid nibble: ' + value);74}75};76const unpackByte = (tag, value) => {77if (tag === constants_1.Tags.NIBBLE_8) {78return unpackNibble(value);79}80else if (tag === constants_1.Tags.HEX_8) {81return unpackHex(value);82}83else {84throw new Error('unknown tag: ' + tag);85}86};87const readPacked8 = (tag) => {88const startByte = readByte();89let value = '';90for (let i = 0; i < (startByte & 127); i++) {91const curByte = readByte();92value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4));93value += String.fromCharCode(unpackByte(tag, curByte & 0x0f));94}95if (startByte >> 7 !== 0) {96value = value.slice(0, -1);97}98return value;99};100const isListTag = (tag) => {101return tag === constants_1.Tags.LIST_EMPTY || tag === constants_1.Tags.LIST_8 || tag === constants_1.Tags.LIST_16;102};103const readListSize = (tag) => {104switch (tag) {105case constants_1.Tags.LIST_EMPTY:106return 0;107case constants_1.Tags.LIST_8:108return readByte();109case constants_1.Tags.LIST_16:110return readInt(2);111default:112throw new Error('invalid tag for list size: ' + tag);113}114};115const getToken = (index) => {116if (index < 3 || index >= constants_1.SingleByteTokens.length) {117throw new Error('invalid token index: ' + index);118}119return constants_1.SingleByteTokens[index];120};121const readString = (tag) => {122if (tag >= 3 && tag <= 235) {123const token = getToken(tag);124return token; // === 's.whatsapp.net' ? 'c.us' : token125}126switch (tag) {127case constants_1.Tags.DICTIONARY_0:128case constants_1.Tags.DICTIONARY_1:129case constants_1.Tags.DICTIONARY_2:130case constants_1.Tags.DICTIONARY_3:131return getTokenDouble(tag - constants_1.Tags.DICTIONARY_0, readByte());132case constants_1.Tags.LIST_EMPTY:133return null;134case constants_1.Tags.BINARY_8:135return readStringFromChars(readByte());136case constants_1.Tags.BINARY_20:137return readStringFromChars(readInt20());138case constants_1.Tags.BINARY_32:139return readStringFromChars(readInt(4));140case constants_1.Tags.JID_PAIR:141const i = readString(readByte());142const j = readString(readByte());143if (typeof i === 'string' && j) {144return i + '@' + j;145}146throw new Error('invalid jid pair: ' + i + ', ' + j);147case constants_1.Tags.HEX_8:148case constants_1.Tags.NIBBLE_8:149return readPacked8(tag);150default:151throw new Error('invalid string with tag: ' + tag);152}153};154const readList = (tag) => ([...new Array(readListSize(tag))].map(() => decode(buffer, indexRef)));155const getTokenDouble = (index1, index2) => {156const n = 256 * index1 + index2;157if (n < 0 || n > constants_1.DoubleByteTokens.length) {158throw new Error('invalid double token index: ' + n);159}160return constants_1.DoubleByteTokens[n];161};162const listSize = readListSize(readByte());163const descrTag = readByte();164if (descrTag === constants_1.Tags.STREAM_END) {165throw new Error('unexpected stream end');166}167const header = readString(descrTag);168const attrs = {};169let data;170if (listSize === 0 || !header) {171throw new Error('invalid node');172}173// read the attributes in174const attributesLength = (listSize - 1) >> 1;175for (let i = 0; i < attributesLength; i++) {176const key = readString(readByte());177const b = readByte();178attrs[key] = readString(b);179}180if (listSize % 2 === 0) {181const tag = readByte();182if (isListTag(tag)) {183data = readList(tag);184}185else {186let decoded;187switch (tag) {188case constants_1.Tags.BINARY_8:189decoded = readBytes(readByte());190break;191case constants_1.Tags.BINARY_20:192decoded = readBytes(readInt20());193break;194case constants_1.Tags.BINARY_32:195decoded = readBytes(readInt(4));196break;197default:198decoded = readString(tag);199break;200}201data = decoded;202}203}204return {205tag: header,206attrs,207content: data208};209}210const encode = ({ tag, attrs, content }, buffer = []) => {211const pushByte = (value) => buffer.push(value & 0xff);212const pushInt = (value, n, littleEndian = false) => {213for (let i = 0; i < n; i++) {214const curShift = littleEndian ? i : n - 1 - i;215buffer.push((value >> (curShift * 8)) & 0xff);216}217};218const pushBytes = (bytes) => (bytes.forEach(b => buffer.push(b)));219const pushInt20 = (value) => (pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]));220const writeByteLength = (length) => {221if (length >= 4294967296) {222throw new Error('string too large to encode: ' + length);223}224if (length >= 1 << 20) {225pushByte(constants_1.Tags.BINARY_32);226pushInt(length, 4); // 32 bit integer227}228else if (length >= 256) {229pushByte(constants_1.Tags.BINARY_20);230pushInt20(length);231}232else {233pushByte(constants_1.Tags.BINARY_8);234pushByte(length);235}236};237const writeStringRaw = (str) => {238const bytes = Buffer.from(str, 'utf-8');239writeByteLength(bytes.length);240pushBytes(bytes);241};242const writeToken = (token) => {243if (token < 245) {244pushByte(token);245}246else if (token <= 500) {247throw new Error('invalid token');248}249};250const writeString = (token, i) => {251if (token === 'c.us') {252token = 's.whatsapp.net';253}254const tokenIndex = constants_1.SingleByteTokens.indexOf(token);255if (!i && token === 's.whatsapp.net') {256writeToken(tokenIndex);257}258else if (tokenIndex >= 0) {259if (tokenIndex < constants_1.Tags.SINGLE_BYTE_MAX) {260writeToken(tokenIndex);261}262else {263const overflow = tokenIndex - constants_1.Tags.SINGLE_BYTE_MAX;264const dictionaryIndex = overflow >> 8;265if (dictionaryIndex < 0 || dictionaryIndex > 3) {266throw new Error('double byte dict token out of range: ' + token + ', ' + tokenIndex);267}268writeToken(constants_1.Tags.DICTIONARY_0 + dictionaryIndex);269writeToken(overflow % 256);270}271}272else if (token) {273const jidSepIndex = token.indexOf('@');274if (jidSepIndex <= 0) {275writeStringRaw(token);276}277else {278writeJid(token.slice(0, jidSepIndex), token.slice(jidSepIndex + 1, token.length));279}280}281};282const writeJid = (left, right) => {283pushByte(constants_1.Tags.JID_PAIR);284left && left.length > 0 ? writeString(left) : writeToken(constants_1.Tags.LIST_EMPTY);285writeString(right);286};287const writeListStart = (listSize) => {288if (listSize === 0) {289pushByte(constants_1.Tags.LIST_EMPTY);290}291else if (listSize < 256) {292pushBytes([constants_1.Tags.LIST_8, listSize]);293}294else {295pushBytes([constants_1.Tags.LIST_16, listSize]);296}297};298const validAttributes = Object.keys(attrs).filter(k => (typeof attrs[k] !== 'undefined' && attrs[k] !== null));299writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' && content !== null ? 1 : 0));300writeString(tag);301validAttributes.forEach((key) => {302if (typeof attrs[key] === 'string') {303writeString(key);304writeString(attrs[key]);305}306});307if (typeof content === 'string') {308writeString(content, true);309}310else if (Buffer.isBuffer(content)) {311writeByteLength(content.length);312pushBytes(content);313}314else if (Array.isArray(content)) {315writeListStart(content.length);316for (const item of content) {317if (item) {318encode(item, buffer);319}320}321}322else if (typeof content === 'undefined' || content === null) {323}324else {325throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);326}327return Buffer.from(buffer);328};329exports.encodeBinaryNodeLegacy = encode;330exports.decodeBinaryNodeLegacy = decode;331332333