blob: fcc2980c7d086832e66bb5373b5574db87a24fbf [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 Walkin26587ab2004-08-23 15:12:04 +0000695<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Warning Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
696<B>Description</B></TD></TR>
697</TBODY><TBODY>
698<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Werror</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
699<FONT SIZE="-1">Treat warnings as errors; abort if any warning is produced.</FONT></TD></TR>
700</TBODY><TBODY>
701<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-lexer</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
702<FONT SIZE="-1">Enable lexer debugging during the ASN.1 parsing stage.</FONT></TD></TR>
703</TBODY><TBODY>
704<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-fixer</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
705 <FONT SIZE="-1">Enable ASN.1 syntax tree fixer debugging during the
706 fixing stage.</FONT></TD></TR>
707</TBODY><TBODY>
708<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-compiler</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
709<FONT SIZE="-1">Enable debugging during the actual compile time.</FONT></TD></TR>
710</TBODY><TBODY>
711<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Language Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
712<B>Description</B></TD></TR>
713</TBODY><TBODY>
Lev Walkin71a8aaf2004-09-08 03:10:54 +0000714<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 +0000715Normally the compiler hides the definitions (asn_DEF_xxx) of the
Lev Walkin71a8aaf2004-09-08 03:10:54 +0000716inner structure elements (members of SEQUENCE, SET and other types).
717This option makes all such definitions global. Enabling this option
Lev Walkinc500b3e2004-09-29 13:37:15 +0000718may pollute the namespace by making lots of asn_DEF_xxx structures
Lev Walkin71a8aaf2004-09-08 03:10:54 +0000719globally visible, but will allow you to manipulate (encode and decode)
720the individual members of any complex ASN.1 structure.</TD></TR>
721</TBODY><TBODY>
Lev Walkin26587ab2004-08-23 15:12:04 +0000722<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fbless-SIZE</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
723<FONT SIZE="-1">Allow SIZE() constraint for INTEGER, ENUMERATED, and other
724types for which this constraint is normally prohibited by the standard.
725This is a violation of an ASN.1 standard and compiler may fail to
726produce the meaningful code.</FONT></TD></TR>
727</TBODY><TBODY>
Lev Walkin6faa68e2004-09-17 08:35:02 +0000728<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fnative-types</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
729<FONT SIZE="-1">Use the native machine's data types (int, double) whenever
Lev Walkin28c89eb2004-09-30 22:45:58 +0000730possible, instead of the compound INTEGER_t, ENUMERATED_t and REAL_t
731types. </FONT></TD></TR>
Lev Walkin26587ab2004-08-23 15:12:04 +0000732</TBODY><TBODY>
Lev Walkin683f9b72004-09-26 13:41:45 +0000733<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fno-constraints</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
734Do not generate ASN.1 subtype constraint checking code. This may make
735a shorter executable.</TD></TR>
736</TBODY><TBODY>
Lev Walkin26587ab2004-08-23 15:12:04 +0000737<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-funnamed-unions</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
738<FONT SIZE="-1">Enable unnamed unions in the definitions of target language's
739structures.</FONT></TD></TR>
740</TBODY><TBODY>
741<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-ftypes88</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin28c89eb2004-09-30 22:45:58 +0000742<FONT SIZE="-1">Pretend to support only ASN.1:1988 embedded types. Certain
743reserved words, such as UniversalString and BMPString, become ordinary
744type references and may be redefined by the specification.</FONT></TD></TR>
Lev Walkin26587ab2004-08-23 15:12:04 +0000745</TBODY><TBODY>
746<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Output Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
747<B>Description</B></TD></TR>
Lev Walkin28c89eb2004-09-30 22:45:58 +0000748<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>
749
750-print-constraints</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000751<FONT SIZE="-1">When -EF are also specified, this option forces the compiler
752to explain its internal understanding of subtype constraints.</FONT></TD></TR>
753</TBODY><TBODY>
754<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-print-lines</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
755<FONT SIZE="-1">Generate &#34;- #line&#34; comments in -E output.</FONT></TD></TR>
756</TBODY>
757</TABLE>
758
759<P>
760</TD></TR>
761</TABLE>
762</DIV><P></P><BR>
763
764<P>
765
766<H1><A NAME="SECTION03320000000000000000">
Lev Walkineb97a702004-08-07 06:03:15 +0000767Recognizing compiler output</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000768</H1>
Lev Walkineb97a702004-08-07 06:03:15 +0000769
770<P>
771After compiling, the following entities will be created in your current
772directory:
773
774<P>
775
776<UL>
777<LI>A set of .c and .h files, generally a single pair for each type defined
778in the ASN.1 specifications. These files will be named similarly to
779the ASN.1 types (<I>Rectangle.c</I> and <I>Rectangle.h</I> for the
780specification defined in the beginning of this document).
781</LI>
782<LI>A set of helper .c and .h files which contain generic encoders, decoders
Lev Walkin26587ab2004-08-23 15:12:04 +0000783and other useful routines. There will be quite a few of them, some
784of them even are not always necessary, but the overall amount of code
785after compiling will be rather small anyway.
Lev Walkineb97a702004-08-07 06:03:15 +0000786</LI>
787</UL>
788It is your responsibility to create .c file with the <I>int main()</I>
789routine and the Makefile (if needed). Compiler helps you with the
790latter by creating the Makefile.am.sample, containing the skeleton
791definition for the automake, should you want to use autotools.
792
793<P>
794In other words, after compiling the Rectangle module, you have the
795following set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h,
796<B>...</B> }, where <B>''...''</B> stands for the
797set of additional ''helper'' files created by the compiler. If you
798add the simple file with the <I>int main()</I> routine, it would even
799be possible to compile everything with the single instruction:
800
801<P>
802
Lev Walkin26587ab2004-08-23 15:12:04 +0000803<BLOCKQUOTE><PRE>
804cc&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 +0000805 HREF="#foot427"><SUP>4.1</SUP></A>
Lev Walkineb97a702004-08-07 06:03:15 +0000806</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000807</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000808
809<P>
810
Lev Walkin26587ab2004-08-23 15:12:04 +0000811<H1><A NAME="SECTION03330000000000000000">
Lev Walkin28c89eb2004-09-30 22:45:58 +0000812Invoking the ASN.1 helper code from an application</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000813</H1>
Lev Walkineb97a702004-08-07 06:03:15 +0000814
815<P>
Lev Walkin26587ab2004-08-23 15:12:04 +0000816First of all, you should to include one or more header files into
817your application. For our Rectangle module, including the Rectangle.h
Lev Walkineb97a702004-08-07 06:03:15 +0000818file is enough:
819
820<P>
821
Lev Walkin26587ab2004-08-23 15:12:04 +0000822<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000823#include&nbsp;&lt;Rectangle.h&gt;
Lev Walkineb97a702004-08-07 06:03:15 +0000824</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000825</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000826The header files defines the C structure corresponding to the ASN.1
827definition of a rectangle and the declaration of the ASN.1 type descriptor,
828which is used as an argument to most of the functions provided by
829the ASN.1 module. For example, here is the code which frees the Rectangle_t
830structure:
831
832<P>
833
Lev Walkin26587ab2004-08-23 15:12:04 +0000834<BLOCKQUOTE><PRE>
835Rectangle_t&nbsp;*rect&nbsp;=&nbsp;...;
836&nbsp;
Lev Walkinc500b3e2004-09-29 13:37:15 +0000837asn_DEF_Rectangle-&gt;free_struct(&amp;asn_DEF_Rectangle,
Lev Walkineb97a702004-08-07 06:03:15 +0000838&nbsp;&nbsp;&nbsp;&nbsp;rect,&nbsp;0);
Lev Walkineb97a702004-08-07 06:03:15 +0000839</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000840</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000841This code defines a <I>rect</I> pointer which points to the Rectangle_t
842structure which needs to be freed. The second line invokes the generic
843free_struct routine created specifically for this Rectangle_t structure.
Lev Walkinc500b3e2004-09-29 13:37:15 +0000844The <I>asn_DEF_Rectangle</I> is the type descriptor, which holds
Lev Walkineb97a702004-08-07 06:03:15 +0000845a collection of generic routines to deal with the Rectangle_t structure.
846
847<P>
848There are several generic functions available:
849
850<P>
851<DL>
Lev Walkineb97a702004-08-07 06:03:15 +0000852<DT><STRONG>ber_decoder</STRONG></DT>
Lev Walkin26587ab2004-08-23 15:12:04 +0000853<DD>This is the generic <I>restartable</I><A NAME="tex2html8"
Lev Walkin28c89eb2004-09-30 22:45:58 +0000854 HREF="#foot253"><SUP>4.2</SUP></A> BER decoder (Basic Encoding Rules). This decoder would create
Lev Walkineb97a702004-08-07 06:03:15 +0000855and/or fill the target structure for you. Please refer to Section
856[<A HREF="#sub:Decoding-BER">Decoding-BER</A>].
857</DD>
858<DT><STRONG>der_encoder</STRONG></DT>
859<DD>This is the generic DER encoder (Distinguished Encoding
Lev Walkin683f9b72004-09-26 13:41:45 +0000860Rules). This encoder will take the target structure and encode it
Lev Walkineb97a702004-08-07 06:03:15 +0000861into a series of bytes. Please refer to Section [<A HREF="#sub:Encoding-DER">Encoding-DER</A>].
862</DD>
Lev Walkin683f9b72004-09-26 13:41:45 +0000863<DT><STRONG>xer_encoder</STRONG></DT>
864<DD>This is the generic XER encoder (XML Encoding Rules).
865This encoder will take the target structure and represent it as an
866XML (text) document. Please refer to Section [<A HREF="#sub:Encoding-XER">Encoding-XER</A>].
867</DD>
868<DT><STRONG>check_constraints</STRONG></DT>
869<DD>Check that the contents of the target structure
870are semantically valid and constrained to appropriate implicit or
871explicit subtype constraints. Please refer to Section sub:Validating-the-target.
872</DD>
Lev Walkineb97a702004-08-07 06:03:15 +0000873<DT><STRONG>print_struct</STRONG></DT>
874<DD>This function convert the contents of the passed target
875structure into human readable form. This form is not formal and cannot
876be converted back into the structure, but it may turn out to be useful
877for debugging or quick-n-dirty printing. Please refer to Section [<A HREF="#sub:Printing-the-target">Printing-the-target</A>].
878</DD>
879<DT><STRONG>free_struct</STRONG></DT>
880<DD>This is a generic disposal which frees the target structure.
881Please refer to Section [<A HREF="#sub:Freeing-the-target">Freeing-the-target</A>].
882</DD>
883</DL>
Lev Walkin683f9b72004-09-26 13:41:45 +0000884check_constraints Check that the contents of the target structure
885are semantically valid and constrained to appropriate implicit or
886explicit subtype constraints. Please refer to Section sub:Validating-the-target.
887
888<P>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000889Each of the above function takes the type descriptor (<I>asn_DEF_...</I>)
Lev Walkineb97a702004-08-07 06:03:15 +0000890and the target structure (<I>rect</I>, in the above example). The
891target structure is typically created by the generic BER decoder or
892by the application itself.
893
894<P>
895Here is how the buffer can be deserialized into the structure:
896
897<P>
898
Lev Walkin26587ab2004-08-23 15:12:04 +0000899<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000900Rectangle_t&nbsp;*
Lev Walkin26587ab2004-08-23 15:12:04 +0000901simple_deserializer(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;buf_size)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +0000902&nbsp;&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;*rect&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Note&nbsp;this&nbsp;0!&nbsp;*/
903&nbsp;&nbsp;&nbsp;&nbsp;ber_dec_rval_t&nbsp;rval;
904&nbsp;
Lev Walkinc500b3e2004-09-29 13:37:15 +0000905&nbsp;&nbsp;&nbsp;&nbsp;rval&nbsp;=&nbsp;asn_DEF_Rectangle-&gt;ber_decoder(0,
906&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,
Lev Walkineb97a702004-08-07 06:03:15 +0000907&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void&nbsp;**)&amp;rect,
908&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size,
909&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0);
910&nbsp;
911&nbsp;&nbsp;&nbsp;&nbsp;if(rval<B>.code</B>&nbsp;==&nbsp;RC_OK)&nbsp;{
912&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;*/
913&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
Lev Walkin26587ab2004-08-23 15:12:04 +0000914&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Free&nbsp;partially&nbsp;decoded&nbsp;rect&nbsp;*/
Lev Walkinc500b3e2004-09-29 13:37:15 +0000915&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn_DEF_Rectangle-&gt;free_struct(
916&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 +0000917&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;
918&nbsp;&nbsp;&nbsp;&nbsp;}
919}
Lev Walkineb97a702004-08-07 06:03:15 +0000920</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000921</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000922The code above defines a function, <I>simple_deserializer</I>, which
923takes a buffer and its length and expected to return a pointer to
924the Rectangle_t structure. Inside, it tries to convert the bytes
925passed into the target structure (rect) using the generic BER decoder
926and returns the rect pointer afterwards. If the structure cannot be
927deserialized, it frees the memory which might be left allocated by
928the unfinished <I>ber_decoder</I> routine and returns NULL. <B>This
929freeing is necessary</B> because the ber_decoder is a restartable procedure,
930and may fail just because there is more data needs to be provided
931before decoding could be finalized. The code above obviously does
932not take into account the way the <I>ber_decoder</I> failed, so the
933freeing is necessary because the part of the buffer may already be
934decoded into the structure by the time something goes wrong.
935
936<P>
937Restartable decoding is a little bit trickier: you need to provide
938the old target structure pointer (which might be already half-decoded)
939and react on RC_WMORE return code. This will be explained later in
940Section sub:Decoding-BER
941
942<P>
943
Lev Walkin26587ab2004-08-23 15:12:04 +0000944<H2><A NAME="SECTION03331000000000000000"></A><A NAME="sub:Decoding-BER"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +0000945Decoding BER
Lev Walkin26587ab2004-08-23 15:12:04 +0000946</H2>
Lev Walkineb97a702004-08-07 06:03:15 +0000947
948<P>
949The Basic Encoding Rules describe the basic way how the structure
950can be encoded and decoded. Several other encoding rules (CER, DER)
951define a more restrictive versions of BER, so the generic BER parser
952is also capable of decoding the data encoded by CER and DER encoders.
953The opposite is not true.
954
955<P>
956The ASN.1 compiler provides the generic BER decoder which is implicitly
957capable of decoding BER, CER and DER encoded data.
958
959<P>
960The decoder is restartable (stream-oriented), which means that in
961case the buffer has less data than it is expected, the decoder will
962process whatever it is available and ask for more data to be provided.
963Please note that the decoder may actually process less data than it
964is given in the buffer, which means that you should be able to make
965the next buffer contain the unprocessed part of the previous buffer.
966
967<P>
968Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
969
970<P>
971
972<UL>
973<LI>You may concatenate these buffers and feed the BER decoder with 300
974bytes of data, or
975</LI>
976<LI>You may feed it the first buffer of 100 bytes of data, realize that
977the ber_decoder consumed only 95 bytes from it and later feed the
978decoder with 205 bytes buffer which consists of 5 unprocessed bytes
979from the first buffer and the latter 200 bytes from the second buffer.
980</LI>
981</UL>
982This is not as convenient as it could be (like, the BER encoder would
983consume the whole 100 bytes and keep these 5 bytes in some temporary
984storage), but in case of stream-based processing it might actually
985be OK. Suggestions are welcome.
986
987<P>
988There are two ways to invoke a BER decoder. The first one is a direct
989reference of the type-specific decoder. This way was shown in the
990previous example of <I>simple_deserializer</I> function. The second
991way is to invoke a <I>ber_decode</I> function, which is just a simple
992wrapper of the former approach into a less wordy notation:
993
994<P>
995
Lev Walkin26587ab2004-08-23 15:12:04 +0000996<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000997rval&nbsp;=&nbsp;ber_decode(0,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;(void&nbsp;**)&amp;rect,
Lev Walkineb97a702004-08-07 06:03:15 +0000998&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size);
Lev Walkineb97a702004-08-07 06:03:15 +0000999</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001000</BLOCKQUOTE>
Lev Walkinc500b3e2004-09-29 13:37:15 +00001001Note that the initial (asn_DEF_Rectangle-&gt;ber_decoder) reference
Lev Walkineb97a702004-08-07 06:03:15 +00001002is gone, and also the last argument (0) is no longer necessary.
1003
1004<P>
1005These two ways of invocations are fully equivalent.
1006
1007<P>
Lev Walkinc500b3e2004-09-29 13:37:15 +00001008The BER de<I>coder</I> may fail because of (<I>the following RC_...
Lev Walkineb97a702004-08-07 06:03:15 +00001009codes are defined in ber_decoder.h</I>):
1010
1011<P>
1012
1013<UL>
1014<LI>RC_WMORE: There is more data expected than it is provided (stream
1015mode continuation feature);
1016</LI>
1017<LI>RC_FAIL: General failure to decode the buffer;
1018</LI>
1019<LI>... other codes may be defined as well.
1020</LI>
1021</UL>
1022Together with the return code (.code) the ber_dec_rval_t type contains
1023the number of bytes which is consumed from the buffer. In the previous
1024hypothetical example of two buffers (of 100 and 200 bytes), the first
1025call to ber_decode() would return with .code = RC_WMORE and .consumed
1026= 95. The .consumed field of the BER decoder return value is <B>always</B>
1027valid, even if the decoder succeeds or fails with any other return
1028code.
1029
1030<P>
1031Please look into ber_decoder.h for the precise definition of ber_decode()
1032and related types.
1033
1034<P>
1035
Lev Walkin26587ab2004-08-23 15:12:04 +00001036<H2><A NAME="SECTION03332000000000000000"></A><A NAME="sub:Encoding-DER"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001037Encoding DER
Lev Walkin26587ab2004-08-23 15:12:04 +00001038</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001039
1040<P>
Lev Walkin683f9b72004-09-26 13:41:45 +00001041The Distinguished Encoding Rules is the <I>canonical</I> variant of
1042BER encoding rules. The DER is best suited to encode the structures
1043where all the lengths are known beforehand. This is probably exactly
1044how you want to encode: either after a BER decoding or after a manual
1045fill-up, the target structure contains the data which size is implicitly
1046known before encoding. The DER encoding is used, for example, to encode
1047X.509 certificates.
Lev Walkineb97a702004-08-07 06:03:15 +00001048
1049<P>
1050As with BER decoder, the DER encoder may be invoked either directly
Lev Walkinc500b3e2004-09-29 13:37:15 +00001051from the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone
1052function, which is somewhat simpler:
Lev Walkineb97a702004-08-07 06:03:15 +00001053
1054<P>
1055
Lev Walkin26587ab2004-08-23 15:12:04 +00001056<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +00001057/*
1058&nbsp;*&nbsp;This&nbsp;is&nbsp;a&nbsp;custom&nbsp;function&nbsp;which&nbsp;writes&nbsp;the
1059&nbsp;*&nbsp;encoded&nbsp;output&nbsp;into&nbsp;some&nbsp;FILE&nbsp;stream.
1060&nbsp;*/
Lev Walkin26587ab2004-08-23 15:12:04 +00001061static&nbsp;int
1062write_stream(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;size,&nbsp;void&nbsp;*app_key)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +00001063&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;*ostream&nbsp;=&nbsp;app_key;
1064&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;wrote;
1065&nbsp;
1066&nbsp;&nbsp;&nbsp;&nbsp;wrote&nbsp;=&nbsp;fwrite(buffer,&nbsp;1,&nbsp;size,&nbsp;ostream);
1067&nbsp;
1068&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(wrote&nbsp;==&nbsp;size)&nbsp;?&nbsp;0&nbsp;:&nbsp;-1;
1069}
1070&nbsp;
1071/*
1072&nbsp;*&nbsp;This&nbsp;is&nbsp;the&nbsp;serializer&nbsp;itself,
1073&nbsp;*&nbsp;it&nbsp;supplies&nbsp;the&nbsp;DER&nbsp;encoder&nbsp;with&nbsp;the
1074&nbsp;*&nbsp;pointer&nbsp;to&nbsp;the&nbsp;custom&nbsp;output&nbsp;function.
1075&nbsp;*/
1076ssize_t
1077simple_serializer(FILE&nbsp;*ostream,&nbsp;Rectangle_t&nbsp;*rect)&nbsp;{
Lev Walkin683f9b72004-09-26 13:41:45 +00001078&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 +00001079&nbsp;
Lev Walkinc500b3e2004-09-29 13:37:15 +00001080&nbsp;&nbsp;&nbsp;&nbsp;er&nbsp;=&nbsp;der_encode(&amp;asn_DEF_Rect,&nbsp;rect,
Lev Walkin26587ab2004-08-23 15:12:04 +00001081&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_stream,&nbsp;ostream);
Lev Walkin683f9b72004-09-26 13:41:45 +00001082&nbsp;&nbsp;&nbsp;&nbsp;if(er.<B>encoded</B>&nbsp;==&nbsp;-1)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +00001083&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*
Lev Walkin683f9b72004-09-26 13:41:45 +00001084&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 +00001085&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
1086&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 +00001087&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 +00001088&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strerror(errno));
1089&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-1;
1090&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
1091&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 +00001092&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;er.encoded;
Lev Walkineb97a702004-08-07 06:03:15 +00001093&nbsp;&nbsp;&nbsp;&nbsp;}
1094}
Lev Walkineb97a702004-08-07 06:03:15 +00001095</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001096</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001097As you see, the DER encoder does not write into some sort of buffer
1098or something. It just invokes the custom function (possible, multiple
1099times) which would save the data into appropriate storage. The optional
1100argument <I>app_key</I> is opaque for the DER encoder code and just
1101used by <I>_write_stream()</I> as the pointer to the appropriate
1102output stream to be used.
1103
1104<P>
1105If the custom write function is not given (passed as 0), then the
1106DER encoder will essentially do the same thing (i.e., encode the data)
1107but no callbacks will be invoked (so the data goes nowhere). It may
1108prove useful to determine the size of the structure's encoding before
Lev Walkin26587ab2004-08-23 15:12:04 +00001109actually doing the encoding<A NAME="tex2html9"
Lev Walkin28c89eb2004-09-30 22:45:58 +00001110 HREF="#foot329"><SUP>4.3</SUP></A>.
Lev Walkineb97a702004-08-07 06:03:15 +00001111
1112<P>
1113Please look into der_encoder.h for the precise definition of der_encode()
1114and related types.
1115
1116<P>
1117
Lev Walkin683f9b72004-09-26 13:41:45 +00001118<H2><A NAME="SECTION03333000000000000000"></A><A NAME="sub:Encoding-XER"></A><BR>
1119Encoding XER
1120</H2>
1121
1122<P>
1123The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
1124Markup Language, a text-based format for information exchange. The
1125encoder routine API comes in two flavors: stdio-based and callback-based.
1126With the callback-based encoder, the encoding process is very similar
1127to the DER one, described in Section sub:Encoding-DER. The
1128following example uses the definition of write_stream() from up there.
1129
1130<P>
1131
1132<BLOCKQUOTE><PRE>
1133/*
1134&nbsp;*&nbsp;This&nbsp;procedure&nbsp;generates&nbsp;the&nbsp;XML&nbsp;document
1135&nbsp;*&nbsp;by&nbsp;invoking&nbsp;the&nbsp;XER&nbsp;encoder.
1136&nbsp;*&nbsp;NOTE:&nbsp;Do&nbsp;not&nbsp;copy&nbsp;this&nbsp;code&nbsp;verbatim!
1137&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;the&nbsp;stdio&nbsp;output&nbsp;is&nbsp;necessary,
1138&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;the&nbsp;xer_fprint()&nbsp;procedure&nbsp;instead.
1139&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;See&nbsp;Section&nbsp;sub:Printing-the-target.
1140&nbsp;*/
1141int
1142print_as_XML(FILE&nbsp;*ostream,&nbsp;Rectangle_t&nbsp;*rect)&nbsp;{
1143&nbsp;&nbsp;&nbsp;&nbsp;asn_enc_rval_t&nbsp;er;&nbsp;&nbsp;/*&nbsp;Encoder&nbsp;return&nbsp;value&nbsp;*/
1144&nbsp;
Lev Walkinc500b3e2004-09-29 13:37:15 +00001145&nbsp;&nbsp;&nbsp;&nbsp;er&nbsp;=&nbsp;xer_encode(&amp;asn_DEF_Rect,&nbsp;rect,
Lev Walkin683f9b72004-09-26 13:41:45 +00001146&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XER_F_BASIC,&nbsp;/*&nbsp;BASIC-XER&nbsp;or&nbsp;CANONICAL-XER&nbsp;*/
1147&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_stream,&nbsp;ostream);
1148&nbsp;
1149&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(er.encoded&nbsp;==&nbsp;-1)&nbsp;?&nbsp;-1&nbsp;:&nbsp;0;
1150}
1151</PRE>
1152</BLOCKQUOTE>
1153Please look into xer_encoder.h for the precise definition of xer_encode()
1154and related types.
1155
1156<P>
1157See Section [<A HREF="#sub:Printing-the-target">Printing-the-target</A>] for the example of stdio-based
1158XML encoder and other pretty-printing suggestions.
1159
1160<P>
1161
1162<H2><A NAME="SECTION03334000000000000000"></A><A NAME="sub:Validating-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001163Validating the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +00001164</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001165
1166<P>
1167Sometimes the target structure needs to be validated. For example,
1168if the structure was created by the application (as opposed to being
1169decoded from some external source), some important information required
1170by the ASN.1 specification might be missing. On the other hand, the
1171successful decoding of the data from some external source does not
1172necessarily mean that the data is fully valid either. It might well
1173be the case that the specification describes some subtype constraints
1174that were not taken into account during decoding, and it would actually
1175be useful to perform the last check when the data is ready to be encoded
1176or when the data has just been decoded to ensure its validity according
1177to some stricter rules.
1178
1179<P>
1180The asn_check_constraints() function checks the type for various
1181implicit and explicit constraints. It is recommended to use asn_check_constraints()
1182function after each decoding and before each encoding.
1183
1184<P>
1185Please look into constraints.h for the precise definition of asn_check_constraints()
1186and related types.
1187
1188<P>
1189
Lev Walkin683f9b72004-09-26 13:41:45 +00001190<H2><A NAME="SECTION03335000000000000000"></A><A NAME="sub:Printing-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001191Printing the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +00001192</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001193
1194<P>
1195There are two ways to print the target structure: either invoke the
1196print_struct member of the ASN.1 type descriptor, or using the asn_fprint()
1197function, which is a simpler wrapper of the former:
1198
1199<P>
1200
Lev Walkin26587ab2004-08-23 15:12:04 +00001201<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +00001202asn_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect);
Lev Walkineb97a702004-08-07 06:03:15 +00001203</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001204</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001205Please look into constr_TYPE.h for the precise definition of asn_fprint()
1206and related types.
1207
1208<P>
Lev Walkin683f9b72004-09-26 13:41:45 +00001209Another practical alternative to this custom format printing would
1210be to invoke XER encoder. The default BASIC-XER encoder performs reasonable
1211formatting for the output to be useful and human readable. To invoke
1212the XER decoder in a manner similar to asn_fprint(), use the xer_fprint()
1213call:
Lev Walkineb97a702004-08-07 06:03:15 +00001214
Lev Walkin683f9b72004-09-26 13:41:45 +00001215<P>
1216
1217<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +00001218xer_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect);
Lev Walkin683f9b72004-09-26 13:41:45 +00001219</PRE>
1220</BLOCKQUOTE>
1221See Section sub:Encoding-XER for XML-related details.
1222
1223<P>
1224
1225<H2><A NAME="SECTION03336000000000000000"></A><A NAME="sub:Freeing-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001226Freeing the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +00001227</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001228
1229<P>
1230Freeing the structure is slightly more complex than it may seem to.
1231When the ASN.1 structure is freed, all the members of the structure
1232and their submembers etc etc are recursively freed too. But it might
1233not be feasible to free the structure itself. Consider the following
1234case:
1235
1236<P>
1237
Lev Walkin26587ab2004-08-23 15:12:04 +00001238<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +00001239struct&nbsp;my_figure&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;custom&nbsp;structure&nbsp;*/
1240&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;*/
1241&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;type&nbsp;is&nbsp;generated&nbsp;by&nbsp;the&nbsp;ASN.1&nbsp;compiler&nbsp;*/
1242&nbsp;&nbsp;&nbsp;&nbsp;<I>Rectangle_t&nbsp;rect;</I>
1243&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;other&nbsp;members&nbsp;of&nbsp;the&nbsp;structure&nbsp;*/
1244};
Lev Walkineb97a702004-08-07 06:03:15 +00001245</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001246</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001247In this example, the application programmer defined a custom structure
1248with one ASN.1-derived member (rect). This member is not a reference
1249to the Rectangle_t, but an in-place inclusion of the Rectangle_t
1250structure. If the freeing is necessary, the usual procedure of freeing
1251everything must not be applied to the &amp;rect pointer itself, because
1252it does not point to the memory block directly allocated by memory
1253allocation routine, but instead lies within such a block allocated
1254for my_figure structure.
1255
1256<P>
1257To solve this problem, the free_struct routine has the additional
1258argument (besides the intuitive type descriptor and target structure
1259pointers), which is the flag specifying whether the outer pointer
1260itself must be freed (0, default) or it should be left intact (non-zero
1261value).
1262
1263<P>
1264
Lev Walkin26587ab2004-08-23 15:12:04 +00001265<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +00001266/*&nbsp;Rectangle_t&nbsp;is&nbsp;defined&nbsp;within&nbsp;my_figure&nbsp;*/
1267struct&nbsp;my_figure&nbsp;*mf&nbsp;=&nbsp;<B>...</B>;
1268/*
1269&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_td
1270&nbsp;*&nbsp;without&nbsp;freeing&nbsp;the&nbsp;mf-&gt;rect&nbsp;pointer
1271&nbsp;*/
Lev Walkinc500b3e2004-09-29 13:37:15 +00001272asn_DEF_Rectangle-&gt;free_struct(
1273&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 +00001274&nbsp;
1275/*&nbsp;Rectangle_t&nbsp;is&nbsp;a&nbsp;stand-alone&nbsp;pointer&nbsp;*/
1276Rectangle_t&nbsp;*rect&nbsp;=&nbsp;<B>...</B>;
1277/*
1278&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_t
1279&nbsp;*&nbsp;and&nbsp;freeing&nbsp;the&nbsp;rect&nbsp;pointer
1280&nbsp;*/
Lev Walkinc500b3e2004-09-29 13:37:15 +00001281asn_DEF_Rectangle-&gt;free_struct(
1282&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 +00001283</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001284</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001285It is safe to invoke the <I>free_struct</I> function with the target
1286structure pointer set to 0 (NULL), the function will do nothing.
1287
1288<P>
1289
1290<H2><A NAME="SECTION04000000000000000000">
1291Bibliography</A>
Lev Walkin26587ab2004-08-23 15:12:04 +00001292</H2><DL COMPACT><DD><P></P><DT><A NAME="ASN1C">ASN1C</A>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001293<DD>The OpenSource ASN.1 Compiler. <A HREF=http://lionet.info/asn1c/>http://lionet.info/asn1c/</A>
Lev Walkin26587ab2004-08-23 15:12:04 +00001294<P></P><DT><A NAME="Dub00">Dub00</A>
Lev Walkineb97a702004-08-07 06:03:15 +00001295<DD>Olivier Dubuisson - <I>ASN.1 Communication between heterogeneous
Lev Walkin28c89eb2004-09-30 22:45:58 +00001296systems</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 +00001297ISBN:0-12-6333361-0.
1298<P></P><DT><A NAME="ITU-T_ASN.1">ITU-T/ASN.1</A>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001299<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 +00001300
1301<P>
1302<BR><HR><H4>Footnotes</H4>
1303<DL>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001304<DT><A NAME="foot159">... supported</A><A
Lev Walkineb97a702004-08-07 06:03:15 +00001305 HREF="asn1c-usage.html#tex2html1"><SUP>2.1</SUP></A></DT>
Lev Walkin26587ab2004-08-23 15:12:04 +00001306<DD>C++ is ''supported'' too, as long as an class-based approach is
1307not a definitive factor.
Lev Walkineb97a702004-08-07 06:03:15 +00001308
1309</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001310<DT><A NAME="foot419">... this</A><A
Lev Walkineb97a702004-08-07 06:03:15 +00001311 HREF="asn1c-usage.html#tex2html2"><SUP>2.2</SUP></A></DT>
Lev Walkin6faa68e2004-09-17 08:35:02 +00001312<DD><I>-fnative-types</I> compiler option is used to produce basic C <I>int</I>
1313types instead of infinite width INTEGER_t structures. See <A HREF=#Table1>Table 1</A>.
Lev Walkineb97a702004-08-07 06:03:15 +00001314
1315</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001316<DT><A NAME="foot166">... binary</A><A
Lev Walkineb97a702004-08-07 06:03:15 +00001317 HREF="asn1c-usage.html#tex2html3"><SUP>2.3</SUP></A></DT>
1318<DD>BER, CER and DER encodings are binary. However, the XER encoding is
1319text (XML) based.
1320
1321</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001322<DT><A NAME="foot420">...asn1c</A><A
Lev Walkin26587ab2004-08-23 15:12:04 +00001323 HREF="asn1c-usage.html#tex2html4"><SUP>3.1</SUP></A></DT>
1324<DD>The 1 symbol in asn<B>1</B>c is a digit, not an ''ell'' letter.
1325
1326</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001327<DT><A NAME="foot421">... specification</A><A
Lev Walkin26587ab2004-08-23 15:12:04 +00001328 HREF="asn1c-usage.html#tex2html5"><SUP>3.2</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001329<DD>This is probably <B>not</B> what you want to try out right now -
Lev Walkin26587ab2004-08-23 15:12:04 +00001330read through the rest of this chapter to find out about <B>-P</B>
1331and <B>-R</B> options.
Lev Walkineb97a702004-08-07 06:03:15 +00001332
1333</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001334<DT><A NAME="foot427">...that&nbsp;simple</A><A
Lev Walkin26587ab2004-08-23 15:12:04 +00001335 HREF="asn1c-usage.html#tex2html7"><SUP>4.1</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001336<DD>Provided that you've also created a .c file with the <I>int main()</I>
1337routine.
1338
1339</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001340<DT><A NAME="foot253">...restartable</A><A
Lev Walkin26587ab2004-08-23 15:12:04 +00001341 HREF="asn1c-usage.html#tex2html8"><SUP>4.2</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001342<DD>Restartable means that if the decoder encounters the end of the buffer,
1343it will fail, but may later be invoked again with the rest of the
1344buffer to continue decoding.
1345
1346</DD>
Lev Walkin28c89eb2004-09-30 22:45:58 +00001347<DT><A NAME="foot329">... encoding</A><A
Lev Walkin26587ab2004-08-23 15:12:04 +00001348 HREF="asn1c-usage.html#tex2html9"><SUP>4.3</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001349<DD>It is actually faster too: the encoder might skip over some computations
1350which aren't important for the size determination.
1351
1352</DD>
1353</DL><BR><HR>
1354<ADDRESS>
1355Lev Walkin
Lev Walkin28c89eb2004-09-30 22:45:58 +000013562004-09-30
Lev Walkineb97a702004-08-07 06:03:15 +00001357</ADDRESS>
1358</BODY>
1359</HTML>