-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBASE64Enc.c
114 lines (101 loc) · 2.63 KB
/
BASE64Enc.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
/*************************************************
* Simple BASE64 Encoder, for embed icons in html
* September 2018 - M.Campinoti
**************************************************/
#include <stdio.h>
#include <string.h>
union conv
{
unsigned int l;
struct bytes
{
char b1;
char b2;
char b3;
char b4;
} b;
};
char convert(char c)
{
if (c == 63)
return 47;
else if (c == 62)
return 43;
else if (c >= 52)
return c - 4;
else if (c >= 26)
return c + 71;
else
return c + 65;
}
unsigned int toBase64(unsigned char b1, unsigned char b2, unsigned char b3)
{
conv src, dest;
src.b.b1 = b3;
src.b.b2 = b2;
src.b.b3 = b1;
src.b.b4 = 0;
//conv.l == b4 b3 b2 b1
dest.b.b1 = convert(src.b.b1 & 0x3f);
src.l = src.l >> 6;
dest.b.b2 = convert(src.b.b1 & 0x3f);
src.l = src.l >> 6;
dest.b.b3 = convert(src.b.b1 & 0x3f);
src.l = src.l >> 6;
dest.b.b4 = convert(src.b.b1 & 0x3f);
return dest.l;
}
int main(int argc, char *argv[])
{
FILE *output_file;
FILE *input_file;
char out_file_name[256];
unsigned char input_buffer[3];
int bytes_read;
conv dest;
if (argc == 2){
if ((input_file = fopen(argv[1], "rb")) == NULL){
printf("(!)input file error\n");
return 1;
}
}
else
{
printf("usage: B64ENC [inputfile]\n");
return 3;
}
// add .html extension
strcpy(&out_file_name[0], argv[1]);
strcat(&out_file_name[0], ".html");
//and open file for writing a base64 encoded png image
if ((output_file = fopen(out_file_name, "wt")) == NULL){
printf("(!)cannot open output file\n");
return 2;
}
fprintf(output_file, "<img src='data:image/png;base64,");
while (!feof(input_file)){
bytes_read = fread(&input_buffer, sizeof(char), 3, input_file);
if (bytes_read = 3){
dest.l = toBase64(input_buffer[0], input_buffer[1], input_buffer[2]);
}
else{
switch (bytes_read){
case 0:
input_buffer[0] = input_buffer[1] = input_buffer[2] = 0x00;
break;
case 1:
input_buffer[1] = input_buffer[2] = 0x00;
break;
case 2:
input_buffer[2] = 0x00;
break;
}
}
//conv.l == b4 b3 b2 b1
fprintf(output_file, "%c%c%c%c", dest.b.b4, dest.b.b3, dest.b.b2, dest.b.b1);
}
fprintf(output_file, "'/>");
fclose(output_file);
fclose(input_file);
return 0;
}