|
|
|
@@ -23,11 +23,9 @@ |
|
|
|
namespace juce
|
|
|
|
{
|
|
|
|
|
|
|
|
MemoryBlock::MemoryBlock() noexcept : size (0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
MemoryBlock::MemoryBlock() noexcept {}
|
|
|
|
|
|
|
|
MemoryBlock::MemoryBlock (const size_t initialSize, const bool initialiseToZero)
|
|
|
|
MemoryBlock::MemoryBlock (size_t initialSize, bool initialiseToZero)
|
|
|
|
{
|
|
|
|
if (initialSize > 0)
|
|
|
|
{
|
|
|
|
@@ -147,7 +145,7 @@ void MemoryBlock::reset() |
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryBlock::ensureSize (const size_t minimumSize, const bool initialiseToZero)
|
|
|
|
void MemoryBlock::ensureSize (size_t minimumSize, bool initialiseToZero)
|
|
|
|
{
|
|
|
|
if (size < minimumSize)
|
|
|
|
setSize (minimumSize, initialiseToZero);
|
|
|
|
@@ -160,23 +158,23 @@ void MemoryBlock::swapWith (MemoryBlock& other) noexcept |
|
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
void MemoryBlock::fillWith (const uint8 value) noexcept
|
|
|
|
void MemoryBlock::fillWith (uint8 value) noexcept
|
|
|
|
{
|
|
|
|
memset (data, (int) value, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryBlock::append (const void* const srcData, const size_t numBytes)
|
|
|
|
void MemoryBlock::append (const void* srcData, size_t numBytes)
|
|
|
|
{
|
|
|
|
if (numBytes > 0)
|
|
|
|
{
|
|
|
|
jassert (srcData != nullptr); // this must not be null!
|
|
|
|
const size_t oldSize = size;
|
|
|
|
auto oldSize = size;
|
|
|
|
setSize (size + numBytes);
|
|
|
|
memcpy (data + oldSize, srcData, numBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryBlock::replaceWith (const void* const srcData, const size_t numBytes)
|
|
|
|
void MemoryBlock::replaceWith (const void* srcData, size_t numBytes)
|
|
|
|
{
|
|
|
|
if (numBytes > 0)
|
|
|
|
{
|
|
|
|
@@ -186,13 +184,13 @@ void MemoryBlock::replaceWith (const void* const srcData, const size_t numBytes) |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryBlock::insert (const void* const srcData, const size_t numBytes, size_t insertPosition)
|
|
|
|
void MemoryBlock::insert (const void* srcData, size_t numBytes, size_t insertPosition)
|
|
|
|
{
|
|
|
|
if (numBytes > 0)
|
|
|
|
{
|
|
|
|
jassert (srcData != nullptr); // this must not be null!
|
|
|
|
insertPosition = jmin (size, insertPosition);
|
|
|
|
const size_t trailingDataSize = size - insertPosition;
|
|
|
|
auto trailingDataSize = size - insertPosition;
|
|
|
|
setSize (size + numBytes, false);
|
|
|
|
|
|
|
|
if (trailingDataSize > 0)
|
|
|
|
@@ -204,7 +202,7 @@ void MemoryBlock::insert (const void* const srcData, const size_t numBytes, size |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryBlock::removeSection (const size_t startByte, const size_t numBytesToRemove)
|
|
|
|
void MemoryBlock::removeSection (size_t startByte, size_t numBytesToRemove)
|
|
|
|
{
|
|
|
|
if (startByte + numBytesToRemove >= size)
|
|
|
|
{
|
|
|
|
@@ -222,7 +220,7 @@ void MemoryBlock::removeSection (const size_t startByte, const size_t numBytesTo |
|
|
|
|
|
|
|
void MemoryBlock::copyFrom (const void* const src, int offset, size_t num) noexcept
|
|
|
|
{
|
|
|
|
const char* d = static_cast<const char*> (src);
|
|
|
|
auto* d = static_cast<const char*> (src);
|
|
|
|
|
|
|
|
if (offset < 0)
|
|
|
|
{
|
|
|
|
@@ -240,7 +238,7 @@ void MemoryBlock::copyFrom (const void* const src, int offset, size_t num) noexc |
|
|
|
|
|
|
|
void MemoryBlock::copyTo (void* const dst, int offset, size_t num) const noexcept
|
|
|
|
{
|
|
|
|
char* d = static_cast<char*> (dst);
|
|
|
|
auto* d = static_cast<char*> (dst);
|
|
|
|
|
|
|
|
if (offset < 0)
|
|
|
|
{
|
|
|
|
@@ -252,7 +250,7 @@ void MemoryBlock::copyTo (void* const dst, int offset, size_t num) const noexcep |
|
|
|
|
|
|
|
if ((size_t) offset + num > size)
|
|
|
|
{
|
|
|
|
const size_t newNum = (size_t) size - (size_t) offset;
|
|
|
|
auto newNum = (size_t) size - (size_t) offset;
|
|
|
|
zeromem (d + newNum, num - newNum);
|
|
|
|
num = newNum;
|
|
|
|
}
|
|
|
|
@@ -267,17 +265,17 @@ String MemoryBlock::toString() const |
|
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
int MemoryBlock::getBitRange (const size_t bitRangeStart, size_t numBits) const noexcept
|
|
|
|
int MemoryBlock::getBitRange (size_t bitRangeStart, size_t numBits) const noexcept
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
size_t byte = bitRangeStart >> 3;
|
|
|
|
size_t offsetInByte = bitRangeStart & 7;
|
|
|
|
auto byte = bitRangeStart >> 3;
|
|
|
|
auto offsetInByte = bitRangeStart & 7;
|
|
|
|
size_t bitsSoFar = 0;
|
|
|
|
|
|
|
|
while (numBits > 0 && (size_t) byte < size)
|
|
|
|
{
|
|
|
|
const size_t bitsThisTime = jmin (numBits, 8 - offsetInByte);
|
|
|
|
auto bitsThisTime = jmin (numBits, 8 - offsetInByte);
|
|
|
|
const int mask = (0xff >> (8 - bitsThisTime)) << offsetInByte;
|
|
|
|
|
|
|
|
res |= (((data[byte] & mask) >> offsetInByte) << bitsSoFar);
|
|
|
|
@@ -293,13 +291,13 @@ int MemoryBlock::getBitRange (const size_t bitRangeStart, size_t numBits) const |
|
|
|
|
|
|
|
void MemoryBlock::setBitRange (const size_t bitRangeStart, size_t numBits, int bitsToSet) noexcept
|
|
|
|
{
|
|
|
|
size_t byte = bitRangeStart >> 3;
|
|
|
|
size_t offsetInByte = bitRangeStart & 7;
|
|
|
|
auto byte = bitRangeStart >> 3;
|
|
|
|
auto offsetInByte = bitRangeStart & 7;
|
|
|
|
uint32 mask = ~((((uint32) 0xffffffff) << (32 - numBits)) >> (32 - numBits));
|
|
|
|
|
|
|
|
while (numBits > 0 && (size_t) byte < size)
|
|
|
|
{
|
|
|
|
const size_t bitsThisTime = jmin (numBits, 8 - offsetInByte);
|
|
|
|
auto bitsThisTime = jmin (numBits, 8 - offsetInByte);
|
|
|
|
|
|
|
|
const uint32 tempMask = (mask << offsetInByte) | ~((((uint32) 0xffffffff) >> offsetInByte) << offsetInByte);
|
|
|
|
const uint32 tempBits = (uint32) bitsToSet << offsetInByte;
|
|
|
|
@@ -319,7 +317,7 @@ void MemoryBlock::loadFromHexString (StringRef hex) |
|
|
|
{
|
|
|
|
ensureSize ((size_t) hex.length() >> 1);
|
|
|
|
char* dest = data;
|
|
|
|
String::CharPointerType t (hex.text);
|
|
|
|
auto t = hex.text;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
@@ -331,7 +329,7 @@ void MemoryBlock::loadFromHexString (StringRef hex) |
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
const juce_wchar c = t.getAndAdvance();
|
|
|
|
auto c = t.getAndAdvance();
|
|
|
|
|
|
|
|
if (c >= '0' && c <= '9') { byte |= c - '0'; break; }
|
|
|
|
if (c >= 'a' && c <= 'z') { byte |= c - ('a' - 10); break; }
|
|
|
|
@@ -354,18 +352,18 @@ static const char base64EncodingTable[] = ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij |
|
|
|
|
|
|
|
String MemoryBlock::toBase64Encoding() const
|
|
|
|
{
|
|
|
|
const size_t numChars = ((size << 3) + 5) / 6;
|
|
|
|
auto numChars = ((size << 3) + 5) / 6;
|
|
|
|
|
|
|
|
String destString ((unsigned int) size); // store the length, followed by a '.', and then the data.
|
|
|
|
const int initialLen = destString.length();
|
|
|
|
auto initialLen = destString.length();
|
|
|
|
destString.preallocateBytes (sizeof (String::CharPointerType::CharType) * (size_t) initialLen + 2 + numChars);
|
|
|
|
|
|
|
|
String::CharPointerType d (destString.getCharPointer());
|
|
|
|
auto d = destString.getCharPointer();
|
|
|
|
d += initialLen;
|
|
|
|
d.write ('.');
|
|
|
|
|
|
|
|
for (size_t i = 0; i < numChars; ++i)
|
|
|
|
d.write ((juce_wchar) (uint8) base64EncodingTable [getBitRange (i * 6, 6)]);
|
|
|
|
d.write ((juce_wchar) (uint8) base64EncodingTable[getBitRange (i * 6, 6)]);
|
|
|
|
|
|
|
|
d.writeNull();
|
|
|
|
return destString;
|
|
|
|
@@ -380,29 +378,30 @@ static const char base64DecodingTable[] = |
|
|
|
|
|
|
|
bool MemoryBlock::fromBase64Encoding (StringRef s)
|
|
|
|
{
|
|
|
|
String::CharPointerType dot (CharacterFunctions::find (s.text, (juce_wchar) '.'));
|
|
|
|
auto dot = CharacterFunctions::find (s.text, (juce_wchar) '.');
|
|
|
|
|
|
|
|
if (dot.isEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const int numBytesNeeded = String (s.text, dot).getIntValue();
|
|
|
|
auto numBytesNeeded = String (s.text, dot).getIntValue();
|
|
|
|
|
|
|
|
setSize ((size_t) numBytesNeeded, true);
|
|
|
|
|
|
|
|
String::CharPointerType srcChars (dot + 1);
|
|
|
|
auto srcChars = dot + 1;
|
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int c = (int) srcChars.getAndAdvance();
|
|
|
|
auto c = (int) srcChars.getAndAdvance();
|
|
|
|
|
|
|
|
if (c == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
c -= 43;
|
|
|
|
|
|
|
|
if (isPositiveAndBelow (c, numElementsInArray (base64DecodingTable)))
|
|
|
|
{
|
|
|
|
setBitRange ((size_t) pos, 6, base64DecodingTable [c]);
|
|
|
|
setBitRange ((size_t) pos, 6, base64DecodingTable[c]);
|
|
|
|
pos += 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|