Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/node_modules/@adiwajshing/baileys/WABinary/Binary.js
2593 views
1
const { hexAt, hexLongIsNegative, hexLongToHex, negateHexLong, NUM_HEX_IN_LONG } = require("./HexHelper");
2
const { inflateSync } = require("zlib")
3
4
var l = "",
5
d = 0;
6
7
const i = 65533,
8
n = new Uint8Array(10),
9
s = new Uint8Array(0);
10
11
function u(e) {
12
if (e === l) return d;
13
for (var t = e.length, r = 0, a = 0; a < t; a++) {
14
var i = e.charCodeAt(a);
15
if (i < 128) r++;
16
else if (i < 2048) r += 2;
17
else if (i < 55296 || (57344 <= i && i <= 65535)) r += 3;
18
else if (55296 <= i && i < 56320 && a + 1 !== t) {
19
var n = e.charCodeAt(a + 1);
20
56320 <= n && n < 57344 ? (a++, (r += 4)) : (r += 3);
21
} else r += 3;
22
}
23
return (l = e), (d = r);
24
}
25
function c(e, t, r) {
26
var a = t >> 21;
27
if (e) {
28
var i = Boolean(2097151 & t || r);
29
return 0 === a || (-1 === a && i);
30
}
31
return 0 === a;
32
}
33
function p(e, t, r, a, i = undefined) {
34
return e.readWithViewParser(t, r, a, i);
35
}
36
function f(e, t, r, a = undefined, i = undefined) {
37
return e.readWithBytesParser(t, r, a, i);
38
}
39
function h(e, t, r, a) {
40
return a ? e.getInt8(t) : e.getUint8(t);
41
}
42
function _(e, t, r, a) {
43
return e.getUint16(t, a);
44
}
45
function m(e, t, r, a) {
46
return e.getInt32(t, a);
47
}
48
function g(e, t, r, a) {
49
return e.getUint32(t, a);
50
}
51
function v(e, t, r, a, i) {
52
return a(e.getInt32(i ? t + 4 : t, i), e.getInt32(i ? t : t + 4, i));
53
}
54
function y(e, t, r, a) {
55
return e.getFloat32(t, a);
56
}
57
function E(e, t, r, a) {
58
return e.getFloat64(t, a);
59
}
60
function S(e, t, r, a) {
61
for (var i = Math.min(a, 10), n = 0, s = 128; n < i && 128 & s; )
62
s = e[t + n++];
63
if (10 === n && s > 1) throw new Error("ParseError: varint exceeds 64 bits");
64
return 128 & s ? n + 1 : n;
65
}
66
function T(e, t, r, a) {
67
var i = 0,
68
n = 0,
69
s = r;
70
10 === r && (n = 1 & e[t + --s]);
71
for (var o = s - 1; o >= 0; o--)
72
(i = (i << 7) | (n >>> 25)), (n = (n << 7) | (127 & e[t + o]));
73
return a(i, n);
74
}
75
function A(e, t, r) {
76
var a = t + e.byteOffset,
77
i = e.buffer;
78
return 0 === a && r === i.byteLength ? i : i.slice(a, a + r);
79
}
80
function b(e, t, r) {
81
return e.subarray(t, t + r);
82
}
83
function C(e, t, r) {
84
for (var a = t + r, n = [], s = null, o = t; o < a; o++) {
85
n.length > 5e3 &&
86
(s || (s = []), s.push(String.fromCharCode.apply(String, n)), (n = []));
87
var l = 0 | e[o];
88
if (0 == (128 & l)) n.push(l);
89
else if (192 == (224 & l)) {
90
var d = H(e, o + 1, a);
91
if (d) {
92
o++;
93
var u = ((31 & l) << 6) | (63 & d);
94
u >= 128 ? n.push(u) : n.push(i);
95
} else n.push(i);
96
} else if (224 == (240 & l)) {
97
var c = H(e, o + 1, a),
98
p = H(e, o + 2, a);
99
if (c && p) {
100
o += 2;
101
var f = ((15 & l) << 12) | ((63 & c) << 6) | (63 & p);
102
f >= 2048 && !(55296 <= f && f < 57344) ? n.push(f) : n.push(i);
103
} else c ? (o++, n.push(i)) : n.push(i);
104
} else if (240 == (248 & l)) {
105
var h = H(e, o + 1, a),
106
_ = H(e, o + 2, a),
107
m = H(e, o + 3, a);
108
if (h && _ && m) {
109
o += 3;
110
var g = ((7 & l) << 18) | ((63 & h) << 12) | ((63 & _) << 6) | (63 & m);
111
if (g >= 65536 && g <= 1114111) {
112
var v = g - 65536;
113
n.push(55296 | (v >> 10), 56320 | (1023 & v));
114
} else n.push(i);
115
} else h && _ ? ((o += 2), n.push(i)) : h ? (o++, n.push(i)) : n.push(i);
116
} else n.push(i);
117
}
118
var y = String.fromCharCode.apply(String, n);
119
return s ? (s.push(y), s.join("")) : y;
120
}
121
function P(e, t, r, a, i) {
122
return e.writeToView(t, r, a, i);
123
}
124
function O(e, t, r, a, i = undefined) {
125
return e.writeToBytes(t, r, a, i);
126
}
127
function M(e, t, r, a) {
128
e[t] = a;
129
}
130
function w(e, t, r, a, i) {
131
e.setUint16(t, a, i);
132
}
133
function I(e, t, r, a, i) {
134
e.setInt16(t, a, i);
135
}
136
function R(e, t, r, a, i) {
137
e.setUint32(t, a, i);
138
}
139
function D(e, t, r, a, i) {
140
e.setInt32(t, a, i);
141
}
142
function N(e, t, r, a, i) {
143
var n = a < 0,
144
s = n ? -a : a,
145
o = Math.floor(s / 4294967296),
146
l = s - 4294967296 * o;
147
n && ((o = ~o), 0 === l ? o++ : (l = -l)),
148
e.setUint32(i ? t + 4 : t, o, i),
149
e.setUint32(i ? t : t + 4, l, i);
150
}
151
function L(e, t, r, a, i) {
152
e.setFloat32(t, a, i);
153
}
154
function k(e, t, r, a, i) {
155
e.setFloat64(t, a, i);
156
}
157
function U(e, t, r, a, i) {
158
for (var n = a, s = i, o = t + r - 1, l = t; l < o; l++)
159
(e[l] = 128 | (127 & s)), (s = (n << 25) | (s >>> 7)), (n >>>= 7);
160
e[o] = s;
161
}
162
function G(e, t, r, a) {
163
for (var i = t, n = a.length, s = 0; s < n; s++) {
164
var o = a.charCodeAt(s);
165
if (o < 128) e[i++] = o;
166
else if (o < 2048) (e[i++] = 192 | (o >> 6)), (e[i++] = 128 | (63 & o));
167
else if (o < 55296 || 57344 <= o)
168
(e[i++] = 224 | (o >> 12)),
169
(e[i++] = 128 | ((o >> 6) & 63)),
170
(e[i++] = 128 | (63 & o));
171
else if (55296 <= o && o < 56320 && s + 1 !== n) {
172
var l = a.charCodeAt(s + 1);
173
if (56320 <= l && l < 57344) {
174
s++;
175
var d = 65536 + (((1023 & o) << 10) | (1023 & l));
176
(e[i++] = 240 | (d >> 18)),
177
(e[i++] = 128 | ((d >> 12) & 63)),
178
(e[i++] = 128 | ((d >> 6) & 63)),
179
(e[i++] = 128 | (63 & d));
180
} else (e[i++] = 239), (e[i++] = 191), (e[i++] = 189);
181
} else (e[i++] = 239), (e[i++] = 191), (e[i++] = 189);
182
}
183
}
184
function F(e, t, r, i, n) {
185
for (
186
var s = hexLongIsNegative(i),
187
o = hexLongToHex(i),
188
l = 0,
189
d = 0,
190
u = 0;
191
u < 16;
192
u++
193
)
194
(l = (l << 4) | (d >>> 28)), (d = (d << 4) | hexAt(o, u));
195
s && ((l = ~l), 0 === d ? l++ : (d = -d)),
196
e.setUint32(n ? t + 4 : t, l, n),
197
e.setUint32(n ? t : t + 4, d, n);
198
}
199
function x(e, t, r, a) {
200
for (var i = 0; i < r; i++) e[t + i] = a[i];
201
}
202
function B(e, t) {
203
var r, a;
204
for (e ? ((r = 5), (a = e >>> 3)) : ((r = 1), (a = t >>> 7)); a; )
205
r++, (a >>>= 7);
206
return r;
207
}
208
function Y(e, t, r, a) {
209
if ("number" != typeof e || e != e || Math.floor(e) !== e || e < t || e >= r) {
210
console.trace('here')
211
throw new TypeError(
212
"string" == typeof e
213
? `WriteError: string "${e}" is not a valid ${a}`
214
: `WriteError: ${String(e)} is not a valid ${a}`
215
);
216
}
217
218
}
219
function K(e, t, r) {
220
var a =
221
4294967296 * (t >= 0 || e ? t : 4294967296 + t) +
222
(r >= 0 ? r : 4294967296 + r);
223
if (!c(e, t, r))
224
throw new Error(`ReadError: integer exceeded 53 bits (${a})`);
225
return a;
226
}
227
function j(e, t) {
228
return K(!0, e, t);
229
}
230
function W(e, t) {
231
return K(!1, e, t);
232
}
233
function H(e, t, r) {
234
if (t >= r) return 0;
235
var a = 0 | e[t];
236
return 128 == (192 & a) ? a : 0;
237
}
238
239
module.exports.numUtf8Bytes = u;
240
module.exports.longFitsInDouble = c;
241
module.exports.parseInt64OrThrow = j;
242
module.exports.parseUint64OrThrow = W;
243
244
class Binary {
245
/** @type {Uint8Array} */
246
buffer;
247
readEndIndex;
248
writeIndex;
249
bytesTrashed = 0;
250
earliestIndex = 0;
251
readIndex = 0;
252
/** @type {DataView} */
253
view = null;
254
littleEndian = false;
255
hiddenReads = 0;
256
hiddenWrites = 0;
257
258
constructor(data = new Uint8Array(0), littleEndian = false) {
259
if (data instanceof ArrayBuffer) {
260
this.buffer = new Uint8Array(data);
261
this.readEndIndex = data.byteLength;
262
this.writeIndex = data.byteLength;
263
}
264
265
if (data instanceof Uint8Array) {
266
this.buffer = data;
267
this.readEndIndex = data.length;
268
this.writeIndex = data.length;
269
}
270
271
this.littleEndian = littleEndian;
272
}
273
274
size() {
275
return this.readEndIndex - this.readIndex;
276
}
277
278
peek(e, t = undefined) {
279
this.hiddenReads++;
280
281
const r = this.readIndex;
282
const a = this.bytesTrashed;
283
284
try {
285
return e(this, t);
286
} finally {
287
this.hiddenReads--, (this.readIndex = r - (this.bytesTrashed - a));
288
}
289
}
290
291
advance(e) {
292
this.shiftReadOrThrow(e);
293
}
294
295
readWithViewParser(e, t, r, a) {
296
return t(this.getView(), this.shiftReadOrThrow(e), e, r, a);
297
}
298
299
readWithBytesParser(e, t, r, a) {
300
return t(this.buffer, this.shiftReadOrThrow(e), e, r, a);
301
}
302
303
readUint8() {
304
//return this.readWithViewParser(1, h, false)
305
return p(this, 1, h, !1);
306
}
307
readInt8() {
308
return p(this, 1, h, !0);
309
}
310
readUint16(e = this.littleEndian) {
311
return p(this, 2, _, e);
312
}
313
readInt32(e = this.littleEndian) {
314
return p(this, 4, m, e);
315
}
316
readUint32(e = this.littleEndian) {
317
return p(this, 4, g, e);
318
}
319
readInt64(e = this.littleEndian) {
320
return p(this, 8, v, j, e);
321
}
322
readUint64(e = this.littleEndian) {
323
return p(this, 8, v, W, e);
324
}
325
readLong(e, t = this.littleEndian) {
326
return p(this, 8, v, e, t);
327
}
328
readFloat32(e = this.littleEndian) {
329
return p(this, 4, y, e);
330
}
331
readFloat64(e = this.littleEndian) {
332
return p(this, 8, E, e);
333
}
334
readVarInt(e) {
335
var t = f(this, 0, S, this.size());
336
return f(this, t, T, e);
337
}
338
readBuffer(e = this.size()) {
339
return 0 === e ? new ArrayBuffer(0) : f(this, e, A);
340
}
341
readByteArray(e = this.size()) {
342
return 0 === e ? new Uint8Array(0) : f(this, e, b);
343
}
344
readBinary(e = this.size(), t = this.littleEndian) {
345
if (0 === e) return new Binary(void 0, t);
346
var r = f(this, e, b);
347
return new Binary(r, t);
348
}
349
indexOf(e) {
350
if (0 === e.length) return 0;
351
for (
352
var t = this.buffer,
353
r = this.readEndIndex,
354
a = this.readIndex,
355
i = 0,
356
n = a,
357
s = a;
358
s < r;
359
s++
360
)
361
if (t[s] === e[i]) {
362
if ((0 === i && (n = s), ++i === e.byteLength))
363
return s - a - e.byteLength + 1;
364
} else i > 0 && ((i = 0), (s = n));
365
return -1;
366
1;
367
}
368
readString(e) {
369
return f(this, e, C);
370
}
371
ensureCapacity(e) {
372
this.maybeReallocate(this.readIndex + e);
373
}
374
ensureAdditionalCapacity(e) {
375
this.maybeReallocate(this.writeIndex + e);
376
}
377
writeToView(e, t, r, a) {
378
var i = this.shiftWriteMaybeReallocate(e);
379
return t(this.getView(), i, e, r, a);
380
}
381
writeToBytes(e, t, r, a) {
382
var i = this.shiftWriteMaybeReallocate(e);
383
return t(this.buffer, i, e, r, a);
384
}
385
write(...e) {
386
for (var t = 0; t < e.length; t++) {
387
var r = e[t];
388
"string" == typeof r
389
? this.writeString(r)
390
: "number" == typeof r
391
? this.writeUint8(r)
392
: r instanceof Binary
393
? this.writeBinary(r)
394
: r instanceof ArrayBuffer
395
? this.writeBuffer(r)
396
: r instanceof Uint8Array && this.writeByteArray(r);
397
}
398
}
399
writeUint8(e) {
400
Y(e, 0, 256, "uint8"), O(this, 1, M, e, !1);
401
}
402
writeInt8(e) {
403
Y(e, -128, 128, "signed int8"), O(this, 1, M, e, !0);
404
}
405
writeUint16(e, t = this.littleEndian) {
406
Y(e, 0, 65536, "uint16"), P(this, 2, w, e, t);
407
}
408
writeInt16(e, t = this.littleEndian) {
409
Y(e, -32768, 32768, "signed int16"), P(this, 2, I, e, t);
410
}
411
writeUint32(e, t = this.littleEndian) {
412
Y(e, 0, 4294967296, "uint32"), P(this, 4, R, e, t);
413
}
414
writeInt32(e, t = this.littleEndian) {
415
Y(e, -2147483648, 2147483648, "signed int32"), P(this, 4, D, e, t);
416
}
417
writeUint64(e, t = this.littleEndian) {
418
Y(e, 0, 0x10000000000000000, "uint64"), P(this, 8, N, e, t);
419
}
420
writeInt64(e, t = this.littleEndian) {
421
Y(e, -0x8000000000000000, 0x8000000000000000, "signed int64"),
422
P(this, 8, N, e, t);
423
}
424
writeFloat32(e, t = this.littleEndian) {
425
P(this, 4, L, e, t);
426
}
427
writeFloat64(e, t = this.littleEndian) {
428
P(this, 8, k, e, t);
429
}
430
writeVarInt(e) {
431
Y(e, -0x8000000000000000, 0x8000000000000000, "varint (signed int64)");
432
var t = e < 0,
433
r = t ? -e : e,
434
a = Math.floor(r / 4294967296),
435
i = r - 4294967296 * a;
436
t && ((a = ~a), 0 === i ? a++ : (i = -i)), O(this, B(a, i), U, a, i);
437
}
438
writeVarIntFromHexLong(e) {
439
for (
440
var t = hexLongIsNegative(e),
441
r = t ? negateHexLong(e) : e,
442
i = hexLongToHex(r),
443
n = 0,
444
s = 0,
445
o = 0;
446
o < NUM_HEX_IN_LONG;
447
o++
448
)
449
(n = (n << 4) | (s >>> 28)), (s = (s << 4) | hexAt(i, o));
450
t && ((n = ~n), 0 === s ? n++ : (s = -s)), O(this, B(n, s), U, n, s);
451
}
452
writeBinary(e) {
453
var t = e.peek((e) => e.readByteArray());
454
if (t.length) {
455
var r = this.shiftWriteMaybeReallocate(t.length);
456
this.buffer.set(t, r);
457
}
458
}
459
writeBuffer(e) {
460
this.writeByteArray(new Uint8Array(e));
461
}
462
writeByteArray(e) {
463
var t = this.shiftWriteMaybeReallocate(e.length);
464
this.buffer.set(e, t);
465
}
466
writeBufferView(e) {
467
this.writeByteArray(new Uint8Array(e.buffer, e.byteOffset, e.byteLength));
468
}
469
writeString(e) {
470
O(this, u(e), G, e);
471
}
472
writeHexLong(e, t = this.littleEndian) {
473
P(this, 8, F, e, t);
474
}
475
writeBytes(...e) {
476
for (var t = 0; t < e.length; t++) Y(e[t], 0, 256, "byte");
477
O(this, e.length, x, e);
478
}
479
writeAtomically(e, t) {
480
this.hiddenWrites++;
481
var r = this.writeIndex,
482
a = this.bytesTrashed;
483
try {
484
var i = e(this, t);
485
return (r = this.writeIndex), (a = this.bytesTrashed), i;
486
} finally {
487
this.hiddenWrites--, (this.writeIndex = r - (this.bytesTrashed - a));
488
}
489
}
490
491
writeWithVarIntLength(e, t) {
492
var r = this.writeIndex,
493
a = this.writeAtomically(e, t),
494
i = this.writeIndex;
495
this.writeVarInt(i - r);
496
for (var s = this.writeIndex - i, o = this.buffer, l = 0; l < s; l++)
497
n[l] = o[i + l];
498
for (var d = i - 1; d >= r; d--) o[d + s] = o[d];
499
for (var u = 0; u < s; u++) o[r + u] = n[u];
500
return a;
501
}
502
503
static build(...e) {
504
let t = 0;
505
let r = 0;
506
for (t = 0, r = 0; r < e.length; r++) {
507
let a = e[r];
508
"string" == typeof a
509
? (t += u(a))
510
: "number" == typeof a
511
? t++
512
: a instanceof Binary
513
? (t += a.size())
514
: a instanceof ArrayBuffer
515
? (t += a.byteLength)
516
: a instanceof Uint8Array && (t += a.length);
517
}
518
519
var i = new Binary();
520
return i.ensureCapacity(t), i.write.apply(i, arguments), i;
521
}
522
523
getView() {
524
return (
525
this.view ||
526
(this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset))
527
);
528
}
529
530
shiftReadOrThrow(e) {
531
if (e < 0)
532
throw new Error("ReadError: given negative number of bytes to read");
533
var t = this.readIndex,
534
r = t + e;
535
if (r > this.readEndIndex)
536
throw new Error(
537
t === this.readEndIndex
538
? "ReadError: tried to read from depleted binary"
539
: "ReadError: tried to read beyond end of binary"
540
);
541
return (
542
(this.readIndex = r), this.hiddenReads || (this.earliestIndex = r), t
543
);
544
}
545
546
maybeReallocate(e) {
547
const t = this.buffer;
548
if (e <= t.length) {
549
return e;
550
}
551
552
const r = this.earliestIndex;
553
const a = e - r;
554
const i = Math.max(a, 2 * (t.length - r), 64);
555
const n = new Uint8Array(i);
556
return (
557
r
558
? (n.set(t.subarray(r)),
559
(this.bytesTrashed += r),
560
(this.readIndex -= r),
561
(this.readEndIndex -= r),
562
(this.writeIndex -= r),
563
(this.earliestIndex = 0))
564
: n.set(t),
565
(this.buffer = n),
566
(this.view = null),
567
a
568
);
569
}
570
571
shiftWriteMaybeReallocate(e) {
572
const t = this.maybeReallocate(this.writeIndex + e);
573
const r = this.writeIndex;
574
return (
575
(this.writeIndex = t), this.hiddenWrites || (this.readEndIndex = t), r
576
);
577
}
578
decompressed = () => {
579
if (2 & this.readUint8()) {
580
const result = inflateSync(this.readByteArray())
581
return new Binary(result)
582
}
583
return this
584
}
585
}
586
587
module.exports.Binary = Binary
588