blob: ded6c758337d3c4e522d2acbd8a22672d20e4806 [file] [log] [blame]
vlm9ca15042004-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>
vlm03eb4e12004-09-08 03:10:54 +00009<TITLE>Using the Open ASN.1 Compiler</TITLE>
10<META NAME="description" CONTENT="Using the Open ASN.1 Compiler">
vlm9ca15042004-08-07 06:03:15 +000011<META NAME="asn1c, ASN.1, free, compiler, BER, DER, PER, XER\">
12
vlm9ca15042004-08-07 06:03:15 +000013
14
15</HEAD>
16
17<BODY >
18
19<P>
20
21<P>
22
23<P>
vlm03eb4e12004-09-08 03:10:54 +000024<H1 ALIGN="CENTER">Using the Open ASN.1 Compiler</H1><DIV>
vlm9ca15042004-08-07 06:03:15 +000025
26<P ALIGN="CENTER"><STRONG>Lev Walkin &lt;vlm@lionet.info&gt;</STRONG></P>
27</DIV>
vlm763ac412004-09-26 13:41:45 +000028<!-- MATH
29 $Revision$
30 -->
vlmfcec8e52004-08-23 15:12:04 +000031
32<P>
vlm9ca15042004-08-07 06:03:15 +000033<BR>
34
35<H2><A NAME="SECTION01000000000000000000">
36Contents</A>
37</H2>
38<!--Table of Contents-->
39
40<UL>
vlm763ac412004-09-26 13:41:45 +000041<LI><A NAME="tex2html46"
vlm9ca15042004-08-07 06:03:15 +000042 HREF="asn1c-usage.html#SECTION02000000000000000000">ASN.1 Basics</A>
43<UL>
vlm763ac412004-09-26 13:41:45 +000044<LI><A NAME="tex2html47"
vlm9ca15042004-08-07 06:03:15 +000045 HREF="asn1c-usage.html#SECTION02100000000000000000">Abstract Syntax Notation: ASN.1</A>
46<UL>
vlm763ac412004-09-26 13:41:45 +000047<LI><A NAME="tex2html48"
vlm9ca15042004-08-07 06:03:15 +000048 HREF="asn1c-usage.html#SECTION02110000000000000000">Some of the ASN.1 Basic Types</A>
49<UL>
vlmfcec8e52004-08-23 15:12:04 +000050<LI><A NAME="tex2html49"
vlm763ac412004-09-26 13:41:45 +000051 HREF="asn1c-usage.html#SECTION02111000000000000000">The BOOLEAN type</A>
vlmfcec8e52004-08-23 15:12:04 +000052<LI><A NAME="tex2html50"
vlm763ac412004-09-26 13:41:45 +000053 HREF="asn1c-usage.html#SECTION02112000000000000000">The INTEGER type</A>
vlmfcec8e52004-08-23 15:12:04 +000054<LI><A NAME="tex2html51"
vlm763ac412004-09-26 13:41:45 +000055 HREF="asn1c-usage.html#SECTION02113000000000000000">The ENUMERATED type</A>
vlmfcec8e52004-08-23 15:12:04 +000056<LI><A NAME="tex2html52"
vlm763ac412004-09-26 13:41:45 +000057 HREF="asn1c-usage.html#SECTION02114000000000000000">The OCTET STRING type</A>
vlmfcec8e52004-08-23 15:12:04 +000058<LI><A NAME="tex2html53"
vlm763ac412004-09-26 13:41:45 +000059 HREF="asn1c-usage.html#SECTION02115000000000000000">The OBJECT IDENTIFIER type</A>
60<LI><A NAME="tex2html54"
vlm9ca15042004-08-07 06:03:15 +000061 HREF="asn1c-usage.html#SECTION02116000000000000000">The RELATIVE-OID type</A>
62</UL>
vlm763ac412004-09-26 13:41:45 +000063<LI><A NAME="tex2html55"
vlm9ca15042004-08-07 06:03:15 +000064 HREF="asn1c-usage.html#SECTION02120000000000000000">Some of the ASN.1 String Types</A>
65<UL>
vlmfcec8e52004-08-23 15:12:04 +000066<LI><A NAME="tex2html56"
vlm763ac412004-09-26 13:41:45 +000067 HREF="asn1c-usage.html#SECTION02121000000000000000">The IA5String type</A>
vlmfcec8e52004-08-23 15:12:04 +000068<LI><A NAME="tex2html57"
vlm763ac412004-09-26 13:41:45 +000069 HREF="asn1c-usage.html#SECTION02122000000000000000">The UTF8String type</A>
vlmfcec8e52004-08-23 15:12:04 +000070<LI><A NAME="tex2html58"
vlm763ac412004-09-26 13:41:45 +000071 HREF="asn1c-usage.html#SECTION02123000000000000000">The NumericString type</A>
vlmfcec8e52004-08-23 15:12:04 +000072<LI><A NAME="tex2html59"
vlm763ac412004-09-26 13:41:45 +000073 HREF="asn1c-usage.html#SECTION02124000000000000000">The PrintableString type</A>
74<LI><A NAME="tex2html60"
vlm9ca15042004-08-07 06:03:15 +000075 HREF="asn1c-usage.html#SECTION02125000000000000000">The VisibleString type</A>
76</UL>
vlm763ac412004-09-26 13:41:45 +000077<LI><A NAME="tex2html61"
vlm9ca15042004-08-07 06:03:15 +000078 HREF="asn1c-usage.html#SECTION02130000000000000000">ASN.1 Constructed Types</A>
79<UL>
vlmfcec8e52004-08-23 15:12:04 +000080<LI><A NAME="tex2html62"
vlm763ac412004-09-26 13:41:45 +000081 HREF="asn1c-usage.html#SECTION02131000000000000000">The SEQUENCE type</A>
vlmfcec8e52004-08-23 15:12:04 +000082<LI><A NAME="tex2html63"
vlm763ac412004-09-26 13:41:45 +000083 HREF="asn1c-usage.html#SECTION02132000000000000000">The SET type</A>
vlmfcec8e52004-08-23 15:12:04 +000084<LI><A NAME="tex2html64"
vlm763ac412004-09-26 13:41:45 +000085 HREF="asn1c-usage.html#SECTION02133000000000000000">The CHOICE type</A>
vlmfcec8e52004-08-23 15:12:04 +000086<LI><A NAME="tex2html65"
vlm763ac412004-09-26 13:41:45 +000087 HREF="asn1c-usage.html#SECTION02134000000000000000">The SEQUENCE OF type</A>
88<LI><A NAME="tex2html66"
vlm9ca15042004-08-07 06:03:15 +000089 HREF="asn1c-usage.html#SECTION02135000000000000000">The SET OF type</A>
90</UL>
91</UL>
92</UL><BR>
vlm763ac412004-09-26 13:41:45 +000093<LI><A NAME="tex2html67"
vlm9ca15042004-08-07 06:03:15 +000094 HREF="asn1c-usage.html#SECTION03000000000000000000">Using the ASN.1 Compiler</A>
95<UL>
vlmfcec8e52004-08-23 15:12:04 +000096<LI><A NAME="tex2html68"
vlm763ac412004-09-26 13:41:45 +000097 HREF="asn1c-usage.html#SECTION03100000000000000000">Introduction to the ASN.1 Compiler</A>
vlmfcec8e52004-08-23 15:12:04 +000098<LI><A NAME="tex2html69"
vlm763ac412004-09-26 13:41:45 +000099 HREF="asn1c-usage.html#SECTION03200000000000000000">Quick start</A>
100<LI><A NAME="tex2html70"
vlmfcec8e52004-08-23 15:12:04 +0000101 HREF="asn1c-usage.html#SECTION03300000000000000000">Using the ASN.1 Compiler</A>
vlm9ca15042004-08-07 06:03:15 +0000102<UL>
vlmfcec8e52004-08-23 15:12:04 +0000103<LI><A NAME="tex2html71"
vlm763ac412004-09-26 13:41:45 +0000104 HREF="asn1c-usage.html#SECTION03310000000000000000">Command-line options</A>
vlmfcec8e52004-08-23 15:12:04 +0000105<LI><A NAME="tex2html72"
vlm763ac412004-09-26 13:41:45 +0000106 HREF="asn1c-usage.html#SECTION03320000000000000000">Recognizing compiler output</A>
107<LI><A NAME="tex2html73"
vlmfcec8e52004-08-23 15:12:04 +0000108 HREF="asn1c-usage.html#SECTION03330000000000000000">Invoking the ASN.1 helper code from the application</A>
vlm9ca15042004-08-07 06:03:15 +0000109<UL>
vlmfcec8e52004-08-23 15:12:04 +0000110<LI><A NAME="tex2html74"
vlm763ac412004-09-26 13:41:45 +0000111 HREF="asn1c-usage.html#SECTION03331000000000000000">Decoding BER</A>
vlmfcec8e52004-08-23 15:12:04 +0000112<LI><A NAME="tex2html75"
vlm763ac412004-09-26 13:41:45 +0000113 HREF="asn1c-usage.html#SECTION03332000000000000000">Encoding DER</A>
vlmfcec8e52004-08-23 15:12:04 +0000114<LI><A NAME="tex2html76"
vlm763ac412004-09-26 13:41:45 +0000115 HREF="asn1c-usage.html#SECTION03333000000000000000">Encoding XER</A>
vlmfcec8e52004-08-23 15:12:04 +0000116<LI><A NAME="tex2html77"
vlm763ac412004-09-26 13:41:45 +0000117 HREF="asn1c-usage.html#SECTION03334000000000000000">Validating the target structure</A>
118<LI><A NAME="tex2html78"
119 HREF="asn1c-usage.html#SECTION03335000000000000000">Printing the target structure</A>
120<LI><A NAME="tex2html79"
121 HREF="asn1c-usage.html#SECTION03336000000000000000">Freeing the target structure</A>
vlm9ca15042004-08-07 06:03:15 +0000122</UL>
123</UL>
124</UL><BR>
vlm763ac412004-09-26 13:41:45 +0000125<LI><A NAME="tex2html80"
vlm9ca15042004-08-07 06:03:15 +0000126 HREF="asn1c-usage.html#SECTION04000000000000000000">Bibliography</A>
127</UL>
128<!--End of Table of Contents-->
129
130<P>
131
vlmfcec8e52004-08-23 15:12:04 +0000132<P>
133
vlm9ca15042004-08-07 06:03:15 +0000134<H1><A NAME="SECTION02000000000000000000">
135ASN.1 Basics</A>
136</H1>
137
138<P>
139
140<H1><A NAME="SECTION02100000000000000000">
141Abstract Syntax Notation: ASN.1</A>
142</H1>
143
144<P>
145<I>This chapter defines some basic ASN.1 concepts and describes
146several most widely used types. It is by no means an authoritative
147or complete reference. For more complete ASN.1 description, please
148refer to Olivier Dubuisson's book [<A
149 HREF="asn1c-usage.html#Dub00">Dub00</A>] or the ASN.1 body
150of standards itself [<A
151 HREF="asn1c-usage.html#ITU-T_ASN.1">ITU-T/ASN.1</A>].</I>
152
153<P>
154The Abstract Syntax Notation One is used to formally describe the
155semantics of data transmitted across the network. Two communicating
156parties may have different formats of their native data types (i.e.
157number of bits in the integer type), thus it is important to have
158a way to describe the data in a manner which is independent from the
159particular machine's representation. The ASN.1 specifications is used
160to achieve one or more of the following:
161
162<P>
163
164<UL>
165<LI>The specification expressed in the ASN.1 notation is a formal and
166precise way to communicate the data semantics to human readers;
167</LI>
168<LI>The ASN.1 specifications may be used as input for automatic compilers
169which produce the code for some target language (C, C++, Java, etc)
170to encode and decode the data according to some encoding rules (which
171are also defined by the ASN.1 standard).
172</LI>
173</UL>
174Consider the following example:
175
176<P>
177
vlmfcec8e52004-08-23 15:12:04 +0000178<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000179Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
180&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,
181&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER
182}
vlm9ca15042004-08-07 06:03:15 +0000183</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000184</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000185This ASN.1 specification describes a constructed type, <I>Rectangle</I>,
186containing two integer fields. This specification may tell the reader
187that there is this kind of data structure and that some entity may
188be prepared to send or receive it. The question on <I>how</I> that
189entity is going to send or receive the <I>encoded data</I> is outside
190the scope of ASN.1. For example, this data structure may be encoded
191according to some encoding rules and sent to the destination using
192the TCP protocol. The ASN.1 specifies several ways of encoding (or
193''serializing'', or ''marshaling'') the data: BER, CER, DER and
194XER, some of them which will be described later.
195
196<P>
197The complete specification must be wrapped in a module, which looks
198like this:
199
200<P>
201
vlmfcec8e52004-08-23 15:12:04 +0000202<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000203UsageExampleModule1
204&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;iso&nbsp;org(3)&nbsp;dod(6)&nbsp;internet(1)&nbsp;private(4)
205&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enterprise(1)&nbsp;spelio(9363)&nbsp;software(1)
206&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn1c(5)&nbsp;docs(2)&nbsp;usage(1)&nbsp;1&nbsp;}&nbsp;
vlmfcec8e52004-08-23 15:12:04 +0000207&nbsp;&nbsp;&nbsp;&nbsp;DEFINITIONS&nbsp;AUTOMATIC&nbsp;TAGS&nbsp;::=
vlm9ca15042004-08-07 06:03:15 +0000208BEGIN
209&nbsp;
210--&nbsp;This&nbsp;is&nbsp;a&nbsp;comment&nbsp;which&nbsp;describes&nbsp;nothing.
211Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
212&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;Height&nbsp;of&nbsp;the&nbsp;rectangle
vlmfcec8e52004-08-23 15:12:04 +0000213&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
vlm9ca15042004-08-07 06:03:15 +0000214}
215&nbsp;
216END
vlm9ca15042004-08-07 06:03:15 +0000217</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000218</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000219The module header consists of module name (UsageExampleModule1), the
vlmfcec8e52004-08-23 15:12:04 +0000220module object identifier ({...}), a keyword ''DEFINITIONS'', a
221set of module flags (AUTOMATIC TAGS) and ''::= BEGIN''. The module
222ends with an ''END'' statement.
vlm9ca15042004-08-07 06:03:15 +0000223
224<P>
225
226<H1><A NAME="SECTION02110000000000000000">
227Some of the ASN.1 Basic Types</A>
228</H1>
229
230<P>
231
232<H2><A NAME="SECTION02111000000000000000">
233The BOOLEAN type</A>
234</H2>
235
236<P>
237The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF
238or a similar kind of two-way choice.
239
240<P>
241
242<H2><A NAME="SECTION02112000000000000000">
243The INTEGER type</A>
244</H2>
245
246<P>
247The INTEGER type is a signed natural number type without any restrictions
248on its size. If the automatic checking on INTEGER value bounds are
249necessary, the subtype constraints must be used.
250
251<P>
252
vlmfcec8e52004-08-23 15:12:04 +0000253<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000254SimpleInteger&nbsp;::=&nbsp;INTEGER
vlmfcec8e52004-08-23 15:12:04 +0000255&nbsp;
vlm9ca15042004-08-07 06:03:15 +0000256--&nbsp;An&nbsp;integer&nbsp;with&nbsp;a&nbsp;very&nbsp;limited&nbsp;range
vlm4a168ae2004-09-29 13:37:15 +0000257SmallPositiveInt&nbsp;::=&nbsp;INTEGER&nbsp;(0..127)
vlmfcec8e52004-08-23 15:12:04 +0000258&nbsp;
vlm9ca15042004-08-07 06:03:15 +0000259--&nbsp;Integer,&nbsp;negative
260NegativeInt&nbsp;::=&nbsp;INTEGER&nbsp;(MIN..0)
vlm9ca15042004-08-07 06:03:15 +0000261</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000262</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000263
264<P>
265
266<H2><A NAME="SECTION02113000000000000000">
267The ENUMERATED type</A>
268</H2>
269
270<P>
271The ENUMERATED type is semantically equivalent to the INTEGER type
272with some integer values explicitly named.
273
274<P>
275
vlmfcec8e52004-08-23 15:12:04 +0000276<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000277FruitId&nbsp;::=&nbsp;ENUMERATED&nbsp;{&nbsp;apple(1),&nbsp;orange(2)&nbsp;}
vlmfcec8e52004-08-23 15:12:04 +0000278&nbsp;
vlm9ca15042004-08-07 06:03:15 +0000279--&nbsp;The&nbsp;numbers&nbsp;in&nbsp;braces&nbsp;are&nbsp;optional,
vlmfcec8e52004-08-23 15:12:04 +0000280--&nbsp;the&nbsp;enumeration&nbsp;can&nbsp;be&nbsp;performed
vlm9ca15042004-08-07 06:03:15 +0000281--&nbsp;automatically&nbsp;by&nbsp;the&nbsp;compiler
282ComputerOSType&nbsp;::=&nbsp;ENUMERATED&nbsp;{
283&nbsp;&nbsp;&nbsp;&nbsp;FreeBSD,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;will&nbsp;be&nbsp;0
284&nbsp;&nbsp;&nbsp;&nbsp;Windows,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;will&nbsp;be&nbsp;1
285&nbsp;&nbsp;&nbsp;&nbsp;Solaris(5),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;will&nbsp;remain&nbsp;5
286&nbsp;&nbsp;&nbsp;&nbsp;Linux,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;will&nbsp;be&nbsp;6
287&nbsp;&nbsp;&nbsp;&nbsp;MacOS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;will&nbsp;be&nbsp;7
288}
vlm9ca15042004-08-07 06:03:15 +0000289</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000290</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000291
292<P>
293
294<H2><A NAME="SECTION02114000000000000000">
295The OCTET STRING type</A>
296</H2>
297
298<P>
299This type models the sequence of 8-bit bytes. This may be used to
300transmit some opaque data or data serialized by other types of encoders
301(i.e. video file, photo picture, etc).
302
303<P>
304
305<H2><A NAME="SECTION02115000000000000000">
306The OBJECT IDENTIFIER type</A>
307</H2>
308
309<P>
310The OBJECT IDENTIFIER is used to represent the unique identifier of
311any object, starting from the very root of the registration tree.
312If your organization needs to uniquely identify something (a router,
313a room, a person, a standard, or whatever), you are encouraged to
314get your own identification subtree at http://www.iana.org/protocols/forms.htm.
315
316<P>
317For example, the very first ASN.1 module in this document has the
318following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
319
320<P>
321
vlmfcec8e52004-08-23 15:12:04 +0000322<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000323ExampleOID&nbsp;::=&nbsp;OBJECT&nbsp;IDENTIFIER
vlmfcec8e52004-08-23 15:12:04 +0000324&nbsp;
vlm9ca15042004-08-07 06:03:15 +0000325usageExampleModule1-oid&nbsp;ExampleOID
326&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;}
vlmfcec8e52004-08-23 15:12:04 +0000327&nbsp;
vlm9ca15042004-08-07 06:03:15 +0000328--&nbsp;An&nbsp;identifier&nbsp;of&nbsp;the&nbsp;Internet.
329internet-id&nbsp;OBJECT&nbsp;IDENTIFIER
330&nbsp;&nbsp;::=&nbsp;{&nbsp;iso(1)&nbsp;identified-organization(3)
331&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dod(6)&nbsp;internet(1)&nbsp;}
vlm9ca15042004-08-07 06:03:15 +0000332</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000333</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000334As you see, names are optional.
335
336<P>
337
338<H2><A NAME="SECTION02116000000000000000">
339The RELATIVE-OID type</A>
340</H2>
341
342<P>
343The RELATIVE-OID type has the semantics of a subtree of an OBJECT
344IDENTIFIER. There may be no need to repeat the whole sequence of numbers
345from the root of the registration tree where the only thing of interest
346is some of the tree's subsequence.
347
348<P>
349
vlmfcec8e52004-08-23 15:12:04 +0000350<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000351this-document&nbsp;RELATIVE-OID&nbsp;::=&nbsp;{&nbsp;docs(2)&nbsp;usage(1)&nbsp;}
vlmfcec8e52004-08-23 15:12:04 +0000352&nbsp;
vlm9ca15042004-08-07 06:03:15 +0000353this-example&nbsp;RELATIVE-OID&nbsp;::=&nbsp;{
354&nbsp;&nbsp;&nbsp;&nbsp;this-document&nbsp;assorted-examples(0)&nbsp;this-example(1)&nbsp;}
vlm9ca15042004-08-07 06:03:15 +0000355</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000356</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000357
358<P>
359
360<H1><A NAME="SECTION02120000000000000000">
361Some of the ASN.1 String Types</A>
362</H1>
363
364<P>
365
366<H2><A NAME="SECTION02121000000000000000">
367The IA5String type</A>
368</H2>
369
370<P>
371This is essentially the ASCII, with 128 character codes available
vlmfcec8e52004-08-23 15:12:04 +0000372(7 lower bits of an 8-bit byte).
vlm9ca15042004-08-07 06:03:15 +0000373
374<P>
375
376<H2><A NAME="SECTION02122000000000000000">
377The UTF8String type</A>
378</H2>
379
380<P>
381This is the character string which encodes the full Unicode range
382(4 bytes) using multibyte character sequences.
383
384<P>
385
386<H2><A NAME="SECTION02123000000000000000">
387The NumericString type</A>
388</H2>
389
390<P>
391This type represents the character string with the alphabet consisting
392of numbers (''0'' to ''9'') and a space.
393
394<P>
395
396<H2><A NAME="SECTION02124000000000000000">
397The PrintableString type</A>
398</H2>
399
400<P>
401The character string with the following alphabet: space, ''<B>'</B>''
402(single quote), ''<B>(</B>'', ''<B>)</B>'', ''<B>+</B>'',
vlm4a168ae2004-09-29 13:37:15 +0000403''<B>,</B>'' (comma), ''<B>-</B>'', ''<B>.</B>'', ''<B>/</B>'',
vlm9ca15042004-08-07 06:03:15 +0000404digits (''0'' to ''9''), ''<B>:</B>'', ''<B>=</B>'', ''<B>?</B>'',
405upper-case and lower-case letters (''A'' to ''Z'' and ''a''
vlm4a168ae2004-09-29 13:37:15 +0000406to ''z'').
vlm9ca15042004-08-07 06:03:15 +0000407
408<P>
409
410<H2><A NAME="SECTION02125000000000000000">
411The VisibleString type</A>
412</H2>
413
414<P>
415The character string with the alphabet which is more or less a subset
vlm4a168ae2004-09-29 13:37:15 +0000416of ASCII between the space and the ''<B>~</B>''
417symbol (tilde).
418
419<P>
vlmfcec8e52004-08-23 15:12:04 +0000420Alternatively, the alphabet may be described as the PrintableString
421alphabet presented earlier, plus the following characters: ''<B>!</B>'',
422''<B>''</B>'', ''<B>#</B>'', ''<B>$</B>'', ''<B>%</B>'',
423''<B>&amp;</B>'', ''<B>*</B>'', ''<B>;</B>'', ''<B>&lt;</B>'',
424''<B>&gt;</B>'', ''<B>[</B>'', ''<B>&#92;</B>'',
425''<B>]</B>'', ''<B>&#94;</B>'', ''<B>_</B>'',
426''<B>`</B>'' (single left quote), ''<B>{</B>'', ''<B>|</B>'',
427''<B>}</B>'', ''<B>~</B>''.
vlm9ca15042004-08-07 06:03:15 +0000428
429<P>
430
431<H1><A NAME="SECTION02130000000000000000">
432ASN.1 Constructed Types</A>
433</H1>
434
435<P>
436
437<H2><A NAME="SECTION02131000000000000000">
438The SEQUENCE type</A>
439</H2>
440
441<P>
442This is an ordered collection of other simple or constructed types.
443The SEQUENCE constructed type resembles the C ''struct'' statement.
444
445<P>
446
vlmfcec8e52004-08-23 15:12:04 +0000447<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000448Address&nbsp;::=&nbsp;SEQUENCE&nbsp;{
449&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;The&nbsp;apartment&nbsp;number&nbsp;may&nbsp;be&nbsp;omitted
450&nbsp;&nbsp;&nbsp;&nbsp;apartmentNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NumericString&nbsp;OPTIONAL,
451&nbsp;&nbsp;&nbsp;&nbsp;streetName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
452&nbsp;&nbsp;&nbsp;&nbsp;cityName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
453&nbsp;&nbsp;&nbsp;&nbsp;stateName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
454&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;This&nbsp;one&nbsp;may&nbsp;be&nbsp;omitted&nbsp;too
455&nbsp;&nbsp;&nbsp;&nbsp;zipNo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NumericString&nbsp;OPTIONAL
456}
vlm9ca15042004-08-07 06:03:15 +0000457</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000458</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000459
460<P>
461
462<H2><A NAME="SECTION02132000000000000000">
463The SET type</A>
464</H2>
465
466<P>
467This is a collection of other simple or constructed types. Ordering
468is not important. The data may arrive in the order which is different
469from the order of specification. Data is encoded in the order not
470necessarily corresponding to the order of specification.
471
472<P>
473
474<H2><A NAME="SECTION02133000000000000000">
475The CHOICE type</A>
476</H2>
477
478<P>
479This type is just a choice between the subtypes specified in it. The
480CHOICE type contains at most one of the subtypes specified, and it
481is always implicitly known which choice is being decoded or encoded.
482This one resembles the C ''union'' statement.
483
484<P>
485The following type defines a response code, which may be either an
486integer code or a boolean ''true''/''false'' code.
487
488<P>
489
vlmfcec8e52004-08-23 15:12:04 +0000490<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000491ResponseCode&nbsp;::=&nbsp;CHOICE&nbsp;{
492&nbsp;&nbsp;&nbsp;&nbsp;intCode&nbsp;&nbsp;&nbsp;&nbsp;INTEGER,
493&nbsp;&nbsp;&nbsp;&nbsp;boolCode&nbsp;&nbsp;&nbsp;BOOLEAN
494}
vlm9ca15042004-08-07 06:03:15 +0000495</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000496</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000497
498<P>
499
500<H2><A NAME="SECTION02134000000000000000">
501The SEQUENCE OF type</A>
502</H2>
503
504<P>
505This one is the list (array) of simple or constructed types:
506
507<P>
508
vlmfcec8e52004-08-23 15:12:04 +0000509<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000510--&nbsp;Example&nbsp;1
511ManyIntegers&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;INTEGER
vlmfcec8e52004-08-23 15:12:04 +0000512&nbsp;
vlm9ca15042004-08-07 06:03:15 +0000513--&nbsp;Example&nbsp;2
514ManyRectangles&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;Rectangle
vlmfcec8e52004-08-23 15:12:04 +0000515&nbsp;
vlm9ca15042004-08-07 06:03:15 +0000516--&nbsp;More&nbsp;complex&nbsp;example:
517--&nbsp;an&nbsp;array&nbsp;of&nbsp;structures&nbsp;defined&nbsp;in&nbsp;place.
518ManyCircles&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;SEQUENCE&nbsp;{
519&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
520&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;}
vlm9ca15042004-08-07 06:03:15 +0000521</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000522</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000523
524<P>
525
526<H2><A NAME="SECTION02135000000000000000">
527The SET OF type</A>
528</H2>
529
530<P>
531The SET OF type models the bag of structures. It resembles the SEQUENCE
532OF type, but the order is not important: i.e. the elements may arrive
533in the order which is not necessarily the same as the in-memory order
534on the remote machines.
535
536<P>
537
vlmfcec8e52004-08-23 15:12:04 +0000538<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000539--&nbsp;A&nbsp;set&nbsp;of&nbsp;structures&nbsp;defined&nbsp;elsewhere
540SetOfApples&nbsp;::&nbsp;SET&nbsp;OF&nbsp;Apple
vlmfcec8e52004-08-23 15:12:04 +0000541&nbsp;
vlm9ca15042004-08-07 06:03:15 +0000542--&nbsp;Set&nbsp;of&nbsp;integers&nbsp;encoding&nbsp;the&nbsp;kind&nbsp;of&nbsp;a&nbsp;fruit
543FruitBag&nbsp;::=&nbsp;SET&nbsp;OF&nbsp;ENUMERATED&nbsp;{&nbsp;apple,&nbsp;orange&nbsp;}
vlm9ca15042004-08-07 06:03:15 +0000544</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000545</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000546
547<P>
548
549<H1><A NAME="SECTION03000000000000000000">
550Using the ASN.1 Compiler</A>
551</H1>
552
553<P>
554
555<H1><A NAME="SECTION03100000000000000000">
556Introduction to the ASN.1 Compiler</A>
557</H1>
558
559<P>
560The purpose of the ASN.1 compiler, of which this document is part,
561is to convert the ASN.1 specifications to some other target language
562(currently, only C is supported<A NAME="tex2html1"
vlm4a168ae2004-09-29 13:37:15 +0000563 HREF="#foot145"><SUP>2.1</SUP></A>). The compiler reads the specification and emits a series of target
vlm9ca15042004-08-07 06:03:15 +0000564language structures and surrounding maintenance code. For example,
565the C structure which may be created by compiler to represent the
566simple <I>Rectangle</I> specification defined earlier in this document,
567may look like this<A NAME="tex2html2"
vlm4a168ae2004-09-29 13:37:15 +0000568 HREF="#foot404"><SUP>2.2</SUP></A>:
vlm9ca15042004-08-07 06:03:15 +0000569
570<P>
571
vlmfcec8e52004-08-23 15:12:04 +0000572<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000573typedef&nbsp;struct&nbsp;Rectangle_s&nbsp;{
574&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;height;
575&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;width;
576}&nbsp;Rectangle_t;
vlm9ca15042004-08-07 06:03:15 +0000577</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000578</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000579This would not be of much value for such a simple specification, so
580the compiler goes further and actually produces the code which fills
vlmfcec8e52004-08-23 15:12:04 +0000581in this structure by parsing the opaque binary<A NAME="tex2html3"
vlm4a168ae2004-09-29 13:37:15 +0000582 HREF="#foot152"><SUP>2.3</SUP></A> data provided in some buffer. It also produces the code that takes
vlm9ca15042004-08-07 06:03:15 +0000583this structure as an argument and performs structure serialization
584by emitting a series of bytes.
585
586<P>
587
vlmfcec8e52004-08-23 15:12:04 +0000588<H1><A NAME="SECTION03200000000000000000">
vlm9ca15042004-08-07 06:03:15 +0000589Quick start</A>
590</H1>
591
592<P>
vlmfcec8e52004-08-23 15:12:04 +0000593After building and installing the compiler, the <I>asn1c</I><A NAME="tex2html4"
vlm4a168ae2004-09-29 13:37:15 +0000594 HREF="#foot405"><SUP>3.1</SUP></A> command may be used to compile the ASN.1 specification<A NAME="tex2html5"
595 HREF="#foot406"><SUP>3.2</SUP></A>:
vlm9ca15042004-08-07 06:03:15 +0000596
597<P>
598
vlmfcec8e52004-08-23 15:12:04 +0000599<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000600asn1c&nbsp;<I>&lt;spec.asn1&gt;</I>
vlm9ca15042004-08-07 06:03:15 +0000601</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000602</BLOCKQUOTE>
603If several specifications contain interdependencies, all of the files
604must be specified altogether:
vlm9ca15042004-08-07 06:03:15 +0000605
606<P>
607
vlmfcec8e52004-08-23 15:12:04 +0000608<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000609asn1c&nbsp;<I>&lt;spec1.asn1&gt;&nbsp;&lt;spec2.asn1&gt;&nbsp;...</I>
vlm9ca15042004-08-07 06:03:15 +0000610</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000611</BLOCKQUOTE>
612The compiler <B>-E</B> and <B>-EF</B> options are used for testing
613the parser and the semantic fixer, respectively. These options will
614instruct the compiler to dump out the parsed (and fixed, if <B>-F</B>
615is involved) ASN.1 specification as it was &#34;understood&#34;
616by the compiler. It might be useful to check whether a particular
617syntactic construction is properly supported by the compiler.
vlm9ca15042004-08-07 06:03:15 +0000618
619<P>
620
vlmfcec8e52004-08-23 15:12:04 +0000621<BLOCKQUOTE><PRE>
622asn1c&nbsp;<B>-EF</B>&nbsp;<I>&lt;spec-to-test.asn1&gt;</I>
vlm9ca15042004-08-07 06:03:15 +0000623</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000624</BLOCKQUOTE>
625The <B>-P</B> option is used to dump the compiled output on the
626screen instead of creating a bunch of .c and .h files on disk in the
627current directory. You would probably want to start with <B>-P</B>
628option instead of creating a mess in your current directory. Another
629option, <B>-R</B>, asks compiler to only generate the files which
630need to be generated, and supress linking in the numerous support
631files.
632
633<P>
634Print the compiled output instead of creating multiple source files:
vlm9ca15042004-08-07 06:03:15 +0000635
636<P>
637
vlmfcec8e52004-08-23 15:12:04 +0000638<BLOCKQUOTE><PRE>
639asn1c&nbsp;<B>-P</B>&nbsp;<I>&lt;spec-to-compile-and-print.asn1&gt;</I>
640</PRE>
641</BLOCKQUOTE>
642
643<P>
644
645<H1><A NAME="SECTION03300000000000000000">
646Using the ASN.1 Compiler</A>
vlm9ca15042004-08-07 06:03:15 +0000647</H1>
648
649<P>
650
vlmfcec8e52004-08-23 15:12:04 +0000651<H1><A NAME="SECTION03310000000000000000">
652Command-line options</A>
653</H1>
654
655<P>
656The <A HREF=#Table1>Table 1</A> summarizes various options affecting
657the compiler's behavior.
658
659<P>
660<BR><P></P>
vlm4a168ae2004-09-29 13:37:15 +0000661<DIV ALIGN="CENTER"><A NAME="409"></A>
vlmfcec8e52004-08-23 15:12:04 +0000662<TABLE>
663<CAPTION><STRONG><A NAME=Table1>Table 1:</A></STRONG>
664The list of asn1c command line options</CAPTION>
665<TR><TD><TABLE COLS=2 BORDER FRAME=BOX RULES=GROUPS>
666<COLGROUP><COL ALIGN=LEFT><COLGROUP><COL ALIGN=JUSTIFY WIDTH="3in">
667<TBODY>
668<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>
669<B>Overall Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
670<B>Description</B></TD></TR>
671</TBODY><TBODY>
672<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-E</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
673<FONT SIZE="-1">Stop after the parsing stage and print the reconstructed ASN.1
674specification code to the standard output.</FONT></TD></TR>
675</TBODY><TBODY>
676<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-F</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
677<FONT SIZE="-1">Used together with -E, instructs the compiler to stop after
678the ASN.1 syntax tree fixing stage and dump the reconstructed ASN.1
679specification to the standard output.</FONT></TD></TR>
680</TBODY><TBODY>
681<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-P</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
682<FONT SIZE="-1">Dump the compiled output to the standard output instead of
683cre- ating the target language files on disk.</FONT></TD></TR>
684</TBODY><TBODY>
685<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-R</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
686<FONT SIZE="-1">Restrict the compiler to generate only the ASN.1 tables, omit-
687ting the usual support code.</FONT></TD></TR>
688</TBODY><TBODY>
689<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-S <I>&lt;directory&gt;</I></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
690<FONT SIZE="-1">Use the specified directory with ASN.1 skeleton files.</FONT></TD></TR>
691</TBODY><TBODY>
vlmfcec8e52004-08-23 15:12:04 +0000692<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Warning Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
693<B>Description</B></TD></TR>
694</TBODY><TBODY>
695<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Werror</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
696<FONT SIZE="-1">Treat warnings as errors; abort if any warning is produced.</FONT></TD></TR>
697</TBODY><TBODY>
698<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-lexer</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
699<FONT SIZE="-1">Enable lexer debugging during the ASN.1 parsing stage.</FONT></TD></TR>
700</TBODY><TBODY>
701<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-fixer</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
702 <FONT SIZE="-1">Enable ASN.1 syntax tree fixer debugging during the
703 fixing stage.</FONT></TD></TR>
704</TBODY><TBODY>
705<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-compiler</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
706<FONT SIZE="-1">Enable debugging during the actual compile time.</FONT></TD></TR>
707</TBODY><TBODY>
708<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Language Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
709<B>Description</B></TD></TR>
710</TBODY><TBODY>
vlm03eb4e12004-09-08 03:10:54 +0000711<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fall-defs-global</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
vlm4a168ae2004-09-29 13:37:15 +0000712Normally the compiler hides the definitions (asn_DEF_xxx) of the
vlm03eb4e12004-09-08 03:10:54 +0000713inner structure elements (members of SEQUENCE, SET and other types).
714This option makes all such definitions global. Enabling this option
vlm4a168ae2004-09-29 13:37:15 +0000715may pollute the namespace by making lots of asn_DEF_xxx structures
vlm03eb4e12004-09-08 03:10:54 +0000716globally visible, but will allow you to manipulate (encode and decode)
717the individual members of any complex ASN.1 structure.</TD></TR>
718</TBODY><TBODY>
vlmfcec8e52004-08-23 15:12:04 +0000719<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fbless-SIZE</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
720<FONT SIZE="-1">Allow SIZE() constraint for INTEGER, ENUMERATED, and other
721types for which this constraint is normally prohibited by the standard.
722This is a violation of an ASN.1 standard and compiler may fail to
723produce the meaningful code.</FONT></TD></TR>
724</TBODY><TBODY>
vlm11115252004-09-17 08:35:02 +0000725<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fnative-types</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
726<FONT SIZE="-1">Use the native machine's data types (int, double) whenever
727possible, instead of the compound ASN.1 INTEGER_t, ENUMERATED_t
728and REAL_t types. </FONT></TD></TR>
vlmfcec8e52004-08-23 15:12:04 +0000729</TBODY><TBODY>
vlm763ac412004-09-26 13:41:45 +0000730<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fno-constraints</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
731Do not generate ASN.1 subtype constraint checking code. This may make
732a shorter executable.</TD></TR>
733</TBODY><TBODY>
vlmfcec8e52004-08-23 15:12:04 +0000734<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-funnamed-unions</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
735<FONT SIZE="-1">Enable unnamed unions in the definitions of target language's
736structures.</FONT></TD></TR>
737</TBODY><TBODY>
738<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-ftypes88</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
739<FONT SIZE="-1">Use only ASN.1:1988 embedded types.</FONT></TD></TR>
740</TBODY><TBODY>
741<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Output Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
742<B>Description</B></TD></TR>
743</TBODY><TBODY>
744<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-print-constraints</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
745<FONT SIZE="-1">When -EF are also specified, this option forces the compiler
746to explain its internal understanding of subtype constraints.</FONT></TD></TR>
747</TBODY><TBODY>
748<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-print-lines</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
749<FONT SIZE="-1">Generate &#34;- #line&#34; comments in -E output.</FONT></TD></TR>
750</TBODY>
751</TABLE>
752
753<P>
754</TD></TR>
755</TABLE>
756</DIV><P></P><BR>
757
758<P>
759
760<H1><A NAME="SECTION03320000000000000000">
vlm9ca15042004-08-07 06:03:15 +0000761Recognizing compiler output</A>
vlmfcec8e52004-08-23 15:12:04 +0000762</H1>
vlm9ca15042004-08-07 06:03:15 +0000763
764<P>
765After compiling, the following entities will be created in your current
766directory:
767
768<P>
769
770<UL>
771<LI>A set of .c and .h files, generally a single pair for each type defined
772in the ASN.1 specifications. These files will be named similarly to
773the ASN.1 types (<I>Rectangle.c</I> and <I>Rectangle.h</I> for the
774specification defined in the beginning of this document).
775</LI>
776<LI>A set of helper .c and .h files which contain generic encoders, decoders
vlmfcec8e52004-08-23 15:12:04 +0000777and other useful routines. There will be quite a few of them, some
778of them even are not always necessary, but the overall amount of code
779after compiling will be rather small anyway.
vlm9ca15042004-08-07 06:03:15 +0000780</LI>
781</UL>
782It is your responsibility to create .c file with the <I>int main()</I>
783routine and the Makefile (if needed). Compiler helps you with the
784latter by creating the Makefile.am.sample, containing the skeleton
785definition for the automake, should you want to use autotools.
786
787<P>
788In other words, after compiling the Rectangle module, you have the
789following set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h,
790<B>...</B> }, where <B>''...''</B> stands for the
791set of additional ''helper'' files created by the compiler. If you
792add the simple file with the <I>int main()</I> routine, it would even
793be possible to compile everything with the single instruction:
794
795<P>
796
vlmfcec8e52004-08-23 15:12:04 +0000797<BLOCKQUOTE><PRE>
798cc&nbsp;-o&nbsp;rectangle&nbsp;*.c&nbsp;&nbsp;&nbsp;#&nbsp;It&nbsp;could&nbsp;be&nbsp;<I>that</I>&nbsp;simple<A NAME="tex2html7"
vlm4a168ae2004-09-29 13:37:15 +0000799 HREF="#foot412"><SUP>4.1</SUP></A>
vlm9ca15042004-08-07 06:03:15 +0000800</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000801</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000802
803<P>
804
vlmfcec8e52004-08-23 15:12:04 +0000805<H1><A NAME="SECTION03330000000000000000">
vlm9ca15042004-08-07 06:03:15 +0000806Invoking the ASN.1 helper code from the application</A>
vlmfcec8e52004-08-23 15:12:04 +0000807</H1>
vlm9ca15042004-08-07 06:03:15 +0000808
809<P>
vlmfcec8e52004-08-23 15:12:04 +0000810First of all, you should to include one or more header files into
811your application. For our Rectangle module, including the Rectangle.h
vlm9ca15042004-08-07 06:03:15 +0000812file is enough:
813
814<P>
815
vlmfcec8e52004-08-23 15:12:04 +0000816<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000817#include&nbsp;&lt;Rectangle.h&gt;
vlm9ca15042004-08-07 06:03:15 +0000818</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000819</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000820The header files defines the C structure corresponding to the ASN.1
821definition of a rectangle and the declaration of the ASN.1 type descriptor,
822which is used as an argument to most of the functions provided by
823the ASN.1 module. For example, here is the code which frees the Rectangle_t
824structure:
825
826<P>
827
vlmfcec8e52004-08-23 15:12:04 +0000828<BLOCKQUOTE><PRE>
829Rectangle_t&nbsp;*rect&nbsp;=&nbsp;...;
830&nbsp;
vlm4a168ae2004-09-29 13:37:15 +0000831asn_DEF_Rectangle-&gt;free_struct(&amp;asn_DEF_Rectangle,
vlm9ca15042004-08-07 06:03:15 +0000832&nbsp;&nbsp;&nbsp;&nbsp;rect,&nbsp;0);
vlm9ca15042004-08-07 06:03:15 +0000833</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000834</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000835This code defines a <I>rect</I> pointer which points to the Rectangle_t
836structure which needs to be freed. The second line invokes the generic
837free_struct routine created specifically for this Rectangle_t structure.
vlm4a168ae2004-09-29 13:37:15 +0000838The <I>asn_DEF_Rectangle</I> is the type descriptor, which holds
vlm9ca15042004-08-07 06:03:15 +0000839a collection of generic routines to deal with the Rectangle_t structure.
840
841<P>
842There are several generic functions available:
843
844<P>
845<DL>
vlm9ca15042004-08-07 06:03:15 +0000846<DT><STRONG>ber_decoder</STRONG></DT>
vlmfcec8e52004-08-23 15:12:04 +0000847<DD>This is the generic <I>restartable</I><A NAME="tex2html8"
vlm4a168ae2004-09-29 13:37:15 +0000848 HREF="#foot239"><SUP>4.2</SUP></A> BER decoder (Basic Encoding Rules). This decoder would create
vlm9ca15042004-08-07 06:03:15 +0000849and/or fill the target structure for you. Please refer to Section
850[<A HREF="#sub:Decoding-BER">Decoding-BER</A>].
851</DD>
852<DT><STRONG>der_encoder</STRONG></DT>
853<DD>This is the generic DER encoder (Distinguished Encoding
vlm763ac412004-09-26 13:41:45 +0000854Rules). This encoder will take the target structure and encode it
vlm9ca15042004-08-07 06:03:15 +0000855into a series of bytes. Please refer to Section [<A HREF="#sub:Encoding-DER">Encoding-DER</A>].
856</DD>
vlm763ac412004-09-26 13:41:45 +0000857<DT><STRONG>xer_encoder</STRONG></DT>
858<DD>This is the generic XER encoder (XML Encoding Rules).
859This encoder will take the target structure and represent it as an
860XML (text) document. Please refer to Section [<A HREF="#sub:Encoding-XER">Encoding-XER</A>].
861</DD>
862<DT><STRONG>check_constraints</STRONG></DT>
863<DD>Check that the contents of the target structure
864are semantically valid and constrained to appropriate implicit or
865explicit subtype constraints. Please refer to Section sub:Validating-the-target.
866</DD>
vlm9ca15042004-08-07 06:03:15 +0000867<DT><STRONG>print_struct</STRONG></DT>
868<DD>This function convert the contents of the passed target
869structure into human readable form. This form is not formal and cannot
870be converted back into the structure, but it may turn out to be useful
871for debugging or quick-n-dirty printing. Please refer to Section [<A HREF="#sub:Printing-the-target">Printing-the-target</A>].
872</DD>
873<DT><STRONG>free_struct</STRONG></DT>
874<DD>This is a generic disposal which frees the target structure.
875Please refer to Section [<A HREF="#sub:Freeing-the-target">Freeing-the-target</A>].
876</DD>
877</DL>
vlm763ac412004-09-26 13:41:45 +0000878check_constraints Check that the contents of the target structure
879are semantically valid and constrained to appropriate implicit or
880explicit subtype constraints. Please refer to Section sub:Validating-the-target.
881
882<P>
vlm4a168ae2004-09-29 13:37:15 +0000883Each of the above function takes the type descriptor (<I>asn_DEF_...</I>)
vlm9ca15042004-08-07 06:03:15 +0000884and the target structure (<I>rect</I>, in the above example). The
885target structure is typically created by the generic BER decoder or
886by the application itself.
887
888<P>
889Here is how the buffer can be deserialized into the structure:
890
891<P>
892
vlmfcec8e52004-08-23 15:12:04 +0000893<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +0000894Rectangle_t&nbsp;*
vlmfcec8e52004-08-23 15:12:04 +0000895simple_deserializer(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;buf_size)&nbsp;{
vlm9ca15042004-08-07 06:03:15 +0000896&nbsp;&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;*rect&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Note&nbsp;this&nbsp;0!&nbsp;*/
897&nbsp;&nbsp;&nbsp;&nbsp;ber_dec_rval_t&nbsp;rval;
898&nbsp;
vlm4a168ae2004-09-29 13:37:15 +0000899&nbsp;&nbsp;&nbsp;&nbsp;rval&nbsp;=&nbsp;asn_DEF_Rectangle-&gt;ber_decoder(0,
900&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,
vlm9ca15042004-08-07 06:03:15 +0000901&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void&nbsp;**)&amp;rect,
902&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size,
903&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0);
904&nbsp;
905&nbsp;&nbsp;&nbsp;&nbsp;if(rval<B>.code</B>&nbsp;==&nbsp;RC_OK)&nbsp;{
906&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;*/
907&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
vlmfcec8e52004-08-23 15:12:04 +0000908&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Free&nbsp;partially&nbsp;decoded&nbsp;rect&nbsp;*/
vlm4a168ae2004-09-29 13:37:15 +0000909&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn_DEF_Rectangle-&gt;free_struct(
910&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect,&nbsp;0);
vlm9ca15042004-08-07 06:03:15 +0000911&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;
912&nbsp;&nbsp;&nbsp;&nbsp;}
913}
vlm9ca15042004-08-07 06:03:15 +0000914</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000915</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +0000916The code above defines a function, <I>simple_deserializer</I>, which
917takes a buffer and its length and expected to return a pointer to
918the Rectangle_t structure. Inside, it tries to convert the bytes
919passed into the target structure (rect) using the generic BER decoder
920and returns the rect pointer afterwards. If the structure cannot be
921deserialized, it frees the memory which might be left allocated by
922the unfinished <I>ber_decoder</I> routine and returns NULL. <B>This
923freeing is necessary</B> because the ber_decoder is a restartable procedure,
924and may fail just because there is more data needs to be provided
925before decoding could be finalized. The code above obviously does
926not take into account the way the <I>ber_decoder</I> failed, so the
927freeing is necessary because the part of the buffer may already be
928decoded into the structure by the time something goes wrong.
929
930<P>
931Restartable decoding is a little bit trickier: you need to provide
932the old target structure pointer (which might be already half-decoded)
933and react on RC_WMORE return code. This will be explained later in
934Section sub:Decoding-BER
935
936<P>
937
vlmfcec8e52004-08-23 15:12:04 +0000938<H2><A NAME="SECTION03331000000000000000"></A><A NAME="sub:Decoding-BER"></A><BR>
vlm9ca15042004-08-07 06:03:15 +0000939Decoding BER
vlmfcec8e52004-08-23 15:12:04 +0000940</H2>
vlm9ca15042004-08-07 06:03:15 +0000941
942<P>
943The Basic Encoding Rules describe the basic way how the structure
944can be encoded and decoded. Several other encoding rules (CER, DER)
945define a more restrictive versions of BER, so the generic BER parser
946is also capable of decoding the data encoded by CER and DER encoders.
947The opposite is not true.
948
949<P>
950The ASN.1 compiler provides the generic BER decoder which is implicitly
951capable of decoding BER, CER and DER encoded data.
952
953<P>
954The decoder is restartable (stream-oriented), which means that in
955case the buffer has less data than it is expected, the decoder will
956process whatever it is available and ask for more data to be provided.
957Please note that the decoder may actually process less data than it
958is given in the buffer, which means that you should be able to make
959the next buffer contain the unprocessed part of the previous buffer.
960
961<P>
962Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
963
964<P>
965
966<UL>
967<LI>You may concatenate these buffers and feed the BER decoder with 300
968bytes of data, or
969</LI>
970<LI>You may feed it the first buffer of 100 bytes of data, realize that
971the ber_decoder consumed only 95 bytes from it and later feed the
972decoder with 205 bytes buffer which consists of 5 unprocessed bytes
973from the first buffer and the latter 200 bytes from the second buffer.
974</LI>
975</UL>
976This is not as convenient as it could be (like, the BER encoder would
977consume the whole 100 bytes and keep these 5 bytes in some temporary
978storage), but in case of stream-based processing it might actually
979be OK. Suggestions are welcome.
980
981<P>
982There are two ways to invoke a BER decoder. The first one is a direct
983reference of the type-specific decoder. This way was shown in the
984previous example of <I>simple_deserializer</I> function. The second
985way is to invoke a <I>ber_decode</I> function, which is just a simple
986wrapper of the former approach into a less wordy notation:
987
988<P>
989
vlmfcec8e52004-08-23 15:12:04 +0000990<BLOCKQUOTE><PRE>
vlm4a168ae2004-09-29 13:37:15 +0000991rval&nbsp;=&nbsp;ber_decode(0,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;(void&nbsp;**)&amp;rect,
vlm9ca15042004-08-07 06:03:15 +0000992&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size);
vlm9ca15042004-08-07 06:03:15 +0000993</PRE>
vlmfcec8e52004-08-23 15:12:04 +0000994</BLOCKQUOTE>
vlm4a168ae2004-09-29 13:37:15 +0000995Note that the initial (asn_DEF_Rectangle-&gt;ber_decoder) reference
vlm9ca15042004-08-07 06:03:15 +0000996is gone, and also the last argument (0) is no longer necessary.
997
998<P>
999These two ways of invocations are fully equivalent.
1000
1001<P>
vlm4a168ae2004-09-29 13:37:15 +00001002The BER de<I>coder</I> may fail because of (<I>the following RC_...
vlm9ca15042004-08-07 06:03:15 +00001003codes are defined in ber_decoder.h</I>):
1004
1005<P>
1006
1007<UL>
1008<LI>RC_WMORE: There is more data expected than it is provided (stream
1009mode continuation feature);
1010</LI>
1011<LI>RC_FAIL: General failure to decode the buffer;
1012</LI>
1013<LI>... other codes may be defined as well.
1014</LI>
1015</UL>
1016Together with the return code (.code) the ber_dec_rval_t type contains
1017the number of bytes which is consumed from the buffer. In the previous
1018hypothetical example of two buffers (of 100 and 200 bytes), the first
1019call to ber_decode() would return with .code = RC_WMORE and .consumed
1020= 95. The .consumed field of the BER decoder return value is <B>always</B>
1021valid, even if the decoder succeeds or fails with any other return
1022code.
1023
1024<P>
1025Please look into ber_decoder.h for the precise definition of ber_decode()
1026and related types.
1027
1028<P>
1029
vlmfcec8e52004-08-23 15:12:04 +00001030<H2><A NAME="SECTION03332000000000000000"></A><A NAME="sub:Encoding-DER"></A><BR>
vlm9ca15042004-08-07 06:03:15 +00001031Encoding DER
vlmfcec8e52004-08-23 15:12:04 +00001032</H2>
vlm9ca15042004-08-07 06:03:15 +00001033
1034<P>
vlm763ac412004-09-26 13:41:45 +00001035The Distinguished Encoding Rules is the <I>canonical</I> variant of
1036BER encoding rules. The DER is best suited to encode the structures
1037where all the lengths are known beforehand. This is probably exactly
1038how you want to encode: either after a BER decoding or after a manual
1039fill-up, the target structure contains the data which size is implicitly
1040known before encoding. The DER encoding is used, for example, to encode
1041X.509 certificates.
vlm9ca15042004-08-07 06:03:15 +00001042
1043<P>
1044As with BER decoder, the DER encoder may be invoked either directly
vlm4a168ae2004-09-29 13:37:15 +00001045from the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone
1046function, which is somewhat simpler:
vlm9ca15042004-08-07 06:03:15 +00001047
1048<P>
1049
vlmfcec8e52004-08-23 15:12:04 +00001050<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +00001051/*
1052&nbsp;*&nbsp;This&nbsp;is&nbsp;a&nbsp;custom&nbsp;function&nbsp;which&nbsp;writes&nbsp;the
1053&nbsp;*&nbsp;encoded&nbsp;output&nbsp;into&nbsp;some&nbsp;FILE&nbsp;stream.
1054&nbsp;*/
vlmfcec8e52004-08-23 15:12:04 +00001055static&nbsp;int
1056write_stream(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;size,&nbsp;void&nbsp;*app_key)&nbsp;{
vlm9ca15042004-08-07 06:03:15 +00001057&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;*ostream&nbsp;=&nbsp;app_key;
1058&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;wrote;
1059&nbsp;
1060&nbsp;&nbsp;&nbsp;&nbsp;wrote&nbsp;=&nbsp;fwrite(buffer,&nbsp;1,&nbsp;size,&nbsp;ostream);
1061&nbsp;
1062&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(wrote&nbsp;==&nbsp;size)&nbsp;?&nbsp;0&nbsp;:&nbsp;-1;
1063}
1064&nbsp;
1065/*
1066&nbsp;*&nbsp;This&nbsp;is&nbsp;the&nbsp;serializer&nbsp;itself,
1067&nbsp;*&nbsp;it&nbsp;supplies&nbsp;the&nbsp;DER&nbsp;encoder&nbsp;with&nbsp;the
1068&nbsp;*&nbsp;pointer&nbsp;to&nbsp;the&nbsp;custom&nbsp;output&nbsp;function.
1069&nbsp;*/
1070ssize_t
1071simple_serializer(FILE&nbsp;*ostream,&nbsp;Rectangle_t&nbsp;*rect)&nbsp;{
vlm763ac412004-09-26 13:41:45 +00001072&nbsp;&nbsp;&nbsp;&nbsp;asn_enc_rval_t&nbsp;er;&nbsp;&nbsp;/*&nbsp;Encoder&nbsp;return&nbsp;value&nbsp;*/
vlm9ca15042004-08-07 06:03:15 +00001073&nbsp;
vlm4a168ae2004-09-29 13:37:15 +00001074&nbsp;&nbsp;&nbsp;&nbsp;er&nbsp;=&nbsp;der_encode(&amp;asn_DEF_Rect,&nbsp;rect,
vlmfcec8e52004-08-23 15:12:04 +00001075&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_stream,&nbsp;ostream);
vlm763ac412004-09-26 13:41:45 +00001076&nbsp;&nbsp;&nbsp;&nbsp;if(er.<B>encoded</B>&nbsp;==&nbsp;-1)&nbsp;{
vlm9ca15042004-08-07 06:03:15 +00001077&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*
vlm763ac412004-09-26 13:41:45 +00001078&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Failed&nbsp;to&nbsp;encode&nbsp;the&nbsp;rectangle&nbsp;data.
vlm9ca15042004-08-07 06:03:15 +00001079&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
1080&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''Cannot&nbsp;encode&nbsp;%s:&nbsp;%s&#92;n'',
vlm763ac412004-09-26 13:41:45 +00001081&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;er.<B>failed_type</B>-&gt;name,
vlm9ca15042004-08-07 06:03:15 +00001082&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strerror(errno));
1083&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-1;
1084&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
1085&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Return&nbsp;the&nbsp;number&nbsp;of&nbsp;bytes&nbsp;*/
vlm763ac412004-09-26 13:41:45 +00001086&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;er.encoded;
vlm9ca15042004-08-07 06:03:15 +00001087&nbsp;&nbsp;&nbsp;&nbsp;}
1088}
vlm9ca15042004-08-07 06:03:15 +00001089</PRE>
vlmfcec8e52004-08-23 15:12:04 +00001090</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +00001091As you see, the DER encoder does not write into some sort of buffer
1092or something. It just invokes the custom function (possible, multiple
1093times) which would save the data into appropriate storage. The optional
1094argument <I>app_key</I> is opaque for the DER encoder code and just
1095used by <I>_write_stream()</I> as the pointer to the appropriate
1096output stream to be used.
1097
1098<P>
1099If the custom write function is not given (passed as 0), then the
1100DER encoder will essentially do the same thing (i.e., encode the data)
1101but no callbacks will be invoked (so the data goes nowhere). It may
1102prove useful to determine the size of the structure's encoding before
vlmfcec8e52004-08-23 15:12:04 +00001103actually doing the encoding<A NAME="tex2html9"
vlm4a168ae2004-09-29 13:37:15 +00001104 HREF="#foot315"><SUP>4.3</SUP></A>.
vlm9ca15042004-08-07 06:03:15 +00001105
1106<P>
1107Please look into der_encoder.h for the precise definition of der_encode()
1108and related types.
1109
1110<P>
1111
vlm763ac412004-09-26 13:41:45 +00001112<H2><A NAME="SECTION03333000000000000000"></A><A NAME="sub:Encoding-XER"></A><BR>
1113Encoding XER
1114</H2>
1115
1116<P>
1117The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
1118Markup Language, a text-based format for information exchange. The
1119encoder routine API comes in two flavors: stdio-based and callback-based.
1120With the callback-based encoder, the encoding process is very similar
1121to the DER one, described in Section sub:Encoding-DER. The
1122following example uses the definition of write_stream() from up there.
1123
1124<P>
1125
1126<BLOCKQUOTE><PRE>
1127/*
1128&nbsp;*&nbsp;This&nbsp;procedure&nbsp;generates&nbsp;the&nbsp;XML&nbsp;document
1129&nbsp;*&nbsp;by&nbsp;invoking&nbsp;the&nbsp;XER&nbsp;encoder.
1130&nbsp;*&nbsp;NOTE:&nbsp;Do&nbsp;not&nbsp;copy&nbsp;this&nbsp;code&nbsp;verbatim!
1131&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;the&nbsp;stdio&nbsp;output&nbsp;is&nbsp;necessary,
1132&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;the&nbsp;xer_fprint()&nbsp;procedure&nbsp;instead.
1133&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;See&nbsp;Section&nbsp;sub:Printing-the-target.
1134&nbsp;*/
1135int
1136print_as_XML(FILE&nbsp;*ostream,&nbsp;Rectangle_t&nbsp;*rect)&nbsp;{
1137&nbsp;&nbsp;&nbsp;&nbsp;asn_enc_rval_t&nbsp;er;&nbsp;&nbsp;/*&nbsp;Encoder&nbsp;return&nbsp;value&nbsp;*/
1138&nbsp;
vlm4a168ae2004-09-29 13:37:15 +00001139&nbsp;&nbsp;&nbsp;&nbsp;er&nbsp;=&nbsp;xer_encode(&amp;asn_DEF_Rect,&nbsp;rect,
vlm763ac412004-09-26 13:41:45 +00001140&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XER_F_BASIC,&nbsp;/*&nbsp;BASIC-XER&nbsp;or&nbsp;CANONICAL-XER&nbsp;*/
1141&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_stream,&nbsp;ostream);
1142&nbsp;
1143&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(er.encoded&nbsp;==&nbsp;-1)&nbsp;?&nbsp;-1&nbsp;:&nbsp;0;
1144}
1145</PRE>
1146</BLOCKQUOTE>
1147Please look into xer_encoder.h for the precise definition of xer_encode()
1148and related types.
1149
1150<P>
1151See Section [<A HREF="#sub:Printing-the-target">Printing-the-target</A>] for the example of stdio-based
1152XML encoder and other pretty-printing suggestions.
1153
1154<P>
1155
1156<H2><A NAME="SECTION03334000000000000000"></A><A NAME="sub:Validating-the-target"></A><BR>
vlm9ca15042004-08-07 06:03:15 +00001157Validating the target structure
vlmfcec8e52004-08-23 15:12:04 +00001158</H2>
vlm9ca15042004-08-07 06:03:15 +00001159
1160<P>
1161Sometimes the target structure needs to be validated. For example,
1162if the structure was created by the application (as opposed to being
1163decoded from some external source), some important information required
1164by the ASN.1 specification might be missing. On the other hand, the
1165successful decoding of the data from some external source does not
1166necessarily mean that the data is fully valid either. It might well
1167be the case that the specification describes some subtype constraints
1168that were not taken into account during decoding, and it would actually
1169be useful to perform the last check when the data is ready to be encoded
1170or when the data has just been decoded to ensure its validity according
1171to some stricter rules.
1172
1173<P>
1174The asn_check_constraints() function checks the type for various
1175implicit and explicit constraints. It is recommended to use asn_check_constraints()
1176function after each decoding and before each encoding.
1177
1178<P>
1179Please look into constraints.h for the precise definition of asn_check_constraints()
1180and related types.
1181
1182<P>
1183
vlm763ac412004-09-26 13:41:45 +00001184<H2><A NAME="SECTION03335000000000000000"></A><A NAME="sub:Printing-the-target"></A><BR>
vlm9ca15042004-08-07 06:03:15 +00001185Printing the target structure
vlmfcec8e52004-08-23 15:12:04 +00001186</H2>
vlm9ca15042004-08-07 06:03:15 +00001187
1188<P>
1189There are two ways to print the target structure: either invoke the
1190print_struct member of the ASN.1 type descriptor, or using the asn_fprint()
1191function, which is a simpler wrapper of the former:
1192
1193<P>
1194
vlmfcec8e52004-08-23 15:12:04 +00001195<BLOCKQUOTE><PRE>
vlm4a168ae2004-09-29 13:37:15 +00001196asn_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect);
vlm9ca15042004-08-07 06:03:15 +00001197</PRE>
vlmfcec8e52004-08-23 15:12:04 +00001198</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +00001199Please look into constr_TYPE.h for the precise definition of asn_fprint()
1200and related types.
1201
1202<P>
vlm763ac412004-09-26 13:41:45 +00001203Another practical alternative to this custom format printing would
1204be to invoke XER encoder. The default BASIC-XER encoder performs reasonable
1205formatting for the output to be useful and human readable. To invoke
1206the XER decoder in a manner similar to asn_fprint(), use the xer_fprint()
1207call:
vlm9ca15042004-08-07 06:03:15 +00001208
vlm763ac412004-09-26 13:41:45 +00001209<P>
1210
1211<BLOCKQUOTE><PRE>
vlm4a168ae2004-09-29 13:37:15 +00001212xer_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect);
vlm763ac412004-09-26 13:41:45 +00001213</PRE>
1214</BLOCKQUOTE>
1215See Section sub:Encoding-XER for XML-related details.
1216
1217<P>
1218
1219<H2><A NAME="SECTION03336000000000000000"></A><A NAME="sub:Freeing-the-target"></A><BR>
vlm9ca15042004-08-07 06:03:15 +00001220Freeing the target structure
vlmfcec8e52004-08-23 15:12:04 +00001221</H2>
vlm9ca15042004-08-07 06:03:15 +00001222
1223<P>
1224Freeing the structure is slightly more complex than it may seem to.
1225When the ASN.1 structure is freed, all the members of the structure
1226and their submembers etc etc are recursively freed too. But it might
1227not be feasible to free the structure itself. Consider the following
1228case:
1229
1230<P>
1231
vlmfcec8e52004-08-23 15:12:04 +00001232<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +00001233struct&nbsp;my_figure&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;custom&nbsp;structure&nbsp;*/
1234&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;*/
1235&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;type&nbsp;is&nbsp;generated&nbsp;by&nbsp;the&nbsp;ASN.1&nbsp;compiler&nbsp;*/
1236&nbsp;&nbsp;&nbsp;&nbsp;<I>Rectangle_t&nbsp;rect;</I>
1237&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;other&nbsp;members&nbsp;of&nbsp;the&nbsp;structure&nbsp;*/
1238};
vlm9ca15042004-08-07 06:03:15 +00001239</PRE>
vlmfcec8e52004-08-23 15:12:04 +00001240</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +00001241In this example, the application programmer defined a custom structure
1242with one ASN.1-derived member (rect). This member is not a reference
1243to the Rectangle_t, but an in-place inclusion of the Rectangle_t
1244structure. If the freeing is necessary, the usual procedure of freeing
1245everything must not be applied to the &amp;rect pointer itself, because
1246it does not point to the memory block directly allocated by memory
1247allocation routine, but instead lies within such a block allocated
1248for my_figure structure.
1249
1250<P>
1251To solve this problem, the free_struct routine has the additional
1252argument (besides the intuitive type descriptor and target structure
1253pointers), which is the flag specifying whether the outer pointer
1254itself must be freed (0, default) or it should be left intact (non-zero
1255value).
1256
1257<P>
1258
vlmfcec8e52004-08-23 15:12:04 +00001259<BLOCKQUOTE><PRE>
vlm9ca15042004-08-07 06:03:15 +00001260/*&nbsp;Rectangle_t&nbsp;is&nbsp;defined&nbsp;within&nbsp;my_figure&nbsp;*/
1261struct&nbsp;my_figure&nbsp;*mf&nbsp;=&nbsp;<B>...</B>;
1262/*
1263&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_td
1264&nbsp;*&nbsp;without&nbsp;freeing&nbsp;the&nbsp;mf-&gt;rect&nbsp;pointer
1265&nbsp;*/
vlm4a168ae2004-09-29 13:37:15 +00001266asn_DEF_Rectangle-&gt;free_struct(
1267&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;&amp;mf-&gt;rect,&nbsp;<I>1</I>&nbsp;/*&nbsp;!free&nbsp;*/);
vlm9ca15042004-08-07 06:03:15 +00001268&nbsp;
1269/*&nbsp;Rectangle_t&nbsp;is&nbsp;a&nbsp;stand-alone&nbsp;pointer&nbsp;*/
1270Rectangle_t&nbsp;*rect&nbsp;=&nbsp;<B>...</B>;
1271/*
1272&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_t
1273&nbsp;*&nbsp;and&nbsp;freeing&nbsp;the&nbsp;rect&nbsp;pointer
1274&nbsp;*/
vlm4a168ae2004-09-29 13:37:15 +00001275asn_DEF_Rectangle-&gt;free_struct(
1276&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect,&nbsp;<I>0</I>&nbsp;/*&nbsp;free&nbsp;the&nbsp;pointer&nbsp;too&nbsp;*/);
vlm9ca15042004-08-07 06:03:15 +00001277</PRE>
vlmfcec8e52004-08-23 15:12:04 +00001278</BLOCKQUOTE>
vlm9ca15042004-08-07 06:03:15 +00001279It is safe to invoke the <I>free_struct</I> function with the target
1280structure pointer set to 0 (NULL), the function will do nothing.
1281
1282<P>
1283
1284<H2><A NAME="SECTION04000000000000000000">
1285Bibliography</A>
vlmfcec8e52004-08-23 15:12:04 +00001286</H2><DL COMPACT><DD><P></P><DT><A NAME="ASN1C">ASN1C</A>
vlm03eb4e12004-09-08 03:10:54 +00001287<DD>The OpenSource ASN.1 Compiler. http://lionet.info/asn1/
vlmfcec8e52004-08-23 15:12:04 +00001288<P></P><DT><A NAME="Dub00">Dub00</A>
vlm9ca15042004-08-07 06:03:15 +00001289<DD>Olivier Dubuisson - <I>ASN.1 Communication between heterogeneous
1290systems</I> - Morgan Kaufmann Publishers, 2000. http://asn1.elibel.tm.fr/en/book/.
1291ISBN:0-12-6333361-0.
1292<P></P><DT><A NAME="ITU-T_ASN.1">ITU-T/ASN.1</A>
1293<DD>ITU-T Study Group 17 - Languages for Telecommunication Systems http://www.itu.int/ITU-T/studygroups/com17/languages/</DL>
1294
1295<P>
1296<BR><HR><H4>Footnotes</H4>
1297<DL>
vlm4a168ae2004-09-29 13:37:15 +00001298<DT><A NAME="foot145">... supported</A><A
vlm9ca15042004-08-07 06:03:15 +00001299 HREF="asn1c-usage.html#tex2html1"><SUP>2.1</SUP></A></DT>
vlmfcec8e52004-08-23 15:12:04 +00001300<DD>C++ is ''supported'' too, as long as an class-based approach is
1301not a definitive factor.
vlm9ca15042004-08-07 06:03:15 +00001302
1303</DD>
vlm4a168ae2004-09-29 13:37:15 +00001304<DT><A NAME="foot404">... this</A><A
vlm9ca15042004-08-07 06:03:15 +00001305 HREF="asn1c-usage.html#tex2html2"><SUP>2.2</SUP></A></DT>
vlm11115252004-09-17 08:35:02 +00001306<DD><I>-fnative-types</I> compiler option is used to produce basic C <I>int</I>
1307types instead of infinite width INTEGER_t structures. See <A HREF=#Table1>Table 1</A>.
vlm9ca15042004-08-07 06:03:15 +00001308
1309</DD>
vlm4a168ae2004-09-29 13:37:15 +00001310<DT><A NAME="foot152">... binary</A><A
vlm9ca15042004-08-07 06:03:15 +00001311 HREF="asn1c-usage.html#tex2html3"><SUP>2.3</SUP></A></DT>
1312<DD>BER, CER and DER encodings are binary. However, the XER encoding is
1313text (XML) based.
1314
1315</DD>
vlm4a168ae2004-09-29 13:37:15 +00001316<DT><A NAME="foot405">...asn1c</A><A
vlmfcec8e52004-08-23 15:12:04 +00001317 HREF="asn1c-usage.html#tex2html4"><SUP>3.1</SUP></A></DT>
1318<DD>The 1 symbol in asn<B>1</B>c is a digit, not an ''ell'' letter.
1319
1320</DD>
vlm4a168ae2004-09-29 13:37:15 +00001321<DT><A NAME="foot406">... specification</A><A
vlmfcec8e52004-08-23 15:12:04 +00001322 HREF="asn1c-usage.html#tex2html5"><SUP>3.2</SUP></A></DT>
vlm9ca15042004-08-07 06:03:15 +00001323<DD>This is probably <B>not</B> what you want to try out right now -
vlmfcec8e52004-08-23 15:12:04 +00001324read through the rest of this chapter to find out about <B>-P</B>
1325and <B>-R</B> options.
vlm9ca15042004-08-07 06:03:15 +00001326
1327</DD>
vlm4a168ae2004-09-29 13:37:15 +00001328<DT><A NAME="foot412">...that&nbsp;simple</A><A
vlmfcec8e52004-08-23 15:12:04 +00001329 HREF="asn1c-usage.html#tex2html7"><SUP>4.1</SUP></A></DT>
vlm9ca15042004-08-07 06:03:15 +00001330<DD>Provided that you've also created a .c file with the <I>int main()</I>
1331routine.
1332
1333</DD>
vlm4a168ae2004-09-29 13:37:15 +00001334<DT><A NAME="foot239">...restartable</A><A
vlmfcec8e52004-08-23 15:12:04 +00001335 HREF="asn1c-usage.html#tex2html8"><SUP>4.2</SUP></A></DT>
vlm9ca15042004-08-07 06:03:15 +00001336<DD>Restartable means that if the decoder encounters the end of the buffer,
1337it will fail, but may later be invoked again with the rest of the
1338buffer to continue decoding.
1339
1340</DD>
vlm4a168ae2004-09-29 13:37:15 +00001341<DT><A NAME="foot315">... encoding</A><A
vlmfcec8e52004-08-23 15:12:04 +00001342 HREF="asn1c-usage.html#tex2html9"><SUP>4.3</SUP></A></DT>
vlm9ca15042004-08-07 06:03:15 +00001343<DD>It is actually faster too: the encoder might skip over some computations
1344which aren't important for the size determination.
1345
1346</DD>
1347</DL><BR><HR>
1348<ADDRESS>
1349Lev Walkin
vlm4a168ae2004-09-29 13:37:15 +000013502004-09-29
vlm9ca15042004-08-07 06:03:15 +00001351</ADDRESS>
1352</BODY>
1353</HTML>