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
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <io.h> #include <fcntl.h> #include <stdbool.h>
#ifndef MAX_PATH #define MAX_PATH 256 #endif
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char * base64_encode( const unsigned char * bindata, char * base64, int binlength ) { int i, j; unsigned char current;
for ( i = 0, j = 0 ; i < binlength ; i += 3 ) { current = (bindata[i] >> 2) ; current &= (unsigned char)0x3F; base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ; if ( i + 1 >= binlength ) { base64[j++] = base64char[(int)current]; base64[j++] = '='; base64[j++] = '='; break; } current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F ); base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ; if ( i + 2 >= binlength ) { base64[j++] = base64char[(int)current]; base64[j++] = '='; break; } current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 ); base64[j++] = base64char[(int)current];
current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ; base64[j++] = base64char[(int)current]; } base64[j] = '\0'; return base64; }
int base64_decode( const char * base64, unsigned char * bindata ) { int i, j; unsigned char k; unsigned char temp[4]; for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 ) { memset( temp, 0xFF, sizeof(temp) ); for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i] ) temp[0]= k; } for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i+1] ) temp[1]= k; } for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i+2] ) temp[2]= k; } for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i+3] ) temp[3]= k; }
bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) | ((unsigned char)((unsigned char)(temp[1]>>4)&0x03)); if ( base64[i+2] == '=' ) break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) | ((unsigned char)((unsigned char)(temp[2]>>2)&0x0F)); if ( base64[i+3] == '=' ) break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) | ((unsigned char)(temp[3]&0x3F)); } return j; }
void encode(FILE * fp_in, FILE * fp_out) { unsigned char bindata[2050]; char base64[4096]; size_t bytes; while ( !feof( fp_in ) ) { bytes = fread( bindata, 1, 2049, fp_in ); base64_encode( bindata, base64, bytes ); fprintf( fp_out, "%s", base64 ); } }
void decode(FILE * fp_in, FILE * fp_out) { int i; unsigned char bindata[2050]; char base64[4096]; size_t bytes; while ( !feof( fp_in ) ) { for ( i = 0 ; i < 2048 ; i ++ ) { base64[i] = fgetc(fp_in); if ( base64[i] == EOF ) break; else if ( base64[i] == '\n' || base64[i] == '\r' ) i --; } bytes = base64_decode( base64, bindata ); fwrite( bindata, bytes, 1, fp_out ); } }
void help(const char * filepath) { fprintf( stderr, "Usage: %s [-d] [input_filename] [-o output_filepath]\n", filepath ); fprintf( stderr, "\t-d\tdecode data\n" ); fprintf( stderr, "\t-o\toutput filepath\n\n" ); }
int main(int argc, char * argv[]) { FILE * fp_input = NULL; FILE * fp_output = NULL; bool isencode = true; bool needHelp = false; int opt = 0; char input_filename[MAX_PATH] = ""; char output_filename[MAX_PATH] = "";
opterr = 0; while ( (opt = getopt(argc, argv, "hdo:")) != -1 ) { switch(opt) { case 'd': isencode = false; break; case 'o': strncpy(output_filename, optarg, sizeof(output_filename)); output_filename[sizeof(output_filename)-1] = '\0'; break; case 'h': needHelp = true; break; default: fprintf(stderr, "%s: invalid option -- %c\n", argv[0], optopt); needHelp = true; break; } } if ( optind < argc ) { strncpy(input_filename, argv[optind], sizeof(input_filename)); input_filename[sizeof(input_filename)-1] = '\0'; }
if (needHelp) { help(argv[0]); return EXIT_FAILURE; }
if ( !strcmp(input_filename, "") ) { fp_input = stdin; if (isencode) _setmode( _fileno(stdin), _O_BINARY ); } else { if (isencode) fp_input = fopen(input_filename, "rb"); else fp_input = fopen(input_filename, "r"); } if ( fp_input == NULL ) { fprintf(stderr, "Input file open error\n"); return EXIT_FAILURE; }
if ( !strcmp(output_filename, "") ) { fp_output = stdout; if (!isencode) _setmode( _fileno(stdout), _O_BINARY ); } else { if (isencode) fp_output = fopen(output_filename, "w"); else fp_output = fopen(output_filename, "wb"); } if ( fp_output == NULL ) { fclose(fp_input); fp_input = NULL; fprintf(stderr, "Output file open error\n"); return EXIT_FAILURE; }
if (isencode) encode(fp_input, fp_output); else decode(fp_input, fp_output); fclose(fp_input); fclose(fp_output); fp_input = fp_output = NULL; return EXIT_SUCCESS; }
|