Skip to content

Commit

Permalink
Update for latest Zig
Browse files Browse the repository at this point in the history
  • Loading branch information
squeek502 committed Nov 9, 2023
1 parent 8111b1c commit 05b320f
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
submodules: true
- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.12.0-dev.1344+37295696e
version: 0.12.0-dev.1513+77bc8e7b6
- uses: ilammy/msvc-dev-cmd@v1
if: ${{ matrix.os == 'windows-latest' }}
with:
Expand Down
4 changes: 2 additions & 2 deletions src/ani.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ fn getAniheaderFlags(reader: anytype) !u32 {
const riff_header = try reader.readBytesNoEof(4);
if (!std.mem.eql(u8, &riff_header, "RIFF")) return error.InvalidFormat;

_ = try reader.readIntLittle(u32); // size of RIFF chunk
_ = try reader.readInt(u32, .little); // size of RIFF chunk

const form_type = try reader.readBytesNoEof(4);
if (!std.mem.eql(u8, &form_type, "ACON")) return error.InvalidFormat;

while (true) {
const chunk_id = try reader.readBytesNoEof(4);
const chunk_len = try reader.readIntLittle(u32);
const chunk_len = try reader.readInt(u32, .little);
if (!std.mem.eql(u8, &chunk_id, "anih")) {
// TODO: Move file cursor instead of skipBytes
try reader.skipBytes(chunk_len, .{});
Expand Down
14 changes: 8 additions & 6 deletions src/bmp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

const std = @import("std");
const BitmapHeader = @import("ico.zig").BitmapHeader;
const builtin = @import("builtin");
const native_endian = builtin.cpu.arch.endian();

pub const windows_format_id = std.mem.readIntNative(u16, "BM");
pub const windows_format_id = std.mem.readInt(u16, "BM", native_endian);
pub const file_header_len = 14;

pub const ReadError = error{
Expand Down Expand Up @@ -89,19 +91,19 @@ pub fn read(reader: anytype, max_size: u64) ReadError!BitmapInfo {
var bitmap_info: BitmapInfo = undefined;
const file_header = reader.readBytesNoEof(file_header_len) catch return error.UnexpectedEOF;

const id = std.mem.readIntNative(u16, file_header[0..2]);
const id = std.mem.readInt(u16, file_header[0..2], native_endian);
if (id != windows_format_id) return error.InvalidFileHeader;

bitmap_info.pixel_data_offset = std.mem.readIntLittle(u32, file_header[10..14]);
bitmap_info.pixel_data_offset = std.mem.readInt(u32, file_header[10..14], .little);
if (bitmap_info.pixel_data_offset > max_size) return error.ImpossiblePixelDataOffset;

bitmap_info.dib_header_size = reader.readIntLittle(u32) catch return error.UnexpectedEOF;
bitmap_info.dib_header_size = reader.readInt(u32, .little) catch return error.UnexpectedEOF;
if (bitmap_info.pixel_data_offset < file_header_len + bitmap_info.dib_header_size) return error.ImpossiblePixelDataOffset;
const dib_version = BitmapHeader.Version.get(bitmap_info.dib_header_size);
switch (dib_version) {
.@"nt3.1", .@"nt4.0", .@"nt5.0" => {
var dib_header_buf: [@sizeOf(BITMAPINFOHEADER)]u8 align(@alignOf(BITMAPINFOHEADER)) = undefined;
std.mem.writeIntLittle(u32, dib_header_buf[0..4], bitmap_info.dib_header_size);
std.mem.writeInt(u32, dib_header_buf[0..4], bitmap_info.dib_header_size, .little);
reader.readNoEof(dib_header_buf[4..]) catch return error.UnexpectedEOF;
var dib_header: *BITMAPINFOHEADER = @ptrCast(&dib_header_buf);
structFieldsLittleToNative(BITMAPINFOHEADER, dib_header);
Expand All @@ -116,7 +118,7 @@ pub fn read(reader: anytype, max_size: u64) ReadError!BitmapInfo {
},
.@"win2.0" => {
var dib_header_buf: [@sizeOf(BITMAPCOREHEADER)]u8 align(@alignOf(BITMAPCOREHEADER)) = undefined;
std.mem.writeIntLittle(u32, dib_header_buf[0..4], bitmap_info.dib_header_size);
std.mem.writeInt(u32, dib_header_buf[0..4], bitmap_info.dib_header_size, .little);
reader.readNoEof(dib_header_buf[4..]) catch return error.UnexpectedEOF;
var dib_header: *BITMAPCOREHEADER = @ptrCast(&dib_header_buf);
structFieldsLittleToNative(BITMAPCOREHEADER, dib_header);
Expand Down
189 changes: 95 additions & 94 deletions src/compile.zig

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions src/errors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const bmp = @import("bmp.zig");
const parse = @import("parse.zig");
const lang = @import("lang.zig");
const CodePage = @import("code_pages.zig").CodePage;
const builtin = @import("builtin");
const native_endian = builtin.cpu.arch.endian();

pub const Diagnostics = struct {
errors: std.ArrayListUnmanaged(ErrorDetails) = .{},
Expand Down Expand Up @@ -655,24 +657,24 @@ pub const ErrorDetails = struct {
},
.bmp_ignored_palette_bytes => {
const bytes = strings[self.extra.number];
const ignored_bytes = std.mem.readIntNative(u64, bytes[0..8]);
const ignored_bytes = std.mem.readInt(u64, bytes[0..8], native_endian);
try writer.print("bitmap has {d} extra bytes preceding the pixel data which will be ignored", .{ignored_bytes});
},
.bmp_missing_palette_bytes => {
const bytes = strings[self.extra.number];
const missing_bytes = std.mem.readIntNative(u64, bytes[0..8]);
const missing_bytes = std.mem.readInt(u64, bytes[0..8], native_endian);
try writer.print("bitmap has {d} missing color palette bytes which will be padded with zeroes", .{missing_bytes});
},
.rc_would_miscompile_bmp_palette_padding => {
const bytes = strings[self.extra.number];
const miscompiled_bytes = std.mem.readIntNative(u64, bytes[0..8]);
const miscompiled_bytes = std.mem.readInt(u64, bytes[0..8], native_endian);
try writer.print("the missing color palette bytes would be miscompiled by the Win32 RC compiler (the added padding bytes would include {d} bytes of the pixel data)", .{miscompiled_bytes});
},
.bmp_too_many_missing_palette_bytes => switch (self.type) {
.err, .warning => {
const bytes = strings[self.extra.number];
const missing_bytes = std.mem.readIntNative(u64, bytes[0..8]);
const max_missing_bytes = std.mem.readIntNative(u64, bytes[8..16]);
const missing_bytes = std.mem.readInt(u64, bytes[0..8], native_endian);
const max_missing_bytes = std.mem.readInt(u64, bytes[8..16], native_endian);
try writer.print("bitmap has {} missing color palette bytes which exceeds the maximum of {}", .{ missing_bytes, max_missing_bytes });
},
// TODO: command line option
Expand Down
4 changes: 2 additions & 2 deletions src/fnt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub const FontDirEntry = struct {
inline for (@typeInfo(FontDirEntry).Struct.fields) |field| {
switch (field.type) {
u8 => @field(entry, field.name) = try reader.readByte(),
u16, u32 => @field(entry, field.name) = try reader.readIntLittle(field.type),
u16, u32 => @field(entry, field.name) = try reader.readInt(field.type, .little),
[60]u8 => @field(entry, field.name) = try reader.readBytesNoEof(60),
else => @compileError("unknown field in FontDirEntry"),
}
Expand All @@ -201,7 +201,7 @@ pub const FontDirEntry = struct {
inline for (@typeInfo(FontDirEntry).Struct.fields) |field| {
switch (field.type) {
u8 => try writer.writeByte(@field(entry, field.name)),
u16, u32 => try writer.writeIntLittle(field.type, @field(entry, field.name)),
u16, u32 => try writer.writeInt(field.type, @field(entry, field.name), .little),
[60]u8 => try writer.writeAll(&@field(entry, field.name)),
else => @compileError("unknown field in FontDirEntry"),
}
Expand Down
68 changes: 35 additions & 33 deletions src/ico.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! https://learn.microsoft.com/en-us/windows/win32/menurc/localheader

const std = @import("std");
const builtin = @import("builtin");
const native_endian = builtin.cpu.arch.endian();

pub const ReadError = std.mem.Allocator.Error || error{ InvalidHeader, InvalidImageType, ImpossibleDataSize, UnexpectedEOF, ReadError };

Expand Down Expand Up @@ -37,17 +39,17 @@ pub fn read(allocator: std.mem.Allocator, reader: anytype, max_size: u64) ReadEr
// to do this. Maybe it makes more sense to handle the translation
// at the call site instead of having a helper function here.
pub fn readAnyError(allocator: std.mem.Allocator, reader: anytype, max_size: u64) !IconDir {
const reserved = try reader.readIntLittle(u16);
const reserved = try reader.readInt(u16, .little);
if (reserved != 0) {
return error.InvalidHeader;
}

const image_type = reader.readEnum(ImageType, .Little) catch |err| switch (err) {
const image_type = reader.readEnum(ImageType, .little) catch |err| switch (err) {
error.InvalidValue => return error.InvalidImageType,
else => |e| return e,
};

const num_images = try reader.readIntLittle(u16);
const num_images = try reader.readInt(u16, .little);

// To avoid over-allocation in the case of a file that says it has way more
// entries than it actually does, we use an ArrayList with a conservatively
Expand All @@ -66,19 +68,19 @@ pub fn readAnyError(allocator: std.mem.Allocator, reader: anytype, max_size: u64
switch (image_type) {
.icon => {
entry.type_specific_data = .{ .icon = .{
.color_planes = try reader.readIntLittle(u16),
.bits_per_pixel = try reader.readIntLittle(u16),
.color_planes = try reader.readInt(u16, .little),
.bits_per_pixel = try reader.readInt(u16, .little),
} };
},
.cursor => {
entry.type_specific_data = .{ .cursor = .{
.hotspot_x = try reader.readIntLittle(u16),
.hotspot_y = try reader.readIntLittle(u16),
.hotspot_x = try reader.readInt(u16, .little),
.hotspot_y = try reader.readInt(u16, .little),
} };
},
}
entry.data_size_in_bytes = try reader.readIntLittle(u32);
entry.data_offset_from_start_of_file = try reader.readIntLittle(u32);
entry.data_size_in_bytes = try reader.readInt(u32, .little);
entry.data_offset_from_start_of_file = try reader.readInt(u32, .little);
// Validate that the offset/data size is feasible
if (@as(u64, entry.data_offset_from_start_of_file) + entry.data_size_in_bytes > max_size) {
return error.ImpossibleDataSize;
Expand Down Expand Up @@ -133,10 +135,10 @@ pub const IconDir = struct {
}

pub fn writeResData(self: IconDir, writer: anytype, first_image_id: u16) !void {
try writer.writeIntLittle(u16, 0);
try writer.writeIntLittle(u16, @intFromEnum(self.image_type));
try writer.writeInt(u16, 0, .little);
try writer.writeInt(u16, @intFromEnum(self.image_type), .little);
// We know that entries.len must fit into a u16
try writer.writeIntLittle(u16, @as(u16, @intCast(self.entries.len)));
try writer.writeInt(u16, @as(u16, @intCast(self.entries.len)), .little);

var image_id = first_image_id;
for (self.entries) |entry| {
Expand Down Expand Up @@ -173,23 +175,23 @@ pub const Entry = struct {
pub fn writeResData(self: Entry, writer: anytype, id: u16) !void {
switch (self.type_specific_data) {
.icon => |icon_data| {
try writer.writeIntLittle(u8, @as(u8, @truncate(self.width)));
try writer.writeIntLittle(u8, @as(u8, @truncate(self.height)));
try writer.writeIntLittle(u8, self.num_colors);
try writer.writeIntLittle(u8, self.reserved);
try writer.writeIntLittle(u16, icon_data.color_planes);
try writer.writeIntLittle(u16, icon_data.bits_per_pixel);
try writer.writeIntLittle(u32, self.data_size_in_bytes);
try writer.writeInt(u8, @as(u8, @truncate(self.width)), .little);
try writer.writeInt(u8, @as(u8, @truncate(self.height)), .little);
try writer.writeInt(u8, self.num_colors, .little);
try writer.writeInt(u8, self.reserved, .little);
try writer.writeInt(u16, icon_data.color_planes, .little);
try writer.writeInt(u16, icon_data.bits_per_pixel, .little);
try writer.writeInt(u32, self.data_size_in_bytes, .little);
},
.cursor => |cursor_data| {
try writer.writeIntLittle(u16, self.width);
try writer.writeIntLittle(u16, self.height);
try writer.writeIntLittle(u16, cursor_data.hotspot_x);
try writer.writeIntLittle(u16, cursor_data.hotspot_y);
try writer.writeIntLittle(u32, self.data_size_in_bytes + 4);
try writer.writeInt(u16, self.width, .little);
try writer.writeInt(u16, self.height, .little);
try writer.writeInt(u16, cursor_data.hotspot_x, .little);
try writer.writeInt(u16, cursor_data.hotspot_y, .little);
try writer.writeInt(u32, self.data_size_in_bytes + 4, .little);
},
}
try writer.writeIntLittle(u16, id);
try writer.writeInt(u16, id, .little);
}
};

Expand Down Expand Up @@ -235,21 +237,21 @@ pub const ImageFormat = enum {
png,
riff,

const riff_header = std.mem.readIntNative(u32, "RIFF");
const png_signature = std.mem.readIntNative(u64, "\x89PNG\r\n\x1a\n");
const ihdr_code = std.mem.readIntNative(u32, "IHDR");
const acon_form_type = std.mem.readIntNative(u32, "ACON");
const riff_header = std.mem.readInt(u32, "RIFF", native_endian);
const png_signature = std.mem.readInt(u64, "\x89PNG\r\n\x1a\n", native_endian);
const ihdr_code = std.mem.readInt(u32, "IHDR", native_endian);
const acon_form_type = std.mem.readInt(u32, "ACON", native_endian);

pub fn detect(header_bytes: *const [16]u8) ImageFormat {
if (std.mem.readIntNative(u32, header_bytes[0..4]) == riff_header) return .riff;
if (std.mem.readIntNative(u64, header_bytes[0..8]) == png_signature) return .png;
if (std.mem.readInt(u32, header_bytes[0..4], native_endian) == riff_header) return .riff;
if (std.mem.readInt(u64, header_bytes[0..8], native_endian) == png_signature) return .png;
return .dib;
}

pub fn validate(format: ImageFormat, header_bytes: *const [16]u8) bool {
return switch (format) {
.png => std.mem.readIntNative(u32, header_bytes[12..16]) == ihdr_code,
.riff => std.mem.readIntNative(u32, header_bytes[8..12]) == acon_form_type,
.png => std.mem.readInt(u32, header_bytes[12..16], native_endian) == ihdr_code,
.riff => std.mem.readInt(u32, header_bytes[8..12], native_endian) == acon_form_type,
.dib => true,
};
}
Expand Down
32 changes: 16 additions & 16 deletions src/res.zig
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ pub const NameOrOrdinal = union(enum) {
try writer.writeAll(std.mem.sliceAsBytes(name[0 .. name.len + 1]));
},
.ordinal => |ordinal| {
try writer.writeIntLittle(u16, 0xffff);
try writer.writeIntLittle(u16, ordinal);
try writer.writeInt(u16, 0xffff, .little);
try writer.writeInt(u16, ordinal, .little);
},
}
}

pub fn writeEmpty(writer: anytype) !void {
try writer.writeIntLittle(u16, 0);
try writer.writeInt(u16, 0, .little);
}

pub fn fromString(allocator: Allocator, bytes: SourceBytes) !NameOrOrdinal {
Expand Down Expand Up @@ -963,19 +963,19 @@ pub const FixedFileInfo = struct {
};

pub fn write(self: FixedFileInfo, writer: anytype) !void {
try writer.writeIntLittle(u32, signature);
try writer.writeIntLittle(u32, version);
try writer.writeIntLittle(u32, self.file_version.mostSignificantCombinedParts());
try writer.writeIntLittle(u32, self.file_version.leastSignificantCombinedParts());
try writer.writeIntLittle(u32, self.product_version.mostSignificantCombinedParts());
try writer.writeIntLittle(u32, self.product_version.leastSignificantCombinedParts());
try writer.writeIntLittle(u32, self.file_flags_mask);
try writer.writeIntLittle(u32, self.file_flags);
try writer.writeIntLittle(u32, self.file_os);
try writer.writeIntLittle(u32, self.file_type);
try writer.writeIntLittle(u32, self.file_subtype);
try writer.writeIntLittle(u32, self.file_date.mostSignificantCombinedParts());
try writer.writeIntLittle(u32, self.file_date.leastSignificantCombinedParts());
try writer.writeInt(u32, signature, .little);
try writer.writeInt(u32, version, .little);
try writer.writeInt(u32, self.file_version.mostSignificantCombinedParts(), .little);
try writer.writeInt(u32, self.file_version.leastSignificantCombinedParts(), .little);
try writer.writeInt(u32, self.product_version.mostSignificantCombinedParts(), .little);
try writer.writeInt(u32, self.product_version.leastSignificantCombinedParts(), .little);
try writer.writeInt(u32, self.file_flags_mask, .little);
try writer.writeInt(u32, self.file_flags, .little);
try writer.writeInt(u32, self.file_os, .little);
try writer.writeInt(u32, self.file_type, .little);
try writer.writeInt(u32, self.file_subtype, .little);
try writer.writeInt(u32, self.file_date.mostSignificantCombinedParts(), .little);
try writer.writeInt(u32, self.file_date.leastSignificantCombinedParts(), .little);
}
};

Expand Down
Loading

0 comments on commit 05b320f

Please sign in to comment.