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