forked from facebookarchive/fb-adb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adbenc.c
146 lines (130 loc) · 3.62 KB
/
adbenc.c
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
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in
* the LICENSE file in the root directory of this source tree. An
* additional grant of patent rights can be found in the PATENTS file
* in the same directory.
*
*/
#include <errno.h>
#include <stddef.h>
#include "adbenc.h"
#include "util.h"
#include "fs.h"
/* The sequences [\n ~ .] and [\r ~ .] typed into an adb pty act as an
* emergency escape sequences and cause immediate disconnection. Make
* sure we never send these bytes. The easiest way to do that is to
* make sure we never send `~', a.k.a. adb_forbidden: adb_escape1
* followed by adb_escape1 is adb_escape1, and adb_escape1 followed by
* anything else is adb_forbidden. */
static const char adb_forbidden = '~';
static const char adb_escape1 = '!';
static const char adb_escape2 = '@';
void
adb_encode(unsigned* inout_state,
char** inout_enc,
char* encend,
const char** inout_in,
const char* inend)
{
unsigned state = *inout_state;
char* enc = *inout_enc;
const char* in = *inout_in;
while (in < inend && enc < encend) {
if (state == 0) {
if (*in == adb_escape1) {
*enc++ = adb_escape1;
state = 1;
} else if (*in == adb_forbidden) {
*enc++ = adb_escape1;
state = 2;
} else {
*enc++ = *in++;
}
} else if (state == 1) {
*enc++ = adb_escape1;
in++;
state = 0;
} else if (state == 2) {
*enc++ = adb_escape2;
in++;
state = 0;
}
}
*inout_state = state;
*inout_enc = enc;
*inout_in = in;
}
void
adb_decode(unsigned* inout_state,
char** inout_dec,
char* decend,
const char** inout_in,
const char* inend)
{
unsigned state = *inout_state;
char* dec = *inout_dec;
const char* in = *inout_in;
while (in < inend && dec < decend) {
char c = *in++;
if (state == 0) {
if (c == adb_escape1)
state = 1;
else
*dec++ = c;
} else if (state == 1) {
if (c == adb_escape1)
*dec++ = adb_escape1;
else
*dec++ = adb_forbidden;
state = 0;
}
}
*inout_state = state;
*inout_dec = dec;
*inout_in = in;
}
size_t
read_all_adb_encoded(int fd, void* buf, size_t sz)
{
char encbuf[4096];
unsigned state = 0;
char* dec = buf;
char* decend = dec + sz;
ssize_t ret;
size_t nr_read = 0;
while (nr_read < sz) {
do {
WITH_IO_SIGNALS_ALLOWED();
ret = read(fd, encbuf, XMIN(sz - nr_read, sizeof (encbuf)));
} while (ret == -1 && errno == EINTR);
if (ret < 0)
die_errno("read[adbenc]");
if (ret < 1)
break;
const char* in = encbuf;
const char* inend = encbuf + ret;
char* cur_dec = dec;
adb_decode(&state, &dec, decend, &in, inend);
nr_read += dec - cur_dec;
}
return nr_read;
}
void
write_all_adb_encoded(int fd, const void* buf, size_t sz)
{
char encbuf[4096];
unsigned state = 0;
const char* in = buf;
const char* inend = in + sz;
size_t nr_written = 0;
while (nr_written < sz) {
char* enc = encbuf;
char* encend = enc + sizeof (encbuf);
adb_encode(&state, &enc, encend, &in, inend);
write_all(fd, encbuf, enc - encbuf);
nr_written += enc - encbuf;
}
}