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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // First check, cannot fail
  22. assert(std::strlen(kBase64) == 64);
  23. // Test regular C strings
  24. {
  25. const char strHelloWorld[] = "Hello World\n\0";
  26. const char b64HelloWorld[] = "SGVsbG8gV29ybGQK\0";
  27. // encode "Hello World" to base64
  28. size_t bufSize = std::strlen(strHelloWorld);
  29. char bufEncoded[carla_base64_encoded_len(bufSize) + 1];
  30. carla_base64_encode((const uint8_t*)strHelloWorld, bufSize, bufEncoded);
  31. assert(std::strcmp(b64HelloWorld, bufEncoded) == 0);
  32. // decode base64 "SGVsbG8gV29ybGQK" back to "Hello World"
  33. uint8_t bufDecoded[carla_base64_decoded_max_len(b64HelloWorld)];
  34. size_t bufDecSize = carla_base64_decode(b64HelloWorld, bufDecoded);
  35. char strDecoded[bufDecSize+1];
  36. std::strncpy(strDecoded, (char*)bufDecoded, bufDecSize);
  37. strDecoded[bufDecSize] = '\0';
  38. assert(std::strcmp(strHelloWorld, strDecoded) == 0);
  39. assert(bufSize == bufDecSize);
  40. }
  41. struct Blob {
  42. char s[4];
  43. int i;
  44. double d;
  45. void* ptr;
  46. int32_t i32;
  47. int64_t i64;
  48. bool b;
  49. } blob;
  50. const size_t blobSize = sizeof(Blob);
  51. carla_zeroStruct<Blob>(blob);
  52. blob.s[0] = '1';
  53. blob.s[1] = 's';
  54. blob.s[2] = 't';
  55. blob.i = 228;
  56. blob.d = 3.33333333333;
  57. blob.ptr = (void*)0x500;
  58. blob.i32 = 32;
  59. blob.i64 = 64;
  60. blob.b = true;
  61. // binary -> base64
  62. char blobEnc[carla_base64_encoded_len(blobSize) + 1];
  63. carla_base64_encode((const uint8_t*)&blob, blobSize, blobEnc);
  64. std::printf("BINARY '%s', size:" P_SIZE "\n", blobEnc, blobSize);
  65. {
  66. // base64 -> binary
  67. uint8_t blobDec[carla_base64_decoded_max_len(blobEnc)];
  68. carla_base64_decode(blobEnc, blobDec);
  69. Blob blobTest;
  70. carla_zeroStruct<Blob>(blobTest);
  71. std::memcpy(&blobTest, blobDec, sizeof(Blob));
  72. assert(blobTest.s[0] == '1');
  73. assert(blobTest.s[1] == 's');
  74. assert(blobTest.s[2] == 't');
  75. assert(blobTest.i == 228);
  76. assert(blobTest.d == 3.33333333333);
  77. assert(blobTest.ptr == (void*)0x500);
  78. assert(blobTest.i32 == 32);
  79. assert(blobTest.i64 == 64);
  80. assert(blobTest.b == true);
  81. }
  82. {
  83. // CarlaString usage
  84. CarlaString string;
  85. string.importBinaryAsBase64<Blob>(&blob);
  86. assert(std::strcmp(blobEnc, (const char*)string) == 0);
  87. assert(std::strlen(blobEnc) == string.length());
  88. Blob* blobNew = string.exportAsBase64Binary<Blob>();
  89. assert(blobNew->s[0] == '1');
  90. assert(blobNew->s[1] == 's');
  91. assert(blobNew->s[2] == 't');
  92. assert(blobNew->i == 228);
  93. assert(blobNew->d == 3.33333333333);
  94. assert(blobNew->ptr == (void*)0x500);
  95. assert(blobNew->i32 == 32);
  96. assert(blobNew->i64 == 64);
  97. assert(blobNew->b == true);
  98. delete blobNew;
  99. }
  100. return 0;
  101. }