1414
1515#include < config.h>
1616#include " blockhash.h"
17+ #include < cstddef>
1718#include < stdint.h> // uint32_t
1819#include < string.h> // memcpy, memcmp
1920#include < algorithm> // std::min
@@ -27,7 +28,7 @@ typedef unsigned long uword_t; // a machine word NOLINT
2728
2829BlockHash::BlockHash (const char * source_data,
2930 size_t source_size,
30- int starting_offset)
31+ size_t starting_offset)
3132 : source_data_(source_data),
3233 source_size_ (source_size),
3334 hash_table_mask_(0 ),
@@ -95,7 +96,7 @@ BlockHash* BlockHash::CreateTargetHash(const char* target_data,
9596 size_t dictionary_size) {
9697 BlockHash* new_target_hash = new BlockHash (target_data,
9798 target_size,
98- static_cast <int >(dictionary_size));
99+ static_cast <ptrdiff_t >(dictionary_size));
99100 if (!new_target_hash->Init (/* populate_hash_table = */ false )) {
100101 delete new_target_hash;
101102 return NULL ;
@@ -156,9 +157,9 @@ void BlockHash::AddBlock(uint32_t hash_value) {
156157 return ;
157158 }
158159 // The initial value of last_block_added_ is -1.
159- int block_number = last_block_added_ + 1 ;
160- const int total_blocks =
161- static_cast <int >(source_size_ / kBlockSize ); // round down
160+ size_t block_number = last_block_added_ + 1 ;
161+ const size_t total_blocks =
162+ static_cast <size_t >(source_size_ / kBlockSize ); // round down
162163 if (block_number >= total_blocks) {
163164 VCD_DFATAL << " BlockHash::AddBlock() called"
164165 " with block number " << block_number
@@ -174,7 +175,7 @@ void BlockHash::AddBlock(uint32_t hash_value) {
174175 return ;
175176 }
176177 const uint32_t hash_table_index = GetHashTableIndex (hash_value);
177- const int first_matching_block = hash_table_[hash_table_index];
178+ const size_t first_matching_block = hash_table_[hash_table_index];
178179 if (first_matching_block < 0 ) {
179180 // This is the first entry with this hash value
180181 hash_table_[hash_table_index] = block_number;
@@ -200,14 +201,14 @@ void BlockHash::AddAllBlocks() {
200201 AddAllBlocksThroughIndex (static_cast <int >(source_size_));
201202}
202203
203- void BlockHash::AddAllBlocksThroughIndex (int end_index) {
204- if (end_index > static_cast <int >(source_size_)) {
204+ void BlockHash::AddAllBlocksThroughIndex (size_t end_index) {
205+ if (end_index > static_cast <size_t >(source_size_)) {
205206 VCD_DFATAL << " BlockHash::AddAllBlocksThroughIndex() called"
206207 " with index " << end_index
207208 << " higher than end index " << source_size_ << VCD_ENDL ;
208209 return ;
209210 }
210- const int last_index_added = last_block_added_ * kBlockSize ;
211+ const size_t last_index_added = last_block_added_ * kBlockSize ;
211212 if (end_index <= last_index_added) {
212213 VCD_DFATAL << " BlockHash::AddAllBlocksThroughIndex() called"
213214 " with index " << end_index
@@ -221,11 +222,11 @@ void BlockHash::AddAllBlocksThroughIndex(int end_index) {
221222 // See: https://github.com/google/open-vcdiff/issues/40
222223 return ;
223224 }
224- int end_limit = end_index;
225+ size_t end_limit = end_index;
225226 // Don't allow reading any indices at or past source_size_.
226227 // The Hash function extends (kBlockSize - 1) bytes past the index,
227228 // so leave a margin of that size.
228- int last_legal_hash_index = static_cast <int >(source_size () - kBlockSize );
229+ size_t last_legal_hash_index = static_cast <size_t >(source_size () - kBlockSize );
229230 if (end_limit > last_legal_hash_index) {
230231 end_limit = last_legal_hash_index + 1 ;
231232 }
@@ -300,9 +301,9 @@ bool BlockHash::BlockContentsMatch(const char* block1, const char* block2) {
300301 return BlockContentsMatchInline (block1, block2);
301302}
302303
303- inline int BlockHash::SkipNonMatchingBlocks (int block_number,
304+ inline size_t BlockHash::SkipNonMatchingBlocks (size_t block_number,
304305 const char * block_ptr) const {
305- int probes = 0 ;
306+ size_t probes = 0 ;
306307 while ((block_number >= 0 ) &&
307308 !BlockContentsMatchInline (block_ptr,
308309 &source_data_[block_number * kBlockSize ])) {
@@ -317,18 +318,18 @@ inline int BlockHash::SkipNonMatchingBlocks(int block_number,
317318// Init() must have been called and returned true before using
318319// FirstMatchingBlock or NextMatchingBlock. No check is performed
319320// for this condition; the code will crash if this condition is violated.
320- inline int BlockHash::FirstMatchingBlockInline (uint32_t hash_value,
321+ inline size_t BlockHash::FirstMatchingBlockInline (uint32_t hash_value,
321322 const char * block_ptr) const {
322323 return SkipNonMatchingBlocks (hash_table_[GetHashTableIndex (hash_value)],
323324 block_ptr);
324325}
325326
326- int BlockHash::FirstMatchingBlock (uint32_t hash_value,
327+ size_t BlockHash::FirstMatchingBlock (uint32_t hash_value,
327328 const char * block_ptr) const {
328329 return FirstMatchingBlockInline (hash_value, block_ptr);
329330}
330331
331- int BlockHash::NextMatchingBlock (int block_number,
332+ size_t BlockHash::NextMatchingBlock (size_t block_number,
332333 const char * block_ptr) const {
333334 if (static_cast <size_t >(block_number) >= GetNumberOfBlocks ()) {
334335 VCD_DFATAL << " NextMatchingBlock called for invalid block number "
@@ -343,7 +344,7 @@ int BlockHash::NextMatchingBlock(int block_number,
343344// dictionary is made up of spaces (' ') and the search string is also
344345// made up of spaces, there will be one match for each block in the
345346// dictionary.
346- inline bool BlockHash::TooManyMatches (int * match_counter) {
347+ inline bool BlockHash::TooManyMatches (size_t * match_counter) {
347348 ++(*match_counter);
348349 return (*match_counter) > kMaxMatchesToCheck ;
349350}
@@ -352,12 +353,12 @@ inline bool BlockHash::TooManyMatches(int* match_counter) {
352353// that match the corresponding bytes to the left of target_match_start.
353354// Will not examine more than max_bytes bytes, which is to say that
354355// the return value will be in the range [0, max_bytes] inclusive.
355- int BlockHash::MatchingBytesToLeft (const char * source_match_start,
356+ size_t BlockHash::MatchingBytesToLeft (const char * source_match_start,
356357 const char * target_match_start,
357- int max_bytes) {
358+ size_t max_bytes) {
358359 const char * source_ptr = source_match_start;
359360 const char * target_ptr = target_match_start;
360- int bytes_found = 0 ;
361+ size_t bytes_found = 0 ;
361362 while (bytes_found < max_bytes) {
362363 --source_ptr;
363364 --target_ptr;
@@ -373,12 +374,12 @@ int BlockHash::MatchingBytesToLeft(const char* source_match_start,
373374// that match the corresponding bytes starting at target_match_end.
374375// Will not examine more than max_bytes bytes, which is to say that
375376// the return value will be in the range [0, max_bytes] inclusive.
376- int BlockHash::MatchingBytesToRight (const char * source_match_end,
377+ size_t BlockHash::MatchingBytesToRight (const char * source_match_end,
377378 const char * target_match_end,
378- int max_bytes) {
379+ size_t max_bytes) {
379380 const char * source_ptr = source_match_end;
380381 const char * target_ptr = target_match_end;
381- int bytes_found = 0 ;
382+ size_t bytes_found = 0 ;
382383 while ((bytes_found < max_bytes) && (*source_ptr == *target_ptr)) {
383384 ++bytes_found;
384385 ++source_ptr;
@@ -399,24 +400,24 @@ void BlockHash::FindBestMatch(uint32_t hash_value,
399400 const char * target_start,
400401 size_t target_size,
401402 Match* best_match) const {
402- int match_counter = 0 ;
403- for (int block_number = FirstMatchingBlockInline (hash_value,
403+ size_t match_counter = 0 ;
404+ for (ptrdiff_t block_number = FirstMatchingBlockInline (hash_value,
404405 target_candidate_start);
405406 (block_number >= 0 ) && !TooManyMatches (&match_counter);
406407 block_number = NextMatchingBlock (block_number, target_candidate_start)) {
407- int source_match_offset = block_number * kBlockSize ;
408- const int source_match_end = source_match_offset + kBlockSize ;
408+ ptrdiff_t source_match_offset = block_number * kBlockSize ;
409+ const ptrdiff_t source_match_end = source_match_offset + kBlockSize ;
409410
410- int target_match_offset =
411- static_cast <int >(target_candidate_start - target_start);
412- const int target_match_end = target_match_offset + kBlockSize ;
411+ ptrdiff_t target_match_offset =
412+ static_cast <ptrdiff_t >(target_candidate_start - target_start);
413+ const ptrdiff_t target_match_end = target_match_offset + kBlockSize ;
413414
414415 size_t match_size = kBlockSize ;
415416 {
416417 // Extend match start towards beginning of unencoded data
417- const int limit_bytes_to_left = std::min (source_match_offset,
418+ const ptrdiff_t limit_bytes_to_left = std::min (source_match_offset,
418419 target_match_offset);
419- const int matching_bytes_to_left =
420+ const ptrdiff_t matching_bytes_to_left =
420421 MatchingBytesToLeft (source_data_ + source_match_offset,
421422 target_start + target_match_offset,
422423 limit_bytes_to_left);
@@ -433,7 +434,7 @@ void BlockHash::FindBestMatch(uint32_t hash_value,
433434 match_size +=
434435 MatchingBytesToRight (source_data_ + source_match_end,
435436 target_start + target_match_end,
436- static_cast <int >(limit_bytes_to_right));
437+ static_cast <ptrdiff_t >(limit_bytes_to_right));
437438 }
438439 // Update in/out parameter if the best match found was better
439440 // than any match already stored in *best_match.
0 commit comments