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
21 #include "libavutil/timer.h"
26 #include "libavutil/common.h"
27 #include "libavutil/base64.h"
29 #define MAX_DATA_SIZE 1024
30 #define MAX_ENCODED_SIZE 2048
32 static int test_encode_decode(const uint8_t *data, unsigned int data_size,
33 const char *encoded_ref)
35 char encoded[MAX_ENCODED_SIZE];
36 uint8_t data2[MAX_DATA_SIZE];
37 int data2_size, max_data2_size = MAX_DATA_SIZE;
39 if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
40 printf("Failed: cannot encode the input data\n");
43 if (encoded_ref && strcmp(encoded, encoded_ref)) {
44 printf("Failed: encoded string differs from reference\n"
45 "Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref);
49 if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) != data_size) {
50 printf("Failed: cannot decode the encoded string\n"
51 "Encoded:\n%s\n", encoded);
54 if ((data2_size = av_base64_decode(data2, encoded, data_size)) != data_size) {
55 printf("Failed: cannot decode with minimal buffer\n"
56 "Encoded:\n%s\n", encoded);
59 if (memcmp(data2, data, data_size)) {
60 printf("Failed: encoded/decoded data differs from original data\n");
63 if (av_base64_decode(NULL, encoded, 0) != 0) {
64 printf("Failed: decode to NULL buffer\n");
67 if (strlen(encoded)) {
68 char *end = strchr(encoded, '=');
70 end = encoded + strlen(encoded) - 1;
72 if (av_base64_decode(NULL, encoded, 0) >= 0) {
73 printf("Failed: error detection\n");
82 int main(int argc, char ** argv)
84 int i, error_count = 0;
87 const char *encoded_ref;
93 { "4444", "NDQ0NA=="},
94 { "55555", "NTU1NTU="},
95 { "666666", "NjY2NjY2"},
96 { "abc:def", "YWJjOmRlZg=="},
98 char in[1024], out[2048];
100 printf("Encoding/decoding tests\n");
101 for (i = 0; i < FF_ARRAY_ELEMS(tests); i++)
102 error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref);
104 if (argc>1 && !strcmp(argv[1], "-t")) {
105 memset(in, 123, sizeof(in));
106 for(i=0; i<10000; i++){
108 av_base64_encode(out, sizeof(out), in, sizeof(in));
111 for(i=0; i<10000; i++){
113 av_base64_decode(in, out, sizeof(in));
117 for(i=0; i<10000; i++){
119 av_base64_decode(NULL, out, 0);
120 STOP_TIMER("syntax check")
125 printf("Error Count: %d.\n", error_count);
127 return !!error_count;