Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Base64.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Carla Tests
  3. * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #include "CarlaBase64Utils.hpp"
  18. #include "CarlaString.hpp"
  19. int main()
  20. {
  21. CARLA_ASSERT(std::strlen(kBase64) == 64);
  22. struct Blob {
  23. char s[4];
  24. int i;
  25. double d;
  26. char padding[100];
  27. void* ptr;
  28. Blob()
  29. : s{'1', 's', 't', 0},
  30. i(228),
  31. d(3.33333333333),
  32. ptr((void*)0x500)
  33. {
  34. carla_zeroMem(padding, sizeof(char)*100);
  35. }
  36. } blob;
  37. // binary -> base64
  38. void* const test0 = &blob;
  39. size_t test0Len = sizeof(Blob);
  40. char buf0[carla_base64_encoded_len(test0Len) + 1];
  41. carla_base64_encode((const uint8_t*)test0, test0Len, buf0);
  42. printf("BINARY '%s'\n", buf0);
  43. char buf0dec[carla_base64_decoded_max_len(buf0)+1];
  44. carla_zeroMem(buf0dec, sizeof(buf0dec));
  45. carla_base64_decode(buf0, (uint8_t*)buf0dec);
  46. Blob blobTester;
  47. blobTester.s[0] = 0;
  48. blobTester.i = 0;
  49. blobTester.d = 9999.99999999999999;
  50. std::memcpy(&blobTester, buf0dec, sizeof(Blob));
  51. CARLA_ASSERT(std::strcmp(blobTester.s, "1st") == 0);
  52. CARLA_ASSERT(blobTester.i == 228);
  53. CARLA_ASSERT(blobTester.d == 3.33333333333);
  54. // string -> base64
  55. const char* const test1 = "Hello World!";
  56. size_t test1Len = std::strlen(test1);
  57. char buf1[carla_base64_encoded_len(test1Len) + 1];
  58. carla_base64_encode((const uint8_t*)test1, test1Len, buf1);
  59. printf("'%s' => '%s'\n", test1, buf1);
  60. // base64 -> string
  61. const char* const test2 = "SGVsbG8gV29ybGQh";
  62. char buf2[carla_base64_decoded_max_len(test2)+1];
  63. carla_zeroMem(buf2, sizeof(buf2));
  64. carla_base64_decode(test2, (uint8_t*)buf2);
  65. printf("'%s' => '%s'\n", test2, buf2);
  66. printf("'%s' == '%s'\n", buf1, test2);
  67. printf("'%s' == '%s'\n", buf2, test1);
  68. CARLA_ASSERT(std::strcmp(buf1, test2) == 0);
  69. CARLA_ASSERT(std::strcmp(buf2, test1) == 0);
  70. // -----------------------------------------------------------------
  71. blob.s[0] = 'X';
  72. blob.i = 92320;
  73. blob.d = 9999887.99999999999999;
  74. CarlaString string;
  75. string.importBinaryAsBase64<Blob>(&blob);
  76. Blob* blob3 = string.exportAsBase64Binary<Blob>();
  77. CARLA_ASSERT(std::strcmp(blob3->s, blob.s) == 0);
  78. CARLA_ASSERT(blob3->i == blob.i);
  79. CARLA_ASSERT(blob3->d == blob.d);
  80. CARLA_ASSERT(blob3->ptr == blob.ptr);
  81. delete blob3;
  82. // -----------------------------------------------------------------
  83. return 0;
  84. }