| 1 |
|
| 2 |
/* Base64 encoder utility - written by Istvan Varga, Jan 2003 */ |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <string.h> |
| 7 |
#include <errno.h> |
| 8 |
|
| 9 |
static int nr_infiles = 0; |
| 10 |
static int max_infiles = 0; |
| 11 |
static char **infile_names = NULL; |
| 12 |
static int style = 0; |
| 13 |
static int output_is_stdout = 1; |
| 14 |
static char *outflname = NULL; |
| 15 |
static FILE *outfile = NULL; |
| 16 |
|
| 17 |
/* convert 6 bits of input and write to output file */ |
| 18 |
extern int encode_byte(FILE*, FILE*); |
| 19 |
/* convert an entire input file */ |
| 20 |
extern void encode_file(char*, FILE*, int); |
| 21 |
/* line width */ |
| 22 |
extern int maxlinepos; |
| 23 |
|
| 24 |
int main(int argc, char **argv) |
| 25 |
{ |
| 26 |
int i, j; |
| 27 |
|
| 28 |
/* parse command line options */ |
| 29 |
i = 0; |
| 30 |
j = argc; |
| 31 |
while (--j) { |
| 32 |
i++; |
| 33 |
if (!strcmp(argv[i], "-w")) { /* line width */ |
| 34 |
if (!(--j)) { |
| 35 |
fprintf(stderr, "csb64enc: missing option for -w\n"); |
| 36 |
exit(-1); |
| 37 |
} |
| 38 |
i++; |
| 39 |
maxlinepos = (int) atoi(argv[i]); |
| 40 |
if (maxlinepos < 20) maxlinepos = 20; |
| 41 |
if (maxlinepos > 200) maxlinepos = 200; |
| 42 |
} |
| 43 |
else if (!strcmp(argv[i], "-o")) { /* output file name */ |
| 44 |
if (!(--j)) { |
| 45 |
fprintf(stderr, "csb64enc: missing option for -o\n"); |
| 46 |
exit(-1); |
| 47 |
} |
| 48 |
i++; |
| 49 |
outflname = argv[i]; |
| 50 |
if (!strcmp(outflname, "-")) |
| 51 |
output_is_stdout = 1; |
| 52 |
else |
| 53 |
output_is_stdout = 0; |
| 54 |
} |
| 55 |
else if (!strcmp(argv[i], "-s")) { |
| 56 |
if (!(--j)) { |
| 57 |
fprintf(stderr, "csb64enc: missing option for -s\n"); |
| 58 |
exit(-1); |
| 59 |
} |
| 60 |
i++; |
| 61 |
style = atoi(argv[i]); |
| 62 |
} |
| 63 |
else { /* input file name */ |
| 64 |
if (*(argv[i]) == '-') { |
| 65 |
fprintf(stderr, "csb64enc: invalid option: %s\n", argv[i]); |
| 66 |
exit(-1); |
| 67 |
} |
| 68 |
if (nr_infiles >= max_infiles) { |
| 69 |
/* extend number of input files */ |
| 70 |
max_infiles = max_infiles + (max_infiles >> 2) + 16; |
| 71 |
infile_names = (char**) realloc(infile_names, |
| 72 |
sizeof(char*) * max_infiles); |
| 73 |
if (infile_names == NULL) { |
| 74 |
fprintf(stderr, "csb64enc: not enough memory\n"); |
| 75 |
exit(-1); |
| 76 |
} |
| 77 |
} |
| 78 |
infile_names[nr_infiles] = argv[i]; |
| 79 |
nr_infiles++; |
| 80 |
} |
| 81 |
} |
| 82 |
if (!nr_infiles) { |
| 83 |
/* print usage */ |
| 84 |
fprintf(stderr, "csb64enc: no input files\n"); |
| 85 |
fprintf(stderr, |
| 86 |
"usage: csb64enc [OPTIONS ... ] infile1 [ infile2 [ ... ]]\n"); |
| 87 |
fprintf(stderr, "options:\n"); |
| 88 |
fprintf(stderr, |
| 89 |
" -w <n> set line width to n (default: 72)\n"); |
| 90 |
fprintf(stderr, |
| 91 |
" -o <fname> output file name (default: stdout)\n"); |
| 92 |
fprintf(stderr, |
| 93 |
" -s <n> output style to 0 (CsFileB default)\n" |
| 94 |
" or 2 (CsFileC) or 1 (MIDI)\n"); |
| 95 |
exit(-1); |
| 96 |
} |
| 97 |
/* open output file */ |
| 98 |
if (!output_is_stdout) { |
| 99 |
outfile = fopen(outflname, "w"); |
| 100 |
if (outfile == NULL) { |
| 101 |
fprintf(stderr, "csb64enc: error opening output file %s: %s\n", |
| 102 |
outflname, strerror(errno)); |
| 103 |
exit(-1); |
| 104 |
} |
| 105 |
} |
| 106 |
else |
| 107 |
outfile = stdout; |
| 108 |
/* encode all input files */ |
| 109 |
for (i = 0; i < nr_infiles; i++) |
| 110 |
encode_file(infile_names[i], outfile, style); |
| 111 |
/* close output file */ |
| 112 |
if (!output_is_stdout) { |
| 113 |
fflush(outfile); |
| 114 |
fclose(outfile); |
| 115 |
} |
| 116 |
free(infile_names); |
| 117 |
return 0; |
| 118 |
} |
| 119 |
|