forked from bitbegin/RedAdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadb.red
299 lines (269 loc) · 5.65 KB
/
adb.red
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
Red []
#system [
#include %adb-windows.reds
int-to-bin*: func [int [integer!] bin [red-binary!]
/local
p [int-ptr!]
s [series!]
][
s: GET_BUFFER(bin)
p: as int-ptr! s/tail
p/1: int
s/tail: as cell! p + 1
]
]
adb: context [
str-to-int: func [id [string!]][
(to integer! id/1) or
(shift/left to integer! id/2 8) or
(shift/left to integer! id/3 16) or
(shift/left to integer! id/4 24)
]
A_SYNC: str-to-int "SYNC"
A_CNXN: str-to-int "CNXN"
A_OPEN: str-to-int "OPEN"
A_OKAY: str-to-int "OKAY"
A_CLSE: str-to-int "CLSE"
A_WRTE: str-to-int "WRTE"
A_VERSION: str-to-int "^@^@^@^A" ;#{01000000}
ID_STAT: str-to-int "STAT"
ID_LIST: str-to-int "LIST"
ID_ULNK: str-to-int "ULNK"
ID_SEND: str-to-int "SEND"
ID_RECV: str-to-int "RECV"
ID_DENT: str-to-int "DENT"
ID_DONE: str-to-int "DONE"
ID_DATA: str-to-int "DATA"
ID_OKAY: str-to-int "OKAY"
ID_FAIL: str-to-int "FAIL"
ID_QUIT: str-to-int "QUIT"
PACKET_SIZE: 1024 * 64
MAX_PAYLOAD: 4096
adb-mode: no
make-null-string: func [len [integer!]][
head insert/dup make string! len null len
]
make-null-binary: func [len [integer!]][
head insert/dup make binary! len null len
]
get-adbs: routine [return: [integer!]][
adbs
]
get-local-id: routine [
iadb [integer!]
return: [integer!]
][
get-local-id* iadb
]
get-remote-id: routine [
iadb [integer!]
return: [integer!]
][
get-remote-id* iadb
]
set-local-id: routine [
iadb [integer!]
local-id [integer!]
][
set-local-id* iadb local-id
]
set-remote-id: routine [
iadb [integer!]
remote-id [integer!]
][
set-remote-id* iadb remote-id
]
get-error: routine [return: [string!]
/local
text [c-string!]
][
text: get-error-msg*
string/load text length? text UTF-16LE
]
get-adb-error: routine [return: [string!]
/local
text [c-string!]
][
text: parse-adb-result*
string/load text length? text UTF-8
]
int-to-bin: routine [int [integer!] bin [binary!]
][
int-to-bin* int bin
]
format-message: func [
blk [block!]
;command [integer!] ;-- command identifier constant
;arg0 [integer!] ;-- first argument
;arg1 [integer!] ;-- second argument
;data-length [integer!] ;-- length of payload (0 is allowed)
;data-crc32 [integer!] ;-- crc32 of data payload
;magic [integer!] ;-- command ^ 0xffffffff
return: [binary!]
/local
msg [binary!]
bin [binary!]
][
msg: make-null-binary 4 * 6
bin: make binary! 4
clear msg
foreach i blk [
clear bin
int-to-bin i bin
append msg bin
]
msg
]
init-adb: routine [return: [integer!]][
init-adb*
]
close-adb: routine [
adb [integer!]
][
close-adb* adb
]
close-adbs: routine [
/local
iadb [integer!]
][
iadb: adbs
while [iadb > 0][
close-adb* iadb
iadb: iadb - 1
]
adbs: 0
]
pipe-raw: routine [
iadb [integer!]
data [binary!]
write [logic!]
return: [integer!]
][
pipe* iadb data write
]
pipe: func [
iadb [integer!]
data [string! binary!]
return: [integer!]
/write /read
][
if string? data [data: to binary! data]
either write [
pipe-raw iadb to binary! data yes
][
pipe-raw iadb to binary! data no
]
]
read: func [
iadb [integer!]
data [string! binary!]
][
either adb-mode [
pipe/read iadb data
][
;-- TCP mode
]
]
write: func [
iadb [integer!]
data [string! binary!]
][
either adb-mode [
pipe/write iadb data
][
;-- TCP mode
]
]
;; higher level
init: func [return: [integer!]
/local
ret [integer!]
][
if all [ret: init-adb get-adbs] [adb-mode: yes]
ret
]
close: func [][
if adb-mode [close-adbs]
]
receive-message: func [
iadb [integer!]
cmd [string! block!]
return: [binary!]
/authed
/local
recv-cmd [string!]
msg [string!]
data [string!]
buffer [binary!]
s [binary!]
][
until [
buffer: make-null-binary MAX_PAYLOAD
read iadb buffer
recv-cmd: either buffer/1 = #{00} [clear buffer][
if cmd = to binary! "ALL" [return buffer]
copy/part buffer 4
]
foreach c cmd [s: find recv-cmd to binary! c if s [break]]
s
]
switch/default s [
to binary! "AUTH" [
;-- we no rsa function in red, so TBC
]
to binary! "OKAY" [
set-remote-id iadb str-to-int skip buffer 4 ;arg0
]
to binary! "CNXN" [
if positive? str-to-int skip buffer 12 [ ;data-length
data: receive-message iadb "ALL"
]
]
to binary! "WRTE" [
buffer: receive-message iadb "ALL"
send-message iadb A_OKAY ""
]
to binary! "CLSE" [
send-message iadb A_CLSE ""
]
][buffer]
buffer
]
send-message: func [
iadb [integer!]
cmd [integer!]
data [string! binary!]
/local
len [integer!]
sum [integer!]
msg [block!]
magic [integer!]
][
if string? data [data: to binary! data]
magic: cmd xor -1
len: length? data
sum: 0
foreach c data [sum: sum + (to integer! c)]
case [
cmd = A_CNXN [
msg: [cmd A_VERSION MAX_PAYLOAD len sum magic]
]
cmd = A_OPEN [
msg: [cmd get-local-id iadb 0 len sum magic]
]
cmd = A_CLSE [
msg: [cmd 0 get-remote-id iadb len sum magic]
]
any [cmd = A_WRTE cmd = A_OKAY] [
msg: [cmd get-local-id iadb get-remote-id iadb len sum magic]
]
]
write iadb format-message reduce msg
unless empty? data [write iadb data]
if cmd = A_WRTE [
if empty? receive-message iadb "OKAY" [
close-adb iadb
]
]
]
]