base64.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // LCOV_EXCL_START
  19. #include "libavutil/timer.h"
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include "libavutil/common.h"
  23. #include "libavutil/base64.h"
  24. #define MAX_DATA_SIZE 1024
  25. #define MAX_ENCODED_SIZE 2048
  26. static int test_encode_decode(const uint8_t *data, unsigned int data_size,
  27. const char *encoded_ref)
  28. {
  29. char encoded[MAX_ENCODED_SIZE];
  30. uint8_t data2[MAX_DATA_SIZE];
  31. int data2_size, max_data2_size = MAX_DATA_SIZE;
  32. if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
  33. printf("Failed: cannot encode the input data\n");
  34. return 1;
  35. }
  36. if (encoded_ref && strcmp(encoded, encoded_ref)) {
  37. printf("Failed: encoded string differs from reference\n"
  38. "Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref);
  39. return 1;
  40. }
  41. if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) != data_size) {
  42. printf("Failed: cannot decode the encoded string\n"
  43. "Encoded:\n%s\n", encoded);
  44. return 1;
  45. }
  46. if ((data2_size = av_base64_decode(data2, encoded, data_size)) != data_size) {
  47. printf("Failed: cannot decode with minimal buffer\n"
  48. "Encoded:\n%s\n", encoded);
  49. return 1;
  50. }
  51. if (memcmp(data2, data, data_size)) {
  52. printf("Failed: encoded/decoded data differs from original data\n");
  53. return 1;
  54. }
  55. if (av_base64_decode(NULL, encoded, 0) != 0) {
  56. printf("Failed: decode to NULL buffer\n");
  57. return 1;
  58. }
  59. if (strlen(encoded)) {
  60. char *end = strchr(encoded, '=');
  61. if (!end)
  62. end = encoded + strlen(encoded) - 1;
  63. *end = '%';
  64. if (av_base64_decode(NULL, encoded, 0) >= 0) {
  65. printf("Failed: error detection\n");
  66. return 1;
  67. }
  68. }
  69. printf("Passed!\n");
  70. return 0;
  71. }
  72. int main(int argc, char ** argv)
  73. {
  74. int i, error_count = 0;
  75. struct test {
  76. const uint8_t *data;
  77. const char *encoded_ref;
  78. } tests[] = {
  79. { "", ""},
  80. { "1", "MQ=="},
  81. { "22", "MjI="},
  82. { "333", "MzMz"},
  83. { "4444", "NDQ0NA=="},
  84. { "55555", "NTU1NTU="},
  85. { "666666", "NjY2NjY2"},
  86. { "abc:def", "YWJjOmRlZg=="},
  87. };
  88. char in[1024], out[2048];
  89. printf("Encoding/decoding tests\n");
  90. for (i = 0; i < FF_ARRAY_ELEMS(tests); i++)
  91. error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref);
  92. if (argc>1 && !strcmp(argv[1], "-t")) {
  93. memset(in, 123, sizeof(in));
  94. for(i=0; i<10000; i++){
  95. START_TIMER
  96. av_base64_encode(out, sizeof(out), in, sizeof(in));
  97. STOP_TIMER("encode")
  98. }
  99. for(i=0; i<10000; i++){
  100. START_TIMER
  101. av_base64_decode(in, out, sizeof(in));
  102. STOP_TIMER("decode")
  103. }
  104. for(i=0; i<10000; i++){
  105. START_TIMER
  106. av_base64_decode(NULL, out, 0);
  107. STOP_TIMER("syntax check")
  108. }
  109. }
  110. if (error_count)
  111. printf("Error Count: %d.\n", error_count);
  112. return !!error_count;
  113. }
  114. // LCOV_EXCL_STOP