Path: blob/master/node_modules/@adiwajshing/baileys/lib/WABinary/encode.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.encodeBinaryNode = void 0;26const constants = __importStar(require("./constants"));27const jid_utils_1 = require("./jid-utils");28const encodeBinaryNode = ({ tag, attrs, content }, opts = constants, buffer = [0]) => {29const { TAGS, TOKEN_MAP } = opts;30const pushByte = (value) => buffer.push(value & 0xff);31const pushInt = (value, n, littleEndian = false) => {32for (let i = 0; i < n; i++) {33const curShift = littleEndian ? i : n - 1 - i;34buffer.push((value >> (curShift * 8)) & 0xff);35}36};37const pushBytes = (bytes) => (bytes.forEach(b => buffer.push(b)));38const pushInt16 = (value) => {39pushBytes([(value >> 8) & 0xff, value & 0xff]);40};41const pushInt20 = (value) => (pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]));42const writeByteLength = (length) => {43if (length >= 4294967296) {44throw new Error('string too large to encode: ' + length);45}46if (length >= 1 << 20) {47pushByte(TAGS.BINARY_32);48pushInt(length, 4); // 32 bit integer49}50else if (length >= 256) {51pushByte(TAGS.BINARY_20);52pushInt20(length);53}54else {55pushByte(TAGS.BINARY_8);56pushByte(length);57}58};59const writeStringRaw = (str) => {60const bytes = Buffer.from(str, 'utf-8');61writeByteLength(bytes.length);62pushBytes(bytes);63};64const writeJid = ({ agent, device, user, server }) => {65if (typeof agent !== 'undefined' || typeof device !== 'undefined') {66pushByte(TAGS.AD_JID);67pushByte(agent || 0);68pushByte(device || 0);69writeString(user);70}71else {72pushByte(TAGS.JID_PAIR);73if (user.length) {74writeString(user);75}76else {77pushByte(TAGS.LIST_EMPTY);78}79writeString(server);80}81};82const packNibble = (char) => {83switch (char) {84case '-':85return 10;86case '.':87return 11;88case '\0':89return 15;90default:91if (char >= '0' && char <= '9') {92return char.charCodeAt(0) - '0'.charCodeAt(0);93}94throw new Error(`invalid byte for nibble "${char}"`);95}96};97const packHex = (char) => {98if (char >= '0' && char <= '9') {99return char.charCodeAt(0) - '0'.charCodeAt(0);100}101if (char >= 'A' && char <= 'F') {102return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0);103}104if (char >= 'a' && char <= 'f') {105return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0);106}107if (char === '\0') {108return 15;109}110throw new Error(`Invalid hex char "${char}"`);111};112const writePackedBytes = (str, type) => {113if (str.length > TAGS.PACKED_MAX) {114throw new Error('Too many bytes to pack');115}116pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8);117let roundedLength = Math.ceil(str.length / 2.0);118if (str.length % 2 !== 0) {119roundedLength |= 128;120}121pushByte(roundedLength);122const packFunction = type === 'nibble' ? packNibble : packHex;123const packBytePair = (v1, v2) => {124const result = (packFunction(v1) << 4) | packFunction(v2);125return result;126};127const strLengthHalf = Math.floor(str.length / 2);128for (let i = 0; i < strLengthHalf; i++) {129pushByte(packBytePair(str[2 * i], str[2 * i + 1]));130}131if (str.length % 2 !== 0) {132pushByte(packBytePair(str[str.length - 1], '\x00'));133}134};135const isNibble = (str) => {136if (str.length > TAGS.PACKED_MAX) {137return false;138}139for (let i = 0; i < str.length; i++) {140const char = str[i];141const isInNibbleRange = char >= '0' && char <= '9';142if (!isInNibbleRange && char !== '-' && char !== '.') {143return false;144}145}146return true;147};148const isHex = (str) => {149if (str.length > TAGS.PACKED_MAX) {150return false;151}152for (let i = 0; i < str.length; i++) {153const char = str[i];154const isInNibbleRange = char >= '0' && char <= '9';155if (!isInNibbleRange && !(char >= 'A' && char <= 'F') && !(char >= 'a' && char <= 'f')) {156return false;157}158}159return true;160};161const writeString = (str) => {162const tokenIndex = TOKEN_MAP[str];163if (tokenIndex) {164if (typeof tokenIndex.dict === 'number') {165pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);166}167pushByte(tokenIndex.index);168}169else if (isNibble(str)) {170writePackedBytes(str, 'nibble');171}172else if (isHex(str)) {173writePackedBytes(str, 'hex');174}175else if (str) {176const decodedJid = (0, jid_utils_1.jidDecode)(str);177if (decodedJid) {178writeJid(decodedJid);179}180else {181writeStringRaw(str);182}183}184};185const writeListStart = (listSize) => {186if (listSize === 0) {187pushByte(TAGS.LIST_EMPTY);188}189else if (listSize < 256) {190pushBytes([TAGS.LIST_8, listSize]);191}192else {193pushByte(TAGS.LIST_16);194pushInt16(listSize);195}196};197const validAttributes = Object.keys(attrs).filter(k => (typeof attrs[k] !== 'undefined' && attrs[k] !== null));198writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));199writeString(tag);200for (const key of validAttributes) {201if (typeof attrs[key] === 'string') {202writeString(key);203writeString(attrs[key]);204}205}206if (typeof content === 'string') {207writeString(content);208}209else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {210writeByteLength(content.length);211pushBytes(content);212}213else if (Array.isArray(content)) {214writeListStart(content.length);215for (const item of content) {216(0, exports.encodeBinaryNode)(item, opts, buffer);217}218}219else if (typeof content === 'undefined') {220// do nothing221}222else {223throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);224}225return Buffer.from(buffer);226};227exports.encodeBinaryNode = encodeBinaryNode;228229230