From 98a9d57c0bc2a217b7320d09d98a8adc7d13984e Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 29 Dec 2017 10:42:18 -0500 Subject: [PATCH] Imputes the alignment requirement for CAS headers. Also stops adding a spurious 0xff as the final byte on the tape. --- Storage/Tape/Formats/CAS.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Storage/Tape/Formats/CAS.cpp b/Storage/Tape/Formats/CAS.cpp index 6d8948c96..cc6872acb 100644 --- a/Storage/Tape/Formats/CAS.cpp +++ b/Storage/Tape/Formats/CAS.cpp @@ -41,15 +41,18 @@ CAS::CAS(const char *file_name) { chunk.long_header = bytes_are_equal && ((lookahead[0] == 0xd3) || (lookahead[0] == 0xd0) || (lookahead[0] == 0xea)); chunk.has_gap = chunk.long_header && (chunks_.size() > 1); - // Keep going until another header arrives or the file ends. - while(std::memcmp(lookahead, header_signature, sizeof(header_signature)) && !file.eof()) { + // Keep going until another header arrives or the file ends. Headers require the magic byte sequence, + // and also must be eight-byte aligned within the file. + while( !file.eof() && + (std::memcmp(lookahead, header_signature, sizeof(header_signature)) || ((file.tell()-10)&7))) { chunk.data.push_back(lookahead[0]); get_next(file, lookahead, 1); } - // If the file ended, flush the lookahead. + // If the file ended, flush the lookahead. The final thing in it will be a 0xff from the read that + // triggered the eof, so don't include that. if(file.eof()) { - for(std::size_t index = 0; index < sizeof(lookahead); index++) + for(std::size_t index = 0; index < sizeof(lookahead) - 1; index++) chunk.data.push_back(lookahead[index]); } }