FFmpeg  4.0
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++){
119  av_base64_decode(NULL, out, 0);
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
#define NULL
Definition: coverity.c:32
static int test_encode_decode(const uint8_t *data, unsigned int data_size, const char *encoded_ref)
Definition: base64.c:32
#define MAX_DATA_SIZE
Definition: base64.c:29
#define MAX_ENCODED_SIZE
Definition: base64.c:30
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
const TestCase tests[]
Definition: fifo_muxer.c:245
const char data[16]
Definition: mxf.c:90
high precision timer, useful to profile code
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:138
#define FF_ARRAY_ELEMS(a)
#define START_TIMER
Definition: timer.h:137
static void test(const char *pattern, const char *host)
Definition: noproxy.c:23
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
common internal and external API header
#define STOP_TIMER(id)
Definition: timer.h:138
int av_base64_decode(uint8_t *out, const char *in_str, int out_size)
Decode a base64-encoded string.
Definition: base64.c:79
FILE * out
Definition: movenc.c:54
int main(int argc, char **argv)
Definition: base64.c:82