-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.c
170 lines (153 loc) · 4.62 KB
/
utils.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
#include "utils.h"
#include <stdlib.h>
#include <string.h>
#include "webslides.h"
// ---------------------------------------------------------------------------
int base64encode(const void *data_buf, size_t dataLength, char *result,
size_t resultSize) {
const char base64chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const uint8_t *data = (const uint8_t *)data_buf;
size_t resultIndex = 0;
size_t x;
uint32_t n = 0;
int padCount = dataLength % 3;
uint8_t n0, n1, n2, n3;
for (x = 0; x < dataLength; x += 3) {
n = ((uint32_t)data[x]) << 16;
if ((x + 1) < dataLength) n += ((uint32_t)data[x + 1]) << 8;
if ((x + 2) < dataLength) n += data[x + 2];
n0 = (uint8_t)(n >> 18) & 63;
n1 = (uint8_t)(n >> 12) & 63;
n2 = (uint8_t)(n >> 6) & 63;
n3 = (uint8_t)n & 63;
if (resultIndex >= resultSize) return 1;
result[resultIndex++] = base64chars[n0];
if (resultIndex >= resultSize) return 1;
result[resultIndex++] = base64chars[n1];
if ((x + 1) < dataLength) {
if (resultIndex >= resultSize) return 1;
result[resultIndex++] = base64chars[n2];
}
if ((x + 2) < dataLength) {
if (resultIndex >= resultSize) return 1;
result[resultIndex++] = base64chars[n3];
}
}
if (padCount > 0) {
for (; padCount < 3; padCount++) {
if (resultIndex >= resultSize) return 1;
result[resultIndex++] = '=';
}
}
if (resultIndex >= resultSize) return 1;
result[resultIndex] = 0;
return 0;
}
// ---------------------------------------------------------------------------
char *read_file(char *fname) {
FILE *f = fopen(fname, "rb");
if (!f) {
return NULL;
}
fseek(f, 0, SEEK_END);
size_t s = ftell(f);
fseek(f, 0, SEEK_SET);
char *in = calloc(s + 1, 1);
fread(in, s, 1, f);
fclose(f);
return in;
}
// ---------------------------------------------------------------------------
char *encode_file_base64(char *fname) {
FILE *f = fopen(fname, "rb");
fseek(f, 0, SEEK_END);
size_t s = ftell(f);
fseek(f, 0, SEEK_SET);
char *in = malloc(s);
fread(in, s, 1, f);
fclose(f);
char *out = malloc(s * 2 + 8);
base64encode(in, s, out, s * 2 + 8);
free(in);
return out;
}
// ---------------------------------------------------------------------------
char *encode_array_base64(char *array, size_t len) {
char *out = malloc(len * 2 + 8);
base64encode(array, len, out, len * 2 + 8);
return out;
}
// ---------------------------------------------------------------------------
char *replace_string_first(char *input, char *search, char *replace) {
size_t search_len = strlen(search);
size_t input_len = strlen(input);
size_t replace_len = strlen(replace);
char *nbuff = malloc(input_len - search_len + replace_len + 1);
size_t i, out = 0;
for (i = 0; i < input_len; i++) {
if (i < input_len - search_len &&
strncmp(input + i, search, search_len) == 0) {
for (size_t j = 0; j < replace_len; j++) {
nbuff[out++] = replace[j];
}
i += search_len - 1;
} else {
nbuff[out++] = input[i];
}
}
nbuff[out] = 0;
free(input);
return nbuff;
}
// ---------------------------------------------------------------------------
char *encode_array(SlideInfo *info, int offset, int len, int b64,
progress_t cb) {
// calculate size
size_t rlen = 0;
for (int i = 0; i < len; i++) {
char *d = ((char **)(info + i))[offset];
if (d) {
rlen += strlen(d) * (b64 + 1) + 8;
} else {
rlen += 8;
}
}
char *buff = malloc(rlen);
strcpy(buff, "[");
for (int i = 0; i < len; i++) {
char *data = ((char **)(info + i))[offset];
char *b64_data = data;
if (b64) {
if (!data) data = "";
b64_data = calloc(strlen(data) * 2 + 8, 1);
base64encode(data, strlen(data), b64_data, strlen(data) * 2 + 8);
}
strcat(buff, "\"");
strcat(buff, b64_data);
strcat(buff, "\"");
if (b64) free(b64_data);
if (i != len - 1) strcat(buff, ", ");
if (cb) cb(i);
}
strcat(buff, "]\n");
return buff;
}
// ---------------------------------------------------------------------------
void append_elem(char **orig, const char *append, const char *split) {
char *tmp = NULL;
if (*orig == NULL) {
*orig = calloc(strlen(append) + strlen(split) + 1, sizeof(char));
strcat(*orig, split);
strcat(*orig, append);
} else {
tmp = strdup(*orig);
char* appended = calloc(strlen(*orig) + strlen(split) + strlen(append) + 1, sizeof(char));
strcat(appended, tmp);
strcat(appended, split);
strcat(appended, append);
free(tmp);
free(*orig);
*orig = appended;
}
}