X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/72a12576750f52947eb043106ba5c12c0d07decf..b1ab9ed8d0e0f1c3b66d7daa8fd5564444c56195:/libsecurity_filedb/lib/ReadWriteSection.cpp?ds=inline diff --git a/libsecurity_filedb/lib/ReadWriteSection.cpp b/libsecurity_filedb/lib/ReadWriteSection.cpp new file mode 100644 index 00000000..be2a5e75 --- /dev/null +++ b/libsecurity_filedb/lib/ReadWriteSection.cpp @@ -0,0 +1,53 @@ +#include "ReadWriteSection.h" + +uint32 WriteSection::put(uint32 inOffset, uint32 inValue) +{ + uint32 aLength = CheckUInt32Add(inOffset, sizeof(inValue)); + if (aLength > mCapacity) + grow(aLength); + + if (mAddress == NULL) + CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT); + + *reinterpret_cast(mAddress + inOffset) = htonl(inValue); + return aLength; +} + + + +uint32 WriteSection::put(uint32 inOffset, uint32 inLength, const uint8 *inData) +{ + // if we are being asked to put 0 bytes, just return + if (inLength == 0 || inData == NULL) + { + return inOffset; + } + + uint32 aLength = CheckUInt32Add(inOffset, inLength); + + // Round up to nearest multiple of 4 bytes, to pad with zeros + uint32 aNewOffset = align(aLength); + if (aNewOffset > mCapacity) + grow(aNewOffset); + + if (mAddress == NULL) + CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT); + + memcpy(mAddress + inOffset, inData, inLength); + + for (uint32 anOffset = aLength; anOffset < aNewOffset; anOffset++) + mAddress[anOffset] = 0; + + return aNewOffset; +} + + + +void WriteSection::grow(size_t inNewCapacity) +{ + size_t n = CheckUInt32Multiply(mCapacity, 2); + size_t aNewCapacity = max(n, inNewCapacity); + mAddress = reinterpret_cast(mAllocator.realloc(mAddress, aNewCapacity)); + memset(mAddress + mCapacity, 0, aNewCapacity - mCapacity); + mCapacity = aNewCapacity; +}