blob: 205b9dc878a7ed5c151b6a38d602ad0e7d58cf53 [file] [log] [blame]
vlme18adea2004-06-03 03:49:45 +00001#LyX 1.3 created this file. For more info see http://www.lyx.org/
2\lyxformat 221
3\textclass book
vlmfcec8e52004-08-23 15:12:04 +00004\begin_preamble
vlmfcec8e52004-08-23 15:12:04 +00005\usepackage{extramarks}
6\lhead{\firstxmark}
7\rfoot{\lastxmark}
vlmc3bfd812004-09-30 22:26:19 +00008\usepackage{color}
9\definecolor{gray40}{gray}{.4}
10\definecolor{urlblue}{rgb}{0,0,.6}
11\usepackage[colorlinks=true,
12linkcolor={gray40},
13urlcolor={urlblue},
14pdfauthor={Lev Walkin},
vlmc7737f62005-02-02 09:53:46 +000015pdftitle={Using the Open Source ASN.1 Compiler},
vlmc3bfd812004-09-30 22:26:19 +000016pdfkeywords={ASN.1,asn1c,compiler}
17]{hyperref}
18%\fancyhf{}
19%\fancyhead[LE,RO]{\thepage}
20%\fancyhead[LO]{\rightmark}
21%\fancyhead[RE]{\leftmark}
22%\fancyfoot[R]{\lastxmark}
vlmfcec8e52004-08-23 15:12:04 +000023\end_preamble
vlme18adea2004-06-03 03:49:45 +000024\language english
25\inputencoding latin1
vlmfcec8e52004-08-23 15:12:04 +000026\fontscheme times
vlme18adea2004-06-03 03:49:45 +000027\graphics default
28\paperfontsize default
29\spacing single
30\papersize Default
31\paperpackage a4
32\use_geometry 0
33\use_amsmath 0
34\use_natbib 0
35\use_numerical_citations 0
36\paperorientation portrait
vlmc3bfd812004-09-30 22:26:19 +000037\secnumdepth 2
38\tocdepth 2
vlme18adea2004-06-03 03:49:45 +000039\paragraph_separation indent
40\defskip medskip
41\quotes_language swedish
42\quotes_times 2
43\papercolumns 1
vlmfcec8e52004-08-23 15:12:04 +000044\papersides 2
45\paperpagestyle fancy
vlme18adea2004-06-03 03:49:45 +000046
47\layout Title
48
vlmc7737f62005-02-02 09:53:46 +000049Using the Open Source ASN.1 Compiler
vlme18adea2004-06-03 03:49:45 +000050\layout Author
51
52Lev Walkin <
vlmc3bfd812004-09-30 22:26:19 +000053\begin_inset ERT
54status Collapsed
vlme18adea2004-06-03 03:49:45 +000055
vlmc3bfd812004-09-30 22:26:19 +000056\layout Standard
57
58\backslash
59href{mailto:vlm@lionet.info?Subject=asn1c}{vlm@lionet.info}
vlme18adea2004-06-03 03:49:45 +000060\end_inset
61
62>
63\layout Standard
64
65
vlmfcec8e52004-08-23 15:12:04 +000066\begin_inset ERT
67status Open
68
69\layout Standard
70
71\backslash
vlm485123c2005-02-22 07:28:26 +000072lhead{This document describes
vlmc3bfd812004-09-30 22:26:19 +000073\backslash
vlm11bea882005-10-06 10:09:34 +000074href{http://lionet.info/asn1c}{asn1c-0.9.19}}
vlmc3bfd812004-09-30 22:26:19 +000075\layout Standard
76
77\backslash
78rhead{$Revision$}
vlmfcec8e52004-08-23 15:12:04 +000079\end_inset
80
81
82\layout Standard
83
84
vlme18adea2004-06-03 03:49:45 +000085\begin_inset LatexCommand \tableofcontents{}
86
87\end_inset
88
89
vlmfcec8e52004-08-23 15:12:04 +000090\layout Standard
91
92
93\begin_inset ERT
vlmc3bfd812004-09-30 22:26:19 +000094status Open
vlmfcec8e52004-08-23 15:12:04 +000095
96\layout Standard
97
98\backslash
99pagestyle{headings}
100\end_inset
101
102
vlmcaba97b2004-08-07 05:00:07 +0000103\layout Part
104
vlm856978f2005-02-21 14:16:10 +0000105Using the ASN.1 Compiler
vlme18adea2004-06-03 03:49:45 +0000106\layout Chapter
107
vlm856978f2005-02-21 14:16:10 +0000108Introduction to the ASN.1 Compiler
vlme18adea2004-06-03 03:49:45 +0000109\layout Standard
110
vlm856978f2005-02-21 14:16:10 +0000111The purpose of the ASN.1 compiler, of which this document is part, is to
vlm485123c2005-02-22 07:28:26 +0000112 convert the specifications in ASN.1 notation into some other language.
vlm856978f2005-02-21 14:16:10 +0000113 At this moment, only C and C++ target languages are supported, the latter
vlm485123c2005-02-22 07:28:26 +0000114 is in upward compatibility mode.
vlme18adea2004-06-03 03:49:45 +0000115\layout Standard
116
vlm856978f2005-02-21 14:16:10 +0000117The compiler reads the specification and emits a series of target language
118 structures (C's structs, unions, enums) describing the corresponding ASN.1
119 types.
vlm485123c2005-02-22 07:28:26 +0000120 The compiler also creates the code which allows automatic serialization
121 and deserialization of these structures using several standardized encoding
122 rules (BER, DER, XER).
vlme18adea2004-06-03 03:49:45 +0000123\layout Standard
124
vlm856978f2005-02-21 14:16:10 +0000125For example, suppose the following ASN.1 module is given
126\begin_inset Foot
127collapsed true
vlme18adea2004-06-03 03:49:45 +0000128
vlme18adea2004-06-03 03:49:45 +0000129\layout Standard
130
vlm856978f2005-02-21 14:16:10 +0000131Please look into Part
132\begin_inset LatexCommand \vref{par:ASN.1-Basics}
133
vlme18adea2004-06-03 03:49:45 +0000134\end_inset
135
vlm856978f2005-02-21 14:16:10 +0000136 for a quick reference on how to understand the ASN.1 notation.
vlme18adea2004-06-03 03:49:45 +0000137\end_inset
138
vlm856978f2005-02-21 14:16:10 +0000139:
vlme18adea2004-06-03 03:49:45 +0000140\layout LyX-Code
141
vlm856978f2005-02-21 14:16:10 +0000142RectangleTest DEFINITIONS ::=
vlme18adea2004-06-03 03:49:45 +0000143\layout LyX-Code
144
145BEGIN
146\layout LyX-Code
147
148
149\layout LyX-Code
150
vlme18adea2004-06-03 03:49:45 +0000151Rectangle ::= SEQUENCE {
152\layout LyX-Code
153
154 height INTEGER, -- Height of the rectangle
155\layout LyX-Code
156
vlmfcec8e52004-08-23 15:12:04 +0000157 width INTEGER -- Width of the rectangle
vlme18adea2004-06-03 03:49:45 +0000158\layout LyX-Code
159
160}
161\layout LyX-Code
162
163
164\layout LyX-Code
165
166END
167\layout Standard
168
vlm856978f2005-02-21 14:16:10 +0000169The compiler would read this ASN.1 definition and produce the following C
170 type
vlme18adea2004-06-03 03:49:45 +0000171\begin_inset Foot
vlm4cc45722005-07-24 08:28:39 +0000172collapsed false
vlme18adea2004-06-03 03:49:45 +0000173
174\layout Standard
175
176
177\emph on
vlm03af0f02004-09-14 12:48:17 +0000178-fnative-types
vlme18adea2004-06-03 03:49:45 +0000179\emph default
180 compiler option is used to produce basic C
181\emph on
182int
183\emph default
vlmfcec8e52004-08-23 15:12:04 +0000184 types instead of infinite width INTEGER_t structures.
vlm4cc45722005-07-24 08:28:39 +0000185 See Section
186\begin_inset LatexCommand \vref{sec:Command-line-options}
vlmfcec8e52004-08-23 15:12:04 +0000187
188\end_inset
189
190.
vlme18adea2004-06-03 03:49:45 +0000191\end_inset
192
193:
194\layout LyX-Code
195
196typedef struct Rectangle_s {
197\layout LyX-Code
198
199 int height;
200\layout LyX-Code
201
202 int width;
203\layout LyX-Code
204
205} Rectangle_t;
206\layout Standard
207
vlm856978f2005-02-21 14:16:10 +0000208It would also create the code for converting this structure into platform-indepe
209ndent wire representation (a serializer API) and the decoder of such wire
210 representation back into local, machine-specific type (a deserializer API).
211\layout Section
vlme18adea2004-06-03 03:49:45 +0000212
vlm856978f2005-02-21 14:16:10 +0000213Quick start with asn1c
vlme18adea2004-06-03 03:49:45 +0000214\layout Standard
215
vlmfcec8e52004-08-23 15:12:04 +0000216After building and installing the compiler, the
217\emph on
218asn1c
219\begin_inset Foot
220collapsed false
221
222\layout Standard
223
224The 1 symbol in asn
225\series bold
2261
227\series default
228c is a digit, not an
229\begin_inset Quotes sld
230\end_inset
231
232ell
233\begin_inset Quotes srd
234\end_inset
235
236 letter.
237\end_inset
238
239
240\emph default
vlm856978f2005-02-21 14:16:10 +0000241 command may be used to compile the ASN.1 module
vlme18adea2004-06-03 03:49:45 +0000242\begin_inset Foot
vlm4cc45722005-07-24 08:28:39 +0000243collapsed false
vlme18adea2004-06-03 03:49:45 +0000244
245\layout Standard
246
247This is probably
248\series bold
249not
250\series default
251 what you want to try out right now -- read through the rest of this chapter
vlm4cc45722005-07-24 08:28:39 +0000252 and check the Section
253\begin_inset LatexCommand \vref{sec:Command-line-options}
vlm856978f2005-02-21 14:16:10 +0000254
255\end_inset
256
vlmfcec8e52004-08-23 15:12:04 +0000257 to find out about
258\series bold
259-P
260\series default
261 and
262\series bold
263-R
264\series default
265 options.
vlme18adea2004-06-03 03:49:45 +0000266\end_inset
267
268:
269\layout LyX-Code
270
271asn1c
272\emph on
vlm856978f2005-02-21 14:16:10 +0000273<module.asn1>
vlme18adea2004-06-03 03:49:45 +0000274\layout Standard
275
vlm856978f2005-02-21 14:16:10 +0000276If several ASN.1 modules contain interdependencies, all of the files must
vlmfcec8e52004-08-23 15:12:04 +0000277 be specified altogether:
vlme18adea2004-06-03 03:49:45 +0000278\layout LyX-Code
279
280asn1c
281\emph on
vlm856978f2005-02-21 14:16:10 +0000282<module1.asn1> <module2.asn1> ...
vlme18adea2004-06-03 03:49:45 +0000283\layout Standard
284
vlmfcec8e52004-08-23 15:12:04 +0000285The compiler
286\series bold
287-E
288\series default
289 and
290\series bold
291-EF
292\series default
293 options are used for testing the parser and the semantic fixer, respectively.
294 These options will instruct the compiler to dump out the parsed (and fixed,
295 if
296\series bold
297-F
298\series default
299 is involved) ASN.1 specification as it was "understood" by the compiler.
300 It might be useful to check whether a particular syntactic construction
vlme18adea2004-06-03 03:49:45 +0000301 is properly supported by the compiler.
302\layout LyX-Code
303
vlmfcec8e52004-08-23 15:12:04 +0000304asn1c
305\series bold
306-EF
307\series default
308
vlme18adea2004-06-03 03:49:45 +0000309\emph on
vlm856978f2005-02-21 14:16:10 +0000310<module-to-test.asn1>
vlme18adea2004-06-03 03:49:45 +0000311\layout Standard
312
vlmfcec8e52004-08-23 15:12:04 +0000313The
314\series bold
315-P
316\series default
317 option is used to dump the compiled output on the screen instead of creating
318 a bunch of .c and .h files on disk in the current directory.
319 You would probably want to start with
320\series bold
321-P
322\series default
323 option instead of creating a mess in your current directory.
324 Another option,
325\series bold
326-R
327\series default
328, asks compiler to only generate the files which need to be generated, and
329 supress linking in the numerous support files.
330\layout Standard
331
332Print the compiled output instead of creating multiple source files:
333\layout LyX-Code
334
335asn1c
336\series bold
337-P
338\series default
339
340\emph on
vlm856978f2005-02-21 14:16:10 +0000341<module-to-compile-and-print.asn1>
vlme18adea2004-06-03 03:49:45 +0000342\layout Section
343
vlm856978f2005-02-21 14:16:10 +0000344Recognizing compiler output
345\layout Standard
346
347After compiling, the following entities will be created in your current
348 directory:
349\layout Itemize
350
351A set of .c and .h files, generally a single pair for each type defined in
352 the ASN.1 specifications.
353 These files will be named similarly to the ASN.1 types (
354\emph on
355Rectangle.c
356\emph default
357 and
358\emph on
359Rectangle.h
360\emph default
361 for the RectangleTest ASN.1 module defined in the beginning of this document).
362\layout Itemize
363
364A set of helper .c and .h files which contain generic encoders, decoders and
365 other useful routines.
366 There will be quite a few of them, some of them even are not always necessary,
367 but the overall amount of code after compilation will be rather small anyway.
368\layout Itemize
369
370A
371\emph on
372Makefile.am.sample
373\emph default
374 file mentioning all the files created at the earlier steps.
375 This file is suitable for either automake suite or the plain `make` utility.
376\layout Standard
377
378It is your responsibility to create .c file with the
379\emph on
380 int main()
381\emph default
382 routine.
383\layout Standard
384
385In other words, after compiling the Rectangle module, you have the following
386 set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h,
387\series bold
388\SpecialChar \ldots{}
389
390\series default
391 }, where
392\series bold
393
394\begin_inset Quotes sld
395\end_inset
396
397\SpecialChar \ldots{}
398
399\begin_inset Quotes srd
400\end_inset
401
402
403\series default
404 stands for the set of additional
405\begin_inset Quotes sld
406\end_inset
407
408helper
409\begin_inset Quotes srd
410\end_inset
411
412 files created by the compiler.
413 If you add a simple file with the
414\emph on
415int main()
416\emph default
417 routine, it would even be possible to compile everything with the single
418 instruction:
419\layout LyX-Code
420
421cc -I.
422 -o rectangle.exe *.c # It could be
423\emph on
424that
425\emph default
426 simple
427\layout Standard
428
429Refer to the Chapter
430\begin_inset LatexCommand \vref{cha:Step-by-step-examples}
431
432\end_inset
433
434 for a sample
435\emph on
436int main()
437\emph default
438 routine.
439\layout Section
440
vlmfcec8e52004-08-23 15:12:04 +0000441
vlm4cc45722005-07-24 08:28:39 +0000442\begin_inset LatexCommand \label{sec:Command-line-options}
vlmfcec8e52004-08-23 15:12:04 +0000443
444\end_inset
445
vlm4cc45722005-07-24 08:28:39 +0000446Command line options
vlmfcec8e52004-08-23 15:12:04 +0000447\layout Standard
448
vlm4cc45722005-07-24 08:28:39 +0000449The following table summarizes the asn1c command line options.
vlmfcec8e52004-08-23 15:12:04 +0000450\layout Standard
451
452
453\begin_inset Tabular
vlm11bea882005-10-06 10:09:34 +0000454<lyxtabular version="3" rows="27" columns="2">
vlmc3bfd812004-09-30 22:26:19 +0000455<features islongtable="true">
vlmfcec8e52004-08-23 15:12:04 +0000456<column alignment="left" valignment="top" leftline="true" width="0">
457<column alignment="block" valignment="top" leftline="true" rightline="true" width="3in">
458<row topline="true" bottomline="true">
459<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
460\begin_inset Text
461
462\layout Standard
463
464
465\series bold
vlm37d47c02005-03-05 01:07:04 +0000466\size small
vlmfcec8e52004-08-23 15:12:04 +0000467Overall Options
468\end_inset
469</cell>
470<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
471\begin_inset Text
472
473\layout Standard
474
475
476\series bold
vlm37d47c02005-03-05 01:07:04 +0000477\size small
vlmfcec8e52004-08-23 15:12:04 +0000478Description
479\end_inset
480</cell>
481</row>
482<row topline="true">
483<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
484\begin_inset Text
485
486\layout Standard
487
vlm37d47c02005-03-05 01:07:04 +0000488
489\size small
vlmfcec8e52004-08-23 15:12:04 +0000490-E
491\end_inset
492</cell>
493<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
494\begin_inset Text
495
496\layout Standard
497
498
499\size small
500Stop after the parsing stage and print the reconstructed ASN.1 specification
501 code to the standard output.
502\end_inset
503</cell>
504</row>
505<row topline="true">
506<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
507\begin_inset Text
508
509\layout Standard
510
vlm37d47c02005-03-05 01:07:04 +0000511
512\size small
vlmfcec8e52004-08-23 15:12:04 +0000513-F
514\end_inset
515</cell>
516<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
517\begin_inset Text
518
519\layout Standard
520
521
522\size small
523Used together with -E, instructs the compiler to stop after the ASN.1 syntax
524 tree fixing stage and dump the reconstructed ASN.1 specification to the
525 standard output.
526\end_inset
527</cell>
528</row>
529<row topline="true">
530<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
531\begin_inset Text
532
533\layout Standard
534
vlm37d47c02005-03-05 01:07:04 +0000535
536\size small
vlmfcec8e52004-08-23 15:12:04 +0000537-P
538\end_inset
539</cell>
540<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
541\begin_inset Text
542
543\layout Standard
544
545
546\size small
547Dump the compiled output to the standard output instead of cre- ating the
548 target language files on disk.
549\end_inset
550</cell>
551</row>
552<row topline="true">
553<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
554\begin_inset Text
555
556\layout Standard
557
vlm37d47c02005-03-05 01:07:04 +0000558
559\size small
vlmfcec8e52004-08-23 15:12:04 +0000560-R
561\end_inset
562</cell>
563<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
564\begin_inset Text
565
566\layout Standard
567
568
569\size small
570Restrict the compiler to generate only the ASN.1 tables, omit- ting the usual
571 support code.
572\end_inset
573</cell>
574</row>
vlm60e7ef02004-10-13 09:13:56 +0000575<row topline="true">
vlmfcec8e52004-08-23 15:12:04 +0000576<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
577\begin_inset Text
578
579\layout Standard
580
vlm37d47c02005-03-05 01:07:04 +0000581
582\size small
vlmfcec8e52004-08-23 15:12:04 +0000583-S
584\emph on
585<directory>
586\end_inset
587</cell>
588<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
589\begin_inset Text
590
591\layout Standard
592
593
594\size small
595Use the specified directory with ASN.1 skeleton files.
596\end_inset
597</cell>
598</row>
vlm60e7ef02004-10-13 09:13:56 +0000599<row topline="true" bottomline="true">
600<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
601\begin_inset Text
602
603\layout Standard
604
vlm37d47c02005-03-05 01:07:04 +0000605
606\size small
vlm60e7ef02004-10-13 09:13:56 +0000607-X
608\end_inset
609</cell>
610<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
611\begin_inset Text
612
613\layout Standard
614
vlm37d47c02005-03-05 01:07:04 +0000615
616\size small
vlm60e7ef02004-10-13 09:13:56 +0000617Generate the XML DTD for the specified ASN.1 modules.
618\end_inset
619</cell>
620</row>
vlmfcec8e52004-08-23 15:12:04 +0000621<row topline="true">
622<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
623\begin_inset Text
624
625\layout Standard
626
627
628\series bold
vlm37d47c02005-03-05 01:07:04 +0000629\size small
vlmfcec8e52004-08-23 15:12:04 +0000630Warning Options
631\end_inset
632</cell>
633<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
634\begin_inset Text
635
636\layout Standard
637
638
639\series bold
vlm37d47c02005-03-05 01:07:04 +0000640\size small
vlmfcec8e52004-08-23 15:12:04 +0000641Description
642\end_inset
643</cell>
644</row>
645<row topline="true">
646<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
647\begin_inset Text
648
649\layout Standard
650
vlm37d47c02005-03-05 01:07:04 +0000651
652\size small
vlmfcec8e52004-08-23 15:12:04 +0000653-Werror
654\end_inset
655</cell>
656<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
657\begin_inset Text
658
659\layout Standard
660
661
662\size small
663Treat warnings as errors; abort if any warning is produced.
664\end_inset
665</cell>
666</row>
667<row topline="true">
668<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
669\begin_inset Text
670
671\layout Standard
672
vlm37d47c02005-03-05 01:07:04 +0000673
674\size small
vlmfcec8e52004-08-23 15:12:04 +0000675-Wdebug-lexer
676\end_inset
677</cell>
678<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
679\begin_inset Text
680
681\layout Standard
682
683
684\size small
685Enable lexer debugging during the ASN.1 parsing stage.
686\end_inset
687</cell>
688</row>
689<row topline="true">
690<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
691\begin_inset Text
692
693\layout Standard
694
vlm37d47c02005-03-05 01:07:04 +0000695
696\size small
vlmfcec8e52004-08-23 15:12:04 +0000697-Wdebug-fixer
698\end_inset
699</cell>
700<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
701\begin_inset Text
702
703\layout Standard
704
705
706\size small
707 Enable ASN.1 syntax tree fixer debugging during the fixing stage.
708\end_inset
709</cell>
710</row>
vlm4cc45722005-07-24 08:28:39 +0000711<row topline="true" bottomline="true" newpage="true">
vlmfcec8e52004-08-23 15:12:04 +0000712<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
713\begin_inset Text
714
715\layout Standard
716
vlm37d47c02005-03-05 01:07:04 +0000717
718\size small
vlmfcec8e52004-08-23 15:12:04 +0000719-Wdebug-compiler
720\end_inset
721</cell>
722<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
723\begin_inset Text
724
725\layout Standard
726
727
728\size small
729Enable debugging during the actual compile time.
730\end_inset
731</cell>
732</row>
733<row topline="true">
734<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
735\begin_inset Text
736
737\layout Standard
738
739
740\series bold
vlm37d47c02005-03-05 01:07:04 +0000741\size small
vlmfcec8e52004-08-23 15:12:04 +0000742Language Options
743\end_inset
744</cell>
745<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
746\begin_inset Text
747
748\layout Standard
749
750
751\series bold
vlm37d47c02005-03-05 01:07:04 +0000752\size small
vlmfcec8e52004-08-23 15:12:04 +0000753Description
754\end_inset
755</cell>
756</row>
757<row topline="true">
758<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
759\begin_inset Text
760
761\layout Standard
762
vlm37d47c02005-03-05 01:07:04 +0000763
764\size small
vlm12c8f692004-09-06 08:07:29 +0000765-fall-defs-global
766\end_inset
767</cell>
768<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
769\begin_inset Text
770
771\layout Standard
772
vlm37d47c02005-03-05 01:07:04 +0000773
774\size small
vlmba56d852004-09-29 13:29:17 +0000775Normally the compiler hides the definitions (asn_DEF_xxx) of the inner structure
776 elements (members of SEQUENCE, SET and other types).
vlm12c8f692004-09-06 08:07:29 +0000777 This option makes all such definitions global.
vlmba56d852004-09-29 13:29:17 +0000778 Enabling this option may pollute the namespace by making lots of asn_DEF_xxx
vlm12c8f692004-09-06 08:07:29 +0000779 structures globally visible, but will allow you to manipulate (encode and
780 decode) the individual members of any complex ASN.1 structure.
781\end_inset
782</cell>
783</row>
784<row topline="true">
785<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
786\begin_inset Text
787
788\layout Standard
789
vlm37d47c02005-03-05 01:07:04 +0000790
791\size small
vlmfcec8e52004-08-23 15:12:04 +0000792-fbless-SIZE
793\end_inset
794</cell>
795<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
796\begin_inset Text
797
798\layout Standard
799
800
801\size small
802Allow SIZE() constraint for INTEGER, ENUMERATED, and other types for which
803 this constraint is normally prohibited by the standard.
804 This is a violation of an ASN.1 standard and compiler may fail to produce
805 the meaningful code.
806\end_inset
807</cell>
808</row>
809<row topline="true">
810<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
811\begin_inset Text
812
813\layout Standard
814
vlm37d47c02005-03-05 01:07:04 +0000815
816\size small
817-fcompound-names
818\end_inset
819</cell>
820<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
821\begin_inset Text
822
823\layout Standard
824
825
826\size small
827Use complex names for C structures.
828 Using complex names prevents name clashes in case the module reuses the
829 same identifiers in multiple contexts.
830\end_inset
831</cell>
832</row>
833<row topline="true">
834<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
835\begin_inset Text
836
837\layout Standard
838
vlm11bea882005-10-06 10:09:34 +0000839
840\size small
vlm4cc45722005-07-24 08:28:39 +0000841-findirect-choice
842\end_inset
843</cell>
844<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
845\begin_inset Text
846
847\layout Standard
848
vlm11bea882005-10-06 10:09:34 +0000849
850\size small
vlm4cc45722005-07-24 08:28:39 +0000851When generating code for a CHOICE type, compile the CHOICE members as indirect
852 pointers instead of declaring them inline.
853 Consider using this option together with
854\series bold
855-fno-include-deps
856\series default
857 to prevent circular references.
858
859\end_inset
860</cell>
861</row>
862<row topline="true">
863<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
864\begin_inset Text
865
866\layout Standard
867
vlm11bea882005-10-06 10:09:34 +0000868
869\size small
vlm4cc45722005-07-24 08:28:39 +0000870-fknown-extern-type=
871\emph on
872<name>
873\end_inset
874</cell>
875<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
876\begin_inset Text
877
878\layout Standard
879
vlm11bea882005-10-06 10:09:34 +0000880
881\size small
vlm4cc45722005-07-24 08:28:39 +0000882Pretend the specified type is known.
883 The compiler will assume the target language source files for the given
884 type have been provided manually.
885
886\end_inset
887</cell>
888</row>
889<row topline="true">
890<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
891\begin_inset Text
892
893\layout Standard
894
vlm37d47c02005-03-05 01:07:04 +0000895
896\size small
vlm03af0f02004-09-14 12:48:17 +0000897-fnative-types
vlmfcec8e52004-08-23 15:12:04 +0000898\end_inset
899</cell>
900<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
901\begin_inset Text
902
903\layout Standard
904
905
906\size small
vlm03af0f02004-09-14 12:48:17 +0000907Use the native machine's data types (int, double) whenever possible, instead
vlmc3bfd812004-09-30 22:26:19 +0000908 of the compound INTEGER_t, ENUMERATED_t and REAL_t types.
vlmfcec8e52004-08-23 15:12:04 +0000909
910\end_inset
911</cell>
912</row>
913<row topline="true">
914<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
915\begin_inset Text
916
917\layout Standard
918
vlm37d47c02005-03-05 01:07:04 +0000919
920\size small
vlmbfc49bd2004-09-26 13:13:13 +0000921-fno-constraints
922\end_inset
923</cell>
924<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
925\begin_inset Text
926
927\layout Standard
928
vlm37d47c02005-03-05 01:07:04 +0000929
930\size small
vlmbfc49bd2004-09-26 13:13:13 +0000931Do not generate ASN.1 subtype constraint checking code.
vlm37d47c02005-03-05 01:07:04 +0000932 This may produce a shorter executable.
vlmbfc49bd2004-09-26 13:13:13 +0000933\end_inset
934</cell>
935</row>
936<row topline="true">
937<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
938\begin_inset Text
939
940\layout Standard
941
vlm37d47c02005-03-05 01:07:04 +0000942
943\size small
944-fno-include-deps
945\end_inset
946</cell>
947<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
948\begin_inset Text
949
950\layout Standard
951
952
953\size small
954Do not generate courtesy #include lines for non-critical dependencies.
955\end_inset
956</cell>
957</row>
958<row topline="true">
959<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
960\begin_inset Text
961
962\layout Standard
963
964
965\size small
vlmfcec8e52004-08-23 15:12:04 +0000966-funnamed-unions
967\end_inset
968</cell>
969<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
970\begin_inset Text
971
972\layout Standard
973
974
975\size small
976Enable unnamed unions in the definitions of target language's structures.
977\end_inset
978</cell>
979</row>
vlm11bea882005-10-06 10:09:34 +0000980<row topline="true">
981<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
982\begin_inset Text
983
984\layout Standard
985
986
987\size small
988-fskeletons-copy
989\end_inset
990</cell>
991<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
992\begin_inset Text
993
994\layout Standard
995
996
997\size small
998Copy support files rather than symlink them.
999\end_inset
1000</cell>
1001</row>
vlmfcec8e52004-08-23 15:12:04 +00001002<row topline="true" bottomline="true">
1003<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1004\begin_inset Text
1005
1006\layout Standard
1007
vlm37d47c02005-03-05 01:07:04 +00001008
1009\size small
vlmfcec8e52004-08-23 15:12:04 +00001010-ftypes88
1011\end_inset
1012</cell>
1013<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1014\begin_inset Text
1015
1016\layout Standard
1017
1018
1019\size small
vlmc3bfd812004-09-30 22:26:19 +00001020Pretend to support only ASN.1:1988 embedded types.
1021 Certain reserved words, such as UniversalString and BMPString, become ordinary
1022 type references and may be redefined by the specification.
vlmfcec8e52004-08-23 15:12:04 +00001023\end_inset
1024</cell>
1025</row>
vlm4cc45722005-07-24 08:28:39 +00001026<row topline="true">
vlmfcec8e52004-08-23 15:12:04 +00001027<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1028\begin_inset Text
1029
1030\layout Standard
1031
1032
1033\series bold
vlm37d47c02005-03-05 01:07:04 +00001034\size small
vlmfcec8e52004-08-23 15:12:04 +00001035Output Options
1036\end_inset
1037</cell>
1038<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1039\begin_inset Text
1040
1041\layout Standard
1042
1043
1044\series bold
vlm37d47c02005-03-05 01:07:04 +00001045\size small
vlmfcec8e52004-08-23 15:12:04 +00001046Description
1047\end_inset
1048</cell>
1049</row>
1050<row topline="true">
1051<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1052\begin_inset Text
1053
1054\layout Standard
1055
vlm37d47c02005-03-05 01:07:04 +00001056
1057\size small
vlmfcec8e52004-08-23 15:12:04 +00001058-print-constraints
1059\end_inset
1060</cell>
1061<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1062\begin_inset Text
1063
1064\layout Standard
1065
1066
1067\size small
1068When -EF are also specified, this option forces the compiler to explain
1069 its internal understanding of subtype constraints.
1070\end_inset
1071</cell>
1072</row>
1073<row topline="true" bottomline="true">
1074<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1075\begin_inset Text
1076
1077\layout Standard
1078
vlm37d47c02005-03-05 01:07:04 +00001079
1080\size small
vlmfcec8e52004-08-23 15:12:04 +00001081-print-lines
1082\end_inset
1083</cell>
1084<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1085\begin_inset Text
1086
1087\layout Standard
1088
1089
1090\size small
1091Generate "-- #line" comments in -E output.
1092\end_inset
1093</cell>
1094</row>
1095</lyxtabular>
1096
1097\end_inset
1098
1099
vlm856978f2005-02-21 14:16:10 +00001100\layout Chapter
1101
1102Using the ASN.1 Compiler
vlmfcec8e52004-08-23 15:12:04 +00001103\layout Section
vlme18adea2004-06-03 03:49:45 +00001104
vlm856978f2005-02-21 14:16:10 +00001105Invoking the ASN.1 helper code
vlmc3bfd812004-09-30 22:26:19 +00001106\begin_inset OptArg
1107collapsed true
1108
1109\layout Standard
1110
1111Invoking the helper code
1112\end_inset
1113
1114
vlme18adea2004-06-03 03:49:45 +00001115\layout Standard
1116
vlm485123c2005-02-22 07:28:26 +00001117First of all, you should include one or more header files into your application.
1118 Typically, it is enough to include the header file of the main PDU type.
1119 For our Rectangle module, including the Rectangle.h file is sufficient:
vlme18adea2004-06-03 03:49:45 +00001120\layout LyX-Code
1121
1122#include <Rectangle.h>
1123\layout Standard
1124
1125The header files defines the C structure corresponding to the ASN.1 definition
1126 of a rectangle and the declaration of the ASN.1 type descriptor, which is
1127 used as an argument to most of the functions provided by the ASN.1 module.
1128 For example, here is the code which frees the Rectangle_t structure:
1129\layout LyX-Code
1130
vlmfcec8e52004-08-23 15:12:04 +00001131Rectangle_t *rect = ...;
1132\layout LyX-Code
1133
1134
vlme18adea2004-06-03 03:49:45 +00001135\layout LyX-Code
1136
vlm823f1442005-08-29 10:23:20 +00001137asn_DEF_Rectangle.free_struct(&asn_DEF_Rectangle,
vlme18adea2004-06-03 03:49:45 +00001138\layout LyX-Code
1139
1140 rect, 0);
1141\layout Standard
1142
1143This code defines a
1144\emph on
1145rect
1146\emph default
1147 pointer which points to the Rectangle_t structure which needs to be freed.
vlm485123c2005-02-22 07:28:26 +00001148 The second line invokes the generic
1149\emph on
1150free_struct()
1151\emph default
1152 routine created specifically for this Rectangle_t structure.
vlme18adea2004-06-03 03:49:45 +00001153 The
1154\emph on
vlmba56d852004-09-29 13:29:17 +00001155asn_DEF_Rectangle
vlme18adea2004-06-03 03:49:45 +00001156\emph default
vlm485123c2005-02-22 07:28:26 +00001157 is the type descriptor, which holds a collection of routines to deal with
1158 the Rectangle_t structure.
vlme18adea2004-06-03 03:49:45 +00001159\layout Standard
1160
vlm485123c2005-02-22 07:28:26 +00001161The following member functions of the asn_DEF_Rectangle type descriptor
1162 are of interest:
vlme18adea2004-06-03 03:49:45 +00001163\layout Description
1164
vlme18adea2004-06-03 03:49:45 +00001165ber_decoder This is the generic
1166\emph on
1167restartable
1168\begin_inset Foot
1169collapsed false
1170
1171\layout Standard
1172
1173Restartable means that if the decoder encounters the end of the buffer,
1174 it will fail, but may later be invoked again with the rest of the buffer
1175 to continue decoding.
1176\end_inset
1177
1178
1179\emph default
1180BER decoder (Basic Encoding Rules).
1181 This decoder would create and/or fill the target structure for you.
1182 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001183\begin_inset LatexCommand \vref{sub:Decoding-BER}
vlme18adea2004-06-03 03:49:45 +00001184
1185\end_inset
1186
1187.
1188\layout Description
1189
1190der_encoder This is the generic DER encoder (Distinguished Encoding Rules).
vlm68d43e92004-09-24 20:56:07 +00001191 This encoder will take the target structure and encode it into a series
vlme18adea2004-06-03 03:49:45 +00001192 of bytes.
1193 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001194\begin_inset LatexCommand \vref{sub:Encoding-DER}
vlme18adea2004-06-03 03:49:45 +00001195
1196\end_inset
1197
1198.
1199\layout Description
1200
vlm856978f2005-02-21 14:16:10 +00001201xer_encoder This is the XER encoder (XML Encoding Rules).
vlm68d43e92004-09-24 20:56:07 +00001202 This encoder will take the target structure and represent it as an XML
vlm856978f2005-02-21 14:16:10 +00001203 (text) document using either BASIC-XER or CANONICAL-XER encoding rules.
vlm68d43e92004-09-24 20:56:07 +00001204 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001205\begin_inset LatexCommand \vref{sub:Encoding-XER}
1206
1207\end_inset
1208
1209.
1210\layout Description
1211
1212xer_decoder This is the generic XER decoder.
1213 It takes both BASIC-XER or CANONICAL-XER encodings and deserializes the
1214 data into a local, machine-dependent representation.
1215 Please refer to Section
1216\begin_inset LatexCommand \vref{sub:Decoding-XER}
vlm68d43e92004-09-24 20:56:07 +00001217
1218\end_inset
1219
1220.
1221\layout Description
1222
1223check_constraints Check that the contents of the target structure are semantical
1224ly valid and constrained to appropriate implicit or explicit subtype constraints.
1225 Please refer to Section
1226\begin_inset LatexCommand \vref{sub:Validating-the-target}
1227
1228\end_inset
1229
1230.
1231\layout Description
1232
vlme18adea2004-06-03 03:49:45 +00001233print_struct This function convert the contents of the passed target structure
1234 into human readable form.
1235 This form is not formal and cannot be converted back into the structure,
1236 but it may turn out to be useful for debugging or quick-n-dirty printing.
1237 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001238\begin_inset LatexCommand \vref{sub:Printing-the-target}
vlme18adea2004-06-03 03:49:45 +00001239
1240\end_inset
1241
1242.
1243\layout Description
1244
1245free_struct This is a generic disposal which frees the target structure.
1246 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001247\begin_inset LatexCommand \vref{sub:Freeing-the-target}
vlm68d43e92004-09-24 20:56:07 +00001248
1249\end_inset
1250
1251.
1252\layout Standard
1253
vlme18adea2004-06-03 03:49:45 +00001254Each of the above function takes the type descriptor (
1255\emph on
vlmba56d852004-09-29 13:29:17 +00001256asn_DEF_\SpecialChar \ldots{}
vlme18adea2004-06-03 03:49:45 +00001257
1258\emph default
1259) and the target structure (
1260\emph on
1261rect
1262\emph default
1263, in the above example).
vlm856978f2005-02-21 14:16:10 +00001264\layout Subsection
1265
1266
1267\begin_inset LatexCommand \label{sub:Decoding-BER}
1268
1269\end_inset
1270
1271Decoding BER
vlme18adea2004-06-03 03:49:45 +00001272\layout Standard
1273
vlm856978f2005-02-21 14:16:10 +00001274The Basic Encoding Rules describe the most widely used (by the ASN.1 community)
vlm485123c2005-02-22 07:28:26 +00001275 way to encode and decode a given structure in a machine-independent way.
vlm856978f2005-02-21 14:16:10 +00001276 Several other encoding rules (CER, DER) define a more restrictive versions
1277 of BER, so the generic BER parser is also capable of decoding the data
1278 encoded by CER and DER encoders.
1279 The opposite is not true.
1280\layout Standard
1281
vlm485123c2005-02-22 07:28:26 +00001282
1283\emph on
vlm856978f2005-02-21 14:16:10 +00001284The ASN.1 compiler provides the generic BER decoder which is implicitly capable
1285 of decoding BER, CER and DER encoded data.
1286\layout Standard
1287
1288The decoder is restartable (stream-oriented), which means that in case the
1289 buffer has less data than it is expected, the decoder will process whatever
1290 there is available and ask for more data to be provided.
1291 Please note that the decoder may actually process less data than it was
1292 given in the buffer, which means that you must be able to make the next
1293 buffer contain the unprocessed part of the previous buffer.
1294\layout Standard
1295
1296Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
1297\layout Itemize
1298
1299You may concatenate these buffers and feed the BER decoder with 300 bytes
1300 of data, or
1301\layout Itemize
1302
1303You may feed it the first buffer of 100 bytes of data, realize that the
1304 ber_decoder consumed only 95 bytes from it and later feed the decoder with
1305 205 bytes buffer which consists of 5 unprocessed bytes from the first buffer
vlm485123c2005-02-22 07:28:26 +00001306 and the additional 200 bytes from the second buffer.
vlm856978f2005-02-21 14:16:10 +00001307\layout Standard
1308
vlm485123c2005-02-22 07:28:26 +00001309This is not as convenient as it could be (like, the BER encoder could consume
vlm856978f2005-02-21 14:16:10 +00001310 the whole 100 bytes and keep these 5 bytes in some temporary storage),
vlm485123c2005-02-22 07:28:26 +00001311 but in case of existing stream based processing it might actually fit well
1312 into existing algorithm.
vlm856978f2005-02-21 14:16:10 +00001313 Suggestions are welcome.
1314\layout Standard
1315
vlm485123c2005-02-22 07:28:26 +00001316Here is the simplest example of BER decoding.
vlme18adea2004-06-03 03:49:45 +00001317\layout LyX-Code
1318
1319Rectangle_t *
1320\layout LyX-Code
1321
vlmfcec8e52004-08-23 15:12:04 +00001322simple_deserializer(const void *buffer, size_t buf_size) {
vlme18adea2004-06-03 03:49:45 +00001323\layout LyX-Code
1324
1325 Rectangle_t *rect = 0; /* Note this 0! */
1326\layout LyX-Code
1327
vlmaf2155e2004-10-20 15:48:55 +00001328 asn_dec_rval_t rval;
vlme18adea2004-06-03 03:49:45 +00001329\layout LyX-Code
1330
1331
1332\layout LyX-Code
1333
vlm856978f2005-02-21 14:16:10 +00001334 rval =
1335\series bold
vlm823f1442005-08-29 10:23:20 +00001336asn_DEF_Rectangle.ber_decoder
vlm856978f2005-02-21 14:16:10 +00001337\series default
1338(0,
vlme18adea2004-06-03 03:49:45 +00001339\layout LyX-Code
1340
vlmba56d852004-09-29 13:29:17 +00001341 &asn_DEF_Rectangle,
vlme18adea2004-06-03 03:49:45 +00001342\layout LyX-Code
1343
1344 (void **)&rect,
1345\layout LyX-Code
1346
1347 buffer, buf_size,
1348\layout LyX-Code
1349
1350 0);
1351\layout LyX-Code
1352
1353
1354\layout LyX-Code
1355
1356 if(rval
1357\series bold
1358.code
1359\series default
1360 == RC_OK) {
1361\layout LyX-Code
1362
1363 return rect; /* Decoding succeeded */
1364\layout LyX-Code
1365
1366 } else {
1367\layout LyX-Code
1368
vlmfcec8e52004-08-23 15:12:04 +00001369 /* Free partially decoded rect */
1370\layout LyX-Code
1371
vlm823f1442005-08-29 10:23:20 +00001372 asn_DEF_Rectangle.free_struct(
vlme18adea2004-06-03 03:49:45 +00001373\layout LyX-Code
1374
vlmba56d852004-09-29 13:29:17 +00001375 &asn_DEF_Rectangle, rect, 0);
vlme18adea2004-06-03 03:49:45 +00001376\layout LyX-Code
1377
1378 return 0;
1379\layout LyX-Code
1380
1381 }
1382\layout LyX-Code
1383
1384}
1385\layout Standard
1386
1387The code above defines a function,
1388\emph on
1389simple_deserializer
1390\emph default
vlm485123c2005-02-22 07:28:26 +00001391, which takes a buffer and its length and is expected to return a pointer
1392 to the Rectangle_t structure.
vlme18adea2004-06-03 03:49:45 +00001393 Inside, it tries to convert the bytes passed into the target structure
vlm485123c2005-02-22 07:28:26 +00001394 (rect) using the BER decoder and returns the rect pointer afterwards.
vlme18adea2004-06-03 03:49:45 +00001395 If the structure cannot be deserialized, it frees the memory which might
1396 be left allocated by the unfinished
1397\emph on
1398ber_decoder
1399\emph default
vlm856978f2005-02-21 14:16:10 +00001400 routine and returns 0 (no data).
vlm485123c2005-02-22 07:28:26 +00001401 (This
vlme18adea2004-06-03 03:49:45 +00001402\series bold
vlm856978f2005-02-21 14:16:10 +00001403 freeing is necessary
vlme18adea2004-06-03 03:49:45 +00001404\series default
1405 because the ber_decoder is a restartable procedure, and may fail just because
vlm485123c2005-02-22 07:28:26 +00001406 there is more data needs to be provided before decoding could be finalized).
vlme18adea2004-06-03 03:49:45 +00001407 The code above obviously does not take into account the way the
1408\emph on
vlm485123c2005-02-22 07:28:26 +00001409ber_decoder()
vlme18adea2004-06-03 03:49:45 +00001410\emph default
1411 failed, so the freeing is necessary because the part of the buffer may
1412 already be decoded into the structure by the time something goes wrong.
1413\layout Standard
1414
vlm485123c2005-02-22 07:28:26 +00001415A little less wordy would be to invoke a globally available
vlme18adea2004-06-03 03:49:45 +00001416\emph on
vlm485123c2005-02-22 07:28:26 +00001417ber_decode()
vlme18adea2004-06-03 03:49:45 +00001418\emph default
vlm485123c2005-02-22 07:28:26 +00001419 function instead of dereferencing the asn_DEF_Rectangle type descriptor:
vlme18adea2004-06-03 03:49:45 +00001420\layout LyX-Code
1421
vlmba56d852004-09-29 13:29:17 +00001422rval = ber_decode(0, &asn_DEF_Rectangle, (void **)&rect,
vlme18adea2004-06-03 03:49:45 +00001423\layout LyX-Code
1424
1425 buffer, buf_size);
1426\layout Standard
1427
vlm823f1442005-08-29 10:23:20 +00001428Note that the initial (asn_DEF_Rectangle.ber_decoder) reference is gone,
vlme18adea2004-06-03 03:49:45 +00001429 and also the last argument (0) is no longer necessary.
1430\layout Standard
1431
vlm856978f2005-02-21 14:16:10 +00001432These two ways of BER decoder invocations are fully equivalent.
vlme18adea2004-06-03 03:49:45 +00001433\layout Standard
1434
1435The BER de
1436\emph on
1437coder
1438\emph default
vlmba56d852004-09-29 13:29:17 +00001439 may fail because of (
vlme18adea2004-06-03 03:49:45 +00001440\emph on
1441the following RC_\SpecialChar \ldots{}
1442 codes are defined in ber_decoder.h
1443\emph default
1444):
1445\layout Itemize
1446
1447RC_WMORE: There is more data expected than it is provided (stream mode continuat
1448ion feature);
1449\layout Itemize
1450
1451RC_FAIL: General failure to decode the buffer;
1452\layout Itemize
1453
1454\SpecialChar \ldots{}
1455 other codes may be defined as well.
1456\layout Standard
1457
vlmaf2155e2004-10-20 15:48:55 +00001458Together with the return code (.code) the asn_dec_rval_t type contains the
vlme18adea2004-06-03 03:49:45 +00001459 number of bytes which is consumed from the buffer.
1460 In the previous hypothetical example of two buffers (of 100 and 200 bytes),
1461 the first call to ber_decode() would return with .code = RC_WMORE and .consumed
1462 = 95.
1463 The .consumed field of the BER decoder return value is
1464\series bold
1465always
1466\series default
1467 valid, even if the decoder succeeds or fails with any other return code.
1468\layout Standard
1469
1470Please look into ber_decoder.h for the precise definition of ber_decode()
1471 and related types.
vlmfcec8e52004-08-23 15:12:04 +00001472\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001473
1474
1475\begin_inset LatexCommand \label{sub:Encoding-DER}
1476
1477\end_inset
1478
1479Encoding DER
1480\layout Standard
1481
vlm68d43e92004-09-24 20:56:07 +00001482The Distinguished Encoding Rules is the
1483\emph on
1484canonical
1485\emph default
1486 variant of BER encoding rules.
1487 The DER is best suited to encode the structures where all the lengths are
1488 known beforehand.
vlme18adea2004-06-03 03:49:45 +00001489 This is probably exactly how you want to encode: either after a BER decoding
1490 or after a manual fill-up, the target structure contains the data which
1491 size is implicitly known before encoding.
vlm856978f2005-02-21 14:16:10 +00001492 Among other uses, the DER encoding is used to encode X.509 certificates.
vlme18adea2004-06-03 03:49:45 +00001493\layout Standard
1494
1495As with BER decoder, the DER encoder may be invoked either directly from
vlmba56d852004-09-29 13:29:17 +00001496 the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone function,
vlme18adea2004-06-03 03:49:45 +00001497 which is somewhat simpler:
1498\layout LyX-Code
1499
vlme18adea2004-06-03 03:49:45 +00001500
1501\layout LyX-Code
1502
1503/*
1504\layout LyX-Code
1505
1506 * This is the serializer itself,
1507\layout LyX-Code
1508
1509 * it supplies the DER encoder with the
1510\layout LyX-Code
1511
1512 * pointer to the custom output function.
1513\layout LyX-Code
1514
1515 */
1516\layout LyX-Code
1517
1518ssize_t
1519\layout LyX-Code
1520
1521simple_serializer(FILE *ostream, Rectangle_t *rect) {
1522\layout LyX-Code
1523
vlm68d43e92004-09-24 20:56:07 +00001524 asn_enc_rval_t er; /* Encoder return value */
vlme18adea2004-06-03 03:49:45 +00001525\layout LyX-Code
1526
1527
1528\layout LyX-Code
1529
vlmba56d852004-09-29 13:29:17 +00001530 er = der_encode(&asn_DEF_Rect, rect,
vlme18adea2004-06-03 03:49:45 +00001531\layout LyX-Code
1532
vlmfcec8e52004-08-23 15:12:04 +00001533 write_stream, ostream);
vlme18adea2004-06-03 03:49:45 +00001534\layout LyX-Code
1535
vlm68d43e92004-09-24 20:56:07 +00001536 if(er.
vlme18adea2004-06-03 03:49:45 +00001537\series bold
vlm68d43e92004-09-24 20:56:07 +00001538encoded
vlme18adea2004-06-03 03:49:45 +00001539\series default
1540 == -1) {
1541\layout LyX-Code
1542
1543 /*
1544\layout LyX-Code
1545
vlm68d43e92004-09-24 20:56:07 +00001546 * Failed to encode the rectangle data.
vlme18adea2004-06-03 03:49:45 +00001547\layout LyX-Code
1548
1549 */
1550\layout LyX-Code
1551
1552 fprintf(stderr,
1553\begin_inset Quotes sld
1554\end_inset
1555
1556Cannot encode %s: %s
1557\backslash
1558n
1559\begin_inset Quotes srd
1560\end_inset
1561
1562,
1563\layout LyX-Code
1564
vlm68d43e92004-09-24 20:56:07 +00001565 er.
vlme18adea2004-06-03 03:49:45 +00001566\series bold
vlm68d43e92004-09-24 20:56:07 +00001567failed_type
vlme18adea2004-06-03 03:49:45 +00001568\series default
1569->name,
1570\layout LyX-Code
1571
1572 strerror(errno));
1573\layout LyX-Code
1574
1575 return -1;
1576\layout LyX-Code
1577
1578 } else {
1579\layout LyX-Code
1580
1581 /* Return the number of bytes */
1582\layout LyX-Code
1583
vlm68d43e92004-09-24 20:56:07 +00001584 return er.encoded;
vlme18adea2004-06-03 03:49:45 +00001585\layout LyX-Code
1586
1587 }
1588\layout LyX-Code
1589
1590}
1591\layout Standard
1592
1593As you see, the DER encoder does not write into some sort of buffer or something.
1594 It just invokes the custom function (possible, multiple times) which would
1595 save the data into appropriate storage.
1596 The optional argument
1597\emph on
1598app_key
1599\emph default
1600 is opaque for the DER encoder code and just used by
1601\emph on
1602_write_stream()
1603\emph default
1604 as the pointer to the appropriate output stream to be used.
1605\layout Standard
1606
1607If the custom write function is not given (passed as 0), then the DER encoder
1608 will essentially do the same thing (i.e., encode the data) but no callbacks
1609 will be invoked (so the data goes nowhere).
1610 It may prove useful to determine the size of the structure's encoding before
1611 actually doing the encoding
1612\begin_inset Foot
1613collapsed false
1614
1615\layout Standard
1616
1617It is actually faster too: the encoder might skip over some computations
1618 which aren't important for the size determination.
1619\end_inset
1620
1621.
1622\layout Standard
1623
1624Please look into der_encoder.h for the precise definition of der_encode()
1625 and related types.
vlmfcec8e52004-08-23 15:12:04 +00001626\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001627
1628
vlm68d43e92004-09-24 20:56:07 +00001629\begin_inset LatexCommand \label{sub:Encoding-XER}
1630
1631\end_inset
1632
1633Encoding XER
1634\layout Standard
1635
1636The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
1637 Markup Language, a text-based format for information exchange.
1638 The encoder routine API comes in two flavors: stdio-based and callback-based.
1639 With the callback-based encoder, the encoding process is very similar to
1640 the DER one, described in Section
1641\begin_inset LatexCommand \vref{sub:Encoding-DER}
1642
1643\end_inset
1644
1645.
1646 The following example uses the definition of write_stream() from up there.
1647\layout LyX-Code
1648
1649/*
1650\layout LyX-Code
1651
1652 * This procedure generates the XML document
1653\layout LyX-Code
1654
1655 * by invoking the XER encoder.
1656\layout LyX-Code
1657
1658 * NOTE: Do not copy this code verbatim!
1659\layout LyX-Code
1660
1661 * If the stdio output is necessary,
1662\layout LyX-Code
1663
1664 * use the xer_fprint() procedure instead.
1665\layout LyX-Code
1666
1667 * See Section
1668\begin_inset LatexCommand \vref{sub:Printing-the-target}
1669
1670\end_inset
1671
1672.
1673\layout LyX-Code
1674
1675 */
1676\layout LyX-Code
1677
1678int
1679\layout LyX-Code
1680
1681print_as_XML(FILE *ostream, Rectangle_t *rect) {
1682\layout LyX-Code
1683
1684 asn_enc_rval_t er; /* Encoder return value */
1685\layout LyX-Code
1686
1687
1688\layout LyX-Code
1689
vlm856978f2005-02-21 14:16:10 +00001690 er = xer_encode(&asn_DEF_Rectangle, rect,
vlm68d43e92004-09-24 20:56:07 +00001691\layout LyX-Code
1692
1693 XER_F_BASIC, /* BASIC-XER or CANONICAL-XER */
1694\layout LyX-Code
1695
1696 write_stream, ostream);
1697\layout LyX-Code
1698
1699
1700\layout LyX-Code
1701
1702 return (er.encoded == -1) ? -1 : 0;
1703\layout LyX-Code
1704
1705}
1706\layout Standard
1707
1708Please look into xer_encoder.h for the precise definition of xer_encode()
1709 and related types.
1710\layout Standard
1711
1712See Section
1713\begin_inset LatexCommand \ref{sub:Printing-the-target}
1714
1715\end_inset
1716
1717 for the example of stdio-based XML encoder and other pretty-printing suggestion
1718s.
1719\layout Subsection
1720
1721
vlm856978f2005-02-21 14:16:10 +00001722\begin_inset LatexCommand \label{sub:Decoding-XER}
1723
1724\end_inset
1725
1726Decoding XER
1727\layout Standard
1728
1729The data encoded using the XER rules can be subsequently decoded using the
1730 xer_decode() API call:
1731\layout LyX-Code
1732
1733Rectangle_t *
1734\layout LyX-Code
1735
1736XML_to_Rectangle(const void *buffer, size_t buf_size) {
1737\layout LyX-Code
1738
1739 Rectangle_t *rect = 0; /* Note this 0! */
1740\layout LyX-Code
1741
1742 asn_dec_rval_t rval;
1743\layout LyX-Code
1744
1745
1746\layout LyX-Code
1747
1748 rval = xer_decode(0, &asn_DEF_Rectangle, (void **)&rect,
1749\layout LyX-Code
1750
1751 buffer, buf_size);
1752\layout LyX-Code
1753
1754 if(rval
1755\series bold
1756.code
1757\series default
1758 == RC_OK) {
1759\layout LyX-Code
1760
1761 return rect; /* Decoding succeeded */
1762\layout LyX-Code
1763
1764 } else {
1765\layout LyX-Code
1766
1767 /* Free partially decoded rect */
1768\layout LyX-Code
1769
vlm823f1442005-08-29 10:23:20 +00001770 asn_DEF_Rectangle.free_struct(
vlm856978f2005-02-21 14:16:10 +00001771\layout LyX-Code
1772
1773 &asn_DEF_Rectangle, rect, 0);
1774\layout LyX-Code
1775
1776 return 0;
1777\layout LyX-Code
1778
1779 }
1780\layout LyX-Code
1781
1782}
1783\layout Standard
1784
1785The decoder takes both BASIC-XER and CANONICAL-XER encodings.
1786\layout Standard
1787
vlm485123c2005-02-22 07:28:26 +00001788The decoder shares its data consumption properties with BER decoder; please
1789 read the Section
1790\begin_inset LatexCommand \vref{sub:Decoding-BER}
1791
1792\end_inset
1793
1794 to know more.
1795\layout Standard
1796
vlm856978f2005-02-21 14:16:10 +00001797Please look into xer_decoder.h for the precise definition of xer_decode()
1798 and related types.
1799\layout Subsection
1800
1801
vlme18adea2004-06-03 03:49:45 +00001802\begin_inset LatexCommand \label{sub:Validating-the-target}
1803
1804\end_inset
1805
1806Validating the target structure
1807\layout Standard
1808
1809Sometimes the target structure needs to be validated.
1810 For example, if the structure was created by the application (as opposed
1811 to being decoded from some external source), some important information
1812 required by the ASN.1 specification might be missing.
1813 On the other hand, the successful decoding of the data from some external
1814 source does not necessarily mean that the data is fully valid either.
1815 It might well be the case that the specification describes some subtype
1816 constraints that were not taken into account during decoding, and it would
1817 actually be useful to perform the last check when the data is ready to
1818 be encoded or when the data has just been decoded to ensure its validity
1819 according to some stricter rules.
1820\layout Standard
1821
1822The asn_check_constraints() function checks the type for various implicit
1823 and explicit constraints.
1824 It is recommended to use asn_check_constraints() function after each decoding
1825 and before each encoding.
1826\layout Standard
1827
1828Please look into constraints.h for the precise definition of asn_check_constraint
1829s() and related types.
vlmfcec8e52004-08-23 15:12:04 +00001830\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001831
1832
1833\begin_inset LatexCommand \label{sub:Printing-the-target}
1834
1835\end_inset
1836
1837Printing the target structure
1838\layout Standard
1839
1840There are two ways to print the target structure: either invoke the print_struct
1841 member of the ASN.1 type descriptor, or using the asn_fprint() function,
1842 which is a simpler wrapper of the former:
1843\layout LyX-Code
1844
vlmba56d852004-09-29 13:29:17 +00001845asn_fprint(stdout, &asn_DEF_Rectangle, rect);
vlme18adea2004-06-03 03:49:45 +00001846\layout Standard
1847
1848Please look into constr_TYPE.h for the precise definition of asn_fprint()
1849 and related types.
vlm68d43e92004-09-24 20:56:07 +00001850\layout Standard
1851
1852Another practical alternative to this custom format printing would be to
1853 invoke XER encoder.
1854 The default BASIC-XER encoder performs reasonable formatting for the output
1855 to be useful and human readable.
1856 To invoke the XER decoder in a manner similar to asn_fprint(), use the
1857 xer_fprint() call:
1858\layout LyX-Code
1859
vlmba56d852004-09-29 13:29:17 +00001860xer_fprint(stdout, &asn_DEF_Rectangle, rect);
vlm68d43e92004-09-24 20:56:07 +00001861\layout Standard
1862
1863See Section
1864\begin_inset LatexCommand \vref{sub:Encoding-XER}
1865
1866\end_inset
1867
1868 for XML-related details.
vlmfcec8e52004-08-23 15:12:04 +00001869\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001870
1871
1872\begin_inset LatexCommand \label{sub:Freeing-the-target}
1873
1874\end_inset
1875
1876Freeing the target structure
1877\layout Standard
1878
1879Freeing the structure is slightly more complex than it may seem to.
1880 When the ASN.1 structure is freed, all the members of the structure and
1881 their submembers etc etc are recursively freed too.
1882 But it might not be feasible to free the structure itself.
1883 Consider the following case:
1884\layout LyX-Code
1885
1886struct my_figure { /* The custom structure */
1887\layout LyX-Code
1888
1889 int flags; /* <some custom member> */
1890\layout LyX-Code
1891
1892 /* The type is generated by the ASN.1 compiler */
1893\layout LyX-Code
1894
1895
1896\emph on
1897Rectangle_t rect;
1898\layout LyX-Code
1899
1900 /* other members of the structure */
1901\layout LyX-Code
1902
1903};
1904\layout Standard
1905
1906In this example, the application programmer defined a custom structure with
1907 one ASN.1-derived member (rect).
1908 This member is not a reference to the Rectangle_t, but an in-place inclusion
1909 of the Rectangle_t structure.
1910 If the freeing is necessary, the usual procedure of freeing everything
1911 must not be applied to the &rect pointer itself, because it does not point
vlm485123c2005-02-22 07:28:26 +00001912 to the memory block directly allocated by the memory allocation routine,
1913 but instead lies within a block allocated for the my_figure structure.
vlme18adea2004-06-03 03:49:45 +00001914\layout Standard
1915
1916To solve this problem, the free_struct routine has the additional argument
vlm485123c2005-02-22 07:28:26 +00001917 (besides the obvious type descriptor and target structure pointers), which
1918 is the flag specifying whether the outer pointer itself must be freed (0,
1919 default) or it should be left intact (non-zero value).
vlme18adea2004-06-03 03:49:45 +00001920\layout LyX-Code
1921
vlm856978f2005-02-21 14:16:10 +00001922
1923\series bold
1924/* 1.
1925 Rectangle_t is defined within my_figure */
vlme18adea2004-06-03 03:49:45 +00001926\layout LyX-Code
1927
vlm856978f2005-02-21 14:16:10 +00001928struct my_figure {
1929\layout LyX-Code
1930
1931 Rectangle_t rect;
1932\layout LyX-Code
1933
1934} *mf =
vlme18adea2004-06-03 03:49:45 +00001935\series bold
1936...
1937\series default
1938;
1939\layout LyX-Code
1940
1941/*
1942\layout LyX-Code
1943
vlm856978f2005-02-21 14:16:10 +00001944 * Freeing the Rectangle_t
vlme18adea2004-06-03 03:49:45 +00001945\layout LyX-Code
1946
vlm856978f2005-02-21 14:16:10 +00001947 * without freeing the mf->rect area
vlme18adea2004-06-03 03:49:45 +00001948\layout LyX-Code
1949
1950 */
1951\layout LyX-Code
1952
vlm823f1442005-08-29 10:23:20 +00001953asn_DEF_Rectangle.free_struct(
vlme18adea2004-06-03 03:49:45 +00001954\layout LyX-Code
1955
vlmba56d852004-09-29 13:29:17 +00001956 &asn_DEF_Rectangle, &mf->rect,
vlm856978f2005-02-21 14:16:10 +00001957\series bold
vlme18adea2004-06-03 03:49:45 +000019581
vlm856978f2005-02-21 14:16:10 +00001959\emph on
vlme18adea2004-06-03 03:49:45 +00001960
vlm856978f2005-02-21 14:16:10 +00001961\emph default
1962/* !free */
1963\series default
1964);
vlme18adea2004-06-03 03:49:45 +00001965\layout LyX-Code
1966
vlm856978f2005-02-21 14:16:10 +00001967
1968\layout LyX-Code
1969
1970
1971\layout LyX-Code
1972
1973
1974\series bold
1975/* 2.
1976 Rectangle_t is a stand-alone pointer */
vlme18adea2004-06-03 03:49:45 +00001977\layout LyX-Code
1978
1979Rectangle_t *rect =
1980\series bold
1981...
1982\series default
1983;
1984\layout LyX-Code
1985
1986/*
1987\layout LyX-Code
1988
1989 * Freeing the Rectangle_t
1990\layout LyX-Code
1991
1992 * and freeing the rect pointer
1993\layout LyX-Code
1994
1995 */
1996\layout LyX-Code
1997
vlm823f1442005-08-29 10:23:20 +00001998asn_DEF_Rectangle.free_struct(
vlme18adea2004-06-03 03:49:45 +00001999\layout LyX-Code
2000
vlmba56d852004-09-29 13:29:17 +00002001 &asn_DEF_Rectangle, rect,
vlm856978f2005-02-21 14:16:10 +00002002\series bold
vlme18adea2004-06-03 03:49:45 +000020030
vlm856978f2005-02-21 14:16:10 +00002004\emph on
2005
vlme18adea2004-06-03 03:49:45 +00002006\emph default
vlm856978f2005-02-21 14:16:10 +00002007/* free the pointer too */
2008\series default
2009);
vlme18adea2004-06-03 03:49:45 +00002010\layout Standard
2011
2012It is safe to invoke the
2013\emph on
2014free_struct
2015\emph default
2016 function with the target structure pointer set to 0 (NULL), the function
2017 will do nothing.
vlma1fd7f92005-01-31 10:10:04 +00002018\layout Chapter
vlm4cb0edf2005-01-14 11:38:49 +00002019
vlmc7737f62005-02-02 09:53:46 +00002020
2021\begin_inset LatexCommand \label{cha:Step-by-step-examples}
2022
2023\end_inset
2024
vlma1fd7f92005-01-31 10:10:04 +00002025Step by step examples
2026\layout Section
2027
2028A
2029\begin_inset Quotes sld
2030\end_inset
2031
2032Rectangle
2033\begin_inset Quotes srd
2034\end_inset
2035
2036 Encoder
2037\layout Standard
2038
vlmc7737f62005-02-02 09:53:46 +00002039This example will help you to create a simple BER and XER encoder of a
vlma1fd7f92005-01-31 10:10:04 +00002040\begin_inset Quotes sld
2041\end_inset
2042
2043Rectangle
2044\begin_inset Quotes srd
2045\end_inset
2046
2047 type used throughout this document.
2048\layout Enumerate
2049
2050Create a file named
2051\series bold
2052rectangle.asn1
2053\series default
2054 with the following contents:
2055\begin_deeper
2056\layout LyX-Code
2057
2058RectangleModule1 DEFINITIONS ::=
2059\layout LyX-Code
2060
2061BEGIN
2062\layout LyX-Code
2063
2064
2065\layout LyX-Code
2066
2067Rectangle ::= SEQUENCE {
2068\layout LyX-Code
2069
2070 height INTEGER,
2071\layout LyX-Code
2072
2073 width INTEGER
2074\layout LyX-Code
2075
2076}
2077\layout LyX-Code
2078
2079
2080\layout LyX-Code
2081
2082END
2083\end_deeper
2084\layout Enumerate
2085
2086Compile it into the set of .c and .h files using asn1c compiler
2087\begin_inset LatexCommand \cite{ASN1C}
2088
2089\end_inset
2090
2091:
2092\begin_deeper
2093\layout LyX-Code
2094
2095
2096\emph on
2097asn1c -fnative-types
2098\series bold
2099\emph default
2100rectangle.asn1
2101\end_deeper
2102\layout Enumerate
2103
2104Alternatively, use the Online ASN.1 compiler
2105\begin_inset LatexCommand \cite{AONL}
2106
2107\end_inset
2108
2109 by uploading the
2110\series bold
2111rectangle.asn1
2112\series default
2113 file into the Web form and unpacking the produced archive on your computer.
2114\layout Enumerate
2115
2116By this time, you should have gotten multiple files in the current directory,
2117 including the
2118\series bold
2119Rectangle.c
2120\series default
2121 and
2122\series bold
2123Rectangle.h
2124\series default
2125.
2126\layout Enumerate
2127
2128Create a main() routine which creates the Rectangle_t structure in memory
2129 and encodes it using BER and XER encoding rules.
2130 Let's name the file
2131\series bold
2132main.c
2133\series default
2134:
2135\begin_inset ERT
2136status Open
2137
2138\layout Standard
2139
2140\backslash
2141clearpage{}
2142\end_inset
2143
2144
2145\begin_deeper
2146\layout LyX-Code
2147
2148
2149\size small
2150#include <stdio.h>
2151\layout LyX-Code
2152
2153
2154\size small
2155#include <sys/types.h>
2156\layout LyX-Code
2157
2158
2159\size small
2160#include <Rectangle.h> /* Rectangle ASN.1 type */
2161\layout LyX-Code
2162
2163
2164\layout LyX-Code
2165
2166
2167\size small
2168/*
2169\layout LyX-Code
2170
2171
2172\size small
2173 * This is a custom function which writes the
2174\layout LyX-Code
2175
2176
2177\size small
2178 * encoded output into some FILE stream.
2179\layout LyX-Code
2180
2181
2182\size small
2183 */
2184\layout LyX-Code
2185
2186
2187\size small
2188static int
2189\layout LyX-Code
2190
2191
2192\size small
2193write_out(const void *buffer, size_t size, void *app_key) {
2194\layout LyX-Code
2195
2196
2197\size small
2198 FILE *out_fp = app_key;
2199\layout LyX-Code
2200
2201
2202\size small
2203 size_t wrote;
2204\layout LyX-Code
2205
2206
2207\size small
2208
2209\layout LyX-Code
2210
2211
2212\size small
2213 wrote = fwrite(buffer, 1, size, out_fp);
2214\layout LyX-Code
2215
2216
2217\size small
2218
2219\layout LyX-Code
2220
2221
2222\size small
2223 return (wrote == size) ? 0 : -1;
2224\layout LyX-Code
2225
2226
2227\size small
2228}
2229\layout LyX-Code
2230
2231
2232\layout LyX-Code
2233
2234
2235\size small
vlm6c37dac2005-01-31 15:06:32 +00002236int main(int ac, char **av) {
vlma1fd7f92005-01-31 10:10:04 +00002237\layout LyX-Code
2238
2239
2240\size small
2241 Rectangle_t *rectangle; /* Type to encode */
2242\layout LyX-Code
2243
2244
2245\size small
2246 asn_enc_rval_t ec; /* Encoder return value */
2247\layout LyX-Code
2248
2249
2250\size small
2251
2252\layout LyX-Code
2253
2254
2255\size small
2256 /* Allocate the Rectangle_t */
2257\layout LyX-Code
2258
2259
2260\size small
vlmc7737f62005-02-02 09:53:46 +00002261 rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */
vlma1fd7f92005-01-31 10:10:04 +00002262\layout LyX-Code
2263
2264
2265\size small
2266 if(!rectangle) {
2267\layout LyX-Code
2268
2269
2270\size small
2271 perror(
2272\begin_inset Quotes sld
2273\end_inset
2274
2275calloc() failed
2276\begin_inset Quotes srd
2277\end_inset
2278
2279);
2280\layout LyX-Code
2281
2282
2283\size small
2284 exit(71); /* better, EX_OSERR */
2285\layout LyX-Code
2286
2287
2288\size small
2289 }
2290\layout LyX-Code
2291
2292
2293\size small
2294
2295\layout LyX-Code
2296
2297
2298\size small
2299 /* Initialize the Rectangle members */
2300\layout LyX-Code
2301
2302
2303\size small
2304 rectangle->height = 42; /* any random value */
2305\layout LyX-Code
2306
2307
2308\size small
2309 rectangle->width = 23; /* any random value */
2310\layout LyX-Code
2311
2312
2313\size small
2314
2315\layout LyX-Code
2316
2317
2318\size small
2319 /* BER encode the data if filename is given */
2320\layout LyX-Code
2321
2322
2323\size small
2324 if(ac < 2) {
2325\layout LyX-Code
2326
2327
2328\size small
2329 fprintf(stderr,
2330\begin_inset Quotes sld
2331\end_inset
2332
2333Specify filename for BER output
2334\backslash
2335n
2336\begin_inset Quotes srd
2337\end_inset
2338
2339);
2340\layout LyX-Code
2341
2342
2343\size small
2344 } else {
2345\layout LyX-Code
2346
2347
2348\size small
2349 const char *filename = av[1];
2350\layout LyX-Code
2351
2352
2353\size small
2354 FILE *fp = fopen(filename,
2355\begin_inset Quotes sld
2356\end_inset
2357
vlmdae8e612005-07-21 18:45:37 +00002358wb
vlma1fd7f92005-01-31 10:10:04 +00002359\begin_inset Quotes srd
2360\end_inset
2361
2362); /* for BER output */
2363\layout LyX-Code
2364
2365
2366\layout LyX-Code
2367
2368
2369\size small
2370 if(!fp) {
2371\layout LyX-Code
2372
2373
2374\size small
2375 perror(filename);
2376\layout LyX-Code
2377
2378
2379\size small
2380 exit(71); /* better, EX_OSERR */
2381\layout LyX-Code
2382
2383
2384\size small
2385 }
2386\layout LyX-Code
2387
2388
2389\size small
2390
2391\layout LyX-Code
2392
2393
2394\size small
2395 /* Encode the Rectangle type as BER (DER) */
2396\layout LyX-Code
2397
2398
2399\size small
2400 ec = der_encode(&asn_DEF_Rectangle,
2401\layout LyX-Code
2402
2403
2404\size small
vlm6c37dac2005-01-31 15:06:32 +00002405 rectangle, write_out, fp);
vlma1fd7f92005-01-31 10:10:04 +00002406\layout LyX-Code
2407
2408
2409\size small
2410 fclose(fp);
2411\layout LyX-Code
2412
2413
2414\size small
2415 if(ec.encoded == -1) {
2416\layout LyX-Code
2417
2418
2419\size small
2420 fprintf(stderr,
2421\layout LyX-Code
2422
2423
2424\size small
2425
2426\begin_inset Quotes sld
2427\end_inset
2428
2429Could not encode Rectangle (at %s)
2430\backslash
2431n
2432\begin_inset Quotes srd
2433\end_inset
2434
2435,
2436\layout LyX-Code
2437
2438
2439\size small
2440 ec.failed_type ? ec.failed_type->name :
2441\begin_inset Quotes sld
2442\end_inset
2443
2444unknown
2445\begin_inset Quotes srd
2446\end_inset
2447
2448);
2449\layout LyX-Code
2450
2451
2452\size small
2453 exit(65); /* better, EX_DATAERR */
2454\layout LyX-Code
2455
2456
2457\size small
2458 } else {
2459\layout LyX-Code
2460
2461
2462\size small
2463 fprintf(stderr,
2464\begin_inset Quotes sld
2465\end_inset
2466
2467Created %s with BER encoded Rectangle
2468\backslash
2469n
2470\begin_inset Quotes srd
2471\end_inset
2472
2473,
2474\layout LyX-Code
2475
2476
2477\size small
2478 filename);
2479\layout LyX-Code
2480
2481
2482\size small
2483 }
2484\layout LyX-Code
2485
2486
2487\size small
2488 }
2489\layout LyX-Code
2490
2491
2492\size small
2493
2494\layout LyX-Code
2495
2496
2497\size small
2498 /* Also print the constructed Rectangle XER encoded (XML) */
2499\layout LyX-Code
2500
2501
2502\size small
2503 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
2504\layout LyX-Code
2505
2506
2507\size small
2508
2509\layout LyX-Code
2510
2511
2512\size small
2513 return 0; /* Encoding finished successfully */
2514\layout LyX-Code
2515
2516
2517\size small
2518}
2519\end_deeper
2520\layout Enumerate
2521
2522Compile all files together using C compiler (varies by platform):
2523\begin_deeper
2524\layout LyX-Code
2525
2526
2527\emph on
2528cc -I.
2529 -o
2530\series bold
2531rencode
2532\series default
2533 *.c
2534\end_deeper
2535\layout Enumerate
2536
2537Voila! You have just created the BER and XER encoder of a Rectangle type,
2538 named
2539\series bold
2540rencode
2541\series default
2542!
2543\layout Standard
2544
2545
2546\begin_inset ERT
2547status Collapsed
2548
2549\layout Standard
2550
2551\backslash
2552clearpage{}
2553\end_inset
2554
2555
2556\layout Section
2557
vlmc7737f62005-02-02 09:53:46 +00002558
2559\begin_inset LatexCommand \label{sec:A-Rectangle-Decoder}
2560
2561\end_inset
2562
vlma1fd7f92005-01-31 10:10:04 +00002563A
vlm4cb0edf2005-01-14 11:38:49 +00002564\begin_inset Quotes sld
2565\end_inset
2566
2567Rectangle
2568\begin_inset Quotes srd
2569\end_inset
2570
2571 Decoder
2572\layout Standard
2573
vlmc7737f62005-02-02 09:53:46 +00002574This example will help you to create a simple BER decoder of a simple
vlm4cb0edf2005-01-14 11:38:49 +00002575\begin_inset Quotes sld
2576\end_inset
2577
2578Rectangle
2579\begin_inset Quotes srd
2580\end_inset
2581
2582 type used throughout this document.
2583\layout Enumerate
2584
2585Create a file named
2586\series bold
2587rectangle.asn1
2588\series default
2589 with the following contents:
2590\begin_deeper
2591\layout LyX-Code
2592
2593RectangleModule1 DEFINITIONS ::=
2594\layout LyX-Code
2595
2596BEGIN
2597\layout LyX-Code
2598
2599
2600\layout LyX-Code
2601
2602Rectangle ::= SEQUENCE {
2603\layout LyX-Code
2604
2605 height INTEGER,
2606\layout LyX-Code
2607
2608 width INTEGER
2609\layout LyX-Code
2610
2611}
2612\layout LyX-Code
2613
2614
2615\layout LyX-Code
2616
2617END
2618\end_deeper
2619\layout Enumerate
2620
2621Compile it into the set of .c and .h files using asn1c compiler
2622\begin_inset LatexCommand \cite{ASN1C}
2623
2624\end_inset
2625
2626:
2627\begin_deeper
2628\layout LyX-Code
2629
2630
2631\emph on
2632asn1c -fnative-types
2633\series bold
2634\emph default
2635rectangle.asn1
2636\end_deeper
2637\layout Enumerate
2638
2639Alternatively, use the Online ASN.1 compiler
2640\begin_inset LatexCommand \cite{AONL}
2641
2642\end_inset
2643
2644 by uploading the
2645\series bold
2646rectangle.asn1
2647\series default
vlmabd51e22005-01-17 11:05:04 +00002648 file into the Web form and unpacking the produced archive on your computer.
vlm4cb0edf2005-01-14 11:38:49 +00002649\layout Enumerate
2650
vlmabd51e22005-01-17 11:05:04 +00002651By this time, you should have gotten multiple files in the current directory,
2652 including the
2653\series bold
2654Rectangle.c
2655\series default
2656 and
2657\series bold
2658Rectangle.h
2659\series default
2660.
vlm4cb0edf2005-01-14 11:38:49 +00002661\layout Enumerate
2662
2663Create a main() routine which takes the binary input file, decodes it as
vlmabd51e22005-01-17 11:05:04 +00002664 it were a BER-encoded Rectangle type, and prints out the text (XML) representat
2665ion of the Rectangle type.
vlm4cb0edf2005-01-14 11:38:49 +00002666 Let's name the file
2667\series bold
2668main.c
2669\series default
2670:
vlmabd51e22005-01-17 11:05:04 +00002671\begin_inset ERT
vlmc7737f62005-02-02 09:53:46 +00002672status Collapsed
vlmabd51e22005-01-17 11:05:04 +00002673
2674\layout Standard
2675
2676\backslash
2677clearpage{}
2678\end_inset
2679
2680
vlm4cb0edf2005-01-14 11:38:49 +00002681\begin_deeper
2682\layout LyX-Code
2683
2684
2685\size small
2686#include <stdio.h>
2687\layout LyX-Code
2688
2689
2690\size small
2691#include <sys/types.h>
2692\layout LyX-Code
2693
2694
2695\size small
2696#include <Rectangle.h> /* Rectangle ASN.1 type */
2697\layout LyX-Code
2698
2699
2700\size small
2701
2702\layout LyX-Code
2703
2704
2705\size small
2706int main(int ac, char **av) {
2707\layout LyX-Code
2708
2709
2710\size small
2711 char buf[1024]; /* Temporary buffer */
2712\layout LyX-Code
2713
2714
2715\size small
2716 Rectangle_t *rectangle = 0; /* Type to decode */
2717\layout LyX-Code
2718
2719
2720\size small
2721 asn_dec_rval_t rval; /* Decoder return value */
2722\layout LyX-Code
2723
2724
2725\size small
2726 FILE *fp; /* Input file handler */
2727\layout LyX-Code
2728
2729
2730\size small
2731 size_t size; /* Number of bytes read */
2732\layout LyX-Code
2733
2734
2735\size small
2736 char *filename; /* Input file name */
2737\layout LyX-Code
2738
2739
2740\size small
2741
2742\layout LyX-Code
2743
2744
2745\size small
2746 /* Require a single filename argument */
2747\layout LyX-Code
2748
2749
2750\size small
2751 if(ac != 2) {
2752\layout LyX-Code
2753
2754
2755\size small
2756 fprintf(stderr,
2757\begin_inset Quotes sld
2758\end_inset
2759
2760Usage: %s <file.ber>
2761\backslash
2762n
2763\begin_inset Quotes srd
2764\end_inset
2765
2766, av[0]);
2767\layout LyX-Code
2768
2769
2770\size small
2771 exit(64); /* better, EX_USAGE */
2772\layout LyX-Code
2773
2774
2775\size small
2776 } else {
2777\layout LyX-Code
2778
2779
2780\size small
2781 filename = av[1];
2782\layout LyX-Code
2783
2784
2785\size small
2786 }
2787\layout LyX-Code
2788
2789
2790\size small
2791
2792\layout LyX-Code
2793
2794
2795\size small
2796 /* Open input file as read-only binary */
2797\layout LyX-Code
2798
2799
2800\size small
2801 fp = fopen(filename,
2802\begin_inset Quotes sld
2803\end_inset
2804
2805rb
2806\begin_inset Quotes srd
2807\end_inset
2808
2809);
2810\layout LyX-Code
2811
2812
2813\size small
2814 if(!fp) {
2815\layout LyX-Code
2816
2817
2818\size small
2819 perror(filename);
2820\layout LyX-Code
2821
2822
2823\size small
2824 exit(66); /* better, EX_NOINPUT */
2825\layout LyX-Code
2826
2827
2828\size small
2829 }
2830\layout LyX-Code
2831
2832
2833\size small
2834
2835\layout LyX-Code
2836
2837
2838\size small
2839 /* Read up to the buffer size */
2840\layout LyX-Code
2841
2842
2843\size small
2844 size = fread(buf, 1, sizeof(buf), fp);
2845\layout LyX-Code
2846
2847
2848\size small
2849 fclose(fp);
2850\layout LyX-Code
2851
2852
2853\size small
2854 if(!size) {
2855\layout LyX-Code
2856
2857
2858\size small
2859 fprintf(stderr,
2860\begin_inset Quotes sld
2861\end_inset
2862
2863%s: Empty or broken
2864\backslash
2865n
2866\begin_inset Quotes srd
2867\end_inset
2868
2869, filename);
2870\layout LyX-Code
2871
2872
2873\size small
2874 exit(65); /* better, EX_DATAERR */
2875\layout LyX-Code
2876
2877
2878\size small
2879 }
2880\layout LyX-Code
2881
2882
2883\size small
2884
2885\layout LyX-Code
2886
2887
2888\size small
2889 /* Decode the input buffer as Rectangle type */
2890\layout LyX-Code
2891
2892
2893\size small
2894 rval = ber_decode(0, &asn_DEF_Rectangle,
2895\layout LyX-Code
2896
2897
2898\size small
2899 (void **)&rectangle, buf, size);
2900\layout LyX-Code
2901
2902
2903\size small
2904 if(rval.code != RC_OK) {
2905\layout LyX-Code
2906
2907
2908\size small
2909 fprintf(stderr,
2910\layout LyX-Code
2911
2912
2913\size small
2914
2915\begin_inset Quotes sld
2916\end_inset
2917
2918%s: Broken Rectangle encoding at byte %ld
2919\backslash
2920n
2921\begin_inset Quotes srd
2922\end_inset
2923
2924,
2925\layout LyX-Code
2926
2927
2928\size small
2929 filename, (long)rval.consumed);
2930\layout LyX-Code
2931
2932
2933\size small
2934 exit(65); /* better, EX_DATAERR */
2935\layout LyX-Code
2936
2937
2938\size small
2939 }
2940\layout LyX-Code
2941
2942
2943\size small
2944
2945\layout LyX-Code
2946
2947
2948\size small
2949 /* Print the decoded Rectangle type as XML */
2950\layout LyX-Code
2951
2952
2953\size small
2954 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
2955\layout LyX-Code
2956
2957
2958\size small
2959
2960\layout LyX-Code
2961
2962
2963\size small
2964 return 0; /* Decoding finished successfully */
2965\layout LyX-Code
2966
2967
2968\size small
2969}
2970\end_deeper
2971\layout Enumerate
2972
vlmabd51e22005-01-17 11:05:04 +00002973Compile all files together using C compiler (varies by platform):
vlm4cb0edf2005-01-14 11:38:49 +00002974\begin_deeper
2975\layout LyX-Code
2976
2977
2978\emph on
2979cc -I.
vlmabd51e22005-01-17 11:05:04 +00002980 -o
2981\series bold
2982rdecode
2983\series default
2984 *.c
vlm4cb0edf2005-01-14 11:38:49 +00002985\end_deeper
2986\layout Enumerate
2987
vlma1fd7f92005-01-31 10:10:04 +00002988Voila! You have just created the BER decoder of a Rectangle type, named
2989
vlm4cb0edf2005-01-14 11:38:49 +00002990\series bold
2991rdecode
2992\series default
2993!
vlmc7737f62005-02-02 09:53:46 +00002994\layout Chapter
2995
2996Constraint validation examples
2997\layout Standard
2998
2999This chapter shows how to define ASN.1 constraints and use the generated
3000 validation code.
3001\layout Section
3002
3003Adding constraints into
3004\begin_inset Quotes sld
3005\end_inset
3006
3007Rectangle
3008\begin_inset Quotes srd
3009\end_inset
3010
3011 type
3012\layout Standard
3013
3014This example shows how to add basic constraints to the ASN.1 specification
3015 and how to invoke the constraints validation code in your application.
3016\layout Enumerate
3017
3018Create a file named
3019\series bold
3020rectangle.asn1
3021\series default
3022 with the following contents:
3023\begin_deeper
3024\layout LyX-Code
3025
3026RectangleModuleWithConstraints DEFINITIONS ::=
3027\layout LyX-Code
3028
3029BEGIN
3030\layout LyX-Code
3031
3032
3033\layout LyX-Code
3034
3035Rectangle ::= SEQUENCE {
3036\layout LyX-Code
3037
3038 height INTEGER (0..100), -- Value range constraint
3039\layout LyX-Code
3040
3041 width INTEGER (0..MAX) -- Makes width non-negative
3042\layout LyX-Code
3043
3044}
3045\layout LyX-Code
3046
3047
3048\layout LyX-Code
3049
3050END
3051\end_deeper
3052\layout Enumerate
3053
3054Compile the file according to procedures shown in the previous chapter.
3055\layout Enumerate
3056
3057Modify the Rectangle type processing routine (you can start with the main()
3058 routine shown in the Section
3059\begin_inset LatexCommand \vref{sec:A-Rectangle-Decoder}
3060
3061\end_inset
3062
3063) by placing the following snippet of code
3064\emph on
3065before
3066\emph default
3067 encoding and/or
3068\emph on
3069after
3070\emph default
3071 decoding the Rectangle type
3072\begin_inset Foot
3073collapsed true
3074
3075\layout Standard
3076
3077Placing the constraint checking code
3078\emph on
3079before
3080\emph default
3081 encoding helps to make sure you know the data is correct and within constraints
3082 before sharing the data with anyone else.
3083\layout Standard
3084
3085Placing the constraint checking code
3086\emph on
3087after
3088\emph default
3089 decoding, but before any further action depending on the decoded data,
3090 helps to make sure the application got the valid contents before making
3091 use of it.
3092\end_inset
3093
3094:
3095\begin_inset ERT
3096status Collapsed
3097
3098\layout Standard
3099
3100\backslash
3101clearpage{}
3102\end_inset
3103
3104
3105\begin_deeper
3106\layout LyX-Code
3107
3108
3109\size small
3110int ret; /* Return value */
3111\layout LyX-Code
3112
3113
3114\size small
3115char errbuf[128]; /* Buffer for error message */
3116\layout LyX-Code
3117
3118
3119\size small
3120size_t errlen = sizeof(errbuf); /* Size of the buffer */
3121\layout LyX-Code
3122
3123
3124\size small
3125
3126\layout LyX-Code
3127
3128
3129\size small
3130/* ...
3131 here may go Rectangle decoding code ...
3132 */
3133\layout LyX-Code
3134
3135
3136\size small
3137
3138\layout LyX-Code
3139
3140
3141\size small
vlm823f1442005-08-29 10:23:20 +00003142ret = asn_check_constraints(&asn_DEF_Rectangle,
vlmc7737f62005-02-02 09:53:46 +00003143\layout LyX-Code
3144
3145
3146\size small
3147 rectangle, errbuf, &errlen);
3148\layout LyX-Code
3149
3150
3151\size small
3152/* assert(errlen < sizeof(errbuf)); // you may rely on that */
3153\layout LyX-Code
3154
3155
3156\size small
3157if(ret) {
3158\layout LyX-Code
3159
3160
3161\size small
3162 fprintf(stderr,
3163\begin_inset Quotes sld
3164\end_inset
3165
3166Constraint validation failed: %s
3167\backslash
3168n
3169\begin_inset Quotes srd
3170\end_inset
3171
3172,
3173\layout LyX-Code
3174
3175
3176\size small
3177 errbuf /* errbuf is properly nul-terminated */
3178\layout LyX-Code
3179
3180
3181\size small
3182 );
3183\layout LyX-Code
3184
3185
3186\size small
3187 /* exit(...); // Replace with appropriate action */
3188\layout LyX-Code
3189
3190
3191\size small
3192}
3193\layout LyX-Code
3194
3195
3196\size small
3197
3198\layout LyX-Code
3199
3200
3201\size small
3202/* ...
3203 here may go Rectangle encoding code ...
3204 */
3205\end_deeper
3206\layout Enumerate
3207
3208Compile the resulting C code as shown in the previous chapters.
3209\layout Enumerate
3210
vlm602b7262005-02-02 11:06:38 +00003211Try to test the constraints checking code by assigning integer value 101
3212 to the
3213\series bold
3214.height
3215\series default
3216 member of the Rectangle structure, or a negative value to the
3217\series bold
3218.width
3219\series default
3220 member.
3221 In either case, the program should print
3222\begin_inset Quotes sld
3223\end_inset
3224
3225Constraint validation failed
3226\begin_inset Quotes srd
3227\end_inset
3228
3229 message, followed by the short explanation why validation did not succeed.
3230\layout Enumerate
3231
vlmc7737f62005-02-02 09:53:46 +00003232Done.
vlm856978f2005-02-21 14:16:10 +00003233\layout Part
3234
3235
3236\begin_inset LatexCommand \label{par:ASN.1-Basics}
3237
3238\end_inset
3239
3240ASN.1 Basics
3241\layout Chapter
3242
3243
3244\begin_inset LatexCommand \label{cha:Abstract-Syntax-Notation:}
3245
3246\end_inset
3247
3248Abstract Syntax Notation: ASN.1
3249\layout Standard
3250
3251
3252\emph on
3253This chapter defines some basic ASN.1 concepts and describes several most
3254 widely used types.
3255 It is by no means an authoritative or complete reference.
3256 For more complete ASN.1 description, please refer to Olivier Dubuisson's
3257 book
3258\begin_inset LatexCommand \cite{Dub00}
3259
3260\end_inset
3261
3262 or the ASN.1 body of standards itself
3263\begin_inset LatexCommand \cite{ITU-T/ASN.1}
3264
3265\end_inset
3266
3267.
3268\layout Standard
3269
3270The Abstract Syntax Notation One is used to formally describe the semantics
3271 of data transmitted across the network.
3272 Two communicating parties may have different formats of their native data
3273 types (i.e.
3274 number of bits in the integer type), thus it is important to have a way
3275 to describe the data in a manner which is independent from the particular
3276 machine's representation.
3277 The ASN.1 specifications are used to achieve the following:
3278\layout Itemize
3279
3280The specification expressed in the ASN.1 notation is a formal and precise
3281 way to communicate the data semantics to human readers;
3282\layout Itemize
3283
3284The ASN.1 specifications may be used as input for automatic compilers which
3285 produce the code for some target language (C, C++, Java, etc) to encode
3286 and decode the data according to some encoding rules (which are also defined
3287 by the ASN.1 standard).
3288\layout Standard
3289
3290Consider the following example:
3291\layout LyX-Code
3292
3293Rectangle ::= SEQUENCE {
3294\layout LyX-Code
3295
3296 height INTEGER,
3297\layout LyX-Code
3298
3299 width INTEGER
3300\layout LyX-Code
3301
3302}
3303\layout Standard
3304
3305This ASN.1 specification describes a constructed type,
3306\emph on
3307Rectangle
3308\emph default
3309, containing two integer fields.
3310 This specification may tell the reader that there exists this kind of data
3311 structure and that some entity may be prepared to send or receive it.
3312 The question on
3313\emph on
3314how
3315\emph default
3316 that entity is going to send or receive the
3317\emph on
3318encoded data
3319\emph default
3320 is outside the scope of ASN.1.
3321 For example, this data structure may be encoded according to some encoding
3322 rules and sent to the destination using the TCP protocol.
3323 The ASN.1 specifies several ways of encoding (or
3324\begin_inset Quotes sld
3325\end_inset
3326
3327serializing
3328\begin_inset Quotes srd
3329\end_inset
3330
3331, or
3332\begin_inset Quotes sld
3333\end_inset
3334
3335marshaling
3336\begin_inset Quotes srd
3337\end_inset
3338
3339) the data: BER, PER, XER and others, including CER and DER derivatives
3340 from BER.
3341\layout Standard
3342
3343The complete specification must be wrapped in a module, which looks like
3344 this:
3345\layout LyX-Code
3346
3347RectangleModule1
3348\layout LyX-Code
3349
3350 { iso org(3) dod(6) internet(1) private(4)
3351\layout LyX-Code
3352
3353 enterprise(1) spelio(9363) software(1)
3354\layout LyX-Code
3355
3356 asn1c(5) docs(2) rectangle(1) 1 }
3357\layout LyX-Code
3358
3359 DEFINITIONS AUTOMATIC TAGS ::=
3360\layout LyX-Code
3361
3362BEGIN
3363\layout LyX-Code
3364
3365
3366\layout LyX-Code
3367
3368-- This is a comment which describes nothing.
3369\layout LyX-Code
3370
3371Rectangle ::= SEQUENCE {
3372\layout LyX-Code
3373
3374 height INTEGER, -- Height of the rectangle
3375\layout LyX-Code
3376
3377 width INTEGER -- Width of the rectangle
3378\layout LyX-Code
3379
3380}
3381\layout LyX-Code
3382
3383
3384\layout LyX-Code
3385
3386END
3387\layout Standard
3388
3389The module header consists of module name (RectangleModule1), the module
3390 object identifier ({...}), a keyword
3391\begin_inset Quotes sld
3392\end_inset
3393
3394DEFINITIONS
3395\begin_inset Quotes srd
3396\end_inset
3397
3398, a set of module flags (AUTOMATIC TAGS) and
3399\begin_inset Quotes sld
3400\end_inset
3401
3402::= BEGIN
3403\begin_inset Quotes srd
3404\end_inset
3405
3406.
3407 The module ends with an
3408\begin_inset Quotes sld
3409\end_inset
3410
3411END
3412\begin_inset Quotes srd
3413\end_inset
3414
3415 statement.
3416\layout Section
3417
3418Some of the ASN.1 Basic Types
3419\layout Subsection
3420
3421The BOOLEAN type
3422\layout Standard
3423
3424The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF or
3425 a similar kind of two-way choice.
3426\layout Subsection
3427
3428The INTEGER type
3429\layout Standard
3430
3431The INTEGER type is a signed natural number type without any restrictions
3432 on its size.
3433 If the automatic checking on INTEGER value bounds are necessary, the subtype
3434 constraints must be used.
3435\layout LyX-Code
3436
3437SimpleInteger ::= INTEGER
3438\layout LyX-Code
3439
3440
3441\layout LyX-Code
3442
3443-- An integer with a very limited range
3444\layout LyX-Code
3445
3446SmallPositiveInt ::= INTEGER (0..127)
3447\layout LyX-Code
3448
3449
3450\layout LyX-Code
3451
3452-- Integer, negative
3453\layout LyX-Code
3454
3455NegativeInt ::= INTEGER (MIN..0)
3456\layout Subsection
3457
3458The ENUMERATED type
3459\layout Standard
3460
3461The ENUMERATED type is semantically equivalent to the INTEGER type with
3462 some integer values explicitly named.
3463\layout LyX-Code
3464
3465FruitId ::= ENUMERATED { apple(1), orange(2) }
3466\layout LyX-Code
3467
3468
3469\layout LyX-Code
3470
3471-- The numbers in braces are optional,
3472\layout LyX-Code
3473
3474-- the enumeration can be performed
3475\layout LyX-Code
3476
3477-- automatically by the compiler
3478\layout LyX-Code
3479
3480ComputerOSType ::= ENUMERATED {
3481\layout LyX-Code
3482
3483 FreeBSD, -- acquires value 0
3484\layout LyX-Code
3485
3486 Windows, -- acquires value 1
3487\layout LyX-Code
3488
3489 Solaris(5), -- remains 5
3490\layout LyX-Code
3491
3492 Linux, -- becomes 6
3493\layout LyX-Code
3494
3495 MacOS -- becomes 7
3496\layout LyX-Code
3497
3498}
3499\layout Subsection
3500
3501The OCTET STRING type
3502\layout Standard
3503
3504This type models the sequence of 8-bit bytes.
3505 This may be used to transmit some opaque data or data serialized by other
3506 types of encoders (i.e.
3507 video file, photo picture, etc).
3508\layout Subsection
3509
3510The OBJECT IDENTIFIER type
3511\layout Standard
3512
3513The OBJECT IDENTIFIER is used to represent the unique identifier of any
3514 object, starting from the very root of the registration tree.
3515 If your organization needs to uniquely identify something (a router, a
3516 room, a person, a standard, or whatever), you are encouraged to get your
3517 own identification subtree at
3518\begin_inset LatexCommand \htmlurl{http://www.iana.org/protocols/forms.htm}
3519
3520\end_inset
3521
3522.
3523\layout Standard
3524
3525For example, the very first ASN.1 module in this Chapter (RectangleModule1)
3526 has the following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
3527\layout LyX-Code
3528
3529ExampleOID ::= OBJECT IDENTIFIER
3530\layout LyX-Code
3531
3532
3533\layout LyX-Code
3534
3535rectangleModule1-oid ExampleOID
3536\layout LyX-Code
3537
3538 ::= { 1 3 6 1 4 1 9363 1 5 2 1 1 }
3539\layout LyX-Code
3540
3541
3542\layout LyX-Code
3543
3544-- An identifier of the Internet.
3545\layout LyX-Code
3546
3547internet-id OBJECT IDENTIFIER
3548\layout LyX-Code
3549
3550 ::= { iso(1) identified-organization(3)
3551\layout LyX-Code
3552
3553 dod(6) internet(1) }
3554\layout Standard
3555
3556As you see, names are optional.
3557\layout Subsection
3558
3559The RELATIVE-OID type
3560\layout Standard
3561
3562The RELATIVE-OID type has the semantics of a subtree of an OBJECT IDENTIFIER.
3563 There may be no need to repeat the whole sequence of numbers from the root
3564 of the registration tree where the only thing of interest is some of the
3565 tree's subsequence.
3566\layout LyX-Code
3567
3568this-document RELATIVE-OID ::= { docs(2) usage(1) }
3569\layout LyX-Code
3570
3571
3572\layout LyX-Code
3573
3574this-example RELATIVE-OID ::= {
3575\layout LyX-Code
3576
3577 this-document assorted-examples(0) this-example(1) }
3578\layout Section
3579
3580Some of the ASN.1 String Types
3581\layout Subsection
3582
3583The IA5String type
3584\layout Standard
3585
3586This is essentially the ASCII, with 128 character codes available (7 lower
3587 bits of an 8-bit byte).
3588\layout Subsection
3589
3590The UTF8String type
3591\layout Standard
3592
3593This is the character string which encodes the full Unicode range (4 bytes)
3594 using multibyte character sequences.
3595\layout Subsection
3596
3597The NumericString type
3598\layout Standard
3599
3600This type represents the character string with the alphabet consisting of
3601 numbers (
3602\begin_inset Quotes sld
3603\end_inset
3604
36050
3606\begin_inset Quotes srd
3607\end_inset
3608
3609 to
3610\begin_inset Quotes sld
3611\end_inset
3612
36139
3614\begin_inset Quotes srd
3615\end_inset
3616
3617) and a space.
3618\layout Subsection
3619
3620The PrintableString type
3621\layout Standard
3622
3623The character string with the following alphabet: space,
3624\begin_inset Quotes sld
3625\end_inset
3626
3627
3628\series bold
3629'
3630\series default
3631
3632\begin_inset Quotes srd
3633\end_inset
3634
3635 (single quote),
3636\begin_inset Quotes sld
3637\end_inset
3638
3639
3640\series bold
3641(
3642\series default
3643
3644\begin_inset Quotes sld
3645\end_inset
3646
3647,
3648\begin_inset Quotes sld
3649\end_inset
3650
3651
3652\series bold
3653)
3654\series default
3655
3656\begin_inset Quotes srd
3657\end_inset
3658
3659,
3660\begin_inset Quotes sld
3661\end_inset
3662
3663
3664\series bold
3665+
3666\series default
3667
3668\begin_inset Quotes srd
3669\end_inset
3670
3671,
3672\begin_inset Quotes sld
3673\end_inset
3674
3675
3676\series bold
3677,
3678\series default
3679
3680\begin_inset Quotes srd
3681\end_inset
3682
3683 (comma),
3684\begin_inset Quotes sld
3685\end_inset
3686
3687
3688\series bold
3689-
3690\series default
3691
3692\begin_inset Quotes srd
3693\end_inset
3694
3695,
3696\begin_inset Quotes sld
3697\end_inset
3698
3699
3700\series bold
3701.
3702\series default
3703
3704\begin_inset Quotes srd
3705\end_inset
3706
3707,
3708\begin_inset Quotes sld
3709\end_inset
3710
3711
3712\series bold
3713/
3714\series default
3715
3716\begin_inset Quotes srd
3717\end_inset
3718
3719, digits (
3720\begin_inset Quotes sld
3721\end_inset
3722
37230
3724\begin_inset Quotes srd
3725\end_inset
3726
3727 to
3728\begin_inset Quotes sld
3729\end_inset
3730
37319
3732\begin_inset Quotes srd
3733\end_inset
3734
3735),
3736\begin_inset Quotes sld
3737\end_inset
3738
3739
3740\series bold
3741:
3742\series default
3743
3744\begin_inset Quotes srd
3745\end_inset
3746
3747,
3748\begin_inset Quotes sld
3749\end_inset
3750
3751
3752\series bold
3753=
3754\series default
3755
3756\begin_inset Quotes srd
3757\end_inset
3758
3759,
3760\begin_inset Quotes sld
3761\end_inset
3762
3763
3764\series bold
3765?
3766\series default
3767
3768\begin_inset Quotes srd
3769\end_inset
3770
3771, upper-case and lower-case letters (
3772\begin_inset Quotes sld
3773\end_inset
3774
3775A
3776\begin_inset Quotes srd
3777\end_inset
3778
3779 to
3780\begin_inset Quotes sld
3781\end_inset
3782
3783Z
3784\begin_inset Quotes srd
3785\end_inset
3786
3787 and
3788\begin_inset Quotes sld
3789\end_inset
3790
3791a
3792\begin_inset Quotes srd
3793\end_inset
3794
3795 to
3796\begin_inset Quotes sld
3797\end_inset
3798
3799z
3800\begin_inset Quotes srd
3801\end_inset
3802
3803).
3804\layout Subsection
3805
3806The VisibleString type
3807\layout Standard
3808
3809The character string with the alphabet which is more or less a subset of
3810 ASCII between the space and the
3811\begin_inset Quotes sld
3812\end_inset
3813
3814
3815\series bold
3816~
3817\series default
3818
3819\begin_inset Quotes srd
3820\end_inset
3821
3822 symbol (tilde).
3823\layout Standard
3824
3825Alternatively, the alphabet may be described as the PrintableString alphabet
3826 presented earlier, plus the following characters:
3827\begin_inset Quotes sld
3828\end_inset
3829
3830
3831\series bold
3832!
3833\series default
3834
3835\begin_inset Quotes srd
3836\end_inset
3837
3838,
3839\begin_inset Quotes sld
3840\end_inset
3841
3842
3843\series bold
3844
3845\begin_inset Quotes srd
3846\end_inset
3847
3848
3849\series default
3850
3851\begin_inset Quotes srd
3852\end_inset
3853
3854,
3855\begin_inset Quotes sld
3856\end_inset
3857
3858
3859\series bold
3860#
3861\series default
3862
3863\begin_inset Quotes srd
3864\end_inset
3865
3866,
3867\begin_inset Quotes sld
3868\end_inset
3869
3870
3871\series bold
3872$
3873\series default
3874
3875\begin_inset Quotes srd
3876\end_inset
3877
3878,
3879\begin_inset Quotes sld
3880\end_inset
3881
3882
3883\series bold
3884%
3885\series default
3886
3887\begin_inset Quotes srd
3888\end_inset
3889
3890,
3891\begin_inset Quotes sld
3892\end_inset
3893
3894
3895\series bold
3896&
3897\series default
3898
3899\begin_inset Quotes srd
3900\end_inset
3901
3902,
3903\begin_inset Quotes sld
3904\end_inset
3905
3906
3907\series bold
3908*
3909\series default
3910
3911\begin_inset Quotes srd
3912\end_inset
3913
3914,
3915\begin_inset Quotes sld
3916\end_inset
3917
3918
3919\series bold
3920;
3921\series default
3922
3923\begin_inset Quotes srd
3924\end_inset
3925
3926,
3927\begin_inset Quotes sld
3928\end_inset
3929
3930
3931\series bold
3932<
3933\series default
3934
3935\begin_inset Quotes srd
3936\end_inset
3937
3938,
3939\begin_inset Quotes sld
3940\end_inset
3941
3942
3943\series bold
3944>
3945\series default
3946
3947\begin_inset Quotes srd
3948\end_inset
3949
3950,
3951\begin_inset Quotes sld
3952\end_inset
3953
3954
3955\series bold
3956[
3957\series default
3958
3959\begin_inset Quotes srd
3960\end_inset
3961
3962,
3963\begin_inset Quotes sld
3964\end_inset
3965
3966
3967\series bold
3968
3969\backslash
3970
3971\series default
3972
3973\begin_inset Quotes srd
3974\end_inset
3975
3976,
3977\begin_inset Quotes sld
3978\end_inset
3979
3980
3981\series bold
3982]
3983\series default
3984
3985\begin_inset Quotes srd
3986\end_inset
3987
3988,
3989\begin_inset Quotes sld
3990\end_inset
3991
3992
3993\series bold
3994^
3995\series default
3996
3997\begin_inset Quotes srd
3998\end_inset
3999
4000,
4001\begin_inset Quotes sld
4002\end_inset
4003
4004
4005\series bold
4006_
4007\series default
4008
4009\begin_inset Quotes srd
4010\end_inset
4011
4012,
4013\begin_inset Quotes sld
4014\end_inset
4015
4016
4017\series bold
4018`
4019\series default
4020
4021\begin_inset Quotes srd
4022\end_inset
4023
4024 (single left quote),
4025\begin_inset Quotes sld
4026\end_inset
4027
4028
4029\series bold
4030{
4031\series default
4032
4033\begin_inset Quotes srd
4034\end_inset
4035
4036,
4037\begin_inset Quotes sld
4038\end_inset
4039
4040
4041\series bold
4042|
4043\series default
4044
4045\begin_inset Quotes srd
4046\end_inset
4047
4048,
4049\begin_inset Quotes sld
4050\end_inset
4051
4052
4053\series bold
4054}
4055\series default
4056
4057\begin_inset Quotes srd
4058\end_inset
4059
4060,
4061\begin_inset Quotes sld
4062\end_inset
4063
4064
4065\series bold
4066~
4067\series default
4068
4069\begin_inset Quotes srd
4070\end_inset
4071
4072.
4073\layout Section
4074
4075ASN.1 Constructed Types
4076\layout Subsection
4077
4078The SEQUENCE type
4079\layout Standard
4080
4081This is an ordered collection of other simple or constructed types.
4082 The SEQUENCE constructed type resembles the C
4083\begin_inset Quotes sld
4084\end_inset
4085
4086struct
4087\begin_inset Quotes srd
4088\end_inset
4089
4090 statement.
4091\layout LyX-Code
4092
4093Address ::= SEQUENCE {
4094\layout LyX-Code
4095
4096 -- The apartment number may be omitted
4097\layout LyX-Code
4098
4099 apartmentNumber NumericString OPTIONAL,
4100\layout LyX-Code
4101
4102 streetName PrintableString,
4103\layout LyX-Code
4104
4105 cityName PrintableString,
4106\layout LyX-Code
4107
4108 stateName PrintableString,
4109\layout LyX-Code
4110
4111 -- This one may be omitted too
4112\layout LyX-Code
4113
4114 zipNo NumericString OPTIONAL
4115\layout LyX-Code
4116
4117}
4118\layout Subsection
4119
4120The SET type
4121\layout Standard
4122
4123This is a collection of other simple or constructed types.
4124 Ordering is not important.
4125 The data may arrive in the order which is different from the order of specifica
4126tion.
4127 Data is encoded in the order not necessarily corresponding to the order
4128 of specification.
4129\layout Subsection
4130
4131The CHOICE type
4132\layout Standard
4133
4134This type is just a choice between the subtypes specified in it.
4135 The CHOICE type contains at most one of the subtypes specified, and it
4136 is always implicitly known which choice is being decoded or encoded.
4137 This one resembles the C
4138\begin_inset Quotes sld
4139\end_inset
4140
4141union
4142\begin_inset Quotes srd
4143\end_inset
4144
4145 statement.
4146\layout Standard
4147
4148The following type defines a response code, which may be either an integer
4149 code or a boolean
4150\begin_inset Quotes sld
4151\end_inset
4152
4153true
4154\begin_inset Quotes srd
4155\end_inset
4156
4157/
4158\begin_inset Quotes srd
4159\end_inset
4160
4161false
4162\begin_inset Quotes srd
4163\end_inset
4164
4165 code.
4166\layout LyX-Code
4167
4168ResponseCode ::= CHOICE {
4169\layout LyX-Code
4170
4171 intCode INTEGER,
4172\layout LyX-Code
4173
4174 boolCode BOOLEAN
4175\layout LyX-Code
4176
4177}
4178\layout LyX-Code
4179
4180\layout Subsection
4181
4182The SEQUENCE OF type
4183\layout Standard
4184
4185This one is the list (array) of simple or constructed types:
4186\layout LyX-Code
4187
4188-- Example 1
4189\layout LyX-Code
4190
4191ManyIntegers ::= SEQUENCE OF INTEGER
4192\layout LyX-Code
4193
4194
4195\layout LyX-Code
4196
4197-- Example 2
4198\layout LyX-Code
4199
4200ManyRectangles ::= SEQUENCE OF Rectangle
4201\layout LyX-Code
4202
4203
4204\layout LyX-Code
4205
4206-- More complex example:
4207\layout LyX-Code
4208
4209-- an array of structures defined in place.
4210\layout LyX-Code
4211
4212ManyCircles ::= SEQUENCE OF SEQUENCE {
4213\layout LyX-Code
4214
4215 radius INTEGER
4216\layout LyX-Code
4217
4218 }
4219\layout Subsection
4220
4221The SET OF type
4222\layout Standard
4223
4224The SET OF type models the bag of structures.
4225 It resembles the SEQUENCE OF type, but the order is not important: i.e.
4226 the elements may arrive in the order which is not necessarily the same
4227 as the in-memory order on the remote machines.
4228\layout LyX-Code
4229
4230-- A set of structures defined elsewhere
4231\layout LyX-Code
4232
4233SetOfApples :: SET OF Apple
4234\layout LyX-Code
4235
4236
4237\layout LyX-Code
4238
4239-- Set of integers encoding the kind of a fruit
4240\layout LyX-Code
4241
4242FruitBag ::= SET OF ENUMERATED { apple, orange }
vlme18adea2004-06-03 03:49:45 +00004243\layout Bibliography
vlmfcec8e52004-08-23 15:12:04 +00004244\bibitem [ASN1C]{ASN1C}
4245
vlmc7737f62005-02-02 09:53:46 +00004246The Open Source ASN.1 Compiler.
vlmfcec8e52004-08-23 15:12:04 +00004247
vlmc7737f62005-02-02 09:53:46 +00004248\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c}
vlmfcec8e52004-08-23 15:12:04 +00004249
4250\end_inset
4251
4252
4253\layout Bibliography
vlm4cb0edf2005-01-14 11:38:49 +00004254\bibitem [AONL]{AONL}
4255
4256Online ASN.1 Compiler.
4257
4258\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c/asn1c.cgi}
4259
4260\end_inset
4261
4262
4263\layout Bibliography
vlme18adea2004-06-03 03:49:45 +00004264\bibitem [Dub00]{Dub00}
4265
vlmaf2155e2004-10-20 15:48:55 +00004266Olivier Dubuisson ---
vlme18adea2004-06-03 03:49:45 +00004267\emph on
4268ASN.1 Communication between heterogeneous systems
4269\emph default
vlmaf2155e2004-10-20 15:48:55 +00004270 --- Morgan Kaufmann Publishers, 2000.
vlme18adea2004-06-03 03:49:45 +00004271
4272\begin_inset LatexCommand \htmlurl{http://asn1.elibel.tm.fr/en/book/}
4273
4274\end_inset
4275
4276.
4277 ISBN:0-12-6333361-0.
4278\layout Bibliography
4279\bibitem [ITU-T/ASN.1]{ITU-T/ASN.1}
4280
4281ITU-T Study Group 17 -- Languages for Telecommunication Systems
4282\begin_inset LatexCommand \url{http://www.itu.int/ITU-T/studygroups/com17/languages/}
4283
4284\end_inset
4285
4286
4287\the_end