2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/common.h"
24 #include "libavutil/base64.h"
25 #include "libavutil/timer.h"
27 #define MAX_DATA_SIZE 1024
28 #define MAX_ENCODED_SIZE 2048
30 static int test_encode_decode(const uint8_t *data, unsigned int data_size,
31 const char *encoded_ref)
33 char encoded[MAX_ENCODED_SIZE];
34 uint8_t data2[MAX_DATA_SIZE];
35 int data2_size, max_data2_size = MAX_DATA_SIZE;
37 if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
38 printf("Failed: cannot encode the input data\n");
41 if (encoded_ref && strcmp(encoded, encoded_ref)) {
42 printf("Failed: encoded string differs from reference\n"
43 "Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref);
47 if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) != data_size) {
48 printf("Failed: cannot decode the encoded string\n"
49 "Encoded:\n%s\n", encoded);
52 if ((data2_size = av_base64_decode(data2, encoded, data_size)) != data_size) {
53 printf("Failed: cannot decode with minimal buffer\n"
54 "Encoded:\n%s\n", encoded);
57 if (memcmp(data2, data, data_size)) {
58 printf("Failed: encoded/decoded data differs from original data\n");
61 if (av_base64_decode(NULL, encoded, 0) != 0) {
62 printf("Failed: decode to NULL buffer\n");
65 if (strlen(encoded)) {
66 char *end = strchr(encoded, '=');
68 end = encoded + strlen(encoded) - 1;
70 if (av_base64_decode(NULL, encoded, 0) >= 0) {
71 printf("Failed: error detection\n");
80 int main(int argc, char ** argv)
82 int i, error_count = 0;
85 const char *encoded_ref;
91 { "4444", "NDQ0NA=="},
92 { "55555", "NTU1NTU="},
93 { "666666", "NjY2NjY2"},
94 { "abc:def", "YWJjOmRlZg=="},
96 char in[1024], out[2048];
98 printf("Encoding/decoding tests\n");
99 for (i = 0; i < FF_ARRAY_ELEMS(tests); i++)
100 error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref);
102 if (argc>1 && !strcmp(argv[1], "-t")) {
103 memset(in, 123, sizeof(in));
104 for(i=0; i<10000; i++){
106 av_base64_encode(out, sizeof(out), in, sizeof(in));
109 for(i=0; i<10000; i++){
111 av_base64_decode(in, out, sizeof(in));
115 for(i=0; i<10000; i++){
117 av_base64_decode(NULL, out, 0);
118 STOP_TIMER("syntax check")
123 printf("Error Count: %d.\n", error_count);
125 return !!error_count;