| author | |
| committer | |
| log | 725f765c376dda291d3d5afe5446221a97c189f7 |
| tree | d2c1d77794ba13140aa4a3f0303d56bbec2bd27f |
| parent | 46062f1c13d8ee9eebf49806660c2271f2acc613 |
| parent | 13b1e10b8f3d8df92417999ed972e5f682c82a46 |
| signature |
mem: fix UB in `readInt`/`writeInt` and delete variants169 files changed, 1472 insertions(+), 1821 deletions(-)
deps/aro/Compilation.zig+1-1| ... | @@ -402,7 +402,7 @@ pub fn generateBuiltinMacros(comp: *Compilation) !Source { | ... | @@ -402,7 +402,7 @@ pub fn generateBuiltinMacros(comp: *Compilation) !Source { |
| 402 | \\#define __ORDER_PDP_ENDIAN__ 3412 | 402 | \\#define __ORDER_PDP_ENDIAN__ 3412 |
| 403 | \\ | 403 | \\ |
| 404 | ); | 404 | ); |
| 405 | if (comp.target.cpu.arch.endian() == .Little) try w.writeAll( | 405 | if (comp.target.cpu.arch.endian() == .little) try w.writeAll( |
| 406 | \\#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ | 406 | \\#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ |
| 407 | \\#define __LITTLE_ENDIAN__ 1 | 407 | \\#define __LITTLE_ENDIAN__ 1 |
| 408 | \\ | 408 | \\ |
deps/aro/target.zig+2-2| ... | @@ -398,8 +398,8 @@ pub fn ldEmulationOption(target: std.Target, arm_endianness: ?std.builtin.Endian | ... | @@ -398,8 +398,8 @@ pub fn ldEmulationOption(target: std.Target, arm_endianness: ?std.builtin.Endian |
| 398 | .thumb, | 398 | .thumb, |
| 399 | .thumbeb, | 399 | .thumbeb, |
| 400 | => switch (arm_endianness orelse target.cpu.arch.endian()) { | 400 | => switch (arm_endianness orelse target.cpu.arch.endian()) { |
| 401 | .Little => "armelf_linux_eabi", | 401 | .little => "armelf_linux_eabi", |
| 402 | .Big => "armelfb_linux_eabi", | 402 | .big => "armelfb_linux_eabi", |
| 403 | }, | 403 | }, |
| 404 | .aarch64 => "aarch64linux", | 404 | .aarch64 => "aarch64linux", |
| 405 | .aarch64_be => "aarch64linuxb", | 405 | .aarch64_be => "aarch64linuxb", |
doc/langref.html.in+2-2| ... | @@ -3376,11 +3376,11 @@ fn doTheTest() !void { | ... | @@ -3376,11 +3376,11 @@ fn doTheTest() !void { |
| 3376 | 3376 | ||
| 3377 | var ordered: [2]u8 = @bitCast(full); | 3377 | var ordered: [2]u8 = @bitCast(full); |
| 3378 | switch (native_endian) { | 3378 | switch (native_endian) { |
| 3379 | .Big => { | 3379 | .big => { |
| 3380 | try expect(ordered[0] == 0x12); | 3380 | try expect(ordered[0] == 0x12); |
| 3381 | try expect(ordered[1] == 0x34); | 3381 | try expect(ordered[1] == 0x34); |
| 3382 | }, | 3382 | }, |
| 3383 | .Little => { | 3383 | .little => { |
| 3384 | try expect(ordered[0] == 0x34); | 3384 | try expect(ordered[0] == 0x34); |
| 3385 | try expect(ordered[1] == 0x12); | 3385 | try expect(ordered[1] == 0x12); |
| 3386 | }, | 3386 | }, |
lib/compiler_rt/common.zig+1-1| ... | @@ -249,7 +249,7 @@ pub fn HalveInt(comptime T: type, comptime signed_half: bool) type { | ... | @@ -249,7 +249,7 @@ pub fn HalveInt(comptime T: type, comptime signed_half: bool) type { |
| 249 | pub const HalfT = if (signed_half) HalfTS else HalfTU; | 249 | pub const HalfT = if (signed_half) HalfTS else HalfTU; |
| 250 | 250 | ||
| 251 | all: T, | 251 | all: T, |
| 252 | s: if (native_endian == .Little) | 252 | s: if (native_endian == .little) |
| 253 | extern struct { low: HalfT, high: HalfT } | 253 | extern struct { low: HalfT, high: HalfT } |
| 254 | else | 254 | else |
| 255 | extern struct { high: HalfT, low: HalfT }, | 255 | extern struct { high: HalfT, low: HalfT }, |
lib/compiler_rt/fmod.zig+6-6| ... | @@ -142,16 +142,16 @@ pub fn fmodq(a: f128, b: f128) callconv(.C) f128 { | ... | @@ -142,16 +142,16 @@ pub fn fmodq(a: f128, b: f128) callconv(.C) f128 { |
| 142 | const bPtr_u16: [*]u16 = @ptrCast(&bmod); | 142 | const bPtr_u16: [*]u16 = @ptrCast(&bmod); |
| 143 | 143 | ||
| 144 | const exp_and_sign_index = comptime switch (builtin.target.cpu.arch.endian()) { | 144 | const exp_and_sign_index = comptime switch (builtin.target.cpu.arch.endian()) { |
| 145 | .Little => 7, | 145 | .little => 7, |
| 146 | .Big => 0, | 146 | .big => 0, |
| 147 | }; | 147 | }; |
| 148 | const low_index = comptime switch (builtin.target.cpu.arch.endian()) { | 148 | const low_index = comptime switch (builtin.target.cpu.arch.endian()) { |
| 149 | .Little => 0, | 149 | .little => 0, |
| 150 | .Big => 1, | 150 | .big => 1, |
| 151 | }; | 151 | }; |
| 152 | const high_index = comptime switch (builtin.target.cpu.arch.endian()) { | 152 | const high_index = comptime switch (builtin.target.cpu.arch.endian()) { |
| 153 | .Little => 1, | 153 | .little => 1, |
| 154 | .Big => 0, | 154 | .big => 0, |
| 155 | }; | 155 | }; |
| 156 | 156 | ||
| 157 | const signA = aPtr_u16[exp_and_sign_index] & 0x8000; | 157 | const signA = aPtr_u16[exp_and_sign_index] & 0x8000; |
lib/compiler_rt/udivmod.zig+2-2| ... | @@ -5,8 +5,8 @@ const Log2Int = std.math.Log2Int; | ... | @@ -5,8 +5,8 @@ const Log2Int = std.math.Log2Int; |
| 5 | const HalveInt = @import("common.zig").HalveInt; | 5 | const HalveInt = @import("common.zig").HalveInt; |
| 6 | 6 | ||
| 7 | const lo = switch (builtin.cpu.arch.endian()) { | 7 | const lo = switch (builtin.cpu.arch.endian()) { |
| 8 | .Big => 1, | 8 | .big => 1, |
| 9 | .Little => 0, | 9 | .little => 0, |
| 10 | }; | 10 | }; |
| 11 | const hi = 1 - lo; | 11 | const hi = 1 - lo; |
| 12 | 12 |
lib/compiler_rt/udivmodei4.zig+2-2| ... | @@ -15,12 +15,12 @@ const endian = builtin.cpu.arch.endian(); | ... | @@ -15,12 +15,12 @@ const endian = builtin.cpu.arch.endian(); |
| 15 | 15 | ||
| 16 | /// Get the value of a limb. | 16 | /// Get the value of a limb. |
| 17 | inline fn limb(x: []const u32, i: usize) u32 { | 17 | inline fn limb(x: []const u32, i: usize) u32 { |
| 18 | return if (endian == .Little) x[i] else x[x.len - 1 - i]; | 18 | return if (endian == .little) x[i] else x[x.len - 1 - i]; |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | /// Change the value of a limb. | 21 | /// Change the value of a limb. |
| 22 | inline fn limb_set(x: []u32, i: usize, v: u32) void { | 22 | inline fn limb_set(x: []u32, i: usize, v: u32) void { |
| 23 | if (endian == .Little) { | 23 | if (endian == .little) { |
| 24 | x[i] = v; | 24 | x[i] = v; |
| 25 | } else { | 25 | } else { |
| 26 | x[x.len - 1 - i] = v; | 26 | x[x.len - 1 - i] = v; |
lib/std/Build/Step/CheckObject.zig+2-2| ... | @@ -1624,8 +1624,8 @@ const WasmDumper = struct { | ... | @@ -1624,8 +1624,8 @@ const WasmDumper = struct { |
| 1624 | switch (opcode) { | 1624 | switch (opcode) { |
| 1625 | .i32_const => try writer.print("i32.const {x}\n", .{try std.leb.readILEB128(i32, reader)}), | 1625 | .i32_const => try writer.print("i32.const {x}\n", .{try std.leb.readILEB128(i32, reader)}), |
| 1626 | .i64_const => try writer.print("i64.const {x}\n", .{try std.leb.readILEB128(i64, reader)}), | 1626 | .i64_const => try writer.print("i64.const {x}\n", .{try std.leb.readILEB128(i64, reader)}), |
| 1627 | .f32_const => try writer.print("f32.const {x}\n", .{@as(f32, @bitCast(try reader.readIntLittle(u32)))}), | 1627 | .f32_const => try writer.print("f32.const {x}\n", .{@as(f32, @bitCast(try reader.readInt(u32, .little)))}), |
| 1628 | .f64_const => try writer.print("f64.const {x}\n", .{@as(f64, @bitCast(try reader.readIntLittle(u64)))}), | 1628 | .f64_const => try writer.print("f64.const {x}\n", .{@as(f64, @bitCast(try reader.readInt(u64, .little)))}), |
| 1629 | .global_get => try writer.print("global.get {x}\n", .{try std.leb.readULEB128(u32, reader)}), | 1629 | .global_get => try writer.print("global.get {x}\n", .{try std.leb.readULEB128(u32, reader)}), |
| 1630 | else => unreachable, | 1630 | else => unreachable, |
| 1631 | } | 1631 | } |
lib/std/base64.zig+4-4| ... | @@ -104,14 +104,14 @@ pub const Base64Encoder = struct { | ... | @@ -104,14 +104,14 @@ pub const Base64Encoder = struct { |
| 104 | var idx: usize = 0; | 104 | var idx: usize = 0; |
| 105 | var out_idx: usize = 0; | 105 | var out_idx: usize = 0; |
| 106 | while (idx + 15 < source.len) : (idx += 12) { | 106 | while (idx + 15 < source.len) : (idx += 12) { |
| 107 | const bits = std.mem.readIntBig(u128, source[idx..][0..16]); | 107 | const bits = std.mem.readInt(u128, source[idx..][0..16], .big); |
| 108 | inline for (0..16) |i| { | 108 | inline for (0..16) |i| { |
| 109 | dest[out_idx + i] = encoder.alphabet_chars[@truncate((bits >> (122 - i * 6)) & 0x3f)]; | 109 | dest[out_idx + i] = encoder.alphabet_chars[@truncate((bits >> (122 - i * 6)) & 0x3f)]; |
| 110 | } | 110 | } |
| 111 | out_idx += 16; | 111 | out_idx += 16; |
| 112 | } | 112 | } |
| 113 | while (idx + 3 < source.len) : (idx += 3) { | 113 | while (idx + 3 < source.len) : (idx += 3) { |
| 114 | const bits = std.mem.readIntBig(u32, source[idx..][0..4]); | 114 | const bits = std.mem.readInt(u32, source[idx..][0..4], .big); |
| 115 | dest[out_idx] = encoder.alphabet_chars[(bits >> 26) & 0x3f]; | 115 | dest[out_idx] = encoder.alphabet_chars[(bits >> 26) & 0x3f]; |
| 116 | dest[out_idx + 1] = encoder.alphabet_chars[(bits >> 20) & 0x3f]; | 116 | dest[out_idx + 1] = encoder.alphabet_chars[(bits >> 20) & 0x3f]; |
| 117 | dest[out_idx + 2] = encoder.alphabet_chars[(bits >> 14) & 0x3f]; | 117 | dest[out_idx + 2] = encoder.alphabet_chars[(bits >> 14) & 0x3f]; |
| ... | @@ -226,7 +226,7 @@ pub const Base64Decoder = struct { | ... | @@ -226,7 +226,7 @@ pub const Base64Decoder = struct { |
| 226 | if ((new_bits & invalid_char_tst) != 0) return error.InvalidCharacter; | 226 | if ((new_bits & invalid_char_tst) != 0) return error.InvalidCharacter; |
| 227 | bits |= (new_bits << (24 * i)); | 227 | bits |= (new_bits << (24 * i)); |
| 228 | } | 228 | } |
| 229 | std.mem.writeIntLittle(u128, dest[dest_idx..][0..16], bits); | 229 | std.mem.writeInt(u128, dest[dest_idx..][0..16], bits, .little); |
| 230 | } | 230 | } |
| 231 | while (fast_src_idx + 4 < source.len and dest_idx + 3 < dest.len) : ({ | 231 | while (fast_src_idx + 4 < source.len and dest_idx + 3 < dest.len) : ({ |
| 232 | fast_src_idx += 4; | 232 | fast_src_idx += 4; |
| ... | @@ -237,7 +237,7 @@ pub const Base64Decoder = struct { | ... | @@ -237,7 +237,7 @@ pub const Base64Decoder = struct { |
| 237 | bits |= decoder.fast_char_to_index[2][source[fast_src_idx + 2]]; | 237 | bits |= decoder.fast_char_to_index[2][source[fast_src_idx + 2]]; |
| 238 | bits |= decoder.fast_char_to_index[3][source[fast_src_idx + 3]]; | 238 | bits |= decoder.fast_char_to_index[3][source[fast_src_idx + 3]]; |
| 239 | if ((bits & invalid_char_tst) != 0) return error.InvalidCharacter; | 239 | if ((bits & invalid_char_tst) != 0) return error.InvalidCharacter; |
| 240 | std.mem.writeIntLittle(u32, dest[dest_idx..][0..4], bits); | 240 | std.mem.writeInt(u32, dest[dest_idx..][0..4], bits, .little); |
| 241 | } | 241 | } |
| 242 | var remaining = source[fast_src_idx..]; | 242 | var remaining = source[fast_src_idx..]; |
| 243 | for (remaining, fast_src_idx..) |c, src_idx| { | 243 | for (remaining, fast_src_idx..) |c, src_idx| { |
lib/std/builtin.zig+2-2| ... | @@ -450,8 +450,8 @@ pub const FloatMode = enum { | ... | @@ -450,8 +450,8 @@ pub const FloatMode = enum { |
| 450 | /// This data structure is used by the Zig language code generation and | 450 | /// This data structure is used by the Zig language code generation and |
| 451 | /// therefore must be kept in sync with the compiler implementation. | 451 | /// therefore must be kept in sync with the compiler implementation. |
| 452 | pub const Endian = enum { | 452 | pub const Endian = enum { |
| 453 | Big, | 453 | big, |
| 454 | Little, | 454 | little, |
| 455 | }; | 455 | }; |
| 456 | 456 | ||
| 457 | /// This data structure is used by the Zig language code generation and | 457 | /// This data structure is used by the Zig language code generation and |
lib/std/child_process.zig+2-2| ... | @@ -1376,7 +1376,7 @@ fn writeIntFd(fd: i32, value: ErrInt) !void { | ... | @@ -1376,7 +1376,7 @@ fn writeIntFd(fd: i32, value: ErrInt) !void { |
| 1376 | .capable_io_mode = .blocking, | 1376 | .capable_io_mode = .blocking, |
| 1377 | .intended_io_mode = .blocking, | 1377 | .intended_io_mode = .blocking, |
| 1378 | }; | 1378 | }; |
| 1379 | file.writer().writeIntNative(u64, @as(u64, @intCast(value))) catch return error.SystemResources; | 1379 | file.writer().writeInt(u64, @intCast(value), .little) catch return error.SystemResources; |
| 1380 | } | 1380 | } |
| 1381 | 1381 | ||
| 1382 | fn readIntFd(fd: i32) !ErrInt { | 1382 | fn readIntFd(fd: i32) !ErrInt { |
| ... | @@ -1385,7 +1385,7 @@ fn readIntFd(fd: i32) !ErrInt { | ... | @@ -1385,7 +1385,7 @@ fn readIntFd(fd: i32) !ErrInt { |
| 1385 | .capable_io_mode = .blocking, | 1385 | .capable_io_mode = .blocking, |
| 1386 | .intended_io_mode = .blocking, | 1386 | .intended_io_mode = .blocking, |
| 1387 | }; | 1387 | }; |
| 1388 | return @as(ErrInt, @intCast(file.reader().readIntNative(u64) catch return error.SystemResources)); | 1388 | return @as(ErrInt, @intCast(file.reader().readInt(u64, .little) catch return error.SystemResources)); |
| 1389 | } | 1389 | } |
| 1390 | 1390 | ||
| 1391 | /// Caller must free result. | 1391 | /// Caller must free result. |
lib/std/coff.zig+20-20| ... | @@ -663,7 +663,7 @@ pub const Symbol = struct { | ... | @@ -663,7 +663,7 @@ pub const Symbol = struct { |
| 663 | 663 | ||
| 664 | pub fn getNameOffset(self: Symbol) ?u32 { | 664 | pub fn getNameOffset(self: Symbol) ?u32 { |
| 665 | if (!std.mem.eql(u8, self.name[0..4], "\x00\x00\x00\x00")) return null; | 665 | if (!std.mem.eql(u8, self.name[0..4], "\x00\x00\x00\x00")) return null; |
| 666 | const offset = std.mem.readIntLittle(u32, self.name[4..8]); | 666 | const offset = std.mem.readInt(u32, self.name[4..8], .little); |
| 667 | return offset; | 667 | return offset; |
| 668 | } | 668 | } |
| 669 | }; | 669 | }; |
| ... | @@ -1075,7 +1075,7 @@ pub const Coff = struct { | ... | @@ -1075,7 +1075,7 @@ pub const Coff = struct { |
| 1075 | var stream = std.io.fixedBufferStream(data); | 1075 | var stream = std.io.fixedBufferStream(data); |
| 1076 | const reader = stream.reader(); | 1076 | const reader = stream.reader(); |
| 1077 | try stream.seekTo(pe_pointer_offset); | 1077 | try stream.seekTo(pe_pointer_offset); |
| 1078 | var coff_header_offset = try reader.readIntLittle(u32); | 1078 | var coff_header_offset = try reader.readInt(u32, .little); |
| 1079 | try stream.seekTo(coff_header_offset); | 1079 | try stream.seekTo(coff_header_offset); |
| 1080 | var buf: [4]u8 = undefined; | 1080 | var buf: [4]u8 = undefined; |
| 1081 | try reader.readNoEof(&buf); | 1081 | try reader.readNoEof(&buf); |
| ... | @@ -1142,7 +1142,7 @@ pub const Coff = struct { | ... | @@ -1142,7 +1142,7 @@ pub const Coff = struct { |
| 1142 | if (!mem.eql(u8, &cv_signature, "RSDS")) | 1142 | if (!mem.eql(u8, &cv_signature, "RSDS")) |
| 1143 | return error.InvalidPEMagic; | 1143 | return error.InvalidPEMagic; |
| 1144 | try reader.readNoEof(self.guid[0..]); | 1144 | try reader.readNoEof(self.guid[0..]); |
| 1145 | self.age = try reader.readIntLittle(u32); | 1145 | self.age = try reader.readInt(u32, .little); |
| 1146 | 1146 | ||
| 1147 | // Finally read the null-terminated string. | 1147 | // Finally read the null-terminated string. |
| 1148 | var byte = try reader.readByte(); | 1148 | var byte = try reader.readByte(); |
| ... | @@ -1223,7 +1223,7 @@ pub const Coff = struct { | ... | @@ -1223,7 +1223,7 @@ pub const Coff = struct { |
| 1223 | if (coff_header.pointer_to_symbol_table == 0) return null; | 1223 | if (coff_header.pointer_to_symbol_table == 0) return null; |
| 1224 | 1224 | ||
| 1225 | const offset = coff_header.pointer_to_symbol_table + Symbol.sizeOf() * coff_header.number_of_symbols; | 1225 | const offset = coff_header.pointer_to_symbol_table + Symbol.sizeOf() * coff_header.number_of_symbols; |
| 1226 | const size = mem.readIntLittle(u32, self.data[offset..][0..4]); | 1226 | const size = mem.readInt(u32, self.data[offset..][0..4], .little); |
| 1227 | if ((offset + size) > self.data.len) return error.InvalidStrtabSize; | 1227 | if ((offset + size) > self.data.len) return error.InvalidStrtabSize; |
| 1228 | 1228 | ||
| 1229 | return Strtab{ .buffer = self.data[offset..][0..size] }; | 1229 | return Strtab{ .buffer = self.data[offset..][0..size] }; |
| ... | @@ -1324,9 +1324,9 @@ pub const Symtab = struct { | ... | @@ -1324,9 +1324,9 @@ pub const Symtab = struct { |
| 1324 | fn asSymbol(raw: []const u8) Symbol { | 1324 | fn asSymbol(raw: []const u8) Symbol { |
| 1325 | return .{ | 1325 | return .{ |
| 1326 | .name = raw[0..8].*, | 1326 | .name = raw[0..8].*, |
| 1327 | .value = mem.readIntLittle(u32, raw[8..12]), | 1327 | .value = mem.readInt(u32, raw[8..12], .little), |
| 1328 | .section_number = @as(SectionNumber, @enumFromInt(mem.readIntLittle(u16, raw[12..14]))), | 1328 | .section_number = @as(SectionNumber, @enumFromInt(mem.readInt(u16, raw[12..14], .little))), |
| 1329 | .type = @as(SymType, @bitCast(mem.readIntLittle(u16, raw[14..16]))), | 1329 | .type = @as(SymType, @bitCast(mem.readInt(u16, raw[14..16], .little))), |
| 1330 | .storage_class = @as(StorageClass, @enumFromInt(raw[16])), | 1330 | .storage_class = @as(StorageClass, @enumFromInt(raw[16])), |
| 1331 | .number_of_aux_symbols = raw[17], | 1331 | .number_of_aux_symbols = raw[17], |
| 1332 | }; | 1332 | }; |
| ... | @@ -1335,27 +1335,27 @@ pub const Symtab = struct { | ... | @@ -1335,27 +1335,27 @@ pub const Symtab = struct { |
| 1335 | fn asDebugInfo(raw: []const u8) DebugInfoDefinition { | 1335 | fn asDebugInfo(raw: []const u8) DebugInfoDefinition { |
| 1336 | return .{ | 1336 | return .{ |
| 1337 | .unused_1 = raw[0..4].*, | 1337 | .unused_1 = raw[0..4].*, |
| 1338 | .linenumber = mem.readIntLittle(u16, raw[4..6]), | 1338 | .linenumber = mem.readInt(u16, raw[4..6], .little), |
| 1339 | .unused_2 = raw[6..12].*, | 1339 | .unused_2 = raw[6..12].*, |
| 1340 | .pointer_to_next_function = mem.readIntLittle(u32, raw[12..16]), | 1340 | .pointer_to_next_function = mem.readInt(u32, raw[12..16], .little), |
| 1341 | .unused_3 = raw[16..18].*, | 1341 | .unused_3 = raw[16..18].*, |
| 1342 | }; | 1342 | }; |
| 1343 | } | 1343 | } |
| 1344 | 1344 | ||
| 1345 | fn asFuncDef(raw: []const u8) FunctionDefinition { | 1345 | fn asFuncDef(raw: []const u8) FunctionDefinition { |
| 1346 | return .{ | 1346 | return .{ |
| 1347 | .tag_index = mem.readIntLittle(u32, raw[0..4]), | 1347 | .tag_index = mem.readInt(u32, raw[0..4], .little), |
| 1348 | .total_size = mem.readIntLittle(u32, raw[4..8]), | 1348 | .total_size = mem.readInt(u32, raw[4..8], .little), |
| 1349 | .pointer_to_linenumber = mem.readIntLittle(u32, raw[8..12]), | 1349 | .pointer_to_linenumber = mem.readInt(u32, raw[8..12], .little), |
| 1350 | .pointer_to_next_function = mem.readIntLittle(u32, raw[12..16]), | 1350 | .pointer_to_next_function = mem.readInt(u32, raw[12..16], .little), |
| 1351 | .unused = raw[16..18].*, | 1351 | .unused = raw[16..18].*, |
| 1352 | }; | 1352 | }; |
| 1353 | } | 1353 | } |
| 1354 | 1354 | ||
| 1355 | fn asWeakExtDef(raw: []const u8) WeakExternalDefinition { | 1355 | fn asWeakExtDef(raw: []const u8) WeakExternalDefinition { |
| 1356 | return .{ | 1356 | return .{ |
| 1357 | .tag_index = mem.readIntLittle(u32, raw[0..4]), | 1357 | .tag_index = mem.readInt(u32, raw[0..4], .little), |
| 1358 | .flag = @as(WeakExternalFlag, @enumFromInt(mem.readIntLittle(u32, raw[4..8]))), | 1358 | .flag = @as(WeakExternalFlag, @enumFromInt(mem.readInt(u32, raw[4..8], .little))), |
| 1359 | .unused = raw[8..18].*, | 1359 | .unused = raw[8..18].*, |
| 1360 | }; | 1360 | }; |
| 1361 | } | 1361 | } |
| ... | @@ -1368,11 +1368,11 @@ pub const Symtab = struct { | ... | @@ -1368,11 +1368,11 @@ pub const Symtab = struct { |
| 1368 | 1368 | ||
| 1369 | fn asSectDef(raw: []const u8) SectionDefinition { | 1369 | fn asSectDef(raw: []const u8) SectionDefinition { |
| 1370 | return .{ | 1370 | return .{ |
| 1371 | .length = mem.readIntLittle(u32, raw[0..4]), | 1371 | .length = mem.readInt(u32, raw[0..4], .little), |
| 1372 | .number_of_relocations = mem.readIntLittle(u16, raw[4..6]), | 1372 | .number_of_relocations = mem.readInt(u16, raw[4..6], .little), |
| 1373 | .number_of_linenumbers = mem.readIntLittle(u16, raw[6..8]), | 1373 | .number_of_linenumbers = mem.readInt(u16, raw[6..8], .little), |
| 1374 | .checksum = mem.readIntLittle(u32, raw[8..12]), | 1374 | .checksum = mem.readInt(u32, raw[8..12], .little), |
| 1375 | .number = mem.readIntLittle(u16, raw[12..14]), | 1375 | .number = mem.readInt(u16, raw[12..14], .little), |
| 1376 | .selection = @as(ComdatSelection, @enumFromInt(raw[14])), | 1376 | .selection = @as(ComdatSelection, @enumFromInt(raw[14])), |
| 1377 | .unused = raw[15..18].*, | 1377 | .unused = raw[15..18].*, |
| 1378 | }; | 1378 | }; |
lib/std/compress/gzip.zig+5-5| ... | @@ -58,7 +58,7 @@ pub fn Decompress(comptime ReaderType: type) type { | ... | @@ -58,7 +58,7 @@ pub fn Decompress(comptime ReaderType: type) type { |
| 58 | const FLG = header[3]; | 58 | const FLG = header[3]; |
| 59 | // Modification time, as a Unix timestamp. | 59 | // Modification time, as a Unix timestamp. |
| 60 | // If zero there's no timestamp available. | 60 | // If zero there's no timestamp available. |
| 61 | const MTIME = mem.readIntLittle(u32, header[4..8]); | 61 | const MTIME = mem.readInt(u32, header[4..8], .little); |
| 62 | // Extra flags | 62 | // Extra flags |
| 63 | const XFL = header[8]; | 63 | const XFL = header[8]; |
| 64 | // Operating system where the compression took place | 64 | // Operating system where the compression took place |
| ... | @@ -66,7 +66,7 @@ pub fn Decompress(comptime ReaderType: type) type { | ... | @@ -66,7 +66,7 @@ pub fn Decompress(comptime ReaderType: type) type { |
| 66 | _ = XFL; | 66 | _ = XFL; |
| 67 | 67 | ||
| 68 | const extra = if (FLG & FEXTRA != 0) blk: { | 68 | const extra = if (FLG & FEXTRA != 0) blk: { |
| 69 | const len = try hashed_reader.readIntLittle(u16); | 69 | const len = try hashed_reader.readInt(u16, .little); |
| 70 | const tmp_buf = try allocator.alloc(u8, len); | 70 | const tmp_buf = try allocator.alloc(u8, len); |
| 71 | errdefer allocator.free(tmp_buf); | 71 | errdefer allocator.free(tmp_buf); |
| 72 | 72 | ||
| ... | @@ -88,7 +88,7 @@ pub fn Decompress(comptime ReaderType: type) type { | ... | @@ -88,7 +88,7 @@ pub fn Decompress(comptime ReaderType: type) type { |
| 88 | errdefer if (comment) |p| allocator.free(p); | 88 | errdefer if (comment) |p| allocator.free(p); |
| 89 | 89 | ||
| 90 | if (FLG & FHCRC != 0) { | 90 | if (FLG & FHCRC != 0) { |
| 91 | const hash = try source.readIntLittle(u16); | 91 | const hash = try source.readInt(u16, .little); |
| 92 | if (hash != @as(u16, @truncate(hasher.hasher.final()))) | 92 | if (hash != @as(u16, @truncate(hasher.hasher.final()))) |
| 93 | return error.WrongChecksum; | 93 | return error.WrongChecksum; |
| 94 | } | 94 | } |
| ... | @@ -133,12 +133,12 @@ pub fn Decompress(comptime ReaderType: type) type { | ... | @@ -133,12 +133,12 @@ pub fn Decompress(comptime ReaderType: type) type { |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | // We've reached the end of stream, check if the checksum matches | 135 | // We've reached the end of stream, check if the checksum matches |
| 136 | const hash = try self.in_reader.readIntLittle(u32); | 136 | const hash = try self.in_reader.readInt(u32, .little); |
| 137 | if (hash != self.hasher.final()) | 137 | if (hash != self.hasher.final()) |
| 138 | return error.WrongChecksum; | 138 | return error.WrongChecksum; |
| 139 | 139 | ||
| 140 | // The ISIZE field is the size of the uncompressed input modulo 2^32 | 140 | // The ISIZE field is the size of the uncompressed input modulo 2^32 |
| 141 | const input_size = try self.in_reader.readIntLittle(u32); | 141 | const input_size = try self.in_reader.readInt(u32, .little); |
| 142 | if (self.read_amt & 0xffffffff != input_size) | 142 | if (self.read_amt & 0xffffffff != input_size) |
| 143 | return error.CorruptedData; | 143 | return error.CorruptedData; |
| 144 | 144 |
lib/std/compress/lzma/decode.zig+3-3| ... | @@ -58,12 +58,12 @@ pub const Params = struct { | ... | @@ -58,12 +58,12 @@ pub const Params = struct { |
| 58 | props /= 5; | 58 | props /= 5; |
| 59 | const pb = @as(u3, @intCast(props)); | 59 | const pb = @as(u3, @intCast(props)); |
| 60 | 60 | ||
| 61 | const dict_size_provided = try reader.readIntLittle(u32); | 61 | const dict_size_provided = try reader.readInt(u32, .little); |
| 62 | const dict_size = @max(0x1000, dict_size_provided); | 62 | const dict_size = @max(0x1000, dict_size_provided); |
| 63 | 63 | ||
| 64 | const unpacked_size = switch (options.unpacked_size) { | 64 | const unpacked_size = switch (options.unpacked_size) { |
| 65 | .read_from_header => blk: { | 65 | .read_from_header => blk: { |
| 66 | const unpacked_size_provided = try reader.readIntLittle(u64); | 66 | const unpacked_size_provided = try reader.readInt(u64, .little); |
| 67 | const marker_mandatory = unpacked_size_provided == 0xFFFF_FFFF_FFFF_FFFF; | 67 | const marker_mandatory = unpacked_size_provided == 0xFFFF_FFFF_FFFF_FFFF; |
| 68 | break :blk if (marker_mandatory) | 68 | break :blk if (marker_mandatory) |
| 69 | null | 69 | null |
| ... | @@ -71,7 +71,7 @@ pub const Params = struct { | ... | @@ -71,7 +71,7 @@ pub const Params = struct { |
| 71 | unpacked_size_provided; | 71 | unpacked_size_provided; |
| 72 | }, | 72 | }, |
| 73 | .read_header_but_use_provided => |x| blk: { | 73 | .read_header_but_use_provided => |x| blk: { |
| 74 | _ = try reader.readIntLittle(u64); | 74 | _ = try reader.readInt(u64, .little); |
| 75 | break :blk x; | 75 | break :blk x; |
| 76 | }, | 76 | }, |
| 77 | .use_provided => |x| x, | 77 | .use_provided => |x| x, |
lib/std/compress/lzma/decode/rangecoder.zig+1-1| ... | @@ -12,7 +12,7 @@ pub const RangeDecoder = struct { | ... | @@ -12,7 +12,7 @@ pub const RangeDecoder = struct { |
| 12 | } | 12 | } |
| 13 | return RangeDecoder{ | 13 | return RangeDecoder{ |
| 14 | .range = 0xFFFF_FFFF, | 14 | .range = 0xFFFF_FFFF, |
| 15 | .code = try reader.readIntBig(u32), | 15 | .code = try reader.readInt(u32, .big), |
| 16 | }; | 16 | }; |
| 17 | } | 17 | } |
| 18 | 18 |
lib/std/compress/lzma2/decode.zig+3-3| ... | @@ -97,12 +97,12 @@ pub const Decoder = struct { | ... | @@ -97,12 +97,12 @@ pub const Decoder = struct { |
| 97 | const unpacked_size = blk: { | 97 | const unpacked_size = blk: { |
| 98 | var tmp: u64 = status & 0x1F; | 98 | var tmp: u64 = status & 0x1F; |
| 99 | tmp <<= 16; | 99 | tmp <<= 16; |
| 100 | tmp |= try reader.readIntBig(u16); | 100 | tmp |= try reader.readInt(u16, .big); |
| 101 | break :blk tmp + 1; | 101 | break :blk tmp + 1; |
| 102 | }; | 102 | }; |
| 103 | 103 | ||
| 104 | const packed_size = blk: { | 104 | const packed_size = blk: { |
| 105 | const tmp: u17 = try reader.readIntBig(u16); | 105 | const tmp: u17 = try reader.readInt(u16, .big); |
| 106 | break :blk tmp + 1; | 106 | break :blk tmp + 1; |
| 107 | }; | 107 | }; |
| 108 | 108 | ||
| ... | @@ -155,7 +155,7 @@ pub const Decoder = struct { | ... | @@ -155,7 +155,7 @@ pub const Decoder = struct { |
| 155 | accum: *LzAccumBuffer, | 155 | accum: *LzAccumBuffer, |
| 156 | reset_dict: bool, | 156 | reset_dict: bool, |
| 157 | ) !void { | 157 | ) !void { |
| 158 | const unpacked_size = @as(u17, try reader.readIntBig(u16)) + 1; | 158 | const unpacked_size = @as(u17, try reader.readInt(u16, .big)) + 1; |
| 159 | 159 | ||
| 160 | if (reset_dict) { | 160 | if (reset_dict) { |
| 161 | try accum.reset(writer); | 161 | try accum.reset(writer); |
lib/std/compress/xz.zig+5-5| ... | @@ -12,7 +12,7 @@ pub const Check = enum(u4) { | ... | @@ -12,7 +12,7 @@ pub const Check = enum(u4) { |
| 12 | }; | 12 | }; |
| 13 | 13 | ||
| 14 | fn readStreamFlags(reader: anytype, check: *Check) !void { | 14 | fn readStreamFlags(reader: anytype, check: *Check) !void { |
| 15 | var bit_reader = std.io.bitReader(.Little, reader); | 15 | var bit_reader = std.io.bitReader(.little, reader); |
| 16 | 16 | ||
| 17 | const reserved1 = try bit_reader.readBitsNoEof(u8, 8); | 17 | const reserved1 = try bit_reader.readBitsNoEof(u8, 8); |
| 18 | if (reserved1 != 0) | 18 | if (reserved1 != 0) |
| ... | @@ -52,7 +52,7 @@ pub fn Decompress(comptime ReaderType: type) type { | ... | @@ -52,7 +52,7 @@ pub fn Decompress(comptime ReaderType: type) type { |
| 52 | break :blk hasher.hasher.final(); | 52 | break :blk hasher.hasher.final(); |
| 53 | }; | 53 | }; |
| 54 | 54 | ||
| 55 | const hash_b = try source.readIntLittle(u32); | 55 | const hash_b = try source.readInt(u32, .little); |
| 56 | if (hash_a != hash_b) | 56 | if (hash_a != hash_b) |
| 57 | return error.WrongChecksum; | 57 | return error.WrongChecksum; |
| 58 | 58 | ||
| ... | @@ -105,20 +105,20 @@ pub fn Decompress(comptime ReaderType: type) type { | ... | @@ -105,20 +105,20 @@ pub fn Decompress(comptime ReaderType: type) type { |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | const hash_a = hasher.hasher.final(); | 107 | const hash_a = hasher.hasher.final(); |
| 108 | const hash_b = try counting_reader.readIntLittle(u32); | 108 | const hash_b = try counting_reader.readInt(u32, .little); |
| 109 | if (hash_a != hash_b) | 109 | if (hash_a != hash_b) |
| 110 | return error.WrongChecksum; | 110 | return error.WrongChecksum; |
| 111 | 111 | ||
| 112 | break :blk counter.bytes_read; | 112 | break :blk counter.bytes_read; |
| 113 | }; | 113 | }; |
| 114 | 114 | ||
| 115 | const hash_a = try self.in_reader.readIntLittle(u32); | 115 | const hash_a = try self.in_reader.readInt(u32, .little); |
| 116 | 116 | ||
| 117 | const hash_b = blk: { | 117 | const hash_b = blk: { |
| 118 | var hasher = std.compress.hashedReader(self.in_reader, Crc32.init()); | 118 | var hasher = std.compress.hashedReader(self.in_reader, Crc32.init()); |
| 119 | const hashed_reader = hasher.reader(); | 119 | const hashed_reader = hasher.reader(); |
| 120 | 120 | ||
| 121 | const backward_size = (@as(u64, try hashed_reader.readIntLittle(u32)) + 1) * 4; | 121 | const backward_size = (@as(u64, try hashed_reader.readInt(u32, .little)) + 1) * 4; |
| 122 | if (backward_size != index_size) | 122 | if (backward_size != index_size) |
| 123 | return error.CorruptInput; | 123 | return error.CorruptInput; |
| 124 | 124 |
lib/std/compress/xz/block.zig+3-3| ... | @@ -148,7 +148,7 @@ pub fn Decoder(comptime ReaderType: type) type { | ... | @@ -148,7 +148,7 @@ pub fn Decoder(comptime ReaderType: type) type { |
| 148 | } | 148 | } |
| 149 | 149 | ||
| 150 | const hash_a = header_hasher.hasher.final(); | 150 | const hash_a = header_hasher.hasher.final(); |
| 151 | const hash_b = try header_reader.readIntLittle(u32); | 151 | const hash_b = try header_reader.readInt(u32, .little); |
| 152 | if (hash_a != hash_b) | 152 | if (hash_a != hash_b) |
| 153 | return error.WrongChecksum; | 153 | return error.WrongChecksum; |
| 154 | } | 154 | } |
| ... | @@ -182,13 +182,13 @@ pub fn Decoder(comptime ReaderType: type) type { | ... | @@ -182,13 +182,13 @@ pub fn Decoder(comptime ReaderType: type) type { |
| 182 | .none => {}, | 182 | .none => {}, |
| 183 | .crc32 => { | 183 | .crc32 => { |
| 184 | const hash_a = Crc32.hash(unpacked_bytes); | 184 | const hash_a = Crc32.hash(unpacked_bytes); |
| 185 | const hash_b = try self.inner_reader.readIntLittle(u32); | 185 | const hash_b = try self.inner_reader.readInt(u32, .little); |
| 186 | if (hash_a != hash_b) | 186 | if (hash_a != hash_b) |
| 187 | return error.WrongChecksum; | 187 | return error.WrongChecksum; |
| 188 | }, | 188 | }, |
| 189 | .crc64 => { | 189 | .crc64 => { |
| 190 | const hash_a = Crc64.hash(unpacked_bytes); | 190 | const hash_a = Crc64.hash(unpacked_bytes); |
| 191 | const hash_b = try self.inner_reader.readIntLittle(u64); | 191 | const hash_b = try self.inner_reader.readInt(u64, .little); |
| 192 | if (hash_a != hash_b) | 192 | if (hash_a != hash_b) |
| 193 | return error.WrongChecksum; | 193 | return error.WrongChecksum; |
| 194 | }, | 194 | }, |
lib/std/compress/zlib.zig+4-4| ... | @@ -36,7 +36,7 @@ pub fn DecompressStream(comptime ReaderType: type) type { | ... | @@ -36,7 +36,7 @@ pub fn DecompressStream(comptime ReaderType: type) type { |
| 36 | 36 | ||
| 37 | fn init(allocator: mem.Allocator, source: ReaderType) !Self { | 37 | fn init(allocator: mem.Allocator, source: ReaderType) !Self { |
| 38 | // Zlib header format is specified in RFC1950 | 38 | // Zlib header format is specified in RFC1950 |
| 39 | const header_u16 = try source.readIntBig(u16); | 39 | const header_u16 = try source.readInt(u16, .big); |
| 40 | 40 | ||
| 41 | // verify the header checksum | 41 | // verify the header checksum |
| 42 | if (header_u16 % 31 != 0) | 42 | if (header_u16 % 31 != 0) |
| ... | @@ -81,7 +81,7 @@ pub fn DecompressStream(comptime ReaderType: type) type { | ... | @@ -81,7 +81,7 @@ pub fn DecompressStream(comptime ReaderType: type) type { |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | // We've reached the end of stream, check if the checksum matches | 83 | // We've reached the end of stream, check if the checksum matches |
| 84 | const hash = try self.in_reader.readIntBig(u32); | 84 | const hash = try self.in_reader.readInt(u32, .big); |
| 85 | if (hash != self.hasher.final()) | 85 | if (hash != self.hasher.final()) |
| 86 | return error.WrongChecksum; | 86 | return error.WrongChecksum; |
| 87 | 87 | ||
| ... | @@ -132,7 +132,7 @@ pub fn CompressStream(comptime WriterType: type) type { | ... | @@ -132,7 +132,7 @@ pub fn CompressStream(comptime WriterType: type) type { |
| 132 | }; | 132 | }; |
| 133 | header.checksum = @as(u5, @truncate(31 - @as(u16, @bitCast(header)) % 31)); | 133 | header.checksum = @as(u5, @truncate(31 - @as(u16, @bitCast(header)) % 31)); |
| 134 | 134 | ||
| 135 | try dest.writeIntBig(u16, @as(u16, @bitCast(header))); | 135 | try dest.writeInt(u16, @as(u16, @bitCast(header)), .big); |
| 136 | 136 | ||
| 137 | const compression_level: deflate.Compression = switch (options.level) { | 137 | const compression_level: deflate.Compression = switch (options.level) { |
| 138 | .no_compression => .no_compression, | 138 | .no_compression => .no_compression, |
| ... | @@ -171,7 +171,7 @@ pub fn CompressStream(comptime WriterType: type) type { | ... | @@ -171,7 +171,7 @@ pub fn CompressStream(comptime WriterType: type) type { |
| 171 | pub fn finish(self: *Self) !void { | 171 | pub fn finish(self: *Self) !void { |
| 172 | const hash = self.hasher.final(); | 172 | const hash = self.hasher.final(); |
| 173 | try self.deflator.close(); | 173 | try self.deflator.close(); |
| 174 | try self.in_writer.writeIntBig(u32, hash); | 174 | try self.in_writer.writeInt(u32, hash, .big); |
| 175 | } | 175 | } |
| 176 | }; | 176 | }; |
| 177 | } | 177 | } |
lib/std/compress/zstandard.zig+1-1| ... | @@ -201,7 +201,7 @@ pub fn DecompressStream( | ... | @@ -201,7 +201,7 @@ pub fn DecompressStream( |
| 201 | if (block_header.last_block) { | 201 | if (block_header.last_block) { |
| 202 | self.state = .LastBlock; | 202 | self.state = .LastBlock; |
| 203 | if (self.frame_context.has_checksum) { | 203 | if (self.frame_context.has_checksum) { |
| 204 | const checksum = source_reader.readIntLittle(u32) catch | 204 | const checksum = source_reader.readInt(u32, .little) catch |
| 205 | return error.MalformedFrame; | 205 | return error.MalformedFrame; |
| 206 | if (comptime options.verify_checksum) { | 206 | if (comptime options.verify_checksum) { |
| 207 | if (self.frame_context.hasher_opt) |*hasher| { | 207 | if (self.frame_context.hasher_opt) |*hasher| { |
lib/std/compress/zstandard/decode/block.zig+3-5| ... | @@ -13,8 +13,6 @@ const readers = @import("../readers.zig"); | ... | @@ -13,8 +13,6 @@ const readers = @import("../readers.zig"); |
| 13 | 13 | ||
| 14 | const decodeFseTable = @import("fse.zig").decodeFseTable; | 14 | const decodeFseTable = @import("fse.zig").decodeFseTable; |
| 15 | 15 | ||
| 16 | const readInt = std.mem.readIntLittle; | ||
| 17 | |||
| 18 | pub const Error = error{ | 16 | pub const Error = error{ |
| 19 | BlockSizeOverMaximum, | 17 | BlockSizeOverMaximum, |
| 20 | MalformedBlockSize, | 18 | MalformedBlockSize, |
| ... | @@ -1031,9 +1029,9 @@ fn decodeStreams(size_format: u2, stream_data: []const u8) !LiteralsSection.Stre | ... | @@ -1031,9 +1029,9 @@ fn decodeStreams(size_format: u2, stream_data: []const u8) !LiteralsSection.Stre |
| 1031 | 1029 | ||
| 1032 | if (stream_data.len < 6) return error.MalformedLiteralsSection; | 1030 | if (stream_data.len < 6) return error.MalformedLiteralsSection; |
| 1033 | 1031 | ||
| 1034 | const stream_1_length = @as(usize, readInt(u16, stream_data[0..2])); | 1032 | const stream_1_length: usize = std.mem.readInt(u16, stream_data[0..2], .little); |
| 1035 | const stream_2_length = @as(usize, readInt(u16, stream_data[2..4])); | 1033 | const stream_2_length: usize = std.mem.readInt(u16, stream_data[2..4], .little); |
| 1036 | const stream_3_length = @as(usize, readInt(u16, stream_data[4..6])); | 1034 | const stream_3_length: usize = std.mem.readInt(u16, stream_data[4..6], .little); |
| 1037 | 1035 | ||
| 1038 | const stream_1_start = 6; | 1036 | const stream_1_start = 6; |
| 1039 | const stream_2_start = stream_1_start + stream_1_length; | 1037 | const stream_2_start = stream_1_start + stream_1_length; |
lib/std/compress/zstandard/decompress.zig+14-17| ... | @@ -15,9 +15,6 @@ pub const block = @import("decode/block.zig"); | ... | @@ -15,9 +15,6 @@ pub const block = @import("decode/block.zig"); |
| 15 | 15 | ||
| 16 | const readers = @import("readers.zig"); | 16 | const readers = @import("readers.zig"); |
| 17 | 17 | ||
| 18 | const readInt = std.mem.readIntLittle; | ||
| 19 | const readIntSlice = std.mem.readIntSliceLittle; | ||
| 20 | |||
| 21 | /// Returns `true` is `magic` is a valid magic number for a skippable frame | 18 | /// Returns `true` is `magic` is a valid magic number for a skippable frame |
| 22 | pub fn isSkippableMagic(magic: u32) bool { | 19 | pub fn isSkippableMagic(magic: u32) bool { |
| 23 | return frame.Skippable.magic_number_min <= magic and magic <= frame.Skippable.magic_number_max; | 20 | return frame.Skippable.magic_number_min <= magic and magic <= frame.Skippable.magic_number_max; |
| ... | @@ -31,7 +28,7 @@ pub fn isSkippableMagic(magic: u32) bool { | ... | @@ -31,7 +28,7 @@ pub fn isSkippableMagic(magic: u32) bool { |
| 31 | /// skippable frames. | 28 | /// skippable frames. |
| 32 | /// - `error.EndOfStream` if `source` contains fewer than 4 bytes | 29 | /// - `error.EndOfStream` if `source` contains fewer than 4 bytes |
| 33 | pub fn decodeFrameType(source: anytype) error{ BadMagic, EndOfStream }!frame.Kind { | 30 | pub fn decodeFrameType(source: anytype) error{ BadMagic, EndOfStream }!frame.Kind { |
| 34 | const magic = try source.readIntLittle(u32); | 31 | const magic = try source.readInt(u32, .little); |
| 35 | return frameType(magic); | 32 | return frameType(magic); |
| 36 | } | 33 | } |
| 37 | 34 | ||
| ... | @@ -65,14 +62,14 @@ pub const HeaderError = error{ BadMagic, EndOfStream, ReservedBitSet }; | ... | @@ -65,14 +62,14 @@ pub const HeaderError = error{ BadMagic, EndOfStream, ReservedBitSet }; |
| 65 | /// - `error.ReservedBitSet` if the frame is a Zstandard frame and any of the | 62 | /// - `error.ReservedBitSet` if the frame is a Zstandard frame and any of the |
| 66 | /// reserved bits are set | 63 | /// reserved bits are set |
| 67 | pub fn decodeFrameHeader(source: anytype) (@TypeOf(source).Error || HeaderError)!FrameHeader { | 64 | pub fn decodeFrameHeader(source: anytype) (@TypeOf(source).Error || HeaderError)!FrameHeader { |
| 68 | const magic = try source.readIntLittle(u32); | 65 | const magic = try source.readInt(u32, .little); |
| 69 | const frame_type = try frameType(magic); | 66 | const frame_type = try frameType(magic); |
| 70 | switch (frame_type) { | 67 | switch (frame_type) { |
| 71 | .zstandard => return FrameHeader{ .zstandard = try decodeZstandardHeader(source) }, | 68 | .zstandard => return FrameHeader{ .zstandard = try decodeZstandardHeader(source) }, |
| 72 | .skippable => return FrameHeader{ | 69 | .skippable => return FrameHeader{ |
| 73 | .skippable = .{ | 70 | .skippable = .{ |
| 74 | .magic_number = magic, | 71 | .magic_number = magic, |
| 75 | .frame_size = try source.readIntLittle(u32), | 72 | .frame_size = try source.readInt(u32, .little), |
| 76 | }, | 73 | }, |
| 77 | }, | 74 | }, |
| 78 | } | 75 | } |
| ... | @@ -193,7 +190,7 @@ pub fn decodeFrame( | ... | @@ -193,7 +190,7 @@ pub fn decodeFrame( |
| 193 | switch (try decodeFrameType(fbs.reader())) { | 190 | switch (try decodeFrameType(fbs.reader())) { |
| 194 | .zstandard => return decodeZstandardFrame(dest, src, verify_checksum), | 191 | .zstandard => return decodeZstandardFrame(dest, src, verify_checksum), |
| 195 | .skippable => { | 192 | .skippable => { |
| 196 | const content_size = try fbs.reader().readIntLittle(u32); | 193 | const content_size = try fbs.reader().readInt(u32, .little); |
| 197 | if (content_size > std.math.maxInt(usize) - 8) return error.SkippableSizeTooLarge; | 194 | if (content_size > std.math.maxInt(usize) - 8) return error.SkippableSizeTooLarge; |
| 198 | const read_count = @as(usize, content_size) + 8; | 195 | const read_count = @as(usize, content_size) + 8; |
| 199 | if (read_count > src.len) return error.SkippableSizeTooLarge; | 196 | if (read_count > src.len) return error.SkippableSizeTooLarge; |
| ... | @@ -238,7 +235,7 @@ pub fn decodeFrameArrayList( | ... | @@ -238,7 +235,7 @@ pub fn decodeFrameArrayList( |
| 238 | ) (error{ BadMagic, OutOfMemory, SkippableSizeTooLarge } || FrameContext.Error || FrameError)!usize { | 235 | ) (error{ BadMagic, OutOfMemory, SkippableSizeTooLarge } || FrameContext.Error || FrameError)!usize { |
| 239 | var fbs = std.io.fixedBufferStream(src); | 236 | var fbs = std.io.fixedBufferStream(src); |
| 240 | const reader = fbs.reader(); | 237 | const reader = fbs.reader(); |
| 241 | const magic = try reader.readIntLittle(u32); | 238 | const magic = try reader.readInt(u32, .little); |
| 242 | switch (try frameType(magic)) { | 239 | switch (try frameType(magic)) { |
| 243 | .zstandard => return decodeZstandardFrameArrayList( | 240 | .zstandard => return decodeZstandardFrameArrayList( |
| 244 | allocator, | 241 | allocator, |
| ... | @@ -248,7 +245,7 @@ pub fn decodeFrameArrayList( | ... | @@ -248,7 +245,7 @@ pub fn decodeFrameArrayList( |
| 248 | window_size_max, | 245 | window_size_max, |
| 249 | ), | 246 | ), |
| 250 | .skippable => { | 247 | .skippable => { |
| 251 | const content_size = try fbs.reader().readIntLittle(u32); | 248 | const content_size = try fbs.reader().readInt(u32, .little); |
| 252 | if (content_size > std.math.maxInt(usize) - 8) return error.SkippableSizeTooLarge; | 249 | if (content_size > std.math.maxInt(usize) - 8) return error.SkippableSizeTooLarge; |
| 253 | const read_count = @as(usize, content_size) + 8; | 250 | const read_count = @as(usize, content_size) + 8; |
| 254 | if (read_count > src.len) return error.SkippableSizeTooLarge; | 251 | if (read_count > src.len) return error.SkippableSizeTooLarge; |
| ... | @@ -302,7 +299,7 @@ pub fn decodeZstandardFrame( | ... | @@ -302,7 +299,7 @@ pub fn decodeZstandardFrame( |
| 302 | WindowSizeUnknown, | 299 | WindowSizeUnknown, |
| 303 | DictionaryIdFlagUnsupported, | 300 | DictionaryIdFlagUnsupported, |
| 304 | } || FrameError)!ReadWriteCount { | 301 | } || FrameError)!ReadWriteCount { |
| 305 | assert(readInt(u32, src[0..4]) == frame.Zstandard.magic_number); | 302 | assert(std.mem.readInt(u32, src[0..4], .little) == frame.Zstandard.magic_number); |
| 306 | var consumed_count: usize = 4; | 303 | var consumed_count: usize = 4; |
| 307 | 304 | ||
| 308 | var frame_context = context: { | 305 | var frame_context = context: { |
| ... | @@ -354,7 +351,7 @@ pub fn decodeZStandardFrameBlocks( | ... | @@ -354,7 +351,7 @@ pub fn decodeZStandardFrameBlocks( |
| 354 | if (written_count != content_size) return error.BadContentSize; | 351 | if (written_count != content_size) return error.BadContentSize; |
| 355 | if (frame_context.has_checksum) { | 352 | if (frame_context.has_checksum) { |
| 356 | if (src.len < consumed_count + 4) return error.EndOfStream; | 353 | if (src.len < consumed_count + 4) return error.EndOfStream; |
| 357 | const checksum = readIntSlice(u32, src[consumed_count .. consumed_count + 4]); | 354 | const checksum = std.mem.readInt(u32, src[consumed_count..][0..4], .little); |
| 358 | consumed_count += 4; | 355 | consumed_count += 4; |
| 359 | if (frame_context.hasher_opt) |*hasher| { | 356 | if (frame_context.hasher_opt) |*hasher| { |
| 360 | if (checksum != computeChecksum(hasher)) return error.ChecksumFailure; | 357 | if (checksum != computeChecksum(hasher)) return error.ChecksumFailure; |
| ... | @@ -445,7 +442,7 @@ pub fn decodeZstandardFrameArrayList( | ... | @@ -445,7 +442,7 @@ pub fn decodeZstandardFrameArrayList( |
| 445 | verify_checksum: bool, | 442 | verify_checksum: bool, |
| 446 | window_size_max: usize, | 443 | window_size_max: usize, |
| 447 | ) (error{OutOfMemory} || FrameContext.Error || FrameError)!usize { | 444 | ) (error{OutOfMemory} || FrameContext.Error || FrameError)!usize { |
| 448 | assert(readInt(u32, src[0..4]) == frame.Zstandard.magic_number); | 445 | assert(std.mem.readInt(u32, src[0..4], .little) == frame.Zstandard.magic_number); |
| 449 | var consumed_count: usize = 4; | 446 | var consumed_count: usize = 4; |
| 450 | 447 | ||
| 451 | var frame_context = context: { | 448 | var frame_context = context: { |
| ... | @@ -520,7 +517,7 @@ pub fn decodeZstandardFrameBlocksArrayList( | ... | @@ -520,7 +517,7 @@ pub fn decodeZstandardFrameBlocksArrayList( |
| 520 | 517 | ||
| 521 | if (frame_context.has_checksum) { | 518 | if (frame_context.has_checksum) { |
| 522 | if (src.len < consumed_count + 4) return error.EndOfStream; | 519 | if (src.len < consumed_count + 4) return error.EndOfStream; |
| 523 | const checksum = readIntSlice(u32, src[consumed_count .. consumed_count + 4]); | 520 | const checksum = std.mem.readInt(u32, src[consumed_count..][0..4], .little); |
| 524 | consumed_count += 4; | 521 | consumed_count += 4; |
| 525 | if (frame_context.hasher_opt) |*hasher| { | 522 | if (frame_context.hasher_opt) |*hasher| { |
| 526 | if (checksum != computeChecksum(hasher)) return error.ChecksumFailure; | 523 | if (checksum != computeChecksum(hasher)) return error.ChecksumFailure; |
| ... | @@ -569,9 +566,9 @@ fn decodeFrameBlocksInner( | ... | @@ -569,9 +566,9 @@ fn decodeFrameBlocksInner( |
| 569 | /// Decode the header of a skippable frame. The first four bytes of `src` must | 566 | /// Decode the header of a skippable frame. The first four bytes of `src` must |
| 570 | /// be a valid magic number for a skippable frame. | 567 | /// be a valid magic number for a skippable frame. |
| 571 | pub fn decodeSkippableHeader(src: *const [8]u8) SkippableHeader { | 568 | pub fn decodeSkippableHeader(src: *const [8]u8) SkippableHeader { |
| 572 | const magic = readInt(u32, src[0..4]); | 569 | const magic = std.mem.readInt(u32, src[0..4], .little); |
| 573 | assert(isSkippableMagic(magic)); | 570 | assert(isSkippableMagic(magic)); |
| 574 | const frame_size = readInt(u32, src[4..8]); | 571 | const frame_size = std.mem.readInt(u32, src[4..8], .little); |
| 575 | return .{ | 572 | return .{ |
| 576 | .magic_number = magic, | 573 | .magic_number = magic, |
| 577 | .frame_size = frame_size, | 574 | .frame_size = frame_size, |
| ... | @@ -612,13 +609,13 @@ pub fn decodeZstandardHeader( | ... | @@ -612,13 +609,13 @@ pub fn decodeZstandardHeader( |
| 612 | if (descriptor.dictionary_id_flag > 0) { | 609 | if (descriptor.dictionary_id_flag > 0) { |
| 613 | // if flag is 3 then field_size = 4, else field_size = flag | 610 | // if flag is 3 then field_size = 4, else field_size = flag |
| 614 | const field_size = (@as(u4, 1) << descriptor.dictionary_id_flag) >> 1; | 611 | const field_size = (@as(u4, 1) << descriptor.dictionary_id_flag) >> 1; |
| 615 | dictionary_id = try source.readVarInt(u32, .Little, field_size); | 612 | dictionary_id = try source.readVarInt(u32, .little, field_size); |
| 616 | } | 613 | } |
| 617 | 614 | ||
| 618 | var content_size: ?u64 = null; | 615 | var content_size: ?u64 = null; |
| 619 | if (descriptor.single_segment_flag or descriptor.content_size_flag > 0) { | 616 | if (descriptor.single_segment_flag or descriptor.content_size_flag > 0) { |
| 620 | const field_size = @as(u4, 1) << descriptor.content_size_flag; | 617 | const field_size = @as(u4, 1) << descriptor.content_size_flag; |
| 621 | content_size = try source.readVarInt(u64, .Little, field_size); | 618 | content_size = try source.readVarInt(u64, .little, field_size); |
| 622 | if (field_size == 2) content_size.? += 256; | 619 | if (field_size == 2) content_size.? += 256; |
| 623 | } | 620 | } |
| 624 | 621 |
lib/std/compress/zstandard/readers.zig+4-4| ... | @@ -31,11 +31,11 @@ pub const ReversedByteReader = struct { | ... | @@ -31,11 +31,11 @@ pub const ReversedByteReader = struct { |
| 31 | /// FSE compressed data. | 31 | /// FSE compressed data. |
| 32 | pub const ReverseBitReader = struct { | 32 | pub const ReverseBitReader = struct { |
| 33 | byte_reader: ReversedByteReader, | 33 | byte_reader: ReversedByteReader, |
| 34 | bit_reader: std.io.BitReader(.Big, ReversedByteReader.Reader), | 34 | bit_reader: std.io.BitReader(.big, ReversedByteReader.Reader), |
| 35 | 35 | ||
| 36 | pub fn init(self: *ReverseBitReader, bytes: []const u8) error{BitStreamHasNoStartBit}!void { | 36 | pub fn init(self: *ReverseBitReader, bytes: []const u8) error{BitStreamHasNoStartBit}!void { |
| 37 | self.byte_reader = ReversedByteReader.init(bytes); | 37 | self.byte_reader = ReversedByteReader.init(bytes); |
| 38 | self.bit_reader = std.io.bitReader(.Big, self.byte_reader.reader()); | 38 | self.bit_reader = std.io.bitReader(.big, self.byte_reader.reader()); |
| 39 | if (bytes.len == 0) return; | 39 | if (bytes.len == 0) return; |
| 40 | var i: usize = 0; | 40 | var i: usize = 0; |
| 41 | while (i < 8 and 0 == self.readBitsNoEof(u1, 1) catch unreachable) : (i += 1) {} | 41 | while (i < 8 and 0 == self.readBitsNoEof(u1, 1) catch unreachable) : (i += 1) {} |
| ... | @@ -61,7 +61,7 @@ pub const ReverseBitReader = struct { | ... | @@ -61,7 +61,7 @@ pub const ReverseBitReader = struct { |
| 61 | 61 | ||
| 62 | pub fn BitReader(comptime Reader: type) type { | 62 | pub fn BitReader(comptime Reader: type) type { |
| 63 | return struct { | 63 | return struct { |
| 64 | underlying: std.io.BitReader(.Little, Reader), | 64 | underlying: std.io.BitReader(.little, Reader), |
| 65 | 65 | ||
| 66 | pub fn readBitsNoEof(self: *@This(), comptime U: type, num_bits: usize) !U { | 66 | pub fn readBitsNoEof(self: *@This(), comptime U: type, num_bits: usize) !U { |
| 67 | return self.underlying.readBitsNoEof(U, num_bits); | 67 | return self.underlying.readBitsNoEof(U, num_bits); |
| ... | @@ -78,5 +78,5 @@ pub fn BitReader(comptime Reader: type) type { | ... | @@ -78,5 +78,5 @@ pub fn BitReader(comptime Reader: type) type { |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | pub fn bitReader(reader: anytype) BitReader(@TypeOf(reader)) { | 80 | pub fn bitReader(reader: anytype) BitReader(@TypeOf(reader)) { |
| 81 | return .{ .underlying = std.io.bitReader(.Little, reader) }; | 81 | return .{ .underlying = std.io.bitReader(.little, reader) }; |
| 82 | } | 82 | } |
lib/std/crypto/25519/field.zig+9-11| ... | @@ -1,8 +1,6 @@ | ... | @@ -1,8 +1,6 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); | 2 | const builtin = @import("builtin"); |
| 3 | const crypto = std.crypto; | 3 | const crypto = std.crypto; |
| 4 | const readIntLittle = std.mem.readIntLittle; | ||
| 5 | const writeIntLittle = std.mem.writeIntLittle; | ||
| 6 | 4 | ||
| 7 | const NonCanonicalError = crypto.errors.NonCanonicalError; | 5 | const NonCanonicalError = crypto.errors.NonCanonicalError; |
| 8 | const NotSquareError = crypto.errors.NotSquareError; | 6 | const NotSquareError = crypto.errors.NotSquareError; |
| ... | @@ -73,11 +71,11 @@ pub const Fe = struct { | ... | @@ -73,11 +71,11 @@ pub const Fe = struct { |
| 73 | /// Unpack a field element | 71 | /// Unpack a field element |
| 74 | pub fn fromBytes(s: [32]u8) Fe { | 72 | pub fn fromBytes(s: [32]u8) Fe { |
| 75 | var fe: Fe = undefined; | 73 | var fe: Fe = undefined; |
| 76 | fe.limbs[0] = readIntLittle(u64, s[0..8]) & MASK51; | 74 | fe.limbs[0] = std.mem.readInt(u64, s[0..8], .little) & MASK51; |
| 77 | fe.limbs[1] = (readIntLittle(u64, s[6..14]) >> 3) & MASK51; | 75 | fe.limbs[1] = (std.mem.readInt(u64, s[6..14], .little) >> 3) & MASK51; |
| 78 | fe.limbs[2] = (readIntLittle(u64, s[12..20]) >> 6) & MASK51; | 76 | fe.limbs[2] = (std.mem.readInt(u64, s[12..20], .little) >> 6) & MASK51; |
| 79 | fe.limbs[3] = (readIntLittle(u64, s[19..27]) >> 1) & MASK51; | 77 | fe.limbs[3] = (std.mem.readInt(u64, s[19..27], .little) >> 1) & MASK51; |
| 80 | fe.limbs[4] = (readIntLittle(u64, s[24..32]) >> 12) & MASK51; | 78 | fe.limbs[4] = (std.mem.readInt(u64, s[24..32], .little) >> 12) & MASK51; |
| 81 | 79 | ||
| 82 | return fe; | 80 | return fe; |
| 83 | } | 81 | } |
| ... | @@ -87,10 +85,10 @@ pub const Fe = struct { | ... | @@ -87,10 +85,10 @@ pub const Fe = struct { |
| 87 | var reduced = fe; | 85 | var reduced = fe; |
| 88 | reduced.reduce(); | 86 | reduced.reduce(); |
| 89 | var s: [32]u8 = undefined; | 87 | var s: [32]u8 = undefined; |
| 90 | writeIntLittle(u64, s[0..8], reduced.limbs[0] | (reduced.limbs[1] << 51)); | 88 | std.mem.writeInt(u64, s[0..8], reduced.limbs[0] | (reduced.limbs[1] << 51), .little); |
| 91 | writeIntLittle(u64, s[8..16], (reduced.limbs[1] >> 13) | (reduced.limbs[2] << 38)); | 89 | std.mem.writeInt(u64, s[8..16], (reduced.limbs[1] >> 13) | (reduced.limbs[2] << 38), .little); |
| 92 | writeIntLittle(u64, s[16..24], (reduced.limbs[2] >> 26) | (reduced.limbs[3] << 25)); | 90 | std.mem.writeInt(u64, s[16..24], (reduced.limbs[2] >> 26) | (reduced.limbs[3] << 25), .little); |
| 93 | writeIntLittle(u64, s[24..32], (reduced.limbs[3] >> 39) | (reduced.limbs[4] << 12)); | 91 | std.mem.writeInt(u64, s[24..32], (reduced.limbs[3] >> 39) | (reduced.limbs[4] << 12), .little); |
| 94 | 92 | ||
| 95 | return s; | 93 | return s; |
| 96 | } | 94 | } |
lib/std/crypto/25519/scalar.zig+6-6| ... | @@ -15,7 +15,7 @@ pub const zero = [_]u8{0} ** 32; | ... | @@ -15,7 +15,7 @@ pub const zero = [_]u8{0} ** 32; |
| 15 | 15 | ||
| 16 | const field_order_s = s: { | 16 | const field_order_s = s: { |
| 17 | var s: [32]u8 = undefined; | 17 | var s: [32]u8 = undefined; |
| 18 | mem.writeIntLittle(u256, &s, field_order); | 18 | mem.writeInt(u256, &s, field_order, .little); |
| 19 | break :s s; | 19 | break :s s; |
| 20 | }; | 20 | }; |
| 21 | 21 | ||
| ... | @@ -127,9 +127,9 @@ pub const Scalar = struct { | ... | @@ -127,9 +127,9 @@ pub const Scalar = struct { |
| 127 | var bytes: CompressedScalar = undefined; | 127 | var bytes: CompressedScalar = undefined; |
| 128 | var i: usize = 0; | 128 | var i: usize = 0; |
| 129 | while (i < 4) : (i += 1) { | 129 | while (i < 4) : (i += 1) { |
| 130 | mem.writeIntLittle(u64, bytes[i * 7 ..][0..8], expanded.limbs[i]); | 130 | mem.writeInt(u64, bytes[i * 7 ..][0..8], expanded.limbs[i], .little); |
| 131 | } | 131 | } |
| 132 | mem.writeIntLittle(u32, bytes[i * 7 ..][0..4], @as(u32, @intCast(expanded.limbs[i]))); | 132 | mem.writeInt(u32, bytes[i * 7 ..][0..4], @intCast(expanded.limbs[i]), .little); |
| 133 | return bytes; | 133 | return bytes; |
| 134 | } | 134 | } |
| 135 | 135 | ||
| ... | @@ -580,7 +580,7 @@ const ScalarDouble = struct { | ... | @@ -580,7 +580,7 @@ const ScalarDouble = struct { |
| 580 | var limbs: Limbs = undefined; | 580 | var limbs: Limbs = undefined; |
| 581 | var i: usize = 0; | 581 | var i: usize = 0; |
| 582 | while (i < 9) : (i += 1) { | 582 | while (i < 9) : (i += 1) { |
| 583 | limbs[i] = mem.readIntLittle(u64, bytes[i * 7 ..][0..8]) & 0xffffffffffffff; | 583 | limbs[i] = mem.readInt(u64, bytes[i * 7 ..][0..8], .little) & 0xffffffffffffff; |
| 584 | } | 584 | } |
| 585 | limbs[i] = @as(u64, bytes[i * 7]); | 585 | limbs[i] = @as(u64, bytes[i * 7]); |
| 586 | return ScalarDouble{ .limbs = limbs }; | 586 | return ScalarDouble{ .limbs = limbs }; |
| ... | @@ -590,9 +590,9 @@ const ScalarDouble = struct { | ... | @@ -590,9 +590,9 @@ const ScalarDouble = struct { |
| 590 | var limbs: Limbs = undefined; | 590 | var limbs: Limbs = undefined; |
| 591 | var i: usize = 0; | 591 | var i: usize = 0; |
| 592 | while (i < 4) : (i += 1) { | 592 | while (i < 4) : (i += 1) { |
| 593 | limbs[i] = mem.readIntLittle(u64, bytes[i * 7 ..][0..8]) & 0xffffffffffffff; | 593 | limbs[i] = mem.readInt(u64, bytes[i * 7 ..][0..8], .little) & 0xffffffffffffff; |
| 594 | } | 594 | } |
| 595 | limbs[i] = @as(u64, mem.readIntLittle(u32, bytes[i * 7 ..][0..4])); | 595 | limbs[i] = @as(u64, mem.readInt(u32, bytes[i * 7 ..][0..4], .little)); |
| 596 | @memset(limbs[5..], 0); | 596 | @memset(limbs[5..], 0); |
| 597 | return ScalarDouble{ .limbs = limbs }; | 597 | return ScalarDouble{ .limbs = limbs }; |
| 598 | } | 598 | } |
lib/std/crypto/Certificate.zig+4-4| ... | @@ -1075,7 +1075,7 @@ pub const rsa = struct { | ... | @@ -1075,7 +1075,7 @@ pub const rsa = struct { |
| 1075 | // Reject modulus below 512 bits. | 1075 | // Reject modulus below 512 bits. |
| 1076 | // 512-bit RSA was factored in 1999, so this limit barely means anything, | 1076 | // 512-bit RSA was factored in 1999, so this limit barely means anything, |
| 1077 | // but establish some limit now to ratchet in what we can. | 1077 | // but establish some limit now to ratchet in what we can. |
| 1078 | const _n = Modulus.fromBytes(modulus_bytes, .Big) catch return error.CertificatePublicKeyInvalid; | 1078 | const _n = Modulus.fromBytes(modulus_bytes, .big) catch return error.CertificatePublicKeyInvalid; |
| 1079 | if (_n.bits() < 512) return error.CertificatePublicKeyInvalid; | 1079 | if (_n.bits() < 512) return error.CertificatePublicKeyInvalid; |
| 1080 | 1080 | ||
| 1081 | // Exponent must be odd and greater than 2. | 1081 | // Exponent must be odd and greater than 2. |
| ... | @@ -1085,7 +1085,7 @@ pub const rsa = struct { | ... | @@ -1085,7 +1085,7 @@ pub const rsa = struct { |
| 1085 | // Windows commonly does. | 1085 | // Windows commonly does. |
| 1086 | // [1] https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/ns-wincrypt-rsapubkey | 1086 | // [1] https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/ns-wincrypt-rsapubkey |
| 1087 | if (pub_bytes.len > 4) return error.CertificatePublicKeyInvalid; | 1087 | if (pub_bytes.len > 4) return error.CertificatePublicKeyInvalid; |
| 1088 | const _e = Fe.fromBytes(_n, pub_bytes, .Big) catch return error.CertificatePublicKeyInvalid; | 1088 | const _e = Fe.fromBytes(_n, pub_bytes, .big) catch return error.CertificatePublicKeyInvalid; |
| 1089 | if (!_e.isOdd()) return error.CertificatePublicKeyInvalid; | 1089 | if (!_e.isOdd()) return error.CertificatePublicKeyInvalid; |
| 1090 | const e_v = _e.toPrimitive(u32) catch return error.CertificatePublicKeyInvalid; | 1090 | const e_v = _e.toPrimitive(u32) catch return error.CertificatePublicKeyInvalid; |
| 1091 | if (e_v < 2) return error.CertificatePublicKeyInvalid; | 1091 | if (e_v < 2) return error.CertificatePublicKeyInvalid; |
| ... | @@ -1116,10 +1116,10 @@ pub const rsa = struct { | ... | @@ -1116,10 +1116,10 @@ pub const rsa = struct { |
| 1116 | }; | 1116 | }; |
| 1117 | 1117 | ||
| 1118 | fn encrypt(comptime modulus_len: usize, msg: [modulus_len]u8, public_key: PublicKey) ![modulus_len]u8 { | 1118 | fn encrypt(comptime modulus_len: usize, msg: [modulus_len]u8, public_key: PublicKey) ![modulus_len]u8 { |
| 1119 | const m = Fe.fromBytes(public_key.n, &msg, .Big) catch return error.MessageTooLong; | 1119 | const m = Fe.fromBytes(public_key.n, &msg, .big) catch return error.MessageTooLong; |
| 1120 | const e = public_key.n.powPublic(m, public_key.e) catch unreachable; | 1120 | const e = public_key.n.powPublic(m, public_key.e) catch unreachable; |
| 1121 | var res: [modulus_len]u8 = undefined; | 1121 | var res: [modulus_len]u8 = undefined; |
| 1122 | e.toBytes(&res, .Big) catch unreachable; | 1122 | e.toBytes(&res, .big) catch unreachable; |
| 1123 | return res; | 1123 | return res; |
| 1124 | } | 1124 | } |
| 1125 | }; | 1125 | }; |
lib/std/crypto/Certificate/Bundle/macos.zig+2-2| ... | @@ -32,7 +32,7 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void { | ... | @@ -32,7 +32,7 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void { |
| 32 | 32 | ||
| 33 | var table_idx: u32 = 0; | 33 | var table_idx: u32 = 0; |
| 34 | while (table_idx < table_list.len) : (table_idx += 1) { | 34 | while (table_idx < table_list.len) : (table_idx += 1) { |
| 35 | table_list[table_idx] = try reader.readIntBig(u32); | 35 | table_list[table_idx] = try reader.readInt(u32, .big); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | const now_sec = std.time.timestamp(); | 38 | const now_sec = std.time.timestamp(); |
| ... | @@ -51,7 +51,7 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void { | ... | @@ -51,7 +51,7 @@ pub fn rescanMac(cb: *Bundle, gpa: Allocator) RescanMacError!void { |
| 51 | 51 | ||
| 52 | var record_idx: u32 = 0; | 52 | var record_idx: u32 = 0; |
| 53 | while (record_idx < record_list.len) : (record_idx += 1) { | 53 | while (record_idx < record_list.len) : (record_idx += 1) { |
| 54 | record_list[record_idx] = try reader.readIntBig(u32); | 54 | record_list[record_idx] = try reader.readInt(u32, .big); |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | for (record_list) |record_offset| { | 57 | for (record_list) |record_offset| { |
lib/std/crypto/aegis.zig+4-4| ... | @@ -106,8 +106,8 @@ const State128L = struct { | ... | @@ -106,8 +106,8 @@ const State128L = struct { |
| 106 | fn mac(state: *State128L, comptime tag_bits: u9, adlen: usize, mlen: usize) [tag_bits / 8]u8 { | 106 | fn mac(state: *State128L, comptime tag_bits: u9, adlen: usize, mlen: usize) [tag_bits / 8]u8 { |
| 107 | const blocks = &state.blocks; | 107 | const blocks = &state.blocks; |
| 108 | var sizes: [16]u8 = undefined; | 108 | var sizes: [16]u8 = undefined; |
| 109 | mem.writeIntLittle(u64, sizes[0..8], adlen * 8); | 109 | mem.writeInt(u64, sizes[0..8], adlen * 8, .little); |
| 110 | mem.writeIntLittle(u64, sizes[8..16], mlen * 8); | 110 | mem.writeInt(u64, sizes[8..16], mlen * 8, .little); |
| 111 | const tmp = AesBlock.fromBytes(&sizes).xorBlocks(blocks[2]); | 111 | const tmp = AesBlock.fromBytes(&sizes).xorBlocks(blocks[2]); |
| 112 | var i: usize = 0; | 112 | var i: usize = 0; |
| 113 | while (i < 7) : (i += 1) { | 113 | while (i < 7) : (i += 1) { |
| ... | @@ -284,8 +284,8 @@ const State256 = struct { | ... | @@ -284,8 +284,8 @@ const State256 = struct { |
| 284 | fn mac(state: *State256, comptime tag_bits: u9, adlen: usize, mlen: usize) [tag_bits / 8]u8 { | 284 | fn mac(state: *State256, comptime tag_bits: u9, adlen: usize, mlen: usize) [tag_bits / 8]u8 { |
| 285 | const blocks = &state.blocks; | 285 | const blocks = &state.blocks; |
| 286 | var sizes: [16]u8 = undefined; | 286 | var sizes: [16]u8 = undefined; |
| 287 | mem.writeIntLittle(u64, sizes[0..8], adlen * 8); | 287 | mem.writeInt(u64, sizes[0..8], adlen * 8, .little); |
| 288 | mem.writeIntLittle(u64, sizes[8..16], mlen * 8); | 288 | mem.writeInt(u64, sizes[8..16], mlen * 8, .little); |
| 289 | const tmp = AesBlock.fromBytes(&sizes).xorBlocks(blocks[3]); | 289 | const tmp = AesBlock.fromBytes(&sizes).xorBlocks(blocks[3]); |
| 290 | var i: usize = 0; | 290 | var i: usize = 0; |
| 291 | while (i < 7) : (i += 1) { | 291 | while (i < 7) : (i += 1) { |
lib/std/crypto/aes.zig+1-1| ... | @@ -50,7 +50,7 @@ test "ctr" { | ... | @@ -50,7 +50,7 @@ test "ctr" { |
| 50 | 50 | ||
| 51 | var out: [exp_out.len]u8 = undefined; | 51 | var out: [exp_out.len]u8 = undefined; |
| 52 | var ctx = Aes128.initEnc(key); | 52 | var ctx = Aes128.initEnc(key); |
| 53 | ctr(AesEncryptCtx(Aes128), ctx, out[0..], in[0..], iv, std.builtin.Endian.Big); | 53 | ctr(AesEncryptCtx(Aes128), ctx, out[0..], in[0..], iv, std.builtin.Endian.big); |
| 54 | try testing.expectEqualSlices(u8, exp_out[0..], out[0..]); | 54 | try testing.expectEqualSlices(u8, exp_out[0..], out[0..]); |
| 55 | } | 55 | } |
| 56 | 56 |
lib/std/crypto/aes/soft.zig+18-18| ... | @@ -15,20 +15,20 @@ pub const Block = struct { | ... | @@ -15,20 +15,20 @@ pub const Block = struct { |
| 15 | 15 | ||
| 16 | /// Convert a byte sequence into an internal representation. | 16 | /// Convert a byte sequence into an internal representation. |
| 17 | pub inline fn fromBytes(bytes: *const [16]u8) Block { | 17 | pub inline fn fromBytes(bytes: *const [16]u8) Block { |
| 18 | const s0 = mem.readIntLittle(u32, bytes[0..4]); | 18 | const s0 = mem.readInt(u32, bytes[0..4], .little); |
| 19 | const s1 = mem.readIntLittle(u32, bytes[4..8]); | 19 | const s1 = mem.readInt(u32, bytes[4..8], .little); |
| 20 | const s2 = mem.readIntLittle(u32, bytes[8..12]); | 20 | const s2 = mem.readInt(u32, bytes[8..12], .little); |
| 21 | const s3 = mem.readIntLittle(u32, bytes[12..16]); | 21 | const s3 = mem.readInt(u32, bytes[12..16], .little); |
| 22 | return Block{ .repr = BlockVec{ s0, s1, s2, s3 } }; | 22 | return Block{ .repr = BlockVec{ s0, s1, s2, s3 } }; |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | /// Convert the internal representation of a block into a byte sequence. | 25 | /// Convert the internal representation of a block into a byte sequence. |
| 26 | pub inline fn toBytes(block: Block) [16]u8 { | 26 | pub inline fn toBytes(block: Block) [16]u8 { |
| 27 | var bytes: [16]u8 = undefined; | 27 | var bytes: [16]u8 = undefined; |
| 28 | mem.writeIntLittle(u32, bytes[0..4], block.repr[0]); | 28 | mem.writeInt(u32, bytes[0..4], block.repr[0], .little); |
| 29 | mem.writeIntLittle(u32, bytes[4..8], block.repr[1]); | 29 | mem.writeInt(u32, bytes[4..8], block.repr[1], .little); |
| 30 | mem.writeIntLittle(u32, bytes[8..12], block.repr[2]); | 30 | mem.writeInt(u32, bytes[8..12], block.repr[2], .little); |
| 31 | mem.writeIntLittle(u32, bytes[12..16], block.repr[3]); | 31 | mem.writeInt(u32, bytes[12..16], block.repr[3], .little); |
| 32 | return bytes; | 32 | return bytes; |
| 33 | } | 33 | } |
| 34 | 34 | ||
| ... | @@ -123,13 +123,13 @@ pub const Block = struct { | ... | @@ -123,13 +123,13 @@ pub const Block = struct { |
| 123 | // Last round uses s-box directly and XORs to produce output. | 123 | // Last round uses s-box directly and XORs to produce output. |
| 124 | var x: [4]u8 = undefined; | 124 | var x: [4]u8 = undefined; |
| 125 | x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s0)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s3 >> 24))); | 125 | x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s0)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s3 >> 24))); |
| 126 | var t0 = mem.readIntLittle(u32, &x); | 126 | var t0 = mem.readInt(u32, &x, .little); |
| 127 | x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s1)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s0 >> 24))); | 127 | x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s1)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s0 >> 24))); |
| 128 | var t1 = mem.readIntLittle(u32, &x); | 128 | var t1 = mem.readInt(u32, &x, .little); |
| 129 | x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s2)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s1 >> 24))); | 129 | x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s2)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s1 >> 24))); |
| 130 | var t2 = mem.readIntLittle(u32, &x); | 130 | var t2 = mem.readInt(u32, &x, .little); |
| 131 | x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s3)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s2 >> 24))); | 131 | x = sbox_lookup(&sbox_encrypt, @as(u8, @truncate(s3)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s2 >> 24))); |
| 132 | var t3 = mem.readIntLittle(u32, &x); | 132 | var t3 = mem.readInt(u32, &x, .little); |
| 133 | 133 | ||
| 134 | t0 ^= round_key.repr[0]; | 134 | t0 ^= round_key.repr[0]; |
| 135 | t1 ^= round_key.repr[1]; | 135 | t1 ^= round_key.repr[1]; |
| ... | @@ -219,13 +219,13 @@ pub const Block = struct { | ... | @@ -219,13 +219,13 @@ pub const Block = struct { |
| 219 | // Last round uses s-box directly and XORs to produce output. | 219 | // Last round uses s-box directly and XORs to produce output. |
| 220 | var x: [4]u8 = undefined; | 220 | var x: [4]u8 = undefined; |
| 221 | x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s0)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s1 >> 24))); | 221 | x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s0)), @as(u8, @truncate(s3 >> 8)), @as(u8, @truncate(s2 >> 16)), @as(u8, @truncate(s1 >> 24))); |
| 222 | var t0 = mem.readIntLittle(u32, &x); | 222 | var t0 = mem.readInt(u32, &x, .little); |
| 223 | x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s1)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s2 >> 24))); | 223 | x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s1)), @as(u8, @truncate(s0 >> 8)), @as(u8, @truncate(s3 >> 16)), @as(u8, @truncate(s2 >> 24))); |
| 224 | var t1 = mem.readIntLittle(u32, &x); | 224 | var t1 = mem.readInt(u32, &x, .little); |
| 225 | x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s2)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s3 >> 24))); | 225 | x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s2)), @as(u8, @truncate(s1 >> 8)), @as(u8, @truncate(s0 >> 16)), @as(u8, @truncate(s3 >> 24))); |
| 226 | var t2 = mem.readIntLittle(u32, &x); | 226 | var t2 = mem.readInt(u32, &x, .little); |
| 227 | x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s3)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s0 >> 24))); | 227 | x = sbox_lookup(&sbox_decrypt, @as(u8, @truncate(s3)), @as(u8, @truncate(s2 >> 8)), @as(u8, @truncate(s1 >> 16)), @as(u8, @truncate(s0 >> 24))); |
| 228 | var t3 = mem.readIntLittle(u32, &x); | 228 | var t3 = mem.readInt(u32, &x, .little); |
| 229 | 229 | ||
| 230 | t0 ^= round_key.repr[0]; | 230 | t0 ^= round_key.repr[0]; |
| 231 | t1 ^= round_key.repr[1]; | 231 | t1 ^= round_key.repr[1]; |
| ... | @@ -349,14 +349,14 @@ fn KeySchedule(comptime Aes: type) type { | ... | @@ -349,14 +349,14 @@ fn KeySchedule(comptime Aes: type) type { |
| 349 | // Apply sbox_encrypt to each byte in w. | 349 | // Apply sbox_encrypt to each byte in w. |
| 350 | fn func(w: u32) u32 { | 350 | fn func(w: u32) u32 { |
| 351 | const x = sbox_lookup(&sbox_key_schedule, @as(u8, @truncate(w)), @as(u8, @truncate(w >> 8)), @as(u8, @truncate(w >> 16)), @as(u8, @truncate(w >> 24))); | 351 | const x = sbox_lookup(&sbox_key_schedule, @as(u8, @truncate(w)), @as(u8, @truncate(w >> 8)), @as(u8, @truncate(w >> 16)), @as(u8, @truncate(w >> 24))); |
| 352 | return mem.readIntLittle(u32, &x); | 352 | return mem.readInt(u32, &x, .little); |
| 353 | } | 353 | } |
| 354 | }.func; | 354 | }.func; |
| 355 | 355 | ||
| 356 | var round_keys: [rounds + 1]Block = undefined; | 356 | var round_keys: [rounds + 1]Block = undefined; |
| 357 | comptime var i: usize = 0; | 357 | comptime var i: usize = 0; |
| 358 | inline while (i < words_in_key) : (i += 1) { | 358 | inline while (i < words_in_key) : (i += 1) { |
| 359 | round_keys[i / 4].repr[i % 4] = mem.readIntBig(u32, key[4 * i ..][0..4]); | 359 | round_keys[i / 4].repr[i % 4] = mem.readInt(u32, key[4 * i ..][0..4], .big); |
| 360 | } | 360 | } |
| 361 | inline while (i < round_keys.len * 4) : (i += 1) { | 361 | inline while (i < round_keys.len * 4) : (i += 1) { |
| 362 | var t = round_keys[(i - 1) / 4].repr[(i - 1) % 4]; | 362 | var t = round_keys[(i - 1) / 4].repr[(i - 1) % 4]; |
lib/std/crypto/aes_gcm.zig+10-10| ... | @@ -33,7 +33,7 @@ fn AesGcm(comptime Aes: anytype) type { | ... | @@ -33,7 +33,7 @@ fn AesGcm(comptime Aes: anytype) type { |
| 33 | var t: [16]u8 = undefined; | 33 | var t: [16]u8 = undefined; |
| 34 | var j: [16]u8 = undefined; | 34 | var j: [16]u8 = undefined; |
| 35 | j[0..nonce_length].* = npub; | 35 | j[0..nonce_length].* = npub; |
| 36 | mem.writeIntBig(u32, j[nonce_length..][0..4], 1); | 36 | mem.writeInt(u32, j[nonce_length..][0..4], 1, .big); |
| 37 | aes.encrypt(&t, &j); | 37 | aes.encrypt(&t, &j); |
| 38 | 38 | ||
| 39 | const block_count = (math.divCeil(usize, ad.len, Ghash.block_length) catch unreachable) + (math.divCeil(usize, c.len, Ghash.block_length) catch unreachable) + 1; | 39 | const block_count = (math.divCeil(usize, ad.len, Ghash.block_length) catch unreachable) + (math.divCeil(usize, c.len, Ghash.block_length) catch unreachable) + 1; |
| ... | @@ -41,14 +41,14 @@ fn AesGcm(comptime Aes: anytype) type { | ... | @@ -41,14 +41,14 @@ fn AesGcm(comptime Aes: anytype) type { |
| 41 | mac.update(ad); | 41 | mac.update(ad); |
| 42 | mac.pad(); | 42 | mac.pad(); |
| 43 | 43 | ||
| 44 | mem.writeIntBig(u32, j[nonce_length..][0..4], 2); | 44 | mem.writeInt(u32, j[nonce_length..][0..4], 2, .big); |
| 45 | modes.ctr(@TypeOf(aes), aes, c, m, j, std.builtin.Endian.Big); | 45 | modes.ctr(@TypeOf(aes), aes, c, m, j, std.builtin.Endian.big); |
| 46 | mac.update(c[0..m.len][0..]); | 46 | mac.update(c[0..m.len][0..]); |
| 47 | mac.pad(); | 47 | mac.pad(); |
| 48 | 48 | ||
| 49 | var final_block = h; | 49 | var final_block = h; |
| 50 | mem.writeIntBig(u64, final_block[0..8], ad.len * 8); | 50 | mem.writeInt(u64, final_block[0..8], ad.len * 8, .big); |
| 51 | mem.writeIntBig(u64, final_block[8..16], m.len * 8); | 51 | mem.writeInt(u64, final_block[8..16], m.len * 8, .big); |
| 52 | mac.update(&final_block); | 52 | mac.update(&final_block); |
| 53 | mac.final(tag); | 53 | mac.final(tag); |
| 54 | for (t, 0..) |x, i| { | 54 | for (t, 0..) |x, i| { |
| ... | @@ -75,7 +75,7 @@ fn AesGcm(comptime Aes: anytype) type { | ... | @@ -75,7 +75,7 @@ fn AesGcm(comptime Aes: anytype) type { |
| 75 | var t: [16]u8 = undefined; | 75 | var t: [16]u8 = undefined; |
| 76 | var j: [16]u8 = undefined; | 76 | var j: [16]u8 = undefined; |
| 77 | j[0..nonce_length].* = npub; | 77 | j[0..nonce_length].* = npub; |
| 78 | mem.writeIntBig(u32, j[nonce_length..][0..4], 1); | 78 | mem.writeInt(u32, j[nonce_length..][0..4], 1, .big); |
| 79 | aes.encrypt(&t, &j); | 79 | aes.encrypt(&t, &j); |
| 80 | 80 | ||
| 81 | const block_count = (math.divCeil(usize, ad.len, Ghash.block_length) catch unreachable) + (math.divCeil(usize, c.len, Ghash.block_length) catch unreachable) + 1; | 81 | const block_count = (math.divCeil(usize, ad.len, Ghash.block_length) catch unreachable) + (math.divCeil(usize, c.len, Ghash.block_length) catch unreachable) + 1; |
| ... | @@ -87,8 +87,8 @@ fn AesGcm(comptime Aes: anytype) type { | ... | @@ -87,8 +87,8 @@ fn AesGcm(comptime Aes: anytype) type { |
| 87 | mac.pad(); | 87 | mac.pad(); |
| 88 | 88 | ||
| 89 | var final_block = h; | 89 | var final_block = h; |
| 90 | mem.writeIntBig(u64, final_block[0..8], ad.len * 8); | 90 | mem.writeInt(u64, final_block[0..8], ad.len * 8, .big); |
| 91 | mem.writeIntBig(u64, final_block[8..16], m.len * 8); | 91 | mem.writeInt(u64, final_block[8..16], m.len * 8, .big); |
| 92 | mac.update(&final_block); | 92 | mac.update(&final_block); |
| 93 | var computed_tag: [Ghash.mac_length]u8 = undefined; | 93 | var computed_tag: [Ghash.mac_length]u8 = undefined; |
| 94 | mac.final(&computed_tag); | 94 | mac.final(&computed_tag); |
| ... | @@ -103,8 +103,8 @@ fn AesGcm(comptime Aes: anytype) type { | ... | @@ -103,8 +103,8 @@ fn AesGcm(comptime Aes: anytype) type { |
| 103 | return error.AuthenticationFailed; | 103 | return error.AuthenticationFailed; |
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | mem.writeIntBig(u32, j[nonce_length..][0..4], 2); | 106 | mem.writeInt(u32, j[nonce_length..][0..4], 2, .big); |
| 107 | modes.ctr(@TypeOf(aes), aes, m, c, j, std.builtin.Endian.Big); | 107 | modes.ctr(@TypeOf(aes), aes, m, c, j, std.builtin.Endian.big); |
| 108 | } | 108 | } |
| 109 | }; | 109 | }; |
| 110 | } | 110 | } |
lib/std/crypto/aes_ocb.zig+4-4| ... | @@ -29,10 +29,10 @@ fn AesOcb(comptime Aes: anytype) type { | ... | @@ -29,10 +29,10 @@ fn AesOcb(comptime Aes: anytype) type { |
| 29 | upto: usize, | 29 | upto: usize, |
| 30 | 30 | ||
| 31 | inline fn double(l: Block) Block { | 31 | inline fn double(l: Block) Block { |
| 32 | const l_ = mem.readIntBig(u128, &l); | 32 | const l_ = mem.readInt(u128, &l, .big); |
| 33 | const l_2 = (l_ << 1) ^ (0x87 & -%(l_ >> 127)); | 33 | const l_2 = (l_ << 1) ^ (0x87 & -%(l_ >> 127)); |
| 34 | var l2: Block = undefined; | 34 | var l2: Block = undefined; |
| 35 | mem.writeIntBig(u128, &l2, l_2); | 35 | mem.writeInt(u128, &l2, l_2, .big); |
| 36 | return l2; | 36 | return l2; |
| 37 | } | 37 | } |
| 38 | 38 | ||
| ... | @@ -94,10 +94,10 @@ fn AesOcb(comptime Aes: anytype) type { | ... | @@ -94,10 +94,10 @@ fn AesOcb(comptime Aes: anytype) type { |
| 94 | nx[15] &= 0xc0; | 94 | nx[15] &= 0xc0; |
| 95 | var ktop_: Block = undefined; | 95 | var ktop_: Block = undefined; |
| 96 | aes_enc_ctx.encrypt(&ktop_, &nx); | 96 | aes_enc_ctx.encrypt(&ktop_, &nx); |
| 97 | const ktop = mem.readIntBig(u128, &ktop_); | 97 | const ktop = mem.readInt(u128, &ktop_, .big); |
| 98 | var stretch = (@as(u192, ktop) << 64) | @as(u192, @as(u64, @truncate(ktop >> 64)) ^ @as(u64, @truncate(ktop >> 56))); | 98 | var stretch = (@as(u192, ktop) << 64) | @as(u192, @as(u64, @truncate(ktop >> 64)) ^ @as(u64, @truncate(ktop >> 56))); |
| 99 | var offset: Block = undefined; | 99 | var offset: Block = undefined; |
| 100 | mem.writeIntBig(u128, &offset, @as(u128, @truncate(stretch >> (64 - @as(u7, bottom))))); | 100 | mem.writeInt(u128, &offset, @as(u128, @truncate(stretch >> (64 - @as(u7, bottom)))), .big); |
| 101 | return offset; | 101 | return offset; |
| 102 | } | 102 | } |
| 103 | 103 |
lib/std/crypto/argon2.zig+17-17| ... | @@ -110,27 +110,27 @@ fn initHash( | ... | @@ -110,27 +110,27 @@ fn initHash( |
| 110 | var parameters: [24]u8 = undefined; | 110 | var parameters: [24]u8 = undefined; |
| 111 | var tmp: [4]u8 = undefined; | 111 | var tmp: [4]u8 = undefined; |
| 112 | var b2 = Blake2b512.init(.{}); | 112 | var b2 = Blake2b512.init(.{}); |
| 113 | mem.writeIntLittle(u32, parameters[0..4], params.p); | 113 | mem.writeInt(u32, parameters[0..4], params.p, .little); |
| 114 | mem.writeIntLittle(u32, parameters[4..8], @as(u32, @intCast(dk_len))); | 114 | mem.writeInt(u32, parameters[4..8], @as(u32, @intCast(dk_len)), .little); |
| 115 | mem.writeIntLittle(u32, parameters[8..12], params.m); | 115 | mem.writeInt(u32, parameters[8..12], params.m, .little); |
| 116 | mem.writeIntLittle(u32, parameters[12..16], params.t); | 116 | mem.writeInt(u32, parameters[12..16], params.t, .little); |
| 117 | mem.writeIntLittle(u32, parameters[16..20], version); | 117 | mem.writeInt(u32, parameters[16..20], version, .little); |
| 118 | mem.writeIntLittle(u32, parameters[20..24], @intFromEnum(mode)); | 118 | mem.writeInt(u32, parameters[20..24], @intFromEnum(mode), .little); |
| 119 | b2.update(&parameters); | 119 | b2.update(&parameters); |
| 120 | mem.writeIntLittle(u32, &tmp, @as(u32, @intCast(password.len))); | 120 | mem.writeInt(u32, &tmp, @as(u32, @intCast(password.len)), .little); |
| 121 | b2.update(&tmp); | 121 | b2.update(&tmp); |
| 122 | b2.update(password); | 122 | b2.update(password); |
| 123 | mem.writeIntLittle(u32, &tmp, @as(u32, @intCast(salt.len))); | 123 | mem.writeInt(u32, &tmp, @as(u32, @intCast(salt.len)), .little); |
| 124 | b2.update(&tmp); | 124 | b2.update(&tmp); |
| 125 | b2.update(salt); | 125 | b2.update(salt); |
| 126 | const secret = params.secret orelse ""; | 126 | const secret = params.secret orelse ""; |
| 127 | std.debug.assert(secret.len <= max_int); | 127 | std.debug.assert(secret.len <= max_int); |
| 128 | mem.writeIntLittle(u32, &tmp, @as(u32, @intCast(secret.len))); | 128 | mem.writeInt(u32, &tmp, @as(u32, @intCast(secret.len)), .little); |
| 129 | b2.update(&tmp); | 129 | b2.update(&tmp); |
| 130 | b2.update(secret); | 130 | b2.update(secret); |
| 131 | const ad = params.ad orelse ""; | 131 | const ad = params.ad orelse ""; |
| 132 | std.debug.assert(ad.len <= max_int); | 132 | std.debug.assert(ad.len <= max_int); |
| 133 | mem.writeIntLittle(u32, &tmp, @as(u32, @intCast(ad.len))); | 133 | mem.writeInt(u32, &tmp, @as(u32, @intCast(ad.len)), .little); |
| 134 | b2.update(&tmp); | 134 | b2.update(&tmp); |
| 135 | b2.update(ad); | 135 | b2.update(ad); |
| 136 | b2.final(h0[0..Blake2b512.digest_length]); | 136 | b2.final(h0[0..Blake2b512.digest_length]); |
| ... | @@ -140,7 +140,7 @@ fn initHash( | ... | @@ -140,7 +140,7 @@ fn initHash( |
| 140 | fn blake2bLong(out: []u8, in: []const u8) void { | 140 | fn blake2bLong(out: []u8, in: []const u8) void { |
| 141 | const H = Blake2b512; | 141 | const H = Blake2b512; |
| 142 | var outlen_bytes: [4]u8 = undefined; | 142 | var outlen_bytes: [4]u8 = undefined; |
| 143 | mem.writeIntLittle(u32, &outlen_bytes, @as(u32, @intCast(out.len))); | 143 | mem.writeInt(u32, &outlen_bytes, @as(u32, @intCast(out.len)), .little); |
| 144 | 144 | ||
| 145 | var out_buf: [H.digest_length]u8 = undefined; | 145 | var out_buf: [H.digest_length]u8 = undefined; |
| 146 | 146 | ||
| ... | @@ -183,18 +183,18 @@ fn initBlocks( | ... | @@ -183,18 +183,18 @@ fn initBlocks( |
| 183 | var lane: u24 = 0; | 183 | var lane: u24 = 0; |
| 184 | while (lane < threads) : (lane += 1) { | 184 | while (lane < threads) : (lane += 1) { |
| 185 | const j = lane * (memory / threads); | 185 | const j = lane * (memory / threads); |
| 186 | mem.writeIntLittle(u32, h0[Blake2b512.digest_length + 4 ..][0..4], lane); | 186 | mem.writeInt(u32, h0[Blake2b512.digest_length + 4 ..][0..4], lane, .little); |
| 187 | 187 | ||
| 188 | mem.writeIntLittle(u32, h0[Blake2b512.digest_length..][0..4], 0); | 188 | mem.writeInt(u32, h0[Blake2b512.digest_length..][0..4], 0, .little); |
| 189 | blake2bLong(&block0, h0); | 189 | blake2bLong(&block0, h0); |
| 190 | for (&blocks.items[j + 0], 0..) |*v, i| { | 190 | for (&blocks.items[j + 0], 0..) |*v, i| { |
| 191 | v.* = mem.readIntLittle(u64, block0[i * 8 ..][0..8]); | 191 | v.* = mem.readInt(u64, block0[i * 8 ..][0..8], .little); |
| 192 | } | 192 | } |
| 193 | 193 | ||
| 194 | mem.writeIntLittle(u32, h0[Blake2b512.digest_length..][0..4], 1); | 194 | mem.writeInt(u32, h0[Blake2b512.digest_length..][0..4], 1, .little); |
| 195 | blake2bLong(&block0, h0); | 195 | blake2bLong(&block0, h0); |
| 196 | for (&blocks.items[j + 1], 0..) |*v, i| { | 196 | for (&blocks.items[j + 1], 0..) |*v, i| { |
| 197 | v.* = mem.readIntLittle(u64, block0[i * 8 ..][0..8]); | 197 | v.* = mem.readInt(u64, block0[i * 8 ..][0..8], .little); |
| 198 | } | 198 | } |
| 199 | } | 199 | } |
| 200 | } | 200 | } |
| ... | @@ -433,7 +433,7 @@ fn finalize( | ... | @@ -433,7 +433,7 @@ fn finalize( |
| 433 | } | 433 | } |
| 434 | var block: [1024]u8 = undefined; | 434 | var block: [1024]u8 = undefined; |
| 435 | for (blocks.items[memory - 1], 0..) |v, i| { | 435 | for (blocks.items[memory - 1], 0..) |v, i| { |
| 436 | mem.writeIntLittle(u64, block[i * 8 ..][0..8], v); | 436 | mem.writeInt(u64, block[i * 8 ..][0..8], v, .little); |
| 437 | } | 437 | } |
| 438 | blake2bLong(out, &block); | 438 | blake2bLong(out, &block); |
| 439 | } | 439 | } |
lib/std/crypto/ascon.zig+10-9| ... | @@ -8,11 +8,12 @@ | ... | @@ -8,11 +8,12 @@ |
| 8 | //! It is not meant to be used directly, but as a building block for symmetric cryptography. | 8 | //! It is not meant to be used directly, but as a building block for symmetric cryptography. |
| 9 | 9 | ||
| 10 | const std = @import("std"); | 10 | const std = @import("std"); |
| 11 | const builtin = std.builtin; | 11 | const builtin = @import("builtin"); |
| 12 | const debug = std.debug; | 12 | const debug = std.debug; |
| 13 | const mem = std.mem; | 13 | const mem = std.mem; |
| 14 | const testing = std.testing; | 14 | const testing = std.testing; |
| 15 | const rotr = std.math.rotr; | 15 | const rotr = std.math.rotr; |
| 16 | const native_endian = builtin.cpu.arch.endian(); | ||
| 16 | 17 | ||
| 17 | /// An Ascon state. | 18 | /// An Ascon state. |
| 18 | /// | 19 | /// |
| ... | @@ -20,7 +21,7 @@ const rotr = std.math.rotr; | ... | @@ -20,7 +21,7 @@ const rotr = std.math.rotr; |
| 20 | /// | 21 | /// |
| 21 | /// The NIST submission (v1.2) serializes these words as big-endian, | 22 | /// The NIST submission (v1.2) serializes these words as big-endian, |
| 22 | /// but software implementations are free to use native endianness. | 23 | /// but software implementations are free to use native endianness. |
| 23 | pub fn State(comptime endian: builtin.Endian) type { | 24 | pub fn State(comptime endian: std.builtin.Endian) type { |
| 24 | return struct { | 25 | return struct { |
| 25 | const Self = @This(); | 26 | const Self = @This(); |
| 26 | 27 | ||
| ... | @@ -95,8 +96,8 @@ pub fn State(comptime endian: builtin.Endian) type { | ... | @@ -95,8 +96,8 @@ pub fn State(comptime endian: builtin.Endian) type { |
| 95 | /// XOR a byte into the state at a given offset. | 96 | /// XOR a byte into the state at a given offset. |
| 96 | pub fn addByte(self: *Self, byte: u8, offset: usize) void { | 97 | pub fn addByte(self: *Self, byte: u8, offset: usize) void { |
| 97 | const z = switch (endian) { | 98 | const z = switch (endian) { |
| 98 | .Big => 64 - 8 - 8 * @as(u6, @truncate(offset % 8)), | 99 | .big => 64 - 8 - 8 * @as(u6, @truncate(offset % 8)), |
| 99 | .Little => 8 * @as(u6, @truncate(offset % 8)), | 100 | .little => 8 * @as(u6, @truncate(offset % 8)), |
| 100 | }; | 101 | }; |
| 101 | self.st[offset / 8] ^= @as(u64, byte) << z; | 102 | self.st[offset / 8] ^= @as(u64, byte) << z; |
| 102 | } | 103 | } |
| ... | @@ -133,14 +134,14 @@ pub fn State(comptime endian: builtin.Endian) type { | ... | @@ -133,14 +134,14 @@ pub fn State(comptime endian: builtin.Endian) type { |
| 133 | 134 | ||
| 134 | var i: usize = 0; | 135 | var i: usize = 0; |
| 135 | while (i + 8 <= in.len) : (i += 8) { | 136 | while (i + 8 <= in.len) : (i += 8) { |
| 136 | const x = mem.readIntNative(u64, in[i..][0..8]) ^ mem.nativeTo(u64, self.st[i / 8], endian); | 137 | const x = mem.readInt(u64, in[i..][0..8], native_endian) ^ mem.nativeTo(u64, self.st[i / 8], endian); |
| 137 | mem.writeIntNative(u64, out[i..][0..8], x); | 138 | mem.writeInt(u64, out[i..][0..8], x, native_endian); |
| 138 | } | 139 | } |
| 139 | if (i < in.len) { | 140 | if (i < in.len) { |
| 140 | var padded = [_]u8{0} ** 8; | 141 | var padded = [_]u8{0} ** 8; |
| 141 | @memcpy(padded[0 .. in.len - i], in[i..]); | 142 | @memcpy(padded[0 .. in.len - i], in[i..]); |
| 142 | const x = mem.readIntNative(u64, &padded) ^ mem.nativeTo(u64, self.st[i / 8], endian); | 143 | const x = mem.readInt(u64, &padded, native_endian) ^ mem.nativeTo(u64, self.st[i / 8], endian); |
| 143 | mem.writeIntNative(u64, &padded, x); | 144 | mem.writeInt(u64, &padded, x, native_endian); |
| 144 | @memcpy(out[i..], padded[0 .. in.len - i]); | 145 | @memcpy(out[i..], padded[0 .. in.len - i]); |
| 145 | } | 146 | } |
| 146 | } | 147 | } |
| ... | @@ -214,7 +215,7 @@ pub fn State(comptime endian: builtin.Endian) type { | ... | @@ -214,7 +215,7 @@ pub fn State(comptime endian: builtin.Endian) type { |
| 214 | } | 215 | } |
| 215 | 216 | ||
| 216 | test "ascon" { | 217 | test "ascon" { |
| 217 | const Ascon = State(.Big); | 218 | const Ascon = State(.big); |
| 218 | const bytes = [_]u8{0x01} ** Ascon.block_bytes; | 219 | const bytes = [_]u8{0x01} ** Ascon.block_bytes; |
| 219 | var st = Ascon.init(bytes); | 220 | var st = Ascon.init(bytes); |
| 220 | var out: [Ascon.block_bytes]u8 = undefined; | 221 | var out: [Ascon.block_bytes]u8 = undefined; |
lib/std/crypto/bcrypt.zig+2-2| ... | @@ -451,7 +451,7 @@ pub fn bcrypt( | ... | @@ -451,7 +451,7 @@ pub fn bcrypt( |
| 451 | 451 | ||
| 452 | var ct: [ct_length]u8 = undefined; | 452 | var ct: [ct_length]u8 = undefined; |
| 453 | for (cdata, 0..) |c, i| { | 453 | for (cdata, 0..) |c, i| { |
| 454 | mem.writeIntBig(u32, ct[i * 4 ..][0..4], c); | 454 | mem.writeInt(u32, ct[i * 4 ..][0..4], c, .big); |
| 455 | } | 455 | } |
| 456 | return ct[0..dk_length].*; | 456 | return ct[0..dk_length].*; |
| 457 | } | 457 | } |
| ... | @@ -547,7 +547,7 @@ const pbkdf_prf = struct { | ... | @@ -547,7 +547,7 @@ const pbkdf_prf = struct { |
| 547 | // copy out | 547 | // copy out |
| 548 | var out: [32]u8 = undefined; | 548 | var out: [32]u8 = undefined; |
| 549 | for (cdata, 0..) |v, i| { | 549 | for (cdata, 0..) |v, i| { |
| 550 | std.mem.writeIntLittle(u32, out[4 * i ..][0..4], v); | 550 | std.mem.writeInt(u32, out[4 * i ..][0..4], v, .little); |
| 551 | } | 551 | } |
| 552 | 552 | ||
| 553 | // zap | 553 | // zap |
lib/std/crypto/blake2.zig+10-10| ... | @@ -85,12 +85,12 @@ pub fn Blake2s(comptime out_bits: usize) type { | ... | @@ -85,12 +85,12 @@ pub fn Blake2s(comptime out_bits: usize) type { |
| 85 | d.buf_len = 0; | 85 | d.buf_len = 0; |
| 86 | 86 | ||
| 87 | if (options.salt) |salt| { | 87 | if (options.salt) |salt| { |
| 88 | d.h[4] ^= mem.readIntLittle(u32, salt[0..4]); | 88 | d.h[4] ^= mem.readInt(u32, salt[0..4], .little); |
| 89 | d.h[5] ^= mem.readIntLittle(u32, salt[4..8]); | 89 | d.h[5] ^= mem.readInt(u32, salt[4..8], .little); |
| 90 | } | 90 | } |
| 91 | if (options.context) |context| { | 91 | if (options.context) |context| { |
| 92 | d.h[6] ^= mem.readIntLittle(u32, context[0..4]); | 92 | d.h[6] ^= mem.readInt(u32, context[0..4], .little); |
| 93 | d.h[7] ^= mem.readIntLittle(u32, context[4..8]); | 93 | d.h[7] ^= mem.readInt(u32, context[4..8], .little); |
| 94 | } | 94 | } |
| 95 | if (key_len > 0) { | 95 | if (key_len > 0) { |
| 96 | @memset(d.buf[key_len..], 0); | 96 | @memset(d.buf[key_len..], 0); |
| ... | @@ -143,7 +143,7 @@ pub fn Blake2s(comptime out_bits: usize) type { | ... | @@ -143,7 +143,7 @@ pub fn Blake2s(comptime out_bits: usize) type { |
| 143 | var v: [16]u32 = undefined; | 143 | var v: [16]u32 = undefined; |
| 144 | 144 | ||
| 145 | for (&m, 0..) |*r, i| { | 145 | for (&m, 0..) |*r, i| { |
| 146 | r.* = mem.readIntLittle(u32, b[4 * i ..][0..4]); | 146 | r.* = mem.readInt(u32, b[4 * i ..][0..4], .little); |
| 147 | } | 147 | } |
| 148 | 148 | ||
| 149 | var k: usize = 0; | 149 | var k: usize = 0; |
| ... | @@ -521,12 +521,12 @@ pub fn Blake2b(comptime out_bits: usize) type { | ... | @@ -521,12 +521,12 @@ pub fn Blake2b(comptime out_bits: usize) type { |
| 521 | d.buf_len = 0; | 521 | d.buf_len = 0; |
| 522 | 522 | ||
| 523 | if (options.salt) |salt| { | 523 | if (options.salt) |salt| { |
| 524 | d.h[4] ^= mem.readIntLittle(u64, salt[0..8]); | 524 | d.h[4] ^= mem.readInt(u64, salt[0..8], .little); |
| 525 | d.h[5] ^= mem.readIntLittle(u64, salt[8..16]); | 525 | d.h[5] ^= mem.readInt(u64, salt[8..16], .little); |
| 526 | } | 526 | } |
| 527 | if (options.context) |context| { | 527 | if (options.context) |context| { |
| 528 | d.h[6] ^= mem.readIntLittle(u64, context[0..8]); | 528 | d.h[6] ^= mem.readInt(u64, context[0..8], .little); |
| 529 | d.h[7] ^= mem.readIntLittle(u64, context[8..16]); | 529 | d.h[7] ^= mem.readInt(u64, context[8..16], .little); |
| 530 | } | 530 | } |
| 531 | if (key_len > 0) { | 531 | if (key_len > 0) { |
| 532 | @memset(d.buf[key_len..], 0); | 532 | @memset(d.buf[key_len..], 0); |
| ... | @@ -579,7 +579,7 @@ pub fn Blake2b(comptime out_bits: usize) type { | ... | @@ -579,7 +579,7 @@ pub fn Blake2b(comptime out_bits: usize) type { |
| 579 | var v: [16]u64 = undefined; | 579 | var v: [16]u64 = undefined; |
| 580 | 580 | ||
| 581 | for (&m, 0..) |*r, i| { | 581 | for (&m, 0..) |*r, i| { |
| 582 | r.* = mem.readIntLittle(u64, b[8 * i ..][0..8]); | 582 | r.* = mem.readInt(u64, b[8 * i ..][0..8], .little); |
| 583 | } | 583 | } |
| 584 | 584 | ||
| 585 | var k: usize = 0; | 585 | var k: usize = 0; |
lib/std/crypto/blake3.zig+2-2| ... | @@ -212,7 +212,7 @@ fn first8Words(words: [16]u32) [8]u32 { | ... | @@ -212,7 +212,7 @@ fn first8Words(words: [16]u32) [8]u32 { |
| 212 | fn wordsFromLittleEndianBytes(comptime count: usize, bytes: [count * 4]u8) [count]u32 { | 212 | fn wordsFromLittleEndianBytes(comptime count: usize, bytes: [count * 4]u8) [count]u32 { |
| 213 | var words: [count]u32 = undefined; | 213 | var words: [count]u32 = undefined; |
| 214 | for (&words, 0..) |*word, i| { | 214 | for (&words, 0..) |*word, i| { |
| 215 | word.* = mem.readIntSliceLittle(u32, bytes[4 * i ..]); | 215 | word.* = mem.readInt(u32, bytes[4 * i ..][0..4], .little); |
| 216 | } | 216 | } |
| 217 | return words; | 217 | return words; |
| 218 | } | 218 | } |
| ... | @@ -252,7 +252,7 @@ const Output = struct { | ... | @@ -252,7 +252,7 @@ const Output = struct { |
| 252 | var word_counter: usize = 0; | 252 | var word_counter: usize = 0; |
| 253 | while (out_word_it.next()) |out_word| { | 253 | while (out_word_it.next()) |out_word| { |
| 254 | var word_bytes: [4]u8 = undefined; | 254 | var word_bytes: [4]u8 = undefined; |
| 255 | mem.writeIntLittle(u32, &word_bytes, words[word_counter]); | 255 | mem.writeInt(u32, &word_bytes, words[word_counter], .little); |
| 256 | @memcpy(out_word, word_bytes[0..out_word.len]); | 256 | @memcpy(out_word, word_bytes[0..out_word.len]); |
| 257 | word_counter += 1; | 257 | word_counter += 1; |
| 258 | } | 258 | } |
lib/std/crypto/chacha20.zig+73-73| ... | @@ -87,10 +87,10 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type | ... | @@ -87,10 +87,10 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type |
| 87 | switch (degree) { | 87 | switch (degree) { |
| 88 | 1 => { | 88 | 1 => { |
| 89 | const constant_le = Lane{ | 89 | const constant_le = Lane{ |
| 90 | mem.readIntLittle(u32, c[0..4]), | 90 | mem.readInt(u32, c[0..4], .little), |
| 91 | mem.readIntLittle(u32, c[4..8]), | 91 | mem.readInt(u32, c[4..8], .little), |
| 92 | mem.readIntLittle(u32, c[8..12]), | 92 | mem.readInt(u32, c[8..12], .little), |
| 93 | mem.readIntLittle(u32, c[12..16]), | 93 | mem.readInt(u32, c[12..16], .little), |
| 94 | }; | 94 | }; |
| 95 | return BlockVec{ | 95 | return BlockVec{ |
| 96 | constant_le, | 96 | constant_le, |
| ... | @@ -101,14 +101,14 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type | ... | @@ -101,14 +101,14 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type |
| 101 | }, | 101 | }, |
| 102 | 2 => { | 102 | 2 => { |
| 103 | const constant_le = Lane{ | 103 | const constant_le = Lane{ |
| 104 | mem.readIntLittle(u32, c[0..4]), | 104 | mem.readInt(u32, c[0..4], .little), |
| 105 | mem.readIntLittle(u32, c[4..8]), | 105 | mem.readInt(u32, c[4..8], .little), |
| 106 | mem.readIntLittle(u32, c[8..12]), | 106 | mem.readInt(u32, c[8..12], .little), |
| 107 | mem.readIntLittle(u32, c[12..16]), | 107 | mem.readInt(u32, c[12..16], .little), |
| 108 | mem.readIntLittle(u32, c[0..4]), | 108 | mem.readInt(u32, c[0..4], .little), |
| 109 | mem.readIntLittle(u32, c[4..8]), | 109 | mem.readInt(u32, c[4..8], .little), |
| 110 | mem.readIntLittle(u32, c[8..12]), | 110 | mem.readInt(u32, c[8..12], .little), |
| 111 | mem.readIntLittle(u32, c[12..16]), | 111 | mem.readInt(u32, c[12..16], .little), |
| 112 | }; | 112 | }; |
| 113 | const n1 = @addWithOverflow(d[0], 1); | 113 | const n1 = @addWithOverflow(d[0], 1); |
| 114 | return BlockVec{ | 114 | return BlockVec{ |
| ... | @@ -123,22 +123,22 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type | ... | @@ -123,22 +123,22 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type |
| 123 | const n2 = @addWithOverflow(d[0], 2); | 123 | const n2 = @addWithOverflow(d[0], 2); |
| 124 | const n3 = @addWithOverflow(d[0], 3); | 124 | const n3 = @addWithOverflow(d[0], 3); |
| 125 | const constant_le = Lane{ | 125 | const constant_le = Lane{ |
| 126 | mem.readIntLittle(u32, c[0..4]), | 126 | mem.readInt(u32, c[0..4], .little), |
| 127 | mem.readIntLittle(u32, c[4..8]), | 127 | mem.readInt(u32, c[4..8], .little), |
| 128 | mem.readIntLittle(u32, c[8..12]), | 128 | mem.readInt(u32, c[8..12], .little), |
| 129 | mem.readIntLittle(u32, c[12..16]), | 129 | mem.readInt(u32, c[12..16], .little), |
| 130 | mem.readIntLittle(u32, c[0..4]), | 130 | mem.readInt(u32, c[0..4], .little), |
| 131 | mem.readIntLittle(u32, c[4..8]), | 131 | mem.readInt(u32, c[4..8], .little), |
| 132 | mem.readIntLittle(u32, c[8..12]), | 132 | mem.readInt(u32, c[8..12], .little), |
| 133 | mem.readIntLittle(u32, c[12..16]), | 133 | mem.readInt(u32, c[12..16], .little), |
| 134 | mem.readIntLittle(u32, c[0..4]), | 134 | mem.readInt(u32, c[0..4], .little), |
| 135 | mem.readIntLittle(u32, c[4..8]), | 135 | mem.readInt(u32, c[4..8], .little), |
| 136 | mem.readIntLittle(u32, c[8..12]), | 136 | mem.readInt(u32, c[8..12], .little), |
| 137 | mem.readIntLittle(u32, c[12..16]), | 137 | mem.readInt(u32, c[12..16], .little), |
| 138 | mem.readIntLittle(u32, c[0..4]), | 138 | mem.readInt(u32, c[0..4], .little), |
| 139 | mem.readIntLittle(u32, c[4..8]), | 139 | mem.readInt(u32, c[4..8], .little), |
| 140 | mem.readIntLittle(u32, c[8..12]), | 140 | mem.readInt(u32, c[8..12], .little), |
| 141 | mem.readIntLittle(u32, c[12..16]), | 141 | mem.readInt(u32, c[12..16], .little), |
| 142 | }; | 142 | }; |
| 143 | return BlockVec{ | 143 | return BlockVec{ |
| 144 | constant_le, | 144 | constant_le, |
| ... | @@ -218,10 +218,10 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type | ... | @@ -218,10 +218,10 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type |
| 218 | inline fn hashToBytes(comptime dm: usize, out: *[64 * dm]u8, x: BlockVec) void { | 218 | inline fn hashToBytes(comptime dm: usize, out: *[64 * dm]u8, x: BlockVec) void { |
| 219 | for (0..dm) |d| { | 219 | for (0..dm) |d| { |
| 220 | for (0..4) |i| { | 220 | for (0..4) |i| { |
| 221 | mem.writeIntLittle(u32, out[64 * d + 16 * i + 0 ..][0..4], x[i][0 + 4 * d]); | 221 | mem.writeInt(u32, out[64 * d + 16 * i + 0 ..][0..4], x[i][0 + 4 * d], .little); |
| 222 | mem.writeIntLittle(u32, out[64 * d + 16 * i + 4 ..][0..4], x[i][1 + 4 * d]); | 222 | mem.writeInt(u32, out[64 * d + 16 * i + 4 ..][0..4], x[i][1 + 4 * d], .little); |
| 223 | mem.writeIntLittle(u32, out[64 * d + 16 * i + 8 ..][0..4], x[i][2 + 4 * d]); | 223 | mem.writeInt(u32, out[64 * d + 16 * i + 8 ..][0..4], x[i][2 + 4 * d], .little); |
| 224 | mem.writeIntLittle(u32, out[64 * d + 16 * i + 12 ..][0..4], x[i][3 + 4 * d]); | 224 | mem.writeInt(u32, out[64 * d + 16 * i + 12 ..][0..4], x[i][3 + 4 * d], .little); |
| 225 | } | 225 | } |
| 226 | } | 226 | } |
| 227 | } | 227 | } |
| ... | @@ -309,20 +309,20 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type | ... | @@ -309,20 +309,20 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type |
| 309 | fn hchacha20(input: [16]u8, key: [32]u8) [32]u8 { | 309 | fn hchacha20(input: [16]u8, key: [32]u8) [32]u8 { |
| 310 | var c: [4]u32 = undefined; | 310 | var c: [4]u32 = undefined; |
| 311 | for (c, 0..) |_, i| { | 311 | for (c, 0..) |_, i| { |
| 312 | c[i] = mem.readIntLittle(u32, input[4 * i ..][0..4]); | 312 | c[i] = mem.readInt(u32, input[4 * i ..][0..4], .little); |
| 313 | } | 313 | } |
| 314 | const ctx = initContext(keyToWords(key), c); | 314 | const ctx = initContext(keyToWords(key), c); |
| 315 | var x: BlockVec = undefined; | 315 | var x: BlockVec = undefined; |
| 316 | chacha20Core(x[0..], ctx); | 316 | chacha20Core(x[0..], ctx); |
| 317 | var out: [32]u8 = undefined; | 317 | var out: [32]u8 = undefined; |
| 318 | mem.writeIntLittle(u32, out[0..4], x[0][0]); | 318 | mem.writeInt(u32, out[0..4], x[0][0], .little); |
| 319 | mem.writeIntLittle(u32, out[4..8], x[0][1]); | 319 | mem.writeInt(u32, out[4..8], x[0][1], .little); |
| 320 | mem.writeIntLittle(u32, out[8..12], x[0][2]); | 320 | mem.writeInt(u32, out[8..12], x[0][2], .little); |
| 321 | mem.writeIntLittle(u32, out[12..16], x[0][3]); | 321 | mem.writeInt(u32, out[12..16], x[0][3], .little); |
| 322 | mem.writeIntLittle(u32, out[16..20], x[3][0]); | 322 | mem.writeInt(u32, out[16..20], x[3][0], .little); |
| 323 | mem.writeIntLittle(u32, out[20..24], x[3][1]); | 323 | mem.writeInt(u32, out[20..24], x[3][1], .little); |
| 324 | mem.writeIntLittle(u32, out[24..28], x[3][2]); | 324 | mem.writeInt(u32, out[24..28], x[3][2], .little); |
| 325 | mem.writeIntLittle(u32, out[28..32], x[3][3]); | 325 | mem.writeInt(u32, out[28..32], x[3][3], .little); |
| 326 | return out; | 326 | return out; |
| 327 | } | 327 | } |
| 328 | }; | 328 | }; |
| ... | @@ -336,10 +336,10 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type { | ... | @@ -336,10 +336,10 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type { |
| 336 | fn initContext(key: [8]u32, d: [4]u32) BlockVec { | 336 | fn initContext(key: [8]u32, d: [4]u32) BlockVec { |
| 337 | const c = "expand 32-byte k"; | 337 | const c = "expand 32-byte k"; |
| 338 | const constant_le = comptime [4]u32{ | 338 | const constant_le = comptime [4]u32{ |
| 339 | mem.readIntLittle(u32, c[0..4]), | 339 | mem.readInt(u32, c[0..4], .little), |
| 340 | mem.readIntLittle(u32, c[4..8]), | 340 | mem.readInt(u32, c[4..8], .little), |
| 341 | mem.readIntLittle(u32, c[8..12]), | 341 | mem.readInt(u32, c[8..12], .little), |
| 342 | mem.readIntLittle(u32, c[12..16]), | 342 | mem.readInt(u32, c[12..16], .little), |
| 343 | }; | 343 | }; |
| 344 | return BlockVec{ | 344 | return BlockVec{ |
| 345 | constant_le[0], constant_le[1], constant_le[2], constant_le[3], | 345 | constant_le[0], constant_le[1], constant_le[2], constant_le[3], |
| ... | @@ -396,10 +396,10 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type { | ... | @@ -396,10 +396,10 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type { |
| 396 | 396 | ||
| 397 | inline fn hashToBytes(out: *[64]u8, x: BlockVec) void { | 397 | inline fn hashToBytes(out: *[64]u8, x: BlockVec) void { |
| 398 | for (0..4) |i| { | 398 | for (0..4) |i| { |
| 399 | mem.writeIntLittle(u32, out[16 * i + 0 ..][0..4], x[i * 4 + 0]); | 399 | mem.writeInt(u32, out[16 * i + 0 ..][0..4], x[i * 4 + 0], .little); |
| 400 | mem.writeIntLittle(u32, out[16 * i + 4 ..][0..4], x[i * 4 + 1]); | 400 | mem.writeInt(u32, out[16 * i + 4 ..][0..4], x[i * 4 + 1], .little); |
| 401 | mem.writeIntLittle(u32, out[16 * i + 8 ..][0..4], x[i * 4 + 2]); | 401 | mem.writeInt(u32, out[16 * i + 8 ..][0..4], x[i * 4 + 2], .little); |
| 402 | mem.writeIntLittle(u32, out[16 * i + 12 ..][0..4], x[i * 4 + 3]); | 402 | mem.writeInt(u32, out[16 * i + 12 ..][0..4], x[i * 4 + 3], .little); |
| 403 | } | 403 | } |
| 404 | } | 404 | } |
| 405 | 405 | ||
| ... | @@ -477,20 +477,20 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type { | ... | @@ -477,20 +477,20 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type { |
| 477 | fn hchacha20(input: [16]u8, key: [32]u8) [32]u8 { | 477 | fn hchacha20(input: [16]u8, key: [32]u8) [32]u8 { |
| 478 | var c: [4]u32 = undefined; | 478 | var c: [4]u32 = undefined; |
| 479 | for (c, 0..) |_, i| { | 479 | for (c, 0..) |_, i| { |
| 480 | c[i] = mem.readIntLittle(u32, input[4 * i ..][0..4]); | 480 | c[i] = mem.readInt(u32, input[4 * i ..][0..4], .little); |
| 481 | } | 481 | } |
| 482 | const ctx = initContext(keyToWords(key), c); | 482 | const ctx = initContext(keyToWords(key), c); |
| 483 | var x: BlockVec = undefined; | 483 | var x: BlockVec = undefined; |
| 484 | chacha20Core(x[0..], ctx); | 484 | chacha20Core(x[0..], ctx); |
| 485 | var out: [32]u8 = undefined; | 485 | var out: [32]u8 = undefined; |
| 486 | mem.writeIntLittle(u32, out[0..4], x[0]); | 486 | mem.writeInt(u32, out[0..4], x[0], .little); |
| 487 | mem.writeIntLittle(u32, out[4..8], x[1]); | 487 | mem.writeInt(u32, out[4..8], x[1], .little); |
| 488 | mem.writeIntLittle(u32, out[8..12], x[2]); | 488 | mem.writeInt(u32, out[8..12], x[2], .little); |
| 489 | mem.writeIntLittle(u32, out[12..16], x[3]); | 489 | mem.writeInt(u32, out[12..16], x[3], .little); |
| 490 | mem.writeIntLittle(u32, out[16..20], x[12]); | 490 | mem.writeInt(u32, out[16..20], x[12], .little); |
| 491 | mem.writeIntLittle(u32, out[20..24], x[13]); | 491 | mem.writeInt(u32, out[20..24], x[13], .little); |
| 492 | mem.writeIntLittle(u32, out[24..28], x[14]); | 492 | mem.writeInt(u32, out[24..28], x[14], .little); |
| 493 | mem.writeIntLittle(u32, out[28..32], x[15]); | 493 | mem.writeInt(u32, out[28..32], x[15], .little); |
| 494 | return out; | 494 | return out; |
| 495 | } | 495 | } |
| 496 | }; | 496 | }; |
| ... | @@ -519,7 +519,7 @@ fn ChaChaImpl(comptime rounds_nb: usize) type { | ... | @@ -519,7 +519,7 @@ fn ChaChaImpl(comptime rounds_nb: usize) type { |
| 519 | fn keyToWords(key: [32]u8) [8]u32 { | 519 | fn keyToWords(key: [32]u8) [8]u32 { |
| 520 | var k: [8]u32 = undefined; | 520 | var k: [8]u32 = undefined; |
| 521 | for (0..8) |i| { | 521 | for (0..8) |i| { |
| 522 | k[i] = mem.readIntLittle(u32, key[i * 4 ..][0..4]); | 522 | k[i] = mem.readInt(u32, key[i * 4 ..][0..4], .little); |
| 523 | } | 523 | } |
| 524 | return k; | 524 | return k; |
| 525 | } | 525 | } |
| ... | @@ -552,9 +552,9 @@ fn ChaChaIETF(comptime rounds_nb: usize) type { | ... | @@ -552,9 +552,9 @@ fn ChaChaIETF(comptime rounds_nb: usize) type { |
| 552 | 552 | ||
| 553 | var d: [4]u32 = undefined; | 553 | var d: [4]u32 = undefined; |
| 554 | d[0] = counter; | 554 | d[0] = counter; |
| 555 | d[1] = mem.readIntLittle(u32, nonce[0..4]); | 555 | d[1] = mem.readInt(u32, nonce[0..4], .little); |
| 556 | d[2] = mem.readIntLittle(u32, nonce[4..8]); | 556 | d[2] = mem.readInt(u32, nonce[4..8], .little); |
| 557 | d[3] = mem.readIntLittle(u32, nonce[8..12]); | 557 | d[3] = mem.readInt(u32, nonce[8..12], .little); |
| 558 | ChaChaImpl(rounds_nb).chacha20Xor(out, in, keyToWords(key), d, false); | 558 | ChaChaImpl(rounds_nb).chacha20Xor(out, in, keyToWords(key), d, false); |
| 559 | } | 559 | } |
| 560 | 560 | ||
| ... | @@ -564,9 +564,9 @@ fn ChaChaIETF(comptime rounds_nb: usize) type { | ... | @@ -564,9 +564,9 @@ fn ChaChaIETF(comptime rounds_nb: usize) type { |
| 564 | 564 | ||
| 565 | var d: [4]u32 = undefined; | 565 | var d: [4]u32 = undefined; |
| 566 | d[0] = counter; | 566 | d[0] = counter; |
| 567 | d[1] = mem.readIntLittle(u32, nonce[0..4]); | 567 | d[1] = mem.readInt(u32, nonce[0..4], .little); |
| 568 | d[2] = mem.readIntLittle(u32, nonce[4..8]); | 568 | d[2] = mem.readInt(u32, nonce[4..8], .little); |
| 569 | d[3] = mem.readIntLittle(u32, nonce[8..12]); | 569 | d[3] = mem.readInt(u32, nonce[8..12], .little); |
| 570 | ChaChaImpl(rounds_nb).chacha20Stream(out, keyToWords(key), d, false); | 570 | ChaChaImpl(rounds_nb).chacha20Stream(out, keyToWords(key), d, false); |
| 571 | } | 571 | } |
| 572 | }; | 572 | }; |
| ... | @@ -592,8 +592,8 @@ fn ChaChaWith64BitNonce(comptime rounds_nb: usize) type { | ... | @@ -592,8 +592,8 @@ fn ChaChaWith64BitNonce(comptime rounds_nb: usize) type { |
| 592 | var c: [4]u32 = undefined; | 592 | var c: [4]u32 = undefined; |
| 593 | c[0] = @as(u32, @truncate(counter)); | 593 | c[0] = @as(u32, @truncate(counter)); |
| 594 | c[1] = @as(u32, @truncate(counter >> 32)); | 594 | c[1] = @as(u32, @truncate(counter >> 32)); |
| 595 | c[2] = mem.readIntLittle(u32, nonce[0..4]); | 595 | c[2] = mem.readInt(u32, nonce[0..4], .little); |
| 596 | c[3] = mem.readIntLittle(u32, nonce[4..8]); | 596 | c[3] = mem.readInt(u32, nonce[4..8], .little); |
| 597 | ChaChaImpl(rounds_nb).chacha20Xor(out, in, k, c, true); | 597 | ChaChaImpl(rounds_nb).chacha20Xor(out, in, k, c, true); |
| 598 | } | 598 | } |
| 599 | 599 | ||
| ... | @@ -605,8 +605,8 @@ fn ChaChaWith64BitNonce(comptime rounds_nb: usize) type { | ... | @@ -605,8 +605,8 @@ fn ChaChaWith64BitNonce(comptime rounds_nb: usize) type { |
| 605 | var c: [4]u32 = undefined; | 605 | var c: [4]u32 = undefined; |
| 606 | c[0] = @as(u32, @truncate(counter)); | 606 | c[0] = @as(u32, @truncate(counter)); |
| 607 | c[1] = @as(u32, @truncate(counter >> 32)); | 607 | c[1] = @as(u32, @truncate(counter >> 32)); |
| 608 | c[2] = mem.readIntLittle(u32, nonce[0..4]); | 608 | c[2] = mem.readInt(u32, nonce[0..4], .little); |
| 609 | c[3] = mem.readIntLittle(u32, nonce[4..8]); | 609 | c[3] = mem.readInt(u32, nonce[4..8], .little); |
| 610 | ChaChaImpl(rounds_nb).chacha20Stream(out, k, c, true); | 610 | ChaChaImpl(rounds_nb).chacha20Stream(out, k, c, true); |
| 611 | } | 611 | } |
| 612 | }; | 612 | }; |
| ... | @@ -672,8 +672,8 @@ fn ChaChaPoly1305(comptime rounds_nb: usize) type { | ... | @@ -672,8 +672,8 @@ fn ChaChaPoly1305(comptime rounds_nb: usize) type { |
| 672 | mac.update(zeros[0..padding]); | 672 | mac.update(zeros[0..padding]); |
| 673 | } | 673 | } |
| 674 | var lens: [16]u8 = undefined; | 674 | var lens: [16]u8 = undefined; |
| 675 | mem.writeIntLittle(u64, lens[0..8], ad.len); | 675 | mem.writeInt(u64, lens[0..8], ad.len, .little); |
| 676 | mem.writeIntLittle(u64, lens[8..16], m.len); | 676 | mem.writeInt(u64, lens[8..16], m.len, .little); |
| 677 | mac.update(lens[0..]); | 677 | mac.update(lens[0..]); |
| 678 | mac.final(tag); | 678 | mac.final(tag); |
| 679 | } | 679 | } |
| ... | @@ -708,8 +708,8 @@ fn ChaChaPoly1305(comptime rounds_nb: usize) type { | ... | @@ -708,8 +708,8 @@ fn ChaChaPoly1305(comptime rounds_nb: usize) type { |
| 708 | mac.update(zeros[0..padding]); | 708 | mac.update(zeros[0..padding]); |
| 709 | } | 709 | } |
| 710 | var lens: [16]u8 = undefined; | 710 | var lens: [16]u8 = undefined; |
| 711 | mem.writeIntLittle(u64, lens[0..8], ad.len); | 711 | mem.writeInt(u64, lens[0..8], ad.len, .little); |
| 712 | mem.writeIntLittle(u64, lens[8..16], c.len); | 712 | mem.writeInt(u64, lens[8..16], c.len, .little); |
| 713 | mac.update(lens[0..]); | 713 | mac.update(lens[0..]); |
| 714 | var computed_tag: [16]u8 = undefined; | 714 | var computed_tag: [16]u8 = undefined; |
| 715 | mac.final(computed_tag[0..]); | 715 | mac.final(computed_tag[0..]); |
lib/std/crypto/cmac.zig+2-2| ... | @@ -76,7 +76,7 @@ pub fn Cmac(comptime BlockCipher: type) type { | ... | @@ -76,7 +76,7 @@ pub fn Cmac(comptime BlockCipher: type) type { |
| 76 | 76 | ||
| 77 | fn double(l: Block) Block { | 77 | fn double(l: Block) Block { |
| 78 | const Int = std.meta.Int(.unsigned, block_length * 8); | 78 | const Int = std.meta.Int(.unsigned, block_length * 8); |
| 79 | const l_ = mem.readIntBig(Int, &l); | 79 | const l_ = mem.readInt(Int, &l, .big); |
| 80 | const l_2 = switch (block_length) { | 80 | const l_2 = switch (block_length) { |
| 81 | 8 => (l_ << 1) ^ (0x1b & -%(l_ >> 63)), // mod x^64 + x^4 + x^3 + x + 1 | 81 | 8 => (l_ << 1) ^ (0x1b & -%(l_ >> 63)), // mod x^64 + x^4 + x^3 + x + 1 |
| 82 | 16 => (l_ << 1) ^ (0x87 & -%(l_ >> 127)), // mod x^128 + x^7 + x^2 + x + 1 | 82 | 16 => (l_ << 1) ^ (0x87 & -%(l_ >> 127)), // mod x^128 + x^7 + x^2 + x + 1 |
| ... | @@ -85,7 +85,7 @@ pub fn Cmac(comptime BlockCipher: type) type { | ... | @@ -85,7 +85,7 @@ pub fn Cmac(comptime BlockCipher: type) type { |
| 85 | else => @compileError("unsupported block length"), | 85 | else => @compileError("unsupported block length"), |
| 86 | }; | 86 | }; |
| 87 | var l2: Block = undefined; | 87 | var l2: Block = undefined; |
| 88 | mem.writeIntBig(Int, &l2, l_2); | 88 | mem.writeInt(Int, &l2, l_2, .big); |
| 89 | return l2; | 89 | return l2; |
| 90 | } | 90 | } |
| 91 | }; | 91 | }; |
lib/std/crypto/ecdsa.zig+16-16| ... | @@ -209,17 +209,17 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { | ... | @@ -209,17 +209,17 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { |
| 209 | 209 | ||
| 210 | const k = deterministicScalar(h_slice.*, self.secret_key.bytes, self.noise); | 210 | const k = deterministicScalar(h_slice.*, self.secret_key.bytes, self.noise); |
| 211 | 211 | ||
| 212 | const p = try Curve.basePoint.mul(k.toBytes(.Big), .Big); | 212 | const p = try Curve.basePoint.mul(k.toBytes(.big), .big); |
| 213 | const xs = p.affineCoordinates().x.toBytes(.Big); | 213 | const xs = p.affineCoordinates().x.toBytes(.big); |
| 214 | const r = reduceToScalar(Curve.Fe.encoded_length, xs); | 214 | const r = reduceToScalar(Curve.Fe.encoded_length, xs); |
| 215 | if (r.isZero()) return error.IdentityElement; | 215 | if (r.isZero()) return error.IdentityElement; |
| 216 | 216 | ||
| 217 | const k_inv = k.invert(); | 217 | const k_inv = k.invert(); |
| 218 | const zrs = z.add(r.mul(try Curve.scalar.Scalar.fromBytes(self.secret_key.bytes, .Big))); | 218 | const zrs = z.add(r.mul(try Curve.scalar.Scalar.fromBytes(self.secret_key.bytes, .big))); |
| 219 | const s = k_inv.mul(zrs); | 219 | const s = k_inv.mul(zrs); |
| 220 | if (s.isZero()) return error.IdentityElement; | 220 | if (s.isZero()) return error.IdentityElement; |
| 221 | 221 | ||
| 222 | return Signature{ .r = r.toBytes(.Big), .s = s.toBytes(.Big) }; | 222 | return Signature{ .r = r.toBytes(.big), .s = s.toBytes(.big) }; |
| 223 | } | 223 | } |
| 224 | }; | 224 | }; |
| 225 | 225 | ||
| ... | @@ -232,8 +232,8 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { | ... | @@ -232,8 +232,8 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { |
| 232 | public_key: PublicKey, | 232 | public_key: PublicKey, |
| 233 | 233 | ||
| 234 | fn init(sig: Signature, public_key: PublicKey) (IdentityElementError || NonCanonicalError)!Verifier { | 234 | fn init(sig: Signature, public_key: PublicKey) (IdentityElementError || NonCanonicalError)!Verifier { |
| 235 | const r = try Curve.scalar.Scalar.fromBytes(sig.r, .Big); | 235 | const r = try Curve.scalar.Scalar.fromBytes(sig.r, .big); |
| 236 | const s = try Curve.scalar.Scalar.fromBytes(sig.s, .Big); | 236 | const s = try Curve.scalar.Scalar.fromBytes(sig.s, .big); |
| 237 | if (r.isZero() or s.isZero()) return error.IdentityElement; | 237 | if (r.isZero() or s.isZero()) return error.IdentityElement; |
| 238 | 238 | ||
| 239 | return Verifier{ | 239 | return Verifier{ |
| ... | @@ -262,11 +262,11 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { | ... | @@ -262,11 +262,11 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { |
| 262 | } | 262 | } |
| 263 | 263 | ||
| 264 | const s_inv = self.s.invert(); | 264 | const s_inv = self.s.invert(); |
| 265 | const v1 = z.mul(s_inv).toBytes(.Little); | 265 | const v1 = z.mul(s_inv).toBytes(.little); |
| 266 | const v2 = self.r.mul(s_inv).toBytes(.Little); | 266 | const v2 = self.r.mul(s_inv).toBytes(.little); |
| 267 | const v1g = try Curve.basePoint.mulPublic(v1, .Little); | 267 | const v1g = try Curve.basePoint.mulPublic(v1, .little); |
| 268 | const v2pk = try self.public_key.p.mulPublic(v2, .Little); | 268 | const v2pk = try self.public_key.p.mulPublic(v2, .little); |
| 269 | const vxs = v1g.add(v2pk).affineCoordinates().x.toBytes(.Big); | 269 | const vxs = v1g.add(v2pk).affineCoordinates().x.toBytes(.big); |
| 270 | const vr = reduceToScalar(Curve.Fe.encoded_length, vxs); | 270 | const vr = reduceToScalar(Curve.Fe.encoded_length, vxs); |
| 271 | if (!self.r.equivalent(vr)) { | 271 | if (!self.r.equivalent(vr)) { |
| 272 | return error.SignatureVerificationFailed; | 272 | return error.SignatureVerificationFailed; |
| ... | @@ -295,13 +295,13 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { | ... | @@ -295,13 +295,13 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { |
| 295 | } | 295 | } |
| 296 | const h = [_]u8{0x00} ** Hash.digest_length; | 296 | const h = [_]u8{0x00} ** Hash.digest_length; |
| 297 | const k0 = [_]u8{0x01} ** SecretKey.encoded_length; | 297 | const k0 = [_]u8{0x01} ** SecretKey.encoded_length; |
| 298 | const secret_key = deterministicScalar(h, k0, seed_).toBytes(.Big); | 298 | const secret_key = deterministicScalar(h, k0, seed_).toBytes(.big); |
| 299 | return fromSecretKey(SecretKey{ .bytes = secret_key }); | 299 | return fromSecretKey(SecretKey{ .bytes = secret_key }); |
| 300 | } | 300 | } |
| 301 | 301 | ||
| 302 | /// Return the public key corresponding to the secret key. | 302 | /// Return the public key corresponding to the secret key. |
| 303 | pub fn fromSecretKey(secret_key: SecretKey) IdentityElementError!KeyPair { | 303 | pub fn fromSecretKey(secret_key: SecretKey) IdentityElementError!KeyPair { |
| 304 | const public_key = try Curve.basePoint.mul(secret_key.bytes, .Big); | 304 | const public_key = try Curve.basePoint.mul(secret_key.bytes, .big); |
| 305 | return KeyPair{ .secret_key = secret_key, .public_key = PublicKey{ .p = public_key } }; | 305 | return KeyPair{ .secret_key = secret_key, .public_key = PublicKey{ .p = public_key } }; |
| 306 | } | 306 | } |
| 307 | 307 | ||
| ... | @@ -326,11 +326,11 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { | ... | @@ -326,11 +326,11 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { |
| 326 | if (unreduced_len >= 48) { | 326 | if (unreduced_len >= 48) { |
| 327 | var xs = [_]u8{0} ** 64; | 327 | var xs = [_]u8{0} ** 64; |
| 328 | @memcpy(xs[xs.len - s.len ..], s[0..]); | 328 | @memcpy(xs[xs.len - s.len ..], s[0..]); |
| 329 | return Curve.scalar.Scalar.fromBytes64(xs, .Big); | 329 | return Curve.scalar.Scalar.fromBytes64(xs, .big); |
| 330 | } | 330 | } |
| 331 | var xs = [_]u8{0} ** 48; | 331 | var xs = [_]u8{0} ** 48; |
| 332 | @memcpy(xs[xs.len - s.len ..], s[0..]); | 332 | @memcpy(xs[xs.len - s.len ..], s[0..]); |
| 333 | return Curve.scalar.Scalar.fromBytes48(xs, .Big); | 333 | return Curve.scalar.Scalar.fromBytes48(xs, .big); |
| 334 | } | 334 | } |
| 335 | 335 | ||
| 336 | // Create a deterministic scalar according to a secret key and optional noise. | 336 | // Create a deterministic scalar according to a secret key and optional noise. |
| ... | @@ -362,7 +362,7 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { | ... | @@ -362,7 +362,7 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { |
| 362 | Hmac.create(m_v, m_v, &k); | 362 | Hmac.create(m_v, m_v, &k); |
| 363 | @memcpy(t[t_off..t_end], m_v[0 .. t_end - t_off]); | 363 | @memcpy(t[t_off..t_end], m_v[0 .. t_end - t_off]); |
| 364 | } | 364 | } |
| 365 | if (Curve.scalar.Scalar.fromBytes(t, .Big)) |s| return s else |_| {} | 365 | if (Curve.scalar.Scalar.fromBytes(t, .big)) |s| return s else |_| {} |
| 366 | m_i.* = 0x00; | 366 | m_i.* = 0x00; |
| 367 | Hmac.create(&k, m[0 .. m_v.len + 1], &k); | 367 | Hmac.create(&k, m[0 .. m_v.len + 1], &k); |
| 368 | Hmac.create(m_v, m_v, &k); | 368 | Hmac.create(m_v, m_v, &k); |
lib/std/crypto/ff.zig+20-20| ... | @@ -137,8 +137,8 @@ pub fn Uint(comptime max_bits: comptime_int) type { | ... | @@ -137,8 +137,8 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 137 | @memset(bytes, 0); | 137 | @memset(bytes, 0); |
| 138 | var shift: usize = 0; | 138 | var shift: usize = 0; |
| 139 | var out_i: usize = switch (endian) { | 139 | var out_i: usize = switch (endian) { |
| 140 | .Big => bytes.len - 1, | 140 | .big => bytes.len - 1, |
| 141 | .Little => 0, | 141 | .little => 0, |
| 142 | }; | 142 | }; |
| 143 | for (0..self.limbs.len) |i| { | 143 | for (0..self.limbs.len) |i| { |
| 144 | var remaining_bits = t_bits; | 144 | var remaining_bits = t_bits; |
| ... | @@ -150,7 +150,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { | ... | @@ -150,7 +150,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 150 | remaining_bits -= consumed; | 150 | remaining_bits -= consumed; |
| 151 | shift = 0; | 151 | shift = 0; |
| 152 | switch (endian) { | 152 | switch (endian) { |
| 153 | .Big => { | 153 | .big => { |
| 154 | if (out_i == 0) { | 154 | if (out_i == 0) { |
| 155 | if (i != self.limbs.len - 1 or limb != 0) { | 155 | if (i != self.limbs.len - 1 or limb != 0) { |
| 156 | return error.Overflow; | 156 | return error.Overflow; |
| ... | @@ -159,7 +159,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { | ... | @@ -159,7 +159,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 159 | } | 159 | } |
| 160 | out_i -= 1; | 160 | out_i -= 1; |
| 161 | }, | 161 | }, |
| 162 | .Little => { | 162 | .little => { |
| 163 | out_i += 1; | 163 | out_i += 1; |
| 164 | if (out_i == bytes.len) { | 164 | if (out_i == bytes.len) { |
| 165 | if (i != self.limbs.len - 1 or limb != 0) { | 165 | if (i != self.limbs.len - 1 or limb != 0) { |
| ... | @@ -182,8 +182,8 @@ pub fn Uint(comptime max_bits: comptime_int) type { | ... | @@ -182,8 +182,8 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 182 | var out = Self.zero; | 182 | var out = Self.zero; |
| 183 | var out_i: usize = 0; | 183 | var out_i: usize = 0; |
| 184 | var i: usize = switch (endian) { | 184 | var i: usize = switch (endian) { |
| 185 | .Big => bytes.len - 1, | 185 | .big => bytes.len - 1, |
| 186 | .Little => 0, | 186 | .little => 0, |
| 187 | }; | 187 | }; |
| 188 | while (true) { | 188 | while (true) { |
| 189 | const bi = bytes[i]; | 189 | const bi = bytes[i]; |
| ... | @@ -203,11 +203,11 @@ pub fn Uint(comptime max_bits: comptime_int) type { | ... | @@ -203,11 +203,11 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 203 | out.limbs.set(out_i, overflow); | 203 | out.limbs.set(out_i, overflow); |
| 204 | } | 204 | } |
| 205 | switch (endian) { | 205 | switch (endian) { |
| 206 | .Big => { | 206 | .big => { |
| 207 | if (i == 0) break; | 207 | if (i == 0) break; |
| 208 | i -= 1; | 208 | i -= 1; |
| 209 | }, | 209 | }, |
| 210 | .Little => { | 210 | .little => { |
| 211 | i += 1; | 211 | i += 1; |
| 212 | if (i == bytes.len) break; | 212 | if (i == bytes.len) break; |
| 213 | }, | 213 | }, |
| ... | @@ -227,7 +227,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { | ... | @@ -227,7 +227,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 227 | Limb, | 227 | Limb, |
| 228 | x.limbs.constSlice(), | 228 | x.limbs.constSlice(), |
| 229 | y.limbs.constSlice(), | 229 | y.limbs.constSlice(), |
| 230 | .Little, | 230 | .little, |
| 231 | ); | 231 | ); |
| 232 | } | 232 | } |
| 233 | 233 | ||
| ... | @@ -667,15 +667,15 @@ pub fn Modulus(comptime max_bits: comptime_int) type { | ... | @@ -667,15 +667,15 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 667 | var out = self.one(); | 667 | var out = self.one(); |
| 668 | self.toMontgomery(&out) catch unreachable; | 668 | self.toMontgomery(&out) catch unreachable; |
| 669 | 669 | ||
| 670 | if (public and e.len < 3 or (e.len == 3 and e[if (endian == .Big) 0 else 2] <= 0b1111)) { | 670 | if (public and e.len < 3 or (e.len == 3 and e[if (endian == .big) 0 else 2] <= 0b1111)) { |
| 671 | // Do not use a precomputation table for short, public exponents | 671 | // Do not use a precomputation table for short, public exponents |
| 672 | var x_m = x; | 672 | var x_m = x; |
| 673 | if (x.montgomery == false) { | 673 | if (x.montgomery == false) { |
| 674 | self.toMontgomery(&x_m) catch unreachable; | 674 | self.toMontgomery(&x_m) catch unreachable; |
| 675 | } | 675 | } |
| 676 | var s = switch (endian) { | 676 | var s = switch (endian) { |
| 677 | .Big => 0, | 677 | .big => 0, |
| 678 | .Little => e.len - 1, | 678 | .little => e.len - 1, |
| 679 | }; | 679 | }; |
| 680 | while (true) { | 680 | while (true) { |
| 681 | const b = e[s]; | 681 | const b = e[s]; |
| ... | @@ -690,11 +690,11 @@ pub fn Modulus(comptime max_bits: comptime_int) type { | ... | @@ -690,11 +690,11 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 690 | if (j == 0) break; | 690 | if (j == 0) break; |
| 691 | } | 691 | } |
| 692 | switch (endian) { | 692 | switch (endian) { |
| 693 | .Big => { | 693 | .big => { |
| 694 | s += 1; | 694 | s += 1; |
| 695 | if (s == e.len) break; | 695 | if (s == e.len) break; |
| 696 | }, | 696 | }, |
| 697 | .Little => { | 697 | .little => { |
| 698 | if (s == 0) break; | 698 | if (s == 0) break; |
| 699 | s -= 1; | 699 | s -= 1; |
| 700 | }, | 700 | }, |
| ... | @@ -711,8 +711,8 @@ pub fn Modulus(comptime max_bits: comptime_int) type { | ... | @@ -711,8 +711,8 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 711 | } | 711 | } |
| 712 | var t0 = self.zero; | 712 | var t0 = self.zero; |
| 713 | var s = switch (endian) { | 713 | var s = switch (endian) { |
| 714 | .Big => 0, | 714 | .big => 0, |
| 715 | .Little => e.len - 1, | 715 | .little => e.len - 1, |
| 716 | }; | 716 | }; |
| 717 | while (true) { | 717 | while (true) { |
| 718 | const b = e[s]; | 718 | const b = e[s]; |
| ... | @@ -737,11 +737,11 @@ pub fn Modulus(comptime max_bits: comptime_int) type { | ... | @@ -737,11 +737,11 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 737 | } | 737 | } |
| 738 | } | 738 | } |
| 739 | switch (endian) { | 739 | switch (endian) { |
| 740 | .Big => { | 740 | .big => { |
| 741 | s += 1; | 741 | s += 1; |
| 742 | if (s == e.len) break; | 742 | if (s == e.len) break; |
| 743 | }, | 743 | }, |
| 744 | .Little => { | 744 | .little => { |
| 745 | if (s == 0) break; | 745 | if (s == 0) break; |
| 746 | s -= 1; | 746 | s -= 1; |
| 747 | }, | 747 | }, |
| ... | @@ -791,10 +791,10 @@ pub fn Modulus(comptime max_bits: comptime_int) type { | ... | @@ -791,10 +791,10 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 791 | var e_normalized = Fe{ .v = e.v.normalize() }; | 791 | var e_normalized = Fe{ .v = e.v.normalize() }; |
| 792 | var buf_: [Fe.encoded_bytes]u8 = undefined; | 792 | var buf_: [Fe.encoded_bytes]u8 = undefined; |
| 793 | var buf = buf_[0 .. math.divCeil(usize, e_normalized.v.limbs_count() * t_bits, 8) catch unreachable]; | 793 | var buf = buf_[0 .. math.divCeil(usize, e_normalized.v.limbs_count() * t_bits, 8) catch unreachable]; |
| 794 | e_normalized.toBytes(buf, .Little) catch unreachable; | 794 | e_normalized.toBytes(buf, .little) catch unreachable; |
| 795 | const leading = @clz(e_normalized.v.limbs.get(e_normalized.v.limbs_count() - carry_bits)); | 795 | const leading = @clz(e_normalized.v.limbs.get(e_normalized.v.limbs_count() - carry_bits)); |
| 796 | buf = buf[0 .. buf.len - leading / 8]; | 796 | buf = buf[0 .. buf.len - leading / 8]; |
| 797 | return self.powWithEncodedPublicExponent(x, buf, .Little); | 797 | return self.powWithEncodedPublicExponent(x, buf, .little); |
| 798 | } | 798 | } |
| 799 | 799 | ||
| 800 | /// Returns x^e (mod m), with the exponent provided as a byte string. | 800 | /// Returns x^e (mod m), with the exponent provided as a byte string. |
lib/std/crypto/ghash_polyval.zig+2-2| ... | @@ -13,7 +13,7 @@ const Precomp = u128; | ... | @@ -13,7 +13,7 @@ const Precomp = u128; |
| 13 | /// It is not a general purpose hash function - The key must be secret, unpredictable and never reused. | 13 | /// It is not a general purpose hash function - The key must be secret, unpredictable and never reused. |
| 14 | /// | 14 | /// |
| 15 | /// GHASH is typically used to compute the authentication tag in the AES-GCM construction. | 15 | /// GHASH is typically used to compute the authentication tag in the AES-GCM construction. |
| 16 | pub const Ghash = Hash(.Big, true); | 16 | pub const Ghash = Hash(.big, true); |
| 17 | 17 | ||
| 18 | /// POLYVAL is a universal hash function that uses multiplication by a fixed | 18 | /// POLYVAL is a universal hash function that uses multiplication by a fixed |
| 19 | /// parameter within a Galois field. | 19 | /// parameter within a Galois field. |
| ... | @@ -21,7 +21,7 @@ pub const Ghash = Hash(.Big, true); | ... | @@ -21,7 +21,7 @@ pub const Ghash = Hash(.Big, true); |
| 21 | /// It is not a general purpose hash function - The key must be secret, unpredictable and never reused. | 21 | /// It is not a general purpose hash function - The key must be secret, unpredictable and never reused. |
| 22 | /// | 22 | /// |
| 23 | /// POLYVAL is typically used to compute the authentication tag in the AES-GCM-SIV construction. | 23 | /// POLYVAL is typically used to compute the authentication tag in the AES-GCM-SIV construction. |
| 24 | pub const Polyval = Hash(.Little, false); | 24 | pub const Polyval = Hash(.little, false); |
| 25 | 25 | ||
| 26 | fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type { | 26 | fn Hash(comptime endian: std.builtin.Endian, comptime shift_key: bool) type { |
| 27 | return struct { | 27 | return struct { |
lib/std/crypto/isap.zig+12-12| ... | @@ -4,7 +4,7 @@ const debug = std.debug; | ... | @@ -4,7 +4,7 @@ const debug = std.debug; |
| 4 | const mem = std.mem; | 4 | const mem = std.mem; |
| 5 | const math = std.math; | 5 | const math = std.math; |
| 6 | const testing = std.testing; | 6 | const testing = std.testing; |
| 7 | const Ascon = crypto.core.Ascon(.Big); | 7 | const Ascon = crypto.core.Ascon(.big); |
| 8 | const AuthenticationError = crypto.errors.AuthenticationError; | 8 | const AuthenticationError = crypto.errors.AuthenticationError; |
| 9 | 9 | ||
| 10 | /// ISAPv2 is an authenticated encryption system hardened against side channels and fault attacks. | 10 | /// ISAPv2 is an authenticated encryption system hardened against side channels and fault attacks. |
| ... | @@ -55,9 +55,9 @@ pub const IsapA128A = struct { | ... | @@ -55,9 +55,9 @@ pub const IsapA128A = struct { |
| 55 | fn trickle(k: [16]u8, iv: [8]u8, y: []const u8, comptime out_len: usize) [out_len]u8 { | 55 | fn trickle(k: [16]u8, iv: [8]u8, y: []const u8, comptime out_len: usize) [out_len]u8 { |
| 56 | var isap = IsapA128A{ | 56 | var isap = IsapA128A{ |
| 57 | .st = Ascon.initFromWords(.{ | 57 | .st = Ascon.initFromWords(.{ |
| 58 | mem.readIntBig(u64, k[0..8]), | 58 | mem.readInt(u64, k[0..8], .big), |
| 59 | mem.readIntBig(u64, k[8..16]), | 59 | mem.readInt(u64, k[8..16], .big), |
| 60 | mem.readIntBig(u64, iv[0..8]), | 60 | mem.readInt(u64, iv[0..8], .big), |
| 61 | 0, | 61 | 0, |
| 62 | 0, | 62 | 0, |
| 63 | }), | 63 | }), |
| ... | @@ -85,9 +85,9 @@ pub const IsapA128A = struct { | ... | @@ -85,9 +85,9 @@ pub const IsapA128A = struct { |
| 85 | fn mac(c: []const u8, ad: []const u8, npub: [16]u8, key: [16]u8) [16]u8 { | 85 | fn mac(c: []const u8, ad: []const u8, npub: [16]u8, key: [16]u8) [16]u8 { |
| 86 | var isap = IsapA128A{ | 86 | var isap = IsapA128A{ |
| 87 | .st = Ascon.initFromWords(.{ | 87 | .st = Ascon.initFromWords(.{ |
| 88 | mem.readIntBig(u64, npub[0..8]), | 88 | mem.readInt(u64, npub[0..8], .big), |
| 89 | mem.readIntBig(u64, npub[8..16]), | 89 | mem.readInt(u64, npub[8..16], .big), |
| 90 | mem.readIntBig(u64, iv1[0..]), | 90 | mem.readInt(u64, iv1[0..], .big), |
| 91 | 0, | 91 | 0, |
| 92 | 0, | 92 | 0, |
| 93 | }), | 93 | }), |
| ... | @@ -116,11 +116,11 @@ pub const IsapA128A = struct { | ... | @@ -116,11 +116,11 @@ pub const IsapA128A = struct { |
| 116 | const nb = trickle(key, iv3, npub[0..], 24); | 116 | const nb = trickle(key, iv3, npub[0..], 24); |
| 117 | var isap = IsapA128A{ | 117 | var isap = IsapA128A{ |
| 118 | .st = Ascon.initFromWords(.{ | 118 | .st = Ascon.initFromWords(.{ |
| 119 | mem.readIntBig(u64, nb[0..8]), | 119 | mem.readInt(u64, nb[0..8], .big), |
| 120 | mem.readIntBig(u64, nb[8..16]), | 120 | mem.readInt(u64, nb[8..16], .big), |
| 121 | mem.readIntBig(u64, nb[16..24]), | 121 | mem.readInt(u64, nb[16..24], .big), |
| 122 | mem.readIntBig(u64, npub[0..8]), | 122 | mem.readInt(u64, npub[0..8], .big), |
| 123 | mem.readIntBig(u64, npub[8..16]), | 123 | mem.readInt(u64, npub[8..16], .big), |
| 124 | }), | 124 | }), |
| 125 | }; | 125 | }; |
| 126 | isap.st.permuteR(6); | 126 | isap.st.permuteR(6); |
lib/std/crypto/keccak_p.zig+13-11| ... | @@ -1,7 +1,9 @@ | ... | @@ -1,7 +1,9 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); | ||
| 2 | const assert = std.debug.assert; | 3 | const assert = std.debug.assert; |
| 3 | const math = std.math; | 4 | const math = std.math; |
| 4 | const mem = std.mem; | 5 | const mem = std.mem; |
| 6 | const native_endian = builtin.cpu.arch.endian(); | ||
| 5 | 7 | ||
| 6 | /// The Keccak-f permutation. | 8 | /// The Keccak-f permutation. |
| 7 | pub fn KeccakF(comptime f: u11) type { | 9 | pub fn KeccakF(comptime f: u11) type { |
| ... | @@ -43,7 +45,7 @@ pub fn KeccakF(comptime f: u11) type { | ... | @@ -43,7 +45,7 @@ pub fn KeccakF(comptime f: u11) type { |
| 43 | pub fn init(bytes: [block_bytes]u8) Self { | 45 | pub fn init(bytes: [block_bytes]u8) Self { |
| 44 | var self: Self = undefined; | 46 | var self: Self = undefined; |
| 45 | inline for (&self.st, 0..) |*r, i| { | 47 | inline for (&self.st, 0..) |*r, i| { |
| 46 | r.* = mem.readIntLittle(T, bytes[@sizeOf(T) * i ..][0..@sizeOf(T)]); | 48 | r.* = mem.readInt(T, bytes[@sizeOf(T) * i ..][0..@sizeOf(T)], .little); |
| 47 | } | 49 | } |
| 48 | return self; | 50 | return self; |
| 49 | } | 51 | } |
| ... | @@ -64,12 +66,12 @@ pub fn KeccakF(comptime f: u11) type { | ... | @@ -64,12 +66,12 @@ pub fn KeccakF(comptime f: u11) type { |
| 64 | pub fn setBytes(self: *Self, bytes: []const u8) void { | 66 | pub fn setBytes(self: *Self, bytes: []const u8) void { |
| 65 | var i: usize = 0; | 67 | var i: usize = 0; |
| 66 | while (i + @sizeOf(T) <= bytes.len) : (i += @sizeOf(T)) { | 68 | while (i + @sizeOf(T) <= bytes.len) : (i += @sizeOf(T)) { |
| 67 | self.st[i / @sizeOf(T)] = mem.readIntLittle(T, bytes[i..][0..@sizeOf(T)]); | 69 | self.st[i / @sizeOf(T)] = mem.readInt(T, bytes[i..][0..@sizeOf(T)], .little); |
| 68 | } | 70 | } |
| 69 | if (i < bytes.len) { | 71 | if (i < bytes.len) { |
| 70 | var padded = [_]u8{0} ** @sizeOf(T); | 72 | var padded = [_]u8{0} ** @sizeOf(T); |
| 71 | @memcpy(padded[0 .. bytes.len - i], bytes[i..]); | 73 | @memcpy(padded[0 .. bytes.len - i], bytes[i..]); |
| 72 | self.st[i / @sizeOf(T)] = mem.readIntLittle(T, padded[0..]); | 74 | self.st[i / @sizeOf(T)] = mem.readInt(T, padded[0..], .little); |
| 73 | } | 75 | } |
| 74 | } | 76 | } |
| 75 | 77 | ||
| ... | @@ -83,12 +85,12 @@ pub fn KeccakF(comptime f: u11) type { | ... | @@ -83,12 +85,12 @@ pub fn KeccakF(comptime f: u11) type { |
| 83 | pub fn addBytes(self: *Self, bytes: []const u8) void { | 85 | pub fn addBytes(self: *Self, bytes: []const u8) void { |
| 84 | var i: usize = 0; | 86 | var i: usize = 0; |
| 85 | while (i + @sizeOf(T) <= bytes.len) : (i += @sizeOf(T)) { | 87 | while (i + @sizeOf(T) <= bytes.len) : (i += @sizeOf(T)) { |
| 86 | self.st[i / @sizeOf(T)] ^= mem.readIntLittle(T, bytes[i..][0..@sizeOf(T)]); | 88 | self.st[i / @sizeOf(T)] ^= mem.readInt(T, bytes[i..][0..@sizeOf(T)], .little); |
| 87 | } | 89 | } |
| 88 | if (i < bytes.len) { | 90 | if (i < bytes.len) { |
| 89 | var padded = [_]u8{0} ** @sizeOf(T); | 91 | var padded = [_]u8{0} ** @sizeOf(T); |
| 90 | @memcpy(padded[0 .. bytes.len - i], bytes[i..]); | 92 | @memcpy(padded[0 .. bytes.len - i], bytes[i..]); |
| 91 | self.st[i / @sizeOf(T)] ^= mem.readIntLittle(T, padded[0..]); | 93 | self.st[i / @sizeOf(T)] ^= mem.readInt(T, padded[0..], .little); |
| 92 | } | 94 | } |
| 93 | } | 95 | } |
| 94 | 96 | ||
| ... | @@ -96,11 +98,11 @@ pub fn KeccakF(comptime f: u11) type { | ... | @@ -96,11 +98,11 @@ pub fn KeccakF(comptime f: u11) type { |
| 96 | pub fn extractBytes(self: *Self, out: []u8) void { | 98 | pub fn extractBytes(self: *Self, out: []u8) void { |
| 97 | var i: usize = 0; | 99 | var i: usize = 0; |
| 98 | while (i + @sizeOf(T) <= out.len) : (i += @sizeOf(T)) { | 100 | while (i + @sizeOf(T) <= out.len) : (i += @sizeOf(T)) { |
| 99 | mem.writeIntLittle(T, out[i..][0..@sizeOf(T)], self.st[i / @sizeOf(T)]); | 101 | mem.writeInt(T, out[i..][0..@sizeOf(T)], self.st[i / @sizeOf(T)], .little); |
| 100 | } | 102 | } |
| 101 | if (i < out.len) { | 103 | if (i < out.len) { |
| 102 | var padded = [_]u8{0} ** @sizeOf(T); | 104 | var padded = [_]u8{0} ** @sizeOf(T); |
| 103 | mem.writeIntLittle(T, padded[0..], self.st[i / @sizeOf(T)]); | 105 | mem.writeInt(T, padded[0..], self.st[i / @sizeOf(T)], .little); |
| 104 | @memcpy(out[i..], padded[0 .. out.len - i]); | 106 | @memcpy(out[i..], padded[0 .. out.len - i]); |
| 105 | } | 107 | } |
| 106 | } | 108 | } |
| ... | @@ -111,14 +113,14 @@ pub fn KeccakF(comptime f: u11) type { | ... | @@ -111,14 +113,14 @@ pub fn KeccakF(comptime f: u11) type { |
| 111 | 113 | ||
| 112 | var i: usize = 0; | 114 | var i: usize = 0; |
| 113 | while (i + @sizeOf(T) <= in.len) : (i += @sizeOf(T)) { | 115 | while (i + @sizeOf(T) <= in.len) : (i += @sizeOf(T)) { |
| 114 | const x = mem.readIntNative(T, in[i..][0..@sizeOf(T)]) ^ mem.nativeToLittle(T, self.st[i / @sizeOf(T)]); | 116 | const x = mem.readInt(T, in[i..][0..@sizeOf(T)], native_endian) ^ mem.nativeToLittle(T, self.st[i / @sizeOf(T)]); |
| 115 | mem.writeIntNative(T, out[i..][0..@sizeOf(T)], x); | 117 | mem.writeInt(T, out[i..][0..@sizeOf(T)], x, native_endian); |
| 116 | } | 118 | } |
| 117 | if (i < in.len) { | 119 | if (i < in.len) { |
| 118 | var padded = [_]u8{0} ** @sizeOf(T); | 120 | var padded = [_]u8{0} ** @sizeOf(T); |
| 119 | @memcpy(padded[0 .. in.len - i], in[i..]); | 121 | @memcpy(padded[0 .. in.len - i], in[i..]); |
| 120 | const x = mem.readIntNative(T, &padded) ^ mem.nativeToLittle(T, self.st[i / @sizeOf(T)]); | 122 | const x = mem.readInt(T, &padded, native_endian) ^ mem.nativeToLittle(T, self.st[i / @sizeOf(T)]); |
| 121 | mem.writeIntNative(T, &padded, x); | 123 | mem.writeInt(T, &padded, x, native_endian); |
| 122 | @memcpy(out[i..], padded[0 .. in.len - i]); | 124 | @memcpy(out[i..], padded[0 .. in.len - i]); |
| 123 | } | 125 | } |
| 124 | } | 126 | } |
lib/std/crypto/md5.zig+2-2| ... | @@ -112,7 +112,7 @@ pub const Md5 = struct { | ... | @@ -112,7 +112,7 @@ pub const Md5 = struct { |
| 112 | d.round(d.buf[0..]); | 112 | d.round(d.buf[0..]); |
| 113 | 113 | ||
| 114 | for (d.s, 0..) |s, j| { | 114 | for (d.s, 0..) |s, j| { |
| 115 | mem.writeIntLittle(u32, out[4 * j ..][0..4], s); | 115 | mem.writeInt(u32, out[4 * j ..][0..4], s, .little); |
| 116 | } | 116 | } |
| 117 | } | 117 | } |
| 118 | 118 | ||
| ... | @@ -121,7 +121,7 @@ pub const Md5 = struct { | ... | @@ -121,7 +121,7 @@ pub const Md5 = struct { |
| 121 | 121 | ||
| 122 | var i: usize = 0; | 122 | var i: usize = 0; |
| 123 | while (i < 16) : (i += 1) { | 123 | while (i < 16) : (i += 1) { |
| 124 | s[i] = mem.readIntLittle(u32, b[i * 4 ..][0..4]); | 124 | s[i] = mem.readInt(u32, b[i * 4 ..][0..4], .little); |
| 125 | } | 125 | } |
| 126 | 126 | ||
| 127 | var v: [4]u32 = [_]u32{ | 127 | var v: [4]u32 = [_]u32{ |
lib/std/crypto/pcurves/common.zig+11-11| ... | @@ -51,13 +51,13 @@ pub fn Field(comptime params: FieldParams) type { | ... | @@ -51,13 +51,13 @@ pub fn Field(comptime params: FieldParams) type { |
| 51 | 51 | ||
| 52 | /// Reject non-canonical encodings of an element. | 52 | /// Reject non-canonical encodings of an element. |
| 53 | pub fn rejectNonCanonical(s_: [encoded_length]u8, endian: std.builtin.Endian) NonCanonicalError!void { | 53 | pub fn rejectNonCanonical(s_: [encoded_length]u8, endian: std.builtin.Endian) NonCanonicalError!void { |
| 54 | var s = if (endian == .Little) s_ else orderSwap(s_); | 54 | var s = if (endian == .little) s_ else orderSwap(s_); |
| 55 | const field_order_s = comptime fos: { | 55 | const field_order_s = comptime fos: { |
| 56 | var fos: [encoded_length]u8 = undefined; | 56 | var fos: [encoded_length]u8 = undefined; |
| 57 | mem.writeIntLittle(std.meta.Int(.unsigned, encoded_length * 8), &fos, field_order); | 57 | mem.writeInt(std.meta.Int(.unsigned, encoded_length * 8), &fos, field_order, .little); |
| 58 | break :fos fos; | 58 | break :fos fos; |
| 59 | }; | 59 | }; |
| 60 | if (crypto.utils.timingSafeCompare(u8, &s, &field_order_s, .Little) != .lt) { | 60 | if (crypto.utils.timingSafeCompare(u8, &s, &field_order_s, .little) != .lt) { |
| 61 | return error.NonCanonical; | 61 | return error.NonCanonical; |
| 62 | } | 62 | } |
| 63 | } | 63 | } |
| ... | @@ -71,8 +71,8 @@ pub fn Field(comptime params: FieldParams) type { | ... | @@ -71,8 +71,8 @@ pub fn Field(comptime params: FieldParams) type { |
| 71 | 71 | ||
| 72 | /// Unpack a field element. | 72 | /// Unpack a field element. |
| 73 | pub fn fromBytes(s_: [encoded_length]u8, endian: std.builtin.Endian) NonCanonicalError!Fe { | 73 | pub fn fromBytes(s_: [encoded_length]u8, endian: std.builtin.Endian) NonCanonicalError!Fe { |
| 74 | var s = if (endian == .Little) s_ else orderSwap(s_); | 74 | var s = if (endian == .little) s_ else orderSwap(s_); |
| 75 | try rejectNonCanonical(s, .Little); | 75 | try rejectNonCanonical(s, .little); |
| 76 | var limbs_z: NonMontgomeryDomainFieldElement = undefined; | 76 | var limbs_z: NonMontgomeryDomainFieldElement = undefined; |
| 77 | fiat.fromBytes(&limbs_z, s); | 77 | fiat.fromBytes(&limbs_z, s); |
| 78 | var limbs: MontgomeryDomainFieldElement = undefined; | 78 | var limbs: MontgomeryDomainFieldElement = undefined; |
| ... | @@ -86,7 +86,7 @@ pub fn Field(comptime params: FieldParams) type { | ... | @@ -86,7 +86,7 @@ pub fn Field(comptime params: FieldParams) type { |
| 86 | fiat.fromMontgomery(&limbs_z, fe.limbs); | 86 | fiat.fromMontgomery(&limbs_z, fe.limbs); |
| 87 | var s: [encoded_length]u8 = undefined; | 87 | var s: [encoded_length]u8 = undefined; |
| 88 | fiat.toBytes(&s, limbs_z); | 88 | fiat.toBytes(&s, limbs_z); |
| 89 | return if (endian == .Little) s else orderSwap(s); | 89 | return if (endian == .little) s else orderSwap(s); |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | /// Element as an integer. | 92 | /// Element as an integer. |
| ... | @@ -95,14 +95,14 @@ pub fn Field(comptime params: FieldParams) type { | ... | @@ -95,14 +95,14 @@ pub fn Field(comptime params: FieldParams) type { |
| 95 | /// Create a field element from an integer. | 95 | /// Create a field element from an integer. |
| 96 | pub fn fromInt(comptime x: IntRepr) NonCanonicalError!Fe { | 96 | pub fn fromInt(comptime x: IntRepr) NonCanonicalError!Fe { |
| 97 | var s: [encoded_length]u8 = undefined; | 97 | var s: [encoded_length]u8 = undefined; |
| 98 | mem.writeIntLittle(IntRepr, &s, x); | 98 | mem.writeInt(IntRepr, &s, x, .little); |
| 99 | return fromBytes(s, .Little); | 99 | return fromBytes(s, .little); |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | /// Return the field element as an integer. | 102 | /// Return the field element as an integer. |
| 103 | pub fn toInt(fe: Fe) IntRepr { | 103 | pub fn toInt(fe: Fe) IntRepr { |
| 104 | const s = fe.toBytes(.Little); | 104 | const s = fe.toBytes(.little); |
| 105 | return mem.readIntLittle(IntRepr, &s); | 105 | return mem.readInt(IntRepr, &s, .little); |
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | /// Return true if the field element is zero. | 108 | /// Return true if the field element is zero. |
| ... | @@ -119,7 +119,7 @@ pub fn Field(comptime params: FieldParams) type { | ... | @@ -119,7 +119,7 @@ pub fn Field(comptime params: FieldParams) type { |
| 119 | 119 | ||
| 120 | /// Return true if the element is odd. | 120 | /// Return true if the element is odd. |
| 121 | pub fn isOdd(fe: Fe) bool { | 121 | pub fn isOdd(fe: Fe) bool { |
| 122 | const s = fe.toBytes(.Little); | 122 | const s = fe.toBytes(.little); |
| 123 | return @as(u1, @truncate(s[0])) != 0; | 123 | return @as(u1, @truncate(s[0])) != 0; |
| 124 | } | 124 | } |
| 125 | 125 |
lib/std/crypto/pcurves/p256.zig+12-12| ... | @@ -87,15 +87,15 @@ pub const P256 = struct { | ... | @@ -87,15 +87,15 @@ pub const P256 = struct { |
| 87 | }, | 87 | }, |
| 88 | 2, 3 => { | 88 | 2, 3 => { |
| 89 | if (encoded.len != 32) return error.InvalidEncoding; | 89 | if (encoded.len != 32) return error.InvalidEncoding; |
| 90 | const x = try Fe.fromBytes(encoded[0..32].*, .Big); | 90 | const x = try Fe.fromBytes(encoded[0..32].*, .big); |
| 91 | const y_is_odd = (encoding_type == 3); | 91 | const y_is_odd = (encoding_type == 3); |
| 92 | const y = try recoverY(x, y_is_odd); | 92 | const y = try recoverY(x, y_is_odd); |
| 93 | return P256{ .x = x, .y = y }; | 93 | return P256{ .x = x, .y = y }; |
| 94 | }, | 94 | }, |
| 95 | 4 => { | 95 | 4 => { |
| 96 | if (encoded.len != 64) return error.InvalidEncoding; | 96 | if (encoded.len != 64) return error.InvalidEncoding; |
| 97 | const x = try Fe.fromBytes(encoded[0..32].*, .Big); | 97 | const x = try Fe.fromBytes(encoded[0..32].*, .big); |
| 98 | const y = try Fe.fromBytes(encoded[32..64].*, .Big); | 98 | const y = try Fe.fromBytes(encoded[32..64].*, .big); |
| 99 | return P256.fromAffineCoordinates(.{ .x = x, .y = y }); | 99 | return P256.fromAffineCoordinates(.{ .x = x, .y = y }); |
| 100 | }, | 100 | }, |
| 101 | else => return error.InvalidEncoding, | 101 | else => return error.InvalidEncoding, |
| ... | @@ -107,7 +107,7 @@ pub const P256 = struct { | ... | @@ -107,7 +107,7 @@ pub const P256 = struct { |
| 107 | var out: [33]u8 = undefined; | 107 | var out: [33]u8 = undefined; |
| 108 | const xy = p.affineCoordinates(); | 108 | const xy = p.affineCoordinates(); |
| 109 | out[0] = if (xy.y.isOdd()) 3 else 2; | 109 | out[0] = if (xy.y.isOdd()) 3 else 2; |
| 110 | out[1..].* = xy.x.toBytes(.Big); | 110 | out[1..].* = xy.x.toBytes(.big); |
| 111 | return out; | 111 | return out; |
| 112 | } | 112 | } |
| 113 | 113 | ||
| ... | @@ -116,15 +116,15 @@ pub const P256 = struct { | ... | @@ -116,15 +116,15 @@ pub const P256 = struct { |
| 116 | var out: [65]u8 = undefined; | 116 | var out: [65]u8 = undefined; |
| 117 | out[0] = 4; | 117 | out[0] = 4; |
| 118 | const xy = p.affineCoordinates(); | 118 | const xy = p.affineCoordinates(); |
| 119 | out[1..33].* = xy.x.toBytes(.Big); | 119 | out[1..33].* = xy.x.toBytes(.big); |
| 120 | out[33..65].* = xy.y.toBytes(.Big); | 120 | out[33..65].* = xy.y.toBytes(.big); |
| 121 | return out; | 121 | return out; |
| 122 | } | 122 | } |
| 123 | 123 | ||
| 124 | /// Return a random point. | 124 | /// Return a random point. |
| 125 | pub fn random() P256 { | 125 | pub fn random() P256 { |
| 126 | const n = scalar.random(.Little); | 126 | const n = scalar.random(.little); |
| 127 | return basePoint.mul(n, .Little) catch unreachable; | 127 | return basePoint.mul(n, .little) catch unreachable; |
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | /// Flip the sign of the X coordinate. | 130 | /// Flip the sign of the X coordinate. |
| ... | @@ -400,7 +400,7 @@ pub const P256 = struct { | ... | @@ -400,7 +400,7 @@ pub const P256 = struct { |
| 400 | /// Multiply an elliptic curve point by a scalar. | 400 | /// Multiply an elliptic curve point by a scalar. |
| 401 | /// Return error.IdentityElement if the result is the identity element. | 401 | /// Return error.IdentityElement if the result is the identity element. |
| 402 | pub fn mul(p: P256, s_: [32]u8, endian: std.builtin.Endian) IdentityElementError!P256 { | 402 | pub fn mul(p: P256, s_: [32]u8, endian: std.builtin.Endian) IdentityElementError!P256 { |
| 403 | const s = if (endian == .Little) s_ else Fe.orderSwap(s_); | 403 | const s = if (endian == .little) s_ else Fe.orderSwap(s_); |
| 404 | if (p.is_base) { | 404 | if (p.is_base) { |
| 405 | return pcMul16(&basePointPc, s, false); | 405 | return pcMul16(&basePointPc, s, false); |
| 406 | } | 406 | } |
| ... | @@ -412,7 +412,7 @@ pub const P256 = struct { | ... | @@ -412,7 +412,7 @@ pub const P256 = struct { |
| 412 | /// Multiply an elliptic curve point by a *PUBLIC* scalar *IN VARIABLE TIME* | 412 | /// Multiply an elliptic curve point by a *PUBLIC* scalar *IN VARIABLE TIME* |
| 413 | /// This can be used for signature verification. | 413 | /// This can be used for signature verification. |
| 414 | pub fn mulPublic(p: P256, s_: [32]u8, endian: std.builtin.Endian) IdentityElementError!P256 { | 414 | pub fn mulPublic(p: P256, s_: [32]u8, endian: std.builtin.Endian) IdentityElementError!P256 { |
| 415 | const s = if (endian == .Little) s_ else Fe.orderSwap(s_); | 415 | const s = if (endian == .little) s_ else Fe.orderSwap(s_); |
| 416 | if (p.is_base) { | 416 | if (p.is_base) { |
| 417 | return pcMul16(&basePointPc, s, true); | 417 | return pcMul16(&basePointPc, s, true); |
| 418 | } | 418 | } |
| ... | @@ -424,8 +424,8 @@ pub const P256 = struct { | ... | @@ -424,8 +424,8 @@ pub const P256 = struct { |
| 424 | /// Double-base multiplication of public parameters - Compute (p1*s1)+(p2*s2) *IN VARIABLE TIME* | 424 | /// Double-base multiplication of public parameters - Compute (p1*s1)+(p2*s2) *IN VARIABLE TIME* |
| 425 | /// This can be used for signature verification. | 425 | /// This can be used for signature verification. |
| 426 | pub fn mulDoubleBasePublic(p1: P256, s1_: [32]u8, p2: P256, s2_: [32]u8, endian: std.builtin.Endian) IdentityElementError!P256 { | 426 | pub fn mulDoubleBasePublic(p1: P256, s1_: [32]u8, p2: P256, s2_: [32]u8, endian: std.builtin.Endian) IdentityElementError!P256 { |
| 427 | const s1 = if (endian == .Little) s1_ else Fe.orderSwap(s1_); | 427 | const s1 = if (endian == .little) s1_ else Fe.orderSwap(s1_); |
| 428 | const s2 = if (endian == .Little) s2_ else Fe.orderSwap(s2_); | 428 | const s2 = if (endian == .little) s2_ else Fe.orderSwap(s2_); |
| 429 | try p1.rejectIdentity(); | 429 | try p1.rejectIdentity(); |
| 430 | var pc1_array: [9]P256 = undefined; | 430 | var pc1_array: [9]P256 = undefined; |
| 431 | const pc1 = if (p1.is_base) basePointPc[0..9] else pc: { | 431 | const pc1 = if (p1.is_base) basePointPc[0..9] else pc: { |
lib/std/crypto/pcurves/p256/scalar.zig+5-5| ... | @@ -174,7 +174,7 @@ pub const Scalar = struct { | ... | @@ -174,7 +174,7 @@ pub const Scalar = struct { |
| 174 | var s: [48]u8 = undefined; | 174 | var s: [48]u8 = undefined; |
| 175 | while (true) { | 175 | while (true) { |
| 176 | crypto.random.bytes(&s); | 176 | crypto.random.bytes(&s); |
| 177 | const n = Scalar.fromBytes48(s, .Little); | 177 | const n = Scalar.fromBytes48(s, .little); |
| 178 | if (!n.isZero()) { | 178 | if (!n.isZero()) { |
| 179 | return n; | 179 | return n; |
| 180 | } | 180 | } |
| ... | @@ -191,7 +191,7 @@ const ScalarDouble = struct { | ... | @@ -191,7 +191,7 @@ const ScalarDouble = struct { |
| 191 | debug.assert(bits > 0 and bits <= 512 and bits >= Fe.saturated_bits and bits <= Fe.saturated_bits * 3); | 191 | debug.assert(bits > 0 and bits <= 512 and bits >= Fe.saturated_bits and bits <= Fe.saturated_bits * 3); |
| 192 | 192 | ||
| 193 | var s = s_; | 193 | var s = s_; |
| 194 | if (endian == .Big) { | 194 | if (endian == .big) { |
| 195 | for (s_, 0..) |x, i| s[s.len - 1 - i] = x; | 195 | for (s_, 0..) |x, i| s[s.len - 1 - i] = x; |
| 196 | } | 196 | } |
| 197 | var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero, .x3 = Fe.zero }; | 197 | var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero, .x3 = Fe.zero }; |
| ... | @@ -199,19 +199,19 @@ const ScalarDouble = struct { | ... | @@ -199,19 +199,19 @@ const ScalarDouble = struct { |
| 199 | var b = [_]u8{0} ** encoded_length; | 199 | var b = [_]u8{0} ** encoded_length; |
| 200 | const len = @min(s.len, 24); | 200 | const len = @min(s.len, 24); |
| 201 | b[0..len].* = s[0..len].*; | 201 | b[0..len].* = s[0..len].*; |
| 202 | t.x1 = Fe.fromBytes(b, .Little) catch unreachable; | 202 | t.x1 = Fe.fromBytes(b, .little) catch unreachable; |
| 203 | } | 203 | } |
| 204 | if (s_.len >= 24) { | 204 | if (s_.len >= 24) { |
| 205 | var b = [_]u8{0} ** encoded_length; | 205 | var b = [_]u8{0} ** encoded_length; |
| 206 | const len = @min(s.len - 24, 24); | 206 | const len = @min(s.len - 24, 24); |
| 207 | b[0..len].* = s[24..][0..len].*; | 207 | b[0..len].* = s[24..][0..len].*; |
| 208 | t.x2 = Fe.fromBytes(b, .Little) catch unreachable; | 208 | t.x2 = Fe.fromBytes(b, .little) catch unreachable; |
| 209 | } | 209 | } |
| 210 | if (s_.len >= 48) { | 210 | if (s_.len >= 48) { |
| 211 | var b = [_]u8{0} ** encoded_length; | 211 | var b = [_]u8{0} ** encoded_length; |
| 212 | const len = s.len - 48; | 212 | const len = s.len - 48; |
| 213 | b[0..len].* = s[48..][0..len].*; | 213 | b[0..len].* = s[48..][0..len].*; |
| 214 | t.x3 = Fe.fromBytes(b, .Little) catch unreachable; | 214 | t.x3 = Fe.fromBytes(b, .little) catch unreachable; |
| 215 | } | 215 | } |
| 216 | return t; | 216 | return t; |
| 217 | } | 217 | } |
lib/std/crypto/pcurves/p384.zig+12-12| ... | @@ -87,15 +87,15 @@ pub const P384 = struct { | ... | @@ -87,15 +87,15 @@ pub const P384 = struct { |
| 87 | }, | 87 | }, |
| 88 | 2, 3 => { | 88 | 2, 3 => { |
| 89 | if (encoded.len != 48) return error.InvalidEncoding; | 89 | if (encoded.len != 48) return error.InvalidEncoding; |
| 90 | const x = try Fe.fromBytes(encoded[0..48].*, .Big); | 90 | const x = try Fe.fromBytes(encoded[0..48].*, .big); |
| 91 | const y_is_odd = (encoding_type == 3); | 91 | const y_is_odd = (encoding_type == 3); |
| 92 | const y = try recoverY(x, y_is_odd); | 92 | const y = try recoverY(x, y_is_odd); |
| 93 | return P384{ .x = x, .y = y }; | 93 | return P384{ .x = x, .y = y }; |
| 94 | }, | 94 | }, |
| 95 | 4 => { | 95 | 4 => { |
| 96 | if (encoded.len != 96) return error.InvalidEncoding; | 96 | if (encoded.len != 96) return error.InvalidEncoding; |
| 97 | const x = try Fe.fromBytes(encoded[0..48].*, .Big); | 97 | const x = try Fe.fromBytes(encoded[0..48].*, .big); |
| 98 | const y = try Fe.fromBytes(encoded[48..96].*, .Big); | 98 | const y = try Fe.fromBytes(encoded[48..96].*, .big); |
| 99 | return P384.fromAffineCoordinates(.{ .x = x, .y = y }); | 99 | return P384.fromAffineCoordinates(.{ .x = x, .y = y }); |
| 100 | }, | 100 | }, |
| 101 | else => return error.InvalidEncoding, | 101 | else => return error.InvalidEncoding, |
| ... | @@ -107,7 +107,7 @@ pub const P384 = struct { | ... | @@ -107,7 +107,7 @@ pub const P384 = struct { |
| 107 | var out: [49]u8 = undefined; | 107 | var out: [49]u8 = undefined; |
| 108 | const xy = p.affineCoordinates(); | 108 | const xy = p.affineCoordinates(); |
| 109 | out[0] = if (xy.y.isOdd()) 3 else 2; | 109 | out[0] = if (xy.y.isOdd()) 3 else 2; |
| 110 | out[1..].* = xy.x.toBytes(.Big); | 110 | out[1..].* = xy.x.toBytes(.big); |
| 111 | return out; | 111 | return out; |
| 112 | } | 112 | } |
| 113 | 113 | ||
| ... | @@ -116,15 +116,15 @@ pub const P384 = struct { | ... | @@ -116,15 +116,15 @@ pub const P384 = struct { |
| 116 | var out: [97]u8 = undefined; | 116 | var out: [97]u8 = undefined; |
| 117 | out[0] = 4; | 117 | out[0] = 4; |
| 118 | const xy = p.affineCoordinates(); | 118 | const xy = p.affineCoordinates(); |
| 119 | out[1..49].* = xy.x.toBytes(.Big); | 119 | out[1..49].* = xy.x.toBytes(.big); |
| 120 | out[49..97].* = xy.y.toBytes(.Big); | 120 | out[49..97].* = xy.y.toBytes(.big); |
| 121 | return out; | 121 | return out; |
| 122 | } | 122 | } |
| 123 | 123 | ||
| 124 | /// Return a random point. | 124 | /// Return a random point. |
| 125 | pub fn random() P384 { | 125 | pub fn random() P384 { |
| 126 | const n = scalar.random(.Little); | 126 | const n = scalar.random(.little); |
| 127 | return basePoint.mul(n, .Little) catch unreachable; | 127 | return basePoint.mul(n, .little) catch unreachable; |
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | /// Flip the sign of the X coordinate. | 130 | /// Flip the sign of the X coordinate. |
| ... | @@ -400,7 +400,7 @@ pub const P384 = struct { | ... | @@ -400,7 +400,7 @@ pub const P384 = struct { |
| 400 | /// Multiply an elliptic curve point by a scalar. | 400 | /// Multiply an elliptic curve point by a scalar. |
| 401 | /// Return error.IdentityElement if the result is the identity element. | 401 | /// Return error.IdentityElement if the result is the identity element. |
| 402 | pub fn mul(p: P384, s_: [48]u8, endian: std.builtin.Endian) IdentityElementError!P384 { | 402 | pub fn mul(p: P384, s_: [48]u8, endian: std.builtin.Endian) IdentityElementError!P384 { |
| 403 | const s = if (endian == .Little) s_ else Fe.orderSwap(s_); | 403 | const s = if (endian == .little) s_ else Fe.orderSwap(s_); |
| 404 | if (p.is_base) { | 404 | if (p.is_base) { |
| 405 | return pcMul16(&basePointPc, s, false); | 405 | return pcMul16(&basePointPc, s, false); |
| 406 | } | 406 | } |
| ... | @@ -412,7 +412,7 @@ pub const P384 = struct { | ... | @@ -412,7 +412,7 @@ pub const P384 = struct { |
| 412 | /// Multiply an elliptic curve point by a *PUBLIC* scalar *IN VARIABLE TIME* | 412 | /// Multiply an elliptic curve point by a *PUBLIC* scalar *IN VARIABLE TIME* |
| 413 | /// This can be used for signature verification. | 413 | /// This can be used for signature verification. |
| 414 | pub fn mulPublic(p: P384, s_: [48]u8, endian: std.builtin.Endian) IdentityElementError!P384 { | 414 | pub fn mulPublic(p: P384, s_: [48]u8, endian: std.builtin.Endian) IdentityElementError!P384 { |
| 415 | const s = if (endian == .Little) s_ else Fe.orderSwap(s_); | 415 | const s = if (endian == .little) s_ else Fe.orderSwap(s_); |
| 416 | if (p.is_base) { | 416 | if (p.is_base) { |
| 417 | return pcMul16(&basePointPc, s, true); | 417 | return pcMul16(&basePointPc, s, true); |
| 418 | } | 418 | } |
| ... | @@ -424,8 +424,8 @@ pub const P384 = struct { | ... | @@ -424,8 +424,8 @@ pub const P384 = struct { |
| 424 | /// Double-base multiplication of public parameters - Compute (p1*s1)+(p2*s2) *IN VARIABLE TIME* | 424 | /// Double-base multiplication of public parameters - Compute (p1*s1)+(p2*s2) *IN VARIABLE TIME* |
| 425 | /// This can be used for signature verification. | 425 | /// This can be used for signature verification. |
| 426 | pub fn mulDoubleBasePublic(p1: P384, s1_: [48]u8, p2: P384, s2_: [48]u8, endian: std.builtin.Endian) IdentityElementError!P384 { | 426 | pub fn mulDoubleBasePublic(p1: P384, s1_: [48]u8, p2: P384, s2_: [48]u8, endian: std.builtin.Endian) IdentityElementError!P384 { |
| 427 | const s1 = if (endian == .Little) s1_ else Fe.orderSwap(s1_); | 427 | const s1 = if (endian == .little) s1_ else Fe.orderSwap(s1_); |
| 428 | const s2 = if (endian == .Little) s2_ else Fe.orderSwap(s2_); | 428 | const s2 = if (endian == .little) s2_ else Fe.orderSwap(s2_); |
| 429 | try p1.rejectIdentity(); | 429 | try p1.rejectIdentity(); |
| 430 | var pc1_array: [9]P384 = undefined; | 430 | var pc1_array: [9]P384 = undefined; |
| 431 | const pc1 = if (p1.is_base) basePointPc[0..9] else pc: { | 431 | const pc1 = if (p1.is_base) basePointPc[0..9] else pc: { |
lib/std/crypto/pcurves/p384/scalar.zig+4-4| ... | @@ -163,7 +163,7 @@ pub const Scalar = struct { | ... | @@ -163,7 +163,7 @@ pub const Scalar = struct { |
| 163 | var s: [64]u8 = undefined; | 163 | var s: [64]u8 = undefined; |
| 164 | while (true) { | 164 | while (true) { |
| 165 | crypto.random.bytes(&s); | 165 | crypto.random.bytes(&s); |
| 166 | const n = Scalar.fromBytes64(s, .Little); | 166 | const n = Scalar.fromBytes64(s, .little); |
| 167 | if (!n.isZero()) { | 167 | if (!n.isZero()) { |
| 168 | return n; | 168 | return n; |
| 169 | } | 169 | } |
| ... | @@ -179,7 +179,7 @@ const ScalarDouble = struct { | ... | @@ -179,7 +179,7 @@ const ScalarDouble = struct { |
| 179 | debug.assert(bits > 0 and bits <= 512 and bits >= Fe.saturated_bits and bits <= Fe.saturated_bits * 2); | 179 | debug.assert(bits > 0 and bits <= 512 and bits >= Fe.saturated_bits and bits <= Fe.saturated_bits * 2); |
| 180 | 180 | ||
| 181 | var s = s_; | 181 | var s = s_; |
| 182 | if (endian == .Big) { | 182 | if (endian == .big) { |
| 183 | for (s_, 0..) |x, i| s[s.len - 1 - i] = x; | 183 | for (s_, 0..) |x, i| s[s.len - 1 - i] = x; |
| 184 | } | 184 | } |
| 185 | var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero }; | 185 | var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero }; |
| ... | @@ -187,13 +187,13 @@ const ScalarDouble = struct { | ... | @@ -187,13 +187,13 @@ const ScalarDouble = struct { |
| 187 | var b = [_]u8{0} ** encoded_length; | 187 | var b = [_]u8{0} ** encoded_length; |
| 188 | const len = @min(s.len, 32); | 188 | const len = @min(s.len, 32); |
| 189 | b[0..len].* = s[0..len].*; | 189 | b[0..len].* = s[0..len].*; |
| 190 | t.x1 = Fe.fromBytes(b, .Little) catch unreachable; | 190 | t.x1 = Fe.fromBytes(b, .little) catch unreachable; |
| 191 | } | 191 | } |
| 192 | if (s_.len >= 32) { | 192 | if (s_.len >= 32) { |
| 193 | var b = [_]u8{0} ** encoded_length; | 193 | var b = [_]u8{0} ** encoded_length; |
| 194 | const len = @min(s.len - 32, 32); | 194 | const len = @min(s.len - 32, 32); |
| 195 | b[0..len].* = s[32..][0..len].*; | 195 | b[0..len].* = s[32..][0..len].*; |
| 196 | t.x2 = Fe.fromBytes(b, .Little) catch unreachable; | 196 | t.x2 = Fe.fromBytes(b, .little) catch unreachable; |
| 197 | } | 197 | } |
| 198 | return t; | 198 | return t; |
| 199 | } | 199 | } |
lib/std/crypto/pcurves/secp256k1.zig+26-26| ... | @@ -41,7 +41,7 @@ pub const Secp256k1 = struct { | ... | @@ -41,7 +41,7 @@ pub const Secp256k1 = struct { |
| 41 | 41 | ||
| 42 | const lambda_s = s: { | 42 | const lambda_s = s: { |
| 43 | var buf: [32]u8 = undefined; | 43 | var buf: [32]u8 = undefined; |
| 44 | mem.writeIntLittle(u256, &buf, Endormorphism.lambda); | 44 | mem.writeInt(u256, &buf, Endormorphism.lambda, .little); |
| 45 | break :s buf; | 45 | break :s buf; |
| 46 | }; | 46 | }; |
| 47 | 47 | ||
| ... | @@ -54,12 +54,12 @@ pub const Secp256k1 = struct { | ... | @@ -54,12 +54,12 @@ pub const Secp256k1 = struct { |
| 54 | pub fn splitScalar(s: [32]u8, endian: std.builtin.Endian) NonCanonicalError!SplitScalar { | 54 | pub fn splitScalar(s: [32]u8, endian: std.builtin.Endian) NonCanonicalError!SplitScalar { |
| 55 | const b1_neg_s = comptime s: { | 55 | const b1_neg_s = comptime s: { |
| 56 | var buf: [32]u8 = undefined; | 56 | var buf: [32]u8 = undefined; |
| 57 | mem.writeIntLittle(u256, &buf, 303414439467246543595250775667605759171); | 57 | mem.writeInt(u256, &buf, 303414439467246543595250775667605759171, .little); |
| 58 | break :s buf; | 58 | break :s buf; |
| 59 | }; | 59 | }; |
| 60 | const b2_neg_s = comptime s: { | 60 | const b2_neg_s = comptime s: { |
| 61 | var buf: [32]u8 = undefined; | 61 | var buf: [32]u8 = undefined; |
| 62 | mem.writeIntLittle(u256, &buf, scalar.field_order - 64502973549206556628585045361533709077); | 62 | mem.writeInt(u256, &buf, scalar.field_order - 64502973549206556628585045361533709077, .little); |
| 63 | break :s buf; | 63 | break :s buf; |
| 64 | }; | 64 | }; |
| 65 | const k = mem.readInt(u256, &s, endian); | 65 | const k = mem.readInt(u256, &s, endian); |
| ... | @@ -72,16 +72,16 @@ pub const Secp256k1 = struct { | ... | @@ -72,16 +72,16 @@ pub const Secp256k1 = struct { |
| 72 | 72 | ||
| 73 | var buf: [32]u8 = undefined; | 73 | var buf: [32]u8 = undefined; |
| 74 | 74 | ||
| 75 | mem.writeIntLittle(u256, &buf, c1); | 75 | mem.writeInt(u256, &buf, c1, .little); |
| 76 | const c1x = try scalar.mul(buf, b1_neg_s, .Little); | 76 | const c1x = try scalar.mul(buf, b1_neg_s, .little); |
| 77 | 77 | ||
| 78 | mem.writeIntLittle(u256, &buf, c2); | 78 | mem.writeInt(u256, &buf, c2, .little); |
| 79 | const c2x = try scalar.mul(buf, b2_neg_s, .Little); | 79 | const c2x = try scalar.mul(buf, b2_neg_s, .little); |
| 80 | 80 | ||
| 81 | const r2 = try scalar.add(c1x, c2x, .Little); | 81 | const r2 = try scalar.add(c1x, c2x, .little); |
| 82 | 82 | ||
| 83 | var r1 = try scalar.mul(r2, lambda_s, .Little); | 83 | var r1 = try scalar.mul(r2, lambda_s, .little); |
| 84 | r1 = try scalar.sub(s, r1, .Little); | 84 | r1 = try scalar.sub(s, r1, .little); |
| 85 | 85 | ||
| 86 | return SplitScalar{ .r1 = r1, .r2 = r2 }; | 86 | return SplitScalar{ .r1 = r1, .r2 = r2 }; |
| 87 | } | 87 | } |
| ... | @@ -140,15 +140,15 @@ pub const Secp256k1 = struct { | ... | @@ -140,15 +140,15 @@ pub const Secp256k1 = struct { |
| 140 | }, | 140 | }, |
| 141 | 2, 3 => { | 141 | 2, 3 => { |
| 142 | if (encoded.len != 32) return error.InvalidEncoding; | 142 | if (encoded.len != 32) return error.InvalidEncoding; |
| 143 | const x = try Fe.fromBytes(encoded[0..32].*, .Big); | 143 | const x = try Fe.fromBytes(encoded[0..32].*, .big); |
| 144 | const y_is_odd = (encoding_type == 3); | 144 | const y_is_odd = (encoding_type == 3); |
| 145 | const y = try recoverY(x, y_is_odd); | 145 | const y = try recoverY(x, y_is_odd); |
| 146 | return Secp256k1{ .x = x, .y = y }; | 146 | return Secp256k1{ .x = x, .y = y }; |
| 147 | }, | 147 | }, |
| 148 | 4 => { | 148 | 4 => { |
| 149 | if (encoded.len != 64) return error.InvalidEncoding; | 149 | if (encoded.len != 64) return error.InvalidEncoding; |
| 150 | const x = try Fe.fromBytes(encoded[0..32].*, .Big); | 150 | const x = try Fe.fromBytes(encoded[0..32].*, .big); |
| 151 | const y = try Fe.fromBytes(encoded[32..64].*, .Big); | 151 | const y = try Fe.fromBytes(encoded[32..64].*, .big); |
| 152 | return Secp256k1.fromAffineCoordinates(.{ .x = x, .y = y }); | 152 | return Secp256k1.fromAffineCoordinates(.{ .x = x, .y = y }); |
| 153 | }, | 153 | }, |
| 154 | else => return error.InvalidEncoding, | 154 | else => return error.InvalidEncoding, |
| ... | @@ -160,7 +160,7 @@ pub const Secp256k1 = struct { | ... | @@ -160,7 +160,7 @@ pub const Secp256k1 = struct { |
| 160 | var out: [33]u8 = undefined; | 160 | var out: [33]u8 = undefined; |
| 161 | const xy = p.affineCoordinates(); | 161 | const xy = p.affineCoordinates(); |
| 162 | out[0] = if (xy.y.isOdd()) 3 else 2; | 162 | out[0] = if (xy.y.isOdd()) 3 else 2; |
| 163 | out[1..].* = xy.x.toBytes(.Big); | 163 | out[1..].* = xy.x.toBytes(.big); |
| 164 | return out; | 164 | return out; |
| 165 | } | 165 | } |
| 166 | 166 | ||
| ... | @@ -169,15 +169,15 @@ pub const Secp256k1 = struct { | ... | @@ -169,15 +169,15 @@ pub const Secp256k1 = struct { |
| 169 | var out: [65]u8 = undefined; | 169 | var out: [65]u8 = undefined; |
| 170 | out[0] = 4; | 170 | out[0] = 4; |
| 171 | const xy = p.affineCoordinates(); | 171 | const xy = p.affineCoordinates(); |
| 172 | out[1..33].* = xy.x.toBytes(.Big); | 172 | out[1..33].* = xy.x.toBytes(.big); |
| 173 | out[33..65].* = xy.y.toBytes(.Big); | 173 | out[33..65].* = xy.y.toBytes(.big); |
| 174 | return out; | 174 | return out; |
| 175 | } | 175 | } |
| 176 | 176 | ||
| 177 | /// Return a random point. | 177 | /// Return a random point. |
| 178 | pub fn random() Secp256k1 { | 178 | pub fn random() Secp256k1 { |
| 179 | const n = scalar.random(.Little); | 179 | const n = scalar.random(.little); |
| 180 | return basePoint.mul(n, .Little) catch unreachable; | 180 | return basePoint.mul(n, .little) catch unreachable; |
| 181 | } | 181 | } |
| 182 | 182 | ||
| 183 | /// Flip the sign of the X coordinate. | 183 | /// Flip the sign of the X coordinate. |
| ... | @@ -428,7 +428,7 @@ pub const Secp256k1 = struct { | ... | @@ -428,7 +428,7 @@ pub const Secp256k1 = struct { |
| 428 | /// Multiply an elliptic curve point by a scalar. | 428 | /// Multiply an elliptic curve point by a scalar. |
| 429 | /// Return error.IdentityElement if the result is the identity element. | 429 | /// Return error.IdentityElement if the result is the identity element. |
| 430 | pub fn mul(p: Secp256k1, s_: [32]u8, endian: std.builtin.Endian) IdentityElementError!Secp256k1 { | 430 | pub fn mul(p: Secp256k1, s_: [32]u8, endian: std.builtin.Endian) IdentityElementError!Secp256k1 { |
| 431 | const s = if (endian == .Little) s_ else Fe.orderSwap(s_); | 431 | const s = if (endian == .little) s_ else Fe.orderSwap(s_); |
| 432 | if (p.is_base) { | 432 | if (p.is_base) { |
| 433 | return pcMul16(&basePointPc, s, false); | 433 | return pcMul16(&basePointPc, s, false); |
| 434 | } | 434 | } |
| ... | @@ -440,24 +440,24 @@ pub const Secp256k1 = struct { | ... | @@ -440,24 +440,24 @@ pub const Secp256k1 = struct { |
| 440 | /// Multiply an elliptic curve point by a *PUBLIC* scalar *IN VARIABLE TIME* | 440 | /// Multiply an elliptic curve point by a *PUBLIC* scalar *IN VARIABLE TIME* |
| 441 | /// This can be used for signature verification. | 441 | /// This can be used for signature verification. |
| 442 | pub fn mulPublic(p: Secp256k1, s_: [32]u8, endian: std.builtin.Endian) (IdentityElementError || NonCanonicalError)!Secp256k1 { | 442 | pub fn mulPublic(p: Secp256k1, s_: [32]u8, endian: std.builtin.Endian) (IdentityElementError || NonCanonicalError)!Secp256k1 { |
| 443 | const s = if (endian == .Little) s_ else Fe.orderSwap(s_); | 443 | const s = if (endian == .little) s_ else Fe.orderSwap(s_); |
| 444 | const zero = comptime scalar.Scalar.zero.toBytes(.Little); | 444 | const zero = comptime scalar.Scalar.zero.toBytes(.little); |
| 445 | if (mem.eql(u8, &zero, &s)) { | 445 | if (mem.eql(u8, &zero, &s)) { |
| 446 | return error.IdentityElement; | 446 | return error.IdentityElement; |
| 447 | } | 447 | } |
| 448 | const pc = precompute(p, 8); | 448 | const pc = precompute(p, 8); |
| 449 | var lambda_p = try pcMul(&pc, Endormorphism.lambda_s, true); | 449 | var lambda_p = try pcMul(&pc, Endormorphism.lambda_s, true); |
| 450 | var split_scalar = try Endormorphism.splitScalar(s, .Little); | 450 | var split_scalar = try Endormorphism.splitScalar(s, .little); |
| 451 | var px = p; | 451 | var px = p; |
| 452 | 452 | ||
| 453 | // If a key is negative, flip the sign to keep it half-sized, | 453 | // If a key is negative, flip the sign to keep it half-sized, |
| 454 | // and flip the sign of the Y point coordinate to compensate. | 454 | // and flip the sign of the Y point coordinate to compensate. |
| 455 | if (split_scalar.r1[split_scalar.r1.len / 2] != 0) { | 455 | if (split_scalar.r1[split_scalar.r1.len / 2] != 0) { |
| 456 | split_scalar.r1 = scalar.neg(split_scalar.r1, .Little) catch zero; | 456 | split_scalar.r1 = scalar.neg(split_scalar.r1, .little) catch zero; |
| 457 | px = px.neg(); | 457 | px = px.neg(); |
| 458 | } | 458 | } |
| 459 | if (split_scalar.r2[split_scalar.r2.len / 2] != 0) { | 459 | if (split_scalar.r2[split_scalar.r2.len / 2] != 0) { |
| 460 | split_scalar.r2 = scalar.neg(split_scalar.r2, .Little) catch zero; | 460 | split_scalar.r2 = scalar.neg(split_scalar.r2, .little) catch zero; |
| 461 | lambda_p = lambda_p.neg(); | 461 | lambda_p = lambda_p.neg(); |
| 462 | } | 462 | } |
| 463 | return mulDoubleBasePublicEndo(px, split_scalar.r1, lambda_p, split_scalar.r2); | 463 | return mulDoubleBasePublicEndo(px, split_scalar.r1, lambda_p, split_scalar.r2); |
| ... | @@ -502,8 +502,8 @@ pub const Secp256k1 = struct { | ... | @@ -502,8 +502,8 @@ pub const Secp256k1 = struct { |
| 502 | /// Double-base multiplication of public parameters - Compute (p1*s1)+(p2*s2) *IN VARIABLE TIME* | 502 | /// Double-base multiplication of public parameters - Compute (p1*s1)+(p2*s2) *IN VARIABLE TIME* |
| 503 | /// This can be used for signature verification. | 503 | /// This can be used for signature verification. |
| 504 | pub fn mulDoubleBasePublic(p1: Secp256k1, s1_: [32]u8, p2: Secp256k1, s2_: [32]u8, endian: std.builtin.Endian) IdentityElementError!Secp256k1 { | 504 | pub fn mulDoubleBasePublic(p1: Secp256k1, s1_: [32]u8, p2: Secp256k1, s2_: [32]u8, endian: std.builtin.Endian) IdentityElementError!Secp256k1 { |
| 505 | const s1 = if (endian == .Little) s1_ else Fe.orderSwap(s1_); | 505 | const s1 = if (endian == .little) s1_ else Fe.orderSwap(s1_); |
| 506 | const s2 = if (endian == .Little) s2_ else Fe.orderSwap(s2_); | 506 | const s2 = if (endian == .little) s2_ else Fe.orderSwap(s2_); |
| 507 | try p1.rejectIdentity(); | 507 | try p1.rejectIdentity(); |
| 508 | var pc1_array: [9]Secp256k1 = undefined; | 508 | var pc1_array: [9]Secp256k1 = undefined; |
| 509 | const pc1 = if (p1.is_base) basePointPc[0..9] else pc: { | 509 | const pc1 = if (p1.is_base) basePointPc[0..9] else pc: { |
lib/std/crypto/pcurves/secp256k1/scalar.zig+5-5| ... | @@ -174,7 +174,7 @@ pub const Scalar = struct { | ... | @@ -174,7 +174,7 @@ pub const Scalar = struct { |
| 174 | var s: [48]u8 = undefined; | 174 | var s: [48]u8 = undefined; |
| 175 | while (true) { | 175 | while (true) { |
| 176 | crypto.random.bytes(&s); | 176 | crypto.random.bytes(&s); |
| 177 | const n = Scalar.fromBytes48(s, .Little); | 177 | const n = Scalar.fromBytes48(s, .little); |
| 178 | if (!n.isZero()) { | 178 | if (!n.isZero()) { |
| 179 | return n; | 179 | return n; |
| 180 | } | 180 | } |
| ... | @@ -191,7 +191,7 @@ const ScalarDouble = struct { | ... | @@ -191,7 +191,7 @@ const ScalarDouble = struct { |
| 191 | debug.assert(bits > 0 and bits <= 512 and bits >= Fe.saturated_bits and bits <= Fe.saturated_bits * 3); | 191 | debug.assert(bits > 0 and bits <= 512 and bits >= Fe.saturated_bits and bits <= Fe.saturated_bits * 3); |
| 192 | 192 | ||
| 193 | var s = s_; | 193 | var s = s_; |
| 194 | if (endian == .Big) { | 194 | if (endian == .big) { |
| 195 | for (s_, 0..) |x, i| s[s.len - 1 - i] = x; | 195 | for (s_, 0..) |x, i| s[s.len - 1 - i] = x; |
| 196 | } | 196 | } |
| 197 | var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero, .x3 = Fe.zero }; | 197 | var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero, .x3 = Fe.zero }; |
| ... | @@ -199,19 +199,19 @@ const ScalarDouble = struct { | ... | @@ -199,19 +199,19 @@ const ScalarDouble = struct { |
| 199 | var b = [_]u8{0} ** encoded_length; | 199 | var b = [_]u8{0} ** encoded_length; |
| 200 | const len = @min(s.len, 24); | 200 | const len = @min(s.len, 24); |
| 201 | b[0..len].* = s[0..len].*; | 201 | b[0..len].* = s[0..len].*; |
| 202 | t.x1 = Fe.fromBytes(b, .Little) catch unreachable; | 202 | t.x1 = Fe.fromBytes(b, .little) catch unreachable; |
| 203 | } | 203 | } |
| 204 | if (s_.len >= 24) { | 204 | if (s_.len >= 24) { |
| 205 | var b = [_]u8{0} ** encoded_length; | 205 | var b = [_]u8{0} ** encoded_length; |
| 206 | const len = @min(s.len - 24, 24); | 206 | const len = @min(s.len - 24, 24); |
| 207 | b[0..len].* = s[24..][0..len].*; | 207 | b[0..len].* = s[24..][0..len].*; |
| 208 | t.x2 = Fe.fromBytes(b, .Little) catch unreachable; | 208 | t.x2 = Fe.fromBytes(b, .little) catch unreachable; |
| 209 | } | 209 | } |
| 210 | if (s_.len >= 48) { | 210 | if (s_.len >= 48) { |
| 211 | var b = [_]u8{0} ** encoded_length; | 211 | var b = [_]u8{0} ** encoded_length; |
| 212 | const len = s.len - 48; | 212 | const len = s.len - 48; |
| 213 | b[0..len].* = s[48..][0..len].*; | 213 | b[0..len].* = s[48..][0..len].*; |
| 214 | t.x3 = Fe.fromBytes(b, .Little) catch unreachable; | 214 | t.x3 = Fe.fromBytes(b, .little) catch unreachable; |
| 215 | } | 215 | } |
| 216 | return t; | 216 | return t; |
| 217 | } | 217 | } |
lib/std/crypto/pcurves/tests/p256.zig+20-20| ... | @@ -5,12 +5,12 @@ const testing = std.testing; | ... | @@ -5,12 +5,12 @@ const testing = std.testing; |
| 5 | const P256 = @import("../p256.zig").P256; | 5 | const P256 = @import("../p256.zig").P256; |
| 6 | 6 | ||
| 7 | test "p256 ECDH key exchange" { | 7 | test "p256 ECDH key exchange" { |
| 8 | const dha = P256.scalar.random(.Little); | 8 | const dha = P256.scalar.random(.little); |
| 9 | const dhb = P256.scalar.random(.Little); | 9 | const dhb = P256.scalar.random(.little); |
| 10 | const dhA = try P256.basePoint.mul(dha, .Little); | 10 | const dhA = try P256.basePoint.mul(dha, .little); |
| 11 | const dhB = try P256.basePoint.mul(dhb, .Little); | 11 | const dhB = try P256.basePoint.mul(dhb, .little); |
| 12 | const shareda = try dhA.mul(dhb, .Little); | 12 | const shareda = try dhA.mul(dhb, .little); |
| 13 | const sharedb = try dhB.mul(dha, .Little); | 13 | const sharedb = try dhB.mul(dha, .little); |
| 14 | try testing.expect(shareda.equivalent(sharedb)); | 14 | try testing.expect(shareda.equivalent(sharedb)); |
| 15 | } | 15 | } |
| 16 | 16 | ||
| ... | @@ -21,7 +21,7 @@ test "p256 point from affine coordinates" { | ... | @@ -21,7 +21,7 @@ test "p256 point from affine coordinates" { |
| 21 | _ = try fmt.hexToBytes(&xs, xh); | 21 | _ = try fmt.hexToBytes(&xs, xh); |
| 22 | var ys: [32]u8 = undefined; | 22 | var ys: [32]u8 = undefined; |
| 23 | _ = try fmt.hexToBytes(&ys, yh); | 23 | _ = try fmt.hexToBytes(&ys, yh); |
| 24 | var p = try P256.fromSerializedAffineCoordinates(xs, ys, .Big); | 24 | var p = try P256.fromSerializedAffineCoordinates(xs, ys, .big); |
| 25 | try testing.expect(p.equivalent(P256.basePoint)); | 25 | try testing.expect(p.equivalent(P256.basePoint)); |
| 26 | } | 26 | } |
| 27 | 27 | ||
| ... | @@ -44,7 +44,7 @@ test "p256 test vectors" { | ... | @@ -44,7 +44,7 @@ test "p256 test vectors" { |
| 44 | p = p.add(P256.basePoint); | 44 | p = p.add(P256.basePoint); |
| 45 | var xs: [32]u8 = undefined; | 45 | var xs: [32]u8 = undefined; |
| 46 | _ = try fmt.hexToBytes(&xs, xh); | 46 | _ = try fmt.hexToBytes(&xs, xh); |
| 47 | try testing.expectEqualSlices(u8, &x.toBytes(.Big), &xs); | 47 | try testing.expectEqualSlices(u8, &x.toBytes(.big), &xs); |
| 48 | } | 48 | } |
| 49 | } | 49 | } |
| 50 | 50 | ||
| ... | @@ -61,7 +61,7 @@ test "p256 test vectors - doubling" { | ... | @@ -61,7 +61,7 @@ test "p256 test vectors - doubling" { |
| 61 | p = p.dbl(); | 61 | p = p.dbl(); |
| 62 | var xs: [32]u8 = undefined; | 62 | var xs: [32]u8 = undefined; |
| 63 | _ = try fmt.hexToBytes(&xs, xh); | 63 | _ = try fmt.hexToBytes(&xs, xh); |
| 64 | try testing.expectEqualSlices(u8, &x.toBytes(.Big), &xs); | 64 | try testing.expectEqualSlices(u8, &x.toBytes(.big), &xs); |
| 65 | } | 65 | } |
| 66 | } | 66 | } |
| 67 | 67 | ||
| ... | @@ -80,20 +80,20 @@ test "p256 uncompressed sec1 encoding/decoding" { | ... | @@ -80,20 +80,20 @@ test "p256 uncompressed sec1 encoding/decoding" { |
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | test "p256 public key is the neutral element" { | 82 | test "p256 public key is the neutral element" { |
| 83 | const n = P256.scalar.Scalar.zero.toBytes(.Little); | 83 | const n = P256.scalar.Scalar.zero.toBytes(.little); |
| 84 | const p = P256.random(); | 84 | const p = P256.random(); |
| 85 | try testing.expectError(error.IdentityElement, p.mul(n, .Little)); | 85 | try testing.expectError(error.IdentityElement, p.mul(n, .little)); |
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | test "p256 public key is the neutral element (public verification)" { | 88 | test "p256 public key is the neutral element (public verification)" { |
| 89 | const n = P256.scalar.Scalar.zero.toBytes(.Little); | 89 | const n = P256.scalar.Scalar.zero.toBytes(.little); |
| 90 | const p = P256.random(); | 90 | const p = P256.random(); |
| 91 | try testing.expectError(error.IdentityElement, p.mulPublic(n, .Little)); | 91 | try testing.expectError(error.IdentityElement, p.mulPublic(n, .little)); |
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | test "p256 field element non-canonical encoding" { | 94 | test "p256 field element non-canonical encoding" { |
| 95 | const s = [_]u8{0xff} ** 32; | 95 | const s = [_]u8{0xff} ** 32; |
| 96 | try testing.expectError(error.NonCanonical, P256.Fe.fromBytes(s, .Little)); | 96 | try testing.expectError(error.NonCanonical, P256.Fe.fromBytes(s, .little)); |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | test "p256 neutral element decoding" { | 99 | test "p256 neutral element decoding" { |
| ... | @@ -107,8 +107,8 @@ test "p256 double base multiplication" { | ... | @@ -107,8 +107,8 @@ test "p256 double base multiplication" { |
| 107 | const p2 = P256.basePoint.dbl(); | 107 | const p2 = P256.basePoint.dbl(); |
| 108 | const s1 = [_]u8{0x01} ** 32; | 108 | const s1 = [_]u8{0x01} ** 32; |
| 109 | const s2 = [_]u8{0x02} ** 32; | 109 | const s2 = [_]u8{0x02} ** 32; |
| 110 | const pr1 = try P256.mulDoubleBasePublic(p1, s1, p2, s2, .Little); | 110 | const pr1 = try P256.mulDoubleBasePublic(p1, s1, p2, s2, .little); |
| 111 | const pr2 = (try p1.mul(s1, .Little)).add(try p2.mul(s2, .Little)); | 111 | const pr2 = (try p1.mul(s1, .little)).add(try p2.mul(s2, .little)); |
| 112 | try testing.expect(pr1.equivalent(pr2)); | 112 | try testing.expect(pr1.equivalent(pr2)); |
| 113 | } | 113 | } |
| 114 | 114 | ||
| ... | @@ -117,8 +117,8 @@ test "p256 double base multiplication with large scalars" { | ... | @@ -117,8 +117,8 @@ test "p256 double base multiplication with large scalars" { |
| 117 | const p2 = P256.basePoint.dbl(); | 117 | const p2 = P256.basePoint.dbl(); |
| 118 | const s1 = [_]u8{0xee} ** 32; | 118 | const s1 = [_]u8{0xee} ** 32; |
| 119 | const s2 = [_]u8{0xdd} ** 32; | 119 | const s2 = [_]u8{0xdd} ** 32; |
| 120 | const pr1 = try P256.mulDoubleBasePublic(p1, s1, p2, s2, .Little); | 120 | const pr1 = try P256.mulDoubleBasePublic(p1, s1, p2, s2, .little); |
| 121 | const pr2 = (try p1.mul(s1, .Little)).add(try p2.mul(s2, .Little)); | 121 | const pr2 = (try p1.mul(s1, .little)).add(try p2.mul(s2, .little)); |
| 122 | try testing.expect(pr1.equivalent(pr2)); | 122 | try testing.expect(pr1.equivalent(pr2)); |
| 123 | } | 123 | } |
| 124 | 124 | ||
| ... | @@ -130,9 +130,9 @@ test "p256 scalar inverse" { | ... | @@ -130,9 +130,9 @@ test "p256 scalar inverse" { |
| 130 | const scalar = try P256.scalar.Scalar.fromBytes(.{ | 130 | const scalar = try P256.scalar.Scalar.fromBytes(.{ |
| 131 | 0x94, 0xa1, 0xbb, 0xb1, 0x4b, 0x90, 0x6a, 0x61, 0xa2, 0x80, 0xf2, 0x45, 0xf9, 0xe9, 0x3c, 0x7f, | 131 | 0x94, 0xa1, 0xbb, 0xb1, 0x4b, 0x90, 0x6a, 0x61, 0xa2, 0x80, 0xf2, 0x45, 0xf9, 0xe9, 0x3c, 0x7f, |
| 132 | 0x3b, 0x4a, 0x62, 0x47, 0x82, 0x4f, 0x5d, 0x33, 0xb9, 0x67, 0x07, 0x87, 0x64, 0x2a, 0x68, 0xde, | 132 | 0x3b, 0x4a, 0x62, 0x47, 0x82, 0x4f, 0x5d, 0x33, 0xb9, 0x67, 0x07, 0x87, 0x64, 0x2a, 0x68, 0xde, |
| 133 | }, .Big); | 133 | }, .big); |
| 134 | const inverse = scalar.invert(); | 134 | const inverse = scalar.invert(); |
| 135 | try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big)); | 135 | try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.big)); |
| 136 | } | 136 | } |
| 137 | 137 | ||
| 138 | test "p256 scalar parity" { | 138 | test "p256 scalar parity" { |
lib/std/crypto/pcurves/tests/p384.zig+20-20| ... | @@ -5,12 +5,12 @@ const testing = std.testing; | ... | @@ -5,12 +5,12 @@ const testing = std.testing; |
| 5 | const P384 = @import("../p384.zig").P384; | 5 | const P384 = @import("../p384.zig").P384; |
| 6 | 6 | ||
| 7 | test "p384 ECDH key exchange" { | 7 | test "p384 ECDH key exchange" { |
| 8 | const dha = P384.scalar.random(.Little); | 8 | const dha = P384.scalar.random(.little); |
| 9 | const dhb = P384.scalar.random(.Little); | 9 | const dhb = P384.scalar.random(.little); |
| 10 | const dhA = try P384.basePoint.mul(dha, .Little); | 10 | const dhA = try P384.basePoint.mul(dha, .little); |
| 11 | const dhB = try P384.basePoint.mul(dhb, .Little); | 11 | const dhB = try P384.basePoint.mul(dhb, .little); |
| 12 | const shareda = try dhA.mul(dhb, .Little); | 12 | const shareda = try dhA.mul(dhb, .little); |
| 13 | const sharedb = try dhB.mul(dha, .Little); | 13 | const sharedb = try dhB.mul(dha, .little); |
| 14 | try testing.expect(shareda.equivalent(sharedb)); | 14 | try testing.expect(shareda.equivalent(sharedb)); |
| 15 | } | 15 | } |
| 16 | 16 | ||
| ... | @@ -21,7 +21,7 @@ test "p384 point from affine coordinates" { | ... | @@ -21,7 +21,7 @@ test "p384 point from affine coordinates" { |
| 21 | _ = try fmt.hexToBytes(&xs, xh); | 21 | _ = try fmt.hexToBytes(&xs, xh); |
| 22 | var ys: [48]u8 = undefined; | 22 | var ys: [48]u8 = undefined; |
| 23 | _ = try fmt.hexToBytes(&ys, yh); | 23 | _ = try fmt.hexToBytes(&ys, yh); |
| 24 | var p = try P384.fromSerializedAffineCoordinates(xs, ys, .Big); | 24 | var p = try P384.fromSerializedAffineCoordinates(xs, ys, .big); |
| 25 | try testing.expect(p.equivalent(P384.basePoint)); | 25 | try testing.expect(p.equivalent(P384.basePoint)); |
| 26 | } | 26 | } |
| 27 | 27 | ||
| ... | @@ -45,7 +45,7 @@ test "p384 test vectors" { | ... | @@ -45,7 +45,7 @@ test "p384 test vectors" { |
| 45 | p = p.add(P384.basePoint); | 45 | p = p.add(P384.basePoint); |
| 46 | var xs: [48]u8 = undefined; | 46 | var xs: [48]u8 = undefined; |
| 47 | _ = try fmt.hexToBytes(&xs, xh); | 47 | _ = try fmt.hexToBytes(&xs, xh); |
| 48 | try testing.expectEqualSlices(u8, &x.toBytes(.Big), &xs); | 48 | try testing.expectEqualSlices(u8, &x.toBytes(.big), &xs); |
| 49 | } | 49 | } |
| 50 | } | 50 | } |
| 51 | 51 | ||
| ... | @@ -62,7 +62,7 @@ test "p384 test vectors - doubling" { | ... | @@ -62,7 +62,7 @@ test "p384 test vectors - doubling" { |
| 62 | p = p.dbl(); | 62 | p = p.dbl(); |
| 63 | var xs: [48]u8 = undefined; | 63 | var xs: [48]u8 = undefined; |
| 64 | _ = try fmt.hexToBytes(&xs, xh); | 64 | _ = try fmt.hexToBytes(&xs, xh); |
| 65 | try testing.expectEqualSlices(u8, &x.toBytes(.Big), &xs); | 65 | try testing.expectEqualSlices(u8, &x.toBytes(.big), &xs); |
| 66 | } | 66 | } |
| 67 | } | 67 | } |
| 68 | 68 | ||
| ... | @@ -83,20 +83,20 @@ test "p384 uncompressed sec1 encoding/decoding" { | ... | @@ -83,20 +83,20 @@ test "p384 uncompressed sec1 encoding/decoding" { |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | test "p384 public key is the neutral element" { | 85 | test "p384 public key is the neutral element" { |
| 86 | const n = P384.scalar.Scalar.zero.toBytes(.Little); | 86 | const n = P384.scalar.Scalar.zero.toBytes(.little); |
| 87 | const p = P384.random(); | 87 | const p = P384.random(); |
| 88 | try testing.expectError(error.IdentityElement, p.mul(n, .Little)); | 88 | try testing.expectError(error.IdentityElement, p.mul(n, .little)); |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | test "p384 public key is the neutral element (public verification)" { | 91 | test "p384 public key is the neutral element (public verification)" { |
| 92 | const n = P384.scalar.Scalar.zero.toBytes(.Little); | 92 | const n = P384.scalar.Scalar.zero.toBytes(.little); |
| 93 | const p = P384.random(); | 93 | const p = P384.random(); |
| 94 | try testing.expectError(error.IdentityElement, p.mulPublic(n, .Little)); | 94 | try testing.expectError(error.IdentityElement, p.mulPublic(n, .little)); |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | test "p384 field element non-canonical encoding" { | 97 | test "p384 field element non-canonical encoding" { |
| 98 | const s = [_]u8{0xff} ** 48; | 98 | const s = [_]u8{0xff} ** 48; |
| 99 | try testing.expectError(error.NonCanonical, P384.Fe.fromBytes(s, .Little)); | 99 | try testing.expectError(error.NonCanonical, P384.Fe.fromBytes(s, .little)); |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | test "p384 neutral element decoding" { | 102 | test "p384 neutral element decoding" { |
| ... | @@ -110,8 +110,8 @@ test "p384 double base multiplication" { | ... | @@ -110,8 +110,8 @@ test "p384 double base multiplication" { |
| 110 | const p2 = P384.basePoint.dbl(); | 110 | const p2 = P384.basePoint.dbl(); |
| 111 | const s1 = [_]u8{0x01} ** 48; | 111 | const s1 = [_]u8{0x01} ** 48; |
| 112 | const s2 = [_]u8{0x02} ** 48; | 112 | const s2 = [_]u8{0x02} ** 48; |
| 113 | const pr1 = try P384.mulDoubleBasePublic(p1, s1, p2, s2, .Little); | 113 | const pr1 = try P384.mulDoubleBasePublic(p1, s1, p2, s2, .little); |
| 114 | const pr2 = (try p1.mul(s1, .Little)).add(try p2.mul(s2, .Little)); | 114 | const pr2 = (try p1.mul(s1, .little)).add(try p2.mul(s2, .little)); |
| 115 | try testing.expect(pr1.equivalent(pr2)); | 115 | try testing.expect(pr1.equivalent(pr2)); |
| 116 | } | 116 | } |
| 117 | 117 | ||
| ... | @@ -120,8 +120,8 @@ test "p384 double base multiplication with large scalars" { | ... | @@ -120,8 +120,8 @@ test "p384 double base multiplication with large scalars" { |
| 120 | const p2 = P384.basePoint.dbl(); | 120 | const p2 = P384.basePoint.dbl(); |
| 121 | const s1 = [_]u8{0xee} ** 48; | 121 | const s1 = [_]u8{0xee} ** 48; |
| 122 | const s2 = [_]u8{0xdd} ** 48; | 122 | const s2 = [_]u8{0xdd} ** 48; |
| 123 | const pr1 = try P384.mulDoubleBasePublic(p1, s1, p2, s2, .Little); | 123 | const pr1 = try P384.mulDoubleBasePublic(p1, s1, p2, s2, .little); |
| 124 | const pr2 = (try p1.mul(s1, .Little)).add(try p2.mul(s2, .Little)); | 124 | const pr2 = (try p1.mul(s1, .little)).add(try p2.mul(s2, .little)); |
| 125 | try testing.expect(pr1.equivalent(pr2)); | 125 | try testing.expect(pr1.equivalent(pr2)); |
| 126 | } | 126 | } |
| 127 | 127 | ||
| ... | @@ -134,10 +134,10 @@ test "p384 scalar inverse" { | ... | @@ -134,10 +134,10 @@ test "p384 scalar inverse" { |
| 134 | 0x94, 0xa1, 0xbb, 0xb1, 0x4b, 0x90, 0x6a, 0x61, 0xa2, 0x80, 0xf2, 0x45, 0xf9, 0xe9, 0x3c, 0x7f, | 134 | 0x94, 0xa1, 0xbb, 0xb1, 0x4b, 0x90, 0x6a, 0x61, 0xa2, 0x80, 0xf2, 0x45, 0xf9, 0xe9, 0x3c, 0x7f, |
| 135 | 0x3b, 0x4a, 0x62, 0x47, 0x82, 0x4f, 0x5d, 0x33, 0xb9, 0x67, 0x07, 0x87, 0x64, 0x2a, 0x68, 0xde, | 135 | 0x3b, 0x4a, 0x62, 0x47, 0x82, 0x4f, 0x5d, 0x33, 0xb9, 0x67, 0x07, 0x87, 0x64, 0x2a, 0x68, 0xde, |
| 136 | 0x38, 0x36, 0xe8, 0x0f, 0xa2, 0x84, 0x6b, 0x4e, 0xf3, 0x9a, 0x02, 0x31, 0x24, 0x41, 0x22, 0xca, | 136 | 0x38, 0x36, 0xe8, 0x0f, 0xa2, 0x84, 0x6b, 0x4e, 0xf3, 0x9a, 0x02, 0x31, 0x24, 0x41, 0x22, 0xca, |
| 137 | }, .Big); | 137 | }, .big); |
| 138 | const inverse = scalar.invert(); | 138 | const inverse = scalar.invert(); |
| 139 | const inverse2 = inverse.invert(); | 139 | const inverse2 = inverse.invert(); |
| 140 | try testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big)); | 140 | try testing.expectEqualSlices(u8, &out, &inverse.toBytes(.big)); |
| 141 | try testing.expect(inverse2.equivalent(scalar)); | 141 | try testing.expect(inverse2.equivalent(scalar)); |
| 142 | 142 | ||
| 143 | const sq = scalar.sq(); | 143 | const sq = scalar.sq(); |
lib/std/crypto/pcurves/tests/secp256k1.zig+24-24| ... | @@ -5,22 +5,22 @@ const testing = std.testing; | ... | @@ -5,22 +5,22 @@ const testing = std.testing; |
| 5 | const Secp256k1 = @import("../secp256k1.zig").Secp256k1; | 5 | const Secp256k1 = @import("../secp256k1.zig").Secp256k1; |
| 6 | 6 | ||
| 7 | test "secp256k1 ECDH key exchange" { | 7 | test "secp256k1 ECDH key exchange" { |
| 8 | const dha = Secp256k1.scalar.random(.Little); | 8 | const dha = Secp256k1.scalar.random(.little); |
| 9 | const dhb = Secp256k1.scalar.random(.Little); | 9 | const dhb = Secp256k1.scalar.random(.little); |
| 10 | const dhA = try Secp256k1.basePoint.mul(dha, .Little); | 10 | const dhA = try Secp256k1.basePoint.mul(dha, .little); |
| 11 | const dhB = try Secp256k1.basePoint.mul(dhb, .Little); | 11 | const dhB = try Secp256k1.basePoint.mul(dhb, .little); |
| 12 | const shareda = try dhA.mul(dhb, .Little); | 12 | const shareda = try dhA.mul(dhb, .little); |
| 13 | const sharedb = try dhB.mul(dha, .Little); | 13 | const sharedb = try dhB.mul(dha, .little); |
| 14 | try testing.expect(shareda.equivalent(sharedb)); | 14 | try testing.expect(shareda.equivalent(sharedb)); |
| 15 | } | 15 | } |
| 16 | 16 | ||
| 17 | test "secp256k1 ECDH key exchange including public multiplication" { | 17 | test "secp256k1 ECDH key exchange including public multiplication" { |
| 18 | const dha = Secp256k1.scalar.random(.Little); | 18 | const dha = Secp256k1.scalar.random(.little); |
| 19 | const dhb = Secp256k1.scalar.random(.Little); | 19 | const dhb = Secp256k1.scalar.random(.little); |
| 20 | const dhA = try Secp256k1.basePoint.mul(dha, .Little); | 20 | const dhA = try Secp256k1.basePoint.mul(dha, .little); |
| 21 | const dhB = try Secp256k1.basePoint.mulPublic(dhb, .Little); | 21 | const dhB = try Secp256k1.basePoint.mulPublic(dhb, .little); |
| 22 | const shareda = try dhA.mul(dhb, .Little); | 22 | const shareda = try dhA.mul(dhb, .little); |
| 23 | const sharedb = try dhB.mulPublic(dha, .Little); | 23 | const sharedb = try dhB.mulPublic(dha, .little); |
| 24 | try testing.expect(shareda.equivalent(sharedb)); | 24 | try testing.expect(shareda.equivalent(sharedb)); |
| 25 | } | 25 | } |
| 26 | 26 | ||
| ... | @@ -31,7 +31,7 @@ test "secp256k1 point from affine coordinates" { | ... | @@ -31,7 +31,7 @@ test "secp256k1 point from affine coordinates" { |
| 31 | _ = try fmt.hexToBytes(&xs, xh); | 31 | _ = try fmt.hexToBytes(&xs, xh); |
| 32 | var ys: [32]u8 = undefined; | 32 | var ys: [32]u8 = undefined; |
| 33 | _ = try fmt.hexToBytes(&ys, yh); | 33 | _ = try fmt.hexToBytes(&ys, yh); |
| 34 | var p = try Secp256k1.fromSerializedAffineCoordinates(xs, ys, .Big); | 34 | var p = try Secp256k1.fromSerializedAffineCoordinates(xs, ys, .big); |
| 35 | try testing.expect(p.equivalent(Secp256k1.basePoint)); | 35 | try testing.expect(p.equivalent(Secp256k1.basePoint)); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| ... | @@ -54,7 +54,7 @@ test "secp256k1 test vectors" { | ... | @@ -54,7 +54,7 @@ test "secp256k1 test vectors" { |
| 54 | p = p.add(Secp256k1.basePoint); | 54 | p = p.add(Secp256k1.basePoint); |
| 55 | var xs: [32]u8 = undefined; | 55 | var xs: [32]u8 = undefined; |
| 56 | _ = try fmt.hexToBytes(&xs, xh); | 56 | _ = try fmt.hexToBytes(&xs, xh); |
| 57 | try testing.expectEqualSlices(u8, &x.toBytes(.Big), &xs); | 57 | try testing.expectEqualSlices(u8, &x.toBytes(.big), &xs); |
| 58 | } | 58 | } |
| 59 | } | 59 | } |
| 60 | 60 | ||
| ... | @@ -72,7 +72,7 @@ test "secp256k1 test vectors - doubling" { | ... | @@ -72,7 +72,7 @@ test "secp256k1 test vectors - doubling" { |
| 72 | p = p.dbl(); | 72 | p = p.dbl(); |
| 73 | var xs: [32]u8 = undefined; | 73 | var xs: [32]u8 = undefined; |
| 74 | _ = try fmt.hexToBytes(&xs, xh); | 74 | _ = try fmt.hexToBytes(&xs, xh); |
| 75 | try testing.expectEqualSlices(u8, &x.toBytes(.Big), &xs); | 75 | try testing.expectEqualSlices(u8, &x.toBytes(.big), &xs); |
| 76 | } | 76 | } |
| 77 | } | 77 | } |
| 78 | 78 | ||
| ... | @@ -91,20 +91,20 @@ test "secp256k1 uncompressed sec1 encoding/decoding" { | ... | @@ -91,20 +91,20 @@ test "secp256k1 uncompressed sec1 encoding/decoding" { |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | test "secp256k1 public key is the neutral element" { | 93 | test "secp256k1 public key is the neutral element" { |
| 94 | const n = Secp256k1.scalar.Scalar.zero.toBytes(.Little); | 94 | const n = Secp256k1.scalar.Scalar.zero.toBytes(.little); |
| 95 | const p = Secp256k1.random(); | 95 | const p = Secp256k1.random(); |
| 96 | try testing.expectError(error.IdentityElement, p.mul(n, .Little)); | 96 | try testing.expectError(error.IdentityElement, p.mul(n, .little)); |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | test "secp256k1 public key is the neutral element (public verification)" { | 99 | test "secp256k1 public key is the neutral element (public verification)" { |
| 100 | const n = Secp256k1.scalar.Scalar.zero.toBytes(.Little); | 100 | const n = Secp256k1.scalar.Scalar.zero.toBytes(.little); |
| 101 | const p = Secp256k1.random(); | 101 | const p = Secp256k1.random(); |
| 102 | try testing.expectError(error.IdentityElement, p.mulPublic(n, .Little)); | 102 | try testing.expectError(error.IdentityElement, p.mulPublic(n, .little)); |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | test "secp256k1 field element non-canonical encoding" { | 105 | test "secp256k1 field element non-canonical encoding" { |
| 106 | const s = [_]u8{0xff} ** 32; | 106 | const s = [_]u8{0xff} ** 32; |
| 107 | try testing.expectError(error.NonCanonical, Secp256k1.Fe.fromBytes(s, .Little)); | 107 | try testing.expectError(error.NonCanonical, Secp256k1.Fe.fromBytes(s, .little)); |
| 108 | } | 108 | } |
| 109 | 109 | ||
| 110 | test "secp256k1 neutral element decoding" { | 110 | test "secp256k1 neutral element decoding" { |
| ... | @@ -118,8 +118,8 @@ test "secp256k1 double base multiplication" { | ... | @@ -118,8 +118,8 @@ test "secp256k1 double base multiplication" { |
| 118 | const p2 = Secp256k1.basePoint.dbl(); | 118 | const p2 = Secp256k1.basePoint.dbl(); |
| 119 | const s1 = [_]u8{0x01} ** 32; | 119 | const s1 = [_]u8{0x01} ** 32; |
| 120 | const s2 = [_]u8{0x02} ** 32; | 120 | const s2 = [_]u8{0x02} ** 32; |
| 121 | const pr1 = try Secp256k1.mulDoubleBasePublic(p1, s1, p2, s2, .Little); | 121 | const pr1 = try Secp256k1.mulDoubleBasePublic(p1, s1, p2, s2, .little); |
| 122 | const pr2 = (try p1.mul(s1, .Little)).add(try p2.mul(s2, .Little)); | 122 | const pr2 = (try p1.mul(s1, .little)).add(try p2.mul(s2, .little)); |
| 123 | try testing.expect(pr1.equivalent(pr2)); | 123 | try testing.expect(pr1.equivalent(pr2)); |
| 124 | } | 124 | } |
| 125 | 125 | ||
| ... | @@ -131,9 +131,9 @@ test "secp256k1 scalar inverse" { | ... | @@ -131,9 +131,9 @@ test "secp256k1 scalar inverse" { |
| 131 | const scalar = try Secp256k1.scalar.Scalar.fromBytes(.{ | 131 | const scalar = try Secp256k1.scalar.Scalar.fromBytes(.{ |
| 132 | 0x94, 0xa1, 0xbb, 0xb1, 0x4b, 0x90, 0x6a, 0x61, 0xa2, 0x80, 0xf2, 0x45, 0xf9, 0xe9, 0x3c, 0x7f, | 132 | 0x94, 0xa1, 0xbb, 0xb1, 0x4b, 0x90, 0x6a, 0x61, 0xa2, 0x80, 0xf2, 0x45, 0xf9, 0xe9, 0x3c, 0x7f, |
| 133 | 0x3b, 0x4a, 0x62, 0x47, 0x82, 0x4f, 0x5d, 0x33, 0xb9, 0x67, 0x07, 0x87, 0x64, 0x2a, 0x68, 0xde, | 133 | 0x3b, 0x4a, 0x62, 0x47, 0x82, 0x4f, 0x5d, 0x33, 0xb9, 0x67, 0x07, 0x87, 0x64, 0x2a, 0x68, 0xde, |
| 134 | }, .Big); | 134 | }, .big); |
| 135 | const inverse = scalar.invert(); | 135 | const inverse = scalar.invert(); |
| 136 | try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.Big)); | 136 | try std.testing.expectEqualSlices(u8, &out, &inverse.toBytes(.big)); |
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | test "secp256k1 scalar parity" { | 139 | test "secp256k1 scalar parity" { |
lib/std/crypto/poly1305.zig+8-8| ... | @@ -22,12 +22,12 @@ pub const Poly1305 = struct { | ... | @@ -22,12 +22,12 @@ pub const Poly1305 = struct { |
| 22 | pub fn init(key: *const [key_length]u8) Poly1305 { | 22 | pub fn init(key: *const [key_length]u8) Poly1305 { |
| 23 | return Poly1305{ | 23 | return Poly1305{ |
| 24 | .r = [_]u64{ | 24 | .r = [_]u64{ |
| 25 | mem.readIntLittle(u64, key[0..8]) & 0x0ffffffc0fffffff, | 25 | mem.readInt(u64, key[0..8], .little) & 0x0ffffffc0fffffff, |
| 26 | mem.readIntLittle(u64, key[8..16]) & 0x0ffffffc0ffffffc, | 26 | mem.readInt(u64, key[8..16], .little) & 0x0ffffffc0ffffffc, |
| 27 | }, | 27 | }, |
| 28 | .pad = [_]u64{ | 28 | .pad = [_]u64{ |
| 29 | mem.readIntLittle(u64, key[16..24]), | 29 | mem.readInt(u64, key[16..24], .little), |
| 30 | mem.readIntLittle(u64, key[24..32]), | 30 | mem.readInt(u64, key[24..32], .little), |
| 31 | }, | 31 | }, |
| 32 | }; | 32 | }; |
| 33 | } | 33 | } |
| ... | @@ -56,8 +56,8 @@ pub const Poly1305 = struct { | ... | @@ -56,8 +56,8 @@ pub const Poly1305 = struct { |
| 56 | var i: usize = 0; | 56 | var i: usize = 0; |
| 57 | 57 | ||
| 58 | while (i + block_length <= m.len) : (i += block_length) { | 58 | while (i + block_length <= m.len) : (i += block_length) { |
| 59 | const in0 = mem.readIntLittle(u64, m[i..][0..8]); | 59 | const in0 = mem.readInt(u64, m[i..][0..8], .little); |
| 60 | const in1 = mem.readIntLittle(u64, m[i + 8 ..][0..8]); | 60 | const in1 = mem.readInt(u64, m[i + 8 ..][0..8], .little); |
| 61 | 61 | ||
| 62 | // Add the input message to H | 62 | // Add the input message to H |
| 63 | var v = @addWithOverflow(h0, in0); | 63 | var v = @addWithOverflow(h0, in0); |
| ... | @@ -182,8 +182,8 @@ pub const Poly1305 = struct { | ... | @@ -182,8 +182,8 @@ pub const Poly1305 = struct { |
| 182 | const c = ((h0 & st.pad[0]) | ((h0 | st.pad[0]) & ~st.h[0])) >> 63; | 182 | const c = ((h0 & st.pad[0]) | ((h0 | st.pad[0]) & ~st.h[0])) >> 63; |
| 183 | st.h[1] = h1 +% st.pad[1] +% c; | 183 | st.h[1] = h1 +% st.pad[1] +% c; |
| 184 | 184 | ||
| 185 | mem.writeIntLittle(u64, out[0..8], st.h[0]); | 185 | mem.writeInt(u64, out[0..8], st.h[0], .little); |
| 186 | mem.writeIntLittle(u64, out[8..16], st.h[1]); | 186 | mem.writeInt(u64, out[8..16], st.h[1], .little); |
| 187 | 187 | ||
| 188 | utils.secureZero(u8, @as([*]u8, @ptrCast(st))[0..@sizeOf(Poly1305)]); | 188 | utils.secureZero(u8, @as([*]u8, @ptrCast(st))[0..@sizeOf(Poly1305)]); |
| 189 | } | 189 | } |
lib/std/crypto/salsa20.zig+34-34| ... | @@ -29,10 +29,10 @@ fn SalsaVecImpl(comptime rounds: comptime_int) type { | ... | @@ -29,10 +29,10 @@ fn SalsaVecImpl(comptime rounds: comptime_int) type { |
| 29 | fn initContext(key: [8]u32, d: [4]u32) BlockVec { | 29 | fn initContext(key: [8]u32, d: [4]u32) BlockVec { |
| 30 | const c = "expand 32-byte k"; | 30 | const c = "expand 32-byte k"; |
| 31 | const constant_le = comptime [4]u32{ | 31 | const constant_le = comptime [4]u32{ |
| 32 | mem.readIntLittle(u32, c[0..4]), | 32 | mem.readInt(u32, c[0..4], .little), |
| 33 | mem.readIntLittle(u32, c[4..8]), | 33 | mem.readInt(u32, c[4..8], .little), |
| 34 | mem.readIntLittle(u32, c[8..12]), | 34 | mem.readInt(u32, c[8..12], .little), |
| 35 | mem.readIntLittle(u32, c[12..16]), | 35 | mem.readInt(u32, c[12..16], .little), |
| 36 | }; | 36 | }; |
| 37 | return BlockVec{ | 37 | return BlockVec{ |
| 38 | Lane{ key[0], key[1], key[2], key[3] }, | 38 | Lane{ key[0], key[1], key[2], key[3] }, |
| ... | @@ -112,10 +112,10 @@ fn SalsaVecImpl(comptime rounds: comptime_int) type { | ... | @@ -112,10 +112,10 @@ fn SalsaVecImpl(comptime rounds: comptime_int) type { |
| 112 | fn hashToBytes(out: *[64]u8, x: BlockVec) void { | 112 | fn hashToBytes(out: *[64]u8, x: BlockVec) void { |
| 113 | var i: usize = 0; | 113 | var i: usize = 0; |
| 114 | while (i < 4) : (i += 1) { | 114 | while (i < 4) : (i += 1) { |
| 115 | mem.writeIntLittle(u32, out[16 * i + 0 ..][0..4], x[i][0]); | 115 | mem.writeInt(u32, out[16 * i + 0 ..][0..4], x[i][0], .little); |
| 116 | mem.writeIntLittle(u32, out[16 * i + 4 ..][0..4], x[i][1]); | 116 | mem.writeInt(u32, out[16 * i + 4 ..][0..4], x[i][1], .little); |
| 117 | mem.writeIntLittle(u32, out[16 * i + 8 ..][0..4], x[i][2]); | 117 | mem.writeInt(u32, out[16 * i + 8 ..][0..4], x[i][2], .little); |
| 118 | mem.writeIntLittle(u32, out[16 * i + 12 ..][0..4], x[i][3]); | 118 | mem.writeInt(u32, out[16 * i + 12 ..][0..4], x[i][3], .little); |
| 119 | } | 119 | } |
| 120 | } | 120 | } |
| 121 | 121 | ||
| ... | @@ -158,20 +158,20 @@ fn SalsaVecImpl(comptime rounds: comptime_int) type { | ... | @@ -158,20 +158,20 @@ fn SalsaVecImpl(comptime rounds: comptime_int) type { |
| 158 | fn hsalsa(input: [16]u8, key: [32]u8) [32]u8 { | 158 | fn hsalsa(input: [16]u8, key: [32]u8) [32]u8 { |
| 159 | var c: [4]u32 = undefined; | 159 | var c: [4]u32 = undefined; |
| 160 | for (c, 0..) |_, i| { | 160 | for (c, 0..) |_, i| { |
| 161 | c[i] = mem.readIntLittle(u32, input[4 * i ..][0..4]); | 161 | c[i] = mem.readInt(u32, input[4 * i ..][0..4], .little); |
| 162 | } | 162 | } |
| 163 | const ctx = initContext(keyToWords(key), c); | 163 | const ctx = initContext(keyToWords(key), c); |
| 164 | var x: BlockVec = undefined; | 164 | var x: BlockVec = undefined; |
| 165 | salsaCore(x[0..], ctx, false); | 165 | salsaCore(x[0..], ctx, false); |
| 166 | var out: [32]u8 = undefined; | 166 | var out: [32]u8 = undefined; |
| 167 | mem.writeIntLittle(u32, out[0..4], x[0][0]); | 167 | mem.writeInt(u32, out[0..4], x[0][0], .little); |
| 168 | mem.writeIntLittle(u32, out[4..8], x[1][1]); | 168 | mem.writeInt(u32, out[4..8], x[1][1], .little); |
| 169 | mem.writeIntLittle(u32, out[8..12], x[2][2]); | 169 | mem.writeInt(u32, out[8..12], x[2][2], .little); |
| 170 | mem.writeIntLittle(u32, out[12..16], x[3][3]); | 170 | mem.writeInt(u32, out[12..16], x[3][3], .little); |
| 171 | mem.writeIntLittle(u32, out[16..20], x[1][2]); | 171 | mem.writeInt(u32, out[16..20], x[1][2], .little); |
| 172 | mem.writeIntLittle(u32, out[20..24], x[1][3]); | 172 | mem.writeInt(u32, out[20..24], x[1][3], .little); |
| 173 | mem.writeIntLittle(u32, out[24..28], x[2][0]); | 173 | mem.writeInt(u32, out[24..28], x[2][0], .little); |
| 174 | mem.writeIntLittle(u32, out[28..32], x[2][1]); | 174 | mem.writeInt(u32, out[28..32], x[2][1], .little); |
| 175 | return out; | 175 | return out; |
| 176 | } | 176 | } |
| 177 | }; | 177 | }; |
| ... | @@ -184,10 +184,10 @@ fn SalsaNonVecImpl(comptime rounds: comptime_int) type { | ... | @@ -184,10 +184,10 @@ fn SalsaNonVecImpl(comptime rounds: comptime_int) type { |
| 184 | fn initContext(key: [8]u32, d: [4]u32) BlockVec { | 184 | fn initContext(key: [8]u32, d: [4]u32) BlockVec { |
| 185 | const c = "expand 32-byte k"; | 185 | const c = "expand 32-byte k"; |
| 186 | const constant_le = comptime [4]u32{ | 186 | const constant_le = comptime [4]u32{ |
| 187 | mem.readIntLittle(u32, c[0..4]), | 187 | mem.readInt(u32, c[0..4], .little), |
| 188 | mem.readIntLittle(u32, c[4..8]), | 188 | mem.readInt(u32, c[4..8], .little), |
| 189 | mem.readIntLittle(u32, c[8..12]), | 189 | mem.readInt(u32, c[8..12], .little), |
| 190 | mem.readIntLittle(u32, c[12..16]), | 190 | mem.readInt(u32, c[12..16], .little), |
| 191 | }; | 191 | }; |
| 192 | return BlockVec{ | 192 | return BlockVec{ |
| 193 | constant_le[0], key[0], key[1], key[2], | 193 | constant_le[0], key[0], key[1], key[2], |
| ... | @@ -241,7 +241,7 @@ fn SalsaNonVecImpl(comptime rounds: comptime_int) type { | ... | @@ -241,7 +241,7 @@ fn SalsaNonVecImpl(comptime rounds: comptime_int) type { |
| 241 | 241 | ||
| 242 | fn hashToBytes(out: *[64]u8, x: BlockVec) void { | 242 | fn hashToBytes(out: *[64]u8, x: BlockVec) void { |
| 243 | for (x, 0..) |w, i| { | 243 | for (x, 0..) |w, i| { |
| 244 | mem.writeIntLittle(u32, out[i * 4 ..][0..4], w); | 244 | mem.writeInt(u32, out[i * 4 ..][0..4], w, .little); |
| 245 | } | 245 | } |
| 246 | } | 246 | } |
| 247 | 247 | ||
| ... | @@ -283,20 +283,20 @@ fn SalsaNonVecImpl(comptime rounds: comptime_int) type { | ... | @@ -283,20 +283,20 @@ fn SalsaNonVecImpl(comptime rounds: comptime_int) type { |
| 283 | fn hsalsa(input: [16]u8, key: [32]u8) [32]u8 { | 283 | fn hsalsa(input: [16]u8, key: [32]u8) [32]u8 { |
| 284 | var c: [4]u32 = undefined; | 284 | var c: [4]u32 = undefined; |
| 285 | for (c, 0..) |_, i| { | 285 | for (c, 0..) |_, i| { |
| 286 | c[i] = mem.readIntLittle(u32, input[4 * i ..][0..4]); | 286 | c[i] = mem.readInt(u32, input[4 * i ..][0..4], .little); |
| 287 | } | 287 | } |
| 288 | const ctx = initContext(keyToWords(key), c); | 288 | const ctx = initContext(keyToWords(key), c); |
| 289 | var x: BlockVec = undefined; | 289 | var x: BlockVec = undefined; |
| 290 | salsaCore(x[0..], ctx, false); | 290 | salsaCore(x[0..], ctx, false); |
| 291 | var out: [32]u8 = undefined; | 291 | var out: [32]u8 = undefined; |
| 292 | mem.writeIntLittle(u32, out[0..4], x[0]); | 292 | mem.writeInt(u32, out[0..4], x[0], .little); |
| 293 | mem.writeIntLittle(u32, out[4..8], x[5]); | 293 | mem.writeInt(u32, out[4..8], x[5], .little); |
| 294 | mem.writeIntLittle(u32, out[8..12], x[10]); | 294 | mem.writeInt(u32, out[8..12], x[10], .little); |
| 295 | mem.writeIntLittle(u32, out[12..16], x[15]); | 295 | mem.writeInt(u32, out[12..16], x[15], .little); |
| 296 | mem.writeIntLittle(u32, out[16..20], x[6]); | 296 | mem.writeInt(u32, out[16..20], x[6], .little); |
| 297 | mem.writeIntLittle(u32, out[20..24], x[7]); | 297 | mem.writeInt(u32, out[20..24], x[7], .little); |
| 298 | mem.writeIntLittle(u32, out[24..28], x[8]); | 298 | mem.writeInt(u32, out[24..28], x[8], .little); |
| 299 | mem.writeIntLittle(u32, out[28..32], x[9]); | 299 | mem.writeInt(u32, out[28..32], x[9], .little); |
| 300 | return out; | 300 | return out; |
| 301 | } | 301 | } |
| 302 | }; | 302 | }; |
| ... | @@ -308,7 +308,7 @@ fn keyToWords(key: [32]u8) [8]u32 { | ... | @@ -308,7 +308,7 @@ fn keyToWords(key: [32]u8) [8]u32 { |
| 308 | var k: [8]u32 = undefined; | 308 | var k: [8]u32 = undefined; |
| 309 | var i: usize = 0; | 309 | var i: usize = 0; |
| 310 | while (i < 8) : (i += 1) { | 310 | while (i < 8) : (i += 1) { |
| 311 | k[i] = mem.readIntLittle(u32, key[i * 4 ..][0..4]); | 311 | k[i] = mem.readInt(u32, key[i * 4 ..][0..4], .little); |
| 312 | } | 312 | } |
| 313 | return k; | 313 | return k; |
| 314 | } | 314 | } |
| ... | @@ -335,8 +335,8 @@ pub fn Salsa(comptime rounds: comptime_int) type { | ... | @@ -335,8 +335,8 @@ pub fn Salsa(comptime rounds: comptime_int) type { |
| 335 | debug.assert(in.len == out.len); | 335 | debug.assert(in.len == out.len); |
| 336 | 336 | ||
| 337 | var d: [4]u32 = undefined; | 337 | var d: [4]u32 = undefined; |
| 338 | d[0] = mem.readIntLittle(u32, nonce[0..4]); | 338 | d[0] = mem.readInt(u32, nonce[0..4], .little); |
| 339 | d[1] = mem.readIntLittle(u32, nonce[4..8]); | 339 | d[1] = mem.readInt(u32, nonce[4..8], .little); |
| 340 | d[2] = @as(u32, @truncate(counter)); | 340 | d[2] = @as(u32, @truncate(counter)); |
| 341 | d[3] = @as(u32, @truncate(counter >> 32)); | 341 | d[3] = @as(u32, @truncate(counter >> 32)); |
| 342 | SalsaImpl(rounds).salsaXor(out, in, keyToWords(key), d); | 342 | SalsaImpl(rounds).salsaXor(out, in, keyToWords(key), d); |
lib/std/crypto/scrypt.zig+4-4| ... | @@ -91,7 +91,7 @@ fn smix(b: []align(16) u8, r: u30, n: usize, v: []align(16) u32, xy: []align(16) | ... | @@ -91,7 +91,7 @@ fn smix(b: []align(16) u8, r: u30, n: usize, v: []align(16) u32, xy: []align(16) |
| 91 | var y: []align(16) u32 = @alignCast(xy[32 * r ..]); | 91 | var y: []align(16) u32 = @alignCast(xy[32 * r ..]); |
| 92 | 92 | ||
| 93 | for (x, 0..) |*v1, j| { | 93 | for (x, 0..) |*v1, j| { |
| 94 | v1.* = mem.readIntSliceLittle(u32, b[4 * j ..]); | 94 | v1.* = mem.readInt(u32, b[4 * j ..][0..4], .little); |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | var tmp: [16]u32 align(16) = undefined; | 97 | var tmp: [16]u32 align(16) = undefined; |
| ... | @@ -116,7 +116,7 @@ fn smix(b: []align(16) u8, r: u30, n: usize, v: []align(16) u32, xy: []align(16) | ... | @@ -116,7 +116,7 @@ fn smix(b: []align(16) u8, r: u30, n: usize, v: []align(16) u32, xy: []align(16) |
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | for (x, 0..) |v1, j| { | 118 | for (x, 0..) |v1, j| { |
| 119 | mem.writeIntLittle(u32, b[4 * j ..][0..4], v1); | 119 | mem.writeInt(u32, b[4 * j ..][0..4], v1, .little); |
| 120 | } | 120 | } |
| 121 | } | 121 | } |
| 122 | 122 | ||
| ... | @@ -361,7 +361,7 @@ const crypt_format = struct { | ... | @@ -361,7 +361,7 @@ const crypt_format = struct { |
| 361 | std.debug.assert(dst.len == decodedLen(src.len)); | 361 | std.debug.assert(dst.len == decodedLen(src.len)); |
| 362 | var i: usize = 0; | 362 | var i: usize = 0; |
| 363 | while (i < src.len / 4) : (i += 1) { | 363 | while (i < src.len / 4) : (i += 1) { |
| 364 | mem.writeIntSliceLittle(u24, dst[i * 3 ..], try intDecode(u24, src[i * 4 ..][0..4])); | 364 | mem.writeInt(u24, dst[i * 3 ..][0..3], try intDecode(u24, src[i * 4 ..][0..4]), .little); |
| 365 | } | 365 | } |
| 366 | const leftover = src[i * 4 ..]; | 366 | const leftover = src[i * 4 ..]; |
| 367 | var v: u24 = 0; | 367 | var v: u24 = 0; |
| ... | @@ -377,7 +377,7 @@ const crypt_format = struct { | ... | @@ -377,7 +377,7 @@ const crypt_format = struct { |
| 377 | std.debug.assert(dst.len == encodedLen(src.len)); | 377 | std.debug.assert(dst.len == encodedLen(src.len)); |
| 378 | var i: usize = 0; | 378 | var i: usize = 0; |
| 379 | while (i < src.len / 3) : (i += 1) { | 379 | while (i < src.len / 3) : (i += 1) { |
| 380 | intEncode(dst[i * 4 ..][0..4], mem.readIntSliceLittle(u24, src[i * 3 ..])); | 380 | intEncode(dst[i * 4 ..][0..4], mem.readInt(u24, src[i * 3 ..][0..3], .little)); |
| 381 | } | 381 | } |
| 382 | const leftover = src[i * 3 ..]; | 382 | const leftover = src[i * 3 ..]; |
| 383 | var v: u24 = 0; | 383 | var v: u24 = 0; |
lib/std/crypto/sha1.zig+2-2| ... | @@ -111,7 +111,7 @@ pub const Sha1 = struct { | ... | @@ -111,7 +111,7 @@ pub const Sha1 = struct { |
| 111 | d.round(d.buf[0..]); | 111 | d.round(d.buf[0..]); |
| 112 | 112 | ||
| 113 | for (d.s, 0..) |s, j| { | 113 | for (d.s, 0..) |s, j| { |
| 114 | mem.writeIntBig(u32, out[4 * j ..][0..4], s); | 114 | mem.writeInt(u32, out[4 * j ..][0..4], s, .big); |
| 115 | } | 115 | } |
| 116 | } | 116 | } |
| 117 | 117 | ||
| ... | @@ -151,7 +151,7 @@ pub const Sha1 = struct { | ... | @@ -151,7 +151,7 @@ pub const Sha1 = struct { |
| 151 | roundParam(0, 1, 2, 3, 4, 15), | 151 | roundParam(0, 1, 2, 3, 4, 15), |
| 152 | }; | 152 | }; |
| 153 | inline for (round0a) |r| { | 153 | inline for (round0a) |r| { |
| 154 | s[r.i] = mem.readIntBig(u32, b[r.i * 4 ..][0..4]); | 154 | s[r.i] = mem.readInt(u32, b[r.i * 4 ..][0..4], .big); |
| 155 | 155 | ||
| 156 | v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], @as(u32, 5)) +% 0x5A827999 +% s[r.i & 0xf] +% ((v[r.b] & v[r.c]) | (~v[r.b] & v[r.d])); | 156 | v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], @as(u32, 5)) +% 0x5A827999 +% s[r.i & 0xf] +% ((v[r.b] & v[r.c]) | (~v[r.b] & v[r.d])); |
| 157 | v[r.b] = math.rotl(u32, v[r.b], @as(u32, 30)); | 157 | v[r.b] = math.rotl(u32, v[r.b], @as(u32, 30)); |
lib/std/crypto/sha2.zig+4-4| ... | @@ -171,7 +171,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { | ... | @@ -171,7 +171,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { |
| 171 | const rr = d.s[0 .. params.digest_bits / 32]; | 171 | const rr = d.s[0 .. params.digest_bits / 32]; |
| 172 | 172 | ||
| 173 | for (rr, 0..) |s, j| { | 173 | for (rr, 0..) |s, j| { |
| 174 | mem.writeIntBig(u32, out[4 * j ..][0..4], s); | 174 | mem.writeInt(u32, out[4 * j ..][0..4], s, .big); |
| 175 | } | 175 | } |
| 176 | } | 176 | } |
| 177 | 177 | ||
| ... | @@ -195,7 +195,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { | ... | @@ -195,7 +195,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { |
| 195 | fn round(d: *Self, b: *const [64]u8) void { | 195 | fn round(d: *Self, b: *const [64]u8) void { |
| 196 | var s: [64]u32 align(16) = undefined; | 196 | var s: [64]u32 align(16) = undefined; |
| 197 | for (@as(*align(1) const [16]u32, @ptrCast(b)), 0..) |*elem, i| { | 197 | for (@as(*align(1) const [16]u32, @ptrCast(b)), 0..) |*elem, i| { |
| 198 | s[i] = mem.readIntBig(u32, mem.asBytes(elem)); | 198 | s[i] = mem.readInt(u32, mem.asBytes(elem), .big); |
| 199 | } | 199 | } |
| 200 | 200 | ||
| 201 | if (!@inComptime()) { | 201 | if (!@inComptime()) { |
| ... | @@ -663,7 +663,7 @@ fn Sha2x64(comptime params: Sha2Params64) type { | ... | @@ -663,7 +663,7 @@ fn Sha2x64(comptime params: Sha2Params64) type { |
| 663 | const rr = d.s[0 .. params.digest_bits / 64]; | 663 | const rr = d.s[0 .. params.digest_bits / 64]; |
| 664 | 664 | ||
| 665 | for (rr, 0..) |s, j| { | 665 | for (rr, 0..) |s, j| { |
| 666 | mem.writeIntBig(u64, out[8 * j ..][0..8], s); | 666 | mem.writeInt(u64, out[8 * j ..][0..8], s, .big); |
| 667 | } | 667 | } |
| 668 | } | 668 | } |
| 669 | 669 | ||
| ... | @@ -678,7 +678,7 @@ fn Sha2x64(comptime params: Sha2Params64) type { | ... | @@ -678,7 +678,7 @@ fn Sha2x64(comptime params: Sha2Params64) type { |
| 678 | 678 | ||
| 679 | var i: usize = 0; | 679 | var i: usize = 0; |
| 680 | while (i < 16) : (i += 1) { | 680 | while (i < 16) : (i += 1) { |
| 681 | s[i] = mem.readIntBig(u64, b[i * 8 ..][0..8]); | 681 | s[i] = mem.readInt(u64, b[i * 8 ..][0..8], .big); |
| 682 | } | 682 | } |
| 683 | while (i < 80) : (i += 1) { | 683 | while (i < 80) : (i += 1) { |
| 684 | s[i] = s[i - 16] +% s[i - 7] +% | 684 | s[i] = s[i - 16] +% s[i - 7] +% |
lib/std/crypto/siphash.zig+4-4| ... | @@ -56,8 +56,8 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round | ... | @@ -56,8 +56,8 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round |
| 56 | msg_len: u8, | 56 | msg_len: u8, |
| 57 | 57 | ||
| 58 | fn init(key: *const [key_length]u8) Self { | 58 | fn init(key: *const [key_length]u8) Self { |
| 59 | const k0 = mem.readIntLittle(u64, key[0..8]); | 59 | const k0 = mem.readInt(u64, key[0..8], .little); |
| 60 | const k1 = mem.readIntLittle(u64, key[8..16]); | 60 | const k1 = mem.readInt(u64, key[8..16], .little); |
| 61 | 61 | ||
| 62 | var d = Self{ | 62 | var d = Self{ |
| 63 | .v0 = k0 ^ 0x736f6d6570736575, | 63 | .v0 = k0 ^ 0x736f6d6570736575, |
| ... | @@ -124,7 +124,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round | ... | @@ -124,7 +124,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round |
| 124 | } | 124 | } |
| 125 | 125 | ||
| 126 | fn round(self: *Self, b: [8]u8) void { | 126 | fn round(self: *Self, b: [8]u8) void { |
| 127 | const m = mem.readIntLittle(u64, &b); | 127 | const m = mem.readInt(u64, &b, .little); |
| 128 | self.v3 ^= m; | 128 | self.v3 ^= m; |
| 129 | 129 | ||
| 130 | comptime var i: usize = 0; | 130 | comptime var i: usize = 0; |
| ... | @@ -213,7 +213,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) | ... | @@ -213,7 +213,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) |
| 213 | /// Return an authentication tag for the current state | 213 | /// Return an authentication tag for the current state |
| 214 | /// Assumes `out` is less than or equal to `mac_length`. | 214 | /// Assumes `out` is less than or equal to `mac_length`. |
| 215 | pub fn final(self: *Self, out: *[mac_length]u8) void { | 215 | pub fn final(self: *Self, out: *[mac_length]u8) void { |
| 216 | mem.writeIntLittle(T, out, self.state.final(self.buf[0..self.buf_len])); | 216 | mem.writeInt(T, out, self.state.final(self.buf[0..self.buf_len]), .little); |
| 217 | } | 217 | } |
| 218 | 218 | ||
| 219 | pub fn finalResult(self: *Self) [mac_length]u8 { | 219 | pub fn finalResult(self: *Self) [mac_length]u8 { |
lib/std/crypto/tls.zig+1-1| ... | @@ -370,7 +370,7 @@ pub fn hkdfExpandLabel( | ... | @@ -370,7 +370,7 @@ pub fn hkdfExpandLabel( |
| 370 | const max_context_len = 255; | 370 | const max_context_len = 255; |
| 371 | const tls13 = "tls13 "; | 371 | const tls13 = "tls13 "; |
| 372 | var buf: [2 + 1 + tls13.len + max_label_len + 1 + max_context_len]u8 = undefined; | 372 | var buf: [2 + 1 + tls13.len + max_label_len + 1 + max_context_len]u8 = undefined; |
| 373 | mem.writeIntBig(u16, buf[0..2], len); | 373 | mem.writeInt(u16, buf[0..2], len, .big); |
| 374 | buf[2] = @as(u8, @intCast(tls13.len + label.len)); | 374 | buf[2] = @as(u8, @intCast(tls13.len + label.len)); |
| 375 | buf[3..][0..tls13.len].* = tls13.*; | 375 | buf[3..][0..tls13.len].* = tls13.*; |
| 376 | var i: usize = 3 + tls13.len; | 376 | var i: usize = 3 + tls13.len; |
lib/std/crypto/tls/Client.zig+7-7| ... | @@ -332,10 +332,10 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In | ... | @@ -332,10 +332,10 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In |
| 332 | const pk = PublicKey.fromSec1(server_pub_key) catch { | 332 | const pk = PublicKey.fromSec1(server_pub_key) catch { |
| 333 | return error.TlsDecryptFailure; | 333 | return error.TlsDecryptFailure; |
| 334 | }; | 334 | }; |
| 335 | const mul = pk.p.mulPublic(secp256r1_kp.secret_key.bytes, .Big) catch { | 335 | const mul = pk.p.mulPublic(secp256r1_kp.secret_key.bytes, .big) catch { |
| 336 | return error.TlsDecryptFailure; | 336 | return error.TlsDecryptFailure; |
| 337 | }; | 337 | }; |
| 338 | shared_key = &mul.affineCoordinates().x.toBytes(.Big); | 338 | shared_key = &mul.affineCoordinates().x.toBytes(.big); |
| 339 | }, | 339 | }, |
| 340 | else => { | 340 | else => { |
| 341 | return error.TlsIllegalParameter; | 341 | return error.TlsIllegalParameter; |
| ... | @@ -1049,10 +1049,10 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) | ... | @@ -1049,10 +1049,10 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) |
| 1049 | } | 1049 | } |
| 1050 | const ct: tls.ContentType = @enumFromInt(frag[in]); | 1050 | const ct: tls.ContentType = @enumFromInt(frag[in]); |
| 1051 | in += 1; | 1051 | in += 1; |
| 1052 | const legacy_version = mem.readIntBig(u16, frag[in..][0..2]); | 1052 | const legacy_version = mem.readInt(u16, frag[in..][0..2], .big); |
| 1053 | in += 2; | 1053 | in += 2; |
| 1054 | _ = legacy_version; | 1054 | _ = legacy_version; |
| 1055 | const record_len = mem.readIntBig(u16, frag[in..][0..2]); | 1055 | const record_len = mem.readInt(u16, frag[in..][0..2], .big); |
| 1056 | if (record_len > max_ciphertext_len) return error.TlsRecordOverflow; | 1056 | if (record_len > max_ciphertext_len) return error.TlsRecordOverflow; |
| 1057 | in += 2; | 1057 | in += 2; |
| 1058 | const end = in + record_len; | 1058 | const end = in + record_len; |
| ... | @@ -1136,7 +1136,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) | ... | @@ -1136,7 +1136,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) |
| 1136 | while (true) { | 1136 | while (true) { |
| 1137 | const handshake_type: tls.HandshakeType = @enumFromInt(cleartext[ct_i]); | 1137 | const handshake_type: tls.HandshakeType = @enumFromInt(cleartext[ct_i]); |
| 1138 | ct_i += 1; | 1138 | ct_i += 1; |
| 1139 | const handshake_len = mem.readIntBig(u24, cleartext[ct_i..][0..3]); | 1139 | const handshake_len = mem.readInt(u24, cleartext[ct_i..][0..3], .big); |
| 1140 | ct_i += 3; | 1140 | ct_i += 3; |
| 1141 | const next_handshake_i = ct_i + handshake_len; | 1141 | const next_handshake_i = ct_i + handshake_len; |
| 1142 | if (next_handshake_i > cleartext.len - 1) | 1142 | if (next_handshake_i > cleartext.len - 1) |
| ... | @@ -1284,8 +1284,8 @@ const native_endian = builtin.cpu.arch.endian(); | ... | @@ -1284,8 +1284,8 @@ const native_endian = builtin.cpu.arch.endian(); |
| 1284 | 1284 | ||
| 1285 | inline fn big(x: anytype) @TypeOf(x) { | 1285 | inline fn big(x: anytype) @TypeOf(x) { |
| 1286 | return switch (native_endian) { | 1286 | return switch (native_endian) { |
| 1287 | .Big => x, | 1287 | .big => x, |
| 1288 | .Little => @byteSwap(x), | 1288 | .little => @byteSwap(x), |
| 1289 | }; | 1289 | }; |
| 1290 | } | 1290 | } |
| 1291 | 1291 |
lib/std/crypto/utils.zig+10-10| ... | @@ -54,7 +54,7 @@ pub fn timingSafeCompare(comptime T: type, a: []const T, b: []const T, endian: E | ... | @@ -54,7 +54,7 @@ pub fn timingSafeCompare(comptime T: type, a: []const T, b: []const T, endian: E |
| 54 | const Cext = std.meta.Int(.unsigned, bits + 1); | 54 | const Cext = std.meta.Int(.unsigned, bits + 1); |
| 55 | var gt: T = 0; | 55 | var gt: T = 0; |
| 56 | var eq: T = 1; | 56 | var eq: T = 1; |
| 57 | if (endian == .Little) { | 57 | if (endian == .little) { |
| 58 | var i = a.len; | 58 | var i = a.len; |
| 59 | while (i != 0) { | 59 | while (i != 0) { |
| 60 | i -= 1; | 60 | i -= 1; |
| ... | @@ -84,7 +84,7 @@ pub fn timingSafeAdd(comptime T: type, a: []const T, b: []const T, result: []T, | ... | @@ -84,7 +84,7 @@ pub fn timingSafeAdd(comptime T: type, a: []const T, b: []const T, result: []T, |
| 84 | const len = a.len; | 84 | const len = a.len; |
| 85 | debug.assert(len == b.len and len == result.len); | 85 | debug.assert(len == b.len and len == result.len); |
| 86 | var carry: u1 = 0; | 86 | var carry: u1 = 0; |
| 87 | if (endian == .Little) { | 87 | if (endian == .little) { |
| 88 | var i: usize = 0; | 88 | var i: usize = 0; |
| 89 | while (i < len) : (i += 1) { | 89 | while (i < len) : (i += 1) { |
| 90 | const ov1 = @addWithOverflow(a[i], b[i]); | 90 | const ov1 = @addWithOverflow(a[i], b[i]); |
| ... | @@ -111,7 +111,7 @@ pub fn timingSafeSub(comptime T: type, a: []const T, b: []const T, result: []T, | ... | @@ -111,7 +111,7 @@ pub fn timingSafeSub(comptime T: type, a: []const T, b: []const T, result: []T, |
| 111 | const len = a.len; | 111 | const len = a.len; |
| 112 | debug.assert(len == b.len and len == result.len); | 112 | debug.assert(len == b.len and len == result.len); |
| 113 | var borrow: u1 = 0; | 113 | var borrow: u1 = 0; |
| 114 | if (endian == .Little) { | 114 | if (endian == .little) { |
| 115 | var i: usize = 0; | 115 | var i: usize = 0; |
| 116 | while (i < len) : (i += 1) { | 116 | while (i < len) : (i += 1) { |
| 117 | const ov1 = @subWithOverflow(a[i], b[i]); | 117 | const ov1 = @subWithOverflow(a[i], b[i]); |
| ... | @@ -165,14 +165,14 @@ test "crypto.utils.timingSafeEql (vectors)" { | ... | @@ -165,14 +165,14 @@ test "crypto.utils.timingSafeEql (vectors)" { |
| 165 | test "crypto.utils.timingSafeCompare" { | 165 | test "crypto.utils.timingSafeCompare" { |
| 166 | var a = [_]u8{10} ** 32; | 166 | var a = [_]u8{10} ** 32; |
| 167 | var b = [_]u8{10} ** 32; | 167 | var b = [_]u8{10} ** 32; |
| 168 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .Big), .eq); | 168 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .big), .eq); |
| 169 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .Little), .eq); | 169 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .little), .eq); |
| 170 | a[31] = 1; | 170 | a[31] = 1; |
| 171 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .Big), .lt); | 171 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .big), .lt); |
| 172 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .Little), .lt); | 172 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .little), .lt); |
| 173 | a[0] = 20; | 173 | a[0] = 20; |
| 174 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .Big), .gt); | 174 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .big), .gt); |
| 175 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .Little), .lt); | 175 | try testing.expectEqual(timingSafeCompare(u8, &a, &b, .little), .lt); |
| 176 | } | 176 | } |
| 177 | 177 | ||
| 178 | test "crypto.utils.timingSafe{Add,Sub}" { | 178 | test "crypto.utils.timingSafe{Add,Sub}" { |
| ... | @@ -185,7 +185,7 @@ test "crypto.utils.timingSafe{Add,Sub}" { | ... | @@ -185,7 +185,7 @@ test "crypto.utils.timingSafe{Add,Sub}" { |
| 185 | while (iterations != 0) : (iterations -= 1) { | 185 | while (iterations != 0) : (iterations -= 1) { |
| 186 | random.bytes(&a); | 186 | random.bytes(&a); |
| 187 | random.bytes(&b); | 187 | random.bytes(&b); |
| 188 | const endian = if (iterations % 2 == 0) Endian.Big else Endian.Little; | 188 | const endian = if (iterations % 2 == 0) Endian.big else Endian.little; |
| 189 | _ = timingSafeSub(u8, &a, &b, &c, endian); // a-b | 189 | _ = timingSafeSub(u8, &a, &b, &c, endian); // a-b |
| 190 | _ = timingSafeAdd(u8, &c, &b, &c, endian); // (a-b)+b | 190 | _ = timingSafeAdd(u8, &c, &b, &c, endian); // (a-b)+b |
| 191 | try testing.expectEqualSlices(u8, &c, &a); | 191 | try testing.expectEqualSlices(u8, &c, &a); |
lib/std/debug.zig+8-8| ... | @@ -1098,8 +1098,8 @@ pub fn readElfDebugInfo( | ... | @@ -1098,8 +1098,8 @@ pub fn readElfDebugInfo( |
| 1098 | if (hdr.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion; | 1098 | if (hdr.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion; |
| 1099 | 1099 | ||
| 1100 | const endian: std.builtin.Endian = switch (hdr.e_ident[elf.EI_DATA]) { | 1100 | const endian: std.builtin.Endian = switch (hdr.e_ident[elf.EI_DATA]) { |
| 1101 | elf.ELFDATA2LSB => .Little, | 1101 | elf.ELFDATA2LSB => .little, |
| 1102 | elf.ELFDATA2MSB => .Big, | 1102 | elf.ELFDATA2MSB => .big, |
| 1103 | else => return error.InvalidElfEndian, | 1103 | else => return error.InvalidElfEndian, |
| 1104 | }; | 1104 | }; |
| 1105 | assert(endian == native_endian); // this is our own debug info | 1105 | assert(endian == native_endian); // this is our own debug info |
| ... | @@ -1135,8 +1135,8 @@ pub fn readElfDebugInfo( | ... | @@ -1135,8 +1135,8 @@ pub fn readElfDebugInfo( |
| 1135 | const gnu_debuglink = try chopSlice(mapped_mem, shdr.sh_offset, shdr.sh_size); | 1135 | const gnu_debuglink = try chopSlice(mapped_mem, shdr.sh_offset, shdr.sh_size); |
| 1136 | const debug_filename = mem.sliceTo(@as([*:0]const u8, @ptrCast(gnu_debuglink.ptr)), 0); | 1136 | const debug_filename = mem.sliceTo(@as([*:0]const u8, @ptrCast(gnu_debuglink.ptr)), 0); |
| 1137 | const crc_offset = mem.alignForward(usize, @intFromPtr(&debug_filename[debug_filename.len]) + 1, 4) - @intFromPtr(gnu_debuglink.ptr); | 1137 | const crc_offset = mem.alignForward(usize, @intFromPtr(&debug_filename[debug_filename.len]) + 1, 4) - @intFromPtr(gnu_debuglink.ptr); |
| 1138 | const crc_bytes = gnu_debuglink[crc_offset .. crc_offset + 4]; | 1138 | const crc_bytes = gnu_debuglink[crc_offset..][0..4]; |
| 1139 | separate_debug_crc = mem.readIntSliceNative(u32, crc_bytes); | 1139 | separate_debug_crc = mem.readInt(u32, crc_bytes, native_endian); |
| 1140 | separate_debug_filename = debug_filename; | 1140 | separate_debug_filename = debug_filename; |
| 1141 | continue; | 1141 | continue; |
| 1142 | } | 1142 | } |
| ... | @@ -1868,10 +1868,10 @@ pub const DebugInfo = struct { | ... | @@ -1868,10 +1868,10 @@ pub const DebugInfo = struct { |
| 1868 | elf.PT_NOTE => { | 1868 | elf.PT_NOTE => { |
| 1869 | // Look for .note.gnu.build-id | 1869 | // Look for .note.gnu.build-id |
| 1870 | const note_bytes = @as([*]const u8, @ptrFromInt(info.dlpi_addr + phdr.p_vaddr))[0..phdr.p_memsz]; | 1870 | const note_bytes = @as([*]const u8, @ptrFromInt(info.dlpi_addr + phdr.p_vaddr))[0..phdr.p_memsz]; |
| 1871 | const name_size = mem.readIntSliceNative(u32, note_bytes[0..4]); | 1871 | const name_size = mem.readInt(u32, note_bytes[0..4], native_endian); |
| 1872 | if (name_size != 4) continue; | 1872 | if (name_size != 4) continue; |
| 1873 | const desc_size = mem.readIntSliceNative(u32, note_bytes[4..8]); | 1873 | const desc_size = mem.readInt(u32, note_bytes[4..8], native_endian); |
| 1874 | const note_type = mem.readIntSliceNative(u32, note_bytes[8..12]); | 1874 | const note_type = mem.readInt(u32, note_bytes[8..12], native_endian); |
| 1875 | if (note_type != elf.NT_GNU_BUILD_ID) continue; | 1875 | if (note_type != elf.NT_GNU_BUILD_ID) continue; |
| 1876 | if (!mem.eql(u8, "GNU\x00", note_bytes[12..16])) continue; | 1876 | if (!mem.eql(u8, "GNU\x00", note_bytes[12..16])) continue; |
| 1877 | context.build_id = note_bytes[16..][0..desc_size]; | 1877 | context.build_id = note_bytes[16..][0..desc_size]; |
| ... | @@ -2040,7 +2040,7 @@ pub const ModuleDebugInfo = switch (native_os) { | ... | @@ -2040,7 +2040,7 @@ pub const ModuleDebugInfo = switch (native_os) { |
| 2040 | if (missing_debug_info) return error.MissingDebugInfo; | 2040 | if (missing_debug_info) return error.MissingDebugInfo; |
| 2041 | 2041 | ||
| 2042 | var di = DW.DwarfInfo{ | 2042 | var di = DW.DwarfInfo{ |
| 2043 | .endian = .Little, | 2043 | .endian = .little, |
| 2044 | .sections = sections, | 2044 | .sections = sections, |
| 2045 | .is_macho = true, | 2045 | .is_macho = true, |
| 2046 | }; | 2046 | }; |
lib/std/dwarf.zig+4-3| ... | @@ -7,6 +7,7 @@ const mem = std.mem; | ... | @@ -7,6 +7,7 @@ const mem = std.mem; |
| 7 | const math = std.math; | 7 | const math = std.math; |
| 8 | const leb = @import("leb128.zig"); | 8 | const leb = @import("leb128.zig"); |
| 9 | const assert = std.debug.assert; | 9 | const assert = std.debug.assert; |
| 10 | const native_endian = builtin.cpu.arch.endian(); | ||
| 10 | 11 | ||
| 11 | pub const TAG = @import("dwarf/TAG.zig"); | 12 | pub const TAG = @import("dwarf/TAG.zig"); |
| 12 | pub const AT = @import("dwarf/AT.zig"); | 13 | pub const AT = @import("dwarf/AT.zig"); |
| ... | @@ -1741,7 +1742,7 @@ pub const DwarfInfo = struct { | ... | @@ -1741,7 +1742,7 @@ pub const DwarfInfo = struct { |
| 1741 | context.cfa = switch (row.cfa.rule) { | 1742 | context.cfa = switch (row.cfa.rule) { |
| 1742 | .val_offset => |offset| blk: { | 1743 | .val_offset => |offset| blk: { |
| 1743 | const register = row.cfa.register orelse return error.InvalidCFARule; | 1744 | const register = row.cfa.register orelse return error.InvalidCFARule; |
| 1744 | const value = mem.readIntSliceNative(usize, try abi.regBytes(context.thread_context, register, context.reg_context)); | 1745 | const value = mem.readInt(usize, (try abi.regBytes(context.thread_context, register, context.reg_context))[0..@sizeOf(usize)], native_endian); |
| 1745 | break :blk try call_frame.applyOffset(value, offset); | 1746 | break :blk try call_frame.applyOffset(value, offset); |
| 1746 | }, | 1747 | }, |
| 1747 | .expression => |expression| blk: { | 1748 | .expression => |expression| blk: { |
| ... | @@ -1814,11 +1815,11 @@ pub const DwarfInfo = struct { | ... | @@ -1814,11 +1815,11 @@ pub const DwarfInfo = struct { |
| 1814 | } | 1815 | } |
| 1815 | 1816 | ||
| 1816 | if (has_return_address) { | 1817 | if (has_return_address) { |
| 1817 | context.pc = abi.stripInstructionPtrAuthCode(mem.readIntSliceNative(usize, try abi.regBytes( | 1818 | context.pc = abi.stripInstructionPtrAuthCode(mem.readInt(usize, (try abi.regBytes( |
| 1818 | context.thread_context, | 1819 | context.thread_context, |
| 1819 | cie.return_address_register, | 1820 | cie.return_address_register, |
| 1820 | context.reg_context, | 1821 | context.reg_context, |
| 1821 | ))); | 1822 | ))[0..@sizeOf(usize)], native_endian)); |
| 1822 | } else { | 1823 | } else { |
| 1823 | context.pc = 0; | 1824 | context.pc = 0; |
| 1824 | } | 1825 | } |
lib/std/dwarf/call_frame.zig+5-4| ... | @@ -7,6 +7,7 @@ const dwarf = std.dwarf; | ... | @@ -7,6 +7,7 @@ const dwarf = std.dwarf; |
| 7 | const abi = dwarf.abi; | 7 | const abi = dwarf.abi; |
| 8 | const expressions = dwarf.expressions; | 8 | const expressions = dwarf.expressions; |
| 9 | const assert = std.debug.assert; | 9 | const assert = std.debug.assert; |
| 10 | const native_endian = builtin.cpu.arch.endian(); | ||
| 10 | 11 | ||
| 11 | const Opcode = enum(u8) { | 12 | const Opcode = enum(u8) { |
| 12 | advance_loc = 0x1 << 6, | 13 | advance_loc = 0x1 << 6, |
| ... | @@ -386,12 +387,12 @@ pub const VirtualMachine = struct { | ... | @@ -386,12 +387,12 @@ pub const VirtualMachine = struct { |
| 386 | const addr = try applyOffset(cfa, offset); | 387 | const addr = try applyOffset(cfa, offset); |
| 387 | if (expression_context.isValidMemory) |isValidMemory| if (!isValidMemory(addr)) return error.InvalidAddress; | 388 | if (expression_context.isValidMemory) |isValidMemory| if (!isValidMemory(addr)) return error.InvalidAddress; |
| 388 | const ptr: *const usize = @ptrFromInt(addr); | 389 | const ptr: *const usize = @ptrFromInt(addr); |
| 389 | mem.writeIntSliceNative(usize, out, ptr.*); | 390 | mem.writeInt(usize, out[0..@sizeOf(usize)], ptr.*, native_endian); |
| 390 | } else return error.InvalidCFA; | 391 | } else return error.InvalidCFA; |
| 391 | }, | 392 | }, |
| 392 | .val_offset => |offset| { | 393 | .val_offset => |offset| { |
| 393 | if (context.cfa) |cfa| { | 394 | if (context.cfa) |cfa| { |
| 394 | mem.writeIntSliceNative(usize, out, try applyOffset(cfa, offset)); | 395 | mem.writeInt(usize, out[0..@sizeOf(usize)], try applyOffset(cfa, offset), native_endian); |
| 395 | } else return error.InvalidCFA; | 396 | } else return error.InvalidCFA; |
| 396 | }, | 397 | }, |
| 397 | .register => |register| { | 398 | .register => |register| { |
| ... | @@ -409,14 +410,14 @@ pub const VirtualMachine = struct { | ... | @@ -409,14 +410,14 @@ pub const VirtualMachine = struct { |
| 409 | 410 | ||
| 410 | if (!context.isValidMemory(addr)) return error.InvalidExpressionAddress; | 411 | if (!context.isValidMemory(addr)) return error.InvalidExpressionAddress; |
| 411 | const ptr: *usize = @ptrFromInt(addr); | 412 | const ptr: *usize = @ptrFromInt(addr); |
| 412 | mem.writeIntSliceNative(usize, out, ptr.*); | 413 | mem.writeInt(usize, out[0..@sizeOf(usize)], ptr.*, native_endian); |
| 413 | }, | 414 | }, |
| 414 | .val_expression => |expression| { | 415 | .val_expression => |expression| { |
| 415 | context.stack_machine.reset(); | 416 | context.stack_machine.reset(); |
| 416 | const value = try context.stack_machine.run(expression, context.allocator, expression_context, context.cfa.?); | 417 | const value = try context.stack_machine.run(expression, context.allocator, expression_context, context.cfa.?); |
| 417 | if (value) |v| { | 418 | if (value) |v| { |
| 418 | if (v != .generic) return error.InvalidExpressionValue; | 419 | if (v != .generic) return error.InvalidExpressionValue; |
| 419 | mem.writeIntSliceNative(usize, out, v.generic); | 420 | mem.writeInt(usize, out[0..@sizeOf(usize)], v.generic, native_endian); |
| 420 | } else return error.NoExpressionValue; | 421 | } else return error.NoExpressionValue; |
| 421 | }, | 422 | }, |
| 422 | .architectural => return error.UnimplementedRegisterRule, | 423 | .architectural => return error.UnimplementedRegisterRule, |
lib/std/dwarf/expressions.zig+17-16| ... | @@ -6,6 +6,7 @@ const dwarf = std.dwarf; | ... | @@ -6,6 +6,7 @@ const dwarf = std.dwarf; |
| 6 | const abi = dwarf.abi; | 6 | const abi = dwarf.abi; |
| 7 | const mem = std.mem; | 7 | const mem = std.mem; |
| 8 | const assert = std.debug.assert; | 8 | const assert = std.debug.assert; |
| 9 | const native_endian = builtin.cpu.arch.endian(); | ||
| 9 | 10 | ||
| 10 | /// Expressions can be evaluated in different contexts, each requiring its own set of inputs. | 11 | /// Expressions can be evaluated in different contexts, each requiring its own set of inputs. |
| 11 | /// Callers should specify all the fields relevant to their context. If a field is required | 12 | /// Callers should specify all the fields relevant to their context. If a field is required |
| ... | @@ -147,10 +148,10 @@ pub fn StackMachine(comptime options: ExpressionOptions) type { | ... | @@ -147,10 +148,10 @@ pub fn StackMachine(comptime options: ExpressionOptions) type { |
| 147 | .regval_type => |regval_type| regval_type.value, | 148 | .regval_type => |regval_type| regval_type.value, |
| 148 | .const_type => |const_type| { | 149 | .const_type => |const_type| { |
| 149 | const value: u64 = switch (const_type.value_bytes.len) { | 150 | const value: u64 = switch (const_type.value_bytes.len) { |
| 150 | 1 => mem.readIntSliceNative(u8, const_type.value_bytes), | 151 | 1 => mem.readInt(u8, const_type.value_bytes[0..1], native_endian), |
| 151 | 2 => mem.readIntSliceNative(u16, const_type.value_bytes), | 152 | 2 => mem.readInt(u16, const_type.value_bytes[0..2], native_endian), |
| 152 | 4 => mem.readIntSliceNative(u32, const_type.value_bytes), | 153 | 4 => mem.readInt(u32, const_type.value_bytes[0..4], native_endian), |
| 153 | 8 => mem.readIntSliceNative(u64, const_type.value_bytes), | 154 | 8 => mem.readInt(u64, const_type.value_bytes[0..8], native_endian), |
| 154 | else => return error.InvalidIntegralTypeSize, | 155 | else => return error.InvalidIntegralTypeSize, |
| 155 | }; | 156 | }; |
| 156 | 157 | ||
| ... | @@ -352,7 +353,7 @@ pub fn StackMachine(comptime options: ExpressionOptions) type { | ... | @@ -352,7 +353,7 @@ pub fn StackMachine(comptime options: ExpressionOptions) type { |
| 352 | const debug_addr_index = operand.?.generic; | 353 | const debug_addr_index = operand.?.generic; |
| 353 | const offset = context.compile_unit.?.addr_base + debug_addr_index; | 354 | const offset = context.compile_unit.?.addr_base + debug_addr_index; |
| 354 | if (offset >= context.debug_addr.?.len) return error.InvalidExpression; | 355 | if (offset >= context.debug_addr.?.len) return error.InvalidExpression; |
| 355 | const value = mem.readIntSliceNative(usize, context.debug_addr.?[offset..][0..@sizeOf(usize)]); | 356 | const value = mem.readInt(usize, context.debug_addr.?[offset..][0..@sizeOf(usize)], native_endian); |
| 356 | try self.stack.append(allocator, .{ .generic = value }); | 357 | try self.stack.append(allocator, .{ .generic = value }); |
| 357 | }, | 358 | }, |
| 358 | 359 | ||
| ... | @@ -386,21 +387,21 @@ pub fn StackMachine(comptime options: ExpressionOptions) type { | ... | @@ -386,21 +387,21 @@ pub fn StackMachine(comptime options: ExpressionOptions) type { |
| 386 | if (context.thread_context == null) return error.IncompleteExpressionContext; | 387 | if (context.thread_context == null) return error.IncompleteExpressionContext; |
| 387 | 388 | ||
| 388 | const base_register = operand.?.base_register; | 389 | const base_register = operand.?.base_register; |
| 389 | var value: i64 = @intCast(mem.readIntSliceNative(usize, try abi.regBytes( | 390 | var value: i64 = @intCast(mem.readInt(usize, (try abi.regBytes( |
| 390 | context.thread_context.?, | 391 | context.thread_context.?, |
| 391 | base_register.base_register, | 392 | base_register.base_register, |
| 392 | context.reg_context, | 393 | context.reg_context, |
| 393 | ))); | 394 | ))[0..@sizeOf(usize)], native_endian)); |
| 394 | value += base_register.offset; | 395 | value += base_register.offset; |
| 395 | try self.stack.append(allocator, .{ .generic = @intCast(value) }); | 396 | try self.stack.append(allocator, .{ .generic = @intCast(value) }); |
| 396 | }, | 397 | }, |
| 397 | OP.regval_type => { | 398 | OP.regval_type => { |
| 398 | const register_type = operand.?.register_type; | 399 | const register_type = operand.?.register_type; |
| 399 | const value = mem.readIntSliceNative(usize, try abi.regBytes( | 400 | const value = mem.readInt(usize, (try abi.regBytes( |
| 400 | context.thread_context.?, | 401 | context.thread_context.?, |
| 401 | register_type.register, | 402 | register_type.register, |
| 402 | context.reg_context, | 403 | context.reg_context, |
| 403 | )); | 404 | ))[0..@sizeOf(usize)], native_endian); |
| 404 | try self.stack.append(allocator, .{ | 405 | try self.stack.append(allocator, .{ |
| 405 | .regval_type = .{ | 406 | .regval_type = .{ |
| 406 | .type_offset = register_type.type_offset, | 407 | .type_offset = register_type.type_offset, |
| ... | @@ -756,7 +757,7 @@ pub fn StackMachine(comptime options: ExpressionOptions) type { | ... | @@ -756,7 +757,7 @@ pub fn StackMachine(comptime options: ExpressionOptions) type { |
| 756 | 757 | ||
| 757 | var block_stream = std.io.fixedBufferStream(block); | 758 | var block_stream = std.io.fixedBufferStream(block); |
| 758 | const register = (try readOperand(&block_stream, block[0], context)).?.register; | 759 | const register = (try readOperand(&block_stream, block[0], context)).?.register; |
| 759 | const value = mem.readIntSliceNative(usize, try abi.regBytes(context.thread_context.?, register, context.reg_context)); | 760 | const value = mem.readInt(usize, (try abi.regBytes(context.thread_context.?, register, context.reg_context))[0..@sizeOf(usize)], native_endian); |
| 760 | try self.stack.append(allocator, .{ .generic = value }); | 761 | try self.stack.append(allocator, .{ .generic = value }); |
| 761 | } else { | 762 | } else { |
| 762 | var stack_machine: Self = .{}; | 763 | var stack_machine: Self = .{}; |
| ... | @@ -1121,9 +1122,9 @@ test "DWARF expressions" { | ... | @@ -1121,9 +1122,9 @@ test "DWARF expressions" { |
| 1121 | var mock_debug_addr = std.ArrayList(u8).init(allocator); | 1122 | var mock_debug_addr = std.ArrayList(u8).init(allocator); |
| 1122 | defer mock_debug_addr.deinit(); | 1123 | defer mock_debug_addr.deinit(); |
| 1123 | 1124 | ||
| 1124 | try mock_debug_addr.writer().writeIntNative(u16, 0); | 1125 | try mock_debug_addr.writer().writeInt(u16, 0, native_endian); |
| 1125 | try mock_debug_addr.writer().writeIntNative(usize, input[11]); | 1126 | try mock_debug_addr.writer().writeInt(usize, input[11], native_endian); |
| 1126 | try mock_debug_addr.writer().writeIntNative(usize, input[12]); | 1127 | try mock_debug_addr.writer().writeInt(usize, input[12], native_endian); |
| 1127 | 1128 | ||
| 1128 | const context = ExpressionContext{ | 1129 | const context = ExpressionContext{ |
| 1129 | .compile_unit = &mock_compile_unit, | 1130 | .compile_unit = &mock_compile_unit, |
| ... | @@ -1185,7 +1186,7 @@ test "DWARF expressions" { | ... | @@ -1185,7 +1186,7 @@ test "DWARF expressions" { |
| 1185 | 1186 | ||
| 1186 | // TODO: Test fbreg (once implemented): mock a DIE and point compile_unit.frame_base at it | 1187 | // TODO: Test fbreg (once implemented): mock a DIE and point compile_unit.frame_base at it |
| 1187 | 1188 | ||
| 1188 | mem.writeIntSliceNative(usize, reg_bytes, 0xee); | 1189 | mem.writeInt(usize, reg_bytes[0..@sizeOf(usize)], 0xee, native_endian); |
| 1189 | (try abi.regValueNative(usize, &thread_context, abi.fpRegNum(reg_context), reg_context)).* = 1; | 1190 | (try abi.regValueNative(usize, &thread_context, abi.fpRegNum(reg_context), reg_context)).* = 1; |
| 1190 | (try abi.regValueNative(usize, &thread_context, abi.spRegNum(reg_context), reg_context)).* = 2; | 1191 | (try abi.regValueNative(usize, &thread_context, abi.spRegNum(reg_context), reg_context)).* = 2; |
| 1191 | (try abi.regValueNative(usize, &thread_context, abi.ipRegNum(), reg_context)).* = 3; | 1192 | (try abi.regValueNative(usize, &thread_context, abi.ipRegNum(), reg_context)).* = 3; |
| ... | @@ -1538,7 +1539,7 @@ test "DWARF expressions" { | ... | @@ -1538,7 +1539,7 @@ test "DWARF expressions" { |
| 1538 | 1539 | ||
| 1539 | const value: usize = @truncate(0xffeeffee_ffeeffee); | 1540 | const value: usize = @truncate(0xffeeffee_ffeeffee); |
| 1540 | var value_bytes: [options.addr_size]u8 = undefined; | 1541 | var value_bytes: [options.addr_size]u8 = undefined; |
| 1541 | mem.writeIntSliceNative(usize, &value_bytes, value); | 1542 | mem.writeInt(usize, &value_bytes, value, native_endian); |
| 1542 | 1543 | ||
| 1543 | // Convert to generic type | 1544 | // Convert to generic type |
| 1544 | stack_machine.reset(); | 1545 | stack_machine.reset(); |
| ... | @@ -1613,7 +1614,7 @@ test "DWARF expressions" { | ... | @@ -1613,7 +1614,7 @@ test "DWARF expressions" { |
| 1613 | }; | 1614 | }; |
| 1614 | 1615 | ||
| 1615 | if (abi.regBytes(&thread_context, 0, reg_context)) |reg_bytes| { | 1616 | if (abi.regBytes(&thread_context, 0, reg_context)) |reg_bytes| { |
| 1616 | mem.writeIntSliceNative(usize, reg_bytes, 0xee); | 1617 | mem.writeInt(usize, reg_bytes[0..@sizeOf(usize)], 0xee, native_endian); |
| 1617 | 1618 | ||
| 1618 | var sub_program = std.ArrayList(u8).init(allocator); | 1619 | var sub_program = std.ArrayList(u8).init(allocator); |
| 1619 | defer sub_program.deinit(); | 1620 | defer sub_program.deinit(); |
lib/std/elf.zig+2-2| ... | @@ -497,8 +497,8 @@ pub const Header = struct { | ... | @@ -497,8 +497,8 @@ pub const Header = struct { |
| 497 | if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion; | 497 | if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion; |
| 498 | 498 | ||
| 499 | const endian: std.builtin.Endian = switch (hdr32.e_ident[EI_DATA]) { | 499 | const endian: std.builtin.Endian = switch (hdr32.e_ident[EI_DATA]) { |
| 500 | ELFDATA2LSB => .Little, | 500 | ELFDATA2LSB => .little, |
| 501 | ELFDATA2MSB => .Big, | 501 | ELFDATA2MSB => .big, |
| 502 | else => return error.InvalidElfEndian, | 502 | else => return error.InvalidElfEndian, |
| 503 | }; | 503 | }; |
| 504 | const need_bswap = endian != native_endian; | 504 | const need_bswap = endian != native_endian; |
lib/std/fmt/parse_float/FloatStream.zig+1-1| ... | @@ -98,7 +98,7 @@ pub fn skipChars2(self: *FloatStream, c1: u8, c2: u8) void { | ... | @@ -98,7 +98,7 @@ pub fn skipChars2(self: *FloatStream, c1: u8, c2: u8) void { |
| 98 | } | 98 | } |
| 99 | 99 | ||
| 100 | pub fn readU64Unchecked(self: FloatStream) u64 { | 100 | pub fn readU64Unchecked(self: FloatStream) u64 { |
| 101 | return std.mem.readIntSliceLittle(u64, self.slice[self.offset..]); | 101 | return std.mem.readInt(u64, self.slice[self.offset..][0..8], .little); |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | pub fn readU64(self: FloatStream) ?u64 { | 104 | pub fn readU64(self: FloatStream) ?u64 { |
lib/std/fmt/parse_float/decimal.zig+1-1| ... | @@ -260,7 +260,7 @@ pub fn Decimal(comptime T: type) type { | ... | @@ -260,7 +260,7 @@ pub fn Decimal(comptime T: type) type { |
| 260 | if (!isEightDigits(v)) { | 260 | if (!isEightDigits(v)) { |
| 261 | break; | 261 | break; |
| 262 | } | 262 | } |
| 263 | std.mem.writeIntSliceLittle(u64, d.digits[d.num_digits..], v - 0x3030_3030_3030_3030); | 263 | std.mem.writeInt(u64, d.digits[d.num_digits..][0..8], v - 0x3030_3030_3030_3030, .little); |
| 264 | d.num_digits += 8; | 264 | d.num_digits += 8; |
| 265 | stream.advance(8); | 265 | stream.advance(8); |
| 266 | } | 266 | } |
lib/std/fs/path.zig+1-1| ... | @@ -1828,7 +1828,7 @@ test "ComponentIterator windows" { | ... | @@ -1828,7 +1828,7 @@ test "ComponentIterator windows" { |
| 1828 | 1828 | ||
| 1829 | test "ComponentIterator windows UTF-16" { | 1829 | test "ComponentIterator windows UTF-16" { |
| 1830 | // TODO: Fix on big endian architectures | 1830 | // TODO: Fix on big endian architectures |
| 1831 | if (builtin.cpu.arch.endian() != .Little) { | 1831 | if (builtin.cpu.arch.endian() != .little) { |
| 1832 | return error.SkipZigTest; | 1832 | return error.SkipZigTest; |
| 1833 | } | 1833 | } |
| 1834 | 1834 |
lib/std/hash/cityhash.zig+2-2| ... | @@ -6,11 +6,11 @@ inline fn offsetPtr(ptr: [*]const u8, offset: usize) [*]const u8 { | ... | @@ -6,11 +6,11 @@ inline fn offsetPtr(ptr: [*]const u8, offset: usize) [*]const u8 { |
| 6 | } | 6 | } |
| 7 | 7 | ||
| 8 | fn fetch32(ptr: [*]const u8, offset: usize) u32 { | 8 | fn fetch32(ptr: [*]const u8, offset: usize) u32 { |
| 9 | return std.mem.readIntLittle(u32, offsetPtr(ptr, offset)[0..4]); | 9 | return std.mem.readInt(u32, offsetPtr(ptr, offset)[0..4], .little); |
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | fn fetch64(ptr: [*]const u8, offset: usize) u64 { | 12 | fn fetch64(ptr: [*]const u8, offset: usize) u64 { |
| 13 | return std.mem.readIntLittle(u64, offsetPtr(ptr, offset)[0..8]); | 13 | return std.mem.readInt(u64, offsetPtr(ptr, offset)[0..8], .little); |
| 14 | } | 14 | } |
| 15 | 15 | ||
| 16 | pub const CityHash32 = struct { | 16 | pub const CityHash32 = struct { |
lib/std/hash/crc.zig+1-1| ... | @@ -163,7 +163,7 @@ pub fn Crc32WithPoly(comptime poly: Polynomial) type { | ... | @@ -163,7 +163,7 @@ pub fn Crc32WithPoly(comptime poly: Polynomial) type { |
| 163 | const p = input[i..][0..8]; | 163 | const p = input[i..][0..8]; |
| 164 | 164 | ||
| 165 | // Unrolling this way gives ~50Mb/s increase | 165 | // Unrolling this way gives ~50Mb/s increase |
| 166 | self.crc ^= std.mem.readIntLittle(u32, p[0..4]); | 166 | self.crc ^= std.mem.readInt(u32, p[0..4], .little); |
| 167 | 167 | ||
| 168 | self.crc = | 168 | self.crc = |
| 169 | lookup_tables[0][p[7]] ^ | 169 | lookup_tables[0][p[7]] ^ |
lib/std/hash/murmur.zig+7-7| ... | @@ -18,7 +18,7 @@ pub const Murmur2_32 = struct { | ... | @@ -18,7 +18,7 @@ pub const Murmur2_32 = struct { |
| 18 | var h1: u32 = seed ^ len; | 18 | var h1: u32 = seed ^ len; |
| 19 | for (@as([*]align(1) const u32, @ptrCast(str.ptr))[0..(len >> 2)]) |v| { | 19 | for (@as([*]align(1) const u32, @ptrCast(str.ptr))[0..(len >> 2)]) |v| { |
| 20 | var k1: u32 = v; | 20 | var k1: u32 = v; |
| 21 | if (native_endian == .Big) | 21 | if (native_endian == .big) |
| 22 | k1 = @byteSwap(k1); | 22 | k1 = @byteSwap(k1); |
| 23 | k1 *%= m; | 23 | k1 *%= m; |
| 24 | k1 ^= k1 >> 24; | 24 | k1 ^= k1 >> 24; |
| ... | @@ -102,7 +102,7 @@ pub const Murmur2_64 = struct { | ... | @@ -102,7 +102,7 @@ pub const Murmur2_64 = struct { |
| 102 | var h1: u64 = seed ^ (@as(u64, str.len) *% m); | 102 | var h1: u64 = seed ^ (@as(u64, str.len) *% m); |
| 103 | for (@as([*]align(1) const u64, @ptrCast(str.ptr))[0 .. str.len / 8]) |v| { | 103 | for (@as([*]align(1) const u64, @ptrCast(str.ptr))[0 .. str.len / 8]) |v| { |
| 104 | var k1: u64 = v; | 104 | var k1: u64 = v; |
| 105 | if (native_endian == .Big) | 105 | if (native_endian == .big) |
| 106 | k1 = @byteSwap(k1); | 106 | k1 = @byteSwap(k1); |
| 107 | k1 *%= m; | 107 | k1 *%= m; |
| 108 | k1 ^= k1 >> 47; | 108 | k1 ^= k1 >> 47; |
| ... | @@ -115,7 +115,7 @@ pub const Murmur2_64 = struct { | ... | @@ -115,7 +115,7 @@ pub const Murmur2_64 = struct { |
| 115 | if (rest > 0) { | 115 | if (rest > 0) { |
| 116 | var k1: u64 = 0; | 116 | var k1: u64 = 0; |
| 117 | @memcpy(@as([*]u8, @ptrCast(&k1))[0..rest], str[offset..]); | 117 | @memcpy(@as([*]u8, @ptrCast(&k1))[0..rest], str[offset..]); |
| 118 | if (native_endian == .Big) | 118 | if (native_endian == .big) |
| 119 | k1 = @byteSwap(k1); | 119 | k1 = @byteSwap(k1); |
| 120 | h1 ^= k1; | 120 | h1 ^= k1; |
| 121 | h1 *%= m; | 121 | h1 *%= m; |
| ... | @@ -182,7 +182,7 @@ pub const Murmur3_32 = struct { | ... | @@ -182,7 +182,7 @@ pub const Murmur3_32 = struct { |
| 182 | var h1: u32 = seed; | 182 | var h1: u32 = seed; |
| 183 | for (@as([*]align(1) const u32, @ptrCast(str.ptr))[0..(len >> 2)]) |v| { | 183 | for (@as([*]align(1) const u32, @ptrCast(str.ptr))[0..(len >> 2)]) |v| { |
| 184 | var k1: u32 = v; | 184 | var k1: u32 = v; |
| 185 | if (native_endian == .Big) | 185 | if (native_endian == .big) |
| 186 | k1 = @byteSwap(k1); | 186 | k1 = @byteSwap(k1); |
| 187 | k1 *%= c1; | 187 | k1 *%= c1; |
| 188 | k1 = rotl32(k1, 15); | 188 | k1 = rotl32(k1, 15); |
| ... | @@ -286,7 +286,7 @@ test "murmur2_32" { | ... | @@ -286,7 +286,7 @@ test "murmur2_32" { |
| 286 | var v1: u64 = 0x1234567812345678; | 286 | var v1: u64 = 0x1234567812345678; |
| 287 | var v0le: u32 = v0; | 287 | var v0le: u32 = v0; |
| 288 | var v1le: u64 = v1; | 288 | var v1le: u64 = v1; |
| 289 | if (native_endian == .Big) { | 289 | if (native_endian == .big) { |
| 290 | v0le = @byteSwap(v0le); | 290 | v0le = @byteSwap(v0le); |
| 291 | v1le = @byteSwap(v1le); | 291 | v1le = @byteSwap(v1le); |
| 292 | } | 292 | } |
| ... | @@ -310,7 +310,7 @@ test "murmur2_64" { | ... | @@ -310,7 +310,7 @@ test "murmur2_64" { |
| 310 | var v1: u64 = 0x1234567812345678; | 310 | var v1: u64 = 0x1234567812345678; |
| 311 | var v0le: u32 = v0; | 311 | var v0le: u32 = v0; |
| 312 | var v1le: u64 = v1; | 312 | var v1le: u64 = v1; |
| 313 | if (native_endian == .Big) { | 313 | if (native_endian == .big) { |
| 314 | v0le = @byteSwap(v0le); | 314 | v0le = @byteSwap(v0le); |
| 315 | v1le = @byteSwap(v1le); | 315 | v1le = @byteSwap(v1le); |
| 316 | } | 316 | } |
| ... | @@ -334,7 +334,7 @@ test "murmur3_32" { | ... | @@ -334,7 +334,7 @@ test "murmur3_32" { |
| 334 | var v1: u64 = 0x1234567812345678; | 334 | var v1: u64 = 0x1234567812345678; |
| 335 | var v0le: u32 = v0; | 335 | var v0le: u32 = v0; |
| 336 | var v1le: u64 = v1; | 336 | var v1le: u64 = v1; |
| 337 | if (native_endian == .Big) { | 337 | if (native_endian == .big) { |
| 338 | v0le = @byteSwap(v0le); | 338 | v0le = @byteSwap(v0le); |
| 339 | v1le = @byteSwap(v1le); | 339 | v1le = @byteSwap(v1le); |
| 340 | } | 340 | } |
lib/std/hash/verify.zig+1-1| ... | @@ -37,7 +37,7 @@ pub fn smhasher(comptime hash_fn: anytype) u32 { | ... | @@ -37,7 +37,7 @@ pub fn smhasher(comptime hash_fn: anytype) u32 { |
| 37 | for (0..256) |i| { | 37 | for (0..256) |i| { |
| 38 | buf[i] = @intCast(i); | 38 | buf[i] = @intCast(i); |
| 39 | const h = hashMaybeSeed(hash_fn, 256 - i, buf[0..i]); | 39 | const h = hashMaybeSeed(hash_fn, 256 - i, buf[0..i]); |
| 40 | std.mem.writeIntLittle(HashResult, buf_all[i * hash_size ..][0..hash_size], h); | 40 | std.mem.writeInt(HashResult, buf_all[i * hash_size ..][0..hash_size], h, .little); |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | return @truncate(hashMaybeSeed(hash_fn, 0, buf_all[0..])); | 43 | return @truncate(hashMaybeSeed(hash_fn, 0, buf_all[0..])); |
lib/std/hash/wyhash.zig+1-1| ... | @@ -131,7 +131,7 @@ pub const Wyhash = struct { | ... | @@ -131,7 +131,7 @@ pub const Wyhash = struct { |
| 131 | inline fn read(comptime bytes: usize, data: []const u8) u64 { | 131 | inline fn read(comptime bytes: usize, data: []const u8) u64 { |
| 132 | std.debug.assert(bytes <= 8); | 132 | std.debug.assert(bytes <= 8); |
| 133 | const T = std.meta.Int(.unsigned, 8 * bytes); | 133 | const T = std.meta.Int(.unsigned, 8 * bytes); |
| 134 | return @as(u64, std.mem.readIntLittle(T, data[0..bytes])); | 134 | return @as(u64, std.mem.readInt(T, data[0..bytes], .little)); |
| 135 | } | 135 | } |
| 136 | 136 | ||
| 137 | inline fn mum(a: *u64, b: *u64) void { | 137 | inline fn mum(a: *u64, b: *u64) void { |
lib/std/hash/xxhash.zig+12-12| ... | @@ -53,10 +53,10 @@ pub const XxHash64 = struct { | ... | @@ -53,10 +53,10 @@ pub const XxHash64 = struct { |
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | fn processStripe(self: *Accumulator, buf: *const [32]u8) void { | 55 | fn processStripe(self: *Accumulator, buf: *const [32]u8) void { |
| 56 | self.acc1 = round(self.acc1, mem.readIntLittle(u64, buf[0..8])); | 56 | self.acc1 = round(self.acc1, mem.readInt(u64, buf[0..8], .little)); |
| 57 | self.acc2 = round(self.acc2, mem.readIntLittle(u64, buf[8..16])); | 57 | self.acc2 = round(self.acc2, mem.readInt(u64, buf[8..16], .little)); |
| 58 | self.acc3 = round(self.acc3, mem.readIntLittle(u64, buf[16..24])); | 58 | self.acc3 = round(self.acc3, mem.readInt(u64, buf[16..24], .little)); |
| 59 | self.acc4 = round(self.acc4, mem.readIntLittle(u64, buf[24..32])); | 59 | self.acc4 = round(self.acc4, mem.readInt(u64, buf[24..32], .little)); |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | fn merge(self: Accumulator) u64 { | 62 | fn merge(self: Accumulator) u64 { |
| ... | @@ -139,7 +139,7 @@ pub const XxHash64 = struct { | ... | @@ -139,7 +139,7 @@ pub const XxHash64 = struct { |
| 139 | 139 | ||
| 140 | fn finalize8(v: u64, bytes: *const [8]u8) u64 { | 140 | fn finalize8(v: u64, bytes: *const [8]u8) u64 { |
| 141 | var acc = v; | 141 | var acc = v; |
| 142 | const lane = mem.readIntLittle(u64, bytes); | 142 | const lane = mem.readInt(u64, bytes, .little); |
| 143 | acc ^= round(0, lane); | 143 | acc ^= round(0, lane); |
| 144 | acc = rotl(u64, acc, 27) *% prime_1; | 144 | acc = rotl(u64, acc, 27) *% prime_1; |
| 145 | acc +%= prime_4; | 145 | acc +%= prime_4; |
| ... | @@ -148,7 +148,7 @@ pub const XxHash64 = struct { | ... | @@ -148,7 +148,7 @@ pub const XxHash64 = struct { |
| 148 | 148 | ||
| 149 | fn finalize4(v: u64, bytes: *const [4]u8) u64 { | 149 | fn finalize4(v: u64, bytes: *const [4]u8) u64 { |
| 150 | var acc = v; | 150 | var acc = v; |
| 151 | const lane = @as(u64, mem.readIntLittle(u32, bytes)); | 151 | const lane = @as(u64, mem.readInt(u32, bytes, .little)); |
| 152 | acc ^= lane *% prime_1; | 152 | acc ^= lane *% prime_1; |
| 153 | acc = rotl(u64, acc, 23) *% prime_2; | 153 | acc = rotl(u64, acc, 23) *% prime_2; |
| 154 | acc +%= prime_3; | 154 | acc +%= prime_3; |
| ... | @@ -291,10 +291,10 @@ pub const XxHash32 = struct { | ... | @@ -291,10 +291,10 @@ pub const XxHash32 = struct { |
| 291 | } | 291 | } |
| 292 | 292 | ||
| 293 | fn processStripe(self: *Accumulator, buf: *const [16]u8) void { | 293 | fn processStripe(self: *Accumulator, buf: *const [16]u8) void { |
| 294 | self.acc1 = round(self.acc1, mem.readIntLittle(u32, buf[0..4])); | 294 | self.acc1 = round(self.acc1, mem.readInt(u32, buf[0..4], .little)); |
| 295 | self.acc2 = round(self.acc2, mem.readIntLittle(u32, buf[4..8])); | 295 | self.acc2 = round(self.acc2, mem.readInt(u32, buf[4..8], .little)); |
| 296 | self.acc3 = round(self.acc3, mem.readIntLittle(u32, buf[8..12])); | 296 | self.acc3 = round(self.acc3, mem.readInt(u32, buf[8..12], .little)); |
| 297 | self.acc4 = round(self.acc4, mem.readIntLittle(u32, buf[12..16])); | 297 | self.acc4 = round(self.acc4, mem.readInt(u32, buf[12..16], .little)); |
| 298 | } | 298 | } |
| 299 | 299 | ||
| 300 | fn merge(self: Accumulator) u32 { | 300 | fn merge(self: Accumulator) u32 { |
| ... | @@ -390,7 +390,7 @@ pub const XxHash32 = struct { | ... | @@ -390,7 +390,7 @@ pub const XxHash32 = struct { |
| 390 | 390 | ||
| 391 | fn finalize4(v: u32, bytes: *const [4]u8) u32 { | 391 | fn finalize4(v: u32, bytes: *const [4]u8) u32 { |
| 392 | var acc = v; | 392 | var acc = v; |
| 393 | const lane = mem.readIntLittle(u32, bytes); | 393 | const lane = mem.readInt(u32, bytes, .little); |
| 394 | acc +%= lane *% prime_3; | 394 | acc +%= lane *% prime_3; |
| 395 | acc = rotl(u32, acc, 17) *% prime_4; | 395 | acc = rotl(u32, acc, 17) *% prime_4; |
| 396 | return acc; | 396 | return acc; |
| ... | @@ -472,7 +472,7 @@ pub const XxHash3 = struct { | ... | @@ -472,7 +472,7 @@ pub const XxHash3 = struct { |
| 472 | } | 472 | } |
| 473 | 473 | ||
| 474 | inline fn swap(x: anytype) @TypeOf(x) { | 474 | inline fn swap(x: anytype) @TypeOf(x) { |
| 475 | return if (builtin.cpu.arch.endian() == .Big) @byteSwap(x) else x; | 475 | return if (builtin.cpu.arch.endian() == .big) @byteSwap(x) else x; |
| 476 | } | 476 | } |
| 477 | 477 | ||
| 478 | inline fn disableAutoVectorization(x: anytype) void { | 478 | inline fn disableAutoVectorization(x: anytype) void { |
lib/std/heap/WasmPageAllocator.zig+1-1| ... | @@ -28,7 +28,7 @@ const PageStatus = enum(u1) { | ... | @@ -28,7 +28,7 @@ const PageStatus = enum(u1) { |
| 28 | const FreeBlock = struct { | 28 | const FreeBlock = struct { |
| 29 | data: []u128, | 29 | data: []u128, |
| 30 | 30 | ||
| 31 | const Io = std.packed_int_array.PackedIntIo(u1, .Little); | 31 | const Io = std.packed_int_array.PackedIntIo(u1, .little); |
| 32 | 32 | ||
| 33 | fn totalPages(self: FreeBlock) usize { | 33 | fn totalPages(self: FreeBlock) usize { |
| 34 | return self.data.len * 128; | 34 | return self.data.len * 128; |
lib/std/http/Client.zig+1-1| ... | @@ -1581,7 +1581,7 @@ pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !Fetc | ... | @@ -1581,7 +1581,7 @@ pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !Fetc |
| 1581 | 1581 | ||
| 1582 | test { | 1582 | test { |
| 1583 | const native_endian = comptime builtin.cpu.arch.endian(); | 1583 | const native_endian = comptime builtin.cpu.arch.endian(); |
| 1584 | if (builtin.zig_backend == .stage2_llvm and native_endian == .Big) { | 1584 | if (builtin.zig_backend == .stage2_llvm and native_endian == .big) { |
| 1585 | // https://github.com/ziglang/zig/issues/13782 | 1585 | // https://github.com/ziglang/zig/issues/13782 |
| 1586 | return error.SkipZigTest; | 1586 | return error.SkipZigTest; |
| 1587 | } | 1587 | } |
lib/std/http/Server.zig+1-1| ... | @@ -739,7 +739,7 @@ test "HTTP server handles a chunked transfer coding request" { | ... | @@ -739,7 +739,7 @@ test "HTTP server handles a chunked transfer coding request" { |
| 739 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; | 739 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; |
| 740 | 740 | ||
| 741 | const native_endian = comptime builtin.cpu.arch.endian(); | 741 | const native_endian = comptime builtin.cpu.arch.endian(); |
| 742 | if (builtin.zig_backend == .stage2_llvm and native_endian == .Big) { | 742 | if (builtin.zig_backend == .stage2_llvm and native_endian == .big) { |
| 743 | // https://github.com/ziglang/zig/issues/13782 | 743 | // https://github.com/ziglang/zig/issues/13782 |
| 744 | return error.SkipZigTest; | 744 | return error.SkipZigTest; |
| 745 | } | 745 | } |
lib/std/http/protocol.zig+2-2| ... | @@ -617,8 +617,8 @@ inline fn int32(array: *const [4]u8) u32 { | ... | @@ -617,8 +617,8 @@ inline fn int32(array: *const [4]u8) u32 { |
| 617 | 617 | ||
| 618 | inline fn intShift(comptime T: type, x: anytype) T { | 618 | inline fn intShift(comptime T: type, x: anytype) T { |
| 619 | switch (@import("builtin").cpu.arch.endian()) { | 619 | switch (@import("builtin").cpu.arch.endian()) { |
| 620 | .Little => return @as(T, @truncate(x >> (@bitSizeOf(@TypeOf(x)) - @bitSizeOf(T)))), | 620 | .little => return @as(T, @truncate(x >> (@bitSizeOf(@TypeOf(x)) - @bitSizeOf(T)))), |
| 621 | .Big => return @as(T, @truncate(x)), | 621 | .big => return @as(T, @truncate(x)), |
| 622 | } | 622 | } |
| 623 | } | 623 | } |
| 624 | 624 |
lib/std/io.zig-16| ... | @@ -269,22 +269,6 @@ pub fn GenericReader( | ... | @@ -269,22 +269,6 @@ pub fn GenericReader( |
| 269 | return @errorCast(self.any().readBoundedBytes(num_bytes)); | 269 | return @errorCast(self.any().readBoundedBytes(num_bytes)); |
| 270 | } | 270 | } |
| 271 | 271 | ||
| 272 | pub inline fn readIntNative(self: Self, comptime T: type) NoEofError!T { | ||
| 273 | return @errorCast(self.any().readIntNative(T)); | ||
| 274 | } | ||
| 275 | |||
| 276 | pub inline fn readIntForeign(self: Self, comptime T: type) NoEofError!T { | ||
| 277 | return @errorCast(self.any().readIntForeign(T)); | ||
| 278 | } | ||
| 279 | |||
| 280 | pub inline fn readIntLittle(self: Self, comptime T: type) NoEofError!T { | ||
| 281 | return @errorCast(self.any().readIntLittle(T)); | ||
| 282 | } | ||
| 283 | |||
| 284 | pub inline fn readIntBig(self: Self, comptime T: type) NoEofError!T { | ||
| 285 | return @errorCast(self.any().readIntBig(T)); | ||
| 286 | } | ||
| 287 | |||
| 288 | pub inline fn readInt(self: Self, comptime T: type, endian: std.builtin.Endian) NoEofError!T { | 272 | pub inline fn readInt(self: Self, comptime T: type, endian: std.builtin.Endian) NoEofError!T { |
| 289 | return @errorCast(self.any().readInt(T, endian)); | 273 | return @errorCast(self.any().readInt(T, endian)); |
| 290 | } | 274 | } |
lib/std/io/Reader.zig+3-25| ... | @@ -276,30 +276,8 @@ pub fn readBoundedBytes(self: Self, comptime num_bytes: usize) anyerror!std.Boun | ... | @@ -276,30 +276,8 @@ pub fn readBoundedBytes(self: Self, comptime num_bytes: usize) anyerror!std.Boun |
| 276 | return result; | 276 | return result; |
| 277 | } | 277 | } |
| 278 | 278 | ||
| 279 | /// Reads a native-endian integer | 279 | pub inline fn readInt(self: Self, comptime T: type, endian: std.builtin.Endian) anyerror!T { |
| 280 | pub fn readIntNative(self: Self, comptime T: type) anyerror!T { | 280 | const bytes = try self.readBytesNoEof(@divExact(@typeInfo(T).Int.bits, 8)); |
| 281 | const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))); | ||
| 282 | return mem.readIntNative(T, &bytes); | ||
| 283 | } | ||
| 284 | |||
| 285 | /// Reads a foreign-endian integer | ||
| 286 | pub fn readIntForeign(self: Self, comptime T: type) anyerror!T { | ||
| 287 | const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))); | ||
| 288 | return mem.readIntForeign(T, &bytes); | ||
| 289 | } | ||
| 290 | |||
| 291 | pub fn readIntLittle(self: Self, comptime T: type) anyerror!T { | ||
| 292 | const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))); | ||
| 293 | return mem.readIntLittle(T, &bytes); | ||
| 294 | } | ||
| 295 | |||
| 296 | pub fn readIntBig(self: Self, comptime T: type) anyerror!T { | ||
| 297 | const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))); | ||
| 298 | return mem.readIntBig(T, &bytes); | ||
| 299 | } | ||
| 300 | |||
| 301 | pub fn readInt(self: Self, comptime T: type, endian: std.builtin.Endian) anyerror!T { | ||
| 302 | const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))); | ||
| 303 | return mem.readInt(T, &bytes, endian); | 281 | return mem.readInt(T, &bytes, endian); |
| 304 | } | 282 | } |
| 305 | 283 | ||
| ... | @@ -356,7 +334,7 @@ pub fn readStruct(self: Self, comptime T: type) anyerror!T { | ... | @@ -356,7 +334,7 @@ pub fn readStruct(self: Self, comptime T: type) anyerror!T { |
| 356 | 334 | ||
| 357 | pub fn readStructBig(self: Self, comptime T: type) anyerror!T { | 335 | pub fn readStructBig(self: Self, comptime T: type) anyerror!T { |
| 358 | var res = try self.readStruct(T); | 336 | var res = try self.readStruct(T); |
| 359 | if (native_endian != std.builtin.Endian.Big) { | 337 | if (native_endian != std.builtin.Endian.big) { |
| 360 | mem.byteSwapAllFields(T, &res); | 338 | mem.byteSwapAllFields(T, &res); |
| 361 | } | 339 | } |
| 362 | return res; | 340 | return res; |
lib/std/io/bit_reader.zig+6-6| ... | @@ -63,14 +63,14 @@ pub fn BitReader(comptime endian: std.builtin.Endian, comptime ReaderType: type) | ... | @@ -63,14 +63,14 @@ pub fn BitReader(comptime endian: std.builtin.Endian, comptime ReaderType: type) |
| 63 | const n = if (self.bit_count >= bits) @as(u3, @intCast(bits)) else self.bit_count; | 63 | const n = if (self.bit_count >= bits) @as(u3, @intCast(bits)) else self.bit_count; |
| 64 | const shift = u7_bit_count - n; | 64 | const shift = u7_bit_count - n; |
| 65 | switch (endian) { | 65 | switch (endian) { |
| 66 | .Big => { | 66 | .big => { |
| 67 | out_buffer = @as(Buf, self.bit_buffer >> shift); | 67 | out_buffer = @as(Buf, self.bit_buffer >> shift); |
| 68 | if (n >= u7_bit_count) | 68 | if (n >= u7_bit_count) |
| 69 | self.bit_buffer = 0 | 69 | self.bit_buffer = 0 |
| 70 | else | 70 | else |
| 71 | self.bit_buffer <<= n; | 71 | self.bit_buffer <<= n; |
| 72 | }, | 72 | }, |
| 73 | .Little => { | 73 | .little => { |
| 74 | const value = (self.bit_buffer << shift) >> shift; | 74 | const value = (self.bit_buffer << shift) >> shift; |
| 75 | out_buffer = @as(Buf, value); | 75 | out_buffer = @as(Buf, value); |
| 76 | if (n >= u7_bit_count) | 76 | if (n >= u7_bit_count) |
| ... | @@ -93,7 +93,7 @@ pub fn BitReader(comptime endian: std.builtin.Endian, comptime ReaderType: type) | ... | @@ -93,7 +93,7 @@ pub fn BitReader(comptime endian: std.builtin.Endian, comptime ReaderType: type) |
| 93 | }; | 93 | }; |
| 94 | 94 | ||
| 95 | switch (endian) { | 95 | switch (endian) { |
| 96 | .Big => { | 96 | .big => { |
| 97 | if (n >= u8_bit_count) { | 97 | if (n >= u8_bit_count) { |
| 98 | out_buffer <<= @as(u3, @intCast(u8_bit_count - 1)); | 98 | out_buffer <<= @as(u3, @intCast(u8_bit_count - 1)); |
| 99 | out_buffer <<= 1; | 99 | out_buffer <<= 1; |
| ... | @@ -109,7 +109,7 @@ pub fn BitReader(comptime endian: std.builtin.Endian, comptime ReaderType: type) | ... | @@ -109,7 +109,7 @@ pub fn BitReader(comptime endian: std.builtin.Endian, comptime ReaderType: type) |
| 109 | self.bit_buffer = @as(u7, @truncate(next_byte << @as(u3, @intCast(n - 1)))); | 109 | self.bit_buffer = @as(u7, @truncate(next_byte << @as(u3, @intCast(n - 1)))); |
| 110 | self.bit_count = shift; | 110 | self.bit_count = shift; |
| 111 | }, | 111 | }, |
| 112 | .Little => { | 112 | .little => { |
| 113 | if (n >= u8_bit_count) { | 113 | if (n >= u8_bit_count) { |
| 114 | out_buffer |= @as(Buf, next_byte) << @as(BufShift, @intCast(out_bits.*)); | 114 | out_buffer |= @as(Buf, next_byte) << @as(BufShift, @intCast(out_bits.*)); |
| 115 | out_bits.* += u8_bit_count; | 115 | out_bits.* += u8_bit_count; |
| ... | @@ -168,7 +168,7 @@ test "api coverage" { | ... | @@ -168,7 +168,7 @@ test "api coverage" { |
| 168 | const mem_le = [_]u8{ 0b00011101, 0b10010101 }; | 168 | const mem_le = [_]u8{ 0b00011101, 0b10010101 }; |
| 169 | 169 | ||
| 170 | var mem_in_be = io.fixedBufferStream(&mem_be); | 170 | var mem_in_be = io.fixedBufferStream(&mem_be); |
| 171 | var bit_stream_be = bitReader(.Big, mem_in_be.reader()); | 171 | var bit_stream_be = bitReader(.big, mem_in_be.reader()); |
| 172 | 172 | ||
| 173 | var out_bits: usize = undefined; | 173 | var out_bits: usize = undefined; |
| 174 | 174 | ||
| ... | @@ -205,7 +205,7 @@ test "api coverage" { | ... | @@ -205,7 +205,7 @@ test "api coverage" { |
| 205 | try expectError(error.EndOfStream, bit_stream_be.readBitsNoEof(u1, 1)); | 205 | try expectError(error.EndOfStream, bit_stream_be.readBitsNoEof(u1, 1)); |
| 206 | 206 | ||
| 207 | var mem_in_le = io.fixedBufferStream(&mem_le); | 207 | var mem_in_le = io.fixedBufferStream(&mem_le); |
| 208 | var bit_stream_le = bitReader(.Little, mem_in_le.reader()); | 208 | var bit_stream_le = bitReader(.little, mem_in_le.reader()); |
| 209 | 209 | ||
| 210 | try expect(1 == try bit_stream_le.readBits(u2, 1, &out_bits)); | 210 | try expect(1 == try bit_stream_le.readBits(u2, 1, &out_bits)); |
| 211 | try expect(out_bits == 1); | 211 | try expect(out_bits == 1); |
lib/std/io/bit_writer.zig+10-10| ... | @@ -51,8 +51,8 @@ pub fn BitWriter(comptime endian: std.builtin.Endian, comptime WriterType: type) | ... | @@ -51,8 +51,8 @@ pub fn BitWriter(comptime endian: std.builtin.Endian, comptime WriterType: type) |
| 51 | 51 | ||
| 52 | const high_byte_shift = @as(BufShift, @intCast(buf_bit_count - u8_bit_count)); | 52 | const high_byte_shift = @as(BufShift, @intCast(buf_bit_count - u8_bit_count)); |
| 53 | var in_buffer = switch (endian) { | 53 | var in_buffer = switch (endian) { |
| 54 | .Big => buf_value << @as(BufShift, @intCast(buf_bit_count - bits)), | 54 | .big => buf_value << @as(BufShift, @intCast(buf_bit_count - bits)), |
| 55 | .Little => buf_value, | 55 | .little => buf_value, |
| 56 | }; | 56 | }; |
| 57 | var in_bits = bits; | 57 | var in_bits = bits; |
| 58 | 58 | ||
| ... | @@ -60,13 +60,13 @@ pub fn BitWriter(comptime endian: std.builtin.Endian, comptime WriterType: type) | ... | @@ -60,13 +60,13 @@ pub fn BitWriter(comptime endian: std.builtin.Endian, comptime WriterType: type) |
| 60 | const bits_remaining = u8_bit_count - self.bit_count; | 60 | const bits_remaining = u8_bit_count - self.bit_count; |
| 61 | const n = @as(u3, @intCast(if (bits_remaining > bits) bits else bits_remaining)); | 61 | const n = @as(u3, @intCast(if (bits_remaining > bits) bits else bits_remaining)); |
| 62 | switch (endian) { | 62 | switch (endian) { |
| 63 | .Big => { | 63 | .big => { |
| 64 | const shift = @as(BufShift, @intCast(high_byte_shift + self.bit_count)); | 64 | const shift = @as(BufShift, @intCast(high_byte_shift + self.bit_count)); |
| 65 | const v = @as(u8, @intCast(in_buffer >> shift)); | 65 | const v = @as(u8, @intCast(in_buffer >> shift)); |
| 66 | self.bit_buffer |= v; | 66 | self.bit_buffer |= v; |
| 67 | in_buffer <<= n; | 67 | in_buffer <<= n; |
| 68 | }, | 68 | }, |
| 69 | .Little => { | 69 | .little => { |
| 70 | const v = @as(u8, @truncate(in_buffer)) << @as(u3, @intCast(self.bit_count)); | 70 | const v = @as(u8, @truncate(in_buffer)) << @as(u3, @intCast(self.bit_count)); |
| 71 | self.bit_buffer |= v; | 71 | self.bit_buffer |= v; |
| 72 | in_buffer >>= n; | 72 | in_buffer >>= n; |
| ... | @@ -86,13 +86,13 @@ pub fn BitWriter(comptime endian: std.builtin.Endian, comptime WriterType: type) | ... | @@ -86,13 +86,13 @@ pub fn BitWriter(comptime endian: std.builtin.Endian, comptime WriterType: type) |
| 86 | //copy bytes until we can't fill one anymore, then leave the rest in bit_buffer | 86 | //copy bytes until we can't fill one anymore, then leave the rest in bit_buffer |
| 87 | while (in_bits >= u8_bit_count) { | 87 | while (in_bits >= u8_bit_count) { |
| 88 | switch (endian) { | 88 | switch (endian) { |
| 89 | .Big => { | 89 | .big => { |
| 90 | const v = @as(u8, @intCast(in_buffer >> high_byte_shift)); | 90 | const v = @as(u8, @intCast(in_buffer >> high_byte_shift)); |
| 91 | try self.forward_writer.writeByte(v); | 91 | try self.forward_writer.writeByte(v); |
| 92 | in_buffer <<= @as(u3, @intCast(u8_bit_count - 1)); | 92 | in_buffer <<= @as(u3, @intCast(u8_bit_count - 1)); |
| 93 | in_buffer <<= 1; | 93 | in_buffer <<= 1; |
| 94 | }, | 94 | }, |
| 95 | .Little => { | 95 | .little => { |
| 96 | const v = @as(u8, @truncate(in_buffer)); | 96 | const v = @as(u8, @truncate(in_buffer)); |
| 97 | try self.forward_writer.writeByte(v); | 97 | try self.forward_writer.writeByte(v); |
| 98 | in_buffer >>= @as(u3, @intCast(u8_bit_count - 1)); | 98 | in_buffer >>= @as(u3, @intCast(u8_bit_count - 1)); |
| ... | @@ -105,8 +105,8 @@ pub fn BitWriter(comptime endian: std.builtin.Endian, comptime WriterType: type) | ... | @@ -105,8 +105,8 @@ pub fn BitWriter(comptime endian: std.builtin.Endian, comptime WriterType: type) |
| 105 | if (in_bits > 0) { | 105 | if (in_bits > 0) { |
| 106 | self.bit_count = @as(u4, @intCast(in_bits)); | 106 | self.bit_count = @as(u4, @intCast(in_bits)); |
| 107 | self.bit_buffer = switch (endian) { | 107 | self.bit_buffer = switch (endian) { |
| 108 | .Big => @as(u8, @truncate(in_buffer >> high_byte_shift)), | 108 | .big => @as(u8, @truncate(in_buffer >> high_byte_shift)), |
| 109 | .Little => @as(u8, @truncate(in_buffer)), | 109 | .little => @as(u8, @truncate(in_buffer)), |
| 110 | }; | 110 | }; |
| 111 | } | 111 | } |
| 112 | } | 112 | } |
| ... | @@ -148,7 +148,7 @@ test "api coverage" { | ... | @@ -148,7 +148,7 @@ test "api coverage" { |
| 148 | var mem_le = [_]u8{0} ** 2; | 148 | var mem_le = [_]u8{0} ** 2; |
| 149 | 149 | ||
| 150 | var mem_out_be = io.fixedBufferStream(&mem_be); | 150 | var mem_out_be = io.fixedBufferStream(&mem_be); |
| 151 | var bit_stream_be = bitWriter(.Big, mem_out_be.writer()); | 151 | var bit_stream_be = bitWriter(.big, mem_out_be.writer()); |
| 152 | 152 | ||
| 153 | try bit_stream_be.writeBits(@as(u2, 1), 1); | 153 | try bit_stream_be.writeBits(@as(u2, 1), 1); |
| 154 | try bit_stream_be.writeBits(@as(u5, 2), 2); | 154 | try bit_stream_be.writeBits(@as(u5, 2), 2); |
| ... | @@ -172,7 +172,7 @@ test "api coverage" { | ... | @@ -172,7 +172,7 @@ test "api coverage" { |
| 172 | try bit_stream_be.writeBits(@as(u0, 0), 0); | 172 | try bit_stream_be.writeBits(@as(u0, 0), 0); |
| 173 | 173 | ||
| 174 | var mem_out_le = io.fixedBufferStream(&mem_le); | 174 | var mem_out_le = io.fixedBufferStream(&mem_le); |
| 175 | var bit_stream_le = bitWriter(.Little, mem_out_le.writer()); | 175 | var bit_stream_le = bitWriter(.little, mem_out_le.writer()); |
| 176 | 176 | ||
| 177 | try bit_stream_le.writeBits(@as(u2, 1), 1); | 177 | try bit_stream_le.writeBits(@as(u2, 1), 1); |
| 178 | try bit_stream_le.writeBits(@as(u5, 2), 2); | 178 | try bit_stream_le.writeBits(@as(u5, 2), 2); |
lib/std/io/test.zig+1-1| ... | @@ -183,7 +183,7 @@ test "GenericReader methods can return error.EndOfStream" { | ... | @@ -183,7 +183,7 @@ test "GenericReader methods can return error.EndOfStream" { |
| 183 | var fbs = std.io.fixedBufferStream(""); | 183 | var fbs = std.io.fixedBufferStream(""); |
| 184 | try std.testing.expectError( | 184 | try std.testing.expectError( |
| 185 | error.EndOfStream, | 185 | error.EndOfStream, |
| 186 | fbs.reader().readEnum(enum(u8) { a, b }, .Little), | 186 | fbs.reader().readEnum(enum(u8) { a, b }, .little), |
| 187 | ); | 187 | ); |
| 188 | try std.testing.expectError( | 188 | try std.testing.expectError( |
| 189 | error.EndOfStream, | 189 | error.EndOfStream, |
lib/std/io/writer.zig+2-28| ... | @@ -45,34 +45,8 @@ pub fn Writer( | ... | @@ -45,34 +45,8 @@ pub fn Writer( |
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | /// Write a native-endian integer. | 48 | pub inline fn writeInt(self: Self, comptime T: type, value: T, endian: std.builtin.Endian) Error!void { |
| 49 | pub fn writeIntNative(self: Self, comptime T: type, value: T) Error!void { | 49 | var bytes: [@divExact(@typeInfo(T).Int.bits, 8)]u8 = undefined; |
| 50 | var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; | ||
| 51 | mem.writeIntNative(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); | ||
| 52 | return self.writeAll(&bytes); | ||
| 53 | } | ||
| 54 | |||
| 55 | /// Write a foreign-endian integer. | ||
| 56 | pub fn writeIntForeign(self: Self, comptime T: type, value: T) Error!void { | ||
| 57 | var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; | ||
| 58 | mem.writeIntForeign(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); | ||
| 59 | return self.writeAll(&bytes); | ||
| 60 | } | ||
| 61 | |||
| 62 | pub fn writeIntLittle(self: Self, comptime T: type, value: T) Error!void { | ||
| 63 | var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; | ||
| 64 | mem.writeIntLittle(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); | ||
| 65 | return self.writeAll(&bytes); | ||
| 66 | } | ||
| 67 | |||
| 68 | pub fn writeIntBig(self: Self, comptime T: type, value: T) Error!void { | ||
| 69 | var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; | ||
| 70 | mem.writeIntBig(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); | ||
| 71 | return self.writeAll(&bytes); | ||
| 72 | } | ||
| 73 | |||
| 74 | pub fn writeInt(self: Self, comptime T: type, value: T, endian: std.builtin.Endian) Error!void { | ||
| 75 | var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; | ||
| 76 | mem.writeInt(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value, endian); | 50 | mem.writeInt(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value, endian); |
| 77 | return self.writeAll(&bytes); | 51 | return self.writeAll(&bytes); |
| 78 | } | 52 | } |
lib/std/math/big/int.zig+9-9| ... | @@ -798,7 +798,7 @@ pub const Mutable = struct { | ... | @@ -798,7 +798,7 @@ pub const Mutable = struct { |
| 798 | const endian_mask: usize = (@sizeOf(Limb) - 1) << 3; | 798 | const endian_mask: usize = (@sizeOf(Limb) - 1) << 3; |
| 799 | 799 | ||
| 800 | var bytes = std.mem.sliceAsBytes(r.limbs); | 800 | var bytes = std.mem.sliceAsBytes(r.limbs); |
| 801 | var bits = std.packed_int_array.PackedIntSliceEndian(u1, .Little).init(bytes, limbs_required * @bitSizeOf(Limb)); | 801 | var bits = std.packed_int_array.PackedIntSliceEndian(u1, .little).init(bytes, limbs_required * @bitSizeOf(Limb)); |
| 802 | 802 | ||
| 803 | var k: usize = 0; | 803 | var k: usize = 0; |
| 804 | while (k < ((bit_count + 1) / 2)) : (k += 1) { | 804 | while (k < ((bit_count + 1) / 2)) : (k += 1) { |
| ... | @@ -807,7 +807,7 @@ pub const Mutable = struct { | ... | @@ -807,7 +807,7 @@ pub const Mutable = struct { |
| 807 | 807 | ||
| 808 | // This "endian mask" remaps a low (LE) byte to the corresponding high | 808 | // This "endian mask" remaps a low (LE) byte to the corresponding high |
| 809 | // (BE) byte in the Limb, without changing which limbs we are indexing | 809 | // (BE) byte in the Limb, without changing which limbs we are indexing |
| 810 | if (native_endian == .Big) { | 810 | if (native_endian == .big) { |
| 811 | i ^= endian_mask; | 811 | i ^= endian_mask; |
| 812 | rev_i ^= endian_mask; | 812 | rev_i ^= endian_mask; |
| 813 | } | 813 | } |
| ... | @@ -821,8 +821,8 @@ pub const Mutable = struct { | ... | @@ -821,8 +821,8 @@ pub const Mutable = struct { |
| 821 | // Calculate signed-magnitude representation for output | 821 | // Calculate signed-magnitude representation for output |
| 822 | if (signedness == .signed) { | 822 | if (signedness == .signed) { |
| 823 | const last_bit = switch (native_endian) { | 823 | const last_bit = switch (native_endian) { |
| 824 | .Little => bits.get(bit_count - 1), | 824 | .little => bits.get(bit_count - 1), |
| 825 | .Big => bits.get((bit_count - 1) ^ endian_mask), | 825 | .big => bits.get((bit_count - 1) ^ endian_mask), |
| 826 | }; | 826 | }; |
| 827 | if (last_bit == 1) { | 827 | if (last_bit == 1) { |
| 828 | r.bitNotWrap(r.toConst(), .unsigned, bit_count); // Bitwise NOT. | 828 | r.bitNotWrap(r.toConst(), .unsigned, bit_count); // Bitwise NOT. |
| ... | @@ -869,7 +869,7 @@ pub const Mutable = struct { | ... | @@ -869,7 +869,7 @@ pub const Mutable = struct { |
| 869 | 869 | ||
| 870 | // This "endian mask" remaps a low (LE) byte to the corresponding high | 870 | // This "endian mask" remaps a low (LE) byte to the corresponding high |
| 871 | // (BE) byte in the Limb, without changing which limbs we are indexing | 871 | // (BE) byte in the Limb, without changing which limbs we are indexing |
| 872 | if (native_endian == .Big) { | 872 | if (native_endian == .big) { |
| 873 | i ^= endian_mask; | 873 | i ^= endian_mask; |
| 874 | rev_i ^= endian_mask; | 874 | rev_i ^= endian_mask; |
| 875 | } | 875 | } |
| ... | @@ -883,8 +883,8 @@ pub const Mutable = struct { | ... | @@ -883,8 +883,8 @@ pub const Mutable = struct { |
| 883 | // Calculate signed-magnitude representation for output | 883 | // Calculate signed-magnitude representation for output |
| 884 | if (signedness == .signed) { | 884 | if (signedness == .signed) { |
| 885 | const last_byte = switch (native_endian) { | 885 | const last_byte = switch (native_endian) { |
| 886 | .Little => bytes[byte_count - 1], | 886 | .little => bytes[byte_count - 1], |
| 887 | .Big => bytes[(byte_count - 1) ^ endian_mask], | 887 | .big => bytes[(byte_count - 1) ^ endian_mask], |
| 888 | }; | 888 | }; |
| 889 | 889 | ||
| 890 | if (last_byte & (1 << 7) != 0) { // Check sign bit of last byte | 890 | if (last_byte & (1 << 7) != 0) { // Check sign bit of last byte |
| ... | @@ -1912,8 +1912,8 @@ pub const Mutable = struct { | ... | @@ -1912,8 +1912,8 @@ pub const Mutable = struct { |
| 1912 | if (signedness == .signed) { | 1912 | if (signedness == .signed) { |
| 1913 | const total_bits = bit_offset + bit_count; | 1913 | const total_bits = bit_offset + bit_count; |
| 1914 | var last_byte = switch (endian) { | 1914 | var last_byte = switch (endian) { |
| 1915 | .Little => ((total_bits + 7) / 8) - 1, | 1915 | .little => ((total_bits + 7) / 8) - 1, |
| 1916 | .Big => buffer.len - ((total_bits + 7) / 8), | 1916 | .big => buffer.len - ((total_bits + 7) / 8), |
| 1917 | }; | 1917 | }; |
| 1918 | 1918 | ||
| 1919 | const sign_bit = @as(u8, 1) << @as(u3, @intCast((total_bits - 1) % 8)); | 1919 | const sign_bit = @as(u8, 1) << @as(u3, @intCast((total_bits - 1) % 8)); |
lib/std/math/big/int_test.zig+41-41| ... | @@ -2774,7 +2774,7 @@ test "big int conversion read/write twos complement" { | ... | @@ -2774,7 +2774,7 @@ test "big int conversion read/write twos complement" { |
| 2774 | var buffer1 = try testing.allocator.alloc(u8, 64); | 2774 | var buffer1 = try testing.allocator.alloc(u8, 64); |
| 2775 | defer testing.allocator.free(buffer1); | 2775 | defer testing.allocator.free(buffer1); |
| 2776 | 2776 | ||
| 2777 | const endians = [_]std.builtin.Endian{ .Little, .Big }; | 2777 | const endians = [_]std.builtin.Endian{ .little, .big }; |
| 2778 | const abi_size = 64; | 2778 | const abi_size = 64; |
| 2779 | 2779 | ||
| 2780 | for (endians) |endian| { | 2780 | for (endians) |endian| { |
| ... | @@ -2804,26 +2804,26 @@ test "big int conversion read twos complement with padding" { | ... | @@ -2804,26 +2804,26 @@ test "big int conversion read twos complement with padding" { |
| 2804 | // (3) should sign-extend any bits from bit_count to 8 * abi_size | 2804 | // (3) should sign-extend any bits from bit_count to 8 * abi_size |
| 2805 | 2805 | ||
| 2806 | var bit_count: usize = 12 * 8 + 1; | 2806 | var bit_count: usize = 12 * 8 + 1; |
| 2807 | a.toConst().writeTwosComplement(buffer1[0..13], .Little); | 2807 | a.toConst().writeTwosComplement(buffer1[0..13], .little); |
| 2808 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0xaa, 0xaa, 0xaa })); | 2808 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0xaa, 0xaa, 0xaa })); |
| 2809 | a.toConst().writeTwosComplement(buffer1[0..13], .Big); | 2809 | a.toConst().writeTwosComplement(buffer1[0..13], .big); |
| 2810 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xaa, 0xaa, 0xaa })); | 2810 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xaa, 0xaa, 0xaa })); |
| 2811 | a.toConst().writeTwosComplement(buffer1[0..16], .Little); | 2811 | a.toConst().writeTwosComplement(buffer1[0..16], .little); |
| 2812 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0, 0x0, 0x0 })); | 2812 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0, 0x0, 0x0 })); |
| 2813 | a.toConst().writeTwosComplement(buffer1[0..16], .Big); | 2813 | a.toConst().writeTwosComplement(buffer1[0..16], .big); |
| 2814 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0x0, 0x0, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd })); | 2814 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0x0, 0x0, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd })); |
| 2815 | 2815 | ||
| 2816 | @memset(buffer1, 0xaa); | 2816 | @memset(buffer1, 0xaa); |
| 2817 | try a.set(-0x01_02030405_06070809_0a0b0c0d); | 2817 | try a.set(-0x01_02030405_06070809_0a0b0c0d); |
| 2818 | bit_count = 12 * 8 + 2; | 2818 | bit_count = 12 * 8 + 2; |
| 2819 | 2819 | ||
| 2820 | a.toConst().writeTwosComplement(buffer1[0..13], .Little); | 2820 | a.toConst().writeTwosComplement(buffer1[0..13], .little); |
| 2821 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xf3, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xaa, 0xaa, 0xaa })); | 2821 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xf3, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xaa, 0xaa, 0xaa })); |
| 2822 | a.toConst().writeTwosComplement(buffer1[0..13], .Big); | 2822 | a.toConst().writeTwosComplement(buffer1[0..13], .big); |
| 2823 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf3, 0xaa, 0xaa, 0xaa })); | 2823 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf3, 0xaa, 0xaa, 0xaa })); |
| 2824 | a.toConst().writeTwosComplement(buffer1[0..16], .Little); | 2824 | a.toConst().writeTwosComplement(buffer1[0..16], .little); |
| 2825 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xf3, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xff, 0xff })); | 2825 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xf3, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xff, 0xff })); |
| 2826 | a.toConst().writeTwosComplement(buffer1[0..16], .Big); | 2826 | a.toConst().writeTwosComplement(buffer1[0..16], .big); |
| 2827 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xff, 0xff, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf3 })); | 2827 | try testing.expect(std.mem.eql(u8, buffer1, &[_]u8{ 0xff, 0xff, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf3 })); |
| 2828 | } | 2828 | } |
| 2829 | 2829 | ||
| ... | @@ -2838,13 +2838,13 @@ test "big int write twos complement +/- zero" { | ... | @@ -2838,13 +2838,13 @@ test "big int write twos complement +/- zero" { |
| 2838 | 2838 | ||
| 2839 | // Test zero | 2839 | // Test zero |
| 2840 | 2840 | ||
| 2841 | m.toConst().writeTwosComplement(buffer1[0..13], .Little); | 2841 | m.toConst().writeTwosComplement(buffer1[0..13], .little); |
| 2842 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 13) ++ ([_]u8{0xaa} ** 3)))); | 2842 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 13) ++ ([_]u8{0xaa} ** 3)))); |
| 2843 | m.toConst().writeTwosComplement(buffer1[0..13], .Big); | 2843 | m.toConst().writeTwosComplement(buffer1[0..13], .big); |
| 2844 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 13) ++ ([_]u8{0xaa} ** 3)))); | 2844 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 13) ++ ([_]u8{0xaa} ** 3)))); |
| 2845 | m.toConst().writeTwosComplement(buffer1[0..16], .Little); | 2845 | m.toConst().writeTwosComplement(buffer1[0..16], .little); |
| 2846 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 16)))); | 2846 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 16)))); |
| 2847 | m.toConst().writeTwosComplement(buffer1[0..16], .Big); | 2847 | m.toConst().writeTwosComplement(buffer1[0..16], .big); |
| 2848 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 16)))); | 2848 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 16)))); |
| 2849 | 2849 | ||
| 2850 | @memset(buffer1, 0xaa); | 2850 | @memset(buffer1, 0xaa); |
| ... | @@ -2852,13 +2852,13 @@ test "big int write twos complement +/- zero" { | ... | @@ -2852,13 +2852,13 @@ test "big int write twos complement +/- zero" { |
| 2852 | 2852 | ||
| 2853 | // Test negative zero | 2853 | // Test negative zero |
| 2854 | 2854 | ||
| 2855 | m.toConst().writeTwosComplement(buffer1[0..13], .Little); | 2855 | m.toConst().writeTwosComplement(buffer1[0..13], .little); |
| 2856 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 13) ++ ([_]u8{0xaa} ** 3)))); | 2856 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 13) ++ ([_]u8{0xaa} ** 3)))); |
| 2857 | m.toConst().writeTwosComplement(buffer1[0..13], .Big); | 2857 | m.toConst().writeTwosComplement(buffer1[0..13], .big); |
| 2858 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 13) ++ ([_]u8{0xaa} ** 3)))); | 2858 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 13) ++ ([_]u8{0xaa} ** 3)))); |
| 2859 | m.toConst().writeTwosComplement(buffer1[0..16], .Little); | 2859 | m.toConst().writeTwosComplement(buffer1[0..16], .little); |
| 2860 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 16)))); | 2860 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 16)))); |
| 2861 | m.toConst().writeTwosComplement(buffer1[0..16], .Big); | 2861 | m.toConst().writeTwosComplement(buffer1[0..16], .big); |
| 2862 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 16)))); | 2862 | try testing.expect(std.mem.eql(u8, buffer1, &(([_]u8{0} ** 16)))); |
| 2863 | } | 2863 | } |
| 2864 | 2864 | ||
| ... | @@ -2881,19 +2881,19 @@ test "big int conversion write twos complement with padding" { | ... | @@ -2881,19 +2881,19 @@ test "big int conversion write twos complement with padding" { |
| 2881 | // Test 0x01_02030405_06070809_0a0b0c0d | 2881 | // Test 0x01_02030405_06070809_0a0b0c0d |
| 2882 | 2882 | ||
| 2883 | buffer = &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0xb }; | 2883 | buffer = &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0xb }; |
| 2884 | m.readTwosComplement(buffer[0..13], bit_count, .Little, .unsigned); | 2884 | m.readTwosComplement(buffer[0..13], bit_count, .little, .unsigned); |
| 2885 | try testing.expect(m.toConst().orderAgainstScalar(0x01_02030405_06070809_0a0b0c0d) == .eq); | 2885 | try testing.expect(m.toConst().orderAgainstScalar(0x01_02030405_06070809_0a0b0c0d) == .eq); |
| 2886 | 2886 | ||
| 2887 | buffer = &[_]u8{ 0xb, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd }; | 2887 | buffer = &[_]u8{ 0xb, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd }; |
| 2888 | m.readTwosComplement(buffer[0..13], bit_count, .Big, .unsigned); | 2888 | m.readTwosComplement(buffer[0..13], bit_count, .big, .unsigned); |
| 2889 | try testing.expect(m.toConst().orderAgainstScalar(0x01_02030405_06070809_0a0b0c0d) == .eq); | 2889 | try testing.expect(m.toConst().orderAgainstScalar(0x01_02030405_06070809_0a0b0c0d) == .eq); |
| 2890 | 2890 | ||
| 2891 | buffer = &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0xab, 0xaa, 0xaa, 0xaa }; | 2891 | buffer = &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0xab, 0xaa, 0xaa, 0xaa }; |
| 2892 | m.readTwosComplement(buffer[0..16], bit_count, .Little, .unsigned); | 2892 | m.readTwosComplement(buffer[0..16], bit_count, .little, .unsigned); |
| 2893 | try testing.expect(m.toConst().orderAgainstScalar(0x01_02030405_06070809_0a0b0c0d) == .eq); | 2893 | try testing.expect(m.toConst().orderAgainstScalar(0x01_02030405_06070809_0a0b0c0d) == .eq); |
| 2894 | 2894 | ||
| 2895 | buffer = &[_]u8{ 0xaa, 0xaa, 0xaa, 0xab, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd }; | 2895 | buffer = &[_]u8{ 0xaa, 0xaa, 0xaa, 0xab, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd }; |
| 2896 | m.readTwosComplement(buffer[0..16], bit_count, .Big, .unsigned); | 2896 | m.readTwosComplement(buffer[0..16], bit_count, .big, .unsigned); |
| 2897 | try testing.expect(m.toConst().orderAgainstScalar(0x01_02030405_06070809_0a0b0c0d) == .eq); | 2897 | try testing.expect(m.toConst().orderAgainstScalar(0x01_02030405_06070809_0a0b0c0d) == .eq); |
| 2898 | 2898 | ||
| 2899 | bit_count = @sizeOf(Limb) * 8; | 2899 | bit_count = @sizeOf(Limb) * 8; |
| ... | @@ -2901,19 +2901,19 @@ test "big int conversion write twos complement with padding" { | ... | @@ -2901,19 +2901,19 @@ test "big int conversion write twos complement with padding" { |
| 2901 | // Test 0x0a0a0a0a_02030405_06070809_0a0b0c0d | 2901 | // Test 0x0a0a0a0a_02030405_06070809_0a0b0c0d |
| 2902 | 2902 | ||
| 2903 | buffer = &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0xaa }; | 2903 | buffer = &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0xaa }; |
| 2904 | m.readTwosComplement(buffer[0..13], bit_count, .Little, .unsigned); | 2904 | m.readTwosComplement(buffer[0..13], bit_count, .little, .unsigned); |
| 2905 | try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaa_02030405_06070809_0a0b0c0d))) == .eq); | 2905 | try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaa_02030405_06070809_0a0b0c0d))) == .eq); |
| 2906 | 2906 | ||
| 2907 | buffer = &[_]u8{ 0xaa, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd }; | 2907 | buffer = &[_]u8{ 0xaa, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd }; |
| 2908 | m.readTwosComplement(buffer[0..13], bit_count, .Big, .unsigned); | 2908 | m.readTwosComplement(buffer[0..13], bit_count, .big, .unsigned); |
| 2909 | try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaa_02030405_06070809_0a0b0c0d))) == .eq); | 2909 | try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaa_02030405_06070809_0a0b0c0d))) == .eq); |
| 2910 | 2910 | ||
| 2911 | buffer = &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0xaa, 0xaa, 0xaa, 0xaa }; | 2911 | buffer = &[_]u8{ 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2912 | m.readTwosComplement(buffer[0..16], bit_count, .Little, .unsigned); | 2912 | m.readTwosComplement(buffer[0..16], bit_count, .little, .unsigned); |
| 2913 | try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaaaaaaaa_02030405_06070809_0a0b0c0d))) == .eq); | 2913 | try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaaaaaaaa_02030405_06070809_0a0b0c0d))) == .eq); |
| 2914 | 2914 | ||
| 2915 | buffer = &[_]u8{ 0xaa, 0xaa, 0xaa, 0xaa, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd }; | 2915 | buffer = &[_]u8{ 0xaa, 0xaa, 0xaa, 0xaa, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd }; |
| 2916 | m.readTwosComplement(buffer[0..16], bit_count, .Big, .unsigned); | 2916 | m.readTwosComplement(buffer[0..16], bit_count, .big, .unsigned); |
| 2917 | try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaaaaaaaa_02030405_06070809_0a0b0c0d))) == .eq); | 2917 | try testing.expect(m.toConst().orderAgainstScalar(@as(Limb, @truncate(0xaaaaaaaa_02030405_06070809_0a0b0c0d))) == .eq); |
| 2918 | 2918 | ||
| 2919 | bit_count = 12 * 8 + 2; | 2919 | bit_count = 12 * 8 + 2; |
| ... | @@ -2921,42 +2921,42 @@ test "big int conversion write twos complement with padding" { | ... | @@ -2921,42 +2921,42 @@ test "big int conversion write twos complement with padding" { |
| 2921 | // Test -0x01_02030405_06070809_0a0b0c0d | 2921 | // Test -0x01_02030405_06070809_0a0b0c0d |
| 2922 | 2922 | ||
| 2923 | buffer = &[_]u8{ 0xf3, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0x02 }; | 2923 | buffer = &[_]u8{ 0xf3, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0x02 }; |
| 2924 | m.readTwosComplement(buffer[0..13], bit_count, .Little, .signed); | 2924 | m.readTwosComplement(buffer[0..13], bit_count, .little, .signed); |
| 2925 | try testing.expect(m.toConst().orderAgainstScalar(-0x01_02030405_06070809_0a0b0c0d) == .eq); | 2925 | try testing.expect(m.toConst().orderAgainstScalar(-0x01_02030405_06070809_0a0b0c0d) == .eq); |
| 2926 | 2926 | ||
| 2927 | buffer = &[_]u8{ 0x02, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf3 }; | 2927 | buffer = &[_]u8{ 0x02, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf3 }; |
| 2928 | m.readTwosComplement(buffer[0..13], bit_count, .Big, .signed); | 2928 | m.readTwosComplement(buffer[0..13], bit_count, .big, .signed); |
| 2929 | try testing.expect(m.toConst().orderAgainstScalar(-0x01_02030405_06070809_0a0b0c0d) == .eq); | 2929 | try testing.expect(m.toConst().orderAgainstScalar(-0x01_02030405_06070809_0a0b0c0d) == .eq); |
| 2930 | 2930 | ||
| 2931 | buffer = &[_]u8{ 0xf3, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0x02, 0xaa, 0xaa, 0xaa }; | 2931 | buffer = &[_]u8{ 0xf3, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0x02, 0xaa, 0xaa, 0xaa }; |
| 2932 | m.readTwosComplement(buffer[0..16], bit_count, .Little, .signed); | 2932 | m.readTwosComplement(buffer[0..16], bit_count, .little, .signed); |
| 2933 | try testing.expect(m.toConst().orderAgainstScalar(-0x01_02030405_06070809_0a0b0c0d) == .eq); | 2933 | try testing.expect(m.toConst().orderAgainstScalar(-0x01_02030405_06070809_0a0b0c0d) == .eq); |
| 2934 | 2934 | ||
| 2935 | buffer = &[_]u8{ 0xaa, 0xaa, 0xaa, 0x02, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf3 }; | 2935 | buffer = &[_]u8{ 0xaa, 0xaa, 0xaa, 0x02, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf3 }; |
| 2936 | m.readTwosComplement(buffer[0..16], bit_count, .Big, .signed); | 2936 | m.readTwosComplement(buffer[0..16], bit_count, .big, .signed); |
| 2937 | try testing.expect(m.toConst().orderAgainstScalar(-0x01_02030405_06070809_0a0b0c0d) == .eq); | 2937 | try testing.expect(m.toConst().orderAgainstScalar(-0x01_02030405_06070809_0a0b0c0d) == .eq); |
| 2938 | 2938 | ||
| 2939 | // Test 0 | 2939 | // Test 0 |
| 2940 | 2940 | ||
| 2941 | buffer = &([_]u8{0} ** 16); | 2941 | buffer = &([_]u8{0} ** 16); |
| 2942 | m.readTwosComplement(buffer[0..13], bit_count, .Little, .unsigned); | 2942 | m.readTwosComplement(buffer[0..13], bit_count, .little, .unsigned); |
| 2943 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2943 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2944 | m.readTwosComplement(buffer[0..13], bit_count, .Big, .unsigned); | 2944 | m.readTwosComplement(buffer[0..13], bit_count, .big, .unsigned); |
| 2945 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2945 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2946 | m.readTwosComplement(buffer[0..16], bit_count, .Little, .unsigned); | 2946 | m.readTwosComplement(buffer[0..16], bit_count, .little, .unsigned); |
| 2947 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2947 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2948 | m.readTwosComplement(buffer[0..16], bit_count, .Big, .unsigned); | 2948 | m.readTwosComplement(buffer[0..16], bit_count, .big, .unsigned); |
| 2949 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2949 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2950 | 2950 | ||
| 2951 | bit_count = 0; | 2951 | bit_count = 0; |
| 2952 | buffer = &([_]u8{0xaa} ** 16); | 2952 | buffer = &([_]u8{0xaa} ** 16); |
| 2953 | m.readTwosComplement(buffer[0..13], bit_count, .Little, .unsigned); | 2953 | m.readTwosComplement(buffer[0..13], bit_count, .little, .unsigned); |
| 2954 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2954 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2955 | m.readTwosComplement(buffer[0..13], bit_count, .Big, .unsigned); | 2955 | m.readTwosComplement(buffer[0..13], bit_count, .big, .unsigned); |
| 2956 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2956 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2957 | m.readTwosComplement(buffer[0..16], bit_count, .Little, .unsigned); | 2957 | m.readTwosComplement(buffer[0..16], bit_count, .little, .unsigned); |
| 2958 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2958 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2959 | m.readTwosComplement(buffer[0..16], bit_count, .Big, .unsigned); | 2959 | m.readTwosComplement(buffer[0..16], bit_count, .big, .unsigned); |
| 2960 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2960 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2961 | } | 2961 | } |
| 2962 | 2962 | ||
| ... | @@ -2975,15 +2975,15 @@ test "big int conversion write twos complement zero" { | ... | @@ -2975,15 +2975,15 @@ test "big int conversion write twos complement zero" { |
| 2975 | var buffer: []const u8 = undefined; | 2975 | var buffer: []const u8 = undefined; |
| 2976 | 2976 | ||
| 2977 | buffer = &([_]u8{0} ** 13); | 2977 | buffer = &([_]u8{0} ** 13); |
| 2978 | m.readTwosComplement(buffer[0..13], bit_count, .Little, .unsigned); | 2978 | m.readTwosComplement(buffer[0..13], bit_count, .little, .unsigned); |
| 2979 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2979 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2980 | m.readTwosComplement(buffer[0..13], bit_count, .Big, .unsigned); | 2980 | m.readTwosComplement(buffer[0..13], bit_count, .big, .unsigned); |
| 2981 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2981 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2982 | 2982 | ||
| 2983 | buffer = &([_]u8{0} ** 16); | 2983 | buffer = &([_]u8{0} ** 16); |
| 2984 | m.readTwosComplement(buffer[0..16], bit_count, .Little, .unsigned); | 2984 | m.readTwosComplement(buffer[0..16], bit_count, .little, .unsigned); |
| 2985 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2985 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2986 | m.readTwosComplement(buffer[0..16], bit_count, .Big, .unsigned); | 2986 | m.readTwosComplement(buffer[0..16], bit_count, .big, .unsigned); |
| 2987 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); | 2987 | try testing.expect(m.toConst().orderAgainstScalar(0x0) == .eq); |
| 2988 | } | 2988 | } |
| 2989 | 2989 |
lib/std/mem.zig+126-456| ... | @@ -1499,12 +1499,12 @@ test "containsAtLeast" { | ... | @@ -1499,12 +1499,12 @@ test "containsAtLeast" { |
| 1499 | pub fn readVarInt(comptime ReturnType: type, bytes: []const u8, endian: Endian) ReturnType { | 1499 | pub fn readVarInt(comptime ReturnType: type, bytes: []const u8, endian: Endian) ReturnType { |
| 1500 | var result: ReturnType = 0; | 1500 | var result: ReturnType = 0; |
| 1501 | switch (endian) { | 1501 | switch (endian) { |
| 1502 | .Big => { | 1502 | .big => { |
| 1503 | for (bytes) |b| { | 1503 | for (bytes) |b| { |
| 1504 | result = (result << 8) | b; | 1504 | result = (result << 8) | b; |
| 1505 | } | 1505 | } |
| 1506 | }, | 1506 | }, |
| 1507 | .Little => { | 1507 | .little => { |
| 1508 | const ShiftType = math.Log2Int(ReturnType); | 1508 | const ShiftType = math.Log2Int(ReturnType); |
| 1509 | for (bytes, 0..) |b, index| { | 1509 | for (bytes, 0..) |b, index| { |
| 1510 | result = result | (@as(ReturnType, b) << @as(ShiftType, @intCast(index * 8))); | 1510 | result = result | (@as(ReturnType, b) << @as(ShiftType, @intCast(index * 8))); |
| ... | @@ -1539,8 +1539,8 @@ pub fn readVarPackedInt( | ... | @@ -1539,8 +1539,8 @@ pub fn readVarPackedInt( |
| 1539 | const pad = @as(Log2N, @intCast(@bitSizeOf(T) - bit_count)); | 1539 | const pad = @as(Log2N, @intCast(@bitSizeOf(T) - bit_count)); |
| 1540 | 1540 | ||
| 1541 | const lowest_byte = switch (endian) { | 1541 | const lowest_byte = switch (endian) { |
| 1542 | .Big => bytes.len - (bit_offset / 8) - read_size, | 1542 | .big => bytes.len - (bit_offset / 8) - read_size, |
| 1543 | .Little => bit_offset / 8, | 1543 | .little => bit_offset / 8, |
| 1544 | }; | 1544 | }; |
| 1545 | const read_bytes = bytes[lowest_byte..][0..read_size]; | 1545 | const read_bytes = bytes[lowest_byte..][0..read_size]; |
| 1546 | 1546 | ||
| ... | @@ -1550,7 +1550,7 @@ pub fn readVarPackedInt( | ... | @@ -1550,7 +1550,7 @@ pub fn readVarPackedInt( |
| 1550 | const value = if (read_size == 1) b: { | 1550 | const value = if (read_size == 1) b: { |
| 1551 | break :b @as(uN, @truncate(read_bytes[0] >> bit_shift)); | 1551 | break :b @as(uN, @truncate(read_bytes[0] >> bit_shift)); |
| 1552 | } else b: { | 1552 | } else b: { |
| 1553 | const i: u1 = @intFromBool(endian == .Big); | 1553 | const i: u1 = @intFromBool(endian == .big); |
| 1554 | const head = @as(uN, @truncate(read_bytes[i] >> bit_shift)); | 1554 | const head = @as(uN, @truncate(read_bytes[i] >> bit_shift)); |
| 1555 | const tail_shift = @as(Log2N, @intCast(@as(u4, 8) - bit_shift)); | 1555 | const tail_shift = @as(Log2N, @intCast(@as(u4, 8) - bit_shift)); |
| 1556 | const tail = @as(uN, @truncate(read_bytes[1 - i])); | 1556 | const tail = @as(uN, @truncate(read_bytes[1 - i])); |
| ... | @@ -1565,13 +1565,13 @@ pub fn readVarPackedInt( | ... | @@ -1565,13 +1565,13 @@ pub fn readVarPackedInt( |
| 1565 | // Copy the value out (respecting endianness), accounting for bit_shift | 1565 | // Copy the value out (respecting endianness), accounting for bit_shift |
| 1566 | var int: uN = 0; | 1566 | var int: uN = 0; |
| 1567 | switch (endian) { | 1567 | switch (endian) { |
| 1568 | .Big => { | 1568 | .big => { |
| 1569 | for (read_bytes[0 .. read_size - 1]) |elem| { | 1569 | for (read_bytes[0 .. read_size - 1]) |elem| { |
| 1570 | int = elem | (int << 8); | 1570 | int = elem | (int << 8); |
| 1571 | } | 1571 | } |
| 1572 | int = (read_bytes[read_size - 1] >> bit_shift) | (int << (@as(u4, 8) - bit_shift)); | 1572 | int = (read_bytes[read_size - 1] >> bit_shift) | (int << (@as(u4, 8) - bit_shift)); |
| 1573 | }, | 1573 | }, |
| 1574 | .Little => { | 1574 | .little => { |
| 1575 | int = read_bytes[0] >> bit_shift; | 1575 | int = read_bytes[0] >> bit_shift; |
| 1576 | for (read_bytes[1..], 0..) |elem, i| { | 1576 | for (read_bytes[1..], 0..) |elem, i| { |
| 1577 | int |= (@as(uN, elem) << @as(Log2N, @intCast((8 * (i + 1) - bit_shift)))); | 1577 | int |= (@as(uN, elem) << @as(Log2N, @intCast((8 * (i + 1) - bit_shift)))); |
| ... | @@ -1587,68 +1587,29 @@ pub fn readVarPackedInt( | ... | @@ -1587,68 +1587,29 @@ pub fn readVarPackedInt( |
| 1587 | /// Reads an integer from memory with bit count specified by T. | 1587 | /// Reads an integer from memory with bit count specified by T. |
| 1588 | /// The bit count of T must be evenly divisible by 8. | 1588 | /// The bit count of T must be evenly divisible by 8. |
| 1589 | /// This function cannot fail and cannot cause undefined behavior. | 1589 | /// This function cannot fail and cannot cause undefined behavior. |
| 1590 | /// Assumes the endianness of memory is native. This means the function can | 1590 | pub inline fn readInt(comptime T: type, buffer: *const [@divExact(@typeInfo(T).Int.bits, 8)]u8, endian: Endian) T { |
| 1591 | /// simply pointer cast memory. | 1591 | const value: T = @bitCast(buffer.*); |
| 1592 | pub fn readIntNative(comptime T: type, bytes: *const [@divExact(@typeInfo(T).Int.bits, 8)]u8) T { | 1592 | return if (endian == native_endian) value else @byteSwap(value); |
| 1593 | return @as(*align(1) const T, @ptrCast(bytes)).*; | ||
| 1594 | } | 1593 | } |
| 1595 | 1594 | ||
| 1596 | /// Reads an integer from memory with bit count specified by T. | 1595 | test readInt { |
| 1597 | /// The bit count of T must be evenly divisible by 8. | 1596 | try testing.expect(readInt(u0, &[_]u8{}, .big) == 0x0); |
| 1598 | /// This function cannot fail and cannot cause undefined behavior. | 1597 | try testing.expect(readInt(u0, &[_]u8{}, .little) == 0x0); |
| 1599 | /// Assumes the endianness of memory is foreign, so it must byte-swap. | ||
| 1600 | pub fn readIntForeign(comptime T: type, bytes: *const [@divExact(@typeInfo(T).Int.bits, 8)]u8) T { | ||
| 1601 | return @byteSwap(readIntNative(T, bytes)); | ||
| 1602 | } | ||
| 1603 | 1598 | ||
| 1604 | pub const readIntLittle = switch (native_endian) { | 1599 | try testing.expect(readInt(u8, &[_]u8{0x32}, .big) == 0x32); |
| 1605 | .Little => readIntNative, | 1600 | try testing.expect(readInt(u8, &[_]u8{0x12}, .little) == 0x12); |
| 1606 | .Big => readIntForeign, | ||
| 1607 | }; | ||
| 1608 | |||
| 1609 | pub const readIntBig = switch (native_endian) { | ||
| 1610 | .Little => readIntForeign, | ||
| 1611 | .Big => readIntNative, | ||
| 1612 | }; | ||
| 1613 | |||
| 1614 | /// Asserts that bytes.len >= @typeInfo(T).Int.bits / 8. Reads the integer starting from index 0 | ||
| 1615 | /// and ignores extra bytes. | ||
| 1616 | /// The bit count of T must be evenly divisible by 8. | ||
| 1617 | /// Assumes the endianness of memory is native. This means the function can | ||
| 1618 | /// simply pointer cast memory. | ||
| 1619 | pub fn readIntSliceNative(comptime T: type, bytes: []const u8) T { | ||
| 1620 | const n = @divExact(@typeInfo(T).Int.bits, 8); | ||
| 1621 | assert(bytes.len >= n); | ||
| 1622 | return readIntNative(T, bytes[0..n]); | ||
| 1623 | } | ||
| 1624 | 1601 | ||
| 1625 | /// Asserts that bytes.len >= @typeInfo(T).Int.bits / 8. Reads the integer starting from index 0 | 1602 | try testing.expect(readInt(u16, &[_]u8{ 0x12, 0x34 }, .big) == 0x1234); |
| 1626 | /// and ignores extra bytes. | 1603 | try testing.expect(readInt(u16, &[_]u8{ 0x12, 0x34 }, .little) == 0x3412); |
| 1627 | /// The bit count of T must be evenly divisible by 8. | ||
| 1628 | /// Assumes the endianness of memory is foreign, so it must byte-swap. | ||
| 1629 | pub fn readIntSliceForeign(comptime T: type, bytes: []const u8) T { | ||
| 1630 | return @byteSwap(readIntSliceNative(T, bytes)); | ||
| 1631 | } | ||
| 1632 | 1604 | ||
| 1633 | pub const readIntSliceLittle = switch (native_endian) { | 1605 | try testing.expect(readInt(u72, &[_]u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 }, .big) == 0x123456789abcdef024); |
| 1634 | .Little => readIntSliceNative, | 1606 | try testing.expect(readInt(u72, &[_]u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }, .little) == 0xfedcba9876543210ec); |
| 1635 | .Big => readIntSliceForeign, | ||
| 1636 | }; | ||
| 1637 | 1607 | ||
| 1638 | pub const readIntSliceBig = switch (native_endian) { | 1608 | try testing.expect(readInt(i8, &[_]u8{0xff}, .big) == -1); |
| 1639 | .Little => readIntSliceForeign, | 1609 | try testing.expect(readInt(i8, &[_]u8{0xfe}, .little) == -2); |
| 1640 | .Big => readIntSliceNative, | ||
| 1641 | }; | ||
| 1642 | 1610 | ||
| 1643 | /// Reads an integer from memory with bit count specified by T. | 1611 | try testing.expect(readInt(i16, &[_]u8{ 0xff, 0xfd }, .big) == -3); |
| 1644 | /// The bit count of T must be evenly divisible by 8. | 1612 | try testing.expect(readInt(i16, &[_]u8{ 0xfc, 0xff }, .little) == -4); |
| 1645 | /// This function cannot fail and cannot cause undefined behavior. | ||
| 1646 | pub fn readInt(comptime T: type, bytes: *const [@divExact(@typeInfo(T).Int.bits, 8)]u8, endian: Endian) T { | ||
| 1647 | if (endian == native_endian) { | ||
| 1648 | return readIntNative(T, bytes); | ||
| 1649 | } else { | ||
| 1650 | return readIntForeign(T, bytes); | ||
| 1651 | } | ||
| 1652 | } | 1613 | } |
| 1653 | 1614 | ||
| 1654 | fn readPackedIntLittle(comptime T: type, bytes: []const u8, bit_offset: usize) T { | 1615 | fn readPackedIntLittle(comptime T: type, bytes: []const u8, bit_offset: usize) T { |
| ... | @@ -1668,7 +1629,7 @@ fn readPackedIntLittle(comptime T: type, bytes: []const u8, bit_offset: usize) T | ... | @@ -1668,7 +1629,7 @@ fn readPackedIntLittle(comptime T: type, bytes: []const u8, bit_offset: usize) T |
| 1668 | // Read by loading a LoadInt, and then follow it up with a 1-byte read | 1629 | // Read by loading a LoadInt, and then follow it up with a 1-byte read |
| 1669 | // of the tail if bit_offset pushed us over a byte boundary. | 1630 | // of the tail if bit_offset pushed us over a byte boundary. |
| 1670 | const read_bytes = bytes[bit_offset / 8 ..]; | 1631 | const read_bytes = bytes[bit_offset / 8 ..]; |
| 1671 | const val = @as(uN, @truncate(readIntLittle(LoadInt, read_bytes[0..load_size]) >> bit_shift)); | 1632 | const val = @as(uN, @truncate(readInt(LoadInt, read_bytes[0..load_size], .little) >> bit_shift)); |
| 1672 | if (bit_shift > load_tail_bits) { | 1633 | if (bit_shift > load_tail_bits) { |
| 1673 | const tail_bits = @as(Log2N, @intCast(bit_shift - load_tail_bits)); | 1634 | const tail_bits = @as(Log2N, @intCast(bit_shift - load_tail_bits)); |
| 1674 | const tail_byte = read_bytes[load_size]; | 1635 | const tail_byte = read_bytes[load_size]; |
| ... | @@ -1696,7 +1657,7 @@ fn readPackedIntBig(comptime T: type, bytes: []const u8, bit_offset: usize) T { | ... | @@ -1696,7 +1657,7 @@ fn readPackedIntBig(comptime T: type, bytes: []const u8, bit_offset: usize) T { |
| 1696 | // of the tail if bit_offset pushed us over a byte boundary. | 1657 | // of the tail if bit_offset pushed us over a byte boundary. |
| 1697 | const end = bytes.len - (bit_offset / 8); | 1658 | const end = bytes.len - (bit_offset / 8); |
| 1698 | const read_bytes = bytes[(end - byte_count)..end]; | 1659 | const read_bytes = bytes[(end - byte_count)..end]; |
| 1699 | const val = @as(uN, @truncate(readIntBig(LoadInt, bytes[(end - load_size)..end][0..load_size]) >> bit_shift)); | 1660 | const val = @as(uN, @truncate(readInt(LoadInt, bytes[(end - load_size)..end][0..load_size], .big) >> bit_shift)); |
| 1700 | if (bit_shift > load_tail_bits) { | 1661 | if (bit_shift > load_tail_bits) { |
| 1701 | const tail_bits = @as(Log2N, @intCast(bit_shift - load_tail_bits)); | 1662 | const tail_bits = @as(Log2N, @intCast(bit_shift - load_tail_bits)); |
| 1702 | const tail_byte = if (bit_count < 8) @as(uN, @truncate(read_bytes[0])) else @as(uN, read_bytes[0]); | 1663 | const tail_byte = if (bit_count < 8) @as(uN, @truncate(read_bytes[0])) else @as(uN, read_bytes[0]); |
| ... | @@ -1705,13 +1666,13 @@ fn readPackedIntBig(comptime T: type, bytes: []const u8, bit_offset: usize) T { | ... | @@ -1705,13 +1666,13 @@ fn readPackedIntBig(comptime T: type, bytes: []const u8, bit_offset: usize) T { |
| 1705 | } | 1666 | } |
| 1706 | 1667 | ||
| 1707 | pub const readPackedIntNative = switch (native_endian) { | 1668 | pub const readPackedIntNative = switch (native_endian) { |
| 1708 | .Little => readPackedIntLittle, | 1669 | .little => readPackedIntLittle, |
| 1709 | .Big => readPackedIntBig, | 1670 | .big => readPackedIntBig, |
| 1710 | }; | 1671 | }; |
| 1711 | 1672 | ||
| 1712 | pub const readPackedIntForeign = switch (native_endian) { | 1673 | pub const readPackedIntForeign = switch (native_endian) { |
| 1713 | .Little => readPackedIntBig, | 1674 | .little => readPackedIntBig, |
| 1714 | .Big => readPackedIntLittle, | 1675 | .big => readPackedIntLittle, |
| 1715 | }; | 1676 | }; |
| 1716 | 1677 | ||
| 1717 | /// Loads an integer from packed memory. | 1678 | /// Loads an integer from packed memory. |
| ... | @@ -1724,94 +1685,68 @@ pub const readPackedIntForeign = switch (native_endian) { | ... | @@ -1724,94 +1685,68 @@ pub const readPackedIntForeign = switch (native_endian) { |
| 1724 | /// | 1685 | /// |
| 1725 | pub fn readPackedInt(comptime T: type, bytes: []const u8, bit_offset: usize, endian: Endian) T { | 1686 | pub fn readPackedInt(comptime T: type, bytes: []const u8, bit_offset: usize, endian: Endian) T { |
| 1726 | switch (endian) { | 1687 | switch (endian) { |
| 1727 | .Little => return readPackedIntLittle(T, bytes, bit_offset), | 1688 | .little => return readPackedIntLittle(T, bytes, bit_offset), |
| 1728 | .Big => return readPackedIntBig(T, bytes, bit_offset), | 1689 | .big => return readPackedIntBig(T, bytes, bit_offset), |
| 1729 | } | 1690 | } |
| 1730 | } | 1691 | } |
| 1731 | 1692 | ||
| 1732 | /// Asserts that bytes.len >= @typeInfo(T).Int.bits / 8. Reads the integer starting from index 0 | ||
| 1733 | /// and ignores extra bytes. | ||
| 1734 | /// The bit count of T must be evenly divisible by 8. | ||
| 1735 | pub fn readIntSlice(comptime T: type, bytes: []const u8, endian: Endian) T { | ||
| 1736 | const n = @divExact(@typeInfo(T).Int.bits, 8); | ||
| 1737 | assert(bytes.len >= n); | ||
| 1738 | return readInt(T, bytes[0..n], endian); | ||
| 1739 | } | ||
| 1740 | |||
| 1741 | test "comptime read/write int" { | 1693 | test "comptime read/write int" { |
| 1742 | comptime { | 1694 | comptime { |
| 1743 | var bytes: [2]u8 = undefined; | 1695 | var bytes: [2]u8 = undefined; |
| 1744 | writeIntLittle(u16, &bytes, 0x1234); | 1696 | writeInt(u16, &bytes, 0x1234, .little); |
| 1745 | const result = readIntBig(u16, &bytes); | 1697 | const result = readInt(u16, &bytes, .big); |
| 1746 | try testing.expect(result == 0x3412); | 1698 | try testing.expect(result == 0x3412); |
| 1747 | } | 1699 | } |
| 1748 | comptime { | 1700 | comptime { |
| 1749 | var bytes: [2]u8 = undefined; | 1701 | var bytes: [2]u8 = undefined; |
| 1750 | writeIntBig(u16, &bytes, 0x1234); | 1702 | writeInt(u16, &bytes, 0x1234, .big); |
| 1751 | const result = readIntLittle(u16, &bytes); | 1703 | const result = readInt(u16, &bytes, .little); |
| 1752 | try testing.expect(result == 0x3412); | 1704 | try testing.expect(result == 0x3412); |
| 1753 | } | 1705 | } |
| 1754 | } | 1706 | } |
| 1755 | 1707 | ||
| 1756 | test "readIntBig and readIntLittle" { | ||
| 1757 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; | ||
| 1758 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; | ||
| 1759 | |||
| 1760 | try testing.expect(readIntSliceBig(u0, &[_]u8{}) == 0x0); | ||
| 1761 | try testing.expect(readIntSliceLittle(u0, &[_]u8{}) == 0x0); | ||
| 1762 | |||
| 1763 | try testing.expect(readIntSliceBig(u8, &[_]u8{0x32}) == 0x32); | ||
| 1764 | try testing.expect(readIntSliceLittle(u8, &[_]u8{0x12}) == 0x12); | ||
| 1765 | |||
| 1766 | try testing.expect(readIntSliceBig(u16, &[_]u8{ 0x12, 0x34 }) == 0x1234); | ||
| 1767 | try testing.expect(readIntSliceLittle(u16, &[_]u8{ 0x12, 0x34 }) == 0x3412); | ||
| 1768 | |||
| 1769 | try testing.expect(readIntSliceBig(u72, &[_]u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 }) == 0x123456789abcdef024); | ||
| 1770 | try testing.expect(readIntSliceLittle(u72, &[_]u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }) == 0xfedcba9876543210ec); | ||
| 1771 | |||
| 1772 | try testing.expect(readIntSliceBig(i8, &[_]u8{0xff}) == -1); | ||
| 1773 | try testing.expect(readIntSliceLittle(i8, &[_]u8{0xfe}) == -2); | ||
| 1774 | |||
| 1775 | try testing.expect(readIntSliceBig(i16, &[_]u8{ 0xff, 0xfd }) == -3); | ||
| 1776 | try testing.expect(readIntSliceLittle(i16, &[_]u8{ 0xfc, 0xff }) == -4); | ||
| 1777 | } | ||
| 1778 | |||
| 1779 | /// Writes an integer to memory, storing it in twos-complement. | ||
| 1780 | /// This function always succeeds, has defined behavior for all inputs, and | ||
| 1781 | /// accepts any integer bit width. | ||
| 1782 | /// This function stores in native endian, which means it is implemented as a simple | ||
| 1783 | /// memory store. | ||
| 1784 | pub fn writeIntNative(comptime T: type, buf: *[@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8, value: T) void { | ||
| 1785 | @as(*align(1) T, @ptrCast(buf)).* = value; | ||
| 1786 | } | ||
| 1787 | |||
| 1788 | /// Writes an integer to memory, storing it in twos-complement. | 1708 | /// Writes an integer to memory, storing it in twos-complement. |
| 1789 | /// This function always succeeds, has defined behavior for all inputs, but | 1709 | /// This function always succeeds, has defined behavior for all inputs, but |
| 1790 | /// the integer bit width must be divisible by 8. | 1710 | /// the integer bit width must be divisible by 8. |
| 1791 | /// This function stores in foreign endian, which means it does a @byteSwap first. | 1711 | pub inline fn writeInt(comptime T: type, buffer: *[@divExact(@typeInfo(T).Int.bits, 8)]u8, value: T, endian: Endian) void { |
| 1792 | pub fn writeIntForeign(comptime T: type, buf: *[@divExact(@typeInfo(T).Int.bits, 8)]u8, value: T) void { | 1712 | buffer.* = @bitCast(if (endian == native_endian) value else @byteSwap(value)); |
| 1793 | writeIntNative(T, buf, @byteSwap(value)); | ||
| 1794 | } | 1713 | } |
| 1795 | 1714 | ||
| 1796 | pub const writeIntLittle = switch (native_endian) { | 1715 | test writeInt { |
| 1797 | .Little => writeIntNative, | 1716 | var buf0: [0]u8 = undefined; |
| 1798 | .Big => writeIntForeign, | 1717 | var buf1: [1]u8 = undefined; |
| 1799 | }; | 1718 | var buf2: [2]u8 = undefined; |
| 1719 | var buf9: [9]u8 = undefined; | ||
| 1800 | 1720 | ||
| 1801 | pub const writeIntBig = switch (native_endian) { | 1721 | writeInt(u0, &buf0, 0x0, .big); |
| 1802 | .Little => writeIntForeign, | 1722 | try testing.expect(eql(u8, buf0[0..], &[_]u8{})); |
| 1803 | .Big => writeIntNative, | 1723 | writeInt(u0, &buf0, 0x0, .little); |
| 1804 | }; | 1724 | try testing.expect(eql(u8, buf0[0..], &[_]u8{})); |
| 1805 | 1725 | ||
| 1806 | /// Writes an integer to memory, storing it in twos-complement. | 1726 | writeInt(u8, &buf1, 0x12, .big); |
| 1807 | /// This function always succeeds, has defined behavior for all inputs, but | 1727 | try testing.expect(eql(u8, buf1[0..], &[_]u8{0x12})); |
| 1808 | /// the integer bit width must be divisible by 8. | 1728 | writeInt(u8, &buf1, 0x34, .little); |
| 1809 | pub fn writeInt(comptime T: type, buffer: *[@divExact(@typeInfo(T).Int.bits, 8)]u8, value: T, endian: Endian) void { | 1729 | try testing.expect(eql(u8, buf1[0..], &[_]u8{0x34})); |
| 1810 | if (endian == native_endian) { | 1730 | |
| 1811 | return writeIntNative(T, buffer, value); | 1731 | writeInt(u16, &buf2, 0x1234, .big); |
| 1812 | } else { | 1732 | try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0x12, 0x34 })); |
| 1813 | return writeIntForeign(T, buffer, value); | 1733 | writeInt(u16, &buf2, 0x5678, .little); |
| 1814 | } | 1734 | try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0x78, 0x56 })); |
| 1735 | |||
| 1736 | writeInt(u72, &buf9, 0x123456789abcdef024, .big); | ||
| 1737 | try testing.expect(eql(u8, buf9[0..], &[_]u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 })); | ||
| 1738 | writeInt(u72, &buf9, 0xfedcba9876543210ec, .little); | ||
| 1739 | try testing.expect(eql(u8, buf9[0..], &[_]u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe })); | ||
| 1740 | |||
| 1741 | writeInt(i8, &buf1, -1, .big); | ||
| 1742 | try testing.expect(eql(u8, buf1[0..], &[_]u8{0xff})); | ||
| 1743 | writeInt(i8, &buf1, -2, .little); | ||
| 1744 | try testing.expect(eql(u8, buf1[0..], &[_]u8{0xfe})); | ||
| 1745 | |||
| 1746 | writeInt(i16, &buf2, -3, .big); | ||
| 1747 | try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0xff, 0xfd })); | ||
| 1748 | writeInt(i16, &buf2, -4, .little); | ||
| 1749 | try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0xfc, 0xff })); | ||
| 1815 | } | 1750 | } |
| 1816 | 1751 | ||
| 1817 | fn writePackedIntLittle(comptime T: type, bytes: []u8, bit_offset: usize, value: T) void { | 1752 | fn writePackedIntLittle(comptime T: type, bytes: []u8, bit_offset: usize, value: T) void { |
| ... | @@ -1844,7 +1779,7 @@ fn writePackedIntLittle(comptime T: type, bytes: []u8, bit_offset: usize, value: | ... | @@ -1844,7 +1779,7 @@ fn writePackedIntLittle(comptime T: type, bytes: []u8, bit_offset: usize, value: |
| 1844 | write_value |= @as(StoreInt, tail) << (8 * (store_size - 1)); | 1779 | write_value |= @as(StoreInt, tail) << (8 * (store_size - 1)); |
| 1845 | } | 1780 | } |
| 1846 | 1781 | ||
| 1847 | writeIntLittle(StoreInt, write_bytes[0..store_size], write_value); | 1782 | writeInt(StoreInt, write_bytes[0..store_size], write_value, .little); |
| 1848 | } | 1783 | } |
| 1849 | 1784 | ||
| 1850 | fn writePackedIntBig(comptime T: type, bytes: []u8, bit_offset: usize, value: T) void { | 1785 | fn writePackedIntBig(comptime T: type, bytes: []u8, bit_offset: usize, value: T) void { |
| ... | @@ -1879,17 +1814,17 @@ fn writePackedIntBig(comptime T: type, bytes: []u8, bit_offset: usize, value: T) | ... | @@ -1879,17 +1814,17 @@ fn writePackedIntBig(comptime T: type, bytes: []u8, bit_offset: usize, value: T) |
| 1879 | write_value |= @as(StoreInt, tail) << (8 * (store_size - 1)); | 1814 | write_value |= @as(StoreInt, tail) << (8 * (store_size - 1)); |
| 1880 | } | 1815 | } |
| 1881 | 1816 | ||
| 1882 | writeIntBig(StoreInt, write_bytes[(byte_count - store_size)..][0..store_size], write_value); | 1817 | writeInt(StoreInt, write_bytes[(byte_count - store_size)..][0..store_size], write_value, .big); |
| 1883 | } | 1818 | } |
| 1884 | 1819 | ||
| 1885 | pub const writePackedIntNative = switch (native_endian) { | 1820 | pub const writePackedIntNative = switch (native_endian) { |
| 1886 | .Little => writePackedIntLittle, | 1821 | .little => writePackedIntLittle, |
| 1887 | .Big => writePackedIntBig, | 1822 | .big => writePackedIntBig, |
| 1888 | }; | 1823 | }; |
| 1889 | 1824 | ||
| 1890 | pub const writePackedIntForeign = switch (native_endian) { | 1825 | pub const writePackedIntForeign = switch (native_endian) { |
| 1891 | .Little => writePackedIntBig, | 1826 | .little => writePackedIntBig, |
| 1892 | .Big => writePackedIntLittle, | 1827 | .big => writePackedIntLittle, |
| 1893 | }; | 1828 | }; |
| 1894 | 1829 | ||
| 1895 | /// Stores an integer to packed memory. | 1830 | /// Stores an integer to packed memory. |
| ... | @@ -1903,94 +1838,9 @@ pub const writePackedIntForeign = switch (native_endian) { | ... | @@ -1903,94 +1838,9 @@ pub const writePackedIntForeign = switch (native_endian) { |
| 1903 | /// | 1838 | /// |
| 1904 | pub fn writePackedInt(comptime T: type, bytes: []u8, bit_offset: usize, value: T, endian: Endian) void { | 1839 | pub fn writePackedInt(comptime T: type, bytes: []u8, bit_offset: usize, value: T, endian: Endian) void { |
| 1905 | switch (endian) { | 1840 | switch (endian) { |
| 1906 | .Little => writePackedIntLittle(T, bytes, bit_offset, value), | 1841 | .little => writePackedIntLittle(T, bytes, bit_offset, value), |
| 1907 | .Big => writePackedIntBig(T, bytes, bit_offset, value), | 1842 | .big => writePackedIntBig(T, bytes, bit_offset, value), |
| 1908 | } | ||
| 1909 | } | ||
| 1910 | |||
| 1911 | /// Writes a twos-complement little-endian integer to memory. | ||
| 1912 | /// Asserts that buf.len >= @typeInfo(T).Int.bits / 8. | ||
| 1913 | /// The bit count of T must be divisible by 8. | ||
| 1914 | /// Any extra bytes in buffer after writing the integer are set to zero. To | ||
| 1915 | /// avoid the branch to check for extra buffer bytes, use writeIntLittle | ||
| 1916 | /// instead. | ||
| 1917 | fn writeIntSliceLittleNative(comptime T: type, buffer: []u8, value: T) void { | ||
| 1918 | assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8)); | ||
| 1919 | |||
| 1920 | if (@typeInfo(T).Int.bits == 0) { | ||
| 1921 | return @memset(buffer, 0); | ||
| 1922 | } else if (@typeInfo(T).Int.bits == 8) { | ||
| 1923 | @memset(buffer, 0); | ||
| 1924 | buffer[0] = @as(u8, @bitCast(value)); | ||
| 1925 | return; | ||
| 1926 | } | ||
| 1927 | |||
| 1928 | const write_end = @divExact(@typeInfo(T).Int.bits, 8); | ||
| 1929 | @as(*align(1) T, @ptrCast(buffer)).* = value; | ||
| 1930 | @memset(buffer[write_end..], 0); | ||
| 1931 | } | ||
| 1932 | |||
| 1933 | fn writeIntSliceLittleForeign(comptime T: type, buffer: []u8, value: T) void { | ||
| 1934 | return writeIntSliceLittleNative(T, buffer, @byteSwap(value)); | ||
| 1935 | } | ||
| 1936 | |||
| 1937 | pub const writeIntSliceLittle = switch (native_endian) { | ||
| 1938 | .Little => writeIntSliceLittleNative, | ||
| 1939 | .Big => writeIntSliceLittleForeign, | ||
| 1940 | }; | ||
| 1941 | |||
| 1942 | /// Writes a twos-complement big-endian integer to memory. | ||
| 1943 | /// Asserts that buffer.len >= @typeInfo(T).Int.bits / 8. | ||
| 1944 | /// The bit count of T must be divisible by 8. | ||
| 1945 | /// Any extra bytes in buffer before writing the integer are set to zero. To | ||
| 1946 | /// avoid the branch to check for extra buffer bytes, use writeIntBig instead. | ||
| 1947 | fn writeIntSliceBigNative(comptime T: type, buffer: []u8, value: T) void { | ||
| 1948 | assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8)); | ||
| 1949 | |||
| 1950 | if (@typeInfo(T).Int.bits == 0) { | ||
| 1951 | return @memset(buffer, 0); | ||
| 1952 | } else if (@typeInfo(T).Int.bits == 8) { | ||
| 1953 | @memset(buffer, 0); | ||
| 1954 | buffer[buffer.len - 1] = @as(u8, @bitCast(value)); | ||
| 1955 | return; | ||
| 1956 | } | 1843 | } |
| 1957 | |||
| 1958 | const write_start = buffer.len - @divExact(@typeInfo(T).Int.bits, 8); | ||
| 1959 | @memset(buffer[0..write_start], 0); | ||
| 1960 | @as(*align(1) T, @ptrCast(buffer[write_start..])).* = value; | ||
| 1961 | } | ||
| 1962 | |||
| 1963 | fn writeIntSliceBigForeign(comptime T: type, buffer: []u8, value: T) void { | ||
| 1964 | return writeIntSliceBigNative(T, buffer, @byteSwap(value)); | ||
| 1965 | } | ||
| 1966 | |||
| 1967 | pub const writeIntSliceBig = switch (native_endian) { | ||
| 1968 | .Little => writeIntSliceBigForeign, | ||
| 1969 | .Big => writeIntSliceBigNative, | ||
| 1970 | }; | ||
| 1971 | |||
| 1972 | pub const writeIntSliceNative = switch (native_endian) { | ||
| 1973 | .Little => writeIntSliceLittleNative, | ||
| 1974 | .Big => writeIntSliceBigNative, | ||
| 1975 | }; | ||
| 1976 | |||
| 1977 | pub const writeIntSliceForeign = switch (native_endian) { | ||
| 1978 | .Little => writeIntSliceBigForeign, | ||
| 1979 | .Big => writeIntSliceLittleForeign, | ||
| 1980 | }; | ||
| 1981 | |||
| 1982 | /// Writes a twos-complement integer to memory, with the specified endianness. | ||
| 1983 | /// Asserts that buf.len >= @typeInfo(T).Int.bits / 8. | ||
| 1984 | /// The bit count of T must be evenly divisible by 8. | ||
| 1985 | /// Any extra bytes in buffer not part of the integer are set to zero, with | ||
| 1986 | /// respect to endianness. To avoid the branch to check for extra buffer bytes, | ||
| 1987 | /// use writeInt instead. | ||
| 1988 | pub fn writeIntSlice(comptime T: type, buffer: []u8, value: T, endian: Endian) void { | ||
| 1989 | comptime assert(@typeInfo(T).Int.bits % 8 == 0); | ||
| 1990 | return switch (endian) { | ||
| 1991 | .Little => writeIntSliceLittle(T, buffer, value), | ||
| 1992 | .Big => writeIntSliceBig(T, buffer, value), | ||
| 1993 | }; | ||
| 1994 | } | 1844 | } |
| 1995 | 1845 | ||
| 1996 | /// Stores an integer to packed memory with provided bit_count, bit_offset, and signedness. | 1846 | /// Stores an integer to packed memory with provided bit_count, bit_offset, and signedness. |
| ... | @@ -2010,8 +1860,8 @@ pub fn writeVarPackedInt(bytes: []u8, bit_offset: usize, bit_count: usize, value | ... | @@ -2010,8 +1860,8 @@ pub fn writeVarPackedInt(bytes: []u8, bit_offset: usize, bit_count: usize, value |
| 2010 | const bit_shift = @as(u3, @intCast(bit_offset % 8)); | 1860 | const bit_shift = @as(u3, @intCast(bit_offset % 8)); |
| 2011 | const write_size = (bit_count + bit_shift + 7) / 8; | 1861 | const write_size = (bit_count + bit_shift + 7) / 8; |
| 2012 | const lowest_byte = switch (endian) { | 1862 | const lowest_byte = switch (endian) { |
| 2013 | .Big => bytes.len - (bit_offset / 8) - write_size, | 1863 | .big => bytes.len - (bit_offset / 8) - write_size, |
| 2014 | .Little => bit_offset / 8, | 1864 | .little => bit_offset / 8, |
| 2015 | }; | 1865 | }; |
| 2016 | const write_bytes = bytes[lowest_byte..][0..write_size]; | 1866 | const write_bytes = bytes[lowest_byte..][0..write_size]; |
| 2017 | 1867 | ||
| ... | @@ -2027,8 +1877,8 @@ pub fn writeVarPackedInt(bytes: []u8, bit_offset: usize, bit_count: usize, value | ... | @@ -2027,8 +1877,8 @@ pub fn writeVarPackedInt(bytes: []u8, bit_offset: usize, bit_count: usize, value |
| 2027 | var remaining: T = value; | 1877 | var remaining: T = value; |
| 2028 | 1878 | ||
| 2029 | // Iterate bytes forward for Little-endian, backward for Big-endian | 1879 | // Iterate bytes forward for Little-endian, backward for Big-endian |
| 2030 | const delta: i2 = if (endian == .Big) -1 else 1; | 1880 | const delta: i2 = if (endian == .big) -1 else 1; |
| 2031 | const start = if (endian == .Big) @as(isize, @intCast(write_bytes.len - 1)) else 0; | 1881 | const start = if (endian == .big) @as(isize, @intCast(write_bytes.len - 1)) else 0; |
| 2032 | 1882 | ||
| 2033 | var i: isize = start; // isize for signed index arithmetic | 1883 | var i: isize = start; // isize for signed index arithmetic |
| 2034 | 1884 | ||
| ... | @@ -2055,45 +1905,6 @@ pub fn writeVarPackedInt(bytes: []u8, bit_offset: usize, bit_count: usize, value | ... | @@ -2055,45 +1905,6 @@ pub fn writeVarPackedInt(bytes: []u8, bit_offset: usize, bit_count: usize, value |
| 2055 | write_bytes[@as(usize, @intCast(i))] |= @as(u8, @intCast(@as(uN, @bitCast(remaining)) & tail_mask)); | 1905 | write_bytes[@as(usize, @intCast(i))] |= @as(u8, @intCast(@as(uN, @bitCast(remaining)) & tail_mask)); |
| 2056 | } | 1906 | } |
| 2057 | 1907 | ||
| 2058 | test "writeIntBig and writeIntLittle" { | ||
| 2059 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; | ||
| 2060 | |||
| 2061 | var buf0: [0]u8 = undefined; | ||
| 2062 | var buf1: [1]u8 = undefined; | ||
| 2063 | var buf2: [2]u8 = undefined; | ||
| 2064 | var buf9: [9]u8 = undefined; | ||
| 2065 | |||
| 2066 | writeIntBig(u0, &buf0, 0x0); | ||
| 2067 | try testing.expect(eql(u8, buf0[0..], &[_]u8{})); | ||
| 2068 | writeIntLittle(u0, &buf0, 0x0); | ||
| 2069 | try testing.expect(eql(u8, buf0[0..], &[_]u8{})); | ||
| 2070 | |||
| 2071 | writeIntBig(u8, &buf1, 0x12); | ||
| 2072 | try testing.expect(eql(u8, buf1[0..], &[_]u8{0x12})); | ||
| 2073 | writeIntLittle(u8, &buf1, 0x34); | ||
| 2074 | try testing.expect(eql(u8, buf1[0..], &[_]u8{0x34})); | ||
| 2075 | |||
| 2076 | writeIntBig(u16, &buf2, 0x1234); | ||
| 2077 | try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0x12, 0x34 })); | ||
| 2078 | writeIntLittle(u16, &buf2, 0x5678); | ||
| 2079 | try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0x78, 0x56 })); | ||
| 2080 | |||
| 2081 | writeIntBig(u72, &buf9, 0x123456789abcdef024); | ||
| 2082 | try testing.expect(eql(u8, buf9[0..], &[_]u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 })); | ||
| 2083 | writeIntLittle(u72, &buf9, 0xfedcba9876543210ec); | ||
| 2084 | try testing.expect(eql(u8, buf9[0..], &[_]u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe })); | ||
| 2085 | |||
| 2086 | writeIntBig(i8, &buf1, -1); | ||
| 2087 | try testing.expect(eql(u8, buf1[0..], &[_]u8{0xff})); | ||
| 2088 | writeIntLittle(i8, &buf1, -2); | ||
| 2089 | try testing.expect(eql(u8, buf1[0..], &[_]u8{0xfe})); | ||
| 2090 | |||
| 2091 | writeIntBig(i16, &buf2, -3); | ||
| 2092 | try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0xff, 0xfd })); | ||
| 2093 | writeIntLittle(i16, &buf2, -4); | ||
| 2094 | try testing.expect(eql(u8, buf2[0..], &[_]u8{ 0xfc, 0xff })); | ||
| 2095 | } | ||
| 2096 | |||
| 2097 | /// Swap the byte order of all the members of the fields of a struct | 1908 | /// Swap the byte order of all the members of the fields of a struct |
| 2098 | /// (Changing their endianness) | 1909 | /// (Changing their endianness) |
| 2099 | pub fn byteSwapAllFields(comptime S: type, ptr: *S) void { | 1910 | pub fn byteSwapAllFields(comptime S: type, ptr: *S) void { |
| ... | @@ -3311,12 +3122,12 @@ fn testReadIntImpl() !void { | ... | @@ -3311,12 +3122,12 @@ fn testReadIntImpl() !void { |
| 3311 | 0x56, | 3122 | 0x56, |
| 3312 | 0x78, | 3123 | 0x78, |
| 3313 | }; | 3124 | }; |
| 3314 | try testing.expect(readInt(u32, &bytes, Endian.Big) == 0x12345678); | 3125 | try testing.expect(readInt(u32, &bytes, .big) == 0x12345678); |
| 3315 | try testing.expect(readIntBig(u32, &bytes) == 0x12345678); | 3126 | try testing.expect(readInt(u32, &bytes, .big) == 0x12345678); |
| 3316 | try testing.expect(readIntBig(i32, &bytes) == 0x12345678); | 3127 | try testing.expect(readInt(i32, &bytes, .big) == 0x12345678); |
| 3317 | try testing.expect(readInt(u32, &bytes, Endian.Little) == 0x78563412); | 3128 | try testing.expect(readInt(u32, &bytes, .little) == 0x78563412); |
| 3318 | try testing.expect(readIntLittle(u32, &bytes) == 0x78563412); | 3129 | try testing.expect(readInt(u32, &bytes, .little) == 0x78563412); |
| 3319 | try testing.expect(readIntLittle(i32, &bytes) == 0x78563412); | 3130 | try testing.expect(readInt(i32, &bytes, .little) == 0x78563412); |
| 3320 | } | 3131 | } |
| 3321 | { | 3132 | { |
| 3322 | const buf = [_]u8{ | 3133 | const buf = [_]u8{ |
| ... | @@ -3325,7 +3136,7 @@ fn testReadIntImpl() !void { | ... | @@ -3325,7 +3136,7 @@ fn testReadIntImpl() !void { |
| 3325 | 0x12, | 3136 | 0x12, |
| 3326 | 0x34, | 3137 | 0x34, |
| 3327 | }; | 3138 | }; |
| 3328 | const answer = readInt(u32, &buf, Endian.Big); | 3139 | const answer = readInt(u32, &buf, .big); |
| 3329 | try testing.expect(answer == 0x00001234); | 3140 | try testing.expect(answer == 0x00001234); |
| 3330 | } | 3141 | } |
| 3331 | { | 3142 | { |
| ... | @@ -3335,7 +3146,7 @@ fn testReadIntImpl() !void { | ... | @@ -3335,7 +3146,7 @@ fn testReadIntImpl() !void { |
| 3335 | 0x00, | 3146 | 0x00, |
| 3336 | 0x00, | 3147 | 0x00, |
| 3337 | }; | 3148 | }; |
| 3338 | const answer = readInt(u32, &buf, Endian.Little); | 3149 | const answer = readInt(u32, &buf, .little); |
| 3339 | try testing.expect(answer == 0x00003412); | 3150 | try testing.expect(answer == 0x00003412); |
| 3340 | } | 3151 | } |
| 3341 | { | 3152 | { |
| ... | @@ -3343,153 +3154,13 @@ fn testReadIntImpl() !void { | ... | @@ -3343,153 +3154,13 @@ fn testReadIntImpl() !void { |
| 3343 | 0xff, | 3154 | 0xff, |
| 3344 | 0xfe, | 3155 | 0xfe, |
| 3345 | }; | 3156 | }; |
| 3346 | try testing.expect(readIntBig(u16, &bytes) == 0xfffe); | 3157 | try testing.expect(readInt(u16, &bytes, .big) == 0xfffe); |
| 3347 | try testing.expect(readIntBig(i16, &bytes) == -0x0002); | 3158 | try testing.expect(readInt(i16, &bytes, .big) == -0x0002); |
| 3348 | try testing.expect(readIntLittle(u16, &bytes) == 0xfeff); | 3159 | try testing.expect(readInt(u16, &bytes, .little) == 0xfeff); |
| 3349 | try testing.expect(readIntLittle(i16, &bytes) == -0x0101); | 3160 | try testing.expect(readInt(i16, &bytes, .little) == -0x0101); |
| 3350 | } | 3161 | } |
| 3351 | } | 3162 | } |
| 3352 | 3163 | ||
| 3353 | test writeIntSlice { | ||
| 3354 | try testWriteIntImpl(); | ||
| 3355 | try comptime testWriteIntImpl(); | ||
| 3356 | } | ||
| 3357 | fn testWriteIntImpl() !void { | ||
| 3358 | var bytes: [8]u8 = undefined; | ||
| 3359 | |||
| 3360 | writeIntSlice(u0, bytes[0..], 0, Endian.Big); | ||
| 3361 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3362 | 0x00, 0x00, 0x00, 0x00, | ||
| 3363 | 0x00, 0x00, 0x00, 0x00, | ||
| 3364 | })); | ||
| 3365 | |||
| 3366 | writeIntSlice(u0, bytes[0..], 0, Endian.Little); | ||
| 3367 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3368 | 0x00, 0x00, 0x00, 0x00, | ||
| 3369 | 0x00, 0x00, 0x00, 0x00, | ||
| 3370 | })); | ||
| 3371 | |||
| 3372 | writeIntSlice(u64, bytes[0..], 0x12345678CAFEBABE, Endian.Big); | ||
| 3373 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3374 | 0x12, | ||
| 3375 | 0x34, | ||
| 3376 | 0x56, | ||
| 3377 | 0x78, | ||
| 3378 | 0xCA, | ||
| 3379 | 0xFE, | ||
| 3380 | 0xBA, | ||
| 3381 | 0xBE, | ||
| 3382 | })); | ||
| 3383 | |||
| 3384 | writeIntSlice(u64, bytes[0..], 0xBEBAFECA78563412, Endian.Little); | ||
| 3385 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3386 | 0x12, | ||
| 3387 | 0x34, | ||
| 3388 | 0x56, | ||
| 3389 | 0x78, | ||
| 3390 | 0xCA, | ||
| 3391 | 0xFE, | ||
| 3392 | 0xBA, | ||
| 3393 | 0xBE, | ||
| 3394 | })); | ||
| 3395 | |||
| 3396 | writeIntSlice(u32, bytes[0..], 0x12345678, Endian.Big); | ||
| 3397 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3398 | 0x00, | ||
| 3399 | 0x00, | ||
| 3400 | 0x00, | ||
| 3401 | 0x00, | ||
| 3402 | 0x12, | ||
| 3403 | 0x34, | ||
| 3404 | 0x56, | ||
| 3405 | 0x78, | ||
| 3406 | })); | ||
| 3407 | |||
| 3408 | writeIntSlice(u32, bytes[0..], 0x78563412, Endian.Little); | ||
| 3409 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3410 | 0x12, | ||
| 3411 | 0x34, | ||
| 3412 | 0x56, | ||
| 3413 | 0x78, | ||
| 3414 | 0x00, | ||
| 3415 | 0x00, | ||
| 3416 | 0x00, | ||
| 3417 | 0x00, | ||
| 3418 | })); | ||
| 3419 | |||
| 3420 | writeIntSlice(u16, bytes[0..], 0x1234, Endian.Big); | ||
| 3421 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3422 | 0x00, | ||
| 3423 | 0x00, | ||
| 3424 | 0x00, | ||
| 3425 | 0x00, | ||
| 3426 | 0x00, | ||
| 3427 | 0x00, | ||
| 3428 | 0x12, | ||
| 3429 | 0x34, | ||
| 3430 | })); | ||
| 3431 | |||
| 3432 | writeIntSlice(u16, bytes[0..], 0x1234, Endian.Little); | ||
| 3433 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3434 | 0x34, | ||
| 3435 | 0x12, | ||
| 3436 | 0x00, | ||
| 3437 | 0x00, | ||
| 3438 | 0x00, | ||
| 3439 | 0x00, | ||
| 3440 | 0x00, | ||
| 3441 | 0x00, | ||
| 3442 | })); | ||
| 3443 | |||
| 3444 | writeIntSlice(i16, bytes[0..], @as(i16, -21555), Endian.Little); | ||
| 3445 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3446 | 0xCD, | ||
| 3447 | 0xAB, | ||
| 3448 | 0x00, | ||
| 3449 | 0x00, | ||
| 3450 | 0x00, | ||
| 3451 | 0x00, | ||
| 3452 | 0x00, | ||
| 3453 | 0x00, | ||
| 3454 | })); | ||
| 3455 | |||
| 3456 | writeIntSlice(i16, bytes[0..], @as(i16, -21555), Endian.Big); | ||
| 3457 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3458 | 0x00, | ||
| 3459 | 0x00, | ||
| 3460 | 0x00, | ||
| 3461 | 0x00, | ||
| 3462 | 0x00, | ||
| 3463 | 0x00, | ||
| 3464 | 0xAB, | ||
| 3465 | 0xCD, | ||
| 3466 | })); | ||
| 3467 | |||
| 3468 | writeIntSlice(u8, bytes[0..], 0x12, Endian.Big); | ||
| 3469 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3470 | 0x00, 0x00, 0x00, 0x00, | ||
| 3471 | 0x00, 0x00, 0x00, 0x12, | ||
| 3472 | })); | ||
| 3473 | |||
| 3474 | writeIntSlice(u8, bytes[0..], 0x12, Endian.Little); | ||
| 3475 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3476 | 0x12, 0x00, 0x00, 0x00, | ||
| 3477 | 0x00, 0x00, 0x00, 0x00, | ||
| 3478 | })); | ||
| 3479 | |||
| 3480 | writeIntSlice(i8, bytes[0..], -1, Endian.Big); | ||
| 3481 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3482 | 0x00, 0x00, 0x00, 0x00, | ||
| 3483 | 0x00, 0x00, 0x00, 0xff, | ||
| 3484 | })); | ||
| 3485 | |||
| 3486 | writeIntSlice(i8, bytes[0..], -1, Endian.Little); | ||
| 3487 | try testing.expect(eql(u8, &bytes, &[_]u8{ | ||
| 3488 | 0xff, 0x00, 0x00, 0x00, | ||
| 3489 | 0x00, 0x00, 0x00, 0x00, | ||
| 3490 | })); | ||
| 3491 | } | ||
| 3492 | |||
| 3493 | /// Returns the smallest number in a slice. O(n). | 3164 | /// Returns the smallest number in a slice. O(n). |
| 3494 | /// `slice` must not be empty. | 3165 | /// `slice` must not be empty. |
| 3495 | pub fn min(comptime T: type, slice: []const T) T { | 3166 | pub fn min(comptime T: type, slice: []const T) T { |
| ... | @@ -3932,48 +3603,48 @@ test "replaceOwned" { | ... | @@ -3932,48 +3603,48 @@ test "replaceOwned" { |
| 3932 | /// Converts a little-endian integer to host endianness. | 3603 | /// Converts a little-endian integer to host endianness. |
| 3933 | pub fn littleToNative(comptime T: type, x: T) T { | 3604 | pub fn littleToNative(comptime T: type, x: T) T { |
| 3934 | return switch (native_endian) { | 3605 | return switch (native_endian) { |
| 3935 | .Little => x, | 3606 | .little => x, |
| 3936 | .Big => @byteSwap(x), | 3607 | .big => @byteSwap(x), |
| 3937 | }; | 3608 | }; |
| 3938 | } | 3609 | } |
| 3939 | 3610 | ||
| 3940 | /// Converts a big-endian integer to host endianness. | 3611 | /// Converts a big-endian integer to host endianness. |
| 3941 | pub fn bigToNative(comptime T: type, x: T) T { | 3612 | pub fn bigToNative(comptime T: type, x: T) T { |
| 3942 | return switch (native_endian) { | 3613 | return switch (native_endian) { |
| 3943 | .Little => @byteSwap(x), | 3614 | .little => @byteSwap(x), |
| 3944 | .Big => x, | 3615 | .big => x, |
| 3945 | }; | 3616 | }; |
| 3946 | } | 3617 | } |
| 3947 | 3618 | ||
| 3948 | /// Converts an integer from specified endianness to host endianness. | 3619 | /// Converts an integer from specified endianness to host endianness. |
| 3949 | pub fn toNative(comptime T: type, x: T, endianness_of_x: Endian) T { | 3620 | pub fn toNative(comptime T: type, x: T, endianness_of_x: Endian) T { |
| 3950 | return switch (endianness_of_x) { | 3621 | return switch (endianness_of_x) { |
| 3951 | .Little => littleToNative(T, x), | 3622 | .little => littleToNative(T, x), |
| 3952 | .Big => bigToNative(T, x), | 3623 | .big => bigToNative(T, x), |
| 3953 | }; | 3624 | }; |
| 3954 | } | 3625 | } |
| 3955 | 3626 | ||
| 3956 | /// Converts an integer which has host endianness to the desired endianness. | 3627 | /// Converts an integer which has host endianness to the desired endianness. |
| 3957 | pub fn nativeTo(comptime T: type, x: T, desired_endianness: Endian) T { | 3628 | pub fn nativeTo(comptime T: type, x: T, desired_endianness: Endian) T { |
| 3958 | return switch (desired_endianness) { | 3629 | return switch (desired_endianness) { |
| 3959 | .Little => nativeToLittle(T, x), | 3630 | .little => nativeToLittle(T, x), |
| 3960 | .Big => nativeToBig(T, x), | 3631 | .big => nativeToBig(T, x), |
| 3961 | }; | 3632 | }; |
| 3962 | } | 3633 | } |
| 3963 | 3634 | ||
| 3964 | /// Converts an integer which has host endianness to little endian. | 3635 | /// Converts an integer which has host endianness to little endian. |
| 3965 | pub fn nativeToLittle(comptime T: type, x: T) T { | 3636 | pub fn nativeToLittle(comptime T: type, x: T) T { |
| 3966 | return switch (native_endian) { | 3637 | return switch (native_endian) { |
| 3967 | .Little => x, | 3638 | .little => x, |
| 3968 | .Big => @byteSwap(x), | 3639 | .big => @byteSwap(x), |
| 3969 | }; | 3640 | }; |
| 3970 | } | 3641 | } |
| 3971 | 3642 | ||
| 3972 | /// Converts an integer which has host endianness to big endian. | 3643 | /// Converts an integer which has host endianness to big endian. |
| 3973 | pub fn nativeToBig(comptime T: type, x: T) T { | 3644 | pub fn nativeToBig(comptime T: type, x: T) T { |
| 3974 | return switch (native_endian) { | 3645 | return switch (native_endian) { |
| 3975 | .Little => @byteSwap(x), | 3646 | .little => @byteSwap(x), |
| 3976 | .Big => x, | 3647 | .big => x, |
| 3977 | }; | 3648 | }; |
| 3978 | } | 3649 | } |
| 3979 | 3650 | ||
| ... | @@ -4077,8 +3748,8 @@ pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) { | ... | @@ -4077,8 +3748,8 @@ pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) { |
| 4077 | test "asBytes" { | 3748 | test "asBytes" { |
| 4078 | const deadbeef = @as(u32, 0xDEADBEEF); | 3749 | const deadbeef = @as(u32, 0xDEADBEEF); |
| 4079 | const deadbeef_bytes = switch (native_endian) { | 3750 | const deadbeef_bytes = switch (native_endian) { |
| 4080 | .Big => "\xDE\xAD\xBE\xEF", | 3751 | .big => "\xDE\xAD\xBE\xEF", |
| 4081 | .Little => "\xEF\xBE\xAD\xDE", | 3752 | .little => "\xEF\xBE\xAD\xDE", |
| 4082 | }; | 3753 | }; |
| 4083 | 3754 | ||
| 4084 | try testing.expect(eql(u8, asBytes(&deadbeef), deadbeef_bytes)); | 3755 | try testing.expect(eql(u8, asBytes(&deadbeef), deadbeef_bytes)); |
| ... | @@ -4102,10 +3773,10 @@ test "asBytes" { | ... | @@ -4102,10 +3773,10 @@ test "asBytes" { |
| 4102 | .d = 0xA1, | 3773 | .d = 0xA1, |
| 4103 | }; | 3774 | }; |
| 4104 | switch (native_endian) { | 3775 | switch (native_endian) { |
| 4105 | .Little => { | 3776 | .little => { |
| 4106 | try testing.expect(eql(u8, asBytes(&inst), "\xBE\xEF\xDE\xA1")); | 3777 | try testing.expect(eql(u8, asBytes(&inst), "\xBE\xEF\xDE\xA1")); |
| 4107 | }, | 3778 | }, |
| 4108 | .Big => { | 3779 | .big => { |
| 4109 | try testing.expect(eql(u8, asBytes(&inst), "\xA1\xDE\xEF\xBE")); | 3780 | try testing.expect(eql(u8, asBytes(&inst), "\xA1\xDE\xEF\xBE")); |
| 4110 | }, | 3781 | }, |
| 4111 | } | 3782 | } |
| ... | @@ -4137,14 +3808,14 @@ pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 { | ... | @@ -4137,14 +3808,14 @@ pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 { |
| 4137 | test "toBytes" { | 3808 | test "toBytes" { |
| 4138 | var my_bytes = toBytes(@as(u32, 0x12345678)); | 3809 | var my_bytes = toBytes(@as(u32, 0x12345678)); |
| 4139 | switch (native_endian) { | 3810 | switch (native_endian) { |
| 4140 | .Big => try testing.expect(eql(u8, &my_bytes, "\x12\x34\x56\x78")), | 3811 | .big => try testing.expect(eql(u8, &my_bytes, "\x12\x34\x56\x78")), |
| 4141 | .Little => try testing.expect(eql(u8, &my_bytes, "\x78\x56\x34\x12")), | 3812 | .little => try testing.expect(eql(u8, &my_bytes, "\x78\x56\x34\x12")), |
| 4142 | } | 3813 | } |
| 4143 | 3814 | ||
| 4144 | my_bytes[0] = '\x99'; | 3815 | my_bytes[0] = '\x99'; |
| 4145 | switch (native_endian) { | 3816 | switch (native_endian) { |
| 4146 | .Big => try testing.expect(eql(u8, &my_bytes, "\x99\x34\x56\x78")), | 3817 | .big => try testing.expect(eql(u8, &my_bytes, "\x99\x34\x56\x78")), |
| 4147 | .Little => try testing.expect(eql(u8, &my_bytes, "\x99\x56\x34\x12")), | 3818 | .little => try testing.expect(eql(u8, &my_bytes, "\x99\x56\x34\x12")), |
| 4148 | } | 3819 | } |
| 4149 | } | 3820 | } |
| 4150 | 3821 | ||
| ... | @@ -4169,15 +3840,15 @@ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, | ... | @@ -4169,15 +3840,15 @@ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, |
| 4169 | test "bytesAsValue" { | 3840 | test "bytesAsValue" { |
| 4170 | const deadbeef = @as(u32, 0xDEADBEEF); | 3841 | const deadbeef = @as(u32, 0xDEADBEEF); |
| 4171 | const deadbeef_bytes = switch (native_endian) { | 3842 | const deadbeef_bytes = switch (native_endian) { |
| 4172 | .Big => "\xDE\xAD\xBE\xEF", | 3843 | .big => "\xDE\xAD\xBE\xEF", |
| 4173 | .Little => "\xEF\xBE\xAD\xDE", | 3844 | .little => "\xEF\xBE\xAD\xDE", |
| 4174 | }; | 3845 | }; |
| 4175 | 3846 | ||
| 4176 | try testing.expect(deadbeef == bytesAsValue(u32, deadbeef_bytes).*); | 3847 | try testing.expect(deadbeef == bytesAsValue(u32, deadbeef_bytes).*); |
| 4177 | 3848 | ||
| 4178 | var codeface_bytes: [4]u8 = switch (native_endian) { | 3849 | var codeface_bytes: [4]u8 = switch (native_endian) { |
| 4179 | .Big => "\xC0\xDE\xFA\xCE", | 3850 | .big => "\xC0\xDE\xFA\xCE", |
| 4180 | .Little => "\xCE\xFA\xDE\xC0", | 3851 | .little => "\xCE\xFA\xDE\xC0", |
| 4181 | }.*; | 3852 | }.*; |
| 4182 | var codeface = bytesAsValue(u32, &codeface_bytes); | 3853 | var codeface = bytesAsValue(u32, &codeface_bytes); |
| 4183 | try testing.expect(codeface.* == 0xC0DEFACE); | 3854 | try testing.expect(codeface.* == 0xC0DEFACE); |
| ... | @@ -4199,8 +3870,8 @@ test "bytesAsValue" { | ... | @@ -4199,8 +3870,8 @@ test "bytesAsValue" { |
| 4199 | .d = 0xA1, | 3870 | .d = 0xA1, |
| 4200 | }; | 3871 | }; |
| 4201 | const inst_bytes = switch (native_endian) { | 3872 | const inst_bytes = switch (native_endian) { |
| 4202 | .Little => "\xBE\xEF\xDE\xA1", | 3873 | .little => "\xBE\xEF\xDE\xA1", |
| 4203 | .Big => "\xA1\xDE\xEF\xBE", | 3874 | .big => "\xA1\xDE\xEF\xBE", |
| 4204 | }; | 3875 | }; |
| 4205 | const inst2 = bytesAsValue(S, inst_bytes); | 3876 | const inst2 = bytesAsValue(S, inst_bytes); |
| 4206 | try testing.expect(meta.eql(inst, inst2.*)); | 3877 | try testing.expect(meta.eql(inst, inst2.*)); |
| ... | @@ -4227,8 +3898,8 @@ pub fn bytesToValue(comptime T: type, bytes: anytype) T { | ... | @@ -4227,8 +3898,8 @@ pub fn bytesToValue(comptime T: type, bytes: anytype) T { |
| 4227 | } | 3898 | } |
| 4228 | test "bytesToValue" { | 3899 | test "bytesToValue" { |
| 4229 | const deadbeef_bytes = switch (native_endian) { | 3900 | const deadbeef_bytes = switch (native_endian) { |
| 4230 | .Big => "\xDE\xAD\xBE\xEF", | 3901 | .big => "\xDE\xAD\xBE\xEF", |
| 4231 | .Little => "\xEF\xBE\xAD\xDE", | 3902 | .little => "\xEF\xBE\xAD\xDE", |
| 4232 | }; | 3903 | }; |
| 4233 | 3904 | ||
| 4234 | const deadbeef = bytesToValue(u32, deadbeef_bytes); | 3905 | const deadbeef = bytesToValue(u32, deadbeef_bytes); |
| ... | @@ -4357,8 +4028,8 @@ test "sliceAsBytes" { | ... | @@ -4357,8 +4028,8 @@ test "sliceAsBytes" { |
| 4357 | const slice = sliceAsBytes(bytes[0..]); | 4028 | const slice = sliceAsBytes(bytes[0..]); |
| 4358 | try testing.expect(slice.len == 4); | 4029 | try testing.expect(slice.len == 4); |
| 4359 | try testing.expect(eql(u8, slice, switch (native_endian) { | 4030 | try testing.expect(eql(u8, slice, switch (native_endian) { |
| 4360 | .Big => "\xDE\xAD\xBE\xEF", | 4031 | .big => "\xDE\xAD\xBE\xEF", |
| 4361 | .Little => "\xAD\xDE\xEF\xBE", | 4032 | .little => "\xAD\xDE\xEF\xBE", |
| 4362 | })); | 4033 | })); |
| 4363 | } | 4034 | } |
| 4364 | 4035 | ||
| ... | @@ -4533,7 +4204,7 @@ test "doNotOptimizeAway" { | ... | @@ -4533,7 +4204,7 @@ test "doNotOptimizeAway" { |
| 4533 | doNotOptimizeAway(@as(f64, 0.0)); | 4204 | doNotOptimizeAway(@as(f64, 0.0)); |
| 4534 | doNotOptimizeAway([_]u8{0} ** 4); | 4205 | doNotOptimizeAway([_]u8{0} ** 4); |
| 4535 | doNotOptimizeAway([_]u8{0} ** 100); | 4206 | doNotOptimizeAway([_]u8{0} ** 100); |
| 4536 | doNotOptimizeAway(@as(std.builtin.Endian, .Little)); | 4207 | doNotOptimizeAway(@as(std.builtin.Endian, .little)); |
| 4537 | } | 4208 | } |
| 4538 | 4209 | ||
| 4539 | test "alignForward" { | 4210 | test "alignForward" { |
| ... | @@ -4671,7 +4342,6 @@ pub fn alignInSlice(slice: anytype, comptime new_alignment: usize) ?AlignedSlice | ... | @@ -4671,7 +4342,6 @@ pub fn alignInSlice(slice: anytype, comptime new_alignment: usize) ?AlignedSlice |
| 4671 | } | 4342 | } |
| 4672 | 4343 | ||
| 4673 | test "read/write(Var)PackedInt" { | 4344 | test "read/write(Var)PackedInt" { |
| 4674 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; | ||
| 4675 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; | 4345 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; |
| 4676 | 4346 | ||
| 4677 | switch (builtin.cpu.arch) { | 4347 | switch (builtin.cpu.arch) { |
| ... | @@ -4686,7 +4356,7 @@ test "read/write(Var)PackedInt" { | ... | @@ -4686,7 +4356,7 @@ test "read/write(Var)PackedInt" { |
| 4686 | return error.SkipZigTest; | 4356 | return error.SkipZigTest; |
| 4687 | } | 4357 | } |
| 4688 | 4358 | ||
| 4689 | const foreign_endian: Endian = if (native_endian == .Big) .Little else .Big; | 4359 | const foreign_endian: Endian = if (native_endian == .big) .little else .big; |
| 4690 | const expect = std.testing.expect; | 4360 | const expect = std.testing.expect; |
| 4691 | var prng = std.rand.DefaultPrng.init(1234); | 4361 | var prng = std.rand.DefaultPrng.init(1234); |
| 4692 | const random = prng.random(); | 4362 | const random = prng.random(); |
lib/std/net.zig+5-5| ... | @@ -602,8 +602,8 @@ pub const Ip6Address = extern struct { | ... | @@ -602,8 +602,8 @@ pub const Ip6Address = extern struct { |
| 602 | } | 602 | } |
| 603 | const big_endian_parts = @as(*align(1) const [8]u16, @ptrCast(&self.sa.addr)); | 603 | const big_endian_parts = @as(*align(1) const [8]u16, @ptrCast(&self.sa.addr)); |
| 604 | const native_endian_parts = switch (native_endian) { | 604 | const native_endian_parts = switch (native_endian) { |
| 605 | .Big => big_endian_parts.*, | 605 | .big => big_endian_parts.*, |
| 606 | .Little => blk: { | 606 | .little => blk: { |
| 607 | var buf: [8]u16 = undefined; | 607 | var buf: [8]u16 = undefined; |
| 608 | for (big_endian_parts, 0..) |part, i| { | 608 | for (big_endian_parts, 0..) |part, i| { |
| 609 | buf[i] = mem.bigToNative(u16, part); | 609 | buf[i] = mem.bigToNative(u16, part); |
| ... | @@ -1052,7 +1052,7 @@ fn linuxLookupName( | ... | @@ -1052,7 +1052,7 @@ fn linuxLookupName( |
| 1052 | } else { | 1052 | } else { |
| 1053 | sa6.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; | 1053 | sa6.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; |
| 1054 | da6.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; | 1054 | da6.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; |
| 1055 | mem.writeIntNative(u32, da6.addr[12..], addr.addr.in.sa.addr); | 1055 | mem.writeInt(u32, da6.addr[12..], addr.addr.in.sa.addr, native_endian); |
| 1056 | da4.addr = addr.addr.in.sa.addr; | 1056 | da4.addr = addr.addr.in.sa.addr; |
| 1057 | da = @ptrCast(&da4); | 1057 | da = @ptrCast(&da4); |
| 1058 | dalen = @sizeOf(os.sockaddr.in); | 1058 | dalen = @sizeOf(os.sockaddr.in); |
| ... | @@ -1073,7 +1073,7 @@ fn linuxLookupName( | ... | @@ -1073,7 +1073,7 @@ fn linuxLookupName( |
| 1073 | os.getsockname(fd, sa, &salen) catch break :syscalls; | 1073 | os.getsockname(fd, sa, &salen) catch break :syscalls; |
| 1074 | if (addr.addr.any.family == os.AF.INET) { | 1074 | if (addr.addr.any.family == os.AF.INET) { |
| 1075 | // TODO sa6.addr[12..16] should return *[4]u8, making this cast unnecessary. | 1075 | // TODO sa6.addr[12..16] should return *[4]u8, making this cast unnecessary. |
| 1076 | mem.writeIntNative(u32, @as(*[4]u8, @ptrCast(&sa6.addr[12])), sa4.addr); | 1076 | mem.writeInt(u32, @as(*[4]u8, @ptrCast(&sa6.addr[12])), sa4.addr, native_endian); |
| 1077 | } | 1077 | } |
| 1078 | if (dscope == @as(i32, scopeOf(sa6.addr))) key |= DAS_MATCHINGSCOPE; | 1078 | if (dscope == @as(i32, scopeOf(sa6.addr))) key |= DAS_MATCHINGSCOPE; |
| 1079 | if (dlabel == labelOf(sa6.addr)) key |= DAS_MATCHINGLABEL; | 1079 | if (dlabel == labelOf(sa6.addr)) key |= DAS_MATCHINGLABEL; |
| ... | @@ -1569,7 +1569,7 @@ fn resMSendRc( | ... | @@ -1569,7 +1569,7 @@ fn resMSendRc( |
| 1569 | ); | 1569 | ); |
| 1570 | for (0..ns.len) |i| { | 1570 | for (0..ns.len) |i| { |
| 1571 | if (ns[i].any.family != os.AF.INET) continue; | 1571 | if (ns[i].any.family != os.AF.INET) continue; |
| 1572 | mem.writeIntNative(u32, ns[i].in6.sa.addr[12..], ns[i].in.sa.addr); | 1572 | mem.writeInt(u32, ns[i].in6.sa.addr[12..], ns[i].in.sa.addr, native_endian); |
| 1573 | ns[i].in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; | 1573 | ns[i].in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; |
| 1574 | ns[i].any.family = os.AF.INET6; | 1574 | ns[i].any.family = os.AF.INET6; |
| 1575 | ns[i].in6.sa.flowinfo = 0; | 1575 | ns[i].in6.sa.flowinfo = 0; |
lib/std/os/linux.zig+3-3| ... | @@ -206,11 +206,11 @@ fn splitValueBE64(val: i64) [2]u32 { | ... | @@ -206,11 +206,11 @@ fn splitValueBE64(val: i64) [2]u32 { |
| 206 | fn splitValue64(val: i64) [2]u32 { | 206 | fn splitValue64(val: i64) [2]u32 { |
| 207 | const u: u64 = @bitCast(val); | 207 | const u: u64 = @bitCast(val); |
| 208 | switch (native_endian) { | 208 | switch (native_endian) { |
| 209 | .Little => return [2]u32{ | 209 | .little => return [2]u32{ |
| 210 | @as(u32, @truncate(u)), | 210 | @as(u32, @truncate(u)), |
| 211 | @as(u32, @truncate(u >> 32)), | 211 | @as(u32, @truncate(u >> 32)), |
| 212 | }, | 212 | }, |
| 213 | .Big => return [2]u32{ | 213 | .big => return [2]u32{ |
| 214 | @as(u32, @truncate(u >> 32)), | 214 | @as(u32, @truncate(u >> 32)), |
| 215 | @as(u32, @truncate(u)), | 215 | @as(u32, @truncate(u)), |
| 216 | }, | 216 | }, |
| ... | @@ -6028,7 +6028,7 @@ pub const AUDIT = struct { | ... | @@ -6028,7 +6028,7 @@ pub const AUDIT = struct { |
| 6028 | 6028 | ||
| 6029 | fn toAudit(arch: std.Target.Cpu.Arch) u32 { | 6029 | fn toAudit(arch: std.Target.Cpu.Arch) u32 { |
| 6030 | var res: u32 = @intFromEnum(arch.toElfMachine()); | 6030 | var res: u32 = @intFromEnum(arch.toElfMachine()); |
| 6031 | if (arch.endian() == .Little) res |= LE; | 6031 | if (arch.endian() == .little) res |= LE; |
| 6032 | switch (arch) { | 6032 | switch (arch) { |
| 6033 | .aarch64, | 6033 | .aarch64, |
| 6034 | .mips64, | 6034 | .mips64, |
lib/std/os/linux/bpf.zig+4-4| ... | @@ -696,8 +696,8 @@ pub const Insn = packed struct { | ... | @@ -696,8 +696,8 @@ pub const Insn = packed struct { |
| 696 | fn endian_swap(endian: std.builtin.Endian, comptime size: Size, dst: Reg) Insn { | 696 | fn endian_swap(endian: std.builtin.Endian, comptime size: Size, dst: Reg) Insn { |
| 697 | return Insn{ | 697 | return Insn{ |
| 698 | .code = switch (endian) { | 698 | .code = switch (endian) { |
| 699 | .Big => 0xdc, | 699 | .big => 0xdc, |
| 700 | .Little => 0xd4, | 700 | .little => 0xd4, |
| 701 | }, | 701 | }, |
| 702 | .dst = @intFromEnum(dst), | 702 | .dst = @intFromEnum(dst), |
| 703 | .src = 0, | 703 | .src = 0, |
| ... | @@ -712,11 +712,11 @@ pub const Insn = packed struct { | ... | @@ -712,11 +712,11 @@ pub const Insn = packed struct { |
| 712 | } | 712 | } |
| 713 | 713 | ||
| 714 | pub fn le(comptime size: Size, dst: Reg) Insn { | 714 | pub fn le(comptime size: Size, dst: Reg) Insn { |
| 715 | return endian_swap(.Little, size, dst); | 715 | return endian_swap(.little, size, dst); |
| 716 | } | 716 | } |
| 717 | 717 | ||
| 718 | pub fn be(comptime size: Size, dst: Reg) Insn { | 718 | pub fn be(comptime size: Size, dst: Reg) Insn { |
| 719 | return endian_swap(.Big, size, dst); | 719 | return endian_swap(.big, size, dst); |
| 720 | } | 720 | } |
| 721 | 721 | ||
| 722 | pub fn call(helper: Helper) Insn { | 722 | pub fn call(helper: Helper) Insn { |
lib/std/os/linux/seccomp.zig+1-1| ... | @@ -54,7 +54,7 @@ | ... | @@ -54,7 +54,7 @@ |
| 54 | //! manner to the OpenSSH example: | 54 | //! manner to the OpenSSH example: |
| 55 | //! | 55 | //! |
| 56 | //! ```zig | 56 | //! ```zig |
| 57 | //! const offset = if (native_endian == .Little) struct { | 57 | //! const offset = if (native_endian == .little) struct { |
| 58 | //! pub const low = 0; | 58 | //! pub const low = 0; |
| 59 | //! pub const high = @sizeOf(u32); | 59 | //! pub const high = @sizeOf(u32); |
| 60 | //! } else struct { | 60 | //! } else struct { |
lib/std/os/test.zig+3-3| ... | @@ -609,7 +609,7 @@ test "mmap" { | ... | @@ -609,7 +609,7 @@ test "mmap" { |
| 609 | 609 | ||
| 610 | var i: u32 = 0; | 610 | var i: u32 = 0; |
| 611 | while (i < alloc_size / @sizeOf(u32)) : (i += 1) { | 611 | while (i < alloc_size / @sizeOf(u32)) : (i += 1) { |
| 612 | try stream.writeIntNative(u32, i); | 612 | try stream.writeInt(u32, i, .little); |
| 613 | } | 613 | } |
| 614 | } | 614 | } |
| 615 | 615 | ||
| ... | @@ -633,7 +633,7 @@ test "mmap" { | ... | @@ -633,7 +633,7 @@ test "mmap" { |
| 633 | 633 | ||
| 634 | var i: u32 = 0; | 634 | var i: u32 = 0; |
| 635 | while (i < alloc_size / @sizeOf(u32)) : (i += 1) { | 635 | while (i < alloc_size / @sizeOf(u32)) : (i += 1) { |
| 636 | try testing.expectEqual(i, try stream.readIntNative(u32)); | 636 | try testing.expectEqual(i, try stream.readInt(u32, .little)); |
| 637 | } | 637 | } |
| 638 | } | 638 | } |
| 639 | 639 | ||
| ... | @@ -657,7 +657,7 @@ test "mmap" { | ... | @@ -657,7 +657,7 @@ test "mmap" { |
| 657 | 657 | ||
| 658 | var i: u32 = alloc_size / 2 / @sizeOf(u32); | 658 | var i: u32 = alloc_size / 2 / @sizeOf(u32); |
| 659 | while (i < alloc_size / @sizeOf(u32)) : (i += 1) { | 659 | while (i < alloc_size / @sizeOf(u32)) : (i += 1) { |
| 660 | try testing.expectEqual(i, try stream.readIntNative(u32)); | 660 | try testing.expectEqual(i, try stream.readInt(u32, .little)); |
| 661 | } | 661 | } |
| 662 | } | 662 | } |
| 663 | 663 |
lib/std/os/windows.zig+2-2| ... | @@ -3360,13 +3360,13 @@ pub const GUID = extern struct { | ... | @@ -3360,13 +3360,13 @@ pub const GUID = extern struct { |
| 3360 | Data4: [8]u8, | 3360 | Data4: [8]u8, |
| 3361 | 3361 | ||
| 3362 | const hex_offsets = switch (builtin.target.cpu.arch.endian()) { | 3362 | const hex_offsets = switch (builtin.target.cpu.arch.endian()) { |
| 3363 | .Big => [16]u6{ | 3363 | .big => [16]u6{ |
| 3364 | 0, 2, 4, 6, | 3364 | 0, 2, 4, 6, |
| 3365 | 9, 11, 14, 16, | 3365 | 9, 11, 14, 16, |
| 3366 | 19, 21, 24, 26, | 3366 | 19, 21, 24, 26, |
| 3367 | 28, 30, 32, 34, | 3367 | 28, 30, 32, 34, |
| 3368 | }, | 3368 | }, |
| 3369 | .Little => [16]u6{ | 3369 | .little => [16]u6{ |
| 3370 | 6, 4, 2, 0, | 3370 | 6, 4, 2, 0, |
| 3371 | 11, 9, 16, 14, | 3371 | 11, 9, 16, 14, |
| 3372 | 19, 21, 24, 26, | 3372 | 19, 21, 24, 26, |
lib/std/packed_int_array.zig+20-20| ... | @@ -79,12 +79,12 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { | ... | @@ -79,12 +79,12 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { |
| 79 | if (endian != native_endian) value = @byteSwap(value); | 79 | if (endian != native_endian) value = @byteSwap(value); |
| 80 | 80 | ||
| 81 | switch (endian) { | 81 | switch (endian) { |
| 82 | .Big => { | 82 | .big => { |
| 83 | value <<= @as(Shift, @intCast(head_keep_bits)); | 83 | value <<= @as(Shift, @intCast(head_keep_bits)); |
| 84 | value >>= @as(Shift, @intCast(head_keep_bits)); | 84 | value >>= @as(Shift, @intCast(head_keep_bits)); |
| 85 | value >>= @as(Shift, @intCast(tail_keep_bits)); | 85 | value >>= @as(Shift, @intCast(tail_keep_bits)); |
| 86 | }, | 86 | }, |
| 87 | .Little => { | 87 | .little => { |
| 88 | value <<= @as(Shift, @intCast(tail_keep_bits)); | 88 | value <<= @as(Shift, @intCast(tail_keep_bits)); |
| 89 | value >>= @as(Shift, @intCast(tail_keep_bits)); | 89 | value >>= @as(Shift, @intCast(tail_keep_bits)); |
| 90 | value >>= @as(Shift, @intCast(head_keep_bits)); | 90 | value >>= @as(Shift, @intCast(head_keep_bits)); |
| ... | @@ -115,8 +115,8 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { | ... | @@ -115,8 +115,8 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { |
| 115 | const head_keep_bits = bit_index - (start_byte * 8); | 115 | const head_keep_bits = bit_index - (start_byte * 8); |
| 116 | const tail_keep_bits = container_bits - (int_bits + head_keep_bits); | 116 | const tail_keep_bits = container_bits - (int_bits + head_keep_bits); |
| 117 | const keep_shift = switch (endian) { | 117 | const keep_shift = switch (endian) { |
| 118 | .Big => @as(Shift, @intCast(tail_keep_bits)), | 118 | .big => @as(Shift, @intCast(tail_keep_bits)), |
| 119 | .Little => @as(Shift, @intCast(head_keep_bits)), | 119 | .little => @as(Shift, @intCast(head_keep_bits)), |
| 120 | }; | 120 | }; |
| 121 | 121 | ||
| 122 | //position the bits where they need to be in the container | 122 | //position the bits where they need to be in the container |
| ... | @@ -388,10 +388,10 @@ test "PackedIntArray" { | ... | @@ -388,10 +388,10 @@ test "PackedIntArray" { |
| 388 | 388 | ||
| 389 | test "PackedIntIo" { | 389 | test "PackedIntIo" { |
| 390 | const bytes = [_]u8{ 0b01101_000, 0b01011_110, 0b00011_101 }; | 390 | const bytes = [_]u8{ 0b01101_000, 0b01011_110, 0b00011_101 }; |
| 391 | try testing.expectEqual(@as(u15, 0x2bcd), PackedIntIo(u15, .Little).get(&bytes, 0, 3)); | 391 | try testing.expectEqual(@as(u15, 0x2bcd), PackedIntIo(u15, .little).get(&bytes, 0, 3)); |
| 392 | try testing.expectEqual(@as(u16, 0xabcd), PackedIntIo(u16, .Little).get(&bytes, 0, 3)); | 392 | try testing.expectEqual(@as(u16, 0xabcd), PackedIntIo(u16, .little).get(&bytes, 0, 3)); |
| 393 | try testing.expectEqual(@as(u17, 0x1abcd), PackedIntIo(u17, .Little).get(&bytes, 0, 3)); | 393 | try testing.expectEqual(@as(u17, 0x1abcd), PackedIntIo(u17, .little).get(&bytes, 0, 3)); |
| 394 | try testing.expectEqual(@as(u18, 0x3abcd), PackedIntIo(u18, .Little).get(&bytes, 0, 3)); | 394 | try testing.expectEqual(@as(u18, 0x3abcd), PackedIntIo(u18, .little).get(&bytes, 0, 3)); |
| 395 | } | 395 | } |
| 396 | 396 | ||
| 397 | test "PackedIntArray init" { | 397 | test "PackedIntArray init" { |
| ... | @@ -555,16 +555,16 @@ test "PackedInt(Array/Slice) sliceCast" { | ... | @@ -555,16 +555,16 @@ test "PackedInt(Array/Slice) sliceCast" { |
| 555 | var i = @as(usize, 0); | 555 | var i = @as(usize, 0); |
| 556 | while (i < packed_slice_cast_2.len) : (i += 1) { | 556 | while (i < packed_slice_cast_2.len) : (i += 1) { |
| 557 | const val = switch (native_endian) { | 557 | const val = switch (native_endian) { |
| 558 | .Big => 0b01, | 558 | .big => 0b01, |
| 559 | .Little => 0b10, | 559 | .little => 0b10, |
| 560 | }; | 560 | }; |
| 561 | try testing.expect(packed_slice_cast_2.get(i) == val); | 561 | try testing.expect(packed_slice_cast_2.get(i) == val); |
| 562 | } | 562 | } |
| 563 | i = 0; | 563 | i = 0; |
| 564 | while (i < packed_slice_cast_4.len) : (i += 1) { | 564 | while (i < packed_slice_cast_4.len) : (i += 1) { |
| 565 | const val = switch (native_endian) { | 565 | const val = switch (native_endian) { |
| 566 | .Big => 0b0101, | 566 | .big => 0b0101, |
| 567 | .Little => 0b1010, | 567 | .little => 0b1010, |
| 568 | }; | 568 | }; |
| 569 | try testing.expect(packed_slice_cast_4.get(i) == val); | 569 | try testing.expect(packed_slice_cast_4.get(i) == val); |
| 570 | } | 570 | } |
| ... | @@ -577,8 +577,8 @@ test "PackedInt(Array/Slice) sliceCast" { | ... | @@ -577,8 +577,8 @@ test "PackedInt(Array/Slice) sliceCast" { |
| 577 | i = 0; | 577 | i = 0; |
| 578 | while (i < packed_slice_cast_3.len) : (i += 1) { | 578 | while (i < packed_slice_cast_3.len) : (i += 1) { |
| 579 | const val = switch (native_endian) { | 579 | const val = switch (native_endian) { |
| 580 | .Big => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000), | 580 | .big => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000), |
| 581 | .Little => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000), | 581 | .little => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000), |
| 582 | }; | 582 | }; |
| 583 | try testing.expect(packed_slice_cast_3.get(i) == val); | 583 | try testing.expect(packed_slice_cast_3.get(i) == val); |
| 584 | } | 584 | } |
| ... | @@ -586,7 +586,7 @@ test "PackedInt(Array/Slice) sliceCast" { | ... | @@ -586,7 +586,7 @@ test "PackedInt(Array/Slice) sliceCast" { |
| 586 | 586 | ||
| 587 | test "PackedInt(Array/Slice)Endian" { | 587 | test "PackedInt(Array/Slice)Endian" { |
| 588 | { | 588 | { |
| 589 | const PackedArrayBe = PackedIntArrayEndian(u4, .Big, 8); | 589 | const PackedArrayBe = PackedIntArrayEndian(u4, .big, 8); |
| 590 | var packed_array_be = PackedArrayBe.init([_]u4{ 0, 1, 2, 3, 4, 5, 6, 7 }); | 590 | var packed_array_be = PackedArrayBe.init([_]u4{ 0, 1, 2, 3, 4, 5, 6, 7 }); |
| 591 | try testing.expect(packed_array_be.bytes[0] == 0b00000001); | 591 | try testing.expect(packed_array_be.bytes[0] == 0b00000001); |
| 592 | try testing.expect(packed_array_be.bytes[1] == 0b00100011); | 592 | try testing.expect(packed_array_be.bytes[1] == 0b00100011); |
| ... | @@ -596,14 +596,14 @@ test "PackedInt(Array/Slice)Endian" { | ... | @@ -596,14 +596,14 @@ test "PackedInt(Array/Slice)Endian" { |
| 596 | try testing.expect(packed_array_be.get(i) == i); | 596 | try testing.expect(packed_array_be.get(i) == i); |
| 597 | } | 597 | } |
| 598 | 598 | ||
| 599 | var packed_slice_le = packed_array_be.sliceCastEndian(u4, .Little); | 599 | var packed_slice_le = packed_array_be.sliceCastEndian(u4, .little); |
| 600 | i = 0; | 600 | i = 0; |
| 601 | while (i < packed_slice_le.len) : (i += 1) { | 601 | while (i < packed_slice_le.len) : (i += 1) { |
| 602 | const val = if (i % 2 == 0) i + 1 else i - 1; | 602 | const val = if (i % 2 == 0) i + 1 else i - 1; |
| 603 | try testing.expect(packed_slice_le.get(i) == val); | 603 | try testing.expect(packed_slice_le.get(i) == val); |
| 604 | } | 604 | } |
| 605 | 605 | ||
| 606 | var packed_slice_le_shift = packed_array_be.slice(1, 5).sliceCastEndian(u4, .Little); | 606 | var packed_slice_le_shift = packed_array_be.slice(1, 5).sliceCastEndian(u4, .little); |
| 607 | i = 0; | 607 | i = 0; |
| 608 | while (i < packed_slice_le_shift.len) : (i += 1) { | 608 | while (i < packed_slice_le_shift.len) : (i += 1) { |
| 609 | const val = if (i % 2 == 0) i else i + 2; | 609 | const val = if (i % 2 == 0) i else i + 2; |
| ... | @@ -612,7 +612,7 @@ test "PackedInt(Array/Slice)Endian" { | ... | @@ -612,7 +612,7 @@ test "PackedInt(Array/Slice)Endian" { |
| 612 | } | 612 | } |
| 613 | 613 | ||
| 614 | { | 614 | { |
| 615 | const PackedArrayBe = PackedIntArrayEndian(u11, .Big, 8); | 615 | const PackedArrayBe = PackedIntArrayEndian(u11, .big, 8); |
| 616 | var packed_array_be = PackedArrayBe.init([_]u11{ 0, 1, 2, 3, 4, 5, 6, 7 }); | 616 | var packed_array_be = PackedArrayBe.init([_]u11{ 0, 1, 2, 3, 4, 5, 6, 7 }); |
| 617 | try testing.expect(packed_array_be.bytes[0] == 0b00000000); | 617 | try testing.expect(packed_array_be.bytes[0] == 0b00000000); |
| 618 | try testing.expect(packed_array_be.bytes[1] == 0b00000000); | 618 | try testing.expect(packed_array_be.bytes[1] == 0b00000000); |
| ... | @@ -625,7 +625,7 @@ test "PackedInt(Array/Slice)Endian" { | ... | @@ -625,7 +625,7 @@ test "PackedInt(Array/Slice)Endian" { |
| 625 | try testing.expect(packed_array_be.get(i) == i); | 625 | try testing.expect(packed_array_be.get(i) == i); |
| 626 | } | 626 | } |
| 627 | 627 | ||
| 628 | var packed_slice_le = packed_array_be.sliceCastEndian(u11, .Little); | 628 | var packed_slice_le = packed_array_be.sliceCastEndian(u11, .little); |
| 629 | try testing.expect(packed_slice_le.get(0) == 0b00000000000); | 629 | try testing.expect(packed_slice_le.get(0) == 0b00000000000); |
| 630 | try testing.expect(packed_slice_le.get(1) == 0b00010000000); | 630 | try testing.expect(packed_slice_le.get(1) == 0b00010000000); |
| 631 | try testing.expect(packed_slice_le.get(2) == 0b00000000100); | 631 | try testing.expect(packed_slice_le.get(2) == 0b00000000100); |
| ... | @@ -635,7 +635,7 @@ test "PackedInt(Array/Slice)Endian" { | ... | @@ -635,7 +635,7 @@ test "PackedInt(Array/Slice)Endian" { |
| 635 | try testing.expect(packed_slice_le.get(6) == 0b10000010000); | 635 | try testing.expect(packed_slice_le.get(6) == 0b10000010000); |
| 636 | try testing.expect(packed_slice_le.get(7) == 0b00000111001); | 636 | try testing.expect(packed_slice_le.get(7) == 0b00000111001); |
| 637 | 637 | ||
| 638 | var packed_slice_le_shift = packed_array_be.slice(1, 5).sliceCastEndian(u11, .Little); | 638 | var packed_slice_le_shift = packed_array_be.slice(1, 5).sliceCastEndian(u11, .little); |
| 639 | try testing.expect(packed_slice_le_shift.get(0) == 0b00010000000); | 639 | try testing.expect(packed_slice_le_shift.get(0) == 0b00010000000); |
| 640 | try testing.expect(packed_slice_le_shift.get(1) == 0b00000000100); | 640 | try testing.expect(packed_slice_le_shift.get(1) == 0b00000000100); |
| 641 | try testing.expect(packed_slice_le_shift.get(2) == 0b00000000000); | 641 | try testing.expect(packed_slice_le_shift.get(2) == 0b00000000000); |
lib/std/pdb.zig+14-14| ... | @@ -464,12 +464,12 @@ pub const PDBStringTableHeader = extern struct { | ... | @@ -464,12 +464,12 @@ pub const PDBStringTableHeader = extern struct { |
| 464 | }; | 464 | }; |
| 465 | 465 | ||
| 466 | fn readSparseBitVector(stream: anytype, allocator: mem.Allocator) ![]u32 { | 466 | fn readSparseBitVector(stream: anytype, allocator: mem.Allocator) ![]u32 { |
| 467 | const num_words = try stream.readIntLittle(u32); | 467 | const num_words = try stream.readInt(u32, .little); |
| 468 | var list = ArrayList(u32).init(allocator); | 468 | var list = ArrayList(u32).init(allocator); |
| 469 | errdefer list.deinit(); | 469 | errdefer list.deinit(); |
| 470 | var word_i: u32 = 0; | 470 | var word_i: u32 = 0; |
| 471 | while (word_i != num_words) : (word_i += 1) { | 471 | while (word_i != num_words) : (word_i += 1) { |
| 472 | const word = try stream.readIntLittle(u32); | 472 | const word = try stream.readInt(u32, .little); |
| 473 | var bit_i: u5 = 0; | 473 | var bit_i: u5 = 0; |
| 474 | while (true) : (bit_i += 1) { | 474 | while (true) : (bit_i += 1) { |
| 475 | if (word & (@as(u32, 1) << bit_i) != 0) { | 475 | if (word & (@as(u32, 1) << bit_i) != 0) { |
| ... | @@ -599,7 +599,7 @@ pub const Pdb = struct { | ... | @@ -599,7 +599,7 @@ pub const Pdb = struct { |
| 599 | 599 | ||
| 600 | var sect_cont_offset: usize = 0; | 600 | var sect_cont_offset: usize = 0; |
| 601 | if (section_contrib_size != 0) { | 601 | if (section_contrib_size != 0) { |
| 602 | const version = reader.readEnum(SectionContrSubstreamVersion, .Little) catch |err| switch (err) { | 602 | const version = reader.readEnum(SectionContrSubstreamVersion, .little) catch |err| switch (err) { |
| 603 | error.InvalidValue => return error.InvalidDebugInfo, | 603 | error.InvalidValue => return error.InvalidDebugInfo, |
| 604 | else => |e| return e, | 604 | else => |e| return e, |
| 605 | }; | 605 | }; |
| ... | @@ -625,10 +625,10 @@ pub const Pdb = struct { | ... | @@ -625,10 +625,10 @@ pub const Pdb = struct { |
| 625 | const reader = stream.reader(); | 625 | const reader = stream.reader(); |
| 626 | 626 | ||
| 627 | // Parse the InfoStreamHeader. | 627 | // Parse the InfoStreamHeader. |
| 628 | const version = try reader.readIntLittle(u32); | 628 | const version = try reader.readInt(u32, .little); |
| 629 | const signature = try reader.readIntLittle(u32); | 629 | const signature = try reader.readInt(u32, .little); |
| 630 | _ = signature; | 630 | _ = signature; |
| 631 | const age = try reader.readIntLittle(u32); | 631 | const age = try reader.readInt(u32, .little); |
| 632 | const guid = try reader.readBytesNoEof(16); | 632 | const guid = try reader.readBytesNoEof(16); |
| 633 | 633 | ||
| 634 | if (version != 20000404) // VC70, only value observed by LLVM team | 634 | if (version != 20000404) // VC70, only value observed by LLVM team |
| ... | @@ -639,7 +639,7 @@ pub const Pdb = struct { | ... | @@ -639,7 +639,7 @@ pub const Pdb = struct { |
| 639 | 639 | ||
| 640 | // Find the string table. | 640 | // Find the string table. |
| 641 | const string_table_index = str_tab_index: { | 641 | const string_table_index = str_tab_index: { |
| 642 | const name_bytes_len = try reader.readIntLittle(u32); | 642 | const name_bytes_len = try reader.readInt(u32, .little); |
| 643 | const name_bytes = try self.allocator.alloc(u8, name_bytes_len); | 643 | const name_bytes = try self.allocator.alloc(u8, name_bytes_len); |
| 644 | defer self.allocator.free(name_bytes); | 644 | defer self.allocator.free(name_bytes); |
| 645 | try reader.readNoEof(name_bytes); | 645 | try reader.readNoEof(name_bytes); |
| ... | @@ -667,8 +667,8 @@ pub const Pdb = struct { | ... | @@ -667,8 +667,8 @@ pub const Pdb = struct { |
| 667 | defer self.allocator.free(deleted); | 667 | defer self.allocator.free(deleted); |
| 668 | 668 | ||
| 669 | for (present) |_| { | 669 | for (present) |_| { |
| 670 | const name_offset = try reader.readIntLittle(u32); | 670 | const name_offset = try reader.readInt(u32, .little); |
| 671 | const name_index = try reader.readIntLittle(u32); | 671 | const name_index = try reader.readInt(u32, .little); |
| 672 | if (name_offset > name_bytes.len) | 672 | if (name_offset > name_bytes.len) |
| 673 | return error.InvalidDebugInfo; | 673 | return error.InvalidDebugInfo; |
| 674 | const name = mem.sliceTo(name_bytes[name_offset..], 0); | 674 | const name = mem.sliceTo(name_bytes[name_offset..], 0); |
| ... | @@ -821,7 +821,7 @@ pub const Pdb = struct { | ... | @@ -821,7 +821,7 @@ pub const Pdb = struct { |
| 821 | return error.MissingDebugInfo; | 821 | return error.MissingDebugInfo; |
| 822 | const reader = stream.reader(); | 822 | const reader = stream.reader(); |
| 823 | 823 | ||
| 824 | const signature = try reader.readIntLittle(u32); | 824 | const signature = try reader.readInt(u32, .little); |
| 825 | if (signature != 4) | 825 | if (signature != 4) |
| 826 | return error.InvalidDebugInfo; | 826 | return error.InvalidDebugInfo; |
| 827 | 827 | ||
| ... | @@ -899,7 +899,7 @@ const Msf = struct { | ... | @@ -899,7 +899,7 @@ const Msf = struct { |
| 899 | try file.seekTo(superblock.BlockSize * superblock.BlockMapAddr); | 899 | try file.seekTo(superblock.BlockSize * superblock.BlockMapAddr); |
| 900 | var dir_blocks = try allocator.alloc(u32, dir_block_count); | 900 | var dir_blocks = try allocator.alloc(u32, dir_block_count); |
| 901 | for (dir_blocks) |*b| { | 901 | for (dir_blocks) |*b| { |
| 902 | b.* = try in.readIntLittle(u32); | 902 | b.* = try in.readInt(u32, .little); |
| 903 | } | 903 | } |
| 904 | var directory = MsfStream.init( | 904 | var directory = MsfStream.init( |
| 905 | superblock.BlockSize, | 905 | superblock.BlockSize, |
| ... | @@ -908,7 +908,7 @@ const Msf = struct { | ... | @@ -908,7 +908,7 @@ const Msf = struct { |
| 908 | ); | 908 | ); |
| 909 | 909 | ||
| 910 | const begin = directory.pos; | 910 | const begin = directory.pos; |
| 911 | const stream_count = try directory.reader().readIntLittle(u32); | 911 | const stream_count = try directory.reader().readInt(u32, .little); |
| 912 | const stream_sizes = try allocator.alloc(u32, stream_count); | 912 | const stream_sizes = try allocator.alloc(u32, stream_count); |
| 913 | defer allocator.free(stream_sizes); | 913 | defer allocator.free(stream_sizes); |
| 914 | 914 | ||
| ... | @@ -917,7 +917,7 @@ const Msf = struct { | ... | @@ -917,7 +917,7 @@ const Msf = struct { |
| 917 | // and must be taken into account when resolving stream indices. | 917 | // and must be taken into account when resolving stream indices. |
| 918 | const Nil = 0xFFFFFFFF; | 918 | const Nil = 0xFFFFFFFF; |
| 919 | for (stream_sizes) |*s| { | 919 | for (stream_sizes) |*s| { |
| 920 | const size = try directory.reader().readIntLittle(u32); | 920 | const size = try directory.reader().readInt(u32, .little); |
| 921 | s.* = if (size == Nil) 0 else blockCountFromSize(size, superblock.BlockSize); | 921 | s.* = if (size == Nil) 0 else blockCountFromSize(size, superblock.BlockSize); |
| 922 | } | 922 | } |
| 923 | 923 | ||
| ... | @@ -932,7 +932,7 @@ const Msf = struct { | ... | @@ -932,7 +932,7 @@ const Msf = struct { |
| 932 | var blocks = try allocator.alloc(u32, size); | 932 | var blocks = try allocator.alloc(u32, size); |
| 933 | var j: u32 = 0; | 933 | var j: u32 = 0; |
| 934 | while (j < size) : (j += 1) { | 934 | while (j < size) : (j += 1) { |
| 935 | const block_id = try directory.reader().readIntLittle(u32); | 935 | const block_id = try directory.reader().readInt(u32, .little); |
| 936 | const n = (block_id % superblock.BlockSize); | 936 | const n = (block_id % superblock.BlockSize); |
| 937 | // 0 is for SuperBlock, 1 and 2 for FPMs. | 937 | // 0 is for SuperBlock, 1 and 2 for FPMs. |
| 938 | if (block_id == 0 or n == 1 or n == 2 or block_id * superblock.BlockSize > file_len) | 938 | if (block_id == 0 or n == 1 or n == 2 or block_id * superblock.BlockSize > file_len) |
lib/std/rand.zig+5-4| ... | @@ -104,15 +104,16 @@ pub const Random = struct { | ... | @@ -104,15 +104,16 @@ pub const Random = struct { |
| 104 | pub fn int(r: Random, comptime T: type) T { | 104 | pub fn int(r: Random, comptime T: type) T { |
| 105 | const bits = @typeInfo(T).Int.bits; | 105 | const bits = @typeInfo(T).Int.bits; |
| 106 | const UnsignedT = std.meta.Int(.unsigned, bits); | 106 | const UnsignedT = std.meta.Int(.unsigned, bits); |
| 107 | const ByteAlignedT = std.meta.Int(.unsigned, @divTrunc(bits + 7, 8) * 8); | 107 | const ceil_bytes = comptime std.math.divCeil(u16, bits, 8) catch unreachable; |
| 108 | const ByteAlignedT = std.meta.Int(.unsigned, ceil_bytes * 8); | ||
| 108 | 109 | ||
| 109 | var rand_bytes: [@sizeOf(ByteAlignedT)]u8 = undefined; | 110 | var rand_bytes: [ceil_bytes]u8 = undefined; |
| 110 | r.bytes(rand_bytes[0..]); | 111 | r.bytes(&rand_bytes); |
| 111 | 112 | ||
| 112 | // use LE instead of native endian for better portability maybe? | 113 | // use LE instead of native endian for better portability maybe? |
| 113 | // TODO: endian portability is pointless if the underlying prng isn't endian portable. | 114 | // TODO: endian portability is pointless if the underlying prng isn't endian portable. |
| 114 | // TODO: document the endian portability of this library. | 115 | // TODO: document the endian portability of this library. |
| 115 | const byte_aligned_result = mem.readIntSliceLittle(ByteAlignedT, &rand_bytes); | 116 | const byte_aligned_result = mem.readInt(ByteAlignedT, &rand_bytes, .little); |
| 116 | const unsigned_result: UnsignedT = @truncate(byte_aligned_result); | 117 | const unsigned_result: UnsignedT = @truncate(byte_aligned_result); |
| 117 | return @bitCast(unsigned_result); | 118 | return @bitCast(unsigned_result); |
| 118 | } | 119 | } |
lib/std/rand/Ascon.zig+1-1| ... | @@ -13,7 +13,7 @@ const mem = std.mem; | ... | @@ -13,7 +13,7 @@ const mem = std.mem; |
| 13 | const Random = std.rand.Random; | 13 | const Random = std.rand.Random; |
| 14 | const Self = @This(); | 14 | const Self = @This(); |
| 15 | 15 | ||
| 16 | const Ascon = std.crypto.core.Ascon(.Little); | 16 | const Ascon = std.crypto.core.Ascon(.little); |
| 17 | 17 | ||
| 18 | state: Ascon, | 18 | state: Ascon, |
| 19 | 19 |
lib/std/rand/Isaac64.zig+1-1| ... | @@ -228,7 +228,7 @@ test "isaac64 fill" { | ... | @@ -228,7 +228,7 @@ test "isaac64 fill" { |
| 228 | for (seq) |s| { | 228 | for (seq) |s| { |
| 229 | var buf0: [8]u8 = undefined; | 229 | var buf0: [8]u8 = undefined; |
| 230 | var buf1: [7]u8 = undefined; | 230 | var buf1: [7]u8 = undefined; |
| 231 | std.mem.writeIntLittle(u64, &buf0, s); | 231 | std.mem.writeInt(u64, &buf0, s, .little); |
| 232 | r.fill(&buf1); | 232 | r.fill(&buf1); |
| 233 | try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..])); | 233 | try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..])); |
| 234 | } | 234 | } |
lib/std/rand/Pcg.zig+2-2| ... | @@ -111,8 +111,8 @@ test "pcg fill" { | ... | @@ -111,8 +111,8 @@ test "pcg fill" { |
| 111 | var i: u32 = 0; | 111 | var i: u32 = 0; |
| 112 | while (i < seq.len) : (i += 2) { | 112 | while (i < seq.len) : (i += 2) { |
| 113 | var buf0: [8]u8 = undefined; | 113 | var buf0: [8]u8 = undefined; |
| 114 | std.mem.writeIntLittle(u32, buf0[0..4], seq[i]); | 114 | std.mem.writeInt(u32, buf0[0..4], seq[i], .little); |
| 115 | std.mem.writeIntLittle(u32, buf0[4..8], seq[i + 1]); | 115 | std.mem.writeInt(u32, buf0[4..8], seq[i + 1], .little); |
| 116 | 116 | ||
| 117 | var buf1: [7]u8 = undefined; | 117 | var buf1: [7]u8 = undefined; |
| 118 | r.fill(&buf1); | 118 | r.fill(&buf1); |
lib/std/rand/RomuTrio.zig+1-1| ... | @@ -115,7 +115,7 @@ test "RomuTrio fill" { | ... | @@ -115,7 +115,7 @@ test "RomuTrio fill" { |
| 115 | for (seq) |s| { | 115 | for (seq) |s| { |
| 116 | var buf0: [8]u8 = undefined; | 116 | var buf0: [8]u8 = undefined; |
| 117 | var buf1: [7]u8 = undefined; | 117 | var buf1: [7]u8 = undefined; |
| 118 | std.mem.writeIntLittle(u64, &buf0, s); | 118 | std.mem.writeInt(u64, &buf0, s, .little); |
| 119 | r.fill(&buf1); | 119 | r.fill(&buf1); |
| 120 | try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..])); | 120 | try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..])); |
| 121 | } | 121 | } |
lib/std/rand/Sfc64.zig+1-1| ... | @@ -125,7 +125,7 @@ test "Sfc64 fill" { | ... | @@ -125,7 +125,7 @@ test "Sfc64 fill" { |
| 125 | for (seq) |s| { | 125 | for (seq) |s| { |
| 126 | var buf0: [8]u8 = undefined; | 126 | var buf0: [8]u8 = undefined; |
| 127 | var buf1: [7]u8 = undefined; | 127 | var buf1: [7]u8 = undefined; |
| 128 | std.mem.writeIntLittle(u64, &buf0, s); | 128 | std.mem.writeInt(u64, &buf0, s, .little); |
| 129 | r.fill(&buf1); | 129 | r.fill(&buf1); |
| 130 | try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..])); | 130 | try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..])); |
| 131 | } | 131 | } |
lib/std/rand/Xoroshiro128.zig+1-1| ... | @@ -140,7 +140,7 @@ test "xoroshiro fill" { | ... | @@ -140,7 +140,7 @@ test "xoroshiro fill" { |
| 140 | for (seq) |s| { | 140 | for (seq) |s| { |
| 141 | var buf0: [8]u8 = undefined; | 141 | var buf0: [8]u8 = undefined; |
| 142 | var buf1: [7]u8 = undefined; | 142 | var buf1: [7]u8 = undefined; |
| 143 | std.mem.writeIntLittle(u64, &buf0, s); | 143 | std.mem.writeInt(u64, &buf0, s, .little); |
| 144 | r.fill(&buf1); | 144 | r.fill(&buf1); |
| 145 | try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..])); | 145 | try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..])); |
| 146 | } | 146 | } |
lib/std/rand/Xoshiro256.zig+1-1| ... | @@ -139,7 +139,7 @@ test "xoroshiro fill" { | ... | @@ -139,7 +139,7 @@ test "xoroshiro fill" { |
| 139 | for (seq) |s| { | 139 | for (seq) |s| { |
| 140 | var buf0: [8]u8 = undefined; | 140 | var buf0: [8]u8 = undefined; |
| 141 | var buf1: [7]u8 = undefined; | 141 | var buf1: [7]u8 = undefined; |
| 142 | std.mem.writeIntLittle(u64, &buf0, s); | 142 | std.mem.writeInt(u64, &buf0, s, .little); |
| 143 | r.fill(&buf1); | 143 | r.fill(&buf1); |
| 144 | try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..])); | 144 | try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..])); |
| 145 | } | 145 | } |
lib/std/rand/test.zig+1-1| ... | @@ -71,7 +71,7 @@ const Dilbert = struct { | ... | @@ -71,7 +71,7 @@ const Dilbert = struct { |
| 71 | for (seq) |s| { | 71 | for (seq) |s| { |
| 72 | var buf0: [8]u8 = undefined; | 72 | var buf0: [8]u8 = undefined; |
| 73 | var buf1: [8]u8 = undefined; | 73 | var buf1: [8]u8 = undefined; |
| 74 | std.mem.writeIntBig(u64, &buf0, s); | 74 | std.mem.writeInt(u64, &buf0, s, .big); |
| 75 | r.fill(&buf1); | 75 | r.fill(&buf1); |
| 76 | try std.testing.expect(std.mem.eql(u8, buf0[0..], buf1[0..])); | 76 | try std.testing.expect(std.mem.eql(u8, buf0[0..], buf1[0..])); |
| 77 | } | 77 | } |
lib/std/target.zig+2-2| ... | @@ -1191,7 +1191,7 @@ pub const Target = struct { | ... | @@ -1191,7 +1191,7 @@ pub const Target = struct { |
| 1191 | .loongarch32, | 1191 | .loongarch32, |
| 1192 | .loongarch64, | 1192 | .loongarch64, |
| 1193 | .arc, | 1193 | .arc, |
| 1194 | => .Little, | 1194 | => .little, |
| 1195 | 1195 | ||
| 1196 | .armeb, | 1196 | .armeb, |
| 1197 | .aarch64_be, | 1197 | .aarch64_be, |
| ... | @@ -1207,7 +1207,7 @@ pub const Target = struct { | ... | @@ -1207,7 +1207,7 @@ pub const Target = struct { |
| 1207 | .tce, | 1207 | .tce, |
| 1208 | .lanai, | 1208 | .lanai, |
| 1209 | .s390x, | 1209 | .s390x, |
| 1210 | => .Big, | 1210 | => .big, |
| 1211 | }; | 1211 | }; |
| 1212 | } | 1212 | } |
| 1213 | 1213 |
lib/std/tz.zig+6-6| ... | @@ -59,7 +59,7 @@ pub const Tz = struct { | ... | @@ -59,7 +59,7 @@ pub const Tz = struct { |
| 59 | if (!std.mem.eql(u8, &legacy_header.magic, "TZif")) return error.BadHeader; | 59 | if (!std.mem.eql(u8, &legacy_header.magic, "TZif")) return error.BadHeader; |
| 60 | if (legacy_header.version != 0 and legacy_header.version != '2' and legacy_header.version != '3') return error.BadVersion; | 60 | if (legacy_header.version != 0 and legacy_header.version != '2' and legacy_header.version != '3') return error.BadVersion; |
| 61 | 61 | ||
| 62 | if (builtin.target.cpu.arch.endian() != std.builtin.Endian.Big) { | 62 | if (builtin.target.cpu.arch.endian() != std.builtin.Endian.big) { |
| 63 | std.mem.byteSwapAllFields(@TypeOf(legacy_header.counts), &legacy_header.counts); | 63 | std.mem.byteSwapAllFields(@TypeOf(legacy_header.counts), &legacy_header.counts); |
| 64 | } | 64 | } |
| 65 | 65 | ||
| ... | @@ -73,7 +73,7 @@ pub const Tz = struct { | ... | @@ -73,7 +73,7 @@ pub const Tz = struct { |
| 73 | var header = try reader.readStruct(Header); | 73 | var header = try reader.readStruct(Header); |
| 74 | if (!std.mem.eql(u8, &header.magic, "TZif")) return error.BadHeader; | 74 | if (!std.mem.eql(u8, &header.magic, "TZif")) return error.BadHeader; |
| 75 | if (header.version != '2' and header.version != '3') return error.BadVersion; | 75 | if (header.version != '2' and header.version != '3') return error.BadVersion; |
| 76 | if (builtin.target.cpu.arch.endian() != std.builtin.Endian.Big) { | 76 | if (builtin.target.cpu.arch.endian() != std.builtin.Endian.big) { |
| 77 | std.mem.byteSwapAllFields(@TypeOf(header.counts), &header.counts); | 77 | std.mem.byteSwapAllFields(@TypeOf(header.counts), &header.counts); |
| 78 | } | 78 | } |
| 79 | 79 | ||
| ... | @@ -98,7 +98,7 @@ pub const Tz = struct { | ... | @@ -98,7 +98,7 @@ pub const Tz = struct { |
| 98 | // Parse transition types | 98 | // Parse transition types |
| 99 | var i: usize = 0; | 99 | var i: usize = 0; |
| 100 | while (i < header.counts.timecnt) : (i += 1) { | 100 | while (i < header.counts.timecnt) : (i += 1) { |
| 101 | transitions[i].ts = if (legacy) try reader.readIntBig(i32) else try reader.readIntBig(i64); | 101 | transitions[i].ts = if (legacy) try reader.readInt(i32, .big) else try reader.readInt(i64, .big); |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | i = 0; | 104 | i = 0; |
| ... | @@ -111,7 +111,7 @@ pub const Tz = struct { | ... | @@ -111,7 +111,7 @@ pub const Tz = struct { |
| 111 | // Parse time types | 111 | // Parse time types |
| 112 | i = 0; | 112 | i = 0; |
| 113 | while (i < header.counts.typecnt) : (i += 1) { | 113 | while (i < header.counts.typecnt) : (i += 1) { |
| 114 | const offset = try reader.readIntBig(i32); | 114 | const offset = try reader.readInt(i32, .big); |
| 115 | if (offset < -2147483648) return error.Malformed; // rfc8536: utoff [...] MUST NOT be -2**31 | 115 | if (offset < -2147483648) return error.Malformed; // rfc8536: utoff [...] MUST NOT be -2**31 |
| 116 | const dst = try reader.readByte(); | 116 | const dst = try reader.readByte(); |
| 117 | if (dst != 0 and dst != 1) return error.Malformed; // rfc8536: (is)dst [...] The value MUST be 0 or 1. | 117 | if (dst != 0 and dst != 1) return error.Malformed; // rfc8536: (is)dst [...] The value MUST be 0 or 1. |
| ... | @@ -144,12 +144,12 @@ pub const Tz = struct { | ... | @@ -144,12 +144,12 @@ pub const Tz = struct { |
| 144 | // Parse leap seconds | 144 | // Parse leap seconds |
| 145 | i = 0; | 145 | i = 0; |
| 146 | while (i < header.counts.leapcnt) : (i += 1) { | 146 | while (i < header.counts.leapcnt) : (i += 1) { |
| 147 | const occur: i64 = if (legacy) try reader.readIntBig(i32) else try reader.readIntBig(i64); | 147 | const occur: i64 = if (legacy) try reader.readInt(i32, .big) else try reader.readInt(i64, .big); |
| 148 | if (occur < 0) return error.Malformed; // rfc8536: occur [...] MUST be nonnegative | 148 | if (occur < 0) return error.Malformed; // rfc8536: occur [...] MUST be nonnegative |
| 149 | if (i > 0 and leapseconds[i - 1].occurrence + 2419199 > occur) return error.Malformed; // rfc8536: occur [...] each later value MUST be at least 2419199 greater than the previous value | 149 | if (i > 0 and leapseconds[i - 1].occurrence + 2419199 > occur) return error.Malformed; // rfc8536: occur [...] each later value MUST be at least 2419199 greater than the previous value |
| 150 | if (occur > std.math.maxInt(i48)) return error.Malformed; // Unreasonably far into the future | 150 | if (occur > std.math.maxInt(i48)) return error.Malformed; // Unreasonably far into the future |
| 151 | 151 | ||
| 152 | const corr = try reader.readIntBig(i32); | 152 | const corr = try reader.readInt(i32, .big); |
| 153 | if (i == 0 and corr != -1 and corr != 1) return error.Malformed; // rfc8536: The correction value in the first leap-second record, if present, MUST be either one (1) or minus one (-1) | 153 | if (i == 0 and corr != -1 and corr != 1) return error.Malformed; // rfc8536: The correction value in the first leap-second record, if present, MUST be either one (1) or minus one (-1) |
| 154 | if (i > 0 and leapseconds[i - 1].correction != corr + 1 and leapseconds[i - 1].correction != corr - 1) return error.Malformed; // rfc8536: The correction values in adjacent leap-second records MUST differ by exactly one (1) | 154 | if (i > 0 and leapseconds[i - 1].correction != corr + 1 and leapseconds[i - 1].correction != corr - 1) return error.Malformed; // rfc8536: The correction values in adjacent leap-second records MUST differ by exactly one (1) |
| 155 | if (corr > std.math.maxInt(i16)) return error.Malformed; // Unreasonably large correction | 155 | if (corr > std.math.maxInt(i16)) return error.Malformed; // Unreasonably large correction |
lib/std/unicode.zig+25-24| ... | @@ -1,8 +1,9 @@ | ... | @@ -1,8 +1,9 @@ |
| 1 | const std = @import("./std.zig"); | 1 | const std = @import("./std.zig"); |
| 2 | const builtin = @import("builtin"); | ||
| 2 | const assert = std.debug.assert; | 3 | const assert = std.debug.assert; |
| 3 | const testing = std.testing; | 4 | const testing = std.testing; |
| 4 | const mem = std.mem; | 5 | const mem = std.mem; |
| 5 | const builtin = @import("builtin"); | 6 | const native_endian = builtin.cpu.arch.endian(); |
| 6 | 7 | ||
| 7 | /// Use this to replace an unknown, unrecognized, or unrepresentable character. | 8 | /// Use this to replace an unknown, unrecognized, or unrepresentable character. |
| 8 | /// | 9 | /// |
| ... | @@ -175,7 +176,7 @@ pub fn utf8CountCodepoints(s: []const u8) !usize { | ... | @@ -175,7 +176,7 @@ pub fn utf8CountCodepoints(s: []const u8) !usize { |
| 175 | while (i < s.len) { | 176 | while (i < s.len) { |
| 176 | // Fast path for ASCII sequences | 177 | // Fast path for ASCII sequences |
| 177 | while (i + N <= s.len) : (i += N) { | 178 | while (i + N <= s.len) : (i += N) { |
| 178 | const v = mem.readIntNative(usize, s[i..][0..N]); | 179 | const v = mem.readInt(usize, s[i..][0..N], native_endian); |
| 179 | if (v & MASK != 0) break; | 180 | if (v & MASK != 0) break; |
| 180 | len += N; | 181 | len += N; |
| 181 | } | 182 | } |
| ... | @@ -451,12 +452,12 @@ pub const Utf16LeIterator = struct { | ... | @@ -451,12 +452,12 @@ pub const Utf16LeIterator = struct { |
| 451 | assert(it.i <= it.bytes.len); | 452 | assert(it.i <= it.bytes.len); |
| 452 | if (it.i == it.bytes.len) return null; | 453 | if (it.i == it.bytes.len) return null; |
| 453 | var code_units: [2]u16 = undefined; | 454 | var code_units: [2]u16 = undefined; |
| 454 | code_units[0] = mem.readIntLittle(u16, it.bytes[it.i..][0..2]); | 455 | code_units[0] = mem.readInt(u16, it.bytes[it.i..][0..2], .little); |
| 455 | it.i += 2; | 456 | it.i += 2; |
| 456 | if (utf16IsHighSurrogate(code_units[0])) { | 457 | if (utf16IsHighSurrogate(code_units[0])) { |
| 457 | // surrogate pair | 458 | // surrogate pair |
| 458 | if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf; | 459 | if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf; |
| 459 | code_units[1] = mem.readIntLittle(u16, it.bytes[it.i..][0..2]); | 460 | code_units[1] = mem.readInt(u16, it.bytes[it.i..][0..2], .little); |
| 460 | const codepoint = try utf16DecodeSurrogatePair(&code_units); | 461 | const codepoint = try utf16DecodeSurrogatePair(&code_units); |
| 461 | it.i += 2; | 462 | it.i += 2; |
| 462 | return codepoint; | 463 | return codepoint; |
| ... | @@ -877,16 +878,16 @@ test "utf16leToUtf8" { | ... | @@ -877,16 +878,16 @@ test "utf16leToUtf8" { |
| 877 | const utf16le_as_bytes = mem.sliceAsBytes(utf16le[0..]); | 878 | const utf16le_as_bytes = mem.sliceAsBytes(utf16le[0..]); |
| 878 | 879 | ||
| 879 | { | 880 | { |
| 880 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 'A'); | 881 | mem.writeInt(u16, utf16le_as_bytes[0..2], 'A', .little); |
| 881 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 'a'); | 882 | mem.writeInt(u16, utf16le_as_bytes[2..4], 'a', .little); |
| 882 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); | 883 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); |
| 883 | defer std.testing.allocator.free(utf8); | 884 | defer std.testing.allocator.free(utf8); |
| 884 | try testing.expect(mem.eql(u8, utf8, "Aa")); | 885 | try testing.expect(mem.eql(u8, utf8, "Aa")); |
| 885 | } | 886 | } |
| 886 | 887 | ||
| 887 | { | 888 | { |
| 888 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0x80); | 889 | mem.writeInt(u16, utf16le_as_bytes[0..2], 0x80, .little); |
| 889 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xffff); | 890 | mem.writeInt(u16, utf16le_as_bytes[2..4], 0xffff, .little); |
| 890 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); | 891 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); |
| 891 | defer std.testing.allocator.free(utf8); | 892 | defer std.testing.allocator.free(utf8); |
| 892 | try testing.expect(mem.eql(u8, utf8, "\xc2\x80" ++ "\xef\xbf\xbf")); | 893 | try testing.expect(mem.eql(u8, utf8, "\xc2\x80" ++ "\xef\xbf\xbf")); |
| ... | @@ -894,8 +895,8 @@ test "utf16leToUtf8" { | ... | @@ -894,8 +895,8 @@ test "utf16leToUtf8" { |
| 894 | 895 | ||
| 895 | { | 896 | { |
| 896 | // the values just outside the surrogate half range | 897 | // the values just outside the surrogate half range |
| 897 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd7ff); | 898 | mem.writeInt(u16, utf16le_as_bytes[0..2], 0xd7ff, .little); |
| 898 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xe000); | 899 | mem.writeInt(u16, utf16le_as_bytes[2..4], 0xe000, .little); |
| 899 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); | 900 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); |
| 900 | defer std.testing.allocator.free(utf8); | 901 | defer std.testing.allocator.free(utf8); |
| 901 | try testing.expect(mem.eql(u8, utf8, "\xed\x9f\xbf" ++ "\xee\x80\x80")); | 902 | try testing.expect(mem.eql(u8, utf8, "\xed\x9f\xbf" ++ "\xee\x80\x80")); |
| ... | @@ -903,8 +904,8 @@ test "utf16leToUtf8" { | ... | @@ -903,8 +904,8 @@ test "utf16leToUtf8" { |
| 903 | 904 | ||
| 904 | { | 905 | { |
| 905 | // smallest surrogate pair | 906 | // smallest surrogate pair |
| 906 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd800); | 907 | mem.writeInt(u16, utf16le_as_bytes[0..2], 0xd800, .little); |
| 907 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00); | 908 | mem.writeInt(u16, utf16le_as_bytes[2..4], 0xdc00, .little); |
| 908 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); | 909 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); |
| 909 | defer std.testing.allocator.free(utf8); | 910 | defer std.testing.allocator.free(utf8); |
| 910 | try testing.expect(mem.eql(u8, utf8, "\xf0\x90\x80\x80")); | 911 | try testing.expect(mem.eql(u8, utf8, "\xf0\x90\x80\x80")); |
| ... | @@ -912,24 +913,24 @@ test "utf16leToUtf8" { | ... | @@ -912,24 +913,24 @@ test "utf16leToUtf8" { |
| 912 | 913 | ||
| 913 | { | 914 | { |
| 914 | // largest surrogate pair | 915 | // largest surrogate pair |
| 915 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff); | 916 | mem.writeInt(u16, utf16le_as_bytes[0..2], 0xdbff, .little); |
| 916 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdfff); | 917 | mem.writeInt(u16, utf16le_as_bytes[2..4], 0xdfff, .little); |
| 917 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); | 918 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); |
| 918 | defer std.testing.allocator.free(utf8); | 919 | defer std.testing.allocator.free(utf8); |
| 919 | try testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xbf\xbf")); | 920 | try testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xbf\xbf")); |
| 920 | } | 921 | } |
| 921 | 922 | ||
| 922 | { | 923 | { |
| 923 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff); | 924 | mem.writeInt(u16, utf16le_as_bytes[0..2], 0xdbff, .little); |
| 924 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00); | 925 | mem.writeInt(u16, utf16le_as_bytes[2..4], 0xdc00, .little); |
| 925 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); | 926 | const utf8 = try utf16leToUtf8Alloc(std.testing.allocator, &utf16le); |
| 926 | defer std.testing.allocator.free(utf8); | 927 | defer std.testing.allocator.free(utf8); |
| 927 | try testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80")); | 928 | try testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80")); |
| 928 | } | 929 | } |
| 929 | 930 | ||
| 930 | { | 931 | { |
| 931 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdcdc); | 932 | mem.writeInt(u16, utf16le_as_bytes[0..2], 0xdcdc, .little); |
| 932 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdcdc); | 933 | mem.writeInt(u16, utf16le_as_bytes[2..4], 0xdcdc, .little); |
| 933 | const result = utf16leToUtf8Alloc(std.testing.allocator, &utf16le); | 934 | const result = utf16leToUtf8Alloc(std.testing.allocator, &utf16le); |
| 934 | try std.testing.expectError(error.UnexpectedSecondSurrogateHalf, result); | 935 | try std.testing.expectError(error.UnexpectedSecondSurrogateHalf, result); |
| 935 | } | 936 | } |
| ... | @@ -1140,12 +1141,12 @@ test "fmtUtf16le" { | ... | @@ -1140,12 +1141,12 @@ test "fmtUtf16le" { |
| 1140 | try expectFmt("", "{}", .{fmtUtf16le(utf8ToUtf16LeStringLiteral(""))}); | 1141 | try expectFmt("", "{}", .{fmtUtf16le(utf8ToUtf16LeStringLiteral(""))}); |
| 1141 | try expectFmt("foo", "{}", .{fmtUtf16le(utf8ToUtf16LeStringLiteral("foo"))}); | 1142 | try expectFmt("foo", "{}", .{fmtUtf16le(utf8ToUtf16LeStringLiteral("foo"))}); |
| 1142 | try expectFmt("𐐷", "{}", .{fmtUtf16le(utf8ToUtf16LeStringLiteral("𐐷"))}); | 1143 | try expectFmt("𐐷", "{}", .{fmtUtf16le(utf8ToUtf16LeStringLiteral("𐐷"))}); |
| 1143 | try expectFmt("", "{}", .{fmtUtf16le(&[_]u16{std.mem.readIntNative(u16, "\xff\xd7")})}); | 1144 | try expectFmt("", "{}", .{fmtUtf16le(&[_]u16{std.mem.readInt(u16, "\xff\xd7", native_endian)})}); |
| 1144 | try expectFmt("�", "{}", .{fmtUtf16le(&[_]u16{std.mem.readIntNative(u16, "\x00\xd8")})}); | 1145 | try expectFmt("�", "{}", .{fmtUtf16le(&[_]u16{std.mem.readInt(u16, "\x00\xd8", native_endian)})}); |
| 1145 | try expectFmt("�", "{}", .{fmtUtf16le(&[_]u16{std.mem.readIntNative(u16, "\xff\xdb")})}); | 1146 | try expectFmt("�", "{}", .{fmtUtf16le(&[_]u16{std.mem.readInt(u16, "\xff\xdb", native_endian)})}); |
| 1146 | try expectFmt("�", "{}", .{fmtUtf16le(&[_]u16{std.mem.readIntNative(u16, "\x00\xdc")})}); | 1147 | try expectFmt("�", "{}", .{fmtUtf16le(&[_]u16{std.mem.readInt(u16, "\x00\xdc", native_endian)})}); |
| 1147 | try expectFmt("�", "{}", .{fmtUtf16le(&[_]u16{std.mem.readIntNative(u16, "\xff\xdf")})}); | 1148 | try expectFmt("�", "{}", .{fmtUtf16le(&[_]u16{std.mem.readInt(u16, "\xff\xdf", native_endian)})}); |
| 1148 | try expectFmt("", "{}", .{fmtUtf16le(&[_]u16{std.mem.readIntNative(u16, "\x00\xe0")})}); | 1149 | try expectFmt("", "{}", .{fmtUtf16le(&[_]u16{std.mem.readInt(u16, "\x00\xe0", native_endian)})}); |
| 1149 | } | 1150 | } |
| 1150 | 1151 | ||
| 1151 | test "utf8ToUtf16LeStringLiteral" { | 1152 | test "utf8ToUtf16LeStringLiteral" { |
lib/std/zig/Server.zig+3-3| ... | @@ -279,12 +279,12 @@ fn bswap_u32_array(slice: []u32) void { | ... | @@ -279,12 +279,12 @@ fn bswap_u32_array(slice: []u32) void { |
| 279 | 279 | ||
| 280 | /// workaround for https://github.com/ziglang/zig/issues/14904 | 280 | /// workaround for https://github.com/ziglang/zig/issues/14904 |
| 281 | fn bswap_and_workaround_u32(bytes_ptr: *const [4]u8) u32 { | 281 | fn bswap_and_workaround_u32(bytes_ptr: *const [4]u8) u32 { |
| 282 | return std.mem.readIntLittle(u32, bytes_ptr); | 282 | return std.mem.readInt(u32, bytes_ptr, .little); |
| 283 | } | 283 | } |
| 284 | 284 | ||
| 285 | /// workaround for https://github.com/ziglang/zig/issues/14904 | 285 | /// workaround for https://github.com/ziglang/zig/issues/14904 |
| 286 | fn bswap_and_workaround_tag(bytes_ptr: *const [4]u8) InMessage.Tag { | 286 | fn bswap_and_workaround_tag(bytes_ptr: *const [4]u8) InMessage.Tag { |
| 287 | const int = std.mem.readIntLittle(u32, bytes_ptr); | 287 | const int = std.mem.readInt(u32, bytes_ptr, .little); |
| 288 | return @as(InMessage.Tag, @enumFromInt(int)); | 288 | return @as(InMessage.Tag, @enumFromInt(int)); |
| 289 | } | 289 | } |
| 290 | 290 | ||
| ... | @@ -297,4 +297,4 @@ const std = @import("std"); | ... | @@ -297,4 +297,4 @@ const std = @import("std"); |
| 297 | const Allocator = std.mem.Allocator; | 297 | const Allocator = std.mem.Allocator; |
| 298 | const assert = std.debug.assert; | 298 | const assert = std.debug.assert; |
| 299 | const native_endian = builtin.target.cpu.arch.endian(); | 299 | const native_endian = builtin.target.cpu.arch.endian(); |
| 300 | const need_bswap = native_endian != .Little; | 300 | const need_bswap = native_endian != .little; |
lib/std/zig/system/NativeTargetInfo.zig+4-4| ... | @@ -488,8 +488,8 @@ fn glibcVerFromSoFile(file: fs.File) !std.SemanticVersion { | ... | @@ -488,8 +488,8 @@ fn glibcVerFromSoFile(file: fs.File) !std.SemanticVersion { |
| 488 | const hdr64 = @as(*elf.Elf64_Ehdr, @ptrCast(&hdr_buf)); | 488 | const hdr64 = @as(*elf.Elf64_Ehdr, @ptrCast(&hdr_buf)); |
| 489 | if (!mem.eql(u8, hdr32.e_ident[0..4], elf.MAGIC)) return error.InvalidElfMagic; | 489 | if (!mem.eql(u8, hdr32.e_ident[0..4], elf.MAGIC)) return error.InvalidElfMagic; |
| 490 | const elf_endian: std.builtin.Endian = switch (hdr32.e_ident[elf.EI_DATA]) { | 490 | const elf_endian: std.builtin.Endian = switch (hdr32.e_ident[elf.EI_DATA]) { |
| 491 | elf.ELFDATA2LSB => .Little, | 491 | elf.ELFDATA2LSB => .little, |
| 492 | elf.ELFDATA2MSB => .Big, | 492 | elf.ELFDATA2MSB => .big, |
| 493 | else => return error.InvalidElfEndian, | 493 | else => return error.InvalidElfEndian, |
| 494 | }; | 494 | }; |
| 495 | const need_bswap = elf_endian != native_endian; | 495 | const need_bswap = elf_endian != native_endian; |
| ... | @@ -635,8 +635,8 @@ pub fn abiAndDynamicLinkerFromFile( | ... | @@ -635,8 +635,8 @@ pub fn abiAndDynamicLinkerFromFile( |
| 635 | const hdr64 = @as(*elf.Elf64_Ehdr, @ptrCast(&hdr_buf)); | 635 | const hdr64 = @as(*elf.Elf64_Ehdr, @ptrCast(&hdr_buf)); |
| 636 | if (!mem.eql(u8, hdr32.e_ident[0..4], elf.MAGIC)) return error.InvalidElfMagic; | 636 | if (!mem.eql(u8, hdr32.e_ident[0..4], elf.MAGIC)) return error.InvalidElfMagic; |
| 637 | const elf_endian: std.builtin.Endian = switch (hdr32.e_ident[elf.EI_DATA]) { | 637 | const elf_endian: std.builtin.Endian = switch (hdr32.e_ident[elf.EI_DATA]) { |
| 638 | elf.ELFDATA2LSB => .Little, | 638 | elf.ELFDATA2LSB => .little, |
| 639 | elf.ELFDATA2MSB => .Big, | 639 | elf.ELFDATA2MSB => .big, |
| 640 | else => return error.InvalidElfEndian, | 640 | else => return error.InvalidElfEndian, |
| 641 | }; | 641 | }; |
| 642 | const need_bswap = elf_endian != native_endian; | 642 | const need_bswap = elf_endian != native_endian; |
lib/test_runner.zig+3-1| ... | @@ -13,7 +13,9 @@ var cmdline_buffer: [4096]u8 = undefined; | ... | @@ -13,7 +13,9 @@ var cmdline_buffer: [4096]u8 = undefined; |
| 13 | var fba = std.heap.FixedBufferAllocator.init(&cmdline_buffer); | 13 | var fba = std.heap.FixedBufferAllocator.init(&cmdline_buffer); |
| 14 | 14 | ||
| 15 | pub fn main() void { | 15 | pub fn main() void { |
| 16 | if (builtin.zig_backend == .stage2_aarch64) { | 16 | if (builtin.zig_backend == .stage2_aarch64 or |
| 17 | builtin.zig_backend == .stage2_wasm) | ||
| 18 | { | ||
| 17 | return mainSimple() catch @panic("test failure"); | 19 | return mainSimple() catch @panic("test failure"); |
| 18 | } | 20 | } |
| 19 | 21 |
src/Package/Fetch/git.zig+12-12| ... | @@ -305,12 +305,12 @@ const Odb = struct { | ... | @@ -305,12 +305,12 @@ const Odb = struct { |
| 305 | const n_objects = odb.index_header.fan_out_table[255]; | 305 | const n_objects = odb.index_header.fan_out_table[255]; |
| 306 | const offset_values_start = IndexHeader.size + n_objects * (oid_length + 4); | 306 | const offset_values_start = IndexHeader.size + n_objects * (oid_length + 4); |
| 307 | try odb.index_file.seekTo(offset_values_start + found_index * 4); | 307 | try odb.index_file.seekTo(offset_values_start + found_index * 4); |
| 308 | const l1_offset: packed struct { value: u31, big: bool } = @bitCast(try odb.index_file.reader().readIntBig(u32)); | 308 | const l1_offset: packed struct { value: u31, big: bool } = @bitCast(try odb.index_file.reader().readInt(u32, .big)); |
| 309 | const pack_offset = pack_offset: { | 309 | const pack_offset = pack_offset: { |
| 310 | if (l1_offset.big) { | 310 | if (l1_offset.big) { |
| 311 | const l2_offset_values_start = offset_values_start + n_objects * 4; | 311 | const l2_offset_values_start = offset_values_start + n_objects * 4; |
| 312 | try odb.index_file.seekTo(l2_offset_values_start + l1_offset.value * 4); | 312 | try odb.index_file.seekTo(l2_offset_values_start + l1_offset.value * 4); |
| 313 | break :pack_offset try odb.index_file.reader().readIntBig(u64); | 313 | break :pack_offset try odb.index_file.reader().readInt(u64, .big); |
| 314 | } else { | 314 | } else { |
| 315 | break :pack_offset l1_offset.value; | 315 | break :pack_offset l1_offset.value; |
| 316 | } | 316 | } |
| ... | @@ -845,12 +845,12 @@ const PackHeader = struct { | ... | @@ -845,12 +845,12 @@ const PackHeader = struct { |
| 845 | else => |other| return other, | 845 | else => |other| return other, |
| 846 | }; | 846 | }; |
| 847 | if (!mem.eql(u8, &actual_signature, signature)) return error.InvalidHeader; | 847 | if (!mem.eql(u8, &actual_signature, signature)) return error.InvalidHeader; |
| 848 | const version = reader.readIntBig(u32) catch |e| switch (e) { | 848 | const version = reader.readInt(u32, .big) catch |e| switch (e) { |
| 849 | error.EndOfStream => return error.InvalidHeader, | 849 | error.EndOfStream => return error.InvalidHeader, |
| 850 | else => |other| return other, | 850 | else => |other| return other, |
| 851 | }; | 851 | }; |
| 852 | if (version != supported_version) return error.UnsupportedVersion; | 852 | if (version != supported_version) return error.UnsupportedVersion; |
| 853 | const total_objects = reader.readIntBig(u32) catch |e| switch (e) { | 853 | const total_objects = reader.readInt(u32, .big) catch |e| switch (e) { |
| 854 | error.EndOfStream => return error.InvalidHeader, | 854 | error.EndOfStream => return error.InvalidHeader, |
| 855 | else => |other| return other, | 855 | else => |other| return other, |
| 856 | }; | 856 | }; |
| ... | @@ -966,14 +966,14 @@ const IndexHeader = struct { | ... | @@ -966,14 +966,14 @@ const IndexHeader = struct { |
| 966 | fn read(reader: anytype) !IndexHeader { | 966 | fn read(reader: anytype) !IndexHeader { |
| 967 | var header_bytes = try reader.readBytesNoEof(size); | 967 | var header_bytes = try reader.readBytesNoEof(size); |
| 968 | if (!mem.eql(u8, header_bytes[0..4], signature)) return error.InvalidHeader; | 968 | if (!mem.eql(u8, header_bytes[0..4], signature)) return error.InvalidHeader; |
| 969 | const version = mem.readIntBig(u32, header_bytes[4..8]); | 969 | const version = mem.readInt(u32, header_bytes[4..8], .big); |
| 970 | if (version != supported_version) return error.UnsupportedVersion; | 970 | if (version != supported_version) return error.UnsupportedVersion; |
| 971 | 971 | ||
| 972 | var fan_out_table: [256]u32 = undefined; | 972 | var fan_out_table: [256]u32 = undefined; |
| 973 | var fan_out_table_stream = std.io.fixedBufferStream(header_bytes[8..]); | 973 | var fan_out_table_stream = std.io.fixedBufferStream(header_bytes[8..]); |
| 974 | const fan_out_table_reader = fan_out_table_stream.reader(); | 974 | const fan_out_table_reader = fan_out_table_stream.reader(); |
| 975 | for (&fan_out_table) |*entry| { | 975 | for (&fan_out_table) |*entry| { |
| 976 | entry.* = fan_out_table_reader.readIntBig(u32) catch unreachable; | 976 | entry.* = fan_out_table_reader.readInt(u32, .big) catch unreachable; |
| 977 | } | 977 | } |
| 978 | return .{ .fan_out_table = fan_out_table }; | 978 | return .{ .fan_out_table = fan_out_table }; |
| 979 | } | 979 | } |
| ... | @@ -1041,9 +1041,9 @@ pub fn indexPack(allocator: Allocator, pack: std.fs.File, index_writer: anytype) | ... | @@ -1041,9 +1041,9 @@ pub fn indexPack(allocator: Allocator, pack: std.fs.File, index_writer: anytype) |
| 1041 | var index_hashed_writer = hashedWriter(index_writer, Sha1.init(.{})); | 1041 | var index_hashed_writer = hashedWriter(index_writer, Sha1.init(.{})); |
| 1042 | const writer = index_hashed_writer.writer(); | 1042 | const writer = index_hashed_writer.writer(); |
| 1043 | try writer.writeAll(IndexHeader.signature); | 1043 | try writer.writeAll(IndexHeader.signature); |
| 1044 | try writer.writeIntBig(u32, IndexHeader.supported_version); | 1044 | try writer.writeInt(u32, IndexHeader.supported_version, .big); |
| 1045 | for (fan_out_table) |fan_out_entry| { | 1045 | for (fan_out_table) |fan_out_entry| { |
| 1046 | try writer.writeIntBig(u32, fan_out_entry); | 1046 | try writer.writeInt(u32, fan_out_entry, .big); |
| 1047 | } | 1047 | } |
| 1048 | 1048 | ||
| 1049 | for (oids.items) |oid| { | 1049 | for (oids.items) |oid| { |
| ... | @@ -1051,7 +1051,7 @@ pub fn indexPack(allocator: Allocator, pack: std.fs.File, index_writer: anytype) | ... | @@ -1051,7 +1051,7 @@ pub fn indexPack(allocator: Allocator, pack: std.fs.File, index_writer: anytype) |
| 1051 | } | 1051 | } |
| 1052 | 1052 | ||
| 1053 | for (oids.items) |oid| { | 1053 | for (oids.items) |oid| { |
| 1054 | try writer.writeIntBig(u32, index_entries.get(oid).?.crc32); | 1054 | try writer.writeInt(u32, index_entries.get(oid).?.crc32, .big); |
| 1055 | } | 1055 | } |
| 1056 | 1056 | ||
| 1057 | var big_offsets = std.ArrayListUnmanaged(u64){}; | 1057 | var big_offsets = std.ArrayListUnmanaged(u64){}; |
| ... | @@ -1059,15 +1059,15 @@ pub fn indexPack(allocator: Allocator, pack: std.fs.File, index_writer: anytype) | ... | @@ -1059,15 +1059,15 @@ pub fn indexPack(allocator: Allocator, pack: std.fs.File, index_writer: anytype) |
| 1059 | for (oids.items) |oid| { | 1059 | for (oids.items) |oid| { |
| 1060 | const offset = index_entries.get(oid).?.offset; | 1060 | const offset = index_entries.get(oid).?.offset; |
| 1061 | if (offset <= std.math.maxInt(u31)) { | 1061 | if (offset <= std.math.maxInt(u31)) { |
| 1062 | try writer.writeIntBig(u32, @intCast(offset)); | 1062 | try writer.writeInt(u32, @intCast(offset), .big); |
| 1063 | } else { | 1063 | } else { |
| 1064 | const index = big_offsets.items.len; | 1064 | const index = big_offsets.items.len; |
| 1065 | try big_offsets.append(allocator, offset); | 1065 | try big_offsets.append(allocator, offset); |
| 1066 | try writer.writeIntBig(u32, @as(u32, @intCast(index)) | (1 << 31)); | 1066 | try writer.writeInt(u32, @as(u32, @intCast(index)) | (1 << 31), .big); |
| 1067 | } | 1067 | } |
| 1068 | } | 1068 | } |
| 1069 | for (big_offsets.items) |offset| { | 1069 | for (big_offsets.items) |offset| { |
| 1070 | try writer.writeIntBig(u64, offset); | 1070 | try writer.writeInt(u64, offset, .big); |
| 1071 | } | 1071 | } |
| 1072 | 1072 | ||
| 1073 | try writer.writeAll(&pack_checksum); | 1073 | try writer.writeAll(&pack_checksum); |
src/Sema.zig+24-5| ... | @@ -26835,7 +26835,7 @@ fn structFieldPtrByIndex( | ... | @@ -26835,7 +26835,7 @@ fn structFieldPtrByIndex( |
| 26835 | // cause miscompilations; it only means the field pointer uses bit masking when it | 26835 | // cause miscompilations; it only means the field pointer uses bit masking when it |
| 26836 | // might not be strictly necessary. | 26836 | // might not be strictly necessary. |
| 26837 | if (parent_align != .none and ptr_ty_data.packed_offset.bit_offset % 8 == 0 and | 26837 | if (parent_align != .none and ptr_ty_data.packed_offset.bit_offset % 8 == 0 and |
| 26838 | target.cpu.arch.endian() == .Little) | 26838 | target.cpu.arch.endian() == .little) |
| 26839 | { | 26839 | { |
| 26840 | const elem_size_bytes = try sema.typeAbiSize(ptr_ty_data.child.toType()); | 26840 | const elem_size_bytes = try sema.typeAbiSize(ptr_ty_data.child.toType()); |
| 26841 | const elem_size_bits = ptr_ty_data.child.toType().bitSize(mod); | 26841 | const elem_size_bits = ptr_ty_data.child.toType().bitSize(mod); |
| ... | @@ -29684,6 +29684,7 @@ fn storePtrVal( | ... | @@ -29684,6 +29684,7 @@ fn storePtrVal( |
| 29684 | try sema.checkComptimeVarStore(block, src, mut_kit.mut_decl); | 29684 | try sema.checkComptimeVarStore(block, src, mut_kit.mut_decl); |
| 29685 | 29685 | ||
| 29686 | switch (mut_kit.pointee) { | 29686 | switch (mut_kit.pointee) { |
| 29687 | .opv => {}, | ||
| 29687 | .direct => |val_ptr| { | 29688 | .direct => |val_ptr| { |
| 29688 | if (mut_kit.mut_decl.runtime_index == .comptime_field_ptr) { | 29689 | if (mut_kit.mut_decl.runtime_index == .comptime_field_ptr) { |
| 29689 | val_ptr.* = (try val_ptr.intern(operand_ty, mod)).toValue(); | 29690 | val_ptr.* = (try val_ptr.intern(operand_ty, mod)).toValue(); |
| ... | @@ -29741,6 +29742,7 @@ fn storePtrVal( | ... | @@ -29741,6 +29742,7 @@ fn storePtrVal( |
| 29741 | const ComptimePtrMutationKit = struct { | 29742 | const ComptimePtrMutationKit = struct { |
| 29742 | mut_decl: InternPool.Key.Ptr.Addr.MutDecl, | 29743 | mut_decl: InternPool.Key.Ptr.Addr.MutDecl, |
| 29743 | pointee: union(enum) { | 29744 | pointee: union(enum) { |
| 29745 | opv, | ||
| 29744 | /// The pointer type matches the actual comptime Value so a direct | 29746 | /// The pointer type matches the actual comptime Value so a direct |
| 29745 | /// modification is possible. | 29747 | /// modification is possible. |
| 29746 | direct: *Value, | 29748 | direct: *Value, |
| ... | @@ -29793,6 +29795,7 @@ fn beginComptimePtrMutation( | ... | @@ -29793,6 +29795,7 @@ fn beginComptimePtrMutation( |
| 29793 | const eu_ty = mod.intern_pool.typeOf(eu_ptr).toType().childType(mod); | 29795 | const eu_ty = mod.intern_pool.typeOf(eu_ptr).toType().childType(mod); |
| 29794 | var parent = try sema.beginComptimePtrMutation(block, src, eu_ptr.toValue(), eu_ty); | 29796 | var parent = try sema.beginComptimePtrMutation(block, src, eu_ptr.toValue(), eu_ty); |
| 29795 | switch (parent.pointee) { | 29797 | switch (parent.pointee) { |
| 29798 | .opv => unreachable, | ||
| 29796 | .direct => |val_ptr| { | 29799 | .direct => |val_ptr| { |
| 29797 | const payload_ty = parent.ty.errorUnionPayload(mod); | 29800 | const payload_ty = parent.ty.errorUnionPayload(mod); |
| 29798 | if (val_ptr.ip_index == .none and val_ptr.tag() == .eu_payload) { | 29801 | if (val_ptr.ip_index == .none and val_ptr.tag() == .eu_payload) { |
| ... | @@ -29835,6 +29838,7 @@ fn beginComptimePtrMutation( | ... | @@ -29835,6 +29838,7 @@ fn beginComptimePtrMutation( |
| 29835 | const opt_ty = mod.intern_pool.typeOf(opt_ptr).toType().childType(mod); | 29838 | const opt_ty = mod.intern_pool.typeOf(opt_ptr).toType().childType(mod); |
| 29836 | var parent = try sema.beginComptimePtrMutation(block, src, opt_ptr.toValue(), opt_ty); | 29839 | var parent = try sema.beginComptimePtrMutation(block, src, opt_ptr.toValue(), opt_ty); |
| 29837 | switch (parent.pointee) { | 29840 | switch (parent.pointee) { |
| 29841 | .opv => unreachable, | ||
| 29838 | .direct => |val_ptr| { | 29842 | .direct => |val_ptr| { |
| 29839 | const payload_ty = parent.ty.optionalChild(mod); | 29843 | const payload_ty = parent.ty.optionalChild(mod); |
| 29840 | switch (val_ptr.ip_index) { | 29844 | switch (val_ptr.ip_index) { |
| ... | @@ -29888,16 +29892,30 @@ fn beginComptimePtrMutation( | ... | @@ -29888,16 +29892,30 @@ fn beginComptimePtrMutation( |
| 29888 | var parent = try sema.beginComptimePtrMutation(block, src, elem_ptr.base.toValue(), base_elem_ty); | 29892 | var parent = try sema.beginComptimePtrMutation(block, src, elem_ptr.base.toValue(), base_elem_ty); |
| 29889 | 29893 | ||
| 29890 | switch (parent.pointee) { | 29894 | switch (parent.pointee) { |
| 29895 | .opv => unreachable, | ||
| 29891 | .direct => |val_ptr| switch (parent.ty.zigTypeTag(mod)) { | 29896 | .direct => |val_ptr| switch (parent.ty.zigTypeTag(mod)) { |
| 29892 | .Array, .Vector => { | 29897 | .Array, .Vector => { |
| 29898 | const elem_ty = parent.ty.childType(mod); | ||
| 29893 | const check_len = parent.ty.arrayLenIncludingSentinel(mod); | 29899 | const check_len = parent.ty.arrayLenIncludingSentinel(mod); |
| 29900 | if ((try sema.typeHasOnePossibleValue(ptr_elem_ty)) != null) { | ||
| 29901 | if (elem_ptr.index > check_len) { | ||
| 29902 | // TODO have the parent include the decl so we can say "declared here" | ||
| 29903 | return sema.fail(block, src, "comptime store of index {d} out of bounds of array length {d}", .{ | ||
| 29904 | elem_ptr.index, check_len, | ||
| 29905 | }); | ||
| 29906 | } | ||
| 29907 | return .{ | ||
| 29908 | .mut_decl = parent.mut_decl, | ||
| 29909 | .pointee = .opv, | ||
| 29910 | .ty = elem_ty, | ||
| 29911 | }; | ||
| 29912 | } | ||
| 29894 | if (elem_ptr.index >= check_len) { | 29913 | if (elem_ptr.index >= check_len) { |
| 29895 | // TODO have the parent include the decl so we can say "declared here" | 29914 | // TODO have the parent include the decl so we can say "declared here" |
| 29896 | return sema.fail(block, src, "comptime store of index {d} out of bounds of array length {d}", .{ | 29915 | return sema.fail(block, src, "comptime store of index {d} out of bounds of array length {d}", .{ |
| 29897 | elem_ptr.index, check_len, | 29916 | elem_ptr.index, check_len, |
| 29898 | }); | 29917 | }); |
| 29899 | } | 29918 | } |
| 29900 | const elem_ty = parent.ty.childType(mod); | ||
| 29901 | 29919 | ||
| 29902 | // We might have a pointer to multiple elements of the array (e.g. a pointer | 29920 | // We might have a pointer to multiple elements of the array (e.g. a pointer |
| 29903 | // to a sub-array). In this case, we just have to reinterpret the relevant | 29921 | // to a sub-array). In this case, we just have to reinterpret the relevant |
| ... | @@ -30072,6 +30090,7 @@ fn beginComptimePtrMutation( | ... | @@ -30072,6 +30090,7 @@ fn beginComptimePtrMutation( |
| 30072 | 30090 | ||
| 30073 | var parent = try sema.beginComptimePtrMutation(block, src, field_ptr.base.toValue(), base_child_ty); | 30091 | var parent = try sema.beginComptimePtrMutation(block, src, field_ptr.base.toValue(), base_child_ty); |
| 30074 | switch (parent.pointee) { | 30092 | switch (parent.pointee) { |
| 30093 | .opv => unreachable, | ||
| 30075 | .direct => |val_ptr| switch (val_ptr.ip_index) { | 30094 | .direct => |val_ptr| switch (val_ptr.ip_index) { |
| 30076 | .empty_struct => { | 30095 | .empty_struct => { |
| 30077 | const duped = try sema.arena.create(Value); | 30096 | const duped = try sema.arena.create(Value); |
| ... | @@ -30717,8 +30736,8 @@ fn bitCastUnionFieldVal( | ... | @@ -30717,8 +30736,8 @@ fn bitCastUnionFieldVal( |
| 30717 | if (field_size > old_size) { | 30736 | if (field_size > old_size) { |
| 30718 | const min_size = @max(old_size, 1); | 30737 | const min_size = @max(old_size, 1); |
| 30719 | switch (endian) { | 30738 | switch (endian) { |
| 30720 | .Little => @memset(buffer[min_size - 1 ..], 0xaa), | 30739 | .little => @memset(buffer[min_size - 1 ..], 0xaa), |
| 30721 | .Big => @memset(buffer[0 .. buffer.len - min_size + 1], 0xaa), | 30740 | .big => @memset(buffer[0 .. buffer.len - min_size + 1], 0xaa), |
| 30722 | } | 30741 | } |
| 30723 | } | 30742 | } |
| 30724 | 30743 | ||
| ... | @@ -30727,7 +30746,7 @@ fn bitCastUnionFieldVal( | ... | @@ -30727,7 +30746,7 @@ fn bitCastUnionFieldVal( |
| 30727 | error.ReinterpretDeclRef => return null, | 30746 | error.ReinterpretDeclRef => return null, |
| 30728 | }; | 30747 | }; |
| 30729 | 30748 | ||
| 30730 | break :offset if (endian == .Big) buffer.len - field_size else 0; | 30749 | break :offset if (endian == .big) buffer.len - field_size else 0; |
| 30731 | }, | 30750 | }, |
| 30732 | .Auto => unreachable, | 30751 | .Auto => unreachable, |
| 30733 | }; | 30752 | }; |
src/arch/sparc64/CodeGen.zig+2-2| ... | @@ -1231,8 +1231,8 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1231,8 +1231,8 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { |
| 1231 | const abi_size = int_info.bits >> 3; | 1231 | const abi_size = int_info.bits >> 3; |
| 1232 | const abi_align = operand_ty.abiAlignment(mod); | 1232 | const abi_align = operand_ty.abiAlignment(mod); |
| 1233 | const opposite_endian_asi = switch (self.target.cpu.arch.endian()) { | 1233 | const opposite_endian_asi = switch (self.target.cpu.arch.endian()) { |
| 1234 | Endian.Big => ASI.asi_primary_little, | 1234 | Endian.big => ASI.asi_primary_little, |
| 1235 | Endian.Little => ASI.asi_primary, | 1235 | Endian.little => ASI.asi_primary, |
| 1236 | }; | 1236 | }; |
| 1237 | 1237 | ||
| 1238 | switch (operand) { | 1238 | switch (operand) { |
src/arch/sparc64/Emit.zig+1-1| ... | @@ -677,7 +677,7 @@ fn writeInstruction(emit: *Emit, instruction: Instruction) !void { | ... | @@ -677,7 +677,7 @@ fn writeInstruction(emit: *Emit, instruction: Instruction) !void { |
| 677 | // SPARCv9 instructions are always arranged in BE regardless of the | 677 | // SPARCv9 instructions are always arranged in BE regardless of the |
| 678 | // endianness mode the CPU is running in (Section 3.1 of the ISA specification). | 678 | // endianness mode the CPU is running in (Section 3.1 of the ISA specification). |
| 679 | // This is to ease porting in case someone wants to do a LE SPARCv9 backend. | 679 | // This is to ease porting in case someone wants to do a LE SPARCv9 backend. |
| 680 | const endian = Endian.Big; | 680 | const endian = Endian.big; |
| 681 | 681 | ||
| 682 | std.mem.writeInt(u32, try emit.code.addManyAsArray(4), instruction.toU32(), endian); | 682 | std.mem.writeInt(u32, try emit.code.addManyAsArray(4), instruction.toU32(), endian); |
| 683 | } | 683 | } |
src/arch/wasm/CodeGen.zig+1-1| ... | @@ -3364,7 +3364,7 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { | ... | @@ -3364,7 +3364,7 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3364 | const backing_int_ty = struct_type.backingIntType(ip).toType(); | 3364 | const backing_int_ty = struct_type.backingIntType(ip).toType(); |
| 3365 | const int_val = try mod.intValue( | 3365 | const int_val = try mod.intValue( |
| 3366 | backing_int_ty, | 3366 | backing_int_ty, |
| 3367 | mem.readIntLittle(u64, &buf), | 3367 | mem.readInt(u64, &buf, .little), |
| 3368 | ); | 3368 | ); |
| 3369 | return func.lowerConstant(int_val, backing_int_ty); | 3369 | return func.lowerConstant(int_val, backing_int_ty); |
| 3370 | }, | 3370 | }, |
src/arch/wasm/Emit.zig+2-2| ... | @@ -330,14 +330,14 @@ fn emitImm64(emit: *Emit, inst: Mir.Inst.Index) !void { | ... | @@ -330,14 +330,14 @@ fn emitImm64(emit: *Emit, inst: Mir.Inst.Index) !void { |
| 330 | fn emitFloat32(emit: *Emit, inst: Mir.Inst.Index) !void { | 330 | fn emitFloat32(emit: *Emit, inst: Mir.Inst.Index) !void { |
| 331 | const value: f32 = emit.mir.instructions.items(.data)[inst].float32; | 331 | const value: f32 = emit.mir.instructions.items(.data)[inst].float32; |
| 332 | try emit.code.append(std.wasm.opcode(.f32_const)); | 332 | try emit.code.append(std.wasm.opcode(.f32_const)); |
| 333 | try emit.code.writer().writeIntLittle(u32, @as(u32, @bitCast(value))); | 333 | try emit.code.writer().writeInt(u32, @bitCast(value), .little); |
| 334 | } | 334 | } |
| 335 | 335 | ||
| 336 | fn emitFloat64(emit: *Emit, inst: Mir.Inst.Index) !void { | 336 | fn emitFloat64(emit: *Emit, inst: Mir.Inst.Index) !void { |
| 337 | const extra_index = emit.mir.instructions.items(.data)[inst].payload; | 337 | const extra_index = emit.mir.instructions.items(.data)[inst].payload; |
| 338 | const value = emit.mir.extraData(Mir.Float64, extra_index); | 338 | const value = emit.mir.extraData(Mir.Float64, extra_index); |
| 339 | try emit.code.append(std.wasm.opcode(.f64_const)); | 339 | try emit.code.append(std.wasm.opcode(.f64_const)); |
| 340 | try emit.code.writer().writeIntLittle(u64, value.data.toU64()); | 340 | try emit.code.writer().writeInt(u64, value.data.toU64(), .little); |
| 341 | } | 341 | } |
| 342 | 342 | ||
| 343 | fn emitMemArg(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) !void { | 343 | fn emitMemArg(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) !void { |
src/arch/x86_64/Disassembler.zig+12-12| ... | @@ -360,13 +360,13 @@ fn parseImm(dis: *Disassembler, kind: Encoding.Op) !Immediate { | ... | @@ -360,13 +360,13 @@ fn parseImm(dis: *Disassembler, kind: Encoding.Op) !Immediate { |
| 360 | var creader = std.io.countingReader(stream.reader()); | 360 | var creader = std.io.countingReader(stream.reader()); |
| 361 | const reader = creader.reader(); | 361 | const reader = creader.reader(); |
| 362 | const imm = switch (kind) { | 362 | const imm = switch (kind) { |
| 363 | .imm8s, .rel8 => Immediate.s(try reader.readInt(i8, .Little)), | 363 | .imm8s, .rel8 => Immediate.s(try reader.readInt(i8, .little)), |
| 364 | .imm16s, .rel16 => Immediate.s(try reader.readInt(i16, .Little)), | 364 | .imm16s, .rel16 => Immediate.s(try reader.readInt(i16, .little)), |
| 365 | .imm32s, .rel32 => Immediate.s(try reader.readInt(i32, .Little)), | 365 | .imm32s, .rel32 => Immediate.s(try reader.readInt(i32, .little)), |
| 366 | .imm8 => Immediate.u(try reader.readInt(u8, .Little)), | 366 | .imm8 => Immediate.u(try reader.readInt(u8, .little)), |
| 367 | .imm16 => Immediate.u(try reader.readInt(u16, .Little)), | 367 | .imm16 => Immediate.u(try reader.readInt(u16, .little)), |
| 368 | .imm32 => Immediate.u(try reader.readInt(u32, .Little)), | 368 | .imm32 => Immediate.u(try reader.readInt(u32, .little)), |
| 369 | .imm64 => Immediate.u(try reader.readInt(u64, .Little)), | 369 | .imm64 => Immediate.u(try reader.readInt(u64, .little)), |
| 370 | else => unreachable, | 370 | else => unreachable, |
| 371 | }; | 371 | }; |
| 372 | dis.pos += std.math.cast(usize, creader.bytes_read) orelse return error.Overflow; | 372 | dis.pos += std.math.cast(usize, creader.bytes_read) orelse return error.Overflow; |
| ... | @@ -376,7 +376,7 @@ fn parseImm(dis: *Disassembler, kind: Encoding.Op) !Immediate { | ... | @@ -376,7 +376,7 @@ fn parseImm(dis: *Disassembler, kind: Encoding.Op) !Immediate { |
| 376 | fn parseOffset(dis: *Disassembler) !u64 { | 376 | fn parseOffset(dis: *Disassembler) !u64 { |
| 377 | var stream = std.io.fixedBufferStream(dis.code[dis.pos..]); | 377 | var stream = std.io.fixedBufferStream(dis.code[dis.pos..]); |
| 378 | const reader = stream.reader(); | 378 | const reader = stream.reader(); |
| 379 | const offset = try reader.readInt(u64, .Little); | 379 | const offset = try reader.readInt(u64, .little); |
| 380 | dis.pos += 8; | 380 | dis.pos += 8; |
| 381 | return offset; | 381 | return offset; |
| 382 | } | 382 | } |
| ... | @@ -457,16 +457,16 @@ fn parseDisplacement(dis: *Disassembler, modrm: ModRm, sib: ?Sib) !i32 { | ... | @@ -457,16 +457,16 @@ fn parseDisplacement(dis: *Disassembler, modrm: ModRm, sib: ?Sib) !i32 { |
| 457 | const disp = disp: { | 457 | const disp = disp: { |
| 458 | if (sib) |info| { | 458 | if (sib) |info| { |
| 459 | if (info.base == 0b101 and modrm.mod == 0) { | 459 | if (info.base == 0b101 and modrm.mod == 0) { |
| 460 | break :disp try reader.readInt(i32, .Little); | 460 | break :disp try reader.readInt(i32, .little); |
| 461 | } | 461 | } |
| 462 | } | 462 | } |
| 463 | if (modrm.rip()) { | 463 | if (modrm.rip()) { |
| 464 | break :disp try reader.readInt(i32, .Little); | 464 | break :disp try reader.readInt(i32, .little); |
| 465 | } | 465 | } |
| 466 | break :disp switch (modrm.mod) { | 466 | break :disp switch (modrm.mod) { |
| 467 | 0b00 => 0, | 467 | 0b00 => 0, |
| 468 | 0b01 => try reader.readInt(i8, .Little), | 468 | 0b01 => try reader.readInt(i8, .little), |
| 469 | 0b10 => try reader.readInt(i32, .Little), | 469 | 0b10 => try reader.readInt(i32, .little), |
| 470 | 0b11 => unreachable, | 470 | 0b11 => unreachable, |
| 471 | }; | 471 | }; |
| 472 | }; | 472 | }; |
src/arch/x86_64/Emit.zig+1-1| ... | @@ -253,7 +253,7 @@ fn fixupRelocs(emit: *Emit) Error!void { | ... | @@ -253,7 +253,7 @@ fn fixupRelocs(emit: *Emit) Error!void { |
| 253 | const target = emit.code_offset_mapping.get(reloc.target) orelse | 253 | const target = emit.code_offset_mapping.get(reloc.target) orelse |
| 254 | return emit.fail("JMP/CALL relocation target not found!", .{}); | 254 | return emit.fail("JMP/CALL relocation target not found!", .{}); |
| 255 | const disp = @as(i32, @intCast(@as(i64, @intCast(target)) - @as(i64, @intCast(reloc.source + reloc.length)))); | 255 | const disp = @as(i32, @intCast(@as(i64, @intCast(target)) - @as(i64, @intCast(reloc.source + reloc.length)))); |
| 256 | mem.writeIntLittle(i32, emit.code.items[reloc.offset..][0..4], disp); | 256 | mem.writeInt(i32, emit.code.items[reloc.offset..][0..4], disp, .little); |
| 257 | } | 257 | } |
| 258 | } | 258 | } |
| 259 | 259 |
src/arch/x86_64/encoder.zig+4-4| ... | @@ -998,7 +998,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -998,7 +998,7 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 998 | /// | 998 | /// |
| 999 | /// It is sign-extended to 64 bits by the cpu. | 999 | /// It is sign-extended to 64 bits by the cpu. |
| 1000 | pub fn disp32(self: Self, disp: i32) !void { | 1000 | pub fn disp32(self: Self, disp: i32) !void { |
| 1001 | try self.writer.writeIntLittle(i32, disp); | 1001 | try self.writer.writeInt(i32, disp, .little); |
| 1002 | } | 1002 | } |
| 1003 | 1003 | ||
| 1004 | /// Encode an 8 bit immediate | 1004 | /// Encode an 8 bit immediate |
| ... | @@ -1012,21 +1012,21 @@ fn Encoder(comptime T: type, comptime opts: Options) type { | ... | @@ -1012,21 +1012,21 @@ fn Encoder(comptime T: type, comptime opts: Options) type { |
| 1012 | /// | 1012 | /// |
| 1013 | /// It is sign-extended to 64 bits by the cpu. | 1013 | /// It is sign-extended to 64 bits by the cpu. |
| 1014 | pub fn imm16(self: Self, imm: u16) !void { | 1014 | pub fn imm16(self: Self, imm: u16) !void { |
| 1015 | try self.writer.writeIntLittle(u16, imm); | 1015 | try self.writer.writeInt(u16, imm, .little); |
| 1016 | } | 1016 | } |
| 1017 | 1017 | ||
| 1018 | /// Encode an 32 bit immediate | 1018 | /// Encode an 32 bit immediate |
| 1019 | /// | 1019 | /// |
| 1020 | /// It is sign-extended to 64 bits by the cpu. | 1020 | /// It is sign-extended to 64 bits by the cpu. |
| 1021 | pub fn imm32(self: Self, imm: u32) !void { | 1021 | pub fn imm32(self: Self, imm: u32) !void { |
| 1022 | try self.writer.writeIntLittle(u32, imm); | 1022 | try self.writer.writeInt(u32, imm, .little); |
| 1023 | } | 1023 | } |
| 1024 | 1024 | ||
| 1025 | /// Encode an 64 bit immediate | 1025 | /// Encode an 64 bit immediate |
| 1026 | /// | 1026 | /// |
| 1027 | /// It is sign-extended to 64 bits by the cpu. | 1027 | /// It is sign-extended to 64 bits by the cpu. |
| 1028 | pub fn imm64(self: Self, imm: u64) !void { | 1028 | pub fn imm64(self: Self, imm: u64) !void { |
| 1029 | try self.writer.writeIntLittle(u64, imm); | 1029 | try self.writer.writeInt(u64, imm, .little); |
| 1030 | } | 1030 | } |
| 1031 | }; | 1031 | }; |
| 1032 | } | 1032 | } |
src/codegen/c.zig+12-9| ... | @@ -4652,7 +4652,10 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca | ... | @@ -4652,7 +4652,10 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca |
| 4652 | try writer.writeAll(", &"); | 4652 | try writer.writeAll(", &"); |
| 4653 | try f.writeCValue(writer, operand_lval, .Other); | 4653 | try f.writeCValue(writer, operand_lval, .Other); |
| 4654 | try writer.writeAll(", sizeof("); | 4654 | try writer.writeAll(", sizeof("); |
| 4655 | try f.renderType(writer, dest_ty); | 4655 | try f.renderType( |
| 4656 | writer, | ||
| 4657 | if (dest_ty.abiSize(mod) <= operand_ty.abiSize(mod)) dest_ty else operand_ty, | ||
| 4658 | ); | ||
| 4656 | try writer.writeAll("));\n"); | 4659 | try writer.writeAll("));\n"); |
| 4657 | 4660 | ||
| 4658 | // Ensure padding bits have the expected value. | 4661 | // Ensure padding bits have the expected value. |
| ... | @@ -4666,8 +4669,8 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca | ... | @@ -4666,8 +4669,8 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca |
| 4666 | try f.writeCValue(writer, local, .Other); | 4669 | try f.writeCValue(writer, local, .Other); |
| 4667 | if (dest_cty.castTag(.array)) |pl| { | 4670 | if (dest_cty.castTag(.array)) |pl| { |
| 4668 | try writer.print("[{d}]", .{switch (target.cpu.arch.endian()) { | 4671 | try writer.print("[{d}]", .{switch (target.cpu.arch.endian()) { |
| 4669 | .Little => pl.data.len - 1, | 4672 | .little => pl.data.len - 1, |
| 4670 | .Big => 0, | 4673 | .big => 0, |
| 4671 | }}); | 4674 | }}); |
| 4672 | const elem_cty = f.indexToCType(pl.data.elem_type); | 4675 | const elem_cty = f.indexToCType(pl.data.elem_type); |
| 4673 | wrap_cty = elem_cty.toSignedness(dest_info.signedness); | 4676 | wrap_cty = elem_cty.toSignedness(dest_info.signedness); |
| ... | @@ -4697,8 +4700,8 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca | ... | @@ -4697,8 +4700,8 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca |
| 4697 | try f.writeCValue(writer, local, .Other); | 4700 | try f.writeCValue(writer, local, .Other); |
| 4698 | if (dest_cty.castTag(.array)) |pl| { | 4701 | if (dest_cty.castTag(.array)) |pl| { |
| 4699 | try writer.print("[{d}]", .{switch (target.cpu.arch.endian()) { | 4702 | try writer.print("[{d}]", .{switch (target.cpu.arch.endian()) { |
| 4700 | .Little => pl.data.len - 1, | 4703 | .little => pl.data.len - 1, |
| 4701 | .Big => 0, | 4704 | .big => 0, |
| 4702 | }}); | 4705 | }}); |
| 4703 | } | 4706 | } |
| 4704 | if (need_bitcasts) try writer.writeByte(')'); | 4707 | if (need_bitcasts) try writer.writeByte(')'); |
| ... | @@ -7652,13 +7655,13 @@ fn formatIntLiteral( | ... | @@ -7652,13 +7655,13 @@ fn formatIntLiteral( |
| 7652 | else => .{ | 7655 | else => .{ |
| 7653 | .cty = CType.initTag(.void), | 7656 | .cty = CType.initTag(.void), |
| 7654 | .count = 1, | 7657 | .count = 1, |
| 7655 | .endian = .Little, | 7658 | .endian = .little, |
| 7656 | .homogeneous = true, | 7659 | .homogeneous = true, |
| 7657 | }, | 7660 | }, |
| 7658 | .zig_u128, .zig_i128 => .{ | 7661 | .zig_u128, .zig_i128 => .{ |
| 7659 | .cty = CType.initTag(.uint64_t), | 7662 | .cty = CType.initTag(.uint64_t), |
| 7660 | .count = 2, | 7663 | .count = 2, |
| 7661 | .endian = .Big, | 7664 | .endian = .big, |
| 7662 | .homogeneous = false, | 7665 | .homogeneous = false, |
| 7663 | }, | 7666 | }, |
| 7664 | .array => info: { | 7667 | .array => info: { |
| ... | @@ -7729,8 +7732,8 @@ fn formatIntLiteral( | ... | @@ -7729,8 +7732,8 @@ fn formatIntLiteral( |
| 7729 | const most_significant_limb_i = wrap.len - limbs_per_c_limb; | 7732 | const most_significant_limb_i = wrap.len - limbs_per_c_limb; |
| 7730 | while (limb_offset < wrap.len) : (limb_offset += limbs_per_c_limb) { | 7733 | while (limb_offset < wrap.len) : (limb_offset += limbs_per_c_limb) { |
| 7731 | const limb_i = switch (c_limb_info.endian) { | 7734 | const limb_i = switch (c_limb_info.endian) { |
| 7732 | .Little => limb_offset, | 7735 | .little => limb_offset, |
| 7733 | .Big => most_significant_limb_i - limb_offset, | 7736 | .big => most_significant_limb_i - limb_offset, |
| 7734 | }; | 7737 | }; |
| 7735 | var c_limb_mut = BigInt.Mutable{ | 7738 | var c_limb_mut = BigInt.Mutable{ |
| 7736 | .limbs = wrap.limbs[limb_i..][0..limbs_per_c_limb], | 7739 | .limbs = wrap.limbs[limb_i..][0..limbs_per_c_limb], |
src/codegen/llvm.zig+3-3| ... | @@ -342,8 +342,8 @@ const DataLayoutBuilder = struct { | ... | @@ -342,8 +342,8 @@ const DataLayoutBuilder = struct { |
| 342 | writer: anytype, | 342 | writer: anytype, |
| 343 | ) @TypeOf(writer).Error!void { | 343 | ) @TypeOf(writer).Error!void { |
| 344 | try writer.writeByte(switch (self.target.cpu.arch.endian()) { | 344 | try writer.writeByte(switch (self.target.cpu.arch.endian()) { |
| 345 | .Little => 'e', | 345 | .little => 'e', |
| 346 | .Big => 'E', | 346 | .big => 'E', |
| 347 | }); | 347 | }); |
| 348 | switch (self.target.cpu.arch) { | 348 | switch (self.target.cpu.arch) { |
| 349 | .amdgcn, | 349 | .amdgcn, |
| ... | @@ -10453,7 +10453,7 @@ pub const FuncGen = struct { | ... | @@ -10453,7 +10453,7 @@ pub const FuncGen = struct { |
| 10453 | else | 10453 | else |
| 10454 | payload_llvm_ty; | 10454 | payload_llvm_ty; |
| 10455 | const loaded = try fg.wip.load(access_kind, load_llvm_ty, payload_ptr, payload_alignment, ""); | 10455 | const loaded = try fg.wip.load(access_kind, load_llvm_ty, payload_ptr, payload_alignment, ""); |
| 10456 | const shifted = if (payload_llvm_ty != load_llvm_ty and o.target.cpu.arch.endian() == .Big) | 10456 | const shifted = if (payload_llvm_ty != load_llvm_ty and o.target.cpu.arch.endian() == .big) |
| 10457 | try fg.wip.bin(.lshr, loaded, try o.builder.intValue( | 10457 | try fg.wip.bin(.lshr, loaded, try o.builder.intValue( |
| 10458 | load_llvm_ty, | 10458 | load_llvm_ty, |
| 10459 | (payload_ty.abiSize(mod) - (std.math.divCeil(u64, payload_ty.bitSize(mod), 8) catch unreachable)) * 8, | 10459 | (payload_ty.abiSize(mod) - (std.math.divCeil(u64, payload_ty.bitSize(mod), 8) catch unreachable)) * 8, |
src/glibc.zig+5-5| ... | @@ -751,7 +751,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: *std.Progress.Node) !vo | ... | @@ -751,7 +751,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: *std.Progress.Node) !vo |
| 751 | 751 | ||
| 752 | var inc_i: usize = 0; | 752 | var inc_i: usize = 0; |
| 753 | 753 | ||
| 754 | const fn_inclusions_len = mem.readIntLittle(u16, metadata.inclusions[inc_i..][0..2]); | 754 | const fn_inclusions_len = mem.readInt(u16, metadata.inclusions[inc_i..][0..2], .little); |
| 755 | inc_i += 2; | 755 | inc_i += 2; |
| 756 | 756 | ||
| 757 | var sym_i: usize = 0; | 757 | var sym_i: usize = 0; |
| ... | @@ -768,7 +768,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: *std.Progress.Node) !vo | ... | @@ -768,7 +768,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: *std.Progress.Node) !vo |
| 768 | versions_len = 0; | 768 | versions_len = 0; |
| 769 | break :n name; | 769 | break :n name; |
| 770 | }; | 770 | }; |
| 771 | const targets = mem.readIntLittle(u32, metadata.inclusions[inc_i..][0..4]); | 771 | const targets = mem.readInt(u32, metadata.inclusions[inc_i..][0..4], .little); |
| 772 | inc_i += 4; | 772 | inc_i += 4; |
| 773 | 773 | ||
| 774 | const lib_index = metadata.inclusions[inc_i]; | 774 | const lib_index = metadata.inclusions[inc_i]; |
| ... | @@ -882,7 +882,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: *std.Progress.Node) !vo | ... | @@ -882,7 +882,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: *std.Progress.Node) !vo |
| 882 | 882 | ||
| 883 | try stubs_asm.appendSlice(".data\n"); | 883 | try stubs_asm.appendSlice(".data\n"); |
| 884 | 884 | ||
| 885 | const obj_inclusions_len = mem.readIntLittle(u16, metadata.inclusions[inc_i..][0..2]); | 885 | const obj_inclusions_len = mem.readInt(u16, metadata.inclusions[inc_i..][0..2], .little); |
| 886 | inc_i += 2; | 886 | inc_i += 2; |
| 887 | 887 | ||
| 888 | sym_i = 0; | 888 | sym_i = 0; |
| ... | @@ -899,10 +899,10 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: *std.Progress.Node) !vo | ... | @@ -899,10 +899,10 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: *std.Progress.Node) !vo |
| 899 | versions_len = 0; | 899 | versions_len = 0; |
| 900 | break :n name; | 900 | break :n name; |
| 901 | }; | 901 | }; |
| 902 | const targets = mem.readIntLittle(u32, metadata.inclusions[inc_i..][0..4]); | 902 | const targets = mem.readInt(u32, metadata.inclusions[inc_i..][0..4], .little); |
| 903 | inc_i += 4; | 903 | inc_i += 4; |
| 904 | 904 | ||
| 905 | const size = mem.readIntLittle(u16, metadata.inclusions[inc_i..][0..2]); | 905 | const size = mem.readInt(u16, metadata.inclusions[inc_i..][0..2], .little); |
| 906 | inc_i += 2; | 906 | inc_i += 2; |
| 907 | 907 | ||
| 908 | const lib_index = metadata.inclusions[inc_i]; | 908 | const lib_index = metadata.inclusions[inc_i]; |
src/link/Coff.zig+9-9| ... | @@ -856,12 +856,12 @@ fn writeOffsetTableEntry(self: *Coff, index: usize) !void { | ... | @@ -856,12 +856,12 @@ fn writeOffsetTableEntry(self: *Coff, index: usize) !void { |
| 856 | switch (self.ptr_width) { | 856 | switch (self.ptr_width) { |
| 857 | .p32 => { | 857 | .p32 => { |
| 858 | var buf: [4]u8 = undefined; | 858 | var buf: [4]u8 = undefined; |
| 859 | mem.writeIntLittle(u32, &buf, @as(u32, @intCast(entry_value + self.getImageBase()))); | 859 | mem.writeInt(u32, &buf, @as(u32, @intCast(entry_value + self.getImageBase())), .little); |
| 860 | try self.base.file.?.pwriteAll(&buf, file_offset); | 860 | try self.base.file.?.pwriteAll(&buf, file_offset); |
| 861 | }, | 861 | }, |
| 862 | .p64 => { | 862 | .p64 => { |
| 863 | var buf: [8]u8 = undefined; | 863 | var buf: [8]u8 = undefined; |
| 864 | mem.writeIntLittle(u64, &buf, entry_value + self.getImageBase()); | 864 | mem.writeInt(u64, &buf, entry_value + self.getImageBase(), .little); |
| 865 | try self.base.file.?.pwriteAll(&buf, file_offset); | 865 | try self.base.file.?.pwriteAll(&buf, file_offset); |
| 866 | }, | 866 | }, |
| 867 | } | 867 | } |
| ... | @@ -889,14 +889,14 @@ fn writeOffsetTableEntry(self: *Coff, index: usize) !void { | ... | @@ -889,14 +889,14 @@ fn writeOffsetTableEntry(self: *Coff, index: usize) !void { |
| 889 | switch (self.ptr_width) { | 889 | switch (self.ptr_width) { |
| 890 | .p32 => { | 890 | .p32 => { |
| 891 | var buf: [4]u8 = undefined; | 891 | var buf: [4]u8 = undefined; |
| 892 | mem.writeIntLittle(u32, &buf, @as(u32, @intCast(entry_value + slide))); | 892 | mem.writeInt(u32, &buf, @as(u32, @intCast(entry_value + slide)), .little); |
| 893 | writeMem(handle, pvaddr, &buf) catch |err| { | 893 | writeMem(handle, pvaddr, &buf) catch |err| { |
| 894 | log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)}); | 894 | log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)}); |
| 895 | }; | 895 | }; |
| 896 | }, | 896 | }, |
| 897 | .p64 => { | 897 | .p64 => { |
| 898 | var buf: [8]u8 = undefined; | 898 | var buf: [8]u8 = undefined; |
| 899 | mem.writeIntLittle(u64, &buf, entry_value + slide); | 899 | mem.writeInt(u64, &buf, entry_value + slide, .little); |
| 900 | writeMem(handle, pvaddr, &buf) catch |err| { | 900 | writeMem(handle, pvaddr, &buf) catch |err| { |
| 901 | log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)}); | 901 | log.warn("writing to protected memory failed with error: {s}", .{@errorName(err)}); |
| 902 | }; | 902 | }; |
| ... | @@ -2076,7 +2076,7 @@ fn writeImportTables(self: *Coff) !void { | ... | @@ -2076,7 +2076,7 @@ fn writeImportTables(self: *Coff) !void { |
| 2076 | lookup_table_offset += lookup_entry_size; | 2076 | lookup_table_offset += lookup_entry_size; |
| 2077 | 2077 | ||
| 2078 | // Names table entry | 2078 | // Names table entry |
| 2079 | mem.writeIntLittle(u16, buffer.items[names_table_offset..][0..2], 0); // Hint set to 0 until we learn how to parse DLLs | 2079 | mem.writeInt(u16, buffer.items[names_table_offset..][0..2], 0, .little); // Hint set to 0 until we learn how to parse DLLs |
| 2080 | names_table_offset += 2; | 2080 | names_table_offset += 2; |
| 2081 | @memcpy(buffer.items[names_table_offset..][0..import_name.len], import_name); | 2081 | @memcpy(buffer.items[names_table_offset..][0..import_name.len], import_name); |
| 2082 | names_table_offset += @as(u32, @intCast(import_name.len)); | 2082 | names_table_offset += @as(u32, @intCast(import_name.len)); |
| ... | @@ -2089,7 +2089,7 @@ fn writeImportTables(self: *Coff) !void { | ... | @@ -2089,7 +2089,7 @@ fn writeImportTables(self: *Coff) !void { |
| 2089 | } | 2089 | } |
| 2090 | 2090 | ||
| 2091 | // IAT sentinel | 2091 | // IAT sentinel |
| 2092 | mem.writeIntLittle(u64, buffer.items[iat_offset..][0..lookup_entry_size], 0); | 2092 | mem.writeInt(u64, buffer.items[iat_offset..][0..lookup_entry_size], 0, .little); |
| 2093 | iat_offset += 8; | 2093 | iat_offset += 8; |
| 2094 | 2094 | ||
| 2095 | // Lookup table sentinel | 2095 | // Lookup table sentinel |
| ... | @@ -2157,7 +2157,7 @@ fn writeStrtab(self: *Coff) !void { | ... | @@ -2157,7 +2157,7 @@ fn writeStrtab(self: *Coff) !void { |
| 2157 | buffer.appendSliceAssumeCapacity(self.strtab.items()); | 2157 | buffer.appendSliceAssumeCapacity(self.strtab.items()); |
| 2158 | // Here, we do a trick in that we do not commit the size of the strtab to strtab buffer, instead | 2158 | // Here, we do a trick in that we do not commit the size of the strtab to strtab buffer, instead |
| 2159 | // we write the length of the strtab to a temporary buffer that goes to file. | 2159 | // we write the length of the strtab to a temporary buffer that goes to file. |
| 2160 | mem.writeIntLittle(u32, buffer.items[0..4], @as(u32, @intCast(self.strtab.len()))); | 2160 | mem.writeInt(u32, buffer.items[0..4], @as(u32, @intCast(self.strtab.len())), .little); |
| 2161 | 2161 | ||
| 2162 | try self.base.file.?.pwriteAll(buffer.items, self.strtab_offset.?); | 2162 | try self.base.file.?.pwriteAll(buffer.items, self.strtab_offset.?); |
| 2163 | } | 2163 | } |
| ... | @@ -2180,7 +2180,7 @@ fn writeHeader(self: *Coff) !void { | ... | @@ -2180,7 +2180,7 @@ fn writeHeader(self: *Coff) !void { |
| 2180 | 2180 | ||
| 2181 | try buffer.ensureTotalCapacity(self.getSizeOfHeaders()); | 2181 | try buffer.ensureTotalCapacity(self.getSizeOfHeaders()); |
| 2182 | writer.writeAll(msdos_stub) catch unreachable; | 2182 | writer.writeAll(msdos_stub) catch unreachable; |
| 2183 | mem.writeIntLittle(u32, buffer.items[0x3c..][0..4], msdos_stub.len); | 2183 | mem.writeInt(u32, buffer.items[0x3c..][0..4], msdos_stub.len, .little); |
| 2184 | 2184 | ||
| 2185 | writer.writeAll("PE\x00\x00") catch unreachable; | 2185 | writer.writeAll("PE\x00\x00") catch unreachable; |
| 2186 | var flags = coff.CoffHeaderFlags{ | 2186 | var flags = coff.CoffHeaderFlags{ |
| ... | @@ -2548,7 +2548,7 @@ fn setSymbolName(self: *Coff, symbol: *coff.Symbol, name: []const u8) !void { | ... | @@ -2548,7 +2548,7 @@ fn setSymbolName(self: *Coff, symbol: *coff.Symbol, name: []const u8) !void { |
| 2548 | } | 2548 | } |
| 2549 | const offset = try self.strtab.insert(self.base.allocator, name); | 2549 | const offset = try self.strtab.insert(self.base.allocator, name); |
| 2550 | @memset(symbol.name[0..4], 0); | 2550 | @memset(symbol.name[0..4], 0); |
| 2551 | mem.writeIntLittle(u32, symbol.name[4..8], offset); | 2551 | mem.writeInt(u32, symbol.name[4..8], offset, .little); |
| 2552 | } | 2552 | } |
| 2553 | 2553 | ||
| 2554 | fn logSymAttributes(sym: *const coff.Symbol, buf: *[4]u8) []const u8 { | 2554 | fn logSymAttributes(sym: *const coff.Symbol, buf: *[4]u8) []const u8 { |
src/link/Coff/Relocation.zig+11-10| ... | @@ -137,7 +137,7 @@ fn resolveAarch64(self: Relocation, ctx: Context) void { | ... | @@ -137,7 +137,7 @@ fn resolveAarch64(self: Relocation, ctx: Context) void { |
| 137 | }; | 137 | }; |
| 138 | inst.pc_relative_address.immhi = @as(u19, @truncate(pages >> 2)); | 138 | inst.pc_relative_address.immhi = @as(u19, @truncate(pages >> 2)); |
| 139 | inst.pc_relative_address.immlo = @as(u2, @truncate(pages)); | 139 | inst.pc_relative_address.immlo = @as(u2, @truncate(pages)); |
| 140 | mem.writeIntLittle(u32, buffer[0..4], inst.toU32()); | 140 | mem.writeInt(u32, buffer[0..4], inst.toU32(), .little); |
| 141 | }, | 141 | }, |
| 142 | .got_pageoff, .import_pageoff, .pageoff => { | 142 | .got_pageoff, .import_pageoff, .pageoff => { |
| 143 | assert(!self.pcrel); | 143 | assert(!self.pcrel); |
| ... | @@ -151,7 +151,7 @@ fn resolveAarch64(self: Relocation, ctx: Context) void { | ... | @@ -151,7 +151,7 @@ fn resolveAarch64(self: Relocation, ctx: Context) void { |
| 151 | ), buffer[0..4]), | 151 | ), buffer[0..4]), |
| 152 | }; | 152 | }; |
| 153 | inst.add_subtract_immediate.imm12 = narrowed; | 153 | inst.add_subtract_immediate.imm12 = narrowed; |
| 154 | mem.writeIntLittle(u32, buffer[0..4], inst.toU32()); | 154 | mem.writeInt(u32, buffer[0..4], inst.toU32(), .little); |
| 155 | } else { | 155 | } else { |
| 156 | var inst = aarch64.Instruction{ | 156 | var inst = aarch64.Instruction{ |
| 157 | .load_store_register = mem.bytesToValue(meta.TagPayload( | 157 | .load_store_register = mem.bytesToValue(meta.TagPayload( |
| ... | @@ -173,18 +173,19 @@ fn resolveAarch64(self: Relocation, ctx: Context) void { | ... | @@ -173,18 +173,19 @@ fn resolveAarch64(self: Relocation, ctx: Context) void { |
| 173 | } | 173 | } |
| 174 | }; | 174 | }; |
| 175 | inst.load_store_register.offset = offset; | 175 | inst.load_store_register.offset = offset; |
| 176 | mem.writeIntLittle(u32, buffer[0..4], inst.toU32()); | 176 | mem.writeInt(u32, buffer[0..4], inst.toU32(), .little); |
| 177 | } | 177 | } |
| 178 | }, | 178 | }, |
| 179 | .direct => { | 179 | .direct => { |
| 180 | assert(!self.pcrel); | 180 | assert(!self.pcrel); |
| 181 | switch (self.length) { | 181 | switch (self.length) { |
| 182 | 2 => mem.writeIntLittle( | 182 | 2 => mem.writeInt( |
| 183 | u32, | 183 | u32, |
| 184 | buffer[0..4], | 184 | buffer[0..4], |
| 185 | @as(u32, @truncate(ctx.target_vaddr + ctx.image_base)), | 185 | @as(u32, @truncate(ctx.target_vaddr + ctx.image_base)), |
| 186 | .little, | ||
| 186 | ), | 187 | ), |
| 187 | 3 => mem.writeIntLittle(u64, buffer[0..8], ctx.target_vaddr + ctx.image_base), | 188 | 3 => mem.writeInt(u64, buffer[0..8], ctx.target_vaddr + ctx.image_base, .little), |
| 188 | else => unreachable, | 189 | else => unreachable, |
| 189 | } | 190 | } |
| 190 | }, | 191 | }, |
| ... | @@ -207,17 +208,17 @@ fn resolveX86(self: Relocation, ctx: Context) void { | ... | @@ -207,17 +208,17 @@ fn resolveX86(self: Relocation, ctx: Context) void { |
| 207 | .got, .import => { | 208 | .got, .import => { |
| 208 | assert(self.pcrel); | 209 | assert(self.pcrel); |
| 209 | const disp = @as(i32, @intCast(ctx.target_vaddr)) - @as(i32, @intCast(ctx.source_vaddr)) - 4; | 210 | const disp = @as(i32, @intCast(ctx.target_vaddr)) - @as(i32, @intCast(ctx.source_vaddr)) - 4; |
| 210 | mem.writeIntLittle(i32, buffer[0..4], disp); | 211 | mem.writeInt(i32, buffer[0..4], disp, .little); |
| 211 | }, | 212 | }, |
| 212 | .direct => { | 213 | .direct => { |
| 213 | if (self.pcrel) { | 214 | if (self.pcrel) { |
| 214 | const disp = @as(i32, @intCast(ctx.target_vaddr)) - @as(i32, @intCast(ctx.source_vaddr)) - 4; | 215 | const disp = @as(i32, @intCast(ctx.target_vaddr)) - @as(i32, @intCast(ctx.source_vaddr)) - 4; |
| 215 | mem.writeIntLittle(i32, buffer[0..4], disp); | 216 | mem.writeInt(i32, buffer[0..4], disp, .little); |
| 216 | } else switch (ctx.ptr_width) { | 217 | } else switch (ctx.ptr_width) { |
| 217 | .p32 => mem.writeIntLittle(u32, buffer[0..4], @as(u32, @intCast(ctx.target_vaddr + ctx.image_base))), | 218 | .p32 => mem.writeInt(u32, buffer[0..4], @as(u32, @intCast(ctx.target_vaddr + ctx.image_base)), .little), |
| 218 | .p64 => switch (self.length) { | 219 | .p64 => switch (self.length) { |
| 219 | 2 => mem.writeIntLittle(u32, buffer[0..4], @as(u32, @truncate(ctx.target_vaddr + ctx.image_base))), | 220 | 2 => mem.writeInt(u32, buffer[0..4], @as(u32, @truncate(ctx.target_vaddr + ctx.image_base)), .little), |
| 220 | 3 => mem.writeIntLittle(u64, buffer[0..8], ctx.target_vaddr + ctx.image_base), | 221 | 3 => mem.writeInt(u64, buffer[0..8], ctx.target_vaddr + ctx.image_base, .little), |
| 221 | else => unreachable, | 222 | else => unreachable, |
| 222 | }, | 223 | }, |
| 223 | } | 224 | } |
src/link/Elf.zig+2-2| ... | @@ -2719,8 +2719,8 @@ fn writeHeader(self: *Elf) !void { | ... | @@ -2719,8 +2719,8 @@ fn writeHeader(self: *Elf) !void { |
| 2719 | 2719 | ||
| 2720 | const endian = self.base.options.target.cpu.arch.endian(); | 2720 | const endian = self.base.options.target.cpu.arch.endian(); |
| 2721 | hdr_buf[index] = switch (endian) { | 2721 | hdr_buf[index] = switch (endian) { |
| 2722 | .Little => elf.ELFDATA2LSB, | 2722 | .little => elf.ELFDATA2LSB, |
| 2723 | .Big => elf.ELFDATA2MSB, | 2723 | .big => elf.ELFDATA2MSB, |
| 2724 | }; | 2724 | }; |
| 2725 | index += 1; | 2725 | index += 1; |
| 2726 | 2726 |
src/link/Elf/Atom.zig+42-42| ... | @@ -786,43 +786,43 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void { | ... | @@ -786,43 +786,43 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void { |
| 786 | 786 | ||
| 787 | elf.R_X86_64_PLT32, | 787 | elf.R_X86_64_PLT32, |
| 788 | elf.R_X86_64_PC32, | 788 | elf.R_X86_64_PC32, |
| 789 | => try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P))), | 789 | => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little), |
| 790 | 790 | ||
| 791 | elf.R_X86_64_GOTPCREL => try cwriter.writeIntLittle(i32, @as(i32, @intCast(G + GOT + A - P))), | 791 | elf.R_X86_64_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little), |
| 792 | elf.R_X86_64_GOTPC32 => try cwriter.writeIntLittle(i32, @as(i32, @intCast(GOT + A - P))), | 792 | elf.R_X86_64_GOTPC32 => try cwriter.writeInt(i32, @as(i32, @intCast(GOT + A - P)), .little), |
| 793 | elf.R_X86_64_GOTPC64 => try cwriter.writeIntLittle(i64, GOT + A - P), | 793 | elf.R_X86_64_GOTPC64 => try cwriter.writeInt(i64, GOT + A - P, .little), |
| 794 | 794 | ||
| 795 | elf.R_X86_64_GOTPCRELX => { | 795 | elf.R_X86_64_GOTPCRELX => { |
| 796 | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { | 796 | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { |
| 797 | x86_64.relaxGotpcrelx(code[r_offset - 2 ..]) catch break :blk; | 797 | x86_64.relaxGotpcrelx(code[r_offset - 2 ..]) catch break :blk; |
| 798 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P))); | 798 | try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little); |
| 799 | continue; | 799 | continue; |
| 800 | } | 800 | } |
| 801 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(G + GOT + A - P))); | 801 | try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little); |
| 802 | }, | 802 | }, |
| 803 | 803 | ||
| 804 | elf.R_X86_64_REX_GOTPCRELX => { | 804 | elf.R_X86_64_REX_GOTPCRELX => { |
| 805 | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { | 805 | if (!target.flags.import and !target.isIFunc(elf_file) and !target.isAbs(elf_file)) blk: { |
| 806 | x86_64.relaxRexGotpcrelx(code[r_offset - 3 ..]) catch break :blk; | 806 | x86_64.relaxRexGotpcrelx(code[r_offset - 3 ..]) catch break :blk; |
| 807 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - P))); | 807 | try cwriter.writeInt(i32, @as(i32, @intCast(S + A - P)), .little); |
| 808 | continue; | 808 | continue; |
| 809 | } | 809 | } |
| 810 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(G + GOT + A - P))); | 810 | try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A - P)), .little); |
| 811 | }, | 811 | }, |
| 812 | 812 | ||
| 813 | elf.R_X86_64_32 => try cwriter.writeIntLittle(u32, @as(u32, @truncate(@as(u64, @intCast(S + A))))), | 813 | elf.R_X86_64_32 => try cwriter.writeInt(u32, @as(u32, @truncate(@as(u64, @intCast(S + A)))), .little), |
| 814 | elf.R_X86_64_32S => try cwriter.writeIntLittle(i32, @as(i32, @truncate(S + A))), | 814 | elf.R_X86_64_32S => try cwriter.writeInt(i32, @as(i32, @truncate(S + A)), .little), |
| 815 | 815 | ||
| 816 | elf.R_X86_64_TPOFF32 => try cwriter.writeIntLittle(i32, @as(i32, @truncate(S + A - TP))), | 816 | elf.R_X86_64_TPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - TP)), .little), |
| 817 | elf.R_X86_64_TPOFF64 => try cwriter.writeIntLittle(i64, S + A - TP), | 817 | elf.R_X86_64_TPOFF64 => try cwriter.writeInt(i64, S + A - TP, .little), |
| 818 | 818 | ||
| 819 | elf.R_X86_64_DTPOFF32 => try cwriter.writeIntLittle(i32, @as(i32, @truncate(S + A - DTP))), | 819 | elf.R_X86_64_DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @truncate(S + A - DTP)), .little), |
| 820 | elf.R_X86_64_DTPOFF64 => try cwriter.writeIntLittle(i64, S + A - DTP), | 820 | elf.R_X86_64_DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little), |
| 821 | 821 | ||
| 822 | elf.R_X86_64_TLSGD => { | 822 | elf.R_X86_64_TLSGD => { |
| 823 | if (target.flags.has_tlsgd) { | 823 | if (target.flags.has_tlsgd) { |
| 824 | const S_ = @as(i64, @intCast(target.tlsGdAddress(elf_file))); | 824 | const S_ = @as(i64, @intCast(target.tlsGdAddress(elf_file))); |
| 825 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S_ + A - P))); | 825 | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 826 | } else if (target.flags.has_gottp) { | 826 | } else if (target.flags.has_gottp) { |
| 827 | const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file))); | 827 | const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file))); |
| 828 | try x86_64.relaxTlsGdToIe(self, rels[i .. i + 2], @intCast(S_ - P), elf_file, &stream); | 828 | try x86_64.relaxTlsGdToIe(self, rels[i .. i + 2], @intCast(S_ - P), elf_file, &stream); |
| ... | @@ -843,7 +843,7 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void { | ... | @@ -843,7 +843,7 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void { |
| 843 | if (elf_file.got.tlsld_index) |entry_index| { | 843 | if (elf_file.got.tlsld_index) |entry_index| { |
| 844 | const tlsld_entry = elf_file.got.entries.items[entry_index]; | 844 | const tlsld_entry = elf_file.got.entries.items[entry_index]; |
| 845 | const S_ = @as(i64, @intCast(tlsld_entry.address(elf_file))); | 845 | const S_ = @as(i64, @intCast(tlsld_entry.address(elf_file))); |
| 846 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S_ + A - P))); | 846 | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 847 | } else { | 847 | } else { |
| 848 | try x86_64.relaxTlsLdToLe( | 848 | try x86_64.relaxTlsLdToLe( |
| 849 | self, | 849 | self, |
| ... | @@ -859,10 +859,10 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void { | ... | @@ -859,10 +859,10 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void { |
| 859 | elf.R_X86_64_GOTPC32_TLSDESC => { | 859 | elf.R_X86_64_GOTPC32_TLSDESC => { |
| 860 | if (target.flags.has_tlsdesc) { | 860 | if (target.flags.has_tlsdesc) { |
| 861 | const S_ = @as(i64, @intCast(target.tlsDescAddress(elf_file))); | 861 | const S_ = @as(i64, @intCast(target.tlsDescAddress(elf_file))); |
| 862 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S_ + A - P))); | 862 | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 863 | } else { | 863 | } else { |
| 864 | try x86_64.relaxGotPcTlsDesc(code[r_offset - 3 ..]); | 864 | try x86_64.relaxGotPcTlsDesc(code[r_offset - 3 ..]); |
| 865 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S - TP))); | 865 | try cwriter.writeInt(i32, @as(i32, @intCast(S - TP)), .little); |
| 866 | } | 866 | } |
| 867 | }, | 867 | }, |
| 868 | 868 | ||
| ... | @@ -874,18 +874,18 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void { | ... | @@ -874,18 +874,18 @@ pub fn resolveRelocsAlloc(self: Atom, elf_file: *Elf, code: []u8) !void { |
| 874 | elf.R_X86_64_GOTTPOFF => { | 874 | elf.R_X86_64_GOTTPOFF => { |
| 875 | if (target.flags.has_gottp) { | 875 | if (target.flags.has_gottp) { |
| 876 | const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file))); | 876 | const S_ = @as(i64, @intCast(target.gotTpAddress(elf_file))); |
| 877 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S_ + A - P))); | 877 | try cwriter.writeInt(i32, @as(i32, @intCast(S_ + A - P)), .little); |
| 878 | } else { | 878 | } else { |
| 879 | x86_64.relaxGotTpOff(code[r_offset - 3 ..]) catch unreachable; | 879 | x86_64.relaxGotTpOff(code[r_offset - 3 ..]) catch unreachable; |
| 880 | try cwriter.writeIntLittle(i32, @as(i32, @intCast(S - TP))); | 880 | try cwriter.writeInt(i32, @as(i32, @intCast(S - TP)), .little); |
| 881 | } | 881 | } |
| 882 | }, | 882 | }, |
| 883 | 883 | ||
| 884 | elf.R_X86_64_GOT32 => try cwriter.writeIntLittle(i32, @as(i32, @intCast(G + GOT + A))), | 884 | elf.R_X86_64_GOT32 => try cwriter.writeInt(i32, @as(i32, @intCast(G + GOT + A)), .little), |
| 885 | 885 | ||
| 886 | // Zig custom relocations | 886 | // Zig custom relocations |
| 887 | Elf.R_X86_64_ZIG_GOT32 => try cwriter.writeIntLittle(u32, @as(u32, @intCast(ZIG_GOT + A))), | 887 | Elf.R_X86_64_ZIG_GOT32 => try cwriter.writeInt(u32, @as(u32, @intCast(ZIG_GOT + A)), .little), |
| 888 | Elf.R_X86_64_ZIG_GOTPCREL => try cwriter.writeIntLittle(i32, @as(i32, @intCast(ZIG_GOT + A - P))), | 888 | Elf.R_X86_64_ZIG_GOTPCREL => try cwriter.writeInt(i32, @as(i32, @intCast(ZIG_GOT + A - P)), .little), |
| 889 | 889 | ||
| 890 | else => {}, | 890 | else => {}, |
| 891 | } | 891 | } |
| ... | @@ -920,7 +920,7 @@ fn resolveDynAbsReloc( | ... | @@ -920,7 +920,7 @@ fn resolveDynAbsReloc( |
| 920 | .copyrel, | 920 | .copyrel, |
| 921 | .cplt, | 921 | .cplt, |
| 922 | .none, | 922 | .none, |
| 923 | => try writer.writeIntLittle(i32, @as(i32, @truncate(S + A))), | 923 | => try writer.writeInt(i32, @as(i32, @truncate(S + A)), .little), |
| 924 | 924 | ||
| 925 | .dyn_copyrel => { | 925 | .dyn_copyrel => { |
| 926 | if (is_writeable or elf_file.base.options.z_nocopyreloc) { | 926 | if (is_writeable or elf_file.base.options.z_nocopyreloc) { |
| ... | @@ -932,7 +932,7 @@ fn resolveDynAbsReloc( | ... | @@ -932,7 +932,7 @@ fn resolveDynAbsReloc( |
| 932 | }); | 932 | }); |
| 933 | try applyDynamicReloc(A, elf_file, writer); | 933 | try applyDynamicReloc(A, elf_file, writer); |
| 934 | } else { | 934 | } else { |
| 935 | try writer.writeIntLittle(i32, @as(i32, @truncate(S + A))); | 935 | try writer.writeInt(i32, @as(i32, @truncate(S + A)), .little); |
| 936 | } | 936 | } |
| 937 | }, | 937 | }, |
| 938 | 938 | ||
| ... | @@ -946,7 +946,7 @@ fn resolveDynAbsReloc( | ... | @@ -946,7 +946,7 @@ fn resolveDynAbsReloc( |
| 946 | }); | 946 | }); |
| 947 | try applyDynamicReloc(A, elf_file, writer); | 947 | try applyDynamicReloc(A, elf_file, writer); |
| 948 | } else { | 948 | } else { |
| 949 | try writer.writeIntLittle(i32, @as(i32, @truncate(S + A))); | 949 | try writer.writeInt(i32, @as(i32, @truncate(S + A)), .little); |
| 950 | } | 950 | } |
| 951 | }, | 951 | }, |
| 952 | 952 | ||
| ... | @@ -984,7 +984,7 @@ fn resolveDynAbsReloc( | ... | @@ -984,7 +984,7 @@ fn resolveDynAbsReloc( |
| 984 | fn applyDynamicReloc(value: i64, elf_file: *Elf, writer: anytype) !void { | 984 | fn applyDynamicReloc(value: i64, elf_file: *Elf, writer: anytype) !void { |
| 985 | _ = elf_file; | 985 | _ = elf_file; |
| 986 | // if (elf_file.options.apply_dynamic_relocs) { | 986 | // if (elf_file.options.apply_dynamic_relocs) { |
| 987 | try writer.writeIntLittle(i64, value); | 987 | try writer.writeInt(i64, value, .little); |
| 988 | // } | 988 | // } |
| 989 | } | 989 | } |
| 990 | 990 | ||
| ... | @@ -1058,22 +1058,22 @@ pub fn resolveRelocsNonAlloc(self: Atom, elf_file: *Elf, code: []u8, undefs: any | ... | @@ -1058,22 +1058,22 @@ pub fn resolveRelocsNonAlloc(self: Atom, elf_file: *Elf, code: []u8, undefs: any |
| 1058 | 1058 | ||
| 1059 | switch (r_type) { | 1059 | switch (r_type) { |
| 1060 | elf.R_X86_64_NONE => unreachable, | 1060 | elf.R_X86_64_NONE => unreachable, |
| 1061 | elf.R_X86_64_8 => try cwriter.writeIntLittle(u8, @as(u8, @bitCast(@as(i8, @intCast(S + A))))), | 1061 | elf.R_X86_64_8 => try cwriter.writeInt(u8, @as(u8, @bitCast(@as(i8, @intCast(S + A)))), .little), |
| 1062 | elf.R_X86_64_16 => try cwriter.writeIntLittle(u16, @as(u16, @bitCast(@as(i16, @intCast(S + A))))), | 1062 | elf.R_X86_64_16 => try cwriter.writeInt(u16, @as(u16, @bitCast(@as(i16, @intCast(S + A)))), .little), |
| 1063 | elf.R_X86_64_32 => try cwriter.writeIntLittle(u32, @as(u32, @bitCast(@as(i32, @intCast(S + A))))), | 1063 | elf.R_X86_64_32 => try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(S + A)))), .little), |
| 1064 | elf.R_X86_64_32S => try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A))), | 1064 | elf.R_X86_64_32S => try cwriter.writeInt(i32, @as(i32, @intCast(S + A)), .little), |
| 1065 | elf.R_X86_64_64 => try cwriter.writeIntLittle(i64, S + A), | 1065 | elf.R_X86_64_64 => try cwriter.writeInt(i64, S + A, .little), |
| 1066 | elf.R_X86_64_DTPOFF32 => try cwriter.writeIntLittle(i32, @as(i32, @intCast(S + A - DTP))), | 1066 | elf.R_X86_64_DTPOFF32 => try cwriter.writeInt(i32, @as(i32, @intCast(S + A - DTP)), .little), |
| 1067 | elf.R_X86_64_DTPOFF64 => try cwriter.writeIntLittle(i64, S + A - DTP), | 1067 | elf.R_X86_64_DTPOFF64 => try cwriter.writeInt(i64, S + A - DTP, .little), |
| 1068 | elf.R_X86_64_GOTOFF64 => try cwriter.writeIntLittle(i64, S + A - GOT), | 1068 | elf.R_X86_64_GOTOFF64 => try cwriter.writeInt(i64, S + A - GOT, .little), |
| 1069 | elf.R_X86_64_GOTPC64 => try cwriter.writeIntLittle(i64, GOT + A), | 1069 | elf.R_X86_64_GOTPC64 => try cwriter.writeInt(i64, GOT + A, .little), |
| 1070 | elf.R_X86_64_SIZE32 => { | 1070 | elf.R_X86_64_SIZE32 => { |
| 1071 | const size = @as(i64, @intCast(target.elfSym(elf_file).st_size)); | 1071 | const size = @as(i64, @intCast(target.elfSym(elf_file).st_size)); |
| 1072 | try cwriter.writeIntLittle(u32, @as(u32, @bitCast(@as(i32, @intCast(size + A))))); | 1072 | try cwriter.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(size + A)))), .little); |
| 1073 | }, | 1073 | }, |
| 1074 | elf.R_X86_64_SIZE64 => { | 1074 | elf.R_X86_64_SIZE64 => { |
| 1075 | const size = @as(i64, @intCast(target.elfSym(elf_file).st_size)); | 1075 | const size = @as(i64, @intCast(target.elfSym(elf_file).st_size)); |
| 1076 | try cwriter.writeIntLittle(i64, @as(i64, @intCast(size + A))); | 1076 | try cwriter.writeInt(i64, @as(i64, @intCast(size + A)), .little); |
| 1077 | }, | 1077 | }, |
| 1078 | else => try self.reportUnhandledRelocError(rel, elf_file), | 1078 | else => try self.reportUnhandledRelocError(rel, elf_file), |
| 1079 | } | 1079 | } |
| ... | @@ -1254,7 +1254,7 @@ const x86_64 = struct { | ... | @@ -1254,7 +1254,7 @@ const x86_64 = struct { |
| 1254 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // movq %fs:0,%rax | 1254 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // movq %fs:0,%rax |
| 1255 | 0x48, 0x03, 0x05, 0, 0, 0, 0, // add foo@gottpoff(%rip), %rax | 1255 | 0x48, 0x03, 0x05, 0, 0, 0, 0, // add foo@gottpoff(%rip), %rax |
| 1256 | }; | 1256 | }; |
| 1257 | std.mem.writeIntLittle(i32, insts[12..][0..4], value - 12); | 1257 | std.mem.writeInt(i32, insts[12..][0..4], value - 12, .little); |
| 1258 | try stream.seekBy(-4); | 1258 | try stream.seekBy(-4); |
| 1259 | try writer.writeAll(&insts); | 1259 | try writer.writeAll(&insts); |
| 1260 | }, | 1260 | }, |
| ... | @@ -1292,7 +1292,7 @@ const x86_64 = struct { | ... | @@ -1292,7 +1292,7 @@ const x86_64 = struct { |
| 1292 | 0x64, 0x48, 0x8b, 0, // mov %fs:(%rax), %rax | 1292 | 0x64, 0x48, 0x8b, 0, // mov %fs:(%rax), %rax |
| 1293 | 0x48, 0x2d, 0, 0, 0, 0, // sub $tls_size, %rax | 1293 | 0x48, 0x2d, 0, 0, 0, 0, // sub $tls_size, %rax |
| 1294 | }; | 1294 | }; |
| 1295 | std.mem.writeIntLittle(i32, insts[8..][0..4], value); | 1295 | std.mem.writeInt(i32, insts[8..][0..4], value, .little); |
| 1296 | try stream.seekBy(-3); | 1296 | try stream.seekBy(-3); |
| 1297 | try writer.writeAll(&insts); | 1297 | try writer.writeAll(&insts); |
| 1298 | }, | 1298 | }, |
| ... | @@ -1306,7 +1306,7 @@ const x86_64 = struct { | ... | @@ -1306,7 +1306,7 @@ const x86_64 = struct { |
| 1306 | 0x48, 0x2d, 0, 0, 0, 0, // sub $tls_size, %rax | 1306 | 0x48, 0x2d, 0, 0, 0, 0, // sub $tls_size, %rax |
| 1307 | 0x90, // nop | 1307 | 0x90, // nop |
| 1308 | }; | 1308 | }; |
| 1309 | std.mem.writeIntLittle(i32, insts[8..][0..4], value); | 1309 | std.mem.writeInt(i32, insts[8..][0..4], value, .little); |
| 1310 | try stream.seekBy(-3); | 1310 | try stream.seekBy(-3); |
| 1311 | try writer.writeAll(&insts); | 1311 | try writer.writeAll(&insts); |
| 1312 | }, | 1312 | }, |
| ... | @@ -1392,7 +1392,7 @@ const x86_64 = struct { | ... | @@ -1392,7 +1392,7 @@ const x86_64 = struct { |
| 1392 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // movq %fs:0,%rax | 1392 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // movq %fs:0,%rax |
| 1393 | 0x48, 0x81, 0xc0, 0, 0, 0, 0, // add $tp_offset, %rax | 1393 | 0x48, 0x81, 0xc0, 0, 0, 0, 0, // add $tp_offset, %rax |
| 1394 | }; | 1394 | }; |
| 1395 | std.mem.writeIntLittle(i32, insts[12..][0..4], value); | 1395 | std.mem.writeInt(i32, insts[12..][0..4], value, .little); |
| 1396 | try stream.seekBy(-4); | 1396 | try stream.seekBy(-4); |
| 1397 | try writer.writeAll(&insts); | 1397 | try writer.writeAll(&insts); |
| 1398 | relocs_log.debug(" relaxing {} and {}", .{ | 1398 | relocs_log.debug(" relaxing {} and {}", .{ |
src/link/Elf/eh_frame.zig+15-13| ... | @@ -33,7 +33,7 @@ pub const Fde = struct { | ... | @@ -33,7 +33,7 @@ pub const Fde = struct { |
| 33 | 33 | ||
| 34 | pub fn ciePointer(fde: Fde, elf_file: *Elf) u32 { | 34 | pub fn ciePointer(fde: Fde, elf_file: *Elf) u32 { |
| 35 | const fde_data = fde.data(elf_file); | 35 | const fde_data = fde.data(elf_file); |
| 36 | return std.mem.readIntLittle(u32, fde_data[4..8]); | 36 | return std.mem.readInt(u32, fde_data[4..8], .little); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | pub fn calcSize(fde: Fde) usize { | 39 | pub fn calcSize(fde: Fde) usize { |
| ... | @@ -217,10 +217,10 @@ pub const Iterator = struct { | ... | @@ -217,10 +217,10 @@ pub const Iterator = struct { |
| 217 | var stream = std.io.fixedBufferStream(it.data[it.pos..]); | 217 | var stream = std.io.fixedBufferStream(it.data[it.pos..]); |
| 218 | const reader = stream.reader(); | 218 | const reader = stream.reader(); |
| 219 | 219 | ||
| 220 | var size = try reader.readIntLittle(u32); | 220 | var size = try reader.readInt(u32, .little); |
| 221 | if (size == 0xFFFFFFFF) @panic("TODO"); | 221 | if (size == 0xFFFFFFFF) @panic("TODO"); |
| 222 | 222 | ||
| 223 | const id = try reader.readIntLittle(u32); | 223 | const id = try reader.readInt(u32, .little); |
| 224 | const record = Record{ | 224 | const record = Record{ |
| 225 | .tag = if (id == 0) .cie else .fde, | 225 | .tag = if (id == 0) .cie else .fde, |
| 226 | .offset = it.pos, | 226 | .offset = it.pos, |
| ... | @@ -298,10 +298,10 @@ fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file: | ... | @@ -298,10 +298,10 @@ fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file: |
| 298 | 298 | ||
| 299 | var where = contents[offset..]; | 299 | var where = contents[offset..]; |
| 300 | switch (rel.r_type()) { | 300 | switch (rel.r_type()) { |
| 301 | elf.R_X86_64_32 => std.mem.writeIntLittle(i32, where[0..4], @as(i32, @truncate(S + A))), | 301 | elf.R_X86_64_32 => std.mem.writeInt(i32, where[0..4], @as(i32, @truncate(S + A)), .little), |
| 302 | elf.R_X86_64_64 => std.mem.writeIntLittle(i64, where[0..8], S + A), | 302 | elf.R_X86_64_64 => std.mem.writeInt(i64, where[0..8], S + A, .little), |
| 303 | elf.R_X86_64_PC32 => std.mem.writeIntLittle(i32, where[0..4], @as(i32, @intCast(S - P + A))), | 303 | elf.R_X86_64_PC32 => std.mem.writeInt(i32, where[0..4], @as(i32, @intCast(S - P + A)), .little), |
| 304 | elf.R_X86_64_PC64 => std.mem.writeIntLittle(i64, where[0..8], S - P + A), | 304 | elf.R_X86_64_PC64 => std.mem.writeInt(i64, where[0..8], S - P + A, .little), |
| 305 | else => unreachable, | 305 | else => unreachable, |
| 306 | } | 306 | } |
| 307 | } | 307 | } |
| ... | @@ -338,10 +338,11 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { | ... | @@ -338,10 +338,11 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { |
| 338 | const contents = try gpa.dupe(u8, fde.data(elf_file)); | 338 | const contents = try gpa.dupe(u8, fde.data(elf_file)); |
| 339 | defer gpa.free(contents); | 339 | defer gpa.free(contents); |
| 340 | 340 | ||
| 341 | std.mem.writeIntLittle( | 341 | std.mem.writeInt( |
| 342 | i32, | 342 | i32, |
| 343 | contents[4..8], | 343 | contents[4..8], |
| 344 | @as(i32, @truncate(@as(i64, @intCast(fde.out_offset + 4)) - @as(i64, @intCast(fde.cie(elf_file).out_offset)))), | 344 | @truncate(@as(i64, @intCast(fde.out_offset + 4)) - @as(i64, @intCast(fde.cie(elf_file).out_offset))), |
| 345 | .little, | ||
| 345 | ); | 346 | ); |
| 346 | 347 | ||
| 347 | for (fde.relocs(elf_file)) |rel| { | 348 | for (fde.relocs(elf_file)) |rel| { |
| ... | @@ -353,7 +354,7 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { | ... | @@ -353,7 +354,7 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void { |
| 353 | } | 354 | } |
| 354 | } | 355 | } |
| 355 | 356 | ||
| 356 | try writer.writeIntLittle(u32, 0); | 357 | try writer.writeInt(u32, 0, .little); |
| 357 | } | 358 | } |
| 358 | 359 | ||
| 359 | pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { | 360 | pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { |
| ... | @@ -365,14 +366,15 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { | ... | @@ -365,14 +366,15 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { |
| 365 | const eh_frame_shdr = elf_file.shdrs.items[elf_file.eh_frame_section_index.?]; | 366 | const eh_frame_shdr = elf_file.shdrs.items[elf_file.eh_frame_section_index.?]; |
| 366 | const eh_frame_hdr_shdr = elf_file.shdrs.items[elf_file.eh_frame_hdr_section_index.?]; | 367 | const eh_frame_hdr_shdr = elf_file.shdrs.items[elf_file.eh_frame_hdr_section_index.?]; |
| 367 | const num_fdes = @as(u32, @intCast(@divExact(eh_frame_hdr_shdr.sh_size - eh_frame_hdr_header_size, 8))); | 368 | const num_fdes = @as(u32, @intCast(@divExact(eh_frame_hdr_shdr.sh_size - eh_frame_hdr_header_size, 8))); |
| 368 | try writer.writeIntLittle( | 369 | try writer.writeInt( |
| 369 | u32, | 370 | u32, |
| 370 | @as(u32, @bitCast(@as( | 371 | @as(u32, @bitCast(@as( |
| 371 | i32, | 372 | i32, |
| 372 | @truncate(@as(i64, @intCast(eh_frame_shdr.sh_addr)) - @as(i64, @intCast(eh_frame_hdr_shdr.sh_addr)) - 4), | 373 | @truncate(@as(i64, @intCast(eh_frame_shdr.sh_addr)) - @as(i64, @intCast(eh_frame_hdr_shdr.sh_addr)) - 4), |
| 373 | ))), | 374 | ))), |
| 375 | .little, | ||
| 374 | ); | 376 | ); |
| 375 | try writer.writeIntLittle(u32, num_fdes); | 377 | try writer.writeInt(u32, num_fdes, .little); |
| 376 | 378 | ||
| 377 | const Entry = struct { | 379 | const Entry = struct { |
| 378 | init_addr: u32, | 380 | init_addr: u32, |
| ... | @@ -401,7 +403,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { | ... | @@ -401,7 +403,7 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void { |
| 401 | const S = @as(i64, @intCast(sym.address(.{}, elf_file))); | 403 | const S = @as(i64, @intCast(sym.address(.{}, elf_file))); |
| 402 | const A = rel.r_addend; | 404 | const A = rel.r_addend; |
| 403 | entries.appendAssumeCapacity(.{ | 405 | entries.appendAssumeCapacity(.{ |
| 404 | .init_addr = @as(u32, @bitCast(@as(i32, @truncate(S + A - @as(i64, @intCast(eh_frame_hdr_shdr.sh_addr)))))), | 406 | .init_addr = @bitCast(@as(i32, @truncate(S + A - @as(i64, @intCast(eh_frame_hdr_shdr.sh_addr))))), |
| 405 | .fde_addr = @as( | 407 | .fde_addr = @as( |
| 406 | u32, | 408 | u32, |
| 407 | @bitCast(@as(i32, @truncate(P - @as(i64, @intCast(eh_frame_hdr_shdr.sh_addr))))), | 409 | @bitCast(@as(i32, @truncate(P - @as(i64, @intCast(eh_frame_hdr_shdr.sh_addr))))), |
src/link/Elf/synthetic_sections.zig+15-15| ... | @@ -878,9 +878,9 @@ pub const PltSection = struct { | ... | @@ -878,9 +878,9 @@ pub const PltSection = struct { |
| 878 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp qword ptr [rip] -> .got.plt[2] | 878 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp qword ptr [rip] -> .got.plt[2] |
| 879 | }; | 879 | }; |
| 880 | var disp = @as(i64, @intCast(got_plt_addr + 8)) - @as(i64, @intCast(plt_addr + 8)) - 4; | 880 | var disp = @as(i64, @intCast(got_plt_addr + 8)) - @as(i64, @intCast(plt_addr + 8)) - 4; |
| 881 | mem.writeIntLittle(i32, preamble[8..][0..4], @as(i32, @intCast(disp))); | 881 | mem.writeInt(i32, preamble[8..][0..4], @as(i32, @intCast(disp)), .little); |
| 882 | disp = @as(i64, @intCast(got_plt_addr + 16)) - @as(i64, @intCast(plt_addr + 14)) - 4; | 882 | disp = @as(i64, @intCast(got_plt_addr + 16)) - @as(i64, @intCast(plt_addr + 14)) - 4; |
| 883 | mem.writeIntLittle(i32, preamble[14..][0..4], @as(i32, @intCast(disp))); | 883 | mem.writeInt(i32, preamble[14..][0..4], @as(i32, @intCast(disp)), .little); |
| 884 | try writer.writeAll(&preamble); | 884 | try writer.writeAll(&preamble); |
| 885 | try writer.writeByteNTimes(0xcc, preamble_size - preamble.len); | 885 | try writer.writeByteNTimes(0xcc, preamble_size - preamble.len); |
| 886 | 886 | ||
| ... | @@ -894,8 +894,8 @@ pub const PltSection = struct { | ... | @@ -894,8 +894,8 @@ pub const PltSection = struct { |
| 894 | 0x41, 0xbb, 0x00, 0x00, 0x00, 0x00, // mov r11d, N | 894 | 0x41, 0xbb, 0x00, 0x00, 0x00, 0x00, // mov r11d, N |
| 895 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp qword ptr [rip] -> .got.plt[N] | 895 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp qword ptr [rip] -> .got.plt[N] |
| 896 | }; | 896 | }; |
| 897 | mem.writeIntLittle(i32, entry[6..][0..4], @as(i32, @intCast(i))); | 897 | mem.writeInt(i32, entry[6..][0..4], @as(i32, @intCast(i)), .little); |
| 898 | mem.writeIntLittle(i32, entry[12..][0..4], @as(i32, @intCast(disp))); | 898 | mem.writeInt(i32, entry[12..][0..4], @as(i32, @intCast(disp)), .little); |
| 899 | try writer.writeAll(&entry); | 899 | try writer.writeAll(&entry); |
| 900 | } | 900 | } |
| 901 | } | 901 | } |
| ... | @@ -971,17 +971,17 @@ pub const GotPltSection = struct { | ... | @@ -971,17 +971,17 @@ pub const GotPltSection = struct { |
| 971 | { | 971 | { |
| 972 | // [0]: _DYNAMIC | 972 | // [0]: _DYNAMIC |
| 973 | const symbol = elf_file.symbol(elf_file.dynamic_index.?); | 973 | const symbol = elf_file.symbol(elf_file.dynamic_index.?); |
| 974 | try writer.writeIntLittle(u64, symbol.value); | 974 | try writer.writeInt(u64, symbol.value, .little); |
| 975 | } | 975 | } |
| 976 | // [1]: 0x0 | 976 | // [1]: 0x0 |
| 977 | // [2]: 0x0 | 977 | // [2]: 0x0 |
| 978 | try writer.writeIntLittle(u64, 0x0); | 978 | try writer.writeInt(u64, 0x0, .little); |
| 979 | try writer.writeIntLittle(u64, 0x0); | 979 | try writer.writeInt(u64, 0x0, .little); |
| 980 | if (elf_file.plt_section_index) |shndx| { | 980 | if (elf_file.plt_section_index) |shndx| { |
| 981 | const plt_addr = elf_file.shdrs.items[shndx].sh_addr; | 981 | const plt_addr = elf_file.shdrs.items[shndx].sh_addr; |
| 982 | for (0..elf_file.plt.symbols.items.len) |_| { | 982 | for (0..elf_file.plt.symbols.items.len) |_| { |
| 983 | // [N]: .plt | 983 | // [N]: .plt |
| 984 | try writer.writeIntLittle(u64, plt_addr); | 984 | try writer.writeInt(u64, plt_addr, .little); |
| 985 | } | 985 | } |
| 986 | } | 986 | } |
| 987 | } | 987 | } |
| ... | @@ -1023,7 +1023,7 @@ pub const PltGotSection = struct { | ... | @@ -1023,7 +1023,7 @@ pub const PltGotSection = struct { |
| 1023 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp qword ptr [rip] -> .got[N] | 1023 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp qword ptr [rip] -> .got[N] |
| 1024 | 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, | 1024 | 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, |
| 1025 | }; | 1025 | }; |
| 1026 | mem.writeIntLittle(i32, entry[6..][0..4], @as(i32, @intCast(disp))); | 1026 | mem.writeInt(i32, entry[6..][0..4], @as(i32, @intCast(disp)), .little); |
| 1027 | try writer.writeAll(&entry); | 1027 | try writer.writeAll(&entry); |
| 1028 | } | 1028 | } |
| 1029 | } | 1029 | } |
| ... | @@ -1258,8 +1258,8 @@ pub const HashSection = struct { | ... | @@ -1258,8 +1258,8 @@ pub const HashSection = struct { |
| 1258 | } | 1258 | } |
| 1259 | 1259 | ||
| 1260 | try hs.buffer.ensureTotalCapacityPrecise(gpa, (2 + nsyms * 2) * 4); | 1260 | try hs.buffer.ensureTotalCapacityPrecise(gpa, (2 + nsyms * 2) * 4); |
| 1261 | hs.buffer.writer(gpa).writeIntLittle(u32, @as(u32, @intCast(nsyms))) catch unreachable; | 1261 | hs.buffer.writer(gpa).writeInt(u32, @as(u32, @intCast(nsyms)), .little) catch unreachable; |
| 1262 | hs.buffer.writer(gpa).writeIntLittle(u32, @as(u32, @intCast(nsyms))) catch unreachable; | 1262 | hs.buffer.writer(gpa).writeInt(u32, @as(u32, @intCast(nsyms)), .little) catch unreachable; |
| 1263 | hs.buffer.writer(gpa).writeAll(mem.sliceAsBytes(buckets)) catch unreachable; | 1263 | hs.buffer.writer(gpa).writeAll(mem.sliceAsBytes(buckets)) catch unreachable; |
| 1264 | hs.buffer.writer(gpa).writeAll(mem.sliceAsBytes(chains)) catch unreachable; | 1264 | hs.buffer.writer(gpa).writeAll(mem.sliceAsBytes(chains)) catch unreachable; |
| 1265 | } | 1265 | } |
| ... | @@ -1322,10 +1322,10 @@ pub const GnuHashSection = struct { | ... | @@ -1322,10 +1322,10 @@ pub const GnuHashSection = struct { |
| 1322 | var counting = std.io.countingWriter(writer); | 1322 | var counting = std.io.countingWriter(writer); |
| 1323 | const cwriter = counting.writer(); | 1323 | const cwriter = counting.writer(); |
| 1324 | 1324 | ||
| 1325 | try cwriter.writeIntLittle(u32, hash.num_buckets); | 1325 | try cwriter.writeInt(u32, hash.num_buckets, .little); |
| 1326 | try cwriter.writeIntLittle(u32, export_off); | 1326 | try cwriter.writeInt(u32, export_off, .little); |
| 1327 | try cwriter.writeIntLittle(u32, hash.num_bloom); | 1327 | try cwriter.writeInt(u32, hash.num_bloom, .little); |
| 1328 | try cwriter.writeIntLittle(u32, bloom_shift); | 1328 | try cwriter.writeInt(u32, bloom_shift, .little); |
| 1329 | 1329 | ||
| 1330 | const gpa = elf_file.base.allocator; | 1330 | const gpa = elf_file.base.allocator; |
| 1331 | const hashes = try gpa.alloc(u32, exports.len); | 1331 | const hashes = try gpa.alloc(u32, exports.len); |
src/link/MachO.zig+7-7| ... | @@ -1222,7 +1222,7 @@ fn writeOffsetTableEntry(self: *MachO, index: usize) !void { | ... | @@ -1222,7 +1222,7 @@ fn writeOffsetTableEntry(self: *MachO, index: usize) !void { |
| 1222 | log.debug("writing GOT entry {d}: @{x} => {x}", .{ index, vmaddr, entry_value }); | 1222 | log.debug("writing GOT entry {d}: @{x} => {x}", .{ index, vmaddr, entry_value }); |
| 1223 | 1223 | ||
| 1224 | var buf: [@sizeOf(u64)]u8 = undefined; | 1224 | var buf: [@sizeOf(u64)]u8 = undefined; |
| 1225 | mem.writeIntLittle(u64, &buf, entry_value); | 1225 | mem.writeInt(u64, &buf, entry_value, .little); |
| 1226 | try self.base.file.?.pwriteAll(&buf, file_offset); | 1226 | try self.base.file.?.pwriteAll(&buf, file_offset); |
| 1227 | 1227 | ||
| 1228 | if (is_hot_update_compatible) { | 1228 | if (is_hot_update_compatible) { |
| ... | @@ -1329,7 +1329,7 @@ fn writeStubTableEntry(self: *MachO, index: usize) !void { | ... | @@ -1329,7 +1329,7 @@ fn writeStubTableEntry(self: *MachO, index: usize) !void { |
| 1329 | 1329 | ||
| 1330 | { | 1330 | { |
| 1331 | var buf: [@sizeOf(u64)]u8 = undefined; | 1331 | var buf: [@sizeOf(u64)]u8 = undefined; |
| 1332 | mem.writeIntLittle(u64, &buf, stub_helper_addr); | 1332 | mem.writeInt(u64, &buf, stub_helper_addr, .little); |
| 1333 | const off = laptr_header.offset + @sizeOf(u64) * index; | 1333 | const off = laptr_header.offset + @sizeOf(u64) * index; |
| 1334 | try self.base.file.?.pwriteAll(&buf, off); | 1334 | try self.base.file.?.pwriteAll(&buf, off); |
| 1335 | } | 1335 | } |
| ... | @@ -3773,7 +3773,7 @@ fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void { | ... | @@ -3773,7 +3773,7 @@ fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void { |
| 3773 | const base_offset = sym.n_value - segment.vmaddr; | 3773 | const base_offset = sym.n_value - segment.vmaddr; |
| 3774 | const rel_offset = @as(u32, @intCast(rel.r_address - ctx.base_offset)); | 3774 | const rel_offset = @as(u32, @intCast(rel.r_address - ctx.base_offset)); |
| 3775 | const offset = @as(u64, @intCast(base_offset + rel_offset)); | 3775 | const offset = @as(u64, @intCast(base_offset + rel_offset)); |
| 3776 | const addend = mem.readIntLittle(i64, code[rel_offset..][0..8]); | 3776 | const addend = mem.readInt(i64, code[rel_offset..][0..8], .little); |
| 3777 | 3777 | ||
| 3778 | const dylib_ordinal = @divTrunc(@as(i16, @bitCast(bind_sym.n_desc)), macho.N_SYMBOL_RESOLVER); | 3778 | const dylib_ordinal = @divTrunc(@as(i16, @bitCast(bind_sym.n_desc)), macho.N_SYMBOL_RESOLVER); |
| 3779 | log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{ | 3779 | log.debug(" | bind at {x}, import('{s}') in dylib({d})", .{ |
| ... | @@ -4502,7 +4502,7 @@ pub fn writeDysymtab(self: *MachO, ctx: SymtabCtx) !void { | ... | @@ -4502,7 +4502,7 @@ pub fn writeDysymtab(self: *MachO, ctx: SymtabCtx) !void { |
| 4502 | if (!self.stub_table.lookup.contains(entry)) continue; | 4502 | if (!self.stub_table.lookup.contains(entry)) continue; |
| 4503 | const target_sym = self.getSymbol(entry); | 4503 | const target_sym = self.getSymbol(entry); |
| 4504 | assert(target_sym.undf()); | 4504 | assert(target_sym.undf()); |
| 4505 | try writer.writeIntLittle(u32, iundefsym + ctx.imports_table.get(entry).?); | 4505 | try writer.writeInt(u32, iundefsym + ctx.imports_table.get(entry).?, .little); |
| 4506 | } | 4506 | } |
| 4507 | } | 4507 | } |
| 4508 | 4508 | ||
| ... | @@ -4513,9 +4513,9 @@ pub fn writeDysymtab(self: *MachO, ctx: SymtabCtx) !void { | ... | @@ -4513,9 +4513,9 @@ pub fn writeDysymtab(self: *MachO, ctx: SymtabCtx) !void { |
| 4513 | if (!self.got_table.lookup.contains(entry)) continue; | 4513 | if (!self.got_table.lookup.contains(entry)) continue; |
| 4514 | const target_sym = self.getSymbol(entry); | 4514 | const target_sym = self.getSymbol(entry); |
| 4515 | if (target_sym.undf()) { | 4515 | if (target_sym.undf()) { |
| 4516 | try writer.writeIntLittle(u32, iundefsym + ctx.imports_table.get(entry).?); | 4516 | try writer.writeInt(u32, iundefsym + ctx.imports_table.get(entry).?, .little); |
| 4517 | } else { | 4517 | } else { |
| 4518 | try writer.writeIntLittle(u32, macho.INDIRECT_SYMBOL_LOCAL); | 4518 | try writer.writeInt(u32, macho.INDIRECT_SYMBOL_LOCAL, .little); |
| 4519 | } | 4519 | } |
| 4520 | } | 4520 | } |
| 4521 | } | 4521 | } |
| ... | @@ -4527,7 +4527,7 @@ pub fn writeDysymtab(self: *MachO, ctx: SymtabCtx) !void { | ... | @@ -4527,7 +4527,7 @@ pub fn writeDysymtab(self: *MachO, ctx: SymtabCtx) !void { |
| 4527 | if (!self.stub_table.lookup.contains(entry)) continue; | 4527 | if (!self.stub_table.lookup.contains(entry)) continue; |
| 4528 | const target_sym = self.getSymbol(entry); | 4528 | const target_sym = self.getSymbol(entry); |
| 4529 | assert(target_sym.undf()); | 4529 | assert(target_sym.undf()); |
| 4530 | try writer.writeIntLittle(u32, iundefsym + ctx.imports_table.get(entry).?); | 4530 | try writer.writeInt(u32, iundefsym + ctx.imports_table.get(entry).?, .little); |
| 4531 | } | 4531 | } |
| 4532 | } | 4532 | } |
| 4533 | 4533 |
src/link/MachO/Archive.zig+4-4| ... | @@ -123,7 +123,7 @@ fn parseName(allocator: Allocator, name_or_length: ar_hdr.NameOrLength, reader: | ... | @@ -123,7 +123,7 @@ fn parseName(allocator: Allocator, name_or_length: ar_hdr.NameOrLength, reader: |
| 123 | } | 123 | } |
| 124 | 124 | ||
| 125 | fn parseTableOfContents(self: *Archive, allocator: Allocator, reader: anytype) !void { | 125 | fn parseTableOfContents(self: *Archive, allocator: Allocator, reader: anytype) !void { |
| 126 | const symtab_size = try reader.readIntLittle(u32); | 126 | const symtab_size = try reader.readInt(u32, .little); |
| 127 | var symtab = try allocator.alloc(u8, symtab_size); | 127 | var symtab = try allocator.alloc(u8, symtab_size); |
| 128 | defer allocator.free(symtab); | 128 | defer allocator.free(symtab); |
| 129 | 129 | ||
| ... | @@ -132,7 +132,7 @@ fn parseTableOfContents(self: *Archive, allocator: Allocator, reader: anytype) ! | ... | @@ -132,7 +132,7 @@ fn parseTableOfContents(self: *Archive, allocator: Allocator, reader: anytype) ! |
| 132 | return error.MalformedArchive; | 132 | return error.MalformedArchive; |
| 133 | }; | 133 | }; |
| 134 | 134 | ||
| 135 | const strtab_size = try reader.readIntLittle(u32); | 135 | const strtab_size = try reader.readInt(u32, .little); |
| 136 | var strtab = try allocator.alloc(u8, strtab_size); | 136 | var strtab = try allocator.alloc(u8, strtab_size); |
| 137 | defer allocator.free(strtab); | 137 | defer allocator.free(strtab); |
| 138 | 138 | ||
| ... | @@ -145,11 +145,11 @@ fn parseTableOfContents(self: *Archive, allocator: Allocator, reader: anytype) ! | ... | @@ -145,11 +145,11 @@ fn parseTableOfContents(self: *Archive, allocator: Allocator, reader: anytype) ! |
| 145 | var symtab_reader = symtab_stream.reader(); | 145 | var symtab_reader = symtab_stream.reader(); |
| 146 | 146 | ||
| 147 | while (true) { | 147 | while (true) { |
| 148 | const n_strx = symtab_reader.readIntLittle(u32) catch |err| switch (err) { | 148 | const n_strx = symtab_reader.readInt(u32, .little) catch |err| switch (err) { |
| 149 | error.EndOfStream => break, | 149 | error.EndOfStream => break, |
| 150 | else => |e| return e, | 150 | else => |e| return e, |
| 151 | }; | 151 | }; |
| 152 | const object_offset = try symtab_reader.readIntLittle(u32); | 152 | const object_offset = try symtab_reader.readInt(u32, .little); |
| 153 | 153 | ||
| 154 | const sym_name = mem.sliceTo(@as([*:0]const u8, @ptrCast(strtab.ptr + n_strx)), 0); | 154 | const sym_name = mem.sliceTo(@as([*:0]const u8, @ptrCast(strtab.ptr + n_strx)), 0); |
| 155 | const owned_name = try allocator.dupe(u8, sym_name); | 155 | const owned_name = try allocator.dupe(u8, sym_name); |
src/link/MachO/Atom.zig+26-26| ... | @@ -443,9 +443,9 @@ pub fn parseRelocTarget(macho_file: *MachO, ctx: struct { | ... | @@ -443,9 +443,9 @@ pub fn parseRelocTarget(macho_file: *MachO, ctx: struct { |
| 443 | 443 | ||
| 444 | const address_in_section = if (ctx.rel.r_pcrel == 0) blk: { | 444 | const address_in_section = if (ctx.rel.r_pcrel == 0) blk: { |
| 445 | break :blk if (ctx.rel.r_length == 3) | 445 | break :blk if (ctx.rel.r_length == 3) |
| 446 | mem.readIntLittle(u64, ctx.code[rel_offset..][0..8]) | 446 | mem.readInt(u64, ctx.code[rel_offset..][0..8], .little) |
| 447 | else | 447 | else |
| 448 | mem.readIntLittle(u32, ctx.code[rel_offset..][0..4]); | 448 | mem.readInt(u32, ctx.code[rel_offset..][0..4], .little); |
| 449 | } else blk: { | 449 | } else blk: { |
| 450 | assert(macho_file.base.options.target.cpu.arch == .x86_64); | 450 | assert(macho_file.base.options.target.cpu.arch == .x86_64); |
| 451 | const correction: u3 = switch (@as(macho.reloc_type_x86_64, @enumFromInt(ctx.rel.r_type))) { | 451 | const correction: u3 = switch (@as(macho.reloc_type_x86_64, @enumFromInt(ctx.rel.r_type))) { |
| ... | @@ -455,7 +455,7 @@ pub fn parseRelocTarget(macho_file: *MachO, ctx: struct { | ... | @@ -455,7 +455,7 @@ pub fn parseRelocTarget(macho_file: *MachO, ctx: struct { |
| 455 | .X86_64_RELOC_SIGNED_4 => 4, | 455 | .X86_64_RELOC_SIGNED_4 => 4, |
| 456 | else => unreachable, | 456 | else => unreachable, |
| 457 | }; | 457 | }; |
| 458 | const addend = mem.readIntLittle(i32, ctx.code[rel_offset..][0..4]); | 458 | const addend = mem.readInt(i32, ctx.code[rel_offset..][0..4], .little); |
| 459 | const target_address = @as(i64, @intCast(ctx.base_addr)) + ctx.rel.r_address + 4 + correction + addend; | 459 | const target_address = @as(i64, @intCast(ctx.base_addr)) + ctx.rel.r_address + 4 + correction + addend; |
| 460 | break :blk @as(u64, @intCast(target_address)); | 460 | break :blk @as(u64, @intCast(target_address)); |
| 461 | }; | 461 | }; |
| ... | @@ -781,7 +781,7 @@ fn resolveRelocsArm64( | ... | @@ -781,7 +781,7 @@ fn resolveRelocsArm64( |
| 781 | ), code), | 781 | ), code), |
| 782 | }; | 782 | }; |
| 783 | inst.unconditional_branch_immediate.imm26 = @as(u26, @truncate(@as(u28, @bitCast(displacement >> 2)))); | 783 | inst.unconditional_branch_immediate.imm26 = @as(u26, @truncate(@as(u28, @bitCast(displacement >> 2)))); |
| 784 | mem.writeIntLittle(u32, code, inst.toU32()); | 784 | mem.writeInt(u32, code, inst.toU32(), .little); |
| 785 | }, | 785 | }, |
| 786 | 786 | ||
| 787 | .ARM64_RELOC_PAGE21, | 787 | .ARM64_RELOC_PAGE21, |
| ... | @@ -802,7 +802,7 @@ fn resolveRelocsArm64( | ... | @@ -802,7 +802,7 @@ fn resolveRelocsArm64( |
| 802 | }; | 802 | }; |
| 803 | inst.pc_relative_address.immhi = @as(u19, @truncate(pages >> 2)); | 803 | inst.pc_relative_address.immhi = @as(u19, @truncate(pages >> 2)); |
| 804 | inst.pc_relative_address.immlo = @as(u2, @truncate(pages)); | 804 | inst.pc_relative_address.immlo = @as(u2, @truncate(pages)); |
| 805 | mem.writeIntLittle(u32, code, inst.toU32()); | 805 | mem.writeInt(u32, code, inst.toU32(), .little); |
| 806 | addend = null; | 806 | addend = null; |
| 807 | }, | 807 | }, |
| 808 | 808 | ||
| ... | @@ -821,7 +821,7 @@ fn resolveRelocsArm64( | ... | @@ -821,7 +821,7 @@ fn resolveRelocsArm64( |
| 821 | ), code), | 821 | ), code), |
| 822 | }; | 822 | }; |
| 823 | inst.add_subtract_immediate.imm12 = off; | 823 | inst.add_subtract_immediate.imm12 = off; |
| 824 | mem.writeIntLittle(u32, code, inst.toU32()); | 824 | mem.writeInt(u32, code, inst.toU32(), .little); |
| 825 | } else { | 825 | } else { |
| 826 | var inst = aarch64.Instruction{ | 826 | var inst = aarch64.Instruction{ |
| 827 | .load_store_register = mem.bytesToValue(meta.TagPayload( | 827 | .load_store_register = mem.bytesToValue(meta.TagPayload( |
| ... | @@ -839,7 +839,7 @@ fn resolveRelocsArm64( | ... | @@ -839,7 +839,7 @@ fn resolveRelocsArm64( |
| 839 | 3 => .load_store_64, | 839 | 3 => .load_store_64, |
| 840 | }); | 840 | }); |
| 841 | inst.load_store_register.offset = off; | 841 | inst.load_store_register.offset = off; |
| 842 | mem.writeIntLittle(u32, code, inst.toU32()); | 842 | mem.writeInt(u32, code, inst.toU32(), .little); |
| 843 | } | 843 | } |
| 844 | addend = null; | 844 | addend = null; |
| 845 | }, | 845 | }, |
| ... | @@ -858,7 +858,7 @@ fn resolveRelocsArm64( | ... | @@ -858,7 +858,7 @@ fn resolveRelocsArm64( |
| 858 | ), code), | 858 | ), code), |
| 859 | }; | 859 | }; |
| 860 | inst.load_store_register.offset = off; | 860 | inst.load_store_register.offset = off; |
| 861 | mem.writeIntLittle(u32, code, inst.toU32()); | 861 | mem.writeInt(u32, code, inst.toU32(), .little); |
| 862 | addend = null; | 862 | addend = null; |
| 863 | }, | 863 | }, |
| 864 | 864 | ||
| ... | @@ -918,7 +918,7 @@ fn resolveRelocsArm64( | ... | @@ -918,7 +918,7 @@ fn resolveRelocsArm64( |
| 918 | .sf = @as(u1, @truncate(reg_info.size)), | 918 | .sf = @as(u1, @truncate(reg_info.size)), |
| 919 | }, | 919 | }, |
| 920 | }; | 920 | }; |
| 921 | mem.writeIntLittle(u32, code, inst.toU32()); | 921 | mem.writeInt(u32, code, inst.toU32(), .little); |
| 922 | addend = null; | 922 | addend = null; |
| 923 | }, | 923 | }, |
| 924 | 924 | ||
| ... | @@ -926,14 +926,14 @@ fn resolveRelocsArm64( | ... | @@ -926,14 +926,14 @@ fn resolveRelocsArm64( |
| 926 | relocs_log.debug(" | target_addr = 0x{x}", .{target_addr}); | 926 | relocs_log.debug(" | target_addr = 0x{x}", .{target_addr}); |
| 927 | const result = math.cast(i32, @as(i64, @intCast(target_addr)) - @as(i64, @intCast(source_addr))) orelse | 927 | const result = math.cast(i32, @as(i64, @intCast(target_addr)) - @as(i64, @intCast(source_addr))) orelse |
| 928 | return error.Overflow; | 928 | return error.Overflow; |
| 929 | mem.writeIntLittle(u32, atom_code[rel_offset..][0..4], @as(u32, @bitCast(result))); | 929 | mem.writeInt(u32, atom_code[rel_offset..][0..4], @as(u32, @bitCast(result)), .little); |
| 930 | }, | 930 | }, |
| 931 | 931 | ||
| 932 | .ARM64_RELOC_UNSIGNED => { | 932 | .ARM64_RELOC_UNSIGNED => { |
| 933 | var ptr_addend = if (rel.r_length == 3) | 933 | var ptr_addend = if (rel.r_length == 3) |
| 934 | mem.readIntLittle(i64, atom_code[rel_offset..][0..8]) | 934 | mem.readInt(i64, atom_code[rel_offset..][0..8], .little) |
| 935 | else | 935 | else |
| 936 | mem.readIntLittle(i32, atom_code[rel_offset..][0..4]); | 936 | mem.readInt(i32, atom_code[rel_offset..][0..4], .little); |
| 937 | 937 | ||
| 938 | if (rel.r_extern == 0) { | 938 | if (rel.r_extern == 0) { |
| 939 | const base_addr = if (target.sym_index >= object.source_address_lookup.len) | 939 | const base_addr = if (target.sym_index >= object.source_address_lookup.len) |
| ... | @@ -954,9 +954,9 @@ fn resolveRelocsArm64( | ... | @@ -954,9 +954,9 @@ fn resolveRelocsArm64( |
| 954 | relocs_log.debug(" | target_addr = 0x{x}", .{result}); | 954 | relocs_log.debug(" | target_addr = 0x{x}", .{result}); |
| 955 | 955 | ||
| 956 | if (rel.r_length == 3) { | 956 | if (rel.r_length == 3) { |
| 957 | mem.writeIntLittle(u64, atom_code[rel_offset..][0..8], @as(u64, @bitCast(result))); | 957 | mem.writeInt(u64, atom_code[rel_offset..][0..8], @as(u64, @bitCast(result)), .little); |
| 958 | } else { | 958 | } else { |
| 959 | mem.writeIntLittle(u32, atom_code[rel_offset..][0..4], @as(u32, @truncate(@as(u64, @bitCast(result))))); | 959 | mem.writeInt(u32, atom_code[rel_offset..][0..4], @as(u32, @truncate(@as(u64, @bitCast(result)))), .little); |
| 960 | } | 960 | } |
| 961 | 961 | ||
| 962 | subtractor = null; | 962 | subtractor = null; |
| ... | @@ -1045,25 +1045,25 @@ fn resolveRelocsX86( | ... | @@ -1045,25 +1045,25 @@ fn resolveRelocsX86( |
| 1045 | 1045 | ||
| 1046 | switch (rel_type) { | 1046 | switch (rel_type) { |
| 1047 | .X86_64_RELOC_BRANCH => { | 1047 | .X86_64_RELOC_BRANCH => { |
| 1048 | const addend = mem.readIntLittle(i32, atom_code[rel_offset..][0..4]); | 1048 | const addend = mem.readInt(i32, atom_code[rel_offset..][0..4], .little); |
| 1049 | const adjusted_target_addr = @as(u64, @intCast(@as(i64, @intCast(target_addr)) + addend)); | 1049 | const adjusted_target_addr = @as(u64, @intCast(@as(i64, @intCast(target_addr)) + addend)); |
| 1050 | relocs_log.debug(" | target_addr = 0x{x}", .{adjusted_target_addr}); | 1050 | relocs_log.debug(" | target_addr = 0x{x}", .{adjusted_target_addr}); |
| 1051 | const disp = try Relocation.calcPcRelativeDisplacementX86(source_addr, adjusted_target_addr, 0); | 1051 | const disp = try Relocation.calcPcRelativeDisplacementX86(source_addr, adjusted_target_addr, 0); |
| 1052 | mem.writeIntLittle(i32, atom_code[rel_offset..][0..4], disp); | 1052 | mem.writeInt(i32, atom_code[rel_offset..][0..4], disp, .little); |
| 1053 | }, | 1053 | }, |
| 1054 | 1054 | ||
| 1055 | .X86_64_RELOC_GOT, | 1055 | .X86_64_RELOC_GOT, |
| 1056 | .X86_64_RELOC_GOT_LOAD, | 1056 | .X86_64_RELOC_GOT_LOAD, |
| 1057 | => { | 1057 | => { |
| 1058 | const addend = mem.readIntLittle(i32, atom_code[rel_offset..][0..4]); | 1058 | const addend = mem.readInt(i32, atom_code[rel_offset..][0..4], .little); |
| 1059 | const adjusted_target_addr = @as(u64, @intCast(@as(i64, @intCast(target_addr)) + addend)); | 1059 | const adjusted_target_addr = @as(u64, @intCast(@as(i64, @intCast(target_addr)) + addend)); |
| 1060 | relocs_log.debug(" | target_addr = 0x{x}", .{adjusted_target_addr}); | 1060 | relocs_log.debug(" | target_addr = 0x{x}", .{adjusted_target_addr}); |
| 1061 | const disp = try Relocation.calcPcRelativeDisplacementX86(source_addr, adjusted_target_addr, 0); | 1061 | const disp = try Relocation.calcPcRelativeDisplacementX86(source_addr, adjusted_target_addr, 0); |
| 1062 | mem.writeIntLittle(i32, atom_code[rel_offset..][0..4], disp); | 1062 | mem.writeInt(i32, atom_code[rel_offset..][0..4], disp, .little); |
| 1063 | }, | 1063 | }, |
| 1064 | 1064 | ||
| 1065 | .X86_64_RELOC_TLV => { | 1065 | .X86_64_RELOC_TLV => { |
| 1066 | const addend = mem.readIntLittle(i32, atom_code[rel_offset..][0..4]); | 1066 | const addend = mem.readInt(i32, atom_code[rel_offset..][0..4], .little); |
| 1067 | const adjusted_target_addr = @as(u64, @intCast(@as(i64, @intCast(target_addr)) + addend)); | 1067 | const adjusted_target_addr = @as(u64, @intCast(@as(i64, @intCast(target_addr)) + addend)); |
| 1068 | relocs_log.debug(" | target_addr = 0x{x}", .{adjusted_target_addr}); | 1068 | relocs_log.debug(" | target_addr = 0x{x}", .{adjusted_target_addr}); |
| 1069 | const disp = try Relocation.calcPcRelativeDisplacementX86(source_addr, adjusted_target_addr, 0); | 1069 | const disp = try Relocation.calcPcRelativeDisplacementX86(source_addr, adjusted_target_addr, 0); |
| ... | @@ -1073,7 +1073,7 @@ fn resolveRelocsX86( | ... | @@ -1073,7 +1073,7 @@ fn resolveRelocsX86( |
| 1073 | atom_code[rel_offset - 2] = 0x8d; | 1073 | atom_code[rel_offset - 2] = 0x8d; |
| 1074 | } | 1074 | } |
| 1075 | 1075 | ||
| 1076 | mem.writeIntLittle(i32, atom_code[rel_offset..][0..4], disp); | 1076 | mem.writeInt(i32, atom_code[rel_offset..][0..4], disp, .little); |
| 1077 | }, | 1077 | }, |
| 1078 | 1078 | ||
| 1079 | .X86_64_RELOC_SIGNED, | 1079 | .X86_64_RELOC_SIGNED, |
| ... | @@ -1088,7 +1088,7 @@ fn resolveRelocsX86( | ... | @@ -1088,7 +1088,7 @@ fn resolveRelocsX86( |
| 1088 | .X86_64_RELOC_SIGNED_4 => 4, | 1088 | .X86_64_RELOC_SIGNED_4 => 4, |
| 1089 | else => unreachable, | 1089 | else => unreachable, |
| 1090 | }; | 1090 | }; |
| 1091 | var addend = mem.readIntLittle(i32, atom_code[rel_offset..][0..4]) + correction; | 1091 | var addend = mem.readInt(i32, atom_code[rel_offset..][0..4], .little) + correction; |
| 1092 | 1092 | ||
| 1093 | if (rel.r_extern == 0) { | 1093 | if (rel.r_extern == 0) { |
| 1094 | const base_addr = if (target.sym_index >= object.source_address_lookup.len) | 1094 | const base_addr = if (target.sym_index >= object.source_address_lookup.len) |
| ... | @@ -1104,14 +1104,14 @@ fn resolveRelocsX86( | ... | @@ -1104,14 +1104,14 @@ fn resolveRelocsX86( |
| 1104 | relocs_log.debug(" | target_addr = 0x{x}", .{adjusted_target_addr}); | 1104 | relocs_log.debug(" | target_addr = 0x{x}", .{adjusted_target_addr}); |
| 1105 | 1105 | ||
| 1106 | const disp = try Relocation.calcPcRelativeDisplacementX86(source_addr, adjusted_target_addr, correction); | 1106 | const disp = try Relocation.calcPcRelativeDisplacementX86(source_addr, adjusted_target_addr, correction); |
| 1107 | mem.writeIntLittle(i32, atom_code[rel_offset..][0..4], disp); | 1107 | mem.writeInt(i32, atom_code[rel_offset..][0..4], disp, .little); |
| 1108 | }, | 1108 | }, |
| 1109 | 1109 | ||
| 1110 | .X86_64_RELOC_UNSIGNED => { | 1110 | .X86_64_RELOC_UNSIGNED => { |
| 1111 | var addend = if (rel.r_length == 3) | 1111 | var addend = if (rel.r_length == 3) |
| 1112 | mem.readIntLittle(i64, atom_code[rel_offset..][0..8]) | 1112 | mem.readInt(i64, atom_code[rel_offset..][0..8], .little) |
| 1113 | else | 1113 | else |
| 1114 | mem.readIntLittle(i32, atom_code[rel_offset..][0..4]); | 1114 | mem.readInt(i32, atom_code[rel_offset..][0..4], .little); |
| 1115 | 1115 | ||
| 1116 | if (rel.r_extern == 0) { | 1116 | if (rel.r_extern == 0) { |
| 1117 | const base_addr = if (target.sym_index >= object.source_address_lookup.len) | 1117 | const base_addr = if (target.sym_index >= object.source_address_lookup.len) |
| ... | @@ -1132,9 +1132,9 @@ fn resolveRelocsX86( | ... | @@ -1132,9 +1132,9 @@ fn resolveRelocsX86( |
| 1132 | relocs_log.debug(" | target_addr = 0x{x}", .{result}); | 1132 | relocs_log.debug(" | target_addr = 0x{x}", .{result}); |
| 1133 | 1133 | ||
| 1134 | if (rel.r_length == 3) { | 1134 | if (rel.r_length == 3) { |
| 1135 | mem.writeIntLittle(u64, atom_code[rel_offset..][0..8], @as(u64, @bitCast(result))); | 1135 | mem.writeInt(u64, atom_code[rel_offset..][0..8], @as(u64, @bitCast(result)), .little); |
| 1136 | } else { | 1136 | } else { |
| 1137 | mem.writeIntLittle(u32, atom_code[rel_offset..][0..4], @as(u32, @truncate(@as(u64, @bitCast(result))))); | 1137 | mem.writeInt(u32, atom_code[rel_offset..][0..4], @as(u32, @truncate(@as(u64, @bitCast(result)))), .little); |
| 1138 | } | 1138 | } |
| 1139 | 1139 | ||
| 1140 | subtractor = null; | 1140 | subtractor = null; |
src/link/MachO/CodeSignature.zig+29-29| ... | @@ -115,14 +115,14 @@ pub fn writeAdhocSignature( | ... | @@ -115,14 +115,14 @@ pub fn writeAdhocSignature( |
| 115 | self.code_directory.inner.length = self.code_directory.size(); | 115 | self.code_directory.inner.length = self.code_directory.size(); |
| 116 | header.length += self.code_directory.size(); | 116 | header.length += self.code_directory.size(); |
| 117 | 117 | ||
| 118 | try writer.writeIntBig(u32, header.magic); | 118 | try writer.writeInt(u32, header.magic, .big); |
| 119 | try writer.writeIntBig(u32, header.length); | 119 | try writer.writeInt(u32, header.length, .big); |
| 120 | try writer.writeIntBig(u32, header.count); | 120 | try writer.writeInt(u32, header.count, .big); |
| 121 | 121 | ||
| 122 | var offset: u32 = @sizeOf(macho.SuperBlob) + @sizeOf(macho.BlobIndex) * @as(u32, @intCast(blobs.items.len)); | 122 | var offset: u32 = @sizeOf(macho.SuperBlob) + @sizeOf(macho.BlobIndex) * @as(u32, @intCast(blobs.items.len)); |
| 123 | for (blobs.items) |blob| { | 123 | for (blobs.items) |blob| { |
| 124 | try writer.writeIntBig(u32, blob.slotType()); | 124 | try writer.writeInt(u32, blob.slotType(), .big); |
| 125 | try writer.writeIntBig(u32, offset); | 125 | try writer.writeInt(u32, offset, .big); |
| 126 | offset += blob.size(); | 126 | offset += blob.size(); |
| 127 | } | 127 | } |
| 128 | 128 | ||
| ... | @@ -272,27 +272,27 @@ const CodeDirectory = struct { | ... | @@ -272,27 +272,27 @@ const CodeDirectory = struct { |
| 272 | } | 272 | } |
| 273 | 273 | ||
| 274 | fn write(self: CodeDirectory, writer: anytype) !void { | 274 | fn write(self: CodeDirectory, writer: anytype) !void { |
| 275 | try writer.writeIntBig(u32, self.inner.magic); | 275 | try writer.writeInt(u32, self.inner.magic, .big); |
| 276 | try writer.writeIntBig(u32, self.inner.length); | 276 | try writer.writeInt(u32, self.inner.length, .big); |
| 277 | try writer.writeIntBig(u32, self.inner.version); | 277 | try writer.writeInt(u32, self.inner.version, .big); |
| 278 | try writer.writeIntBig(u32, self.inner.flags); | 278 | try writer.writeInt(u32, self.inner.flags, .big); |
| 279 | try writer.writeIntBig(u32, self.inner.hashOffset); | 279 | try writer.writeInt(u32, self.inner.hashOffset, .big); |
| 280 | try writer.writeIntBig(u32, self.inner.identOffset); | 280 | try writer.writeInt(u32, self.inner.identOffset, .big); |
| 281 | try writer.writeIntBig(u32, self.inner.nSpecialSlots); | 281 | try writer.writeInt(u32, self.inner.nSpecialSlots, .big); |
| 282 | try writer.writeIntBig(u32, self.inner.nCodeSlots); | 282 | try writer.writeInt(u32, self.inner.nCodeSlots, .big); |
| 283 | try writer.writeIntBig(u32, self.inner.codeLimit); | 283 | try writer.writeInt(u32, self.inner.codeLimit, .big); |
| 284 | try writer.writeByte(self.inner.hashSize); | 284 | try writer.writeByte(self.inner.hashSize); |
| 285 | try writer.writeByte(self.inner.hashType); | 285 | try writer.writeByte(self.inner.hashType); |
| 286 | try writer.writeByte(self.inner.platform); | 286 | try writer.writeByte(self.inner.platform); |
| 287 | try writer.writeByte(self.inner.pageSize); | 287 | try writer.writeByte(self.inner.pageSize); |
| 288 | try writer.writeIntBig(u32, self.inner.spare2); | 288 | try writer.writeInt(u32, self.inner.spare2, .big); |
| 289 | try writer.writeIntBig(u32, self.inner.scatterOffset); | 289 | try writer.writeInt(u32, self.inner.scatterOffset, .big); |
| 290 | try writer.writeIntBig(u32, self.inner.teamOffset); | 290 | try writer.writeInt(u32, self.inner.teamOffset, .big); |
| 291 | try writer.writeIntBig(u32, self.inner.spare3); | 291 | try writer.writeInt(u32, self.inner.spare3, .big); |
| 292 | try writer.writeIntBig(u64, self.inner.codeLimit64); | 292 | try writer.writeInt(u64, self.inner.codeLimit64, .big); |
| 293 | try writer.writeIntBig(u64, self.inner.execSegBase); | 293 | try writer.writeInt(u64, self.inner.execSegBase, .big); |
| 294 | try writer.writeIntBig(u64, self.inner.execSegLimit); | 294 | try writer.writeInt(u64, self.inner.execSegLimit, .big); |
| 295 | try writer.writeIntBig(u64, self.inner.execSegFlags); | 295 | try writer.writeInt(u64, self.inner.execSegFlags, .big); |
| 296 | 296 | ||
| 297 | try writer.writeAll(self.ident); | 297 | try writer.writeAll(self.ident); |
| 298 | try writer.writeByte(0); | 298 | try writer.writeByte(0); |
| ... | @@ -325,9 +325,9 @@ const Requirements = struct { | ... | @@ -325,9 +325,9 @@ const Requirements = struct { |
| 325 | } | 325 | } |
| 326 | 326 | ||
| 327 | fn write(self: Requirements, writer: anytype) !void { | 327 | fn write(self: Requirements, writer: anytype) !void { |
| 328 | try writer.writeIntBig(u32, macho.CSMAGIC_REQUIREMENTS); | 328 | try writer.writeInt(u32, macho.CSMAGIC_REQUIREMENTS, .big); |
| 329 | try writer.writeIntBig(u32, self.size()); | 329 | try writer.writeInt(u32, self.size(), .big); |
| 330 | try writer.writeIntBig(u32, 0); | 330 | try writer.writeInt(u32, 0, .big); |
| 331 | } | 331 | } |
| 332 | }; | 332 | }; |
| 333 | 333 | ||
| ... | @@ -348,8 +348,8 @@ const Entitlements = struct { | ... | @@ -348,8 +348,8 @@ const Entitlements = struct { |
| 348 | } | 348 | } |
| 349 | 349 | ||
| 350 | fn write(self: Entitlements, writer: anytype) !void { | 350 | fn write(self: Entitlements, writer: anytype) !void { |
| 351 | try writer.writeIntBig(u32, macho.CSMAGIC_EMBEDDED_ENTITLEMENTS); | 351 | try writer.writeInt(u32, macho.CSMAGIC_EMBEDDED_ENTITLEMENTS, .big); |
| 352 | try writer.writeIntBig(u32, self.size()); | 352 | try writer.writeInt(u32, self.size(), .big); |
| 353 | try writer.writeAll(self.inner); | 353 | try writer.writeAll(self.inner); |
| 354 | } | 354 | } |
| 355 | }; | 355 | }; |
| ... | @@ -371,8 +371,8 @@ const Signature = struct { | ... | @@ -371,8 +371,8 @@ const Signature = struct { |
| 371 | } | 371 | } |
| 372 | 372 | ||
| 373 | fn write(self: Signature, writer: anytype) !void { | 373 | fn write(self: Signature, writer: anytype) !void { |
| 374 | try writer.writeIntBig(u32, macho.CSMAGIC_BLOBWRAPPER); | 374 | try writer.writeInt(u32, macho.CSMAGIC_BLOBWRAPPER, .big); |
| 375 | try writer.writeIntBig(u32, self.size()); | 375 | try writer.writeInt(u32, self.size(), .big); |
| 376 | } | 376 | } |
| 377 | }; | 377 | }; |
| 378 | 378 |
src/link/MachO/DwarfInfo.zig+17-17| ... | @@ -121,19 +121,19 @@ pub const CompileUnit = struct { | ... | @@ -121,19 +121,19 @@ pub const CompileUnit = struct { |
| 121 | address_size: u8, | 121 | address_size: u8, |
| 122 | 122 | ||
| 123 | fn read(reader: anytype) !Header { | 123 | fn read(reader: anytype) !Header { |
| 124 | var length: u64 = try reader.readIntLittle(u32); | 124 | var length: u64 = try reader.readInt(u32, .little); |
| 125 | 125 | ||
| 126 | const is_64bit = length == 0xffffffff; | 126 | const is_64bit = length == 0xffffffff; |
| 127 | if (is_64bit) { | 127 | if (is_64bit) { |
| 128 | length = try reader.readIntLittle(u64); | 128 | length = try reader.readInt(u64, .little); |
| 129 | } | 129 | } |
| 130 | 130 | ||
| 131 | const version = try reader.readIntLittle(u16); | 131 | const version = try reader.readInt(u16, .little); |
| 132 | const debug_abbrev_offset = if (is_64bit) | 132 | const debug_abbrev_offset = if (is_64bit) |
| 133 | try reader.readIntLittle(u64) | 133 | try reader.readInt(u64, .little) |
| 134 | else | 134 | else |
| 135 | try reader.readIntLittle(u32); | 135 | try reader.readInt(u32, .little); |
| 136 | const address_size = try reader.readIntLittle(u8); | 136 | const address_size = try reader.readInt(u8, .little); |
| 137 | 137 | ||
| 138 | return Header{ | 138 | return Header{ |
| 139 | .is_64bit = is_64bit, | 139 | .is_64bit = is_64bit, |
| ... | @@ -251,9 +251,9 @@ pub const Attribute = struct { | ... | @@ -251,9 +251,9 @@ pub const Attribute = struct { |
| 251 | }, | 251 | }, |
| 252 | dwarf.FORM.strp => { | 252 | dwarf.FORM.strp => { |
| 253 | const off = if (cuh.is_64bit) | 253 | const off = if (cuh.is_64bit) |
| 254 | mem.readIntLittle(u64, debug_info[0..8]) | 254 | mem.readInt(u64, debug_info[0..8], .little) |
| 255 | else | 255 | else |
| 256 | mem.readIntLittle(u32, debug_info[0..4]); | 256 | mem.readInt(u32, debug_info[0..4], .little); |
| 257 | return ctx.getString(off); | 257 | return ctx.getString(off); |
| 258 | }, | 258 | }, |
| 259 | else => return null, | 259 | else => return null, |
| ... | @@ -267,9 +267,9 @@ pub const Attribute = struct { | ... | @@ -267,9 +267,9 @@ pub const Attribute = struct { |
| 267 | 267 | ||
| 268 | return switch (self.form) { | 268 | return switch (self.form) { |
| 269 | dwarf.FORM.data1 => debug_info[0], | 269 | dwarf.FORM.data1 => debug_info[0], |
| 270 | dwarf.FORM.data2 => mem.readIntLittle(u16, debug_info[0..2]), | 270 | dwarf.FORM.data2 => mem.readInt(u16, debug_info[0..2], .little), |
| 271 | dwarf.FORM.data4 => mem.readIntLittle(u32, debug_info[0..4]), | 271 | dwarf.FORM.data4 => mem.readInt(u32, debug_info[0..4], .little), |
| 272 | dwarf.FORM.data8 => mem.readIntLittle(u64, debug_info[0..8]), | 272 | dwarf.FORM.data8 => mem.readInt(u64, debug_info[0..8], .little), |
| 273 | dwarf.FORM.udata => try leb.readULEB128(u64, reader), | 273 | dwarf.FORM.udata => try leb.readULEB128(u64, reader), |
| 274 | dwarf.FORM.sdata => try leb.readILEB128(i64, reader), | 274 | dwarf.FORM.sdata => try leb.readILEB128(i64, reader), |
| 275 | else => null, | 275 | else => null, |
| ... | @@ -281,9 +281,9 @@ pub const Attribute = struct { | ... | @@ -281,9 +281,9 @@ pub const Attribute = struct { |
| 281 | const debug_info = self.getDebugInfo(ctx); | 281 | const debug_info = self.getDebugInfo(ctx); |
| 282 | return switch (cuh.address_size) { | 282 | return switch (cuh.address_size) { |
| 283 | 1 => debug_info[0], | 283 | 1 => debug_info[0], |
| 284 | 2 => mem.readIntLittle(u16, debug_info[0..2]), | 284 | 2 => mem.readInt(u16, debug_info[0..2], .little), |
| 285 | 4 => mem.readIntLittle(u32, debug_info[0..4]), | 285 | 4 => mem.readInt(u32, debug_info[0..4], .little), |
| 286 | 8 => mem.readIntLittle(u64, debug_info[0..8]), | 286 | 8 => mem.readInt(u64, debug_info[0..8], .little), |
| 287 | else => unreachable, | 287 | else => unreachable, |
| 288 | }; | 288 | }; |
| 289 | } | 289 | } |
| ... | @@ -380,9 +380,9 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head | ... | @@ -380,9 +380,9 @@ fn findFormSize(self: DwarfInfo, form: u64, di_off: usize, cuh: CompileUnit.Head |
| 380 | dwarf.FORM.block, | 380 | dwarf.FORM.block, |
| 381 | => { | 381 | => { |
| 382 | const len: u64 = switch (form) { | 382 | const len: u64 = switch (form) { |
| 383 | dwarf.FORM.block1 => try reader.readIntLittle(u8), | 383 | dwarf.FORM.block1 => try reader.readInt(u8, .little), |
| 384 | dwarf.FORM.block2 => try reader.readIntLittle(u16), | 384 | dwarf.FORM.block2 => try reader.readInt(u16, .little), |
| 385 | dwarf.FORM.block4 => try reader.readIntLittle(u32), | 385 | dwarf.FORM.block4 => try reader.readInt(u32, .little), |
| 386 | dwarf.FORM.block => try leb.readULEB128(u64, reader), | 386 | dwarf.FORM.block => try leb.readULEB128(u64, reader), |
| 387 | else => unreachable, | 387 | else => unreachable, |
| 388 | }; | 388 | }; |
src/link/MachO/Relocation.zig+9-9| ... | @@ -128,7 +128,7 @@ fn resolveAarch64(self: Relocation, source_addr: u64, target_addr: i64, code: [] | ... | @@ -128,7 +128,7 @@ fn resolveAarch64(self: Relocation, source_addr: u64, target_addr: i64, code: [] |
| 128 | ), buffer[0..4]), | 128 | ), buffer[0..4]), |
| 129 | }; | 129 | }; |
| 130 | inst.unconditional_branch_immediate.imm26 = @as(u26, @truncate(@as(u28, @bitCast(displacement >> 2)))); | 130 | inst.unconditional_branch_immediate.imm26 = @as(u26, @truncate(@as(u28, @bitCast(displacement >> 2)))); |
| 131 | mem.writeIntLittle(u32, buffer[0..4], inst.toU32()); | 131 | mem.writeInt(u32, buffer[0..4], inst.toU32(), .little); |
| 132 | }, | 132 | }, |
| 133 | .page, .got_page => { | 133 | .page, .got_page => { |
| 134 | const source_page = @as(i32, @intCast(source_addr >> 12)); | 134 | const source_page = @as(i32, @intCast(source_addr >> 12)); |
| ... | @@ -142,7 +142,7 @@ fn resolveAarch64(self: Relocation, source_addr: u64, target_addr: i64, code: [] | ... | @@ -142,7 +142,7 @@ fn resolveAarch64(self: Relocation, source_addr: u64, target_addr: i64, code: [] |
| 142 | }; | 142 | }; |
| 143 | inst.pc_relative_address.immhi = @as(u19, @truncate(pages >> 2)); | 143 | inst.pc_relative_address.immhi = @as(u19, @truncate(pages >> 2)); |
| 144 | inst.pc_relative_address.immlo = @as(u2, @truncate(pages)); | 144 | inst.pc_relative_address.immlo = @as(u2, @truncate(pages)); |
| 145 | mem.writeIntLittle(u32, buffer[0..4], inst.toU32()); | 145 | mem.writeInt(u32, buffer[0..4], inst.toU32(), .little); |
| 146 | }, | 146 | }, |
| 147 | .pageoff, .got_pageoff => { | 147 | .pageoff, .got_pageoff => { |
| 148 | const narrowed = @as(u12, @truncate(@as(u64, @intCast(target_addr)))); | 148 | const narrowed = @as(u12, @truncate(@as(u64, @intCast(target_addr)))); |
| ... | @@ -154,7 +154,7 @@ fn resolveAarch64(self: Relocation, source_addr: u64, target_addr: i64, code: [] | ... | @@ -154,7 +154,7 @@ fn resolveAarch64(self: Relocation, source_addr: u64, target_addr: i64, code: [] |
| 154 | ), buffer[0..4]), | 154 | ), buffer[0..4]), |
| 155 | }; | 155 | }; |
| 156 | inst.add_subtract_immediate.imm12 = narrowed; | 156 | inst.add_subtract_immediate.imm12 = narrowed; |
| 157 | mem.writeIntLittle(u32, buffer[0..4], inst.toU32()); | 157 | mem.writeInt(u32, buffer[0..4], inst.toU32(), .little); |
| 158 | } else { | 158 | } else { |
| 159 | var inst = aarch64.Instruction{ | 159 | var inst = aarch64.Instruction{ |
| 160 | .load_store_register = mem.bytesToValue(meta.TagPayload( | 160 | .load_store_register = mem.bytesToValue(meta.TagPayload( |
| ... | @@ -176,12 +176,12 @@ fn resolveAarch64(self: Relocation, source_addr: u64, target_addr: i64, code: [] | ... | @@ -176,12 +176,12 @@ fn resolveAarch64(self: Relocation, source_addr: u64, target_addr: i64, code: [] |
| 176 | } | 176 | } |
| 177 | }; | 177 | }; |
| 178 | inst.load_store_register.offset = offset; | 178 | inst.load_store_register.offset = offset; |
| 179 | mem.writeIntLittle(u32, buffer[0..4], inst.toU32()); | 179 | mem.writeInt(u32, buffer[0..4], inst.toU32(), .little); |
| 180 | } | 180 | } |
| 181 | }, | 181 | }, |
| 182 | .tlv_initializer, .unsigned => switch (self.length) { | 182 | .tlv_initializer, .unsigned => switch (self.length) { |
| 183 | 2 => mem.writeIntLittle(u32, buffer[0..4], @as(u32, @truncate(@as(u64, @bitCast(target_addr))))), | 183 | 2 => mem.writeInt(u32, buffer[0..4], @as(u32, @truncate(@as(u64, @bitCast(target_addr)))), .little), |
| 184 | 3 => mem.writeIntLittle(u64, buffer[0..8], @as(u64, @bitCast(target_addr))), | 184 | 3 => mem.writeInt(u64, buffer[0..8], @as(u64, @bitCast(target_addr)), .little), |
| 185 | else => unreachable, | 185 | else => unreachable, |
| 186 | }, | 186 | }, |
| 187 | .got, .signed, .tlv => unreachable, // Invalid target architecture. | 187 | .got, .signed, .tlv => unreachable, // Invalid target architecture. |
| ... | @@ -192,15 +192,15 @@ fn resolveX8664(self: Relocation, source_addr: u64, target_addr: i64, code: []u8 | ... | @@ -192,15 +192,15 @@ fn resolveX8664(self: Relocation, source_addr: u64, target_addr: i64, code: []u8 |
| 192 | switch (self.type) { | 192 | switch (self.type) { |
| 193 | .branch, .got, .tlv, .signed => { | 193 | .branch, .got, .tlv, .signed => { |
| 194 | const displacement = @as(i32, @intCast(@as(i64, @intCast(target_addr)) - @as(i64, @intCast(source_addr)) - 4)); | 194 | const displacement = @as(i32, @intCast(@as(i64, @intCast(target_addr)) - @as(i64, @intCast(source_addr)) - 4)); |
| 195 | mem.writeIntLittle(u32, code[self.offset..][0..4], @as(u32, @bitCast(displacement))); | 195 | mem.writeInt(u32, code[self.offset..][0..4], @as(u32, @bitCast(displacement)), .little); |
| 196 | }, | 196 | }, |
| 197 | .tlv_initializer, .unsigned => { | 197 | .tlv_initializer, .unsigned => { |
| 198 | switch (self.length) { | 198 | switch (self.length) { |
| 199 | 2 => { | 199 | 2 => { |
| 200 | mem.writeIntLittle(u32, code[self.offset..][0..4], @as(u32, @truncate(@as(u64, @bitCast(target_addr))))); | 200 | mem.writeInt(u32, code[self.offset..][0..4], @as(u32, @truncate(@as(u64, @bitCast(target_addr)))), .little); |
| 201 | }, | 201 | }, |
| 202 | 3 => { | 202 | 3 => { |
| 203 | mem.writeIntLittle(u64, code[self.offset..][0..8], @as(u64, @bitCast(target_addr))); | 203 | mem.writeInt(u64, code[self.offset..][0..8], @as(u64, @bitCast(target_addr)), .little); |
| 204 | }, | 204 | }, |
| 205 | else => unreachable, | 205 | else => unreachable, |
| 206 | } | 206 | } |
src/link/MachO/UnwindInfo.zig+1-1| ... | @@ -149,7 +149,7 @@ const Page = struct { | ... | @@ -149,7 +149,7 @@ const Page = struct { |
| 149 | 149 | ||
| 150 | for (page.page_encodings[0..page.page_encodings_count]) |record_id| { | 150 | for (page.page_encodings[0..page.page_encodings_count]) |record_id| { |
| 151 | const enc = info.records.items[record_id].compactUnwindEncoding; | 151 | const enc = info.records.items[record_id].compactUnwindEncoding; |
| 152 | try writer.writeIntLittle(u32, enc); | 152 | try writer.writeInt(u32, enc, .little); |
| 153 | } | 153 | } |
| 154 | 154 | ||
| 155 | assert(page.count > 0); | 155 | assert(page.count > 0); |
src/link/MachO/eh_frame.zig+26-26| ... | @@ -209,7 +209,7 @@ pub fn write(macho_file: *MachO, unwind_info: *UnwindInfo) !void { | ... | @@ -209,7 +209,7 @@ pub fn write(macho_file: *MachO, unwind_info: *UnwindInfo) !void { |
| 209 | const writer = buffer.writer(); | 209 | const writer = buffer.writer(); |
| 210 | 210 | ||
| 211 | for (eh_records.values()) |record| { | 211 | for (eh_records.values()) |record| { |
| 212 | try writer.writeIntLittle(u32, record.size); | 212 | try writer.writeInt(u32, record.size, .little); |
| 213 | try buffer.appendSlice(record.data); | 213 | try buffer.appendSlice(record.data); |
| 214 | } | 214 | } |
| 215 | 215 | ||
| ... | @@ -259,7 +259,7 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { | ... | @@ -259,7 +259,7 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { |
| 259 | base_offset: u64, | 259 | base_offset: u64, |
| 260 | }) u64 { | 260 | }) u64 { |
| 261 | assert(rec.tag == .fde); | 261 | assert(rec.tag == .fde); |
| 262 | const addend = mem.readIntLittle(i64, rec.data[4..][0..8]); | 262 | const addend = mem.readInt(i64, rec.data[4..][0..8], .little); |
| 263 | return @as(u64, @intCast(@as(i64, @intCast(ctx.base_addr + ctx.base_offset + 8)) + addend)); | 263 | return @as(u64, @intCast(@as(i64, @intCast(ctx.base_addr + ctx.base_offset + 8)) + addend)); |
| 264 | } | 264 | } |
| 265 | 265 | ||
| ... | @@ -269,7 +269,7 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { | ... | @@ -269,7 +269,7 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { |
| 269 | }) !void { | 269 | }) !void { |
| 270 | assert(rec.tag == .fde); | 270 | assert(rec.tag == .fde); |
| 271 | const addend = @as(i64, @intCast(value)) - @as(i64, @intCast(ctx.base_addr + ctx.base_offset + 8)); | 271 | const addend = @as(i64, @intCast(value)) - @as(i64, @intCast(ctx.base_addr + ctx.base_offset + 8)); |
| 272 | mem.writeIntLittle(i64, rec.data[4..][0..8], addend); | 272 | mem.writeInt(i64, rec.data[4..][0..8], addend, .little); |
| 273 | } | 273 | } |
| 274 | 274 | ||
| 275 | pub fn getPersonalityPointerReloc( | 275 | pub fn getPersonalityPointerReloc( |
| ... | @@ -343,13 +343,13 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { | ... | @@ -343,13 +343,13 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { |
| 343 | const target_addr = macho_file.getGotEntryAddress(target).?; | 343 | const target_addr = macho_file.getGotEntryAddress(target).?; |
| 344 | const result = math.cast(i32, @as(i64, @intCast(target_addr)) - @as(i64, @intCast(source_addr))) orelse | 344 | const result = math.cast(i32, @as(i64, @intCast(target_addr)) - @as(i64, @intCast(source_addr))) orelse |
| 345 | return error.Overflow; | 345 | return error.Overflow; |
| 346 | mem.writeIntLittle(i32, rec.data[rel_offset..][0..4], result); | 346 | mem.writeInt(i32, rec.data[rel_offset..][0..4], result, .little); |
| 347 | }, | 347 | }, |
| 348 | .ARM64_RELOC_UNSIGNED => { | 348 | .ARM64_RELOC_UNSIGNED => { |
| 349 | assert(rel.r_extern == 1); | 349 | assert(rel.r_extern == 1); |
| 350 | const target_addr = Atom.getRelocTargetAddress(macho_file, target, false); | 350 | const target_addr = Atom.getRelocTargetAddress(macho_file, target, false); |
| 351 | const result = @as(i64, @intCast(target_addr)) - @as(i64, @intCast(source_addr)); | 351 | const result = @as(i64, @intCast(target_addr)) - @as(i64, @intCast(source_addr)); |
| 352 | mem.writeIntLittle(i64, rec.data[rel_offset..][0..8], @as(i64, @intCast(result))); | 352 | mem.writeInt(i64, rec.data[rel_offset..][0..8], @as(i64, @intCast(result)), .little); |
| 353 | }, | 353 | }, |
| 354 | else => unreachable, | 354 | else => unreachable, |
| 355 | } | 355 | } |
| ... | @@ -359,10 +359,10 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { | ... | @@ -359,10 +359,10 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { |
| 359 | switch (rel_type) { | 359 | switch (rel_type) { |
| 360 | .X86_64_RELOC_GOT => { | 360 | .X86_64_RELOC_GOT => { |
| 361 | const target_addr = macho_file.getGotEntryAddress(target).?; | 361 | const target_addr = macho_file.getGotEntryAddress(target).?; |
| 362 | const addend = mem.readIntLittle(i32, rec.data[rel_offset..][0..4]); | 362 | const addend = mem.readInt(i32, rec.data[rel_offset..][0..4], .little); |
| 363 | const adjusted_target_addr = @as(u64, @intCast(@as(i64, @intCast(target_addr)) + addend)); | 363 | const adjusted_target_addr = @as(u64, @intCast(@as(i64, @intCast(target_addr)) + addend)); |
| 364 | const disp = try Relocation.calcPcRelativeDisplacementX86(source_addr, adjusted_target_addr, 0); | 364 | const disp = try Relocation.calcPcRelativeDisplacementX86(source_addr, adjusted_target_addr, 0); |
| 365 | mem.writeIntLittle(i32, rec.data[rel_offset..][0..4], disp); | 365 | mem.writeInt(i32, rec.data[rel_offset..][0..4], disp, .little); |
| 366 | }, | 366 | }, |
| 367 | else => unreachable, | 367 | else => unreachable, |
| 368 | } | 368 | } |
| ... | @@ -375,7 +375,7 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { | ... | @@ -375,7 +375,7 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { |
| 375 | pub fn getCiePointerSource(rec: Record, object_id: u32, macho_file: *MachO, offset: u32) u32 { | 375 | pub fn getCiePointerSource(rec: Record, object_id: u32, macho_file: *MachO, offset: u32) u32 { |
| 376 | assert(rec.tag == .fde); | 376 | assert(rec.tag == .fde); |
| 377 | const cpu_arch = macho_file.base.options.target.cpu.arch; | 377 | const cpu_arch = macho_file.base.options.target.cpu.arch; |
| 378 | const addend = mem.readIntLittle(u32, rec.data[0..4]); | 378 | const addend = mem.readInt(u32, rec.data[0..4], .little); |
| 379 | switch (cpu_arch) { | 379 | switch (cpu_arch) { |
| 380 | .aarch64 => { | 380 | .aarch64 => { |
| 381 | const relocs = getRelocs(macho_file, object_id, offset); | 381 | const relocs = getRelocs(macho_file, object_id, offset); |
| ... | @@ -397,12 +397,12 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { | ... | @@ -397,12 +397,12 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { |
| 397 | 397 | ||
| 398 | pub fn getCiePointer(rec: Record) u32 { | 398 | pub fn getCiePointer(rec: Record) u32 { |
| 399 | assert(rec.tag == .fde); | 399 | assert(rec.tag == .fde); |
| 400 | return mem.readIntLittle(u32, rec.data[0..4]); | 400 | return mem.readInt(u32, rec.data[0..4], .little); |
| 401 | } | 401 | } |
| 402 | 402 | ||
| 403 | pub fn setCiePointer(rec: *Record, ptr: u32) void { | 403 | pub fn setCiePointer(rec: *Record, ptr: u32) void { |
| 404 | assert(rec.tag == .fde); | 404 | assert(rec.tag == .fde); |
| 405 | mem.writeIntLittle(u32, rec.data[0..4], ptr); | 405 | mem.writeInt(u32, rec.data[0..4], ptr, .little); |
| 406 | } | 406 | } |
| 407 | 407 | ||
| 408 | pub fn getAugmentationString(rec: Record) []const u8 { | 408 | pub fn getAugmentationString(rec: Record) []const u8 { |
| ... | @@ -509,14 +509,14 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { | ... | @@ -509,14 +509,14 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { |
| 509 | if (enc == EH_PE.omit) return null; | 509 | if (enc == EH_PE.omit) return null; |
| 510 | 510 | ||
| 511 | var ptr: i64 = switch (enc & 0x0F) { | 511 | var ptr: i64 = switch (enc & 0x0F) { |
| 512 | EH_PE.absptr => @as(i64, @bitCast(try reader.readIntLittle(u64))), | 512 | EH_PE.absptr => @as(i64, @bitCast(try reader.readInt(u64, .little))), |
| 513 | EH_PE.udata2 => @as(i16, @bitCast(try reader.readIntLittle(u16))), | 513 | EH_PE.udata2 => @as(i16, @bitCast(try reader.readInt(u16, .little))), |
| 514 | EH_PE.udata4 => @as(i32, @bitCast(try reader.readIntLittle(u32))), | 514 | EH_PE.udata4 => @as(i32, @bitCast(try reader.readInt(u32, .little))), |
| 515 | EH_PE.udata8 => @as(i64, @bitCast(try reader.readIntLittle(u64))), | 515 | EH_PE.udata8 => @as(i64, @bitCast(try reader.readInt(u64, .little))), |
| 516 | EH_PE.uleb128 => @as(i64, @bitCast(try leb.readULEB128(u64, reader))), | 516 | EH_PE.uleb128 => @as(i64, @bitCast(try leb.readULEB128(u64, reader))), |
| 517 | EH_PE.sdata2 => try reader.readIntLittle(i16), | 517 | EH_PE.sdata2 => try reader.readInt(i16, .little), |
| 518 | EH_PE.sdata4 => try reader.readIntLittle(i32), | 518 | EH_PE.sdata4 => try reader.readInt(i32, .little), |
| 519 | EH_PE.sdata8 => try reader.readIntLittle(i64), | 519 | EH_PE.sdata8 => try reader.readInt(i64, .little), |
| 520 | EH_PE.sleb128 => try leb.readILEB128(i64, reader), | 520 | EH_PE.sleb128 => try leb.readILEB128(i64, reader), |
| 521 | else => return null, | 521 | else => return null, |
| 522 | }; | 522 | }; |
| ... | @@ -552,14 +552,14 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { | ... | @@ -552,14 +552,14 @@ pub fn EhFrameRecord(comptime is_mutable: bool) type { |
| 552 | } | 552 | } |
| 553 | 553 | ||
| 554 | switch (enc & 0x0F) { | 554 | switch (enc & 0x0F) { |
| 555 | EH_PE.absptr => try writer.writeIntLittle(u64, @as(u64, @bitCast(actual))), | 555 | EH_PE.absptr => try writer.writeInt(u64, @as(u64, @bitCast(actual)), .little), |
| 556 | EH_PE.udata2 => try writer.writeIntLittle(u16, @as(u16, @bitCast(@as(i16, @intCast(actual))))), | 556 | EH_PE.udata2 => try writer.writeInt(u16, @as(u16, @bitCast(@as(i16, @intCast(actual)))), .little), |
| 557 | EH_PE.udata4 => try writer.writeIntLittle(u32, @as(u32, @bitCast(@as(i32, @intCast(actual))))), | 557 | EH_PE.udata4 => try writer.writeInt(u32, @as(u32, @bitCast(@as(i32, @intCast(actual)))), .little), |
| 558 | EH_PE.udata8 => try writer.writeIntLittle(u64, @as(u64, @bitCast(actual))), | 558 | EH_PE.udata8 => try writer.writeInt(u64, @as(u64, @bitCast(actual)), .little), |
| 559 | EH_PE.uleb128 => try leb.writeULEB128(writer, @as(u64, @bitCast(actual))), | 559 | EH_PE.uleb128 => try leb.writeULEB128(writer, @as(u64, @bitCast(actual))), |
| 560 | EH_PE.sdata2 => try writer.writeIntLittle(i16, @as(i16, @intCast(actual))), | 560 | EH_PE.sdata2 => try writer.writeInt(i16, @as(i16, @intCast(actual)), .little), |
| 561 | EH_PE.sdata4 => try writer.writeIntLittle(i32, @as(i32, @intCast(actual))), | 561 | EH_PE.sdata4 => try writer.writeInt(i32, @as(i32, @intCast(actual)), .little), |
| 562 | EH_PE.sdata8 => try writer.writeIntLittle(i64, actual), | 562 | EH_PE.sdata8 => try writer.writeInt(i64, actual, .little), |
| 563 | EH_PE.sleb128 => try leb.writeILEB128(writer, actual), | 563 | EH_PE.sleb128 => try leb.writeILEB128(writer, actual), |
| 564 | else => unreachable, | 564 | else => unreachable, |
| 565 | } | 565 | } |
| ... | @@ -586,13 +586,13 @@ pub const Iterator = struct { | ... | @@ -586,13 +586,13 @@ pub const Iterator = struct { |
| 586 | var stream = std.io.fixedBufferStream(it.data[it.pos..]); | 586 | var stream = std.io.fixedBufferStream(it.data[it.pos..]); |
| 587 | const reader = stream.reader(); | 587 | const reader = stream.reader(); |
| 588 | 588 | ||
| 589 | var size = try reader.readIntLittle(u32); | 589 | var size = try reader.readInt(u32, .little); |
| 590 | if (size == 0xFFFFFFFF) { | 590 | if (size == 0xFFFFFFFF) { |
| 591 | log.debug("MachO doesn't support 64bit DWARF CFI __eh_frame records", .{}); | 591 | log.debug("MachO doesn't support 64bit DWARF CFI __eh_frame records", .{}); |
| 592 | return error.BadDwarfCfi; | 592 | return error.BadDwarfCfi; |
| 593 | } | 593 | } |
| 594 | 594 | ||
| 595 | const id = try reader.readIntLittle(u32); | 595 | const id = try reader.readInt(u32, .little); |
| 596 | const tag: EhFrameRecordTag = if (id == 0) .cie else .fde; | 596 | const tag: EhFrameRecordTag = if (id == 0) .cie else .fde; |
| 597 | const offset: u32 = 4; | 597 | const offset: u32 = 4; |
| 598 | const record = EhFrameRecord(false){ | 598 | const record = EhFrameRecord(false){ |
src/link/MachO/stubs.zig+19-19| ... | @@ -53,7 +53,7 @@ pub fn writeStubHelperPreambleCode(args: struct { | ... | @@ -53,7 +53,7 @@ pub fn writeStubHelperPreambleCode(args: struct { |
| 53 | args.dyld_private_addr, | 53 | args.dyld_private_addr, |
| 54 | 0, | 54 | 0, |
| 55 | ); | 55 | ); |
| 56 | try writer.writeIntLittle(i32, disp); | 56 | try writer.writeInt(i32, disp, .little); |
| 57 | } | 57 | } |
| 58 | try writer.writeAll(&.{ 0x41, 0x53, 0xff, 0x25 }); | 58 | try writer.writeAll(&.{ 0x41, 0x53, 0xff, 0x25 }); |
| 59 | { | 59 | { |
| ... | @@ -62,37 +62,37 @@ pub fn writeStubHelperPreambleCode(args: struct { | ... | @@ -62,37 +62,37 @@ pub fn writeStubHelperPreambleCode(args: struct { |
| 62 | args.dyld_stub_binder_got_addr, | 62 | args.dyld_stub_binder_got_addr, |
| 63 | 0, | 63 | 0, |
| 64 | ); | 64 | ); |
| 65 | try writer.writeIntLittle(i32, disp); | 65 | try writer.writeInt(i32, disp, .little); |
| 66 | } | 66 | } |
| 67 | }, | 67 | }, |
| 68 | .aarch64 => { | 68 | .aarch64 => { |
| 69 | { | 69 | { |
| 70 | const pages = Relocation.calcNumberOfPages(args.source_addr, args.dyld_private_addr); | 70 | const pages = Relocation.calcNumberOfPages(args.source_addr, args.dyld_private_addr); |
| 71 | try writer.writeIntLittle(u32, aarch64.Instruction.adrp(.x17, pages).toU32()); | 71 | try writer.writeInt(u32, aarch64.Instruction.adrp(.x17, pages).toU32(), .little); |
| 72 | } | 72 | } |
| 73 | { | 73 | { |
| 74 | const off = try Relocation.calcPageOffset(args.dyld_private_addr, .arithmetic); | 74 | const off = try Relocation.calcPageOffset(args.dyld_private_addr, .arithmetic); |
| 75 | try writer.writeIntLittle(u32, aarch64.Instruction.add(.x17, .x17, off, false).toU32()); | 75 | try writer.writeInt(u32, aarch64.Instruction.add(.x17, .x17, off, false).toU32(), .little); |
| 76 | } | 76 | } |
| 77 | try writer.writeIntLittle(u32, aarch64.Instruction.stp( | 77 | try writer.writeInt(u32, aarch64.Instruction.stp( |
| 78 | .x16, | 78 | .x16, |
| 79 | .x17, | 79 | .x17, |
| 80 | aarch64.Register.sp, | 80 | aarch64.Register.sp, |
| 81 | aarch64.Instruction.LoadStorePairOffset.pre_index(-16), | 81 | aarch64.Instruction.LoadStorePairOffset.pre_index(-16), |
| 82 | ).toU32()); | 82 | ).toU32(), .little); |
| 83 | { | 83 | { |
| 84 | const pages = Relocation.calcNumberOfPages(args.source_addr + 12, args.dyld_stub_binder_got_addr); | 84 | const pages = Relocation.calcNumberOfPages(args.source_addr + 12, args.dyld_stub_binder_got_addr); |
| 85 | try writer.writeIntLittle(u32, aarch64.Instruction.adrp(.x16, pages).toU32()); | 85 | try writer.writeInt(u32, aarch64.Instruction.adrp(.x16, pages).toU32(), .little); |
| 86 | } | 86 | } |
| 87 | { | 87 | { |
| 88 | const off = try Relocation.calcPageOffset(args.dyld_stub_binder_got_addr, .load_store_64); | 88 | const off = try Relocation.calcPageOffset(args.dyld_stub_binder_got_addr, .load_store_64); |
| 89 | try writer.writeIntLittle(u32, aarch64.Instruction.ldr( | 89 | try writer.writeInt(u32, aarch64.Instruction.ldr( |
| 90 | .x16, | 90 | .x16, |
| 91 | .x16, | 91 | .x16, |
| 92 | aarch64.Instruction.LoadStoreOffset.imm(off), | 92 | aarch64.Instruction.LoadStoreOffset.imm(off), |
| 93 | ).toU32()); | 93 | ).toU32(), .little); |
| 94 | } | 94 | } |
| 95 | try writer.writeIntLittle(u32, aarch64.Instruction.br(.x16).toU32()); | 95 | try writer.writeInt(u32, aarch64.Instruction.br(.x16).toU32(), .little); |
| 96 | }, | 96 | }, |
| 97 | else => unreachable, | 97 | else => unreachable, |
| 98 | } | 98 | } |
| ... | @@ -108,7 +108,7 @@ pub fn writeStubHelperCode(args: struct { | ... | @@ -108,7 +108,7 @@ pub fn writeStubHelperCode(args: struct { |
| 108 | try writer.writeAll(&.{ 0x68, 0x0, 0x0, 0x0, 0x0, 0xe9 }); | 108 | try writer.writeAll(&.{ 0x68, 0x0, 0x0, 0x0, 0x0, 0xe9 }); |
| 109 | { | 109 | { |
| 110 | const disp = try Relocation.calcPcRelativeDisplacementX86(args.source_addr + 6, args.target_addr, 0); | 110 | const disp = try Relocation.calcPcRelativeDisplacementX86(args.source_addr + 6, args.target_addr, 0); |
| 111 | try writer.writeIntLittle(i32, disp); | 111 | try writer.writeInt(i32, disp, .little); |
| 112 | } | 112 | } |
| 113 | }, | 113 | }, |
| 114 | .aarch64 => { | 114 | .aarch64 => { |
| ... | @@ -117,13 +117,13 @@ pub fn writeStubHelperCode(args: struct { | ... | @@ -117,13 +117,13 @@ pub fn writeStubHelperCode(args: struct { |
| 117 | const div_res = try std.math.divExact(u64, stub_size - @sizeOf(u32), 4); | 117 | const div_res = try std.math.divExact(u64, stub_size - @sizeOf(u32), 4); |
| 118 | break :blk std.math.cast(u18, div_res) orelse return error.Overflow; | 118 | break :blk std.math.cast(u18, div_res) orelse return error.Overflow; |
| 119 | }; | 119 | }; |
| 120 | try writer.writeIntLittle(u32, aarch64.Instruction.ldrLiteral( | 120 | try writer.writeInt(u32, aarch64.Instruction.ldrLiteral( |
| 121 | .w16, | 121 | .w16, |
| 122 | literal, | 122 | literal, |
| 123 | ).toU32()); | 123 | ).toU32(), .little); |
| 124 | { | 124 | { |
| 125 | const disp = try Relocation.calcPcRelativeDisplacementArm64(args.source_addr + 4, args.target_addr); | 125 | const disp = try Relocation.calcPcRelativeDisplacementArm64(args.source_addr + 4, args.target_addr); |
| 126 | try writer.writeIntLittle(u32, aarch64.Instruction.b(disp).toU32()); | 126 | try writer.writeInt(u32, aarch64.Instruction.b(disp).toU32(), .little); |
| 127 | } | 127 | } |
| 128 | try writer.writeAll(&.{ 0x0, 0x0, 0x0, 0x0 }); | 128 | try writer.writeAll(&.{ 0x0, 0x0, 0x0, 0x0 }); |
| 129 | }, | 129 | }, |
| ... | @@ -141,23 +141,23 @@ pub fn writeStubCode(args: struct { | ... | @@ -141,23 +141,23 @@ pub fn writeStubCode(args: struct { |
| 141 | try writer.writeAll(&.{ 0xff, 0x25 }); | 141 | try writer.writeAll(&.{ 0xff, 0x25 }); |
| 142 | { | 142 | { |
| 143 | const disp = try Relocation.calcPcRelativeDisplacementX86(args.source_addr + 2, args.target_addr, 0); | 143 | const disp = try Relocation.calcPcRelativeDisplacementX86(args.source_addr + 2, args.target_addr, 0); |
| 144 | try writer.writeIntLittle(i32, disp); | 144 | try writer.writeInt(i32, disp, .little); |
| 145 | } | 145 | } |
| 146 | }, | 146 | }, |
| 147 | .aarch64 => { | 147 | .aarch64 => { |
| 148 | { | 148 | { |
| 149 | const pages = Relocation.calcNumberOfPages(args.source_addr, args.target_addr); | 149 | const pages = Relocation.calcNumberOfPages(args.source_addr, args.target_addr); |
| 150 | try writer.writeIntLittle(u32, aarch64.Instruction.adrp(.x16, pages).toU32()); | 150 | try writer.writeInt(u32, aarch64.Instruction.adrp(.x16, pages).toU32(), .little); |
| 151 | } | 151 | } |
| 152 | { | 152 | { |
| 153 | const off = try Relocation.calcPageOffset(args.target_addr, .load_store_64); | 153 | const off = try Relocation.calcPageOffset(args.target_addr, .load_store_64); |
| 154 | try writer.writeIntLittle(u32, aarch64.Instruction.ldr( | 154 | try writer.writeInt(u32, aarch64.Instruction.ldr( |
| 155 | .x16, | 155 | .x16, |
| 156 | .x16, | 156 | .x16, |
| 157 | aarch64.Instruction.LoadStoreOffset.imm(off), | 157 | aarch64.Instruction.LoadStoreOffset.imm(off), |
| 158 | ).toU32()); | 158 | ).toU32(), .little); |
| 159 | } | 159 | } |
| 160 | try writer.writeIntLittle(u32, aarch64.Instruction.br(.x16).toU32()); | 160 | try writer.writeInt(u32, aarch64.Instruction.br(.x16).toU32(), .little); |
| 161 | }, | 161 | }, |
| 162 | else => unreachable, | 162 | else => unreachable, |
| 163 | } | 163 | } |
src/link/MachO/thunks.zig+3-3| ... | @@ -349,10 +349,10 @@ pub fn writeThunkCode(macho_file: *MachO, thunk: *const Thunk, writer: anytype) | ... | @@ -349,10 +349,10 @@ pub fn writeThunkCode(macho_file: *MachO, thunk: *const Thunk, writer: anytype) |
| 349 | .atom => macho_file.getSymbol(target).n_value, | 349 | .atom => macho_file.getSymbol(target).n_value, |
| 350 | }; | 350 | }; |
| 351 | const pages = Relocation.calcNumberOfPages(source_addr, target_addr); | 351 | const pages = Relocation.calcNumberOfPages(source_addr, target_addr); |
| 352 | try writer.writeIntLittle(u32, aarch64.Instruction.adrp(.x16, pages).toU32()); | 352 | try writer.writeInt(u32, aarch64.Instruction.adrp(.x16, pages).toU32(), .little); |
| 353 | const off = try Relocation.calcPageOffset(target_addr, .arithmetic); | 353 | const off = try Relocation.calcPageOffset(target_addr, .arithmetic); |
| 354 | try writer.writeIntLittle(u32, aarch64.Instruction.add(.x16, .x16, off, false).toU32()); | 354 | try writer.writeInt(u32, aarch64.Instruction.add(.x16, .x16, off, false).toU32(), .little); |
| 355 | try writer.writeIntLittle(u32, aarch64.Instruction.br(.x16).toU32()); | 355 | try writer.writeInt(u32, aarch64.Instruction.br(.x16).toU32(), .little); |
| 356 | } | 356 | } |
| 357 | } | 357 | } |
| 358 | 358 |
src/link/MachO/zld.zig+2-2| ... | @@ -796,7 +796,7 @@ fn writePointerEntries(macho_file: *MachO, sect_id: u8, table: anytype) !void { | ... | @@ -796,7 +796,7 @@ fn writePointerEntries(macho_file: *MachO, sect_id: u8, table: anytype) !void { |
| 796 | defer buffer.deinit(); | 796 | defer buffer.deinit(); |
| 797 | for (table.entries.items) |entry| { | 797 | for (table.entries.items) |entry| { |
| 798 | const sym = macho_file.getSymbol(entry); | 798 | const sym = macho_file.getSymbol(entry); |
| 799 | buffer.writer().writeIntLittle(u64, sym.n_value) catch unreachable; | 799 | buffer.writer().writeInt(u64, sym.n_value, .little) catch unreachable; |
| 800 | } | 800 | } |
| 801 | log.debug("writing __DATA_CONST,__got contents at file offset 0x{x}", .{header.offset}); | 801 | log.debug("writing __DATA_CONST,__got contents at file offset 0x{x}", .{header.offset}); |
| 802 | try macho_file.base.file.?.pwriteAll(buffer.items, header.offset); | 802 | try macho_file.base.file.?.pwriteAll(buffer.items, header.offset); |
| ... | @@ -880,7 +880,7 @@ fn writeLaSymbolPtrs(macho_file: *MachO) !void { | ... | @@ -880,7 +880,7 @@ fn writeLaSymbolPtrs(macho_file: *MachO) !void { |
| 880 | for (0..macho_file.stub_table.count()) |index| { | 880 | for (0..macho_file.stub_table.count()) |index| { |
| 881 | const target_addr = stub_helper_header.addr + stubs.stubHelperPreambleSize(cpu_arch) + | 881 | const target_addr = stub_helper_header.addr + stubs.stubHelperPreambleSize(cpu_arch) + |
| 882 | stubs.stubHelperSize(cpu_arch) * index; | 882 | stubs.stubHelperSize(cpu_arch) * index; |
| 883 | buffer.writer().writeIntLittle(u64, target_addr) catch unreachable; | 883 | buffer.writer().writeInt(u64, target_addr, .little) catch unreachable; |
| 884 | } | 884 | } |
| 885 | 885 | ||
| 886 | log.debug("writing __DATA,__la_symbol_ptr contents at file offset 0x{x}", .{ | 886 | log.debug("writing __DATA,__la_symbol_ptr contents at file offset 0x{x}", .{ |
src/link/Plan9.zig+7-7| ... | @@ -348,7 +348,7 @@ fn putFn(self: *Plan9, decl_index: Module.Decl.Index, out: FnDeclOutput) !void { | ... | @@ -348,7 +348,7 @@ fn putFn(self: *Plan9, decl_index: Module.Decl.Index, out: FnDeclOutput) !void { |
| 348 | // every 'z' starts with 0 | 348 | // every 'z' starts with 0 |
| 349 | try a.append(0); | 349 | try a.append(0); |
| 350 | // path component value of '/' | 350 | // path component value of '/' |
| 351 | try a.writer().writeIntBig(u16, 1); | 351 | try a.writer().writeInt(u16, 1, .big); |
| 352 | 352 | ||
| 353 | // getting the full file path | 353 | // getting the full file path |
| 354 | var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; | 354 | var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; |
| ... | @@ -381,11 +381,11 @@ fn addPathComponents(self: *Plan9, path: []const u8, a: *std.ArrayList(u8)) !voi | ... | @@ -381,11 +381,11 @@ fn addPathComponents(self: *Plan9, path: []const u8, a: *std.ArrayList(u8)) !voi |
| 381 | var it = std.mem.tokenizeScalar(u8, path, sep); | 381 | var it = std.mem.tokenizeScalar(u8, path, sep); |
| 382 | while (it.next()) |component| { | 382 | while (it.next()) |component| { |
| 383 | if (self.file_segments.get(component)) |num| { | 383 | if (self.file_segments.get(component)) |num| { |
| 384 | try a.writer().writeIntBig(u16, num); | 384 | try a.writer().writeInt(u16, num, .big); |
| 385 | } else { | 385 | } else { |
| 386 | self.file_segments_i += 1; | 386 | self.file_segments_i += 1; |
| 387 | try self.file_segments.put(self.base.allocator, component, self.file_segments_i); | 387 | try self.file_segments.put(self.base.allocator, component, self.file_segments_i); |
| 388 | try a.writer().writeIntBig(u16, self.file_segments_i); | 388 | try a.writer().writeInt(u16, self.file_segments_i, .big); |
| 389 | } | 389 | } |
| 390 | } | 390 | } |
| 391 | } | 391 | } |
| ... | @@ -607,7 +607,7 @@ pub fn changeLine(l: *std.ArrayList(u8), delta_line: i32) !void { | ... | @@ -607,7 +607,7 @@ pub fn changeLine(l: *std.ArrayList(u8), delta_line: i32) !void { |
| 607 | try l.append(toadd); | 607 | try l.append(toadd); |
| 608 | } else if (delta_line != 0) { | 608 | } else if (delta_line != 0) { |
| 609 | try l.append(0); | 609 | try l.append(0); |
| 610 | try l.writer().writeIntBig(i32, delta_line); | 610 | try l.writer().writeInt(i32, delta_line, .big); |
| 611 | } | 611 | } |
| 612 | } | 612 | } |
| 613 | 613 | ||
| ... | @@ -922,7 +922,7 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No | ... | @@ -922,7 +922,7 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No |
| 922 | @memcpy(hdr_slice, self.hdr.toU8s()[0..hdr_size]); | 922 | @memcpy(hdr_slice, self.hdr.toU8s()[0..hdr_size]); |
| 923 | // write the fat header for 64 bit entry points | 923 | // write the fat header for 64 bit entry points |
| 924 | if (self.sixtyfour_bit) { | 924 | if (self.sixtyfour_bit) { |
| 925 | mem.writeIntSliceBig(u64, hdr_buf[32..40], self.entry_val.?); | 925 | mem.writeInt(u64, hdr_buf[32..40], self.entry_val.?, .big); |
| 926 | } | 926 | } |
| 927 | // perform the relocs | 927 | // perform the relocs |
| 928 | { | 928 | { |
| ... | @@ -1311,9 +1311,9 @@ pub fn writeSym(self: *Plan9, w: anytype, sym: aout.Sym) !void { | ... | @@ -1311,9 +1311,9 @@ pub fn writeSym(self: *Plan9, w: anytype, sym: aout.Sym) !void { |
| 1311 | // log.debug("write sym{{name: {s}, value: {x}}}", .{ sym.name, sym.value }); | 1311 | // log.debug("write sym{{name: {s}, value: {x}}}", .{ sym.name, sym.value }); |
| 1312 | if (sym.type == .bad) return; // we don't want to write free'd symbols | 1312 | if (sym.type == .bad) return; // we don't want to write free'd symbols |
| 1313 | if (!self.sixtyfour_bit) { | 1313 | if (!self.sixtyfour_bit) { |
| 1314 | try w.writeIntBig(u32, @as(u32, @intCast(sym.value))); | 1314 | try w.writeInt(u32, @as(u32, @intCast(sym.value)), .big); |
| 1315 | } else { | 1315 | } else { |
| 1316 | try w.writeIntBig(u64, sym.value); | 1316 | try w.writeInt(u64, sym.value, .big); |
| 1317 | } | 1317 | } |
| 1318 | try w.writeByte(@intFromEnum(sym.type)); | 1318 | try w.writeByte(@intFromEnum(sym.type)); |
| 1319 | try w.writeAll(sym.name); | 1319 | try w.writeAll(sym.name); |
src/link/Plan9/aout.zig+1-1| ... | @@ -21,7 +21,7 @@ pub const ExecHdr = extern struct { | ... | @@ -21,7 +21,7 @@ pub const ExecHdr = extern struct { |
| 21 | var buf: [40]u8 = undefined; | 21 | var buf: [40]u8 = undefined; |
| 22 | var i: u8 = 0; | 22 | var i: u8 = 0; |
| 23 | inline for (std.meta.fields(@This())) |f| { | 23 | inline for (std.meta.fields(@This())) |f| { |
| 24 | std.mem.writeIntSliceBig(u32, buf[i..][0..4], @field(self, f.name)); | 24 | std.mem.writeInt(u32, buf[i..][0..4], @field(self, f.name), .big); |
| 25 | i += 4; | 25 | i += 4; |
| 26 | } | 26 | } |
| 27 | return buf; | 27 | return buf; |
src/link/Wasm.zig+5-5| ... | @@ -2371,7 +2371,7 @@ fn setupErrorsLen(wasm: *Wasm) !void { | ... | @@ -2371,7 +2371,7 @@ fn setupErrorsLen(wasm: *Wasm) !void { |
| 2371 | atom.deinit(wasm); | 2371 | atom.deinit(wasm); |
| 2372 | break :blk index; | 2372 | break :blk index; |
| 2373 | } else new_atom: { | 2373 | } else new_atom: { |
| 2374 | const atom_index = @as(Atom.Index, @intCast(wasm.managed_atoms.items.len)); | 2374 | const atom_index: Atom.Index = @intCast(wasm.managed_atoms.items.len); |
| 2375 | try wasm.symbol_atom.put(wasm.base.allocator, loc, atom_index); | 2375 | try wasm.symbol_atom.put(wasm.base.allocator, loc, atom_index); |
| 2376 | try wasm.managed_atoms.append(wasm.base.allocator, undefined); | 2376 | try wasm.managed_atoms.append(wasm.base.allocator, undefined); |
| 2377 | break :new_atom atom_index; | 2377 | break :new_atom atom_index; |
| ... | @@ -2380,7 +2380,7 @@ fn setupErrorsLen(wasm: *Wasm) !void { | ... | @@ -2380,7 +2380,7 @@ fn setupErrorsLen(wasm: *Wasm) !void { |
| 2380 | atom.* = Atom.empty; | 2380 | atom.* = Atom.empty; |
| 2381 | atom.sym_index = loc.index; | 2381 | atom.sym_index = loc.index; |
| 2382 | atom.size = 2; | 2382 | atom.size = 2; |
| 2383 | try atom.code.writer(wasm.base.allocator).writeIntLittle(u16, @as(u16, @intCast(errors_len))); | 2383 | try atom.code.writer(wasm.base.allocator).writeInt(u16, @intCast(errors_len), .little); |
| 2384 | 2384 | ||
| 2385 | try wasm.parseAtom(atom_index, .{ .data = .read_only }); | 2385 | try wasm.parseAtom(atom_index, .{ .data = .read_only }); |
| 2386 | } | 2386 | } |
| ... | @@ -3151,7 +3151,7 @@ fn populateErrorNameTable(wasm: *Wasm) !void { | ... | @@ -3151,7 +3151,7 @@ fn populateErrorNameTable(wasm: *Wasm) !void { |
| 3151 | const offset = @as(u32, @intCast(atom.code.items.len)); | 3151 | const offset = @as(u32, @intCast(atom.code.items.len)); |
| 3152 | // first we create the data for the slice of the name | 3152 | // first we create the data for the slice of the name |
| 3153 | try atom.code.appendNTimes(wasm.base.allocator, 0, 4); // ptr to name, will be relocated | 3153 | try atom.code.appendNTimes(wasm.base.allocator, 0, 4); // ptr to name, will be relocated |
| 3154 | try atom.code.writer(wasm.base.allocator).writeIntLittle(u32, len - 1); | 3154 | try atom.code.writer(wasm.base.allocator).writeInt(u32, len - 1, .little); |
| 3155 | // create relocation to the error name | 3155 | // create relocation to the error name |
| 3156 | try atom.relocs.append(wasm.base.allocator, .{ | 3156 | try atom.relocs.append(wasm.base.allocator, .{ |
| 3157 | .index = names_atom.sym_index, | 3157 | .index = names_atom.sym_index, |
| ... | @@ -4286,11 +4286,11 @@ fn emitInit(writer: anytype, init_expr: std.wasm.InitExpression) !void { | ... | @@ -4286,11 +4286,11 @@ fn emitInit(writer: anytype, init_expr: std.wasm.InitExpression) !void { |
| 4286 | }, | 4286 | }, |
| 4287 | .f32_const => |val| { | 4287 | .f32_const => |val| { |
| 4288 | try writer.writeByte(std.wasm.opcode(.f32_const)); | 4288 | try writer.writeByte(std.wasm.opcode(.f32_const)); |
| 4289 | try writer.writeIntLittle(u32, @as(u32, @bitCast(val))); | 4289 | try writer.writeInt(u32, @bitCast(val), .little); |
| 4290 | }, | 4290 | }, |
| 4291 | .f64_const => |val| { | 4291 | .f64_const => |val| { |
| 4292 | try writer.writeByte(std.wasm.opcode(.f64_const)); | 4292 | try writer.writeByte(std.wasm.opcode(.f64_const)); |
| 4293 | try writer.writeIntLittle(u64, @as(u64, @bitCast(val))); | 4293 | try writer.writeInt(u64, @bitCast(val), .little); |
| 4294 | }, | 4294 | }, |
| 4295 | .global_get => |val| { | 4295 | .global_get => |val| { |
| 4296 | try writer.writeByte(std.wasm.opcode(.global_get)); | 4296 | try writer.writeByte(std.wasm.opcode(.global_get)); |
src/link/Wasm/Archive.zig+2-2| ... | @@ -141,11 +141,11 @@ fn parseTableOfContents(archive: *Archive, allocator: Allocator, reader: anytype | ... | @@ -141,11 +141,11 @@ fn parseTableOfContents(archive: *Archive, allocator: Allocator, reader: anytype |
| 141 | const size_trimmed = mem.trim(u8, &archive.header.ar_size, " "); | 141 | const size_trimmed = mem.trim(u8, &archive.header.ar_size, " "); |
| 142 | const sym_tab_size = try std.fmt.parseInt(u32, size_trimmed, 10); | 142 | const sym_tab_size = try std.fmt.parseInt(u32, size_trimmed, 10); |
| 143 | 143 | ||
| 144 | const num_symbols = try reader.readIntBig(u32); | 144 | const num_symbols = try reader.readInt(u32, .big); |
| 145 | const symbol_positions = try allocator.alloc(u32, num_symbols); | 145 | const symbol_positions = try allocator.alloc(u32, num_symbols); |
| 146 | defer allocator.free(symbol_positions); | 146 | defer allocator.free(symbol_positions); |
| 147 | for (symbol_positions) |*index| { | 147 | for (symbol_positions) |*index| { |
| 148 | index.* = try reader.readIntBig(u32); | 148 | index.* = try reader.readInt(u32, .big); |
| 149 | } | 149 | } |
| 150 | 150 | ||
| 151 | const sym_tab = try allocator.alloc(u8, sym_tab_size - 4 - (4 * num_symbols)); | 151 | const sym_tab = try allocator.alloc(u8, sym_tab_size - 4 - (4 * num_symbols)); |
src/link/Wasm/Atom.zig+2-2| ... | @@ -114,10 +114,10 @@ pub fn resolveRelocs(atom: *Atom, wasm_bin: *const Wasm) void { | ... | @@ -114,10 +114,10 @@ pub fn resolveRelocs(atom: *Atom, wasm_bin: *const Wasm) void { |
| 114 | .R_WASM_GLOBAL_INDEX_I32, | 114 | .R_WASM_GLOBAL_INDEX_I32, |
| 115 | .R_WASM_MEMORY_ADDR_I32, | 115 | .R_WASM_MEMORY_ADDR_I32, |
| 116 | .R_WASM_SECTION_OFFSET_I32, | 116 | .R_WASM_SECTION_OFFSET_I32, |
| 117 | => std.mem.writeIntLittle(u32, atom.code.items[reloc.offset..][0..4], @as(u32, @intCast(value))), | 117 | => std.mem.writeInt(u32, atom.code.items[reloc.offset..][0..4], @as(u32, @intCast(value)), .little), |
| 118 | .R_WASM_TABLE_INDEX_I64, | 118 | .R_WASM_TABLE_INDEX_I64, |
| 119 | .R_WASM_MEMORY_ADDR_I64, | 119 | .R_WASM_MEMORY_ADDR_I64, |
| 120 | => std.mem.writeIntLittle(u64, atom.code.items[reloc.offset..][0..8], value), | 120 | => std.mem.writeInt(u64, atom.code.items[reloc.offset..][0..8], value, .little), |
| 121 | .R_WASM_GLOBAL_INDEX_LEB, | 121 | .R_WASM_GLOBAL_INDEX_LEB, |
| 122 | .R_WASM_EVENT_INDEX_LEB, | 122 | .R_WASM_EVENT_INDEX_LEB, |
| 123 | .R_WASM_FUNCTION_INDEX_LEB, | 123 | .R_WASM_FUNCTION_INDEX_LEB, |
src/link/Wasm/Object.zig+1-1| ... | @@ -344,7 +344,7 @@ fn Parser(comptime ReaderType: type) type { | ... | @@ -344,7 +344,7 @@ fn Parser(comptime ReaderType: type) type { |
| 344 | fn parseObject(parser: *ObjectParser, gpa: Allocator, is_object_file: *bool) Error!void { | 344 | fn parseObject(parser: *ObjectParser, gpa: Allocator, is_object_file: *bool) Error!void { |
| 345 | errdefer parser.object.deinit(gpa); | 345 | errdefer parser.object.deinit(gpa); |
| 346 | try parser.verifyMagicBytes(); | 346 | try parser.verifyMagicBytes(); |
| 347 | const version = try parser.reader.reader().readIntLittle(u32); | 347 | const version = try parser.reader.reader().readInt(u32, .little); |
| 348 | 348 | ||
| 349 | parser.object.version = version; | 349 | parser.object.version = version; |
| 350 | var relocatable_data = std.ArrayList(RelocatableData).init(gpa); | 350 | var relocatable_data = std.ArrayList(RelocatableData).init(gpa); |
src/resinator/ani.zig+2-2| ... | @@ -25,14 +25,14 @@ fn getAniheaderFlags(reader: anytype) !u32 { | ... | @@ -25,14 +25,14 @@ fn getAniheaderFlags(reader: anytype) !u32 { |
| 25 | const riff_header = try reader.readBytesNoEof(4); | 25 | const riff_header = try reader.readBytesNoEof(4); |
| 26 | if (!std.mem.eql(u8, &riff_header, "RIFF")) return error.InvalidFormat; | 26 | if (!std.mem.eql(u8, &riff_header, "RIFF")) return error.InvalidFormat; |
| 27 | 27 | ||
| 28 | _ = try reader.readIntLittle(u32); // size of RIFF chunk | 28 | _ = try reader.readInt(u32, .little); // size of RIFF chunk |
| 29 | 29 | ||
| 30 | const form_type = try reader.readBytesNoEof(4); | 30 | const form_type = try reader.readBytesNoEof(4); |
| 31 | if (!std.mem.eql(u8, &form_type, "ACON")) return error.InvalidFormat; | 31 | if (!std.mem.eql(u8, &form_type, "ACON")) return error.InvalidFormat; |
| 32 | 32 | ||
| 33 | while (true) { | 33 | while (true) { |
| 34 | const chunk_id = try reader.readBytesNoEof(4); | 34 | const chunk_id = try reader.readBytesNoEof(4); |
| 35 | const chunk_len = try reader.readIntLittle(u32); | 35 | const chunk_len = try reader.readInt(u32, .little); |
| 36 | if (!std.mem.eql(u8, &chunk_id, "anih")) { | 36 | if (!std.mem.eql(u8, &chunk_id, "anih")) { |
| 37 | // TODO: Move file cursor instead of skipBytes | 37 | // TODO: Move file cursor instead of skipBytes |
| 38 | try reader.skipBytes(chunk_len, .{}); | 38 | try reader.skipBytes(chunk_len, .{}); |
src/resinator/bmp.zig+8-6| ... | @@ -20,8 +20,10 @@ | ... | @@ -20,8 +20,10 @@ |
| 20 | 20 | ||
| 21 | const std = @import("std"); | 21 | const std = @import("std"); |
| 22 | const BitmapHeader = @import("ico.zig").BitmapHeader; | 22 | const BitmapHeader = @import("ico.zig").BitmapHeader; |
| 23 | const builtin = @import("builtin"); | ||
| 24 | const native_endian = builtin.cpu.arch.endian(); | ||
| 23 | 25 | ||
| 24 | pub const windows_format_id = std.mem.readIntNative(u16, "BM"); | 26 | pub const windows_format_id = std.mem.readInt(u16, "BM", native_endian); |
| 25 | pub const file_header_len = 14; | 27 | pub const file_header_len = 14; |
| 26 | 28 | ||
| 27 | pub const ReadError = error{ | 29 | pub const ReadError = error{ |
| ... | @@ -89,19 +91,19 @@ pub fn read(reader: anytype, max_size: u64) ReadError!BitmapInfo { | ... | @@ -89,19 +91,19 @@ pub fn read(reader: anytype, max_size: u64) ReadError!BitmapInfo { |
| 89 | var bitmap_info: BitmapInfo = undefined; | 91 | var bitmap_info: BitmapInfo = undefined; |
| 90 | const file_header = reader.readBytesNoEof(file_header_len) catch return error.UnexpectedEOF; | 92 | const file_header = reader.readBytesNoEof(file_header_len) catch return error.UnexpectedEOF; |
| 91 | 93 | ||
| 92 | const id = std.mem.readIntNative(u16, file_header[0..2]); | 94 | const id = std.mem.readInt(u16, file_header[0..2], native_endian); |
| 93 | if (id != windows_format_id) return error.InvalidFileHeader; | 95 | if (id != windows_format_id) return error.InvalidFileHeader; |
| 94 | 96 | ||
| 95 | bitmap_info.pixel_data_offset = std.mem.readIntLittle(u32, file_header[10..14]); | 97 | bitmap_info.pixel_data_offset = std.mem.readInt(u32, file_header[10..14], .little); |
| 96 | if (bitmap_info.pixel_data_offset > max_size) return error.ImpossiblePixelDataOffset; | 98 | if (bitmap_info.pixel_data_offset > max_size) return error.ImpossiblePixelDataOffset; |
| 97 | 99 | ||
| 98 | bitmap_info.dib_header_size = reader.readIntLittle(u32) catch return error.UnexpectedEOF; | 100 | bitmap_info.dib_header_size = reader.readInt(u32, .little) catch return error.UnexpectedEOF; |
| 99 | if (bitmap_info.pixel_data_offset < file_header_len + bitmap_info.dib_header_size) return error.ImpossiblePixelDataOffset; | 101 | if (bitmap_info.pixel_data_offset < file_header_len + bitmap_info.dib_header_size) return error.ImpossiblePixelDataOffset; |
| 100 | const dib_version = BitmapHeader.Version.get(bitmap_info.dib_header_size); | 102 | const dib_version = BitmapHeader.Version.get(bitmap_info.dib_header_size); |
| 101 | switch (dib_version) { | 103 | switch (dib_version) { |
| 102 | .@"nt3.1", .@"nt4.0", .@"nt5.0" => { | 104 | .@"nt3.1", .@"nt4.0", .@"nt5.0" => { |
| 103 | var dib_header_buf: [@sizeOf(BITMAPINFOHEADER)]u8 align(@alignOf(BITMAPINFOHEADER)) = undefined; | 105 | var dib_header_buf: [@sizeOf(BITMAPINFOHEADER)]u8 align(@alignOf(BITMAPINFOHEADER)) = undefined; |
| 104 | std.mem.writeIntLittle(u32, dib_header_buf[0..4], bitmap_info.dib_header_size); | 106 | std.mem.writeInt(u32, dib_header_buf[0..4], bitmap_info.dib_header_size, .little); |
| 105 | reader.readNoEof(dib_header_buf[4..]) catch return error.UnexpectedEOF; | 107 | reader.readNoEof(dib_header_buf[4..]) catch return error.UnexpectedEOF; |
| 106 | var dib_header: *BITMAPINFOHEADER = @ptrCast(&dib_header_buf); | 108 | var dib_header: *BITMAPINFOHEADER = @ptrCast(&dib_header_buf); |
| 107 | structFieldsLittleToNative(BITMAPINFOHEADER, dib_header); | 109 | structFieldsLittleToNative(BITMAPINFOHEADER, dib_header); |
| ... | @@ -116,7 +118,7 @@ pub fn read(reader: anytype, max_size: u64) ReadError!BitmapInfo { | ... | @@ -116,7 +118,7 @@ pub fn read(reader: anytype, max_size: u64) ReadError!BitmapInfo { |
| 116 | }, | 118 | }, |
| 117 | .@"win2.0" => { | 119 | .@"win2.0" => { |
| 118 | var dib_header_buf: [@sizeOf(BITMAPCOREHEADER)]u8 align(@alignOf(BITMAPCOREHEADER)) = undefined; | 120 | var dib_header_buf: [@sizeOf(BITMAPCOREHEADER)]u8 align(@alignOf(BITMAPCOREHEADER)) = undefined; |
| 119 | std.mem.writeIntLittle(u32, dib_header_buf[0..4], bitmap_info.dib_header_size); | 121 | std.mem.writeInt(u32, dib_header_buf[0..4], bitmap_info.dib_header_size, .little); |
| 120 | reader.readNoEof(dib_header_buf[4..]) catch return error.UnexpectedEOF; | 122 | reader.readNoEof(dib_header_buf[4..]) catch return error.UnexpectedEOF; |
| 121 | var dib_header: *BITMAPCOREHEADER = @ptrCast(&dib_header_buf); | 123 | var dib_header: *BITMAPCOREHEADER = @ptrCast(&dib_header_buf); |
| 122 | structFieldsLittleToNative(BITMAPCOREHEADER, dib_header); | 124 | structFieldsLittleToNative(BITMAPCOREHEADER, dib_header); |
src/resinator/compile.zig+95-94| ... | @@ -28,6 +28,7 @@ const windows1252 = @import("windows1252.zig"); | ... | @@ -28,6 +28,7 @@ const windows1252 = @import("windows1252.zig"); |
| 28 | const lang = @import("lang.zig"); | 28 | const lang = @import("lang.zig"); |
| 29 | const code_pages = @import("code_pages.zig"); | 29 | const code_pages = @import("code_pages.zig"); |
| 30 | const errors = @import("errors.zig"); | 30 | const errors = @import("errors.zig"); |
| 31 | const native_endian = builtin.cpu.arch.endian(); | ||
| 31 | 32 | ||
| 32 | pub const CompileOptions = struct { | 33 | pub const CompileOptions = struct { |
| 33 | cwd: std.fs.Dir, | 34 | cwd: std.fs.Dir, |
| ... | @@ -584,8 +585,8 @@ pub const Compiler = struct { | ... | @@ -584,8 +585,8 @@ pub const Compiler = struct { |
| 584 | // > resource if a RESDIR structure contains information about a cursor. | 585 | // > resource if a RESDIR structure contains information about a cursor. |
| 585 | // where LOCALHEADER is `struct { WORD xHotSpot; WORD yHotSpot; }` | 586 | // where LOCALHEADER is `struct { WORD xHotSpot; WORD yHotSpot; }` |
| 586 | if (icon_dir.image_type == .cursor) { | 587 | if (icon_dir.image_type == .cursor) { |
| 587 | try writer.writeIntLittle(u16, entry.type_specific_data.cursor.hotspot_x); | 588 | try writer.writeInt(u16, entry.type_specific_data.cursor.hotspot_x, .little); |
| 588 | try writer.writeIntLittle(u16, entry.type_specific_data.cursor.hotspot_y); | 589 | try writer.writeInt(u16, entry.type_specific_data.cursor.hotspot_y, .little); |
| 589 | } | 590 | } |
| 590 | 591 | ||
| 591 | try file.seekTo(entry.data_offset_from_start_of_file); | 592 | try file.seekTo(entry.data_offset_from_start_of_file); |
| ... | @@ -666,7 +667,7 @@ pub const Compiler = struct { | ... | @@ -666,7 +667,7 @@ pub const Compiler = struct { |
| 666 | }, | 667 | }, |
| 667 | .dib => { | 668 | .dib => { |
| 668 | var bitmap_header: *ico.BitmapHeader = @ptrCast(@alignCast(&header_bytes)); | 669 | var bitmap_header: *ico.BitmapHeader = @ptrCast(@alignCast(&header_bytes)); |
| 669 | if (builtin.cpu.arch.endian() == .Big) { | 670 | if (native_endian == .big) { |
| 670 | std.mem.byteSwapAllFields(ico.BitmapHeader, bitmap_header); | 671 | std.mem.byteSwapAllFields(ico.BitmapHeader, bitmap_header); |
| 671 | } | 672 | } |
| 672 | const bitmap_version = ico.BitmapHeader.Version.get(bitmap_header.bcSize); | 673 | const bitmap_version = ico.BitmapHeader.Version.get(bitmap_header.bcSize); |
| ... | @@ -777,7 +778,7 @@ pub const Compiler = struct { | ... | @@ -777,7 +778,7 @@ pub const Compiler = struct { |
| 777 | if (bitmap_info.getActualPaletteByteLen() > bitmap_info.getExpectedPaletteByteLen()) { | 778 | if (bitmap_info.getActualPaletteByteLen() > bitmap_info.getExpectedPaletteByteLen()) { |
| 778 | const num_ignored_bytes = bitmap_info.getActualPaletteByteLen() - bitmap_info.getExpectedPaletteByteLen(); | 779 | const num_ignored_bytes = bitmap_info.getActualPaletteByteLen() - bitmap_info.getExpectedPaletteByteLen(); |
| 779 | var number_as_bytes: [8]u8 = undefined; | 780 | var number_as_bytes: [8]u8 = undefined; |
| 780 | std.mem.writeIntNative(u64, &number_as_bytes, num_ignored_bytes); | 781 | std.mem.writeInt(u64, &number_as_bytes, num_ignored_bytes, native_endian); |
| 781 | const value_string_index = try self.diagnostics.putString(&number_as_bytes); | 782 | const value_string_index = try self.diagnostics.putString(&number_as_bytes); |
| 782 | try self.addErrorDetails(.{ | 783 | try self.addErrorDetails(.{ |
| 783 | .err = .bmp_ignored_palette_bytes, | 784 | .err = .bmp_ignored_palette_bytes, |
| ... | @@ -792,8 +793,8 @@ pub const Compiler = struct { | ... | @@ -792,8 +793,8 @@ pub const Compiler = struct { |
| 792 | const max_missing_bytes = 4096; | 793 | const max_missing_bytes = 4096; |
| 793 | if (num_padding_bytes > max_missing_bytes) { | 794 | if (num_padding_bytes > max_missing_bytes) { |
| 794 | var numbers_as_bytes: [16]u8 = undefined; | 795 | var numbers_as_bytes: [16]u8 = undefined; |
| 795 | std.mem.writeIntNative(u64, numbers_as_bytes[0..8], num_padding_bytes); | 796 | std.mem.writeInt(u64, numbers_as_bytes[0..8], num_padding_bytes, native_endian); |
| 796 | std.mem.writeIntNative(u64, numbers_as_bytes[8..16], max_missing_bytes); | 797 | std.mem.writeInt(u64, numbers_as_bytes[8..16], max_missing_bytes, native_endian); |
| 797 | const values_string_index = try self.diagnostics.putString(&numbers_as_bytes); | 798 | const values_string_index = try self.diagnostics.putString(&numbers_as_bytes); |
| 798 | try self.addErrorDetails(.{ | 799 | try self.addErrorDetails(.{ |
| 799 | .err = .bmp_too_many_missing_palette_bytes, | 800 | .err = .bmp_too_many_missing_palette_bytes, |
| ... | @@ -809,7 +810,7 @@ pub const Compiler = struct { | ... | @@ -809,7 +810,7 @@ pub const Compiler = struct { |
| 809 | } | 810 | } |
| 810 | 811 | ||
| 811 | var number_as_bytes: [8]u8 = undefined; | 812 | var number_as_bytes: [8]u8 = undefined; |
| 812 | std.mem.writeIntNative(u64, &number_as_bytes, num_padding_bytes); | 813 | std.mem.writeInt(u64, &number_as_bytes, num_padding_bytes, native_endian); |
| 813 | const value_string_index = try self.diagnostics.putString(&number_as_bytes); | 814 | const value_string_index = try self.diagnostics.putString(&number_as_bytes); |
| 814 | try self.addErrorDetails(.{ | 815 | try self.addErrorDetails(.{ |
| 815 | .err = .bmp_missing_palette_bytes, | 816 | .err = .bmp_missing_palette_bytes, |
| ... | @@ -820,7 +821,7 @@ pub const Compiler = struct { | ... | @@ -820,7 +821,7 @@ pub const Compiler = struct { |
| 820 | const pixel_data_len = bitmap_info.getPixelDataLen(file_size); | 821 | const pixel_data_len = bitmap_info.getPixelDataLen(file_size); |
| 821 | if (pixel_data_len > 0) { | 822 | if (pixel_data_len > 0) { |
| 822 | const miscompiled_bytes = @min(pixel_data_len, num_padding_bytes); | 823 | const miscompiled_bytes = @min(pixel_data_len, num_padding_bytes); |
| 823 | std.mem.writeIntNative(u64, &number_as_bytes, miscompiled_bytes); | 824 | std.mem.writeInt(u64, &number_as_bytes, miscompiled_bytes, native_endian); |
| 824 | const miscompiled_bytes_string_index = try self.diagnostics.putString(&number_as_bytes); | 825 | const miscompiled_bytes_string_index = try self.diagnostics.putString(&number_as_bytes); |
| 825 | try self.addErrorDetails(.{ | 826 | try self.addErrorDetails(.{ |
| 826 | .err = .rc_would_miscompile_bmp_palette_padding, | 827 | .err = .rc_would_miscompile_bmp_palette_padding, |
| ... | @@ -984,8 +985,8 @@ pub const Compiler = struct { | ... | @@ -984,8 +985,8 @@ pub const Compiler = struct { |
| 984 | pub fn write(self: Data, writer: anytype) !void { | 985 | pub fn write(self: Data, writer: anytype) !void { |
| 985 | switch (self) { | 986 | switch (self) { |
| 986 | .number => |number| switch (number.is_long) { | 987 | .number => |number| switch (number.is_long) { |
| 987 | false => try writer.writeIntLittle(WORD, number.asWord()), | 988 | false => try writer.writeInt(WORD, number.asWord(), .little), |
| 988 | true => try writer.writeIntLittle(DWORD, number.value), | 989 | true => try writer.writeInt(DWORD, number.value, .little), |
| 989 | }, | 990 | }, |
| 990 | .ascii_string => |ascii_string| { | 991 | .ascii_string => |ascii_string| { |
| 991 | try writer.writeAll(ascii_string); | 992 | try writer.writeAll(ascii_string); |
| ... | @@ -1315,9 +1316,9 @@ pub const Compiler = struct { | ... | @@ -1315,9 +1316,9 @@ pub const Compiler = struct { |
| 1315 | 1316 | ||
| 1316 | try data_writer.writeByte(modifiers.value); | 1317 | try data_writer.writeByte(modifiers.value); |
| 1317 | try data_writer.writeByte(0); // padding | 1318 | try data_writer.writeByte(0); // padding |
| 1318 | try data_writer.writeIntLittle(u16, key); | 1319 | try data_writer.writeInt(u16, key, .little); |
| 1319 | try data_writer.writeIntLittle(u16, cmd_id.asWord()); | 1320 | try data_writer.writeInt(u16, cmd_id.asWord(), .little); |
| 1320 | try data_writer.writeIntLittle(u16, 0); // padding | 1321 | try data_writer.writeInt(u16, 0, .little); // padding |
| 1321 | } | 1322 | } |
| 1322 | } | 1323 | } |
| 1323 | 1324 | ||
| ... | @@ -1700,34 +1701,34 @@ pub const Compiler = struct { | ... | @@ -1700,34 +1701,34 @@ pub const Compiler = struct { |
| 1700 | if (node.help_id == null) break :help_id 0; | 1701 | if (node.help_id == null) break :help_id 0; |
| 1701 | break :help_id evaluateNumberExpression(node.help_id.?, self.source, self.input_code_pages).value; | 1702 | break :help_id evaluateNumberExpression(node.help_id.?, self.source, self.input_code_pages).value; |
| 1702 | }; | 1703 | }; |
| 1703 | try data_writer.writeIntLittle(u16, 1); // version number, always 1 | 1704 | try data_writer.writeInt(u16, 1, .little); // version number, always 1 |
| 1704 | try data_writer.writeIntLittle(u16, 0xFFFF); // signature, always 0xFFFF | 1705 | try data_writer.writeInt(u16, 0xFFFF, .little); // signature, always 0xFFFF |
| 1705 | try data_writer.writeIntLittle(u32, help_id); | 1706 | try data_writer.writeInt(u32, help_id, .little); |
| 1706 | try data_writer.writeIntLittle(u32, optional_statement_values.exstyle); | 1707 | try data_writer.writeInt(u32, optional_statement_values.exstyle, .little); |
| 1707 | try data_writer.writeIntLittle(u32, optional_statement_values.style); | 1708 | try data_writer.writeInt(u32, optional_statement_values.style, .little); |
| 1708 | } else { | 1709 | } else { |
| 1709 | try data_writer.writeIntLittle(u32, optional_statement_values.style); | 1710 | try data_writer.writeInt(u32, optional_statement_values.style, .little); |
| 1710 | try data_writer.writeIntLittle(u32, optional_statement_values.exstyle); | 1711 | try data_writer.writeInt(u32, optional_statement_values.exstyle, .little); |
| 1711 | } | 1712 | } |
| 1712 | // This limit is enforced by the parser, so we know the number of controls | 1713 | // This limit is enforced by the parser, so we know the number of controls |
| 1713 | // is within the range of a u16. | 1714 | // is within the range of a u16. |
| 1714 | try data_writer.writeIntLittle(u16, @as(u16, @intCast(node.controls.len))); | 1715 | try data_writer.writeInt(u16, @as(u16, @intCast(node.controls.len)), .little); |
| 1715 | try data_writer.writeIntLittle(u16, x.asWord()); | 1716 | try data_writer.writeInt(u16, x.asWord(), .little); |
| 1716 | try data_writer.writeIntLittle(u16, y.asWord()); | 1717 | try data_writer.writeInt(u16, y.asWord(), .little); |
| 1717 | try data_writer.writeIntLittle(u16, width.asWord()); | 1718 | try data_writer.writeInt(u16, width.asWord(), .little); |
| 1718 | try data_writer.writeIntLittle(u16, height.asWord()); | 1719 | try data_writer.writeInt(u16, height.asWord(), .little); |
| 1719 | 1720 | ||
| 1720 | // Menu | 1721 | // Menu |
| 1721 | if (optional_statement_values.menu) |menu| { | 1722 | if (optional_statement_values.menu) |menu| { |
| 1722 | try menu.write(data_writer); | 1723 | try menu.write(data_writer); |
| 1723 | } else { | 1724 | } else { |
| 1724 | try data_writer.writeIntLittle(u16, 0); | 1725 | try data_writer.writeInt(u16, 0, .little); |
| 1725 | } | 1726 | } |
| 1726 | // Class | 1727 | // Class |
| 1727 | if (optional_statement_values.class) |class| { | 1728 | if (optional_statement_values.class) |class| { |
| 1728 | try class.write(data_writer); | 1729 | try class.write(data_writer); |
| 1729 | } else { | 1730 | } else { |
| 1730 | try data_writer.writeIntLittle(u16, 0); | 1731 | try data_writer.writeInt(u16, 0, .little); |
| 1731 | } | 1732 | } |
| 1732 | // Caption | 1733 | // Caption |
| 1733 | if (optional_statement_values.caption) |caption| { | 1734 | if (optional_statement_values.caption) |caption| { |
| ... | @@ -1735,7 +1736,7 @@ pub const Compiler = struct { | ... | @@ -1735,7 +1736,7 @@ pub const Compiler = struct { |
| 1735 | defer self.allocator.free(parsed); | 1736 | defer self.allocator.free(parsed); |
| 1736 | try data_writer.writeAll(std.mem.sliceAsBytes(parsed[0 .. parsed.len + 1])); | 1737 | try data_writer.writeAll(std.mem.sliceAsBytes(parsed[0 .. parsed.len + 1])); |
| 1737 | } else { | 1738 | } else { |
| 1738 | try data_writer.writeIntLittle(u16, 0); | 1739 | try data_writer.writeInt(u16, 0, .little); |
| 1739 | } | 1740 | } |
| 1740 | // Font | 1741 | // Font |
| 1741 | if (optional_statement_values.font) |font| { | 1742 | if (optional_statement_values.font) |font| { |
| ... | @@ -1786,18 +1787,18 @@ pub const Compiler = struct { | ... | @@ -1786,18 +1787,18 @@ pub const Compiler = struct { |
| 1786 | switch (resource) { | 1787 | switch (resource) { |
| 1787 | .dialog => { | 1788 | .dialog => { |
| 1788 | // Note: Reverse order from DIALOGEX | 1789 | // Note: Reverse order from DIALOGEX |
| 1789 | try data_writer.writeIntLittle(u32, style); | 1790 | try data_writer.writeInt(u32, style, .little); |
| 1790 | try data_writer.writeIntLittle(u32, exstyle); | 1791 | try data_writer.writeInt(u32, exstyle, .little); |
| 1791 | }, | 1792 | }, |
| 1792 | .dialogex => { | 1793 | .dialogex => { |
| 1793 | const help_id: u32 = if (control.help_id) |help_id_expression| | 1794 | const help_id: u32 = if (control.help_id) |help_id_expression| |
| 1794 | evaluateNumberExpression(help_id_expression, self.source, self.input_code_pages).value | 1795 | evaluateNumberExpression(help_id_expression, self.source, self.input_code_pages).value |
| 1795 | else | 1796 | else |
| 1796 | 0; | 1797 | 0; |
| 1797 | try data_writer.writeIntLittle(u32, help_id); | 1798 | try data_writer.writeInt(u32, help_id, .little); |
| 1798 | // Note: Reverse order from DIALOG | 1799 | // Note: Reverse order from DIALOG |
| 1799 | try data_writer.writeIntLittle(u32, exstyle); | 1800 | try data_writer.writeInt(u32, exstyle, .little); |
| 1800 | try data_writer.writeIntLittle(u32, style); | 1801 | try data_writer.writeInt(u32, style, .little); |
| 1801 | }, | 1802 | }, |
| 1802 | else => unreachable, | 1803 | else => unreachable, |
| 1803 | } | 1804 | } |
| ... | @@ -1807,15 +1808,15 @@ pub const Compiler = struct { | ... | @@ -1807,15 +1808,15 @@ pub const Compiler = struct { |
| 1807 | const control_width = evaluateNumberExpression(control.width, self.source, self.input_code_pages); | 1808 | const control_width = evaluateNumberExpression(control.width, self.source, self.input_code_pages); |
| 1808 | const control_height = evaluateNumberExpression(control.height, self.source, self.input_code_pages); | 1809 | const control_height = evaluateNumberExpression(control.height, self.source, self.input_code_pages); |
| 1809 | 1810 | ||
| 1810 | try data_writer.writeIntLittle(u16, control_x.asWord()); | 1811 | try data_writer.writeInt(u16, control_x.asWord(), .little); |
| 1811 | try data_writer.writeIntLittle(u16, control_y.asWord()); | 1812 | try data_writer.writeInt(u16, control_y.asWord(), .little); |
| 1812 | try data_writer.writeIntLittle(u16, control_width.asWord()); | 1813 | try data_writer.writeInt(u16, control_width.asWord(), .little); |
| 1813 | try data_writer.writeIntLittle(u16, control_height.asWord()); | 1814 | try data_writer.writeInt(u16, control_height.asWord(), .little); |
| 1814 | 1815 | ||
| 1815 | const control_id = evaluateNumberExpression(control.id, self.source, self.input_code_pages); | 1816 | const control_id = evaluateNumberExpression(control.id, self.source, self.input_code_pages); |
| 1816 | switch (resource) { | 1817 | switch (resource) { |
| 1817 | .dialog => try data_writer.writeIntLittle(u16, control_id.asWord()), | 1818 | .dialog => try data_writer.writeInt(u16, control_id.asWord(), .little), |
| 1818 | .dialogex => try data_writer.writeIntLittle(u32, control_id.value), | 1819 | .dialogex => try data_writer.writeInt(u32, control_id.value, .little), |
| 1819 | else => unreachable, | 1820 | else => unreachable, |
| 1820 | } | 1821 | } |
| 1821 | 1822 | ||
| ... | @@ -1948,7 +1949,7 @@ pub const Compiler = struct { | ... | @@ -1948,7 +1949,7 @@ pub const Compiler = struct { |
| 1948 | } | 1949 | } |
| 1949 | // We know the extra_data_buf size fits within a u16. | 1950 | // We know the extra_data_buf size fits within a u16. |
| 1950 | const extra_data_size: u16 = @intCast(extra_data_buf.items.len); | 1951 | const extra_data_size: u16 = @intCast(extra_data_buf.items.len); |
| 1951 | try data_writer.writeIntLittle(u16, extra_data_size); | 1952 | try data_writer.writeInt(u16, extra_data_size, .little); |
| 1952 | try data_writer.writeAll(extra_data_buf.items); | 1953 | try data_writer.writeAll(extra_data_buf.items); |
| 1953 | } | 1954 | } |
| 1954 | 1955 | ||
| ... | @@ -1962,21 +1963,21 @@ pub const Compiler = struct { | ... | @@ -1962,21 +1963,21 @@ pub const Compiler = struct { |
| 1962 | 1963 | ||
| 1963 | // I'm assuming this is some sort of version | 1964 | // I'm assuming this is some sort of version |
| 1964 | // TODO: Try to find something mentioning this | 1965 | // TODO: Try to find something mentioning this |
| 1965 | try data_writer.writeIntLittle(u16, 1); | 1966 | try data_writer.writeInt(u16, 1, .little); |
| 1966 | try data_writer.writeIntLittle(u16, button_width.asWord()); | 1967 | try data_writer.writeInt(u16, button_width.asWord(), .little); |
| 1967 | try data_writer.writeIntLittle(u16, button_height.asWord()); | 1968 | try data_writer.writeInt(u16, button_height.asWord(), .little); |
| 1968 | try data_writer.writeIntLittle(u16, @as(u16, @intCast(node.buttons.len))); | 1969 | try data_writer.writeInt(u16, @as(u16, @intCast(node.buttons.len)), .little); |
| 1969 | 1970 | ||
| 1970 | for (node.buttons) |button_or_sep| { | 1971 | for (node.buttons) |button_or_sep| { |
| 1971 | switch (button_or_sep.id) { | 1972 | switch (button_or_sep.id) { |
| 1972 | .literal => { // This is always SEPARATOR | 1973 | .literal => { // This is always SEPARATOR |
| 1973 | std.debug.assert(button_or_sep.cast(.literal).?.token.id == .literal); | 1974 | std.debug.assert(button_or_sep.cast(.literal).?.token.id == .literal); |
| 1974 | try data_writer.writeIntLittle(u16, 0); | 1975 | try data_writer.writeInt(u16, 0, .little); |
| 1975 | }, | 1976 | }, |
| 1976 | .simple_statement => { | 1977 | .simple_statement => { |
| 1977 | const value_node = button_or_sep.cast(.simple_statement).?.value; | 1978 | const value_node = button_or_sep.cast(.simple_statement).?.value; |
| 1978 | const value = evaluateNumberExpression(value_node, self.source, self.input_code_pages); | 1979 | const value = evaluateNumberExpression(value_node, self.source, self.input_code_pages); |
| 1979 | try data_writer.writeIntLittle(u16, value.asWord()); | 1980 | try data_writer.writeInt(u16, value.asWord(), .little); |
| 1980 | }, | 1981 | }, |
| 1981 | else => unreachable, // This is a bug in the parser | 1982 | else => unreachable, // This is a bug in the parser |
| 1982 | } | 1983 | } |
| ... | @@ -2007,21 +2008,21 @@ pub const Compiler = struct { | ... | @@ -2007,21 +2008,21 @@ pub const Compiler = struct { |
| 2007 | pub fn writeDialogFont(self: *Compiler, resource: Resource, values: FontStatementValues, writer: anytype) !void { | 2008 | pub fn writeDialogFont(self: *Compiler, resource: Resource, values: FontStatementValues, writer: anytype) !void { |
| 2008 | const node = values.node; | 2009 | const node = values.node; |
| 2009 | const point_size = evaluateNumberExpression(node.point_size, self.source, self.input_code_pages); | 2010 | const point_size = evaluateNumberExpression(node.point_size, self.source, self.input_code_pages); |
| 2010 | try writer.writeIntLittle(u16, point_size.asWord()); | 2011 | try writer.writeInt(u16, point_size.asWord(), .little); |
| 2011 | 2012 | ||
| 2012 | if (resource == .dialogex) { | 2013 | if (resource == .dialogex) { |
| 2013 | try writer.writeIntLittle(u16, values.weight); | 2014 | try writer.writeInt(u16, values.weight, .little); |
| 2014 | } | 2015 | } |
| 2015 | 2016 | ||
| 2016 | if (resource == .dialogex) { | 2017 | if (resource == .dialogex) { |
| 2017 | try writer.writeIntLittle(u8, @intFromBool(values.italic)); | 2018 | try writer.writeInt(u8, @intFromBool(values.italic), .little); |
| 2018 | } | 2019 | } |
| 2019 | 2020 | ||
| 2020 | if (node.char_set) |char_set| { | 2021 | if (node.char_set) |char_set| { |
| 2021 | const value = evaluateNumberExpression(char_set, self.source, self.input_code_pages); | 2022 | const value = evaluateNumberExpression(char_set, self.source, self.input_code_pages); |
| 2022 | try writer.writeIntLittle(u8, @as(u8, @truncate(value.value))); | 2023 | try writer.writeInt(u8, @as(u8, @truncate(value.value)), .little); |
| 2023 | } else if (resource == .dialogex) { | 2024 | } else if (resource == .dialogex) { |
| 2024 | try writer.writeIntLittle(u8, 1); // DEFAULT_CHARSET | 2025 | try writer.writeInt(u8, 1, .little); // DEFAULT_CHARSET |
| 2025 | } | 2026 | } |
| 2026 | 2027 | ||
| 2027 | const typeface = try self.parseQuotedStringAsWideString(node.typeface); | 2028 | const typeface = try self.parseQuotedStringAsWideString(node.typeface); |
| ... | @@ -2076,9 +2077,9 @@ pub const Compiler = struct { | ... | @@ -2076,9 +2077,9 @@ pub const Compiler = struct { |
| 2076 | pub fn writeMenuData(self: *Compiler, node: *Node.Menu, data_writer: anytype, resource: Resource) !void { | 2077 | pub fn writeMenuData(self: *Compiler, node: *Node.Menu, data_writer: anytype, resource: Resource) !void { |
| 2077 | // menu header | 2078 | // menu header |
| 2078 | const version: u16 = if (resource == .menu) 0 else 1; | 2079 | const version: u16 = if (resource == .menu) 0 else 1; |
| 2079 | try data_writer.writeIntLittle(u16, version); | 2080 | try data_writer.writeInt(u16, version, .little); |
| 2080 | const header_size: u16 = if (resource == .menu) 0 else 4; | 2081 | const header_size: u16 = if (resource == .menu) 0 else 4; |
| 2081 | try data_writer.writeIntLittle(u16, header_size); // cbHeaderSize | 2082 | try data_writer.writeInt(u16, header_size, .little); // cbHeaderSize |
| 2082 | // Note: There can be extra bytes at the end of this header (`rgbExtra`), | 2083 | // Note: There can be extra bytes at the end of this header (`rgbExtra`), |
| 2083 | // but they are always zero-length for us, so we don't write anything | 2084 | // but they are always zero-length for us, so we don't write anything |
| 2084 | // (the length of the rgbExtra field is inferred from the header_size). | 2085 | // (the length of the rgbExtra field is inferred from the header_size). |
| ... | @@ -2088,9 +2089,9 @@ pub const Compiler = struct { | ... | @@ -2088,9 +2089,9 @@ pub const Compiler = struct { |
| 2088 | if (resource == .menuex) { | 2089 | if (resource == .menuex) { |
| 2089 | if (node.help_id) |help_id_node| { | 2090 | if (node.help_id) |help_id_node| { |
| 2090 | const help_id = evaluateNumberExpression(help_id_node, self.source, self.input_code_pages); | 2091 | const help_id = evaluateNumberExpression(help_id_node, self.source, self.input_code_pages); |
| 2091 | try data_writer.writeIntLittle(u32, help_id.value); | 2092 | try data_writer.writeInt(u32, help_id.value, .little); |
| 2092 | } else { | 2093 | } else { |
| 2093 | try data_writer.writeIntLittle(u32, 0); | 2094 | try data_writer.writeInt(u32, 0, .little); |
| 2094 | } | 2095 | } |
| 2095 | } | 2096 | } |
| 2096 | 2097 | ||
| ... | @@ -2110,9 +2111,9 @@ pub const Compiler = struct { | ... | @@ -2110,9 +2111,9 @@ pub const Compiler = struct { |
| 2110 | // compiler still uses this alternate form, so that's what we use too. | 2111 | // compiler still uses this alternate form, so that's what we use too. |
| 2111 | var flags = res.MenuItemFlags{}; | 2112 | var flags = res.MenuItemFlags{}; |
| 2112 | if (is_last_of_parent) flags.markLast(); | 2113 | if (is_last_of_parent) flags.markLast(); |
| 2113 | try writer.writeIntLittle(u16, flags.value); | 2114 | try writer.writeInt(u16, flags.value, .little); |
| 2114 | try writer.writeIntLittle(u16, 0); // id | 2115 | try writer.writeInt(u16, 0, .little); // id |
| 2115 | try writer.writeIntLittle(u16, 0); // null-terminated UTF-16 text | 2116 | try writer.writeInt(u16, 0, .little); // null-terminated UTF-16 text |
| 2116 | }, | 2117 | }, |
| 2117 | .menu_item => { | 2118 | .menu_item => { |
| 2118 | const menu_item = @fieldParentPtr(Node.MenuItem, "base", node); | 2119 | const menu_item = @fieldParentPtr(Node.MenuItem, "base", node); |
| ... | @@ -2123,10 +2124,10 @@ pub const Compiler = struct { | ... | @@ -2123,10 +2124,10 @@ pub const Compiler = struct { |
| 2123 | flags.apply(option); | 2124 | flags.apply(option); |
| 2124 | } | 2125 | } |
| 2125 | if (is_last_of_parent) flags.markLast(); | 2126 | if (is_last_of_parent) flags.markLast(); |
| 2126 | try writer.writeIntLittle(u16, flags.value); | 2127 | try writer.writeInt(u16, flags.value, .little); |
| 2127 | 2128 | ||
| 2128 | var result = evaluateNumberExpression(menu_item.result, self.source, self.input_code_pages); | 2129 | var result = evaluateNumberExpression(menu_item.result, self.source, self.input_code_pages); |
| 2129 | try writer.writeIntLittle(u16, result.asWord()); | 2130 | try writer.writeInt(u16, result.asWord(), .little); |
| 2130 | 2131 | ||
| 2131 | var text = try self.parseQuotedStringAsWideString(menu_item.text); | 2132 | var text = try self.parseQuotedStringAsWideString(menu_item.text); |
| 2132 | defer self.allocator.free(text); | 2133 | defer self.allocator.free(text); |
| ... | @@ -2141,7 +2142,7 @@ pub const Compiler = struct { | ... | @@ -2141,7 +2142,7 @@ pub const Compiler = struct { |
| 2141 | flags.apply(option); | 2142 | flags.apply(option); |
| 2142 | } | 2143 | } |
| 2143 | if (is_last_of_parent) flags.markLast(); | 2144 | if (is_last_of_parent) flags.markLast(); |
| 2144 | try writer.writeIntLittle(u16, flags.value); | 2145 | try writer.writeInt(u16, flags.value, .little); |
| 2145 | 2146 | ||
| 2146 | var text = try self.parseQuotedStringAsWideString(popup.text); | 2147 | var text = try self.parseQuotedStringAsWideString(popup.text); |
| 2147 | defer self.allocator.free(text); | 2148 | defer self.allocator.free(text); |
| ... | @@ -2157,30 +2158,30 @@ pub const Compiler = struct { | ... | @@ -2157,30 +2158,30 @@ pub const Compiler = struct { |
| 2157 | 2158 | ||
| 2158 | if (menu_item.type) |flags| { | 2159 | if (menu_item.type) |flags| { |
| 2159 | const value = evaluateNumberExpression(flags, self.source, self.input_code_pages); | 2160 | const value = evaluateNumberExpression(flags, self.source, self.input_code_pages); |
| 2160 | try writer.writeIntLittle(u32, value.value); | 2161 | try writer.writeInt(u32, value.value, .little); |
| 2161 | } else { | 2162 | } else { |
| 2162 | try writer.writeIntLittle(u32, 0); | 2163 | try writer.writeInt(u32, 0, .little); |
| 2163 | } | 2164 | } |
| 2164 | 2165 | ||
| 2165 | if (menu_item.state) |state| { | 2166 | if (menu_item.state) |state| { |
| 2166 | const value = evaluateNumberExpression(state, self.source, self.input_code_pages); | 2167 | const value = evaluateNumberExpression(state, self.source, self.input_code_pages); |
| 2167 | try writer.writeIntLittle(u32, value.value); | 2168 | try writer.writeInt(u32, value.value, .little); |
| 2168 | } else { | 2169 | } else { |
| 2169 | try writer.writeIntLittle(u32, 0); | 2170 | try writer.writeInt(u32, 0, .little); |
| 2170 | } | 2171 | } |
| 2171 | 2172 | ||
| 2172 | if (menu_item.id) |id| { | 2173 | if (menu_item.id) |id| { |
| 2173 | const value = evaluateNumberExpression(id, self.source, self.input_code_pages); | 2174 | const value = evaluateNumberExpression(id, self.source, self.input_code_pages); |
| 2174 | try writer.writeIntLittle(u32, value.value); | 2175 | try writer.writeInt(u32, value.value, .little); |
| 2175 | } else { | 2176 | } else { |
| 2176 | try writer.writeIntLittle(u32, 0); | 2177 | try writer.writeInt(u32, 0, .little); |
| 2177 | } | 2178 | } |
| 2178 | 2179 | ||
| 2179 | var flags: u16 = 0; | 2180 | var flags: u16 = 0; |
| 2180 | if (is_last_of_parent) flags |= comptime @as(u16, @intCast(res.MF.END)); | 2181 | if (is_last_of_parent) flags |= comptime @as(u16, @intCast(res.MF.END)); |
| 2181 | // This constant doesn't seem to have a named #define, it's different than MF_POPUP | 2182 | // This constant doesn't seem to have a named #define, it's different than MF_POPUP |
| 2182 | if (node_type == .popup_ex) flags |= 0x01; | 2183 | if (node_type == .popup_ex) flags |= 0x01; |
| 2183 | try writer.writeIntLittle(u16, flags); | 2184 | try writer.writeInt(u16, flags, .little); |
| 2184 | 2185 | ||
| 2185 | var text = try self.parseQuotedStringAsWideString(menu_item.text); | 2186 | var text = try self.parseQuotedStringAsWideString(menu_item.text); |
| 2186 | defer self.allocator.free(text); | 2187 | defer self.allocator.free(text); |
| ... | @@ -2195,9 +2196,9 @@ pub const Compiler = struct { | ... | @@ -2195,9 +2196,9 @@ pub const Compiler = struct { |
| 2195 | if (node_type == .popup_ex) { | 2196 | if (node_type == .popup_ex) { |
| 2196 | if (menu_item.help_id) |help_id_node| { | 2197 | if (menu_item.help_id) |help_id_node| { |
| 2197 | const help_id = evaluateNumberExpression(help_id_node, self.source, self.input_code_pages); | 2198 | const help_id = evaluateNumberExpression(help_id_node, self.source, self.input_code_pages); |
| 2198 | try writer.writeIntLittle(u32, help_id.value); | 2199 | try writer.writeInt(u32, help_id.value, .little); |
| 2199 | } else { | 2200 | } else { |
| 2200 | try writer.writeIntLittle(u32, 0); | 2201 | try writer.writeInt(u32, 0, .little); |
| 2201 | } | 2202 | } |
| 2202 | 2203 | ||
| 2203 | for (menu_item.items, 0..) |item, i| { | 2204 | for (menu_item.items, 0..) |item, i| { |
| ... | @@ -2218,15 +2219,15 @@ pub const Compiler = struct { | ... | @@ -2218,15 +2219,15 @@ pub const Compiler = struct { |
| 2218 | var limited_writer = limitedWriter(data_buffer.writer(), std.math.maxInt(u16)); | 2219 | var limited_writer = limitedWriter(data_buffer.writer(), std.math.maxInt(u16)); |
| 2219 | const data_writer = limited_writer.writer(); | 2220 | const data_writer = limited_writer.writer(); |
| 2220 | 2221 | ||
| 2221 | try data_writer.writeIntLittle(u16, 0); // placeholder size | 2222 | try data_writer.writeInt(u16, 0, .little); // placeholder size |
| 2222 | try data_writer.writeIntLittle(u16, res.FixedFileInfo.byte_len); | 2223 | try data_writer.writeInt(u16, res.FixedFileInfo.byte_len, .little); |
| 2223 | try data_writer.writeIntLittle(u16, res.VersionNode.type_binary); | 2224 | try data_writer.writeInt(u16, res.VersionNode.type_binary, .little); |
| 2224 | const key_bytes = std.mem.sliceAsBytes(res.FixedFileInfo.key[0 .. res.FixedFileInfo.key.len + 1]); | 2225 | const key_bytes = std.mem.sliceAsBytes(res.FixedFileInfo.key[0 .. res.FixedFileInfo.key.len + 1]); |
| 2225 | try data_writer.writeAll(key_bytes); | 2226 | try data_writer.writeAll(key_bytes); |
| 2226 | // The number of bytes written up to this point is always the same, since the name | 2227 | // The number of bytes written up to this point is always the same, since the name |
| 2227 | // of the node is a constant (FixedFileInfo.key). The total number of bytes | 2228 | // of the node is a constant (FixedFileInfo.key). The total number of bytes |
| 2228 | // written so far is 38, so we need 2 padding bytes to get back to DWORD alignment | 2229 | // written so far is 38, so we need 2 padding bytes to get back to DWORD alignment |
| 2229 | try data_writer.writeIntLittle(u16, 0); | 2230 | try data_writer.writeInt(u16, 0, .little); |
| 2230 | 2231 | ||
| 2231 | var fixed_file_info = res.FixedFileInfo{}; | 2232 | var fixed_file_info = res.FixedFileInfo{}; |
| 2232 | for (node.fixed_info) |fixed_info| { | 2233 | for (node.fixed_info) |fixed_info| { |
| ... | @@ -2321,7 +2322,7 @@ pub const Compiler = struct { | ... | @@ -2321,7 +2322,7 @@ pub const Compiler = struct { |
| 2321 | // limited the writer to maxInt(u16) | 2322 | // limited the writer to maxInt(u16) |
| 2322 | const data_size: u16 = @intCast(data_buffer.items.len); | 2323 | const data_size: u16 = @intCast(data_buffer.items.len); |
| 2323 | // And now that we know the full size of this node (including its children), set its size | 2324 | // And now that we know the full size of this node (including its children), set its size |
| 2324 | std.mem.writeIntLittle(u16, data_buffer.items[0..2], data_size); | 2325 | std.mem.writeInt(u16, data_buffer.items[0..2], data_size, .little); |
| 2325 | 2326 | ||
| 2326 | var header = try self.resourceHeader(node.id, node.versioninfo, .{ | 2327 | var header = try self.resourceHeader(node.id, node.versioninfo, .{ |
| 2327 | .data_size = data_size, | 2328 | .data_size = data_size, |
| ... | @@ -2344,12 +2345,12 @@ pub const Compiler = struct { | ... | @@ -2344,12 +2345,12 @@ pub const Compiler = struct { |
| 2344 | try writeDataPadding(writer, @as(u16, @intCast(buf.items.len))); | 2345 | try writeDataPadding(writer, @as(u16, @intCast(buf.items.len))); |
| 2345 | 2346 | ||
| 2346 | const node_and_children_size_offset = buf.items.len; | 2347 | const node_and_children_size_offset = buf.items.len; |
| 2347 | try writer.writeIntLittle(u16, 0); // placeholder for size | 2348 | try writer.writeInt(u16, 0, .little); // placeholder for size |
| 2348 | const data_size_offset = buf.items.len; | 2349 | const data_size_offset = buf.items.len; |
| 2349 | try writer.writeIntLittle(u16, 0); // placeholder for data size | 2350 | try writer.writeInt(u16, 0, .little); // placeholder for data size |
| 2350 | const data_type_offset = buf.items.len; | 2351 | const data_type_offset = buf.items.len; |
| 2351 | // Data type is string unless the node contains values that are numbers. | 2352 | // Data type is string unless the node contains values that are numbers. |
| 2352 | try writer.writeIntLittle(u16, res.VersionNode.type_string); | 2353 | try writer.writeInt(u16, res.VersionNode.type_string, .little); |
| 2353 | 2354 | ||
| 2354 | switch (node.id) { | 2355 | switch (node.id) { |
| 2355 | inline .block, .block_value => |node_type| { | 2356 | inline .block, .block_value => |node_type| { |
| ... | @@ -2411,17 +2412,17 @@ pub const Compiler = struct { | ... | @@ -2411,17 +2412,17 @@ pub const Compiler = struct { |
| 2411 | const is_empty = parsed_to_first_null.len == 0; | 2412 | const is_empty = parsed_to_first_null.len == 0; |
| 2412 | const is_only = block_or_value.values.len == 1; | 2413 | const is_only = block_or_value.values.len == 1; |
| 2413 | if ((!is_empty or !is_only) and (is_last or value_value_node.trailing_comma)) { | 2414 | if ((!is_empty or !is_only) and (is_last or value_value_node.trailing_comma)) { |
| 2414 | try writer.writeIntLittle(u16, 0); | 2415 | try writer.writeInt(u16, 0, .little); |
| 2415 | values_size += if (has_number_value) 2 else 1; | 2416 | values_size += if (has_number_value) 2 else 1; |
| 2416 | } | 2417 | } |
| 2417 | } | 2418 | } |
| 2418 | } | 2419 | } |
| 2419 | var data_size_slice = buf.items[data_size_offset..]; | 2420 | var data_size_slice = buf.items[data_size_offset..]; |
| 2420 | std.mem.writeIntLittle(u16, data_size_slice[0..@sizeOf(u16)], @as(u16, @intCast(values_size))); | 2421 | std.mem.writeInt(u16, data_size_slice[0..@sizeOf(u16)], @as(u16, @intCast(values_size)), .little); |
| 2421 | 2422 | ||
| 2422 | if (has_number_value) { | 2423 | if (has_number_value) { |
| 2423 | const data_type_slice = buf.items[data_type_offset..]; | 2424 | const data_type_slice = buf.items[data_type_offset..]; |
| 2424 | std.mem.writeIntLittle(u16, data_type_slice[0..@sizeOf(u16)], res.VersionNode.type_binary); | 2425 | std.mem.writeInt(u16, data_type_slice[0..@sizeOf(u16)], res.VersionNode.type_binary, .little); |
| 2425 | } | 2426 | } |
| 2426 | 2427 | ||
| 2427 | if (node_type == .block) { | 2428 | if (node_type == .block) { |
| ... | @@ -2436,7 +2437,7 @@ pub const Compiler = struct { | ... | @@ -2436,7 +2437,7 @@ pub const Compiler = struct { |
| 2436 | 2437 | ||
| 2437 | const node_and_children_size = buf.items.len - node_and_children_size_offset; | 2438 | const node_and_children_size = buf.items.len - node_and_children_size_offset; |
| 2438 | const node_and_children_size_slice = buf.items[node_and_children_size_offset..]; | 2439 | const node_and_children_size_slice = buf.items[node_and_children_size_offset..]; |
| 2439 | std.mem.writeIntLittle(u16, node_and_children_size_slice[0..@sizeOf(u16)], @as(u16, @intCast(node_and_children_size))); | 2440 | std.mem.writeInt(u16, node_and_children_size_slice[0..@sizeOf(u16)], @as(u16, @intCast(node_and_children_size)), .little); |
| 2440 | } | 2441 | } |
| 2441 | 2442 | ||
| 2442 | pub fn writeStringTable(self: *Compiler, node: *Node.StringTable) !void { | 2443 | pub fn writeStringTable(self: *Compiler, node: *Node.StringTable) !void { |
| ... | @@ -2644,17 +2645,17 @@ pub const Compiler = struct { | ... | @@ -2644,17 +2645,17 @@ pub const Compiler = struct { |
| 2644 | } | 2645 | } |
| 2645 | 2646 | ||
| 2646 | fn writeSizeInfo(self: ResourceHeader, writer: anytype, size_info: SizeInfo) !void { | 2647 | fn writeSizeInfo(self: ResourceHeader, writer: anytype, size_info: SizeInfo) !void { |
| 2647 | try writer.writeIntLittle(DWORD, self.data_size); // DataSize | 2648 | try writer.writeInt(DWORD, self.data_size, .little); // DataSize |
| 2648 | try writer.writeIntLittle(DWORD, size_info.bytes); // HeaderSize | 2649 | try writer.writeInt(DWORD, size_info.bytes, .little); // HeaderSize |
| 2649 | try self.type_value.write(writer); // TYPE | 2650 | try self.type_value.write(writer); // TYPE |
| 2650 | try self.name_value.write(writer); // NAME | 2651 | try self.name_value.write(writer); // NAME |
| 2651 | try writer.writeByteNTimes(0, size_info.padding_after_name); | 2652 | try writer.writeByteNTimes(0, size_info.padding_after_name); |
| 2652 | 2653 | ||
| 2653 | try writer.writeIntLittle(DWORD, self.data_version); // DataVersion | 2654 | try writer.writeInt(DWORD, self.data_version, .little); // DataVersion |
| 2654 | try writer.writeIntLittle(WORD, self.memory_flags.value); // MemoryFlags | 2655 | try writer.writeInt(WORD, self.memory_flags.value, .little); // MemoryFlags |
| 2655 | try writer.writeIntLittle(WORD, self.language.asInt()); // LanguageId | 2656 | try writer.writeInt(WORD, self.language.asInt(), .little); // LanguageId |
| 2656 | try writer.writeIntLittle(DWORD, self.version); // Version | 2657 | try writer.writeInt(DWORD, self.version, .little); // Version |
| 2657 | try writer.writeIntLittle(DWORD, self.characteristics); // Characteristics | 2658 | try writer.writeInt(DWORD, self.characteristics, .little); // Characteristics |
| 2658 | } | 2659 | } |
| 2659 | 2660 | ||
| 2660 | pub fn predefinedResourceType(self: ResourceHeader) ?res.RT { | 2661 | pub fn predefinedResourceType(self: ResourceHeader) ?res.RT { |
| ... | @@ -2997,7 +2998,7 @@ pub const FontDir = struct { | ... | @@ -2997,7 +2998,7 @@ pub const FontDir = struct { |
| 2997 | defer header.deinit(compiler.allocator); | 2998 | defer header.deinit(compiler.allocator); |
| 2998 | 2999 | ||
| 2999 | try header.writeAssertNoOverflow(writer); | 3000 | try header.writeAssertNoOverflow(writer); |
| 3000 | try writer.writeIntLittle(u16, num_fonts); | 3001 | try writer.writeInt(u16, num_fonts, .little); |
| 3001 | for (self.fonts.items) |font| { | 3002 | for (self.fonts.items) |font| { |
| 3002 | // The format of the FONTDIR is a strange beast. | 3003 | // The format of the FONTDIR is a strange beast. |
| 3003 | // Technically, each FONT is seemingly meant to be written as a | 3004 | // Technically, each FONT is seemingly meant to be written as a |
| ... | @@ -3049,7 +3050,7 @@ pub const FontDir = struct { | ... | @@ -3049,7 +3050,7 @@ pub const FontDir = struct { |
| 3049 | // device name/face name in the FONTDIR is reliable. | 3050 | // device name/face name in the FONTDIR is reliable. |
| 3050 | 3051 | ||
| 3051 | // First, the ID is written, though | 3052 | // First, the ID is written, though |
| 3052 | try writer.writeIntLittle(u16, font.id); | 3053 | try writer.writeInt(u16, font.id, .little); |
| 3053 | try writer.writeAll(&font.header_bytes); | 3054 | try writer.writeAll(&font.header_bytes); |
| 3054 | try writer.writeByteNTimes(0, 2); | 3055 | try writer.writeByteNTimes(0, 2); |
| 3055 | } | 3056 | } |
| ... | @@ -3186,7 +3187,7 @@ pub const StringTable = struct { | ... | @@ -3186,7 +3187,7 @@ pub const StringTable = struct { |
| 3186 | var string_i: u8 = 0; | 3187 | var string_i: u8 = 0; |
| 3187 | while (true) : (i += 1) { | 3188 | while (true) : (i += 1) { |
| 3188 | if (!self.set_indexes.isSet(i)) { | 3189 | if (!self.set_indexes.isSet(i)) { |
| 3189 | try data_writer.writeIntLittle(u16, 0); | 3190 | try data_writer.writeInt(u16, 0, .little); |
| 3190 | if (i == 15) break else continue; | 3191 | if (i == 15) break else continue; |
| 3191 | } | 3192 | } |
| 3192 | 3193 | ||
| ... | @@ -3217,10 +3218,10 @@ pub const StringTable = struct { | ... | @@ -3217,10 +3218,10 @@ pub const StringTable = struct { |
| 3217 | // If the option is set, then a NUL terminator is added unconditionally. | 3218 | // If the option is set, then a NUL terminator is added unconditionally. |
| 3218 | // We already trimmed any trailing NULs, so we know it will be a new addition to the string. | 3219 | // We already trimmed any trailing NULs, so we know it will be a new addition to the string. |
| 3219 | if (compiler.null_terminate_string_table_strings) string_len_in_utf16_code_units += 1; | 3220 | if (compiler.null_terminate_string_table_strings) string_len_in_utf16_code_units += 1; |
| 3220 | try data_writer.writeIntLittle(u16, string_len_in_utf16_code_units); | 3221 | try data_writer.writeInt(u16, string_len_in_utf16_code_units, .little); |
| 3221 | try data_writer.writeAll(std.mem.sliceAsBytes(trimmed_string)); | 3222 | try data_writer.writeAll(std.mem.sliceAsBytes(trimmed_string)); |
| 3222 | if (compiler.null_terminate_string_table_strings) { | 3223 | if (compiler.null_terminate_string_table_strings) { |
| 3223 | try data_writer.writeIntLittle(u16, 0); | 3224 | try data_writer.writeInt(u16, 0, .little); |
| 3224 | } | 3225 | } |
| 3225 | 3226 | ||
| 3226 | if (i == 15) break; | 3227 | if (i == 15) break; |
src/resinator/errors.zig+7-5| ... | @@ -9,6 +9,8 @@ const bmp = @import("bmp.zig"); | ... | @@ -9,6 +9,8 @@ const bmp = @import("bmp.zig"); |
| 9 | const parse = @import("parse.zig"); | 9 | const parse = @import("parse.zig"); |
| 10 | const lang = @import("lang.zig"); | 10 | const lang = @import("lang.zig"); |
| 11 | const CodePage = @import("code_pages.zig").CodePage; | 11 | const CodePage = @import("code_pages.zig").CodePage; |
| 12 | const builtin = @import("builtin"); | ||
| 13 | const native_endian = builtin.cpu.arch.endian(); | ||
| 12 | 14 | ||
| 13 | pub const Diagnostics = struct { | 15 | pub const Diagnostics = struct { |
| 14 | errors: std.ArrayListUnmanaged(ErrorDetails) = .{}, | 16 | errors: std.ArrayListUnmanaged(ErrorDetails) = .{}, |
| ... | @@ -649,24 +651,24 @@ pub const ErrorDetails = struct { | ... | @@ -649,24 +651,24 @@ pub const ErrorDetails = struct { |
| 649 | }, | 651 | }, |
| 650 | .bmp_ignored_palette_bytes => { | 652 | .bmp_ignored_palette_bytes => { |
| 651 | const bytes = strings[self.extra.number]; | 653 | const bytes = strings[self.extra.number]; |
| 652 | const ignored_bytes = std.mem.readIntNative(u64, bytes[0..8]); | 654 | const ignored_bytes = std.mem.readInt(u64, bytes[0..8], native_endian); |
| 653 | try writer.print("bitmap has {d} extra bytes preceding the pixel data which will be ignored", .{ignored_bytes}); | 655 | try writer.print("bitmap has {d} extra bytes preceding the pixel data which will be ignored", .{ignored_bytes}); |
| 654 | }, | 656 | }, |
| 655 | .bmp_missing_palette_bytes => { | 657 | .bmp_missing_palette_bytes => { |
| 656 | const bytes = strings[self.extra.number]; | 658 | const bytes = strings[self.extra.number]; |
| 657 | const missing_bytes = std.mem.readIntNative(u64, bytes[0..8]); | 659 | const missing_bytes = std.mem.readInt(u64, bytes[0..8], native_endian); |
| 658 | try writer.print("bitmap has {d} missing color palette bytes which will be padded with zeroes", .{missing_bytes}); | 660 | try writer.print("bitmap has {d} missing color palette bytes which will be padded with zeroes", .{missing_bytes}); |
| 659 | }, | 661 | }, |
| 660 | .rc_would_miscompile_bmp_palette_padding => { | 662 | .rc_would_miscompile_bmp_palette_padding => { |
| 661 | const bytes = strings[self.extra.number]; | 663 | const bytes = strings[self.extra.number]; |
| 662 | const miscompiled_bytes = std.mem.readIntNative(u64, bytes[0..8]); | 664 | const miscompiled_bytes = std.mem.readInt(u64, bytes[0..8], native_endian); |
| 663 | 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}); | 665 | 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}); |
| 664 | }, | 666 | }, |
| 665 | .bmp_too_many_missing_palette_bytes => switch (self.type) { | 667 | .bmp_too_many_missing_palette_bytes => switch (self.type) { |
| 666 | .err, .warning => { | 668 | .err, .warning => { |
| 667 | const bytes = strings[self.extra.number]; | 669 | const bytes = strings[self.extra.number]; |
| 668 | const missing_bytes = std.mem.readIntNative(u64, bytes[0..8]); | 670 | const missing_bytes = std.mem.readInt(u64, bytes[0..8], native_endian); |
| 669 | const max_missing_bytes = std.mem.readIntNative(u64, bytes[8..16]); | 671 | const max_missing_bytes = std.mem.readInt(u64, bytes[8..16], native_endian); |
| 670 | try writer.print("bitmap has {} missing color palette bytes which exceeds the maximum of {}", .{ missing_bytes, max_missing_bytes }); | 672 | try writer.print("bitmap has {} missing color palette bytes which exceeds the maximum of {}", .{ missing_bytes, max_missing_bytes }); |
| 671 | }, | 673 | }, |
| 672 | // TODO: command line option | 674 | // TODO: command line option |
src/resinator/ico.zig+35-33| ... | @@ -5,6 +5,8 @@ | ... | @@ -5,6 +5,8 @@ |
| 5 | //! https://learn.microsoft.com/en-us/windows/win32/menurc/localheader | 5 | //! https://learn.microsoft.com/en-us/windows/win32/menurc/localheader |
| 6 | 6 | ||
| 7 | const std = @import("std"); | 7 | const std = @import("std"); |
| 8 | const builtin = @import("builtin"); | ||
| 9 | const native_endian = builtin.cpu.arch.endian(); | ||
| 8 | 10 | ||
| 9 | pub const ReadError = std.mem.Allocator.Error || error{ InvalidHeader, InvalidImageType, ImpossibleDataSize, UnexpectedEOF, ReadError }; | 11 | pub const ReadError = std.mem.Allocator.Error || error{ InvalidHeader, InvalidImageType, ImpossibleDataSize, UnexpectedEOF, ReadError }; |
| 10 | 12 | ||
| ... | @@ -37,17 +39,17 @@ pub fn read(allocator: std.mem.Allocator, reader: anytype, max_size: u64) ReadEr | ... | @@ -37,17 +39,17 @@ pub fn read(allocator: std.mem.Allocator, reader: anytype, max_size: u64) ReadEr |
| 37 | // to do this. Maybe it makes more sense to handle the translation | 39 | // to do this. Maybe it makes more sense to handle the translation |
| 38 | // at the call site instead of having a helper function here. | 40 | // at the call site instead of having a helper function here. |
| 39 | pub fn readAnyError(allocator: std.mem.Allocator, reader: anytype, max_size: u64) !IconDir { | 41 | pub fn readAnyError(allocator: std.mem.Allocator, reader: anytype, max_size: u64) !IconDir { |
| 40 | const reserved = try reader.readIntLittle(u16); | 42 | const reserved = try reader.readInt(u16, .little); |
| 41 | if (reserved != 0) { | 43 | if (reserved != 0) { |
| 42 | return error.InvalidHeader; | 44 | return error.InvalidHeader; |
| 43 | } | 45 | } |
| 44 | 46 | ||
| 45 | const image_type = reader.readEnum(ImageType, .Little) catch |err| switch (err) { | 47 | const image_type = reader.readEnum(ImageType, .little) catch |err| switch (err) { |
| 46 | error.InvalidValue => return error.InvalidImageType, | 48 | error.InvalidValue => return error.InvalidImageType, |
| 47 | else => |e| return e, | 49 | else => |e| return e, |
| 48 | }; | 50 | }; |
| 49 | 51 | ||
| 50 | const num_images = try reader.readIntLittle(u16); | 52 | const num_images = try reader.readInt(u16, .little); |
| 51 | 53 | ||
| 52 | // To avoid over-allocation in the case of a file that says it has way more | 54 | // To avoid over-allocation in the case of a file that says it has way more |
| 53 | // entries than it actually does, we use an ArrayList with a conservatively | 55 | // entries than it actually does, we use an ArrayList with a conservatively |
| ... | @@ -66,19 +68,19 @@ pub fn readAnyError(allocator: std.mem.Allocator, reader: anytype, max_size: u64 | ... | @@ -66,19 +68,19 @@ pub fn readAnyError(allocator: std.mem.Allocator, reader: anytype, max_size: u64 |
| 66 | switch (image_type) { | 68 | switch (image_type) { |
| 67 | .icon => { | 69 | .icon => { |
| 68 | entry.type_specific_data = .{ .icon = .{ | 70 | entry.type_specific_data = .{ .icon = .{ |
| 69 | .color_planes = try reader.readIntLittle(u16), | 71 | .color_planes = try reader.readInt(u16, .little), |
| 70 | .bits_per_pixel = try reader.readIntLittle(u16), | 72 | .bits_per_pixel = try reader.readInt(u16, .little), |
| 71 | } }; | 73 | } }; |
| 72 | }, | 74 | }, |
| 73 | .cursor => { | 75 | .cursor => { |
| 74 | entry.type_specific_data = .{ .cursor = .{ | 76 | entry.type_specific_data = .{ .cursor = .{ |
| 75 | .hotspot_x = try reader.readIntLittle(u16), | 77 | .hotspot_x = try reader.readInt(u16, .little), |
| 76 | .hotspot_y = try reader.readIntLittle(u16), | 78 | .hotspot_y = try reader.readInt(u16, .little), |
| 77 | } }; | 79 | } }; |
| 78 | }, | 80 | }, |
| 79 | } | 81 | } |
| 80 | entry.data_size_in_bytes = try reader.readIntLittle(u32); | 82 | entry.data_size_in_bytes = try reader.readInt(u32, .little); |
| 81 | entry.data_offset_from_start_of_file = try reader.readIntLittle(u32); | 83 | entry.data_offset_from_start_of_file = try reader.readInt(u32, .little); |
| 82 | // Validate that the offset/data size is feasible | 84 | // Validate that the offset/data size is feasible |
| 83 | if (@as(u64, entry.data_offset_from_start_of_file) + entry.data_size_in_bytes > max_size) { | 85 | if (@as(u64, entry.data_offset_from_start_of_file) + entry.data_size_in_bytes > max_size) { |
| 84 | return error.ImpossibleDataSize; | 86 | return error.ImpossibleDataSize; |
| ... | @@ -133,10 +135,10 @@ pub const IconDir = struct { | ... | @@ -133,10 +135,10 @@ pub const IconDir = struct { |
| 133 | } | 135 | } |
| 134 | 136 | ||
| 135 | pub fn writeResData(self: IconDir, writer: anytype, first_image_id: u16) !void { | 137 | pub fn writeResData(self: IconDir, writer: anytype, first_image_id: u16) !void { |
| 136 | try writer.writeIntLittle(u16, 0); | 138 | try writer.writeInt(u16, 0, .little); |
| 137 | try writer.writeIntLittle(u16, @intFromEnum(self.image_type)); | 139 | try writer.writeInt(u16, @intFromEnum(self.image_type), .little); |
| 138 | // We know that entries.len must fit into a u16 | 140 | // We know that entries.len must fit into a u16 |
| 139 | try writer.writeIntLittle(u16, @as(u16, @intCast(self.entries.len))); | 141 | try writer.writeInt(u16, @as(u16, @intCast(self.entries.len)), .little); |
| 140 | 142 | ||
| 141 | var image_id = first_image_id; | 143 | var image_id = first_image_id; |
| 142 | for (self.entries) |entry| { | 144 | for (self.entries) |entry| { |
| ... | @@ -173,23 +175,23 @@ pub const Entry = struct { | ... | @@ -173,23 +175,23 @@ pub const Entry = struct { |
| 173 | pub fn writeResData(self: Entry, writer: anytype, id: u16) !void { | 175 | pub fn writeResData(self: Entry, writer: anytype, id: u16) !void { |
| 174 | switch (self.type_specific_data) { | 176 | switch (self.type_specific_data) { |
| 175 | .icon => |icon_data| { | 177 | .icon => |icon_data| { |
| 176 | try writer.writeIntLittle(u8, @as(u8, @truncate(self.width))); | 178 | try writer.writeInt(u8, @as(u8, @truncate(self.width)), .little); |
| 177 | try writer.writeIntLittle(u8, @as(u8, @truncate(self.height))); | 179 | try writer.writeInt(u8, @as(u8, @truncate(self.height)), .little); |
| 178 | try writer.writeIntLittle(u8, self.num_colors); | 180 | try writer.writeInt(u8, self.num_colors, .little); |
| 179 | try writer.writeIntLittle(u8, self.reserved); | 181 | try writer.writeInt(u8, self.reserved, .little); |
| 180 | try writer.writeIntLittle(u16, icon_data.color_planes); | 182 | try writer.writeInt(u16, icon_data.color_planes, .little); |
| 181 | try writer.writeIntLittle(u16, icon_data.bits_per_pixel); | 183 | try writer.writeInt(u16, icon_data.bits_per_pixel, .little); |
| 182 | try writer.writeIntLittle(u32, self.data_size_in_bytes); | 184 | try writer.writeInt(u32, self.data_size_in_bytes, .little); |
| 183 | }, | 185 | }, |
| 184 | .cursor => |cursor_data| { | 186 | .cursor => |cursor_data| { |
| 185 | try writer.writeIntLittle(u16, self.width); | 187 | try writer.writeInt(u16, self.width, .little); |
| 186 | try writer.writeIntLittle(u16, self.height); | 188 | try writer.writeInt(u16, self.height, .little); |
| 187 | try writer.writeIntLittle(u16, cursor_data.hotspot_x); | 189 | try writer.writeInt(u16, cursor_data.hotspot_x, .little); |
| 188 | try writer.writeIntLittle(u16, cursor_data.hotspot_y); | 190 | try writer.writeInt(u16, cursor_data.hotspot_y, .little); |
| 189 | try writer.writeIntLittle(u32, self.data_size_in_bytes + 4); | 191 | try writer.writeInt(u32, self.data_size_in_bytes + 4, .little); |
| 190 | }, | 192 | }, |
| 191 | } | 193 | } |
| 192 | try writer.writeIntLittle(u16, id); | 194 | try writer.writeInt(u16, id, .little); |
| 193 | } | 195 | } |
| 194 | }; | 196 | }; |
| 195 | 197 | ||
| ... | @@ -235,21 +237,21 @@ pub const ImageFormat = enum { | ... | @@ -235,21 +237,21 @@ pub const ImageFormat = enum { |
| 235 | png, | 237 | png, |
| 236 | riff, | 238 | riff, |
| 237 | 239 | ||
| 238 | const riff_header = std.mem.readIntNative(u32, "RIFF"); | 240 | const riff_header = std.mem.readInt(u32, "RIFF", native_endian); |
| 239 | const png_signature = std.mem.readIntNative(u64, "\x89PNG\r\n\x1a\n"); | 241 | const png_signature = std.mem.readInt(u64, "\x89PNG\r\n\x1a\n", native_endian); |
| 240 | const ihdr_code = std.mem.readIntNative(u32, "IHDR"); | 242 | const ihdr_code = std.mem.readInt(u32, "IHDR", native_endian); |
| 241 | const acon_form_type = std.mem.readIntNative(u32, "ACON"); | 243 | const acon_form_type = std.mem.readInt(u32, "ACON", native_endian); |
| 242 | 244 | ||
| 243 | pub fn detect(header_bytes: *const [16]u8) ImageFormat { | 245 | pub fn detect(header_bytes: *const [16]u8) ImageFormat { |
| 244 | if (std.mem.readIntNative(u32, header_bytes[0..4]) == riff_header) return .riff; | 246 | if (std.mem.readInt(u32, header_bytes[0..4], native_endian) == riff_header) return .riff; |
| 245 | if (std.mem.readIntNative(u64, header_bytes[0..8]) == png_signature) return .png; | 247 | if (std.mem.readInt(u64, header_bytes[0..8], native_endian) == png_signature) return .png; |
| 246 | return .dib; | 248 | return .dib; |
| 247 | } | 249 | } |
| 248 | 250 | ||
| 249 | pub fn validate(format: ImageFormat, header_bytes: *const [16]u8) bool { | 251 | pub fn validate(format: ImageFormat, header_bytes: *const [16]u8) bool { |
| 250 | return switch (format) { | 252 | return switch (format) { |
| 251 | .png => std.mem.readIntNative(u32, header_bytes[12..16]) == ihdr_code, | 253 | .png => std.mem.readInt(u32, header_bytes[12..16], native_endian) == ihdr_code, |
| 252 | .riff => std.mem.readIntNative(u32, header_bytes[8..12]) == acon_form_type, | 254 | .riff => std.mem.readInt(u32, header_bytes[8..12], native_endian) == acon_form_type, |
| 253 | .dib => true, | 255 | .dib => true, |
| 254 | }; | 256 | }; |
| 255 | } | 257 | } |
src/resinator/res.zig+16-16| ... | @@ -249,14 +249,14 @@ pub const NameOrOrdinal = union(enum) { | ... | @@ -249,14 +249,14 @@ pub const NameOrOrdinal = union(enum) { |
| 249 | try writer.writeAll(std.mem.sliceAsBytes(name[0 .. name.len + 1])); | 249 | try writer.writeAll(std.mem.sliceAsBytes(name[0 .. name.len + 1])); |
| 250 | }, | 250 | }, |
| 251 | .ordinal => |ordinal| { | 251 | .ordinal => |ordinal| { |
| 252 | try writer.writeIntLittle(u16, 0xffff); | 252 | try writer.writeInt(u16, 0xffff, .little); |
| 253 | try writer.writeIntLittle(u16, ordinal); | 253 | try writer.writeInt(u16, ordinal, .little); |
| 254 | }, | 254 | }, |
| 255 | } | 255 | } |
| 256 | } | 256 | } |
| 257 | 257 | ||
| 258 | pub fn writeEmpty(writer: anytype) !void { | 258 | pub fn writeEmpty(writer: anytype) !void { |
| 259 | try writer.writeIntLittle(u16, 0); | 259 | try writer.writeInt(u16, 0, .little); |
| 260 | } | 260 | } |
| 261 | 261 | ||
| 262 | pub fn fromString(allocator: Allocator, bytes: SourceBytes) !NameOrOrdinal { | 262 | pub fn fromString(allocator: Allocator, bytes: SourceBytes) !NameOrOrdinal { |
| ... | @@ -963,19 +963,19 @@ pub const FixedFileInfo = struct { | ... | @@ -963,19 +963,19 @@ pub const FixedFileInfo = struct { |
| 963 | }; | 963 | }; |
| 964 | 964 | ||
| 965 | pub fn write(self: FixedFileInfo, writer: anytype) !void { | 965 | pub fn write(self: FixedFileInfo, writer: anytype) !void { |
| 966 | try writer.writeIntLittle(u32, signature); | 966 | try writer.writeInt(u32, signature, .little); |
| 967 | try writer.writeIntLittle(u32, version); | 967 | try writer.writeInt(u32, version, .little); |
| 968 | try writer.writeIntLittle(u32, self.file_version.mostSignificantCombinedParts()); | 968 | try writer.writeInt(u32, self.file_version.mostSignificantCombinedParts(), .little); |
| 969 | try writer.writeIntLittle(u32, self.file_version.leastSignificantCombinedParts()); | 969 | try writer.writeInt(u32, self.file_version.leastSignificantCombinedParts(), .little); |
| 970 | try writer.writeIntLittle(u32, self.product_version.mostSignificantCombinedParts()); | 970 | try writer.writeInt(u32, self.product_version.mostSignificantCombinedParts(), .little); |
| 971 | try writer.writeIntLittle(u32, self.product_version.leastSignificantCombinedParts()); | 971 | try writer.writeInt(u32, self.product_version.leastSignificantCombinedParts(), .little); |
| 972 | try writer.writeIntLittle(u32, self.file_flags_mask); | 972 | try writer.writeInt(u32, self.file_flags_mask, .little); |
| 973 | try writer.writeIntLittle(u32, self.file_flags); | 973 | try writer.writeInt(u32, self.file_flags, .little); |
| 974 | try writer.writeIntLittle(u32, self.file_os); | 974 | try writer.writeInt(u32, self.file_os, .little); |
| 975 | try writer.writeIntLittle(u32, self.file_type); | 975 | try writer.writeInt(u32, self.file_type, .little); |
| 976 | try writer.writeIntLittle(u32, self.file_subtype); | 976 | try writer.writeInt(u32, self.file_subtype, .little); |
| 977 | try writer.writeIntLittle(u32, self.file_date.mostSignificantCombinedParts()); | 977 | try writer.writeInt(u32, self.file_date.mostSignificantCombinedParts(), .little); |
| 978 | try writer.writeIntLittle(u32, self.file_date.leastSignificantCombinedParts()); | 978 | try writer.writeInt(u32, self.file_date.leastSignificantCombinedParts(), .little); |
| 979 | } | 979 | } |
| 980 | }; | 980 | }; |
| 981 | 981 |
src/value.zig+6-6| ... | @@ -778,8 +778,8 @@ pub const Value = struct { | ... | @@ -778,8 +778,8 @@ pub const Value = struct { |
| 778 | .Void => {}, | 778 | .Void => {}, |
| 779 | .Bool => { | 779 | .Bool => { |
| 780 | const byte_index = switch (endian) { | 780 | const byte_index = switch (endian) { |
| 781 | .Little => bit_offset / 8, | 781 | .little => bit_offset / 8, |
| 782 | .Big => buffer.len - bit_offset / 8 - 1, | 782 | .big => buffer.len - bit_offset / 8 - 1, |
| 783 | }; | 783 | }; |
| 784 | if (val.toBool()) { | 784 | if (val.toBool()) { |
| 785 | buffer[byte_index] |= (@as(u8, 1) << @as(u3, @intCast(bit_offset % 8))); | 785 | buffer[byte_index] |= (@as(u8, 1) << @as(u3, @intCast(bit_offset % 8))); |
| ... | @@ -815,7 +815,7 @@ pub const Value = struct { | ... | @@ -815,7 +815,7 @@ pub const Value = struct { |
| 815 | var elem_i: usize = 0; | 815 | var elem_i: usize = 0; |
| 816 | while (elem_i < len) : (elem_i += 1) { | 816 | while (elem_i < len) : (elem_i += 1) { |
| 817 | // On big-endian systems, LLVM reverses the element order of vectors by default | 817 | // On big-endian systems, LLVM reverses the element order of vectors by default |
| 818 | const tgt_elem_i = if (endian == .Big) len - elem_i - 1 else elem_i; | 818 | const tgt_elem_i = if (endian == .big) len - elem_i - 1 else elem_i; |
| 819 | const elem_val = try val.elemValue(mod, tgt_elem_i); | 819 | const elem_val = try val.elemValue(mod, tgt_elem_i); |
| 820 | try elem_val.writeToPackedMemory(elem_ty, mod, buffer, bit_offset + bits); | 820 | try elem_val.writeToPackedMemory(elem_ty, mod, buffer, bit_offset + bits); |
| 821 | bits += elem_bit_size; | 821 | bits += elem_bit_size; |
| ... | @@ -1064,8 +1064,8 @@ pub const Value = struct { | ... | @@ -1064,8 +1064,8 @@ pub const Value = struct { |
| 1064 | .Void => return Value.void, | 1064 | .Void => return Value.void, |
| 1065 | .Bool => { | 1065 | .Bool => { |
| 1066 | const byte = switch (endian) { | 1066 | const byte = switch (endian) { |
| 1067 | .Big => buffer[buffer.len - bit_offset / 8 - 1], | 1067 | .big => buffer[buffer.len - bit_offset / 8 - 1], |
| 1068 | .Little => buffer[bit_offset / 8], | 1068 | .little => buffer[bit_offset / 8], |
| 1069 | }; | 1069 | }; |
| 1070 | if (((byte >> @as(u3, @intCast(bit_offset % 8))) & 1) == 0) { | 1070 | if (((byte >> @as(u3, @intCast(bit_offset % 8))) & 1) == 0) { |
| 1071 | return Value.false; | 1071 | return Value.false; |
| ... | @@ -1127,7 +1127,7 @@ pub const Value = struct { | ... | @@ -1127,7 +1127,7 @@ pub const Value = struct { |
| 1127 | const elem_bit_size = @as(u16, @intCast(elem_ty.bitSize(mod))); | 1127 | const elem_bit_size = @as(u16, @intCast(elem_ty.bitSize(mod))); |
| 1128 | for (elems, 0..) |_, i| { | 1128 | for (elems, 0..) |_, i| { |
| 1129 | // On big-endian systems, LLVM reverses the element order of vectors by default | 1129 | // On big-endian systems, LLVM reverses the element order of vectors by default |
| 1130 | const tgt_elem_i = if (endian == .Big) elems.len - i - 1 else i; | 1130 | const tgt_elem_i = if (endian == .big) elems.len - i - 1 else i; |
| 1131 | elems[tgt_elem_i] = try (try readFromPackedMemory(elem_ty, mod, buffer, bit_offset + bits, arena)).intern(elem_ty, mod); | 1131 | elems[tgt_elem_i] = try (try readFromPackedMemory(elem_ty, mod, buffer, bit_offset + bits, arena)).intern(elem_ty, mod); |
| 1132 | bits += elem_bit_size; | 1132 | bits += elem_bit_size; |
| 1133 | } | 1133 | } |
test/behavior/align.zig+2-2| ... | @@ -616,8 +616,8 @@ test "sub-aligned pointer field access" { | ... | @@ -616,8 +616,8 @@ test "sub-aligned pointer field access" { |
| 616 | const ptr: *align(1) Header = @ptrCast(buf[1..][0..8]); | 616 | const ptr: *align(1) Header = @ptrCast(buf[1..][0..8]); |
| 617 | const x = ptr.bytes_len; | 617 | const x = ptr.bytes_len; |
| 618 | switch (builtin.cpu.arch.endian()) { | 618 | switch (builtin.cpu.arch.endian()) { |
| 619 | .Big => try expect(x == 0x06070809), | 619 | .big => try expect(x == 0x06070809), |
| 620 | .Little => try expect(x == 0x09080706), | 620 | .little => try expect(x == 0x09080706), |
| 621 | } | 621 | } |
| 622 | } | 622 | } |
| 623 | 623 |
test/behavior/bitcast.zig+5-5| ... | @@ -107,14 +107,14 @@ fn testBitCastuXToBytes(comptime N: usize) !void { | ... | @@ -107,14 +107,14 @@ fn testBitCastuXToBytes(comptime N: usize) !void { |
| 107 | 107 | ||
| 108 | const byte_count = (N + 7) / 8; | 108 | const byte_count = (N + 7) / 8; |
| 109 | switch (native_endian) { | 109 | switch (native_endian) { |
| 110 | .Little => { | 110 | .little => { |
| 111 | var byte_i = 0; | 111 | var byte_i = 0; |
| 112 | while (byte_i < (byte_count - 1)) : (byte_i += 1) { | 112 | while (byte_i < (byte_count - 1)) : (byte_i += 1) { |
| 113 | try expect(bytes[byte_i] == 0xff); | 113 | try expect(bytes[byte_i] == 0xff); |
| 114 | } | 114 | } |
| 115 | try expect(((bytes[byte_i] ^ 0xff) << -%@as(u3, @truncate(N))) == 0); | 115 | try expect(((bytes[byte_i] ^ 0xff) << -%@as(u3, @truncate(N))) == 0); |
| 116 | }, | 116 | }, |
| 117 | .Big => { | 117 | .big => { |
| 118 | var byte_i = byte_count - 1; | 118 | var byte_i = byte_count - 1; |
| 119 | while (byte_i > 0) : (byte_i -= 1) { | 119 | while (byte_i > 0) : (byte_i -= 1) { |
| 120 | try expect(bytes[byte_i] == 0xff); | 120 | try expect(bytes[byte_i] == 0xff); |
| ... | @@ -197,11 +197,11 @@ test "@bitCast extern structs at runtime and comptime" { | ... | @@ -197,11 +197,11 @@ test "@bitCast extern structs at runtime and comptime" { |
| 197 | var full = Full{ .number = 0x1234 }; | 197 | var full = Full{ .number = 0x1234 }; |
| 198 | var two_halves = @as(TwoHalves, @bitCast(full)); | 198 | var two_halves = @as(TwoHalves, @bitCast(full)); |
| 199 | switch (native_endian) { | 199 | switch (native_endian) { |
| 200 | .Big => { | 200 | .big => { |
| 201 | try expect(two_halves.half1 == 0x12); | 201 | try expect(two_halves.half1 == 0x12); |
| 202 | try expect(two_halves.half2 == 0x34); | 202 | try expect(two_halves.half2 == 0x34); |
| 203 | }, | 203 | }, |
| 204 | .Little => { | 204 | .little => { |
| 205 | try expect(two_halves.half1 == 0x34); | 205 | try expect(two_halves.half1 == 0x34); |
| 206 | try expect(two_halves.half2 == 0x12); | 206 | try expect(two_halves.half2 == 0x12); |
| 207 | }, | 207 | }, |
| ... | @@ -332,7 +332,7 @@ test "comptime @bitCast packed struct to int and back" { | ... | @@ -332,7 +332,7 @@ test "comptime @bitCast packed struct to int and back" { |
| 332 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO | 332 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 333 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; | 333 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| 334 | 334 | ||
| 335 | if (builtin.zig_backend == .stage2_llvm and native_endian == .Big) { | 335 | if (builtin.zig_backend == .stage2_llvm and native_endian == .big) { |
| 336 | // https://github.com/ziglang/zig/issues/13782 | 336 | // https://github.com/ziglang/zig/issues/13782 |
| 337 | return error.SkipZigTest; | 337 | return error.SkipZigTest; |
| 338 | } | 338 | } |
test/behavior/bugs/12169.zig+1-1| ... | @@ -7,7 +7,7 @@ test { | ... | @@ -7,7 +7,7 @@ test { |
| 7 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | 7 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| 8 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | 8 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 9 | 9 | ||
| 10 | if (comptime builtin.zig_backend == .stage2_llvm and builtin.cpu.arch.endian() == .Big) { | 10 | if (comptime builtin.zig_backend == .stage2_llvm and builtin.cpu.arch.endian() == .big) { |
| 11 | // https://github.com/ziglang/zig/issues/13782 | 11 | // https://github.com/ziglang/zig/issues/13782 |
| 12 | return error.SkipZigTest; | 12 | return error.SkipZigTest; |
| 13 | } | 13 | } |
test/behavior/bugs/1851.zig+1| ... | @@ -7,6 +7,7 @@ test "allocation and looping over 3-byte integer" { | ... | @@ -7,6 +7,7 @@ test "allocation and looping over 3-byte integer" { |
| 7 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | 7 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| 8 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO | 8 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 9 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; | 9 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| 10 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; | ||
| 10 | 11 | ||
| 11 | if (builtin.zig_backend == .stage2_llvm and builtin.os.tag == .macos) { | 12 | if (builtin.zig_backend == .stage2_llvm and builtin.os.tag == .macos) { |
| 12 | return error.SkipZigTest; // TODO | 13 | return error.SkipZigTest; // TODO |
test/behavior/cast.zig+1-1| ... | @@ -1077,7 +1077,7 @@ test "pointer reinterpret const float to int" { | ... | @@ -1077,7 +1077,7 @@ test "pointer reinterpret const float to int" { |
| 1077 | const float_ptr = &float; | 1077 | const float_ptr = &float; |
| 1078 | const int_ptr = @as(*const i32, @ptrCast(float_ptr)); | 1078 | const int_ptr = @as(*const i32, @ptrCast(float_ptr)); |
| 1079 | const int_val = int_ptr.*; | 1079 | const int_val = int_ptr.*; |
| 1080 | if (native_endian == .Little) | 1080 | if (native_endian == .little) |
| 1081 | try expect(int_val == 0x33333303) | 1081 | try expect(int_val == 0x33333303) |
| 1082 | else | 1082 | else |
| 1083 | try expect(int_val == 0x3fe33333); | 1083 | try expect(int_val == 0x3fe33333); |
test/behavior/cast_int.zig+1-1| ... | @@ -163,7 +163,7 @@ test "load non byte-sized optional value" { | ... | @@ -163,7 +163,7 @@ test "load non byte-sized optional value" { |
| 163 | } | 163 | } |
| 164 | 164 | ||
| 165 | test "load non byte-sized value in struct" { | 165 | test "load non byte-sized value in struct" { |
| 166 | if (builtin.cpu.arch.endian() != .Little) return error.SkipZigTest; // packed struct TODO | 166 | if (builtin.cpu.arch.endian() != .little) return error.SkipZigTest; // packed struct TODO |
| 167 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; | 167 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| 168 | 168 | ||
| 169 | // note: this bug is triggered by the == operator, expectEqual will hide it | 169 | // note: this bug is triggered by the == operator, expectEqual will hide it |
test/behavior/comptime_memory.zig+10-2| ... | @@ -73,7 +73,7 @@ test "type pun value and struct" { | ... | @@ -73,7 +73,7 @@ test "type pun value and struct" { |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | fn bigToNativeEndian(comptime T: type, v: T) T { | 75 | fn bigToNativeEndian(comptime T: type, v: T) T { |
| 76 | return if (endian == .Big) v else @byteSwap(v); | 76 | return if (endian == .big) v else @byteSwap(v); |
| 77 | } | 77 | } |
| 78 | test "type pun endianness" { | 78 | test "type pun endianness" { |
| 79 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | 79 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
| ... | @@ -385,7 +385,7 @@ test "accessing reinterpreted memory of parent object" { | ... | @@ -385,7 +385,7 @@ test "accessing reinterpreted memory of parent object" { |
| 385 | b: [4]u8, | 385 | b: [4]u8, |
| 386 | c: f32, | 386 | c: f32, |
| 387 | }; | 387 | }; |
| 388 | const expected = if (endian == .Little) 102 else 38; | 388 | const expected = if (endian == .little) 102 else 38; |
| 389 | 389 | ||
| 390 | comptime { | 390 | comptime { |
| 391 | const x = S{ | 391 | const x = S{ |
| ... | @@ -451,3 +451,11 @@ test "type pun null pointer-like optional" { | ... | @@ -451,3 +451,11 @@ test "type pun null pointer-like optional" { |
| 451 | // note that expectEqual hides the bug | 451 | // note that expectEqual hides the bug |
| 452 | try testing.expect(@as(*const ?*i8, @ptrCast(&p)).* == null); | 452 | try testing.expect(@as(*const ?*i8, @ptrCast(&p)).* == null); |
| 453 | } | 453 | } |
| 454 | |||
| 455 | test "write empty array to end" { | ||
| 456 | comptime var array: [5]u8 = "hello".*; | ||
| 457 | array[5..5].* = .{}; | ||
| 458 | array[5..5].* = [0]u8{}; | ||
| 459 | array[5..5].* = [_]u8{}; | ||
| 460 | try testing.expectEqualStrings("hello", &array); | ||
| 461 | } |
test/behavior/packed-struct.zig+12-12| ... | @@ -165,7 +165,7 @@ test "correct sizeOf and offsets in packed structs" { | ... | @@ -165,7 +165,7 @@ test "correct sizeOf and offsets in packed structs" { |
| 165 | try expectEqual(22, @bitOffsetOf(PStruct, "u10_b")); | 165 | try expectEqual(22, @bitOffsetOf(PStruct, "u10_b")); |
| 166 | try expectEqual(4, @sizeOf(PStruct)); | 166 | try expectEqual(4, @sizeOf(PStruct)); |
| 167 | 167 | ||
| 168 | if (native_endian == .Little) { | 168 | if (native_endian == .little) { |
| 169 | const s1 = @as(PStruct, @bitCast(@as(u32, 0x12345678))); | 169 | const s1 = @as(PStruct, @bitCast(@as(u32, 0x12345678))); |
| 170 | try expectEqual(false, s1.bool_a); | 170 | try expectEqual(false, s1.bool_a); |
| 171 | try expectEqual(false, s1.bool_b); | 171 | try expectEqual(false, s1.bool_b); |
| ... | @@ -206,7 +206,7 @@ test "nested packed structs" { | ... | @@ -206,7 +206,7 @@ test "nested packed structs" { |
| 206 | try expectEqual(3, @offsetOf(S3, "y")); | 206 | try expectEqual(3, @offsetOf(S3, "y")); |
| 207 | try expectEqual(24, @bitOffsetOf(S3, "y")); | 207 | try expectEqual(24, @bitOffsetOf(S3, "y")); |
| 208 | 208 | ||
| 209 | if (native_endian == .Little) { | 209 | if (native_endian == .little) { |
| 210 | const s3 = @as(S3Padded, @bitCast(@as(u64, 0xe952d5c71ff4))).s3; | 210 | const s3 = @as(S3Padded, @bitCast(@as(u64, 0xe952d5c71ff4))).s3; |
| 211 | try expectEqual(@as(u8, 0xf4), s3.x.a); | 211 | try expectEqual(@as(u8, 0xf4), s3.x.a); |
| 212 | try expectEqual(@as(u8, 0x1f), s3.x.b); | 212 | try expectEqual(@as(u8, 0x1f), s3.x.b); |
| ... | @@ -258,7 +258,7 @@ test "nested packed struct unaligned" { | ... | @@ -258,7 +258,7 @@ test "nested packed struct unaligned" { |
| 258 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | 258 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 259 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO | 259 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 260 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; | 260 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| 261 | if (native_endian != .Little) return error.SkipZigTest; // Byte aligned packed struct field pointers have not been implemented yet | 261 | if (native_endian != .little) return error.SkipZigTest; // Byte aligned packed struct field pointers have not been implemented yet |
| 262 | 262 | ||
| 263 | const S1 = packed struct { | 263 | const S1 = packed struct { |
| 264 | a: u4, | 264 | a: u4, |
| ... | @@ -352,13 +352,13 @@ test "byte-aligned field pointer offsets" { | ... | @@ -352,13 +352,13 @@ test "byte-aligned field pointer offsets" { |
| 352 | .d = 4, | 352 | .d = 4, |
| 353 | }; | 353 | }; |
| 354 | switch (comptime builtin.cpu.arch.endian()) { | 354 | switch (comptime builtin.cpu.arch.endian()) { |
| 355 | .Little => { | 355 | .little => { |
| 356 | comptime assert(@TypeOf(&a.a) == *align(4) u8); | 356 | comptime assert(@TypeOf(&a.a) == *align(4) u8); |
| 357 | comptime assert(@TypeOf(&a.b) == *u8); | 357 | comptime assert(@TypeOf(&a.b) == *u8); |
| 358 | comptime assert(@TypeOf(&a.c) == *align(2) u8); | 358 | comptime assert(@TypeOf(&a.c) == *align(2) u8); |
| 359 | comptime assert(@TypeOf(&a.d) == *u8); | 359 | comptime assert(@TypeOf(&a.d) == *u8); |
| 360 | }, | 360 | }, |
| 361 | .Big => { | 361 | .big => { |
| 362 | // TODO re-evaluate packed struct endianness | 362 | // TODO re-evaluate packed struct endianness |
| 363 | comptime assert(@TypeOf(&a.a) == *align(4:0:4) u8); | 363 | comptime assert(@TypeOf(&a.a) == *align(4:0:4) u8); |
| 364 | comptime assert(@TypeOf(&a.b) == *align(4:8:4) u8); | 364 | comptime assert(@TypeOf(&a.b) == *align(4:8:4) u8); |
| ... | @@ -400,11 +400,11 @@ test "byte-aligned field pointer offsets" { | ... | @@ -400,11 +400,11 @@ test "byte-aligned field pointer offsets" { |
| 400 | .b = 2, | 400 | .b = 2, |
| 401 | }; | 401 | }; |
| 402 | switch (comptime builtin.cpu.arch.endian()) { | 402 | switch (comptime builtin.cpu.arch.endian()) { |
| 403 | .Little => { | 403 | .little => { |
| 404 | comptime assert(@TypeOf(&b.a) == *align(4) u16); | 404 | comptime assert(@TypeOf(&b.a) == *align(4) u16); |
| 405 | comptime assert(@TypeOf(&b.b) == *u16); | 405 | comptime assert(@TypeOf(&b.b) == *u16); |
| 406 | }, | 406 | }, |
| 407 | .Big => { | 407 | .big => { |
| 408 | comptime assert(@TypeOf(&b.a) == *align(4:0:4) u16); | 408 | comptime assert(@TypeOf(&b.a) == *align(4:0:4) u16); |
| 409 | comptime assert(@TypeOf(&b.b) == *align(4:16:4) u16); | 409 | comptime assert(@TypeOf(&b.b) == *align(4:16:4) u16); |
| 410 | }, | 410 | }, |
| ... | @@ -433,7 +433,7 @@ test "nested packed struct field pointers" { | ... | @@ -433,7 +433,7 @@ test "nested packed struct field pointers" { |
| 433 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; | 433 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| 434 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // ubsan unaligned pointer access | 434 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // ubsan unaligned pointer access |
| 435 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; | 435 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; |
| 436 | if (native_endian != .Little) return error.SkipZigTest; // Byte aligned packed struct field pointers have not been implemented yet | 436 | if (native_endian != .little) return error.SkipZigTest; // Byte aligned packed struct field pointers have not been implemented yet |
| 437 | 437 | ||
| 438 | const S2 = packed struct { | 438 | const S2 = packed struct { |
| 439 | base: u8, | 439 | base: u8, |
| ... | @@ -491,7 +491,7 @@ test "@intFromPtr on a packed struct field" { | ... | @@ -491,7 +491,7 @@ test "@intFromPtr on a packed struct field" { |
| 491 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | 491 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 492 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO | 492 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 493 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; | 493 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| 494 | if (native_endian != .Little) return error.SkipZigTest; | 494 | if (native_endian != .little) return error.SkipZigTest; |
| 495 | 495 | ||
| 496 | const S = struct { | 496 | const S = struct { |
| 497 | const P = packed struct { | 497 | const P = packed struct { |
| ... | @@ -515,7 +515,7 @@ test "@intFromPtr on a packed struct field unaligned and nested" { | ... | @@ -515,7 +515,7 @@ test "@intFromPtr on a packed struct field unaligned and nested" { |
| 515 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO | 515 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 516 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; | 516 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| 517 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; | 517 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; |
| 518 | if (native_endian != .Little) return error.SkipZigTest; // Byte aligned packed struct field pointers have not been implemented yet | 518 | if (native_endian != .little) return error.SkipZigTest; // Byte aligned packed struct field pointers have not been implemented yet |
| 519 | 519 | ||
| 520 | const S1 = packed struct { | 520 | const S1 = packed struct { |
| 521 | a: u4, | 521 | a: u4, |
| ... | @@ -921,11 +921,11 @@ test "overaligned pointer to packed struct" { | ... | @@ -921,11 +921,11 @@ test "overaligned pointer to packed struct" { |
| 921 | var foo: S align(4) = .{ .a = 123, .b = 456 }; | 921 | var foo: S align(4) = .{ .a = 123, .b = 456 }; |
| 922 | const ptr: *align(4) S = &foo; | 922 | const ptr: *align(4) S = &foo; |
| 923 | switch (comptime builtin.cpu.arch.endian()) { | 923 | switch (comptime builtin.cpu.arch.endian()) { |
| 924 | .Little => { | 924 | .little => { |
| 925 | const ptr_to_b: *u32 = &ptr.b; | 925 | const ptr_to_b: *u32 = &ptr.b; |
| 926 | try expect(ptr_to_b.* == 456); | 926 | try expect(ptr_to_b.* == 456); |
| 927 | }, | 927 | }, |
| 928 | .Big => { | 928 | .big => { |
| 929 | // Byte aligned packed struct field pointers have not been implemented yet. | 929 | // Byte aligned packed struct field pointers have not been implemented yet. |
| 930 | const ptr_to_a: *align(4:0:8) u32 = &ptr.a; | 930 | const ptr_to_a: *align(4:0:8) u32 = &ptr.a; |
| 931 | try expect(ptr_to_a.* == 123); | 931 | try expect(ptr_to_a.* == 123); |
test/behavior/packed_struct_explicit_backing_int.zig+1-1| ... | @@ -24,7 +24,7 @@ test "packed struct explicit backing integer" { | ... | @@ -24,7 +24,7 @@ test "packed struct explicit backing integer" { |
| 24 | try expectEqual(3, @offsetOf(S3, "y")); | 24 | try expectEqual(3, @offsetOf(S3, "y")); |
| 25 | try expectEqual(24, @bitOffsetOf(S3, "y")); | 25 | try expectEqual(24, @bitOffsetOf(S3, "y")); |
| 26 | 26 | ||
| 27 | if (native_endian == .Little) { | 27 | if (native_endian == .little) { |
| 28 | const s3 = @as(S3Padded, @bitCast(@as(u64, 0xe952d5c71ff4))).s3; | 28 | const s3 = @as(S3Padded, @bitCast(@as(u64, 0xe952d5c71ff4))).s3; |
| 29 | try expectEqual(@as(u8, 0xf4), s3.x.a); | 29 | try expectEqual(@as(u8, 0xf4), s3.x.a); |
| 30 | try expectEqual(@as(u8, 0x1f), s3.x.b); | 30 | try expectEqual(@as(u8, 0x1f), s3.x.b); |
test/behavior/ptrcast.zig+7-7| ... | @@ -13,8 +13,8 @@ test "reinterpret bytes as integer with nonzero offset" { | ... | @@ -13,8 +13,8 @@ test "reinterpret bytes as integer with nonzero offset" { |
| 13 | fn testReinterpretBytesAsInteger() !void { | 13 | fn testReinterpretBytesAsInteger() !void { |
| 14 | const bytes = "\x12\x34\x56\x78\xab"; | 14 | const bytes = "\x12\x34\x56\x78\xab"; |
| 15 | const expected = switch (native_endian) { | 15 | const expected = switch (native_endian) { |
| 16 | .Little => 0xab785634, | 16 | .little => 0xab785634, |
| 17 | .Big => 0x345678ab, | 17 | .big => 0x345678ab, |
| 18 | }; | 18 | }; |
| 19 | try expect(@as(*align(1) const u32, @ptrCast(bytes[1..5])).* == expected); | 19 | try expect(@as(*align(1) const u32, @ptrCast(bytes[1..5])).* == expected); |
| 20 | } | 20 | } |
| ... | @@ -48,8 +48,8 @@ fn testReinterpretStructWrappedBytesAsInteger() !void { | ... | @@ -48,8 +48,8 @@ fn testReinterpretStructWrappedBytesAsInteger() !void { |
| 48 | const S = struct { bytes: [5:0]u8 }; | 48 | const S = struct { bytes: [5:0]u8 }; |
| 49 | const obj = S{ .bytes = "\x12\x34\x56\x78\xab".* }; | 49 | const obj = S{ .bytes = "\x12\x34\x56\x78\xab".* }; |
| 50 | const expected = switch (native_endian) { | 50 | const expected = switch (native_endian) { |
| 51 | .Little => 0xab785634, | 51 | .little => 0xab785634, |
| 52 | .Big => 0x345678ab, | 52 | .big => 0x345678ab, |
| 53 | }; | 53 | }; |
| 54 | try expect(@as(*align(1) const u32, @ptrCast(obj.bytes[1..5])).* == expected); | 54 | try expect(@as(*align(1) const u32, @ptrCast(obj.bytes[1..5])).* == expected); |
| 55 | } | 55 | } |
| ... | @@ -176,7 +176,7 @@ test "reinterpret struct field at comptime" { | ... | @@ -176,7 +176,7 @@ test "reinterpret struct field at comptime" { |
| 176 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; | 176 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| 177 | 177 | ||
| 178 | const numNative = comptime Bytes.init(0x12345678); | 178 | const numNative = comptime Bytes.init(0x12345678); |
| 179 | if (native_endian != .Little) { | 179 | if (native_endian != .little) { |
| 180 | try expect(std.mem.eql(u8, &[_]u8{ 0x12, 0x34, 0x56, 0x78 }, &numNative.bytes)); | 180 | try expect(std.mem.eql(u8, &[_]u8{ 0x12, 0x34, 0x56, 0x78 }, &numNative.bytes)); |
| 181 | } else { | 181 | } else { |
| 182 | try expect(std.mem.eql(u8, &[_]u8{ 0x78, 0x56, 0x34, 0x12 }, &numNative.bytes)); | 182 | try expect(std.mem.eql(u8, &[_]u8{ 0x78, 0x56, 0x34, 0x12 }, &numNative.bytes)); |
| ... | @@ -201,7 +201,7 @@ test "ptrcast of const integer has the correct object size" { | ... | @@ -201,7 +201,7 @@ test "ptrcast of const integer has the correct object size" { |
| 201 | const is_bytes = @as([*]const u8, @ptrCast(&is_value))[0..@sizeOf(isize)]; | 201 | const is_bytes = @as([*]const u8, @ptrCast(&is_value))[0..@sizeOf(isize)]; |
| 202 | if (@sizeOf(isize) == 8) { | 202 | if (@sizeOf(isize) == 8) { |
| 203 | switch (native_endian) { | 203 | switch (native_endian) { |
| 204 | .Little => { | 204 | .little => { |
| 205 | try expect(is_bytes[0] == 0xff); | 205 | try expect(is_bytes[0] == 0xff); |
| 206 | try expect(is_bytes[1] == 0xff); | 206 | try expect(is_bytes[1] == 0xff); |
| 207 | try expect(is_bytes[2] == 0xff); | 207 | try expect(is_bytes[2] == 0xff); |
| ... | @@ -212,7 +212,7 @@ test "ptrcast of const integer has the correct object size" { | ... | @@ -212,7 +212,7 @@ test "ptrcast of const integer has the correct object size" { |
| 212 | try expect(is_bytes[6] == 0xff); | 212 | try expect(is_bytes[6] == 0xff); |
| 213 | try expect(is_bytes[7] == 0x7f); | 213 | try expect(is_bytes[7] == 0x7f); |
| 214 | }, | 214 | }, |
| 215 | .Big => { | 215 | .big => { |
| 216 | try expect(is_bytes[0] == 0x7f); | 216 | try expect(is_bytes[0] == 0x7f); |
| 217 | try expect(is_bytes[1] == 0xff); | 217 | try expect(is_bytes[1] == 0xff); |
| 218 | try expect(is_bytes[2] == 0xff); | 218 | try expect(is_bytes[2] == 0xff); |
test/behavior/struct.zig+2-1| ... | @@ -1351,6 +1351,7 @@ test "under-aligned struct field" { | ... | @@ -1351,6 +1351,7 @@ test "under-aligned struct field" { |
| 1351 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | 1351 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 1352 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO | 1352 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 1353 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; | 1353 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| 1354 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; | ||
| 1354 | 1355 | ||
| 1355 | const U = extern union { | 1356 | const U = extern union { |
| 1356 | fd: i32, | 1357 | fd: i32, |
| ... | @@ -1364,7 +1365,7 @@ test "under-aligned struct field" { | ... | @@ -1364,7 +1365,7 @@ test "under-aligned struct field" { |
| 1364 | var runtime: usize = 1234; | 1365 | var runtime: usize = 1234; |
| 1365 | const ptr = &S{ .events = 0, .data = .{ .u64 = runtime } }; | 1366 | const ptr = &S{ .events = 0, .data = .{ .u64 = runtime } }; |
| 1366 | const array = @as(*const [12]u8, @ptrCast(ptr)); | 1367 | const array = @as(*const [12]u8, @ptrCast(ptr)); |
| 1367 | const result = std.mem.readIntNative(u64, array[4..12]); | 1368 | const result = std.mem.readInt(u64, array[4..12], native_endian); |
| 1368 | try expect(result == 1234); | 1369 | try expect(result == 1234); |
| 1369 | } | 1370 | } |
| 1370 | 1371 |
test/behavior/union.zig+3-3| ... | @@ -1686,7 +1686,7 @@ test "memset packed union" { | ... | @@ -1686,7 +1686,7 @@ test "memset packed union" { |
| 1686 | } | 1686 | } |
| 1687 | 1687 | ||
| 1688 | fn littleToNativeEndian(comptime T: type, v: T) T { | 1688 | fn littleToNativeEndian(comptime T: type, v: T) T { |
| 1689 | return if (endian == .Little) v else @byteSwap(v); | 1689 | return if (endian == .little) v else @byteSwap(v); |
| 1690 | } | 1690 | } |
| 1691 | 1691 | ||
| 1692 | test "reinterpret extern union" { | 1692 | test "reinterpret extern union" { |
| ... | @@ -1723,8 +1723,8 @@ test "reinterpret extern union" { | ... | @@ -1723,8 +1723,8 @@ test "reinterpret extern union" { |
| 1723 | 1723 | ||
| 1724 | { | 1724 | { |
| 1725 | const expected, const mask = switch (endian) { | 1725 | const expected, const mask = switch (endian) { |
| 1726 | .Little => .{ 0x2a, 0xff }, | 1726 | .little => .{ 0x2a, 0xff }, |
| 1727 | .Big => .{ 0x2a000000, 0xff000000 }, | 1727 | .big => .{ 0x2a000000, 0xff000000 }, |
| 1728 | }; | 1728 | }; |
| 1729 | 1729 | ||
| 1730 | try expectEqual(@as(u8, 0x2a), u.foo); | 1730 | try expectEqual(@as(u8, 0x2a), u.foo); |
tools/gen_stubs.zig+4-4| ... | @@ -234,12 +234,12 @@ pub fn main() !void { | ... | @@ -234,12 +234,12 @@ pub fn main() !void { |
| 234 | 234 | ||
| 235 | switch (header.is_64) { | 235 | switch (header.is_64) { |
| 236 | true => switch (header.endian) { | 236 | true => switch (header.endian) { |
| 237 | .Big => try parseElf(parse, true, .Big), | 237 | .big => try parseElf(parse, true, .big), |
| 238 | .Little => try parseElf(parse, true, .Little), | 238 | .little => try parseElf(parse, true, .little), |
| 239 | }, | 239 | }, |
| 240 | false => switch (header.endian) { | 240 | false => switch (header.endian) { |
| 241 | .Big => try parseElf(parse, false, .Big), | 241 | .big => try parseElf(parse, false, .big), |
| 242 | .Little => try parseElf(parse, false, .Little), | 242 | .little => try parseElf(parse, false, .little), |
| 243 | }, | 243 | }, |
| 244 | } | 244 | } |
| 245 | } | 245 | } |