From bf9978a57a9d939aefb3babcca28f87cb34331b0 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 31 Oct 2024 16:46:08 -0700 Subject: [PATCH] link.File.Wasm: conform to naming conventions --- src/link/Wasm/Archive.zig | 69 ++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/src/link/Wasm/Archive.zig b/src/link/Wasm/Archive.zig index 36741b9960..6aad90a9f4 100644 --- a/src/link/Wasm/Archive.zig +++ b/src/link/Wasm/Archive.zig @@ -12,65 +12,54 @@ toc: Toc, const Toc = std.StringArrayHashMapUnmanaged(std.ArrayListUnmanaged(u32)); // Archive files start with the ARMAG identifying string. Then follows a -// `struct ar_hdr', and as many bytes of member file data as its `ar_size' +// `struct Header', and as many bytes of member file data as its `size' // member indicates, for each member file. /// String that begins an archive file. const ARMAG: *const [SARMAG:0]u8 = "!\n"; /// Size of that string. const SARMAG: u4 = 8; -/// String in ar_fmag at the end of each header. +/// String in fmag at the end of each header. const ARFMAG: *const [2:0]u8 = "`\n"; -const ar_hdr = extern struct { +const Header = extern struct { /// Member file name, sometimes / terminated. - ar_name: [16]u8, - + name: [16]u8, /// File date, decimal seconds since Epoch. - ar_date: [12]u8, - + date: [12]u8, /// User ID, in ASCII format. - ar_uid: [6]u8, - + uid: [6]u8, /// Group ID, in ASCII format. - ar_gid: [6]u8, - + gid: [6]u8, /// File mode, in ASCII octal. - ar_mode: [8]u8, - + mode: [8]u8, /// File size, in ASCII decimal. - ar_size: [10]u8, - + size: [10]u8, /// Always contains ARFMAG. - ar_fmag: [2]u8, + fmag: [2]u8, const NameOrIndex = union(enum) { name: []const u8, index: u32, }; - fn nameOrIndex(archive: ar_hdr) !NameOrIndex { - const value = getValue(&archive.ar_name); + fn nameOrIndex(archive: Header) !NameOrIndex { + const value = getValue(&archive.name); const slash_index = mem.indexOfScalar(u8, value, '/') orelse return error.MalformedArchive; const len = value.len; if (slash_index == len - 1) { // Name stored directly - return NameOrIndex{ .name = value }; + return .{ .name = value }; } else { // Name follows the header directly and its length is encoded in // the name field. const index = try std.fmt.parseInt(u32, value[slash_index + 1 ..], 10); - return NameOrIndex{ .index = index }; + return .{ .index = index }; } } - fn date(archive: ar_hdr) !u64 { - const value = getValue(&archive.ar_date); - return std.fmt.parseInt(u64, value, 10); - } - - fn size(archive: ar_hdr) !u32 { - const value = getValue(&archive.ar_size); + fn parsedSize(archive: Header) !u32 { + const value = getValue(&archive.size); return std.fmt.parseInt(u32, value, 10); } @@ -98,8 +87,8 @@ pub fn parse(gpa: Allocator, file_contents: []const u8) !Archive { const magic = try reader.readBytesNoEof(SARMAG); if (!mem.eql(u8, &magic, ARMAG)) return error.BadArchiveMagic; - const header = try reader.readStruct(ar_hdr); - if (!mem.eql(u8, &header.ar_fmag, ARFMAG)) return error.BadHeaderDelimiter; + const header = try reader.readStruct(Header); + if (!mem.eql(u8, &header.fmag, ARFMAG)) return error.BadHeaderDelimiter; var toc = try parseTableOfContents(gpa, header, reader); errdefer deinitToc(gpa, &toc); @@ -113,7 +102,7 @@ pub fn parse(gpa: Allocator, file_contents: []const u8) !Archive { }; } -fn parseName(archive: *const Archive, header: ar_hdr) ![]const u8 { +fn parseName(archive: *const Archive, header: Header) ![]const u8 { const name_or_index = try header.nameOrIndex(); switch (name_or_index) { .name => |name| return name, @@ -124,10 +113,10 @@ fn parseName(archive: *const Archive, header: ar_hdr) ![]const u8 { } } -fn parseTableOfContents(gpa: Allocator, header: ar_hdr, reader: anytype) !Toc { +fn parseTableOfContents(gpa: Allocator, header: Header, reader: anytype) !Toc { // size field can have extra spaces padded in front as well as the end, // so we trim those first before parsing the ASCII value. - const size_trimmed = mem.trim(u8, &header.ar_size, " "); + const size_trimmed = mem.trim(u8, &header.size, " "); const sym_tab_size = try std.fmt.parseInt(u32, size_trimmed, 10); const num_symbols = try reader.readInt(u32, .big); @@ -167,14 +156,14 @@ fn parseTableOfContents(gpa: Allocator, header: ar_hdr, reader: anytype) !Toc { } fn parseNameTable(gpa: Allocator, reader: anytype) ![]const u8 { - const header: ar_hdr = try reader.readStruct(ar_hdr); - if (!mem.eql(u8, &header.ar_fmag, ARFMAG)) { + const header: Header = try reader.readStruct(Header); + if (!mem.eql(u8, &header.fmag, ARFMAG)) { return error.InvalidHeaderDelimiter; } - if (!mem.eql(u8, header.ar_name[0..2], "//")) { + if (!mem.eql(u8, header.name[0..2], "//")) { return error.MissingTableName; } - const table_size = try header.size(); + const table_size = try header.parsedSize(); const long_file_names = try gpa.alloc(u8, table_size); errdefer gpa.free(long_file_names); try reader.readNoEof(long_file_names); @@ -186,14 +175,14 @@ fn parseNameTable(gpa: Allocator, reader: anytype) ![]const u8 { /// When found, parses the object file into an `Object` and returns it. pub fn parseObject(archive: Archive, wasm: *const Wasm, file_contents: []const u8, path: Path) !Object { var fbs = std.io.fixedBufferStream(file_contents); - const header = try fbs.reader().readStruct(ar_hdr); + const header = try fbs.reader().readStruct(Header); - if (!mem.eql(u8, &header.ar_fmag, ARFMAG)) return error.BadArchiveHeaderDelimiter; + if (!mem.eql(u8, &header.fmag, ARFMAG)) return error.BadArchiveHeaderDelimiter; const object_name = try archive.parseName(header); - const object_file_size = try header.size(); + const object_file_size = try header.parsedSize(); - return Object.create(wasm, file_contents[@sizeOf(ar_hdr)..][0..object_file_size], path, object_name); + return Object.create(wasm, file_contents[@sizeOf(Header)..][0..object_file_size], path, object_name); } const std = @import("std");