authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2023-09-27 14:00:31-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2023-09-28 02:25:52-07:00
loga362d3963c879af47661f54b0a698729e142619f
treecc4580245fd8f153482c50c42148833975e4f051
parent937138cb90ae9327b0f7a932910c8d6080984f5c

resinator: Update to latest, fix for big endian arch


5 files changed, 47 insertions(+), 34 deletions(-)

src/resinator/bmp.zig+1-1
......@@ -92,7 +92,7 @@ pub fn read(reader: anytype, max_size: u64) ReadError!BitmapInfo {
9292 const id = std.mem.readIntNative(u16, file_header[0..2]);
9393 if (id != windows_format_id) return error.InvalidFileHeader;
9494
95 bitmap_info.pixel_data_offset = std.mem.readIntNative(u32, file_header[10..14]);
95 bitmap_info.pixel_data_offset = std.mem.readIntLittle(u32, file_header[10..14]);
9696 if (bitmap_info.pixel_data_offset > max_size) return error.ImpossiblePixelDataOffset;
9797
9898 bitmap_info.dib_header_size = reader.readIntLittle(u32) catch return error.UnexpectedEOF;
src/resinator/compile.zig+18-16
......@@ -571,7 +571,7 @@ pub const Compiler = struct {
571571 }
572572
573573 try file.seekTo(entry.data_offset_from_start_of_file);
574 const header_bytes = file.reader().readBytesNoEof(16) catch {
574 var header_bytes = file.reader().readBytesNoEof(16) catch {
575575 return self.iconReadError(
576576 error.UnexpectedEOF,
577577 filename_utf8,
......@@ -647,8 +647,11 @@ pub const Compiler = struct {
647647 },
648648 },
649649 .dib => {
650 const bitmap_header: *const ico.BitmapHeader = @ptrCast(@alignCast(&header_bytes));
651 const bitmap_version = ico.BitmapHeader.Version.get(std.mem.littleToNative(u32, bitmap_header.bcSize));
650 var bitmap_header: *ico.BitmapHeader = @ptrCast(@alignCast(&header_bytes));
651 if (builtin.cpu.arch.endian() == .Big) {
652 std.mem.byteSwapAllFields(ico.BitmapHeader, bitmap_header);
653 }
654 const bitmap_version = ico.BitmapHeader.Version.get(bitmap_header.bcSize);
652655
653656 // The Win32 RC compiler only allows headers with
654657 // `bcSize == sizeof(BITMAPINFOHEADER)`, but it seems unlikely
......@@ -684,15 +687,15 @@ pub const Compiler = struct {
684687 .icon => {
685688 // The values in the icon's BITMAPINFOHEADER always take precedence over
686689 // the values in the IconDir, but not in the LOCALHEADER (see above).
687 entry.type_specific_data.icon.color_planes = std.mem.littleToNative(u16, bitmap_header.bcPlanes);
688 entry.type_specific_data.icon.bits_per_pixel = std.mem.littleToNative(u16, bitmap_header.bcBitCount);
690 entry.type_specific_data.icon.color_planes = bitmap_header.bcPlanes;
691 entry.type_specific_data.icon.bits_per_pixel = bitmap_header.bcBitCount;
689692 },
690693 .cursor => {
691694 // Only cursors get the width/height from BITMAPINFOHEADER (icons don't)
692695 entry.width = @intCast(bitmap_header.bcWidth);
693696 entry.height = @intCast(bitmap_header.bcHeight);
694 entry.type_specific_data.cursor.hotspot_x = std.mem.littleToNative(u16, bitmap_header.bcPlanes);
695 entry.type_specific_data.cursor.hotspot_y = std.mem.littleToNative(u16, bitmap_header.bcBitCount);
697 entry.type_specific_data.cursor.hotspot_x = bitmap_header.bcPlanes;
698 entry.type_specific_data.cursor.hotspot_y = bitmap_header.bcBitCount;
696699 },
697700 }
698701 },
......@@ -826,9 +829,11 @@ pub const Compiler = struct {
826829 }
827830 if (bitmap_info.getExpectedPaletteByteLen() > 0) {
828831 try writeResourceDataNoPadding(writer, file_reader, @intCast(bitmap_info.getActualPaletteByteLen()));
829 const padding_bytes = bitmap_info.getMissingPaletteByteLen();
832 // We know that the number of missing palette bytes is <= 4096
833 // (see `bmp_too_many_missing_palette_bytes` error case above)
834 const padding_bytes: usize = @intCast(bitmap_info.getMissingPaletteByteLen());
830835 if (padding_bytes > 0) {
831 try writer.writeByteNTimes(0, @intCast(padding_bytes));
836 try writer.writeByteNTimes(0, padding_bytes);
832837 }
833838 }
834839 try file.seekTo(bitmap_info.pixel_data_offset);
......@@ -2855,7 +2860,7 @@ pub const SearchDir = struct {
28552860pub fn HeaderSlurpingReader(comptime size: usize, comptime ReaderType: anytype) type {
28562861 return struct {
28572862 child_reader: ReaderType,
2858 bytes_read: u64 = 0,
2863 bytes_read: usize = 0,
28592864 slurped_header: [size]u8 = [_]u8{0x00} ** size,
28602865
28612866 pub const Error = ReaderType.Error;
......@@ -2866,10 +2871,9 @@ pub fn HeaderSlurpingReader(comptime size: usize, comptime ReaderType: anytype)
28662871 if (self.bytes_read < size) {
28672872 const bytes_to_add = @min(amt, size - self.bytes_read);
28682873 const end_index = self.bytes_read + bytes_to_add;
2869 const dest = self.slurped_header[@intCast(self.bytes_read)..@intCast(end_index)];
2870 std.mem.copy(u8, dest, buf[0..bytes_to_add]);
2874 std.mem.copy(u8, self.slurped_header[self.bytes_read..end_index], buf[0..bytes_to_add]);
28712875 }
2872 self.bytes_read += amt;
2876 self.bytes_read +|= amt;
28732877 return amt;
28742878 }
28752879
......@@ -3196,9 +3200,7 @@ pub const StringTable = struct {
31963200 // We already trimmed any trailing NULs, so we know it will be a new addition to the string.
31973201 if (compiler.null_terminate_string_table_strings) string_len_in_utf16_code_units += 1;
31983202 try data_writer.writeIntLittle(u16, string_len_in_utf16_code_units);
3199 for (trimmed_string) |wc| {
3200 try data_writer.writeIntLittle(u16, wc);
3201 }
3203 try data_writer.writeAll(std.mem.sliceAsBytes(trimmed_string));
32023204 if (compiler.null_terminate_string_table_strings) {
32033205 try data_writer.writeIntLittle(u16, 0);
32043206 }
src/resinator/errors.zig+14-2
......@@ -7,6 +7,7 @@ const res = @import("res.zig");
77const ico = @import("ico.zig");
88const bmp = @import("bmp.zig");
99const parse = @import("parse.zig");
10const lang = @import("lang.zig");
1011const CodePage = @import("code_pages.zig").CodePage;
1112
1213pub const Diagnostics = struct {
......@@ -558,8 +559,19 @@ pub const ErrorDetails = struct {
558559 .hint => return,
559560 },
560561 .string_already_defined => switch (self.type) {
561 // TODO: better printing of language, using constant names from WinNT.h
562 .err, .warning => return writer.print("string with id {d} (0x{X}) already defined for language {d},{d}", .{ self.extra.string_and_language.id, self.extra.string_and_language.id, self.extra.string_and_language.language.primary_language_id, self.extra.string_and_language.language.sublanguage_id }),
562 .err, .warning => {
563 const language_id = self.extra.string_and_language.language.asInt();
564 const language_name = language_name: {
565 if (std.meta.intToEnum(lang.LanguageId, language_id)) |lang_enum_val| {
566 break :language_name @tagName(lang_enum_val);
567 } else |_| {}
568 if (language_id == lang.LOCALE_CUSTOM_UNSPECIFIED) {
569 break :language_name "LOCALE_CUSTOM_UNSPECIFIED";
570 }
571 break :language_name "<UNKNOWN>";
572 };
573 return writer.print("string with id {d} (0x{X}) already defined for language {s} (0x{X})", .{ self.extra.string_and_language.id, self.extra.string_and_language.id, language_name, language_id });
574 },
563575 .note => return writer.print("previous definition of string with id {d} (0x{X}) here", .{ self.extra.string_and_language.id, self.extra.string_and_language.id }),
564576 .hint => return,
565577 },
src/resinator/literals.zig+7-7
......@@ -395,7 +395,7 @@ pub fn parseQuotedString(
395395 while (try iterative_parser.next()) |parsed| {
396396 const c = parsed.codepoint;
397397 if (parsed.from_escaped_integer) {
398 try buf.append(@intCast(c));
398 try buf.append(std.mem.nativeToLittle(T, @intCast(c)));
399399 } else {
400400 switch (literal_type) {
401401 .ascii => switch (options.output_code_page) {
......@@ -658,7 +658,7 @@ test "parse quoted wide string" {
658658 defer arena_allocator.deinit();
659659 const arena = arena_allocator.allocator();
660660
661 try std.testing.expectEqualSentinel(u16, 0, &[_:0]u16{ 'h', 'e', 'l', 'l', 'o' }, try parseQuotedWideString(arena, .{
661 try std.testing.expectEqualSentinel(u16, 0, std.unicode.utf8ToUtf16LeStringLiteral("hello"), try parseQuotedWideString(arena, .{
662662 .slice =
663663 \\L"hello"
664664 ,
......@@ -672,21 +672,21 @@ test "parse quoted wide string" {
672672 .code_page = .windows1252,
673673 }, .{}));
674674 // hex max of 4 digits
675 try std.testing.expectEqualSentinel(u16, 0, &[_:0]u16{ 0xFFFF, 'f' }, try parseQuotedWideString(arena, .{
675 try std.testing.expectEqualSentinel(u16, 0, &[_:0]u16{ std.mem.nativeToLittle(u16, 0xFFFF), std.mem.nativeToLittle(u16, 'f') }, try parseQuotedWideString(arena, .{
676676 .slice =
677677 \\L"\XfFfFf"
678678 ,
679679 .code_page = .windows1252,
680680 }, .{}));
681681 // octal max of 7 digits
682 try std.testing.expectEqualSentinel(u16, 0, &[_:0]u16{ 0x9493, '3', '3' }, try parseQuotedWideString(arena, .{
682 try std.testing.expectEqualSentinel(u16, 0, &[_:0]u16{ std.mem.nativeToLittle(u16, 0x9493), std.mem.nativeToLittle(u16, '3'), std.mem.nativeToLittle(u16, '3') }, try parseQuotedWideString(arena, .{
683683 .slice =
684684 \\L"\111222333"
685685 ,
686686 .code_page = .windows1252,
687687 }, .{}));
688688 // octal overflow
689 try std.testing.expectEqualSentinel(u16, 0, &[_:0]u16{0xFF01}, try parseQuotedWideString(arena, .{
689 try std.testing.expectEqualSentinel(u16, 0, &[_:0]u16{std.mem.nativeToLittle(u16, 0xFF01)}, try parseQuotedWideString(arena, .{
690690 .slice =
691691 \\L"\777401"
692692 ,
......@@ -757,12 +757,12 @@ test "parse quoted ascii string as wide string" {
757757 .{},
758758 ));
759759 // Maximum escape sequence value is also determined by the L prefix
760 try std.testing.expectEqualSentinel(u16, 0, std.unicode.utf8ToUtf16LeStringLiteral("\x1234"), try parseQuotedStringAsWideString(
760 try std.testing.expectEqualSentinel(u16, 0, &[_:0]u16{ std.mem.nativeToLittle(u16, 0x12), std.mem.nativeToLittle(u16, '3'), std.mem.nativeToLittle(u16, '4') }, try parseQuotedStringAsWideString(
761761 arena,
762762 .{ .slice = "\"\\x1234\"", .code_page = .windows1252 },
763763 .{},
764764 ));
765 try std.testing.expectEqualSentinel(u16, 0, &[_:0]u16{0x1234}, try parseQuotedStringAsWideString(
765 try std.testing.expectEqualSentinel(u16, 0, &[_:0]u16{std.mem.nativeToLittle(u16, 0x1234)}, try parseQuotedStringAsWideString(
766766 arena,
767767 .{ .slice = "L\"\\x1234\"", .code_page = .windows1252 },
768768 .{},
src/resinator/res.zig+7-8
......@@ -218,6 +218,7 @@ pub const ControlClass = enum(u16) {
218218};
219219
220220pub const NameOrOrdinal = union(enum) {
221 // UTF-16 LE
221222 name: [:0]const u16,
222223 ordinal: u16,
223224
......@@ -245,9 +246,7 @@ pub const NameOrOrdinal = union(enum) {
245246 pub fn write(self: NameOrOrdinal, writer: anytype) !void {
246247 switch (self) {
247248 .name => |name| {
248 for (name[0 .. name.len + 1]) |code_unit| {
249 try writer.writeIntLittle(u16, code_unit);
250 }
249 try writer.writeAll(std.mem.sliceAsBytes(name[0 .. name.len + 1]));
251250 },
252251 .ordinal => |ordinal| {
253252 try writer.writeIntLittle(u16, 0xffff);
......@@ -281,7 +280,7 @@ pub const NameOrOrdinal = union(enum) {
281280 try buf.append(std.mem.nativeToLittle(u16, '�'));
282281 } else if (c < 0x7F) {
283282 // ASCII chars in names are always converted to uppercase
284 try buf.append(std.ascii.toUpper(@intCast(c)));
283 try buf.append(std.mem.nativeToLittle(u16, std.ascii.toUpper(@intCast(c))));
285284 } else if (c < 0x10000) {
286285 const short: u16 = @intCast(c);
287286 try buf.append(std.mem.nativeToLittle(u16, short));
......@@ -518,10 +517,10 @@ test "NameOrOrdinal" {
518517 var expected_u8_bytes = "00614982008907933748980730280674788429543776231864944218790698304852300002973622122844631429099469274282385299397783838528QFFL7SHNSIETG0QKLR1UYPBTUV1PMFQRRA0VJDG354GQEDJMUPGPP1W1EXVNTZVEIZ6K3IPQM1AWGEYALMEODYVEZGOD3MFMGEY8FNR4JUETTB1PZDEWSNDRGZUA8SNXP3NGO";
519518 var buf: [256:0]u16 = undefined;
520519 for (expected_u8_bytes, 0..) |byte, i| {
521 buf[i] = byte;
520 buf[i] = std.mem.nativeToLittle(u16, byte);
522521 }
523522 // surrogate pair that is now orphaned
524 buf[255] = 0xD801;
523 buf[255] = std.mem.nativeToLittle(u16, 0xD801);
525524 break :blk buf;
526525 };
527526 try expectNameOrOrdinal(
......@@ -908,7 +907,7 @@ pub const ForcedOrdinal = struct {
908907 var result: u16 = 0;
909908 for (utf16) |code_unit| {
910909 if (result != 0) result *%= 10;
911 result +%= code_unit -% '0';
910 result +%= std.mem.littleToNative(u16, code_unit) -% '0';
912911 }
913912 return result;
914913 }
......@@ -929,7 +928,7 @@ test "forced ordinal" {
929928 try std.testing.expectEqual(@as(u16, 0x4AF0), ForcedOrdinal.fromBytes(.{ .slice = "0\u{10100}", .code_page = .utf8 }));
930929
931930 // From UTF-16
932 try std.testing.expectEqual(@as(u16, 0x122), ForcedOrdinal.fromUtf16Le(&[_:0]u16{ '0', 'Œ' }));
931 try std.testing.expectEqual(@as(u16, 0x122), ForcedOrdinal.fromUtf16Le(&[_:0]u16{ std.mem.nativeToLittle(u16, '0'), std.mem.nativeToLittle(u16, 'Œ') }));
933932 try std.testing.expectEqual(@as(u16, 0x4AF0), ForcedOrdinal.fromUtf16Le(std.unicode.utf8ToUtf16LeStringLiteral("0\u{10100}")));
934933}
935934