The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

166 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. bool Base64::convertToBase64 (OutputStream& base64Result, const void* sourceData, size_t sourceDataSize)
  20. {
  21. static const char lookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  22. auto* source = static_cast<const uint8*> (sourceData);
  23. while (sourceDataSize > 0)
  24. {
  25. char frame[4];
  26. auto byte0 = *source++;
  27. frame[0] = lookup [(byte0 & 0xfcu) >> 2];
  28. uint32 bits = (byte0 & 0x03u) << 4;
  29. if (sourceDataSize > 1)
  30. {
  31. auto byte1 = *source++;
  32. frame[1] = lookup[bits | ((byte1 & 0xf0u) >> 4)];
  33. bits = (byte1 & 0x0fu) << 2;
  34. if (sourceDataSize > 2)
  35. {
  36. auto byte2 = *source++;
  37. frame[2] = lookup[bits | ((byte2 & 0xc0u) >> 6)];
  38. frame[3] = lookup[byte2 & 0x3fu];
  39. sourceDataSize -= 3;
  40. }
  41. else
  42. {
  43. frame[2] = lookup[bits];
  44. frame[3] = '=';
  45. sourceDataSize = 0;
  46. }
  47. }
  48. else
  49. {
  50. frame[1] = lookup[bits];
  51. frame[2] = '=';
  52. frame[3] = '=';
  53. sourceDataSize = 0;
  54. }
  55. if (! base64Result.write (frame, 4))
  56. return false;
  57. }
  58. return true;
  59. }
  60. bool Base64::convertFromBase64 (OutputStream& binaryOutput, StringRef base64TextInput)
  61. {
  62. for (auto s = base64TextInput.text; ! s.isEmpty();)
  63. {
  64. uint8 data[4];
  65. for (int i = 0; i < 4; ++i)
  66. {
  67. auto c = (uint32) s.getAndAdvance();
  68. if (c >= 'A' && c <= 'Z') c -= 'A';
  69. else if (c >= 'a' && c <= 'z') c -= 'a' - 26;
  70. else if (c >= '0' && c <= '9') c += 52 - '0';
  71. else if (c == '+') c = 62;
  72. else if (c == '/') c = 63;
  73. else if (c == '=') { c = 64; if (i <= 1) return false; }
  74. else return false;
  75. data[i] = (uint8) c;
  76. }
  77. binaryOutput.writeByte ((char) ((data[0] << 2) | (data[1] >> 4)));
  78. if (data[2] < 64)
  79. {
  80. binaryOutput.writeByte ((char) ((data[1] << 4) | (data[2] >> 2)));
  81. if (data[3] < 64)
  82. binaryOutput.writeByte ((char) ((data[2] << 6) | data[3]));
  83. }
  84. }
  85. return true;
  86. }
  87. String Base64::toBase64 (const void* sourceData, size_t sourceDataSize)
  88. {
  89. MemoryOutputStream m ((sourceDataSize * 4) / 3 + 3);
  90. [[maybe_unused]] bool ok = convertToBase64 (m, sourceData, sourceDataSize);
  91. jassert (ok); // should always succeed for this simple case
  92. return m.toString();
  93. }
  94. String Base64::toBase64 (const String& text)
  95. {
  96. return toBase64 (text.toRawUTF8(), strlen (text.toRawUTF8()));
  97. }
  98. //==============================================================================
  99. //==============================================================================
  100. #if JUCE_UNIT_TESTS
  101. class Base64Tests final : public UnitTest
  102. {
  103. public:
  104. Base64Tests()
  105. : UnitTest ("Base64 class", UnitTestCategories::text)
  106. {}
  107. static MemoryBlock createRandomData (Random& r)
  108. {
  109. MemoryOutputStream m;
  110. for (int i = r.nextInt (400); --i >= 0;)
  111. m.writeByte ((char) r.nextInt (256));
  112. return m.getMemoryBlock();
  113. }
  114. void runTest() override
  115. {
  116. beginTest ("Base64");
  117. auto r = getRandom();
  118. for (int i = 1000; --i >= 0;)
  119. {
  120. auto original = createRandomData (r);
  121. auto asBase64 = Base64::toBase64 (original.getData(), original.getSize());
  122. MemoryOutputStream out;
  123. expect (Base64::convertFromBase64 (out, asBase64));
  124. auto result = out.getMemoryBlock();
  125. expect (result == original);
  126. }
  127. }
  128. };
  129. static Base64Tests base64Tests;
  130. #endif
  131. } // namespace juce