FFmpeg
base64.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 // LCOV_EXCL_START
20 
21 #include "libavutil/timer.h"
22 
23 #include <stdint.h>
24 #include <stdio.h>
25 
26 #include "libavutil/common.h"
27 #include "libavutil/base64.h"
28 
29 #define MAX_DATA_SIZE 1024
30 #define MAX_ENCODED_SIZE 2048
31 
32 static int test_encode_decode(const uint8_t *data, unsigned int data_size,
33  const char *encoded_ref)
34 {
35  char encoded[MAX_ENCODED_SIZE];
36  uint8_t data2[MAX_DATA_SIZE];
37  int data2_size, max_data2_size = MAX_DATA_SIZE;
38 
39  if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
40  printf("Failed: cannot encode the input data\n");
41  return 1;
42  }
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);
46  return 1;
47  }
48 
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);
52  return 1;
53  }
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);
57  return 1;
58  }
59  if (memcmp(data2, data, data_size)) {
60  printf("Failed: encoded/decoded data differs from original data\n");
61  return 1;
62  }
63  if (av_base64_decode(NULL, encoded, 0) != 0) {
64  printf("Failed: decode to NULL buffer\n");
65  return 1;
66  }
67  if (strlen(encoded)) {
68  char *end = strchr(encoded, '=');
69  if (!end)
70  end = encoded + strlen(encoded) - 1;
71  *end = '%';
72  if (av_base64_decode(NULL, encoded, 0) >= 0) {
73  printf("Failed: error detection\n");
74  return 1;
75  }
76  }
77 
78  printf("Passed!\n");
79  return 0;
80 }
81 
82 int main(int argc, char ** argv)
83 {
84  int i, error_count = 0;
85  struct test {
86  const uint8_t *data;
87  const char *encoded_ref;
88  } tests[] = {
89  { "", ""},
90  { "1", "MQ=="},
91  { "22", "MjI="},
92  { "333", "MzMz"},
93  { "4444", "NDQ0NA=="},
94  { "55555", "NTU1NTU="},
95  { "666666", "NjY2NjY2"},
96  { "abc:def", "YWJjOmRlZg=="},
97  };
98  char in[1024], out[2048];
99 
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);
103 
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));
109  STOP_TIMER("encode")
110  }
111  for(i=0; i<10000; i++){
113  av_base64_decode(in, out, sizeof(in));
114  STOP_TIMER("decode")
115  }
116 
117  for(i=0; i<10000; i++){
120  STOP_TIMER("syntax check")
121  }
122  }
123 
124  if (error_count)
125  printf("Error Count: %d.\n", error_count);
126 
127  return !!error_count;
128 }
129 
130 // LCOV_EXCL_STOP
out
FILE * out
Definition: movenc.c:54
data
const char data[16]
Definition: mxf.c:143
test
Definition: idctdsp.c:34
main
int main(int argc, char **argv)
Definition: base64.c:82
START_TIMER
#define START_TIMER
Definition: timer.h:150
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
test_encode_decode
static int test_encode_decode(const uint8_t *data, unsigned int data_size, const char *encoded_ref)
Definition: base64.c:32
MAX_DATA_SIZE
#define MAX_DATA_SIZE
Definition: base64.c:29
NULL
#define NULL
Definition: coverity.c:32
timer.h
av_base64_decode
int av_base64_decode(uint8_t *out, const char *in_str, int out_size)
Decode a base64-encoded string.
Definition: base64.c:81
base64.h
printf
printf("static const uint8_t my_array[100] = {\n")
tests
const TestCase tests[]
Definition: fifo_muxer.c:242
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
MAX_ENCODED_SIZE
#define MAX_ENCODED_SIZE
Definition: base64.c:30
common.h
av_base64_encode
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
Definition: base64.c:145
STOP_TIMER
#define STOP_TIMER(id)
Definition: timer.h:151