1#include "ReadWriteSection.h" 2 3uint32 WriteSection::put(uint32 inOffset, uint32 inValue) 4{ 5 uint32 aLength = CheckUInt32Add(inOffset, sizeof(inValue)); 6 if (aLength > mCapacity) 7 grow(aLength); 8 9 if (mAddress == NULL) 10 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT); 11 12 *reinterpret_cast<uint32 *>(mAddress + inOffset) = htonl(inValue); 13 return aLength; 14} 15 16 17 18uint32 WriteSection::put(uint32 inOffset, uint32 inLength, const uint8 *inData) 19{ 20 // if we are being asked to put 0 bytes, just return 21 if (inLength == 0 || inData == NULL) 22 { 23 return inOffset; 24 } 25 26 uint32 aLength = CheckUInt32Add(inOffset, inLength); 27 28 // Round up to nearest multiple of 4 bytes, to pad with zeros 29 uint32 aNewOffset = align(aLength); 30 if (aNewOffset > mCapacity) 31 grow(aNewOffset); 32 33 if (mAddress == NULL) 34 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT); 35 36 memcpy(mAddress + inOffset, inData, inLength); 37 38 for (uint32 anOffset = aLength; anOffset < aNewOffset; anOffset++) 39 mAddress[anOffset] = 0; 40 41 return aNewOffset; 42} 43 44 45 46void WriteSection::grow(size_t inNewCapacity) 47{ 48 size_t n = CheckUInt32Multiply((uint32)mCapacity, 2); 49 size_t aNewCapacity = max(n, inNewCapacity); 50 mAddress = reinterpret_cast<uint8 *>(mAllocator.realloc(mAddress, aNewCapacity)); 51 memset(mAddress + mCapacity, 0, aNewCapacity - mCapacity); 52 mCapacity = aNewCapacity; 53} 54