1
0

base64_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * @build: gcc -o bin/base64 unittest/base64_test.c util/base64.c -I. -Iutil
  3. *
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include "base64.h"
  10. static void test() {
  11. unsigned char in[] = "0123456789";
  12. // test encode
  13. int encoded_size = BASE64_ENCODE_OUT_SIZE(10);
  14. char* encoded = (char*)malloc(encoded_size + 1);
  15. encoded_size = hv_base64_encode(in, 10, encoded);
  16. encoded[encoded_size] = '\0';
  17. assert(strcmp(encoded, "MDEyMzQ1Njc4OQ==") == 0);
  18. // test decode
  19. int decoded_size = BASE64_DECODE_OUT_SIZE(encoded_size);
  20. unsigned char* decoded = (unsigned char*)malloc(decoded_size);
  21. decoded_size = hv_base64_decode(encoded, encoded_size, decoded);
  22. assert(decoded_size == 10 && memcmp(in, decoded, decoded_size) == 0);
  23. free(encoded);
  24. free(decoded);
  25. }
  26. int main(int argc, char* argv[]) {
  27. test();
  28. if (argc < 3) {
  29. printf("Usage: base64 infile outfile\n");
  30. printf(" base64 -d infile outfile\n");
  31. return -10;
  32. }
  33. else if (argc == 3) {
  34. // encode file
  35. const char* infile = argv[1];
  36. const char* outfile = argv[2];
  37. FILE* infp = fopen(infile, "rb");
  38. if (infp == NULL) {
  39. printf("Open file '%s' failed!\n", infile);
  40. return -20;
  41. }
  42. fseek(infp, 0, SEEK_END);
  43. long filesize = ftell(infp);
  44. // printf("filesize=%ld\n", filesize);
  45. fseek(infp, 0, SEEK_SET);
  46. unsigned char* filebuf = (unsigned char*)malloc(filesize);
  47. size_t nread = fread(filebuf, 1, filesize, infp);
  48. assert(nread == filesize);
  49. int encoded_size = BASE64_ENCODE_OUT_SIZE(filesize);
  50. char* encoded = (char*)malloc(encoded_size + 1);
  51. encoded_size = hv_base64_encode(filebuf, filesize, encoded);
  52. encoded[encoded_size] = '\0';
  53. FILE* outfp = fopen(outfile, "w");
  54. if (outfp == NULL) {
  55. printf("Save file '%s' failed!\n", infile);
  56. return -20;
  57. }
  58. size_t nwrite = fwrite(encoded, 1, encoded_size, outfp);
  59. assert(nwrite == encoded_size);
  60. free(filebuf);
  61. free(encoded);
  62. fclose(infp);
  63. fclose(outfp);
  64. }
  65. else if (argc == 4) {
  66. const char* flags = argv[1];
  67. if (flags[0] == '-' && flags[1] == 'd') {
  68. // decode file
  69. const char* infile = argv[2];
  70. const char* outfile = argv[3];
  71. FILE* infp = fopen(infile, "r");
  72. if (infp == NULL) {
  73. printf("Open file '%s' failed!\n", infile);
  74. return -20;
  75. }
  76. fseek(infp, 0, SEEK_END);
  77. long filesize = ftell(infp);
  78. // printf("filesize=%ld\n", filesize);
  79. fseek(infp, 0, SEEK_SET);
  80. char* filebuf = (char*)malloc(filesize);
  81. size_t nread = fread(filebuf, 1, filesize, infp);
  82. assert(nread == filesize);
  83. int decoded_size = BASE64_DECODE_OUT_SIZE(filesize);
  84. unsigned char* decoded = (unsigned char*)malloc(decoded_size);
  85. decoded_size = hv_base64_decode(filebuf, filesize, decoded);
  86. FILE* outfp = fopen(outfile, "wb");
  87. if (outfp == NULL) {
  88. printf("Save file '%s' failed!\n", infile);
  89. return -20;
  90. }
  91. size_t nwrite = fwrite(decoded, 1, decoded_size, outfp);
  92. assert(nwrite == decoded_size);
  93. free(filebuf);
  94. free(decoded);
  95. fclose(infp);
  96. fclose(outfp);
  97. }
  98. else {
  99. printf("Unrecognized flags '%s'\n", flags);
  100. return -40;
  101. }
  102. }
  103. return 0;
  104. }