Skip to content
4 changes: 2 additions & 2 deletions rejistry++/src/BinaryBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ namespace Rejistry {
protected:
BinaryBlock() {};

RegistryByteBuffer * _buf;
uint32_t _offset;
RegistryByteBuffer * _buf = 0;
uint32_t _offset = 0;

uint16_t getWord(uint32_t offset) const;

Expand Down
6 changes: 3 additions & 3 deletions rejistry++/src/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ namespace Rejistry {

virtual ~Buffer();

uint32_t _capacity;
uint32_t _limit;
uint32_t _position;
uint32_t _capacity = 0;
uint32_t _limit = 0;
uint32_t _position = 0;
};
};

Expand Down
20 changes: 10 additions & 10 deletions rejistry++/src/ByteBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace Rejistry {
try {
_buffer.resize(length);
}
catch (std::bad_alloc &e)
catch (std::bad_alloc &)
{
throw RegistryParseException("Cannot allocate memory for registry byte buffer.");
}
Expand All @@ -73,31 +73,31 @@ namespace Rejistry {
}

/**
* This method is based off of https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#get-byte:A-int-int-
* * @param dst: destination byte array to write data to
* * @param offset: Offset within the destination byte array to start writting data to
* * @param length: the number of bytes to write to the destination byte array
* Throws exception if offset or length are too large. */
void ByteBuffer::get(ByteArray& dst, const uint32_t offset, const uint32_t length) {
if (length == 0) {
// No data requested.
return;
}

if (offset > dst.size()) {
if (offset >= dst.size()) {
throw RegistryParseException("Offset is greater than destination buffer size.");
}

if ((dst.size() - offset) > length) {
if ((dst.size() - offset) < length) {
throw RegistryParseException("Length is greater than available space in destination buffer.");
}

if ((_position + offset) > _limit) {
throw RegistryParseException("Starting position is beyond end of buffer.");
}

if ((_position + offset + length) > _limit) {
if ((_position + length) > _limit) {
throw RegistryParseException("Number of requested bytes exceeds buffer size.");
}

memcpy(&dst[0], &_buffer[_position + offset], length);
_position += offset;
memcpy(&dst[offset], &_buffer[_position], length);
Comment thread
crayy8 marked this conversation as resolved.
_position += length;
}

/**
Expand Down
19 changes: 6 additions & 13 deletions rejistry++/src/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ namespace Rejistry {
* Copy 'length' bytes from this buffer into the given destination,
* starting at the current position in this buffer and at the given
* offset in the destination. The position of this buffer is incremented
* by length.
*
* @param dst The destination into which to copy bytes.
* @param offset The offset within the destination buffer to copy bytes to.
* @param offset The offset within the destination buffer to start copying data
* @param length The number of bytes to copy from this buffer.
* @throws RegistryParseException
*/
void get(ByteArray& dst, const uint32_t offset, const uint32_t length);

/// Get two bytes from the current position in the buffer.
/// Get two bytes from an offset in the buffer
uint16_t getShort(uint32_t offset) const;
/// Get four bytes from the current position in the buffer.
/// Get four bytes from an offset in the buffer
uint32_t getInt(uint32_t offset) const;
/// Get eight bytes from the current position in the buffer.
/// Get eight bytes from an offset in the buffer
uint64_t getLong(uint32_t offset) const;

private:
Expand All @@ -76,14 +76,7 @@ namespace Rejistry {

void initializeBuffer(const uint8_t * buf, const uint32_t length);

template <typename T> T read() const {
T bytes = read<T>(_position);
if (bytes != NULL) {
_position += sizeof(T);
}
return bytes;
}

/// read at specified offset
template <typename T> T read(uint32_t offset) const {
if (offset + sizeof(T) <= _limit) {
return *((T*)&_buffer[offset]);
Expand Down
30 changes: 19 additions & 11 deletions rejistry++/src/Cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
*/
#include <cstdlib>
#include <memory>

// Local includes
#include "Cell.h"
Expand All @@ -36,15 +37,23 @@
namespace Rejistry {

uint32_t Cell::getLength() const {
return std::abs((int)getDWord(LENGTH_OFFSET));
int32_t raw = (int32_t)getDWord(LENGTH_OFFSET);
if (raw == (int32_t)0x80000000) {
throw RegistryParseException("Invalid cell length.");
}
return (uint32_t)(raw < 0 ? -raw : raw);
}

bool Cell::isActive() const {
return ((int)getDWord(LENGTH_OFFSET) < 0x0);
}

std::vector<uint8_t> Cell::getData() const {
return _buf->getData(getAbsoluteOffset(DATA_OFFSET), getLength() - DATA_OFFSET);
uint32_t len = getLength();
if (len < DATA_OFFSET) {
throw RegistryParseException("Cell length too small.");
}
return _buf->getData(getAbsoluteOffset(DATA_OFFSET), len - DATA_OFFSET);
}

std::string Cell::getDataSignature() const {
Expand All @@ -64,31 +73,31 @@ namespace Rejistry {
}

SubkeyListRecord::SubkeyListRecordPtr Cell::getLFRecord() const {
return new LFRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
return std::make_unique<LFRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
}

SubkeyListRecord::SubkeyListRecordPtr Cell::getLHRecord() const {
return new LHRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
return std::make_unique<LHRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
}

SubkeyListRecord::SubkeyListRecordPtr Cell::getRIRecord() const {
return new RIRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
return std::make_unique<RIRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
}

LIRecord::LIRecordPtr Cell::getLIRecord() const {
return new LIRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
SubkeyListRecord::SubkeyListRecordPtr Cell::getLIRecord() const {
return std::make_unique<LIRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
}

DBRecord::DBRecordPtr Cell::getDBRecord() const {
return new DBRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
return std::make_unique<DBRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
}

DBIndirectRecord::DBIndirectRecordPtr Cell::getDBIndirectRecord() const {
return new DBIndirectRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
return std::make_unique<DBIndirectRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
}

ValueListRecord::ValueListRecordPtr Cell::getValueListRecord(const uint32_t numValues) const {
return new ValueListRecord(_buf, getAbsoluteOffset(DATA_OFFSET), numValues);
return std::make_unique<ValueListRecord>(_buf, getAbsoluteOffset(DATA_OFFSET), numValues);
}

SubkeyListRecord::SubkeyListRecordPtr Cell::getSubkeyList() const {
Expand All @@ -110,5 +119,4 @@ namespace Rejistry {
throw RegistryParseException("Unexpected subkey list type: " + magic);
}
}

};
21 changes: 10 additions & 11 deletions rejistry++/src/Cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,49 +133,49 @@ namespace Rejistry {

/**
* Interprets the cell data as an VKRecord and returns it.
* @returns Pointer to an VKRecord object.
* @returns Pointer to an VKRecord object. (Caller must free using delete)
* @throws RegistryParseException
*/
VKRecord::VKRecordPtr getVKRecord() const;

/**
* Interprets the cell data as an LFRecord and returns it.
* @returns Pointer to an LFRecord object.
* @returns Unique pointer to an LFRecord object.
* @throws RegistryParseException
*/
SubkeyListRecord::SubkeyListRecordPtr getLFRecord() const;

/**
* Interprets the cell data as an LHRecord and returns it.
* @returns Pointer to an LHRecord object.
* @returns Unique pointer to an LHRecord object.
* @throws RegistryParseException
*/
SubkeyListRecord::SubkeyListRecordPtr getLHRecord() const;

/**
* Interprets the cell data as an RIRecord and returns it.
* @returns Pointer to an RIRecord object.
* @returns Unique pointer to an RIRecord object.
* @throws RegistryParseException
*/
SubkeyListRecord::SubkeyListRecordPtr getRIRecord() const;

/**
* Interprets the cell data as an LIRecord and returns it.
* @returns Pointer to an LIRecord object.
* @returns Unique pointer to an LIRecord object.
* @throws RegistryParseException
*/
LIRecord::LIRecordPtr getLIRecord() const;
SubkeyListRecord::SubkeyListRecordPtr getLIRecord() const;

/**
* Interprets the cell data as an DBRecord and returns it.
* @returns Pointer to an DBRecord object.
* @returns Unique pointer to an DBRecord object.
* @throws RegistryParseException
*/
DBRecord::DBRecordPtr getDBRecord() const;

/**
* Interprets the cell data as an DBIndirectRecord and returns it.
* @returns Pointer to an DBIndirectRecord object.
* @returns Unique pointer to an DBIndirectRecord object.
* @throws RegistryParseException
*/
DBIndirectRecord::DBIndirectRecordPtr getDBIndirectRecord() const;
Expand All @@ -184,15 +184,14 @@ namespace Rejistry {
* Interprets the cell data as an ValueListRecord and returns it.
* @param numValues The number of values the value list should attempt
* to parse.
* @returns Pointer to an ValueListRecord object. The caller is responsible
* for freeing the returned record.
* @returns Unique pointer to a ValueListRecord object.
* @throws RegistryParseException
*/
ValueListRecord::ValueListRecordPtr getValueListRecord(const uint32_t numValues) const;

/**
* Interprets the cell data as a SubkeyList and returns it.
* @returns Pointer to an SubkeyList object.
* @returns Unique pointer to an SubkeyList object (do not need to free)
* @throws RegistryParseException
*/
SubkeyListRecord::SubkeyListRecordPtr getSubkeyList() const;
Expand Down
4 changes: 3 additions & 1 deletion rejistry++/src/DBIndirectRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ namespace Rejistry {
}

std::vector<uint8_t> cellData = c->getData();

if (cellData.size() < size) {
throw RegistryParseException("DB indirect cell too small for requested data.");
}
data.insert(data.end(), cellData.begin(), cellData.begin() + size);

length -= size;
Expand Down
3 changes: 2 additions & 1 deletion rejistry++/src/DBIndirectRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define _REJISTRY_DBINDIRECTRECORD_H

#include <cstdint>
#include <memory>
#include <string>

// Local includes
Expand All @@ -41,7 +42,7 @@ namespace Rejistry {
*/
class DBIndirectRecord : public Record {
public:
typedef DBIndirectRecord * DBIndirectRecordPtr;
typedef std::unique_ptr<DBIndirectRecord> DBIndirectRecordPtr;

DBIndirectRecord(RegistryByteBuffer * buf, uint32_t offset) : Record(buf, offset) {}

Expand Down
2 changes: 1 addition & 1 deletion rejistry++/src/DBRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace Rejistry {
throw RegistryParseException("Failed to create Cell for DBRecord.");
}

std::unique_ptr< DBIndirectRecord > dbi(c->getDBIndirectRecord());
auto dbi = c->getDBIndirectRecord();
return dbi->getData(length);
}

Expand Down
3 changes: 2 additions & 1 deletion rejistry++/src/DBRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define _REJISTRY_DBRECORD_H

#include <cstdint>
#include <memory>
#include <string>

// Local includes
Expand All @@ -41,7 +42,7 @@ namespace Rejistry {
*/
class DBRecord : public Record {
public:
typedef DBRecord * DBRecordPtr;
typedef std::unique_ptr<DBRecord> DBRecordPtr;

DBRecord(RegistryByteBuffer * buf, uint32_t offset);

Expand Down
2 changes: 1 addition & 1 deletion rejistry++/src/DirectSubkeyListRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace Rejistry {
private:
static const uint16_t LIST_START_OFFSET = 0x04;

uint32_t _itemSize;
uint32_t _itemSize = 0;

protected:
DirectSubkeyListRecord() {};
Expand Down
11 changes: 9 additions & 2 deletions rejistry++/src/HBIN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Rejistry {
HBIN::HBIN(const REGFHeader * header, RegistryByteBuffer * buf, uint32_t offset) : BinaryBlock(*buf, offset) {
_header = header;

// offset is applied within getDWord by BinaryBlock
if (getDWord(0x0) != 0x6E696268) {
throw RegistryParseException("Invalid HBIN magic header.");
}
Expand All @@ -51,13 +52,19 @@ namespace Rejistry {
Cell::CellPtrList HBIN::getCells() const {
uint32_t nextCellOffset = FIRST_CELL_OFFSET;
Cell::CellPtrList cellList;
uint32_t hbinSize = getRelativeOffsetNextHBIN();

do {
Cell::CellPtr nextCell = new Cell(_buf, getAbsoluteOffset(nextCellOffset));
uint32_t cellLen = nextCell->getLength();
if (cellLen == 0 || nextCellOffset + cellLen < nextCellOffset) {
delete nextCell;
throw RegistryParseException("Invalid cell length.");
}
cellList.push_back(nextCell);
nextCellOffset += nextCell->getLength();
nextCellOffset += cellLen;
}
while (nextCellOffset < getRelativeOffsetNextHBIN());
while (nextCellOffset < hbinSize);

return cellList;
}
Expand Down
2 changes: 1 addition & 1 deletion rejistry++/src/HBIN.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace Rejistry {
HBIN(const HBIN &);
HBIN& operator=(const HBIN &);

const REGFHeader * _header;
const REGFHeader * _header = NULL;
};
};

Expand Down
2 changes: 0 additions & 2 deletions rejistry++/src/LIRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ namespace Rejistry {
public:
static const std::string MAGIC;

typedef LIRecord * LIRecordPtr;

LIRecord(RegistryByteBuffer * buf, uint32_t offset);

virtual ~LIRecord() {}
Expand Down
Loading