Skip to content

Commit 91d4be6

Browse files
authored
Merge pull request #37 from crayy8/rejistry_updates
Rejistry updates
2 parents 366dabe + b10cced commit 91d4be6

25 files changed

Lines changed: 180 additions & 136 deletions

rejistry++/src/BinaryBlock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ namespace Rejistry {
5050
protected:
5151
BinaryBlock() {};
5252

53-
RegistryByteBuffer * _buf;
54-
uint32_t _offset;
53+
RegistryByteBuffer * _buf = 0;
54+
uint32_t _offset = 0;
5555

5656
uint16_t getWord(uint32_t offset) const;
5757

rejistry++/src/Buffer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ namespace Rejistry {
5151

5252
virtual ~Buffer();
5353

54-
uint32_t _capacity;
55-
uint32_t _limit;
56-
uint32_t _position;
54+
uint32_t _capacity = 0;
55+
uint32_t _limit = 0;
56+
uint32_t _position = 0;
5757
};
5858
};
5959

rejistry++/src/ByteBuffer.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace Rejistry {
5858
try {
5959
_buffer.resize(length);
6060
}
61-
catch (std::bad_alloc &e)
61+
catch (std::bad_alloc &)
6262
{
6363
throw RegistryParseException("Cannot allocate memory for registry byte buffer.");
6464
}
@@ -73,31 +73,31 @@ namespace Rejistry {
7373
}
7474

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

83-
if (offset > dst.size()) {
87+
if (offset >= dst.size()) {
8488
throw RegistryParseException("Offset is greater than destination buffer size.");
8589
}
8690

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

91-
if ((_position + offset) > _limit) {
92-
throw RegistryParseException("Starting position is beyond end of buffer.");
93-
}
94-
95-
if ((_position + offset + length) > _limit) {
95+
if ((_position + length) > _limit) {
9696
throw RegistryParseException("Number of requested bytes exceeds buffer size.");
9797
}
9898

99-
memcpy(&dst[0], &_buffer[_position + offset], length);
100-
_position += offset;
99+
memcpy(&dst[offset], &_buffer[_position], length);
100+
_position += length;
101101
}
102102

103103
/**

rejistry++/src/ByteBuffer.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ namespace Rejistry {
5555
* Copy 'length' bytes from this buffer into the given destination,
5656
* starting at the current position in this buffer and at the given
5757
* offset in the destination. The position of this buffer is incremented
58-
* by length.
58+
*
5959
* @param dst The destination into which to copy bytes.
60-
* @param offset The offset within the destination buffer to copy bytes to.
60+
* @param offset The offset within the destination buffer to start copying data
6161
* @param length The number of bytes to copy from this buffer.
6262
* @throws RegistryParseException
6363
*/
6464
void get(ByteArray& dst, const uint32_t offset, const uint32_t length);
6565

66-
/// Get two bytes from the current position in the buffer.
66+
/// Get two bytes from an offset in the buffer
6767
uint16_t getShort(uint32_t offset) const;
68-
/// Get four bytes from the current position in the buffer.
68+
/// Get four bytes from an offset in the buffer
6969
uint32_t getInt(uint32_t offset) const;
70-
/// Get eight bytes from the current position in the buffer.
70+
/// Get eight bytes from an offset in the buffer
7171
uint64_t getLong(uint32_t offset) const;
7272

7373
private:
@@ -76,14 +76,7 @@ namespace Rejistry {
7676

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

79-
template <typename T> T read() const {
80-
T bytes = read<T>(_position);
81-
if (bytes != NULL) {
82-
_position += sizeof(T);
83-
}
84-
return bytes;
85-
}
86-
79+
/// read at specified offset
8780
template <typename T> T read(uint32_t offset) const {
8881
if (offset + sizeof(T) <= _limit) {
8982
return *((T*)&_buffer[offset]);

rejistry++/src/Cell.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
*/
2828
#include <cstdlib>
29+
#include <memory>
2930

3031
// Local includes
3132
#include "Cell.h"
@@ -36,15 +37,23 @@
3637
namespace Rejistry {
3738

3839
uint32_t Cell::getLength() const {
39-
return std::abs((int)getDWord(LENGTH_OFFSET));
40+
int32_t raw = (int32_t)getDWord(LENGTH_OFFSET);
41+
if (raw == (int32_t)0x80000000) {
42+
throw RegistryParseException("Invalid cell length.");
43+
}
44+
return (uint32_t)(raw < 0 ? -raw : raw);
4045
}
4146

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

4651
std::vector<uint8_t> Cell::getData() const {
47-
return _buf->getData(getAbsoluteOffset(DATA_OFFSET), getLength() - DATA_OFFSET);
52+
uint32_t len = getLength();
53+
if (len < DATA_OFFSET) {
54+
throw RegistryParseException("Cell length too small.");
55+
}
56+
return _buf->getData(getAbsoluteOffset(DATA_OFFSET), len - DATA_OFFSET);
4857
}
4958

5059
std::string Cell::getDataSignature() const {
@@ -64,31 +73,31 @@ namespace Rejistry {
6473
}
6574

6675
SubkeyListRecord::SubkeyListRecordPtr Cell::getLFRecord() const {
67-
return new LFRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
76+
return std::make_unique<LFRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
6877
}
6978

7079
SubkeyListRecord::SubkeyListRecordPtr Cell::getLHRecord() const {
71-
return new LHRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
80+
return std::make_unique<LHRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
7281
}
7382

7483
SubkeyListRecord::SubkeyListRecordPtr Cell::getRIRecord() const {
75-
return new RIRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
84+
return std::make_unique<RIRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
7685
}
7786

78-
LIRecord::LIRecordPtr Cell::getLIRecord() const {
79-
return new LIRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
87+
SubkeyListRecord::SubkeyListRecordPtr Cell::getLIRecord() const {
88+
return std::make_unique<LIRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
8089
}
8190

8291
DBRecord::DBRecordPtr Cell::getDBRecord() const {
83-
return new DBRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
92+
return std::make_unique<DBRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
8493
}
8594

8695
DBIndirectRecord::DBIndirectRecordPtr Cell::getDBIndirectRecord() const {
87-
return new DBIndirectRecord(_buf, getAbsoluteOffset(DATA_OFFSET));
96+
return std::make_unique<DBIndirectRecord>(_buf, getAbsoluteOffset(DATA_OFFSET));
8897
}
8998

9099
ValueListRecord::ValueListRecordPtr Cell::getValueListRecord(const uint32_t numValues) const {
91-
return new ValueListRecord(_buf, getAbsoluteOffset(DATA_OFFSET), numValues);
100+
return std::make_unique<ValueListRecord>(_buf, getAbsoluteOffset(DATA_OFFSET), numValues);
92101
}
93102

94103
SubkeyListRecord::SubkeyListRecordPtr Cell::getSubkeyList() const {
@@ -110,5 +119,4 @@ namespace Rejistry {
110119
throw RegistryParseException("Unexpected subkey list type: " + magic);
111120
}
112121
}
113-
114122
};

rejistry++/src/Cell.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,49 +133,49 @@ namespace Rejistry {
133133

134134
/**
135135
* Interprets the cell data as an VKRecord and returns it.
136-
* @returns Pointer to an VKRecord object.
136+
* @returns Pointer to an VKRecord object. (Caller must free using delete)
137137
* @throws RegistryParseException
138138
*/
139139
VKRecord::VKRecordPtr getVKRecord() const;
140140

141141
/**
142142
* Interprets the cell data as an LFRecord and returns it.
143-
* @returns Pointer to an LFRecord object.
143+
* @returns Unique pointer to an LFRecord object.
144144
* @throws RegistryParseException
145145
*/
146146
SubkeyListRecord::SubkeyListRecordPtr getLFRecord() const;
147147

148148
/**
149149
* Interprets the cell data as an LHRecord and returns it.
150-
* @returns Pointer to an LHRecord object.
150+
* @returns Unique pointer to an LHRecord object.
151151
* @throws RegistryParseException
152152
*/
153153
SubkeyListRecord::SubkeyListRecordPtr getLHRecord() const;
154154

155155
/**
156156
* Interprets the cell data as an RIRecord and returns it.
157-
* @returns Pointer to an RIRecord object.
157+
* @returns Unique pointer to an RIRecord object.
158158
* @throws RegistryParseException
159159
*/
160160
SubkeyListRecord::SubkeyListRecordPtr getRIRecord() const;
161161

162162
/**
163163
* Interprets the cell data as an LIRecord and returns it.
164-
* @returns Pointer to an LIRecord object.
164+
* @returns Unique pointer to an LIRecord object.
165165
* @throws RegistryParseException
166166
*/
167-
LIRecord::LIRecordPtr getLIRecord() const;
167+
SubkeyListRecord::SubkeyListRecordPtr getLIRecord() const;
168168

169169
/**
170170
* Interprets the cell data as an DBRecord and returns it.
171-
* @returns Pointer to an DBRecord object.
171+
* @returns Unique pointer to an DBRecord object.
172172
* @throws RegistryParseException
173173
*/
174174
DBRecord::DBRecordPtr getDBRecord() const;
175175

176176
/**
177177
* Interprets the cell data as an DBIndirectRecord and returns it.
178-
* @returns Pointer to an DBIndirectRecord object.
178+
* @returns Unique pointer to an DBIndirectRecord object.
179179
* @throws RegistryParseException
180180
*/
181181
DBIndirectRecord::DBIndirectRecordPtr getDBIndirectRecord() const;
@@ -184,15 +184,14 @@ namespace Rejistry {
184184
* Interprets the cell data as an ValueListRecord and returns it.
185185
* @param numValues The number of values the value list should attempt
186186
* to parse.
187-
* @returns Pointer to an ValueListRecord object. The caller is responsible
188-
* for freeing the returned record.
187+
* @returns Unique pointer to a ValueListRecord object.
189188
* @throws RegistryParseException
190189
*/
191190
ValueListRecord::ValueListRecordPtr getValueListRecord(const uint32_t numValues) const;
192191

193192
/**
194193
* Interprets the cell data as a SubkeyList and returns it.
195-
* @returns Pointer to an SubkeyList object.
194+
* @returns Unique pointer to an SubkeyList object (do not need to free)
196195
* @throws RegistryParseException
197196
*/
198197
SubkeyListRecord::SubkeyListRecordPtr getSubkeyList() const;

rejistry++/src/DBIndirectRecord.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ namespace Rejistry {
5252
}
5353

5454
std::vector<uint8_t> cellData = c->getData();
55-
55+
if (cellData.size() < size) {
56+
throw RegistryParseException("DB indirect cell too small for requested data.");
57+
}
5658
data.insert(data.end(), cellData.begin(), cellData.begin() + size);
5759

5860
length -= size;

rejistry++/src/DBIndirectRecord.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define _REJISTRY_DBINDIRECTRECORD_H
3030

3131
#include <cstdint>
32+
#include <memory>
3233
#include <string>
3334

3435
// Local includes
@@ -41,7 +42,7 @@ namespace Rejistry {
4142
*/
4243
class DBIndirectRecord : public Record {
4344
public:
44-
typedef DBIndirectRecord * DBIndirectRecordPtr;
45+
typedef std::unique_ptr<DBIndirectRecord> DBIndirectRecordPtr;
4546

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

rejistry++/src/DBRecord.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace Rejistry {
5151
throw RegistryParseException("Failed to create Cell for DBRecord.");
5252
}
5353

54-
std::unique_ptr< DBIndirectRecord > dbi(c->getDBIndirectRecord());
54+
auto dbi = c->getDBIndirectRecord();
5555
return dbi->getData(length);
5656
}
5757

rejistry++/src/DBRecord.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define _REJISTRY_DBRECORD_H
3030

3131
#include <cstdint>
32+
#include <memory>
3233
#include <string>
3334

3435
// Local includes
@@ -41,7 +42,7 @@ namespace Rejistry {
4142
*/
4243
class DBRecord : public Record {
4344
public:
44-
typedef DBRecord * DBRecordPtr;
45+
typedef std::unique_ptr<DBRecord> DBRecordPtr;
4546

4647
DBRecord(RegistryByteBuffer * buf, uint32_t offset);
4748

0 commit comments

Comments
 (0)