Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/node_modules/@adiwajshing/baileys/lib/WABinary/index.js
2593 views
1
"use strict";
2
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
if (k2 === undefined) k2 = k;
4
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
}) : (function(o, m, k, k2) {
6
if (k2 === undefined) k2 = k;
7
o[k2] = m[k];
8
}));
9
var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
};
12
Object.defineProperty(exports, "__esModule", { value: true });
13
exports.Binary = exports.getBinaryNodeMessages = exports.reduceBinaryNodeToDictionary = exports.assertNodeErrorFree = exports.getBinaryNodeChildUInt = exports.getBinaryNodeChildBuffer = exports.getBinaryNodeChild = exports.getAllBinaryNodeChildren = exports.getBinaryNodeChildren = exports.encodeBinaryNode = exports.decodeBinaryNode = void 0;
14
const Constants_1 = require("../../WABinary/Constants");
15
const jid_utils_1 = require("./jid-utils");
16
const Binary_1 = require("../../WABinary/Binary");
17
const boom_1 = require("@hapi/boom");
18
const WAProto_1 = require("../../WAProto");
19
const LIST1 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '.', '�', '�', '�', '�'];
20
const LIST2 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
21
function k(data, uint) {
22
let arr = [];
23
for (let a = 0; a < uint; a++) {
24
arr.push(exports.decodeBinaryNode(data));
25
}
26
return arr;
27
}
28
function x(data, t, r, a) {
29
const arr = new Array(2 * a - r);
30
for (let n = 0; n < arr.length - 1; n += 2) {
31
var s = data.readUint8();
32
(arr[n] = t[s >>> 4]), (arr[n + 1] = t[15 & s]);
33
}
34
if (r) {
35
arr[arr.length - 1] = t[data.readUint8() >>> 4];
36
}
37
return arr.join('');
38
}
39
function D(e, t, r) {
40
var a = e.length % 2 == 1;
41
r.writeUint8(t);
42
var i = Math.ceil(e.length / 2);
43
a && (i |= 128), r.writeUint8(i);
44
for (var n = 0, s = 0; s < e.length; s++) {
45
var o = e.charCodeAt(s), l = null;
46
if ((48 <= o && o <= 57 ? (l = o - 48) : 255 === t ? (45 === o ? (l = 10) : 46 === o && (l = 11)) : 251 === t && 65 <= o && o <= 70 && (l = o - 55), null == l))
47
throw new Error(`Cannot nibble encode ${o}`);
48
s % 2 == 0 ? ((n = l << 4), s === e.length - 1 && ((n |= 15), r.writeUint8(n))) : ((n |= l), r.writeUint8(n));
49
}
50
}
51
function N(e, t) {
52
if (e < 256)
53
t.writeUint8(252), t.writeUint8(e);
54
else if (e < 1048576)
55
t.writeUint8(253), t.writeUint8((e >>> 16) & 255), t.writeUint8((e >>> 8) & 255), t.writeUint8(255 & e);
56
else {
57
if (!(e < 4294967296))
58
throw new Error(`Binary with length ${e} is too big for WAP protocol`);
59
t.writeUint8(254), t.writeUint32(e);
60
}
61
}
62
function R(e, t) {
63
var w = null;
64
if ('' === e)
65
return t.writeUint8(252), void t.writeUint8(0);
66
var b = Constants_1.SINGLE_BYTE_TOKEN_MAP;
67
var r = b.get(e);
68
var c = [236, 237, 238, 239];
69
if (null == r) {
70
if (null == w) {
71
w = [];
72
for (var a = 0; a < Constants_1.DICTIONARIES_MAP.length; ++a)
73
w.push(Constants_1.DICTIONARIES_MAP[a]);
74
}
75
for (var n = 0; n < w.length; ++n) {
76
var s = w[n].get(e);
77
if (null != s)
78
return t.writeUint8(c[n]), void t.writeUint8(s);
79
}
80
var o = Binary_1.numUtf8Bytes(e);
81
if (o < 128) {
82
if (!/[^0-9.-]+?/.exec(e))
83
return void D(e, 255, t);
84
if (!/[^0-9A-F]+?/.exec(e))
85
return void D(e, 251, t);
86
}
87
N(o, t), t.writeString(e);
88
}
89
else
90
t.writeUint8(r + 1);
91
}
92
function M(e, t) {
93
var p = 248;
94
var f = 249;
95
if (void 0 === e.tag)
96
return t.writeUint8(p), void t.writeUint8(0);
97
var r = 1;
98
e.attrs && (r += 2 * Object.keys(e.attrs).length),
99
e.content && r++,
100
r < 256 ? (t.writeUint8(p), t.writeUint8(r)) : r < 65536 && (t.writeUint8(f), t.writeUint16(r)),
101
O(e.tag, t),
102
e.attrs &&
103
Object.keys(e.attrs).forEach((r) => {
104
R(r, t), O(e.attrs[r], t);
105
});
106
var a = e.content;
107
if (Array.isArray(a)) {
108
a.length < 256 ? (t.writeUint8(p), t.writeUint8(a.length)) : a.length < 65536 && (t.writeUint8(f), t.writeUint16(a.length));
109
for (var i = 0; i < a.length; i++)
110
M(a[i], t);
111
}
112
else
113
a && O(a, t);
114
}
115
function L(data, t) {
116
const n = data.readUint8();
117
if (n === 0) {
118
return null;
119
}
120
if (n === 248) {
121
return k(data, data.readUint8());
122
}
123
if (n === 249) {
124
return k(data, data.readUint16());
125
}
126
if (n === 252) {
127
return t ? data.readString(data.readUint8()) : data.readByteArray(data.readUint8());
128
}
129
if (n === 253) {
130
const size = ((15 & data.readUint8()) << 16) + (data.readUint8() << 8) + data.readUint8();
131
return t ? data.readString(size) : data.readByteArray(size);
132
}
133
if (n === 254) {
134
return t ? data.readString(data.readUint32()) : data.readByteArray(data.readUint32());
135
}
136
if (n === 250) {
137
const user = L(data, true);
138
if (null != user && 'string' != typeof user)
139
throw new Error(`Decode string got invalid value ${String(t)}, string expected`);
140
const server = decodeStanzaString(data);
141
return jid_utils_1.jidEncode(user, server);
142
}
143
if (n === 247) {
144
const agent = data.readUint8();
145
const device = data.readUint8();
146
const user = decodeStanzaString(data);
147
return jid_utils_1.jidEncode(user, 's.whatsapp.net', device, agent);
148
}
149
if (n === 255) {
150
const number = data.readUint8();
151
return x(data, LIST1, number >>> 7, 127 & number);
152
}
153
if (n === 251) {
154
const number = data.readUint8();
155
return x(data, LIST2, number >>> 7, 127 & number);
156
}
157
if (n <= 0 || n >= 240) {
158
throw new Error('Unable to decode WAP buffer');
159
}
160
if (n >= 236 && n <= 239) {
161
const dict = Constants_1.DICTIONARIES[n - 236];
162
if (!dict) {
163
throw new Error(`Missing WAP dictionary ${n - 236}`);
164
}
165
const index = data.readUint8();
166
const value = dict[index];
167
if (!value) {
168
throw new Error(`Invalid value index ${index} in dict ${n - 236}`);
169
}
170
return value;
171
}
172
const singleToken = Constants_1.SINGLE_BYTE_TOKEN[n - 1];
173
if (!singleToken)
174
throw new Error(`Undefined token with index ${n}`);
175
return singleToken;
176
}
177
function O(e, t) {
178
if (null == e)
179
t.writeUint8(0);
180
else if (typeof e === 'object' && !(e instanceof Uint8Array) && !Buffer.isBuffer(e) && !Array.isArray(e))
181
M(e, t);
182
else if ('string' == typeof e) {
183
const jid = jid_utils_1.jidDecode(e);
184
if (jid) {
185
if (typeof jid.agent !== 'undefined' || typeof jid.device !== 'undefined') {
186
var { user: a, agent: i, device: n } = jid;
187
t.writeUint8(247), t.writeUint8(i || 0), t.writeUint8(n || 0), O(a, t);
188
}
189
else {
190
var { user: s, server: l } = jid;
191
t.writeUint8(250), null != s ? O(s, t) : t.writeUint8(0), O(l, t);
192
}
193
}
194
else {
195
R(e, t);
196
}
197
}
198
else {
199
if (!(e instanceof Uint8Array))
200
throw new Error('Invalid payload type ' + typeof e);
201
N(e.length, t), t.writeByteArray(e);
202
}
203
}
204
function decodeStanzaString(data) {
205
// G
206
const t = L(data, true);
207
if (typeof t != 'string') {
208
throw new Error(`Decode string got invalid value ${String(t)}, string expected`);
209
}
210
return t;
211
}
212
function bufferToUInt(e, t) {
213
let a = 0;
214
for (let i = 0; i < t; i++)
215
a = 256 * a + e[i];
216
return a;
217
}
218
const decodeBinaryNode = (data) => {
219
//U
220
let r = data.readUint8();
221
let t = r === 248 ? data.readUint8() : data.readUint16();
222
if (!t) {
223
throw new Error('Failed to decode node, list cannot be empty');
224
}
225
const a = {};
226
const n = decodeStanzaString(data);
227
for (t -= 1; t > 1;) {
228
const s = decodeStanzaString(data);
229
const l = L(data, true);
230
a[s] = l;
231
t -= 2;
232
}
233
let i = null;
234
1 === t && jid_utils_1.jidDecode(i = L(data, !1)) && (i = String(i));
235
return {
236
tag: n,
237
attrs: a,
238
content: i
239
};
240
};
241
exports.decodeBinaryNode = decodeBinaryNode;
242
const encodeBinaryNode = (node) => {
243
const data = new Binary_1.Binary();
244
O(node, data);
245
const dataArr = data.readByteArray();
246
const result = new Uint8Array(1 + dataArr.length);
247
result[0] = 0;
248
result.set(dataArr, 1);
249
return result;
250
};
251
exports.encodeBinaryNode = encodeBinaryNode;
252
// some extra useful utilities
253
const getBinaryNodeChildren = ({ content }, childTag) => {
254
if (Array.isArray(content)) {
255
return content.filter(item => item.tag == childTag);
256
}
257
return [];
258
};
259
exports.getBinaryNodeChildren = getBinaryNodeChildren;
260
const getAllBinaryNodeChildren = ({ content }) => {
261
if (Array.isArray(content)) {
262
return content;
263
}
264
return [];
265
};
266
exports.getAllBinaryNodeChildren = getAllBinaryNodeChildren;
267
const getBinaryNodeChild = ({ content }, childTag) => {
268
if (Array.isArray(content)) {
269
return content.find(item => item.tag == childTag);
270
}
271
};
272
exports.getBinaryNodeChild = getBinaryNodeChild;
273
const getBinaryNodeChildBuffer = (node, childTag) => {
274
var _a;
275
const child = (_a = exports.getBinaryNodeChild(node, childTag)) === null || _a === void 0 ? void 0 : _a.content;
276
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
277
return child;
278
}
279
};
280
exports.getBinaryNodeChildBuffer = getBinaryNodeChildBuffer;
281
const getBinaryNodeChildUInt = (node, childTag, length) => {
282
const buff = exports.getBinaryNodeChildBuffer(node, childTag);
283
if (buff)
284
return bufferToUInt(buff, length);
285
};
286
exports.getBinaryNodeChildUInt = getBinaryNodeChildUInt;
287
const assertNodeErrorFree = (node) => {
288
const errNode = exports.getBinaryNodeChild(node, 'error');
289
if (errNode) {
290
throw new boom_1.Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });
291
}
292
};
293
exports.assertNodeErrorFree = assertNodeErrorFree;
294
const reduceBinaryNodeToDictionary = (node, tag) => {
295
const nodes = exports.getBinaryNodeChildren(node, tag);
296
const dict = nodes.reduce((dict, { attrs }) => {
297
dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value;
298
return dict;
299
}, {});
300
return dict;
301
};
302
exports.reduceBinaryNodeToDictionary = reduceBinaryNodeToDictionary;
303
const getBinaryNodeMessages = ({ content }) => {
304
const msgs = [];
305
if (Array.isArray(content)) {
306
for (const item of content) {
307
if (item.tag === 'message') {
308
msgs.push(WAProto_1.proto.WebMessageInfo.decode(item.content));
309
}
310
}
311
}
312
return msgs;
313
};
314
exports.getBinaryNodeMessages = getBinaryNodeMessages;
315
__exportStar(require("./generic-utils"), exports);
316
__exportStar(require("./jid-utils"), exports);
317
var Binary_2 = require("../../WABinary/Binary");
318
Object.defineProperty(exports, "Binary", { enumerable: true, get: function () { return Binary_2.Binary; } });
319
__exportStar(require("./types"), exports);
320
__exportStar(require("./Legacy"), exports);
321
322