Skip to content

Commit

Permalink
Fix checksum failure with small isos
Browse files Browse the repository at this point in the history
When the size of an iso was small enough that the fragment size was
smaller than the buffer size it wouldn't calculate or check the checksum
correctly (it would output 19 instead of 20 checksum fragments).

This fixes that by doing 2 things:
 * read the smaller of:
  - remaining bytes
  - fragment size
  - buffer size
 * When reading the last dangling bit of data add it to the checksum
   but do not write a 21st checksum fragment.

This is backwards compatible with the previous code and now you can
checksum any size iso.
  • Loading branch information
bcl committed Oct 28, 2021
1 parent f8484df commit 50cf14b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions libcheckisomd5.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static enum isomd5sum_status checkmd5sum(int isofd, checkCallback cb, void *cbda
size_t previous_fragment = 0UL;
off_t offset = 0LL;
while (offset < total_size) {
const size_t nbyte = MIN((size_t)(total_size - offset), buffer_size);
const size_t nbyte = MIN((size_t)(total_size - offset), MIN(fragment_size, buffer_size));

ssize_t nread = read(isofd, buffer, nbyte);
if (nread <= 0L)
Expand All @@ -89,7 +89,7 @@ static enum isomd5sum_status checkmd5sum(int isofd, checkCallback cb, void *cbda
const size_t current_fragment = offset / fragment_size;
const size_t fragmentsize = FRAGMENT_SUM_SIZE / info->fragmentcount;
/* If we're onto the next fragment, calculate the previous sum and check. */
if (current_fragment != previous_fragment) {
if (current_fragment != previous_fragment && current_fragment < info->fragmentcount) {
if (!validate_fragment(&hashctx, current_fragment, fragmentsize,
info->fragmentsums, NULL)) {
/* Exit immediately if current fragment sum is incorrect */
Expand Down
6 changes: 3 additions & 3 deletions libimplantisomd5.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ int implantISOFD(int isofd, int supported, int forceit, int quiet, char **errstr
const off_t fragment_size = total_size / (FRAGMENT_COUNT + 1);
size_t previous_fragment = 0UL;
off_t offset = 0LL;
while (offset < total_size) {
const size_t nbyte = MIN((size_t)(total_size - offset), buffer_size);
while (offset < total_size && previous_fragment < FRAGMENT_COUNT) {
const size_t nbyte = MIN((size_t)(total_size - offset), MIN(fragment_size, buffer_size));
ssize_t nread = read(isofd, buffer, nbyte);
if (nread <= 0L)
break;
Expand All @@ -119,7 +119,7 @@ int implantISOFD(int isofd, int supported, int forceit, int quiet, char **errstr
const size_t current_fragment = offset / fragment_size;
const size_t fragmentsize = FRAGMENT_SUM_SIZE / FRAGMENT_COUNT;
/* If we're onto the next fragment, calculate the previous sum and check. */
if (current_fragment != previous_fragment) {
if (current_fragment != previous_fragment && current_fragment < FRAGMENT_COUNT) {
validate_fragment(&hashctx, current_fragment, fragmentsize, NULL, fragmentsums);
previous_fragment = current_fragment;
}
Expand Down

0 comments on commit 50cf14b

Please sign in to comment.