Audio plugin host https://kx.studio/carla
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.

419 lines
12KB

  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. MemoryBlock::MemoryBlock() noexcept
  24. : size (0)
  25. {
  26. }
  27. MemoryBlock::MemoryBlock (const size_t initialSize, const bool initialiseToZero)
  28. {
  29. if (initialSize > 0)
  30. {
  31. size = initialSize;
  32. data.allocate (initialSize, initialiseToZero);
  33. }
  34. else
  35. {
  36. size = 0;
  37. }
  38. }
  39. MemoryBlock::MemoryBlock (const MemoryBlock& other)
  40. : size (other.size)
  41. {
  42. if (size > 0)
  43. {
  44. jassert (other.data != nullptr);
  45. data.malloc (size);
  46. memcpy (data, other.data, size);
  47. }
  48. }
  49. MemoryBlock::MemoryBlock (const void* const dataToInitialiseFrom, const size_t sizeInBytes)
  50. : size (sizeInBytes)
  51. {
  52. jassert (((ssize_t) sizeInBytes) >= 0);
  53. if (size > 0)
  54. {
  55. jassert (dataToInitialiseFrom != nullptr); // non-zero size, but a zero pointer passed-in?
  56. data.malloc (size);
  57. if (dataToInitialiseFrom != nullptr)
  58. memcpy (data, dataToInitialiseFrom, size);
  59. }
  60. }
  61. MemoryBlock::~MemoryBlock() noexcept
  62. {
  63. }
  64. MemoryBlock& MemoryBlock::operator= (const MemoryBlock& other)
  65. {
  66. if (this != &other)
  67. {
  68. setSize (other.size, false);
  69. memcpy (data, other.data, size);
  70. }
  71. return *this;
  72. }
  73. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  74. MemoryBlock::MemoryBlock (MemoryBlock&& other) noexcept
  75. : data (static_cast<HeapBlock<char>&&> (other.data)),
  76. size (other.size)
  77. {
  78. }
  79. MemoryBlock& MemoryBlock::operator= (MemoryBlock&& other) noexcept
  80. {
  81. data = static_cast<HeapBlock<char>&&> (other.data);
  82. size = other.size;
  83. return *this;
  84. }
  85. #endif
  86. //==============================================================================
  87. bool MemoryBlock::operator== (const MemoryBlock& other) const noexcept
  88. {
  89. return matches (other.data, other.size);
  90. }
  91. bool MemoryBlock::operator!= (const MemoryBlock& other) const noexcept
  92. {
  93. return ! operator== (other);
  94. }
  95. bool MemoryBlock::matches (const void* dataToCompare, size_t dataSize) const noexcept
  96. {
  97. return size == dataSize
  98. && memcmp (data, dataToCompare, size) == 0;
  99. }
  100. //==============================================================================
  101. // this will resize the block to this size
  102. void MemoryBlock::setSize (const size_t newSize, const bool initialiseToZero)
  103. {
  104. if (size != newSize)
  105. {
  106. if (newSize <= 0)
  107. {
  108. reset();
  109. }
  110. else
  111. {
  112. if (data != nullptr)
  113. {
  114. data.realloc (newSize);
  115. if (initialiseToZero && (newSize > size))
  116. zeromem (data + size, newSize - size);
  117. }
  118. else
  119. {
  120. data.allocate (newSize, initialiseToZero);
  121. }
  122. size = newSize;
  123. }
  124. }
  125. }
  126. void MemoryBlock::reset()
  127. {
  128. data.free();
  129. size = 0;
  130. }
  131. void MemoryBlock::ensureSize (const size_t minimumSize, const bool initialiseToZero)
  132. {
  133. if (size < minimumSize)
  134. setSize (minimumSize, initialiseToZero);
  135. }
  136. void MemoryBlock::swapWith (MemoryBlock& other) noexcept
  137. {
  138. std::swap (size, other.size);
  139. data.swapWith (other.data);
  140. }
  141. //==============================================================================
  142. void MemoryBlock::fillWith (const uint8 value) noexcept
  143. {
  144. memset (data, (int) value, size);
  145. }
  146. void MemoryBlock::append (const void* const srcData, const size_t numBytes)
  147. {
  148. if (numBytes > 0)
  149. {
  150. jassert (srcData != nullptr); // this must not be null!
  151. const size_t oldSize = size;
  152. setSize (size + numBytes);
  153. memcpy (data + oldSize, srcData, numBytes);
  154. }
  155. }
  156. void MemoryBlock::replaceWith (const void* const srcData, const size_t numBytes)
  157. {
  158. if (numBytes > 0)
  159. {
  160. jassert (srcData != nullptr); // this must not be null!
  161. setSize (numBytes);
  162. memcpy (data, srcData, numBytes);
  163. }
  164. }
  165. void MemoryBlock::insert (const void* const srcData, const size_t numBytes, size_t insertPosition)
  166. {
  167. if (numBytes > 0)
  168. {
  169. jassert (srcData != nullptr); // this must not be null!
  170. insertPosition = jmin (size, insertPosition);
  171. const size_t trailingDataSize = size - insertPosition;
  172. setSize (size + numBytes, false);
  173. if (trailingDataSize > 0)
  174. memmove (data + insertPosition + numBytes,
  175. data + insertPosition,
  176. trailingDataSize);
  177. memcpy (data + insertPosition, srcData, numBytes);
  178. }
  179. }
  180. void MemoryBlock::removeSection (const size_t startByte, const size_t numBytesToRemove)
  181. {
  182. if (startByte + numBytesToRemove >= size)
  183. {
  184. setSize (startByte);
  185. }
  186. else if (numBytesToRemove > 0)
  187. {
  188. memmove (data + startByte,
  189. data + startByte + numBytesToRemove,
  190. size - (startByte + numBytesToRemove));
  191. setSize (size - numBytesToRemove);
  192. }
  193. }
  194. void MemoryBlock::copyFrom (const void* const src, int offset, size_t num) noexcept
  195. {
  196. const char* d = static_cast<const char*> (src);
  197. if (offset < 0)
  198. {
  199. d -= offset;
  200. num += (size_t) -offset;
  201. offset = 0;
  202. }
  203. if ((size_t) offset + num > size)
  204. num = size - (size_t) offset;
  205. if (num > 0)
  206. memcpy (data + offset, d, num);
  207. }
  208. void MemoryBlock::copyTo (void* const dst, int offset, size_t num) const noexcept
  209. {
  210. char* d = static_cast<char*> (dst);
  211. if (offset < 0)
  212. {
  213. zeromem (d, (size_t) -offset);
  214. d -= offset;
  215. num -= (size_t) -offset;
  216. offset = 0;
  217. }
  218. if ((size_t) offset + num > size)
  219. {
  220. const size_t newNum = (size_t) size - (size_t) offset;
  221. zeromem (d + newNum, num - newNum);
  222. num = newNum;
  223. }
  224. if (num > 0)
  225. memcpy (d, data + offset, num);
  226. }
  227. String MemoryBlock::toString() const
  228. {
  229. return String::fromUTF8 (data, (int) size);
  230. }
  231. //==============================================================================
  232. int MemoryBlock::getBitRange (const size_t bitRangeStart, size_t numBits) const noexcept
  233. {
  234. int res = 0;
  235. size_t byte = bitRangeStart >> 3;
  236. size_t offsetInByte = bitRangeStart & 7;
  237. size_t bitsSoFar = 0;
  238. while (numBits > 0 && (size_t) byte < size)
  239. {
  240. const size_t bitsThisTime = jmin (numBits, 8 - offsetInByte);
  241. const int mask = (0xff >> (8 - bitsThisTime)) << offsetInByte;
  242. res |= (((data[byte] & mask) >> offsetInByte) << bitsSoFar);
  243. bitsSoFar += bitsThisTime;
  244. numBits -= bitsThisTime;
  245. ++byte;
  246. offsetInByte = 0;
  247. }
  248. return res;
  249. }
  250. void MemoryBlock::setBitRange (const size_t bitRangeStart, size_t numBits, int bitsToSet) noexcept
  251. {
  252. size_t byte = bitRangeStart >> 3;
  253. size_t offsetInByte = bitRangeStart & 7;
  254. uint32 mask = ~((((uint32) 0xffffffff) << (32 - numBits)) >> (32 - numBits));
  255. while (numBits > 0 && (size_t) byte < size)
  256. {
  257. const size_t bitsThisTime = jmin (numBits, 8 - offsetInByte);
  258. const uint32 tempMask = (mask << offsetInByte) | ~((((uint32) 0xffffffff) >> offsetInByte) << offsetInByte);
  259. const uint32 tempBits = (uint32) bitsToSet << offsetInByte;
  260. data[byte] = (char) (((uint32) data[byte] & tempMask) | tempBits);
  261. ++byte;
  262. numBits -= bitsThisTime;
  263. bitsToSet >>= bitsThisTime;
  264. mask >>= bitsThisTime;
  265. offsetInByte = 0;
  266. }
  267. }
  268. //==============================================================================
  269. void MemoryBlock::loadFromHexString (StringRef hex)
  270. {
  271. ensureSize ((size_t) hex.length() >> 1);
  272. char* dest = data;
  273. String::CharPointerType t (hex.text);
  274. for (;;)
  275. {
  276. int byte = 0;
  277. for (int loop = 2; --loop >= 0;)
  278. {
  279. byte <<= 4;
  280. for (;;)
  281. {
  282. const juce_wchar c = t.getAndAdvance();
  283. if (c >= '0' && c <= '9') { byte |= c - '0'; break; }
  284. if (c >= 'a' && c <= 'z') { byte |= c - ('a' - 10); break; }
  285. if (c >= 'A' && c <= 'Z') { byte |= c - ('A' - 10); break; }
  286. if (c == 0)
  287. {
  288. setSize (static_cast<size_t> (dest - data));
  289. return;
  290. }
  291. }
  292. }
  293. *dest++ = (char) byte;
  294. }
  295. }
  296. //==============================================================================
  297. static const char base64EncodingTable[] = ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+";
  298. String MemoryBlock::toBase64Encoding() const
  299. {
  300. const size_t numChars = ((size << 3) + 5) / 6;
  301. String destString ((unsigned int) size); // store the length, followed by a '.', and then the data.
  302. const int initialLen = destString.length();
  303. destString.preallocateBytes (sizeof (String::CharPointerType::CharType) * (size_t) initialLen + 2 + numChars);
  304. String::CharPointerType d (destString.getCharPointer());
  305. d += initialLen;
  306. d.write ('.');
  307. for (size_t i = 0; i < numChars; ++i)
  308. d.write ((juce_wchar) (uint8) base64EncodingTable [getBitRange (i * 6, 6)]);
  309. d.writeNull();
  310. return destString;
  311. }
  312. static const char base64DecodingTable[] =
  313. {
  314. 63, 0, 0, 0, 0, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 0, 0, 0, 0, 0, 0, 0,
  315. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  316. 0, 0, 0, 0, 0, 0, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52
  317. };
  318. bool MemoryBlock::fromBase64Encoding (StringRef s)
  319. {
  320. String::CharPointerType dot (CharacterFunctions::find (s.text, (juce_wchar) '.'));
  321. if (dot.isEmpty())
  322. return false;
  323. const int numBytesNeeded = String (s.text, dot).getIntValue();
  324. setSize ((size_t) numBytesNeeded, true);
  325. String::CharPointerType srcChars (dot + 1);
  326. int pos = 0;
  327. for (;;)
  328. {
  329. int c = (int) srcChars.getAndAdvance();
  330. if (c == 0)
  331. return true;
  332. c -= 43;
  333. if (isPositiveAndBelow (c, numElementsInArray (base64DecodingTable)))
  334. {
  335. setBitRange ((size_t) pos, 6, base64DecodingTable [c]);
  336. pos += 6;
  337. }
  338. }
  339. }