Skip to content

Commit

Permalink
Switch to using 'md5sum_totalbytes =' for the total bytes variable
Browse files Browse the repository at this point in the history
  • Loading branch information
pbatard committed Feb 26, 2024
1 parent 3efd957 commit cd4c261
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 37 deletions.
42 changes: 23 additions & 19 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "boot.h"

/* The hash sum list file may provide a comment with the total size of bytes to process */
STATIC CONST CHAR8 TotalBytesString[] = "TotalBytes:";
STATIC CONST CHAR8 TotalBytesString[] = "md5sum_totalbytes";

/**
Parse a hash sum list file and populate a HASH_LIST structure from it.
Expand Down Expand Up @@ -147,7 +147,7 @@ EFI_STATUS Parse(

// Parse comments
if (HashFile[i] == '#') {
// Look for a "# TotalBytes: 0x0123456789abcdef" comment
// Look for an "md5sum_totalbytes = 0x########" comment

// Set c to the start of the comment (skipping the '#' prefix)
c = i + 1;
Expand All @@ -161,32 +161,36 @@ EFI_STATUS Parse(
while (c < i - 1 && IsWhiteSpace(HashFile[c]))
c++;

// See if we have a match for "TotalBytes:"
// See if we have a match for "md5sum_totalbytes = 0x########"
if (i > c + sizeof(TotalBytesString) - 1 && (CompareMem(&HashFile[c],
TotalBytesString, sizeof(TotalBytesString) - 1) == 0)) {
// Look for an '0x' prefix and parse the 64-bit hexascii value
// if valid.
NumDigits = 0;
// Look for an equal sign
c += sizeof(TotalBytesString) - 1;
while (c < i - 1 && IsWhiteSpace(HashFile[c]))
c++;
NumDigits = 0;
if (c < i - 2 && HashFile[c] == '0' && HashFile[c + 1] == 'x') {
c += 2;
for (; c < i - 1; c++) {
if (HashFile[c] == ' ')
continue;
if (!IsValidHexAscii(HashFile[c])) {
NumDigits = 0;
break;
if (HashFile[c++] == '=') {
// Look for an '0x' prefix and parse a 64-bit *lowercase*
// hexascii value if valid.
while (c < i - 1 && IsWhiteSpace(HashFile[c]))
c++;
if (c < i - 2 && HashFile[c] == '0' && HashFile[c + 1] == 'x') {
for (c += 2; c < i - 1; c++) {
if (HashFile[c] == ' ')
continue;
if (!IsValidHexAscii(HashFile[c])) {
NumDigits = 0;
break;
}
NumDigits++;
TotalBytes <<= 4;
TotalBytes |= (UINT64)(((HashFile[c] - '0') < 0xa) ?
(HashFile[c] - '0') : (HashFile[c] - 'a' + 0xa));
}
NumDigits++;
TotalBytes <<= 4;
TotalBytes |= ((HashFile[c] - '0') < 0xa) ?
(HashFile[c] - '0') : (HashFile[c] - 'a' + 0xa);
}
}
if (NumDigits == 0 || NumDigits > 16) {
PrintWarning(L"Ignoring invalid TotalBytes value");
PrintWarning(L"Ignoring invalid md5sum_totalbytes value");
TotalBytes = 0;
}
}
Expand Down
50 changes: 32 additions & 18 deletions tests/test_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
[FAIL] Invalid data in 'This entry is invalid and should': [21] Aborted

# Hash containing NUL
> echo "001122334455 6778899aabbccddeeff This hash contains a NUL" | tr '\11' '\0' > image/md5sum.txt
> echo "001122334455 6778899aabbccddeeff This hash contains a NUL" | tr '\11' '\0' > image/md5sum.txt
[FAIL] 'md5sum.txt' contains invalid data: [21] Aborted

# Hash containing space
Expand All @@ -41,24 +41,24 @@
[FAIL] Invalid data in '00112233445566778899azbbccddeeff': [21] Aborted

# Hash too short
> echo "00112233445566778899aabbccddeef Hash is too short" > image/md5sum.txt
> echo "00112233445566778899aabbccddeef Hash is too short" > image/md5sum.txt
[FAIL] Invalid data in '00112233445566778899aabbccddeef ': [21] Aborted

# Hash too long
> echo "00112233445566778899aabbccddeeffa Hash is too long" > image/md5sum.txt
> echo "00112233445566778899aabbccddeeffa Hash is too long" > image/md5sum.txt
[FAIL] Invalid data after '00112233445566778899aabbccddeeff': [21] Aborted

# Hash containing uppercase
> echo "00112233445566778899AaBbCcDdEeFf Mixed case hash" > image/md5sum.txt
> echo "00112233445566778899AaBbCcDdEeFf Mixed case hash" > image/md5sum.txt
[FAIL] File 'Mixed case hash': [14] Not Found
1/1 file processed [1 failed]

# Path containing NUL
> echo "00112233445566778899aabbccddeeff This path contains a NUL" | tr '\11' '\0' > image/md5sum.txt
> echo "00112233445566778899aabbccddeeff This path contains a NUL" | tr '\11' '\0' > image/md5sum.txt
[FAIL] 'md5sum.txt' contains invalid data: [21] Aborted

# Path containing TAB
> echo "00112233445566778899aabbccddeeff This file name contains a TAB" > image/md5sum.txt
> echo "00112233445566778899aabbccddeeff This file name contains a TAB" > image/md5sum.txt
[FAIL] Invalid data after '00112233445566778899aabbccddeeff': [21] Aborted

# Path too long
Expand Down Expand Up @@ -112,58 +112,72 @@

# Empty TotalBytes
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "# TotalBytes:" >> image/md5sum.txt
[WARN] Ignoring invalid TotalBytes value
> echo "# md5sum_totalbytes =" >> image/md5sum.txt
[WARN] Ignoring invalid md5sum_totalbytes value
[TEST] TotalBytes = 0x0
[FAIL] File 'file': [14] Not Found
1/1 file processed [1 failed]

# Invalid TotalBytes
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "# TotalBytes: invalid" >> image/md5sum.txt
[WARN] Ignoring invalid TotalBytes value
> echo "# md5sum_totalbytes = invalid" >> image/md5sum.txt
[WARN] Ignoring invalid md5sum_totalbytes value
[TEST] TotalBytes = 0x0
[FAIL] File 'file': [14] Not Found
1/1 file processed [1 failed]

# TotalBytes invalid 0x prefix
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "# TotalBytes: 0 x1234" >> image/md5sum.txt
[WARN] Ignoring invalid TotalBytes value
> echo "# md5sum_totalbytes = 0 x1234" >> image/md5sum.txt
[WARN] Ignoring invalid md5sum_totalbytes value
[TEST] TotalBytes = 0x0
[FAIL] File 'file': [14] Not Found
1/1 file processed [1 failed]

# TotalBytes missing 0x prefix
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "# TotalBytes: 1234" >> image/md5sum.txt
[WARN] Ignoring invalid TotalBytes value
> echo "# md5sum_totalbytes = 1234" >> image/md5sum.txt
[WARN] Ignoring invalid md5sum_totalbytes value
[TEST] TotalBytes = 0x0
[FAIL] File 'file': [14] Not Found
1/1 file processed [1 failed]

# TotalBytes 1 character value
> echo "# TotalBytes: 0x8 " > image/md5sum.txt
> echo "# md5sum_totalbytes = 0x8 " > image/md5sum.txt
> echo "00112233445566778899aabbccddeeff file" >> image/md5sum.txt
[TEST] TotalBytes = 0x8
[FAIL] File 'file': [14] Not Found
1/1 file processed [1 failed]

# TotalBytes 16 characters value
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "#TotalBytes: 0x12345678abcdef0" >> image/md5sum.txt
> echo "#md5sum_totalbytes = 0x12345678abcdef0" >> image/md5sum.txt
[TEST] TotalBytes = 0x12345678ABCDEF0
[FAIL] File 'file': [14] Not Found
1/1 file processed [1 failed]

# TotalBytes 17 characters value
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "# TotalBytes: 0xabcdabcdabcdabcd0 " >> image/md5sum.txt
[WARN] Ignoring invalid TotalBytes value
> echo "# md5sum_totalbytes = 0xabcdabcdabcdabcd0 " >> image/md5sum.txt
[WARN] Ignoring invalid md5sum_totalbytes value
[TEST] TotalBytes = 0x0
[FAIL] File 'file': [14] Not Found
1/1 file processed [1 failed]

# TotalBytes no spaces
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "#md5sum_totalbytes=0x1234abcd" >> image/md5sum.txt
[TEST] TotalBytes = 0x1234ABCD
[FAIL] File 'file': [14] Not Found
1/1 file processed [1 failed]

# TotalBytes multiple spaces
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "# md5sum_totalbytes = 0x1234abcd" >> image/md5sum.txt
[TEST] TotalBytes = 0x1234ABCD
[FAIL] File 'file': [14] Not Found
1/1 file processed [1 failed]

# MD5 basic validation for up to 2-blocks
> # Tests all sizes up to 2-blocks (2 x 64 bytes)
> for size in {000..128}; do
Expand Down

0 comments on commit cd4c261

Please sign in to comment.