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