From dd54c48aa249ce7b7a66c0f2568c1e5fa84e6012 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 22 Feb 2025 17:41:44 -0800 Subject: [PATCH] std.crypto.asn1: fix merge conflicts --- lib/std/crypto/asn1.zig | 22 +++++++++++----------- lib/std/crypto/asn1/Oid.zig | 4 ++-- lib/std/crypto/asn1/der/Decoder.zig | 22 ++++++++++------------ lib/std/crypto/asn1/der/Encoder.zig | 14 +++++++------- lib/std/crypto/asn1/test.zig | 8 ++++---- 5 files changed, 34 insertions(+), 36 deletions(-) diff --git a/lib/std/crypto/asn1.zig b/lib/std/crypto/asn1.zig index b9c0a9e109..7921c70118 100644 --- a/lib/std/crypto/asn1.zig +++ b/lib/std/crypto/asn1.zig @@ -138,22 +138,22 @@ pub const Tag = struct { pub fn fromZig(comptime T: type) Tag { switch (@typeInfo(T)) { - .Struct, .Enum, .Union => { + .@"struct", .@"enum", .@"union" => { if (@hasDecl(T, "asn1_tag")) return T.asn1_tag; }, else => {}, } switch (@typeInfo(T)) { - .Struct, .Union => return universal(.sequence, true), - .Bool => return universal(.boolean, false), - .Int => return universal(.integer, false), - .Enum => |e| { + .@"struct", .@"union" => return universal(.sequence, true), + .bool => return universal(.boolean, false), + .int => return universal(.integer, false), + .@"enum" => |e| { if (@hasDecl(T, "oids")) return Oid.asn1_tag; return universal(if (e.is_exhaustive) .enumerated else .integer, false); }, - .Optional => |o| return fromZig(o.child), - .Null => return universal(.null, false), + .optional => |o| return fromZig(o.child), + .null => return universal(.null, false), else => @compileError("cannot map Zig type to asn1_tag " ++ @typeName(T)), } } @@ -266,12 +266,12 @@ pub const FieldTag = struct { class: Tag.Class, explicit: bool = true, - pub fn explicit(number: std.meta.Tag(Tag.Number), class: Tag.Class) FieldTag { - return FieldTag{ .number = number, .class = class, .explicit = true }; + pub fn initExplicit(number: std.meta.Tag(Tag.Number), class: Tag.Class) FieldTag { + return .{ .number = number, .class = class, .explicit = true }; } - pub fn implicit(number: std.meta.Tag(Tag.Number), class: Tag.Class) FieldTag { - return FieldTag{ .number = number, .class = class, .explicit = false }; + pub fn initImplicit(number: std.meta.Tag(Tag.Number), class: Tag.Class) FieldTag { + return .{ .number = number, .class = class, .explicit = false }; } pub fn fromContainer(comptime Container: type, comptime field_name: []const u8) ?FieldTag { diff --git a/lib/std/crypto/asn1/Oid.zig b/lib/std/crypto/asn1/Oid.zig index 897b4050bf..edca552050 100644 --- a/lib/std/crypto/asn1/Oid.zig +++ b/lib/std/crypto/asn1/Oid.zig @@ -147,7 +147,7 @@ pub fn fromDotComptime(comptime dot_notation: []const u8) Oid { /// - Oid -> enum /// - Enum -> oid pub fn StaticMap(comptime Enum: type) type { - const enum_info = @typeInfo(Enum).Enum; + const enum_info = @typeInfo(Enum).@"enum"; const EnumToOid = std.EnumArray(Enum, []const u8); const ReturnType = struct { oid_to_enum: std.StaticStringMap(Enum), @@ -165,7 +165,7 @@ pub fn StaticMap(comptime Enum: type) type { return struct { pub fn initComptime(comptime key_pairs: anytype) ReturnType { - const struct_info = @typeInfo(@TypeOf(key_pairs)).Struct; + const struct_info = @typeInfo(@TypeOf(key_pairs)).@"struct"; const error_msg = "Each field of '" ++ @typeName(Enum) ++ "' must map to exactly one OID"; if (!enum_info.is_exhaustive or enum_info.fields.len != struct_info.fields.len) { @compileError(error_msg); diff --git a/lib/std/crypto/asn1/der/Decoder.zig b/lib/std/crypto/asn1/der/Decoder.zig index 2eedbee957..333e52cdf3 100644 --- a/lib/std/crypto/asn1/der/Decoder.zig +++ b/lib/std/crypto/asn1/der/Decoder.zig @@ -19,7 +19,7 @@ pub fn any(self: *Decoder, comptime T: type) !T { const tag = Tag.fromZig(T).toExpected(); switch (@typeInfo(T)) { - .Struct => { + .@"struct" => { const ele = try self.element(tag); defer self.index = ele.slice.end; // don't force parsing all fields @@ -37,22 +37,20 @@ pub fn any(self: *Decoder, comptime T: type) !T { } @field(res, f.name) = self.any(f.type) catch |err| brk: { - if (f.default_value) |d| { - break :brk @as(*const f.type, @alignCast(@ptrCast(d))).*; + if (f.defaultValue()) |d| { + break :brk d; } return err; }; // DER encodes null values by skipping them. - if (@typeInfo(f.type) == .Optional and @field(res, f.name) == null) { - if (f.default_value) |d| { - @field(res, f.name) = @as(*const f.type, @alignCast(@ptrCast(d))).*; - } + if (@typeInfo(f.type) == .optional and @field(res, f.name) == null) { + if (f.defaultValue()) |d| @field(res, f.name) = d; } } return res; }, - .Bool => { + .bool => { const ele = try self.element(tag); const bytes = self.view(ele); if (bytes.len != 1) return error.InvalidBool; @@ -63,12 +61,12 @@ pub fn any(self: *Decoder, comptime T: type) !T { else => error.InvalidBool, }; }, - .Int => { + .int => { const ele = try self.element(tag); const bytes = self.view(ele); return try int(T, bytes); }, - .Enum => |e| { + .@"enum" => |e| { const ele = try self.element(tag); const bytes = self.view(ele); if (@hasDecl(T, "oids")) { @@ -76,7 +74,7 @@ pub fn any(self: *Decoder, comptime T: type) !T { } return @enumFromInt(try int(e.tag_type, bytes)); }, - .Optional => |o| return self.any(o.child) catch return null, + .optional => |o| return self.any(o.child) catch return null, else => @compileError("cannot decode type " ++ @typeName(T)), } } @@ -113,7 +111,7 @@ pub fn view(self: Decoder, elem: Element) []const u8 { } fn int(comptime T: type, value: []const u8) error{ NonCanonical, LargeValue }!T { - if (@typeInfo(T).Int.bits % 8 != 0) @compileError("T must be byte aligned"); + if (@typeInfo(T).int.bits % 8 != 0) @compileError("T must be byte aligned"); var bytes = value; if (bytes.len >= 2) { diff --git a/lib/std/crypto/asn1/der/Encoder.zig b/lib/std/crypto/asn1/der/Encoder.zig index 939a1a5aa7..da861e3d7d 100644 --- a/lib/std/crypto/asn1/der/Encoder.zig +++ b/lib/std/crypto/asn1/der/Encoder.zig @@ -28,7 +28,7 @@ fn anyTag(self: *Encoder, tag_: Tag, val: anytype) !void { const merged_tag = self.mergedTag(tag_); switch (@typeInfo(T)) { - .Struct => |info| { + .@"struct" => |info| { inline for (0..info.fields.len) |i| { const f = info.fields[info.fields.len - i - 1]; const field_val = @field(val, f.name); @@ -36,7 +36,7 @@ fn anyTag(self: *Encoder, tag_: Tag, val: anytype) !void { // > The encoding of a set value or sequence value shall not include an encoding for any // > component value which is equal to its default value. - const is_default = if (f.is_comptime) false else if (f.default_value) |v| brk: { + const is_default = if (f.is_comptime) false else if (f.default_value_ptr) |v| brk: { const default_val: *const f.type = @alignCast(@ptrCast(v)); break :brk std.mem.eql(u8, std.mem.asBytes(default_val), std.mem.asBytes(&field_val)); } else false; @@ -57,17 +57,17 @@ fn anyTag(self: *Encoder, tag_: Tag, val: anytype) !void { } } }, - .Bool => try self.buffer.prependSlice(&[_]u8{if (val) 0xff else 0}), - .Int => try self.int(T, val), - .Enum => |e| { + .bool => try self.buffer.prependSlice(&[_]u8{if (val) 0xff else 0}), + .int => try self.int(T, val), + .@"enum" => |e| { if (@hasDecl(T, "oids")) { return self.any(T.oids.enumToOid(val)); } else { try self.int(e.tag_type, @intFromEnum(val)); } }, - .Optional => if (val) |v| return try self.anyTag(tag_, v), - .Null => {}, + .optional => if (val) |v| return try self.anyTag(tag_, v), + .null => {}, else => @compileError("cannot encode type " ++ @typeName(T)), } diff --git a/lib/std/crypto/asn1/test.zig b/lib/std/crypto/asn1/test.zig index 261f5a4310..fe12cba819 100644 --- a/lib/std/crypto/asn1/test.zig +++ b/lib/std/crypto/asn1/test.zig @@ -17,10 +17,10 @@ const AllTypes = struct { h: asn1.Any, pub const asn1_tags = .{ - .a = FieldTag.explicit(0, .context_specific), - .b = FieldTag.explicit(1, .context_specific), - .c = FieldTag.implicit(2, .context_specific), - .g = FieldTag.implicit(3, .context_specific), + .a = FieldTag.initExplicit(0, .context_specific), + .b = FieldTag.initExplicit(1, .context_specific), + .c = FieldTag.initImplicit(2, .context_specific), + .g = FieldTag.initImplicit(3, .context_specific), }; const C = enum {