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.

380 lines
14KB

  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. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce::midi_ci
  19. {
  20. String Encodings::stringFrom7BitText (Span<const std::byte> bytes)
  21. {
  22. std::vector<CharPointer_UTF16::CharType> chars;
  23. while (! bytes.empty())
  24. {
  25. const auto front = (uint8_t) bytes.front();
  26. if ((front < 0x20 || 0x80 <= front) && front != 0x0a)
  27. {
  28. jassertfalse;
  29. return {};
  30. }
  31. if (front == '\\')
  32. {
  33. bytes = Span (bytes.data() + 1, bytes.size() - 1);
  34. if (bytes.empty())
  35. return {};
  36. const auto kind = (uint8_t) bytes.front();
  37. switch (kind)
  38. {
  39. case '"': chars.push_back ('"'); break;
  40. case '\\': chars.push_back ('\\'); break;
  41. case '/': chars.push_back ('/'); break;
  42. case 'b': chars.push_back ('\b'); break;
  43. case 'f': chars.push_back ('\f'); break;
  44. case 'n': chars.push_back ('\n'); break;
  45. case 'r': chars.push_back ('\r'); break;
  46. case 't': chars.push_back ('\t'); break;
  47. case 'u':
  48. {
  49. bytes = Span (bytes.data() + 1, bytes.size() - 1);
  50. if (bytes.size() < 4)
  51. return {};
  52. std::string byteStr (reinterpret_cast<const char*> (bytes.data()), 4);
  53. const auto unit = [&]() -> std::optional<CharPointer_UTF16::CharType>
  54. {
  55. try
  56. {
  57. return (CharPointer_UTF16::CharType) std::stoi (byteStr, {}, 16);
  58. }
  59. catch (...) {}
  60. jassertfalse;
  61. return {};
  62. }();
  63. if (! unit.has_value())
  64. return {};
  65. chars.push_back (*unit);
  66. bytes = Span (bytes.data() + 4, bytes.size() - 4);
  67. continue;
  68. }
  69. default:
  70. return {};
  71. }
  72. bytes = Span (bytes.data() + 1, bytes.size() - 1);
  73. }
  74. else
  75. {
  76. chars.push_back (front);
  77. bytes = Span (bytes.data() + 1, bytes.size() - 1);
  78. }
  79. }
  80. chars.push_back ({});
  81. return String { CharPointer_UTF16 { chars.data() } };
  82. }
  83. std::vector<std::byte> Encodings::stringTo7BitText (const String& text)
  84. {
  85. std::vector<std::byte> result;
  86. for (const auto character : text)
  87. {
  88. if (character == 0x0a || (0x20 <= character && character < 0x80))
  89. {
  90. result.emplace_back (std::byte (character));
  91. }
  92. else
  93. {
  94. // Suspiciously low ASCII value encountered!
  95. jassert (character >= 0x80);
  96. CharPointer_UTF16::CharType points[2]{};
  97. CharPointer_UTF16 asUTF16 { points };
  98. asUTF16.write (character);
  99. std::for_each (points, asUTF16.getAddress(), [&] (CharPointer_UTF16::CharType unit)
  100. {
  101. const auto str = String::toHexString (unit);
  102. result.insert (result.end(), { std::byte { '\\' }, std::byte { 'u' } });
  103. for (const auto c : str)
  104. result.push_back ((std::byte) c);
  105. });
  106. }
  107. }
  108. return result;
  109. }
  110. std::vector<std::byte> Encodings::toMcoded7 (Span<const std::byte> bytes)
  111. {
  112. std::vector<std::byte> result;
  113. result.reserve ((bytes.size() * 8) + 6 / 7);
  114. for (size_t index = 0; index < bytes.size(); index += 7)
  115. {
  116. std::array<std::byte, 7> slice{};
  117. const auto sliceSize = std::min ((size_t) 7, bytes.size() - index);
  118. std::copy (bytes.begin() + index, bytes.begin() + index + sliceSize, slice.begin());
  119. result.push_back ((slice[0] & std::byte { 0x80 }) >> 1
  120. | (slice[1] & std::byte { 0x80 }) >> 2
  121. | (slice[2] & std::byte { 0x80 }) >> 3
  122. | (slice[3] & std::byte { 0x80 }) >> 4
  123. | (slice[4] & std::byte { 0x80 }) >> 5
  124. | (slice[5] & std::byte { 0x80 }) >> 6
  125. | (slice[6] & std::byte { 0x80 }) >> 7);
  126. std::transform (slice.begin(),
  127. std::next (slice.begin(), (ptrdiff_t) sliceSize),
  128. std::back_inserter (result),
  129. [] (const std::byte b) { return b & std::byte { 0x7f }; });
  130. }
  131. return result;
  132. }
  133. std::vector<std::byte> Encodings::fromMcoded7 (Span<const std::byte> bytes)
  134. {
  135. std::vector<std::byte> result;
  136. result.reserve ((bytes.size() * 7) + 7 / 8);
  137. for (size_t index = 0; index < bytes.size(); index += 8)
  138. {
  139. const auto sliceSize = std::min ((size_t) 7, bytes.size() - index - 1);
  140. for (size_t i = 0; i < sliceSize; ++i)
  141. {
  142. const auto highBit = (bytes[index] & std::byte { (uint8_t) (1 << (6 - i)) }) << (i + 1);
  143. result.push_back (highBit | bytes[index + 1 + i]);
  144. }
  145. }
  146. return result;
  147. }
  148. std::optional<std::vector<std::byte>> Encodings::tryEncode (Span<const std::byte> bytes, Encoding mutualEncoding)
  149. {
  150. switch (mutualEncoding)
  151. {
  152. case Encoding::ascii:
  153. {
  154. if (std::all_of (bytes.begin(), bytes.end(), [] (auto b) { return (b & std::byte { 0x80 }) == std::byte{}; }))
  155. return std::vector<std::byte> (bytes.begin(), bytes.end());
  156. jassertfalse;
  157. return {};
  158. }
  159. case Encoding::mcoded7:
  160. return toMcoded7 (bytes);
  161. case Encoding::zlibAndMcoded7:
  162. {
  163. MemoryOutputStream memoryStream;
  164. GZIPCompressorOutputStream (memoryStream).write (bytes.data(), bytes.size());
  165. return toMcoded7 (Span (static_cast<const std::byte*> (memoryStream.getData()), memoryStream.getDataSize()));
  166. }
  167. }
  168. // Unknown encoding!
  169. jassertfalse;
  170. return {};
  171. }
  172. std::vector<std::byte> Encodings::decode (Span<const std::byte> bytes, Encoding mutualEncoding)
  173. {
  174. if (mutualEncoding == Encoding::ascii)
  175. {
  176. // All values must be 7-bit!
  177. jassert (std::none_of (bytes.begin(), bytes.end(), [] (const auto& b) { return (b & std::byte { 0x80 }) != std::byte{}; }));
  178. return std::vector<std::byte> (bytes.begin(), bytes.end());
  179. }
  180. if (mutualEncoding == Encoding::mcoded7)
  181. return fromMcoded7 (bytes);
  182. if (mutualEncoding == Encoding::zlibAndMcoded7)
  183. {
  184. const auto mcoded = fromMcoded7 (bytes);
  185. MemoryInputStream memoryStream (mcoded.data(), mcoded.size(), false);
  186. GZIPDecompressorInputStream zipStream (memoryStream);
  187. const size_t chunkSize = 1 << 8;
  188. std::vector<std::byte> result;
  189. for (;;)
  190. {
  191. const auto previousSize = result.size();
  192. result.resize (previousSize + chunkSize);
  193. const auto read = zipStream.read (result.data() + previousSize, chunkSize);
  194. if (read < 0)
  195. {
  196. // Decompression failed!
  197. jassertfalse;
  198. return {};
  199. }
  200. result.resize ((size_t) read + previousSize);
  201. if (read == 0)
  202. return result;
  203. }
  204. }
  205. // Unknown encoding!
  206. jassertfalse;
  207. return {};
  208. }
  209. #if JUCE_UNIT_TESTS
  210. class EncodingsTests : public UnitTest
  211. {
  212. public:
  213. EncodingsTests() : UnitTest ("Encodings", UnitTestCategories::midi) {}
  214. void runTest() override
  215. {
  216. beginTest ("7-bit text encoding");
  217. {
  218. {
  219. const auto converted = Encodings::stringTo7BitText (juce::CharPointer_UTF8 ("Accepted Beat \xe2\x99\xaa"));
  220. const auto expected = makeByteArray ('A', 'c', 'c', 'e', 'p', 't', 'e', 'd', ' ', 'B', 'e', 'a', 't', ' ', '\\', 'u', '2', '6', '6', 'a');
  221. expect (std::equal (converted.begin(), converted.end(), expected.begin(), expected.end()));
  222. }
  223. {
  224. const auto converted = Encodings::stringTo7BitText (juce::CharPointer_UTF8 ("\xe6\xae\x8b\xe3\x82\x8a\xe3\x82\x8f\xe3\x81\x9a\xe3\x81\x8b""5\xe3\x83\x90\xe3\x82\xa4\xe3\x83\x88"));
  225. const auto expected = makeByteArray ('\\', 'u', '6', 'b', '8', 'b',
  226. '\\', 'u', '3', '0', '8', 'a',
  227. '\\', 'u', '3', '0', '8', 'f',
  228. '\\', 'u', '3', '0', '5', 'a',
  229. '\\', 'u', '3', '0', '4', 'b',
  230. '5',
  231. '\\', 'u', '3', '0', 'd', '0',
  232. '\\', 'u', '3', '0', 'a', '4',
  233. '\\', 'u', '3', '0', 'c', '8');
  234. expect (std::equal (converted.begin(), converted.end(), expected.begin(), expected.end()));
  235. }
  236. }
  237. beginTest ("7-bit text decoding");
  238. {
  239. {
  240. const auto converted = Encodings::stringFrom7BitText (makeByteArray ('A', 'c', 'c', 'e', 'p', 't', 'e', 'd', ' ', 'B', 'e', 'a', 't', ' ', '\\', 'u', '2', '6', '6', 'a'));
  241. const String expected = juce::CharPointer_UTF8 ("Accepted Beat \xe2\x99\xaa");
  242. expect (converted == expected);
  243. }
  244. {
  245. const auto converted = Encodings::stringFrom7BitText (makeByteArray ('\\', 'u', '6', 'b', '8', 'b',
  246. '\\', 'u', '3', '0', '8', 'a',
  247. '\\', 'u', '3', '0', '8', 'f',
  248. '\\', 'u', '3', '0', '5', 'a',
  249. '\\', 'u', '3', '0', '4', 'b',
  250. '5',
  251. '\\', 'u', '3', '0', 'd', '0',
  252. '\\', 'u', '3', '0', 'a', '4',
  253. '\\', 'u', '3', '0', 'c', '8'));
  254. const String expected = juce::CharPointer_UTF8 ("\xe6\xae\x8b\xe3\x82\x8a\xe3\x82\x8f\xe3\x81\x9a\xe3\x81\x8b""5\xe3\x83\x90\xe3\x82\xa4\xe3\x83\x88");
  255. expect (converted == expected);
  256. }
  257. }
  258. beginTest ("Mcoded7 encoding");
  259. {
  260. {
  261. const auto converted = Encodings::toMcoded7 (makeByteArray (0x81, 0x82, 0x83));
  262. const auto expected = makeByteArray (0x70, 0x01, 0x02, 0x03);
  263. expect (rangesEqual (converted, expected));
  264. }
  265. {
  266. const auto converted = Encodings::toMcoded7 (makeByteArray (0x01, 0x82, 0x03, 0x04, 0x85, 0x06, 0x87, 0x08));
  267. const auto expected = makeByteArray (0x25, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x08);
  268. expect (rangesEqual (converted, expected));
  269. }
  270. }
  271. beginTest ("Mcoded7 decoding");
  272. {
  273. {
  274. const auto converted = Encodings::fromMcoded7 (makeByteArray (0x70, 0x01, 0x02, 0x03));
  275. const auto expected = makeByteArray (0x81, 0x82, 0x83);
  276. expect (rangesEqual (converted, expected));
  277. }
  278. {
  279. const auto converted = Encodings::fromMcoded7 (makeByteArray (0x25, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x08));
  280. const auto expected = makeByteArray (0x01, 0x82, 0x03, 0x04, 0x85, 0x06, 0x87, 0x08);
  281. expect (rangesEqual (converted, expected));
  282. }
  283. }
  284. }
  285. private:
  286. static bool deepEqual (const std::optional<var>& a, const std::optional<var>& b)
  287. {
  288. if (a.has_value() && b.has_value())
  289. return JSONUtils::deepEqual (*a, *b);
  290. return a == b;
  291. }
  292. template <typename A, typename B>
  293. static bool rangesEqual (A&& a, B&& b)
  294. {
  295. using std::begin, std::end;
  296. return std::equal (begin (a), end (a), begin (b), end (b));
  297. }
  298. template <typename... Ts>
  299. static std::array<std::byte, sizeof... (Ts)> makeByteArray (Ts&&... ts)
  300. {
  301. jassert (((0 <= (int) ts && (int) ts <= std::numeric_limits<uint8_t>::max()) && ...));
  302. return { std::byte (ts)... };
  303. }
  304. };
  305. static EncodingsTests encodingsTests;
  306. #endif
  307. } // namespace juce::midi_ci