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