std.crypto.asn1: fix merge conflicts

This commit is contained in:
Andrew Kelley
2025-02-22 17:41:44 -08:00
parent ed7004a2ee
commit dd54c48aa2
5 changed files with 34 additions and 36 deletions

View File

@@ -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 {

View File

@@ -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);

View File

@@ -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) {

View File

@@ -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)),
}

View File

@@ -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 {