blob: d7b22b0f316618435bf3058d685caa3f606aa878 [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>
9<TITLE>Using the Free ASN.1 Compiler</TITLE>
10<META NAME="description" CONTENT="Using the Free ASN.1 Compiler">
11<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>
24<H1 ALIGN="CENTER">Using the Free ASN.1 Compiler</H1><DIV>
25
26<P ALIGN="CENTER"><STRONG>Lev Walkin &lt;vlm@lionet.info&gt;</STRONG></P>
27</DIV>
Lev Walkin26587ab2004-08-23 15:12:04 +000028
29<P>
Lev Walkineb97a702004-08-07 06:03:15 +000030<BR>
31
32<H2><A NAME="SECTION01000000000000000000">
33Contents</A>
34</H2>
35<!--Table of Contents-->
36
37<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000038<LI><A NAME="tex2html45"
Lev Walkineb97a702004-08-07 06:03:15 +000039 HREF="asn1c-usage.html#SECTION02000000000000000000">ASN.1 Basics</A>
40<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000041<LI><A NAME="tex2html46"
Lev Walkineb97a702004-08-07 06:03:15 +000042 HREF="asn1c-usage.html#SECTION02100000000000000000">Abstract Syntax Notation: ASN.1</A>
43<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000044<LI><A NAME="tex2html47"
Lev Walkineb97a702004-08-07 06:03:15 +000045 HREF="asn1c-usage.html#SECTION02110000000000000000">Some of the ASN.1 Basic Types</A>
46<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000047<LI><A NAME="tex2html48"
Lev Walkineb97a702004-08-07 06:03:15 +000048 HREF="asn1c-usage.html#SECTION02111000000000000000">The BOOLEAN type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000049<LI><A NAME="tex2html49"
Lev Walkineb97a702004-08-07 06:03:15 +000050 HREF="asn1c-usage.html#SECTION02112000000000000000">The INTEGER type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000051<LI><A NAME="tex2html50"
Lev Walkineb97a702004-08-07 06:03:15 +000052 HREF="asn1c-usage.html#SECTION02113000000000000000">The ENUMERATED type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000053<LI><A NAME="tex2html51"
Lev Walkineb97a702004-08-07 06:03:15 +000054 HREF="asn1c-usage.html#SECTION02114000000000000000">The OCTET STRING type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000055<LI><A NAME="tex2html52"
Lev Walkineb97a702004-08-07 06:03:15 +000056 HREF="asn1c-usage.html#SECTION02115000000000000000">The OBJECT IDENTIFIER type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000057<LI><A NAME="tex2html53"
Lev Walkineb97a702004-08-07 06:03:15 +000058 HREF="asn1c-usage.html#SECTION02116000000000000000">The RELATIVE-OID type</A>
59</UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000060<LI><A NAME="tex2html54"
Lev Walkineb97a702004-08-07 06:03:15 +000061 HREF="asn1c-usage.html#SECTION02120000000000000000">Some of the ASN.1 String Types</A>
62<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000063<LI><A NAME="tex2html55"
Lev Walkineb97a702004-08-07 06:03:15 +000064 HREF="asn1c-usage.html#SECTION02121000000000000000">The IA5String type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000065<LI><A NAME="tex2html56"
Lev Walkineb97a702004-08-07 06:03:15 +000066 HREF="asn1c-usage.html#SECTION02122000000000000000">The UTF8String type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000067<LI><A NAME="tex2html57"
Lev Walkineb97a702004-08-07 06:03:15 +000068 HREF="asn1c-usage.html#SECTION02123000000000000000">The NumericString type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000069<LI><A NAME="tex2html58"
Lev Walkineb97a702004-08-07 06:03:15 +000070 HREF="asn1c-usage.html#SECTION02124000000000000000">The PrintableString type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000071<LI><A NAME="tex2html59"
Lev Walkineb97a702004-08-07 06:03:15 +000072 HREF="asn1c-usage.html#SECTION02125000000000000000">The VisibleString type</A>
73</UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000074<LI><A NAME="tex2html60"
Lev Walkineb97a702004-08-07 06:03:15 +000075 HREF="asn1c-usage.html#SECTION02130000000000000000">ASN.1 Constructed Types</A>
76<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000077<LI><A NAME="tex2html61"
Lev Walkineb97a702004-08-07 06:03:15 +000078 HREF="asn1c-usage.html#SECTION02131000000000000000">The SEQUENCE type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000079<LI><A NAME="tex2html62"
Lev Walkineb97a702004-08-07 06:03:15 +000080 HREF="asn1c-usage.html#SECTION02132000000000000000">The SET type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000081<LI><A NAME="tex2html63"
Lev Walkineb97a702004-08-07 06:03:15 +000082 HREF="asn1c-usage.html#SECTION02133000000000000000">The CHOICE type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000083<LI><A NAME="tex2html64"
Lev Walkineb97a702004-08-07 06:03:15 +000084 HREF="asn1c-usage.html#SECTION02134000000000000000">The SEQUENCE OF type</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000085<LI><A NAME="tex2html65"
Lev Walkineb97a702004-08-07 06:03:15 +000086 HREF="asn1c-usage.html#SECTION02135000000000000000">The SET OF type</A>
87</UL>
88</UL>
89</UL><BR>
Lev Walkin26587ab2004-08-23 15:12:04 +000090<LI><A NAME="tex2html66"
Lev Walkineb97a702004-08-07 06:03:15 +000091 HREF="asn1c-usage.html#SECTION03000000000000000000">Using the ASN.1 Compiler</A>
92<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +000093<LI><A NAME="tex2html67"
Lev Walkineb97a702004-08-07 06:03:15 +000094 HREF="asn1c-usage.html#SECTION03100000000000000000">Introduction to the ASN.1 Compiler</A>
Lev Walkin26587ab2004-08-23 15:12:04 +000095<LI><A NAME="tex2html68"
96 HREF="asn1c-usage.html#SECTION03200000000000000000">Quick start</A>
97<LI><A NAME="tex2html69"
98 HREF="asn1c-usage.html#SECTION03300000000000000000">Using the ASN.1 Compiler</A>
Lev Walkineb97a702004-08-07 06:03:15 +000099<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +0000100<LI><A NAME="tex2html70"
101 HREF="asn1c-usage.html#SECTION03310000000000000000">Command-line options</A>
102<LI><A NAME="tex2html71"
103 HREF="asn1c-usage.html#SECTION03320000000000000000">Recognizing compiler output</A>
104<LI><A NAME="tex2html72"
105 HREF="asn1c-usage.html#SECTION03330000000000000000">Invoking the ASN.1 helper code from the application</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000106<UL>
Lev Walkin26587ab2004-08-23 15:12:04 +0000107<LI><A NAME="tex2html73"
108 HREF="asn1c-usage.html#SECTION03331000000000000000">Decoding BER</A>
109<LI><A NAME="tex2html74"
110 HREF="asn1c-usage.html#SECTION03332000000000000000">Encoding DER</A>
111<LI><A NAME="tex2html75"
112 HREF="asn1c-usage.html#SECTION03333000000000000000">Validating the target structure</A>
113<LI><A NAME="tex2html76"
114 HREF="asn1c-usage.html#SECTION03334000000000000000">Printing the target structure</A>
115<LI><A NAME="tex2html77"
116 HREF="asn1c-usage.html#SECTION03335000000000000000">Freeing the target structure</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000117</UL>
118</UL>
119</UL><BR>
Lev Walkin26587ab2004-08-23 15:12:04 +0000120<LI><A NAME="tex2html78"
Lev Walkineb97a702004-08-07 06:03:15 +0000121 HREF="asn1c-usage.html#SECTION04000000000000000000">Bibliography</A>
122</UL>
123<!--End of Table of Contents-->
124
125<P>
126
Lev Walkin26587ab2004-08-23 15:12:04 +0000127<P>
128
Lev Walkineb97a702004-08-07 06:03:15 +0000129<H1><A NAME="SECTION02000000000000000000">
130ASN.1 Basics</A>
131</H1>
132
133<P>
134
135<H1><A NAME="SECTION02100000000000000000">
136Abstract Syntax Notation: ASN.1</A>
137</H1>
138
139<P>
140<I>This chapter defines some basic ASN.1 concepts and describes
141several most widely used types. It is by no means an authoritative
142or complete reference. For more complete ASN.1 description, please
143refer to Olivier Dubuisson's book [<A
144 HREF="asn1c-usage.html#Dub00">Dub00</A>] or the ASN.1 body
145of standards itself [<A
146 HREF="asn1c-usage.html#ITU-T_ASN.1">ITU-T/ASN.1</A>].</I>
147
148<P>
149The Abstract Syntax Notation One is used to formally describe the
150semantics of data transmitted across the network. Two communicating
151parties may have different formats of their native data types (i.e.
152number of bits in the integer type), thus it is important to have
153a way to describe the data in a manner which is independent from the
154particular machine's representation. The ASN.1 specifications is used
155to achieve one or more of the following:
156
157<P>
158
159<UL>
160<LI>The specification expressed in the ASN.1 notation is a formal and
161precise way to communicate the data semantics to human readers;
162</LI>
163<LI>The ASN.1 specifications may be used as input for automatic compilers
164which produce the code for some target language (C, C++, Java, etc)
165to encode and decode the data according to some encoding rules (which
166are also defined by the ASN.1 standard).
167</LI>
168</UL>
169Consider the following example:
170
171<P>
172
Lev Walkin26587ab2004-08-23 15:12:04 +0000173<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000174Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
175&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,
176&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER
177}
Lev Walkineb97a702004-08-07 06:03:15 +0000178</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000179</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000180This ASN.1 specification describes a constructed type, <I>Rectangle</I>,
181containing two integer fields. This specification may tell the reader
182that there is this kind of data structure and that some entity may
183be prepared to send or receive it. The question on <I>how</I> that
184entity is going to send or receive the <I>encoded data</I> is outside
185the scope of ASN.1. For example, this data structure may be encoded
186according to some encoding rules and sent to the destination using
187the TCP protocol. The ASN.1 specifies several ways of encoding (or
188''serializing'', or ''marshaling'') the data: BER, CER, DER and
189XER, some of them which will be described later.
190
191<P>
192The complete specification must be wrapped in a module, which looks
193like this:
194
195<P>
196
Lev Walkin26587ab2004-08-23 15:12:04 +0000197<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000198UsageExampleModule1
199&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;iso&nbsp;org(3)&nbsp;dod(6)&nbsp;internet(1)&nbsp;private(4)
200&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enterprise(1)&nbsp;spelio(9363)&nbsp;software(1)
201&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 +0000202&nbsp;&nbsp;&nbsp;&nbsp;DEFINITIONS&nbsp;AUTOMATIC&nbsp;TAGS&nbsp;::=
Lev Walkineb97a702004-08-07 06:03:15 +0000203BEGIN
204&nbsp;
205--&nbsp;This&nbsp;is&nbsp;a&nbsp;comment&nbsp;which&nbsp;describes&nbsp;nothing.
206Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
207&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 +0000208&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 +0000209}
210&nbsp;
211END
Lev Walkineb97a702004-08-07 06:03:15 +0000212</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000213</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000214The module header consists of module name (UsageExampleModule1), the
Lev Walkin26587ab2004-08-23 15:12:04 +0000215module object identifier ({...}), a keyword ''DEFINITIONS'', a
216set of module flags (AUTOMATIC TAGS) and ''::= BEGIN''. The module
217ends with an ''END'' statement.
Lev Walkineb97a702004-08-07 06:03:15 +0000218
219<P>
220
221<H1><A NAME="SECTION02110000000000000000">
222Some of the ASN.1 Basic Types</A>
223</H1>
224
225<P>
226
227<H2><A NAME="SECTION02111000000000000000">
228The BOOLEAN type</A>
229</H2>
230
231<P>
232The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF
233or a similar kind of two-way choice.
234
235<P>
236
237<H2><A NAME="SECTION02112000000000000000">
238The INTEGER type</A>
239</H2>
240
241<P>
242The INTEGER type is a signed natural number type without any restrictions
243on its size. If the automatic checking on INTEGER value bounds are
244necessary, the subtype constraints must be used.
245
246<P>
247
Lev Walkin26587ab2004-08-23 15:12:04 +0000248<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000249SimpleInteger&nbsp;::=&nbsp;INTEGER
Lev Walkin26587ab2004-08-23 15:12:04 +0000250&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000251--&nbsp;An&nbsp;integer&nbsp;with&nbsp;a&nbsp;very&nbsp;limited&nbsp;range
252SmallInt&nbsp;::=&nbsp;INTEGER&nbsp;(0..127)
Lev Walkin26587ab2004-08-23 15:12:04 +0000253&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000254--&nbsp;Integer,&nbsp;negative
255NegativeInt&nbsp;::=&nbsp;INTEGER&nbsp;(MIN..0)
Lev Walkineb97a702004-08-07 06:03:15 +0000256</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000257</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000258
259<P>
260
261<H2><A NAME="SECTION02113000000000000000">
262The ENUMERATED type</A>
263</H2>
264
265<P>
266The ENUMERATED type is semantically equivalent to the INTEGER type
267with some integer values explicitly named.
268
269<P>
270
Lev Walkin26587ab2004-08-23 15:12:04 +0000271<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000272FruitId&nbsp;::=&nbsp;ENUMERATED&nbsp;{&nbsp;apple(1),&nbsp;orange(2)&nbsp;}
Lev Walkin26587ab2004-08-23 15:12:04 +0000273&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000274--&nbsp;The&nbsp;numbers&nbsp;in&nbsp;braces&nbsp;are&nbsp;optional,
Lev Walkin26587ab2004-08-23 15:12:04 +0000275--&nbsp;the&nbsp;enumeration&nbsp;can&nbsp;be&nbsp;performed
Lev Walkineb97a702004-08-07 06:03:15 +0000276--&nbsp;automatically&nbsp;by&nbsp;the&nbsp;compiler
277ComputerOSType&nbsp;::=&nbsp;ENUMERATED&nbsp;{
278&nbsp;&nbsp;&nbsp;&nbsp;FreeBSD,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;will&nbsp;be&nbsp;0
279&nbsp;&nbsp;&nbsp;&nbsp;Windows,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;will&nbsp;be&nbsp;1
280&nbsp;&nbsp;&nbsp;&nbsp;Solaris(5),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;will&nbsp;remain&nbsp;5
281&nbsp;&nbsp;&nbsp;&nbsp;Linux,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;will&nbsp;be&nbsp;6
282&nbsp;&nbsp;&nbsp;&nbsp;MacOS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;will&nbsp;be&nbsp;7
283}
Lev Walkineb97a702004-08-07 06:03:15 +0000284</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000285</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000286
287<P>
288
289<H2><A NAME="SECTION02114000000000000000">
290The OCTET STRING type</A>
291</H2>
292
293<P>
294This type models the sequence of 8-bit bytes. This may be used to
295transmit some opaque data or data serialized by other types of encoders
296(i.e. video file, photo picture, etc).
297
298<P>
299
300<H2><A NAME="SECTION02115000000000000000">
301The OBJECT IDENTIFIER type</A>
302</H2>
303
304<P>
305The OBJECT IDENTIFIER is used to represent the unique identifier of
306any object, starting from the very root of the registration tree.
307If your organization needs to uniquely identify something (a router,
308a room, a person, a standard, or whatever), you are encouraged to
309get your own identification subtree at http://www.iana.org/protocols/forms.htm.
310
311<P>
312For example, the very first ASN.1 module in this document has the
313following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
314
315<P>
316
Lev Walkin26587ab2004-08-23 15:12:04 +0000317<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000318ExampleOID&nbsp;::=&nbsp;OBJECT&nbsp;IDENTIFIER
Lev Walkin26587ab2004-08-23 15:12:04 +0000319&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000320usageExampleModule1-oid&nbsp;ExampleOID
321&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 +0000322&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000323--&nbsp;An&nbsp;identifier&nbsp;of&nbsp;the&nbsp;Internet.
324internet-id&nbsp;OBJECT&nbsp;IDENTIFIER
325&nbsp;&nbsp;::=&nbsp;{&nbsp;iso(1)&nbsp;identified-organization(3)
326&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dod(6)&nbsp;internet(1)&nbsp;}
Lev Walkineb97a702004-08-07 06:03:15 +0000327</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000328</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000329As you see, names are optional.
330
331<P>
332
333<H2><A NAME="SECTION02116000000000000000">
334The RELATIVE-OID type</A>
335</H2>
336
337<P>
338The RELATIVE-OID type has the semantics of a subtree of an OBJECT
339IDENTIFIER. There may be no need to repeat the whole sequence of numbers
340from the root of the registration tree where the only thing of interest
341is some of the tree's subsequence.
342
343<P>
344
Lev Walkin26587ab2004-08-23 15:12:04 +0000345<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000346this-document&nbsp;RELATIVE-OID&nbsp;::=&nbsp;{&nbsp;docs(2)&nbsp;usage(1)&nbsp;}
Lev Walkin26587ab2004-08-23 15:12:04 +0000347&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000348this-example&nbsp;RELATIVE-OID&nbsp;::=&nbsp;{
349&nbsp;&nbsp;&nbsp;&nbsp;this-document&nbsp;assorted-examples(0)&nbsp;this-example(1)&nbsp;}
Lev Walkineb97a702004-08-07 06:03:15 +0000350</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000351</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000352
353<P>
354
355<H1><A NAME="SECTION02120000000000000000">
356Some of the ASN.1 String Types</A>
357</H1>
358
359<P>
360
361<H2><A NAME="SECTION02121000000000000000">
362The IA5String type</A>
363</H2>
364
365<P>
366This is essentially the ASCII, with 128 character codes available
Lev Walkin26587ab2004-08-23 15:12:04 +0000367(7 lower bits of an 8-bit byte).
Lev Walkineb97a702004-08-07 06:03:15 +0000368
369<P>
370
371<H2><A NAME="SECTION02122000000000000000">
372The UTF8String type</A>
373</H2>
374
375<P>
376This is the character string which encodes the full Unicode range
377(4 bytes) using multibyte character sequences.
378
379<P>
380
381<H2><A NAME="SECTION02123000000000000000">
382The NumericString type</A>
383</H2>
384
385<P>
386This type represents the character string with the alphabet consisting
387of numbers (''0'' to ''9'') and a space.
388
389<P>
390
391<H2><A NAME="SECTION02124000000000000000">
392The PrintableString type</A>
393</H2>
394
395<P>
396The character string with the following alphabet: space, ''<B>'</B>''
397(single quote), ''<B>(</B>'', ''<B>)</B>'', ''<B>+</B>'',
398'','' (comma), ''<B>-</B>'', ''<B>.</B>'', ''<B>/</B>'',
399digits (''0'' to ''9''), ''<B>:</B>'', ''<B>=</B>'', ''<B>?</B>'',
400upper-case and lower-case letters (''A'' to ''Z'' and ''a''
401to ''z'')
402
403<P>
404
405<H2><A NAME="SECTION02125000000000000000">
406The VisibleString type</A>
407</H2>
408
409<P>
410The character string with the alphabet which is more or less a subset
Lev Walkin26587ab2004-08-23 15:12:04 +0000411of ASCII between space and ''<B>~</B>'' (tilde).
412Alternatively, the alphabet may be described as the PrintableString
413alphabet presented earlier, plus the following characters: ''<B>!</B>'',
414''<B>''</B>'', ''<B>#</B>'', ''<B>$</B>'', ''<B>%</B>'',
415''<B>&amp;</B>'', ''<B>*</B>'', ''<B>;</B>'', ''<B>&lt;</B>'',
416''<B>&gt;</B>'', ''<B>[</B>'', ''<B>&#92;</B>'',
417''<B>]</B>'', ''<B>&#94;</B>'', ''<B>_</B>'',
418''<B>`</B>'' (single left quote), ''<B>{</B>'', ''<B>|</B>'',
419''<B>}</B>'', ''<B>~</B>''.
Lev Walkineb97a702004-08-07 06:03:15 +0000420
421<P>
422
423<H1><A NAME="SECTION02130000000000000000">
424ASN.1 Constructed Types</A>
425</H1>
426
427<P>
428
429<H2><A NAME="SECTION02131000000000000000">
430The SEQUENCE type</A>
431</H2>
432
433<P>
434This is an ordered collection of other simple or constructed types.
435The SEQUENCE constructed type resembles the C ''struct'' statement.
436
437<P>
438
Lev Walkin26587ab2004-08-23 15:12:04 +0000439<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000440Address&nbsp;::=&nbsp;SEQUENCE&nbsp;{
441&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;The&nbsp;apartment&nbsp;number&nbsp;may&nbsp;be&nbsp;omitted
442&nbsp;&nbsp;&nbsp;&nbsp;apartmentNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NumericString&nbsp;OPTIONAL,
443&nbsp;&nbsp;&nbsp;&nbsp;streetName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
444&nbsp;&nbsp;&nbsp;&nbsp;cityName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
445&nbsp;&nbsp;&nbsp;&nbsp;stateName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
446&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;This&nbsp;one&nbsp;may&nbsp;be&nbsp;omitted&nbsp;too
447&nbsp;&nbsp;&nbsp;&nbsp;zipNo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NumericString&nbsp;OPTIONAL
448}
Lev Walkineb97a702004-08-07 06:03:15 +0000449</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000450</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000451
452<P>
453
454<H2><A NAME="SECTION02132000000000000000">
455The SET type</A>
456</H2>
457
458<P>
459This is a collection of other simple or constructed types. Ordering
460is not important. The data may arrive in the order which is different
461from the order of specification. Data is encoded in the order not
462necessarily corresponding to the order of specification.
463
464<P>
465
466<H2><A NAME="SECTION02133000000000000000">
467The CHOICE type</A>
468</H2>
469
470<P>
471This type is just a choice between the subtypes specified in it. The
472CHOICE type contains at most one of the subtypes specified, and it
473is always implicitly known which choice is being decoded or encoded.
474This one resembles the C ''union'' statement.
475
476<P>
477The following type defines a response code, which may be either an
478integer code or a boolean ''true''/''false'' code.
479
480<P>
481
Lev Walkin26587ab2004-08-23 15:12:04 +0000482<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000483ResponseCode&nbsp;::=&nbsp;CHOICE&nbsp;{
484&nbsp;&nbsp;&nbsp;&nbsp;intCode&nbsp;&nbsp;&nbsp;&nbsp;INTEGER,
485&nbsp;&nbsp;&nbsp;&nbsp;boolCode&nbsp;&nbsp;&nbsp;BOOLEAN
486}
Lev Walkineb97a702004-08-07 06:03:15 +0000487</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000488</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000489
490<P>
491
492<H2><A NAME="SECTION02134000000000000000">
493The SEQUENCE OF type</A>
494</H2>
495
496<P>
497This one is the list (array) of simple or constructed types:
498
499<P>
500
Lev Walkin26587ab2004-08-23 15:12:04 +0000501<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000502--&nbsp;Example&nbsp;1
503ManyIntegers&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;INTEGER
Lev Walkin26587ab2004-08-23 15:12:04 +0000504&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000505--&nbsp;Example&nbsp;2
506ManyRectangles&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;Rectangle
Lev Walkin26587ab2004-08-23 15:12:04 +0000507&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000508--&nbsp;More&nbsp;complex&nbsp;example:
509--&nbsp;an&nbsp;array&nbsp;of&nbsp;structures&nbsp;defined&nbsp;in&nbsp;place.
510ManyCircles&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;SEQUENCE&nbsp;{
511&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
512&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 +0000513</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000514</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000515
516<P>
517
518<H2><A NAME="SECTION02135000000000000000">
519The SET OF type</A>
520</H2>
521
522<P>
523The SET OF type models the bag of structures. It resembles the SEQUENCE
524OF type, but the order is not important: i.e. the elements may arrive
525in the order which is not necessarily the same as the in-memory order
526on the remote machines.
527
528<P>
529
Lev Walkin26587ab2004-08-23 15:12:04 +0000530<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000531--&nbsp;A&nbsp;set&nbsp;of&nbsp;structures&nbsp;defined&nbsp;elsewhere
532SetOfApples&nbsp;::&nbsp;SET&nbsp;OF&nbsp;Apple
Lev Walkin26587ab2004-08-23 15:12:04 +0000533&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000534--&nbsp;Set&nbsp;of&nbsp;integers&nbsp;encoding&nbsp;the&nbsp;kind&nbsp;of&nbsp;a&nbsp;fruit
535FruitBag&nbsp;::=&nbsp;SET&nbsp;OF&nbsp;ENUMERATED&nbsp;{&nbsp;apple,&nbsp;orange&nbsp;}
Lev Walkineb97a702004-08-07 06:03:15 +0000536</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000537</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000538
539<P>
540
541<H1><A NAME="SECTION03000000000000000000">
542Using the ASN.1 Compiler</A>
543</H1>
544
545<P>
546
547<H1><A NAME="SECTION03100000000000000000">
548Introduction to the ASN.1 Compiler</A>
549</H1>
550
551<P>
552The purpose of the ASN.1 compiler, of which this document is part,
553is to convert the ASN.1 specifications to some other target language
554(currently, only C is supported<A NAME="tex2html1"
Lev Walkin26587ab2004-08-23 15:12:04 +0000555 HREF="#foot144"><SUP>2.1</SUP></A>). The compiler reads the specification and emits a series of target
Lev Walkineb97a702004-08-07 06:03:15 +0000556language structures and surrounding maintenance code. For example,
557the C structure which may be created by compiler to represent the
558simple <I>Rectangle</I> specification defined earlier in this document,
559may look like this<A NAME="tex2html2"
Lev Walkin26587ab2004-08-23 15:12:04 +0000560 HREF="#foot379"><SUP>2.2</SUP></A>:
Lev Walkineb97a702004-08-07 06:03:15 +0000561
562<P>
563
Lev Walkin26587ab2004-08-23 15:12:04 +0000564<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000565typedef&nbsp;struct&nbsp;Rectangle_s&nbsp;{
566&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;height;
567&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;width;
568}&nbsp;Rectangle_t;
Lev Walkineb97a702004-08-07 06:03:15 +0000569</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000570</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000571This would not be of much value for such a simple specification, so
572the compiler goes further and actually produces the code which fills
Lev Walkin26587ab2004-08-23 15:12:04 +0000573in this structure by parsing the opaque binary<A NAME="tex2html3"
574 HREF="#foot151"><SUP>2.3</SUP></A> data provided in some buffer. It also produces the code that takes
Lev Walkineb97a702004-08-07 06:03:15 +0000575this structure as an argument and performs structure serialization
576by emitting a series of bytes.
577
578<P>
579
Lev Walkin26587ab2004-08-23 15:12:04 +0000580<H1><A NAME="SECTION03200000000000000000">
Lev Walkineb97a702004-08-07 06:03:15 +0000581Quick start</A>
582</H1>
583
584<P>
Lev Walkin26587ab2004-08-23 15:12:04 +0000585After building and installing the compiler, the <I>asn1c</I><A NAME="tex2html4"
586 HREF="#foot380"><SUP>3.1</SUP></A> command may be used to compile the ASN.1 specification<A NAME="tex2html5"
587 HREF="#foot381"><SUP>3.2</SUP></A>:
Lev Walkineb97a702004-08-07 06:03:15 +0000588
589<P>
590
Lev Walkin26587ab2004-08-23 15:12:04 +0000591<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000592asn1c&nbsp;<I>&lt;spec.asn1&gt;</I>
Lev Walkineb97a702004-08-07 06:03:15 +0000593</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000594</BLOCKQUOTE>
595If several specifications contain interdependencies, all of the files
596must be specified altogether:
Lev Walkineb97a702004-08-07 06:03:15 +0000597
598<P>
599
Lev Walkin26587ab2004-08-23 15:12:04 +0000600<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000601asn1c&nbsp;<I>&lt;spec1.asn1&gt;&nbsp;&lt;spec2.asn1&gt;&nbsp;...</I>
Lev Walkineb97a702004-08-07 06:03:15 +0000602</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000603</BLOCKQUOTE>
604The compiler <B>-E</B> and <B>-EF</B> options are used for testing
605the parser and the semantic fixer, respectively. These options will
606instruct the compiler to dump out the parsed (and fixed, if <B>-F</B>
607is involved) ASN.1 specification as it was &#34;understood&#34;
608by the compiler. It might be useful to check whether a particular
609syntactic construction is properly supported by the compiler.
Lev Walkineb97a702004-08-07 06:03:15 +0000610
611<P>
612
Lev Walkin26587ab2004-08-23 15:12:04 +0000613<BLOCKQUOTE><PRE>
614asn1c&nbsp;<B>-EF</B>&nbsp;<I>&lt;spec-to-test.asn1&gt;</I>
Lev Walkineb97a702004-08-07 06:03:15 +0000615</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000616</BLOCKQUOTE>
617The <B>-P</B> option is used to dump the compiled output on the
618screen instead of creating a bunch of .c and .h files on disk in the
619current directory. You would probably want to start with <B>-P</B>
620option instead of creating a mess in your current directory. Another
621option, <B>-R</B>, asks compiler to only generate the files which
622need to be generated, and supress linking in the numerous support
623files.
624
625<P>
626Print the compiled output instead of creating multiple source files:
Lev Walkineb97a702004-08-07 06:03:15 +0000627
628<P>
629
Lev Walkin26587ab2004-08-23 15:12:04 +0000630<BLOCKQUOTE><PRE>
631asn1c&nbsp;<B>-P</B>&nbsp;<I>&lt;spec-to-compile-and-print.asn1&gt;</I>
632</PRE>
633</BLOCKQUOTE>
634
635<P>
636
637<H1><A NAME="SECTION03300000000000000000">
638Using the ASN.1 Compiler</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000639</H1>
640
641<P>
642
Lev Walkin26587ab2004-08-23 15:12:04 +0000643<H1><A NAME="SECTION03310000000000000000">
644Command-line options</A>
645</H1>
646
647<P>
648The <A HREF=#Table1>Table 1</A> summarizes various options affecting
649the compiler's behavior.
650
651<P>
652<BR><P></P>
653<DIV ALIGN="CENTER"><A NAME="384"></A>
654<TABLE>
655<CAPTION><STRONG><A NAME=Table1>Table 1:</A></STRONG>
656The list of asn1c command line options</CAPTION>
657<TR><TD><TABLE COLS=2 BORDER FRAME=BOX RULES=GROUPS>
658<COLGROUP><COL ALIGN=LEFT><COLGROUP><COL ALIGN=JUSTIFY WIDTH="3in">
659<TBODY>
660<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>
661<B>Overall Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
662<B>Description</B></TD></TR>
663</TBODY><TBODY>
664<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-E</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
665<FONT SIZE="-1">Stop after the parsing stage and print the reconstructed ASN.1
666specification code to the standard output.</FONT></TD></TR>
667</TBODY><TBODY>
668<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-F</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
669<FONT SIZE="-1">Used together with -E, instructs the compiler to stop after
670the ASN.1 syntax tree fixing stage and dump the reconstructed ASN.1
671specification to the standard output.</FONT></TD></TR>
672</TBODY><TBODY>
673<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-P</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
674<FONT SIZE="-1">Dump the compiled output to the standard output instead of
675cre- ating the target language files on disk.</FONT></TD></TR>
676</TBODY><TBODY>
677<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-R</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
678<FONT SIZE="-1">Restrict the compiler to generate only the ASN.1 tables, omit-
679ting the usual support code.</FONT></TD></TR>
680</TBODY><TBODY>
681<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-S <I>&lt;directory&gt;</I></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
682<FONT SIZE="-1">Use the specified directory with ASN.1 skeleton files.</FONT></TD></TR>
683</TBODY><TBODY>
684<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-t <I>&lt;data-string&gt;</I></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
685<FONT SIZE="-1">Interpret the data-string as a sequence of hexadecimal values
686representing the start of BER TLV encoding. Print the human readable
687explanation.</FONT></TD></TR>
688</TBODY><TBODY>
689<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Warning Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
690<B>Description</B></TD></TR>
691</TBODY><TBODY>
692<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Werror</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
693<FONT SIZE="-1">Treat warnings as errors; abort if any warning is produced.</FONT></TD></TR>
694</TBODY><TBODY>
695<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-lexer</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
696<FONT SIZE="-1">Enable lexer debugging during the ASN.1 parsing stage.</FONT></TD></TR>
697</TBODY><TBODY>
698<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-fixer</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
699 <FONT SIZE="-1">Enable ASN.1 syntax tree fixer debugging during the
700 fixing stage.</FONT></TD></TR>
701</TBODY><TBODY>
702<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-compiler</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
703<FONT SIZE="-1">Enable debugging during the actual compile time.</FONT></TD></TR>
704</TBODY><TBODY>
705<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Language Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
706<B>Description</B></TD></TR>
707</TBODY><TBODY>
708<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fbless-SIZE</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
709<FONT SIZE="-1">Allow SIZE() constraint for INTEGER, ENUMERATED, and other
710types for which this constraint is normally prohibited by the standard.
711This is a violation of an ASN.1 standard and compiler may fail to
712produce the meaningful code.</FONT></TD></TR>
713</TBODY><TBODY>
714<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fnative-integers</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
715<FONT SIZE="-1">Use native machine's integer types whenever possible, instead
716of the complex ASN.1 INTEGER and ENUMERATED types. </FONT></TD></TR>
717</TBODY><TBODY>
718<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-funnamed-unions</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
719<FONT SIZE="-1">Enable unnamed unions in the definitions of target language's
720structures.</FONT></TD></TR>
721</TBODY><TBODY>
722<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-ftypes88</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
723<FONT SIZE="-1">Use only ASN.1:1988 embedded types.</FONT></TD></TR>
724</TBODY><TBODY>
725<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Output Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
726<B>Description</B></TD></TR>
727</TBODY><TBODY>
728<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-print-constraints</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
729<FONT SIZE="-1">When -EF are also specified, this option forces the compiler
730to explain its internal understanding of subtype constraints.</FONT></TD></TR>
731</TBODY><TBODY>
732<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-print-lines</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
733<FONT SIZE="-1">Generate &#34;- #line&#34; comments in -E output.</FONT></TD></TR>
734</TBODY>
735</TABLE>
736
737<P>
738</TD></TR>
739</TABLE>
740</DIV><P></P><BR>
741
742<P>
743
744<H1><A NAME="SECTION03320000000000000000">
Lev Walkineb97a702004-08-07 06:03:15 +0000745Recognizing compiler output</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000746</H1>
Lev Walkineb97a702004-08-07 06:03:15 +0000747
748<P>
749After compiling, the following entities will be created in your current
750directory:
751
752<P>
753
754<UL>
755<LI>A set of .c and .h files, generally a single pair for each type defined
756in the ASN.1 specifications. These files will be named similarly to
757the ASN.1 types (<I>Rectangle.c</I> and <I>Rectangle.h</I> for the
758specification defined in the beginning of this document).
759</LI>
760<LI>A set of helper .c and .h files which contain generic encoders, decoders
Lev Walkin26587ab2004-08-23 15:12:04 +0000761and other useful routines. There will be quite a few of them, some
762of them even are not always necessary, but the overall amount of code
763after compiling will be rather small anyway.
Lev Walkineb97a702004-08-07 06:03:15 +0000764</LI>
765</UL>
766It is your responsibility to create .c file with the <I>int main()</I>
767routine and the Makefile (if needed). Compiler helps you with the
768latter by creating the Makefile.am.sample, containing the skeleton
769definition for the automake, should you want to use autotools.
770
771<P>
772In other words, after compiling the Rectangle module, you have the
773following set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h,
774<B>...</B> }, where <B>''...''</B> stands for the
775set of additional ''helper'' files created by the compiler. If you
776add the simple file with the <I>int main()</I> routine, it would even
777be possible to compile everything with the single instruction:
778
779<P>
780
Lev Walkin26587ab2004-08-23 15:12:04 +0000781<BLOCKQUOTE><PRE>
782cc&nbsp;-o&nbsp;rectangle&nbsp;*.c&nbsp;&nbsp;&nbsp;#&nbsp;It&nbsp;could&nbsp;be&nbsp;<I>that</I>&nbsp;simple<A NAME="tex2html7"
783 HREF="#foot387"><SUP>4.1</SUP></A>
Lev Walkineb97a702004-08-07 06:03:15 +0000784</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000785</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000786
787<P>
788
Lev Walkin26587ab2004-08-23 15:12:04 +0000789<H1><A NAME="SECTION03330000000000000000">
Lev Walkineb97a702004-08-07 06:03:15 +0000790Invoking the ASN.1 helper code from the application</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000791</H1>
Lev Walkineb97a702004-08-07 06:03:15 +0000792
793<P>
Lev Walkin26587ab2004-08-23 15:12:04 +0000794First of all, you should to include one or more header files into
795your application. For our Rectangle module, including the Rectangle.h
Lev Walkineb97a702004-08-07 06:03:15 +0000796file is enough:
797
798<P>
799
Lev Walkin26587ab2004-08-23 15:12:04 +0000800<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000801#include&nbsp;&lt;Rectangle.h&gt;
Lev Walkineb97a702004-08-07 06:03:15 +0000802</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000803</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000804The header files defines the C structure corresponding to the ASN.1
805definition of a rectangle and the declaration of the ASN.1 type descriptor,
806which is used as an argument to most of the functions provided by
807the ASN.1 module. For example, here is the code which frees the Rectangle_t
808structure:
809
810<P>
811
Lev Walkin26587ab2004-08-23 15:12:04 +0000812<BLOCKQUOTE><PRE>
813Rectangle_t&nbsp;*rect&nbsp;=&nbsp;...;
814&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000815asn1_DEF_Rectangle-&gt;free_struct(&amp;asn1_DEF_Rectangle,
816&nbsp;&nbsp;&nbsp;&nbsp;rect,&nbsp;0);
Lev Walkineb97a702004-08-07 06:03:15 +0000817</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000818</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000819This code defines a <I>rect</I> pointer which points to the Rectangle_t
820structure which needs to be freed. The second line invokes the generic
821free_struct routine created specifically for this Rectangle_t structure.
822The <I>asn1_DEF_Rectangle</I> is the type descriptor, which holds
823a collection of generic routines to deal with the Rectangle_t structure.
824
825<P>
826There are several generic functions available:
827
828<P>
829<DL>
830<DT><STRONG>check_constraints</STRONG></DT>
831<DD>Check that the contents of the target structure
832are semantically valid and constrained to appropriate implicit or
833explicit subtype constraints. Please refer to Section sub:Validating-the-target.
834</DD>
835<DT><STRONG>ber_decoder</STRONG></DT>
Lev Walkin26587ab2004-08-23 15:12:04 +0000836<DD>This is the generic <I>restartable</I><A NAME="tex2html8"
837 HREF="#foot241"><SUP>4.2</SUP></A> BER decoder (Basic Encoding Rules). This decoder would create
Lev Walkineb97a702004-08-07 06:03:15 +0000838and/or fill the target structure for you. Please refer to Section
839[<A HREF="#sub:Decoding-BER">Decoding-BER</A>].
840</DD>
841<DT><STRONG>der_encoder</STRONG></DT>
842<DD>This is the generic DER encoder (Distinguished Encoding
843Rules). This decoder will take the target structure and encode it
844into a series of bytes. Please refer to Section [<A HREF="#sub:Encoding-DER">Encoding-DER</A>].
845</DD>
846<DT><STRONG>print_struct</STRONG></DT>
847<DD>This function convert the contents of the passed target
848structure into human readable form. This form is not formal and cannot
849be converted back into the structure, but it may turn out to be useful
850for debugging or quick-n-dirty printing. Please refer to Section [<A HREF="#sub:Printing-the-target">Printing-the-target</A>].
851</DD>
852<DT><STRONG>free_struct</STRONG></DT>
853<DD>This is a generic disposal which frees the target structure.
854Please refer to Section [<A HREF="#sub:Freeing-the-target">Freeing-the-target</A>].
855</DD>
856</DL>
857Each of the above function takes the type descriptor (<I>asn1_DEF_...</I>)
858and the target structure (<I>rect</I>, in the above example). The
859target structure is typically created by the generic BER decoder or
860by the application itself.
861
862<P>
863Here is how the buffer can be deserialized into the structure:
864
865<P>
866
Lev Walkin26587ab2004-08-23 15:12:04 +0000867<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000868Rectangle_t&nbsp;*
Lev Walkin26587ab2004-08-23 15:12:04 +0000869simple_deserializer(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;buf_size)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +0000870&nbsp;&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;*rect&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Note&nbsp;this&nbsp;0!&nbsp;*/
871&nbsp;&nbsp;&nbsp;&nbsp;ber_dec_rval_t&nbsp;rval;
872&nbsp;
873&nbsp;&nbsp;&nbsp;&nbsp;rval&nbsp;=&nbsp;asn1_DEF_Rectangle-&gt;ber_decoder(
874&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn1_DEF_Rectangle,
875&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void&nbsp;**)&amp;rect,
876&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size,
877&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0);
878&nbsp;
879&nbsp;&nbsp;&nbsp;&nbsp;if(rval<B>.code</B>&nbsp;==&nbsp;RC_OK)&nbsp;{
880&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;*/
881&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
Lev Walkin26587ab2004-08-23 15:12:04 +0000882&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Free&nbsp;partially&nbsp;decoded&nbsp;rect&nbsp;*/
Lev Walkineb97a702004-08-07 06:03:15 +0000883&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn1_DEF_Rectangle-&gt;free_struct(
884&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn1_DEF_Rectangle,&nbsp;rect,&nbsp;0);
885&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;
886&nbsp;&nbsp;&nbsp;&nbsp;}
887}
Lev Walkineb97a702004-08-07 06:03:15 +0000888</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000889</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000890The code above defines a function, <I>simple_deserializer</I>, which
891takes a buffer and its length and expected to return a pointer to
892the Rectangle_t structure. Inside, it tries to convert the bytes
893passed into the target structure (rect) using the generic BER decoder
894and returns the rect pointer afterwards. If the structure cannot be
895deserialized, it frees the memory which might be left allocated by
896the unfinished <I>ber_decoder</I> routine and returns NULL. <B>This
897freeing is necessary</B> because the ber_decoder is a restartable procedure,
898and may fail just because there is more data needs to be provided
899before decoding could be finalized. The code above obviously does
900not take into account the way the <I>ber_decoder</I> failed, so the
901freeing is necessary because the part of the buffer may already be
902decoded into the structure by the time something goes wrong.
903
904<P>
905Restartable decoding is a little bit trickier: you need to provide
906the old target structure pointer (which might be already half-decoded)
907and react on RC_WMORE return code. This will be explained later in
908Section sub:Decoding-BER
909
910<P>
911
Lev Walkin26587ab2004-08-23 15:12:04 +0000912<H2><A NAME="SECTION03331000000000000000"></A><A NAME="sub:Decoding-BER"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +0000913Decoding BER
Lev Walkin26587ab2004-08-23 15:12:04 +0000914</H2>
Lev Walkineb97a702004-08-07 06:03:15 +0000915
916<P>
917The Basic Encoding Rules describe the basic way how the structure
918can be encoded and decoded. Several other encoding rules (CER, DER)
919define a more restrictive versions of BER, so the generic BER parser
920is also capable of decoding the data encoded by CER and DER encoders.
921The opposite is not true.
922
923<P>
924The ASN.1 compiler provides the generic BER decoder which is implicitly
925capable of decoding BER, CER and DER encoded data.
926
927<P>
928The decoder is restartable (stream-oriented), which means that in
929case the buffer has less data than it is expected, the decoder will
930process whatever it is available and ask for more data to be provided.
931Please note that the decoder may actually process less data than it
932is given in the buffer, which means that you should be able to make
933the next buffer contain the unprocessed part of the previous buffer.
934
935<P>
936Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
937
938<P>
939
940<UL>
941<LI>You may concatenate these buffers and feed the BER decoder with 300
942bytes of data, or
943</LI>
944<LI>You may feed it the first buffer of 100 bytes of data, realize that
945the ber_decoder consumed only 95 bytes from it and later feed the
946decoder with 205 bytes buffer which consists of 5 unprocessed bytes
947from the first buffer and the latter 200 bytes from the second buffer.
948</LI>
949</UL>
950This is not as convenient as it could be (like, the BER encoder would
951consume the whole 100 bytes and keep these 5 bytes in some temporary
952storage), but in case of stream-based processing it might actually
953be OK. Suggestions are welcome.
954
955<P>
956There are two ways to invoke a BER decoder. The first one is a direct
957reference of the type-specific decoder. This way was shown in the
958previous example of <I>simple_deserializer</I> function. The second
959way is to invoke a <I>ber_decode</I> function, which is just a simple
960wrapper of the former approach into a less wordy notation:
961
962<P>
963
Lev Walkin26587ab2004-08-23 15:12:04 +0000964<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000965rval&nbsp;=&nbsp;ber_decode(&amp;asn1_DEF_Rectangle,&nbsp;(void&nbsp;**)&amp;rect,
966&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size);
Lev Walkineb97a702004-08-07 06:03:15 +0000967</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000968</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000969Note that the initial (asn1_DEF_Rectangle-&gt;ber_decoder) reference
970is gone, and also the last argument (0) is no longer necessary.
971
972<P>
973These two ways of invocations are fully equivalent.
974
975<P>
976The BER de<I>coder</I> may fail because (<I>the following RC_...
977codes are defined in ber_decoder.h</I>):
978
979<P>
980
981<UL>
982<LI>RC_WMORE: There is more data expected than it is provided (stream
983mode continuation feature);
984</LI>
985<LI>RC_FAIL: General failure to decode the buffer;
986</LI>
987<LI>... other codes may be defined as well.
988</LI>
989</UL>
990Together with the return code (.code) the ber_dec_rval_t type contains
991the number of bytes which is consumed from the buffer. In the previous
992hypothetical example of two buffers (of 100 and 200 bytes), the first
993call to ber_decode() would return with .code = RC_WMORE and .consumed
994= 95. The .consumed field of the BER decoder return value is <B>always</B>
995valid, even if the decoder succeeds or fails with any other return
996code.
997
998<P>
999Please look into ber_decoder.h for the precise definition of ber_decode()
1000and related types.
1001
1002<P>
1003
Lev Walkin26587ab2004-08-23 15:12:04 +00001004<H2><A NAME="SECTION03332000000000000000"></A><A NAME="sub:Encoding-DER"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001005Encoding DER
Lev Walkin26587ab2004-08-23 15:12:04 +00001006</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001007
1008<P>
1009The Distinguished Encoding Rules is the variant of BER encoding rules
1010which is oriented on representing the structures with length known
1011beforehand. This is probably exactly how you want to encode: either
1012after a BER decoding or after a manual fill-up, the target structure
1013contains the data which size is implicitly known before encoding.
1014The DER encoding is used, for example, to encode X.509 certificates.
1015
1016<P>
1017As with BER decoder, the DER encoder may be invoked either directly
1018from the ASN.1 type descriptor (asn1_DEF_Rectangle) or from the
1019stand-alone function, which is somewhat simpler:
1020
1021<P>
1022
Lev Walkin26587ab2004-08-23 15:12:04 +00001023<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +00001024/*
1025&nbsp;*&nbsp;This&nbsp;is&nbsp;a&nbsp;custom&nbsp;function&nbsp;which&nbsp;writes&nbsp;the
1026&nbsp;*&nbsp;encoded&nbsp;output&nbsp;into&nbsp;some&nbsp;FILE&nbsp;stream.
1027&nbsp;*/
Lev Walkin26587ab2004-08-23 15:12:04 +00001028static&nbsp;int
1029write_stream(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;size,&nbsp;void&nbsp;*app_key)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +00001030&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;*ostream&nbsp;=&nbsp;app_key;
1031&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;wrote;
1032&nbsp;
1033&nbsp;&nbsp;&nbsp;&nbsp;wrote&nbsp;=&nbsp;fwrite(buffer,&nbsp;1,&nbsp;size,&nbsp;ostream);
1034&nbsp;
1035&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(wrote&nbsp;==&nbsp;size)&nbsp;?&nbsp;0&nbsp;:&nbsp;-1;
1036}
1037&nbsp;
1038/*
1039&nbsp;*&nbsp;This&nbsp;is&nbsp;the&nbsp;serializer&nbsp;itself,
1040&nbsp;*&nbsp;it&nbsp;supplies&nbsp;the&nbsp;DER&nbsp;encoder&nbsp;with&nbsp;the
1041&nbsp;*&nbsp;pointer&nbsp;to&nbsp;the&nbsp;custom&nbsp;output&nbsp;function.
1042&nbsp;*/
1043ssize_t
1044simple_serializer(FILE&nbsp;*ostream,&nbsp;Rectangle_t&nbsp;*rect)&nbsp;{
1045&nbsp;&nbsp;&nbsp;&nbsp;der_enc_rval_t&nbsp;rval;&nbsp;&nbsp;/*&nbsp;Return&nbsp;value&nbsp;*/
1046&nbsp;
1047&nbsp;&nbsp;&nbsp;&nbsp;rval&nbsp;=&nbsp;der_encode(&amp;asn1_DEF_Rect,&nbsp;rect,
Lev Walkin26587ab2004-08-23 15:12:04 +00001048&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_stream,&nbsp;ostream);
Lev Walkineb97a702004-08-07 06:03:15 +00001049&nbsp;&nbsp;&nbsp;&nbsp;if(rval<B>.encoded</B>&nbsp;==&nbsp;-1)&nbsp;{
1050&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*
1051&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Failure&nbsp;to&nbsp;encode&nbsp;the&nbsp;rectangle&nbsp;data.
1052&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
1053&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''Cannot&nbsp;encode&nbsp;%s:&nbsp;%s&#92;n'',
1054&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rval<B>.failed_type</B>-&gt;name,
1055&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strerror(errno));
1056&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-1;
1057&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
1058&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Return&nbsp;the&nbsp;number&nbsp;of&nbsp;bytes&nbsp;*/
1059&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;rval.encoded;
1060&nbsp;&nbsp;&nbsp;&nbsp;}
1061}
Lev Walkineb97a702004-08-07 06:03:15 +00001062</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001063</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001064As you see, the DER encoder does not write into some sort of buffer
1065or something. It just invokes the custom function (possible, multiple
1066times) which would save the data into appropriate storage. The optional
1067argument <I>app_key</I> is opaque for the DER encoder code and just
1068used by <I>_write_stream()</I> as the pointer to the appropriate
1069output stream to be used.
1070
1071<P>
1072If the custom write function is not given (passed as 0), then the
1073DER encoder will essentially do the same thing (i.e., encode the data)
1074but no callbacks will be invoked (so the data goes nowhere). It may
1075prove useful to determine the size of the structure's encoding before
Lev Walkin26587ab2004-08-23 15:12:04 +00001076actually doing the encoding<A NAME="tex2html9"
1077 HREF="#foot313"><SUP>4.3</SUP></A>.
Lev Walkineb97a702004-08-07 06:03:15 +00001078
1079<P>
1080Please look into der_encoder.h for the precise definition of der_encode()
1081and related types.
1082
1083<P>
1084
Lev Walkin26587ab2004-08-23 15:12:04 +00001085<H2><A NAME="SECTION03333000000000000000"></A><A NAME="sub:Validating-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001086Validating the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +00001087</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001088
1089<P>
1090Sometimes the target structure needs to be validated. For example,
1091if the structure was created by the application (as opposed to being
1092decoded from some external source), some important information required
1093by the ASN.1 specification might be missing. On the other hand, the
1094successful decoding of the data from some external source does not
1095necessarily mean that the data is fully valid either. It might well
1096be the case that the specification describes some subtype constraints
1097that were not taken into account during decoding, and it would actually
1098be useful to perform the last check when the data is ready to be encoded
1099or when the data has just been decoded to ensure its validity according
1100to some stricter rules.
1101
1102<P>
1103The asn_check_constraints() function checks the type for various
1104implicit and explicit constraints. It is recommended to use asn_check_constraints()
1105function after each decoding and before each encoding.
1106
1107<P>
1108Please look into constraints.h for the precise definition of asn_check_constraints()
1109and related types.
1110
1111<P>
1112
Lev Walkin26587ab2004-08-23 15:12:04 +00001113<H2><A NAME="SECTION03334000000000000000"></A><A NAME="sub:Printing-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001114Printing the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +00001115</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001116
1117<P>
1118There are two ways to print the target structure: either invoke the
1119print_struct member of the ASN.1 type descriptor, or using the asn_fprint()
1120function, which is a simpler wrapper of the former:
1121
1122<P>
1123
Lev Walkin26587ab2004-08-23 15:12:04 +00001124<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +00001125asn_fprint(stdout,&nbsp;&amp;asn1_DEF_Rectangle,&nbsp;rect);
Lev Walkineb97a702004-08-07 06:03:15 +00001126</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001127</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001128Please look into constr_TYPE.h for the precise definition of asn_fprint()
1129and related types.
1130
1131<P>
1132
Lev Walkin26587ab2004-08-23 15:12:04 +00001133<H2><A NAME="SECTION03335000000000000000"></A><A NAME="sub:Freeing-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +00001134Freeing the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +00001135</H2>
Lev Walkineb97a702004-08-07 06:03:15 +00001136
1137<P>
1138Freeing the structure is slightly more complex than it may seem to.
1139When the ASN.1 structure is freed, all the members of the structure
1140and their submembers etc etc are recursively freed too. But it might
1141not be feasible to free the structure itself. Consider the following
1142case:
1143
1144<P>
1145
Lev Walkin26587ab2004-08-23 15:12:04 +00001146<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +00001147struct&nbsp;my_figure&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;custom&nbsp;structure&nbsp;*/
1148&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;*/
1149&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;type&nbsp;is&nbsp;generated&nbsp;by&nbsp;the&nbsp;ASN.1&nbsp;compiler&nbsp;*/
1150&nbsp;&nbsp;&nbsp;&nbsp;<I>Rectangle_t&nbsp;rect;</I>
1151&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;other&nbsp;members&nbsp;of&nbsp;the&nbsp;structure&nbsp;*/
1152};
Lev Walkineb97a702004-08-07 06:03:15 +00001153</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001154</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001155In this example, the application programmer defined a custom structure
1156with one ASN.1-derived member (rect). This member is not a reference
1157to the Rectangle_t, but an in-place inclusion of the Rectangle_t
1158structure. If the freeing is necessary, the usual procedure of freeing
1159everything must not be applied to the &amp;rect pointer itself, because
1160it does not point to the memory block directly allocated by memory
1161allocation routine, but instead lies within such a block allocated
1162for my_figure structure.
1163
1164<P>
1165To solve this problem, the free_struct routine has the additional
1166argument (besides the intuitive type descriptor and target structure
1167pointers), which is the flag specifying whether the outer pointer
1168itself must be freed (0, default) or it should be left intact (non-zero
1169value).
1170
1171<P>
1172
Lev Walkin26587ab2004-08-23 15:12:04 +00001173<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +00001174/*&nbsp;Rectangle_t&nbsp;is&nbsp;defined&nbsp;within&nbsp;my_figure&nbsp;*/
1175struct&nbsp;my_figure&nbsp;*mf&nbsp;=&nbsp;<B>...</B>;
1176/*
1177&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_td
1178&nbsp;*&nbsp;without&nbsp;freeing&nbsp;the&nbsp;mf-&gt;rect&nbsp;pointer
1179&nbsp;*/
1180asn1_DEF_Rectangle-&gt;free_struct(
1181&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn1_DEF_Rectangle,&nbsp;&amp;mf-&gt;rect,&nbsp;<I>1</I>&nbsp;/*&nbsp;!free&nbsp;*/);
1182&nbsp;
1183/*&nbsp;Rectangle_t&nbsp;is&nbsp;a&nbsp;stand-alone&nbsp;pointer&nbsp;*/
1184Rectangle_t&nbsp;*rect&nbsp;=&nbsp;<B>...</B>;
1185/*
1186&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_t
1187&nbsp;*&nbsp;and&nbsp;freeing&nbsp;the&nbsp;rect&nbsp;pointer
1188&nbsp;*/
1189asn1_DEF_Rectangle-&gt;free_struct(
1190&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn1_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 +00001191</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +00001192</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +00001193It is safe to invoke the <I>free_struct</I> function with the target
1194structure pointer set to 0 (NULL), the function will do nothing.
1195
1196<P>
1197
1198<H2><A NAME="SECTION04000000000000000000">
1199Bibliography</A>
Lev Walkin26587ab2004-08-23 15:12:04 +00001200</H2><DL COMPACT><DD><P></P><DT><A NAME="ASN1C">ASN1C</A>
1201<DD>Free ASN.1 Compiler. http://lionet.info/asn1/
1202<P></P><DT><A NAME="Dub00">Dub00</A>
Lev Walkineb97a702004-08-07 06:03:15 +00001203<DD>Olivier Dubuisson - <I>ASN.1 Communication between heterogeneous
1204systems</I> - Morgan Kaufmann Publishers, 2000. http://asn1.elibel.tm.fr/en/book/.
1205ISBN:0-12-6333361-0.
1206<P></P><DT><A NAME="ITU-T_ASN.1">ITU-T/ASN.1</A>
1207<DD>ITU-T Study Group 17 - Languages for Telecommunication Systems http://www.itu.int/ITU-T/studygroups/com17/languages/</DL>
1208
1209<P>
1210<BR><HR><H4>Footnotes</H4>
1211<DL>
Lev Walkin26587ab2004-08-23 15:12:04 +00001212<DT><A NAME="foot144">... supported</A><A
Lev Walkineb97a702004-08-07 06:03:15 +00001213 HREF="asn1c-usage.html#tex2html1"><SUP>2.1</SUP></A></DT>
Lev Walkin26587ab2004-08-23 15:12:04 +00001214<DD>C++ is ''supported'' too, as long as an class-based approach is
1215not a definitive factor.
Lev Walkineb97a702004-08-07 06:03:15 +00001216
1217</DD>
Lev Walkin26587ab2004-08-23 15:12:04 +00001218<DT><A NAME="foot379">... this</A><A
Lev Walkineb97a702004-08-07 06:03:15 +00001219 HREF="asn1c-usage.html#tex2html2"><SUP>2.2</SUP></A></DT>
1220<DD><I>-fnative-integers</I> compiler option is used to produce basic
Lev Walkin26587ab2004-08-23 15:12:04 +00001221C <I>int</I> types instead of infinite width INTEGER_t structures.
1222See <A HREF=#Table1>Table 1</A>.
Lev Walkineb97a702004-08-07 06:03:15 +00001223
1224</DD>
Lev Walkin26587ab2004-08-23 15:12:04 +00001225<DT><A NAME="foot151">... binary</A><A
Lev Walkineb97a702004-08-07 06:03:15 +00001226 HREF="asn1c-usage.html#tex2html3"><SUP>2.3</SUP></A></DT>
1227<DD>BER, CER and DER encodings are binary. However, the XER encoding is
1228text (XML) based.
1229
1230</DD>
Lev Walkin26587ab2004-08-23 15:12:04 +00001231<DT><A NAME="foot380">...asn1c</A><A
1232 HREF="asn1c-usage.html#tex2html4"><SUP>3.1</SUP></A></DT>
1233<DD>The 1 symbol in asn<B>1</B>c is a digit, not an ''ell'' letter.
1234
1235</DD>
1236<DT><A NAME="foot381">... specification</A><A
1237 HREF="asn1c-usage.html#tex2html5"><SUP>3.2</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001238<DD>This is probably <B>not</B> what you want to try out right now -
Lev Walkin26587ab2004-08-23 15:12:04 +00001239read through the rest of this chapter to find out about <B>-P</B>
1240and <B>-R</B> options.
Lev Walkineb97a702004-08-07 06:03:15 +00001241
1242</DD>
Lev Walkin26587ab2004-08-23 15:12:04 +00001243<DT><A NAME="foot387">...that&nbsp;simple</A><A
1244 HREF="asn1c-usage.html#tex2html7"><SUP>4.1</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001245<DD>Provided that you've also created a .c file with the <I>int main()</I>
1246routine.
1247
1248</DD>
Lev Walkin26587ab2004-08-23 15:12:04 +00001249<DT><A NAME="foot241">...restartable</A><A
1250 HREF="asn1c-usage.html#tex2html8"><SUP>4.2</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001251<DD>Restartable means that if the decoder encounters the end of the buffer,
1252it will fail, but may later be invoked again with the rest of the
1253buffer to continue decoding.
1254
1255</DD>
Lev Walkin26587ab2004-08-23 15:12:04 +00001256<DT><A NAME="foot313">... encoding</A><A
1257 HREF="asn1c-usage.html#tex2html9"><SUP>4.3</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001258<DD>It is actually faster too: the encoder might skip over some computations
1259which aren't important for the size determination.
1260
1261</DD>
1262</DL><BR><HR>
1263<ADDRESS>
1264Lev Walkin
Lev Walkin26587ab2004-08-23 15:12:04 +000012652004-08-23
Lev Walkineb97a702004-08-07 06:03:15 +00001266</ADDRESS>
1267</BODY>
1268</HTML>