blob: 9dbae7d8de319fd8401cc53f6b388fbfec6d57f1 [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
vlmc8b4bc32005-11-21 09:18:18 +000074href{http://lionet.info/asn1c}{asn1c-0.9.20}}
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
vlmc8b4bc32005-11-21 09:18:18 +0000454<lyxtabular version="3" rows="26" 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>
vlmc8b4bc32005-11-21 09:18:18 +0000980<row topline="true" bottomline="true">
vlm11bea882005-10-06 10:09:34 +0000981<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>
vlm4cc45722005-07-24 08:28:39 +00001002<row topline="true">
vlmfcec8e52004-08-23 15:12:04 +00001003<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1004\begin_inset Text
1005
1006\layout Standard
1007
1008
1009\series bold
vlm37d47c02005-03-05 01:07:04 +00001010\size small
vlmfcec8e52004-08-23 15:12:04 +00001011Output Options
1012\end_inset
1013</cell>
1014<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1015\begin_inset Text
1016
1017\layout Standard
1018
1019
1020\series bold
vlm37d47c02005-03-05 01:07:04 +00001021\size small
vlmfcec8e52004-08-23 15:12:04 +00001022Description
1023\end_inset
1024</cell>
1025</row>
1026<row topline="true">
1027<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1028\begin_inset Text
1029
1030\layout Standard
1031
vlm37d47c02005-03-05 01:07:04 +00001032
1033\size small
vlmfcec8e52004-08-23 15:12:04 +00001034-print-constraints
1035\end_inset
1036</cell>
1037<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1038\begin_inset Text
1039
1040\layout Standard
1041
1042
1043\size small
1044When -EF are also specified, this option forces the compiler to explain
1045 its internal understanding of subtype constraints.
1046\end_inset
1047</cell>
1048</row>
1049<row topline="true" bottomline="true">
1050<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1051\begin_inset Text
1052
1053\layout Standard
1054
vlm37d47c02005-03-05 01:07:04 +00001055
1056\size small
vlmfcec8e52004-08-23 15:12:04 +00001057-print-lines
1058\end_inset
1059</cell>
1060<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1061\begin_inset Text
1062
1063\layout Standard
1064
1065
1066\size small
1067Generate "-- #line" comments in -E output.
1068\end_inset
1069</cell>
1070</row>
1071</lyxtabular>
1072
1073\end_inset
1074
1075
vlm856978f2005-02-21 14:16:10 +00001076\layout Chapter
1077
1078Using the ASN.1 Compiler
vlmfcec8e52004-08-23 15:12:04 +00001079\layout Section
vlme18adea2004-06-03 03:49:45 +00001080
vlm856978f2005-02-21 14:16:10 +00001081Invoking the ASN.1 helper code
vlmc3bfd812004-09-30 22:26:19 +00001082\begin_inset OptArg
1083collapsed true
1084
1085\layout Standard
1086
1087Invoking the helper code
1088\end_inset
1089
1090
vlme18adea2004-06-03 03:49:45 +00001091\layout Standard
1092
vlm485123c2005-02-22 07:28:26 +00001093First of all, you should include one or more header files into your application.
1094 Typically, it is enough to include the header file of the main PDU type.
1095 For our Rectangle module, including the Rectangle.h file is sufficient:
vlme18adea2004-06-03 03:49:45 +00001096\layout LyX-Code
1097
1098#include <Rectangle.h>
1099\layout Standard
1100
1101The header files defines the C structure corresponding to the ASN.1 definition
1102 of a rectangle and the declaration of the ASN.1 type descriptor, which is
1103 used as an argument to most of the functions provided by the ASN.1 module.
1104 For example, here is the code which frees the Rectangle_t structure:
1105\layout LyX-Code
1106
vlmfcec8e52004-08-23 15:12:04 +00001107Rectangle_t *rect = ...;
1108\layout LyX-Code
1109
1110
vlme18adea2004-06-03 03:49:45 +00001111\layout LyX-Code
1112
vlm823f1442005-08-29 10:23:20 +00001113asn_DEF_Rectangle.free_struct(&asn_DEF_Rectangle,
vlme18adea2004-06-03 03:49:45 +00001114\layout LyX-Code
1115
1116 rect, 0);
1117\layout Standard
1118
1119This code defines a
1120\emph on
1121rect
1122\emph default
1123 pointer which points to the Rectangle_t structure which needs to be freed.
vlm485123c2005-02-22 07:28:26 +00001124 The second line invokes the generic
1125\emph on
1126free_struct()
1127\emph default
1128 routine created specifically for this Rectangle_t structure.
vlme18adea2004-06-03 03:49:45 +00001129 The
1130\emph on
vlmba56d852004-09-29 13:29:17 +00001131asn_DEF_Rectangle
vlme18adea2004-06-03 03:49:45 +00001132\emph default
vlm485123c2005-02-22 07:28:26 +00001133 is the type descriptor, which holds a collection of routines to deal with
1134 the Rectangle_t structure.
vlme18adea2004-06-03 03:49:45 +00001135\layout Standard
1136
vlm485123c2005-02-22 07:28:26 +00001137The following member functions of the asn_DEF_Rectangle type descriptor
1138 are of interest:
vlme18adea2004-06-03 03:49:45 +00001139\layout Description
1140
vlme18adea2004-06-03 03:49:45 +00001141ber_decoder This is the generic
1142\emph on
1143restartable
1144\begin_inset Foot
1145collapsed false
1146
1147\layout Standard
1148
1149Restartable means that if the decoder encounters the end of the buffer,
1150 it will fail, but may later be invoked again with the rest of the buffer
1151 to continue decoding.
1152\end_inset
1153
1154
1155\emph default
1156BER decoder (Basic Encoding Rules).
1157 This decoder would create and/or fill the target structure for you.
1158 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001159\begin_inset LatexCommand \vref{sub:Decoding-BER}
vlme18adea2004-06-03 03:49:45 +00001160
1161\end_inset
1162
1163.
1164\layout Description
1165
1166der_encoder This is the generic DER encoder (Distinguished Encoding Rules).
vlm68d43e92004-09-24 20:56:07 +00001167 This encoder will take the target structure and encode it into a series
vlme18adea2004-06-03 03:49:45 +00001168 of bytes.
1169 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001170\begin_inset LatexCommand \vref{sub:Encoding-DER}
vlme18adea2004-06-03 03:49:45 +00001171
1172\end_inset
1173
1174.
1175\layout Description
1176
vlm856978f2005-02-21 14:16:10 +00001177xer_encoder This is the XER encoder (XML Encoding Rules).
vlm68d43e92004-09-24 20:56:07 +00001178 This encoder will take the target structure and represent it as an XML
vlm856978f2005-02-21 14:16:10 +00001179 (text) document using either BASIC-XER or CANONICAL-XER encoding rules.
vlm68d43e92004-09-24 20:56:07 +00001180 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001181\begin_inset LatexCommand \vref{sub:Encoding-XER}
1182
1183\end_inset
1184
1185.
1186\layout Description
1187
1188xer_decoder This is the generic XER decoder.
1189 It takes both BASIC-XER or CANONICAL-XER encodings and deserializes the
1190 data into a local, machine-dependent representation.
1191 Please refer to Section
1192\begin_inset LatexCommand \vref{sub:Decoding-XER}
vlm68d43e92004-09-24 20:56:07 +00001193
1194\end_inset
1195
1196.
1197\layout Description
1198
1199check_constraints Check that the contents of the target structure are semantical
1200ly valid and constrained to appropriate implicit or explicit subtype constraints.
1201 Please refer to Section
1202\begin_inset LatexCommand \vref{sub:Validating-the-target}
1203
1204\end_inset
1205
1206.
1207\layout Description
1208
vlme18adea2004-06-03 03:49:45 +00001209print_struct This function convert the contents of the passed target structure
1210 into human readable form.
1211 This form is not formal and cannot be converted back into the structure,
1212 but it may turn out to be useful for debugging or quick-n-dirty printing.
1213 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001214\begin_inset LatexCommand \vref{sub:Printing-the-target}
vlme18adea2004-06-03 03:49:45 +00001215
1216\end_inset
1217
1218.
1219\layout Description
1220
1221free_struct This is a generic disposal which frees the target structure.
1222 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001223\begin_inset LatexCommand \vref{sub:Freeing-the-target}
vlm68d43e92004-09-24 20:56:07 +00001224
1225\end_inset
1226
1227.
1228\layout Standard
1229
vlme18adea2004-06-03 03:49:45 +00001230Each of the above function takes the type descriptor (
1231\emph on
vlmba56d852004-09-29 13:29:17 +00001232asn_DEF_\SpecialChar \ldots{}
vlme18adea2004-06-03 03:49:45 +00001233
1234\emph default
1235) and the target structure (
1236\emph on
1237rect
1238\emph default
1239, in the above example).
vlm856978f2005-02-21 14:16:10 +00001240\layout Subsection
1241
1242
1243\begin_inset LatexCommand \label{sub:Decoding-BER}
1244
1245\end_inset
1246
1247Decoding BER
vlme18adea2004-06-03 03:49:45 +00001248\layout Standard
1249
vlm856978f2005-02-21 14:16:10 +00001250The Basic Encoding Rules describe the most widely used (by the ASN.1 community)
vlm485123c2005-02-22 07:28:26 +00001251 way to encode and decode a given structure in a machine-independent way.
vlm856978f2005-02-21 14:16:10 +00001252 Several other encoding rules (CER, DER) define a more restrictive versions
1253 of BER, so the generic BER parser is also capable of decoding the data
1254 encoded by CER and DER encoders.
1255 The opposite is not true.
1256\layout Standard
1257
vlm485123c2005-02-22 07:28:26 +00001258
1259\emph on
vlm856978f2005-02-21 14:16:10 +00001260The ASN.1 compiler provides the generic BER decoder which is implicitly capable
1261 of decoding BER, CER and DER encoded data.
1262\layout Standard
1263
1264The decoder is restartable (stream-oriented), which means that in case the
1265 buffer has less data than it is expected, the decoder will process whatever
1266 there is available and ask for more data to be provided.
1267 Please note that the decoder may actually process less data than it was
1268 given in the buffer, which means that you must be able to make the next
1269 buffer contain the unprocessed part of the previous buffer.
1270\layout Standard
1271
1272Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
1273\layout Itemize
1274
1275You may concatenate these buffers and feed the BER decoder with 300 bytes
1276 of data, or
1277\layout Itemize
1278
1279You may feed it the first buffer of 100 bytes of data, realize that the
1280 ber_decoder consumed only 95 bytes from it and later feed the decoder with
1281 205 bytes buffer which consists of 5 unprocessed bytes from the first buffer
vlm485123c2005-02-22 07:28:26 +00001282 and the additional 200 bytes from the second buffer.
vlm856978f2005-02-21 14:16:10 +00001283\layout Standard
1284
vlm485123c2005-02-22 07:28:26 +00001285This is not as convenient as it could be (like, the BER encoder could consume
vlm856978f2005-02-21 14:16:10 +00001286 the whole 100 bytes and keep these 5 bytes in some temporary storage),
vlm485123c2005-02-22 07:28:26 +00001287 but in case of existing stream based processing it might actually fit well
1288 into existing algorithm.
vlm856978f2005-02-21 14:16:10 +00001289 Suggestions are welcome.
1290\layout Standard
1291
vlm485123c2005-02-22 07:28:26 +00001292Here is the simplest example of BER decoding.
vlme18adea2004-06-03 03:49:45 +00001293\layout LyX-Code
1294
1295Rectangle_t *
1296\layout LyX-Code
1297
vlmfcec8e52004-08-23 15:12:04 +00001298simple_deserializer(const void *buffer, size_t buf_size) {
vlme18adea2004-06-03 03:49:45 +00001299\layout LyX-Code
1300
1301 Rectangle_t *rect = 0; /* Note this 0! */
1302\layout LyX-Code
1303
vlmaf2155e2004-10-20 15:48:55 +00001304 asn_dec_rval_t rval;
vlme18adea2004-06-03 03:49:45 +00001305\layout LyX-Code
1306
1307
1308\layout LyX-Code
1309
vlm856978f2005-02-21 14:16:10 +00001310 rval =
1311\series bold
vlm823f1442005-08-29 10:23:20 +00001312asn_DEF_Rectangle.ber_decoder
vlm856978f2005-02-21 14:16:10 +00001313\series default
1314(0,
vlme18adea2004-06-03 03:49:45 +00001315\layout LyX-Code
1316
vlmba56d852004-09-29 13:29:17 +00001317 &asn_DEF_Rectangle,
vlme18adea2004-06-03 03:49:45 +00001318\layout LyX-Code
1319
1320 (void **)&rect,
1321\layout LyX-Code
1322
1323 buffer, buf_size,
1324\layout LyX-Code
1325
1326 0);
1327\layout LyX-Code
1328
1329
1330\layout LyX-Code
1331
1332 if(rval
1333\series bold
1334.code
1335\series default
1336 == RC_OK) {
1337\layout LyX-Code
1338
1339 return rect; /* Decoding succeeded */
1340\layout LyX-Code
1341
1342 } else {
1343\layout LyX-Code
1344
vlmfcec8e52004-08-23 15:12:04 +00001345 /* Free partially decoded rect */
1346\layout LyX-Code
1347
vlm823f1442005-08-29 10:23:20 +00001348 asn_DEF_Rectangle.free_struct(
vlme18adea2004-06-03 03:49:45 +00001349\layout LyX-Code
1350
vlmba56d852004-09-29 13:29:17 +00001351 &asn_DEF_Rectangle, rect, 0);
vlme18adea2004-06-03 03:49:45 +00001352\layout LyX-Code
1353
1354 return 0;
1355\layout LyX-Code
1356
1357 }
1358\layout LyX-Code
1359
1360}
1361\layout Standard
1362
1363The code above defines a function,
1364\emph on
1365simple_deserializer
1366\emph default
vlm485123c2005-02-22 07:28:26 +00001367, which takes a buffer and its length and is expected to return a pointer
1368 to the Rectangle_t structure.
vlme18adea2004-06-03 03:49:45 +00001369 Inside, it tries to convert the bytes passed into the target structure
vlm485123c2005-02-22 07:28:26 +00001370 (rect) using the BER decoder and returns the rect pointer afterwards.
vlme18adea2004-06-03 03:49:45 +00001371 If the structure cannot be deserialized, it frees the memory which might
1372 be left allocated by the unfinished
1373\emph on
1374ber_decoder
1375\emph default
vlm856978f2005-02-21 14:16:10 +00001376 routine and returns 0 (no data).
vlm485123c2005-02-22 07:28:26 +00001377 (This
vlme18adea2004-06-03 03:49:45 +00001378\series bold
vlm856978f2005-02-21 14:16:10 +00001379 freeing is necessary
vlme18adea2004-06-03 03:49:45 +00001380\series default
1381 because the ber_decoder is a restartable procedure, and may fail just because
vlm485123c2005-02-22 07:28:26 +00001382 there is more data needs to be provided before decoding could be finalized).
vlme18adea2004-06-03 03:49:45 +00001383 The code above obviously does not take into account the way the
1384\emph on
vlm485123c2005-02-22 07:28:26 +00001385ber_decoder()
vlme18adea2004-06-03 03:49:45 +00001386\emph default
1387 failed, so the freeing is necessary because the part of the buffer may
1388 already be decoded into the structure by the time something goes wrong.
1389\layout Standard
1390
vlm485123c2005-02-22 07:28:26 +00001391A little less wordy would be to invoke a globally available
vlme18adea2004-06-03 03:49:45 +00001392\emph on
vlm485123c2005-02-22 07:28:26 +00001393ber_decode()
vlme18adea2004-06-03 03:49:45 +00001394\emph default
vlm485123c2005-02-22 07:28:26 +00001395 function instead of dereferencing the asn_DEF_Rectangle type descriptor:
vlme18adea2004-06-03 03:49:45 +00001396\layout LyX-Code
1397
vlmba56d852004-09-29 13:29:17 +00001398rval = ber_decode(0, &asn_DEF_Rectangle, (void **)&rect,
vlme18adea2004-06-03 03:49:45 +00001399\layout LyX-Code
1400
1401 buffer, buf_size);
1402\layout Standard
1403
vlm823f1442005-08-29 10:23:20 +00001404Note that the initial (asn_DEF_Rectangle.ber_decoder) reference is gone,
vlme18adea2004-06-03 03:49:45 +00001405 and also the last argument (0) is no longer necessary.
1406\layout Standard
1407
vlm856978f2005-02-21 14:16:10 +00001408These two ways of BER decoder invocations are fully equivalent.
vlme18adea2004-06-03 03:49:45 +00001409\layout Standard
1410
1411The BER de
1412\emph on
1413coder
1414\emph default
vlmba56d852004-09-29 13:29:17 +00001415 may fail because of (
vlme18adea2004-06-03 03:49:45 +00001416\emph on
1417the following RC_\SpecialChar \ldots{}
1418 codes are defined in ber_decoder.h
1419\emph default
1420):
1421\layout Itemize
1422
1423RC_WMORE: There is more data expected than it is provided (stream mode continuat
1424ion feature);
1425\layout Itemize
1426
1427RC_FAIL: General failure to decode the buffer;
1428\layout Itemize
1429
1430\SpecialChar \ldots{}
1431 other codes may be defined as well.
1432\layout Standard
1433
vlmaf2155e2004-10-20 15:48:55 +00001434Together with the return code (.code) the asn_dec_rval_t type contains the
vlme18adea2004-06-03 03:49:45 +00001435 number of bytes which is consumed from the buffer.
1436 In the previous hypothetical example of two buffers (of 100 and 200 bytes),
1437 the first call to ber_decode() would return with .code = RC_WMORE and .consumed
1438 = 95.
1439 The .consumed field of the BER decoder return value is
1440\series bold
1441always
1442\series default
1443 valid, even if the decoder succeeds or fails with any other return code.
1444\layout Standard
1445
1446Please look into ber_decoder.h for the precise definition of ber_decode()
1447 and related types.
vlmfcec8e52004-08-23 15:12:04 +00001448\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001449
1450
1451\begin_inset LatexCommand \label{sub:Encoding-DER}
1452
1453\end_inset
1454
1455Encoding DER
1456\layout Standard
1457
vlm68d43e92004-09-24 20:56:07 +00001458The Distinguished Encoding Rules is the
1459\emph on
1460canonical
1461\emph default
1462 variant of BER encoding rules.
1463 The DER is best suited to encode the structures where all the lengths are
1464 known beforehand.
vlme18adea2004-06-03 03:49:45 +00001465 This is probably exactly how you want to encode: either after a BER decoding
1466 or after a manual fill-up, the target structure contains the data which
1467 size is implicitly known before encoding.
vlm856978f2005-02-21 14:16:10 +00001468 Among other uses, the DER encoding is used to encode X.509 certificates.
vlme18adea2004-06-03 03:49:45 +00001469\layout Standard
1470
1471As with BER decoder, the DER encoder may be invoked either directly from
vlmba56d852004-09-29 13:29:17 +00001472 the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone function,
vlme18adea2004-06-03 03:49:45 +00001473 which is somewhat simpler:
1474\layout LyX-Code
1475
vlme18adea2004-06-03 03:49:45 +00001476
1477\layout LyX-Code
1478
1479/*
1480\layout LyX-Code
1481
1482 * This is the serializer itself,
1483\layout LyX-Code
1484
1485 * it supplies the DER encoder with the
1486\layout LyX-Code
1487
1488 * pointer to the custom output function.
1489\layout LyX-Code
1490
1491 */
1492\layout LyX-Code
1493
1494ssize_t
1495\layout LyX-Code
1496
1497simple_serializer(FILE *ostream, Rectangle_t *rect) {
1498\layout LyX-Code
1499
vlm68d43e92004-09-24 20:56:07 +00001500 asn_enc_rval_t er; /* Encoder return value */
vlme18adea2004-06-03 03:49:45 +00001501\layout LyX-Code
1502
1503
1504\layout LyX-Code
1505
vlmba56d852004-09-29 13:29:17 +00001506 er = der_encode(&asn_DEF_Rect, rect,
vlme18adea2004-06-03 03:49:45 +00001507\layout LyX-Code
1508
vlmfcec8e52004-08-23 15:12:04 +00001509 write_stream, ostream);
vlme18adea2004-06-03 03:49:45 +00001510\layout LyX-Code
1511
vlm68d43e92004-09-24 20:56:07 +00001512 if(er.
vlme18adea2004-06-03 03:49:45 +00001513\series bold
vlm68d43e92004-09-24 20:56:07 +00001514encoded
vlme18adea2004-06-03 03:49:45 +00001515\series default
1516 == -1) {
1517\layout LyX-Code
1518
1519 /*
1520\layout LyX-Code
1521
vlm68d43e92004-09-24 20:56:07 +00001522 * Failed to encode the rectangle data.
vlme18adea2004-06-03 03:49:45 +00001523\layout LyX-Code
1524
1525 */
1526\layout LyX-Code
1527
1528 fprintf(stderr,
1529\begin_inset Quotes sld
1530\end_inset
1531
1532Cannot encode %s: %s
1533\backslash
1534n
1535\begin_inset Quotes srd
1536\end_inset
1537
1538,
1539\layout LyX-Code
1540
vlm68d43e92004-09-24 20:56:07 +00001541 er.
vlme18adea2004-06-03 03:49:45 +00001542\series bold
vlm68d43e92004-09-24 20:56:07 +00001543failed_type
vlme18adea2004-06-03 03:49:45 +00001544\series default
1545->name,
1546\layout LyX-Code
1547
1548 strerror(errno));
1549\layout LyX-Code
1550
1551 return -1;
1552\layout LyX-Code
1553
1554 } else {
1555\layout LyX-Code
1556
1557 /* Return the number of bytes */
1558\layout LyX-Code
1559
vlm68d43e92004-09-24 20:56:07 +00001560 return er.encoded;
vlme18adea2004-06-03 03:49:45 +00001561\layout LyX-Code
1562
1563 }
1564\layout LyX-Code
1565
1566}
1567\layout Standard
1568
1569As you see, the DER encoder does not write into some sort of buffer or something.
1570 It just invokes the custom function (possible, multiple times) which would
1571 save the data into appropriate storage.
1572 The optional argument
1573\emph on
1574app_key
1575\emph default
1576 is opaque for the DER encoder code and just used by
1577\emph on
1578_write_stream()
1579\emph default
1580 as the pointer to the appropriate output stream to be used.
1581\layout Standard
1582
1583If the custom write function is not given (passed as 0), then the DER encoder
1584 will essentially do the same thing (i.e., encode the data) but no callbacks
1585 will be invoked (so the data goes nowhere).
1586 It may prove useful to determine the size of the structure's encoding before
1587 actually doing the encoding
1588\begin_inset Foot
1589collapsed false
1590
1591\layout Standard
1592
1593It is actually faster too: the encoder might skip over some computations
1594 which aren't important for the size determination.
1595\end_inset
1596
1597.
1598\layout Standard
1599
1600Please look into der_encoder.h for the precise definition of der_encode()
1601 and related types.
vlmfcec8e52004-08-23 15:12:04 +00001602\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001603
1604
vlm68d43e92004-09-24 20:56:07 +00001605\begin_inset LatexCommand \label{sub:Encoding-XER}
1606
1607\end_inset
1608
1609Encoding XER
1610\layout Standard
1611
1612The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
1613 Markup Language, a text-based format for information exchange.
1614 The encoder routine API comes in two flavors: stdio-based and callback-based.
1615 With the callback-based encoder, the encoding process is very similar to
1616 the DER one, described in Section
1617\begin_inset LatexCommand \vref{sub:Encoding-DER}
1618
1619\end_inset
1620
1621.
1622 The following example uses the definition of write_stream() from up there.
1623\layout LyX-Code
1624
1625/*
1626\layout LyX-Code
1627
1628 * This procedure generates the XML document
1629\layout LyX-Code
1630
1631 * by invoking the XER encoder.
1632\layout LyX-Code
1633
1634 * NOTE: Do not copy this code verbatim!
1635\layout LyX-Code
1636
1637 * If the stdio output is necessary,
1638\layout LyX-Code
1639
1640 * use the xer_fprint() procedure instead.
1641\layout LyX-Code
1642
1643 * See Section
1644\begin_inset LatexCommand \vref{sub:Printing-the-target}
1645
1646\end_inset
1647
1648.
1649\layout LyX-Code
1650
1651 */
1652\layout LyX-Code
1653
1654int
1655\layout LyX-Code
1656
1657print_as_XML(FILE *ostream, Rectangle_t *rect) {
1658\layout LyX-Code
1659
1660 asn_enc_rval_t er; /* Encoder return value */
1661\layout LyX-Code
1662
1663
1664\layout LyX-Code
1665
vlm856978f2005-02-21 14:16:10 +00001666 er = xer_encode(&asn_DEF_Rectangle, rect,
vlm68d43e92004-09-24 20:56:07 +00001667\layout LyX-Code
1668
1669 XER_F_BASIC, /* BASIC-XER or CANONICAL-XER */
1670\layout LyX-Code
1671
1672 write_stream, ostream);
1673\layout LyX-Code
1674
1675
1676\layout LyX-Code
1677
1678 return (er.encoded == -1) ? -1 : 0;
1679\layout LyX-Code
1680
1681}
1682\layout Standard
1683
1684Please look into xer_encoder.h for the precise definition of xer_encode()
1685 and related types.
1686\layout Standard
1687
1688See Section
1689\begin_inset LatexCommand \ref{sub:Printing-the-target}
1690
1691\end_inset
1692
1693 for the example of stdio-based XML encoder and other pretty-printing suggestion
1694s.
1695\layout Subsection
1696
1697
vlm856978f2005-02-21 14:16:10 +00001698\begin_inset LatexCommand \label{sub:Decoding-XER}
1699
1700\end_inset
1701
1702Decoding XER
1703\layout Standard
1704
1705The data encoded using the XER rules can be subsequently decoded using the
1706 xer_decode() API call:
1707\layout LyX-Code
1708
1709Rectangle_t *
1710\layout LyX-Code
1711
1712XML_to_Rectangle(const void *buffer, size_t buf_size) {
1713\layout LyX-Code
1714
1715 Rectangle_t *rect = 0; /* Note this 0! */
1716\layout LyX-Code
1717
1718 asn_dec_rval_t rval;
1719\layout LyX-Code
1720
1721
1722\layout LyX-Code
1723
1724 rval = xer_decode(0, &asn_DEF_Rectangle, (void **)&rect,
1725\layout LyX-Code
1726
1727 buffer, buf_size);
1728\layout LyX-Code
1729
1730 if(rval
1731\series bold
1732.code
1733\series default
1734 == RC_OK) {
1735\layout LyX-Code
1736
1737 return rect; /* Decoding succeeded */
1738\layout LyX-Code
1739
1740 } else {
1741\layout LyX-Code
1742
1743 /* Free partially decoded rect */
1744\layout LyX-Code
1745
vlm823f1442005-08-29 10:23:20 +00001746 asn_DEF_Rectangle.free_struct(
vlm856978f2005-02-21 14:16:10 +00001747\layout LyX-Code
1748
1749 &asn_DEF_Rectangle, rect, 0);
1750\layout LyX-Code
1751
1752 return 0;
1753\layout LyX-Code
1754
1755 }
1756\layout LyX-Code
1757
1758}
1759\layout Standard
1760
1761The decoder takes both BASIC-XER and CANONICAL-XER encodings.
1762\layout Standard
1763
vlm485123c2005-02-22 07:28:26 +00001764The decoder shares its data consumption properties with BER decoder; please
1765 read the Section
1766\begin_inset LatexCommand \vref{sub:Decoding-BER}
1767
1768\end_inset
1769
1770 to know more.
1771\layout Standard
1772
vlm856978f2005-02-21 14:16:10 +00001773Please look into xer_decoder.h for the precise definition of xer_decode()
1774 and related types.
1775\layout Subsection
1776
1777
vlme18adea2004-06-03 03:49:45 +00001778\begin_inset LatexCommand \label{sub:Validating-the-target}
1779
1780\end_inset
1781
1782Validating the target structure
1783\layout Standard
1784
1785Sometimes the target structure needs to be validated.
1786 For example, if the structure was created by the application (as opposed
1787 to being decoded from some external source), some important information
1788 required by the ASN.1 specification might be missing.
1789 On the other hand, the successful decoding of the data from some external
1790 source does not necessarily mean that the data is fully valid either.
1791 It might well be the case that the specification describes some subtype
1792 constraints that were not taken into account during decoding, and it would
1793 actually be useful to perform the last check when the data is ready to
1794 be encoded or when the data has just been decoded to ensure its validity
1795 according to some stricter rules.
1796\layout Standard
1797
1798The asn_check_constraints() function checks the type for various implicit
1799 and explicit constraints.
1800 It is recommended to use asn_check_constraints() function after each decoding
1801 and before each encoding.
1802\layout Standard
1803
1804Please look into constraints.h for the precise definition of asn_check_constraint
1805s() and related types.
vlmfcec8e52004-08-23 15:12:04 +00001806\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001807
1808
1809\begin_inset LatexCommand \label{sub:Printing-the-target}
1810
1811\end_inset
1812
1813Printing the target structure
1814\layout Standard
1815
1816There are two ways to print the target structure: either invoke the print_struct
1817 member of the ASN.1 type descriptor, or using the asn_fprint() function,
1818 which is a simpler wrapper of the former:
1819\layout LyX-Code
1820
vlmba56d852004-09-29 13:29:17 +00001821asn_fprint(stdout, &asn_DEF_Rectangle, rect);
vlme18adea2004-06-03 03:49:45 +00001822\layout Standard
1823
1824Please look into constr_TYPE.h for the precise definition of asn_fprint()
1825 and related types.
vlm68d43e92004-09-24 20:56:07 +00001826\layout Standard
1827
1828Another practical alternative to this custom format printing would be to
1829 invoke XER encoder.
1830 The default BASIC-XER encoder performs reasonable formatting for the output
1831 to be useful and human readable.
1832 To invoke the XER decoder in a manner similar to asn_fprint(), use the
1833 xer_fprint() call:
1834\layout LyX-Code
1835
vlmba56d852004-09-29 13:29:17 +00001836xer_fprint(stdout, &asn_DEF_Rectangle, rect);
vlm68d43e92004-09-24 20:56:07 +00001837\layout Standard
1838
1839See Section
1840\begin_inset LatexCommand \vref{sub:Encoding-XER}
1841
1842\end_inset
1843
1844 for XML-related details.
vlmfcec8e52004-08-23 15:12:04 +00001845\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001846
1847
1848\begin_inset LatexCommand \label{sub:Freeing-the-target}
1849
1850\end_inset
1851
1852Freeing the target structure
1853\layout Standard
1854
1855Freeing the structure is slightly more complex than it may seem to.
1856 When the ASN.1 structure is freed, all the members of the structure and
1857 their submembers etc etc are recursively freed too.
1858 But it might not be feasible to free the structure itself.
1859 Consider the following case:
1860\layout LyX-Code
1861
1862struct my_figure { /* The custom structure */
1863\layout LyX-Code
1864
1865 int flags; /* <some custom member> */
1866\layout LyX-Code
1867
1868 /* The type is generated by the ASN.1 compiler */
1869\layout LyX-Code
1870
1871
1872\emph on
1873Rectangle_t rect;
1874\layout LyX-Code
1875
1876 /* other members of the structure */
1877\layout LyX-Code
1878
1879};
1880\layout Standard
1881
1882In this example, the application programmer defined a custom structure with
1883 one ASN.1-derived member (rect).
1884 This member is not a reference to the Rectangle_t, but an in-place inclusion
1885 of the Rectangle_t structure.
1886 If the freeing is necessary, the usual procedure of freeing everything
1887 must not be applied to the &rect pointer itself, because it does not point
vlm485123c2005-02-22 07:28:26 +00001888 to the memory block directly allocated by the memory allocation routine,
1889 but instead lies within a block allocated for the my_figure structure.
vlme18adea2004-06-03 03:49:45 +00001890\layout Standard
1891
1892To solve this problem, the free_struct routine has the additional argument
vlm485123c2005-02-22 07:28:26 +00001893 (besides the obvious type descriptor and target structure pointers), which
1894 is the flag specifying whether the outer pointer itself must be freed (0,
1895 default) or it should be left intact (non-zero value).
vlme18adea2004-06-03 03:49:45 +00001896\layout LyX-Code
1897
vlm856978f2005-02-21 14:16:10 +00001898
1899\series bold
1900/* 1.
1901 Rectangle_t is defined within my_figure */
vlme18adea2004-06-03 03:49:45 +00001902\layout LyX-Code
1903
vlm856978f2005-02-21 14:16:10 +00001904struct my_figure {
1905\layout LyX-Code
1906
1907 Rectangle_t rect;
1908\layout LyX-Code
1909
1910} *mf =
vlme18adea2004-06-03 03:49:45 +00001911\series bold
1912...
1913\series default
1914;
1915\layout LyX-Code
1916
1917/*
1918\layout LyX-Code
1919
vlm856978f2005-02-21 14:16:10 +00001920 * Freeing the Rectangle_t
vlme18adea2004-06-03 03:49:45 +00001921\layout LyX-Code
1922
vlm856978f2005-02-21 14:16:10 +00001923 * without freeing the mf->rect area
vlme18adea2004-06-03 03:49:45 +00001924\layout LyX-Code
1925
1926 */
1927\layout LyX-Code
1928
vlm823f1442005-08-29 10:23:20 +00001929asn_DEF_Rectangle.free_struct(
vlme18adea2004-06-03 03:49:45 +00001930\layout LyX-Code
1931
vlmba56d852004-09-29 13:29:17 +00001932 &asn_DEF_Rectangle, &mf->rect,
vlm856978f2005-02-21 14:16:10 +00001933\series bold
vlme18adea2004-06-03 03:49:45 +000019341
vlm856978f2005-02-21 14:16:10 +00001935\emph on
vlme18adea2004-06-03 03:49:45 +00001936
vlm856978f2005-02-21 14:16:10 +00001937\emph default
1938/* !free */
1939\series default
1940);
vlme18adea2004-06-03 03:49:45 +00001941\layout LyX-Code
1942
vlm856978f2005-02-21 14:16:10 +00001943
1944\layout LyX-Code
1945
1946
1947\layout LyX-Code
1948
1949
1950\series bold
1951/* 2.
1952 Rectangle_t is a stand-alone pointer */
vlme18adea2004-06-03 03:49:45 +00001953\layout LyX-Code
1954
1955Rectangle_t *rect =
1956\series bold
1957...
1958\series default
1959;
1960\layout LyX-Code
1961
1962/*
1963\layout LyX-Code
1964
1965 * Freeing the Rectangle_t
1966\layout LyX-Code
1967
1968 * and freeing the rect pointer
1969\layout LyX-Code
1970
1971 */
1972\layout LyX-Code
1973
vlm823f1442005-08-29 10:23:20 +00001974asn_DEF_Rectangle.free_struct(
vlme18adea2004-06-03 03:49:45 +00001975\layout LyX-Code
1976
vlmba56d852004-09-29 13:29:17 +00001977 &asn_DEF_Rectangle, rect,
vlm856978f2005-02-21 14:16:10 +00001978\series bold
vlme18adea2004-06-03 03:49:45 +000019790
vlm856978f2005-02-21 14:16:10 +00001980\emph on
1981
vlme18adea2004-06-03 03:49:45 +00001982\emph default
vlm856978f2005-02-21 14:16:10 +00001983/* free the pointer too */
1984\series default
1985);
vlme18adea2004-06-03 03:49:45 +00001986\layout Standard
1987
1988It is safe to invoke the
1989\emph on
1990free_struct
1991\emph default
1992 function with the target structure pointer set to 0 (NULL), the function
1993 will do nothing.
vlma1fd7f92005-01-31 10:10:04 +00001994\layout Chapter
vlm4cb0edf2005-01-14 11:38:49 +00001995
vlmc7737f62005-02-02 09:53:46 +00001996
1997\begin_inset LatexCommand \label{cha:Step-by-step-examples}
1998
1999\end_inset
2000
vlma1fd7f92005-01-31 10:10:04 +00002001Step by step examples
2002\layout Section
2003
2004A
2005\begin_inset Quotes sld
2006\end_inset
2007
2008Rectangle
2009\begin_inset Quotes srd
2010\end_inset
2011
2012 Encoder
2013\layout Standard
2014
vlmc7737f62005-02-02 09:53:46 +00002015This example will help you to create a simple BER and XER encoder of a
vlma1fd7f92005-01-31 10:10:04 +00002016\begin_inset Quotes sld
2017\end_inset
2018
2019Rectangle
2020\begin_inset Quotes srd
2021\end_inset
2022
2023 type used throughout this document.
2024\layout Enumerate
2025
2026Create a file named
2027\series bold
2028rectangle.asn1
2029\series default
2030 with the following contents:
2031\begin_deeper
2032\layout LyX-Code
2033
2034RectangleModule1 DEFINITIONS ::=
2035\layout LyX-Code
2036
2037BEGIN
2038\layout LyX-Code
2039
2040
2041\layout LyX-Code
2042
2043Rectangle ::= SEQUENCE {
2044\layout LyX-Code
2045
2046 height INTEGER,
2047\layout LyX-Code
2048
2049 width INTEGER
2050\layout LyX-Code
2051
2052}
2053\layout LyX-Code
2054
2055
2056\layout LyX-Code
2057
2058END
2059\end_deeper
2060\layout Enumerate
2061
2062Compile it into the set of .c and .h files using asn1c compiler
2063\begin_inset LatexCommand \cite{ASN1C}
2064
2065\end_inset
2066
2067:
2068\begin_deeper
2069\layout LyX-Code
2070
2071
2072\emph on
2073asn1c -fnative-types
2074\series bold
2075\emph default
2076rectangle.asn1
2077\end_deeper
2078\layout Enumerate
2079
2080Alternatively, use the Online ASN.1 compiler
2081\begin_inset LatexCommand \cite{AONL}
2082
2083\end_inset
2084
2085 by uploading the
2086\series bold
2087rectangle.asn1
2088\series default
2089 file into the Web form and unpacking the produced archive on your computer.
2090\layout Enumerate
2091
2092By this time, you should have gotten multiple files in the current directory,
2093 including the
2094\series bold
2095Rectangle.c
2096\series default
2097 and
2098\series bold
2099Rectangle.h
2100\series default
2101.
2102\layout Enumerate
2103
2104Create a main() routine which creates the Rectangle_t structure in memory
2105 and encodes it using BER and XER encoding rules.
2106 Let's name the file
2107\series bold
2108main.c
2109\series default
2110:
2111\begin_inset ERT
2112status Open
2113
2114\layout Standard
2115
2116\backslash
2117clearpage{}
2118\end_inset
2119
2120
2121\begin_deeper
2122\layout LyX-Code
2123
2124
2125\size small
2126#include <stdio.h>
2127\layout LyX-Code
2128
2129
2130\size small
2131#include <sys/types.h>
2132\layout LyX-Code
2133
2134
2135\size small
2136#include <Rectangle.h> /* Rectangle ASN.1 type */
2137\layout LyX-Code
2138
2139
2140\layout LyX-Code
2141
2142
2143\size small
2144/*
2145\layout LyX-Code
2146
2147
2148\size small
2149 * This is a custom function which writes the
2150\layout LyX-Code
2151
2152
2153\size small
2154 * encoded output into some FILE stream.
2155\layout LyX-Code
2156
2157
2158\size small
2159 */
2160\layout LyX-Code
2161
2162
2163\size small
2164static int
2165\layout LyX-Code
2166
2167
2168\size small
2169write_out(const void *buffer, size_t size, void *app_key) {
2170\layout LyX-Code
2171
2172
2173\size small
2174 FILE *out_fp = app_key;
2175\layout LyX-Code
2176
2177
2178\size small
2179 size_t wrote;
2180\layout LyX-Code
2181
2182
2183\size small
2184
2185\layout LyX-Code
2186
2187
2188\size small
2189 wrote = fwrite(buffer, 1, size, out_fp);
2190\layout LyX-Code
2191
2192
2193\size small
2194
2195\layout LyX-Code
2196
2197
2198\size small
2199 return (wrote == size) ? 0 : -1;
2200\layout LyX-Code
2201
2202
2203\size small
2204}
2205\layout LyX-Code
2206
2207
2208\layout LyX-Code
2209
2210
2211\size small
vlm6c37dac2005-01-31 15:06:32 +00002212int main(int ac, char **av) {
vlma1fd7f92005-01-31 10:10:04 +00002213\layout LyX-Code
2214
2215
2216\size small
2217 Rectangle_t *rectangle; /* Type to encode */
2218\layout LyX-Code
2219
2220
2221\size small
2222 asn_enc_rval_t ec; /* Encoder return value */
2223\layout LyX-Code
2224
2225
2226\size small
2227
2228\layout LyX-Code
2229
2230
2231\size small
2232 /* Allocate the Rectangle_t */
2233\layout LyX-Code
2234
2235
2236\size small
vlmc7737f62005-02-02 09:53:46 +00002237 rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */
vlma1fd7f92005-01-31 10:10:04 +00002238\layout LyX-Code
2239
2240
2241\size small
2242 if(!rectangle) {
2243\layout LyX-Code
2244
2245
2246\size small
2247 perror(
2248\begin_inset Quotes sld
2249\end_inset
2250
2251calloc() failed
2252\begin_inset Quotes srd
2253\end_inset
2254
2255);
2256\layout LyX-Code
2257
2258
2259\size small
2260 exit(71); /* better, EX_OSERR */
2261\layout LyX-Code
2262
2263
2264\size small
2265 }
2266\layout LyX-Code
2267
2268
2269\size small
2270
2271\layout LyX-Code
2272
2273
2274\size small
2275 /* Initialize the Rectangle members */
2276\layout LyX-Code
2277
2278
2279\size small
2280 rectangle->height = 42; /* any random value */
2281\layout LyX-Code
2282
2283
2284\size small
2285 rectangle->width = 23; /* any random value */
2286\layout LyX-Code
2287
2288
2289\size small
2290
2291\layout LyX-Code
2292
2293
2294\size small
2295 /* BER encode the data if filename is given */
2296\layout LyX-Code
2297
2298
2299\size small
2300 if(ac < 2) {
2301\layout LyX-Code
2302
2303
2304\size small
2305 fprintf(stderr,
2306\begin_inset Quotes sld
2307\end_inset
2308
2309Specify filename for BER output
2310\backslash
2311n
2312\begin_inset Quotes srd
2313\end_inset
2314
2315);
2316\layout LyX-Code
2317
2318
2319\size small
2320 } else {
2321\layout LyX-Code
2322
2323
2324\size small
2325 const char *filename = av[1];
2326\layout LyX-Code
2327
2328
2329\size small
2330 FILE *fp = fopen(filename,
2331\begin_inset Quotes sld
2332\end_inset
2333
vlmdae8e612005-07-21 18:45:37 +00002334wb
vlma1fd7f92005-01-31 10:10:04 +00002335\begin_inset Quotes srd
2336\end_inset
2337
2338); /* for BER output */
2339\layout LyX-Code
2340
2341
2342\layout LyX-Code
2343
2344
2345\size small
2346 if(!fp) {
2347\layout LyX-Code
2348
2349
2350\size small
2351 perror(filename);
2352\layout LyX-Code
2353
2354
2355\size small
2356 exit(71); /* better, EX_OSERR */
2357\layout LyX-Code
2358
2359
2360\size small
2361 }
2362\layout LyX-Code
2363
2364
2365\size small
2366
2367\layout LyX-Code
2368
2369
2370\size small
2371 /* Encode the Rectangle type as BER (DER) */
2372\layout LyX-Code
2373
2374
2375\size small
2376 ec = der_encode(&asn_DEF_Rectangle,
2377\layout LyX-Code
2378
2379
2380\size small
vlm6c37dac2005-01-31 15:06:32 +00002381 rectangle, write_out, fp);
vlma1fd7f92005-01-31 10:10:04 +00002382\layout LyX-Code
2383
2384
2385\size small
2386 fclose(fp);
2387\layout LyX-Code
2388
2389
2390\size small
2391 if(ec.encoded == -1) {
2392\layout LyX-Code
2393
2394
2395\size small
2396 fprintf(stderr,
2397\layout LyX-Code
2398
2399
2400\size small
2401
2402\begin_inset Quotes sld
2403\end_inset
2404
2405Could not encode Rectangle (at %s)
2406\backslash
2407n
2408\begin_inset Quotes srd
2409\end_inset
2410
2411,
2412\layout LyX-Code
2413
2414
2415\size small
2416 ec.failed_type ? ec.failed_type->name :
2417\begin_inset Quotes sld
2418\end_inset
2419
2420unknown
2421\begin_inset Quotes srd
2422\end_inset
2423
2424);
2425\layout LyX-Code
2426
2427
2428\size small
2429 exit(65); /* better, EX_DATAERR */
2430\layout LyX-Code
2431
2432
2433\size small
2434 } else {
2435\layout LyX-Code
2436
2437
2438\size small
2439 fprintf(stderr,
2440\begin_inset Quotes sld
2441\end_inset
2442
2443Created %s with BER encoded Rectangle
2444\backslash
2445n
2446\begin_inset Quotes srd
2447\end_inset
2448
2449,
2450\layout LyX-Code
2451
2452
2453\size small
2454 filename);
2455\layout LyX-Code
2456
2457
2458\size small
2459 }
2460\layout LyX-Code
2461
2462
2463\size small
2464 }
2465\layout LyX-Code
2466
2467
2468\size small
2469
2470\layout LyX-Code
2471
2472
2473\size small
2474 /* Also print the constructed Rectangle XER encoded (XML) */
2475\layout LyX-Code
2476
2477
2478\size small
2479 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
2480\layout LyX-Code
2481
2482
2483\size small
2484
2485\layout LyX-Code
2486
2487
2488\size small
2489 return 0; /* Encoding finished successfully */
2490\layout LyX-Code
2491
2492
2493\size small
2494}
2495\end_deeper
2496\layout Enumerate
2497
2498Compile all files together using C compiler (varies by platform):
2499\begin_deeper
2500\layout LyX-Code
2501
2502
2503\emph on
2504cc -I.
2505 -o
2506\series bold
2507rencode
2508\series default
2509 *.c
2510\end_deeper
2511\layout Enumerate
2512
2513Voila! You have just created the BER and XER encoder of a Rectangle type,
2514 named
2515\series bold
2516rencode
2517\series default
2518!
2519\layout Standard
2520
2521
2522\begin_inset ERT
2523status Collapsed
2524
2525\layout Standard
2526
2527\backslash
2528clearpage{}
2529\end_inset
2530
2531
2532\layout Section
2533
vlmc7737f62005-02-02 09:53:46 +00002534
2535\begin_inset LatexCommand \label{sec:A-Rectangle-Decoder}
2536
2537\end_inset
2538
vlma1fd7f92005-01-31 10:10:04 +00002539A
vlm4cb0edf2005-01-14 11:38:49 +00002540\begin_inset Quotes sld
2541\end_inset
2542
2543Rectangle
2544\begin_inset Quotes srd
2545\end_inset
2546
2547 Decoder
2548\layout Standard
2549
vlmc7737f62005-02-02 09:53:46 +00002550This example will help you to create a simple BER decoder of a simple
vlm4cb0edf2005-01-14 11:38:49 +00002551\begin_inset Quotes sld
2552\end_inset
2553
2554Rectangle
2555\begin_inset Quotes srd
2556\end_inset
2557
2558 type used throughout this document.
2559\layout Enumerate
2560
2561Create a file named
2562\series bold
2563rectangle.asn1
2564\series default
2565 with the following contents:
2566\begin_deeper
2567\layout LyX-Code
2568
2569RectangleModule1 DEFINITIONS ::=
2570\layout LyX-Code
2571
2572BEGIN
2573\layout LyX-Code
2574
2575
2576\layout LyX-Code
2577
2578Rectangle ::= SEQUENCE {
2579\layout LyX-Code
2580
2581 height INTEGER,
2582\layout LyX-Code
2583
2584 width INTEGER
2585\layout LyX-Code
2586
2587}
2588\layout LyX-Code
2589
2590
2591\layout LyX-Code
2592
2593END
2594\end_deeper
2595\layout Enumerate
2596
2597Compile it into the set of .c and .h files using asn1c compiler
2598\begin_inset LatexCommand \cite{ASN1C}
2599
2600\end_inset
2601
2602:
2603\begin_deeper
2604\layout LyX-Code
2605
2606
2607\emph on
2608asn1c -fnative-types
2609\series bold
2610\emph default
2611rectangle.asn1
2612\end_deeper
2613\layout Enumerate
2614
2615Alternatively, use the Online ASN.1 compiler
2616\begin_inset LatexCommand \cite{AONL}
2617
2618\end_inset
2619
2620 by uploading the
2621\series bold
2622rectangle.asn1
2623\series default
vlmabd51e22005-01-17 11:05:04 +00002624 file into the Web form and unpacking the produced archive on your computer.
vlm4cb0edf2005-01-14 11:38:49 +00002625\layout Enumerate
2626
vlmabd51e22005-01-17 11:05:04 +00002627By this time, you should have gotten multiple files in the current directory,
2628 including the
2629\series bold
2630Rectangle.c
2631\series default
2632 and
2633\series bold
2634Rectangle.h
2635\series default
2636.
vlm4cb0edf2005-01-14 11:38:49 +00002637\layout Enumerate
2638
2639Create a main() routine which takes the binary input file, decodes it as
vlmabd51e22005-01-17 11:05:04 +00002640 it were a BER-encoded Rectangle type, and prints out the text (XML) representat
2641ion of the Rectangle type.
vlm4cb0edf2005-01-14 11:38:49 +00002642 Let's name the file
2643\series bold
2644main.c
2645\series default
2646:
vlmabd51e22005-01-17 11:05:04 +00002647\begin_inset ERT
vlmc7737f62005-02-02 09:53:46 +00002648status Collapsed
vlmabd51e22005-01-17 11:05:04 +00002649
2650\layout Standard
2651
2652\backslash
2653clearpage{}
2654\end_inset
2655
2656
vlm4cb0edf2005-01-14 11:38:49 +00002657\begin_deeper
2658\layout LyX-Code
2659
2660
2661\size small
2662#include <stdio.h>
2663\layout LyX-Code
2664
2665
2666\size small
2667#include <sys/types.h>
2668\layout LyX-Code
2669
2670
2671\size small
2672#include <Rectangle.h> /* Rectangle ASN.1 type */
2673\layout LyX-Code
2674
2675
2676\size small
2677
2678\layout LyX-Code
2679
2680
2681\size small
2682int main(int ac, char **av) {
2683\layout LyX-Code
2684
2685
2686\size small
2687 char buf[1024]; /* Temporary buffer */
2688\layout LyX-Code
2689
2690
2691\size small
2692 Rectangle_t *rectangle = 0; /* Type to decode */
2693\layout LyX-Code
2694
2695
2696\size small
2697 asn_dec_rval_t rval; /* Decoder return value */
2698\layout LyX-Code
2699
2700
2701\size small
2702 FILE *fp; /* Input file handler */
2703\layout LyX-Code
2704
2705
2706\size small
2707 size_t size; /* Number of bytes read */
2708\layout LyX-Code
2709
2710
2711\size small
2712 char *filename; /* Input file name */
2713\layout LyX-Code
2714
2715
2716\size small
2717
2718\layout LyX-Code
2719
2720
2721\size small
2722 /* Require a single filename argument */
2723\layout LyX-Code
2724
2725
2726\size small
2727 if(ac != 2) {
2728\layout LyX-Code
2729
2730
2731\size small
2732 fprintf(stderr,
2733\begin_inset Quotes sld
2734\end_inset
2735
2736Usage: %s <file.ber>
2737\backslash
2738n
2739\begin_inset Quotes srd
2740\end_inset
2741
2742, av[0]);
2743\layout LyX-Code
2744
2745
2746\size small
2747 exit(64); /* better, EX_USAGE */
2748\layout LyX-Code
2749
2750
2751\size small
2752 } else {
2753\layout LyX-Code
2754
2755
2756\size small
2757 filename = av[1];
2758\layout LyX-Code
2759
2760
2761\size small
2762 }
2763\layout LyX-Code
2764
2765
2766\size small
2767
2768\layout LyX-Code
2769
2770
2771\size small
2772 /* Open input file as read-only binary */
2773\layout LyX-Code
2774
2775
2776\size small
2777 fp = fopen(filename,
2778\begin_inset Quotes sld
2779\end_inset
2780
2781rb
2782\begin_inset Quotes srd
2783\end_inset
2784
2785);
2786\layout LyX-Code
2787
2788
2789\size small
2790 if(!fp) {
2791\layout LyX-Code
2792
2793
2794\size small
2795 perror(filename);
2796\layout LyX-Code
2797
2798
2799\size small
2800 exit(66); /* better, EX_NOINPUT */
2801\layout LyX-Code
2802
2803
2804\size small
2805 }
2806\layout LyX-Code
2807
2808
2809\size small
2810
2811\layout LyX-Code
2812
2813
2814\size small
2815 /* Read up to the buffer size */
2816\layout LyX-Code
2817
2818
2819\size small
2820 size = fread(buf, 1, sizeof(buf), fp);
2821\layout LyX-Code
2822
2823
2824\size small
2825 fclose(fp);
2826\layout LyX-Code
2827
2828
2829\size small
2830 if(!size) {
2831\layout LyX-Code
2832
2833
2834\size small
2835 fprintf(stderr,
2836\begin_inset Quotes sld
2837\end_inset
2838
2839%s: Empty or broken
2840\backslash
2841n
2842\begin_inset Quotes srd
2843\end_inset
2844
2845, filename);
2846\layout LyX-Code
2847
2848
2849\size small
2850 exit(65); /* better, EX_DATAERR */
2851\layout LyX-Code
2852
2853
2854\size small
2855 }
2856\layout LyX-Code
2857
2858
2859\size small
2860
2861\layout LyX-Code
2862
2863
2864\size small
2865 /* Decode the input buffer as Rectangle type */
2866\layout LyX-Code
2867
2868
2869\size small
2870 rval = ber_decode(0, &asn_DEF_Rectangle,
2871\layout LyX-Code
2872
2873
2874\size small
2875 (void **)&rectangle, buf, size);
2876\layout LyX-Code
2877
2878
2879\size small
2880 if(rval.code != RC_OK) {
2881\layout LyX-Code
2882
2883
2884\size small
2885 fprintf(stderr,
2886\layout LyX-Code
2887
2888
2889\size small
2890
2891\begin_inset Quotes sld
2892\end_inset
2893
2894%s: Broken Rectangle encoding at byte %ld
2895\backslash
2896n
2897\begin_inset Quotes srd
2898\end_inset
2899
2900,
2901\layout LyX-Code
2902
2903
2904\size small
2905 filename, (long)rval.consumed);
2906\layout LyX-Code
2907
2908
2909\size small
2910 exit(65); /* better, EX_DATAERR */
2911\layout LyX-Code
2912
2913
2914\size small
2915 }
2916\layout LyX-Code
2917
2918
2919\size small
2920
2921\layout LyX-Code
2922
2923
2924\size small
2925 /* Print the decoded Rectangle type as XML */
2926\layout LyX-Code
2927
2928
2929\size small
2930 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
2931\layout LyX-Code
2932
2933
2934\size small
2935
2936\layout LyX-Code
2937
2938
2939\size small
2940 return 0; /* Decoding finished successfully */
2941\layout LyX-Code
2942
2943
2944\size small
2945}
2946\end_deeper
2947\layout Enumerate
2948
vlmabd51e22005-01-17 11:05:04 +00002949Compile all files together using C compiler (varies by platform):
vlm4cb0edf2005-01-14 11:38:49 +00002950\begin_deeper
2951\layout LyX-Code
2952
2953
2954\emph on
2955cc -I.
vlmabd51e22005-01-17 11:05:04 +00002956 -o
2957\series bold
2958rdecode
2959\series default
2960 *.c
vlm4cb0edf2005-01-14 11:38:49 +00002961\end_deeper
2962\layout Enumerate
2963
vlma1fd7f92005-01-31 10:10:04 +00002964Voila! You have just created the BER decoder of a Rectangle type, named
2965
vlm4cb0edf2005-01-14 11:38:49 +00002966\series bold
2967rdecode
2968\series default
2969!
vlmc7737f62005-02-02 09:53:46 +00002970\layout Chapter
2971
2972Constraint validation examples
2973\layout Standard
2974
2975This chapter shows how to define ASN.1 constraints and use the generated
2976 validation code.
2977\layout Section
2978
2979Adding constraints into
2980\begin_inset Quotes sld
2981\end_inset
2982
2983Rectangle
2984\begin_inset Quotes srd
2985\end_inset
2986
2987 type
2988\layout Standard
2989
2990This example shows how to add basic constraints to the ASN.1 specification
2991 and how to invoke the constraints validation code in your application.
2992\layout Enumerate
2993
2994Create a file named
2995\series bold
2996rectangle.asn1
2997\series default
2998 with the following contents:
2999\begin_deeper
3000\layout LyX-Code
3001
3002RectangleModuleWithConstraints DEFINITIONS ::=
3003\layout LyX-Code
3004
3005BEGIN
3006\layout LyX-Code
3007
3008
3009\layout LyX-Code
3010
3011Rectangle ::= SEQUENCE {
3012\layout LyX-Code
3013
3014 height INTEGER (0..100), -- Value range constraint
3015\layout LyX-Code
3016
3017 width INTEGER (0..MAX) -- Makes width non-negative
3018\layout LyX-Code
3019
3020}
3021\layout LyX-Code
3022
3023
3024\layout LyX-Code
3025
3026END
3027\end_deeper
3028\layout Enumerate
3029
3030Compile the file according to procedures shown in the previous chapter.
3031\layout Enumerate
3032
3033Modify the Rectangle type processing routine (you can start with the main()
3034 routine shown in the Section
3035\begin_inset LatexCommand \vref{sec:A-Rectangle-Decoder}
3036
3037\end_inset
3038
3039) by placing the following snippet of code
3040\emph on
3041before
3042\emph default
3043 encoding and/or
3044\emph on
3045after
3046\emph default
3047 decoding the Rectangle type
3048\begin_inset Foot
3049collapsed true
3050
3051\layout Standard
3052
3053Placing the constraint checking code
3054\emph on
3055before
3056\emph default
3057 encoding helps to make sure you know the data is correct and within constraints
3058 before sharing the data with anyone else.
3059\layout Standard
3060
3061Placing the constraint checking code
3062\emph on
3063after
3064\emph default
3065 decoding, but before any further action depending on the decoded data,
3066 helps to make sure the application got the valid contents before making
3067 use of it.
3068\end_inset
3069
3070:
3071\begin_inset ERT
3072status Collapsed
3073
3074\layout Standard
3075
3076\backslash
3077clearpage{}
3078\end_inset
3079
3080
3081\begin_deeper
3082\layout LyX-Code
3083
3084
3085\size small
3086int ret; /* Return value */
3087\layout LyX-Code
3088
3089
3090\size small
3091char errbuf[128]; /* Buffer for error message */
3092\layout LyX-Code
3093
3094
3095\size small
3096size_t errlen = sizeof(errbuf); /* Size of the buffer */
3097\layout LyX-Code
3098
3099
3100\size small
3101
3102\layout LyX-Code
3103
3104
3105\size small
3106/* ...
3107 here may go Rectangle decoding code ...
3108 */
3109\layout LyX-Code
3110
3111
3112\size small
3113
3114\layout LyX-Code
3115
3116
3117\size small
vlm823f1442005-08-29 10:23:20 +00003118ret = asn_check_constraints(&asn_DEF_Rectangle,
vlmc7737f62005-02-02 09:53:46 +00003119\layout LyX-Code
3120
3121
3122\size small
3123 rectangle, errbuf, &errlen);
3124\layout LyX-Code
3125
3126
3127\size small
3128/* assert(errlen < sizeof(errbuf)); // you may rely on that */
3129\layout LyX-Code
3130
3131
3132\size small
3133if(ret) {
3134\layout LyX-Code
3135
3136
3137\size small
3138 fprintf(stderr,
3139\begin_inset Quotes sld
3140\end_inset
3141
3142Constraint validation failed: %s
3143\backslash
3144n
3145\begin_inset Quotes srd
3146\end_inset
3147
3148,
3149\layout LyX-Code
3150
3151
3152\size small
3153 errbuf /* errbuf is properly nul-terminated */
3154\layout LyX-Code
3155
3156
3157\size small
3158 );
3159\layout LyX-Code
3160
3161
3162\size small
3163 /* exit(...); // Replace with appropriate action */
3164\layout LyX-Code
3165
3166
3167\size small
3168}
3169\layout LyX-Code
3170
3171
3172\size small
3173
3174\layout LyX-Code
3175
3176
3177\size small
3178/* ...
3179 here may go Rectangle encoding code ...
3180 */
3181\end_deeper
3182\layout Enumerate
3183
3184Compile the resulting C code as shown in the previous chapters.
3185\layout Enumerate
3186
vlm602b7262005-02-02 11:06:38 +00003187Try to test the constraints checking code by assigning integer value 101
3188 to the
3189\series bold
3190.height
3191\series default
3192 member of the Rectangle structure, or a negative value to the
3193\series bold
3194.width
3195\series default
3196 member.
3197 In either case, the program should print
3198\begin_inset Quotes sld
3199\end_inset
3200
3201Constraint validation failed
3202\begin_inset Quotes srd
3203\end_inset
3204
3205 message, followed by the short explanation why validation did not succeed.
3206\layout Enumerate
3207
vlmc7737f62005-02-02 09:53:46 +00003208Done.
vlm856978f2005-02-21 14:16:10 +00003209\layout Part
3210
3211
3212\begin_inset LatexCommand \label{par:ASN.1-Basics}
3213
3214\end_inset
3215
3216ASN.1 Basics
3217\layout Chapter
3218
3219
3220\begin_inset LatexCommand \label{cha:Abstract-Syntax-Notation:}
3221
3222\end_inset
3223
3224Abstract Syntax Notation: ASN.1
3225\layout Standard
3226
3227
3228\emph on
3229This chapter defines some basic ASN.1 concepts and describes several most
3230 widely used types.
3231 It is by no means an authoritative or complete reference.
3232 For more complete ASN.1 description, please refer to Olivier Dubuisson's
3233 book
3234\begin_inset LatexCommand \cite{Dub00}
3235
3236\end_inset
3237
3238 or the ASN.1 body of standards itself
3239\begin_inset LatexCommand \cite{ITU-T/ASN.1}
3240
3241\end_inset
3242
3243.
3244\layout Standard
3245
3246The Abstract Syntax Notation One is used to formally describe the semantics
3247 of data transmitted across the network.
3248 Two communicating parties may have different formats of their native data
3249 types (i.e.
3250 number of bits in the integer type), thus it is important to have a way
3251 to describe the data in a manner which is independent from the particular
3252 machine's representation.
3253 The ASN.1 specifications are used to achieve the following:
3254\layout Itemize
3255
3256The specification expressed in the ASN.1 notation is a formal and precise
3257 way to communicate the data semantics to human readers;
3258\layout Itemize
3259
3260The ASN.1 specifications may be used as input for automatic compilers which
3261 produce the code for some target language (C, C++, Java, etc) to encode
3262 and decode the data according to some encoding rules (which are also defined
3263 by the ASN.1 standard).
3264\layout Standard
3265
3266Consider the following example:
3267\layout LyX-Code
3268
3269Rectangle ::= SEQUENCE {
3270\layout LyX-Code
3271
3272 height INTEGER,
3273\layout LyX-Code
3274
3275 width INTEGER
3276\layout LyX-Code
3277
3278}
3279\layout Standard
3280
3281This ASN.1 specification describes a constructed type,
3282\emph on
3283Rectangle
3284\emph default
3285, containing two integer fields.
3286 This specification may tell the reader that there exists this kind of data
3287 structure and that some entity may be prepared to send or receive it.
3288 The question on
3289\emph on
3290how
3291\emph default
3292 that entity is going to send or receive the
3293\emph on
3294encoded data
3295\emph default
3296 is outside the scope of ASN.1.
3297 For example, this data structure may be encoded according to some encoding
3298 rules and sent to the destination using the TCP protocol.
3299 The ASN.1 specifies several ways of encoding (or
3300\begin_inset Quotes sld
3301\end_inset
3302
3303serializing
3304\begin_inset Quotes srd
3305\end_inset
3306
3307, or
3308\begin_inset Quotes sld
3309\end_inset
3310
3311marshaling
3312\begin_inset Quotes srd
3313\end_inset
3314
3315) the data: BER, PER, XER and others, including CER and DER derivatives
3316 from BER.
3317\layout Standard
3318
3319The complete specification must be wrapped in a module, which looks like
3320 this:
3321\layout LyX-Code
3322
3323RectangleModule1
3324\layout LyX-Code
3325
3326 { iso org(3) dod(6) internet(1) private(4)
3327\layout LyX-Code
3328
3329 enterprise(1) spelio(9363) software(1)
3330\layout LyX-Code
3331
3332 asn1c(5) docs(2) rectangle(1) 1 }
3333\layout LyX-Code
3334
3335 DEFINITIONS AUTOMATIC TAGS ::=
3336\layout LyX-Code
3337
3338BEGIN
3339\layout LyX-Code
3340
3341
3342\layout LyX-Code
3343
3344-- This is a comment which describes nothing.
3345\layout LyX-Code
3346
3347Rectangle ::= SEQUENCE {
3348\layout LyX-Code
3349
3350 height INTEGER, -- Height of the rectangle
3351\layout LyX-Code
3352
3353 width INTEGER -- Width of the rectangle
3354\layout LyX-Code
3355
3356}
3357\layout LyX-Code
3358
3359
3360\layout LyX-Code
3361
3362END
3363\layout Standard
3364
3365The module header consists of module name (RectangleModule1), the module
3366 object identifier ({...}), a keyword
3367\begin_inset Quotes sld
3368\end_inset
3369
3370DEFINITIONS
3371\begin_inset Quotes srd
3372\end_inset
3373
3374, a set of module flags (AUTOMATIC TAGS) and
3375\begin_inset Quotes sld
3376\end_inset
3377
3378::= BEGIN
3379\begin_inset Quotes srd
3380\end_inset
3381
3382.
3383 The module ends with an
3384\begin_inset Quotes sld
3385\end_inset
3386
3387END
3388\begin_inset Quotes srd
3389\end_inset
3390
3391 statement.
3392\layout Section
3393
3394Some of the ASN.1 Basic Types
3395\layout Subsection
3396
3397The BOOLEAN type
3398\layout Standard
3399
3400The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF or
3401 a similar kind of two-way choice.
3402\layout Subsection
3403
3404The INTEGER type
3405\layout Standard
3406
3407The INTEGER type is a signed natural number type without any restrictions
3408 on its size.
3409 If the automatic checking on INTEGER value bounds are necessary, the subtype
3410 constraints must be used.
3411\layout LyX-Code
3412
3413SimpleInteger ::= INTEGER
3414\layout LyX-Code
3415
3416
3417\layout LyX-Code
3418
3419-- An integer with a very limited range
3420\layout LyX-Code
3421
3422SmallPositiveInt ::= INTEGER (0..127)
3423\layout LyX-Code
3424
3425
3426\layout LyX-Code
3427
3428-- Integer, negative
3429\layout LyX-Code
3430
3431NegativeInt ::= INTEGER (MIN..0)
3432\layout Subsection
3433
3434The ENUMERATED type
3435\layout Standard
3436
3437The ENUMERATED type is semantically equivalent to the INTEGER type with
3438 some integer values explicitly named.
3439\layout LyX-Code
3440
3441FruitId ::= ENUMERATED { apple(1), orange(2) }
3442\layout LyX-Code
3443
3444
3445\layout LyX-Code
3446
3447-- The numbers in braces are optional,
3448\layout LyX-Code
3449
3450-- the enumeration can be performed
3451\layout LyX-Code
3452
3453-- automatically by the compiler
3454\layout LyX-Code
3455
3456ComputerOSType ::= ENUMERATED {
3457\layout LyX-Code
3458
3459 FreeBSD, -- acquires value 0
3460\layout LyX-Code
3461
3462 Windows, -- acquires value 1
3463\layout LyX-Code
3464
3465 Solaris(5), -- remains 5
3466\layout LyX-Code
3467
3468 Linux, -- becomes 6
3469\layout LyX-Code
3470
3471 MacOS -- becomes 7
3472\layout LyX-Code
3473
3474}
3475\layout Subsection
3476
3477The OCTET STRING type
3478\layout Standard
3479
3480This type models the sequence of 8-bit bytes.
3481 This may be used to transmit some opaque data or data serialized by other
3482 types of encoders (i.e.
3483 video file, photo picture, etc).
3484\layout Subsection
3485
3486The OBJECT IDENTIFIER type
3487\layout Standard
3488
3489The OBJECT IDENTIFIER is used to represent the unique identifier of any
3490 object, starting from the very root of the registration tree.
3491 If your organization needs to uniquely identify something (a router, a
3492 room, a person, a standard, or whatever), you are encouraged to get your
3493 own identification subtree at
3494\begin_inset LatexCommand \htmlurl{http://www.iana.org/protocols/forms.htm}
3495
3496\end_inset
3497
3498.
3499\layout Standard
3500
3501For example, the very first ASN.1 module in this Chapter (RectangleModule1)
3502 has the following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
3503\layout LyX-Code
3504
3505ExampleOID ::= OBJECT IDENTIFIER
3506\layout LyX-Code
3507
3508
3509\layout LyX-Code
3510
3511rectangleModule1-oid ExampleOID
3512\layout LyX-Code
3513
3514 ::= { 1 3 6 1 4 1 9363 1 5 2 1 1 }
3515\layout LyX-Code
3516
3517
3518\layout LyX-Code
3519
3520-- An identifier of the Internet.
3521\layout LyX-Code
3522
3523internet-id OBJECT IDENTIFIER
3524\layout LyX-Code
3525
3526 ::= { iso(1) identified-organization(3)
3527\layout LyX-Code
3528
3529 dod(6) internet(1) }
3530\layout Standard
3531
3532As you see, names are optional.
3533\layout Subsection
3534
3535The RELATIVE-OID type
3536\layout Standard
3537
3538The RELATIVE-OID type has the semantics of a subtree of an OBJECT IDENTIFIER.
3539 There may be no need to repeat the whole sequence of numbers from the root
3540 of the registration tree where the only thing of interest is some of the
3541 tree's subsequence.
3542\layout LyX-Code
3543
3544this-document RELATIVE-OID ::= { docs(2) usage(1) }
3545\layout LyX-Code
3546
3547
3548\layout LyX-Code
3549
3550this-example RELATIVE-OID ::= {
3551\layout LyX-Code
3552
3553 this-document assorted-examples(0) this-example(1) }
3554\layout Section
3555
3556Some of the ASN.1 String Types
3557\layout Subsection
3558
3559The IA5String type
3560\layout Standard
3561
3562This is essentially the ASCII, with 128 character codes available (7 lower
3563 bits of an 8-bit byte).
3564\layout Subsection
3565
3566The UTF8String type
3567\layout Standard
3568
3569This is the character string which encodes the full Unicode range (4 bytes)
3570 using multibyte character sequences.
3571\layout Subsection
3572
3573The NumericString type
3574\layout Standard
3575
3576This type represents the character string with the alphabet consisting of
3577 numbers (
3578\begin_inset Quotes sld
3579\end_inset
3580
35810
3582\begin_inset Quotes srd
3583\end_inset
3584
3585 to
3586\begin_inset Quotes sld
3587\end_inset
3588
35899
3590\begin_inset Quotes srd
3591\end_inset
3592
3593) and a space.
3594\layout Subsection
3595
3596The PrintableString type
3597\layout Standard
3598
3599The character string with the following alphabet: space,
3600\begin_inset Quotes sld
3601\end_inset
3602
3603
3604\series bold
3605'
3606\series default
3607
3608\begin_inset Quotes srd
3609\end_inset
3610
3611 (single quote),
3612\begin_inset Quotes sld
3613\end_inset
3614
3615
3616\series bold
3617(
3618\series default
3619
3620\begin_inset Quotes sld
3621\end_inset
3622
3623,
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,
3636\begin_inset Quotes sld
3637\end_inset
3638
3639
3640\series bold
3641+
3642\series default
3643
3644\begin_inset Quotes srd
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 (comma),
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,
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, digits (
3696\begin_inset Quotes sld
3697\end_inset
3698
36990
3700\begin_inset Quotes srd
3701\end_inset
3702
3703 to
3704\begin_inset Quotes sld
3705\end_inset
3706
37079
3708\begin_inset Quotes srd
3709\end_inset
3710
3711),
3712\begin_inset Quotes sld
3713\end_inset
3714
3715
3716\series bold
3717:
3718\series default
3719
3720\begin_inset Quotes srd
3721\end_inset
3722
3723,
3724\begin_inset Quotes sld
3725\end_inset
3726
3727
3728\series bold
3729=
3730\series default
3731
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, upper-case and lower-case letters (
3748\begin_inset Quotes sld
3749\end_inset
3750
3751A
3752\begin_inset Quotes srd
3753\end_inset
3754
3755 to
3756\begin_inset Quotes sld
3757\end_inset
3758
3759Z
3760\begin_inset Quotes srd
3761\end_inset
3762
3763 and
3764\begin_inset Quotes sld
3765\end_inset
3766
3767a
3768\begin_inset Quotes srd
3769\end_inset
3770
3771 to
3772\begin_inset Quotes sld
3773\end_inset
3774
3775z
3776\begin_inset Quotes srd
3777\end_inset
3778
3779).
3780\layout Subsection
3781
3782The VisibleString type
3783\layout Standard
3784
3785The character string with the alphabet which is more or less a subset of
3786 ASCII between the space and the
3787\begin_inset Quotes sld
3788\end_inset
3789
3790
3791\series bold
3792~
3793\series default
3794
3795\begin_inset Quotes srd
3796\end_inset
3797
3798 symbol (tilde).
3799\layout Standard
3800
3801Alternatively, the alphabet may be described as the PrintableString alphabet
3802 presented earlier, plus the following characters:
3803\begin_inset Quotes sld
3804\end_inset
3805
3806
3807\series bold
3808!
3809\series default
3810
3811\begin_inset Quotes srd
3812\end_inset
3813
3814,
3815\begin_inset Quotes sld
3816\end_inset
3817
3818
3819\series bold
3820
3821\begin_inset Quotes srd
3822\end_inset
3823
3824
3825\series default
3826
3827\begin_inset Quotes srd
3828\end_inset
3829
3830,
3831\begin_inset Quotes sld
3832\end_inset
3833
3834
3835\series bold
3836#
3837\series default
3838
3839\begin_inset Quotes srd
3840\end_inset
3841
3842,
3843\begin_inset Quotes sld
3844\end_inset
3845
3846
3847\series bold
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\backslash
3946
3947\series default
3948
3949\begin_inset Quotes srd
3950\end_inset
3951
3952,
3953\begin_inset Quotes sld
3954\end_inset
3955
3956
3957\series bold
3958]
3959\series default
3960
3961\begin_inset Quotes srd
3962\end_inset
3963
3964,
3965\begin_inset Quotes sld
3966\end_inset
3967
3968
3969\series bold
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 (single left quote),
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,
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\layout Section
4050
4051ASN.1 Constructed Types
4052\layout Subsection
4053
4054The SEQUENCE type
4055\layout Standard
4056
4057This is an ordered collection of other simple or constructed types.
4058 The SEQUENCE constructed type resembles the C
4059\begin_inset Quotes sld
4060\end_inset
4061
4062struct
4063\begin_inset Quotes srd
4064\end_inset
4065
4066 statement.
4067\layout LyX-Code
4068
4069Address ::= SEQUENCE {
4070\layout LyX-Code
4071
4072 -- The apartment number may be omitted
4073\layout LyX-Code
4074
4075 apartmentNumber NumericString OPTIONAL,
4076\layout LyX-Code
4077
4078 streetName PrintableString,
4079\layout LyX-Code
4080
4081 cityName PrintableString,
4082\layout LyX-Code
4083
4084 stateName PrintableString,
4085\layout LyX-Code
4086
4087 -- This one may be omitted too
4088\layout LyX-Code
4089
4090 zipNo NumericString OPTIONAL
4091\layout LyX-Code
4092
4093}
4094\layout Subsection
4095
4096The SET type
4097\layout Standard
4098
4099This is a collection of other simple or constructed types.
4100 Ordering is not important.
4101 The data may arrive in the order which is different from the order of specifica
4102tion.
4103 Data is encoded in the order not necessarily corresponding to the order
4104 of specification.
4105\layout Subsection
4106
4107The CHOICE type
4108\layout Standard
4109
4110This type is just a choice between the subtypes specified in it.
4111 The CHOICE type contains at most one of the subtypes specified, and it
4112 is always implicitly known which choice is being decoded or encoded.
4113 This one resembles the C
4114\begin_inset Quotes sld
4115\end_inset
4116
4117union
4118\begin_inset Quotes srd
4119\end_inset
4120
4121 statement.
4122\layout Standard
4123
4124The following type defines a response code, which may be either an integer
4125 code or a boolean
4126\begin_inset Quotes sld
4127\end_inset
4128
4129true
4130\begin_inset Quotes srd
4131\end_inset
4132
4133/
4134\begin_inset Quotes srd
4135\end_inset
4136
4137false
4138\begin_inset Quotes srd
4139\end_inset
4140
4141 code.
4142\layout LyX-Code
4143
4144ResponseCode ::= CHOICE {
4145\layout LyX-Code
4146
4147 intCode INTEGER,
4148\layout LyX-Code
4149
4150 boolCode BOOLEAN
4151\layout LyX-Code
4152
4153}
4154\layout LyX-Code
4155
4156\layout Subsection
4157
4158The SEQUENCE OF type
4159\layout Standard
4160
4161This one is the list (array) of simple or constructed types:
4162\layout LyX-Code
4163
4164-- Example 1
4165\layout LyX-Code
4166
4167ManyIntegers ::= SEQUENCE OF INTEGER
4168\layout LyX-Code
4169
4170
4171\layout LyX-Code
4172
4173-- Example 2
4174\layout LyX-Code
4175
4176ManyRectangles ::= SEQUENCE OF Rectangle
4177\layout LyX-Code
4178
4179
4180\layout LyX-Code
4181
4182-- More complex example:
4183\layout LyX-Code
4184
4185-- an array of structures defined in place.
4186\layout LyX-Code
4187
4188ManyCircles ::= SEQUENCE OF SEQUENCE {
4189\layout LyX-Code
4190
4191 radius INTEGER
4192\layout LyX-Code
4193
4194 }
4195\layout Subsection
4196
4197The SET OF type
4198\layout Standard
4199
4200The SET OF type models the bag of structures.
4201 It resembles the SEQUENCE OF type, but the order is not important: i.e.
4202 the elements may arrive in the order which is not necessarily the same
4203 as the in-memory order on the remote machines.
4204\layout LyX-Code
4205
4206-- A set of structures defined elsewhere
4207\layout LyX-Code
4208
4209SetOfApples :: SET OF Apple
4210\layout LyX-Code
4211
4212
4213\layout LyX-Code
4214
4215-- Set of integers encoding the kind of a fruit
4216\layout LyX-Code
4217
4218FruitBag ::= SET OF ENUMERATED { apple, orange }
vlme18adea2004-06-03 03:49:45 +00004219\layout Bibliography
vlmfcec8e52004-08-23 15:12:04 +00004220\bibitem [ASN1C]{ASN1C}
4221
vlmc7737f62005-02-02 09:53:46 +00004222The Open Source ASN.1 Compiler.
vlmfcec8e52004-08-23 15:12:04 +00004223
vlmc7737f62005-02-02 09:53:46 +00004224\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c}
vlmfcec8e52004-08-23 15:12:04 +00004225
4226\end_inset
4227
4228
4229\layout Bibliography
vlm4cb0edf2005-01-14 11:38:49 +00004230\bibitem [AONL]{AONL}
4231
4232Online ASN.1 Compiler.
4233
4234\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c/asn1c.cgi}
4235
4236\end_inset
4237
4238
4239\layout Bibliography
vlme18adea2004-06-03 03:49:45 +00004240\bibitem [Dub00]{Dub00}
4241
vlmaf2155e2004-10-20 15:48:55 +00004242Olivier Dubuisson ---
vlme18adea2004-06-03 03:49:45 +00004243\emph on
4244ASN.1 Communication between heterogeneous systems
4245\emph default
vlmaf2155e2004-10-20 15:48:55 +00004246 --- Morgan Kaufmann Publishers, 2000.
vlme18adea2004-06-03 03:49:45 +00004247
4248\begin_inset LatexCommand \htmlurl{http://asn1.elibel.tm.fr/en/book/}
4249
4250\end_inset
4251
4252.
4253 ISBN:0-12-6333361-0.
4254\layout Bibliography
4255\bibitem [ITU-T/ASN.1]{ITU-T/ASN.1}
4256
4257ITU-T Study Group 17 -- Languages for Telecommunication Systems
4258\begin_inset LatexCommand \url{http://www.itu.int/ITU-T/studygroups/com17/languages/}
4259
4260\end_inset
4261
4262
4263\the_end