@@ -66,7 +66,7 @@ void BinaryReader::SeekToItem(uint64_t seeklist_offset, uint64_t item_idx) {
66
66
throw ExpKitError (" Seeking is already in progress. Call EndSeek() first." );
67
67
68
68
seek_origin_offset_ = offset_;
69
- offset_ = seeklist_offset;
69
+ SeekTo ( seeklist_offset) ;
70
70
auto value = ReadUInt ();
71
71
auto offset_size = (value & 0x3 ) + 1 ;
72
72
auto item_count = value >> 2 ;
@@ -78,12 +78,12 @@ void BinaryReader::SeekToItem(uint64_t seeklist_offset, uint64_t item_idx) {
78
78
auto hdr_offset = offset_;
79
79
uint64_t item_offset = 0 ;
80
80
if (item_idx != 0 ) {
81
- offset_ += offset_size * (item_idx - 1 );
81
+ Skip ( offset_size * (item_idx - 1 ) );
82
82
item_offset = Uint (offset_size);
83
83
}
84
84
85
- // skip the seek list
86
- offset_ = hdr_offset + offset_size * item_count + item_offset;
85
+ // skip the remaining seek list and jump to the item
86
+ Skip ((item_count - item_idx) * offset_size + item_offset) ;
87
87
DebugLog (
88
88
" SeekToItem(): seeklist_offset=%u, item_idx=%u, offset_size=%u, "
89
89
" item_count=%u, item_offset=%u, new offset=%u" ,
@@ -97,7 +97,7 @@ uint64_t BinaryReader::SeekableListCount() {
97
97
auto offset_size = 1 << (hdr & 0x3 ); // 0=u1, 1=u2, 2=u4, 3=u8
98
98
auto item_count = hdr >> 2 ;
99
99
// skip the seek list
100
- offset_ += offset_size * item_count;
100
+ Skip ( offset_size * item_count) ;
101
101
return item_count;
102
102
}
103
103
@@ -157,6 +157,11 @@ uint16_t BinaryReader::ReadU16() { return *(uint16_t*)Read(2); }
157
157
158
158
uint8_t BinaryReader::ReadU8 () { return *(uint8_t *)Read (1 ); }
159
159
160
+ void BinaryReader::Skip (uint64_t len) {
161
+ SizeCheck (len);
162
+ offset_ += len;
163
+ }
164
+
160
165
uint8_t * BinaryReader::Read (uint16_t len) {
161
166
SizeCheck (len);
162
167
uint8_t * ptr = &data_.data ()[offset_];
@@ -171,6 +176,14 @@ void BinaryReader::SizeCheck(uint64_t len) {
171
176
offset_, len, EndOffset ());
172
177
}
173
178
179
+ void BinaryReader::SeekTo (uint64_t offset) {
180
+ if (offset >= EndOffset ())
181
+ throw ExpKitError (
182
+ " tried to read seek outside of the buffer: from_offset=%lu, to_offset=%lu, struct_end=%lu" ,
183
+ offset_, offset, EndOffset ());
184
+ offset_ = offset;
185
+ }
186
+
174
187
uint64_t BinaryReader::RemainingBytes () { return EndOffset () - offset_; }
175
188
176
189
uint64_t BinaryReader::EndOffset () {
0 commit comments