X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/5dd5f9ec28f304ca377c42fd7f711d6cf12b90e1..5c19dc3ae3bd8e40a9c028b0deddd50ff337692c:/OSX/libsecurity_filedb/lib/ReadWriteSection.cpp diff --git a/OSX/libsecurity_filedb/lib/ReadWriteSection.cpp b/OSX/libsecurity_filedb/lib/ReadWriteSection.cpp new file mode 100644 index 00000000..9fe1b489 --- /dev/null +++ b/OSX/libsecurity_filedb/lib/ReadWriteSection.cpp @@ -0,0 +1,57 @@ +#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((uint32)mCapacity, 2); + size_t aNewCapacity = max(n, inNewCapacity); + mAddress = reinterpret_cast(mAllocator.realloc(mAddress, aNewCapacity)); + + if (mAddress == NULL) + CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT); + + memset(mAddress + mCapacity, 0, aNewCapacity - mCapacity); + mCapacity = aNewCapacity; +}