-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abx-functions.hpp
289 lines (251 loc) · 5.61 KB
/
abx-functions.hpp
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
#ifndef _ABXFUNCTIONS_HPP
#define _ABXFUNCTIONS_HPP
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <openssl/evp.h>
// ABX (Android Binary XML) to XML converter
// Copyright (c) 2022, _that
// SPDX-License-Identifier: GPL-3.0-only
// test: g++ -g abxtoxml.cpp `pkg-config --libs openssl` && ./a.out packages.xml
// based on https://www.cclsolutionsgroup.com/post/android-abx-binary-xml
using namespace std;
class AbxToXml
{
std::istream& m_is;
std::ostream& m_os;
bool mTagOpen = false;
std::vector<std::string> mInternedStrings;
bool mError = true;
public:
AbxToXml(std::istream& is, std::ostream& os) : m_is(is), m_os(os) {}
bool run()
{
mError = true;
char buffer[4];
m_is.read(buffer, 4);
if (memcmp(buffer, "ABX", 3) != 0)
// TODO: handle error: not an ABX file
return false;
if (buffer[3] != 0)
// TODO: handle error: we only understand ABX version 0
return false;
mError = false;
return convert_abx_content();
}
private:
enum Event
{
// based on XmlPullParser.java in libcore
START_DOCUMENT = 0,
END_DOCUMENT = 1,
START_TAG = 2,
END_TAG = 3,
TEXT = 4,
CDSECT = 5,
ENTITY_REF = 6,
IGNORABLE_WHITESPACE = 7,
PROCESSING_INSTRUCTION = 8,
COMMENT = 9,
DOCDECL = 10,
// based on BinaryXmlSerializer.java
ATTRIBUTE = 15,
};
// based on BinaryXmlSerializer.java
enum Type
{
TYPE_NULL = 1 << 4,
TYPE_STRING = 2 << 4,
TYPE_STRING_INTERNED = 3 << 4,
TYPE_BYTES_HEX = 4 << 4,
TYPE_BYTES_BASE64 = 5 << 4,
TYPE_INT = 6 << 4,
TYPE_INT_HEX = 7 << 4,
TYPE_LONG = 8 << 4,
TYPE_LONG_HEX = 9 << 4,
TYPE_FLOAT = 10 << 4,
TYPE_DOUBLE = 11 << 4,
TYPE_BOOLEAN_TRUE = 12 << 4,
TYPE_BOOLEAN_FALSE = 13 << 4,
};
bool convert_abx_content()
{
char b;
while (m_is.get(b))
{
Event ev = Event(b & 0xf);
Type ty = Type(b & 0xf0);
if (ev == ATTRIBUTE)
{
// TODO: verify that tag is still open
m_os << " " << read_data(TYPE_STRING_INTERNED);
m_os << "=\"";
m_os << read_data(ty); // TODO: escaping?
m_os << '"';
continue;
}
if (mTagOpen)
{
m_os << ">";
mTagOpen = false;
}
std::string data = read_data(ty);
dispatch_event(ev, data);
}
return !mError;
}
void dispatch_event(Event ev, const std::string& data)
{
switch (ev)
{
case START_DOCUMENT:
case END_DOCUMENT:
// TODO: track that we actually started the document
break;
case START_TAG:
m_os << "<" << data;
mTagOpen = true;
break;
case END_TAG:
m_os << "</" << data << ">";
break;
case TEXT:
m_os << data;
break;
case CDSECT:
m_os << "<![CDATA[" << data << "]]>";
break;
case ENTITY_REF:
case IGNORABLE_WHITESPACE:
case PROCESSING_INSTRUCTION:
case COMMENT:
case DOCDECL:
m_os << data;
// TODO
break;
default:
m_os << "#error: Invalid event " << int(ev);
mError = true;
break;
}
}
template <typename T> static T read_bswap(std::istream& is)
{
char buffer[sizeof(T)];
is.read(buffer, sizeof(T));
std::reverse(std::begin(buffer), std::end(buffer));
return *reinterpret_cast<T*>(buffer);
}
static uint16_t read_uint16(std::istream& is)
{
return read_bswap<uint16_t>(is);
}
static std::string read_string(std::istream& is)
{
uint16_t length = read_uint16(is);
std::string s;
s.resize(length);
is.read(&s[0], length);
return s;
}
std::string read_data(Type ty)
{
switch (ty)
{
case TYPE_NULL:
return {};
case TYPE_STRING:
return read_string(m_is);
case TYPE_STRING_INTERNED:
{
uint16_t id = read_uint16(m_is);
if (id == 0xffff)
{
std::string s = read_string(m_is);
mInternedStrings.push_back(s);
return s;
}
if (id >= mInternedStrings.size())
{
// TODO: handle error
mError = true;
return "#error: invalid string ID";
}
return mInternedStrings.at(id);
}
case TYPE_BYTES_HEX:
{
std::string s = read_string(m_is);
std::string hex;
static const char* hexdigits = "0123456789abcdef";
for (unsigned char c : s)
{
hex += hexdigits[c >> 4];
hex += hexdigits[c & 0xf];
}
return hex;
}
case TYPE_BYTES_BASE64:
{
std::string s = read_string(m_is);
std::string b64;
auto outlen = ((s.length() + 2) / 3) * 4;
b64.resize(outlen+1); // +1 for null terminator
auto got = EVP_EncodeBlock(
reinterpret_cast<unsigned char *>(&b64[0]),
reinterpret_cast<const unsigned char*>(s.c_str()), s.length());
if (got != outlen)
{
mError = true;
return "#error: base64 encoding failed"; // TODO
}
b64.resize(outlen);
return b64;
}
case TYPE_INT_HEX:
// TODO: output hex instead of dec (what is the exact format?)
case TYPE_INT:
{
int32_t val = read_bswap<int>(m_is);
std::stringstream ss;
ss << val;
return ss.str();
}
case TYPE_LONG_HEX:
// TODO: output hex instead of dec (what is the exact format?)
case TYPE_LONG:
{
int64_t val = read_bswap<long>(m_is);
std::stringstream ss;
ss << val;
return ss.str();
}
case TYPE_FLOAT:
{
float val = read_bswap<float>(m_is);
std::stringstream ss;
ss << val;
return ss.str();
}
case TYPE_DOUBLE:
{
double val = read_bswap<double>(m_is);
std::stringstream ss;
ss << val;
return ss.str();
}
case TYPE_BOOLEAN_TRUE:
return "1";
case TYPE_BOOLEAN_FALSE:
return "0";
}
// TODO
mError = true;
return "#error: invalid type";
}
};
#endif // _ABXFUNCTIONS_HPP