blob: 003586dea6130361a938a9ba29503f91a175bde3 [file] [log] [blame]
Darth98f775972022-08-05 09:39:03 +01001#ifndef _ABXFUNCTIONS_HPP
2#define _ABXFUNCTIONS_HPP
3
4#include <fstream>
5#include <iostream>
6#include <sstream>
7#include <string>
8#include <cstring>
9#include <vector>
10#include <algorithm>
11#include <openssl/evp.h>
12
13// ABX (Android Binary XML) to XML converter
14// Copyright (c) 2022, _that
15// SPDX-License-Identifier: GPL-3.0-only
16
17// test: g++ -g abxtoxml.cpp `pkg-config --libs openssl` && ./a.out packages.xml
18
19// based on https://www.cclsolutionsgroup.com/post/android-abx-binary-xml
20
21using namespace std;
22
23class AbxToXml
24{
25 std::istream& m_is;
26 std::ostream& m_os;
27 bool mTagOpen = false;
28 std::vector<std::string> mInternedStrings;
29 bool mError = true;
30
31public:
32 AbxToXml(std::istream& is, std::ostream& os) : m_is(is), m_os(os) {}
33
34 bool run()
35 {
36 mError = true;
37 char buffer[4];
38 m_is.read(buffer, 4);
39 if (memcmp(buffer, "ABX", 3) != 0)
40 // TODO: handle error: not an ABX file
41 return false;
42
43 if (buffer[3] != 0)
44 // TODO: handle error: we only understand ABX version 0
45 return false;
46
47 mError = false;
48 return convert_abx_content();
49 }
50
51private:
52 enum Event
53 {
54 // based on XmlPullParser.java in libcore
55 START_DOCUMENT = 0,
56 END_DOCUMENT = 1,
57 START_TAG = 2,
58 END_TAG = 3,
59 TEXT = 4,
60 CDSECT = 5,
61 ENTITY_REF = 6,
62 IGNORABLE_WHITESPACE = 7,
63 PROCESSING_INSTRUCTION = 8,
64 COMMENT = 9,
65 DOCDECL = 10,
66
67 // based on BinaryXmlSerializer.java
68 ATTRIBUTE = 15,
69 };
70
71 // based on BinaryXmlSerializer.java
72 enum Type
73 {
74 TYPE_NULL = 1 << 4,
75 TYPE_STRING = 2 << 4,
76 TYPE_STRING_INTERNED = 3 << 4,
77 TYPE_BYTES_HEX = 4 << 4,
78 TYPE_BYTES_BASE64 = 5 << 4,
79 TYPE_INT = 6 << 4,
80 TYPE_INT_HEX = 7 << 4,
81 TYPE_LONG = 8 << 4,
82 TYPE_LONG_HEX = 9 << 4,
83 TYPE_FLOAT = 10 << 4,
84 TYPE_DOUBLE = 11 << 4,
85 TYPE_BOOLEAN_TRUE = 12 << 4,
86 TYPE_BOOLEAN_FALSE = 13 << 4,
87 };
88
89
90 bool convert_abx_content()
91 {
92 char b;
93 while (m_is.get(b))
94 {
95 Event ev = Event(b & 0xf);
96 Type ty = Type(b & 0xf0);
97
98 if (ev == ATTRIBUTE)
99 {
100 // TODO: verify that tag is still open
101 m_os << " " << read_data(TYPE_STRING_INTERNED);
102 m_os << "=\"";
103 m_os << read_data(ty); // TODO: escaping?
104 m_os << '"';
105 continue;
106 }
107 if (mTagOpen)
108 {
109 m_os << ">";
110 mTagOpen = false;
111 }
112 std::string data = read_data(ty);
113 dispatch_event(ev, data);
114 }
115 return !mError;
116 }
117
118 void dispatch_event(Event ev, const std::string& data)
119 {
120 switch (ev)
121 {
122 case START_DOCUMENT:
123 case END_DOCUMENT:
124 // TODO: track that we actually started the document
125 break;
126
127 case START_TAG:
128 m_os << "<" << data;
129 mTagOpen = true;
130 break;
131
132 case END_TAG:
133 m_os << "</" << data << ">";
134 break;
135
136 case TEXT:
137 m_os << data;
138 break;
139
140 case CDSECT:
141 m_os << "<![CDATA[" << data << "]]>";
142 break;
143
144 case ENTITY_REF:
145 case IGNORABLE_WHITESPACE:
146 case PROCESSING_INSTRUCTION:
147 case COMMENT:
148 case DOCDECL:
149 m_os << data;
150 // TODO
151 break;
152
153 default:
154 m_os << "#error: Invalid event " << int(ev);
155 mError = true;
156 break;
157 }
158 }
159
160 template <typename T> static T read_bswap(std::istream& is)
161 {
162 char buffer[sizeof(T)];
163 is.read(buffer, sizeof(T));
164 std::reverse(std::begin(buffer), std::end(buffer));
165 return *reinterpret_cast<T*>(buffer);
166 }
167
168 static uint16_t read_uint16(std::istream& is)
169 {
170 return read_bswap<uint16_t>(is);
171 }
172
173 static std::string read_string(std::istream& is)
174 {
175 uint16_t length = read_uint16(is);
176 std::string s;
177 s.resize(length);
178 is.read(&s[0], length);
179 return s;
180 }
181
182
183 std::string read_data(Type ty)
184 {
185 switch (ty)
186 {
187 case TYPE_NULL:
188 return {};
189
190 case TYPE_STRING:
191 return read_string(m_is);
192
193 case TYPE_STRING_INTERNED:
194 {
195 uint16_t id = read_uint16(m_is);
196 if (id == 0xffff)
197 {
198 std::string s = read_string(m_is);
199 mInternedStrings.push_back(s);
200 return s;
201 }
202 if (id >= mInternedStrings.size())
203 {
204 // TODO: handle error
205 mError = true;
206 return "#error: invalid string ID";
207 }
208 return mInternedStrings.at(id);
209 }
210
211 case TYPE_BYTES_HEX:
212 {
213 std::string s = read_string(m_is);
214 std::string hex;
215 static const char* hexdigits = "0123456789abcdef";
216 for (unsigned char c : s)
217 {
218 hex += hexdigits[c >> 4];
219 hex += hexdigits[c & 0xf];
220 }
221 return hex;
222 }
223
224 case TYPE_BYTES_BASE64:
225 {
226 std::string s = read_string(m_is);
227 std::string b64;
228 auto outlen = ((s.length() + 2) / 3) * 4;
229 b64.resize(outlen+1); // +1 for null terminator
230 auto got = EVP_EncodeBlock(
231 reinterpret_cast<unsigned char *>(&b64[0]),
232 reinterpret_cast<const unsigned char*>(s.c_str()), s.length());
233 if (got != outlen)
234 {
235 mError = true;
236 return "#error: base64 encoding failed"; // TODO
237 }
238 b64.resize(outlen);
239 return b64;
240 }
241
242 case TYPE_INT_HEX:
243 // TODO: output hex instead of dec (what is the exact format?)
244 case TYPE_INT:
245 {
246 int32_t val = read_bswap<int>(m_is);
247 std::stringstream ss;
248 ss << val;
249 return ss.str();
250 }
251
252 case TYPE_LONG_HEX:
253 // TODO: output hex instead of dec (what is the exact format?)
254 case TYPE_LONG:
255 {
256 int64_t val = read_bswap<long>(m_is);
257 std::stringstream ss;
258 ss << val;
259 return ss.str();
260 }
261
262 case TYPE_FLOAT:
263 {
264 float val = read_bswap<float>(m_is);
265 std::stringstream ss;
266 ss << val;
267 return ss.str();
268 }
269
270 case TYPE_DOUBLE:
271 {
272 double val = read_bswap<double>(m_is);
273 std::stringstream ss;
274 ss << val;
275 return ss.str();
276 }
277
278 case TYPE_BOOLEAN_TRUE:
279 return "1";
280
281 case TYPE_BOOLEAN_FALSE:
282 return "0";
283 }
284 // TODO
285 mError = true;
286 return "#error: invalid type";
287 }
288};
289#endif // _ABXFUNCTIONS_HPP