blob: 3c6ed97b5f662350afff2a39551653e035b4d291 [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
vlm823f1442005-08-29 10:23:20 +000074href{http://lionet.info/asn1c}{asn1c-0.9.18}}
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
vlm4cc45722005-07-24 08:28:39 +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
vlm4cc45722005-07-24 08:28:39 +0000839-findirect-choice
840\end_inset
841</cell>
842<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
843\begin_inset Text
844
845\layout Standard
846
847When generating code for a CHOICE type, compile the CHOICE members as indirect
848 pointers instead of declaring them inline.
849 Consider using this option together with
850\series bold
851-fno-include-deps
852\series default
853 to prevent circular references.
854
855\end_inset
856</cell>
857</row>
858<row topline="true">
859<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
860\begin_inset Text
861
862\layout Standard
863
864-fknown-extern-type=
865\emph on
866<name>
867\end_inset
868</cell>
869<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
870\begin_inset Text
871
872\layout Standard
873
874Pretend the specified type is known.
875 The compiler will assume the target language source files for the given
876 type have been provided manually.
877
878\end_inset
879</cell>
880</row>
881<row topline="true">
882<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
883\begin_inset Text
884
885\layout Standard
886
vlm37d47c02005-03-05 01:07:04 +0000887
888\size small
vlm03af0f02004-09-14 12:48:17 +0000889-fnative-types
vlmfcec8e52004-08-23 15:12:04 +0000890\end_inset
891</cell>
892<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
893\begin_inset Text
894
895\layout Standard
896
897
898\size small
vlm03af0f02004-09-14 12:48:17 +0000899Use the native machine's data types (int, double) whenever possible, instead
vlmc3bfd812004-09-30 22:26:19 +0000900 of the compound INTEGER_t, ENUMERATED_t and REAL_t types.
vlmfcec8e52004-08-23 15:12:04 +0000901
902\end_inset
903</cell>
904</row>
905<row topline="true">
906<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
907\begin_inset Text
908
909\layout Standard
910
vlm37d47c02005-03-05 01:07:04 +0000911
912\size small
vlmbfc49bd2004-09-26 13:13:13 +0000913-fno-constraints
914\end_inset
915</cell>
916<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
917\begin_inset Text
918
919\layout Standard
920
vlm37d47c02005-03-05 01:07:04 +0000921
922\size small
vlmbfc49bd2004-09-26 13:13:13 +0000923Do not generate ASN.1 subtype constraint checking code.
vlm37d47c02005-03-05 01:07:04 +0000924 This may produce a shorter executable.
vlmbfc49bd2004-09-26 13:13:13 +0000925\end_inset
926</cell>
927</row>
928<row topline="true">
929<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
930\begin_inset Text
931
932\layout Standard
933
vlm37d47c02005-03-05 01:07:04 +0000934
935\size small
936-fno-include-deps
937\end_inset
938</cell>
939<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
940\begin_inset Text
941
942\layout Standard
943
944
945\size small
946Do not generate courtesy #include lines for non-critical dependencies.
947\end_inset
948</cell>
949</row>
950<row topline="true">
951<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
952\begin_inset Text
953
954\layout Standard
955
956
957\size small
vlmfcec8e52004-08-23 15:12:04 +0000958-funnamed-unions
959\end_inset
960</cell>
961<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
962\begin_inset Text
963
964\layout Standard
965
966
967\size small
968Enable unnamed unions in the definitions of target language's structures.
969\end_inset
970</cell>
971</row>
972<row topline="true" bottomline="true">
973<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
974\begin_inset Text
975
976\layout Standard
977
vlm37d47c02005-03-05 01:07:04 +0000978
979\size small
vlmfcec8e52004-08-23 15:12:04 +0000980-ftypes88
981\end_inset
982</cell>
983<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
984\begin_inset Text
985
986\layout Standard
987
988
989\size small
vlmc3bfd812004-09-30 22:26:19 +0000990Pretend to support only ASN.1:1988 embedded types.
991 Certain reserved words, such as UniversalString and BMPString, become ordinary
992 type references and may be redefined by the specification.
vlmfcec8e52004-08-23 15:12:04 +0000993\end_inset
994</cell>
995</row>
vlm4cc45722005-07-24 08:28:39 +0000996<row topline="true">
vlmfcec8e52004-08-23 15:12:04 +0000997<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
998\begin_inset Text
999
1000\layout Standard
1001
1002
1003\series bold
vlm37d47c02005-03-05 01:07:04 +00001004\size small
vlmfcec8e52004-08-23 15:12:04 +00001005Output Options
1006\end_inset
1007</cell>
1008<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1009\begin_inset Text
1010
1011\layout Standard
1012
1013
1014\series bold
vlm37d47c02005-03-05 01:07:04 +00001015\size small
vlmfcec8e52004-08-23 15:12:04 +00001016Description
1017\end_inset
1018</cell>
1019</row>
1020<row topline="true">
1021<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1022\begin_inset Text
1023
1024\layout Standard
1025
vlm37d47c02005-03-05 01:07:04 +00001026
1027\size small
vlmfcec8e52004-08-23 15:12:04 +00001028-print-constraints
1029\end_inset
1030</cell>
1031<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1032\begin_inset Text
1033
1034\layout Standard
1035
1036
1037\size small
1038When -EF are also specified, this option forces the compiler to explain
1039 its internal understanding of subtype constraints.
1040\end_inset
1041</cell>
1042</row>
1043<row topline="true" bottomline="true">
1044<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1045\begin_inset Text
1046
1047\layout Standard
1048
vlm37d47c02005-03-05 01:07:04 +00001049
1050\size small
vlmfcec8e52004-08-23 15:12:04 +00001051-print-lines
1052\end_inset
1053</cell>
1054<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1055\begin_inset Text
1056
1057\layout Standard
1058
1059
1060\size small
1061Generate "-- #line" comments in -E output.
1062\end_inset
1063</cell>
1064</row>
1065</lyxtabular>
1066
1067\end_inset
1068
1069
vlm856978f2005-02-21 14:16:10 +00001070\layout Chapter
1071
1072Using the ASN.1 Compiler
vlmfcec8e52004-08-23 15:12:04 +00001073\layout Section
vlme18adea2004-06-03 03:49:45 +00001074
vlm856978f2005-02-21 14:16:10 +00001075Invoking the ASN.1 helper code
vlmc3bfd812004-09-30 22:26:19 +00001076\begin_inset OptArg
1077collapsed true
1078
1079\layout Standard
1080
1081Invoking the helper code
1082\end_inset
1083
1084
vlme18adea2004-06-03 03:49:45 +00001085\layout Standard
1086
vlm485123c2005-02-22 07:28:26 +00001087First of all, you should include one or more header files into your application.
1088 Typically, it is enough to include the header file of the main PDU type.
1089 For our Rectangle module, including the Rectangle.h file is sufficient:
vlme18adea2004-06-03 03:49:45 +00001090\layout LyX-Code
1091
1092#include <Rectangle.h>
1093\layout Standard
1094
1095The header files defines the C structure corresponding to the ASN.1 definition
1096 of a rectangle and the declaration of the ASN.1 type descriptor, which is
1097 used as an argument to most of the functions provided by the ASN.1 module.
1098 For example, here is the code which frees the Rectangle_t structure:
1099\layout LyX-Code
1100
vlmfcec8e52004-08-23 15:12:04 +00001101Rectangle_t *rect = ...;
1102\layout LyX-Code
1103
1104
vlme18adea2004-06-03 03:49:45 +00001105\layout LyX-Code
1106
vlm823f1442005-08-29 10:23:20 +00001107asn_DEF_Rectangle.free_struct(&asn_DEF_Rectangle,
vlme18adea2004-06-03 03:49:45 +00001108\layout LyX-Code
1109
1110 rect, 0);
1111\layout Standard
1112
1113This code defines a
1114\emph on
1115rect
1116\emph default
1117 pointer which points to the Rectangle_t structure which needs to be freed.
vlm485123c2005-02-22 07:28:26 +00001118 The second line invokes the generic
1119\emph on
1120free_struct()
1121\emph default
1122 routine created specifically for this Rectangle_t structure.
vlme18adea2004-06-03 03:49:45 +00001123 The
1124\emph on
vlmba56d852004-09-29 13:29:17 +00001125asn_DEF_Rectangle
vlme18adea2004-06-03 03:49:45 +00001126\emph default
vlm485123c2005-02-22 07:28:26 +00001127 is the type descriptor, which holds a collection of routines to deal with
1128 the Rectangle_t structure.
vlme18adea2004-06-03 03:49:45 +00001129\layout Standard
1130
vlm485123c2005-02-22 07:28:26 +00001131The following member functions of the asn_DEF_Rectangle type descriptor
1132 are of interest:
vlme18adea2004-06-03 03:49:45 +00001133\layout Description
1134
vlme18adea2004-06-03 03:49:45 +00001135ber_decoder This is the generic
1136\emph on
1137restartable
1138\begin_inset Foot
1139collapsed false
1140
1141\layout Standard
1142
1143Restartable means that if the decoder encounters the end of the buffer,
1144 it will fail, but may later be invoked again with the rest of the buffer
1145 to continue decoding.
1146\end_inset
1147
1148
1149\emph default
1150BER decoder (Basic Encoding Rules).
1151 This decoder would create and/or fill the target structure for you.
1152 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001153\begin_inset LatexCommand \vref{sub:Decoding-BER}
vlme18adea2004-06-03 03:49:45 +00001154
1155\end_inset
1156
1157.
1158\layout Description
1159
1160der_encoder This is the generic DER encoder (Distinguished Encoding Rules).
vlm68d43e92004-09-24 20:56:07 +00001161 This encoder will take the target structure and encode it into a series
vlme18adea2004-06-03 03:49:45 +00001162 of bytes.
1163 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001164\begin_inset LatexCommand \vref{sub:Encoding-DER}
vlme18adea2004-06-03 03:49:45 +00001165
1166\end_inset
1167
1168.
1169\layout Description
1170
vlm856978f2005-02-21 14:16:10 +00001171xer_encoder This is the XER encoder (XML Encoding Rules).
vlm68d43e92004-09-24 20:56:07 +00001172 This encoder will take the target structure and represent it as an XML
vlm856978f2005-02-21 14:16:10 +00001173 (text) document using either BASIC-XER or CANONICAL-XER encoding rules.
vlm68d43e92004-09-24 20:56:07 +00001174 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001175\begin_inset LatexCommand \vref{sub:Encoding-XER}
1176
1177\end_inset
1178
1179.
1180\layout Description
1181
1182xer_decoder This is the generic XER decoder.
1183 It takes both BASIC-XER or CANONICAL-XER encodings and deserializes the
1184 data into a local, machine-dependent representation.
1185 Please refer to Section
1186\begin_inset LatexCommand \vref{sub:Decoding-XER}
vlm68d43e92004-09-24 20:56:07 +00001187
1188\end_inset
1189
1190.
1191\layout Description
1192
1193check_constraints Check that the contents of the target structure are semantical
1194ly valid and constrained to appropriate implicit or explicit subtype constraints.
1195 Please refer to Section
1196\begin_inset LatexCommand \vref{sub:Validating-the-target}
1197
1198\end_inset
1199
1200.
1201\layout Description
1202
vlme18adea2004-06-03 03:49:45 +00001203print_struct This function convert the contents of the passed target structure
1204 into human readable form.
1205 This form is not formal and cannot be converted back into the structure,
1206 but it may turn out to be useful for debugging or quick-n-dirty printing.
1207 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001208\begin_inset LatexCommand \vref{sub:Printing-the-target}
vlme18adea2004-06-03 03:49:45 +00001209
1210\end_inset
1211
1212.
1213\layout Description
1214
1215free_struct This is a generic disposal which frees the target structure.
1216 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001217\begin_inset LatexCommand \vref{sub:Freeing-the-target}
vlm68d43e92004-09-24 20:56:07 +00001218
1219\end_inset
1220
1221.
1222\layout Standard
1223
vlme18adea2004-06-03 03:49:45 +00001224Each of the above function takes the type descriptor (
1225\emph on
vlmba56d852004-09-29 13:29:17 +00001226asn_DEF_\SpecialChar \ldots{}
vlme18adea2004-06-03 03:49:45 +00001227
1228\emph default
1229) and the target structure (
1230\emph on
1231rect
1232\emph default
1233, in the above example).
vlm856978f2005-02-21 14:16:10 +00001234\layout Subsection
1235
1236
1237\begin_inset LatexCommand \label{sub:Decoding-BER}
1238
1239\end_inset
1240
1241Decoding BER
vlme18adea2004-06-03 03:49:45 +00001242\layout Standard
1243
vlm856978f2005-02-21 14:16:10 +00001244The Basic Encoding Rules describe the most widely used (by the ASN.1 community)
vlm485123c2005-02-22 07:28:26 +00001245 way to encode and decode a given structure in a machine-independent way.
vlm856978f2005-02-21 14:16:10 +00001246 Several other encoding rules (CER, DER) define a more restrictive versions
1247 of BER, so the generic BER parser is also capable of decoding the data
1248 encoded by CER and DER encoders.
1249 The opposite is not true.
1250\layout Standard
1251
vlm485123c2005-02-22 07:28:26 +00001252
1253\emph on
vlm856978f2005-02-21 14:16:10 +00001254The ASN.1 compiler provides the generic BER decoder which is implicitly capable
1255 of decoding BER, CER and DER encoded data.
1256\layout Standard
1257
1258The decoder is restartable (stream-oriented), which means that in case the
1259 buffer has less data than it is expected, the decoder will process whatever
1260 there is available and ask for more data to be provided.
1261 Please note that the decoder may actually process less data than it was
1262 given in the buffer, which means that you must be able to make the next
1263 buffer contain the unprocessed part of the previous buffer.
1264\layout Standard
1265
1266Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
1267\layout Itemize
1268
1269You may concatenate these buffers and feed the BER decoder with 300 bytes
1270 of data, or
1271\layout Itemize
1272
1273You may feed it the first buffer of 100 bytes of data, realize that the
1274 ber_decoder consumed only 95 bytes from it and later feed the decoder with
1275 205 bytes buffer which consists of 5 unprocessed bytes from the first buffer
vlm485123c2005-02-22 07:28:26 +00001276 and the additional 200 bytes from the second buffer.
vlm856978f2005-02-21 14:16:10 +00001277\layout Standard
1278
vlm485123c2005-02-22 07:28:26 +00001279This is not as convenient as it could be (like, the BER encoder could consume
vlm856978f2005-02-21 14:16:10 +00001280 the whole 100 bytes and keep these 5 bytes in some temporary storage),
vlm485123c2005-02-22 07:28:26 +00001281 but in case of existing stream based processing it might actually fit well
1282 into existing algorithm.
vlm856978f2005-02-21 14:16:10 +00001283 Suggestions are welcome.
1284\layout Standard
1285
vlm485123c2005-02-22 07:28:26 +00001286Here is the simplest example of BER decoding.
vlme18adea2004-06-03 03:49:45 +00001287\layout LyX-Code
1288
1289Rectangle_t *
1290\layout LyX-Code
1291
vlmfcec8e52004-08-23 15:12:04 +00001292simple_deserializer(const void *buffer, size_t buf_size) {
vlme18adea2004-06-03 03:49:45 +00001293\layout LyX-Code
1294
1295 Rectangle_t *rect = 0; /* Note this 0! */
1296\layout LyX-Code
1297
vlmaf2155e2004-10-20 15:48:55 +00001298 asn_dec_rval_t rval;
vlme18adea2004-06-03 03:49:45 +00001299\layout LyX-Code
1300
1301
1302\layout LyX-Code
1303
vlm856978f2005-02-21 14:16:10 +00001304 rval =
1305\series bold
vlm823f1442005-08-29 10:23:20 +00001306asn_DEF_Rectangle.ber_decoder
vlm856978f2005-02-21 14:16:10 +00001307\series default
1308(0,
vlme18adea2004-06-03 03:49:45 +00001309\layout LyX-Code
1310
vlmba56d852004-09-29 13:29:17 +00001311 &asn_DEF_Rectangle,
vlme18adea2004-06-03 03:49:45 +00001312\layout LyX-Code
1313
1314 (void **)&rect,
1315\layout LyX-Code
1316
1317 buffer, buf_size,
1318\layout LyX-Code
1319
1320 0);
1321\layout LyX-Code
1322
1323
1324\layout LyX-Code
1325
1326 if(rval
1327\series bold
1328.code
1329\series default
1330 == RC_OK) {
1331\layout LyX-Code
1332
1333 return rect; /* Decoding succeeded */
1334\layout LyX-Code
1335
1336 } else {
1337\layout LyX-Code
1338
vlmfcec8e52004-08-23 15:12:04 +00001339 /* Free partially decoded rect */
1340\layout LyX-Code
1341
vlm823f1442005-08-29 10:23:20 +00001342 asn_DEF_Rectangle.free_struct(
vlme18adea2004-06-03 03:49:45 +00001343\layout LyX-Code
1344
vlmba56d852004-09-29 13:29:17 +00001345 &asn_DEF_Rectangle, rect, 0);
vlme18adea2004-06-03 03:49:45 +00001346\layout LyX-Code
1347
1348 return 0;
1349\layout LyX-Code
1350
1351 }
1352\layout LyX-Code
1353
1354}
1355\layout Standard
1356
1357The code above defines a function,
1358\emph on
1359simple_deserializer
1360\emph default
vlm485123c2005-02-22 07:28:26 +00001361, which takes a buffer and its length and is expected to return a pointer
1362 to the Rectangle_t structure.
vlme18adea2004-06-03 03:49:45 +00001363 Inside, it tries to convert the bytes passed into the target structure
vlm485123c2005-02-22 07:28:26 +00001364 (rect) using the BER decoder and returns the rect pointer afterwards.
vlme18adea2004-06-03 03:49:45 +00001365 If the structure cannot be deserialized, it frees the memory which might
1366 be left allocated by the unfinished
1367\emph on
1368ber_decoder
1369\emph default
vlm856978f2005-02-21 14:16:10 +00001370 routine and returns 0 (no data).
vlm485123c2005-02-22 07:28:26 +00001371 (This
vlme18adea2004-06-03 03:49:45 +00001372\series bold
vlm856978f2005-02-21 14:16:10 +00001373 freeing is necessary
vlme18adea2004-06-03 03:49:45 +00001374\series default
1375 because the ber_decoder is a restartable procedure, and may fail just because
vlm485123c2005-02-22 07:28:26 +00001376 there is more data needs to be provided before decoding could be finalized).
vlme18adea2004-06-03 03:49:45 +00001377 The code above obviously does not take into account the way the
1378\emph on
vlm485123c2005-02-22 07:28:26 +00001379ber_decoder()
vlme18adea2004-06-03 03:49:45 +00001380\emph default
1381 failed, so the freeing is necessary because the part of the buffer may
1382 already be decoded into the structure by the time something goes wrong.
1383\layout Standard
1384
vlm485123c2005-02-22 07:28:26 +00001385A little less wordy would be to invoke a globally available
vlme18adea2004-06-03 03:49:45 +00001386\emph on
vlm485123c2005-02-22 07:28:26 +00001387ber_decode()
vlme18adea2004-06-03 03:49:45 +00001388\emph default
vlm485123c2005-02-22 07:28:26 +00001389 function instead of dereferencing the asn_DEF_Rectangle type descriptor:
vlme18adea2004-06-03 03:49:45 +00001390\layout LyX-Code
1391
vlmba56d852004-09-29 13:29:17 +00001392rval = ber_decode(0, &asn_DEF_Rectangle, (void **)&rect,
vlme18adea2004-06-03 03:49:45 +00001393\layout LyX-Code
1394
1395 buffer, buf_size);
1396\layout Standard
1397
vlm823f1442005-08-29 10:23:20 +00001398Note that the initial (asn_DEF_Rectangle.ber_decoder) reference is gone,
vlme18adea2004-06-03 03:49:45 +00001399 and also the last argument (0) is no longer necessary.
1400\layout Standard
1401
vlm856978f2005-02-21 14:16:10 +00001402These two ways of BER decoder invocations are fully equivalent.
vlme18adea2004-06-03 03:49:45 +00001403\layout Standard
1404
1405The BER de
1406\emph on
1407coder
1408\emph default
vlmba56d852004-09-29 13:29:17 +00001409 may fail because of (
vlme18adea2004-06-03 03:49:45 +00001410\emph on
1411the following RC_\SpecialChar \ldots{}
1412 codes are defined in ber_decoder.h
1413\emph default
1414):
1415\layout Itemize
1416
1417RC_WMORE: There is more data expected than it is provided (stream mode continuat
1418ion feature);
1419\layout Itemize
1420
1421RC_FAIL: General failure to decode the buffer;
1422\layout Itemize
1423
1424\SpecialChar \ldots{}
1425 other codes may be defined as well.
1426\layout Standard
1427
vlmaf2155e2004-10-20 15:48:55 +00001428Together with the return code (.code) the asn_dec_rval_t type contains the
vlme18adea2004-06-03 03:49:45 +00001429 number of bytes which is consumed from the buffer.
1430 In the previous hypothetical example of two buffers (of 100 and 200 bytes),
1431 the first call to ber_decode() would return with .code = RC_WMORE and .consumed
1432 = 95.
1433 The .consumed field of the BER decoder return value is
1434\series bold
1435always
1436\series default
1437 valid, even if the decoder succeeds or fails with any other return code.
1438\layout Standard
1439
1440Please look into ber_decoder.h for the precise definition of ber_decode()
1441 and related types.
vlmfcec8e52004-08-23 15:12:04 +00001442\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001443
1444
1445\begin_inset LatexCommand \label{sub:Encoding-DER}
1446
1447\end_inset
1448
1449Encoding DER
1450\layout Standard
1451
vlm68d43e92004-09-24 20:56:07 +00001452The Distinguished Encoding Rules is the
1453\emph on
1454canonical
1455\emph default
1456 variant of BER encoding rules.
1457 The DER is best suited to encode the structures where all the lengths are
1458 known beforehand.
vlme18adea2004-06-03 03:49:45 +00001459 This is probably exactly how you want to encode: either after a BER decoding
1460 or after a manual fill-up, the target structure contains the data which
1461 size is implicitly known before encoding.
vlm856978f2005-02-21 14:16:10 +00001462 Among other uses, the DER encoding is used to encode X.509 certificates.
vlme18adea2004-06-03 03:49:45 +00001463\layout Standard
1464
1465As with BER decoder, the DER encoder may be invoked either directly from
vlmba56d852004-09-29 13:29:17 +00001466 the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone function,
vlme18adea2004-06-03 03:49:45 +00001467 which is somewhat simpler:
1468\layout LyX-Code
1469
vlme18adea2004-06-03 03:49:45 +00001470
1471\layout LyX-Code
1472
1473/*
1474\layout LyX-Code
1475
1476 * This is the serializer itself,
1477\layout LyX-Code
1478
1479 * it supplies the DER encoder with the
1480\layout LyX-Code
1481
1482 * pointer to the custom output function.
1483\layout LyX-Code
1484
1485 */
1486\layout LyX-Code
1487
1488ssize_t
1489\layout LyX-Code
1490
1491simple_serializer(FILE *ostream, Rectangle_t *rect) {
1492\layout LyX-Code
1493
vlm68d43e92004-09-24 20:56:07 +00001494 asn_enc_rval_t er; /* Encoder return value */
vlme18adea2004-06-03 03:49:45 +00001495\layout LyX-Code
1496
1497
1498\layout LyX-Code
1499
vlmba56d852004-09-29 13:29:17 +00001500 er = der_encode(&asn_DEF_Rect, rect,
vlme18adea2004-06-03 03:49:45 +00001501\layout LyX-Code
1502
vlmfcec8e52004-08-23 15:12:04 +00001503 write_stream, ostream);
vlme18adea2004-06-03 03:49:45 +00001504\layout LyX-Code
1505
vlm68d43e92004-09-24 20:56:07 +00001506 if(er.
vlme18adea2004-06-03 03:49:45 +00001507\series bold
vlm68d43e92004-09-24 20:56:07 +00001508encoded
vlme18adea2004-06-03 03:49:45 +00001509\series default
1510 == -1) {
1511\layout LyX-Code
1512
1513 /*
1514\layout LyX-Code
1515
vlm68d43e92004-09-24 20:56:07 +00001516 * Failed to encode the rectangle data.
vlme18adea2004-06-03 03:49:45 +00001517\layout LyX-Code
1518
1519 */
1520\layout LyX-Code
1521
1522 fprintf(stderr,
1523\begin_inset Quotes sld
1524\end_inset
1525
1526Cannot encode %s: %s
1527\backslash
1528n
1529\begin_inset Quotes srd
1530\end_inset
1531
1532,
1533\layout LyX-Code
1534
vlm68d43e92004-09-24 20:56:07 +00001535 er.
vlme18adea2004-06-03 03:49:45 +00001536\series bold
vlm68d43e92004-09-24 20:56:07 +00001537failed_type
vlme18adea2004-06-03 03:49:45 +00001538\series default
1539->name,
1540\layout LyX-Code
1541
1542 strerror(errno));
1543\layout LyX-Code
1544
1545 return -1;
1546\layout LyX-Code
1547
1548 } else {
1549\layout LyX-Code
1550
1551 /* Return the number of bytes */
1552\layout LyX-Code
1553
vlm68d43e92004-09-24 20:56:07 +00001554 return er.encoded;
vlme18adea2004-06-03 03:49:45 +00001555\layout LyX-Code
1556
1557 }
1558\layout LyX-Code
1559
1560}
1561\layout Standard
1562
1563As you see, the DER encoder does not write into some sort of buffer or something.
1564 It just invokes the custom function (possible, multiple times) which would
1565 save the data into appropriate storage.
1566 The optional argument
1567\emph on
1568app_key
1569\emph default
1570 is opaque for the DER encoder code and just used by
1571\emph on
1572_write_stream()
1573\emph default
1574 as the pointer to the appropriate output stream to be used.
1575\layout Standard
1576
1577If the custom write function is not given (passed as 0), then the DER encoder
1578 will essentially do the same thing (i.e., encode the data) but no callbacks
1579 will be invoked (so the data goes nowhere).
1580 It may prove useful to determine the size of the structure's encoding before
1581 actually doing the encoding
1582\begin_inset Foot
1583collapsed false
1584
1585\layout Standard
1586
1587It is actually faster too: the encoder might skip over some computations
1588 which aren't important for the size determination.
1589\end_inset
1590
1591.
1592\layout Standard
1593
1594Please look into der_encoder.h for the precise definition of der_encode()
1595 and related types.
vlmfcec8e52004-08-23 15:12:04 +00001596\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001597
1598
vlm68d43e92004-09-24 20:56:07 +00001599\begin_inset LatexCommand \label{sub:Encoding-XER}
1600
1601\end_inset
1602
1603Encoding XER
1604\layout Standard
1605
1606The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
1607 Markup Language, a text-based format for information exchange.
1608 The encoder routine API comes in two flavors: stdio-based and callback-based.
1609 With the callback-based encoder, the encoding process is very similar to
1610 the DER one, described in Section
1611\begin_inset LatexCommand \vref{sub:Encoding-DER}
1612
1613\end_inset
1614
1615.
1616 The following example uses the definition of write_stream() from up there.
1617\layout LyX-Code
1618
1619/*
1620\layout LyX-Code
1621
1622 * This procedure generates the XML document
1623\layout LyX-Code
1624
1625 * by invoking the XER encoder.
1626\layout LyX-Code
1627
1628 * NOTE: Do not copy this code verbatim!
1629\layout LyX-Code
1630
1631 * If the stdio output is necessary,
1632\layout LyX-Code
1633
1634 * use the xer_fprint() procedure instead.
1635\layout LyX-Code
1636
1637 * See Section
1638\begin_inset LatexCommand \vref{sub:Printing-the-target}
1639
1640\end_inset
1641
1642.
1643\layout LyX-Code
1644
1645 */
1646\layout LyX-Code
1647
1648int
1649\layout LyX-Code
1650
1651print_as_XML(FILE *ostream, Rectangle_t *rect) {
1652\layout LyX-Code
1653
1654 asn_enc_rval_t er; /* Encoder return value */
1655\layout LyX-Code
1656
1657
1658\layout LyX-Code
1659
vlm856978f2005-02-21 14:16:10 +00001660 er = xer_encode(&asn_DEF_Rectangle, rect,
vlm68d43e92004-09-24 20:56:07 +00001661\layout LyX-Code
1662
1663 XER_F_BASIC, /* BASIC-XER or CANONICAL-XER */
1664\layout LyX-Code
1665
1666 write_stream, ostream);
1667\layout LyX-Code
1668
1669
1670\layout LyX-Code
1671
1672 return (er.encoded == -1) ? -1 : 0;
1673\layout LyX-Code
1674
1675}
1676\layout Standard
1677
1678Please look into xer_encoder.h for the precise definition of xer_encode()
1679 and related types.
1680\layout Standard
1681
1682See Section
1683\begin_inset LatexCommand \ref{sub:Printing-the-target}
1684
1685\end_inset
1686
1687 for the example of stdio-based XML encoder and other pretty-printing suggestion
1688s.
1689\layout Subsection
1690
1691
vlm856978f2005-02-21 14:16:10 +00001692\begin_inset LatexCommand \label{sub:Decoding-XER}
1693
1694\end_inset
1695
1696Decoding XER
1697\layout Standard
1698
1699The data encoded using the XER rules can be subsequently decoded using the
1700 xer_decode() API call:
1701\layout LyX-Code
1702
1703Rectangle_t *
1704\layout LyX-Code
1705
1706XML_to_Rectangle(const void *buffer, size_t buf_size) {
1707\layout LyX-Code
1708
1709 Rectangle_t *rect = 0; /* Note this 0! */
1710\layout LyX-Code
1711
1712 asn_dec_rval_t rval;
1713\layout LyX-Code
1714
1715
1716\layout LyX-Code
1717
1718 rval = xer_decode(0, &asn_DEF_Rectangle, (void **)&rect,
1719\layout LyX-Code
1720
1721 buffer, buf_size);
1722\layout LyX-Code
1723
1724 if(rval
1725\series bold
1726.code
1727\series default
1728 == RC_OK) {
1729\layout LyX-Code
1730
1731 return rect; /* Decoding succeeded */
1732\layout LyX-Code
1733
1734 } else {
1735\layout LyX-Code
1736
1737 /* Free partially decoded rect */
1738\layout LyX-Code
1739
vlm823f1442005-08-29 10:23:20 +00001740 asn_DEF_Rectangle.free_struct(
vlm856978f2005-02-21 14:16:10 +00001741\layout LyX-Code
1742
1743 &asn_DEF_Rectangle, rect, 0);
1744\layout LyX-Code
1745
1746 return 0;
1747\layout LyX-Code
1748
1749 }
1750\layout LyX-Code
1751
1752}
1753\layout Standard
1754
1755The decoder takes both BASIC-XER and CANONICAL-XER encodings.
1756\layout Standard
1757
vlm485123c2005-02-22 07:28:26 +00001758The decoder shares its data consumption properties with BER decoder; please
1759 read the Section
1760\begin_inset LatexCommand \vref{sub:Decoding-BER}
1761
1762\end_inset
1763
1764 to know more.
1765\layout Standard
1766
vlm856978f2005-02-21 14:16:10 +00001767Please look into xer_decoder.h for the precise definition of xer_decode()
1768 and related types.
1769\layout Subsection
1770
1771
vlme18adea2004-06-03 03:49:45 +00001772\begin_inset LatexCommand \label{sub:Validating-the-target}
1773
1774\end_inset
1775
1776Validating the target structure
1777\layout Standard
1778
1779Sometimes the target structure needs to be validated.
1780 For example, if the structure was created by the application (as opposed
1781 to being decoded from some external source), some important information
1782 required by the ASN.1 specification might be missing.
1783 On the other hand, the successful decoding of the data from some external
1784 source does not necessarily mean that the data is fully valid either.
1785 It might well be the case that the specification describes some subtype
1786 constraints that were not taken into account during decoding, and it would
1787 actually be useful to perform the last check when the data is ready to
1788 be encoded or when the data has just been decoded to ensure its validity
1789 according to some stricter rules.
1790\layout Standard
1791
1792The asn_check_constraints() function checks the type for various implicit
1793 and explicit constraints.
1794 It is recommended to use asn_check_constraints() function after each decoding
1795 and before each encoding.
1796\layout Standard
1797
1798Please look into constraints.h for the precise definition of asn_check_constraint
1799s() and related types.
vlmfcec8e52004-08-23 15:12:04 +00001800\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001801
1802
1803\begin_inset LatexCommand \label{sub:Printing-the-target}
1804
1805\end_inset
1806
1807Printing the target structure
1808\layout Standard
1809
1810There are two ways to print the target structure: either invoke the print_struct
1811 member of the ASN.1 type descriptor, or using the asn_fprint() function,
1812 which is a simpler wrapper of the former:
1813\layout LyX-Code
1814
vlmba56d852004-09-29 13:29:17 +00001815asn_fprint(stdout, &asn_DEF_Rectangle, rect);
vlme18adea2004-06-03 03:49:45 +00001816\layout Standard
1817
1818Please look into constr_TYPE.h for the precise definition of asn_fprint()
1819 and related types.
vlm68d43e92004-09-24 20:56:07 +00001820\layout Standard
1821
1822Another practical alternative to this custom format printing would be to
1823 invoke XER encoder.
1824 The default BASIC-XER encoder performs reasonable formatting for the output
1825 to be useful and human readable.
1826 To invoke the XER decoder in a manner similar to asn_fprint(), use the
1827 xer_fprint() call:
1828\layout LyX-Code
1829
vlmba56d852004-09-29 13:29:17 +00001830xer_fprint(stdout, &asn_DEF_Rectangle, rect);
vlm68d43e92004-09-24 20:56:07 +00001831\layout Standard
1832
1833See Section
1834\begin_inset LatexCommand \vref{sub:Encoding-XER}
1835
1836\end_inset
1837
1838 for XML-related details.
vlmfcec8e52004-08-23 15:12:04 +00001839\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001840
1841
1842\begin_inset LatexCommand \label{sub:Freeing-the-target}
1843
1844\end_inset
1845
1846Freeing the target structure
1847\layout Standard
1848
1849Freeing the structure is slightly more complex than it may seem to.
1850 When the ASN.1 structure is freed, all the members of the structure and
1851 their submembers etc etc are recursively freed too.
1852 But it might not be feasible to free the structure itself.
1853 Consider the following case:
1854\layout LyX-Code
1855
1856struct my_figure { /* The custom structure */
1857\layout LyX-Code
1858
1859 int flags; /* <some custom member> */
1860\layout LyX-Code
1861
1862 /* The type is generated by the ASN.1 compiler */
1863\layout LyX-Code
1864
1865
1866\emph on
1867Rectangle_t rect;
1868\layout LyX-Code
1869
1870 /* other members of the structure */
1871\layout LyX-Code
1872
1873};
1874\layout Standard
1875
1876In this example, the application programmer defined a custom structure with
1877 one ASN.1-derived member (rect).
1878 This member is not a reference to the Rectangle_t, but an in-place inclusion
1879 of the Rectangle_t structure.
1880 If the freeing is necessary, the usual procedure of freeing everything
1881 must not be applied to the &rect pointer itself, because it does not point
vlm485123c2005-02-22 07:28:26 +00001882 to the memory block directly allocated by the memory allocation routine,
1883 but instead lies within a block allocated for the my_figure structure.
vlme18adea2004-06-03 03:49:45 +00001884\layout Standard
1885
1886To solve this problem, the free_struct routine has the additional argument
vlm485123c2005-02-22 07:28:26 +00001887 (besides the obvious type descriptor and target structure pointers), which
1888 is the flag specifying whether the outer pointer itself must be freed (0,
1889 default) or it should be left intact (non-zero value).
vlme18adea2004-06-03 03:49:45 +00001890\layout LyX-Code
1891
vlm856978f2005-02-21 14:16:10 +00001892
1893\series bold
1894/* 1.
1895 Rectangle_t is defined within my_figure */
vlme18adea2004-06-03 03:49:45 +00001896\layout LyX-Code
1897
vlm856978f2005-02-21 14:16:10 +00001898struct my_figure {
1899\layout LyX-Code
1900
1901 Rectangle_t rect;
1902\layout LyX-Code
1903
1904} *mf =
vlme18adea2004-06-03 03:49:45 +00001905\series bold
1906...
1907\series default
1908;
1909\layout LyX-Code
1910
1911/*
1912\layout LyX-Code
1913
vlm856978f2005-02-21 14:16:10 +00001914 * Freeing the Rectangle_t
vlme18adea2004-06-03 03:49:45 +00001915\layout LyX-Code
1916
vlm856978f2005-02-21 14:16:10 +00001917 * without freeing the mf->rect area
vlme18adea2004-06-03 03:49:45 +00001918\layout LyX-Code
1919
1920 */
1921\layout LyX-Code
1922
vlm823f1442005-08-29 10:23:20 +00001923asn_DEF_Rectangle.free_struct(
vlme18adea2004-06-03 03:49:45 +00001924\layout LyX-Code
1925
vlmba56d852004-09-29 13:29:17 +00001926 &asn_DEF_Rectangle, &mf->rect,
vlm856978f2005-02-21 14:16:10 +00001927\series bold
vlme18adea2004-06-03 03:49:45 +000019281
vlm856978f2005-02-21 14:16:10 +00001929\emph on
vlme18adea2004-06-03 03:49:45 +00001930
vlm856978f2005-02-21 14:16:10 +00001931\emph default
1932/* !free */
1933\series default
1934);
vlme18adea2004-06-03 03:49:45 +00001935\layout LyX-Code
1936
vlm856978f2005-02-21 14:16:10 +00001937
1938\layout LyX-Code
1939
1940
1941\layout LyX-Code
1942
1943
1944\series bold
1945/* 2.
1946 Rectangle_t is a stand-alone pointer */
vlme18adea2004-06-03 03:49:45 +00001947\layout LyX-Code
1948
1949Rectangle_t *rect =
1950\series bold
1951...
1952\series default
1953;
1954\layout LyX-Code
1955
1956/*
1957\layout LyX-Code
1958
1959 * Freeing the Rectangle_t
1960\layout LyX-Code
1961
1962 * and freeing the rect pointer
1963\layout LyX-Code
1964
1965 */
1966\layout LyX-Code
1967
vlm823f1442005-08-29 10:23:20 +00001968asn_DEF_Rectangle.free_struct(
vlme18adea2004-06-03 03:49:45 +00001969\layout LyX-Code
1970
vlmba56d852004-09-29 13:29:17 +00001971 &asn_DEF_Rectangle, rect,
vlm856978f2005-02-21 14:16:10 +00001972\series bold
vlme18adea2004-06-03 03:49:45 +000019730
vlm856978f2005-02-21 14:16:10 +00001974\emph on
1975
vlme18adea2004-06-03 03:49:45 +00001976\emph default
vlm856978f2005-02-21 14:16:10 +00001977/* free the pointer too */
1978\series default
1979);
vlme18adea2004-06-03 03:49:45 +00001980\layout Standard
1981
1982It is safe to invoke the
1983\emph on
1984free_struct
1985\emph default
1986 function with the target structure pointer set to 0 (NULL), the function
1987 will do nothing.
vlma1fd7f92005-01-31 10:10:04 +00001988\layout Chapter
vlm4cb0edf2005-01-14 11:38:49 +00001989
vlmc7737f62005-02-02 09:53:46 +00001990
1991\begin_inset LatexCommand \label{cha:Step-by-step-examples}
1992
1993\end_inset
1994
vlma1fd7f92005-01-31 10:10:04 +00001995Step by step examples
1996\layout Section
1997
1998A
1999\begin_inset Quotes sld
2000\end_inset
2001
2002Rectangle
2003\begin_inset Quotes srd
2004\end_inset
2005
2006 Encoder
2007\layout Standard
2008
vlmc7737f62005-02-02 09:53:46 +00002009This example will help you to create a simple BER and XER encoder of a
vlma1fd7f92005-01-31 10:10:04 +00002010\begin_inset Quotes sld
2011\end_inset
2012
2013Rectangle
2014\begin_inset Quotes srd
2015\end_inset
2016
2017 type used throughout this document.
2018\layout Enumerate
2019
2020Create a file named
2021\series bold
2022rectangle.asn1
2023\series default
2024 with the following contents:
2025\begin_deeper
2026\layout LyX-Code
2027
2028RectangleModule1 DEFINITIONS ::=
2029\layout LyX-Code
2030
2031BEGIN
2032\layout LyX-Code
2033
2034
2035\layout LyX-Code
2036
2037Rectangle ::= SEQUENCE {
2038\layout LyX-Code
2039
2040 height INTEGER,
2041\layout LyX-Code
2042
2043 width INTEGER
2044\layout LyX-Code
2045
2046}
2047\layout LyX-Code
2048
2049
2050\layout LyX-Code
2051
2052END
2053\end_deeper
2054\layout Enumerate
2055
2056Compile it into the set of .c and .h files using asn1c compiler
2057\begin_inset LatexCommand \cite{ASN1C}
2058
2059\end_inset
2060
2061:
2062\begin_deeper
2063\layout LyX-Code
2064
2065
2066\emph on
2067asn1c -fnative-types
2068\series bold
2069\emph default
2070rectangle.asn1
2071\end_deeper
2072\layout Enumerate
2073
2074Alternatively, use the Online ASN.1 compiler
2075\begin_inset LatexCommand \cite{AONL}
2076
2077\end_inset
2078
2079 by uploading the
2080\series bold
2081rectangle.asn1
2082\series default
2083 file into the Web form and unpacking the produced archive on your computer.
2084\layout Enumerate
2085
2086By this time, you should have gotten multiple files in the current directory,
2087 including the
2088\series bold
2089Rectangle.c
2090\series default
2091 and
2092\series bold
2093Rectangle.h
2094\series default
2095.
2096\layout Enumerate
2097
2098Create a main() routine which creates the Rectangle_t structure in memory
2099 and encodes it using BER and XER encoding rules.
2100 Let's name the file
2101\series bold
2102main.c
2103\series default
2104:
2105\begin_inset ERT
2106status Open
2107
2108\layout Standard
2109
2110\backslash
2111clearpage{}
2112\end_inset
2113
2114
2115\begin_deeper
2116\layout LyX-Code
2117
2118
2119\size small
2120#include <stdio.h>
2121\layout LyX-Code
2122
2123
2124\size small
2125#include <sys/types.h>
2126\layout LyX-Code
2127
2128
2129\size small
2130#include <Rectangle.h> /* Rectangle ASN.1 type */
2131\layout LyX-Code
2132
2133
2134\layout LyX-Code
2135
2136
2137\size small
2138/*
2139\layout LyX-Code
2140
2141
2142\size small
2143 * This is a custom function which writes the
2144\layout LyX-Code
2145
2146
2147\size small
2148 * encoded output into some FILE stream.
2149\layout LyX-Code
2150
2151
2152\size small
2153 */
2154\layout LyX-Code
2155
2156
2157\size small
2158static int
2159\layout LyX-Code
2160
2161
2162\size small
2163write_out(const void *buffer, size_t size, void *app_key) {
2164\layout LyX-Code
2165
2166
2167\size small
2168 FILE *out_fp = app_key;
2169\layout LyX-Code
2170
2171
2172\size small
2173 size_t wrote;
2174\layout LyX-Code
2175
2176
2177\size small
2178
2179\layout LyX-Code
2180
2181
2182\size small
2183 wrote = fwrite(buffer, 1, size, out_fp);
2184\layout LyX-Code
2185
2186
2187\size small
2188
2189\layout LyX-Code
2190
2191
2192\size small
2193 return (wrote == size) ? 0 : -1;
2194\layout LyX-Code
2195
2196
2197\size small
2198}
2199\layout LyX-Code
2200
2201
2202\layout LyX-Code
2203
2204
2205\size small
vlm6c37dac2005-01-31 15:06:32 +00002206int main(int ac, char **av) {
vlma1fd7f92005-01-31 10:10:04 +00002207\layout LyX-Code
2208
2209
2210\size small
2211 Rectangle_t *rectangle; /* Type to encode */
2212\layout LyX-Code
2213
2214
2215\size small
2216 asn_enc_rval_t ec; /* Encoder return value */
2217\layout LyX-Code
2218
2219
2220\size small
2221
2222\layout LyX-Code
2223
2224
2225\size small
2226 /* Allocate the Rectangle_t */
2227\layout LyX-Code
2228
2229
2230\size small
vlmc7737f62005-02-02 09:53:46 +00002231 rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */
vlma1fd7f92005-01-31 10:10:04 +00002232\layout LyX-Code
2233
2234
2235\size small
2236 if(!rectangle) {
2237\layout LyX-Code
2238
2239
2240\size small
2241 perror(
2242\begin_inset Quotes sld
2243\end_inset
2244
2245calloc() failed
2246\begin_inset Quotes srd
2247\end_inset
2248
2249);
2250\layout LyX-Code
2251
2252
2253\size small
2254 exit(71); /* better, EX_OSERR */
2255\layout LyX-Code
2256
2257
2258\size small
2259 }
2260\layout LyX-Code
2261
2262
2263\size small
2264
2265\layout LyX-Code
2266
2267
2268\size small
2269 /* Initialize the Rectangle members */
2270\layout LyX-Code
2271
2272
2273\size small
2274 rectangle->height = 42; /* any random value */
2275\layout LyX-Code
2276
2277
2278\size small
2279 rectangle->width = 23; /* any random value */
2280\layout LyX-Code
2281
2282
2283\size small
2284
2285\layout LyX-Code
2286
2287
2288\size small
2289 /* BER encode the data if filename is given */
2290\layout LyX-Code
2291
2292
2293\size small
2294 if(ac < 2) {
2295\layout LyX-Code
2296
2297
2298\size small
2299 fprintf(stderr,
2300\begin_inset Quotes sld
2301\end_inset
2302
2303Specify filename for BER output
2304\backslash
2305n
2306\begin_inset Quotes srd
2307\end_inset
2308
2309);
2310\layout LyX-Code
2311
2312
2313\size small
2314 } else {
2315\layout LyX-Code
2316
2317
2318\size small
2319 const char *filename = av[1];
2320\layout LyX-Code
2321
2322
2323\size small
2324 FILE *fp = fopen(filename,
2325\begin_inset Quotes sld
2326\end_inset
2327
vlmdae8e612005-07-21 18:45:37 +00002328wb
vlma1fd7f92005-01-31 10:10:04 +00002329\begin_inset Quotes srd
2330\end_inset
2331
2332); /* for BER output */
2333\layout LyX-Code
2334
2335
2336\layout LyX-Code
2337
2338
2339\size small
2340 if(!fp) {
2341\layout LyX-Code
2342
2343
2344\size small
2345 perror(filename);
2346\layout LyX-Code
2347
2348
2349\size small
2350 exit(71); /* better, EX_OSERR */
2351\layout LyX-Code
2352
2353
2354\size small
2355 }
2356\layout LyX-Code
2357
2358
2359\size small
2360
2361\layout LyX-Code
2362
2363
2364\size small
2365 /* Encode the Rectangle type as BER (DER) */
2366\layout LyX-Code
2367
2368
2369\size small
2370 ec = der_encode(&asn_DEF_Rectangle,
2371\layout LyX-Code
2372
2373
2374\size small
vlm6c37dac2005-01-31 15:06:32 +00002375 rectangle, write_out, fp);
vlma1fd7f92005-01-31 10:10:04 +00002376\layout LyX-Code
2377
2378
2379\size small
2380 fclose(fp);
2381\layout LyX-Code
2382
2383
2384\size small
2385 if(ec.encoded == -1) {
2386\layout LyX-Code
2387
2388
2389\size small
2390 fprintf(stderr,
2391\layout LyX-Code
2392
2393
2394\size small
2395
2396\begin_inset Quotes sld
2397\end_inset
2398
2399Could not encode Rectangle (at %s)
2400\backslash
2401n
2402\begin_inset Quotes srd
2403\end_inset
2404
2405,
2406\layout LyX-Code
2407
2408
2409\size small
2410 ec.failed_type ? ec.failed_type->name :
2411\begin_inset Quotes sld
2412\end_inset
2413
2414unknown
2415\begin_inset Quotes srd
2416\end_inset
2417
2418);
2419\layout LyX-Code
2420
2421
2422\size small
2423 exit(65); /* better, EX_DATAERR */
2424\layout LyX-Code
2425
2426
2427\size small
2428 } else {
2429\layout LyX-Code
2430
2431
2432\size small
2433 fprintf(stderr,
2434\begin_inset Quotes sld
2435\end_inset
2436
2437Created %s with BER encoded Rectangle
2438\backslash
2439n
2440\begin_inset Quotes srd
2441\end_inset
2442
2443,
2444\layout LyX-Code
2445
2446
2447\size small
2448 filename);
2449\layout LyX-Code
2450
2451
2452\size small
2453 }
2454\layout LyX-Code
2455
2456
2457\size small
2458 }
2459\layout LyX-Code
2460
2461
2462\size small
2463
2464\layout LyX-Code
2465
2466
2467\size small
2468 /* Also print the constructed Rectangle XER encoded (XML) */
2469\layout LyX-Code
2470
2471
2472\size small
2473 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
2474\layout LyX-Code
2475
2476
2477\size small
2478
2479\layout LyX-Code
2480
2481
2482\size small
2483 return 0; /* Encoding finished successfully */
2484\layout LyX-Code
2485
2486
2487\size small
2488}
2489\end_deeper
2490\layout Enumerate
2491
2492Compile all files together using C compiler (varies by platform):
2493\begin_deeper
2494\layout LyX-Code
2495
2496
2497\emph on
2498cc -I.
2499 -o
2500\series bold
2501rencode
2502\series default
2503 *.c
2504\end_deeper
2505\layout Enumerate
2506
2507Voila! You have just created the BER and XER encoder of a Rectangle type,
2508 named
2509\series bold
2510rencode
2511\series default
2512!
2513\layout Standard
2514
2515
2516\begin_inset ERT
2517status Collapsed
2518
2519\layout Standard
2520
2521\backslash
2522clearpage{}
2523\end_inset
2524
2525
2526\layout Section
2527
vlmc7737f62005-02-02 09:53:46 +00002528
2529\begin_inset LatexCommand \label{sec:A-Rectangle-Decoder}
2530
2531\end_inset
2532
vlma1fd7f92005-01-31 10:10:04 +00002533A
vlm4cb0edf2005-01-14 11:38:49 +00002534\begin_inset Quotes sld
2535\end_inset
2536
2537Rectangle
2538\begin_inset Quotes srd
2539\end_inset
2540
2541 Decoder
2542\layout Standard
2543
vlmc7737f62005-02-02 09:53:46 +00002544This example will help you to create a simple BER decoder of a simple
vlm4cb0edf2005-01-14 11:38:49 +00002545\begin_inset Quotes sld
2546\end_inset
2547
2548Rectangle
2549\begin_inset Quotes srd
2550\end_inset
2551
2552 type used throughout this document.
2553\layout Enumerate
2554
2555Create a file named
2556\series bold
2557rectangle.asn1
2558\series default
2559 with the following contents:
2560\begin_deeper
2561\layout LyX-Code
2562
2563RectangleModule1 DEFINITIONS ::=
2564\layout LyX-Code
2565
2566BEGIN
2567\layout LyX-Code
2568
2569
2570\layout LyX-Code
2571
2572Rectangle ::= SEQUENCE {
2573\layout LyX-Code
2574
2575 height INTEGER,
2576\layout LyX-Code
2577
2578 width INTEGER
2579\layout LyX-Code
2580
2581}
2582\layout LyX-Code
2583
2584
2585\layout LyX-Code
2586
2587END
2588\end_deeper
2589\layout Enumerate
2590
2591Compile it into the set of .c and .h files using asn1c compiler
2592\begin_inset LatexCommand \cite{ASN1C}
2593
2594\end_inset
2595
2596:
2597\begin_deeper
2598\layout LyX-Code
2599
2600
2601\emph on
2602asn1c -fnative-types
2603\series bold
2604\emph default
2605rectangle.asn1
2606\end_deeper
2607\layout Enumerate
2608
2609Alternatively, use the Online ASN.1 compiler
2610\begin_inset LatexCommand \cite{AONL}
2611
2612\end_inset
2613
2614 by uploading the
2615\series bold
2616rectangle.asn1
2617\series default
vlmabd51e22005-01-17 11:05:04 +00002618 file into the Web form and unpacking the produced archive on your computer.
vlm4cb0edf2005-01-14 11:38:49 +00002619\layout Enumerate
2620
vlmabd51e22005-01-17 11:05:04 +00002621By this time, you should have gotten multiple files in the current directory,
2622 including the
2623\series bold
2624Rectangle.c
2625\series default
2626 and
2627\series bold
2628Rectangle.h
2629\series default
2630.
vlm4cb0edf2005-01-14 11:38:49 +00002631\layout Enumerate
2632
2633Create a main() routine which takes the binary input file, decodes it as
vlmabd51e22005-01-17 11:05:04 +00002634 it were a BER-encoded Rectangle type, and prints out the text (XML) representat
2635ion of the Rectangle type.
vlm4cb0edf2005-01-14 11:38:49 +00002636 Let's name the file
2637\series bold
2638main.c
2639\series default
2640:
vlmabd51e22005-01-17 11:05:04 +00002641\begin_inset ERT
vlmc7737f62005-02-02 09:53:46 +00002642status Collapsed
vlmabd51e22005-01-17 11:05:04 +00002643
2644\layout Standard
2645
2646\backslash
2647clearpage{}
2648\end_inset
2649
2650
vlm4cb0edf2005-01-14 11:38:49 +00002651\begin_deeper
2652\layout LyX-Code
2653
2654
2655\size small
2656#include <stdio.h>
2657\layout LyX-Code
2658
2659
2660\size small
2661#include <sys/types.h>
2662\layout LyX-Code
2663
2664
2665\size small
2666#include <Rectangle.h> /* Rectangle ASN.1 type */
2667\layout LyX-Code
2668
2669
2670\size small
2671
2672\layout LyX-Code
2673
2674
2675\size small
2676int main(int ac, char **av) {
2677\layout LyX-Code
2678
2679
2680\size small
2681 char buf[1024]; /* Temporary buffer */
2682\layout LyX-Code
2683
2684
2685\size small
2686 Rectangle_t *rectangle = 0; /* Type to decode */
2687\layout LyX-Code
2688
2689
2690\size small
2691 asn_dec_rval_t rval; /* Decoder return value */
2692\layout LyX-Code
2693
2694
2695\size small
2696 FILE *fp; /* Input file handler */
2697\layout LyX-Code
2698
2699
2700\size small
2701 size_t size; /* Number of bytes read */
2702\layout LyX-Code
2703
2704
2705\size small
2706 char *filename; /* Input file name */
2707\layout LyX-Code
2708
2709
2710\size small
2711
2712\layout LyX-Code
2713
2714
2715\size small
2716 /* Require a single filename argument */
2717\layout LyX-Code
2718
2719
2720\size small
2721 if(ac != 2) {
2722\layout LyX-Code
2723
2724
2725\size small
2726 fprintf(stderr,
2727\begin_inset Quotes sld
2728\end_inset
2729
2730Usage: %s <file.ber>
2731\backslash
2732n
2733\begin_inset Quotes srd
2734\end_inset
2735
2736, av[0]);
2737\layout LyX-Code
2738
2739
2740\size small
2741 exit(64); /* better, EX_USAGE */
2742\layout LyX-Code
2743
2744
2745\size small
2746 } else {
2747\layout LyX-Code
2748
2749
2750\size small
2751 filename = av[1];
2752\layout LyX-Code
2753
2754
2755\size small
2756 }
2757\layout LyX-Code
2758
2759
2760\size small
2761
2762\layout LyX-Code
2763
2764
2765\size small
2766 /* Open input file as read-only binary */
2767\layout LyX-Code
2768
2769
2770\size small
2771 fp = fopen(filename,
2772\begin_inset Quotes sld
2773\end_inset
2774
2775rb
2776\begin_inset Quotes srd
2777\end_inset
2778
2779);
2780\layout LyX-Code
2781
2782
2783\size small
2784 if(!fp) {
2785\layout LyX-Code
2786
2787
2788\size small
2789 perror(filename);
2790\layout LyX-Code
2791
2792
2793\size small
2794 exit(66); /* better, EX_NOINPUT */
2795\layout LyX-Code
2796
2797
2798\size small
2799 }
2800\layout LyX-Code
2801
2802
2803\size small
2804
2805\layout LyX-Code
2806
2807
2808\size small
2809 /* Read up to the buffer size */
2810\layout LyX-Code
2811
2812
2813\size small
2814 size = fread(buf, 1, sizeof(buf), fp);
2815\layout LyX-Code
2816
2817
2818\size small
2819 fclose(fp);
2820\layout LyX-Code
2821
2822
2823\size small
2824 if(!size) {
2825\layout LyX-Code
2826
2827
2828\size small
2829 fprintf(stderr,
2830\begin_inset Quotes sld
2831\end_inset
2832
2833%s: Empty or broken
2834\backslash
2835n
2836\begin_inset Quotes srd
2837\end_inset
2838
2839, filename);
2840\layout LyX-Code
2841
2842
2843\size small
2844 exit(65); /* better, EX_DATAERR */
2845\layout LyX-Code
2846
2847
2848\size small
2849 }
2850\layout LyX-Code
2851
2852
2853\size small
2854
2855\layout LyX-Code
2856
2857
2858\size small
2859 /* Decode the input buffer as Rectangle type */
2860\layout LyX-Code
2861
2862
2863\size small
2864 rval = ber_decode(0, &asn_DEF_Rectangle,
2865\layout LyX-Code
2866
2867
2868\size small
2869 (void **)&rectangle, buf, size);
2870\layout LyX-Code
2871
2872
2873\size small
2874 if(rval.code != RC_OK) {
2875\layout LyX-Code
2876
2877
2878\size small
2879 fprintf(stderr,
2880\layout LyX-Code
2881
2882
2883\size small
2884
2885\begin_inset Quotes sld
2886\end_inset
2887
2888%s: Broken Rectangle encoding at byte %ld
2889\backslash
2890n
2891\begin_inset Quotes srd
2892\end_inset
2893
2894,
2895\layout LyX-Code
2896
2897
2898\size small
2899 filename, (long)rval.consumed);
2900\layout LyX-Code
2901
2902
2903\size small
2904 exit(65); /* better, EX_DATAERR */
2905\layout LyX-Code
2906
2907
2908\size small
2909 }
2910\layout LyX-Code
2911
2912
2913\size small
2914
2915\layout LyX-Code
2916
2917
2918\size small
2919 /* Print the decoded Rectangle type as XML */
2920\layout LyX-Code
2921
2922
2923\size small
2924 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
2925\layout LyX-Code
2926
2927
2928\size small
2929
2930\layout LyX-Code
2931
2932
2933\size small
2934 return 0; /* Decoding finished successfully */
2935\layout LyX-Code
2936
2937
2938\size small
2939}
2940\end_deeper
2941\layout Enumerate
2942
vlmabd51e22005-01-17 11:05:04 +00002943Compile all files together using C compiler (varies by platform):
vlm4cb0edf2005-01-14 11:38:49 +00002944\begin_deeper
2945\layout LyX-Code
2946
2947
2948\emph on
2949cc -I.
vlmabd51e22005-01-17 11:05:04 +00002950 -o
2951\series bold
2952rdecode
2953\series default
2954 *.c
vlm4cb0edf2005-01-14 11:38:49 +00002955\end_deeper
2956\layout Enumerate
2957
vlma1fd7f92005-01-31 10:10:04 +00002958Voila! You have just created the BER decoder of a Rectangle type, named
2959
vlm4cb0edf2005-01-14 11:38:49 +00002960\series bold
2961rdecode
2962\series default
2963!
vlmc7737f62005-02-02 09:53:46 +00002964\layout Chapter
2965
2966Constraint validation examples
2967\layout Standard
2968
2969This chapter shows how to define ASN.1 constraints and use the generated
2970 validation code.
2971\layout Section
2972
2973Adding constraints into
2974\begin_inset Quotes sld
2975\end_inset
2976
2977Rectangle
2978\begin_inset Quotes srd
2979\end_inset
2980
2981 type
2982\layout Standard
2983
2984This example shows how to add basic constraints to the ASN.1 specification
2985 and how to invoke the constraints validation code in your application.
2986\layout Enumerate
2987
2988Create a file named
2989\series bold
2990rectangle.asn1
2991\series default
2992 with the following contents:
2993\begin_deeper
2994\layout LyX-Code
2995
2996RectangleModuleWithConstraints DEFINITIONS ::=
2997\layout LyX-Code
2998
2999BEGIN
3000\layout LyX-Code
3001
3002
3003\layout LyX-Code
3004
3005Rectangle ::= SEQUENCE {
3006\layout LyX-Code
3007
3008 height INTEGER (0..100), -- Value range constraint
3009\layout LyX-Code
3010
3011 width INTEGER (0..MAX) -- Makes width non-negative
3012\layout LyX-Code
3013
3014}
3015\layout LyX-Code
3016
3017
3018\layout LyX-Code
3019
3020END
3021\end_deeper
3022\layout Enumerate
3023
3024Compile the file according to procedures shown in the previous chapter.
3025\layout Enumerate
3026
3027Modify the Rectangle type processing routine (you can start with the main()
3028 routine shown in the Section
3029\begin_inset LatexCommand \vref{sec:A-Rectangle-Decoder}
3030
3031\end_inset
3032
3033) by placing the following snippet of code
3034\emph on
3035before
3036\emph default
3037 encoding and/or
3038\emph on
3039after
3040\emph default
3041 decoding the Rectangle type
3042\begin_inset Foot
3043collapsed true
3044
3045\layout Standard
3046
3047Placing the constraint checking code
3048\emph on
3049before
3050\emph default
3051 encoding helps to make sure you know the data is correct and within constraints
3052 before sharing the data with anyone else.
3053\layout Standard
3054
3055Placing the constraint checking code
3056\emph on
3057after
3058\emph default
3059 decoding, but before any further action depending on the decoded data,
3060 helps to make sure the application got the valid contents before making
3061 use of it.
3062\end_inset
3063
3064:
3065\begin_inset ERT
3066status Collapsed
3067
3068\layout Standard
3069
3070\backslash
3071clearpage{}
3072\end_inset
3073
3074
3075\begin_deeper
3076\layout LyX-Code
3077
3078
3079\size small
3080int ret; /* Return value */
3081\layout LyX-Code
3082
3083
3084\size small
3085char errbuf[128]; /* Buffer for error message */
3086\layout LyX-Code
3087
3088
3089\size small
3090size_t errlen = sizeof(errbuf); /* Size of the buffer */
3091\layout LyX-Code
3092
3093
3094\size small
3095
3096\layout LyX-Code
3097
3098
3099\size small
3100/* ...
3101 here may go Rectangle decoding code ...
3102 */
3103\layout LyX-Code
3104
3105
3106\size small
3107
3108\layout LyX-Code
3109
3110
3111\size small
vlm823f1442005-08-29 10:23:20 +00003112ret = asn_check_constraints(&asn_DEF_Rectangle,
vlmc7737f62005-02-02 09:53:46 +00003113\layout LyX-Code
3114
3115
3116\size small
3117 rectangle, errbuf, &errlen);
3118\layout LyX-Code
3119
3120
3121\size small
3122/* assert(errlen < sizeof(errbuf)); // you may rely on that */
3123\layout LyX-Code
3124
3125
3126\size small
3127if(ret) {
3128\layout LyX-Code
3129
3130
3131\size small
3132 fprintf(stderr,
3133\begin_inset Quotes sld
3134\end_inset
3135
3136Constraint validation failed: %s
3137\backslash
3138n
3139\begin_inset Quotes srd
3140\end_inset
3141
3142,
3143\layout LyX-Code
3144
3145
3146\size small
3147 errbuf /* errbuf is properly nul-terminated */
3148\layout LyX-Code
3149
3150
3151\size small
3152 );
3153\layout LyX-Code
3154
3155
3156\size small
3157 /* exit(...); // Replace with appropriate action */
3158\layout LyX-Code
3159
3160
3161\size small
3162}
3163\layout LyX-Code
3164
3165
3166\size small
3167
3168\layout LyX-Code
3169
3170
3171\size small
3172/* ...
3173 here may go Rectangle encoding code ...
3174 */
3175\end_deeper
3176\layout Enumerate
3177
3178Compile the resulting C code as shown in the previous chapters.
3179\layout Enumerate
3180
vlm602b7262005-02-02 11:06:38 +00003181Try to test the constraints checking code by assigning integer value 101
3182 to the
3183\series bold
3184.height
3185\series default
3186 member of the Rectangle structure, or a negative value to the
3187\series bold
3188.width
3189\series default
3190 member.
3191 In either case, the program should print
3192\begin_inset Quotes sld
3193\end_inset
3194
3195Constraint validation failed
3196\begin_inset Quotes srd
3197\end_inset
3198
3199 message, followed by the short explanation why validation did not succeed.
3200\layout Enumerate
3201
vlmc7737f62005-02-02 09:53:46 +00003202Done.
vlm856978f2005-02-21 14:16:10 +00003203\layout Part
3204
3205
3206\begin_inset LatexCommand \label{par:ASN.1-Basics}
3207
3208\end_inset
3209
3210ASN.1 Basics
3211\layout Chapter
3212
3213
3214\begin_inset LatexCommand \label{cha:Abstract-Syntax-Notation:}
3215
3216\end_inset
3217
3218Abstract Syntax Notation: ASN.1
3219\layout Standard
3220
3221
3222\emph on
3223This chapter defines some basic ASN.1 concepts and describes several most
3224 widely used types.
3225 It is by no means an authoritative or complete reference.
3226 For more complete ASN.1 description, please refer to Olivier Dubuisson's
3227 book
3228\begin_inset LatexCommand \cite{Dub00}
3229
3230\end_inset
3231
3232 or the ASN.1 body of standards itself
3233\begin_inset LatexCommand \cite{ITU-T/ASN.1}
3234
3235\end_inset
3236
3237.
3238\layout Standard
3239
3240The Abstract Syntax Notation One is used to formally describe the semantics
3241 of data transmitted across the network.
3242 Two communicating parties may have different formats of their native data
3243 types (i.e.
3244 number of bits in the integer type), thus it is important to have a way
3245 to describe the data in a manner which is independent from the particular
3246 machine's representation.
3247 The ASN.1 specifications are used to achieve the following:
3248\layout Itemize
3249
3250The specification expressed in the ASN.1 notation is a formal and precise
3251 way to communicate the data semantics to human readers;
3252\layout Itemize
3253
3254The ASN.1 specifications may be used as input for automatic compilers which
3255 produce the code for some target language (C, C++, Java, etc) to encode
3256 and decode the data according to some encoding rules (which are also defined
3257 by the ASN.1 standard).
3258\layout Standard
3259
3260Consider the following example:
3261\layout LyX-Code
3262
3263Rectangle ::= SEQUENCE {
3264\layout LyX-Code
3265
3266 height INTEGER,
3267\layout LyX-Code
3268
3269 width INTEGER
3270\layout LyX-Code
3271
3272}
3273\layout Standard
3274
3275This ASN.1 specification describes a constructed type,
3276\emph on
3277Rectangle
3278\emph default
3279, containing two integer fields.
3280 This specification may tell the reader that there exists this kind of data
3281 structure and that some entity may be prepared to send or receive it.
3282 The question on
3283\emph on
3284how
3285\emph default
3286 that entity is going to send or receive the
3287\emph on
3288encoded data
3289\emph default
3290 is outside the scope of ASN.1.
3291 For example, this data structure may be encoded according to some encoding
3292 rules and sent to the destination using the TCP protocol.
3293 The ASN.1 specifies several ways of encoding (or
3294\begin_inset Quotes sld
3295\end_inset
3296
3297serializing
3298\begin_inset Quotes srd
3299\end_inset
3300
3301, or
3302\begin_inset Quotes sld
3303\end_inset
3304
3305marshaling
3306\begin_inset Quotes srd
3307\end_inset
3308
3309) the data: BER, PER, XER and others, including CER and DER derivatives
3310 from BER.
3311\layout Standard
3312
3313The complete specification must be wrapped in a module, which looks like
3314 this:
3315\layout LyX-Code
3316
3317RectangleModule1
3318\layout LyX-Code
3319
3320 { iso org(3) dod(6) internet(1) private(4)
3321\layout LyX-Code
3322
3323 enterprise(1) spelio(9363) software(1)
3324\layout LyX-Code
3325
3326 asn1c(5) docs(2) rectangle(1) 1 }
3327\layout LyX-Code
3328
3329 DEFINITIONS AUTOMATIC TAGS ::=
3330\layout LyX-Code
3331
3332BEGIN
3333\layout LyX-Code
3334
3335
3336\layout LyX-Code
3337
3338-- This is a comment which describes nothing.
3339\layout LyX-Code
3340
3341Rectangle ::= SEQUENCE {
3342\layout LyX-Code
3343
3344 height INTEGER, -- Height of the rectangle
3345\layout LyX-Code
3346
3347 width INTEGER -- Width of the rectangle
3348\layout LyX-Code
3349
3350}
3351\layout LyX-Code
3352
3353
3354\layout LyX-Code
3355
3356END
3357\layout Standard
3358
3359The module header consists of module name (RectangleModule1), the module
3360 object identifier ({...}), a keyword
3361\begin_inset Quotes sld
3362\end_inset
3363
3364DEFINITIONS
3365\begin_inset Quotes srd
3366\end_inset
3367
3368, a set of module flags (AUTOMATIC TAGS) and
3369\begin_inset Quotes sld
3370\end_inset
3371
3372::= BEGIN
3373\begin_inset Quotes srd
3374\end_inset
3375
3376.
3377 The module ends with an
3378\begin_inset Quotes sld
3379\end_inset
3380
3381END
3382\begin_inset Quotes srd
3383\end_inset
3384
3385 statement.
3386\layout Section
3387
3388Some of the ASN.1 Basic Types
3389\layout Subsection
3390
3391The BOOLEAN type
3392\layout Standard
3393
3394The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF or
3395 a similar kind of two-way choice.
3396\layout Subsection
3397
3398The INTEGER type
3399\layout Standard
3400
3401The INTEGER type is a signed natural number type without any restrictions
3402 on its size.
3403 If the automatic checking on INTEGER value bounds are necessary, the subtype
3404 constraints must be used.
3405\layout LyX-Code
3406
3407SimpleInteger ::= INTEGER
3408\layout LyX-Code
3409
3410
3411\layout LyX-Code
3412
3413-- An integer with a very limited range
3414\layout LyX-Code
3415
3416SmallPositiveInt ::= INTEGER (0..127)
3417\layout LyX-Code
3418
3419
3420\layout LyX-Code
3421
3422-- Integer, negative
3423\layout LyX-Code
3424
3425NegativeInt ::= INTEGER (MIN..0)
3426\layout Subsection
3427
3428The ENUMERATED type
3429\layout Standard
3430
3431The ENUMERATED type is semantically equivalent to the INTEGER type with
3432 some integer values explicitly named.
3433\layout LyX-Code
3434
3435FruitId ::= ENUMERATED { apple(1), orange(2) }
3436\layout LyX-Code
3437
3438
3439\layout LyX-Code
3440
3441-- The numbers in braces are optional,
3442\layout LyX-Code
3443
3444-- the enumeration can be performed
3445\layout LyX-Code
3446
3447-- automatically by the compiler
3448\layout LyX-Code
3449
3450ComputerOSType ::= ENUMERATED {
3451\layout LyX-Code
3452
3453 FreeBSD, -- acquires value 0
3454\layout LyX-Code
3455
3456 Windows, -- acquires value 1
3457\layout LyX-Code
3458
3459 Solaris(5), -- remains 5
3460\layout LyX-Code
3461
3462 Linux, -- becomes 6
3463\layout LyX-Code
3464
3465 MacOS -- becomes 7
3466\layout LyX-Code
3467
3468}
3469\layout Subsection
3470
3471The OCTET STRING type
3472\layout Standard
3473
3474This type models the sequence of 8-bit bytes.
3475 This may be used to transmit some opaque data or data serialized by other
3476 types of encoders (i.e.
3477 video file, photo picture, etc).
3478\layout Subsection
3479
3480The OBJECT IDENTIFIER type
3481\layout Standard
3482
3483The OBJECT IDENTIFIER is used to represent the unique identifier of any
3484 object, starting from the very root of the registration tree.
3485 If your organization needs to uniquely identify something (a router, a
3486 room, a person, a standard, or whatever), you are encouraged to get your
3487 own identification subtree at
3488\begin_inset LatexCommand \htmlurl{http://www.iana.org/protocols/forms.htm}
3489
3490\end_inset
3491
3492.
3493\layout Standard
3494
3495For example, the very first ASN.1 module in this Chapter (RectangleModule1)
3496 has the following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
3497\layout LyX-Code
3498
3499ExampleOID ::= OBJECT IDENTIFIER
3500\layout LyX-Code
3501
3502
3503\layout LyX-Code
3504
3505rectangleModule1-oid ExampleOID
3506\layout LyX-Code
3507
3508 ::= { 1 3 6 1 4 1 9363 1 5 2 1 1 }
3509\layout LyX-Code
3510
3511
3512\layout LyX-Code
3513
3514-- An identifier of the Internet.
3515\layout LyX-Code
3516
3517internet-id OBJECT IDENTIFIER
3518\layout LyX-Code
3519
3520 ::= { iso(1) identified-organization(3)
3521\layout LyX-Code
3522
3523 dod(6) internet(1) }
3524\layout Standard
3525
3526As you see, names are optional.
3527\layout Subsection
3528
3529The RELATIVE-OID type
3530\layout Standard
3531
3532The RELATIVE-OID type has the semantics of a subtree of an OBJECT IDENTIFIER.
3533 There may be no need to repeat the whole sequence of numbers from the root
3534 of the registration tree where the only thing of interest is some of the
3535 tree's subsequence.
3536\layout LyX-Code
3537
3538this-document RELATIVE-OID ::= { docs(2) usage(1) }
3539\layout LyX-Code
3540
3541
3542\layout LyX-Code
3543
3544this-example RELATIVE-OID ::= {
3545\layout LyX-Code
3546
3547 this-document assorted-examples(0) this-example(1) }
3548\layout Section
3549
3550Some of the ASN.1 String Types
3551\layout Subsection
3552
3553The IA5String type
3554\layout Standard
3555
3556This is essentially the ASCII, with 128 character codes available (7 lower
3557 bits of an 8-bit byte).
3558\layout Subsection
3559
3560The UTF8String type
3561\layout Standard
3562
3563This is the character string which encodes the full Unicode range (4 bytes)
3564 using multibyte character sequences.
3565\layout Subsection
3566
3567The NumericString type
3568\layout Standard
3569
3570This type represents the character string with the alphabet consisting of
3571 numbers (
3572\begin_inset Quotes sld
3573\end_inset
3574
35750
3576\begin_inset Quotes srd
3577\end_inset
3578
3579 to
3580\begin_inset Quotes sld
3581\end_inset
3582
35839
3584\begin_inset Quotes srd
3585\end_inset
3586
3587) and a space.
3588\layout Subsection
3589
3590The PrintableString type
3591\layout Standard
3592
3593The character string with the following alphabet: space,
3594\begin_inset Quotes sld
3595\end_inset
3596
3597
3598\series bold
3599'
3600\series default
3601
3602\begin_inset Quotes srd
3603\end_inset
3604
3605 (single quote),
3606\begin_inset Quotes sld
3607\end_inset
3608
3609
3610\series bold
3611(
3612\series default
3613
3614\begin_inset Quotes sld
3615\end_inset
3616
3617,
3618\begin_inset Quotes sld
3619\end_inset
3620
3621
3622\series bold
3623)
3624\series default
3625
3626\begin_inset Quotes srd
3627\end_inset
3628
3629,
3630\begin_inset Quotes sld
3631\end_inset
3632
3633
3634\series bold
3635+
3636\series default
3637
3638\begin_inset Quotes srd
3639\end_inset
3640
3641,
3642\begin_inset Quotes sld
3643\end_inset
3644
3645
3646\series bold
3647,
3648\series default
3649
3650\begin_inset Quotes srd
3651\end_inset
3652
3653 (comma),
3654\begin_inset Quotes sld
3655\end_inset
3656
3657
3658\series bold
3659-
3660\series default
3661
3662\begin_inset Quotes srd
3663\end_inset
3664
3665,
3666\begin_inset Quotes sld
3667\end_inset
3668
3669
3670\series bold
3671.
3672\series default
3673
3674\begin_inset Quotes srd
3675\end_inset
3676
3677,
3678\begin_inset Quotes sld
3679\end_inset
3680
3681
3682\series bold
3683/
3684\series default
3685
3686\begin_inset Quotes srd
3687\end_inset
3688
3689, digits (
3690\begin_inset Quotes sld
3691\end_inset
3692
36930
3694\begin_inset Quotes srd
3695\end_inset
3696
3697 to
3698\begin_inset Quotes sld
3699\end_inset
3700
37019
3702\begin_inset Quotes srd
3703\end_inset
3704
3705),
3706\begin_inset Quotes sld
3707\end_inset
3708
3709
3710\series bold
3711:
3712\series default
3713
3714\begin_inset Quotes srd
3715\end_inset
3716
3717,
3718\begin_inset Quotes sld
3719\end_inset
3720
3721
3722\series bold
3723=
3724\series default
3725
3726\begin_inset Quotes srd
3727\end_inset
3728
3729,
3730\begin_inset Quotes sld
3731\end_inset
3732
3733
3734\series bold
3735?
3736\series default
3737
3738\begin_inset Quotes srd
3739\end_inset
3740
3741, upper-case and lower-case letters (
3742\begin_inset Quotes sld
3743\end_inset
3744
3745A
3746\begin_inset Quotes srd
3747\end_inset
3748
3749 to
3750\begin_inset Quotes sld
3751\end_inset
3752
3753Z
3754\begin_inset Quotes srd
3755\end_inset
3756
3757 and
3758\begin_inset Quotes sld
3759\end_inset
3760
3761a
3762\begin_inset Quotes srd
3763\end_inset
3764
3765 to
3766\begin_inset Quotes sld
3767\end_inset
3768
3769z
3770\begin_inset Quotes srd
3771\end_inset
3772
3773).
3774\layout Subsection
3775
3776The VisibleString type
3777\layout Standard
3778
3779The character string with the alphabet which is more or less a subset of
3780 ASCII between the space and the
3781\begin_inset Quotes sld
3782\end_inset
3783
3784
3785\series bold
3786~
3787\series default
3788
3789\begin_inset Quotes srd
3790\end_inset
3791
3792 symbol (tilde).
3793\layout Standard
3794
3795Alternatively, the alphabet may be described as the PrintableString alphabet
3796 presented earlier, plus the following characters:
3797\begin_inset Quotes sld
3798\end_inset
3799
3800
3801\series bold
3802!
3803\series default
3804
3805\begin_inset Quotes srd
3806\end_inset
3807
3808,
3809\begin_inset Quotes sld
3810\end_inset
3811
3812
3813\series bold
3814
3815\begin_inset Quotes srd
3816\end_inset
3817
3818
3819\series default
3820
3821\begin_inset Quotes srd
3822\end_inset
3823
3824,
3825\begin_inset Quotes sld
3826\end_inset
3827
3828
3829\series bold
3830#
3831\series default
3832
3833\begin_inset Quotes srd
3834\end_inset
3835
3836,
3837\begin_inset Quotes sld
3838\end_inset
3839
3840
3841\series bold
3842$
3843\series default
3844
3845\begin_inset Quotes srd
3846\end_inset
3847
3848,
3849\begin_inset Quotes sld
3850\end_inset
3851
3852
3853\series bold
3854%
3855\series default
3856
3857\begin_inset Quotes srd
3858\end_inset
3859
3860,
3861\begin_inset Quotes sld
3862\end_inset
3863
3864
3865\series bold
3866&
3867\series default
3868
3869\begin_inset Quotes srd
3870\end_inset
3871
3872,
3873\begin_inset Quotes sld
3874\end_inset
3875
3876
3877\series bold
3878*
3879\series default
3880
3881\begin_inset Quotes srd
3882\end_inset
3883
3884,
3885\begin_inset Quotes sld
3886\end_inset
3887
3888
3889\series bold
3890;
3891\series default
3892
3893\begin_inset Quotes srd
3894\end_inset
3895
3896,
3897\begin_inset Quotes sld
3898\end_inset
3899
3900
3901\series bold
3902<
3903\series default
3904
3905\begin_inset Quotes srd
3906\end_inset
3907
3908,
3909\begin_inset Quotes sld
3910\end_inset
3911
3912
3913\series bold
3914>
3915\series default
3916
3917\begin_inset Quotes srd
3918\end_inset
3919
3920,
3921\begin_inset Quotes sld
3922\end_inset
3923
3924
3925\series bold
3926[
3927\series default
3928
3929\begin_inset Quotes srd
3930\end_inset
3931
3932,
3933\begin_inset Quotes sld
3934\end_inset
3935
3936
3937\series bold
3938
3939\backslash
3940
3941\series default
3942
3943\begin_inset Quotes srd
3944\end_inset
3945
3946,
3947\begin_inset Quotes sld
3948\end_inset
3949
3950
3951\series bold
3952]
3953\series default
3954
3955\begin_inset Quotes srd
3956\end_inset
3957
3958,
3959\begin_inset Quotes sld
3960\end_inset
3961
3962
3963\series bold
3964^
3965\series default
3966
3967\begin_inset Quotes srd
3968\end_inset
3969
3970,
3971\begin_inset Quotes sld
3972\end_inset
3973
3974
3975\series bold
3976_
3977\series default
3978
3979\begin_inset Quotes srd
3980\end_inset
3981
3982,
3983\begin_inset Quotes sld
3984\end_inset
3985
3986
3987\series bold
3988`
3989\series default
3990
3991\begin_inset Quotes srd
3992\end_inset
3993
3994 (single left quote),
3995\begin_inset Quotes sld
3996\end_inset
3997
3998
3999\series bold
4000{
4001\series default
4002
4003\begin_inset Quotes srd
4004\end_inset
4005
4006,
4007\begin_inset Quotes sld
4008\end_inset
4009
4010
4011\series bold
4012|
4013\series default
4014
4015\begin_inset Quotes srd
4016\end_inset
4017
4018,
4019\begin_inset Quotes sld
4020\end_inset
4021
4022
4023\series bold
4024}
4025\series default
4026
4027\begin_inset Quotes srd
4028\end_inset
4029
4030,
4031\begin_inset Quotes sld
4032\end_inset
4033
4034
4035\series bold
4036~
4037\series default
4038
4039\begin_inset Quotes srd
4040\end_inset
4041
4042.
4043\layout Section
4044
4045ASN.1 Constructed Types
4046\layout Subsection
4047
4048The SEQUENCE type
4049\layout Standard
4050
4051This is an ordered collection of other simple or constructed types.
4052 The SEQUENCE constructed type resembles the C
4053\begin_inset Quotes sld
4054\end_inset
4055
4056struct
4057\begin_inset Quotes srd
4058\end_inset
4059
4060 statement.
4061\layout LyX-Code
4062
4063Address ::= SEQUENCE {
4064\layout LyX-Code
4065
4066 -- The apartment number may be omitted
4067\layout LyX-Code
4068
4069 apartmentNumber NumericString OPTIONAL,
4070\layout LyX-Code
4071
4072 streetName PrintableString,
4073\layout LyX-Code
4074
4075 cityName PrintableString,
4076\layout LyX-Code
4077
4078 stateName PrintableString,
4079\layout LyX-Code
4080
4081 -- This one may be omitted too
4082\layout LyX-Code
4083
4084 zipNo NumericString OPTIONAL
4085\layout LyX-Code
4086
4087}
4088\layout Subsection
4089
4090The SET type
4091\layout Standard
4092
4093This is a collection of other simple or constructed types.
4094 Ordering is not important.
4095 The data may arrive in the order which is different from the order of specifica
4096tion.
4097 Data is encoded in the order not necessarily corresponding to the order
4098 of specification.
4099\layout Subsection
4100
4101The CHOICE type
4102\layout Standard
4103
4104This type is just a choice between the subtypes specified in it.
4105 The CHOICE type contains at most one of the subtypes specified, and it
4106 is always implicitly known which choice is being decoded or encoded.
4107 This one resembles the C
4108\begin_inset Quotes sld
4109\end_inset
4110
4111union
4112\begin_inset Quotes srd
4113\end_inset
4114
4115 statement.
4116\layout Standard
4117
4118The following type defines a response code, which may be either an integer
4119 code or a boolean
4120\begin_inset Quotes sld
4121\end_inset
4122
4123true
4124\begin_inset Quotes srd
4125\end_inset
4126
4127/
4128\begin_inset Quotes srd
4129\end_inset
4130
4131false
4132\begin_inset Quotes srd
4133\end_inset
4134
4135 code.
4136\layout LyX-Code
4137
4138ResponseCode ::= CHOICE {
4139\layout LyX-Code
4140
4141 intCode INTEGER,
4142\layout LyX-Code
4143
4144 boolCode BOOLEAN
4145\layout LyX-Code
4146
4147}
4148\layout LyX-Code
4149
4150\layout Subsection
4151
4152The SEQUENCE OF type
4153\layout Standard
4154
4155This one is the list (array) of simple or constructed types:
4156\layout LyX-Code
4157
4158-- Example 1
4159\layout LyX-Code
4160
4161ManyIntegers ::= SEQUENCE OF INTEGER
4162\layout LyX-Code
4163
4164
4165\layout LyX-Code
4166
4167-- Example 2
4168\layout LyX-Code
4169
4170ManyRectangles ::= SEQUENCE OF Rectangle
4171\layout LyX-Code
4172
4173
4174\layout LyX-Code
4175
4176-- More complex example:
4177\layout LyX-Code
4178
4179-- an array of structures defined in place.
4180\layout LyX-Code
4181
4182ManyCircles ::= SEQUENCE OF SEQUENCE {
4183\layout LyX-Code
4184
4185 radius INTEGER
4186\layout LyX-Code
4187
4188 }
4189\layout Subsection
4190
4191The SET OF type
4192\layout Standard
4193
4194The SET OF type models the bag of structures.
4195 It resembles the SEQUENCE OF type, but the order is not important: i.e.
4196 the elements may arrive in the order which is not necessarily the same
4197 as the in-memory order on the remote machines.
4198\layout LyX-Code
4199
4200-- A set of structures defined elsewhere
4201\layout LyX-Code
4202
4203SetOfApples :: SET OF Apple
4204\layout LyX-Code
4205
4206
4207\layout LyX-Code
4208
4209-- Set of integers encoding the kind of a fruit
4210\layout LyX-Code
4211
4212FruitBag ::= SET OF ENUMERATED { apple, orange }
vlme18adea2004-06-03 03:49:45 +00004213\layout Bibliography
vlmfcec8e52004-08-23 15:12:04 +00004214\bibitem [ASN1C]{ASN1C}
4215
vlmc7737f62005-02-02 09:53:46 +00004216The Open Source ASN.1 Compiler.
vlmfcec8e52004-08-23 15:12:04 +00004217
vlmc7737f62005-02-02 09:53:46 +00004218\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c}
vlmfcec8e52004-08-23 15:12:04 +00004219
4220\end_inset
4221
4222
4223\layout Bibliography
vlm4cb0edf2005-01-14 11:38:49 +00004224\bibitem [AONL]{AONL}
4225
4226Online ASN.1 Compiler.
4227
4228\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c/asn1c.cgi}
4229
4230\end_inset
4231
4232
4233\layout Bibliography
vlme18adea2004-06-03 03:49:45 +00004234\bibitem [Dub00]{Dub00}
4235
vlmaf2155e2004-10-20 15:48:55 +00004236Olivier Dubuisson ---
vlme18adea2004-06-03 03:49:45 +00004237\emph on
4238ASN.1 Communication between heterogeneous systems
4239\emph default
vlmaf2155e2004-10-20 15:48:55 +00004240 --- Morgan Kaufmann Publishers, 2000.
vlme18adea2004-06-03 03:49:45 +00004241
4242\begin_inset LatexCommand \htmlurl{http://asn1.elibel.tm.fr/en/book/}
4243
4244\end_inset
4245
4246.
4247 ISBN:0-12-6333361-0.
4248\layout Bibliography
4249\bibitem [ITU-T/ASN.1]{ITU-T/ASN.1}
4250
4251ITU-T Study Group 17 -- Languages for Telecommunication Systems
4252\begin_inset LatexCommand \url{http://www.itu.int/ITU-T/studygroups/com17/languages/}
4253
4254\end_inset
4255
4256
4257\the_end