forked from freem/romwak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
romwak.c
795 lines (697 loc) · 20.5 KB
/
romwak.c
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/* ROMWak (ANSI C port by freem, original by Jeff Kurtz)
*
* ROMWak was originally programmed in Delphi. This port is in ANSI C.
* The sources given to me were of version 0.3, though I have a version 0.4
* binary on my computer. I have not analyzed the differences between the two
* versions, as I do not have access to Delphi build tools.
*/
#include <stdio.h>
#include <stdlib.h>
#include "romwak.h"
#define ROMWAK_VERSION "0.3c" /* derived from 0.3 source code; see above note */
/* Usage() - Print program usage. */
void Usage(){
printf("usage: romwak <option> <infile> <outfile> [outfile2] [psize] [pbyte]\n");
printf("You must use one of these options:\n");
printf(" /b - Split file into two files, alternating bytes into separate files.\n");
printf(" /f - Flip low/high bytes of a file. (<outfile> optional.)\n");
printf(" /h - Split file in half (two files).\n");
printf(" /m - Byte merge two files. (stores results in <outfile2>).\n");
printf(" /s - Swap top and bottom halves of a file. (<outfile2> optional.)\n");
printf(" /w - Split file into two files, alternating words into output files.\n");
printf(" /p - Pad file to [psize] in K with [pbyte] value (0-255).\n");
printf("\n");
printf("NOTE: Omission of [outfile2] will result in the second file not being saved.\n");
printf("\n");
printf("See the included README.md for more details. If README.md was not included,\n");
printf("please visit https://github.com/freem/romwak/\n");
}
/*----------------------------------------------------------------------------*/
/* FileExists(char *fileIn) - Helper function to determine if a file exists.
*
* (Params)
* char *fileIn Target filename
*/
bool FileExists(char *fileIn){
/* try opening file */
FILE *f = fopen(fileIn,"r");
if(f != NULL){
/* file open was successful, we don't need this handle. */
fclose(f);
return true;
}
/* file open unsuccessful, print error */
perror("Error attempting to open file");
return false;
}
/*----------------------------------------------------------------------------*/
/* FileSize(FILE *pFile) - Helper function to determine a file's size.
* Does not rewind the file, you must do it yourself.
*
* (Params)
* FILE *pFile Handle to a loaded file.
*/
long FileSize(FILE *pFile){
fseek(pFile,0,SEEK_END); /* SEEK_END is non-portable, sorry */
return ftell(pFile);
}
/*----------------------------------------------------------------------------*/
/* EqualSplit(char *fileIn, char *fileOutA, char *fileOutB) - /h
* Splits a file in half equally.
* (Get filesize, divide it by 2, and split the data. Easy enough.)
*
* (Params)
* char *fileIn Input filename
* char *fileOutA Output filename 1
* char *fileOutB Output filename 2
*/
int EqualSplit(char *fileIn, char *fileOutA, char *fileOutB){
FILE *pInFile, *pOutFile1, *pOutFile2;
long length;
unsigned char *inBuffer;
size_t result;
long halfLength;
unsigned char *outBuf1;
unsigned char *outBuf2;
long i = 0;
long pos = 0;
unsigned char b1, b2;
if(!FileExists(fileIn)){
return EXIT_FAILURE;
}
printf("Splitting file '%s' equally, saving to '%s' and '%s'\n",fileIn,fileOutA,fileOutB);
pInFile = fopen(fileIn,"rb");
if(pInFile == NULL){
perror("Error attempting to open input file");
exit(EXIT_FAILURE);
}
/* find file size */
length = FileSize(pInFile);
rewind(pInFile);
/* copy data into buffer */
inBuffer = (unsigned char*)malloc(length);
if(inBuffer == NULL){
printf("Error allocating memory for input file buffer.");
exit(EXIT_FAILURE);
}
result = fread(inBuffer,sizeof(unsigned char),length,pInFile);
if(result != length){
perror("Error reading input file");
exit(EXIT_FAILURE);
}
fclose(pInFile);
/* prepare output buffers */
halfLength = length/2;
outBuf1 = (unsigned char*)malloc(halfLength);
if(outBuf1 == NULL){
printf("Error allocating memory for first output file buffer.");
exit(EXIT_FAILURE);
}
outBuf2 = (unsigned char*)malloc(halfLength);
if(outBuf2 == NULL){
printf("Error allocating memory for second output file buffer.");
exit(EXIT_FAILURE);
}
/* fill buffers */
while(i<halfLength){
b1 = inBuffer[i];
b2 = inBuffer[i+halfLength];
outBuf1[pos] = b1;
outBuf2[pos] = b2;
i++;
pos++;
}
free(inBuffer);
/* write output files */
pOutFile1 = fopen(fileOutA,"wb");
if(pOutFile1 == NULL){
perror("Error attempting to create first output file");
exit(EXIT_FAILURE);
}
result = fwrite(outBuf1,sizeof(unsigned char),halfLength,pOutFile1);
if(result != halfLength){
perror("Error writing first output file");
exit(EXIT_FAILURE);
}
fclose(pOutFile1);
printf("'%s' saved successfully!\n",fileOutA);
free(outBuf1);
pOutFile2 = fopen(fileOutB,"wb");
if(pOutFile2 == NULL){
perror("Error attempting to create second output file");
exit(EXIT_FAILURE);
}
result = fwrite(outBuf2,sizeof(unsigned char),halfLength,pOutFile2);
if(result != halfLength){
perror("Error writing second output file");
exit(EXIT_FAILURE);
}
fclose(pOutFile2);
printf("'%s' saved successfully!\n",fileOutB);
free(outBuf2);
return EXIT_SUCCESS;
}
/*----------------------------------------------------------------------------*/
/* ByteSplit(char *fileIn, char *fileOutA, char *fileOutB) - /b
*
* (Params)
* char *fileIn Input filename
* char *fileOutA Output filename 1
* char *fileOutB Output filename 2
*/
int ByteSplit(char *fileIn, char *fileOutA, char *fileOutB){
FILE *pInFile, *pOutFile1, *pOutFile2;
long length;
unsigned char *inBuffer;
size_t result;
long halfLength;
unsigned char *outBuf1;
unsigned char *outBuf2;
long i = 0;
long pos1 = 0;
long pos2 = 1;
if(!FileExists(fileIn)){
return EXIT_FAILURE;
}
printf("Splitting file '%s' into bytes, saving to '%s' and '%s'\n",fileIn,fileOutA,fileOutB);
pInFile = fopen(fileIn,"rb");
if(pInFile == NULL){
perror("Error attempting to open input file");
exit(EXIT_FAILURE);
}
/* find file size */
length = FileSize(pInFile);
rewind(pInFile);
/* copy data into buffer */
inBuffer = (unsigned char*)malloc(length);
if(inBuffer == NULL){
printf("Error allocating memory for input file buffer.");
exit(EXIT_FAILURE);
}
result = fread(inBuffer,sizeof(unsigned char),length,pInFile);
if(result != length){
perror("Error reading input file");
exit(EXIT_FAILURE);
}
fclose(pInFile);
/* prepare output buffers */
halfLength = length/2;
outBuf1 = (unsigned char*)malloc(halfLength);
if(outBuf1 == NULL){
printf("Error allocating memory for first output file buffer.");
exit(EXIT_FAILURE);
}
outBuf2 = (unsigned char*)malloc(halfLength);
if(outBuf2 == NULL){
printf("Error allocating memory for second output file buffer.");
exit(EXIT_FAILURE);
}
/* fill buffers */
while(i<halfLength){
outBuf1[i] = inBuffer[pos1];
outBuf2[i] = inBuffer[pos2];
i++;
pos1+=2;
pos2+=2;
}
free(inBuffer);
/* write output files */
pOutFile1 = fopen(fileOutA,"wb");
if(pOutFile1 == NULL){
perror("Error attempting to create first output file");
exit(EXIT_FAILURE);
}
result = fwrite(outBuf1,sizeof(unsigned char),halfLength,pOutFile1);
if(result != halfLength){
perror("Error writing first output file");
exit(EXIT_FAILURE);
}
fclose(pOutFile1);
printf("'%s' saved successfully!\n",fileOutA);
free(outBuf1);
pOutFile2 = fopen(fileOutB,"wb");
if(pOutFile2 == NULL){
perror("Error attempting to create second output file");
exit(EXIT_FAILURE);
}
result = fwrite(outBuf2,sizeof(unsigned char),halfLength,pOutFile2);
if(result != halfLength){
perror("Error writing second output file");
exit(EXIT_FAILURE);
}
fclose(pOutFile2);
printf("'%s' saved successfully!\n",fileOutB);
free(outBuf2);
return EXIT_SUCCESS;
}
/*----------------------------------------------------------------------------*/
/* WordSplit(char *fileIn, char *fileOutA, char *fileOutB) - /w
*
* (Params)
* char *fileIn Input filename
* char *fileOutA Output filename 1
* char *fileOutB Output filename 2
*/
int WordSplit(char *fileIn, char *fileOutA, char *fileOutB){
FILE *pInFile, *pOutFile1, *pOutFile2;
long length;
unsigned char *inBuffer;
size_t result;
long halfLength;
unsigned char *outBuf1;
unsigned char *outBuf2;
long i = 0;
long pos1 = 0;
long pos2 = 2;
if(!FileExists(fileIn)){
return EXIT_FAILURE;
}
printf("Splitting file '%s' into words, saving to '%s' and '%s'\n",fileIn,fileOutA,fileOutB);
pInFile = fopen(fileIn,"rb");
if(pInFile == NULL){
perror("Error attempting to open input file");
exit(EXIT_FAILURE);
}
/* find file size */
length = FileSize(pInFile);
rewind(pInFile);
/* copy data into buffer */
inBuffer = (unsigned char*)malloc(length);
if(inBuffer == NULL){
printf("Error allocating memory for input file buffer.");
exit(EXIT_FAILURE);
}
result = fread(inBuffer,sizeof(unsigned char),length,pInFile);
if(result != length){
perror("Error reading input file");
exit(EXIT_FAILURE);
}
fclose(pInFile);
/* prepare output buffers */
halfLength = length/2;
outBuf1 = (unsigned char*)malloc(halfLength);
if(outBuf1 == NULL){
printf("Error allocating memory for first output file buffer.");
exit(EXIT_FAILURE);
}
outBuf2 = (unsigned char*)malloc(halfLength);
if(outBuf2 == NULL){
printf("Error allocating memory for second output file buffer.");
exit(EXIT_FAILURE);
}
/* fill buffers */
while(i<halfLength){
outBuf1[i] = inBuffer[pos1];
outBuf1[i+1] = inBuffer[pos1+1];
outBuf2[i] = inBuffer[pos2];
outBuf2[i+1] = inBuffer[pos2+1];
i+=2;
pos1+=4;
pos2+=4;
}
free(inBuffer);
/* write output files */
pOutFile1 = fopen(fileOutA,"wb");
if(pOutFile1 == NULL){
perror("Error attempting to create first output file");
exit(EXIT_FAILURE);
}
result = fwrite(outBuf1,sizeof(unsigned char),halfLength,pOutFile1);
if(result != halfLength){
perror("Error writing first output file");
exit(EXIT_FAILURE);
}
fclose(pOutFile1);
printf("'%s' saved successfully!\n",fileOutA);
free(outBuf1);
pOutFile2 = fopen(fileOutB,"wb");
if(pOutFile2 == NULL){
perror("Error attempting to create second output file");
exit(EXIT_FAILURE);
}
result = fwrite(outBuf2,sizeof(unsigned char),halfLength,pOutFile2);
if(result != halfLength){
perror("Error writing second output file");
exit(EXIT_FAILURE);
}
fclose(pOutFile2);
printf("'%s' saved successfully!\n",fileOutB);
free(outBuf2);
return EXIT_SUCCESS;
}
/*----------------------------------------------------------------------------*/
/* FlipByte(char *fileIn, char *fileOut) - /f
* Flip low/high bytes of a file.
*
* (Params)
* char *fileIn Input filename
* char *fileOut Output filename
*/
int FlipByte(char *fileIn, char *fileOut){
FILE *pInFile, *pOutFile;
long length;
unsigned char *buffer;
size_t result;
long i = 0;
unsigned char b1, b2;
if(!FileExists(fileIn)){
return EXIT_FAILURE;
}
/* If the second file is not passed in, flip the file in place. */
if(fileOut == NULL){
fileOut = fileIn;
}
printf("Flipping bytes of '%s', saving to '%s'\n",fileIn,fileOut);
pInFile = fopen(fileIn,"rb");
if(pInFile == NULL){
perror("Error attempting to open input file");
exit(EXIT_FAILURE);
}
/* find file size */
length = FileSize(pInFile);
rewind(pInFile);
/* copy data into buffer */
buffer = (unsigned char*)malloc(length);
if(buffer == NULL){
printf("Error allocating memory for buffer.");
exit(EXIT_FAILURE);
}
result = fread(buffer,sizeof(unsigned char),length,pInFile);
if(result != length){
perror("Error reading input file");
exit(EXIT_FAILURE);
}
fclose(pInFile);
/* Create new file */
pOutFile = fopen(fileOut,"wb");
if(pOutFile == NULL){
perror("Error attempting to create output file");
exit(EXIT_FAILURE);
}
/* flip bytes in buffer */
while(i<length){
b1 = buffer[i];
b2 = buffer[i+1];
buffer[i+1] = b1;
buffer[i] = b2;
i+=2;
}
/* write output file */
result = fwrite(buffer,sizeof(unsigned char),length,pOutFile);
if(result != length){
perror("Error writing output file");
exit(EXIT_FAILURE);
}
fclose(pOutFile);
printf("'%s' saved successfully!\n",fileOut);
free(buffer);
return EXIT_SUCCESS;
}
/*----------------------------------------------------------------------------*/
/* MergeBytes(char *fileIn1, char *fileIn2, char *fileOut) - /m
* Byte merges two files; stores result in fileOut.
*
* (Params)
* char *fileIn1 Input filename 1
* char *fileIn2 Input filename 2
* char *fileOut Output filename
*/
int MergeBytes(char *fileIn1, char *fileIn2, char *fileOut){
FILE *pInFile1, *pInFile2, *pOutFile;
long length1, length2;
unsigned char *inBuf1;
unsigned char *inBuf2;
size_t result;
long outBufLen;
unsigned char *outBuf;
long i = 0;
long curPos = 0;
if(!FileExists(fileIn1)){
return EXIT_FAILURE;
}
if(!FileExists(fileIn2)){
return EXIT_FAILURE;
}
printf("Merging bytes of '%s' and '%s', saving to '%s'\n",fileIn1,fileIn2,fileOut);
/* Read file 1 */
pInFile1 = fopen(fileIn1,"rb");
if(pInFile1 == NULL){
perror("Error attempting to open first input file");
exit(EXIT_FAILURE);
}
/* find first file size */
length1 = FileSize(pInFile1);
rewind(pInFile1);
/* put file 1's contents into buffer */
inBuf1 = (unsigned char*)malloc(length1);
if(inBuf1 == NULL){
printf("Error allocating memory for input file buffer 1.");
exit(EXIT_FAILURE);
}
result = fread(inBuf1,sizeof(unsigned char),length1,pInFile1);
if(result != length1){
perror("Error reading first input file");
exit(EXIT_FAILURE);
}
fclose(pInFile1);
/* Read file 2 */
pInFile2 = fopen(fileIn2,"rb");
if(pInFile2 == NULL){
perror("Error attempting to open second input file");
exit(EXIT_FAILURE);
}
/* find second file size */
length2 = FileSize(pInFile2);
rewind(pInFile2);
/* put file 2's contents into buffer */
inBuf2 = (unsigned char*)malloc(length2);
if(inBuf2 == NULL){
printf("Error allocating memory for input file buffer 2.");
exit(EXIT_FAILURE);
}
result = fread(inBuf2,sizeof(unsigned char),length2,pInFile2);
if(result != length2){
perror("Error reading second input file");
exit(EXIT_FAILURE);
}
fclose(pInFile2);
/* Create new file */
pOutFile = fopen(fileOut,"wb");
if(pOutFile == NULL){
perror("Error attempting to create output file");
exit(EXIT_FAILURE);
}
/* merge bytes into a new buffer */
outBufLen = length1+length2;
outBuf = (unsigned char*)malloc(outBufLen);
if(outBuf == NULL){
printf("Error allocating memory for output file buffer.");
exit(EXIT_FAILURE);
}
while(i<length1){
outBuf[curPos] = inBuf1[i];
outBuf[curPos+1] = inBuf2[i];
curPos+=2;
i++;
}
free(inBuf1);
free(inBuf2);
/* write output file */
result = fwrite(outBuf,sizeof(unsigned char),outBufLen,pOutFile);
if(result != outBufLen){
perror("Error writing output file");
exit(EXIT_FAILURE);
}
fclose(pOutFile);
printf("'%s' saved successfully!\n",fileOut);
free(outBuf);
return EXIT_SUCCESS;
}
/*----------------------------------------------------------------------------*/
/* SwapHalf(char *fileIn, char *fileOut) - /s
* Swaps the top and bottom halves of fileIn; writes to fileOut.
*
* (Params)
* char *fileIn Input filename
* char *fileOut Output filename
*/
int SwapHalf(char *fileIn, char *fileOut){
FILE *pInFile, *pOutFile;
long length, halfLength;
unsigned char *inBufHalf1;
unsigned char *inBufHalf2;
size_t result;
if(!FileExists(fileIn)){
return EXIT_FAILURE;
}
/* If the second file is not passed in, swap the file in place. */
if(fileOut == NULL){
fileOut = fileIn;
}
printf("Swapping halves of '%s', saving to '%s'\n",fileIn,fileOut);
pInFile = fopen(fileIn,"rb");
if(pInFile == NULL){
perror("Error attempting to open input file");
exit(EXIT_FAILURE);
}
/* find current file size */
length = FileSize(pInFile);
rewind(pInFile);
halfLength = length/2;
/* read halves of file into two buffers */
inBufHalf1 = (unsigned char*)malloc(halfLength);
if(inBufHalf1 == NULL){
printf("Error allocating memory for input file buffer 1.");
exit(EXIT_FAILURE);
}
result = fread(inBufHalf1,sizeof(unsigned char),halfLength,pInFile);
if(result != halfLength){
perror("Error reading input file");
exit(EXIT_FAILURE);
}
inBufHalf2 = (unsigned char*)malloc(halfLength);
if(inBufHalf2 == NULL){
printf("Error allocating memory for input file buffer 1.");
exit(EXIT_FAILURE);
}
result = fread(inBufHalf2,sizeof(unsigned char),halfLength,pInFile);
if(result != halfLength){
perror("Error reading input file");
exit(EXIT_FAILURE);
}
close(pInFile);
/* create new file from buffers written in reverse order */
pOutFile = fopen(fileOut,"wb");
if(pOutFile == NULL){
perror("Error attempting to create output file");
exit(EXIT_FAILURE);
}
/* write output file */
result = fwrite(inBufHalf2,sizeof(unsigned char),halfLength,pOutFile);
if(result != halfLength){
perror("Error writing output file");
exit(EXIT_FAILURE);
}
result = fwrite(inBufHalf1,sizeof(unsigned char),halfLength,pOutFile);
if(result != halfLength){
perror("Error writing output file");
exit(EXIT_FAILURE);
}
fclose(pOutFile);
printf("'%s' saved successfully!\n",fileOut);
free(inBufHalf1);
free(inBufHalf2);
return EXIT_SUCCESS;
}
/*----------------------------------------------------------------------------*/
/* PadFile(char *fileIn, char *fileOut, char *padSize, char *padByte) - /p
* Pads fileIn to padSize kilobytes with specified padByte; writes to fileOut.
*
* (Params)
* char *fileIn Input filename
* char *fileOut Output filename
* char *padSize Size to pad file to (in Kilobytes)
* char *padByte Value to pad with
*/
int PadFile(char *fileIn, char *fileOut, char *padSize, char *padByte){
unsigned int shortPadSize = (atoi(padSize));
unsigned char padChar = (unsigned char)atoi(padByte);
FILE *pInFile, *pOutFile;
long length, fullPadSize,bufLength;
unsigned char *buffer;
size_t result;
long remain, bufPos;
long i = 0;
if(!FileExists(fileIn)){
return EXIT_FAILURE;
}
printf("Padding '%s' to %d kilobytes with byte 0x%02X, saving to '%s'\n",
fileIn,shortPadSize,padChar,fileOut);
pInFile = fopen(fileIn,"rb");
if(pInFile == NULL){
perror("Error attempting to open input file");
exit(EXIT_FAILURE);
}
/* find current file size */
length = FileSize(pInFile);
rewind(pInFile);
/* copy data into buffer */
fullPadSize = shortPadSize*1024;
bufLength = fullPadSize;
buffer = (unsigned char*)malloc(bufLength);
if(buffer == NULL){
printf("Error allocating memory for input file buffer.");
exit(EXIT_FAILURE);
}
result = fread(buffer,sizeof(unsigned char),length,pInFile);
if(result != length){
perror("Error reading input file");
exit(EXIT_FAILURE);
}
fclose(pInFile);
/* add padding to buffer */
remain = fullPadSize-(sizeof(unsigned char)*length);
bufPos = result;
while(i<remain){
buffer[bufPos] = padChar;
bufPos++;
i++;
}
/* create new file with padding */
pOutFile = fopen(fileOut,"wb");
if(pOutFile == NULL){
perror("Error attempting to create output file");
exit(EXIT_FAILURE);
}
/* write output file */
result = fwrite(buffer,sizeof(unsigned char),bufLength,pOutFile);
if(result != bufLength){
perror("Error writing output file");
exit(EXIT_FAILURE);
}
fclose(pOutFile);
printf("'%s' saved successfully!\n",fileOut);
free(buffer);
return EXIT_SUCCESS;
}
/*----------------------------------------------------------------------------*/
/* ye olde main */
int main(int argc, char* argv[]){
printf("ROMWak %s - original version by Jeff Kurtz / ANSI C port by freem\n",ROMWAK_VERSION);
if(argc < 2){
Usage();
return EXIT_FAILURE; /* failure to run due to no options */
}
/* command line argument parsing (originally in ROMWAK.DPR) */
/* The original program used /switches, but this port allows shorthand
* switches with a '-' as well, for people who aren't on Windows. */
if(argv[1][0] != '/' && argv[1][0] != '-'){
printf("ERROR: Invalid command %s\n\n",argv[1]);
Usage();
return EXIT_FAILURE; /* command syntax is wrong, broheim */
}
else{
switch(argv[1][1]){
case 'b': /* split file in two, alternating bytes */
return ByteSplit(argv[2],argv[3],argv[4]);
case 'f': /* flip low/high bytes */
return FlipByte(argv[2],argv[3]);
case 'h': /* split file in half (two files) */
return EqualSplit(argv[2],argv[3],argv[4]);
case 'm': /* byte merge two files */
return MergeBytes(argv[2],argv[3],argv[4]);
case 's': /* swap top and bottom halves of a file */
return SwapHalf(argv[2],argv[3]);
case 'w': /* split file in two, alternating words */
return WordSplit(argv[2],argv[3],argv[4]);
case 'p': /* pad file */
return PadFile(argv[2],argv[3],argv[4],argv[5]);
default:
/* option does not exist */
printf("ERROR: Option '/%c' doesn't exist.\n",argv[1][1]);
return EXIT_FAILURE;
}
}
/* should not get here, but if it does, let's not make a fuss. */
return EXIT_SUCCESS;
}