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.

160 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. bool Base64::convertToBase64 (OutputStream& base64Result, const void* sourceData, size_t sourceDataSize)
  18. {
  19. static const char lookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  20. const uint8* source = static_cast<const uint8*> (sourceData);
  21. while (sourceDataSize > 0)
  22. {
  23. char frame[4];
  24. const uint8 byte0 = *source++;
  25. frame[0] = lookup [(byte0 & 0xfcu) >> 2];
  26. uint32 bits = (byte0 & 0x03u) << 4;
  27. if (sourceDataSize > 1)
  28. {
  29. const uint8 byte1 = *source++;
  30. frame[1] = lookup[bits | ((byte1 & 0xf0u) >> 4)];
  31. bits = (byte1 & 0x0fu) << 2;
  32. if (sourceDataSize > 2)
  33. {
  34. const uint8 byte2 = *source++;
  35. frame[2] = lookup[bits | ((byte2 & 0xc0u) >> 6)];
  36. frame[3] = lookup[byte2 & 0x3fu];
  37. sourceDataSize -= 3;
  38. }
  39. else
  40. {
  41. frame[2] = lookup[bits];
  42. frame[3] = '=';
  43. sourceDataSize = 0;
  44. }
  45. }
  46. else
  47. {
  48. frame[1] = lookup[bits];
  49. frame[2] = '=';
  50. frame[3] = '=';
  51. sourceDataSize = 0;
  52. }
  53. if (! base64Result.write (frame, 4))
  54. return false;
  55. }
  56. return true;
  57. }
  58. bool Base64::convertFromBase64 (OutputStream& binaryOutput, StringRef base64TextInput)
  59. {
  60. for (String::CharPointerType s = base64TextInput.text; ! s.isEmpty();)
  61. {
  62. uint8 data[4];
  63. for (int i = 0; i < 4; ++i)
  64. {
  65. uint32 c = (uint32) s.getAndAdvance();
  66. if (c >= 'A' && c <= 'Z') c -= 'A';
  67. else if (c >= 'a' && c <= 'z') c -= 'a' - 26;
  68. else if (c >= '0' && c <= '9') c += 52 - '0';
  69. else if (c == '+') c = 62;
  70. else if (c == '/') c = 63;
  71. else if (c == '=') { c = 64; if (i <= 1) return false; }
  72. else return false;
  73. data[i] = (uint8) c;
  74. }
  75. binaryOutput.writeByte ((char) ((data[0] << 2) | (data[1] >> 4)));
  76. if (data[2] < 64)
  77. {
  78. binaryOutput.writeByte ((char) ((data[1] << 4) | (data[2] >> 2)));
  79. if (data[3] < 64)
  80. binaryOutput.writeByte ((char) ((data[2] << 6) | data[3]));
  81. }
  82. }
  83. return true;
  84. }
  85. String Base64::toBase64 (const void* sourceData, size_t sourceDataSize)
  86. {
  87. MemoryOutputStream m ((sourceDataSize * 4) / 3 + 3);
  88. bool ok = convertToBase64 (m, sourceData, sourceDataSize);
  89. jassert (ok); // should always succeed for this simple case
  90. ignoreUnused (ok);
  91. return m.toString();
  92. }
  93. String Base64::toBase64 (const String& text)
  94. {
  95. return toBase64 (text.toRawUTF8(), strlen (text.toRawUTF8()));
  96. }
  97. //==============================================================================
  98. //==============================================================================
  99. #if JUCE_UNIT_TESTS
  100. class Base64Tests : public UnitTest
  101. {
  102. public:
  103. Base64Tests() : UnitTest ("Base64 class") {}
  104. static MemoryBlock createRandomData (Random& r)
  105. {
  106. MemoryOutputStream m;
  107. for (int i = r.nextInt (400); --i >= 0;)
  108. m.writeByte ((char) r.nextInt (256));
  109. return m.getMemoryBlock();
  110. }
  111. void runTest() override
  112. {
  113. beginTest ("Base64");
  114. Random r = getRandom();
  115. for (int i = 1000; --i >= 0;)
  116. {
  117. const MemoryBlock original (createRandomData (r));
  118. String asBase64 (Base64::toBase64 (original.getData(), original.getSize()));
  119. MemoryOutputStream out;
  120. expect (Base64::convertFromBase64 (out, asBase64));
  121. MemoryBlock result = out.getMemoryBlock();
  122. expect (result == original);
  123. }
  124. }
  125. };
  126. static Base64Tests base64Tests;
  127. #endif