Path: blob/master/node_modules/@adiwajshing/baileys/lib/WABinary/decode.js
1129 views
"use strict";1var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {2if (k2 === undefined) k2 = k;3var desc = Object.getOwnPropertyDescriptor(m, k);4if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {5desc = { enumerable: true, get: function() { return m[k]; } };6}7Object.defineProperty(o, k2, desc);8}) : (function(o, m, k, k2) {9if (k2 === undefined) k2 = k;10o[k2] = m[k];11}));12var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {13Object.defineProperty(o, "default", { enumerable: true, value: v });14}) : function(o, v) {15o["default"] = v;16});17var __importStar = (this && this.__importStar) || function (mod) {18if (mod && mod.__esModule) return mod;19var result = {};20if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);21__setModuleDefault(result, mod);22return result;23};24Object.defineProperty(exports, "__esModule", { value: true });25exports.decodeBinaryNode = exports.decodeDecompressedBinaryNode = exports.decompressingIfRequired = void 0;26const zlib_1 = require("zlib");27const constants = __importStar(require("./constants"));28const jid_utils_1 = require("./jid-utils");29const decompressingIfRequired = (buffer) => {30if (2 & buffer.readUInt8()) {31buffer = (0, zlib_1.inflateSync)(buffer.slice(1));32}33else { // nodes with no compression have a 0x00 prefix, we remove that34buffer = buffer.slice(1);35}36return buffer;37};38exports.decompressingIfRequired = decompressingIfRequired;39const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {40const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts;41const checkEOS = (length) => {42if (indexRef.index + length > buffer.length) {43throw new Error('end of stream');44}45};46const next = () => {47const value = buffer[indexRef.index];48indexRef.index += 1;49return value;50};51const readByte = () => {52checkEOS(1);53return next();54};55const readBytes = (n) => {56checkEOS(n);57const value = buffer.slice(indexRef.index, indexRef.index + n);58indexRef.index += n;59return value;60};61const readStringFromChars = (length) => {62return readBytes(length).toString('utf-8');63};64const readInt = (n, littleEndian = false) => {65checkEOS(n);66let val = 0;67for (let i = 0; i < n; i++) {68const shift = littleEndian ? i : n - 1 - i;69val |= next() << (shift * 8);70}71return val;72};73const readInt20 = () => {74checkEOS(3);75return ((next() & 15) << 16) + (next() << 8) + next();76};77const unpackHex = (value) => {78if (value >= 0 && value < 16) {79return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10;80}81throw new Error('invalid hex: ' + value);82};83const unpackNibble = (value) => {84if (value >= 0 && value <= 9) {85return '0'.charCodeAt(0) + value;86}87switch (value) {88case 10:89return '-'.charCodeAt(0);90case 11:91return '.'.charCodeAt(0);92case 15:93return '\0'.charCodeAt(0);94default:95throw new Error('invalid nibble: ' + value);96}97};98const unpackByte = (tag, value) => {99if (tag === TAGS.NIBBLE_8) {100return unpackNibble(value);101}102else if (tag === TAGS.HEX_8) {103return unpackHex(value);104}105else {106throw new Error('unknown tag: ' + tag);107}108};109const readPacked8 = (tag) => {110const startByte = readByte();111let value = '';112for (let i = 0; i < (startByte & 127); i++) {113const curByte = readByte();114value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4));115value += String.fromCharCode(unpackByte(tag, curByte & 0x0f));116}117if (startByte >> 7 !== 0) {118value = value.slice(0, -1);119}120return value;121};122const isListTag = (tag) => {123return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16;124};125const readListSize = (tag) => {126switch (tag) {127case TAGS.LIST_EMPTY:128return 0;129case TAGS.LIST_8:130return readByte();131case TAGS.LIST_16:132return readInt(2);133default:134throw new Error('invalid tag for list size: ' + tag);135}136};137const readJidPair = () => {138const i = readString(readByte());139const j = readString(readByte());140if (j) {141return (i || '') + '@' + j;142}143throw new Error('invalid jid pair: ' + i + ', ' + j);144};145const readAdJid = () => {146const agent = readByte();147const device = readByte();148const user = readString(readByte());149return (0, jid_utils_1.jidEncode)(user, 's.whatsapp.net', device, agent);150};151const readString = (tag) => {152if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {153return SINGLE_BYTE_TOKENS[tag] || '';154}155switch (tag) {156case TAGS.DICTIONARY_0:157case TAGS.DICTIONARY_1:158case TAGS.DICTIONARY_2:159case TAGS.DICTIONARY_3:160return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte());161case TAGS.LIST_EMPTY:162return '';163case TAGS.BINARY_8:164return readStringFromChars(readByte());165case TAGS.BINARY_20:166return readStringFromChars(readInt20());167case TAGS.BINARY_32:168return readStringFromChars(readInt(4));169case TAGS.JID_PAIR:170return readJidPair();171case TAGS.AD_JID:172return readAdJid();173case TAGS.HEX_8:174case TAGS.NIBBLE_8:175return readPacked8(tag);176default:177throw new Error('invalid string with tag: ' + tag);178}179};180const readList = (tag) => {181const items = [];182const size = readListSize(tag);183for (let i = 0; i < size; i++) {184items.push((0, exports.decodeDecompressedBinaryNode)(buffer, opts, indexRef));185}186return items;187};188const getTokenDouble = (index1, index2) => {189const dict = DOUBLE_BYTE_TOKENS[index1];190if (!dict) {191throw new Error(`Invalid double token dict (${index1})`);192}193const value = dict[index2];194if (typeof value === 'undefined') {195throw new Error(`Invalid double token (${index2})`);196}197return value;198};199const listSize = readListSize(readByte());200const header = readString(readByte());201if (!listSize || !header.length) {202throw new Error('invalid node');203}204const attrs = {};205let data;206if (listSize === 0 || !header) {207throw new Error('invalid node');208}209// read the attributes in210const attributesLength = (listSize - 1) >> 1;211for (let i = 0; i < attributesLength; i++) {212const key = readString(readByte());213const value = readString(readByte());214attrs[key] = value;215}216if (listSize % 2 === 0) {217const tag = readByte();218if (isListTag(tag)) {219data = readList(tag);220}221else {222let decoded;223switch (tag) {224case TAGS.BINARY_8:225decoded = readBytes(readByte());226break;227case TAGS.BINARY_20:228decoded = readBytes(readInt20());229break;230case TAGS.BINARY_32:231decoded = readBytes(readInt(4));232break;233default:234decoded = readString(tag);235break;236}237data = decoded;238}239}240return {241tag: header,242attrs,243content: data244};245};246exports.decodeDecompressedBinaryNode = decodeDecompressedBinaryNode;247const decodeBinaryNode = (buff) => {248const decompBuff = (0, exports.decompressingIfRequired)(buff);249return (0, exports.decodeDecompressedBinaryNode)(decompBuff, constants);250};251exports.decodeBinaryNode = decodeBinaryNode;252253254