-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
298 lines (265 loc) · 10.7 KB
/
main.cpp
File metadata and controls
298 lines (265 loc) · 10.7 KB
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
#include "speck.hpp"
#include <algorithm>
#include <cargs.h>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <iostream>
static struct cag_option options[] = {
{.identifier = 'a', .access_letters = NULL, .access_name = "alloc-size", .value_name = "BYTES", .description = "Minimum number of bytes to allocate, when more memory needs to be allocated."},
{.identifier = 'p', .access_letters = "a", .access_name = "append", .description = "When flag is set, speckage will be appended to the end of the file specified by out or appended speckages will be read, when r is set."},
{.identifier = 'r', .access_letters = "r", .access_name = "read", .value_name = "FILE_PATH", .description = "Path to speck file to read"},
{.identifier = 'i', .access_letters = "i", .access_name = "input", .value_name = "FILE_PATH", .description = "Path to file to speck. Ignored, if -r is set."},
{.identifier = 'd', .access_letters = "d", .access_name = "input_dir", .value_name = "DIRECTORY_PATH", .description = "Path to directory to speck. Ignored, if -r is set."},
{.identifier = 'o', .access_letters = "o", .access_name = "out", .value_name = "FILENAME", .description = "Output path. If -r is set, path to directory to unspeck the files."},
{.identifier = 'b', .access_letters = "b", .access_name = "backslash", .description = "forces all slashes in saved filename to be backslashes."},
{.identifier = 'f', .access_letters = "f", .access_name = "forward_slash", .description = "forces all slashes in saved filename to be forward slashes."},
{.identifier = 'h', .access_letters = "h", .access_name = "help", .description = "Shows the command help"},
{.identifier = 'n', .access_letters = "n", .access_name = "name", .value_name = "NAME", .description = "Name to give the speckage. Only used when creating a speckage. NOTE: this is not the output filename, see -o."},
{.identifier = 's',
.access_letters = "s",
.access_name = "skip_beginning",
.description = "When appending, skips adding an empty header before the appended speckages. Use this when appending to a file that already has appended speckages."}
};
speck::speckage read_speckage(std::filesystem::path input)
{
speck::speckage speckage = speck::read_speckage_from_file(input.string());
if (speckage.data_size == 0)
{
std::printf("[ERROR] Invalid speckage");
return speckage;
}
std::string filename_label = "filename";
uint16_t max_name_length = filename_label.length();
for (auto mapentry : speckage.file_info)
{
max_name_length = std::max(max_name_length, (uint16_t)mapentry.first.length());
}
std::printf("%-*s | %s\n", max_name_length, filename_label.c_str(), "size");
for (const auto mapentry : speckage.file_info)
{
std::printf("%-*s | 0x%x\n", max_name_length, mapentry.first.c_str(), mapentry.second.second);
}
return speckage;
}
void unspeck(const speck::speckage& speckage, std::filesystem::path output_dir)
{
std::printf("\n\nUnspecking into %s...\n", output_dir.c_str());
for (const auto file_info : speckage.file_info)
{
uint64_t size;
char* location = speck::read_file_from_speckage(speckage, file_info.first, size);
auto save_file = output_dir / std::filesystem::path(file_info.first);
std::printf("\t-%s\n", save_file.c_str());
if (!std::filesystem::exists(save_file.parent_path()))
{
std::filesystem::create_directories(save_file.parent_path());
std::printf("\t\t-created directory %s\n", save_file.parent_path().c_str());
}
std::ofstream file(save_file, std::ios::out | std::ios::binary | std::ios::trunc);
file.write(location, size);
file.close();
}
}
int main(int argc, char* argv[])
{
std::vector<std::filesystem::path> input_files;
std::vector<std::filesystem::path> input_directories;
std::string out_speckage_name = "";
bool output_is_set = false;
std::filesystem::path output_file;
bool should_append = false;
bool skip_append_security_footer = false;
bool read_mode = false;
std::filesystem::path read_file;
uint64_t min_expand = 0;
bool force_forward_slash = false;
bool force_backslash = false;
cag_option_context context;
cag_option_init(&context, options, CAG_ARRAY_SIZE(options), argc, argv);
if (argc == 1)
{
printf("Usage: speck [OPTION]...\n");
printf("Creates speckages.\n\n");
cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
return EXIT_SUCCESS;
}
while (cag_option_fetch(&context))
{
switch (cag_option_get_identifier(&context))
{
case 'a': {
char* end = nullptr;
min_expand = std::strtoull(cag_option_get_value(&context), &end, 10);
}
break;
case 'p': {
should_append = true;
break;
}
case 's': {
skip_append_security_footer = true;
break;
}
case 'r':
if (read_mode)
{
std::cout << "[ERROR] Only one read file may be set." << std::endl;
return EXIT_FAILURE;
}
read_mode = true;
read_file = cag_option_get_value(&context);
break;
case 'i':
input_files.emplace_back(cag_option_get_value(&context));
break;
case 'd':
input_directories.emplace_back(cag_option_get_value(&context));
break;
case 'o':
if (output_is_set)
{
std::cout << "[ERROR] Only one output may be set." << std::endl;
return EXIT_FAILURE;
}
output_is_set = true;
output_file = cag_option_get_value(&context);
break;
case 'f':
if (force_backslash)
{
std::cout << "[ERROR] Only one slash style may be set." << std::endl;
return EXIT_FAILURE;
}
force_forward_slash = true;
break;
case 'b':
if (force_forward_slash)
{
std::cout << "[ERROR] Only one slash style may be set." << std::endl;
return EXIT_FAILURE;
}
force_backslash = true;
break;
case 'n':
out_speckage_name = cag_option_get_value(&context);
break;
case 'h':
printf("Usage: speck [OPTION]...\n");
printf("Creates speckages.\n\n");
cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
return EXIT_SUCCESS;
case '?':
cag_option_print_error(&context, stdout);
break;
}
}
// read speckage to output
if (read_mode)
{
if (!should_append)
{
// single speckage file
auto speckage = read_speckage(read_file);
if (output_is_set)
{
unspeck(speckage, output_file);
}
speck::unload_speckage(speckage);
return EXIT_SUCCESS;
} else
{
// appended speckages
auto speckages_infos = speck::discover_appended_speckages(read_file.string());
std::vector<std::string> speckage_names;
printf("found the following %i speckages:\n", speckages_infos.speckage_offset_map.size());
for (const auto& [key, val] : speckages_infos.speckage_offset_map)
{
printf("\t-%s\n", key.c_str());
speckage_names.push_back(key);
}
auto speckages = speck::read_appended_speckages_from_file(speckage_names, speckages_infos);
if (output_is_set)
{
for (const auto& speckage : speckages)
{
unspeck(speckage, output_file / speckage.name);
}
}
return EXIT_SUCCESS;
}
}
// gather all files in a vector
for (const auto directory : input_directories)
{
if (!std::filesystem::is_directory(directory))
{
std::cout << "[ERROR] -d " << directory.string() << " needs to be a directory" << std::endl;
return EXIT_FAILURE;
}
std::filesystem::recursive_directory_iterator it(directory, std::filesystem::directory_options::follow_directory_symlink);
for (const auto& entry : it)
{
if (entry.is_directory())
{
continue;
}
input_files.push_back(entry);
}
}
if (input_files.empty())
{
std::cout << "[ERROR] no input files were supplied" << std::endl;
return EXIT_FAILURE;
}
// specking mode
if (!should_append)
{
speck::speckage speckage;
speckage.min_memory_on_expand = min_expand;
if (out_speckage_name.empty())
{
if (!input_directories.empty())
{
out_speckage_name = input_directories[0].string();
} else if (!input_files.empty())
{
out_speckage_name = input_files[0].string();
}
}
speckage.name = out_speckage_name;
for (const auto filepath : input_files)
{
std::string path = filepath.string();
if (force_backslash)
std::ranges::replace(path, '/', '\\');
if (force_forward_slash)
std::ranges::replace(path, '\\', '/');
if (!speck::add_file_to_speckage(speckage, path))
{
std::cout << "[ERROR] " << path << " could not be specked" << std::endl;
return EXIT_FAILURE;
}
}
speck::save_speckage_to_file(speckage, output_file.string());
return EXIT_SUCCESS;
}
// append mode
if (should_append)
{
std::vector<speck::speckage> speckages;
for (const auto& file : input_files)
{
speck::speckage read = speck::read_speckage_from_file(file.string());
if (read.data_size > 0)
{
speckages.push_back(read);
}
}
if (!speck::append_speckages_to_file(speckages, output_file.string(), !skip_append_security_footer))
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
}