[autofix.ci] apply automated fixes

This commit is contained in:
autofix-ci[bot]
2025-08-02 08:07:56 +00:00
committed by GitHub
parent 6fbc9626db
commit 62a3d33243
2 changed files with 36 additions and 31 deletions

View File

@@ -371,7 +371,7 @@ pub const PEFile = struct {
const updated_optional_header = self.getOptionalHeader();
updated_optional_header.size_of_image = alignSize(new_section.virtual_address + new_section.virtual_size, updated_optional_header.section_alignment);
updated_optional_header.size_of_initialized_data += new_section.size_of_raw_data;
// Update PE checksum after adding section
self.updateChecksum();
}
@@ -432,13 +432,13 @@ pub const PEFile = struct {
pub fn calculateChecksum(self: *const PEFile) u32 {
const data = self.data.items;
const file_size = data.len;
// Find checksum field offset
const checksum_offset = self.optional_header_offset + @offsetOf(OptionalHeader64, "checksum");
var checksum: u64 = 0;
var i: usize = 0;
// Process file as 16-bit words
while (i + 1 < file_size) : (i += 2) {
// Skip the checksum field itself (4 bytes)
@@ -446,17 +446,17 @@ pub const PEFile = struct {
i += 2; // Skip 4 bytes total
continue;
}
// Add 16-bit word to checksum
const word = std.mem.readInt(u16, data[i..][0..2], .little);
checksum += word;
// Handle overflow - fold back the carry
if (checksum > 0xFFFF) {
checksum = (checksum & 0xFFFF) + (checksum >> 16);
}
}
// If file size is odd, last byte is treated as if followed by 0x00
if (file_size & 1 != 0) {
checksum += data[file_size - 1];
@@ -464,14 +464,14 @@ pub const PEFile = struct {
checksum = (checksum & 0xFFFF) + (checksum >> 16);
}
}
// Final fold
checksum = (checksum & 0xFFFF) + (checksum >> 16);
checksum = (checksum + (checksum >> 16)) & 0xFFFF;
// Add file size to checksum
checksum += file_size;
return @intCast(checksum);
}
@@ -651,7 +651,7 @@ pub const PEFile = struct {
// Update the resource section
try self.updateResourceSection(rsrc_section.?, resource_data);
// Update PE checksum after all modifications
self.updateChecksum();
}

View File

@@ -28,10 +28,7 @@ describe.skipIf(isWindows)("Windows PE Checksum Verification", () => {
stderr: "pipe",
});
const [buildStderr, buildExitCode] = await Promise.all([
new Response(buildProc.stderr).text(),
buildProc.exited,
]);
const [buildStderr, buildExitCode] = await Promise.all([new Response(buildProc.stderr).text(), buildProc.exited]);
expect(buildExitCode).toBe(0);
expect(buildStderr).toBe("");
@@ -53,10 +50,10 @@ describe.skipIf(isWindows)("Windows PE Checksum Verification", () => {
// Extract checksum from objdump output
const checksumMatch = objdumpStdout.match(/CheckSum\s+([0-9a-fA-F]+)/);
expect(checksumMatch).not.toBeNull();
const checksum = checksumMatch![1];
console.log("PE checksum:", checksum);
// Checksum should not be 0 after our implementation
expect(checksum).not.toBe("00000000");
});
@@ -89,10 +86,7 @@ describe.skipIf(isWindows)("Windows PE Checksum Verification", () => {
stderr: "pipe",
});
const [buildStderr, buildExitCode] = await Promise.all([
new Response(buildProc.stderr).text(),
buildProc.exited,
]);
const [buildStderr, buildExitCode] = await Promise.all([new Response(buildProc.stderr).text(), buildProc.exited]);
expect(buildExitCode).toBe(0);
expect(buildStderr).toBe("");
@@ -113,10 +107,10 @@ describe.skipIf(isWindows)("Windows PE Checksum Verification", () => {
const checksumMatch = objdumpStdout.match(/CheckSum\s+([0-9a-fA-F]+)/);
expect(checksumMatch).not.toBeNull();
const checksum = checksumMatch![1];
console.log("PE checksum with resources:", checksum);
// Checksum should not be 0
expect(checksum).not.toBe("00000000");
});
@@ -126,9 +120,12 @@ describe.skipIf(isWindows)("Windows PE Checksum Verification", () => {
function createTestIcon() {
// ICO header (6 bytes)
const header = Buffer.from([
0x00, 0x00, // Reserved
0x01, 0x00, // Type (1 = ICO)
0x01, 0x00, // Count (1 icon)
0x00,
0x00, // Reserved
0x01,
0x00, // Type (1 = ICO)
0x01,
0x00, // Count (1 icon)
]);
// Directory entry (16 bytes)
@@ -137,10 +134,18 @@ function createTestIcon() {
0x10, // Height (16)
0x00, // Color count (0 = 256 colors)
0x00, // Reserved
0x01, 0x00, // Planes
0x08, 0x00, // Bit count
0x28, 0x01, 0x00, 0x00, // Bytes in resource (296)
0x16, 0x00, 0x00, 0x00, // Image offset (22)
0x01,
0x00, // Planes
0x08,
0x00, // Bit count
0x28,
0x01,
0x00,
0x00, // Bytes in resource (296)
0x16,
0x00,
0x00,
0x00, // Image offset (22)
]);
// Minimal BMP data
@@ -156,4 +161,4 @@ function createTestIcon() {
const imageData = Buffer.alloc(256); // 16x16x8bpp
return Buffer.concat([header, dirEntry, bmpHeader, imageData]);
}
}