blob: fd0fa9265da73614d438d6ad8a4ef8d052154829 [file] [log] [blame]
Lev Walkineb97a702004-08-07 06:03:15 +00001
2<!--Converted with LaTeX2HTML 2002-2-1 (1.70)
3original version by: Nikos Drakos, CBLU, University of Leeds
4* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan
5* with significant contributions from:
6 Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
7<HTML>
8<HEAD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00009<TITLE>Using the OpenSource ASN.1 Compiler</TITLE>
10<META NAME="description" CONTENT="Using the OpenSource ASN.1 Compiler">
Lev Walkineb97a702004-08-07 06:03:15 +000011<META NAME="asn1c, ASN.1, free, compiler, BER, DER, PER, XER\">
12
Lev Walkineb97a702004-08-07 06:03:15 +000013
14
15</HEAD>
16
17<BODY >
18
19<P>
20
21<P>
22
23<P>
Lev Walkin28c89eb2004-09-30 22:45:58 +000024<H1 ALIGN="CENTER">Using the OpenSource ASN.1 Compiler</H1><DIV>
Lev Walkineb97a702004-08-07 06:03:15 +000025
Lev Walkin28c89eb2004-09-30 22:45:58 +000026<P ALIGN="CENTER"><STRONG>Lev Walkin &lt;<A HREF=mailto:vlm@lionet.info?Subject=asn1c>vlm@lionet.info</A>&gt;</STRONG></P>
Lev Walkineb97a702004-08-07 06:03:15 +000027</DIV>
Lev Walkin28c89eb2004-09-30 22:45:58 +000028
29<P>
Lev Walkin683f9b72004-09-26 13:41:45 +000030<!-- MATH
31 $Revision$
32 -->
Lev Walkin28c89eb2004-09-30 22:45:58 +000033<FONT COLOR=red><B>Download the <A HREF=asn1c-usage.pdf>PDF</A> version</B></FONT>
Lev Walkin26587ab2004-08-23 15:12:04 +000034
35<P>
Lev Walkineb97a702004-08-07 06:03:15 +000036<BR>
37
38<H2><A NAME="SECTION01000000000000000000">
39Contents</A>
40</H2>
41<!--Table of Contents-->
42
43<UL>
Lev Walkin683f9b72004-09-26 13:41:45 +000044<LI><A NAME="tex2html46"
Lev Walkineb97a702004-08-07 06:03:15 +000045 HREF="asn1c-usage.html#SECTION02000000000000000000">ASN.1 Basics</A>
46<UL>
Lev Walkin683f9b72004-09-26 13:41:45 +000047<LI><A NAME="tex2html47"
Lev Walkineb97a702004-08-07 06:03:15 +000048 HREF="asn1c-usage.html#SECTION02100000000000000000">Abstract Syntax Notation: ASN.1</A>
49<UL>
Lev Walkin683f9b72004-09-26 13:41:45 +000050<LI><A NAME="tex2html48"
Lev Walkineb97a702004-08-07 06:03:15 +000051 HREF="asn1c-usage.html#SECTION02110000000000000000">Some of the ASN.1 Basic Types</A>
52<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000053<LI><A NAME="tex2html49"
Lev Walkin683f9b72004-09-26 13:41:45 +000054 HREF="asn1c-usage.html#SECTION02111000000000000000">The BOOLEAN type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000055<LI><A NAME="tex2html50"
Lev Walkin683f9b72004-09-26 13:41:45 +000056 HREF="asn1c-usage.html#SECTION02112000000000000000">The INTEGER type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000057<LI><A NAME="tex2html51"
Lev Walkin683f9b72004-09-26 13:41:45 +000058 HREF="asn1c-usage.html#SECTION02113000000000000000">The ENUMERATED type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000059<LI><A NAME="tex2html52"
Lev Walkin683f9b72004-09-26 13:41:45 +000060 HREF="asn1c-usage.html#SECTION02114000000000000000">The OCTET STRING type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000061<LI><A NAME="tex2html53"
Lev Walkin683f9b72004-09-26 13:41:45 +000062 HREF="asn1c-usage.html#SECTION02115000000000000000">The OBJECT IDENTIFIER type</A>
63<LI><A NAME="tex2html54"
Lev Walkineb97a702004-08-07 06:03:15 +000064 HREF="asn1c-usage.html#SECTION02116000000000000000">The RELATIVE-OID type</A>
65</UL>
Lev Walkin683f9b72004-09-26 13:41:45 +000066<LI><A NAME="tex2html55"
Lev Walkineb97a702004-08-07 06:03:15 +000067 HREF="asn1c-usage.html#SECTION02120000000000000000">Some of the ASN.1 String Types</A>
68<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000069<LI><A NAME="tex2html56"
Lev Walkin683f9b72004-09-26 13:41:45 +000070 HREF="asn1c-usage.html#SECTION02121000000000000000">The IA5String type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000071<LI><A NAME="tex2html57"
Lev Walkin683f9b72004-09-26 13:41:45 +000072 HREF="asn1c-usage.html#SECTION02122000000000000000">The UTF8String type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000073<LI><A NAME="tex2html58"
Lev Walkin683f9b72004-09-26 13:41:45 +000074 HREF="asn1c-usage.html#SECTION02123000000000000000">The NumericString type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000075<LI><A NAME="tex2html59"
Lev Walkin683f9b72004-09-26 13:41:45 +000076 HREF="asn1c-usage.html#SECTION02124000000000000000">The PrintableString type</A>
77<LI><A NAME="tex2html60"
Lev Walkineb97a702004-08-07 06:03:15 +000078 HREF="asn1c-usage.html#SECTION02125000000000000000">The VisibleString type</A>
79</UL>
Lev Walkin683f9b72004-09-26 13:41:45 +000080<LI><A NAME="tex2html61"
Lev Walkineb97a702004-08-07 06:03:15 +000081 HREF="asn1c-usage.html#SECTION02130000000000000000">ASN.1 Constructed Types</A>
82<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000083<LI><A NAME="tex2html62"
Lev Walkin683f9b72004-09-26 13:41:45 +000084 HREF="asn1c-usage.html#SECTION02131000000000000000">The SEQUENCE type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000085<LI><A NAME="tex2html63"
Lev Walkin683f9b72004-09-26 13:41:45 +000086 HREF="asn1c-usage.html#SECTION02132000000000000000">The SET type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000087<LI><A NAME="tex2html64"
Lev Walkin683f9b72004-09-26 13:41:45 +000088 HREF="asn1c-usage.html#SECTION02133000000000000000">The CHOICE type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000089<LI><A NAME="tex2html65"
Lev Walkin683f9b72004-09-26 13:41:45 +000090 HREF="asn1c-usage.html#SECTION02134000000000000000">The SEQUENCE OF type</A>
91<LI><A NAME="tex2html66"
Lev Walkineb97a702004-08-07 06:03:15 +000092 HREF="asn1c-usage.html#SECTION02135000000000000000">The SET OF type</A>
93</UL>
94</UL>
95</UL><BR>
Lev Walkin683f9b72004-09-26 13:41:45 +000096<LI><A NAME="tex2html67"
Lev Walkineb97a702004-08-07 06:03:15 +000097 HREF="asn1c-usage.html#SECTION03000000000000000000">Using the ASN.1 Compiler</A>
98<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000099<LI><A NAME="tex2html68"
Lev Walkin683f9b72004-09-26 13:41:45 +0000100 HREF="asn1c-usage.html#SECTION03100000000000000000">Introduction to the ASN.1 Compiler</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000101<LI><A NAME="tex2html69"
Lev Walkin683f9b72004-09-26 13:41:45 +0000102 HREF="asn1c-usage.html#SECTION03200000000000000000">Quick start</A>
103<LI><A NAME="tex2html70"
Lev Walkin26587ab2004-08-23 15:12:04 +0000104 HREF="asn1c-usage.html#SECTION03300000000000000000">Using the ASN.1 Compiler</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000105<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +0000106<LI><A NAME="tex2html71"
Lev Walkin683f9b72004-09-26 13:41:45 +0000107 HREF="asn1c-usage.html#SECTION03310000000000000000">Command-line options</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000108<LI><A NAME="tex2html72"
Lev Walkin683f9b72004-09-26 13:41:45 +0000109 HREF="asn1c-usage.html#SECTION03320000000000000000">Recognizing compiler output</A>
110<LI><A NAME="tex2html73"
Lev Walkin28c89eb2004-09-30 22:45:58 +0000111 HREF="asn1c-usage.html#SECTION03330000000000000000">Invoking the ASN.1 helper code from an application</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000112<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +0000113<LI><A NAME="tex2html74"
Lev Walkin683f9b72004-09-26 13:41:45 +0000114 HREF="asn1c-usage.html#SECTION03331000000000000000">Decoding BER</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000115<LI><A NAME="tex2html75"
Lev Walkin683f9b72004-09-26 13:41:45 +0000116 HREF="asn1c-usage.html#SECTION03332000000000000000">Encoding DER</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000117<LI><A NAME="tex2html76"
Lev Walkin683f9b72004-09-26 13:41:45 +0000118 HREF="asn1c-usage.html#SECTION03333000000000000000">Encoding XER</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000119<LI><A NAME="tex2html77"
Lev Walkin683f9b72004-09-26 13:41:45 +0000120 HREF="asn1c-usage.html#SECTION03334000000000000000">Validating the target structure</A>
121<LI><A NAME="tex2html78"
122 HREF="asn1c-usage.html#SECTION03335000000000000000">Printing the target structure</A>
123<LI><A NAME="tex2html79"
124 HREF="asn1c-usage.html#SECTION03336000000000000000">Freeing the target structure</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000125</UL>
126</UL>
127</UL><BR>
Lev Walkin683f9b72004-09-26 13:41:45 +0000128<LI><A NAME="tex2html80"
Lev Walkineb97a702004-08-07 06:03:15 +0000129 HREF="asn1c-usage.html#SECTION04000000000000000000">Bibliography</A>
130</UL>
131<!--End of Table of Contents-->
132
133<P>
134
Lev Walkin26587ab2004-08-23 15:12:04 +0000135<P>
136
Lev Walkineb97a702004-08-07 06:03:15 +0000137<H1><A NAME="SECTION02000000000000000000">
138ASN.1 Basics</A>
139</H1>
140
141<P>
142
143<H1><A NAME="SECTION02100000000000000000">
144Abstract Syntax Notation: ASN.1</A>
145</H1>
146
147<P>
148<I>This chapter defines some basic ASN.1 concepts and describes
149several most widely used types. It is by no means an authoritative
150or complete reference. For more complete ASN.1 description, please
151refer to Olivier Dubuisson's book [<A
152 HREF="asn1c-usage.html#Dub00">Dub00</A>] or the ASN.1 body
153of standards itself [<A
154 HREF="asn1c-usage.html#ITU-T_ASN.1">ITU-T/ASN.1</A>].</I>
155
156<P>
157The Abstract Syntax Notation One is used to formally describe the
158semantics of data transmitted across the network. Two communicating
159parties may have different formats of their native data types (i.e.
160number of bits in the integer type), thus it is important to have
161a way to describe the data in a manner which is independent from the
162particular machine's representation. The ASN.1 specifications is used
163to achieve one or more of the following:
164
165<P>
166
167<UL>
168<LI>The specification expressed in the ASN.1 notation is a formal and
169precise way to communicate the data semantics to human readers;
170</LI>
171<LI>The ASN.1 specifications may be used as input for automatic compilers
172which produce the code for some target language (C, C++, Java, etc)
173to encode and decode the data according to some encoding rules (which
174are also defined by the ASN.1 standard).
175</LI>
176</UL>
177Consider the following example:
178
179<P>
180
Lev Walkin26587ab2004-08-23 15:12:04 +0000181<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000182Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
183&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,
184&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER
185}
Lev Walkineb97a702004-08-07 06:03:15 +0000186</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000187</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000188This ASN.1 specification describes a constructed type, <I>Rectangle</I>,
189containing two integer fields. This specification may tell the reader
190that there is this kind of data structure and that some entity may
191be prepared to send or receive it. The question on <I>how</I> that
192entity is going to send or receive the <I>encoded data</I> is outside
193the scope of ASN.1. For example, this data structure may be encoded
194according to some encoding rules and sent to the destination using
195the TCP protocol. The ASN.1 specifies several ways of encoding (or
196''serializing'', or ''marshaling'') the data: BER, CER, DER and
197XER, some of them which will be described later.
198
199<P>
200The complete specification must be wrapped in a module, which looks
201like this:
202
203<P>
204
Lev Walkin26587ab2004-08-23 15:12:04 +0000205<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000206UsageExampleModule1
207&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;iso&nbsp;org(3)&nbsp;dod(6)&nbsp;internet(1)&nbsp;private(4)
208&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enterprise(1)&nbsp;spelio(9363)&nbsp;software(1)
209&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn1c(5)&nbsp;docs(2)&nbsp;usage(1)&nbsp;1&nbsp;}&nbsp;
Lev Walkin26587ab2004-08-23 15:12:04 +0000210&nbsp;&nbsp;&nbsp;&nbsp;DEFINITIONS&nbsp;AUTOMATIC&nbsp;TAGS&nbsp;::=
Lev Walkineb97a702004-08-07 06:03:15 +0000211BEGIN
212&nbsp;
213--&nbsp;This&nbsp;is&nbsp;a&nbsp;comment&nbsp;which&nbsp;describes&nbsp;nothing.
214Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
215&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;Height&nbsp;of&nbsp;the&nbsp;rectangle
Lev Walkin26587ab2004-08-23 15:12:04 +0000216&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;Width&nbsp;of&nbsp;the&nbsp;rectangle
Lev Walkineb97a702004-08-07 06:03:15 +0000217}
218&nbsp;
219END
Lev Walkineb97a702004-08-07 06:03:15 +0000220</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000221</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000222The module header consists of module name (UsageExampleModule1), the
Lev Walkin26587ab2004-08-23 15:12:04 +0000223module object identifier ({...}), a keyword ''DEFINITIONS'', a
224set of module flags (AUTOMATIC TAGS) and ''::= BEGIN''. The module
225ends with an ''END'' statement.
Lev Walkineb97a702004-08-07 06:03:15 +0000226
227<P>
228
229<H1><A NAME="SECTION02110000000000000000">
230Some of the ASN.1 Basic Types</A>
231</H1>
232
233<P>
234
235<H2><A NAME="SECTION02111000000000000000">
236The BOOLEAN type</A>
237</H2>
238
239<P>
240The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF
241or a similar kind of two-way choice.
242
243<P>
244
245<H2><A NAME="SECTION02112000000000000000">
246The INTEGER type</A>
247</H2>
248
249<P>
250The INTEGER type is a signed natural number type without any restrictions
251on its size. If the automatic checking on INTEGER value bounds are
252necessary, the subtype constraints must be used.
253
254<P>
255
Lev Walkin26587ab2004-08-23 15:12:04 +0000256<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000257SimpleInteger&nbsp;::=&nbsp;INTEGER
Lev Walkin26587ab2004-08-23 15:12:04 +0000258&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000259--&nbsp;An&nbsp;integer&nbsp;with&nbsp;a&nbsp;very&nbsp;limited&nbsp;range
Lev Walkinc500b3e2004-09-29 13:37:15 +0000260SmallPositiveInt&nbsp;::=&nbsp;INTEGER&nbsp;(0..127)
Lev Walkin26587ab2004-08-23 15:12:04 +0000261&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000262--&nbsp;Integer,&nbsp;negative
263NegativeInt&nbsp;::=&nbsp;INTEGER&nbsp;(MIN..0)
Lev Walkineb97a702004-08-07 06:03:15 +0000264</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000265</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000266
267<P>
268
269<H2><A NAME="SECTION02113000000000000000">
270The ENUMERATED type</A>
271</H2>
272
273<P>
274The ENUMERATED type is semantically equivalent to the INTEGER type
275with some integer values explicitly named.
276
277<P>
278
Lev Walkin26587ab2004-08-23 15:12:04 +0000279<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000280FruitId&nbsp;::=&nbsp;ENUMERATED&nbsp;{&nbsp;apple(1),&nbsp;orange(2)&nbsp;}
Lev Walkin26587ab2004-08-23 15:12:04 +0000281&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000282--&nbsp;The&nbsp;numbers&nbsp;in&nbsp;braces&nbsp;are&nbsp;optional,
Lev Walkin26587ab2004-08-23 15:12:04 +0000283--&nbsp;the&nbsp;enumeration&nbsp;can&nbsp;be&nbsp;performed
Lev Walkineb97a702004-08-07 06:03:15 +0000284--&nbsp;automatically&nbsp;by&nbsp;the&nbsp;compiler
285ComputerOSType&nbsp;::=&nbsp;ENUMERATED&nbsp;{
Lev Walkin28c89eb2004-09-30 22:45:58 +0000286&nbsp;&nbsp;&nbsp;&nbsp;FreeBSD,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;acquires&nbsp;value&nbsp;0
287&nbsp;&nbsp;&nbsp;&nbsp;Windows,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;acquires&nbsp;value&nbsp;1
288&nbsp;&nbsp;&nbsp;&nbsp;Solaris(5),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;remains&nbsp;5
289&nbsp;&nbsp;&nbsp;&nbsp;Linux,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;becomes&nbsp;6
290&nbsp;&nbsp;&nbsp;&nbsp;MacOS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;becomes&nbsp;7
Lev Walkineb97a702004-08-07 06:03:15 +0000291}
Lev Walkineb97a702004-08-07 06:03:15 +0000292</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000293</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000294
295<P>
296
297<H2><A NAME="SECTION02114000000000000000">
298The OCTET STRING type</A>
299</H2>
300
301<P>
302This type models the sequence of 8-bit bytes. This may be used to
303transmit some opaque data or data serialized by other types of encoders
304(i.e. video file, photo picture, etc).
305
306<P>
307
308<H2><A NAME="SECTION02115000000000000000">
309The OBJECT IDENTIFIER type</A>
310</H2>
311
312<P>
313The OBJECT IDENTIFIER is used to represent the unique identifier of
314any object, starting from the very root of the registration tree.
315If your organization needs to uniquely identify something (a router,
316a room, a person, a standard, or whatever), you are encouraged to
Lev Walkin28c89eb2004-09-30 22:45:58 +0000317get your own identification subtree at <A HREF=http://www.iana.org/protocols/forms.htm>http://www.iana.org/protocols/forms.htm</A>.
Lev Walkineb97a702004-08-07 06:03:15 +0000318
319<P>
320For example, the very first ASN.1 module in this document has the
321following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
322
323<P>
324
Lev Walkin26587ab2004-08-23 15:12:04 +0000325<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000326ExampleOID&nbsp;::=&nbsp;OBJECT&nbsp;IDENTIFIER
Lev Walkin26587ab2004-08-23 15:12:04 +0000327&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000328usageExampleModule1-oid&nbsp;ExampleOID
329&nbsp;&nbsp;::=&nbsp;{&nbsp;1&nbsp;3&nbsp;6&nbsp;1&nbsp;4&nbsp;1&nbsp;9363&nbsp;1&nbsp;5&nbsp;2&nbsp;1&nbsp;1&nbsp;}
Lev Walkin26587ab2004-08-23 15:12:04 +0000330&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000331--&nbsp;An&nbsp;identifier&nbsp;of&nbsp;the&nbsp;Internet.
332internet-id&nbsp;OBJECT&nbsp;IDENTIFIER
333&nbsp;&nbsp;::=&nbsp;{&nbsp;iso(1)&nbsp;identified-organization(3)
334&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dod(6)&nbsp;internet(1)&nbsp;}
Lev Walkineb97a702004-08-07 06:03:15 +0000335</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000336</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000337As you see, names are optional.
338
339<P>
340
341<H2><A NAME="SECTION02116000000000000000">
342The RELATIVE-OID type</A>
343</H2>
344
345<P>
346The RELATIVE-OID type has the semantics of a subtree of an OBJECT
347IDENTIFIER. There may be no need to repeat the whole sequence of numbers
348from the root of the registration tree where the only thing of interest
349is some of the tree's subsequence.
350
351<P>
352
Lev Walkin26587ab2004-08-23 15:12:04 +0000353<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000354this-document&nbsp;RELATIVE-OID&nbsp;::=&nbsp;{&nbsp;docs(2)&nbsp;usage(1)&nbsp;}
Lev Walkin26587ab2004-08-23 15:12:04 +0000355&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000356this-example&nbsp;RELATIVE-OID&nbsp;::=&nbsp;{
357&nbsp;&nbsp;&nbsp;&nbsp;this-document&nbsp;assorted-examples(0)&nbsp;this-example(1)&nbsp;}
Lev Walkineb97a702004-08-07 06:03:15 +0000358</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000359</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000360
361<P>
362
363<H1><A NAME="SECTION02120000000000000000">
364Some of the ASN.1 String Types</A>
365</H1>
366
367<P>
368
369<H2><A NAME="SECTION02121000000000000000">
370The IA5String type</A>
371</H2>
372
373<P>
374This is essentially the ASCII, with 128 character codes available
Lev Walkin26587ab2004-08-23 15:12:04 +0000375(7 lower bits of an 8-bit byte).
Lev Walkineb97a702004-08-07 06:03:15 +0000376
377<P>
378
379<H2><A NAME="SECTION02122000000000000000">
380The UTF8String type</A>
381</H2>
382
383<P>
384This is the character string which encodes the full Unicode range
385(4 bytes) using multibyte character sequences.
386
387<P>
388
389<H2><A NAME="SECTION02123000000000000000">
390The NumericString type</A>
391</H2>
392
393<P>
394This type represents the character string with the alphabet consisting
395of numbers (''0'' to ''9'') and a space.
396
397<P>
398
399<H2><A NAME="SECTION02124000000000000000">
400The PrintableString type</A>
401</H2>
402
403<P>
404The character string with the following alphabet: space, ''<B>'</B>''
405(single quote), ''<B>(</B>'', ''<B>)</B>'', ''<B>+</B>'',
Lev Walkinc500b3e2004-09-29 13:37:15 +0000406''<B>,</B>'' (comma), ''<B>-</B>'', ''<B>.</B>'', ''<B>/</B>'',
Lev Walkineb97a702004-08-07 06:03:15 +0000407digits (''0'' to ''9''), ''<B>:</B>'', ''<B>=</B>'', ''<B>?</B>'',
408upper-case and lower-case letters (''A'' to ''Z'' and ''a''
Lev Walkinc500b3e2004-09-29 13:37:15 +0000409to ''z'').
Lev Walkineb97a702004-08-07 06:03:15 +0000410
411<P>
412
413<H2><A NAME="SECTION02125000000000000000">
414The VisibleString type</A>
415</H2>
416
417<P>
418The character string with the alphabet which is more or less a subset
Lev Walkinc500b3e2004-09-29 13:37:15 +0000419of ASCII between the space and the ''<B>~</B>''
420symbol (tilde).
421
422<P>
Lev Walkin26587ab2004-08-23 15:12:04 +0000423Alternatively, the alphabet may be described as the PrintableString
424alphabet presented earlier, plus the following characters: ''<B>!</B>'',
425''<B>''</B>'', ''<B>#</B>'', ''<B>$</B>'', ''<B>%</B>'',
426''<B>&amp;</B>'', ''<B>*</B>'', ''<B>;</B>'', ''<B>&lt;</B>'',
427''<B>&gt;</B>'', ''<B>[</B>'', ''<B>&#92;</B>'',
428''<B>]</B>'', ''<B>&#94;</B>'', ''<B>_</B>'',
429''<B>`</B>'' (single left quote), ''<B>{</B>'', ''<B>|</B>'',
430''<B>}</B>'', ''<B>~</B>''.
Lev Walkineb97a702004-08-07 06:03:15 +0000431
432<P>
433
434<H1><A NAME="SECTION02130000000000000000">
435ASN.1 Constructed Types</A>
436</H1>
437
438<P>
439
440<H2><A NAME="SECTION02131000000000000000">
441The SEQUENCE type</A>
442</H2>
443
444<P>
445This is an ordered collection of other simple or constructed types.
446The SEQUENCE constructed type resembles the C ''struct'' statement.
447
448<P>
449
Lev Walkin26587ab2004-08-23 15:12:04 +0000450<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000451Address&nbsp;::=&nbsp;SEQUENCE&nbsp;{
452&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;The&nbsp;apartment&nbsp;number&nbsp;may&nbsp;be&nbsp;omitted
Lev Walkin28c89eb2004-09-30 22:45:58 +0000453&nbsp;&nbsp;&nbsp;&nbsp;apartmentNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NumericString&nbsp;OPTIONAL,
Lev Walkineb97a702004-08-07 06:03:15 +0000454&nbsp;&nbsp;&nbsp;&nbsp;streetName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
455&nbsp;&nbsp;&nbsp;&nbsp;cityName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
456&nbsp;&nbsp;&nbsp;&nbsp;stateName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
457&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;This&nbsp;one&nbsp;may&nbsp;be&nbsp;omitted&nbsp;too
458&nbsp;&nbsp;&nbsp;&nbsp;zipNo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NumericString&nbsp;OPTIONAL
459}
Lev Walkineb97a702004-08-07 06:03:15 +0000460</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000461</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000462
463<P>
464
465<H2><A NAME="SECTION02132000000000000000">
466The SET type</A>
467</H2>
468
469<P>
470This is a collection of other simple or constructed types. Ordering
471is not important. The data may arrive in the order which is different
472from the order of specification. Data is encoded in the order not
473necessarily corresponding to the order of specification.
474
475<P>
476
477<H2><A NAME="SECTION02133000000000000000">
478The CHOICE type</A>
479</H2>
480
481<P>
482This type is just a choice between the subtypes specified in it. The
483CHOICE type contains at most one of the subtypes specified, and it
484is always implicitly known which choice is being decoded or encoded.
485This one resembles the C ''union'' statement.
486
487<P>
488The following type defines a response code, which may be either an
489integer code or a boolean ''true''/''false'' code.
490
491<P>
492
Lev Walkin26587ab2004-08-23 15:12:04 +0000493<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000494ResponseCode&nbsp;::=&nbsp;CHOICE&nbsp;{
495&nbsp;&nbsp;&nbsp;&nbsp;intCode&nbsp;&nbsp;&nbsp;&nbsp;INTEGER,
496&nbsp;&nbsp;&nbsp;&nbsp;boolCode&nbsp;&nbsp;&nbsp;BOOLEAN
497}
Lev Walkineb97a702004-08-07 06:03:15 +0000498</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000499</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000500
501<P>
502
503<H2><A NAME="SECTION02134000000000000000">
504The SEQUENCE OF type</A>
505</H2>
506
507<P>
508This one is the list (array) of simple or constructed types:
509
510<P>
511
Lev Walkin26587ab2004-08-23 15:12:04 +0000512<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000513--&nbsp;Example&nbsp;1
514ManyIntegers&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;INTEGER
Lev Walkin26587ab2004-08-23 15:12:04 +0000515&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000516--&nbsp;Example&nbsp;2
517ManyRectangles&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;Rectangle
Lev Walkin26587ab2004-08-23 15:12:04 +0000518&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000519--&nbsp;More&nbsp;complex&nbsp;example:
520--&nbsp;an&nbsp;array&nbsp;of&nbsp;structures&nbsp;defined&nbsp;in&nbsp;place.
521ManyCircles&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;SEQUENCE&nbsp;{
522&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;radius&nbsp;INTEGER
523&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
Lev Walkineb97a702004-08-07 06:03:15 +0000524</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000525</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000526
527<P>
528
529<H2><A NAME="SECTION02135000000000000000">
530The SET OF type</A>
531</H2>
532
533<P>
534The SET OF type models the bag of structures. It resembles the SEQUENCE
535OF type, but the order is not important: i.e. the elements may arrive
536in the order which is not necessarily the same as the in-memory order
537on the remote machines.
538
539<P>
540
Lev Walkin26587ab2004-08-23 15:12:04 +0000541<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000542--&nbsp;A&nbsp;set&nbsp;of&nbsp;structures&nbsp;defined&nbsp;elsewhere
543SetOfApples&nbsp;::&nbsp;SET&nbsp;OF&nbsp;Apple
Lev Walkin26587ab2004-08-23 15:12:04 +0000544&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000545--&nbsp;Set&nbsp;of&nbsp;integers&nbsp;encoding&nbsp;the&nbsp;kind&nbsp;of&nbsp;a&nbsp;fruit
546FruitBag&nbsp;::=&nbsp;SET&nbsp;OF&nbsp;ENUMERATED&nbsp;{&nbsp;apple,&nbsp;orange&nbsp;}
Lev Walkineb97a702004-08-07 06:03:15 +0000547</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000548</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000549
550<P>
551
552<H1><A NAME="SECTION03000000000000000000">
553Using the ASN.1 Compiler</A>
554</H1>
555
556<P>
557
558<H1><A NAME="SECTION03100000000000000000">
559Introduction to the ASN.1 Compiler</A>
560</H1>
561
562<P>
563The purpose of the ASN.1 compiler, of which this document is part,
564is to convert the ASN.1 specifications to some other target language
565(currently, only C is supported<A NAME="tex2html1"
Lev Walkin28c89eb2004-09-30 22:45:58 +0000566 HREF="#foot159"><SUP>2.1</SUP></A>). The compiler reads the specification and emits a series of target
Lev Walkineb97a702004-08-07 06:03:15 +0000567language structures and surrounding maintenance code. For example,
568the C structure which may be created by compiler to represent the
569simple <I>Rectangle</I> specification defined earlier in this document,
570may look like this<A NAME="tex2html2"
Lev Walkin28c89eb2004-09-30 22:45:58 +0000571 HREF="#foot419"><SUP>2.2</SUP></A>:
Lev Walkineb97a702004-08-07 06:03:15 +0000572
573<P>
574
Lev Walkin26587ab2004-08-23 15:12:04 +0000575<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000576typedef&nbsp;struct&nbsp;Rectangle_s&nbsp;{
577&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;height;
578&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;width;
579}&nbsp;Rectangle_t;
Lev Walkineb97a702004-08-07 06:03:15 +0000580</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000581</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000582This would not be of much value for such a simple specification, so
583the compiler goes further and actually produces the code which fills
Lev Walkin26587ab2004-08-23 15:12:04 +0000584in this structure by parsing the opaque binary<A NAME="tex2html3"
Lev Walkin28c89eb2004-09-30 22:45:58 +0000585 HREF="#foot166"><SUP>2.3</SUP></A> data provided in some buffer. It also produces the code that takes
Lev Walkineb97a702004-08-07 06:03:15 +0000586this structure as an argument and performs structure serialization
587by emitting a series of bytes.
588
589<P>
590
Lev Walkin26587ab2004-08-23 15:12:04 +0000591<H1><A NAME="SECTION03200000000000000000">
Lev Walkineb97a702004-08-07 06:03:15 +0000592Quick start</A>
593</H1>
594
595<P>
Lev Walkin26587ab2004-08-23 15:12:04 +0000596After building and installing the compiler, the <I>asn1c</I><A NAME="tex2html4"
Lev Walkin28c89eb2004-09-30 22:45:58 +0000597 HREF="#foot420"><SUP>3.1</SUP></A> command may be used to compile the ASN.1 specification<A NAME="tex2html5"
598 HREF="#foot421"><SUP>3.2</SUP></A>:
Lev Walkineb97a702004-08-07 06:03:15 +0000599
600<P>
601
Lev Walkin26587ab2004-08-23 15:12:04 +0000602<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000603asn1c&nbsp;<I>&lt;spec.asn1&gt;</I>
Lev Walkineb97a702004-08-07 06:03:15 +0000604</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000605</BLOCKQUOTE>
606If several specifications contain interdependencies, all of the files
607must be specified altogether:
Lev Walkineb97a702004-08-07 06:03:15 +0000608
609<P>
610
Lev Walkin26587ab2004-08-23 15:12:04 +0000611<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000612asn1c&nbsp;<I>&lt;spec1.asn1&gt;&nbsp;&lt;spec2.asn1&gt;&nbsp;...</I>
Lev Walkineb97a702004-08-07 06:03:15 +0000613</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000614</BLOCKQUOTE>
615The compiler <B>-E</B> and <B>-EF</B> options are used for testing
616the parser and the semantic fixer, respectively. These options will
617instruct the compiler to dump out the parsed (and fixed, if <B>-F</B>
618is involved) ASN.1 specification as it was &#34;understood&#34;
619by the compiler. It might be useful to check whether a particular
620syntactic construction is properly supported by the compiler.
Lev Walkineb97a702004-08-07 06:03:15 +0000621
622<P>
623
Lev Walkin26587ab2004-08-23 15:12:04 +0000624<BLOCKQUOTE><PRE>
625asn1c&nbsp;<B>-EF</B>&nbsp;<I>&lt;spec-to-test.asn1&gt;</I>
Lev Walkineb97a702004-08-07 06:03:15 +0000626</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000627</BLOCKQUOTE>
628The <B>-P</B> option is used to dump the compiled output on the
629screen instead of creating a bunch of .c and .h files on disk in the
630current directory. You would probably want to start with <B>-P</B>
631option instead of creating a mess in your current directory. Another
632option, <B>-R</B>, asks compiler to only generate the files which
633need to be generated, and supress linking in the numerous support
634files.
635
636<P>
637Print the compiled output instead of creating multiple source files:
Lev Walkineb97a702004-08-07 06:03:15 +0000638
639<P>
640
Lev Walkin26587ab2004-08-23 15:12:04 +0000641<BLOCKQUOTE><PRE>
642asn1c&nbsp;<B>-P</B>&nbsp;<I>&lt;spec-to-compile-and-print.asn1&gt;</I>
643</PRE>
644</BLOCKQUOTE>
645
646<P>
647
648<H1><A NAME="SECTION03300000000000000000">
649Using the ASN.1 Compiler</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000650</H1>
651
652<P>
653
Lev Walkin26587ab2004-08-23 15:12:04 +0000654<H1><A NAME="SECTION03310000000000000000">
655Command-line options</A>
656</H1>
657
658<P>
659The <A HREF=#Table1>Table 1</A> summarizes various options affecting
660the compiler's behavior.
661
662<P>
663<BR><P></P>
Lev Walkin28c89eb2004-09-30 22:45:58 +0000664<DIV ALIGN="CENTER"><A NAME="424"></A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000665<TABLE>
666<CAPTION><STRONG><A NAME=Table1>Table 1:</A></STRONG>
667The list of asn1c command line options</CAPTION>
668<TR><TD><TABLE COLS=2 BORDER FRAME=BOX RULES=GROUPS>
669<COLGROUP><COL ALIGN=LEFT><COLGROUP><COL ALIGN=JUSTIFY WIDTH="3in">
670<TBODY>
671<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>
672<B>Overall Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
673<B>Description</B></TD></TR>
674</TBODY><TBODY>
675<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-E</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
676<FONT SIZE="-1">Stop after the parsing stage and print the reconstructed ASN.1
677specification code to the standard output.</FONT></TD></TR>
678</TBODY><TBODY>
679<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-F</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
680<FONT SIZE="-1">Used together with -E, instructs the compiler to stop after
681the ASN.1 syntax tree fixing stage and dump the reconstructed ASN.1
682specification to the standard output.</FONT></TD></TR>
683</TBODY><TBODY>
684<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-P</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
685<FONT SIZE="-1">Dump the compiled output to the standard output instead of
686cre- ating the target language files on disk.</FONT></TD></TR>
687</TBODY><TBODY>
688<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-R</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
689<FONT SIZE="-1">Restrict the compiler to generate only the ASN.1 tables, omit-
690ting the usual support code.</FONT></TD></TR>
691</TBODY><TBODY>
692<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-S <I>&lt;directory&gt;</I></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
693<FONT SIZE="-1">Use the specified directory with ASN.1 skeleton files.</FONT></TD></TR>
694</TBODY><TBODY>
Lev Walkinf7484512004-10-13 09:13:56 +0000695<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-X</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
696<FONT SIZE="-1">Generate the XML DTD schema for the specified ASN.1 modules.</FONT></TD></TR>
697</TBODY><TBODY>
Lev Walkin26587ab2004-08-23 15:12:04 +0000698<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Warning Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
699<B>Description</B></TD></TR>
700</TBODY><TBODY>
701<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Werror</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
702<FONT SIZE="-1">Treat warnings as errors; abort if any warning is produced.</FONT></TD></TR>
703</TBODY><TBODY>
704<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-lexer</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
705<FONT SIZE="-1">Enable lexer debugging during the ASN.1 parsing stage.</FONT></TD></TR>
706</TBODY><TBODY>
707<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-fixer</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
708 <FONT SIZE="-1">Enable ASN.1 syntax tree fixer debugging during the
709 fixing stage.</FONT></TD></TR>
710</TBODY><TBODY>
711<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-compiler</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
712<FONT SIZE="-1">Enable debugging during the actual compile time.</FONT></TD></TR>
713</TBODY><TBODY>
714<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Language Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
715<B>Description</B></TD></TR>
716</TBODY><TBODY>
Lev Walkin71a8aaf2004-09-08 03:10:54 +0000717<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fall-defs-global</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkinc500b3e2004-09-29 13:37:15 +0000718Normally the compiler hides the definitions (asn_DEF_xxx) of the
Lev Walkin71a8aaf2004-09-08 03:10:54 +0000719inner structure elements (members of SEQUENCE, SET and other types).
720This option makes all such definitions global. Enabling this option
Lev Walkinc500b3e2004-09-29 13:37:15 +0000721may pollute the namespace by making lots of asn_DEF_xxx structures
Lev Walkin71a8aaf2004-09-08 03:10:54 +0000722globally visible, but will allow you to manipulate (encode and decode)
723the individual members of any complex ASN.1 structure.</TD></TR>
724</TBODY><TBODY>
Lev Walkin26587ab2004-08-23 15:12:04 +0000725<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fbless-SIZE</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
726<FONT SIZE="-1">Allow SIZE() constraint for INTEGER, ENUMERATED, and other
727types for which this constraint is normally prohibited by the standard.
728This is a violation of an ASN.1 standard and compiler may fail to
729produce the meaningful code.</FONT></TD></TR>
730</TBODY><TBODY>
Lev Walkin6faa68e2004-09-17 08:35:02 +0000731<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fnative-types</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
732<FONT SIZE="-1">Use the native machine's data types (int, double) whenever
Lev Walkin28c89eb2004-09-30 22:45:58 +0000733possible, instead of the compound INTEGER_t, ENUMERATED_t and REAL_t
734types. </FONT></TD></TR>
Lev Walkin26587ab2004-08-23 15:12:04 +0000735</TBODY><TBODY>
Lev Walkin683f9b72004-09-26 13:41:45 +0000736<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fno-constraints</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
737Do not generate ASN.1 subtype constraint checking code. This may make
738a shorter executable.</TD></TR>
739</TBODY><TBODY>
Lev Walkin26587ab2004-08-23 15:12:04 +0000740<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-funnamed-unions</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
741<FONT SIZE="-1">Enable unnamed unions in the definitions of target language's
742structures.</FONT></TD></TR>
743</TBODY><TBODY>
744<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-ftypes88</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin28c89eb2004-09-30 22:45:58 +0000745<FONT SIZE="-1">Pretend to support only ASN.1:1988 embedded types. Certain
746reserved words, such as UniversalString and BMPString, become ordinary
747type references and may be redefined by the specification.</FONT></TD></TR>
Lev Walkin26587ab2004-08-23 15:12:04 +0000748</TBODY><TBODY>
749<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Output Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
750<B>Description</B></TD></TR>
Lev Walkin28c89eb2004-09-30 22:45:58 +0000751<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>
752
753-print-constraints</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000754<FONT SIZE="-1">When -EF are also specified, this option forces the compiler
755to explain its internal understanding of subtype constraints.</FONT></TD></TR>
756</TBODY><TBODY>
757<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-print-lines</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
758<FONT SIZE="-1">Generate &#34;- #line&#34; comments in -E output.</FONT></TD></TR>
759</TBODY>
760</TABLE>
761
762<P>
763</TD></TR>
764</TABLE>
765</DIV><P></P><BR>
766
767<P>
768
769<H1><A NAME="SECTION03320000000000000000">
Lev Walkineb97a702004-08-07 06:03:15 +0000770Recognizing compiler output</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000771</H1>
Lev Walkineb97a702004-08-07 06:03:15 +0000772
773<P>
774After compiling, the following entities will be created in your current
775directory:
776
777<P>
778
779<UL>
780<LI>A set of .c and .h files, generally a single pair for each type defined
781in the ASN.1 specifications. These files will be named similarly to
782the ASN.1 types (<I>Rectangle.c</I> and <I>Rectangle.h</I> for the
783specification defined in the beginning of this document).
784</LI>
785<LI>A set of helper .c and .h files which contain generic encoders, decoders
Lev Walkin26587ab2004-08-23 15:12:04 +0000786and other useful routines. There will be quite a few of them, some
787of them even are not always necessary, but the overall amount of code
788after compiling will be rather small anyway.
Lev Walkineb97a702004-08-07 06:03:15 +0000789</LI>
790</UL>
791It is your responsibility to create .c file with the <I>int main()</I>
792routine and the Makefile (if needed). Compiler helps you with the
793latter by creating the Makefile.am.sample, containing the skeleton
794definition for the automake, should you want to use autotools.
795
796<P>
797In other words, after compiling the Rectangle module, you have the
798following set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h,
799<B>...</B> }, where <B>''...''</B> stands for the
800set of additional ''helper'' files created by the compiler. If you
801add the simple file with the <I>int main()</I> routine, it would even
802be possible to compile everything with the single instruction:
803
804<P>
805
Lev Walkin26587ab2004-08-23 15:12:04 +0000806<BLOCKQUOTE><PRE>
807cc&nbsp;-o&nbsp;rectangle&nbsp;*.c&nbsp;&nbsp;&nbsp;#&nbsp;It&nbsp;could&nbsp;be&nbsp;<I>that</I>&nbsp;simple<A NAME="tex2html7"
Lev Walkin28c89eb2004-09-30 22:45:58 +0000808 HREF="#foot427"><SUP>4.1</SUP></A>
Lev Walkineb97a702004-08-07 06:03:15 +0000809</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000810</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000811
812<P>
813
Lev Walkin26587ab2004-08-23 15:12:04 +0000814<H1><A NAME="SECTION03330000000000000000">
Lev Walkin28c89eb2004-09-30 22:45:58 +0000815Invoking the ASN.1 helper code from an application</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000816</H1>
Lev Walkineb97a702004-08-07 06:03:15 +0000817
818<P>
Lev Walkin26587ab2004-08-23 15:12:04 +0000819First of all, you should to include one or more header files into
820your application. For our Rectangle module, including the Rectangle.h
Lev Walkineb97a702004-08-07 06:03:15 +0000821file is enough:
822
823<P>
824
Lev Walkin26587ab2004-08-23 15:12:04 +0000825<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000826#include&nbsp;&lt;Rectangle.h&gt;
Lev Walkineb97a702004-08-07 06:03:15 +0000827</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000828</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000829The header files defines the C structure corresponding to the ASN.1
830definition of a rectangle and the declaration of the ASN.1 type descriptor,
831which is used as an argument to most of the functions provided by
832the ASN.1 module. For example, here is the code which frees the Rectangle_t
833structure:
834
835<P>
836
Lev Walkin26587ab2004-08-23 15:12:04 +0000837<BLOCKQUOTE><PRE>
838Rectangle_t&nbsp;*rect&nbsp;=&nbsp;...;
839&nbsp;
Lev Walkinc500b3e2004-09-29 13:37:15 +0000840asn_DEF_Rectangle-&gt;free_struct(&amp;asn_DEF_Rectangle,
Lev Walkineb97a702004-08-07 06:03:15 +0000841&nbsp;&nbsp;&nbsp;&nbsp;rect,&nbsp;0);
Lev Walkineb97a702004-08-07 06:03:15 +0000842</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000843</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000844This code defines a <I>rect</I> pointer which points to the Rectangle_t
845structure which needs to be freed. The second line invokes the generic
846free_struct routine created specifically for this Rectangle_t structure.
Lev Walkinc500b3e2004-09-29 13:37:15 +0000847The <I>asn_DEF_Rectangle</I> is the type descriptor, which holds
Lev Walkineb97a702004-08-07 06:03:15 +0000848a collection of generic routines to deal with the Rectangle_t structure.
849
850<P>
851There are several generic functions available:
852
853<P>
854<DL>
Lev Walkineb97a702004-08-07 06:03:15 +0000855<DT><STRONG>ber_decoder</STRONG></DT>
Lev Walkin26587ab2004-08-23 15:12:04 +0000856<DD>This is the generic <I>restartable</I><A NAME="tex2html8"
Lev Walkin28c89eb2004-09-30 22:45:58 +0000857 HREF="#foot253"><SUP>4.2</SUP></A> BER decoder (Basic Encoding Rules). This decoder would create
Lev Walkineb97a702004-08-07 06:03:15 +0000858and/or fill the target structure for you. Please refer to Section
859[<A HREF="#sub:Decoding-BER">Decoding-BER</A>].
860</DD>
861<DT><STRONG>der_encoder</STRONG></DT>
862<DD>This is the generic DER encoder (Distinguished Encoding
Lev Walkin683f9b72004-09-26 13:41:45 +0000863Rules). This encoder will take the target structure and encode it
Lev Walkineb97a702004-08-07 06:03:15 +0000864into a series of bytes. Please refer to Section [<A HREF="#sub:Encoding-DER">Encoding-DER</A>].
865</DD>
Lev Walkin683f9b72004-09-26 13:41:45 +0000866<DT><STRONG>xer_encoder</STRONG></DT>
867<DD>This is the generic XER encoder (XML Encoding Rules).
868This encoder will take the target structure and represent it as an
869XML (text) document. Please refer to Section [<A HREF="#sub:Encoding-XER">Encoding-XER</A>].
870</DD>
871<DT><STRONG>check_constraints</STRONG></DT>
872<DD>Check that the contents of the target structure
873are semantically valid and constrained to appropriate implicit or
874explicit subtype constraints. Please refer to Section sub:Validating-the-target.
875</DD>
Lev Walkineb97a702004-08-07 06:03:15 +0000876<DT><STRONG>print_struct</STRONG></DT>
877<DD>This function convert the contents of the passed target
878structure into human readable form. This form is not formal and cannot
879be converted back into the structure, but it may turn out to be useful
880for debugging or quick-n-dirty printing. Please refer to Section [<A HREF="#sub:Printing-the-target">Printing-the-target</A>].
881</DD>
882<DT><STRONG>free_struct</STRONG></DT>
883<DD>This is a generic disposal which frees the target structure.
884Please refer to Section [<A HREF="#sub:Freeing-the-target">Freeing-the-target</A>].
885</DD>
886</DL>
Lev Walkin683f9b72004-09-26 13:41:45 +0000887check_constraints Check that the contents of the target structure
888are semantically valid and constrained to appropriate implicit or
889explicit subtype constraints. Please refer to Section sub:Validating-the-target.
890
891<P>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000892Each of the above function takes the type descriptor (<I>asn_DEF_...</I>)
Lev Walkineb97a702004-08-07 06:03:15 +0000893and the target structure (<I>rect</I>, in the above example). The
894target structure is typically created by the generic BER decoder or
895by the application itself.
896
897<P>
898Here is how the buffer can be deserialized into the structure:
899
900<P>
901
Lev Walkin26587ab2004-08-23 15:12:04 +0000902<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000903Rectangle_t&nbsp;*
Lev Walkin26587ab2004-08-23 15:12:04 +0000904simple_deserializer(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;buf_size)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +0000905&nbsp;&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;*rect&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Note&nbsp;this&nbsp;0!&nbsp;*/
Lev Walkina646ccb2004-10-20 15:48:55 +0000906&nbsp;&nbsp;&nbsp;&nbsp;asn_dec_rval_t&nbsp;rval;
Lev Walkineb97a702004-08-07 06:03:15 +0000907&nbsp;
Lev Walkinc500b3e2004-09-29 13:37:15 +0000908&nbsp;&nbsp;&nbsp;&nbsp;rval&nbsp;=&nbsp;asn_DEF_Rectangle-&gt;ber_decoder(0,
909&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,
Lev Walkineb97a702004-08-07 06:03:15 +0000910&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void&nbsp;**)&amp;rect,
911&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size,
912&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0);
913&nbsp;
914&nbsp;&nbsp;&nbsp;&nbsp;if(rval<B>.code</B>&nbsp;==&nbsp;RC_OK)&nbsp;{
915&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;rect;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Decoding&nbsp;succeeded&nbsp;*/
916&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
Lev Walkin26587ab2004-08-23 15:12:04 +0000917&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Free&nbsp;partially&nbsp;decoded&nbsp;rect&nbsp;*/
Lev Walkinc500b3e2004-09-29 13:37:15 +0000918&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn_DEF_Rectangle-&gt;free_struct(
919&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect,&nbsp;0);
Lev Walkineb97a702004-08-07 06:03:15 +0000920&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;
921&nbsp;&nbsp;&nbsp;&nbsp;}
922}
Lev Walkineb97a702004-08-07 06:03:15 +0000923</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000924</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000925The code above defines a function, <I>simple_deserializer</I>, which
926takes a buffer and its length and expected to return a pointer to
927the Rectangle_t structure. Inside, it tries to convert the bytes
928passed into the target structure (rect) using the generic BER decoder
929and returns the rect pointer afterwards. If the structure cannot be
930deserialized, it frees the memory which might be left allocated by
931the unfinished <I>ber_decoder</I> routine and returns NULL. <B>This
932freeing is necessary</B> because the ber_decoder is a restartable procedure,
933and may fail just because there is more data needs to be provided
934before decoding could be finalized. The code above obviously does
935not take into account the way the <I>ber_decoder</I> failed, so the
936freeing is necessary because the part of the buffer may already be
937decoded into the structure by the time something goes wrong.
938
939<P>
940Restartable decoding is a little bit trickier: you need to provide
941the old target structure pointer (which might be already half-decoded)
942and react on RC_WMORE return code. This will be explained later in
943Section sub:Decoding-BER
944
945<P>
946
Lev Walkin26587ab2004-08-23 15:12:04 +0000947<H2><A NAME="SECTION03331000000000000000"></A><A NAME="sub:Decoding-BER"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +0000948Decoding BER
Lev Walkin26587ab2004-08-23 15:12:04 +0000949</H2>
Lev Walkineb97a702004-08-07 06:03:15 +0000950
951<P>
952The Basic Encoding Rules describe the basic way how the structure
953can be encoded and decoded. Several other encoding rules (CER, DER)
954define a more restrictive versions of BER, so the generic BER parser
955is also capable of decoding the data encoded by CER and DER encoders.
956The opposite is not true.
957
958<P>
959The ASN.1 compiler provides the generic BER decoder which is implicitly
960capable of decoding BER, CER and DER encoded data.
961
962<P>
963The decoder is restartable (stream-oriented), which means that in
964case the buffer has less data than it is expected, the decoder will
965process whatever it is available and ask for more data to be provided.
966Please note that the decoder may actually process less data than it
967is given in the buffer, which means that you should be able to make
968the next buffer contain the unprocessed part of the previous buffer.
969
970<P>
971Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
972
973<P>
974
975<UL>
976<LI>You may concatenate these buffers and feed the BER decoder with 300
977bytes of data, or
978</LI>
979<LI>You may feed it the first buffer of 100 bytes of data, realize that
980the ber_decoder consumed only 95 bytes from it and later feed the
981decoder with 205 bytes buffer which consists of 5 unprocessed bytes
982from the first buffer and the latter 200 bytes from the second buffer.
983</LI>
984</UL>
985This is not as convenient as it could be (like, the BER encoder would
986consume the whole 100 bytes and keep these 5 bytes in some temporary
987storage), but in case of stream-based processing it might actually
988be OK. Suggestions are welcome.
989
990<P>
991There are two ways to invoke a BER decoder. The first one is a direct
992reference of the type-specific decoder. This way was shown in the
993previous example of <I>simple_deserializer</I> function. The second
994way is to invoke a <I>ber_decode</I> function, which is just a simple
995wrapper of the former approach into a less wordy notation:
996
997<P>
998
Lev Walkin26587ab2004-08-23 15:12:04 +0000999<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +00001000rval&nbsp;=&nbsp;ber_decode(0,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;(void&nbsp;**)&amp;rect,
Lev Walkineb97a702004-08-07 06:03:15 +00001001&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size);
Lev Walkineb97a702004-08-07 06:03:15 +00001002</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001003</BLOCKQUOTE>
Lev Walkinc500b3e2004-09-29 13:37:15 +00001004Note that the initial (asn_DEF_Rectangle-&gt;ber_decoder) reference
Lev Walkineb97a702004-08-07 06:03:15 +00001005is gone, and also the last argument (0) is no longer necessary.
1006
1007<P>
1008These two ways of invocations are fully equivalent.
1009
1010<P>
Lev Walkinc500b3e2004-09-29 13:37:15 +00001011The BER de<I>coder</I> may fail because of (<I>the following RC_...
Lev Walkineb97a702004-08-07 06:03:15 +00001012codes are defined in ber_decoder.h</I>):
1013
1014<P>
1015
1016<UL>
1017<LI>RC_WMORE: There is more data expected than it is provided (stream
1018mode continuation feature);
1019</LI>
1020<LI>RC_FAIL: General failure to decode the buffer;
1021</LI>
1022<LI>... other codes may be defined as well.
1023</LI>
1024</UL>
Lev Walkina646ccb2004-10-20 15:48:55 +00001025Together with the return code (.code) the asn_dec_rval_t type contains
Lev Walkineb97a702004-08-07 06:03:15 +00001026the number of bytes which is consumed from the buffer. In the previous
1027hypothetical example of two buffers (of 100 and 200 bytes), the first
1028call to ber_decode() would return with .code = RC_WMORE and .consumed
1029= 95. The .consumed field of the BER decoder return value is <B>always</B>
1030valid, even if the decoder succeeds or fails with any other return
1031code.
1032
1033<P>
1034Please look into ber_decoder.h for the precise definition of ber_decode()
1035and related types.
1036
1037<P>
1038
Lev Walkin26587ab2004-08-23 15:12:04 +00001039<H2><A NAME="SECTION03332000000000000000"></A><A NAME="sub:Encoding-DER"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001040Encoding DER
Lev Walkin26587ab2004-08-23 15:12:04 +00001041</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001042
1043<P>
Lev Walkin683f9b72004-09-26 13:41:45 +00001044The Distinguished Encoding Rules is the <I>canonical</I> variant of
1045BER encoding rules. The DER is best suited to encode the structures
1046where all the lengths are known beforehand. This is probably exactly
1047how you want to encode: either after a BER decoding or after a manual
1048fill-up, the target structure contains the data which size is implicitly
1049known before encoding. The DER encoding is used, for example, to encode
1050X.509 certificates.
Lev Walkineb97a702004-08-07 06:03:15 +00001051
1052<P>
1053As with BER decoder, the DER encoder may be invoked either directly
Lev Walkinc500b3e2004-09-29 13:37:15 +00001054from the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone
1055function, which is somewhat simpler:
Lev Walkineb97a702004-08-07 06:03:15 +00001056
1057<P>
1058
Lev Walkin26587ab2004-08-23 15:12:04 +00001059<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +00001060/*
1061&nbsp;*&nbsp;This&nbsp;is&nbsp;a&nbsp;custom&nbsp;function&nbsp;which&nbsp;writes&nbsp;the
1062&nbsp;*&nbsp;encoded&nbsp;output&nbsp;into&nbsp;some&nbsp;FILE&nbsp;stream.
1063&nbsp;*/
Lev Walkin26587ab2004-08-23 15:12:04 +00001064static&nbsp;int
1065write_stream(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;size,&nbsp;void&nbsp;*app_key)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +00001066&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;*ostream&nbsp;=&nbsp;app_key;
1067&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;wrote;
1068&nbsp;
1069&nbsp;&nbsp;&nbsp;&nbsp;wrote&nbsp;=&nbsp;fwrite(buffer,&nbsp;1,&nbsp;size,&nbsp;ostream);
1070&nbsp;
1071&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(wrote&nbsp;==&nbsp;size)&nbsp;?&nbsp;0&nbsp;:&nbsp;-1;
1072}
1073&nbsp;
1074/*
1075&nbsp;*&nbsp;This&nbsp;is&nbsp;the&nbsp;serializer&nbsp;itself,
1076&nbsp;*&nbsp;it&nbsp;supplies&nbsp;the&nbsp;DER&nbsp;encoder&nbsp;with&nbsp;the
1077&nbsp;*&nbsp;pointer&nbsp;to&nbsp;the&nbsp;custom&nbsp;output&nbsp;function.
1078&nbsp;*/
1079ssize_t
1080simple_serializer(FILE&nbsp;*ostream,&nbsp;Rectangle_t&nbsp;*rect)&nbsp;{
Lev Walkin683f9b72004-09-26 13:41:45 +00001081&nbsp;&nbsp;&nbsp;&nbsp;asn_enc_rval_t&nbsp;er;&nbsp;&nbsp;/*&nbsp;Encoder&nbsp;return&nbsp;value&nbsp;*/
Lev Walkineb97a702004-08-07 06:03:15 +00001082&nbsp;
Lev Walkinc500b3e2004-09-29 13:37:15 +00001083&nbsp;&nbsp;&nbsp;&nbsp;er&nbsp;=&nbsp;der_encode(&amp;asn_DEF_Rect,&nbsp;rect,
Lev Walkin26587ab2004-08-23 15:12:04 +00001084&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_stream,&nbsp;ostream);
Lev Walkin683f9b72004-09-26 13:41:45 +00001085&nbsp;&nbsp;&nbsp;&nbsp;if(er.<B>encoded</B>&nbsp;==&nbsp;-1)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +00001086&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*
Lev Walkin683f9b72004-09-26 13:41:45 +00001087&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Failed&nbsp;to&nbsp;encode&nbsp;the&nbsp;rectangle&nbsp;data.
Lev Walkineb97a702004-08-07 06:03:15 +00001088&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
1089&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''Cannot&nbsp;encode&nbsp;%s:&nbsp;%s&#92;n'',
Lev Walkin683f9b72004-09-26 13:41:45 +00001090&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;er.<B>failed_type</B>-&gt;name,
Lev Walkineb97a702004-08-07 06:03:15 +00001091&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strerror(errno));
1092&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-1;
1093&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
1094&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Return&nbsp;the&nbsp;number&nbsp;of&nbsp;bytes&nbsp;*/
Lev Walkin683f9b72004-09-26 13:41:45 +00001095&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;er.encoded;
Lev Walkineb97a702004-08-07 06:03:15 +00001096&nbsp;&nbsp;&nbsp;&nbsp;}
1097}
Lev Walkineb97a702004-08-07 06:03:15 +00001098</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001099</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001100As you see, the DER encoder does not write into some sort of buffer
1101or something. It just invokes the custom function (possible, multiple
1102times) which would save the data into appropriate storage. The optional
1103argument <I>app_key</I> is opaque for the DER encoder code and just
1104used by <I>_write_stream()</I> as the pointer to the appropriate
1105output stream to be used.
1106
1107<P>
1108If the custom write function is not given (passed as 0), then the
1109DER encoder will essentially do the same thing (i.e., encode the data)
1110but no callbacks will be invoked (so the data goes nowhere). It may
1111prove useful to determine the size of the structure's encoding before
Lev Walkin26587ab2004-08-23 15:12:04 +00001112actually doing the encoding<A NAME="tex2html9"
Lev Walkin28c89eb2004-09-30 22:45:58 +00001113 HREF="#foot329"><SUP>4.3</SUP></A>.
Lev Walkineb97a702004-08-07 06:03:15 +00001114
1115<P>
1116Please look into der_encoder.h for the precise definition of der_encode()
1117and related types.
1118
1119<P>
1120
Lev Walkin683f9b72004-09-26 13:41:45 +00001121<H2><A NAME="SECTION03333000000000000000"></A><A NAME="sub:Encoding-XER"></A><BR>
1122Encoding XER
1123</H2>
1124
1125<P>
1126The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
1127Markup Language, a text-based format for information exchange. The
1128encoder routine API comes in two flavors: stdio-based and callback-based.
1129With the callback-based encoder, the encoding process is very similar
1130to the DER one, described in Section sub:Encoding-DER. The
1131following example uses the definition of write_stream() from up there.
1132
1133<P>
1134
1135<BLOCKQUOTE><PRE>
1136/*
1137&nbsp;*&nbsp;This&nbsp;procedure&nbsp;generates&nbsp;the&nbsp;XML&nbsp;document
1138&nbsp;*&nbsp;by&nbsp;invoking&nbsp;the&nbsp;XER&nbsp;encoder.
1139&nbsp;*&nbsp;NOTE:&nbsp;Do&nbsp;not&nbsp;copy&nbsp;this&nbsp;code&nbsp;verbatim!
1140&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;the&nbsp;stdio&nbsp;output&nbsp;is&nbsp;necessary,
1141&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;the&nbsp;xer_fprint()&nbsp;procedure&nbsp;instead.
1142&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;See&nbsp;Section&nbsp;sub:Printing-the-target.
1143&nbsp;*/
1144int
1145print_as_XML(FILE&nbsp;*ostream,&nbsp;Rectangle_t&nbsp;*rect)&nbsp;{
1146&nbsp;&nbsp;&nbsp;&nbsp;asn_enc_rval_t&nbsp;er;&nbsp;&nbsp;/*&nbsp;Encoder&nbsp;return&nbsp;value&nbsp;*/
1147&nbsp;
Lev Walkinc500b3e2004-09-29 13:37:15 +00001148&nbsp;&nbsp;&nbsp;&nbsp;er&nbsp;=&nbsp;xer_encode(&amp;asn_DEF_Rect,&nbsp;rect,
Lev Walkin683f9b72004-09-26 13:41:45 +00001149&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XER_F_BASIC,&nbsp;/*&nbsp;BASIC-XER&nbsp;or&nbsp;CANONICAL-XER&nbsp;*/
1150&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_stream,&nbsp;ostream);
1151&nbsp;
1152&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(er.encoded&nbsp;==&nbsp;-1)&nbsp;?&nbsp;-1&nbsp;:&nbsp;0;
1153}
1154</PRE>
1155</BLOCKQUOTE>
1156Please look into xer_encoder.h for the precise definition of xer_encode()
1157and related types.
1158
1159<P>
1160See Section [<A HREF="#sub:Printing-the-target">Printing-the-target</A>] for the example of stdio-based
1161XML encoder and other pretty-printing suggestions.
1162
1163<P>
1164
1165<H2><A NAME="SECTION03334000000000000000"></A><A NAME="sub:Validating-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001166Validating the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +00001167</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001168
1169<P>
1170Sometimes the target structure needs to be validated. For example,
1171if the structure was created by the application (as opposed to being
1172decoded from some external source), some important information required
1173by the ASN.1 specification might be missing. On the other hand, the
1174successful decoding of the data from some external source does not
1175necessarily mean that the data is fully valid either. It might well
1176be the case that the specification describes some subtype constraints
1177that were not taken into account during decoding, and it would actually
1178be useful to perform the last check when the data is ready to be encoded
1179or when the data has just been decoded to ensure its validity according
1180to some stricter rules.
1181
1182<P>
1183The asn_check_constraints() function checks the type for various
1184implicit and explicit constraints. It is recommended to use asn_check_constraints()
1185function after each decoding and before each encoding.
1186
1187<P>
1188Please look into constraints.h for the precise definition of asn_check_constraints()
1189and related types.
1190
1191<P>
1192
Lev Walkin683f9b72004-09-26 13:41:45 +00001193<H2><A NAME="SECTION03335000000000000000"></A><A NAME="sub:Printing-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001194Printing the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +00001195</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001196
1197<P>
1198There are two ways to print the target structure: either invoke the
1199print_struct member of the ASN.1 type descriptor, or using the asn_fprint()
1200function, which is a simpler wrapper of the former:
1201
1202<P>
1203
Lev Walkin26587ab2004-08-23 15:12:04 +00001204<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +00001205asn_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect);
Lev Walkineb97a702004-08-07 06:03:15 +00001206</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001207</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001208Please look into constr_TYPE.h for the precise definition of asn_fprint()
1209and related types.
1210
1211<P>
Lev Walkin683f9b72004-09-26 13:41:45 +00001212Another practical alternative to this custom format printing would
1213be to invoke XER encoder. The default BASIC-XER encoder performs reasonable
1214formatting for the output to be useful and human readable. To invoke
1215the XER decoder in a manner similar to asn_fprint(), use the xer_fprint()
1216call:
Lev Walkineb97a702004-08-07 06:03:15 +00001217
Lev Walkin683f9b72004-09-26 13:41:45 +00001218<P>
1219
1220<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +00001221xer_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect);
Lev Walkin683f9b72004-09-26 13:41:45 +00001222</PRE>
1223</BLOCKQUOTE>
1224See Section sub:Encoding-XER for XML-related details.
1225
1226<P>
1227
1228<H2><A NAME="SECTION03336000000000000000"></A><A NAME="sub:Freeing-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001229Freeing the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +00001230</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001231
1232<P>
1233Freeing the structure is slightly more complex than it may seem to.
1234When the ASN.1 structure is freed, all the members of the structure
1235and their submembers etc etc are recursively freed too. But it might
1236not be feasible to free the structure itself. Consider the following
1237case:
1238
1239<P>
1240
Lev Walkin26587ab2004-08-23 15:12:04 +00001241<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +00001242struct&nbsp;my_figure&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;custom&nbsp;structure&nbsp;*/
1243&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;flags;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;&lt;some&nbsp;custom&nbsp;member&gt;&nbsp;*/
1244&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;type&nbsp;is&nbsp;generated&nbsp;by&nbsp;the&nbsp;ASN.1&nbsp;compiler&nbsp;*/
1245&nbsp;&nbsp;&nbsp;&nbsp;<I>Rectangle_t&nbsp;rect;</I>
1246&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;other&nbsp;members&nbsp;of&nbsp;the&nbsp;structure&nbsp;*/
1247};
Lev Walkineb97a702004-08-07 06:03:15 +00001248</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001249</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001250In this example, the application programmer defined a custom structure
1251with one ASN.1-derived member (rect). This member is not a reference
1252to the Rectangle_t, but an in-place inclusion of the Rectangle_t
1253structure. If the freeing is necessary, the usual procedure of freeing
1254everything must not be applied to the &amp;rect pointer itself, because
1255it does not point to the memory block directly allocated by memory
1256allocation routine, but instead lies within such a block allocated
1257for my_figure structure.
1258
1259<P>
1260To solve this problem, the free_struct routine has the additional
1261argument (besides the intuitive type descriptor and target structure
1262pointers), which is the flag specifying whether the outer pointer
1263itself must be freed (0, default) or it should be left intact (non-zero
1264value).
1265
1266<P>
1267
Lev Walkin26587ab2004-08-23 15:12:04 +00001268<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +00001269/*&nbsp;Rectangle_t&nbsp;is&nbsp;defined&nbsp;within&nbsp;my_figure&nbsp;*/
1270struct&nbsp;my_figure&nbsp;*mf&nbsp;=&nbsp;<B>...</B>;
1271/*
1272&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_td
1273&nbsp;*&nbsp;without&nbsp;freeing&nbsp;the&nbsp;mf-&gt;rect&nbsp;pointer
1274&nbsp;*/
Lev Walkinc500b3e2004-09-29 13:37:15 +00001275asn_DEF_Rectangle-&gt;free_struct(
1276&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;&amp;mf-&gt;rect,&nbsp;<I>1</I>&nbsp;/*&nbsp;!free&nbsp;*/);
Lev Walkineb97a702004-08-07 06:03:15 +00001277&nbsp;
1278/*&nbsp;Rectangle_t&nbsp;is&nbsp;a&nbsp;stand-alone&nbsp;pointer&nbsp;*/
1279Rectangle_t&nbsp;*rect&nbsp;=&nbsp;<B>...</B>;
1280/*
1281&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_t
1282&nbsp;*&nbsp;and&nbsp;freeing&nbsp;the&nbsp;rect&nbsp;pointer
1283&nbsp;*/
Lev Walkinc500b3e2004-09-29 13:37:15 +00001284asn_DEF_Rectangle-&gt;free_struct(
1285&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect,&nbsp;<I>0</I>&nbsp;/*&nbsp;free&nbsp;the&nbsp;pointer&nbsp;too&nbsp;*/);
Lev Walkineb97a702004-08-07 06:03:15 +00001286</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001287</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001288It is safe to invoke the <I>free_struct</I> function with the target
1289structure pointer set to 0 (NULL), the function will do nothing.
1290
1291<P>
1292
1293<H2><A NAME="SECTION04000000000000000000">
1294Bibliography</A>
Lev Walkin26587ab2004-08-23 15:12:04 +00001295</H2><DL COMPACT><DD><P></P><DT><A NAME="ASN1C">ASN1C</A>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001296<DD>The OpenSource ASN.1 Compiler. <A HREF=http://lionet.info/asn1c/>http://lionet.info/asn1c/</A>
Lev Walkin26587ab2004-08-23 15:12:04 +00001297<P></P><DT><A NAME="Dub00">Dub00</A>
Lev Walkineb97a702004-08-07 06:03:15 +00001298<DD>Olivier Dubuisson - <I>ASN.1 Communication between heterogeneous
Lev Walkin28c89eb2004-09-30 22:45:58 +00001299systems</I> - Morgan Kaufmann Publishers, 2000. <A HREF=http://asn1.elibel.tm.fr/en/book/>http://asn1.elibel.tm.fr/en/book/</A>.
Lev Walkineb97a702004-08-07 06:03:15 +00001300ISBN:0-12-6333361-0.
1301<P></P><DT><A NAME="ITU-T_ASN.1">ITU-T/ASN.1</A>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001302<DD>ITU-T Study Group 17 - Languages for Telecommunication Systems <A HREF=http://www.itu.int/ITU-T/studygroups/com17/languages/>http://www.itu.int/ITU-T/studygroups/com17/languages/</A></DL>
Lev Walkineb97a702004-08-07 06:03:15 +00001303
1304<P>
1305<BR><HR><H4>Footnotes</H4>
1306<DL>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001307<DT><A NAME="foot159">... supported</A><A
Lev Walkineb97a702004-08-07 06:03:15 +00001308 HREF="asn1c-usage.html#tex2html1"><SUP>2.1</SUP></A></DT>
Lev Walkin26587ab2004-08-23 15:12:04 +00001309<DD>C++ is ''supported'' too, as long as an class-based approach is
1310not a definitive factor.
Lev Walkineb97a702004-08-07 06:03:15 +00001311
1312</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001313<DT><A NAME="foot419">... this</A><A
Lev Walkineb97a702004-08-07 06:03:15 +00001314 HREF="asn1c-usage.html#tex2html2"><SUP>2.2</SUP></A></DT>
Lev Walkin6faa68e2004-09-17 08:35:02 +00001315<DD><I>-fnative-types</I> compiler option is used to produce basic C <I>int</I>
1316types instead of infinite width INTEGER_t structures. See <A HREF=#Table1>Table 1</A>.
Lev Walkineb97a702004-08-07 06:03:15 +00001317
1318</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001319<DT><A NAME="foot166">... binary</A><A
Lev Walkineb97a702004-08-07 06:03:15 +00001320 HREF="asn1c-usage.html#tex2html3"><SUP>2.3</SUP></A></DT>
1321<DD>BER, CER and DER encodings are binary. However, the XER encoding is
1322text (XML) based.
1323
1324</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001325<DT><A NAME="foot420">...asn1c</A><A
Lev Walkin26587ab2004-08-23 15:12:04 +00001326 HREF="asn1c-usage.html#tex2html4"><SUP>3.1</SUP></A></DT>
1327<DD>The 1 symbol in asn<B>1</B>c is a digit, not an ''ell'' letter.
1328
1329</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001330<DT><A NAME="foot421">... specification</A><A
Lev Walkin26587ab2004-08-23 15:12:04 +00001331 HREF="asn1c-usage.html#tex2html5"><SUP>3.2</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001332<DD>This is probably <B>not</B> what you want to try out right now -
Lev Walkin26587ab2004-08-23 15:12:04 +00001333read through the rest of this chapter to find out about <B>-P</B>
1334and <B>-R</B> options.
Lev Walkineb97a702004-08-07 06:03:15 +00001335
1336</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001337<DT><A NAME="foot427">...that&nbsp;simple</A><A
Lev Walkin26587ab2004-08-23 15:12:04 +00001338 HREF="asn1c-usage.html#tex2html7"><SUP>4.1</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001339<DD>Provided that you've also created a .c file with the <I>int main()</I>
1340routine.
1341
1342</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001343<DT><A NAME="foot253">...restartable</A><A
Lev Walkin26587ab2004-08-23 15:12:04 +00001344 HREF="asn1c-usage.html#tex2html8"><SUP>4.2</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001345<DD>Restartable means that if the decoder encounters the end of the buffer,
1346it will fail, but may later be invoked again with the rest of the
1347buffer to continue decoding.
1348
1349</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001350<DT><A NAME="foot329">... encoding</A><A
Lev Walkin26587ab2004-08-23 15:12:04 +00001351 HREF="asn1c-usage.html#tex2html9"><SUP>4.3</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001352<DD>It is actually faster too: the encoder might skip over some computations
1353which aren't important for the size determination.
1354
1355</DD>
1356</DL><BR><HR>
1357<ADDRESS>
1358Lev Walkin
Lev Walkin28c89eb2004-09-30 22:45:58 +000013592004-09-30
Lev Walkineb97a702004-08-07 06:03:15 +00001360</ADDRESS>
1361</BODY>
1362</HTML>