blob: 03bfd4bfa5f2499217ccfe2dbbe35bb807373b69 [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 Walkinf47d0682005-03-05 01:07:04 +0000433<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 +0000434<FONT SIZE="-1">Pretend to support only ASN.1:1988 embedded types. Certain
435reserved words, such as UniversalString and BMPString, become ordinary
436type references and may be redefined by the specification.</FONT></TD></TR>
Lev Walkin26587ab2004-08-23 15:12:04 +0000437</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000438<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B><FONT SIZE="-1">Output Options</FONT></B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
439<B><FONT SIZE="-1">Description</FONT></B></TD></TR>
Lev Walkin28c89eb2004-09-30 22:45:58 +0000440<TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>
441
Lev Walkinf47d0682005-03-05 01:07:04 +0000442<FONT SIZE="-1">-print-constraints</FONT></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216">
Lev Walkin26587ab2004-08-23 15:12:04 +0000443<FONT SIZE="-1">When -EF are also specified, this option forces the compiler
444to explain its internal understanding of subtype constraints.</FONT></TD></TR>
445</TBODY><TBODY>
Lev Walkinf47d0682005-03-05 01:07:04 +0000446<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 +0000447<FONT SIZE="-1">Generate &#34;- #line&#34; comments in -E output.</FONT></TD></TR>
448</TBODY>
449</TABLE>
450
451<P>
452</TD></TR>
453</TABLE>
454</DIV><P></P><BR>
455
456<P>
457
Lev Walkin7f70fe52005-02-22 07:28:26 +0000458<H1><A NAME="SECTION02200000000000000000">
459Using the ASN.1 Compiler</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000460</H1>
Lev Walkineb97a702004-08-07 06:03:15 +0000461
462<P>
Lev Walkineb97a702004-08-07 06:03:15 +0000463
Lev Walkin7f70fe52005-02-22 07:28:26 +0000464<H1><A NAME="SECTION02210000000000000000">
465Invoking the ASN.1 helper code</A>
Lev Walkin26587ab2004-08-23 15:12:04 +0000466</H1>
Lev Walkineb97a702004-08-07 06:03:15 +0000467
468<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000469First of all, you should include one or more header files into your
470application. Typically, it is enough to include the header file of
471the main PDU type. For our Rectangle module, including the Rectangle.h
472file is sufficient:
Lev Walkineb97a702004-08-07 06:03:15 +0000473
474<P>
475
Lev Walkin26587ab2004-08-23 15:12:04 +0000476<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000477#include&nbsp;&lt;Rectangle.h&gt;
Lev Walkineb97a702004-08-07 06:03:15 +0000478</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000479</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000480The header files defines the C structure corresponding to the ASN.1
481definition of a rectangle and the declaration of the ASN.1 type descriptor,
482which is used as an argument to most of the functions provided by
483the ASN.1 module. For example, here is the code which frees the Rectangle_t
484structure:
485
486<P>
487
Lev Walkin26587ab2004-08-23 15:12:04 +0000488<BLOCKQUOTE><PRE>
489Rectangle_t&nbsp;*rect&nbsp;=&nbsp;...;
490&nbsp;
Lev Walkinc500b3e2004-09-29 13:37:15 +0000491asn_DEF_Rectangle-&gt;free_struct(&amp;asn_DEF_Rectangle,
Lev Walkineb97a702004-08-07 06:03:15 +0000492&nbsp;&nbsp;&nbsp;&nbsp;rect,&nbsp;0);
Lev Walkineb97a702004-08-07 06:03:15 +0000493</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000494</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000495This code defines a <I>rect</I> pointer which points to the Rectangle_t
496structure which needs to be freed. The second line invokes the generic
Lev Walkin7f70fe52005-02-22 07:28:26 +0000497<I>free_struct()</I> routine created specifically for this Rectangle_t
498structure. The <I>asn_DEF_Rectangle</I> is the type descriptor,
499which holds a collection of routines to deal with the Rectangle_t
500structure.
Lev Walkineb97a702004-08-07 06:03:15 +0000501
502<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000503The following member functions of the asn_DEF_Rectangle type descriptor
504are of interest:
Lev Walkineb97a702004-08-07 06:03:15 +0000505
506<P>
507<DL>
Lev Walkineb97a702004-08-07 06:03:15 +0000508<DT><STRONG>ber_decoder</STRONG></DT>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000509<DD>This is the generic <I>restartable</I><A NAME="tex2html6"
Lev Walkinf47d0682005-03-05 01:07:04 +0000510 HREF="#foot181"><SUP>2.1</SUP></A> BER decoder (Basic Encoding Rules). This decoder would create
Lev Walkineb97a702004-08-07 06:03:15 +0000511and/or fill the target structure for you. Please refer to Section
Lev Walkin7f70fe52005-02-22 07:28:26 +0000512sub:Decoding-BER.
Lev Walkineb97a702004-08-07 06:03:15 +0000513</DD>
514<DT><STRONG>der_encoder</STRONG></DT>
515<DD>This is the generic DER encoder (Distinguished Encoding
Lev Walkin683f9b72004-09-26 13:41:45 +0000516Rules). This encoder will take the target structure and encode it
Lev Walkin7f70fe52005-02-22 07:28:26 +0000517into a series of bytes. Please refer to Section <A HREF="#sub:Encoding-DER">Encoding DER</A>.
Lev Walkineb97a702004-08-07 06:03:15 +0000518</DD>
Lev Walkin683f9b72004-09-26 13:41:45 +0000519<DT><STRONG>xer_encoder</STRONG></DT>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000520<DD>This is the XER encoder (XML Encoding Rules). This
521encoder will take the target structure and represent it as an XML
522(text) document using either BASIC-XER or CANONICAL-XER encoding rules.
523Please refer to Section <A HREF="#sub:Encoding-XER">Encoding XER</A>.
524</DD>
525<DT><STRONG>xer_decoder</STRONG></DT>
526<DD>This is the generic XER decoder. It takes both BASIC-XER
527or CANONICAL-XER encodings and deserializes the data into a local,
528machine-dependent representation. Please refer to Section <A HREF="#sub:Decoding-XER">Decoding XER</A>.
Lev Walkin683f9b72004-09-26 13:41:45 +0000529</DD>
530<DT><STRONG>check_constraints</STRONG></DT>
531<DD>Check that the contents of the target structure
532are semantically valid and constrained to appropriate implicit or
Lev Walkin1411d052005-02-02 11:06:38 +0000533explicit subtype constraints. Please refer to Section <A HREF="#sub:Validating-the-target">Validating the target</A>.
Lev Walkin683f9b72004-09-26 13:41:45 +0000534</DD>
Lev Walkineb97a702004-08-07 06:03:15 +0000535<DT><STRONG>print_struct</STRONG></DT>
536<DD>This function convert the contents of the passed target
537structure into human readable form. This form is not formal and cannot
538be converted back into the structure, but it may turn out to be useful
Lev Walkin7f70fe52005-02-22 07:28:26 +0000539for 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 +0000540</DD>
541<DT><STRONG>free_struct</STRONG></DT>
542<DD>This is a generic disposal which frees the target structure.
Lev Walkin7f70fe52005-02-22 07:28:26 +0000543Please refer to Section <A HREF="#sub:Freeing-the-target">Freeing the target</A>.
Lev Walkineb97a702004-08-07 06:03:15 +0000544</DD>
545</DL>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000546Each of the above function takes the type descriptor (<I>asn_DEF_...</I>)
Lev Walkin7f70fe52005-02-22 07:28:26 +0000547and the target structure (<I>rect</I>, in the above example).
Lev Walkineb97a702004-08-07 06:03:15 +0000548
549<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000550
551<H2><A NAME="SECTION02211000000000000000"></A><A NAME="sub:Decoding-BER"></A><BR>
552Decoding BER
553</H2>
554
555<P>
556The Basic Encoding Rules describe the most widely used (by the ASN.1
557community) way to encode and decode a given structure in a machine-independent
558way. Several other encoding rules (CER, DER) define a more restrictive
559versions of BER, so the generic BER parser is also capable of decoding
560the data encoded by CER and DER encoders. The opposite is not true.
561
562<P>
563<I>The ASN.1 compiler provides the generic BER decoder which is
564implicitly capable of decoding BER, CER and DER encoded data.</I>
565
566<P>
567The decoder is restartable (stream-oriented), which means that in
568case the buffer has less data than it is expected, the decoder will
569process whatever there is available and ask for more data to be provided.
570Please note that the decoder may actually process less data than it
571was given in the buffer, which means that you must be able to make
572the next buffer contain the unprocessed part of the previous buffer.
573
574<P>
575Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
576
577<P>
578
579<UL>
580<LI>You may concatenate these buffers and feed the BER decoder with 300
581bytes of data, or
582</LI>
583<LI>You may feed it the first buffer of 100 bytes of data, realize that
584the ber_decoder consumed only 95 bytes from it and later feed the
585decoder with 205 bytes buffer which consists of 5 unprocessed bytes
586from the first buffer and the additional 200 bytes from the second
587buffer.
588</LI>
589</UL>
590This is not as convenient as it could be (like, the BER encoder could
591consume the whole 100 bytes and keep these 5 bytes in some temporary
592storage), but in case of existing stream based processing it might
593actually fit well into existing algorithm. Suggestions are welcome.
594
595<P>
596Here is the simplest example of BER decoding.
Lev Walkineb97a702004-08-07 06:03:15 +0000597
598<P>
599
Lev Walkin26587ab2004-08-23 15:12:04 +0000600<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000601Rectangle_t&nbsp;*
Lev Walkin26587ab2004-08-23 15:12:04 +0000602simple_deserializer(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;buf_size)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +0000603&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 +0000604&nbsp;&nbsp;&nbsp;&nbsp;asn_dec_rval_t&nbsp;rval;
Lev Walkineb97a702004-08-07 06:03:15 +0000605&nbsp;
Lev Walkin7f70fe52005-02-22 07:28:26 +0000606&nbsp;&nbsp;&nbsp;&nbsp;rval&nbsp;=&nbsp;<B>asn_DEF_Rectangle-&gt;ber_decoder</B>(0,
Lev Walkinc500b3e2004-09-29 13:37:15 +0000607&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,
Lev Walkineb97a702004-08-07 06:03:15 +0000608&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void&nbsp;**)&amp;rect,
609&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size,
610&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0);
611&nbsp;
612&nbsp;&nbsp;&nbsp;&nbsp;if(rval<B>.code</B>&nbsp;==&nbsp;RC_OK)&nbsp;{
613&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;*/
614&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
Lev Walkin26587ab2004-08-23 15:12:04 +0000615&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Free&nbsp;partially&nbsp;decoded&nbsp;rect&nbsp;*/
Lev Walkinc500b3e2004-09-29 13:37:15 +0000616&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn_DEF_Rectangle-&gt;free_struct(
617&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 +0000618&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;
619&nbsp;&nbsp;&nbsp;&nbsp;}
620}
Lev Walkineb97a702004-08-07 06:03:15 +0000621</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000622</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000623The code above defines a function, <I>simple_deserializer</I>, which
Lev Walkin7f70fe52005-02-22 07:28:26 +0000624takes a buffer and its length and is expected to return a pointer
625to the Rectangle_t structure. Inside, it tries to convert the bytes
626passed into the target structure (rect) using the BER decoder and
627returns the rect pointer afterwards. If the structure cannot be deserialized,
628it frees the memory which might be left allocated by the unfinished
629<I>ber_decoder</I> routine and returns 0 (no data). (This <B>freeing
630is necessary</B> because the ber_decoder is a restartable procedure,
Lev Walkineb97a702004-08-07 06:03:15 +0000631and may fail just because there is more data needs to be provided
Lev Walkin7f70fe52005-02-22 07:28:26 +0000632before decoding could be finalized). The code above obviously does
633not take into account the way the <I>ber_decoder()</I> failed, so
634the freeing is necessary because the part of the buffer may already
635be decoded into the structure by the time something goes wrong.
Lev Walkineb97a702004-08-07 06:03:15 +0000636
637<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000638A little less wordy would be to invoke a globally available <I>ber_decode()</I>
639function instead of dereferencing the asn_DEF_Rectangle type descriptor:
Lev Walkineb97a702004-08-07 06:03:15 +0000640
641<P>
642
Lev Walkin26587ab2004-08-23 15:12:04 +0000643<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000644rval&nbsp;=&nbsp;ber_decode(0,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;(void&nbsp;**)&amp;rect,
Lev Walkineb97a702004-08-07 06:03:15 +0000645&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size);
Lev Walkineb97a702004-08-07 06:03:15 +0000646</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000647</BLOCKQUOTE>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000648Note that the initial (asn_DEF_Rectangle-&gt;ber_decoder) reference
Lev Walkineb97a702004-08-07 06:03:15 +0000649is gone, and also the last argument (0) is no longer necessary.
650
651<P>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000652These two ways of BER decoder invocations are fully equivalent.
Lev Walkineb97a702004-08-07 06:03:15 +0000653
654<P>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000655The BER de<I>coder</I> may fail because of (<I>the following RC_...
Lev Walkineb97a702004-08-07 06:03:15 +0000656codes are defined in ber_decoder.h</I>):
657
658<P>
659
660<UL>
661<LI>RC_WMORE: There is more data expected than it is provided (stream
662mode continuation feature);
663</LI>
664<LI>RC_FAIL: General failure to decode the buffer;
665</LI>
666<LI>... other codes may be defined as well.
667</LI>
668</UL>
Lev Walkina646ccb2004-10-20 15:48:55 +0000669Together with the return code (.code) the asn_dec_rval_t type contains
Lev Walkineb97a702004-08-07 06:03:15 +0000670the number of bytes which is consumed from the buffer. In the previous
671hypothetical example of two buffers (of 100 and 200 bytes), the first
672call to ber_decode() would return with .code = RC_WMORE and .consumed
673= 95. The .consumed field of the BER decoder return value is <B>always</B>
674valid, even if the decoder succeeds or fails with any other return
675code.
676
677<P>
678Please look into ber_decoder.h for the precise definition of ber_decode()
679and related types.
680
681<P>
682
Lev Walkin7f70fe52005-02-22 07:28:26 +0000683<H2><A NAME="SECTION02212000000000000000"></A><A NAME="sub:Encoding-DER"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +0000684Encoding DER
Lev Walkin26587ab2004-08-23 15:12:04 +0000685</H2>
Lev Walkineb97a702004-08-07 06:03:15 +0000686
687<P>
Lev Walkin683f9b72004-09-26 13:41:45 +0000688The Distinguished Encoding Rules is the <I>canonical</I> variant of
689BER encoding rules. The DER is best suited to encode the structures
690where all the lengths are known beforehand. This is probably exactly
691how you want to encode: either after a BER decoding or after a manual
692fill-up, the target structure contains the data which size is implicitly
Lev Walkin7f70fe52005-02-22 07:28:26 +0000693known before encoding. Among other uses, the DER encoding is used
694to encode X.509 certificates.
Lev Walkineb97a702004-08-07 06:03:15 +0000695
696<P>
697As with BER decoder, the DER encoder may be invoked either directly
Lev Walkinc500b3e2004-09-29 13:37:15 +0000698from the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone
699function, which is somewhat simpler:
Lev Walkineb97a702004-08-07 06:03:15 +0000700
701<P>
702
Lev Walkin26587ab2004-08-23 15:12:04 +0000703<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000704&nbsp;
705/*
706&nbsp;*&nbsp;This&nbsp;is&nbsp;the&nbsp;serializer&nbsp;itself,
707&nbsp;*&nbsp;it&nbsp;supplies&nbsp;the&nbsp;DER&nbsp;encoder&nbsp;with&nbsp;the
708&nbsp;*&nbsp;pointer&nbsp;to&nbsp;the&nbsp;custom&nbsp;output&nbsp;function.
709&nbsp;*/
710ssize_t
711simple_serializer(FILE&nbsp;*ostream,&nbsp;Rectangle_t&nbsp;*rect)&nbsp;{
Lev Walkin683f9b72004-09-26 13:41:45 +0000712&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 +0000713&nbsp;
Lev Walkinc500b3e2004-09-29 13:37:15 +0000714&nbsp;&nbsp;&nbsp;&nbsp;er&nbsp;=&nbsp;der_encode(&amp;asn_DEF_Rect,&nbsp;rect,
Lev Walkin26587ab2004-08-23 15:12:04 +0000715&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_stream,&nbsp;ostream);
Lev Walkin683f9b72004-09-26 13:41:45 +0000716&nbsp;&nbsp;&nbsp;&nbsp;if(er.<B>encoded</B>&nbsp;==&nbsp;-1)&nbsp;{
Lev Walkineb97a702004-08-07 06:03:15 +0000717&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*
Lev Walkin683f9b72004-09-26 13:41:45 +0000718&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 +0000719&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
720&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 +0000721&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 +0000722&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strerror(errno));
723&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-1;
724&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
725&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 +0000726&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;er.encoded;
Lev Walkineb97a702004-08-07 06:03:15 +0000727&nbsp;&nbsp;&nbsp;&nbsp;}
728}
Lev Walkineb97a702004-08-07 06:03:15 +0000729</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000730</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000731As you see, the DER encoder does not write into some sort of buffer
732or something. It just invokes the custom function (possible, multiple
733times) which would save the data into appropriate storage. The optional
734argument <I>app_key</I> is opaque for the DER encoder code and just
735used by <I>_write_stream()</I> as the pointer to the appropriate
736output stream to be used.
737
738<P>
739If the custom write function is not given (passed as 0), then the
740DER encoder will essentially do the same thing (i.e., encode the data)
741but no callbacks will be invoked (so the data goes nowhere). It may
742prove useful to determine the size of the structure's encoding before
Lev Walkin7f70fe52005-02-22 07:28:26 +0000743actually doing the encoding<A NAME="tex2html7"
Lev Walkinf47d0682005-03-05 01:07:04 +0000744 HREF="#foot250"><SUP>2.2</SUP></A>.
Lev Walkineb97a702004-08-07 06:03:15 +0000745
746<P>
747Please look into der_encoder.h for the precise definition of der_encode()
748and related types.
749
750<P>
751
Lev Walkin7f70fe52005-02-22 07:28:26 +0000752<H2><A NAME="SECTION02213000000000000000"></A><A NAME="sub:Encoding-XER"></A><BR>
Lev Walkin683f9b72004-09-26 13:41:45 +0000753Encoding XER
754</H2>
755
756<P>
757The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
758Markup Language, a text-based format for information exchange. The
759encoder routine API comes in two flavors: stdio-based and callback-based.
760With the callback-based encoder, the encoding process is very similar
Lev Walkin1411d052005-02-02 11:06:38 +0000761to the DER one, described in Section <A HREF="#sub:Encoding-DER">Encoding DER</A>. The
Lev Walkin683f9b72004-09-26 13:41:45 +0000762following example uses the definition of write_stream() from up there.
763
764<P>
765
766<BLOCKQUOTE><PRE>
767/*
768&nbsp;*&nbsp;This&nbsp;procedure&nbsp;generates&nbsp;the&nbsp;XML&nbsp;document
769&nbsp;*&nbsp;by&nbsp;invoking&nbsp;the&nbsp;XER&nbsp;encoder.
770&nbsp;*&nbsp;NOTE:&nbsp;Do&nbsp;not&nbsp;copy&nbsp;this&nbsp;code&nbsp;verbatim!
771&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;the&nbsp;stdio&nbsp;output&nbsp;is&nbsp;necessary,
772&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 +0000773&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 +0000774&nbsp;*/
775int
776print_as_XML(FILE&nbsp;*ostream,&nbsp;Rectangle_t&nbsp;*rect)&nbsp;{
777&nbsp;&nbsp;&nbsp;&nbsp;asn_enc_rval_t&nbsp;er;&nbsp;&nbsp;/*&nbsp;Encoder&nbsp;return&nbsp;value&nbsp;*/
778&nbsp;
Lev Walkin7f70fe52005-02-22 07:28:26 +0000779&nbsp;&nbsp;&nbsp;&nbsp;er&nbsp;=&nbsp;xer_encode(&amp;asn_DEF_Rectangle,&nbsp;rect,
Lev Walkin683f9b72004-09-26 13:41:45 +0000780&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XER_F_BASIC,&nbsp;/*&nbsp;BASIC-XER&nbsp;or&nbsp;CANONICAL-XER&nbsp;*/
781&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write_stream,&nbsp;ostream);
782&nbsp;
783&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(er.encoded&nbsp;==&nbsp;-1)&nbsp;?&nbsp;-1&nbsp;:&nbsp;0;
784}
785</PRE>
786</BLOCKQUOTE>
787Please look into xer_encoder.h for the precise definition of xer_encode()
788and related types.
789
790<P>
Lev Walkin1411d052005-02-02 11:06:38 +0000791See Section [<A HREF="#sub:Printing-the-target">Printing the target</A>] for the example of stdio-based
Lev Walkin683f9b72004-09-26 13:41:45 +0000792XML encoder and other pretty-printing suggestions.
793
794<P>
795
Lev Walkin7f70fe52005-02-22 07:28:26 +0000796<H2><A NAME="SECTION02214000000000000000"></A><A NAME="sub:Decoding-XER"></A><BR>
797Decoding XER
798</H2>
799
800<P>
801The data encoded using the XER rules can be subsequently decoded using
802the xer_decode() API call:
803
804<P>
805
806<BLOCKQUOTE><PRE>
807Rectangle_t&nbsp;*
808XML_to_Rectangle(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;buf_size)&nbsp;{
809&nbsp;&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;*rect&nbsp;=&nbsp;0;&nbsp;/*&nbsp;Note&nbsp;this&nbsp;0!&nbsp;*/
810&nbsp;&nbsp;&nbsp;&nbsp;asn_dec_rval_t&nbsp;rval;
811&nbsp;&nbsp;
812&nbsp;&nbsp;&nbsp;&nbsp;rval&nbsp;=&nbsp;xer_decode(0,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;(void&nbsp;**)&amp;rect,
813&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer,&nbsp;buf_size);
814&nbsp;&nbsp;&nbsp;&nbsp;if(rval<B>.code</B>&nbsp;==&nbsp;RC_OK)&nbsp;{
815&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;*/
816&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
817&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Free&nbsp;partially&nbsp;decoded&nbsp;rect&nbsp;*/
818&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn_DEF_Rectangle-&gt;free_struct(
819&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect,&nbsp;0);
820&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;
821&nbsp;&nbsp;&nbsp;&nbsp;}
822}
823</PRE>
824</BLOCKQUOTE>
825The decoder takes both BASIC-XER and CANONICAL-XER encodings.
826
827<P>
828The decoder shares its data consumption properties with BER decoder;
829please read the Section <A HREF="#sub:Decoding-BER">Decoding BER</A> to know more.
830
831<P>
832Please look into xer_decoder.h for the precise definition of xer_decode()
833and related types.
834
835<P>
836
837<H2><A NAME="SECTION02215000000000000000"></A><A NAME="sub:Validating-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +0000838Validating the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +0000839</H2>
Lev Walkineb97a702004-08-07 06:03:15 +0000840
841<P>
842Sometimes the target structure needs to be validated. For example,
843if the structure was created by the application (as opposed to being
844decoded from some external source), some important information required
845by the ASN.1 specification might be missing. On the other hand, the
846successful decoding of the data from some external source does not
847necessarily mean that the data is fully valid either. It might well
848be the case that the specification describes some subtype constraints
849that were not taken into account during decoding, and it would actually
850be useful to perform the last check when the data is ready to be encoded
851or when the data has just been decoded to ensure its validity according
852to some stricter rules.
853
854<P>
855The asn_check_constraints() function checks the type for various
856implicit and explicit constraints. It is recommended to use asn_check_constraints()
857function after each decoding and before each encoding.
858
859<P>
860Please look into constraints.h for the precise definition of asn_check_constraints()
861and related types.
862
863<P>
864
Lev Walkin7f70fe52005-02-22 07:28:26 +0000865<H2><A NAME="SECTION02216000000000000000"></A><A NAME="sub:Printing-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +0000866Printing the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +0000867</H2>
Lev Walkineb97a702004-08-07 06:03:15 +0000868
869<P>
870There are two ways to print the target structure: either invoke the
871print_struct member of the ASN.1 type descriptor, or using the asn_fprint()
872function, which is a simpler wrapper of the former:
873
874<P>
875
Lev Walkin26587ab2004-08-23 15:12:04 +0000876<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000877asn_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect);
Lev Walkineb97a702004-08-07 06:03:15 +0000878</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000879</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000880Please look into constr_TYPE.h for the precise definition of asn_fprint()
881and related types.
882
883<P>
Lev Walkin683f9b72004-09-26 13:41:45 +0000884Another practical alternative to this custom format printing would
885be to invoke XER encoder. The default BASIC-XER encoder performs reasonable
886formatting for the output to be useful and human readable. To invoke
887the XER decoder in a manner similar to asn_fprint(), use the xer_fprint()
888call:
Lev Walkineb97a702004-08-07 06:03:15 +0000889
Lev Walkin683f9b72004-09-26 13:41:45 +0000890<P>
891
892<BLOCKQUOTE><PRE>
Lev Walkinc500b3e2004-09-29 13:37:15 +0000893xer_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rect);
Lev Walkin683f9b72004-09-26 13:41:45 +0000894</PRE>
895</BLOCKQUOTE>
Lev Walkin1411d052005-02-02 11:06:38 +0000896See Section <A HREF="#sub:Encoding-XER">Encoding XER</A> for XML-related details.
Lev Walkin683f9b72004-09-26 13:41:45 +0000897
898<P>
899
Lev Walkin7f70fe52005-02-22 07:28:26 +0000900<H2><A NAME="SECTION02217000000000000000"></A><A NAME="sub:Freeing-the-target"></A><BR>
Lev Walkineb97a702004-08-07 06:03:15 +0000901Freeing the target structure
Lev Walkin26587ab2004-08-23 15:12:04 +0000902</H2>
Lev Walkineb97a702004-08-07 06:03:15 +0000903
904<P>
905Freeing the structure is slightly more complex than it may seem to.
906When the ASN.1 structure is freed, all the members of the structure
907and their submembers etc etc are recursively freed too. But it might
908not be feasible to free the structure itself. Consider the following
909case:
910
911<P>
912
Lev Walkin26587ab2004-08-23 15:12:04 +0000913<BLOCKQUOTE><PRE>
Lev Walkineb97a702004-08-07 06:03:15 +0000914struct&nbsp;my_figure&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;custom&nbsp;structure&nbsp;*/
915&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;*/
916&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;type&nbsp;is&nbsp;generated&nbsp;by&nbsp;the&nbsp;ASN.1&nbsp;compiler&nbsp;*/
917&nbsp;&nbsp;&nbsp;&nbsp;<I>Rectangle_t&nbsp;rect;</I>
918&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;other&nbsp;members&nbsp;of&nbsp;the&nbsp;structure&nbsp;*/
919};
Lev Walkineb97a702004-08-07 06:03:15 +0000920</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000921</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000922In this example, the application programmer defined a custom structure
923with one ASN.1-derived member (rect). This member is not a reference
924to the Rectangle_t, but an in-place inclusion of the Rectangle_t
925structure. If the freeing is necessary, the usual procedure of freeing
926everything must not be applied to the &amp;rect pointer itself, because
Lev Walkin7f70fe52005-02-22 07:28:26 +0000927it does not point to the memory block directly allocated by the memory
928allocation routine, but instead lies within a block allocated for
929the my_figure structure.
Lev Walkineb97a702004-08-07 06:03:15 +0000930
931<P>
932To solve this problem, the free_struct routine has the additional
Lev Walkin7f70fe52005-02-22 07:28:26 +0000933argument (besides the obvious type descriptor and target structure
Lev Walkineb97a702004-08-07 06:03:15 +0000934pointers), which is the flag specifying whether the outer pointer
935itself must be freed (0, default) or it should be left intact (non-zero
936value).
937
938<P>
939
Lev Walkin26587ab2004-08-23 15:12:04 +0000940<BLOCKQUOTE><PRE>
Lev Walkin7f70fe52005-02-22 07:28:26 +0000941<B>/*&nbsp;1.&nbsp;Rectangle_t&nbsp;is&nbsp;defined&nbsp;within&nbsp;my_figure&nbsp;*/</B>
942struct&nbsp;my_figure&nbsp;{
943&nbsp;&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;rect;
944}&nbsp;*mf&nbsp;=&nbsp;<B>...</B>;
Lev Walkineb97a702004-08-07 06:03:15 +0000945/*
Lev Walkin7f70fe52005-02-22 07:28:26 +0000946&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_t
947&nbsp;*&nbsp;without&nbsp;freeing&nbsp;the&nbsp;mf-&gt;rect&nbsp;area
Lev Walkineb97a702004-08-07 06:03:15 +0000948&nbsp;*/
Lev Walkinc500b3e2004-09-29 13:37:15 +0000949asn_DEF_Rectangle-&gt;free_struct(
Lev Walkin7f70fe52005-02-22 07:28:26 +0000950&nbsp;&nbsp;&nbsp;&nbsp;&amp;asn_DEF_Rectangle,&nbsp;&amp;mf-&gt;rect,&nbsp;<B>1</B>&nbsp;<B>/*&nbsp;!free&nbsp;*/</B>);
951&nbsp;&nbsp;&nbsp;&nbsp;
952&nbsp;&nbsp;
953<B>/*&nbsp;2.&nbsp;Rectangle_t&nbsp;is&nbsp;a&nbsp;stand-alone&nbsp;pointer&nbsp;*/</B>
Lev Walkineb97a702004-08-07 06:03:15 +0000954Rectangle_t&nbsp;*rect&nbsp;=&nbsp;<B>...</B>;
955/*
956&nbsp;*&nbsp;Freeing&nbsp;the&nbsp;Rectangle_t
957&nbsp;*&nbsp;and&nbsp;freeing&nbsp;the&nbsp;rect&nbsp;pointer
958&nbsp;*/
Lev Walkinc500b3e2004-09-29 13:37:15 +0000959asn_DEF_Rectangle-&gt;free_struct(
Lev Walkin7f70fe52005-02-22 07:28:26 +0000960&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 +0000961</PRE>
Lev Walkin26587ab2004-08-23 15:12:04 +0000962</BLOCKQUOTE>
Lev Walkineb97a702004-08-07 06:03:15 +0000963It is safe to invoke the <I>free_struct</I> function with the target
964structure pointer set to 0 (NULL), the function will do nothing.
965
966<P>
Lev Walkin028a28b2005-01-17 11:09:32 +0000967
Lev Walkin7f70fe52005-02-22 07:28:26 +0000968<H1><A NAME="SECTION02300000000000000000"></A><A NAME="cha:Step-by-step-examples"></A><BR>
Lev Walkindee61782005-02-02 09:53:46 +0000969Step by step examples
Lev Walkin028a28b2005-01-17 11:09:32 +0000970</H1>
971
972<P>
Lev Walkine94e3e52005-01-31 10:18:53 +0000973
Lev Walkin7f70fe52005-02-22 07:28:26 +0000974<H1><A NAME="SECTION02310000000000000000">
Lev Walkine94e3e52005-01-31 10:18:53 +0000975A ''Rectangle'' Encoder</A>
976</H1>
977
978<P>
Lev Walkindee61782005-02-02 09:53:46 +0000979This example will help you to create a simple BER and XER encoder
Lev Walkine94e3e52005-01-31 10:18:53 +0000980of a ''Rectangle'' type used throughout this document.
981
982<P>
983
984<OL>
985<LI>Create a file named <B>rectangle.asn1</B> with the following contents:
986
987<P>
988
989<BLOCKQUOTE><PRE>
990RectangleModule1&nbsp;DEFINITIONS&nbsp;::=
991BEGIN
992&nbsp;
993Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
994&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,
995&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER
996}
997&nbsp;
998END
999</PRE>
1000</BLOCKQUOTE>
1001
1002</LI>
1003<LI>Compile it into the set of .c and .h files using asn1c compiler [<A
1004 HREF="asn1c-usage.html#ASN1C">ASN1C</A>]:
1005
1006<P>
1007
1008<BLOCKQUOTE><PRE>
1009<I>asn1c&nbsp;-fnative-types</I>&nbsp;<B>rectangle.asn1</B>
1010</PRE>
1011</BLOCKQUOTE>
1012
1013</LI>
1014<LI>Alternatively, use the Online ASN.1 compiler [<A
1015 HREF="asn1c-usage.html#AONL">AONL</A>] by uploading
1016the <B>rectangle.asn1</B> file into the Web form and unpacking the
1017produced archive on your computer.
1018</LI>
1019<LI>By this time, you should have gotten multiple files in the current
1020directory, including the <B>Rectangle.c</B> and <B>Rectangle.h</B>.
1021</LI>
1022<LI>Create a main() routine which creates the Rectangle_t structure in
1023memory and encodes it using BER and XER encoding rules. Let's name
1024the file <B>main.c</B>:
1025
1026<P>
1027
1028<BLOCKQUOTE><PRE>
1029<FONT SIZE="-1">#include&nbsp;&lt;stdio.h&gt;</FONT>
1030<FONT SIZE="-1">#include&nbsp;&lt;sys/types.h&gt;</FONT>
1031<FONT SIZE="-1">#include&nbsp;&lt;Rectangle.h&gt;&nbsp;&nbsp;&nbsp;/*&nbsp;Rectangle&nbsp;ASN.1&nbsp;type&nbsp;&nbsp;*/</FONT>
1032&nbsp;
1033<FONT SIZE="-1">/*</FONT>
1034&nbsp;<FONT SIZE="-1">*&nbsp;This&nbsp;is&nbsp;a&nbsp;custom&nbsp;function&nbsp;which&nbsp;writes&nbsp;the</FONT>
1035&nbsp;<FONT SIZE="-1">*&nbsp;encoded&nbsp;output&nbsp;into&nbsp;some&nbsp;FILE&nbsp;stream.</FONT>
1036&nbsp;<FONT SIZE="-1">*/</FONT>
1037<FONT SIZE="-1">static&nbsp;int</FONT>
1038<FONT SIZE="-1">write_out(const&nbsp;void&nbsp;*buffer,&nbsp;size_t&nbsp;size,&nbsp;void&nbsp;*app_key)&nbsp;{</FONT>
1039&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;FILE&nbsp;*out_fp&nbsp;=&nbsp;app_key;</FONT>
1040&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;size_t&nbsp;wrote;</FONT>
1041&nbsp;
1042&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;wrote&nbsp;=&nbsp;fwrite(buffer,&nbsp;1,&nbsp;size,&nbsp;out_fp);</FONT>
1043&nbsp;
1044&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;return&nbsp;(wrote&nbsp;==&nbsp;size)&nbsp;?&nbsp;0&nbsp;:&nbsp;-1;</FONT>
1045<FONT SIZE="-1">}</FONT>
1046&nbsp;
Lev Walkin6b0df9f2005-01-31 15:06:32 +00001047<FONT SIZE="-1">int&nbsp;main(int&nbsp;ac,&nbsp;char&nbsp;**av)&nbsp;{</FONT>
Lev Walkine94e3e52005-01-31 10:18:53 +00001048&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>
1049&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>
1050&nbsp;
1051&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Allocate&nbsp;the&nbsp;Rectangle_t&nbsp;*/</FONT>
Lev Walkindee61782005-02-02 09:53:46 +00001052&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 +00001053&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(!rectangle)&nbsp;{</FONT>
1054&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(''calloc()&nbsp;failed'');</FONT>
1055&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(71);&nbsp;/*&nbsp;better,&nbsp;EX_OSERR&nbsp;*/</FONT>
1056&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1057&nbsp;
1058&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Initialize&nbsp;the&nbsp;Rectangle&nbsp;members&nbsp;*/</FONT>
1059&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;rectangle-&gt;height&nbsp;=&nbsp;42;&nbsp;&nbsp;/*&nbsp;any&nbsp;random&nbsp;value&nbsp;*/</FONT>
1060&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;rectangle-&gt;width&nbsp;&nbsp;=&nbsp;23;&nbsp;&nbsp;/*&nbsp;any&nbsp;random&nbsp;value&nbsp;*/</FONT>
1061&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;</FONT>
1062&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>
1063&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(ac&nbsp;&lt;&nbsp;2)&nbsp;{</FONT>
1064&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''Specify&nbsp;filename&nbsp;for&nbsp;BER&nbsp;output&#92;n'');</FONT>
1065&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{</FONT>
1066&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 +00001067&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 +00001068&nbsp;
1069&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!fp)&nbsp;{</FONT>
1070&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(filename);</FONT>
1071&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(71);&nbsp;/*&nbsp;better,&nbsp;EX_OSERR&nbsp;*/</FONT>
1072&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</FONT>
1073&nbsp;<FONT SIZE="-1">&nbsp;</FONT>
1074&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>
1075&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 +00001076&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 +00001077&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose(fp);</FONT>
1078&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(ec.encoded&nbsp;==&nbsp;-1)&nbsp;{</FONT>
1079&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,</FONT>
1080&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>
1081&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>
1082&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(65);&nbsp;/*&nbsp;better,&nbsp;EX_DATAERR&nbsp;*/</FONT>
1083&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{</FONT>
1084&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>
1085&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename);</FONT>
1086&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</FONT>
1087&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1088&nbsp;
1089&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>
1090&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;xer_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rectangle);</FONT>
1091&nbsp;
1092&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;return&nbsp;0;&nbsp;/*&nbsp;Encoding&nbsp;finished&nbsp;successfully&nbsp;*/</FONT>
1093<FONT SIZE="-1">}</FONT>
1094</PRE>
1095</BLOCKQUOTE>
1096
1097</LI>
1098<LI>Compile all files together using C compiler (varies by platform):
1099
1100<P>
1101
1102<BLOCKQUOTE><PRE>
1103<I>cc&nbsp;-I.&nbsp;-o</I>&nbsp;<B><I>rencode</I></B>&nbsp;<I>*.c</I>
1104</PRE>
1105</BLOCKQUOTE>
1106
1107</LI>
1108<LI>Voila! You have just created the BER and XER encoder of a Rectangle
1109type, named <B>rencode</B>!
1110</LI>
1111</OL>
1112
1113
1114<P>
1115
Lev Walkin7f70fe52005-02-22 07:28:26 +00001116<H1><A NAME="SECTION02320000000000000000"></A><A NAME="sec:A-Rectangle-Decoder"></A><BR>
Lev Walkindee61782005-02-02 09:53:46 +00001117A ''Rectangle'' Decoder
Lev Walkine94e3e52005-01-31 10:18:53 +00001118</H1>
1119
1120<P>
Lev Walkindee61782005-02-02 09:53:46 +00001121This example will help you to create a simple BER decoder of a simple
Lev Walkin028a28b2005-01-17 11:09:32 +00001122''Rectangle'' type used throughout this document.
1123
1124<P>
1125
1126<OL>
1127<LI>Create a file named <B>rectangle.asn1</B> with the following contents:
1128
1129<P>
1130
1131<BLOCKQUOTE><PRE>
1132RectangleModule1&nbsp;DEFINITIONS&nbsp;::=
1133BEGIN
1134&nbsp;
1135Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
1136&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,
1137&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER
1138}
1139&nbsp;
1140END
1141</PRE>
1142</BLOCKQUOTE>
1143
1144</LI>
1145<LI>Compile it into the set of .c and .h files using asn1c compiler [<A
1146 HREF="asn1c-usage.html#ASN1C">ASN1C</A>]:
1147
1148<P>
1149
1150<BLOCKQUOTE><PRE>
1151<I>asn1c&nbsp;-fnative-types</I>&nbsp;<B>rectangle.asn1</B>
1152</PRE>
1153</BLOCKQUOTE>
1154
1155</LI>
1156<LI>Alternatively, use the Online ASN.1 compiler [<A
1157 HREF="asn1c-usage.html#AONL">AONL</A>] by uploading
1158the <B>rectangle.asn1</B> file into the Web form and unpacking the
1159produced archive on your computer.
1160</LI>
1161<LI>By this time, you should have gotten multiple files in the current
1162directory, including the <B>Rectangle.c</B> and <B>Rectangle.h</B>.
1163</LI>
1164<LI>Create a main() routine which takes the binary input file, decodes
1165it as it were a BER-encoded Rectangle type, and prints out the text
1166(XML) representation of the Rectangle type. Let's name the file <B>main.c</B>:
1167
1168<P>
1169
1170<BLOCKQUOTE><PRE>
1171<FONT SIZE="-1">#include&nbsp;&lt;stdio.h&gt;</FONT>
1172<FONT SIZE="-1">#include&nbsp;&lt;sys/types.h&gt;</FONT>
1173<FONT SIZE="-1">#include&nbsp;&lt;Rectangle.h&gt;&nbsp;&nbsp;&nbsp;/*&nbsp;Rectangle&nbsp;ASN.1&nbsp;type&nbsp;&nbsp;*/</FONT>
1174&nbsp;
1175<FONT SIZE="-1">int&nbsp;main(int&nbsp;ac,&nbsp;char&nbsp;**av)&nbsp;{</FONT>
1176&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>
1177&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;Rectangle_t&nbsp;*rectangle&nbsp;=&nbsp;0;&nbsp;/*&nbsp;Type&nbsp;to&nbsp;decode&nbsp;*/</FONT>
1178&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;asn_dec_rval_t&nbsp;rval;&nbsp;/*&nbsp;Decoder&nbsp;return&nbsp;value&nbsp;&nbsp;*/</FONT>
1179&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>
1180&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>
1181&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;char&nbsp;*filename;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Input&nbsp;file&nbsp;name&nbsp;*/</FONT>
1182&nbsp;
1183&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Require&nbsp;a&nbsp;single&nbsp;filename&nbsp;argument&nbsp;*/</FONT>
1184&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(ac&nbsp;!=&nbsp;2)&nbsp;{</FONT>
1185&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>
1186&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(64);&nbsp;/*&nbsp;better,&nbsp;EX_USAGE&nbsp;*/</FONT>
1187&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{</FONT>
1188&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename&nbsp;=&nbsp;av[1];</FONT>
1189&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1190&nbsp;
1191&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Open&nbsp;input&nbsp;file&nbsp;as&nbsp;read-only&nbsp;binary&nbsp;*/</FONT>
1192&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;fp&nbsp;=&nbsp;fopen(filename,&nbsp;''rb'');</FONT>
1193&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(!fp)&nbsp;{</FONT>
1194&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(filename);</FONT>
1195&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(66);&nbsp;/*&nbsp;better,&nbsp;EX_NOINPUT&nbsp;*/</FONT>
1196&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1197&nbsp;<FONT SIZE="-1">&nbsp;</FONT>
1198&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Read&nbsp;up&nbsp;to&nbsp;the&nbsp;buffer&nbsp;size&nbsp;*/</FONT>
1199&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;size&nbsp;=&nbsp;fread(buf,&nbsp;1,&nbsp;sizeof(buf),&nbsp;fp);</FONT>
1200&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;fclose(fp);</FONT>
1201&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(!size)&nbsp;{</FONT>
1202&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''%s:&nbsp;Empty&nbsp;or&nbsp;broken&#92;n'',&nbsp;filename);</FONT>
1203&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(65);&nbsp;/*&nbsp;better,&nbsp;EX_DATAERR&nbsp;*/</FONT>
1204&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1205&nbsp;
1206&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Decode&nbsp;the&nbsp;input&nbsp;buffer&nbsp;as&nbsp;Rectangle&nbsp;type&nbsp;*/</FONT>
1207&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;rval&nbsp;=&nbsp;ber_decode(0,&nbsp;&amp;asn_DEF_Rectangle,</FONT>
1208&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void&nbsp;**)&amp;rectangle,&nbsp;buf,&nbsp;size);</FONT>
1209&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(rval.code&nbsp;!=&nbsp;RC_OK)&nbsp;{</FONT>
1210&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,</FONT>
1211&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>
1212&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename,&nbsp;(long)rval.consumed);</FONT>
1213&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(65);&nbsp;/*&nbsp;better,&nbsp;EX_DATAERR&nbsp;*/</FONT>
1214&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;}</FONT>
1215&nbsp;
1216&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Print&nbsp;the&nbsp;decoded&nbsp;Rectangle&nbsp;type&nbsp;as&nbsp;XML&nbsp;*/</FONT>
1217&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;xer_fprint(stdout,&nbsp;&amp;asn_DEF_Rectangle,&nbsp;rectangle);</FONT>
1218&nbsp;
1219&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;return&nbsp;0;&nbsp;/*&nbsp;Decoding&nbsp;finished&nbsp;successfully&nbsp;*/</FONT>
1220<FONT SIZE="-1">}</FONT>
1221</PRE>
1222</BLOCKQUOTE>
1223
1224</LI>
1225<LI>Compile all files together using C compiler (varies by platform):
1226
1227<P>
1228
1229<BLOCKQUOTE><PRE>
1230<I>cc&nbsp;-I.&nbsp;-o</I>&nbsp;<B><I>rdecode</I></B>&nbsp;<I>*.c</I>
1231</PRE>
1232</BLOCKQUOTE>
1233
1234</LI>
Lev Walkine94e3e52005-01-31 10:18:53 +00001235<LI>Voila! You have just created the BER decoder of a Rectangle type,
1236named <B>rdecode</B>!
Lev Walkin028a28b2005-01-17 11:09:32 +00001237</LI>
Lev Walkindee61782005-02-02 09:53:46 +00001238</OL>
1239
1240<P>
1241
Lev Walkin7f70fe52005-02-22 07:28:26 +00001242<H1><A NAME="SECTION02400000000000000000">
Lev Walkindee61782005-02-02 09:53:46 +00001243Constraint validation examples</A>
1244</H1>
1245
1246<P>
1247This chapter shows how to define ASN.1 constraints and use the generated
1248validation code.
1249
1250<P>
1251
Lev Walkin7f70fe52005-02-22 07:28:26 +00001252<H1><A NAME="SECTION02410000000000000000">
Lev Walkindee61782005-02-02 09:53:46 +00001253Adding constraints into ''Rectangle'' type</A>
1254</H1>
1255
1256<P>
1257This example shows how to add basic constraints to the ASN.1 specification
1258and how to invoke the constraints validation code in your application.
1259
1260<P>
1261
1262<OL>
1263<LI>Create a file named <B>rectangle.asn1</B> with the following contents:
1264
1265<P>
1266
1267<BLOCKQUOTE><PRE>
1268RectangleModuleWithConstraints&nbsp;DEFINITIONS&nbsp;::=
1269BEGIN
1270&nbsp;
1271Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
1272&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER&nbsp;(0..100),&nbsp;--&nbsp;Value&nbsp;range&nbsp;constraint
1273&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER&nbsp;(0..MAX)&nbsp;&nbsp;--&nbsp;Makes&nbsp;width&nbsp;non-negative&nbsp;
1274}
1275&nbsp;
1276END
1277</PRE>
1278</BLOCKQUOTE>
1279
1280</LI>
1281<LI>Compile the file according to procedures shown in the previous chapter.
1282</LI>
1283<LI>Modify the Rectangle type processing routine (you can start with the
Lev Walkin1411d052005-02-02 11:06:38 +00001284main() routine shown in the Section <A HREF="#sec:A-Rectangle-Decoder">A Rectangle Decoder</A>)
Lev Walkindee61782005-02-02 09:53:46 +00001285by placing the following snippet of code <I>before</I> encoding and/or
Lev Walkin7f70fe52005-02-22 07:28:26 +00001286<I>after</I> decoding the Rectangle type<A NAME="tex2html8"
Lev Walkinf47d0682005-03-05 01:07:04 +00001287 HREF="#foot923"><SUP>4.1</SUP></A>:
Lev Walkindee61782005-02-02 09:53:46 +00001288
1289<P>
1290
1291<BLOCKQUOTE><PRE>
1292<FONT SIZE="-1">int&nbsp;ret;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Return&nbsp;value&nbsp;*/</FONT>
1293<FONT SIZE="-1">char&nbsp;errbuf[128];&nbsp;&nbsp;/*&nbsp;Buffer&nbsp;for&nbsp;error&nbsp;message&nbsp;*/</FONT>
1294<FONT SIZE="-1">size_t&nbsp;errlen&nbsp;=&nbsp;sizeof(errbuf);&nbsp;&nbsp;/*&nbsp;Size&nbsp;of&nbsp;the&nbsp;buffer&nbsp;*/</FONT>
1295&nbsp;<FONT SIZE="-1">&nbsp;</FONT>
1296<FONT SIZE="-1">/*&nbsp;...&nbsp;here&nbsp;may&nbsp;go&nbsp;Rectangle&nbsp;decoding&nbsp;code&nbsp;...&nbsp;*/</FONT>
1297&nbsp;
1298<FONT SIZE="-1">ret&nbsp;=&nbsp;asn_check_constraints(asn_DEF_Rectangle,</FONT>
1299&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rectangle,&nbsp;errbuf,&nbsp;&amp;errlen);</FONT>
1300<FONT SIZE="-1">/*&nbsp;assert(errlen&nbsp;&lt;&nbsp;sizeof(errbuf));&nbsp;//&nbsp;you&nbsp;may&nbsp;rely&nbsp;on&nbsp;that&nbsp;*/</FONT>
1301<FONT SIZE="-1">if(ret)&nbsp;{</FONT>
1302&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''Constraint&nbsp;validation&nbsp;failed:&nbsp;%s&#92;n'',</FONT>
1303&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>
1304&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);</FONT>
1305&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;exit(...);&nbsp;//&nbsp;Replace&nbsp;with&nbsp;appropriate&nbsp;action&nbsp;*/</FONT>
1306<FONT SIZE="-1">}</FONT>
1307&nbsp;
1308<FONT SIZE="-1">/*&nbsp;...&nbsp;here&nbsp;may&nbsp;go&nbsp;Rectangle&nbsp;encoding&nbsp;code&nbsp;...&nbsp;*/</FONT>
1309</PRE>
1310</BLOCKQUOTE>
1311
1312</LI>
1313<LI>Compile the resulting C code as shown in the previous chapters.
1314</LI>
Lev Walkin1411d052005-02-02 11:06:38 +00001315<LI>Try to test the constraints checking code by assigning integer value
1316101 to the <B>.height</B> member of the Rectangle structure, or
1317a negative value to the <B>.width</B> member. In either case, the
1318program should print ''Constraint validation failed'' message, followed
1319by the short explanation why validation did not succeed.
1320</LI>
Lev Walkindee61782005-02-02 09:53:46 +00001321<LI>Done.
1322</LI>
Lev Walkin7f70fe52005-02-22 07:28:26 +00001323</OL>
1324
1325<P>
1326
1327<H1><A NAME="SECTION03000000000000000000"></A><A NAME="par:ASN.1-Basics"></A><BR>
1328ASN.1 Basics
1329</H1>
1330
1331<P>
1332
1333<H1><A NAME="SECTION03100000000000000000"></A><A NAME="cha:Abstract-Syntax-Notation:"></A><BR>
1334Abstract Syntax Notation: ASN.1
1335</H1>
1336
1337<P>
1338<I>This chapter defines some basic ASN.1 concepts and describes
1339several most widely used types. It is by no means an authoritative
1340or complete reference. For more complete ASN.1 description, please
1341refer to Olivier Dubuisson's book [<A
1342 HREF="asn1c-usage.html#Dub00">Dub00</A>] or the ASN.1 body
1343of standards itself [<A
1344 HREF="asn1c-usage.html#ITU-T_ASN.1">ITU-T/ASN.1</A>].</I>
1345
1346<P>
1347The Abstract Syntax Notation One is used to formally describe the
1348semantics of data transmitted across the network. Two communicating
1349parties may have different formats of their native data types (i.e.
1350number of bits in the integer type), thus it is important to have
1351a way to describe the data in a manner which is independent from the
1352particular machine's representation. The ASN.1 specifications are
1353used to achieve the following:
1354
1355<P>
1356
1357<UL>
1358<LI>The specification expressed in the ASN.1 notation is a formal and
1359precise way to communicate the data semantics to human readers;
1360</LI>
1361<LI>The ASN.1 specifications may be used as input for automatic compilers
1362which produce the code for some target language (C, C++, Java, etc)
1363to encode and decode the data according to some encoding rules (which
1364are also defined by the ASN.1 standard).
1365</LI>
1366</UL>
1367Consider the following example:
1368
1369<P>
1370
1371<BLOCKQUOTE><PRE>
1372Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
1373&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,
1374&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER
1375}
1376</PRE>
1377</BLOCKQUOTE>
1378This ASN.1 specification describes a constructed type, <I>Rectangle</I>,
1379containing two integer fields. This specification may tell the reader
1380that there exists this kind of data structure and that some entity
1381may be prepared to send or receive it. The question on <I>how</I>
1382that entity is going to send or receive the <I>encoded data</I> is
1383outside the scope of ASN.1. For example, this data structure may be
1384encoded according to some encoding rules and sent to the destination
1385using the TCP protocol. The ASN.1 specifies several ways of encoding
1386(or ''serializing'', or ''marshaling'') the data: BER, PER, XER
1387and others, including CER and DER derivatives from BER.
1388
1389<P>
1390The complete specification must be wrapped in a module, which looks
1391like this:
1392
1393<P>
1394
1395<BLOCKQUOTE><PRE>
1396RectangleModule1
1397&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;iso&nbsp;org(3)&nbsp;dod(6)&nbsp;internet(1)&nbsp;private(4)
1398&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enterprise(1)&nbsp;spelio(9363)&nbsp;software(1)
1399&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;asn1c(5)&nbsp;docs(2)&nbsp;rectangle(1)&nbsp;1&nbsp;}&nbsp;
1400&nbsp;&nbsp;&nbsp;&nbsp;DEFINITIONS&nbsp;AUTOMATIC&nbsp;TAGS&nbsp;::=
1401BEGIN
1402&nbsp;
1403--&nbsp;This&nbsp;is&nbsp;a&nbsp;comment&nbsp;which&nbsp;describes&nbsp;nothing.
1404Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
1405&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;Height&nbsp;of&nbsp;the&nbsp;rectangle
1406&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
1407}
1408&nbsp;
1409END
1410</PRE>
1411</BLOCKQUOTE>
1412The module header consists of module name (RectangleModule1), the
1413module object identifier ({...}), a keyword ''DEFINITIONS'', a
1414set of module flags (AUTOMATIC TAGS) and ''::= BEGIN''. The module
1415ends with an ''END'' statement.
1416
1417<P>
1418
1419<H1><A NAME="SECTION03110000000000000000">
1420Some of the ASN.1 Basic Types</A>
1421</H1>
1422
1423<P>
1424
1425<H2><A NAME="SECTION03111000000000000000">
1426The BOOLEAN type</A>
1427</H2>
1428
1429<P>
1430The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF
1431or a similar kind of two-way choice.
1432
1433<P>
1434
1435<H2><A NAME="SECTION03112000000000000000">
1436The INTEGER type</A>
1437</H2>
1438
1439<P>
1440The INTEGER type is a signed natural number type without any restrictions
1441on its size. If the automatic checking on INTEGER value bounds are
1442necessary, the subtype constraints must be used.
1443
1444<P>
1445
1446<BLOCKQUOTE><PRE>
1447SimpleInteger&nbsp;::=&nbsp;INTEGER
1448&nbsp;
1449--&nbsp;An&nbsp;integer&nbsp;with&nbsp;a&nbsp;very&nbsp;limited&nbsp;range
1450SmallPositiveInt&nbsp;::=&nbsp;INTEGER&nbsp;(0..127)
1451&nbsp;
1452--&nbsp;Integer,&nbsp;negative
1453NegativeInt&nbsp;::=&nbsp;INTEGER&nbsp;(MIN..0)
1454</PRE>
1455</BLOCKQUOTE>
1456
1457<P>
1458
1459<H2><A NAME="SECTION03113000000000000000">
1460The ENUMERATED type</A>
1461</H2>
1462
1463<P>
1464The ENUMERATED type is semantically equivalent to the INTEGER type
1465with some integer values explicitly named.
1466
1467<P>
1468
1469<BLOCKQUOTE><PRE>
1470FruitId&nbsp;::=&nbsp;ENUMERATED&nbsp;{&nbsp;apple(1),&nbsp;orange(2)&nbsp;}
1471&nbsp;
1472--&nbsp;The&nbsp;numbers&nbsp;in&nbsp;braces&nbsp;are&nbsp;optional,
1473--&nbsp;the&nbsp;enumeration&nbsp;can&nbsp;be&nbsp;performed
1474--&nbsp;automatically&nbsp;by&nbsp;the&nbsp;compiler
1475ComputerOSType&nbsp;::=&nbsp;ENUMERATED&nbsp;{
1476&nbsp;&nbsp;&nbsp;&nbsp;FreeBSD,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;acquires&nbsp;value&nbsp;0
1477&nbsp;&nbsp;&nbsp;&nbsp;Windows,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;acquires&nbsp;value&nbsp;1
1478&nbsp;&nbsp;&nbsp;&nbsp;Solaris(5),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;remains&nbsp;5
1479&nbsp;&nbsp;&nbsp;&nbsp;Linux,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;becomes&nbsp;6
1480&nbsp;&nbsp;&nbsp;&nbsp;MacOS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;becomes&nbsp;7
1481}
1482</PRE>
1483</BLOCKQUOTE>
1484
1485<P>
1486
1487<H2><A NAME="SECTION03114000000000000000">
1488The OCTET STRING type</A>
1489</H2>
1490
1491<P>
1492This type models the sequence of 8-bit bytes. This may be used to
1493transmit some opaque data or data serialized by other types of encoders
1494(i.e. video file, photo picture, etc).
1495
1496<P>
1497
1498<H2><A NAME="SECTION03115000000000000000">
1499The OBJECT IDENTIFIER type</A>
1500</H2>
1501
1502<P>
1503The OBJECT IDENTIFIER is used to represent the unique identifier of
1504any object, starting from the very root of the registration tree.
1505If your organization needs to uniquely identify something (a router,
1506a room, a person, a standard, or whatever), you are encouraged to
1507get your own identification subtree at <A HREF=http://www.iana.org/protocols/forms.htm>http://www.iana.org/protocols/forms.htm</A>.
1508
1509<P>
1510For example, the very first ASN.1 module in this Chapter (RectangleModule1)
1511has the following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
1512
1513<P>
1514
1515<BLOCKQUOTE><PRE>
1516ExampleOID&nbsp;::=&nbsp;OBJECT&nbsp;IDENTIFIER
1517&nbsp;
1518rectangleModule1-oid&nbsp;ExampleOID
1519&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;}
1520&nbsp;
1521--&nbsp;An&nbsp;identifier&nbsp;of&nbsp;the&nbsp;Internet.
1522internet-id&nbsp;OBJECT&nbsp;IDENTIFIER
1523&nbsp;&nbsp;::=&nbsp;{&nbsp;iso(1)&nbsp;identified-organization(3)
1524&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dod(6)&nbsp;internet(1)&nbsp;}
1525</PRE>
1526</BLOCKQUOTE>
1527As you see, names are optional.
1528
1529<P>
1530
1531<H2><A NAME="SECTION03116000000000000000">
1532The RELATIVE-OID type</A>
1533</H2>
1534
1535<P>
1536The RELATIVE-OID type has the semantics of a subtree of an OBJECT
1537IDENTIFIER. There may be no need to repeat the whole sequence of numbers
1538from the root of the registration tree where the only thing of interest
1539is some of the tree's subsequence.
1540
1541<P>
1542
1543<BLOCKQUOTE><PRE>
1544this-document&nbsp;RELATIVE-OID&nbsp;::=&nbsp;{&nbsp;docs(2)&nbsp;usage(1)&nbsp;}
1545&nbsp;
1546this-example&nbsp;RELATIVE-OID&nbsp;::=&nbsp;{
1547&nbsp;&nbsp;&nbsp;&nbsp;this-document&nbsp;assorted-examples(0)&nbsp;this-example(1)&nbsp;}
1548</PRE>
1549</BLOCKQUOTE>
1550
1551<P>
1552
1553<H1><A NAME="SECTION03120000000000000000">
1554Some of the ASN.1 String Types</A>
1555</H1>
1556
1557<P>
1558
1559<H2><A NAME="SECTION03121000000000000000">
1560The IA5String type</A>
1561</H2>
1562
1563<P>
1564This is essentially the ASCII, with 128 character codes available
1565(7 lower bits of an 8-bit byte).
1566
1567<P>
1568
1569<H2><A NAME="SECTION03122000000000000000">
1570The UTF8String type</A>
1571</H2>
1572
1573<P>
1574This is the character string which encodes the full Unicode range
1575(4 bytes) using multibyte character sequences.
1576
1577<P>
1578
1579<H2><A NAME="SECTION03123000000000000000">
1580The NumericString type</A>
1581</H2>
1582
1583<P>
1584This type represents the character string with the alphabet consisting
1585of numbers (''0'' to ''9'') and a space.
1586
1587<P>
1588
1589<H2><A NAME="SECTION03124000000000000000">
1590The PrintableString type</A>
1591</H2>
1592
1593<P>
1594The character string with the following alphabet: space, ''<B>'</B>''
1595(single quote), ''<B>(</B>'', ''<B>)</B>'', ''<B>+</B>'',
1596''<B>,</B>'' (comma), ''<B>-</B>'', ''<B>.</B>'', ''<B>/</B>'',
1597digits (''0'' to ''9''), ''<B>:</B>'', ''<B>=</B>'', ''<B>?</B>'',
1598upper-case and lower-case letters (''A'' to ''Z'' and ''a''
1599to ''z'').
1600
1601<P>
1602
1603<H2><A NAME="SECTION03125000000000000000">
1604The VisibleString type</A>
1605</H2>
1606
1607<P>
1608The character string with the alphabet which is more or less a subset
1609of ASCII between the space and the ''<B>~</B>''
1610symbol (tilde).
1611
1612<P>
1613Alternatively, the alphabet may be described as the PrintableString
1614alphabet presented earlier, plus the following characters: ''<B>!</B>'',
1615''<B>''</B>'', ''<B>#</B>'', ''<B>$</B>'', ''<B>%</B>'',
1616''<B>&amp;</B>'', ''<B>*</B>'', ''<B>;</B>'', ''<B>&lt;</B>'',
1617''<B>&gt;</B>'', ''<B>[</B>'', ''<B>&#92;</B>'',
1618''<B>]</B>'', ''<B>&#94;</B>'', ''<B>_</B>'',
1619''<B>`</B>'' (single left quote), ''<B>{</B>'', ''<B>|</B>'',
1620''<B>}</B>'', ''<B>~</B>''.
1621
1622<P>
1623
1624<H1><A NAME="SECTION03130000000000000000">
1625ASN.1 Constructed Types</A>
1626</H1>
1627
1628<P>
1629
1630<H2><A NAME="SECTION03131000000000000000">
1631The SEQUENCE type</A>
1632</H2>
1633
1634<P>
1635This is an ordered collection of other simple or constructed types.
1636The SEQUENCE constructed type resembles the C ''struct'' statement.
1637
1638<P>
1639
1640<BLOCKQUOTE><PRE>
1641Address&nbsp;::=&nbsp;SEQUENCE&nbsp;{
1642&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;The&nbsp;apartment&nbsp;number&nbsp;may&nbsp;be&nbsp;omitted
1643&nbsp;&nbsp;&nbsp;&nbsp;apartmentNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NumericString&nbsp;OPTIONAL,
1644&nbsp;&nbsp;&nbsp;&nbsp;streetName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
1645&nbsp;&nbsp;&nbsp;&nbsp;cityName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
1646&nbsp;&nbsp;&nbsp;&nbsp;stateName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintableString,
1647&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;This&nbsp;one&nbsp;may&nbsp;be&nbsp;omitted&nbsp;too
1648&nbsp;&nbsp;&nbsp;&nbsp;zipNo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NumericString&nbsp;OPTIONAL
1649}
1650</PRE>
1651</BLOCKQUOTE>
1652
1653<P>
1654
1655<H2><A NAME="SECTION03132000000000000000">
1656The SET type</A>
1657</H2>
1658
1659<P>
1660This is a collection of other simple or constructed types. Ordering
1661is not important. The data may arrive in the order which is different
1662from the order of specification. Data is encoded in the order not
1663necessarily corresponding to the order of specification.
1664
1665<P>
1666
1667<H2><A NAME="SECTION03133000000000000000">
1668The CHOICE type</A>
1669</H2>
1670
1671<P>
1672This type is just a choice between the subtypes specified in it. The
1673CHOICE type contains at most one of the subtypes specified, and it
1674is always implicitly known which choice is being decoded or encoded.
1675This one resembles the C ''union'' statement.
1676
1677<P>
1678The following type defines a response code, which may be either an
1679integer code or a boolean ''true''/''false'' code.
1680
1681<P>
1682
1683<BLOCKQUOTE><PRE>
1684ResponseCode&nbsp;::=&nbsp;CHOICE&nbsp;{
1685&nbsp;&nbsp;&nbsp;&nbsp;intCode&nbsp;&nbsp;&nbsp;&nbsp;INTEGER,
1686&nbsp;&nbsp;&nbsp;&nbsp;boolCode&nbsp;&nbsp;&nbsp;BOOLEAN
1687}
1688</PRE>
1689</BLOCKQUOTE>
1690
1691<P>
1692
1693<H2><A NAME="SECTION03134000000000000000">
1694The SEQUENCE OF type</A>
1695</H2>
1696
1697<P>
1698This one is the list (array) of simple or constructed types:
1699
1700<P>
1701
1702<BLOCKQUOTE><PRE>
1703--&nbsp;Example&nbsp;1
1704ManyIntegers&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;INTEGER
1705&nbsp;
1706--&nbsp;Example&nbsp;2
1707ManyRectangles&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;Rectangle
1708&nbsp;
1709--&nbsp;More&nbsp;complex&nbsp;example:
1710--&nbsp;an&nbsp;array&nbsp;of&nbsp;structures&nbsp;defined&nbsp;in&nbsp;place.
1711ManyCircles&nbsp;::=&nbsp;SEQUENCE&nbsp;OF&nbsp;SEQUENCE&nbsp;{
1712&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
1713&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;}
1714</PRE>
1715</BLOCKQUOTE>
1716
1717<P>
1718
1719<H2><A NAME="SECTION03135000000000000000">
1720The SET OF type</A>
1721</H2>
1722
1723<P>
1724The SET OF type models the bag of structures. It resembles the SEQUENCE
1725OF type, but the order is not important: i.e. the elements may arrive
1726in the order which is not necessarily the same as the in-memory order
1727on the remote machines.
1728
1729<P>
1730
1731<BLOCKQUOTE><PRE>
1732--&nbsp;A&nbsp;set&nbsp;of&nbsp;structures&nbsp;defined&nbsp;elsewhere
1733SetOfApples&nbsp;::&nbsp;SET&nbsp;OF&nbsp;Apple
1734&nbsp;
1735--&nbsp;Set&nbsp;of&nbsp;integers&nbsp;encoding&nbsp;the&nbsp;kind&nbsp;of&nbsp;a&nbsp;fruit
1736FruitBag&nbsp;::=&nbsp;SET&nbsp;OF&nbsp;ENUMERATED&nbsp;{&nbsp;apple,&nbsp;orange&nbsp;}
1737</PRE>
1738</BLOCKQUOTE>
1739
1740<H2><A NAME="SECTION04000000000000000000">
Lev Walkineb97a702004-08-07 06:03:15 +00001741Bibliography</A>
Lev Walkin26587ab2004-08-23 15:12:04 +00001742</H2><DL COMPACT><DD><P></P><DT><A NAME="ASN1C">ASN1C</A>
Lev Walkin1411d052005-02-02 11:06:38 +00001743<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 +00001744<P></P><DT><A NAME="AONL">AONL</A>
Lev Walkin1411d052005-02-02 11:06:38 +00001745<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 +00001746<P></P><DT><A NAME="Dub00">Dub00</A>
Lev Walkin028a28b2005-01-17 11:09:32 +00001747<DD>Olivier Dubuisson -- <I>ASN.1 Communication between heterogeneous
1748systems</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 +00001749ISBN:0-12-6333361-0.
1750<P></P><DT><A NAME="ITU-T_ASN.1">ITU-T/ASN.1</A>
Lev Walkindee61782005-02-02 09:53:46 +00001751<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>
1752</DL>
Lev Walkineb97a702004-08-07 06:03:15 +00001753
1754<P>
1755<BR><HR><H4>Footnotes</H4>
1756<DL>
Lev Walkinf47d0682005-03-05 01:07:04 +00001757<DT><A NAME="foot843">... given</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001758 HREF="asn1c-usage.html#tex2html1"><SUP>1.1</SUP></A></DT>
1759<DD>Please look into Part par:ASN.1-Basics for a quick reference
1760on how to understand the ASN.1 notation.
Lev Walkineb97a702004-08-07 06:03:15 +00001761
1762</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001763<DT><A NAME="foot844">... type</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001764 HREF="asn1c-usage.html#tex2html2"><SUP>1.2</SUP></A></DT>
Lev Walkin6faa68e2004-09-17 08:35:02 +00001765<DD><I>-fnative-types</I> compiler option is used to produce basic C <I>int</I>
1766types instead of infinite width INTEGER_t structures. See <A HREF=#Table1>Table 1</A>.
Lev Walkineb97a702004-08-07 06:03:15 +00001767
1768</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001769<DT><A NAME="foot845">...asn1c</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001770 HREF="asn1c-usage.html#tex2html3"><SUP>1.3</SUP></A></DT>
Lev Walkin26587ab2004-08-23 15:12:04 +00001771<DD>The 1 symbol in asn<B>1</B>c is a digit, not an ''ell'' letter.
1772
1773</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001774<DT><A NAME="foot846">... module</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001775 HREF="asn1c-usage.html#tex2html4"><SUP>1.4</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001776<DD>This is probably <B>not</B> what you want to try out right now -
Lev Walkin7f70fe52005-02-22 07:28:26 +00001777read through the rest of this chapter and check the <A HREF=#Table1>Table 1</A>
1778to find out about <B>-P</B> and <B>-R</B> options.
Lev Walkineb97a702004-08-07 06:03:15 +00001779
1780</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001781<DT><A NAME="foot181">...restartable</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001782 HREF="asn1c-usage.html#tex2html6"><SUP>2.1</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001783<DD>Restartable means that if the decoder encounters the end of the buffer,
1784it will fail, but may later be invoked again with the rest of the
1785buffer to continue decoding.
1786
1787</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001788<DT><A NAME="foot250">... encoding</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001789 HREF="asn1c-usage.html#tex2html7"><SUP>2.2</SUP></A></DT>
Lev Walkineb97a702004-08-07 06:03:15 +00001790<DD>It is actually faster too: the encoder might skip over some computations
1791which aren't important for the size determination.
1792
1793</DD>
Lev Walkinf47d0682005-03-05 01:07:04 +00001794<DT><A NAME="foot923">... type</A><A
Lev Walkin7f70fe52005-02-22 07:28:26 +00001795 HREF="asn1c-usage.html#tex2html8"><SUP>4.1</SUP></A></DT>
Lev Walkindee61782005-02-02 09:53:46 +00001796<DD>Placing the constraint checking code <I>before</I> encoding helps
1797to make sure you know the data is correct and within constraints before
1798sharing the data with anyone else.
1799
1800<P>
1801Placing the constraint checking code <I>after</I> decoding, but before
1802any further action depending on the decoded data, helps to make sure
1803the application got the valid contents before making use of it.
1804
1805</DD>
Lev Walkineb97a702004-08-07 06:03:15 +00001806</DL><BR><HR>
1807<ADDRESS>
1808Lev Walkin
Lev Walkinf47d0682005-03-05 01:07:04 +000018092005-03-04
Lev Walkineb97a702004-08-07 06:03:15 +00001810</ADDRESS>
1811</BODY>
1812</HTML>