ReadWriteSection.h [plain text]
#ifndef _H_APPLEDL_READWRITESECTION
#define _H_APPLEDL_READWRITESECTION
#include <security_utilities/alloc.h>
#include <security_filedb/AtomicFile.h>
#include <security_utilities/endian.h>
#include <security_cdsa_utilities/cssmerrors.h>
#include <Security/cssm.h>
#include "OverUnderflowCheck.h"
namespace Security
{
typedef Endian<uint32> Atom;
enum {
AtomSize = sizeof(uint32) };
class Range
{
public:
Range(uint32 inOffset, uint32 inSize) : mOffset(inOffset), mSize(inSize) {}
uint32 mOffset;
uint32 mSize;
};
class ReadSection
{
protected:
ReadSection(uint8 *inAddress, size_t inLength) : mAddress(inAddress), mLength(inLength)
{
if (mAddress == NULL)
CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
}
public:
ReadSection() : mAddress(NULL), mLength(0) {}
ReadSection(const uint8 *inAddress, size_t inLength) :
mAddress(const_cast<uint8 *>(inAddress)), mLength(inLength) {}
uint32 size() const { return (uint32)mLength; }
uint32 at(uint32 inOffset) const
{
if (inOffset > mLength)
{
CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
}
return ntohl(*reinterpret_cast<const uint32 *>(mAddress + inOffset));
}
uint32 operator[](uint32 inOffset) const
{
return at(inOffset);
}
ReadSection subsection(uint32 inOffset) const
{
if (inOffset > mLength)
CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
return ReadSection(mAddress + inOffset, mLength - inOffset);
}
ReadSection subsection(uint32 inOffset, uint32 inLength) const
{
if (CheckUInt32Add(inOffset, inLength) > mLength)
CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
return ReadSection(mAddress + inOffset, inLength);
}
ReadSection subsection(const Range &inRange) const
{
return subsection(inRange.mOffset, inRange.mSize);
}
const uint8 *range(const Range &inRange) const
{
if (CheckUInt32Add(inRange.mOffset, inRange.mSize) > mLength)
CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
return mAddress + inRange.mOffset;
}
uint8 *allocCopyRange(const Range &inRange, Allocator &inAllocator) const
{
uint8 *aData;
if (inRange.mSize == 0)
aData = NULL;
else
{
if (CheckUInt32Add(inRange.mOffset, inRange.mSize) > mLength)
CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
aData = reinterpret_cast<uint8 *>(inAllocator.malloc(inRange.mSize));
memcpy(aData, mAddress + inRange.mOffset, inRange.mSize);
}
return aData;
}
static uint32 align(uint32 offset) { return (CheckUInt32Subtract(CheckUInt32Add(offset, AtomSize), 1)) & ~(AtomSize - 1); }
protected:
uint8 *mAddress;
size_t mLength;
};
class WriteSection : public ReadSection
{
public:
static const size_t DefaultCapacity = 64;
WriteSection(Allocator &inAllocator, size_t inCapacity) :
ReadSection(reinterpret_cast<uint8 *>(inAllocator.malloc(inCapacity)), 0),
mAllocator(inAllocator),
mCapacity(inCapacity)
{
if (mCapacity > 0)
memset(mAddress, 0, mCapacity);
}
WriteSection(Allocator &inAllocator = Allocator::standard()) :
ReadSection(reinterpret_cast<uint8 *>(inAllocator.malloc(DefaultCapacity)), 0),
mAllocator(inAllocator),
mCapacity(DefaultCapacity)
{
}
WriteSection(const WriteSection &ws, int length) :
ReadSection(reinterpret_cast<uint8 *>(ws.mAllocator.malloc(length)), length),
mAllocator(ws.mAllocator),
mCapacity(length)
{
memcpy(mAddress, ws.mAddress, length);
}
~WriteSection() { mAllocator.free(mAddress); }
private:
void grow(size_t inNewCapacity);
public:
#if BUG_GCC
uint32 size() const { return ReadSection::size(); }
#else
using ReadSection::size;
#endif
void size(uint32 inLength) { mLength = inLength; }
uint32 put(uint32 inOffset, uint32 inValue);
uint32 put(uint32 inOffset, uint32 inLength, const uint8 *inData);
const uint8 *address() const { return mAddress; }
uint8 *release()
{
uint8 *anAddress = mAddress;
mAddress = NULL;
mCapacity = 0;
return anAddress;
}
private:
Allocator &mAllocator;
size_t mCapacity;
};
}
#endif // _H_APPLEDL_READWRITESECTION