-
Notifications
You must be signed in to change notification settings - Fork 7
/
func.js
161 lines (160 loc) · 4.38 KB
/
func.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
console.hex = (d) => console.log((Object(d).buffer instanceof ArrayBuffer ? new Uint8Array(d.buffer) :
typeof d === 'string' ? (new TextEncoder('utf-8')).encode(d) :
new Uint8ClampedArray(d)).reduce((p, c, i, a) => p + (i % 16 === 0 ? i.toString(16).padStart(6, 0) + ' ' : ' ') +
c.toString(16).padStart(2, 0) + (i === a.length - 1 || i % 16 === 15 ?
' '.repeat((15 - i % 16) * 3) + Array.from(a).splice(i - i % 16, 16).reduce((r, v) =>
r + (v > 31 && v < 127 || v > 159 ? String.fromCharCode(v) : '.'), ' ') + '\n' : ''), ''));
function readString(inArray){
var value=[]
var start = 1
var length = 0
if(inArray[0] <= 253) {
length = inArray[0]
} else {
length = inArray[1] + (inArray[2]<<8) + (inArray[3]<<16)
start = 4
}
for(var i = 0; i< length; i++){
value.push(String.fromCharCode(inArray[start+i]))
}
return value.join('')
}
function readBytes(inArray){
var value=[]
var start = 1
var length = 0
if(inArray[0] <= 253) {
length = inArray[0]
} else {
length = inArray[1] + (inArray[2]<<8) + (inArray[3]<<16)
start = 4
}
for(var i = 0; i< length; i++){
value.push(inArray[start+i])
}
return value
}
function lengthStringOrBytes(inArray){
var start = 1
var length = 0
if(inArray[0] <= 253) {
length = inArray[0]
} else {
length = inArray[1] + (inArray[2]<<8) + (inArray[3]<<16)
start = 4
}
var padding = ((start+length) % 4) ? (4-((start+length) % 4)) : 0
return start+length+padding
}
function readBufferFromString(str){
const textencoder = new TextEncoder()
var value = textencoder.encode(str)
var ret = []
if(value.length <= 253){
ret.push(value.length)
}else{
ret.push(254)
ret.push(value.length & 0xff)
ret.push((value.length >> 8 )& 0xff)
ret.push((value.length >> 16 )& 0xff)
}
for(var i = 0;i<value.length;i++){
ret.push(value[i])
}
var padding = ((ret.length) % 4) ? (4 - ((ret.length) % 4)) : 0
for(var i = 0; i< padding;i++){
ret.push(0)
}
return ret
}
function readArrayFromString(str){
const textencoder = new TextEncoder()
var value = textencoder.encode(str)
var ret = []
for(var i = 0;i<value.length;i++){
ret.push(value[i])
}
return ret
}
function readBufferFromArray(arr){
var value = arr
var ret = []
if(value.length <= 253){
ret.push(value.length)
}else{
ret.push(254)
ret.push(value.length & 0xff)
ret.push((value.length >> 8 )& 0xff)
ret.push((value.length >> 16 )& 0xff)
}
for(var i = 0;i<value.length;i++){
ret.push(value[i])
}
var padding = ((ret.length) % 4) ? (4 - ((ret.length) % 4)) : 0
for(var i = 0; i< padding;i++){
ret.push(0)
}
return ret
}
function utf8Decode(utf8String) {
if (typeof utf8String != 'string') {
console.hex(utf8String)
return (typeof utf8String) + " possible mediadata"//throw new TypeError('parameter ‘utf8String’ is not a string');
}
// note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
const unicodeString = utf8String.replace(
/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars
function(c) { // (note parentheses for precedence)
var cc = ((c.charCodeAt(0)&0x0f)<<12) | ((c.charCodeAt(1)&0x3f)<<6) | ( c.charCodeAt(2)&0x3f);
return String.fromCharCode(cc); }
).replace(
/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars
function(c) { // (note parentheses for precedence)
var cc = (c.charCodeAt(0)&0x1f)<<6 | c.charCodeAt(1)&0x3f;
return String.fromCharCode(cc); }
);
return unicodeString;
}
function stringifyReplacer(key, value) {
if (typeof value === 'bigint') {
return value.toString() + 'n';
} else {
return value;
}
}
function parseReplacer(key, value) {
if (typeof value === 'string') {
return BigInt(value.slice(0, -1));
} else {
return value;
}
}
function r_with_peer(peer){
var r={}
switch (peer.type){
case "broadcast":
case "channel":{
r["_id"]=parseInt(peer.id,10)
r["_access_hash"]=BigInt(peer.access_hash.replace("n",""))
r["_peer"]=0x20adaef8
break
}
case "user":
case "bot":{
r["_id"]=parseInt(peer.id,10)
r["_access_hash"]=BigInt(peer.access_hash.replace("n",""))
r["_peer"]=0x7b8e7de6
break
}
case "chat":{
r["_id"]=parseInt(peer.id,10)
r["_peer"]=0x179be863
break
}
case "self":{
r["_peer"]=0x7da07ec9
break
}
}
return r
}