forked from alesimula/axmldec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
221 lines (193 loc) · 6.61 KB
/
main.cpp
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
/*
* Copyright (c) 2016, 2017, Yutaka Tsutano
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "axmldec_config.hpp"
#include "jitana/util/axml_parser.hpp"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/program_options.hpp>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define PLATFORM_WINDOWS
#include <io.h>
#include <fcntl.h>
#endif
#include <unzip.h>
#define AXMLDEC_VERSION AXMLDEC_VERSION_MAJOR "." AXMLDEC_VERSION_MINOR "." AXMLDEC_VERSION_PATCH
namespace boost_pt = boost::property_tree;
struct membuf : std::streambuf {
membuf(const char* base, size_t size)
{
char* p(const_cast<char*>(base));
this->setg(p, p, p + size);
}
};
struct imemstream : virtual membuf, std::istream {
imemstream(char const* base, size_t size)
: membuf(base, size),
std::istream(static_cast<std::streambuf*>(this))
{
}
};
std::vector<char> extract_file(const std::string& path, const std::string& entry)
{
auto* apk = unzOpen(path.c_str());
if (apk == nullptr) {
throw std::runtime_error("not an APK file");
}
if (unzLocateFile(apk, entry.c_str(), 0) != UNZ_OK) {
unzClose(apk);
throw std::runtime_error(entry+" is not found in APK");
}
if (unzOpenCurrentFile(apk) != UNZ_OK) {
unzClose(apk);
throw std::runtime_error("failed to open "+entry+" in APK");
}
unz_file_info64 info;
if (unzGetCurrentFileInfo64(apk, &info, nullptr, 0, nullptr, 0, nullptr, 0)
!= UNZ_OK) {
unzCloseCurrentFile(apk);
unzClose(apk);
throw std::runtime_error("failed to open "+entry+" in APK");
}
std::vector<char> content(info.uncompressed_size);
constexpr size_t read_size = 1 << 15;
for (size_t offset = 0;; offset += read_size) {
int len = unzReadCurrentFile(apk, content.data() + offset, read_size);
if (len == 0) {
break;
}
else if (len < 0) {
unzCloseCurrentFile(apk);
unzClose(apk);
throw std::runtime_error("failed to read file "+entry+" in APK");
}
}
unzCloseCurrentFile(apk);
unzClose(apk);
return content;
}
void write_xml(const std::string& path, const boost_pt::ptree& pt)
{
// Construct the output stream.
std::ostream* os = &std::cout;
std::ofstream ofs;
if (!path.empty()) {
ofs.open(path);
os = &ofs;
}
// Write the ptree to the output.
{
#if BOOST_MAJOR_VERSION == 1 && BOOST_MINOR_VERSION < 56
boost_pt::xml_writer_settings<char> settings(' ', 4);
#else
boost_pt::xml_writer_settings<std::string> settings(' ', 4);
#endif
boost_pt::write_xml(*os, pt, settings);
}
}
void process_file(const std::string& inpath,
const std::string& entry,
const std::string& outpath)
{
// Property tree for storing the XML content.
boost_pt::ptree pt;
// Load the XML into ptree.
std::istream* is = &std::cin;
std::ifstream ifs;
if (!inpath.empty() && inpath != "-") {
ifs.open(inpath, std::ios::binary);
is = &ifs;
}
else {
//TODO Linux?
#ifdef PLATFORM_WINDOWS
_setmode(_fileno(stdin), _O_BINARY);
#endif
}
if (is->peek() == 'P') {
auto content = extract_file(inpath, entry);
imemstream ims(content.data(), content.size());
jitana::read_axml(ims, pt);
}
else if (is->peek() == 0x03) {
jitana::read_axml(*is, pt);
}
else {
boost_pt::read_xml(*is, pt, boost_pt::xml_parser::trim_whitespace);
}
// Write the tree as an XML file.
write_xml(outpath, pt);
}
int main(int argc, char** argv)
{
namespace po = boost::program_options;
// Declare command line argument options.
po::options_description desc(
"axmldec " AXMLDEC_VERSION " (" AXMLDEC_BUILD_TIMESTAMP ") Copyright (C) 2017 Yutaka Tsutano.\n\n"
"Usage: axmldec [-h] [-v] [INPUT] [ENTRY] [-o OUTPUT]\n"
"Decodes an AXML file, optionally inside an APK file.\n\nOptions");
desc.add_options()
("input,i", po::value<std::string>(), "Path to the input file.\nDefault: standard input")
("entry,e", po::value<std::string>(), "Entry name in the input file.\nDefault: AndroidManifest.xml")
("output,o", po::value<std::string>(), "Path to the output file.\nDefault: standard output")
("version,v", "Print version number.")
("help,h", "Show this help message and exit.");
po::positional_options_description p;
p.add("input", 1);
p.add("entry", 1);
try {
// Process the command line arguments.
po::variables_map vmap;
po::store(po::command_line_parser(argc, argv)
.options(desc)
.positional(p)
.run(),
vmap);
po::notify(vmap);
if (vmap.count("help")) {
// Print help and quit.
std::cout << desc;
return 0;
}
if (vmap.count("version")) {
// Print version and quit.
std::cout << AXMLDEC_VERSION << '\n';
return 0;
}
auto inpath = vmap.count("input")
? vmap["input"].as<std::string>()
: "";
auto entry = vmap.count("entry")
? vmap["entry"].as<std::string>()
: "AndroidManifest.xml";
auto outpath = vmap.count("output")
? vmap["output"].as<std::string>()
: "";
// Process the file.
process_file(inpath, entry, outpath);
}
catch (std::ios::failure& e) {
std::cerr << "error: failed to open the input file\n";
return 1;
}
catch (std::exception& e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
}