summaryrefslogtreecommitdiff
path: root/module/asn1.m
blob: 3ceb85dedd65d7c0574977dd0907be719940ec61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
ASN1: module {
	PATH: con "/dis/lib/asn1.dis";

	# Tag classes
	Universal : con 0;
	Application : con 16r40;
	Context : con 16r80;
	Private : con 16rC0;

	# Universal tags
	BOOLEAN : con 1;
	INTEGER : con 2;
	BIT_STRING : con 3;
	OCTET_STRING : con 4;
	NULL : con 5;
	OBJECT_ID: con 6;
	ObjectDescriptor : con 7;
	EXTERNAL : con 8;
	REAL : con 9;
	ENUMERATED : con 10;
	EMBEDDED_PDV : con 11;
	SEQUENCE : con 16;		# also SEQUENCE OF
	SET : con 17;			# also SET  OF
	NumericString : con 18;
	PrintableString : con 19;
	TeletexString : con 20;
	VideotexString : con 21;
	IA5String : con 22;
	UTCTime : con 23;
	GeneralizedTime : con 24;
	GraphicString : con 25;
	VisibleString : con 26;
	GeneralString : con 27;
	UniversalString : con 28;
	BMPString : con 30;

	Elem: adt {
		tag: Tag;
		val: ref Value;

		is_seq: fn(e: self ref Elem) : (int, list of ref Elem);
		is_set: fn(e: self ref Elem) : (int, list of ref Elem);
		is_int: fn(e: self ref Elem) : (int, int);
		is_bigint: fn(e: self ref Elem) : (int, array of byte);
		is_bitstring: fn(e: self ref Elem) : (int, int, array of byte);
		is_octetstring: fn(e: self ref Elem) : (int, array of byte);
		is_oid: fn(e: self ref Elem) : (int, ref Oid);
		is_string: fn(e: self ref Elem) : (int, string);
		is_time: fn(e: self ref Elem) : (int, string);
		tostring: fn(e: self ref Elem) : string;
	};

	Tag: adt {
		class: int;
		num: int;
		constr: int;	# ignored by encode()

		tostring: fn(t: self Tag) : string;
	};

	Value: adt {
		pick {
			Bool or Int =>
				v: int;
			Octets or BigInt or Real or Other =>
				# BigInt: integer too big for limbo int
				# Real: we don't care to decode these
				#    since they are hardly ever used
				bytes: array of byte;
			BitString =>
				# pack into bytes, with perhaps some
				# unused bits in last byte
				unusedbits: int;
				bits: array of byte;
			Null or EOC =>
				dummy: int;
			ObjId =>
				id: ref Oid;
			String =>
				s: string;
			Seq or Set =>
				l: list of ref Elem;
		}

		tostring : fn(v: self ref Value) : string;
	};

	Oid: adt {
		nums: array of int;

		tostring: fn(o: self ref Oid) : string;
	};

	init: fn();
	decode: fn(a: array of byte) : (string, ref Elem);
	decode_seq: fn(a: array of byte) : (string, list of ref Elem);
	decode_value: fn(a: array of byte, kind, constr: int) : (string, ref Value);
	encode: fn(e: ref Elem) : (string, array of byte);
	oid_lookup: fn(o: ref Oid, tab: array of Oid) : int;
	print_elem: fn(e: ref Elem);
};