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
|
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************
split.c
Simple file splitter/joiner with hashes.
****************************************************************************/
#include "corefile.h"
#include "corestr.h"
#include "hashing.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cassert>
#define DEFAULT_SPLIT_SIZE 100
#define MAX_PARTS 1000
#define SHA1_DIGEST_SIZE 20
/***************************************************************************
CORE IMPLEMENTATION
***************************************************************************/
/*-------------------------------------------------
compute_hash_as_string - compute an SHA1
hash over a buffer and return a string
-------------------------------------------------*/
static void compute_hash_as_string(std::string &buffer, void *data, uint32_t length)
{
// compute the SHA1
util::sha1_creator sha1;
sha1.append(data, length);
const util::sha1_t sha1digest = sha1.finish();
// expand the digest to a string
char expanded[sizeof(sha1digest.m_raw) * 2];
for (int ch = 0; ch < sizeof(sha1digest.m_raw); ch++)
{
expanded[ch * 2 + 0] = "0123456789ABCDEF"[sha1digest.m_raw[ch] >> 4];
expanded[ch * 2 + 1] = "0123456789ABCDEF"[sha1digest.m_raw[ch] & 15];
}
// copy it to the buffer
buffer.assign(expanded, sizeof(expanded));
}
/*-------------------------------------------------
split_file - split a file into multiple parts
-------------------------------------------------*/
static int split_file(const char *filename, const char *basename, uint32_t splitsize)
{
std::string outfilename, basefilename, splitfilename;
util::core_file::ptr outfile, infile, splitfile;
std::string computedhash;
void *splitbuffer = nullptr;
int index, partnum;
uint64_t totallength;
osd_file::error filerr;
int error = 1;
// convert split size to MB
if (splitsize > 500)
{
fprintf(stderr, "Fatal error: maximum split size is 500MB (even that is way huge!)\n");
goto cleanup;
}
splitsize *= 1024 * 1024;
// open the file for read
filerr = util::core_file::open(filename, OPEN_FLAG_READ, infile);
if (filerr != osd_file::error::NONE)
{
fprintf(stderr, "Fatal error: unable to open file '%s'\n", filename);
goto cleanup;
}
// get the total length
totallength = infile->size();
if (totallength < splitsize)
{
fprintf(stderr, "Fatal error: file is smaller than the split size\n");
goto cleanup;
}
if ((uint64_t)splitsize * MAX_PARTS < totallength)
{
fprintf(stderr, "Fatal error: too many splits (maximum is %d)\n", MAX_PARTS);
goto cleanup;
}
// allocate a buffer for reading
splitbuffer = malloc(splitsize);
if (splitbuffer == nullptr)
{
fprintf(stderr, "Fatal error: unable to allocate memory for the split\n");
goto cleanup;
}
// find the base name of the file
basefilename.assign(basename);
index = basefilename.find_last_of(PATH_SEPARATOR[0]);
if (index != -1)
basefilename.erase(0, index + 1);
// compute the split filename
splitfilename.assign(basename).append(".split");
// create the split file
filerr = util::core_file::open(splitfilename, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_NO_BOM, splitfile);
if (filerr != osd_file::error::NONE)
{
fprintf(stderr, "Fatal error: unable to create split file '%s'\n", splitfilename.c_str());
goto cleanup;
}
// write the basics out
splitfile->printf("splitfile=%s\n", basefilename.c_str());
splitfile->printf("splitsize=%d\n", splitsize);
printf("Split file is '%s'\n", splitfilename.c_str());
printf("Splitting file %s into chunks of %dMB...\n", basefilename.c_str(), splitsize / (1024 * 1024));
// now iterate until done
for (partnum = 0; partnum < 1000; partnum++)
{
uint32_t actual, length;
printf("Reading part %d...", partnum);
// read as much as we can from the file
length = infile->read(splitbuffer, splitsize);
if (length == 0)
break;
// hash what we have
compute_hash_as_string(computedhash, splitbuffer, length);
// write that info to the split file
splitfile->printf("hash=%s file=%s.%03d\n", computedhash.c_str(), basefilename.c_str(), partnum);
// compute the full filename for this guy
outfilename = util::string_format("%s.%03d", basename, partnum);
// create it
filerr = util::core_file::open(outfilename, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, outfile);
if (filerr != osd_file::error::NONE)
{
printf("\n");
fprintf(stderr, "Fatal error: unable to create output file '%s'\n", outfilename.c_str());
goto cleanup;
}
printf(" writing %s.%03d...", basefilename.c_str(), partnum);
// write the data
actual = outfile->write(splitbuffer, length);
if (actual != length)
{
printf("\n");
fprintf(stderr, "Fatal error: Error writing output file (out of space?)\n");
goto cleanup;
}
outfile.reset();
printf(" done\n");
// stop if this is the end
if (length < splitsize)
break;
}
printf("File split successfully\n");
// if we get here, clear the errors
error = 0;
cleanup:
if (splitfile)
{
splitfile.reset();
if (error != 0)
remove(splitfilename.c_str());
}
if (infile)
infile.reset();
if (outfile)
{
outfile.reset();
if (error != 0)
remove(outfilename.c_str());
}
if (splitbuffer != nullptr)
free(splitbuffer);
return error;
}
/*-------------------------------------------------
join_file - rejoin a file from its split
parts
-------------------------------------------------*/
static int join_file(const char *filename, const char *outname, int write_output)
{
std::string expectedhash, computedhash;
std::string outfilename, infilename;
std::string basepath;
util::core_file::ptr outfile, infile, splitfile;
void *splitbuffer = nullptr;
osd_file::error filerr;
uint32_t splitsize;
char buffer[256];
int error = 1;
int index;
// open the file for read
filerr = util::core_file::open(filename, OPEN_FLAG_READ, splitfile);
if (filerr != osd_file::error::NONE)
{
fprintf(stderr, "Fatal error: unable to open file '%s'\n", filename);
goto cleanup;
}
// read the first line and verify this is a split file
if (!splitfile->gets(buffer, sizeof(buffer)) || strncmp(buffer, "splitfile=", 10) != 0)
{
fprintf(stderr, "Fatal error: corrupt or incomplete split file at line:\n%s\n", buffer);
goto cleanup;
}
outfilename.assign(strtrimspace(buffer + 10));
// compute the base path
basepath.assign(filename);
index = basepath.find_last_of(PATH_SEPARATOR[0]);
if (index != -1)
basepath.erase(index + 1);
else
basepath.clear();
// override the output filename if specified, otherwise prepend the path
if (outname != nullptr)
outfilename.assign(outname);
else
outfilename.insert(0, basepath);
// read the split size
if (!splitfile->gets(buffer, sizeof(buffer)) || sscanf(buffer, "splitsize=%d", &splitsize) != 1)
{
fprintf(stderr, "Fatal error: corrupt or incomplete split file at line:\n%s\n", buffer);
goto cleanup;
}
// attempt to open the new file
if (write_output)
{
// don't overwrite the original!
filerr = util::core_file::open(outfilename, OPEN_FLAG_READ, outfile);
if (filerr == osd_file::error::NONE)
{
outfile.reset();
fprintf(stderr, "Fatal error: output file '%s' already exists\n", outfilename.c_str());
goto cleanup;
}
// open the output for write
filerr = util::core_file::open(outfilename, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, outfile);
if (filerr != osd_file::error::NONE)
{
fprintf(stderr, "Fatal error: unable to create file '%s'\n", outfilename.c_str());
goto cleanup;
}
}
printf("%s file '%s'...\n", write_output ? "Joining" : "Verifying", outfilename.c_str());
// now iterate through each file
while (splitfile->gets(buffer, sizeof(buffer)))
{
uint32_t length, actual;
// make sure the hash and filename are in the right place
if (strncmp(buffer, "hash=", 5) != 0 || strncmp(buffer + 5 + SHA1_DIGEST_SIZE * 2, " file=", 6) != 0)
{
fprintf(stderr, "Fatal error: corrupt or incomplete split file at line:\n%s\n", buffer);
goto cleanup;
}
expectedhash.assign(buffer + 5, SHA1_DIGEST_SIZE * 2);
infilename.assign(strtrimspace(buffer + 5 + SHA1_DIGEST_SIZE * 2 + 6));
printf(" Reading file '%s'...", infilename.c_str());
// read the file's contents
infilename.insert(0, basepath);
filerr = util::core_file::load(infilename.c_str(), &splitbuffer, length);
if (filerr != osd_file::error::NONE)
{
printf("\n");
fprintf(stderr, "Fatal error: unable to load file '%s'\n", infilename.c_str());
goto cleanup;
}
// hash the contents
compute_hash_as_string(computedhash, splitbuffer, length);
// compare
if (computedhash.compare(expectedhash)!=0)
{
printf("\n");
fprintf(stderr, "Fatal error: file '%s' has incorrect hash\n Expected: %s\n Computed: %s\n", infilename.c_str(), expectedhash.c_str(), computedhash.c_str());
goto cleanup;
}
// append to the file
if (write_output)
{
printf(" writing...");
actual = outfile->write(splitbuffer, length);
if (actual != length)
{
printf("\n");
fprintf(stderr, "Fatal error: Error writing output file (out of space?)\n");
goto cleanup;
}
printf(" done\n");
}
else
printf(" verified\n");
// release allocated memory
free(splitbuffer);
splitbuffer = nullptr;
}
if (write_output)
printf("File re-created successfully\n");
else
printf("File verified successfully\n");
// if we get here, clear the errors
error = 0;
cleanup:
if (splitfile)
splitfile.reset();
if (infile)
infile.reset();
if (outfile)
{
outfile.reset();
if (error != 0)
remove(outfilename.c_str());
}
if (splitbuffer != nullptr)
free(splitbuffer);
return error;
}
/*-------------------------------------------------
main - primary entry point
-------------------------------------------------*/
int main(int argc, char *argv[])
{
int result;
/* skip if no command */
if (argc < 2)
goto usage;
/* split command */
if (core_stricmp(argv[1], "-split") == 0)
{
if (argc != 4 && argc != 5)
goto usage;
result = split_file(argv[2], argv[3], (argc < 5) ? DEFAULT_SPLIT_SIZE : atoi(argv[4]));
}
/* join command */
else if (core_stricmp(argv[1], "-join") == 0)
{
if (argc != 3 && argc != 4)
goto usage;
result = join_file(argv[2], (argc >= 4) ? argv[3] : nullptr, true);
}
/* verify command */
else if (core_stricmp(argv[1], "-verify") == 0)
{
if (argc != 3)
goto usage;
result = join_file(argv[2], nullptr, false);
}
else
goto usage;
return result;
usage:
fprintf(stderr,
"Usage:\n"
" split -split <bigfile> <basename> [<size>] -- split file into parts\n"
" split -join <splitfile> [<outputfile>] -- join file parts into original file\n"
" split -verify <splitfile> -- verify a split file\n"
"\n"
"Where:\n"
" <bigfile> is the large file you wish to split\n"
" <basename> is the base path and name to assign to the split files\n"
" <size> is the optional split size, in MB (100MB is the default)\n"
" <splitfile> is the name of the <basename>.split generated with -split\n"
" <outputfile> is the name of the file to output (defaults to original name)\n"
);
return 0;
}
|