| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use fmt; |
| 5 |
use memio; |
| 6 |
|
| 7 |
|
| 8 |
// Data types specified in the standard |
| 9 |
export type class = enum u8 { |
| 10 |
UNIVERSAL = 0x0, |
| 11 |
APPLICATION = 0x1, |
| 12 |
CONTEXT = 0x2, |
| 13 |
PRIVATE = 0x3, |
| 14 |
}; |
| 15 |
|
| 16 |
// String representation of 'c'. |
| 17 |
export fn strclass(c: class) str = { |
| 18 |
switch (c) { |
| 19 |
case class::UNIVERSAL => |
| 20 |
return "UNIVERSAL"; |
| 21 |
case class::APPLICATION => |
| 22 |
return "APPLICATION"; |
| 23 |
case class::CONTEXT => |
| 24 |
return "CONTEXT_SPECIFIC"; |
| 25 |
case class::PRIVATE => |
| 26 |
return "PRIVATE"; |
| 27 |
}; |
| 28 |
}; |
| 29 |
|
| 30 |
// Universal tags as defined in x.690. Not all are supported by this |
| 31 |
// implemenation. |
| 32 |
export type utag = enum u8 { |
| 33 |
RESERVED = 0x00, |
| 34 |
BOOLEAN = 0x01, |
| 35 |
INTEGER = 0x02, |
| 36 |
BITSTRING = 0x03, |
| 37 |
OCTET_STRING = 0x04, |
| 38 |
NULL = 0x05, |
| 39 |
OID = 0x06, |
| 40 |
OBJECT_DESCRIPTOR = 0x07, |
| 41 |
EXTERNAL = 0x08, |
| 42 |
REAL = 0x09, |
| 43 |
ENUMERATED = 0x0a, |
| 44 |
EMBEDDED_PDV = 0x0b, |
| 45 |
UTF8_STRING = 0x0c, |
| 46 |
RELATIVE_OID = 0x0d, |
| 47 |
TIME = 0x0e, |
| 48 |
RESERVED2 = 0x0f, |
| 49 |
SEQUENCE = 0x10, |
| 50 |
SET = 0x11, |
| 51 |
NUMERIC_STRING = 0x12, |
| 52 |
PRINTABLE_STRING = 0x13, |
| 53 |
TELETEX_STRING = 0x14, // T61String |
| 54 |
VIDEOTEX_STRING = 0x15, |
| 55 |
IA5_STRING = 0x16, |
| 56 |
UTC_TIME = 0x17, |
| 57 |
GENERALIZED_TIME = 0x18, |
| 58 |
GRAPHIC_STRING = 0x19, |
| 59 |
VISIBLE_STRING = 0x1a, // iso646String |
| 60 |
GENERAL_STRING = 0x1b, |
| 61 |
UNIVERSAL_STRING = 0x1c, |
| 62 |
UNKNOWN = 0x1d, |
| 63 |
BMP_STRING = 0x1e, |
| 64 |
DATE = 0x1f, |
| 65 |
TIME_OF_DAY = 0x20, |
| 66 |
DATE_TIME = 0x21, |
| 67 |
DURATION = 0x22, |
| 68 |
OID_IRI = 0x23, |
| 69 |
OID_RELATIVE_IRI = 0x24, |
| 70 |
}; |
| 71 |
|
| 72 |
// String representation of universal tag ids. May return a statically allocated |
| 73 |
// string and will be overwritten on the next call. |
| 74 |
export fn strtag(dh: head) str = { |
| 75 |
static let tagstrbuf: [128]u8 = [0...]; |
| 76 |
|
| 77 |
if (dh.class != class::UNIVERSAL) { |
| 78 |
let tagstr = memio::fixed(tagstrbuf); |
| 79 |
|
| 80 |
fmt::fprint(&tagstr, "[")!; |
| 81 |
if (dh.class != class::CONTEXT) { |
| 82 |
fmt::fprintf(&tagstr, "{} ", strclass(dh.class))!; |
| 83 |
}; |
| 84 |
fmt::fprintf(&tagstr, "{:x}]", dh.tagid)!; |
| 85 |
return memio::string(&tagstr)!; |
| 86 |
}; |
| 87 |
|
| 88 |
if (dh.tagid >> 8 != 0) { |
| 89 |
return "UNKNOWN"; |
| 90 |
}; |
| 91 |
|
| 92 |
switch (dh.tagid: u8) { |
| 93 |
case utag::BOOLEAN => |
| 94 |
return "BOOLEAN"; |
| 95 |
case utag::INTEGER => |
| 96 |
return "INTEGER"; |
| 97 |
case utag::BITSTRING => |
| 98 |
return "BITSTRING"; |
| 99 |
case utag::OCTET_STRING => |
| 100 |
return "OCTET_STRING"; |
| 101 |
case utag::NULL => |
| 102 |
return "NULL"; |
| 103 |
case utag::OID => |
| 104 |
return "OBJECT_IDENTIFIER"; |
| 105 |
case utag::OBJECT_DESCRIPTOR => |
| 106 |
return "OBJECT_DESCRIPTOR"; |
| 107 |
case utag::EXTERNAL => |
| 108 |
return "EXTERNAL"; |
| 109 |
case utag::REAL => |
| 110 |
return "REAL"; |
| 111 |
case utag::ENUMERATED => |
| 112 |
return "ENUMERATED"; |
| 113 |
case utag::EMBEDDED_PDV => |
| 114 |
return "EMBEDDED_PDV"; |
| 115 |
case utag::UTF8_STRING => |
| 116 |
return "UTF8_STRING"; |
| 117 |
case utag::RELATIVE_OID => |
| 118 |
return "RELATIVE_OID"; |
| 119 |
case utag::TIME => |
| 120 |
return "TIME"; |
| 121 |
case utag::SEQUENCE => |
| 122 |
return "SEQUENCE"; |
| 123 |
case utag::SET => |
| 124 |
return "SET"; |
| 125 |
case utag::PRINTABLE_STRING => |
| 126 |
return "PRINTABLE_STRING"; |
| 127 |
case utag::TELETEX_STRING => |
| 128 |
return "TELETEX_STRING"; |
| 129 |
case utag::UTC_TIME => |
| 130 |
return "UTC_TIME"; |
| 131 |
case => |
| 132 |
return "UNKNOWN"; |
| 133 |
}; |
| 134 |
}; |