Commit d0da3d731e

Tristan Ross <tristan.ross@midstall.com>
2024-01-22 06:38:08
std.io: replace readStructBig with readStructEndian
1 parent b0c8a3f
Changed files (4)
lib
std
crypto
Certificate
Bundle
io
src
link
MachO
lib/std/crypto/Certificate/Bundle/macos.zig
@@ -20,12 +20,12 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void {
     var stream = std.io.fixedBufferStream(bytes);
     const reader = stream.reader();
 
-    const db_header = try reader.readStructBig(ApplDbHeader);
+    const db_header = try reader.readStructEndian(ApplDbHeader, .big);
     assert(mem.eql(u8, "kych", &@as([4]u8, @bitCast(db_header.signature))));
 
     try stream.seekTo(db_header.schema_offset);
 
-    const db_schema = try reader.readStructBig(ApplDbSchema);
+    const db_schema = try reader.readStructEndian(ApplDbSchema, .big);
 
     var table_list = try gpa.alloc(u32, db_schema.table_count);
     defer gpa.free(table_list);
@@ -40,7 +40,7 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void {
     for (table_list) |table_offset| {
         try stream.seekTo(db_header.schema_offset + table_offset);
 
-        const table_header = try reader.readStructBig(TableHeader);
+        const table_header = try reader.readStructEndian(TableHeader, .big);
 
         if (@as(std.os.darwin.cssm.DB_RECORDTYPE, @enumFromInt(table_header.table_id)) != .X509_CERTIFICATE) {
             continue;
@@ -57,7 +57,7 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void {
         for (record_list) |record_offset| {
             try stream.seekTo(db_header.schema_offset + table_offset + record_offset);
 
-            const cert_header = try reader.readStructBig(X509CertHeader);
+            const cert_header = try reader.readStructEndian(X509CertHeader, .big);
 
             try cb.bytes.ensureUnusedCapacity(gpa, cert_header.cert_size);
 
lib/std/io/Reader.zig
@@ -332,9 +332,9 @@ pub fn readStruct(self: Self, comptime T: type) anyerror!T {
     return res[0];
 }
 
-pub fn readStructBig(self: Self, comptime T: type) anyerror!T {
+pub fn readStructEndian(self: Self, comptime T: type, endian: std.builtin.Endian) anyerror!T {
     var res = try self.readStruct(T);
-    if (native_endian != std.builtin.Endian.big) {
+    if (native_endian != endian) {
         mem.byteSwapAllFields(T, &res);
     }
     return res;
lib/std/io.zig
@@ -300,8 +300,8 @@ pub fn GenericReader(
             return @errorCast(self.any().readStruct(T));
         }
 
-        pub inline fn readStructBig(self: Self, comptime T: type) NoEofError!T {
-            return @errorCast(self.any().readStructBig(T));
+        pub inline fn readStructEndian(self: Self, comptime T: type, endian: std.builtin.Endian) NoEofError!T {
+            return @errorCast(self.any().readStructEndian(T, endian));
         }
 
         pub const ReadEnumError = NoEofError || error{
src/link/MachO/fat.zig
@@ -1,6 +1,6 @@
 pub fn isFatLibrary(file: std.fs.File) bool {
     const reader = file.reader();
-    const hdr = reader.readStructBig(macho.fat_header) catch return false;
+    const hdr = reader.readStructEndian(macho.fat_header, .big) catch return false;
     defer file.seekTo(0) catch {};
     return hdr.magic == macho.FAT_MAGIC;
 }
@@ -13,7 +13,7 @@ pub const Arch = struct {
 /// Caller owns the memory.
 pub fn parseArchs(gpa: Allocator, file: std.fs.File) ![]const Arch {
     const reader = file.reader();
-    const fat_header = try reader.readStructBig(macho.fat_header);
+    const fat_header = try reader.readStructEndian(macho.fat_header, .big);
     assert(fat_header.magic == macho.FAT_MAGIC);
 
     var archs = try std.ArrayList(Arch).initCapacity(gpa, fat_header.nfat_arch);
@@ -21,7 +21,7 @@ pub fn parseArchs(gpa: Allocator, file: std.fs.File) ![]const Arch {
 
     var fat_arch_index: u32 = 0;
     while (fat_arch_index < fat_header.nfat_arch) : (fat_arch_index += 1) {
-        const fat_arch = try reader.readStructBig(macho.fat_arch);
+        const fat_arch = try reader.readStructEndian(macho.fat_arch, .big);
         // If we come across an architecture that we do not know how to handle, that's
         // fine because we can keep looking for one that might match.
         const arch: std.Target.Cpu.Arch = switch (fat_arch.cputype) {