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.

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