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.

168 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. bool Base64::convertToBase64 (OutputStream& base64Result, const void* sourceData, size_t sourceDataSize)
  24. {
  25. static const char lookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  26. const uint8* source = static_cast<const uint8*> (sourceData);
  27. while (sourceDataSize > 0)
  28. {
  29. char frame[4];
  30. const uint8 byte0 = *source++;
  31. frame[0] = lookup [(byte0 & 0xfcu) >> 2];
  32. uint32 bits = (byte0 & 0x03u) << 4;
  33. if (sourceDataSize > 1)
  34. {
  35. const uint8 byte1 = *source++;
  36. frame[1] = lookup[bits | ((byte1 & 0xf0u) >> 4)];
  37. bits = (byte1 & 0x0fu) << 2;
  38. if (sourceDataSize > 2)
  39. {
  40. const uint8 byte2 = *source++;
  41. frame[2] = lookup[bits | ((byte2 & 0xc0u) >> 6)];
  42. frame[3] = lookup[byte2 & 0x3fu];
  43. sourceDataSize -= 3;
  44. }
  45. else
  46. {
  47. frame[2] = lookup[bits];
  48. frame[3] = '=';
  49. sourceDataSize = 0;
  50. }
  51. }
  52. else
  53. {
  54. frame[1] = lookup[bits];
  55. frame[2] = '=';
  56. frame[3] = '=';
  57. sourceDataSize = 0;
  58. }
  59. if (! base64Result.write (frame, 4))
  60. return false;
  61. }
  62. return true;
  63. }
  64. bool Base64::convertFromBase64 (OutputStream& binaryOutput, StringRef base64TextInput)
  65. {
  66. for (String::CharPointerType s = base64TextInput.text; ! s.isEmpty();)
  67. {
  68. uint8 data[4];
  69. for (int i = 0; i < 4; ++i)
  70. {
  71. uint32 c = (uint32) s.getAndAdvance();
  72. if (c >= 'A' && c <= 'Z') c -= 'A';
  73. else if (c >= 'a' && c <= 'z') c -= 'a' - 26;
  74. else if (c >= '0' && c <= '9') c += 52 - '0';
  75. else if (c == '+') c = 62;
  76. else if (c == '/') c = 63;
  77. else if (c == '=') { c = 64; if (i <= 1) return false; }
  78. else return false;
  79. data[i] = (uint8) c;
  80. }
  81. binaryOutput.writeByte ((char) ((data[0] << 2) | (data[1] >> 4)));
  82. if (data[2] < 64)
  83. {
  84. binaryOutput.writeByte ((char) ((data[1] << 4) | (data[2] >> 2)));
  85. if (data[3] < 64)
  86. binaryOutput.writeByte ((char) ((data[2] << 6) | data[3]));
  87. }
  88. }
  89. return true;
  90. }
  91. String Base64::toBase64 (const void* sourceData, size_t sourceDataSize)
  92. {
  93. MemoryOutputStream m ((sourceDataSize * 4) / 3 + 3);
  94. bool ok = convertToBase64 (m, sourceData, sourceDataSize);
  95. jassert (ok); // should always succeed for this simple case
  96. ignoreUnused (ok);
  97. return m.toString();
  98. }
  99. String Base64::toBase64 (const String& text)
  100. {
  101. return toBase64 (text.toRawUTF8(), strlen (text.toRawUTF8()));
  102. }
  103. //==============================================================================
  104. //==============================================================================
  105. #if JUCE_UNIT_TESTS
  106. class Base64Tests : public UnitTest
  107. {
  108. public:
  109. Base64Tests() : UnitTest ("Base64 class") {}
  110. static MemoryBlock createRandomData (Random& r)
  111. {
  112. MemoryOutputStream m;
  113. for (int i = r.nextInt (400); --i >= 0;)
  114. m.writeByte ((char) r.nextInt (256));
  115. return m.getMemoryBlock();
  116. }
  117. void runTest() override
  118. {
  119. beginTest ("Base64");
  120. Random r = getRandom();
  121. for (int i = 1000; --i >= 0;)
  122. {
  123. const MemoryBlock original (createRandomData (r));
  124. String asBase64 (Base64::toBase64 (original.getData(), original.getSize()));
  125. MemoryOutputStream out;
  126. expect (Base64::convertFromBase64 (out, asBase64));
  127. MemoryBlock result = out.getMemoryBlock();
  128. expect (result == original);
  129. }
  130. }
  131. };
  132. static Base64Tests base64Tests;
  133. #endif