67
66
/*****************************************************************************
68
* b64_encode: stolen from VLC's http.c
69
* simplified by michael
70
* fixed edge cases and made it work from data (vs. strings) by ryan.
67
* b64_encode: Stolen from VLC's http.c.
68
* Simplified by Michael.
69
* Fixed edge cases and made it work from data (vs. strings) by Ryan.
71
70
*****************************************************************************/
73
char *av_base64_encode(char * buf, int buf_len, const uint8_t * src, int len)
72
char *av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
75
74
static const char b64[] =
76
75
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
78
77
unsigned i_bits = 0;
80
int bytes_remaining = len;
79
int bytes_remaining = in_size;
82
if (len >= UINT_MAX / 4 ||
83
buf_len < len * 4 / 3 + 12)
81
if (in_size >= UINT_MAX / 4 ||
82
out_size < (in_size+2) / 3 * 4 + 1)
86
85
while (bytes_remaining) {
87
i_bits = (i_bits << 8) + *src++;
86
i_bits = (i_bits << 8) + *in++;
103
// #define TEST_BASE64
113
uint8_t decode[1000];
106
#define MAX_DATA_SIZE 1024
107
#define MAX_ENCODED_SIZE 2048
109
int test_encode_decode(const uint8_t *data, unsigned int data_size, const char *encoded_ref)
111
char encoded[MAX_ENCODED_SIZE];
112
uint8_t data2[MAX_DATA_SIZE];
113
int data2_size, max_data2_size = MAX_DATA_SIZE;
115
if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
116
printf("Failed: cannot encode the input data\n");
119
if (encoded_ref && strcmp(encoded, encoded_ref)) {
120
printf("Failed: encoded string differs from reference\n"
121
"Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref);
125
if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) < 0) {
126
printf("Failed: cannot decode the encoded string\n"
127
"Encoded:\n%s\n", encoded);
130
if (memcmp(data2, data, data_size)) {
131
printf("Failed: encoded/decoded data differs from original data\n");
141
int i, error_count = 0;
124
"4444", 4, "NDQ0NA=="}, {
125
"55555", 5, "NTU1NTU="}, {
126
"abc:def", 7, "YWJjOmRlZg=="}, {
144
const char *encoded_ref;
150
{ "4444", "NDQ0NA=="},
151
{ "55555", "NTU1NTU="},
152
{ "666666", "NjY2NjY2"},
153
{ "abc:def", "YWJjOmRlZg=="},
129
for (t = tests; t->data; t++) {
132
av_log(NULL, AV_LOG_ERROR, "Encoding %s...\n", (char *) t->data);
133
str = av_base64_encode(t->data, t->len);
135
av_log(NULL, AV_LOG_ERROR, "Encoded to %s...\n", str);
136
if (strcmp(str, t->result) != 0) {
137
av_log(NULL, AV_LOG_ERROR, "failed test %d: %s != %s\n",
138
numtest, str, t->result);
144
av_log(NULL, AV_LOG_ERROR, "Done encoding, about to decode...\n");
145
len = av_base64_decode(decode, t->result, sizeof(decode));
147
av_log(NULL, AV_LOG_ERROR, "failed test %d: len %d != %d\n",
148
numtest, len, t->len);
150
} else if (memcmp(decode, t->data, t->len) != 0) {
151
av_log(NULL, AV_LOG_ERROR, "failed test %d: data\n", numtest);
154
av_log(NULL, AV_LOG_ERROR, "Decoded to %s\n",
165
srand(123141); // time(NULL));
166
for (test_count = 0; test_count < 100; test_count++) {
167
int size = rand() % 1024;
170
char *encoded_result;
172
av_log(NULL, AV_LOG_ERROR, "Test %d: Size %d bytes...",
174
data = (uint8_t *) av_malloc(size);
175
for (ii = 0; ii < size; ii++) {
176
data[ii] = rand() % 255;
179
encoded_result = av_base64_encode(data, size);
180
if (encoded_result) {
181
int decode_buffer_size = size + 10; // try without 10 as well
182
uint8_t *decode_buffer = av_malloc(decode_buffer_size);
185
av_base64_decode(decode_buffer, encoded_result,
188
if (decoded_size != size) {
189
av_log(NULL, AV_LOG_ERROR,
190
"Decoded/Encoded size mismatch (%d != %d)\n",
193
if (memcmp(decode_buffer, data, decoded_size) == 0) {
194
av_log(NULL, AV_LOG_ERROR, "Passed!\n");
196
av_log(NULL, AV_LOG_ERROR,
197
"Failed (Data differs)!\n");
200
av_free(decode_buffer);
203
av_free(encoded_result);
208
// these are invalid strings, that it currently decodes (which it probably shouldn't?)
211
if (av_base64_decode(str, "M=M=", sizeof(str)) != -1) {
212
av_log(NULL, AV_LOG_ERROR,
213
"failed test %d: successful decode of `M=M='\n",
217
if (av_base64_decode(str, "MQ===", sizeof(str)) != -1) {
218
av_log(NULL, AV_LOG_ERROR,
219
"failed test %d: successful decode of `MQ==='\n",
156
printf("Encoding/decoding tests\n");
157
for (i = 0; i < FF_ARRAY_ELEMS(tests); i++)
158
error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref);