-
Notifications
You must be signed in to change notification settings - Fork 10
/
dvcid.cpp
451 lines (389 loc) · 10.4 KB
/
dvcid.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include <iostream>
#include <fstream>
#include <string>
#include <deque>
#include <map>
#include <cstdlib>
#include <getopt.h>
#include "dvcid.hpp"
namespace {
static std::deque<ParsedLine> parsed_XML_SG_;
static std::deque<std::string> orign_XML_SG_;
}
int
main(int argc, char* argv[])
{
Options opts = get_options(argc, argv);
if (opts.help)
{ help_information("", 0); }
/* At least one action needs to be taken */
if (!(opts.query_default || opts.query_current || opts.assign_default ||
opts.assign_current || opts.backup || opts.restore))
{ help_information("invalid option combination: nothing to do: ", 64); }
/* Only one ID can be queried at a time */
if (opts.query_default && opts.query_current)
{
help_information
("invalid option combination: querying more than one IDs: ", 64);
}
/* Backup, restore cannot be performed with query */
if (
(opts.backup || opts.restore) &&
(opts.query_default || opts.query_current)
)
{ help_information("invalid option combination: ", 64); }
/* Cannot restore while assigning */
if (opts.restore && (opts.assign_current || opts.assign_default))
{ help_information("invalid option combination: ", 64); }
/* Cannot query and assign at one time */
if (
(opts.query_current || opts.query_default) &&
(opts.assign_current || opts.assign_default)
)
{ help_information("invalid option combination: ", 64); }
/* Action combination is safe here, option combination still needs to check */
/* Querying */
if (opts.query_current || opts.query_default)
{
parse_file(opts.xml_file);
if (opts.inplace)
{ help_information("invalid option combination: ", 64); }
if (opts.package_name.length() == 0)
{ help_information("package name not spefified: ", 64); }
std::string id = query(opts.package_name, opts.query_current);
if (id.length() != 0)
{ std::cout << id << std::endl; }
}
/* Backing up */
if (opts.backup)
{ copy_file(opts.xml_file, opts.backup_file); }
/* Assigning */
if (opts.assign_current || opts.assign_default)
{
parse_file(opts.xml_file);
if (opts.package_name.length() == 0)
{ help_information("package name not spefified: ", 64); }
if (opts.assign_default)
{
if (!opts.force)
{ assign_safe_guard(opts.package_name, opts.default_id); }
assign(opts.package_name, opts.default_id, false);
}
if (opts.assign_current)
{
if (!opts.force)
{ assign_safe_guard(opts.package_name, opts.current_id); }
assign(opts.package_name, opts.current_id, true);
}
}
/* Writing back */
if (opts.assign_current || opts.assign_default)
{
if (opts.inplace)
{ write_back(get_final(), opts.xml_file); }
else
{ print_out(get_final()); }
}
if (opts.restore)
{ copy_file(opts.restore_file, opts.xml_file); }
return 0;
}
namespace {
std::string
parse_key(std::size_t p, const std::string& line)
{
std::string parsed_key;
bool found_key = false;
for (p = p - 1; p != 0; --p)
{
if (line[p] != ' ')
{
found_key = true;
parsed_key.insert(parsed_key.begin(), line[p]);
}
else if (found_key)
{ break; }
}
return parsed_key;
}
std::string
parse_value (std::size_t p, const std::string& line)
{
std::string parsed_value;
bool found_value = false;
for (p = p + 2; p < line.length(); ++p)
{
if (line[p] != '"')
{
found_value = true;
parsed_value.push_back(line[p]);
}
else if (found_value)
{ break; }
}
return parsed_value;
}
ParsedLine
parse_line(const std::string& line)
{
std::map<std::string, std::string> records;
for (std::size_t i = 0; i < line.length(); ++i)
{
if (line[i] == '=')
{
std::string key = ::parse_key(i, line);
std::string value = ::parse_value(i, line);
records[key] = value;
}
}
return ParsedLine
(
records["id"], records["name"], records["value"], records["package"],
records["defaultValue"], records["defaultSysSet"], records["tag"]
);
}
std::string
rewrite_line(const ParsedLine& parsed_line)
{
return std::string()
.append(" <setting ")
.append("id=\"") .append(parsed_line.id)
.append("\" name=\"") .append(parsed_line.name)
.append("\" value=\"") .append(parsed_line.value)
.append("\" package=\"") .append(parsed_line.package)
.append("\" defaultValue=\"") .append(parsed_line.default_value)
.append("\" defaultSysSet=\"").append(parsed_line.default_sys_set)
.append("\" tag=\"") .append(parsed_line.tag)
.append("\" />");
}
bool
is_valid_device_id(const std::string& str)
{
if (str.length() != 16)
{ return false; }
for (const auto& c : str)
{
if (c < '0' || (c > '9' && c < 'a') || c > 'f')
{ return false; }
}
return true;
}
bool
is_valid_userkey(const std::string& str)
{
if (str.length() != 64)
{ return false; }
for (auto it = str.cbegin(); it != str.cend(); ++it)
{
if (
(*it > '9' && *it < 'A') ||
(*it < '0' || static_cast<int>(*it) > 'F')
)
{ return false; }
}
return true;
}
}
void
parse_file(const std::string& xml_file)
{
std::ifstream input_stream(xml_file);
if (input_stream.fail())
{
std::cerr << "cannot open input: " << xml_file << std::endl;
exit(66);
}
for (std::string line; std::getline(input_stream, line); )
{
::parsed_XML_SG_.push_back(::parse_line(line));
::orign_XML_SG_.push_back(line);
}
input_stream.close();
}
std::string
get_final(bool&& committed)
{
std::string final_copy;
for (std::size_t i = 0; i < ::orign_XML_SG_.size(); ++i)
{
final_copy.append
(
(::parsed_XML_SG_[i].is_crrupted() || !committed) ?
::orign_XML_SG_[i] : ::rewrite_line(::parsed_XML_SG_[i])
)
.append("\n");
}
return final_copy;
}
void
write_back(const std::string& final_copy, const std::string& xml_file)
{
std::ofstream output_stream(xml_file);
if (output_stream.fail())
{
std::cerr << "can't create (user) output file: " << xml_file << std::endl;
exit(73);
}
output_stream << final_copy;
output_stream.close();
}
void
write_back(std::string&& final_copy, const std::string& xml_file)
{
std::ofstream output_stream(xml_file);
if (output_stream.fail())
{
std::cerr << "can't create (user) output file: " << xml_file << std::endl;
exit(73);
}
output_stream << final_copy;
output_stream.close();
}
void
print_out(const std::string& final_copy)
{
std::cout << final_copy;
}
void
print_out(std::string&& final_copy)
{
std::cout << final_copy;
}
std::string
query(const std::string& package_name, const bool& which)
{
for (auto const& parsed_line : ::parsed_XML_SG_)
{
if (parsed_line.package == package_name)
{ return (which ? parsed_line.default_value : parsed_line.value); }
}
return std::string();
}
void
assign_safe_guard(const std::string& package_name, const std::string& device_id)
{
if (package_name == "android")
{
if (!::is_valid_userkey(device_id))
{
std::cerr
<< "the designated ID is NOT a valid userkey: "
<< package_name
<< std::endl
<< "use --force to perform anyway"
<< std::endl;
}
std::cerr
<< "changing ID of this package will cause system wide ID changes: "
<< package_name
<< std::endl
<< "use --force to perform anyway"
<< std::endl;
exit(1);
}
else if (!::is_valid_device_id(device_id))
{
std::cerr
<< "the designated ID is NOT valid: "
<< device_id
<< std::endl
<< "use --force to perform anyway"
<< std::endl;
exit(1);
}
}
void
assign
(
const std::string& package_name,
const std::string& device_id,
const bool&& which
)
{
for (auto& parsed_line : ::parsed_XML_SG_)
{
if (parsed_line.package == package_name)
{
which ?
parsed_line.default_value = device_id :
parsed_line.value = device_id;
}
}
}
void
copy_file(const std::string& src_path, const std::string& dst_path)
{
std::ifstream inpt_stream(src_path, std::ios::binary);
std::ofstream oupt_stream(dst_path, std::ios::binary);
if (inpt_stream.fail() || oupt_stream.fail())
{
if (inpt_stream.fail())
{ std::cerr << "cannot open input: " << src_path << std::endl; }
if (oupt_stream.fail())
{ std::cerr << "cannot open output: " << dst_path << std::endl; }
exit(73);
}
oupt_stream << inpt_stream.rdbuf();
inpt_stream.close();
oupt_stream.close();
}
void
help_information(std::string&& exit_info, int&& error_code)
{
if (exit_info.length() > 0)
{ std::cout << exit_info << error_code << std::endl << std::endl; }
std::cout << ___HELP_INFO___;
exit(error_code);
}
Options
get_options (int& argc, char** (&argv))
{
int c;
Options options;
while ((c = getopt_long(argc, argv, opts_SG_, long_opts_SG_, nullptr)) != -1)
{
switch (c)
{
case 'a':
options.assign_default = true;
options.default_id = optarg;
break;
case 'A':
options.assign_current = true;
options.current_id = optarg;
break;
case 'q':
options.query_default = true;
break;
case 'Q':
options.query_current = true;
break;
case 'b':
options.backup = true;
options.backup_file = optarg;
break;
case 'r':
options.restore = true;
options.restore_file = optarg;
break;
case 'p':
options.package_name = optarg;
break;
case 'f':
options.xml_file = optarg;
break;
case 'i':
options.inplace = true;
break;
case 'y':
options.force = true;
break;
case 'h':
options.help = true;
break;
default:
help_information("", 63);
}
}
return options;
}