-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_payload.cpp
627 lines (581 loc) · 25.8 KB
/
build_payload.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*-*-c++-*-*************************************************************************************************************
* Copyright 2016 - 2022 Inesonic, LLC.
*
* This file is licensed under two licenses.
*
* Inesonic Commercial License, Version 1:
* All rights reserved. Inesonic, LLC retains all rights to this software, including the right to relicense the
* software in source or binary formats under different terms. Unauthorized use under the terms of this license is
* strictly prohibited.
*
* GNU Public License, Version 2:
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
********************************************************************************************************************//**
* \file
*
* This file provides a trivial executable that can read an input file, compress it, and emit it as a C++ compatible
* declaration.
***********************************************************************************************************************/
#include <QByteArray>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <algorithm>
#include <cstdio>
/**
* Function that parses a single input file and dumps its contents.
*
* \param[in] inputStream The input stream to be parsed.
*
* \param[in] outputStream The name of the output file. An empty string indicates stdout.
*
* \param[in] leftIndentation Additional left side indentation.
*
* \param[in] indentation The desired indentation in spaces.
*
* \param[in] width The desired maximum line width.
*
* \param[in] prefix An optional prefix in front of each variable name.
*
* \param[in] variableName The payload variable name or suffix.
*
* \param[in] variableType The variable type for the payload contents.
*
* \param[in] sizeVariableName The size variable name or suffix.
*
* \param[in] sizeVariableType The size variable type.
*
* \param[in] zlibCompress Flag holding true if we should run the payload through the Qt compressor.
*
* \return Returns true on success. Returns false on error.
*/
bool parseAndDumpInput(
std::istream& inputStream,
std::ostream& outputStream,
unsigned leftIndentation,
unsigned indentation,
unsigned width,
const std::string& prefix,
const std::string& variableName,
const std::string& variableType,
const std::string& sizeVariableName,
const std::string& sizeVariableType,
bool zlibCompress
) {
std::vector<unsigned char> inputBuffer;
while (inputStream) {
unsigned char readData;
inputStream.read(reinterpret_cast<char*>(&readData), 1);
inputBuffer.push_back(readData);
}
QByteArray byteArray(reinterpret_cast<const char*>(inputBuffer.data()), static_cast<int>(inputBuffer.size()));
QByteArray outputData = zlibCompress ? qCompress(byteArray, 9) : byteArray;
std::string leftIndentationString;
for (unsigned column=0 ; column<leftIndentation ; ++column) {
leftIndentationString += " ";
}
std::string contentsIndentationString;
for (unsigned column=0 ; column<(leftIndentation + indentation) ; ++column) {
contentsIndentationString += " ";
}
unsigned long numberBytes = outputData.size();
outputStream << leftIndentationString << variableType << " "
<< prefix << variableName << "[" << numberBytes << "] = {";
unsigned valuesPerLine = (width - indentation - leftIndentation + 1) / 6;
unsigned valuesThisLine = valuesPerLine;
for (unsigned long i=0 ; i<numberBytes ; ++i) {
if (valuesThisLine >= valuesPerLine) {
outputStream << std::endl << contentsIndentationString;
valuesThisLine = 1;
} else {
++valuesThisLine;
}
unsigned char v = outputData.at(i);
char buffer[6];
sprintf(buffer, "0x%02X", static_cast<unsigned>(v));
outputStream << buffer;
if (i < (numberBytes - 1)) {
outputStream << ", ";
}
}
outputStream << std::endl
<< leftIndentationString << "};" << std::endl
<< std::endl
<< leftIndentationString << sizeVariableType << " " << prefix << sizeVariableName
<< " = " << numberBytes << ";" << std::endl
<< std::endl;
return true;
}
/**
* Function that performs the work of building a payload from one or more input files.
*
* \param[in] inputs The list of input files. An empty list indicates stdin.
*
* \param[in] outputStream The stream to receive the generated output.
*
* \param[in] description An optional description to place below the copyright message.
*
* \param[in] copyrightMessage The copyright message to be included.
*
* \param[in] noCopyrightMessage A flag holding true if no copyright should be included.
*
* \param[in] indentation The desired indentation in spaces.
*
* \param[in] width The desired maximum line width.
*
* \param[in] namespaceName An optional namespace to enacapsulate the payload in.
*
* \param[in] variableName The payload variable name or suffix.
*
* \param[in] variableType The variable type for the payload contents.
*
* \param[in] sizeVariableName The size variable name or suffix.
*
* \param[in] sizeVariableType The size variable type.
*
* \param[in] zlibCompress Flag holding true if we should run the payload through the Qt compressor.
*
* \return Returns true on success. Returns false on error.
*/
bool buildPayloadHelper(
const std::vector<std::string>& inputs,
std::ostream& outputStream,
const std::string& description,
const std::string& copyrightMessage,
bool noCopyrightMessage,
unsigned indentation,
unsigned width,
const std::string& namespaceName,
const std::string& variableName,
const std::string& variableType,
const std::string& sizeVariableName,
const std::string& sizeVariableType,
bool zlibCompress
) {
bool success = true;
if (!noCopyrightMessage || !description.empty()) {
outputStream << "/*-*-c++-*-*";
for (unsigned column=13 ; column<=width ; ++column) {
outputStream << "*";
}
outputStream << std::endl;
if (!noCopyrightMessage) {
std::istringstream copyrightMessageStream(copyrightMessage);
std::string copyrightLine;
while (std::getline(copyrightMessageStream, copyrightLine)) {
outputStream << "* " << copyrightLine << std::endl;
}
}
if (!noCopyrightMessage && !description.empty()) {
for (unsigned column=1 ; column<=(width-4) ; ++column) {
outputStream << "*";
}
outputStream << "//**" << std::endl;
}
if (!description.empty()) {
outputStream << "* \\file" << std::endl
<< "*" << std::endl;
std::istringstream descriptionStream(description);
std::string descriptionLine;
while (std::getline(descriptionStream, descriptionLine)) {
outputStream << "* " << descriptionLine << std::endl;
}
}
for (unsigned column=1 ; column<=(width-1) ; ++column) {
outputStream << "*";
}
outputStream << "/" << std::endl
<< std::endl;
}
unsigned leftIndentation = 0;
if (!namespaceName.empty()) {
outputStream << "namespace " << namespaceName << "{" << std::endl;
leftIndentation = indentation;
}
if (inputs.empty()) {
success = parseAndDumpInput(
std::cin,
outputStream,
leftIndentation,
indentation,
width,
"",
variableName,
variableType,
sizeVariableName,
sizeVariableType,
zlibCompress
);
} else {
if (inputs.size() == 1) {
std::ifstream inputStream(inputs.at(0), std::ios::binary);
if (inputStream) {
success = parseAndDumpInput(
inputStream,
outputStream,
leftIndentation,
indentation,
width,
"",
variableName,
variableType,
sizeVariableName,
sizeVariableType,
zlibCompress
);
inputStream.close();
} else {
std::cerr << "*** Could not open input file " << inputs.at(0) << std::endl;
success = false;
}
} else {
std::vector<std::string>::const_iterator inputIterator = inputs.cbegin();
std::vector<std::string>::const_iterator inputEndIterator = inputs.cend();
while (success && inputIterator != inputEndIterator) {
std::string inputFilename = *inputIterator;
std::ifstream inputStream(inputFilename, std::ios::binary);
if (inputStream) {
std::size_t forwardSlashPosition = inputFilename.rfind('/');
std::size_t backslashPosition = inputFilename.rfind('\\');
std::string prefix;
if (forwardSlashPosition != std::string::npos) {
if (backslashPosition != std::string::npos) {
std::size_t slashPosition = std::max(forwardSlashPosition, backslashPosition);
prefix = inputFilename.substr(slashPosition + 1);
} else {
prefix = inputFilename.substr(forwardSlashPosition + 1);
}
} else {
if (backslashPosition != std::string::npos) {
prefix = inputFilename.substr(backslashPosition + 1);
} else {
prefix = inputFilename;
}
}
std::replace(prefix.begin(), prefix.end(), '.', '_');
outputStream << "// Contents of " << inputFilename << ":" << std::endl;
success = parseAndDumpInput(
inputStream,
outputStream,
leftIndentation,
indentation,
width,
prefix,
variableName,
variableType,
sizeVariableName,
sizeVariableType,
zlibCompress
);
inputStream.close();
++inputIterator;
} else {
std::cerr << "*** Could not open input file " << inputs.at(0) << std::endl;
success = false;
}
}
}
}
return success;
}
/**
* Function that performs the work of building a payload from one or more input files.
*
* \param[in] inputs The list of input files. An empty list indicates stdin.
*
* \param[in] outputFilename The name of the output file. An empty string indicates stdout.
*
* \param[in] description An optional description to place below the copyright message.
*
* \param[in] copyrightMessage The copyright message to be included.
*
* \param[in] noCopyrightMessage A flag holding true if no copyright should be included.
*
* \param[in] indentation The desired indentation in spaces.
*
* \param[in] width The desired maximum line width.
*
* \param[in] namespaceName An optional namespace to enacapsulate the payload in.
*
* \param[in] variableName The payload variable name or suffix.
*
* \param[in] variableType The variable type for the payload contents.
*
* \param[in] sizeVariableName The size variable name or suffix.
*
* \param[in] sizeVariableType The size variable type.
*
* \param[in] zlibCompress Flag holding true if we should run the payload through the Qt compressor.
*
* \return Returns true on success. Returns false on error.
*/
bool buildPayload(
const std::vector<std::string>& inputs,
const std::string& outputFilename,
const std::string& description,
const std::string& copyrightMessage,
bool noCopyrightMessage,
unsigned indentation,
unsigned width,
const std::string& namespaceName,
const std::string& variableName,
const std::string& variableType,
const std::string& sizeVariableName,
const std::string& sizeVariableType,
bool zlibCompress
) {
bool success;
if (outputFilename.empty()) {
success = buildPayloadHelper(
inputs,
std::cout,
description,
copyrightMessage,
noCopyrightMessage,
indentation,
width,
namespaceName,
variableName,
variableType,
sizeVariableName,
sizeVariableType,
zlibCompress
);
} else {
std::ofstream outputStream(outputFilename);
if (outputStream) {
success = buildPayloadHelper(
inputs,
outputStream,
description,
copyrightMessage,
noCopyrightMessage,
indentation,
width,
namespaceName,
variableName,
variableType,
sizeVariableName,
sizeVariableType,
zlibCompress
);
outputStream.close();
} else {
std::cerr << "*** Could not open output file " << outputFilename << "." << std::endl;
success = false;
}
}
return success;
}
int main(int argumentCount, char* argumentValues[]) {
bool success = true;
bool helpRequested = false;
std::string description;
std::string outputFilename;
std::string copyrightMessage = "Copyright 2020 Inesonic, LLC.\nAll rights reserved.";
bool removeCopyright = false;
unsigned indentation = 4;
unsigned width = 120;
std::string namespaceName;
std::string variableName = "declarations";
std::string variableType = "static const unsigned char";
std::string sizeVariableName = "declarationsSize";
std::string sizeVariableType = "static const unsigned long";
bool useZlib = true;
std::vector<std::string> inputs;
unsigned argumentIndex = 1;
while (success && !helpRequested && argumentIndex < static_cast<unsigned>(argumentCount)) {
std::string argument(argumentValues[argumentIndex]);
unsigned remainingArguments = argumentCount - argumentIndex - 1;
if (argument == "-h" || argument == "--help") {
helpRequested = true;
} else if (argument == "-o" || argument == "--output") {
if (remainingArguments > 0) {
++argumentIndex;
outputFilename = argumentValues[argumentIndex];
} else {
std::cerr << "*** The " << argument << " switch is missing a parameter." << std::endl;
success = false;
}
} else if (argument == "-c" || argument == "--copyright") {
if (remainingArguments > 0) {
++argumentIndex;
copyrightMessage = argumentValues[argumentIndex];
} else {
std::cerr << "*** The " << argument << " switch is missing a parameter." << std::endl;
success = false;
}
} else if (argument == "-C" || argument == "--no-copyright") {
removeCopyright = true;
} else if (argument == "-i" || argument == "--indentation") {
if (remainingArguments > 0) {
++argumentIndex;
indentation = strtoul(argumentValues[argumentIndex], nullptr, 10);
if (indentation <= 0) {
std::cerr << "*** Invalid indentation value " << argumentValues[argumentIndex] << std::endl;
success = false;
}
} else {
std::cerr << "*** The " << argument << " switch is missing a parameter." << std::endl;
success = false;
}
} else if (argument == "-w" || argument == "--width") {
if (remainingArguments > 0) {
++argumentIndex;
width = strtoul(argumentValues[argumentIndex], nullptr, 10);
if (indentation <= 0) {
std::cerr << "*** Invalid width value " << argumentValues[argumentIndex] << std::endl;
success = false;
}
} else {
std::cerr << "*** The " << argument << " switch is missing a parameter." << std::endl;
success = false;
}
} else if (argument == "-n" || argument == "--namespace") {
if (remainingArguments > 0) {
++argumentIndex;
namespaceName = argumentValues[argumentIndex];
} else {
std::cerr << "*** The " << argument << " switch is missing a parameter." << std::endl;
success = false;
}
} else if (argument == "-v" || argument == "--variable") {
if (remainingArguments > 0) {
++argumentIndex;
variableName = argumentValues[argumentIndex];
} else {
std::cerr << "*** The " << argument << " switch is missing a parameter." << std::endl;
success = false;
}
} else if (argument == "-t" || argument == "--type") {
if (remainingArguments > 0) {
++argumentIndex;
variableType = argumentValues[argumentIndex];
} else {
std::cerr << "*** The " << argument << " switch is missing a parameter." << std::endl;
success = false;
}
} else if (argument == "-V" || argument == "--size-variable") {
if (remainingArguments > 0) {
++argumentIndex;
sizeVariableName = argumentValues[argumentIndex];
} else {
std::cerr << "*** The " << argument << " switch is missing a parameter." << std::endl;
success = false;
}
} else if (argument == "-T" || argument == "--size-type") {
if (remainingArguments > 0) {
++argumentIndex;
sizeVariableType = argumentValues[argumentIndex];
} else {
std::cerr << "*** The " << argument << " switch is missing a parameter." << std::endl;
success = false;
}
} else if (argument == "-z" || argument == "--zlib") {
useZlib = true;
} else if (argument == "-Z" || argument == "--no-zlib") {
useZlib = false;
} else {
inputs.push_back(argument);
}
++argumentIndex;
}
if (helpRequested) {
std::cout << "Copyright 2020 Inesonic, LLC" << std::endl
<< "This software is licensed under two terms:" << std::endl
<< " * The Inesonic Commercial License, Version 1" << std::endl
<< " * GNU Public License, Version 2" << std::endl
<< std::endl
<< "You can use this utility to convert a file, in raw binary form, to an C99 or" << std::endl
<< "C++ array suitable for inclusion within a program. You can use the program" << std::endl
<< "to generate binary payloads of files." << std::endl
<< std::endl
<< "Command:" << std::endl
<< " build_payload [options] [ file [ file [ file ... ] ] ]" << std::endl
<< std::endl
<< " -h | --help" << std::endl
<< " Display this help text, then exit. All other switches are ignored." << std::endl
<< std::endl
<< " --o <filename> | --output <filename>" << std::endl
<< " Specifies the name of the output file. Output will be sent to stdout if" << std::endl
<< " this switch is not provided." << std::endl
<< std::endl
<< " -c <message> | --copyright <message>" << std::endl
<< " Sets the displayed copyright message. A standard copyright message will" << std::endl
<< " be included if this switch is not used." << std::endl
<< std::endl
<< " -C | --no-copyright" << std::endl
<< " Removes all copyright messages from the output. Using this switch will" << std::endl
<< " cause the -c/--copyright switch to be ignored." << std::endl
<< std::endl
<< " -i <indentation> | --indentation <indentation>" << std::endl
<< " Specifies the indentation to use when formatting the output. This tool" << std::endl
<< " will use an indentation of 4 spaces by default." << std::endl
<< std::endl
<< " -w <width> | --width <width>" << std::endl
<< " Specifies the maximum line length. The value is ignored when inserting" << std::endl
<< " the description and copyright messages." << std::endl
<< std::endl
<< " -n <namespace> | --namespace <namespace>" << std::endl
<< " Specifies an optional namespace to place the generated content under." << std::endl
<< std::endl
<< " -v <variable|suffix> | --variable <variable|suffix>" << std::endl
<< " Specifies the variable name to use for the payload. If multiple input" << std::endl
<< " files are provided, then this switch specifies a suffix to append to" << std::endl
<< " the generated payloads. Payload names will be based on the supplied" << std::endl
<< " filenames. The default value is \"declarations\"" << std::endl
<< std::endl
<< " -t <variable type> | --type <variable type>" << std::endl
<< " Specifies the type to assign to the payload array. The default value" << std::endl
<< " is \"static const unsigned char\"." << std::endl
<< std::endl
<< " -V <variable|suffix> | --size-variable <variable|suffix>" << std::endl
<< " Specifies the variable name to use for the payload size. If multiple" << std::endl
<< " input files are provided, then this switch specifies a suffix to append" << std::endl
<< " to the generated payload sizes. Payload names will be based on the " << std::endl
<< " supplied filenames. The default value is \"declarationsSize\"" << std::endl
<< std::endl
<< " -T <variable type> | --size-type <variable type<" << std::endl
<< " Specifies the type to assign to the payload size. The default value" << std::endl
<< " is \"static const unsigned long\"." << std::endl
<< std::endl
<< " -z | --zlib" << std::endl
<< " Indicates that the generated payload should be compressed using Qt's" << std::endl
<< " funky variant of the zlib compression algorithm. This is the default" << std::endl
<< " behavior." << std::endl
<< std::endl
<< " -Z | --no-zlib" << std::endl
<< " Indicates that the generated payload should not be compressed using" << std::endl
<< " Qt's variant of the zlib compression algorithm." << std::endl;
} else if (success) {
success = buildPayload(
inputs,
outputFilename,
description,
copyrightMessage,
removeCopyright,
indentation,
width,
namespaceName,
variableName,
variableType,
sizeVariableName,
sizeVariableType,
useZlib
);
}
return success ? 0 : 1;
}