| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 (data_size > 0 && (data2_size = av_base64_decode(data2, encoded, data_size - 1)) != data_size - 1) { | |
| 68 | − | printf("Failed: out of array write\n" | |
| 69 | "Encoded:\n%s\n", encoded); | ||
| 70 | − | return 1; | |
| 71 | } | ||
| 72 | − | if (data_size > 1 && (data2_size = av_base64_decode(data2, encoded, data_size - 2)) != data_size - 2) { | |
| 73 | − | printf("Failed: out of array write\n" | |
| 74 | "Encoded:\n%s\n", encoded); | ||
| 75 | − | return 1; | |
| 76 | } | ||
| 77 | − | if (strlen(encoded)) { | |
| 78 | − | char *end = strchr(encoded, '='); | |
| 79 | − | if (!end) | |
| 80 | − | end = encoded + strlen(encoded) - 1; | |
| 81 | − | *end = '%'; | |
| 82 | − | if (av_base64_decode(NULL, encoded, 0) >= 0) { | |
| 83 | − | printf("Failed: error detection\n"); | |
| 84 | − | return 1; | |
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | − | printf("Passed!\n"); | |
| 89 | − | return 0; | |
| 90 | } | ||
| 91 | |||
| 92 | − | int main(int argc, char ** argv) | |
| 93 | { | ||
| 94 | − | int i, error_count = 0; | |
| 95 | struct test { | ||
| 96 | const uint8_t *data; | ||
| 97 | const char *encoded_ref; | ||
| 98 | − | } tests[] = { | |
| 99 | { "", ""}, | ||
| 100 | { "1", "MQ=="}, | ||
| 101 | { "22", "MjI="}, | ||
| 102 | { "333", "MzMz"}, | ||
| 103 | { "4444", "NDQ0NA=="}, | ||
| 104 | { "55555", "NTU1NTU="}, | ||
| 105 | { "666666", "NjY2NjY2"}, | ||
| 106 | { "abc:def", "YWJjOmRlZg=="}, | ||
| 107 | }; | ||
| 108 | char in[1024], out[2048]; | ||
| 109 | |||
| 110 | − | printf("Encoding/decoding tests\n"); | |
| 111 | − | for (i = 0; i < FF_ARRAY_ELEMS(tests); i++) | |
| 112 | − | error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref); | |
| 113 | |||
| 114 | − | if (argc>1 && !strcmp(argv[1], "-t")) { | |
| 115 | − | memset(in, 123, sizeof(in)); | |
| 116 | − | for(i=0; i<10000; i++){ | |
| 117 | − | START_TIMER | |
| 118 | − | av_base64_encode(out, sizeof(out), in, sizeof(in)); | |
| 119 | − | STOP_TIMER("encode") | |
| 120 | } | ||
| 121 | − | for(i=0; i<10000; i++){ | |
| 122 | − | START_TIMER | |
| 123 | − | av_base64_decode(in, out, sizeof(in)); | |
| 124 | − | STOP_TIMER("decode") | |
| 125 | } | ||
| 126 | |||
| 127 | − | for(i=0; i<10000; i++){ | |
| 128 | − | START_TIMER | |
| 129 | − | av_base64_decode(NULL, out, 0); | |
| 130 | − | STOP_TIMER("syntax check") | |
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | − | if (error_count) | |
| 135 | − | printf("Error Count: %d.\n", error_count); | |
| 136 | |||
| 137 | − | return !!error_count; | |
| 138 | } | ||
| 139 | |||
| 140 | // LCOV_EXCL_STOP | ||
| 141 |