juce_Base64.cpp 5.4 KB

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