Skip to content

Commit

Permalink
Bug fix, it was missing the last block if block aligned and not empty
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianpls committed Dec 7, 2023
1 parent a217135 commit 22df677
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
28 changes: 13 additions & 15 deletions mrf_apps/can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ int can(const options &opt) {
vector<uint32_t> header(header_size / sizeof(uint32_t));

// Reserve space for the header
FSEEK(out_idx, header_size, SEEK_SET);
fwrite(header.data(), sizeof(uint32_t), header.size(), out_idx);

// Running count of output blocks
size_t count = 0;
Expand Down Expand Up @@ -319,23 +319,21 @@ int can(const options &opt) {
}
}

auto extra_bytes = in_size % BSZ;
auto extra_bytes = (in_size % BSZ) ? (in_size % BSZ) : BSZ;

// The very last block may be partial
if (extra_bytes) {
memset(buffer, 0, BSZ);
if (extra_bytes != fread(buffer, 1, extra_bytes, in_idx)) {
cerr << "Error reading block from input file\n";
return IO_ERR;
}
// The very last block may be partial, but it always exists
memset(buffer, 0, BSZ);
if (extra_bytes != fread(buffer, 1, extra_bytes, in_idx)) {
cerr << "Error reading block from input file\n";
return IO_ERR;
}

if (!check(buffer)) {
if (extra_bytes != fwrite(buffer, 1, extra_bytes, out_idx)) {
cerr << "Error writing to output file\n";
return IO_ERR;
}
BIT_SET(line, bit_pos);
if (!check(buffer)) {
if (extra_bytes != fwrite(buffer, 1, extra_bytes, out_idx)) {
cerr << "Error writing to output file\n";
return IO_ERR;
}
BIT_SET(line, bit_pos);
}
line += 4; // Points to the header end
fclose(in_idx);
Expand Down
1 change: 1 addition & 0 deletions mrf_apps/vc/can/can.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<EnableEnhancedInstructionSet>AdvancedVectorExtensions2</EnableEnhancedInstructionSet>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down

0 comments on commit 22df677

Please sign in to comment.