blob: 239e77f443b52211a808bb171287b7df1b90a73e [file] [log] [blame]
Lev Walkineb97a702004-08-07 06:03:15 +00001
2<!--Converted with LaTeX2HTML 2002-2-1 (1.70)
3original version by: Nikos Drakos, CBLU, University of Leeds
4* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan
5* with significant contributions from:
6 Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
7<HTML>
8<HEAD>
Lev Walkindee61782005-02-02 09:53:46 +00009<TITLE>Using the Open Source ASN.1 Compiler</TITLE>
10<META NAME="description" CONTENT="Using the Open Source ASN.1 Compiler">
Lev Walkin1411d052005-02-02 11:06:38 +000011<META NAME="keywords" CONTENT="ASN.1, asn1c, compiler, BER, DER, XER">
Lev Walkineb97a702004-08-07 06:03:15 +000012
Lev Walkineb97a702004-08-07 06:03:15 +000013
14
15</HEAD>
16
17<BODY >
18
19<P>
20
21<P>
22
23<P>
Lev Walkindee61782005-02-02 09:53:46 +000024<H1 ALIGN="CENTER">Using the Open Source ASN.1 Compiler</H1><DIV>
Lev Walkineb97a702004-08-07 06:03:15 +000025
Lev Walkin28c89eb2004-09-30 22:45:58 +000026<P ALIGN="CENTER"><STRONG>Lev Walkin &lt;<A HREF=mailto:vlm@lionet.info?Subject=asn1c>vlm@lionet.info</A>&gt;</STRONG></P>
Lev Walkineb97a702004-08-07 06:03:15 +000027</DIV>
Lev Walkin28c89eb2004-09-30 22:45:58 +000028
29<P>
Lev Walkin683f9b72004-09-26 13:41:45 +000030<!-- MATH
31 $Revision$
32 -->
Lev Walkin28c89eb2004-09-30 22:45:58 +000033<FONT COLOR=red><B>Download the <A HREF=asn1c-usage.pdf>PDF</A> version</B></FONT>
Lev Walkin26587ab2004-08-23 15:12:04 +000034
35<P>
Lev Walkineb97a702004-08-07 06:03:15 +000036<BR>
37
38<H2><A NAME="SECTION01000000000000000000">
39Contents</A>
40</H2>
41<!--Table of Contents-->
42
43<UL>
Lev Walkin7f70fe52005-02-22 07:28:26 +000044<LI><A NAME="tex2html51"
45 HREF="asn1c-usage.html#SECTION02000000000000000000">Using the ASN.1 Compiler</A>
46<UL>
47<LI><A NAME="tex2html52"
48 HREF="asn1c-usage.html#SECTION02100000000000000000">Introduction to the ASN.1 Compiler</A>
49<UL>
Lev Walkindee61782005-02-02 09:53:46 +000050<LI><A NAME="tex2html53"
Lev Walkin7f70fe52005-02-22 07:28:26 +000051 HREF="asn1c-usage.html#SECTION02110000000000000000">Quick start with asn1c</A>
Lev Walkindee61782005-02-02 09:53:46 +000052<LI><A NAME="tex2html54"
Lev Walkin7f70fe52005-02-22 07:28:26 +000053 HREF="asn1c-usage.html#SECTION02120000000000000000">Recognizing compiler output</A>
Lev Walkindee61782005-02-02 09:53:46 +000054<LI><A NAME="tex2html55"
Lev Walkin7f70fe52005-02-22 07:28:26 +000055 HREF="asn1c-usage.html#SECTION02130000000000000000">Command line options</A>
Lev Walkineb97a702004-08-07 06:03:15 +000056</UL>
Lev Walkin7f70fe52005-02-22 07:28:26 +000057<LI><A NAME="tex2html56"
58 HREF="asn1c-usage.html#SECTION02200000000000000000">Using the ASN.1 Compiler</A>
Lev Walkineb97a702004-08-07 06:03:15 +000059<UL>
Lev Walkin7f70fe52005-02-22 07:28:26 +000060<LI><A NAME="tex2html57"
61 HREF="asn1c-usage.html#SECTION02210000000000000000">Invoking the ASN.1 helper code</A>
62<UL>
63<LI><A NAME="tex2html58"
64 HREF="asn1c-usage.html#SECTION02211000000000000000">Decoding BER</A>
65<LI><A NAME="tex2html59"
66 HREF="asn1c-usage.html#SECTION02212000000000000000">Encoding DER</A>
67<LI><A NAME="tex2html60"
68 HREF="asn1c-usage.html#SECTION02213000000000000000">Encoding XER</A>
69<LI><A NAME="tex2html61"
70 HREF="asn1c-usage.html#SECTION02214000000000000000">Decoding XER</A>
71<LI><A NAME="tex2html62"
72 HREF="asn1c-usage.html#SECTION02215000000000000000">Validating the target structure</A>
Lev Walkine94e3e52005-01-31 10:18:53 +000073<LI><A NAME="tex2html63"
Lev Walkin7f70fe52005-02-22 07:28:26 +000074 HREF="asn1c-usage.html#SECTION02216000000000000000">Printing the target structure</A>
Lev Walkine94e3e52005-01-31 10:18:53 +000075<LI><A NAME="tex2html64"
Lev Walkin7f70fe52005-02-22 07:28:26 +000076 HREF="asn1c-usage.html#SECTION02217000000000000000">Freeing the target structure</A>
77</UL>
78</UL>
Lev Walkindee61782005-02-02 09:53:46 +000079<LI><A NAME="tex2html65"
Lev Walkin7f70fe52005-02-22 07:28:26 +000080 HREF="asn1c-usage.html#SECTION02300000000000000000">Step by step examples</A>
81<UL>
Lev Walkindee61782005-02-02 09:53:46 +000082<LI><A NAME="tex2html66"
Lev Walkin7f70fe52005-02-22 07:28:26 +000083 HREF="asn1c-usage.html#SECTION02310000000000000000">A ''Rectangle'' Encoder</A>
Lev Walkindee61782005-02-02 09:53:46 +000084<LI><A NAME="tex2html67"
Lev Walkin7f70fe52005-02-22 07:28:26 +000085 HREF="asn1c-usage.html#SECTION02320000000000000000">A ''Rectangle'' Decoder</A>
Lev Walkineb97a702004-08-07 06:03:15 +000086</UL>
Lev Walkindee61782005-02-02 09:53:46 +000087<LI><A NAME="tex2html68"
Lev Walkin7f70fe52005-02-22 07:28:26 +000088 HREF="asn1c-usage.html#SECTION02400000000000000000">Constraint validation examples</A>
Lev Walkineb97a702004-08-07 06:03:15 +000089<UL>
Lev Walkine94e3e52005-01-31 10:18:53 +000090<LI><A NAME="tex2html69"
Lev Walkin7f70fe52005-02-22 07:28:26 +000091 HREF="asn1c-usage.html#SECTION02410000000000000000">Adding constraints into ''Rectangle'' type</A>
92</UL>
93</UL><BR>
Lev Walkine94e3e52005-01-31 10:18:53 +000094<LI><A NAME="tex2html70"
Lev Walkin7f70fe52005-02-22 07:28:26 +000095 HREF="asn1c-usage.html#SECTION03000000000000000000">ASN.1 Basics</A>
96<UL>
Lev Walkindee61782005-02-02 09:53:46 +000097<LI><A NAME="tex2html71"
Lev Walkin7f70fe52005-02-22 07:28:26 +000098 HREF="asn1c-usage.html#SECTION03100000000000000000">Abstract Syntax Notation: ASN.1</A>
99<UL>
Lev Walkindee61782005-02-02 09:53:46 +0000100<LI><A NAME="tex2html72"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000101 HREF="asn1c-usage.html#SECTION03110000000000000000">Some of the ASN.1 Basic Types</A>
102<UL>
Lev Walkindee61782005-02-02 09:53:46 +0000103<LI><A NAME="tex2html73"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000104 HREF="asn1c-usage.html#SECTION03111000000000000000">The BOOLEAN type</A>
Lev Walkindee61782005-02-02 09:53:46 +0000105<LI><A NAME="tex2html74"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000106 HREF="asn1c-usage.html#SECTION03112000000000000000">The INTEGER type</A>
Lev Walkindee61782005-02-02 09:53:46 +0000107<LI><A NAME="tex2html75"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000108 HREF="asn1c-usage.html#SECTION03113000000000000000">The ENUMERATED type</A>
Lev Walkindee61782005-02-02 09:53:46 +0000109<LI><A NAME="tex2html76"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000110 HREF="asn1c-usage.html#SECTION03114000000000000000">The OCTET STRING type</A>
Lev Walkindee61782005-02-02 09:53:46 +0000111<LI><A NAME="tex2html77"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000112 HREF="asn1c-usage.html#SECTION03115000000000000000">The OBJECT IDENTIFIER type</A>
Lev Walkindee61782005-02-02 09:53:46 +0000113<LI><A NAME="tex2html78"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000114 HREF="asn1c-usage.html#SECTION03116000000000000000">The RELATIVE-OID type</A>
115</UL>
Lev Walkindee61782005-02-02 09:53:46 +0000116<LI><A NAME="tex2html79"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000117 HREF="asn1c-usage.html#SECTION03120000000000000000">Some of the ASN.1 String Types</A>
118<UL>
Lev Walkindee61782005-02-02 09:53:46 +0000119<LI><A NAME="tex2html80"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000120 HREF="asn1c-usage.html#SECTION03121000000000000000">The IA5String type</A>
Lev Walkin028a28b2005-01-17 11:09:32 +0000121<LI><A NAME="tex2html81"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000122 HREF="asn1c-usage.html#SECTION03122000000000000000">The UTF8String type</A>
Lev Walkine94e3e52005-01-31 10:18:53 +0000123<LI><A NAME="tex2html82"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000124 HREF="asn1c-usage.html#SECTION03123000000000000000">The NumericString type</A>
Lev Walkine94e3e52005-01-31 10:18:53 +0000125<LI><A NAME="tex2html83"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000126 HREF="asn1c-usage.html#SECTION03124000000000000000">The PrintableString type</A>
Lev Walkindee61782005-02-02 09:53:46 +0000127<LI><A NAME="tex2html84"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000128 HREF="asn1c-usage.html#SECTION03125000000000000000">The VisibleString type</A>
129</UL>
Lev Walkindee61782005-02-02 09:53:46 +0000130<LI><A NAME="tex2html85"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000131 HREF="asn1c-usage.html#SECTION03130000000000000000">ASN.1 Constructed Types</A>
132<UL>
Lev Walkindee61782005-02-02 09:53:46 +0000133<LI><A NAME="tex2html86"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000134 HREF="asn1c-usage.html#SECTION03131000000000000000">The SEQUENCE type</A>
Lev Walkindee61782005-02-02 09:53:46 +0000135<LI><A NAME="tex2html87"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000136 HREF="asn1c-usage.html#SECTION03132000000000000000">The SET type</A>
Lev Walkindee61782005-02-02 09:53:46 +0000137<LI><A NAME="tex2html88"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000138 HREF="asn1c-usage.html#SECTION03133000000000000000">The CHOICE type</A>
Lev Walkindee61782005-02-02 09:53:46 +0000139<LI><A NAME="tex2html89"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000140 HREF="asn1c-usage.html#SECTION03134000000000000000">The SEQUENCE OF type</A>
Lev Walkindee61782005-02-02 09:53:46 +0000141<LI><A NAME="tex2html90"
Lev Walkin7f70fe52005-02-22 07:28:26 +0000142 HREF="asn1c-usage.html#SECTION03135000000000000000">The SET OF type</A>
Lev Walkine94e3e52005-01-31 10:18:53 +0000143</UL>
Lev Walkindee61782005-02-02 09:53:46 +0000144</UL>
Lev Walkine94e3e52005-01-31 10:18:53 +0000145</UL><BR>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000146<LI><A NAME="tex2html91"
147 HREF="asn1c-usage.html#SECTION04000000000000000000">Bibliography</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000148</UL>
149<!--End of Table of Contents-->
150
151<P>
152
Lev Walkin26587ab2004-08-23 15:12:04 +0000153<P>
154
Lev Walkineb97a702004-08-07 06:03:15 +0000155<H1><A NAME="SECTION02000000000000000000">
Lev Walkin7f70fe52005-02-22 07:28:26 +0000156Using the ASN.1 Compiler</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000157</H1>
158
159<P>
160
161<H1><A NAME="SECTION02100000000000000000">
Lev Walkin7f70fe52005-02-22 07:28:26 +0000162Introduction to the ASN.1 Compiler</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000163</H1>
164
165<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000166The purpose of the ASN.1 compiler, of which this document is part,
167is to convert the specifications in ASN.1 notation into some other
168language. At this moment, only C and C++ target languages are supported,
169the latter is in upward compatibility mode.
Lev Walkineb97a702004-08-07 06:03:15 +0000170
171<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000172The compiler reads the specification and emits a series of target
173language structures (C's structs, unions, enums) describing the corresponding
174ASN.1 types. The compiler also creates the code which allows automatic
175serialization and deserialization of these structures using several
176standardized encoding rules (BER, DER, XER).
Lev Walkineb97a702004-08-07 06:03:15 +0000177
178<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000179For example, suppose the following ASN.1 module is given<A NAME="tex2html1"
Lev Walkinf47d0682005-03-05 01:07:04 +0000180 HREF="#foot843"><SUP>1.1</SUP></A>:
Lev Walkineb97a702004-08-07 06:03:15 +0000181
182<P>
183
Lev Walkin26587ab2004-08-23 15:12:04 +0000184<BLOCKQUOTE><PRE>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000185RectangleTest&nbsp;DEFINITIONS&nbsp;::=
Lev Walkineb97a702004-08-07 06:03:15 +0000186BEGIN
187&nbsp;
Lev Walkineb97a702004-08-07 06:03:15 +0000188Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
189&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 +0000190&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 +0000191}
192&nbsp;
193END
Lev Walkineb97a702004-08-07 06:03:15 +0000194</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000195</BLOCKQUOTE>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000196The compiler would read this ASN.1 definition and produce the following
197C type<A NAME="tex2html2"
Lev Walkinf47d0682005-03-05 01:07:04 +0000198 HREF="#foot844"><SUP>1.2</SUP></A>:
Lev Walkineb97a702004-08-07 06:03:15 +0000199
200<P>
201
Lev Walkin26587ab2004-08-23 15:12:04 +0000202<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000203typedef&nbsp;struct&nbsp;Rectangle_s&nbsp;{
204&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;height;
205&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;width;
206}&nbsp;Rectangle_t;
Lev Walkineb97a702004-08-07 06:03:15 +0000207</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000208</BLOCKQUOTE>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000209It would also create the code for converting this structure into platform-independent
210wire representation (a serializer API) and the decoder of such wire
211representation back into local, machine-specific type (a deserializer
212API).
Lev Walkineb97a702004-08-07 06:03:15 +0000213
214<P>
215
Lev Walkin7f70fe52005-02-22 07:28:26 +0000216<H1><A NAME="SECTION02110000000000000000">
217Quick start with asn1c</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000218</H1>
219
220<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000221After building and installing the compiler, the <I>asn1c</I><A NAME="tex2html3"
Lev Walkinf47d0682005-03-05 01:07:04 +0000222 HREF="#foot845"><SUP>1.3</SUP></A> command may be used to compile the ASN.1 module<A NAME="tex2html4"
223 HREF="#foot846"><SUP>1.4</SUP></A>:
Lev Walkineb97a702004-08-07 06:03:15 +0000224
225<P>
226
Lev Walkin26587ab2004-08-23 15:12:04 +0000227<BLOCKQUOTE><PRE>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000228asn1c&nbsp;<I>&lt;module.asn1&gt;</I>
Lev Walkineb97a702004-08-07 06:03:15 +0000229</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000230</BLOCKQUOTE>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000231If several ASN.1 modules contain interdependencies, all of the files
Lev Walkin26587ab2004-08-23 15:12:04 +0000232must be specified altogether:
Lev Walkineb97a702004-08-07 06:03:15 +0000233
234<P>
235
Lev Walkin26587ab2004-08-23 15:12:04 +0000236<BLOCKQUOTE><PRE>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000237asn1c&nbsp;<I>&lt;module1.asn1&gt;&nbsp;&lt;module2.asn1&gt;&nbsp;...</I>
Lev Walkineb97a702004-08-07 06:03:15 +0000238</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000239</BLOCKQUOTE>
240The compiler <B>-E</B> and <B>-EF</B> options are used for testing
241the parser and the semantic fixer, respectively. These options will
242instruct the compiler to dump out the parsed (and fixed, if <B>-F</B>
243is involved) ASN.1 specification as it was &#34;understood&#34;
244by the compiler. It might be useful to check whether a particular
245syntactic construction is properly supported by the compiler.
Lev Walkineb97a702004-08-07 06:03:15 +0000246
247<P>
248
Lev Walkin26587ab2004-08-23 15:12:04 +0000249<BLOCKQUOTE><PRE>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000250asn1c&nbsp;<B>-EF</B>&nbsp;<I>&lt;module-to-test.asn1&gt;</I>
Lev Walkineb97a702004-08-07 06:03:15 +0000251</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000252</BLOCKQUOTE>
253The <B>-P</B> option is used to dump the compiled output on the
254screen instead of creating a bunch of .c and .h files on disk in the
255current directory. You would probably want to start with <B>-P</B>
256option instead of creating a mess in your current directory. Another
257option, <B>-R</B>, asks compiler to only generate the files which
258need to be generated, and supress linking in the numerous support
259files.
260
261<P>
262Print the compiled output instead of creating multiple source files:
Lev Walkineb97a702004-08-07 06:03:15 +0000263
264<P>
265
Lev Walkin26587ab2004-08-23 15:12:04 +0000266<BLOCKQUOTE><PRE>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000267asn1c&nbsp;<B>-P</B>&nbsp;<I>&lt;module-to-compile-and-print.asn1&gt;</I>
Lev Walkin26587ab2004-08-23 15:12:04 +0000268</PRE>
269</BLOCKQUOTE>
270
271<P>
272
Lev Walkin7f70fe52005-02-22 07:28:26 +0000273<H1><A NAME="SECTION02120000000000000000">
274Recognizing compiler output</A>
Lev Walkineb97a702004-08-07 06:03:15 +0000275</H1>
276
277<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000278After compiling, the following entities will be created in your current
279directory:
Lev Walkineb97a702004-08-07 06:03:15 +0000280
Lev Walkin7f70fe52005-02-22 07:28:26 +0000281<P>
282
283<UL>
284<LI>A set of .c and .h files, generally a single pair for each type defined
285in the ASN.1 specifications. These files will be named similarly to
286the ASN.1 types (<I>Rectangle.c</I> and <I>Rectangle.h</I> for the
287RectangleTest ASN.1 module defined in the beginning of this document).
288</LI>
289<LI>A set of helper .c and .h files which contain generic encoders, decoders
290and other useful routines. There will be quite a few of them, some
291of them even are not always necessary, but the overall amount of code
292after compilation will be rather small anyway.
293</LI>
294<LI>A <I>Makefile.am.sample</I> file mentioning all the files created
295at the earlier steps. This file is suitable for either automake suite
296or the plain `make` utility.
297</LI>
298</UL>
299It is your responsibility to create .c file with the <I>int main()</I>
300routine.
301
302<P>
303In other words, after compiling the Rectangle module, you have the
304following set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h,
305<B>...</B> }, where <B>''...''</B> stands for the
306set of additional ''helper'' files created by the compiler. If you
307add a simple file with the <I>int main()</I> routine, it would even
308be possible to compile everything with the single instruction:
309
310<P>
311
312<BLOCKQUOTE><PRE>
313cc&nbsp;-I.&nbsp;-o&nbsp;rectangle.exe&nbsp;*.c&nbsp;&nbsp;&nbsp;#&nbsp;It&nbsp;could&nbsp;be&nbsp;<I>that</I>&nbsp;simple
314</PRE>
315</BLOCKQUOTE>
316Refer to the Chapter cha:Step-by-step-examples for a sample
317<I>int main()</I> routine.
318
319<P>
320
321<H1><A NAME="SECTION02130000000000000000">
322Command line options</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000323</H1>
324
325<P>
326The <A HREF=#Table1>Table 1</A> summarizes various options affecting
327the compiler's behavior.
328
329<P>
330<BR><P></P>
Lev Walkinf47d0682005-03-05 01:07:04 +0000331<DIV ALIGN="CENTER"><A NAME="851"></A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000332<TABLE>
333<CAPTION><STRONG><A NAME=Table1>Table 1:</A></STRONG>
334The list of asn1c command line options</CAPTION>
335<TR><TD><TABLE COLS=2 BORDER FRAME=BOX RULES=GROUPS>
336<COLGROUP><COL ALIGN=LEFT><COLGROUP><COL ALIGN=JUSTIFY WIDTH="3in">
337<TBODY>
338<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>
Lev Walkinf47d0682005-03-05 01:07:04 +0000339<B><FONT SIZE="-1">Overall Options</FONT></B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
340<B><FONT SIZE="-1">Description</FONT></B></TD></TR>
Lev Walkin26587ab2004-08-23 15:12:04 +0000341</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000342<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-E</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000343<FONT SIZE="-1">Stop after the parsing stage and print the reconstructed ASN.1
344specification code to the standard output.</FONT></TD></TR>
345</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000346<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-F</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000347<FONT SIZE="-1">Used together with -E, instructs the compiler to stop after
348the ASN.1 syntax tree fixing stage and dump the reconstructed ASN.1
349specification to the standard output.</FONT></TD></TR>
350</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000351<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-P</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000352<FONT SIZE="-1">Dump the compiled output to the standard output instead of
353cre- ating the target language files on disk.</FONT></TD></TR>
354</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000355<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-R</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000356<FONT SIZE="-1">Restrict the compiler to generate only the ASN.1 tables, omit-
357ting the usual support code.</FONT></TD></TR>
358</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000359<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-S</FONT> <I><FONT SIZE="-1">&lt;directory&gt;</FONT></I></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000360<FONT SIZE="-1">Use the specified directory with ASN.1 skeleton files.</FONT></TD></TR>
361</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000362<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-X</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
363<FONT SIZE="-1">Generate the XML DTD for the specified ASN.1 modules.</FONT></TD></TR>
Lev Walkinf7484512004-10-13 09:13:56 +0000364</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000365<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B><FONT SIZE="-1">Warning Options</FONT></B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
366<B><FONT SIZE="-1">Description</FONT></B></TD></TR>
Lev Walkin26587ab2004-08-23 15:12:04 +0000367</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000368<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-Werror</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000369<FONT SIZE="-1">Treat warnings as errors; abort if any warning is produced.</FONT></TD></TR>
370</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000371<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-Wdebug-lexer</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000372<FONT SIZE="-1">Enable lexer debugging during the ASN.1 parsing stage.</FONT></TD></TR>
373</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000374<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-Wdebug-fixer</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000375 <FONT SIZE="-1">Enable ASN.1 syntax tree fixer debugging during the
376 fixing stage.</FONT></TD></TR>
377</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000378<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-Wdebug-compiler</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000379<FONT SIZE="-1">Enable debugging during the actual compile time.</FONT></TD></TR>
380</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000381<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B><FONT SIZE="-1">Language Options</FONT></B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
382<B><FONT SIZE="-1">Description</FONT></B></TD></TR>
Lev Walkin26587ab2004-08-23 15:12:04 +0000383</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000384<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-fall-defs-global</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
385<FONT SIZE="-1">Normally the compiler hides the definitions (asn_DEF_xxx)
386of the inner structure elements (members of SEQUENCE, SET and other
387types). This option makes all such definitions global. Enabling this
388option may pollute the namespace by making lots of asn_DEF_xxx structures
Lev Walkin71a8aaf2004-09-08 03:10:54 +0000389globally visible, but will allow you to manipulate (encode and decode)
Lev Walkinf47d0682005-03-05 01:07:04 +0000390the individual members of any complex ASN.1 structure.</FONT></TD></TR>
Lev Walkin71a8aaf2004-09-08 03:10:54 +0000391</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000392<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-fbless-SIZE</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000393<FONT SIZE="-1">Allow SIZE() constraint for INTEGER, ENUMERATED, and other
394types for which this constraint is normally prohibited by the standard.
395This is a violation of an ASN.1 standard and compiler may fail to
396produce the meaningful code.</FONT></TD></TR>
397</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000398<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-fcompound-names</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
399<FONT SIZE="-1">Use complex names for C structures. Using complex names prevents
400name clashes in case the module reuses the same identifiers in multiple
401contexts.</FONT></TD></TR>
402</TBODY><TBODY>
Lev Walkin72a0f5a2005-07-24 08:28:39 +0000403<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-findirect-choice</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
404<FONT SIZE="-1">When generating code for a CHOICE type, compile
405the CHOICE members as indirect pointers instead of
406declaring them inline. Consider using this option
407together with <B>-fno-include-deps</B> to prevent circular references.
408</FONT></TD></TR>
409</TBODY><TBODY>
410<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-fknown-extern-type=<I>&lt;name&gt;</I></FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
411<FONT SIZE="-1">Pretend the specified type is known. The compiler will
412assume the target language source files for the given
413type have been provided manually.
414</FONT></TD></TR>
415</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000416<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-fnative-types</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin6faa68e2004-09-17 08:35:02 +0000417<FONT SIZE="-1">Use the native machine's data types (int, double) whenever
Lev Walkin28c89eb2004-09-30 22:45:58 +0000418possible, instead of the compound INTEGER_t, ENUMERATED_t and REAL_t
419types. </FONT></TD></TR>
Lev Walkin26587ab2004-08-23 15:12:04 +0000420</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000421<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-fno-constraints</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
422<FONT SIZE="-1">Do not generate ASN.1 subtype constraint checking code. This
423may produce a shorter executable.</FONT></TD></TR>
Lev Walkin683f9b72004-09-26 13:41:45 +0000424</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000425<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-fno-include-deps</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
426<FONT SIZE="-1">Do not generate courtesy #include lines for non-critical
427dependencies.</FONT></TD></TR>
428</TBODY><TBODY>
429<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-funnamed-unions</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000430<FONT SIZE="-1">Enable unnamed unions in the definitions of target language's
431structures.</FONT></TD></TR>
432</TBODY><TBODY>
Lev Walkina895afb2005-10-06 10:09:34 +0000433<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-fskeletons-copy</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
434<FONT SIZE="-1">Copy support files rather than symlink them.</FONT></TD></TR>
435</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000436<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-ftypes88</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin28c89eb2004-09-30 22:45:58 +0000437<FONT SIZE="-1">Pretend to support only ASN.1:1988 embedded types. Certain
438reserved words, such as UniversalString and BMPString, become ordinary
439type references and may be redefined by the specification.</FONT></TD></TR>
Lev Walkin26587ab2004-08-23 15:12:04 +0000440</TBODY><TBODY>
Lev Walkina895afb2005-10-06 10:09:34 +0000441
Lev Walkinf47d0682005-03-05 01:07:04 +0000442<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B><FONT SIZE="-1">Output Options</FONT></B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
443<B><FONT SIZE="-1">Description</FONT></B></TD></TR>
Lev Walkin28c89eb2004-09-30 22:45:58 +0000444<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>
445
Lev Walkinf47d0682005-03-05 01:07:04 +0000446<FONT SIZE="-1">-print-constraints</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000447<FONT SIZE="-1">When -EF are also specified, this option forces the compiler
448to explain its internal understanding of subtype constraints.</FONT></TD></TR>
449</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000450<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><FONT SIZE="-1">-print-lines</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000451<FONT SIZE="-1">Generate &#34;- #line&#34; comments in -E output.</FONT></TD></TR>
452</TBODY>
453</TABLE>
454
455<P>
456</TD></TR>
457</TABLE>
458</DIV><P></P><BR>
459
460<P>
461
Lev Walkin7f70fe52005-02-22 07:28:26 +0000462<H1><A NAME="SECTION02200000000000000000">
463Using the ASN.1 Compiler</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000464</H1>
Lev Walkineb97a702004-08-07 06:03:15 +0000465
466<P>
Lev Walkineb97a702004-08-07 06:03:15 +0000467
Lev Walkin7f70fe52005-02-22 07:28:26 +0000468<H1><A NAME="SECTION02210000000000000000">
469Invoking the ASN.1 helper code</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000470</H1>
Lev Walkineb97a702004-08-07 06:03:15 +0000471
472<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000473First of all, you should include one or more header files into your
474application. Typically, it is enough to include the header file of
475the main PDU type. For our Rectangle module, including the Rectangle.h
476file is sufficient:
Lev Walkineb97a702004-08-07 06:03:15 +0000477
478<P>
479
Lev Walkin26587ab2004-08-23 15:12:04 +0000480<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000481#include&nbsp;&lt;Rectangle.h&gt;
Lev Walkineb97a702004-08-07 06:03:15 +0000482</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000483</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000484The header files defines the C structure corresponding to the ASN.1
485definition of a rectangle and the declaration of the ASN.1 type descriptor,
486which is used as an argument to most of the functions provided by
487the ASN.1 module. For example, here is the code which frees the Rectangle_t
488structure:
489
490<P>
491
Lev Walkin26587ab2004-08-23 15:12:04 +0000492<BLOCKQUOTE><PRE>
493Rectangle_t&nbsp;*rect&nbsp;=&nbsp;...;
494&nbsp;
Lev Walkin0f5c5cf2005-08-29 10:23:20 +0000495asn_DEF_Rectangle.free_struct(&amp;asn_DEF_Rectangle,
Lev Walkineb97a702004-08-07 06:03:15 +0000496&nbsp;&nbsp;&nbsp;&nbsp;rect,&nbsp;0);
Lev Walkineb97a702004-08-07 06:03:15 +0000497</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000498</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000499This code defines a <I>rect</I> pointer which points to the Rectangle_t
500structure which needs to be freed. The second line invokes the generic
Lev Walkin7f70fe52005-02-22 07:28:26 +0000501<I>free_struct()</I> routine created specifically for this Rectangle_t
502structure. The <I>asn_DEF_Rectangle</I> is the type descriptor,
503which holds a collection of routines to deal with the Rectangle_t
504structure.
Lev Walkineb97a702004-08-07 06:03:15 +0000505
506<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000507The following member functions of the asn_DEF_Rectangle type descriptor
508are of interest:
Lev Walkineb97a702004-08-07 06:03:15 +0000509
510<P>
511<DL>
Lev Walkineb97a702004-08-07 06:03:15 +0000512<DT><STRONG>ber_decoder</STRONG></DT>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000513<DD>This is the generic <I>restartable</I><A NAME="tex2html6"
Lev Walkinf47d0682005-03-05 01:07:04 +0000514 HREF="#foot181"><SUP>2.1</SUP></A> BER decoder (Basic Encoding Rules). This decoder would create
Lev Walkineb97a702004-08-07 06:03:15 +0000515and/or fill the target structure for you. Please refer to Section
Lev Walkin7f70fe52005-02-22 07:28:26 +0000516sub:Decoding-BER.
Lev Walkineb97a702004-08-07 06:03:15 +0000517</DD>
518<DT><STRONG>der_encoder</STRONG></DT>
519<DD>This is the generic DER encoder (Distinguished Encoding
Lev Walkin683f9b72004-09-26 13:41:45 +0000520Rules). This encoder will take the target structure and encode it
Lev Walkin7f70fe52005-02-22 07:28:26 +0000521into a series of bytes. Please refer to Section <A HREF="#sub:Encoding-DER">Encoding DER</A>.
Lev Walkineb97a702004-08-07 06:03:15 +0000522</DD>
Lev Walkin683f9b72004-09-26 13:41:45 +0000523<DT><STRONG>xer_encoder</STRONG></DT>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000524<DD>This is the XER encoder (XML Encoding Rules). This
525encoder will take the target structure and represent it as an XML
526(text) document using either BASIC-XER or CANONICAL-XER encoding rules.
527Please refer to Section <A HREF="#sub:Encoding-XER">Encoding XER</A>.
528</DD>
529<DT><STRONG>xer_decoder</STRONG></DT>
530<DD>This is the generic XER decoder. It takes both BASIC-XER
531or CANONICAL-XER encodings and deserializes the data into a local,
532machine-dependent representation. Please refer to Section <A HREF="#sub:Decoding-XER">Decoding XER</A>.
Lev Walkin683f9b72004-09-26 13:41:45 +0000533</DD>
534<DT><STRONG>check_constraints</STRONG></DT>
535<DD>Check that the contents of the target structure
536are semantically valid and constrained to appropriate implicit or
Lev Walkin1411d052005-02-02 11:06:38 +0000537explicit subtype constraints. Please refer to Section <A HREF="#sub:Validating-the-target">Validating the target</A>.
Lev Walkin683f9b72004-09-26 13:41:45 +0000538</DD>
Lev Walkineb97a702004-08-07 06:03:15 +0000539<DT><STRONG>print_struct</STRONG></DT>
540<DD>This function convert the contents of the passed target
541structure into human readable form. This form is not formal and cannot
542be converted back into the structure, but it may turn out to be useful
Lev Walkin7f70fe52005-02-22 07:28:26 +0000543for debugging or quick-n-dirty printing. Please refer to Section <A HREF="#sub:Printing-the-target">Printing the target</A>.
Lev Walkineb97a702004-08-07 06:03:15 +0000544</DD>
545<DT><STRONG>free_struct</STRONG></DT>
546<DD>This is a generic disposal which frees the target structure.
Lev Walkin7f70fe52005-02-22 07:28:26 +0000547Please refer to Section <A HREF="#sub:Freeing-the-target">Freeing the target</A>.
Lev Walkineb97a702004-08-07 06:03:15 +0000548</DD>
549</DL>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000550Each of the above function takes the type descriptor (<I>asn_DEF_...</I>)
Lev Walkin7f70fe52005-02-22 07:28:26 +0000551and the target structure (<I>rect</I>, in the above example).
Lev Walkineb97a702004-08-07 06:03:15 +0000552
553<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000554
555<H2><A NAME="SECTION02211000000000000000"></A><A NAME="sub:Decoding-BER"></A><BR>
556Decoding BER
557</H2>
558
559<P>
560The Basic Encoding Rules describe the most widely used (by the ASN.1
561community) way to encode and decode a given structure in a machine-independent
562way. Several other encoding rules (CER, DER) define a more restrictive
563versions of BER, so the generic BER parser is also capable of decoding
564the data encoded by CER and DER encoders. The opposite is not true.
565
566<P>
567<I>The ASN.1 compiler provides the generic BER decoder which is
568implicitly capable of decoding BER, CER and DER encoded data.</I>
569
570<P>
571The decoder is restartable (stream-oriented), which means that in
572case the buffer has less data than it is expected, the decoder will
573process whatever there is available and ask for more data to be provided.
574Please note that the decoder may actually process less data than it
575was given in the buffer, which means that you must be able to make
576the next buffer contain the unprocessed part of the previous buffer.
577
578<P>
579Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
580
581<P>
582
583<UL>
584<LI>You may concatenate these buffers and feed the BER decoder with 300
585bytes of data, or
586</LI>
587<LI>You may feed it the first buffer of 100 bytes of data, realize that
588the ber_decoder consumed only 95 bytes from it and later feed the
589decoder with 205 bytes buffer which consists of 5 unprocessed bytes
590from the first buffer and the additional 200 bytes from the second
591buffer.
592</LI>
593</UL>
594This is not as convenient as it could be (like, the BER encoder could
595consume the whole 100 bytes and keep these 5 bytes in some temporary
596storage), but in case of existing stream based processing it might
597actually fit well into existing algorithm. Suggestions are welcome.
598
599<P>
600Here is the simplest example of BER decoding.
Lev Walkineb97a702004-08-07 06:03:15 +0000601
602<P>
603
Lev Walkin26587ab2004-08-23 15:12:04 +0000604<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000605Rectangle_t&nbsp;*
Lev Walkin26587ab2004-08-23 15:12:04 +0000606simple_deserializer(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;buf_size)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +0000607&nbsp;&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;*rect&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Note&nbsp;this&nbsp;0!&nbsp;*/
Lev Walkina646ccb2004-10-20 15:48:55 +0000608&nbsp;&nbsp;&nbsp;&nbsp;asn_dec_rval_t&nbsp;rval;
Lev Walkineb97a702004-08-07 06:03:15 +0000609&nbsp;
Lev Walkin0f5c5cf2005-08-29 10:23:20 +0000610&nbsp;&nbsp;&nbsp;&nbsp;rval&nbsp;=&nbsp;<B>asn_DEF_Rectangle.ber_decoder</B>(0,
Lev Walkinc500b3e2004-09-29 13:37:15 +0000611&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,
Lev Walkineb97a702004-08-07 06:03:15 +0000612&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void&nbsp;**)&amp;rect,
613&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size,
614&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0);
615&nbsp;
616&nbsp;&nbsp;&nbsp;&nbsp;if(rval<B>.code</B>&nbsp;==&nbsp;RC_OK)&nbsp;{
617&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;*/
618&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
Lev Walkin26587ab2004-08-23 15:12:04 +0000619&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Free&nbsp;partially&nbsp;decoded&nbsp;rect&nbsp;*/
Lev Walkin0f5c5cf2005-08-29 10:23:20 +0000620&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn_DEF_Rectangle.free_struct(
Lev Walkinc500b3e2004-09-29 13:37:15 +0000621&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect,&nbsp;0);
Lev Walkineb97a702004-08-07 06:03:15 +0000622&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;
623&nbsp;&nbsp;&nbsp;&nbsp;}
624}
Lev Walkineb97a702004-08-07 06:03:15 +0000625</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000626</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000627The code above defines a function, <I>simple_deserializer</I>, which
Lev Walkin7f70fe52005-02-22 07:28:26 +0000628takes a buffer and its length and is expected to return a pointer
629to the Rectangle_t structure. Inside, it tries to convert the bytes
630passed into the target structure (rect) using the BER decoder and
631returns the rect pointer afterwards. If the structure cannot be deserialized,
632it frees the memory which might be left allocated by the unfinished
633<I>ber_decoder</I> routine and returns 0 (no data). (This <B>freeing
634is necessary</B> because the ber_decoder is a restartable procedure,
Lev Walkineb97a702004-08-07 06:03:15 +0000635and may fail just because there is more data needs to be provided
Lev Walkin7f70fe52005-02-22 07:28:26 +0000636before decoding could be finalized). The code above obviously does
637not take into account the way the <I>ber_decoder()</I> failed, so
638the freeing is necessary because the part of the buffer may already
639be decoded into the structure by the time something goes wrong.
Lev Walkineb97a702004-08-07 06:03:15 +0000640
641<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000642A little less wordy would be to invoke a globally available <I>ber_decode()</I>
643function instead of dereferencing the asn_DEF_Rectangle type descriptor:
Lev Walkineb97a702004-08-07 06:03:15 +0000644
645<P>
646
Lev Walkin26587ab2004-08-23 15:12:04 +0000647<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000648rval&nbsp;=&nbsp;ber_decode(0,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;(void&nbsp;**)&amp;rect,
Lev Walkineb97a702004-08-07 06:03:15 +0000649&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size);
Lev Walkineb97a702004-08-07 06:03:15 +0000650</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000651</BLOCKQUOTE>
Lev Walkin0f5c5cf2005-08-29 10:23:20 +0000652Note that the initial (asn_DEF_Rectangle.ber_decoder) reference
Lev Walkineb97a702004-08-07 06:03:15 +0000653is gone, and also the last argument (0) is no longer necessary.
654
655<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000656These two ways of BER decoder invocations are fully equivalent.
Lev Walkineb97a702004-08-07 06:03:15 +0000657
658<P>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000659The BER de<I>coder</I> may fail because of (<I>the following RC_...
Lev Walkineb97a702004-08-07 06:03:15 +0000660codes are defined in ber_decoder.h</I>):
661
662<P>
663
664<UL>
665<LI>RC_WMORE: There is more data expected than it is provided (stream
666mode continuation feature);
667</LI>
668<LI>RC_FAIL: General failure to decode the buffer;
669</LI>
670<LI>... other codes may be defined as well.
671</LI>
672</UL>
Lev Walkina646ccb2004-10-20 15:48:55 +0000673Together with the return code (.code) the asn_dec_rval_t type contains
Lev Walkineb97a702004-08-07 06:03:15 +0000674the number of bytes which is consumed from the buffer. In the previous
675hypothetical example of two buffers (of 100 and 200 bytes), the first
676call to ber_decode() would return with .code = RC_WMORE and .consumed
677= 95. The .consumed field of the BER decoder return value is <B>always</B>
678valid, even if the decoder succeeds or fails with any other return
679code.
680
681<P>
682Please look into ber_decoder.h for the precise definition of ber_decode()
683and related types.
684
685<P>
686
Lev Walkin7f70fe52005-02-22 07:28:26 +0000687<H2><A NAME="SECTION02212000000000000000"></A><A NAME="sub:Encoding-DER"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +0000688Encoding DER
Lev Walkin26587ab2004-08-23 15:12:04 +0000689</H2>
Lev Walkineb97a702004-08-07 06:03:15 +0000690
691<P>
Lev Walkin683f9b72004-09-26 13:41:45 +0000692The Distinguished Encoding Rules is the <I>canonical</I> variant of
693BER encoding rules. The DER is best suited to encode the structures
694where all the lengths are known beforehand. This is probably exactly
695how you want to encode: either after a BER decoding or after a manual
696fill-up, the target structure contains the data which size is implicitly
Lev Walkin7f70fe52005-02-22 07:28:26 +0000697known before encoding. Among other uses, the DER encoding is used
698to encode X.509 certificates.
Lev Walkineb97a702004-08-07 06:03:15 +0000699
700<P>
701As with BER decoder, the DER encoder may be invoked either directly
Lev Walkinc500b3e2004-09-29 13:37:15 +0000702from the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone
703function, which is somewhat simpler:
Lev Walkineb97a702004-08-07 06:03:15 +0000704
705<P>
706
Lev Walkin26587ab2004-08-23 15:12:04 +0000707<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000708&nbsp;
709/*
710&nbsp;*&nbsp;This&nbsp;is&nbsp;the&nbsp;serializer&nbsp;itself,
711&nbsp;*&nbsp;it&nbsp;supplies&nbsp;the&nbsp;DER&nbsp;encoder&nbsp;with&nbsp;the
712&nbsp;*&nbsp;pointer&nbsp;to&nbsp;the&nbsp;custom&nbsp;output&nbsp;function.
713&nbsp;*/
714ssize_t
715simple_serializer(FILE&nbsp;*ostream,&nbsp;Rectangle_t&nbsp;*rect)&nbsp;{
Lev Walkin683f9b72004-09-26 13:41:45 +0000716&nbsp;&nbsp;&nbsp;&nbsp;asn_enc_rval_t&nbsp;er;&nbsp;&nbsp;/*&nbsp;Encoder&nbsp;return&nbsp;value&nbsp;*/
Lev Walkineb97a702004-08-07 06:03:15 +0000717&nbsp;
Lev Walkinc500b3e2004-09-29 13:37:15 +0000718&nbsp;&nbsp;&nbsp;&nbsp;er&nbsp;=&nbsp;der_encode(&amp;asn_DEF_Rect,&nbsp;rect,
Lev Walkin26587ab2004-08-23 15:12:04 +0000719&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_stream,&nbsp;ostream);
Lev Walkin683f9b72004-09-26 13:41:45 +0000720&nbsp;&nbsp;&nbsp;&nbsp;if(er.<B>encoded</B>&nbsp;==&nbsp;-1)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +0000721&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*
Lev Walkin683f9b72004-09-26 13:41:45 +0000722&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Failed&nbsp;to&nbsp;encode&nbsp;the&nbsp;rectangle&nbsp;data.
Lev Walkineb97a702004-08-07 06:03:15 +0000723&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
724&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''Cannot&nbsp;encode&nbsp;%s:&nbsp;%s&#92;n'',
Lev Walkin683f9b72004-09-26 13:41:45 +0000725&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;er.<B>failed_type</B>-&gt;name,
Lev Walkineb97a702004-08-07 06:03:15 +0000726&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strerror(errno));
727&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-1;
728&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
729&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Return&nbsp;the&nbsp;number&nbsp;of&nbsp;bytes&nbsp;*/
Lev Walkin683f9b72004-09-26 13:41:45 +0000730&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;er.encoded;
Lev Walkineb97a702004-08-07 06:03:15 +0000731&nbsp;&nbsp;&nbsp;&nbsp;}
732}
Lev Walkineb97a702004-08-07 06:03:15 +0000733</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000734</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000735As you see, the DER encoder does not write into some sort of buffer
736or something. It just invokes the custom function (possible, multiple
737times) which would save the data into appropriate storage. The optional
738argument <I>app_key</I> is opaque for the DER encoder code and just
739used by <I>_write_stream()</I> as the pointer to the appropriate
740output stream to be used.
741
742<P>
743If the custom write function is not given (passed as 0), then the
744DER encoder will essentially do the same thing (i.e., encode the data)
745but no callbacks will be invoked (so the data goes nowhere). It may
746prove useful to determine the size of the structure's encoding before
Lev Walkin7f70fe52005-02-22 07:28:26 +0000747actually doing the encoding<A NAME="tex2html7"
Lev Walkinf47d0682005-03-05 01:07:04 +0000748 HREF="#foot250"><SUP>2.2</SUP></A>.
Lev Walkineb97a702004-08-07 06:03:15 +0000749
750<P>
751Please look into der_encoder.h for the precise definition of der_encode()
752and related types.
753
754<P>
755
Lev Walkin7f70fe52005-02-22 07:28:26 +0000756<H2><A NAME="SECTION02213000000000000000"></A><A NAME="sub:Encoding-XER"></A><BR>
Lev Walkin683f9b72004-09-26 13:41:45 +0000757Encoding XER
758</H2>
759
760<P>
761The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
762Markup Language, a text-based format for information exchange. The
763encoder routine API comes in two flavors: stdio-based and callback-based.
764With the callback-based encoder, the encoding process is very similar
Lev Walkin1411d052005-02-02 11:06:38 +0000765to the DER one, described in Section <A HREF="#sub:Encoding-DER">Encoding DER</A>. The
Lev Walkin683f9b72004-09-26 13:41:45 +0000766following example uses the definition of write_stream() from up there.
767
768<P>
769
770<BLOCKQUOTE><PRE>
771/*
772&nbsp;*&nbsp;This&nbsp;procedure&nbsp;generates&nbsp;the&nbsp;XML&nbsp;document
773&nbsp;*&nbsp;by&nbsp;invoking&nbsp;the&nbsp;XER&nbsp;encoder.
774&nbsp;*&nbsp;NOTE:&nbsp;Do&nbsp;not&nbsp;copy&nbsp;this&nbsp;code&nbsp;verbatim!
775&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;the&nbsp;stdio&nbsp;output&nbsp;is&nbsp;necessary,
776&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;the&nbsp;xer_fprint()&nbsp;procedure&nbsp;instead.
Lev Walkin1411d052005-02-02 11:06:38 +0000777&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;See&nbsp;Section&nbsp;<A HREF="#sub:Printing-the-target">Printing the target</A>.
Lev Walkin683f9b72004-09-26 13:41:45 +0000778&nbsp;*/
779int
780print_as_XML(FILE&nbsp;*ostream,&nbsp;Rectangle_t&nbsp;*rect)&nbsp;{
781&nbsp;&nbsp;&nbsp;&nbsp;asn_enc_rval_t&nbsp;er;&nbsp;&nbsp;/*&nbsp;Encoder&nbsp;return&nbsp;value&nbsp;*/
782&nbsp;
Lev Walkin7f70fe52005-02-22 07:28:26 +0000783&nbsp;&nbsp;&nbsp;&nbsp;er&nbsp;=&nbsp;xer_encode(&amp;asn_DEF_Rectangle,&nbsp;rect,
Lev Walkin683f9b72004-09-26 13:41:45 +0000784&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XER_F_BASIC,&nbsp;/*&nbsp;BASIC-XER&nbsp;or&nbsp;CANONICAL-XER&nbsp;*/
785&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_stream,&nbsp;ostream);
786&nbsp;
787&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(er.encoded&nbsp;==&nbsp;-1)&nbsp;?&nbsp;-1&nbsp;:&nbsp;0;
788}
789</PRE>
790</BLOCKQUOTE>
791Please look into xer_encoder.h for the precise definition of xer_encode()
792and related types.
793
794<P>
Lev Walkin1411d052005-02-02 11:06:38 +0000795See Section [<A HREF="#sub:Printing-the-target">Printing the target</A>] for the example of stdio-based
Lev Walkin683f9b72004-09-26 13:41:45 +0000796XML encoder and other pretty-printing suggestions.
797
798<P>
799
Lev Walkin7f70fe52005-02-22 07:28:26 +0000800<H2><A NAME="SECTION02214000000000000000"></A><A NAME="sub:Decoding-XER"></A><BR>
801Decoding XER
802</H2>
803
804<P>
805The data encoded using the XER rules can be subsequently decoded using
806the xer_decode() API call:
807
808<P>
809
810<BLOCKQUOTE><PRE>
811Rectangle_t&nbsp;*
812XML_to_Rectangle(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;buf_size)&nbsp;{
813&nbsp;&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;*rect&nbsp;=&nbsp;0;&nbsp;/*&nbsp;Note&nbsp;this&nbsp;0!&nbsp;*/
814&nbsp;&nbsp;&nbsp;&nbsp;asn_dec_rval_t&nbsp;rval;
815&nbsp;&nbsp;
816&nbsp;&nbsp;&nbsp;&nbsp;rval&nbsp;=&nbsp;xer_decode(0,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;(void&nbsp;**)&amp;rect,
817&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size);
818&nbsp;&nbsp;&nbsp;&nbsp;if(rval<B>.code</B>&nbsp;==&nbsp;RC_OK)&nbsp;{
819&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;*/
820&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
821&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Free&nbsp;partially&nbsp;decoded&nbsp;rect&nbsp;*/
Lev Walkin0f5c5cf2005-08-29 10:23:20 +0000822&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn_DEF_Rectangle.free_struct(
Lev Walkin7f70fe52005-02-22 07:28:26 +0000823&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect,&nbsp;0);
824&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;
825&nbsp;&nbsp;&nbsp;&nbsp;}
826}
827</PRE>
828</BLOCKQUOTE>
829The decoder takes both BASIC-XER and CANONICAL-XER encodings.
830
831<P>
832The decoder shares its data consumption properties with BER decoder;
833please read the Section <A HREF="#sub:Decoding-BER">Decoding BER</A> to know more.
834
835<P>
836Please look into xer_decoder.h for the precise definition of xer_decode()
837and related types.
838
839<P>
840
841<H2><A NAME="SECTION02215000000000000000"></A><A NAME="sub:Validating-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +0000842Validating the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +0000843</H2>
Lev Walkineb97a702004-08-07 06:03:15 +0000844
845<P>
846Sometimes the target structure needs to be validated. For example,
847if the structure was created by the application (as opposed to being
848decoded from some external source), some important information required
849by the ASN.1 specification might be missing. On the other hand, the
850successful decoding of the data from some external source does not
851necessarily mean that the data is fully valid either. It might well
852be the case that the specification describes some subtype constraints
853that were not taken into account during decoding, and it would actually
854be useful to perform the last check when the data is ready to be encoded
855or when the data has just been decoded to ensure its validity according
856to some stricter rules.
857
858<P>
859The asn_check_constraints() function checks the type for various
860implicit and explicit constraints. It is recommended to use asn_check_constraints()
861function after each decoding and before each encoding.
862
863<P>
864Please look into constraints.h for the precise definition of asn_check_constraints()
865and related types.
866
867<P>
868
Lev Walkin7f70fe52005-02-22 07:28:26 +0000869<H2><A NAME="SECTION02216000000000000000"></A><A NAME="sub:Printing-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +0000870Printing the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +0000871</H2>
Lev Walkineb97a702004-08-07 06:03:15 +0000872
873<P>
874There are two ways to print the target structure: either invoke the
875print_struct member of the ASN.1 type descriptor, or using the asn_fprint()
876function, which is a simpler wrapper of the former:
877
878<P>
879
Lev Walkin26587ab2004-08-23 15:12:04 +0000880<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000881asn_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect);
Lev Walkineb97a702004-08-07 06:03:15 +0000882</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000883</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000884Please look into constr_TYPE.h for the precise definition of asn_fprint()
885and related types.
886
887<P>
Lev Walkin683f9b72004-09-26 13:41:45 +0000888Another practical alternative to this custom format printing would
889be to invoke XER encoder. The default BASIC-XER encoder performs reasonable
890formatting for the output to be useful and human readable. To invoke
891the XER decoder in a manner similar to asn_fprint(), use the xer_fprint()
892call:
Lev Walkineb97a702004-08-07 06:03:15 +0000893
Lev Walkin683f9b72004-09-26 13:41:45 +0000894<P>
895
896<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000897xer_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect);
Lev Walkin683f9b72004-09-26 13:41:45 +0000898</PRE>
899</BLOCKQUOTE>
Lev Walkin1411d052005-02-02 11:06:38 +0000900See Section <A HREF="#sub:Encoding-XER">Encoding XER</A> for XML-related details.
Lev Walkin683f9b72004-09-26 13:41:45 +0000901
902<P>
903
Lev Walkin7f70fe52005-02-22 07:28:26 +0000904<H2><A NAME="SECTION02217000000000000000"></A><A NAME="sub:Freeing-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +0000905Freeing the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +0000906</H2>
Lev Walkineb97a702004-08-07 06:03:15 +0000907
908<P>
909Freeing the structure is slightly more complex than it may seem to.
910When the ASN.1 structure is freed, all the members of the structure
911and their submembers etc etc are recursively freed too. But it might
912not be feasible to free the structure itself. Consider the following
913case:
914
915<P>
916
Lev Walkin26587ab2004-08-23 15:12:04 +0000917<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000918struct&nbsp;my_figure&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;custom&nbsp;structure&nbsp;*/
919&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;*/
920&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;type&nbsp;is&nbsp;generated&nbsp;by&nbsp;the&nbsp;ASN.1&nbsp;compiler&nbsp;*/
921&nbsp;&nbsp;&nbsp;&nbsp;<I>Rectangle_t&nbsp;rect;</I>
922&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;other&nbsp;members&nbsp;of&nbsp;the&nbsp;structure&nbsp;*/
923};
Lev Walkineb97a702004-08-07 06:03:15 +0000924</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000925</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000926In this example, the application programmer defined a custom structure
927with one ASN.1-derived member (rect). This member is not a reference
928to the Rectangle_t, but an in-place inclusion of the Rectangle_t
929structure. If the freeing is necessary, the usual procedure of freeing
930everything must not be applied to the &amp;rect pointer itself, because
Lev Walkin7f70fe52005-02-22 07:28:26 +0000931it does not point to the memory block directly allocated by the memory
932allocation routine, but instead lies within a block allocated for
933the my_figure structure.
Lev Walkineb97a702004-08-07 06:03:15 +0000934
935<P>
936To solve this problem, the free_struct routine has the additional
Lev Walkin7f70fe52005-02-22 07:28:26 +0000937argument (besides the obvious type descriptor and target structure
Lev Walkineb97a702004-08-07 06:03:15 +0000938pointers), which is the flag specifying whether the outer pointer
939itself must be freed (0, default) or it should be left intact (non-zero
940value).
941
942<P>
943
Lev Walkin26587ab2004-08-23 15:12:04 +0000944<BLOCKQUOTE><PRE>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000945<B>/*&nbsp;1.&nbsp;Rectangle_t&nbsp;is&nbsp;defined&nbsp;within&nbsp;my_figure&nbsp;*/</B>
946struct&nbsp;my_figure&nbsp;{
947&nbsp;&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;rect;
948}&nbsp;*mf&nbsp;=&nbsp;<B>...</B>;
Lev Walkineb97a702004-08-07 06:03:15 +0000949/*
Lev Walkin7f70fe52005-02-22 07:28:26 +0000950&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_t
951&nbsp;*&nbsp;without&nbsp;freeing&nbsp;the&nbsp;mf-&gt;rect&nbsp;area
Lev Walkineb97a702004-08-07 06:03:15 +0000952&nbsp;*/
Lev Walkin0f5c5cf2005-08-29 10:23:20 +0000953asn_DEF_Rectangle.free_struct(
Lev Walkin7f70fe52005-02-22 07:28:26 +0000954&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;&amp;mf-&gt;rect,&nbsp;<B>1</B>&nbsp;<B>/*&nbsp;!free&nbsp;*/</B>);
955&nbsp;&nbsp;&nbsp;&nbsp;
956&nbsp;&nbsp;
957<B>/*&nbsp;2.&nbsp;Rectangle_t&nbsp;is&nbsp;a&nbsp;stand-alone&nbsp;pointer&nbsp;*/</B>
Lev Walkineb97a702004-08-07 06:03:15 +0000958Rectangle_t&nbsp;*rect&nbsp;=&nbsp;<B>...</B>;
959/*
960&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_t
961&nbsp;*&nbsp;and&nbsp;freeing&nbsp;the&nbsp;rect&nbsp;pointer
962&nbsp;*/
Lev Walkin0f5c5cf2005-08-29 10:23:20 +0000963asn_DEF_Rectangle.free_struct(
Lev Walkin7f70fe52005-02-22 07:28:26 +0000964&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect,&nbsp;<B>0</B>&nbsp;<B>/*&nbsp;free&nbsp;the&nbsp;pointer&nbsp;too&nbsp;*/</B>);
Lev Walkineb97a702004-08-07 06:03:15 +0000965</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000966</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000967It is safe to invoke the <I>free_struct</I> function with the target
968structure pointer set to 0 (NULL), the function will do nothing.
969
970<P>
Lev Walkin028a28b2005-01-17 11:09:32 +0000971
Lev Walkin7f70fe52005-02-22 07:28:26 +0000972<H1><A NAME="SECTION02300000000000000000"></A><A NAME="cha:Step-by-step-examples"></A><BR>
Lev Walkindee61782005-02-02 09:53:46 +0000973Step by step examples
Lev Walkin028a28b2005-01-17 11:09:32 +0000974</H1>
975
976<P>
Lev Walkine94e3e52005-01-31 10:18:53 +0000977
Lev Walkin7f70fe52005-02-22 07:28:26 +0000978<H1><A NAME="SECTION02310000000000000000">
Lev Walkine94e3e52005-01-31 10:18:53 +0000979A ''Rectangle'' Encoder</A>
980</H1>
981
982<P>
Lev Walkindee61782005-02-02 09:53:46 +0000983This example will help you to create a simple BER and XER encoder
Lev Walkine94e3e52005-01-31 10:18:53 +0000984of a ''Rectangle'' type used throughout this document.
985
986<P>
987
988<OL>
989<LI>Create a file named <B>rectangle.asn1</B> with the following contents:
990
991<P>
992
993<BLOCKQUOTE><PRE>
994RectangleModule1&nbsp;DEFINITIONS&nbsp;::=
995BEGIN
996&nbsp;
997Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
998&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,
999&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER
1000}
1001&nbsp;
1002END
1003</PRE>
1004</BLOCKQUOTE>
1005
1006</LI>
1007<LI>Compile it into the set of .c and .h files using asn1c compiler [<A
1008 HREF="asn1c-usage.html#ASN1C">ASN1C</A>]:
1009
1010<P>
1011
1012<BLOCKQUOTE><PRE>
1013<I>asn1c&nbsp;-fnative-types</I>&nbsp;<B>rectangle.asn1</B>
1014</PRE>
1015</BLOCKQUOTE>
1016
1017</LI>
1018<LI>Alternatively, use the Online ASN.1 compiler [<A
1019 HREF="asn1c-usage.html#AONL">AONL</A>] by uploading
1020the <B>rectangle.asn1</B> file into the Web form and unpacking the
1021produced archive on your computer.
1022</LI>
1023<LI>By this time, you should have gotten multiple files in the current
1024directory, including the <B>Rectangle.c</B> and <B>Rectangle.h</B>.
1025</LI>
1026<LI>Create a main() routine which creates the Rectangle_t structure in
1027memory and encodes it using BER and XER encoding rules. Let's name
1028the file <B>main.c</B>:
1029
1030<P>
1031
1032<BLOCKQUOTE><PRE>
1033<FONT SIZE="-1">#include&nbsp;&lt;stdio.h&gt;</FONT>
1034<FONT SIZE="-1">#include&nbsp;&lt;sys/types.h&gt;</FONT>
1035<FONT SIZE="-1">#include&nbsp;&lt;Rectangle.h&gt;&nbsp;&nbsp;&nbsp;/*&nbsp;Rectangle&nbsp;ASN.1&nbsp;type&nbsp;&nbsp;*/</FONT>
1036&nbsp;
1037<FONT SIZE="-1">/*</FONT>
1038&nbsp;<FONT SIZE="-1">*&nbsp;This&nbsp;is&nbsp;a&nbsp;custom&nbsp;function&nbsp;which&nbsp;writes&nbsp;the</FONT>
1039&nbsp;<FONT SIZE="-1">*&nbsp;encoded&nbsp;output&nbsp;into&nbsp;some&nbsp;FILE&nbsp;stream.</FONT>
1040&nbsp;<FONT SIZE="-1">*/</FONT>
1041<FONT SIZE="-1">static&nbsp;int</FONT>
1042<FONT SIZE="-1">write_out(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;size,&nbsp;void&nbsp;*app_key)&nbsp;{</FONT>
1043&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;FILE&nbsp;*out_fp&nbsp;=&nbsp;app_key;</FONT>
1044&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;size_t&nbsp;wrote;</FONT>
1045&nbsp;
1046&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;wrote&nbsp;=&nbsp;fwrite(buffer,&nbsp;1,&nbsp;size,&nbsp;out_fp);</FONT>
1047&nbsp;
1048&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;return&nbsp;(wrote&nbsp;==&nbsp;size)&nbsp;?&nbsp;0&nbsp;:&nbsp;-1;</FONT>
1049<FONT SIZE="-1">}</FONT>
1050&nbsp;
Lev Walkin6b0df9f2005-01-31 15:06:32 +00001051<FONT SIZE="-1">int&nbsp;main(int&nbsp;ac,&nbsp;char&nbsp;**av)&nbsp;{</FONT>
Lev Walkine94e3e52005-01-31 10:18:53 +00001052&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;*rectangle;&nbsp;/*&nbsp;Type&nbsp;to&nbsp;encode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</FONT>
1053&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;asn_enc_rval_t&nbsp;ec;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Encoder&nbsp;return&nbsp;value&nbsp;&nbsp;*/</FONT>
1054&nbsp;
1055&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Allocate&nbsp;the&nbsp;Rectangle_t&nbsp;*/</FONT>
Lev Walkindee61782005-02-02 09:53:46 +00001056&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;rectangle&nbsp;=&nbsp;calloc(1,&nbsp;sizeof(Rectangle_t));&nbsp;/*&nbsp;not&nbsp;malloc!&nbsp;*/</FONT>
Lev Walkine94e3e52005-01-31 10:18:53 +00001057&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(!rectangle)&nbsp;{</FONT>
1058&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(''calloc()&nbsp;failed'');</FONT>
1059&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(71);&nbsp;/*&nbsp;better,&nbsp;EX_OSERR&nbsp;*/</FONT>
1060&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1061&nbsp;
1062&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Initialize&nbsp;the&nbsp;Rectangle&nbsp;members&nbsp;*/</FONT>
1063&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;rectangle-&gt;height&nbsp;=&nbsp;42;&nbsp;&nbsp;/*&nbsp;any&nbsp;random&nbsp;value&nbsp;*/</FONT>
1064&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;rectangle-&gt;width&nbsp;&nbsp;=&nbsp;23;&nbsp;&nbsp;/*&nbsp;any&nbsp;random&nbsp;value&nbsp;*/</FONT>
1065&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;</FONT>
1066&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;BER&nbsp;encode&nbsp;the&nbsp;data&nbsp;if&nbsp;filename&nbsp;is&nbsp;given&nbsp;*/</FONT>
1067&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(ac&nbsp;&lt;&nbsp;2)&nbsp;{</FONT>
1068&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''Specify&nbsp;filename&nbsp;for&nbsp;BER&nbsp;output&#92;n'');</FONT>
1069&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{</FONT>
1070&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;char&nbsp;*filename&nbsp;=&nbsp;av[1];</FONT>
Lev Walkind4eb17d2005-07-21 18:45:37 +00001071&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;*fp&nbsp;=&nbsp;fopen(filename,&nbsp;''wb'');&nbsp;&nbsp;&nbsp;/*&nbsp;for&nbsp;BER&nbsp;output&nbsp;*/</FONT>
Lev Walkine94e3e52005-01-31 10:18:53 +00001072&nbsp;
1073&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!fp)&nbsp;{</FONT>
1074&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(filename);</FONT>
1075&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(71);&nbsp;/*&nbsp;better,&nbsp;EX_OSERR&nbsp;*/</FONT>
1076&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</FONT>
1077&nbsp;<FONT SIZE="-1">&nbsp;</FONT>
1078&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Encode&nbsp;the&nbsp;Rectangle&nbsp;type&nbsp;as&nbsp;BER&nbsp;(DER)&nbsp;*/</FONT>
1079&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ec&nbsp;=&nbsp;der_encode(&amp;asn_DEF_Rectangle,</FONT>
Lev Walkin6b0df9f2005-01-31 15:06:32 +00001080&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rectangle,&nbsp;write_out,&nbsp;fp);</FONT>
Lev Walkine94e3e52005-01-31 10:18:53 +00001081&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose(fp);</FONT>
1082&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(ec.encoded&nbsp;==&nbsp;-1)&nbsp;{</FONT>
1083&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,</FONT>
1084&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;''Could&nbsp;not&nbsp;encode&nbsp;Rectangle&nbsp;(at&nbsp;%s)&#92;n'',</FONT>
1085&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ec.failed_type&nbsp;?&nbsp;ec.failed_type-&gt;name&nbsp;:&nbsp;''unknown'');</FONT>
1086&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(65);&nbsp;/*&nbsp;better,&nbsp;EX_DATAERR&nbsp;*/</FONT>
1087&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{</FONT>
1088&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''Created&nbsp;%s&nbsp;with&nbsp;BER&nbsp;encoded&nbsp;Rectangle&#92;n'',</FONT>
1089&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename);</FONT>
1090&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</FONT>
1091&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1092&nbsp;
1093&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Also&nbsp;print&nbsp;the&nbsp;constructed&nbsp;Rectangle&nbsp;XER&nbsp;encoded&nbsp;(XML)&nbsp;*/</FONT>
1094&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;xer_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rectangle);</FONT>
1095&nbsp;
1096&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;return&nbsp;0;&nbsp;/*&nbsp;Encoding&nbsp;finished&nbsp;successfully&nbsp;*/</FONT>
1097<FONT SIZE="-1">}</FONT>
1098</PRE>
1099</BLOCKQUOTE>
1100
1101</LI>
1102<LI>Compile all files together using C compiler (varies by platform):
1103
1104<P>
1105
1106<BLOCKQUOTE><PRE>
1107<I>cc&nbsp;-I.&nbsp;-o</I>&nbsp;<B><I>rencode</I></B>&nbsp;<I>*.c</I>
1108</PRE>
1109</BLOCKQUOTE>
1110
1111</LI>
1112<LI>Voila! You have just created the BER and XER encoder of a Rectangle
1113type, named <B>rencode</B>!
1114</LI>
1115</OL>
1116
1117
1118<P>
1119
Lev Walkin7f70fe52005-02-22 07:28:26 +00001120<H1><A NAME="SECTION02320000000000000000"></A><A NAME="sec:A-Rectangle-Decoder"></A><BR>
Lev Walkindee61782005-02-02 09:53:46 +00001121A ''Rectangle'' Decoder
Lev Walkine94e3e52005-01-31 10:18:53 +00001122</H1>
1123
1124<P>
Lev Walkindee61782005-02-02 09:53:46 +00001125This example will help you to create a simple BER decoder of a simple
Lev Walkin028a28b2005-01-17 11:09:32 +00001126''Rectangle'' type used throughout this document.
1127
1128<P>
1129
1130<OL>
1131<LI>Create a file named <B>rectangle.asn1</B> with the following contents:
1132
1133<P>
1134
1135<BLOCKQUOTE><PRE>
1136RectangleModule1&nbsp;DEFINITIONS&nbsp;::=
1137BEGIN
1138&nbsp;
1139Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
1140&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,
1141&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER
1142}
1143&nbsp;
1144END
1145</PRE>
1146</BLOCKQUOTE>
1147
1148</LI>
1149<LI>Compile it into the set of .c and .h files using asn1c compiler [<A
1150 HREF="asn1c-usage.html#ASN1C">ASN1C</A>]:
1151
1152<P>
1153
1154<BLOCKQUOTE><PRE>
1155<I>asn1c&nbsp;-fnative-types</I>&nbsp;<B>rectangle.asn1</B>
1156</PRE>
1157</BLOCKQUOTE>
1158
1159</LI>
1160<LI>Alternatively, use the Online ASN.1 compiler [<A
1161 HREF="asn1c-usage.html#AONL">AONL</A>] by uploading
1162the <B>rectangle.asn1</B> file into the Web form and unpacking the
1163produced archive on your computer.
1164</LI>
1165<LI>By this time, you should have gotten multiple files in the current
1166directory, including the <B>Rectangle.c</B> and <B>Rectangle.h</B>.
1167</LI>
1168<LI>Create a main() routine which takes the binary input file, decodes
1169it as it were a BER-encoded Rectangle type, and prints out the text
1170(XML) representation of the Rectangle type. Let's name the file <B>main.c</B>:
1171
1172<P>
1173
1174<BLOCKQUOTE><PRE>
1175<FONT SIZE="-1">#include&nbsp;&lt;stdio.h&gt;</FONT>
1176<FONT SIZE="-1">#include&nbsp;&lt;sys/types.h&gt;</FONT>
1177<FONT SIZE="-1">#include&nbsp;&lt;Rectangle.h&gt;&nbsp;&nbsp;&nbsp;/*&nbsp;Rectangle&nbsp;ASN.1&nbsp;type&nbsp;&nbsp;*/</FONT>
1178&nbsp;
1179<FONT SIZE="-1">int&nbsp;main(int&nbsp;ac,&nbsp;char&nbsp;**av)&nbsp;{</FONT>
1180&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;char&nbsp;buf[1024];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Temporary&nbsp;buffer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</FONT>
1181&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;*rectangle&nbsp;=&nbsp;0;&nbsp;/*&nbsp;Type&nbsp;to&nbsp;decode&nbsp;*/</FONT>
1182&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;asn_dec_rval_t&nbsp;rval;&nbsp;/*&nbsp;Decoder&nbsp;return&nbsp;value&nbsp;&nbsp;*/</FONT>
1183&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;FILE&nbsp;*fp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Input&nbsp;file&nbsp;handler&nbsp;&nbsp;&nbsp;&nbsp;*/</FONT>
1184&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;size_t&nbsp;size;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Number&nbsp;of&nbsp;bytes&nbsp;read&nbsp;&nbsp;*/</FONT>
1185&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;char&nbsp;*filename;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Input&nbsp;file&nbsp;name&nbsp;*/</FONT>
1186&nbsp;
1187&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Require&nbsp;a&nbsp;single&nbsp;filename&nbsp;argument&nbsp;*/</FONT>
1188&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(ac&nbsp;!=&nbsp;2)&nbsp;{</FONT>
1189&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''Usage:&nbsp;%s&nbsp;&lt;file.ber&gt;&#92;n'',&nbsp;av[0]);</FONT>
1190&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(64);&nbsp;/*&nbsp;better,&nbsp;EX_USAGE&nbsp;*/</FONT>
1191&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{</FONT>
1192&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename&nbsp;=&nbsp;av[1];</FONT>
1193&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1194&nbsp;
1195&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Open&nbsp;input&nbsp;file&nbsp;as&nbsp;read-only&nbsp;binary&nbsp;*/</FONT>
1196&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;fp&nbsp;=&nbsp;fopen(filename,&nbsp;''rb'');</FONT>
1197&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(!fp)&nbsp;{</FONT>
1198&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(filename);</FONT>
1199&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(66);&nbsp;/*&nbsp;better,&nbsp;EX_NOINPUT&nbsp;*/</FONT>
1200&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1201&nbsp;<FONT SIZE="-1">&nbsp;</FONT>
1202&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Read&nbsp;up&nbsp;to&nbsp;the&nbsp;buffer&nbsp;size&nbsp;*/</FONT>
1203&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;size&nbsp;=&nbsp;fread(buf,&nbsp;1,&nbsp;sizeof(buf),&nbsp;fp);</FONT>
1204&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;fclose(fp);</FONT>
1205&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(!size)&nbsp;{</FONT>
1206&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''%s:&nbsp;Empty&nbsp;or&nbsp;broken&#92;n'',&nbsp;filename);</FONT>
1207&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(65);&nbsp;/*&nbsp;better,&nbsp;EX_DATAERR&nbsp;*/</FONT>
1208&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1209&nbsp;
1210&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Decode&nbsp;the&nbsp;input&nbsp;buffer&nbsp;as&nbsp;Rectangle&nbsp;type&nbsp;*/</FONT>
1211&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;rval&nbsp;=&nbsp;ber_decode(0,&nbsp;&amp;asn_DEF_Rectangle,</FONT>
1212&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void&nbsp;**)&amp;rectangle,&nbsp;buf,&nbsp;size);</FONT>
1213&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(rval.code&nbsp;!=&nbsp;RC_OK)&nbsp;{</FONT>
1214&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,</FONT>
1215&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;''%s:&nbsp;Broken&nbsp;Rectangle&nbsp;encoding&nbsp;at&nbsp;byte&nbsp;%ld&#92;n'',</FONT>
1216&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename,&nbsp;(long)rval.consumed);</FONT>
1217&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(65);&nbsp;/*&nbsp;better,&nbsp;EX_DATAERR&nbsp;*/</FONT>
1218&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1219&nbsp;
1220&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Print&nbsp;the&nbsp;decoded&nbsp;Rectangle&nbsp;type&nbsp;as&nbsp;XML&nbsp;*/</FONT>
1221&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;xer_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rectangle);</FONT>
1222&nbsp;
1223&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;return&nbsp;0;&nbsp;/*&nbsp;Decoding&nbsp;finished&nbsp;successfully&nbsp;*/</FONT>
1224<FONT SIZE="-1">}</FONT>
1225</PRE>
1226</BLOCKQUOTE>
1227
1228</LI>
1229<LI>Compile all files together using C compiler (varies by platform):
1230
1231<P>
1232
1233<BLOCKQUOTE><PRE>
1234<I>cc&nbsp;-I.&nbsp;-o</I>&nbsp;<B><I>rdecode</I></B>&nbsp;<I>*.c</I>
1235</PRE>
1236</BLOCKQUOTE>
1237
1238</LI>
Lev Walkine94e3e52005-01-31 10:18:53 +00001239<LI>Voila! You have just created the BER decoder of a Rectangle type,
1240named <B>rdecode</B>!
Lev Walkin028a28b2005-01-17 11:09:32 +00001241</LI>
Lev Walkindee61782005-02-02 09:53:46 +00001242</OL>
1243
1244<P>
1245
Lev Walkin7f70fe52005-02-22 07:28:26 +00001246<H1><A NAME="SECTION02400000000000000000">
Lev Walkindee61782005-02-02 09:53:46 +00001247Constraint validation examples</A>
1248</H1>
1249
1250<P>
1251This chapter shows how to define ASN.1 constraints and use the generated
1252validation code.
1253
1254<P>
1255
Lev Walkin7f70fe52005-02-22 07:28:26 +00001256<H1><A NAME="SECTION02410000000000000000">
Lev Walkindee61782005-02-02 09:53:46 +00001257Adding constraints into ''Rectangle'' type</A>
1258</H1>
1259
1260<P>
1261This example shows how to add basic constraints to the ASN.1 specification
1262and how to invoke the constraints validation code in your application.
1263
1264<P>
1265
1266<OL>
1267<LI>Create a file named <B>rectangle.asn1</B> with the following contents:
1268
1269<P>
1270
1271<BLOCKQUOTE><PRE>
1272RectangleModuleWithConstraints&nbsp;DEFINITIONS&nbsp;::=
1273BEGIN
1274&nbsp;
1275Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
1276&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER&nbsp;(0..100),&nbsp;--&nbsp;Value&nbsp;range&nbsp;constraint
1277&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER&nbsp;(0..MAX)&nbsp;&nbsp;--&nbsp;Makes&nbsp;width&nbsp;non-negative&nbsp;
1278}
1279&nbsp;
1280END
1281</PRE>
1282</BLOCKQUOTE>
1283
1284</LI>
1285<LI>Compile the file according to procedures shown in the previous chapter.
1286</LI>
1287<LI>Modify the Rectangle type processing routine (you can start with the
Lev Walkin1411d052005-02-02 11:06:38 +00001288main() routine shown in the Section <A HREF="#sec:A-Rectangle-Decoder">A Rectangle Decoder</A>)
Lev Walkindee61782005-02-02 09:53:46 +00001289by placing the following snippet of code <I>before</I> encoding and/or
Lev Walkin7f70fe52005-02-22 07:28:26 +00001290<I>after</I> decoding the Rectangle type<A NAME="tex2html8"
Lev Walkinf47d0682005-03-05 01:07:04 +00001291 HREF="#foot923"><SUP>4.1</SUP></A>:
Lev Walkindee61782005-02-02 09:53:46 +00001292
1293<P>
1294
1295<BLOCKQUOTE><PRE>
1296<FONT SIZE="-1">int&nbsp;ret;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Return&nbsp;value&nbsp;*/</FONT>
1297<FONT SIZE="-1">char&nbsp;errbuf[128];&nbsp;&nbsp;/*&nbsp;Buffer&nbsp;for&nbsp;error&nbsp;message&nbsp;*/</FONT>
1298<FONT SIZE="-1">size_t&nbsp;errlen&nbsp;=&nbsp;sizeof(errbuf);&nbsp;&nbsp;/*&nbsp;Size&nbsp;of&nbsp;the&nbsp;buffer&nbsp;*/</FONT>
1299&nbsp;<FONT SIZE="-1">&nbsp;</FONT>
1300<FONT SIZE="-1">/*&nbsp;...&nbsp;here&nbsp;may&nbsp;go&nbsp;Rectangle&nbsp;decoding&nbsp;code&nbsp;...&nbsp;*/</FONT>
1301&nbsp;
Lev Walkin0f5c5cf2005-08-29 10:23:20 +00001302<FONT SIZE="-1">ret&nbsp;=&nbsp;asn_check_constraints(&amp;asn_DEF_Rectangle,</FONT>
Lev Walkindee61782005-02-02 09:53:46 +00001303&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rectangle,&nbsp;errbuf,&nbsp;&amp;errlen);</FONT>
1304<FONT SIZE="-1">/*&nbsp;assert(errlen&nbsp;&lt;&nbsp;sizeof(errbuf));&nbsp;//&nbsp;you&nbsp;may&nbsp;rely&nbsp;on&nbsp;that&nbsp;*/</FONT>
1305<FONT SIZE="-1">if(ret)&nbsp;{</FONT>
1306&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''Constraint&nbsp;validation&nbsp;failed:&nbsp;%s&#92;n'',</FONT>
1307&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errbuf&nbsp;&nbsp;&nbsp;/*&nbsp;errbuf&nbsp;is&nbsp;properly&nbsp;nul-terminated&nbsp;*/</FONT>
1308&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);</FONT>
1309&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;exit(...);&nbsp;//&nbsp;Replace&nbsp;with&nbsp;appropriate&nbsp;action&nbsp;*/</FONT>
1310<FONT SIZE="-1">}</FONT>
1311&nbsp;
1312<FONT SIZE="-1">/*&nbsp;...&nbsp;here&nbsp;may&nbsp;go&nbsp;Rectangle&nbsp;encoding&nbsp;code&nbsp;...&nbsp;*/</FONT>
1313</PRE>
1314</BLOCKQUOTE>
1315
1316</LI>
1317<LI>Compile the resulting C code as shown in the previous chapters.
1318</LI>
Lev Walkin1411d052005-02-02 11:06:38 +00001319<LI>Try to test the constraints checking code by assigning integer value
1320101 to the <B>.height</B> member of the Rectangle structure, or
1321a negative value to the <B>.width</B> member. In either case, the
1322program should print ''Constraint validation failed'' message, followed
1323by the short explanation why validation did not succeed.
1324</LI>
Lev Walkindee61782005-02-02 09:53:46 +00001325<LI>Done.
1326</LI>
Lev Walkin7f70fe52005-02-22 07:28:26 +00001327</OL>
1328
1329<P>
1330
1331<H1><A NAME="SECTION03000000000000000000"></A><A NAME="par:ASN.1-Basics"></A><BR>
1332ASN.1 Basics
1333</H1>
1334
1335<P>
1336
1337<H1><A NAME="SECTION03100000000000000000"></A><A NAME="cha:Abstract-Syntax-Notation:"></A><BR>
1338Abstract Syntax Notation: ASN.1
1339</H1>
1340
1341<P>
1342<I>This chapter defines some basic ASN.1 concepts and describes
1343several most widely used types. It is by no means an authoritative
1344or complete reference. For more complete ASN.1 description, please
1345refer to Olivier Dubuisson's book [<A
1346 HREF="asn1c-usage.html#Dub00">Dub00</A>] or the ASN.1 body
1347of standards itself [<A
1348 HREF="asn1c-usage.html#ITU-T_ASN.1">ITU-T/ASN.1</A>].</I>
1349
1350<P>
1351The Abstract Syntax Notation One is used to formally describe the
1352semantics of data transmitted across the network. Two communicating
1353parties may have different formats of their native data types (i.e.
1354number of bits in the integer type), thus it is important to have
1355a way to describe the data in a manner which is independent from the
1356particular machine's representation. The ASN.1 specifications are
1357used to achieve the following:
1358
1359<P>
1360
1361<UL>
1362<LI>The specification expressed in the ASN.1 notation is a formal and
1363precise way to communicate the data semantics to human readers;
1364</LI>
1365<LI>The ASN.1 specifications may be used as input for automatic compilers
1366which produce the code for some target language (C, C++, Java, etc)
1367to encode and decode the data according to some encoding rules (which
1368are also defined by the ASN.1 standard).
1369</LI>
1370</UL>
1371Consider the following example:
1372
1373<P>
1374
1375<BLOCKQUOTE><PRE>
1376Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
1377&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,
1378&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER
1379}
1380</PRE>
1381</BLOCKQUOTE>
1382This ASN.1 specification describes a constructed type, <I>Rectangle</I>,
1383containing two integer fields. This specification may tell the reader
1384that there exists this kind of data structure and that some entity
1385may be prepared to send or receive it. The question on <I>how</I>
1386that entity is going to send or receive the <I>encoded data</I> is
1387outside the scope of ASN.1. For example, this data structure may be
1388encoded according to some encoding rules and sent to the destination
1389using the TCP protocol. The ASN.1 specifies several ways of encoding
1390(or ''serializing'', or ''marshaling'') the data: BER, PER, XER
1391and others, including CER and DER derivatives from BER.
1392
1393<P>
1394The complete specification must be wrapped in a module, which looks
1395like this:
1396
1397<P>
1398
1399<BLOCKQUOTE><PRE>
1400RectangleModule1
1401&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;iso&nbsp;org(3)&nbsp;dod(6)&nbsp;internet(1)&nbsp;private(4)
1402&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enterprise(1)&nbsp;spelio(9363)&nbsp;software(1)
1403&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn1c(5)&nbsp;docs(2)&nbsp;rectangle(1)&nbsp;1&nbsp;}&nbsp;
1404&nbsp;&nbsp;&nbsp;&nbsp;DEFINITIONS&nbsp;AUTOMATIC&nbsp;TAGS&nbsp;::=
1405BEGIN
1406&nbsp;
1407--&nbsp;This&nbsp;is&nbsp;a&nbsp;comment&nbsp;which&nbsp;describes&nbsp;nothing.
1408Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
1409&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;Height&nbsp;of&nbsp;the&nbsp;rectangle
1410&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
1411}
1412&nbsp;
1413END
1414</PRE>
1415</BLOCKQUOTE>
1416The module header consists of module name (RectangleModule1), the
1417module object identifier ({...}), a keyword ''DEFINITIONS'', a
1418set of module flags (AUTOMATIC TAGS) and ''::= BEGIN''. The module
1419ends with an ''END'' statement.
1420
1421<P>
1422
1423<H1><A NAME="SECTION03110000000000000000">
1424Some of the ASN.1 Basic Types</A>
1425</H1>
1426
1427<P>
1428
1429<H2><A NAME="SECTION03111000000000000000">
1430The BOOLEAN type</A>
1431</H2>
1432
1433<P>
1434The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF
1435or a similar kind of two-way choice.
1436
1437<P>
1438
1439<H2><A NAME="SECTION03112000000000000000">
1440The INTEGER type</A>
1441</H2>
1442
1443<P>
1444The INTEGER type is a signed natural number type without any restrictions
1445on its size. If the automatic checking on INTEGER value bounds are
1446necessary, the subtype constraints must be used.
1447
1448<P>
1449
1450<BLOCKQUOTE><PRE>
1451SimpleInteger&nbsp;::=&nbsp;INTEGER
1452&nbsp;
1453--&nbsp;An&nbsp;integer&nbsp;with&nbsp;a&nbsp;very&nbsp;limited&nbsp;range
1454SmallPositiveInt&nbsp;::=&nbsp;INTEGER&nbsp;(0..127)
1455&nbsp;
1456--&nbsp;Integer,&nbsp;negative
1457NegativeInt&nbsp;::=&nbsp;INTEGER&nbsp;(MIN..0)
1458</PRE>
1459</BLOCKQUOTE>
1460
1461<P>
1462
1463<H2><A NAME="SECTION03113000000000000000">
1464The ENUMERATED type</A>
1465</H2>
1466
1467<P>
1468The ENUMERATED type is semantically equivalent to the INTEGER type
1469with some integer values explicitly named.
1470
1471<P>
1472
1473<BLOCKQUOTE><PRE>
1474FruitId&nbsp;::=&nbsp;ENUMERATED&nbsp;{&nbsp;apple(1),&nbsp;orange(2)&nbsp;}
1475&nbsp;
1476--&nbsp;The&nbsp;numbers&nbsp;in&nbsp;braces&nbsp;are&nbsp;optional,
1477--&nbsp;the&nbsp;enumeration&nbsp;can&nbsp;be&nbsp;performed
1478--&nbsp;automatically&nbsp;by&nbsp;the&nbsp;compiler
1479ComputerOSType&nbsp;::=&nbsp;ENUMERATED&nbsp;{
1480&nbsp;&nbsp;&nbsp;&nbsp;FreeBSD,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;acquires&nbsp;value&nbsp;0
1481&nbsp;&nbsp;&nbsp;&nbsp;Windows,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;acquires&nbsp;value&nbsp;1
1482&nbsp;&nbsp;&nbsp;&nbsp;Solaris(5),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;remains&nbsp;5
1483&nbsp;&nbsp;&nbsp;&nbsp;Linux,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;becomes&nbsp;6
1484&nbsp;&nbsp;&nbsp;&nbsp;MacOS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;becomes&nbsp;7
1485}
1486</PRE>
1487</BLOCKQUOTE>
1488
1489<P>
1490
1491<H2><A NAME="SECTION03114000000000000000">
1492The OCTET STRING type</A>
1493</H2>
1494
1495<P>
1496This type models the sequence of 8-bit bytes. This may be used to
1497transmit some opaque data or data serialized by other types of encoders
1498(i.e. video file, photo picture, etc).
1499
1500<P>
1501
1502<H2><A NAME="SECTION03115000000000000000">
1503The OBJECT IDENTIFIER type</A>
1504</H2>
1505
1506<P>
1507The OBJECT IDENTIFIER is used to represent the unique identifier of
1508any object, starting from the very root of the registration tree.
1509If your organization needs to uniquely identify something (a router,
1510a room, a person, a standard, or whatever), you are encouraged to
1511get your own identification subtree at <A HREF=http://www.iana.org/protocols/forms.htm>http://www.iana.org/protocols/forms.htm</A>.
1512
1513<P>
1514For example, the very first ASN.1 module in this Chapter (RectangleModule1)
1515has the following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
1516
1517<P>
1518
1519<BLOCKQUOTE><PRE>
1520ExampleOID&nbsp;::=&nbsp;OBJECT&nbsp;IDENTIFIER
1521&nbsp;
1522rectangleModule1-oid&nbsp;ExampleOID
1523&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;}
1524&nbsp;
1525--&nbsp;An&nbsp;identifier&nbsp;of&nbsp;the&nbsp;Internet.
1526internet-id&nbsp;OBJECT&nbsp;IDENTIFIER
1527&nbsp;&nbsp;::=&nbsp;{&nbsp;iso(1)&nbsp;identified-organization(3)
1528&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dod(6)&nbsp;internet(1)&nbsp;}
1529</PRE>
1530</BLOCKQUOTE>
1531As you see, names are optional.
1532
1533<P>
1534
1535<H2><A NAME="SECTION03116000000000000000">
1536The RELATIVE-OID type</A>
1537</H2>
1538
1539<P>
1540The RELATIVE-OID type has the semantics of a subtree of an OBJECT
1541IDENTIFIER. There may be no need to repeat the whole sequence of numbers
1542from the root of the registration tree where the only thing of interest
1543is some of the tree's subsequence.
1544
1545<P>
1546
1547<BLOCKQUOTE><PRE>
1548this-document&nbsp;RELATIVE-OID&nbsp;::=&nbsp;{&nbsp;docs(2)&nbsp;usage(1)&nbsp;}
1549&nbsp;
1550this-example&nbsp;RELATIVE-OID&nbsp;::=&nbsp;{
1551&nbsp;&nbsp;&nbsp;&nbsp;this-document&nbsp;assorted-examples(0)&nbsp;this-example(1)&nbsp;}
1552</PRE>
1553</BLOCKQUOTE>
1554
1555<P>
1556
1557<H1><A NAME="SECTION03120000000000000000">
1558Some of the ASN.1 String Types</A>
1559</H1>
1560
1561<P>
1562
1563<H2><A NAME="SECTION03121000000000000000">
1564The IA5String type</A>
1565</H2>
1566
1567<P>
1568This is essentially the ASCII, with 128 character codes available
1569(7 lower bits of an 8-bit byte).
1570
1571<P>
1572
1573<H2><A NAME="SECTION03122000000000000000">
1574The UTF8String type</A>
1575</H2>
1576
1577<P>
1578This is the character string which encodes the full Unicode range
1579(4 bytes) using multibyte character sequences.
1580
1581<P>
1582
1583<H2><A NAME="SECTION03123000000000000000">
1584The NumericString type</A>
1585</H2>
1586
1587<P>
1588This type represents the character string with the alphabet consisting
1589of numbers (''0'' to ''9'') and a space.
1590
1591<P>
1592
1593<H2><A NAME="SECTION03124000000000000000">
1594The PrintableString type</A>
1595</H2>
1596
1597<P>
1598The character string with the following alphabet: space, ''<B>'</B>''
1599(single quote), ''<B>(</B>'', ''<B>)</B>'', ''<B>+</B>'',
1600''<B>,</B>'' (comma), ''<B>-</B>'', ''<B>.</B>'', ''<B>/</B>'',
1601digits (''0'' to ''9''), ''<B>:</B>'', ''<B>=</B>'', ''<B>?</B>'',
1602upper-case and lower-case letters (''A'' to ''Z'' and ''a''
1603to ''z'').
1604
1605<P>
1606
1607<H2><A NAME="SECTION03125000000000000000">
1608The VisibleString type</A>
1609</H2>
1610
1611<P>
1612The character string with the alphabet which is more or less a subset
1613of ASCII between the space and the ''<B>~</B>''
1614symbol (tilde).
1615
1616<P>
1617Alternatively, the alphabet may be described as the PrintableString
1618alphabet presented earlier, plus the following characters: ''<B>!</B>'',
1619''<B>''</B>'', ''<B>#</B>'', ''<B>$</B>'', ''<B>%</B>'',
1620''<B>&amp;</B>'', ''<B>*</B>'', ''<B>;</B>'', ''<B>&lt;</B>'',
1621''<B>&gt;</B>'', ''<B>[</B>'', ''<B>&#92;</B>'',
1622''<B>]</B>'', ''<B>&#94;</B>'', ''<B>_</B>'',
1623''<B>`</B>'' (single left quote), ''<B>{</B>'', ''<B>|</B>'',
1624''<B>}</B>'', ''<B>~</B>''.
1625
1626<P>
1627
1628<H1><A NAME="SECTION03130000000000000000">
1629ASN.1 Constructed Types</A>
1630</H1>
1631
1632<P>
1633
1634<H2><A NAME="SECTION03131000000000000000">
1635The SEQUENCE type</A>
1636</H2>
1637
1638<P>
1639This is an ordered collection of other simple or constructed types.
1640The SEQUENCE constructed type resembles the C ''struct'' statement.
1641
1642<P>
1643
1644<BLOCKQUOTE><PRE>
1645Address&nbsp;::=&nbsp;SEQUENCE&nbsp;{
1646&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;The&nbsp;apartment&nbsp;number&nbsp;may&nbsp;be&nbsp;omitted
1647&nbsp;&nbsp;&nbsp;&nbsp;apartmentNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NumericString&nbsp;OPTIONAL,
1648&nbsp;&nbsp;&nbsp;&nbsp;streetName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
1649&nbsp;&nbsp;&nbsp;&nbsp;cityName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
1650&nbsp;&nbsp;&nbsp;&nbsp;stateName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
1651&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;This&nbsp;one&nbsp;may&nbsp;be&nbsp;omitted&nbsp;too
1652&nbsp;&nbsp;&nbsp;&nbsp;zipNo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NumericString&nbsp;OPTIONAL
1653}
1654</PRE>
1655</BLOCKQUOTE>
1656
1657<P>
1658
1659<H2><A NAME="SECTION03132000000000000000">
1660The SET type</A>
1661</H2>
1662
1663<P>
1664This is a collection of other simple or constructed types. Ordering
1665is not important. The data may arrive in the order which is different
1666from the order of specification. Data is encoded in the order not
1667necessarily corresponding to the order of specification.
1668
1669<P>
1670
1671<H2><A NAME="SECTION03133000000000000000">
1672The CHOICE type</A>
1673</H2>
1674
1675<P>
1676This type is just a choice between the subtypes specified in it. The
1677CHOICE type contains at most one of the subtypes specified, and it
1678is always implicitly known which choice is being decoded or encoded.
1679This one resembles the C ''union'' statement.
1680
1681<P>
1682The following type defines a response code, which may be either an
1683integer code or a boolean ''true''/''false'' code.
1684
1685<P>
1686
1687<BLOCKQUOTE><PRE>
1688ResponseCode&nbsp;::=&nbsp;CHOICE&nbsp;{
1689&nbsp;&nbsp;&nbsp;&nbsp;intCode&nbsp;&nbsp;&nbsp;&nbsp;INTEGER,
1690&nbsp;&nbsp;&nbsp;&nbsp;boolCode&nbsp;&nbsp;&nbsp;BOOLEAN
1691}
1692</PRE>
1693</BLOCKQUOTE>
1694
1695<P>
1696
1697<H2><A NAME="SECTION03134000000000000000">
1698The SEQUENCE OF type</A>
1699</H2>
1700
1701<P>
1702This one is the list (array) of simple or constructed types:
1703
1704<P>
1705
1706<BLOCKQUOTE><PRE>
1707--&nbsp;Example&nbsp;1
1708ManyIntegers&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;INTEGER
1709&nbsp;
1710--&nbsp;Example&nbsp;2
1711ManyRectangles&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;Rectangle
1712&nbsp;
1713--&nbsp;More&nbsp;complex&nbsp;example:
1714--&nbsp;an&nbsp;array&nbsp;of&nbsp;structures&nbsp;defined&nbsp;in&nbsp;place.
1715ManyCircles&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;SEQUENCE&nbsp;{
1716&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
1717&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;}
1718</PRE>
1719</BLOCKQUOTE>
1720
1721<P>
1722
1723<H2><A NAME="SECTION03135000000000000000">
1724The SET OF type</A>
1725</H2>
1726
1727<P>
1728The SET OF type models the bag of structures. It resembles the SEQUENCE
1729OF type, but the order is not important: i.e. the elements may arrive
1730in the order which is not necessarily the same as the in-memory order
1731on the remote machines.
1732
1733<P>
1734
1735<BLOCKQUOTE><PRE>
1736--&nbsp;A&nbsp;set&nbsp;of&nbsp;structures&nbsp;defined&nbsp;elsewhere
1737SetOfApples&nbsp;::&nbsp;SET&nbsp;OF&nbsp;Apple
1738&nbsp;
1739--&nbsp;Set&nbsp;of&nbsp;integers&nbsp;encoding&nbsp;the&nbsp;kind&nbsp;of&nbsp;a&nbsp;fruit
1740FruitBag&nbsp;::=&nbsp;SET&nbsp;OF&nbsp;ENUMERATED&nbsp;{&nbsp;apple,&nbsp;orange&nbsp;}
1741</PRE>
1742</BLOCKQUOTE>
1743
1744<H2><A NAME="SECTION04000000000000000000">
Lev Walkineb97a702004-08-07 06:03:15 +00001745Bibliography</A>
Lev Walkin26587ab2004-08-23 15:12:04 +00001746</H2><DL COMPACT><DD><P></P><DT><A NAME="ASN1C">ASN1C</A>
Lev Walkin1411d052005-02-02 11:06:38 +00001747<DD>The Open Source ASN.1 Compiler. <A HREF=http://lionet.info/asn1c>http://lionet.info/asn1c</A>
Lev Walkin028a28b2005-01-17 11:09:32 +00001748<P></P><DT><A NAME="AONL">AONL</A>
Lev Walkin1411d052005-02-02 11:06:38 +00001749<DD>Online ASN.1 Compiler. <A HREF=http://lionet.info/asn1c/asn1c.cgi>http://lionet.info/asn1c/asn1c.cgi</A>
Lev Walkin26587ab2004-08-23 15:12:04 +00001750<P></P><DT><A NAME="Dub00">Dub00</A>
Lev Walkin028a28b2005-01-17 11:09:32 +00001751<DD>Olivier Dubuisson -- <I>ASN.1 Communication between heterogeneous
1752systems</I> -- Morgan Kaufmann Publishers, 2000. <A HREF=http://asn1.elibel.tm.fr/en/book/>http://asn1.elibel.tm.fr/en/book/</A>.
Lev Walkineb97a702004-08-07 06:03:15 +00001753ISBN:0-12-6333361-0.
1754<P></P><DT><A NAME="ITU-T_ASN.1">ITU-T/ASN.1</A>
Lev Walkindee61782005-02-02 09:53:46 +00001755<DD>ITU-T Study Group 17 - Languages for Telecommunication Systems <A HREF=http://www.itu.int/ITU-T/studygroups/com17/languages/>http://www.itu.int/ITU-T/studygroups/com17/languages/</A>
1756</DL>
Lev Walkineb97a702004-08-07 06:03:15 +00001757
1758<P>
1759<BR><HR><H4>Footnotes</H4>
1760<DL>
Lev Walkinf47d0682005-03-05 01:07:04 +00001761<DT><A NAME="foot843">... given</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001762 HREF="asn1c-usage.html#tex2html1"><SUP>1.1</SUP></A></DT>
1763<DD>Please look into Part par:ASN.1-Basics for a quick reference
1764on how to understand the ASN.1 notation.
Lev Walkineb97a702004-08-07 06:03:15 +00001765
1766</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001767<DT><A NAME="foot844">... type</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001768 HREF="asn1c-usage.html#tex2html2"><SUP>1.2</SUP></A></DT>
Lev Walkin6faa68e2004-09-17 08:35:02 +00001769<DD><I>-fnative-types</I> compiler option is used to produce basic C <I>int</I>
1770types instead of infinite width INTEGER_t structures. See <A HREF=#Table1>Table 1</A>.
Lev Walkineb97a702004-08-07 06:03:15 +00001771
1772</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001773<DT><A NAME="foot845">...asn1c</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001774 HREF="asn1c-usage.html#tex2html3"><SUP>1.3</SUP></A></DT>
Lev Walkin26587ab2004-08-23 15:12:04 +00001775<DD>The 1 symbol in asn<B>1</B>c is a digit, not an ''ell'' letter.
1776
1777</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001778<DT><A NAME="foot846">... module</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001779 HREF="asn1c-usage.html#tex2html4"><SUP>1.4</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001780<DD>This is probably <B>not</B> what you want to try out right now -
Lev Walkin7f70fe52005-02-22 07:28:26 +00001781read through the rest of this chapter and check the <A HREF=#Table1>Table 1</A>
1782to find out about <B>-P</B> and <B>-R</B> options.
Lev Walkineb97a702004-08-07 06:03:15 +00001783
1784</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001785<DT><A NAME="foot181">...restartable</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001786 HREF="asn1c-usage.html#tex2html6"><SUP>2.1</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001787<DD>Restartable means that if the decoder encounters the end of the buffer,
1788it will fail, but may later be invoked again with the rest of the
1789buffer to continue decoding.
1790
1791</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001792<DT><A NAME="foot250">... encoding</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001793 HREF="asn1c-usage.html#tex2html7"><SUP>2.2</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001794<DD>It is actually faster too: the encoder might skip over some computations
1795which aren't important for the size determination.
1796
1797</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001798<DT><A NAME="foot923">... type</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001799 HREF="asn1c-usage.html#tex2html8"><SUP>4.1</SUP></A></DT>
Lev Walkindee61782005-02-02 09:53:46 +00001800<DD>Placing the constraint checking code <I>before</I> encoding helps
1801to make sure you know the data is correct and within constraints before
1802sharing the data with anyone else.
1803
1804<P>
1805Placing the constraint checking code <I>after</I> decoding, but before
1806any further action depending on the decoded data, helps to make sure
1807the application got the valid contents before making use of it.
1808
1809</DD>
Lev Walkineb97a702004-08-07 06:03:15 +00001810</DL><BR><HR>
1811<ADDRESS>
1812Lev Walkin
Lev Walkinf47d0682005-03-05 01:07:04 +000018132005-03-04
Lev Walkineb97a702004-08-07 06:03:15 +00001814</ADDRESS>
1815</BODY>
1816</HTML>