FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include <stdint.h>
21 #include <stdio.h>
22 
23 #include "libavutil/common.h"
24 #include "libavutil/base64.h"
25 #include "libavutil/timer.h"
26 
27 #define MAX_DATA_SIZE 1024
28 #define MAX_ENCODED_SIZE 2048
29 
30 static int test_encode_decode(const uint8_t *data, unsigned int data_size,
31  const char *encoded_ref)
32 {
33  char encoded[MAX_ENCODED_SIZE];
34  uint8_t data2[MAX_DATA_SIZE];
35  int data2_size, max_data2_size = MAX_DATA_SIZE;
36 
37  if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
38  printf("Failed: cannot encode the input data\n");
39  return 1;
40  }
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);
44  return 1;
45  }
46 
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);
50  return 1;
51  }
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);
55  return 1;
56  }
57  if (memcmp(data2, data, data_size)) {
58  printf("Failed: encoded/decoded data differs from original data\n");
59  return 1;
60  }
61  if (av_base64_decode(NULL, encoded, 0) != 0) {
62  printf("Failed: decode to NULL buffer\n");
63  return 1;
64  }
65  if (strlen(encoded)) {
66  char *end = strchr(encoded, '=');
67  if (!end)
68  end = encoded + strlen(encoded) - 1;
69  *end = '%';
70  if (av_base64_decode(NULL, encoded, 0) >= 0) {
71  printf("Failed: error detection\n");
72  return 1;
73  }
74  }
75 
76  printf("Passed!\n");
77  return 0;
78 }
79 
80 int main(int argc, char ** argv)
81 {
82  int i, error_count = 0;
83  struct test {
84  const uint8_t *data;
85  const char *encoded_ref;
86  } tests[] = {
87  { "", ""},
88  { "1", "MQ=="},
89  { "22", "MjI="},
90  { "333", "MzMz"},
91  { "4444", "NDQ0NA=="},
92  { "55555", "NTU1NTU="},
93  { "666666", "NjY2NjY2"},
94  { "abc:def", "YWJjOmRlZg=="},
95  };
96  char in[1024], out[2048];
97 
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);
101 
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));
107  STOP_TIMER("encode")
108  }
109  for(i=0; i<10000; i++){
111  av_base64_decode(in, out, sizeof(in));
112  STOP_TIMER("decode")
113  }
114 
115  for(i=0; i<10000; i++){
117  av_base64_decode(NULL, out, 0);
118  STOP_TIMER("syntax check")
119  }
120  }
121 
122  if (error_count)
123  printf("Error Count: %d.\n", error_count);
124 
125  return !!error_count;
126 }
127 
128 // LCOV_EXCL_STOP
#define NULL
Definition: coverity.c:32
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static int test_encode_decode(const uint8_t *data, unsigned int data_size, const char *encoded_ref)
Definition: base64.c:30
#define MAX_DATA_SIZE
Definition: base64.c:27
#define MAX_ENCODED_SIZE
Definition: base64.c:28
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
const TestCase tests[]
Definition: fifo_muxer.c:397
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:94
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,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
common internal and external API header
#define STOP_TIMER(id)
Definition: timer.h:95
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:80