blob: f000c895bf3e96150a669c271ced7d5417dad4dd [file] [log] [blame]
Lev Walkin84291e02004-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
Lev Walkin26587ab2004-08-23 15:12:04 +00004\begin_preamble
Lev Walkin26587ab2004-08-23 15:12:04 +00005\usepackage{extramarks}
6\lhead{\firstxmark}
7\rfoot{\lastxmark}
Lev Walkind3630792004-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},
Lev Walkindee61782005-02-02 09:53:46 +000015pdftitle={Using the Open Source ASN.1 Compiler},
Lev Walkind3630792004-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}
Lev Walkin26587ab2004-08-23 15:12:04 +000023\end_preamble
Lev Walkin84291e02004-06-03 03:49:45 +000024\language english
25\inputencoding latin1
Lev Walkin26587ab2004-08-23 15:12:04 +000026\fontscheme times
Lev Walkin84291e02004-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
Lev Walkind3630792004-09-30 22:26:19 +000037\secnumdepth 2
38\tocdepth 2
Lev Walkin84291e02004-06-03 03:49:45 +000039\paragraph_separation indent
40\defskip medskip
41\quotes_language swedish
42\quotes_times 2
43\papercolumns 1
Lev Walkin26587ab2004-08-23 15:12:04 +000044\papersides 2
45\paperpagestyle fancy
Lev Walkin84291e02004-06-03 03:49:45 +000046
47\layout Title
48
Lev Walkindee61782005-02-02 09:53:46 +000049Using the Open Source ASN.1 Compiler
Lev Walkin84291e02004-06-03 03:49:45 +000050\layout Author
51
52Lev Walkin <
Lev Walkind3630792004-09-30 22:26:19 +000053\begin_inset ERT
54status Collapsed
Lev Walkin84291e02004-06-03 03:49:45 +000055
Lev Walkind3630792004-09-30 22:26:19 +000056\layout Standard
57
58\backslash
59href{mailto:vlm@lionet.info?Subject=asn1c}{vlm@lionet.info}
Lev Walkin84291e02004-06-03 03:49:45 +000060\end_inset
61
62>
63\layout Standard
64
65
Lev Walkin26587ab2004-08-23 15:12:04 +000066\begin_inset ERT
67status Open
68
69\layout Standard
70
71\backslash
Lev Walkin7f70fe52005-02-22 07:28:26 +000072lhead{This document describes
Lev Walkind3630792004-09-30 22:26:19 +000073\backslash
Lev Walkinf47d0682005-03-05 01:07:04 +000074href{http://lionet.info/asn1c}{asn1c-0.9.11}}
Lev Walkind3630792004-09-30 22:26:19 +000075\layout Standard
76
77\backslash
78rhead{$Revision$}
Lev Walkin26587ab2004-08-23 15:12:04 +000079\end_inset
80
81
82\layout Standard
83
84
Lev Walkin84291e02004-06-03 03:49:45 +000085\begin_inset LatexCommand \tableofcontents{}
86
87\end_inset
88
89
Lev Walkin26587ab2004-08-23 15:12:04 +000090\layout Standard
91
92
93\begin_inset ERT
Lev Walkind3630792004-09-30 22:26:19 +000094status Open
Lev Walkin26587ab2004-08-23 15:12:04 +000095
96\layout Standard
97
98\backslash
99pagestyle{headings}
100\end_inset
101
102
Lev Walkin4a83dd52004-08-07 05:00:07 +0000103\layout Part
104
Lev Walkin09889622005-02-21 14:16:10 +0000105Using the ASN.1 Compiler
Lev Walkin84291e02004-06-03 03:49:45 +0000106\layout Chapter
107
Lev Walkin09889622005-02-21 14:16:10 +0000108Introduction to the ASN.1 Compiler
Lev Walkin84291e02004-06-03 03:49:45 +0000109\layout Standard
110
Lev Walkin09889622005-02-21 14:16:10 +0000111The purpose of the ASN.1 compiler, of which this document is part, is to
Lev Walkin7f70fe52005-02-22 07:28:26 +0000112 convert the specifications in ASN.1 notation into some other language.
Lev Walkin09889622005-02-21 14:16:10 +0000113 At this moment, only C and C++ target languages are supported, the latter
Lev Walkin7f70fe52005-02-22 07:28:26 +0000114 is in upward compatibility mode.
Lev Walkin84291e02004-06-03 03:49:45 +0000115\layout Standard
116
Lev Walkin09889622005-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.
Lev Walkin7f70fe52005-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).
Lev Walkin84291e02004-06-03 03:49:45 +0000123\layout Standard
124
Lev Walkin09889622005-02-21 14:16:10 +0000125For example, suppose the following ASN.1 module is given
126\begin_inset Foot
127collapsed true
Lev Walkin84291e02004-06-03 03:49:45 +0000128
Lev Walkin84291e02004-06-03 03:49:45 +0000129\layout Standard
130
Lev Walkin09889622005-02-21 14:16:10 +0000131Please look into Part
132\begin_inset LatexCommand \vref{par:ASN.1-Basics}
133
Lev Walkin84291e02004-06-03 03:49:45 +0000134\end_inset
135
Lev Walkin09889622005-02-21 14:16:10 +0000136 for a quick reference on how to understand the ASN.1 notation.
Lev Walkin84291e02004-06-03 03:49:45 +0000137\end_inset
138
Lev Walkin09889622005-02-21 14:16:10 +0000139:
Lev Walkin84291e02004-06-03 03:49:45 +0000140\layout LyX-Code
141
Lev Walkin09889622005-02-21 14:16:10 +0000142RectangleTest DEFINITIONS ::=
Lev Walkin84291e02004-06-03 03:49:45 +0000143\layout LyX-Code
144
145BEGIN
146\layout LyX-Code
147
148
149\layout LyX-Code
150
Lev Walkin84291e02004-06-03 03:49:45 +0000151Rectangle ::= SEQUENCE {
152\layout LyX-Code
153
154 height INTEGER, -- Height of the rectangle
155\layout LyX-Code
156
Lev Walkin26587ab2004-08-23 15:12:04 +0000157 width INTEGER -- Width of the rectangle
Lev Walkin84291e02004-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
Lev Walkin09889622005-02-21 14:16:10 +0000169The compiler would read this ASN.1 definition and produce the following C
170 type
Lev Walkin84291e02004-06-03 03:49:45 +0000171\begin_inset Foot
Lev Walkin09889622005-02-21 14:16:10 +0000172collapsed true
Lev Walkin84291e02004-06-03 03:49:45 +0000173
174\layout Standard
175
176
177\emph on
Lev Walkin99301892004-09-14 12:48:17 +0000178-fnative-types
Lev Walkin84291e02004-06-03 03:49:45 +0000179\emph default
180 compiler option is used to produce basic C
181\emph on
182int
183\emph default
Lev Walkin26587ab2004-08-23 15:12:04 +0000184 types instead of infinite width INTEGER_t structures.
185 See Table
186\begin_inset LatexCommand \vref{cap:asn1c-cmdopts}
187
188\end_inset
189
190.
Lev Walkin84291e02004-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
Lev Walkin09889622005-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
Lev Walkin84291e02004-06-03 03:49:45 +0000212
Lev Walkin09889622005-02-21 14:16:10 +0000213Quick start with asn1c
Lev Walkin84291e02004-06-03 03:49:45 +0000214\layout Standard
215
Lev Walkin26587ab2004-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
Lev Walkin09889622005-02-21 14:16:10 +0000241 command may be used to compile the ASN.1 module
Lev Walkin84291e02004-06-03 03:49:45 +0000242\begin_inset Foot
Lev Walkin09889622005-02-21 14:16:10 +0000243collapsed true
Lev Walkin84291e02004-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
Lev Walkin7f70fe52005-02-22 07:28:26 +0000252 and check the Table
Lev Walkin09889622005-02-21 14:16:10 +0000253\begin_inset LatexCommand \vref{cap:asn1c-cmdopts}
254
255\end_inset
256
Lev Walkin26587ab2004-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.
Lev Walkin84291e02004-06-03 03:49:45 +0000266\end_inset
267
268:
269\layout LyX-Code
270
271asn1c
272\emph on
Lev Walkin09889622005-02-21 14:16:10 +0000273<module.asn1>
Lev Walkin84291e02004-06-03 03:49:45 +0000274\layout Standard
275
Lev Walkin09889622005-02-21 14:16:10 +0000276If several ASN.1 modules contain interdependencies, all of the files must
Lev Walkin26587ab2004-08-23 15:12:04 +0000277 be specified altogether:
Lev Walkin84291e02004-06-03 03:49:45 +0000278\layout LyX-Code
279
280asn1c
281\emph on
Lev Walkin09889622005-02-21 14:16:10 +0000282<module1.asn1> <module2.asn1> ...
Lev Walkin84291e02004-06-03 03:49:45 +0000283\layout Standard
284
Lev Walkin26587ab2004-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
Lev Walkin84291e02004-06-03 03:49:45 +0000301 is properly supported by the compiler.
302\layout LyX-Code
303
Lev Walkin26587ab2004-08-23 15:12:04 +0000304asn1c
305\series bold
306-EF
307\series default
308
Lev Walkin84291e02004-06-03 03:49:45 +0000309\emph on
Lev Walkin09889622005-02-21 14:16:10 +0000310<module-to-test.asn1>
Lev Walkin84291e02004-06-03 03:49:45 +0000311\layout Standard
312
Lev Walkin26587ab2004-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
Lev Walkin09889622005-02-21 14:16:10 +0000341<module-to-compile-and-print.asn1>
Lev Walkin84291e02004-06-03 03:49:45 +0000342\layout Section
343
Lev Walkin09889622005-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
441Command line options
Lev Walkin26587ab2004-08-23 15:12:04 +0000442\layout Standard
443
444The Table
445\begin_inset LatexCommand \vref{cap:asn1c-cmdopts}
446
447\end_inset
448
449 summarizes various options affecting the compiler's behavior.
450\layout Standard
451
452
453\begin_inset Float table
Lev Walkin09889622005-02-21 14:16:10 +0000454placement h
Lev Walkin26587ab2004-08-23 15:12:04 +0000455wide false
Lev Walkindd32b592004-09-06 08:07:29 +0000456collapsed false
Lev Walkin26587ab2004-08-23 15:12:04 +0000457
458\layout Standard
459
460
461\begin_inset Tabular
Lev Walkinf47d0682005-03-05 01:07:04 +0000462<lyxtabular version="3" rows="24" columns="2">
Lev Walkind3630792004-09-30 22:26:19 +0000463<features islongtable="true">
Lev Walkin26587ab2004-08-23 15:12:04 +0000464<column alignment="left" valignment="top" leftline="true" width="0">
465<column alignment="block" valignment="top" leftline="true" rightline="true" width="3in">
466<row topline="true" bottomline="true">
467<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
468\begin_inset Text
469
470\layout Standard
471
472
473\series bold
Lev Walkinf47d0682005-03-05 01:07:04 +0000474\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000475Overall Options
476\end_inset
477</cell>
478<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
479\begin_inset Text
480
481\layout Standard
482
483
484\series bold
Lev Walkinf47d0682005-03-05 01:07:04 +0000485\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000486Description
487\end_inset
488</cell>
489</row>
490<row topline="true">
491<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
492\begin_inset Text
493
494\layout Standard
495
Lev Walkinf47d0682005-03-05 01:07:04 +0000496
497\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000498-E
499\end_inset
500</cell>
501<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
502\begin_inset Text
503
504\layout Standard
505
506
507\size small
508Stop after the parsing stage and print the reconstructed ASN.1 specification
509 code to the standard output.
510\end_inset
511</cell>
512</row>
513<row topline="true">
514<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
515\begin_inset Text
516
517\layout Standard
518
Lev Walkinf47d0682005-03-05 01:07:04 +0000519
520\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000521-F
522\end_inset
523</cell>
524<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
525\begin_inset Text
526
527\layout Standard
528
529
530\size small
531Used together with -E, instructs the compiler to stop after the ASN.1 syntax
532 tree fixing stage and dump the reconstructed ASN.1 specification to the
533 standard output.
534\end_inset
535</cell>
536</row>
537<row topline="true">
538<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
539\begin_inset Text
540
541\layout Standard
542
Lev Walkinf47d0682005-03-05 01:07:04 +0000543
544\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000545-P
546\end_inset
547</cell>
548<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
549\begin_inset Text
550
551\layout Standard
552
553
554\size small
555Dump the compiled output to the standard output instead of cre- ating the
556 target language files on disk.
557\end_inset
558</cell>
559</row>
560<row topline="true">
561<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
562\begin_inset Text
563
564\layout Standard
565
Lev Walkinf47d0682005-03-05 01:07:04 +0000566
567\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000568-R
569\end_inset
570</cell>
571<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
572\begin_inset Text
573
574\layout Standard
575
576
577\size small
578Restrict the compiler to generate only the ASN.1 tables, omit- ting the usual
579 support code.
580\end_inset
581</cell>
582</row>
Lev Walkinf7484512004-10-13 09:13:56 +0000583<row topline="true">
Lev Walkin26587ab2004-08-23 15:12:04 +0000584<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
585\begin_inset Text
586
587\layout Standard
588
Lev Walkinf47d0682005-03-05 01:07:04 +0000589
590\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000591-S
592\emph on
593<directory>
594\end_inset
595</cell>
596<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
597\begin_inset Text
598
599\layout Standard
600
601
602\size small
603Use the specified directory with ASN.1 skeleton files.
604\end_inset
605</cell>
606</row>
Lev Walkinf7484512004-10-13 09:13:56 +0000607<row topline="true" bottomline="true">
608<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
609\begin_inset Text
610
611\layout Standard
612
Lev Walkinf47d0682005-03-05 01:07:04 +0000613
614\size small
Lev Walkinf7484512004-10-13 09:13:56 +0000615-X
616\end_inset
617</cell>
618<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
619\begin_inset Text
620
621\layout Standard
622
Lev Walkinf47d0682005-03-05 01:07:04 +0000623
624\size small
Lev Walkinf7484512004-10-13 09:13:56 +0000625Generate the XML DTD for the specified ASN.1 modules.
626\end_inset
627</cell>
628</row>
Lev Walkin26587ab2004-08-23 15:12:04 +0000629<row topline="true">
630<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
631\begin_inset Text
632
633\layout Standard
634
635
636\series bold
Lev Walkinf47d0682005-03-05 01:07:04 +0000637\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000638Warning Options
639\end_inset
640</cell>
641<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
642\begin_inset Text
643
644\layout Standard
645
646
647\series bold
Lev Walkinf47d0682005-03-05 01:07:04 +0000648\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000649Description
650\end_inset
651</cell>
652</row>
653<row topline="true">
654<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
655\begin_inset Text
656
657\layout Standard
658
Lev Walkinf47d0682005-03-05 01:07:04 +0000659
660\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000661-Werror
662\end_inset
663</cell>
664<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
665\begin_inset Text
666
667\layout Standard
668
669
670\size small
671Treat warnings as errors; abort if any warning is produced.
672\end_inset
673</cell>
674</row>
675<row topline="true">
676<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
677\begin_inset Text
678
679\layout Standard
680
Lev Walkinf47d0682005-03-05 01:07:04 +0000681
682\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000683-Wdebug-lexer
684\end_inset
685</cell>
686<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
687\begin_inset Text
688
689\layout Standard
690
691
692\size small
693Enable lexer debugging during the ASN.1 parsing stage.
694\end_inset
695</cell>
696</row>
697<row topline="true">
698<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
699\begin_inset Text
700
701\layout Standard
702
Lev Walkinf47d0682005-03-05 01:07:04 +0000703
704\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000705-Wdebug-fixer
706\end_inset
707</cell>
708<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
709\begin_inset Text
710
711\layout Standard
712
713
714\size small
715 Enable ASN.1 syntax tree fixer debugging during the fixing stage.
716\end_inset
717</cell>
718</row>
719<row topline="true" bottomline="true">
720<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
721\begin_inset Text
722
723\layout Standard
724
Lev Walkinf47d0682005-03-05 01:07:04 +0000725
726\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000727-Wdebug-compiler
728\end_inset
729</cell>
730<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
731\begin_inset Text
732
733\layout Standard
734
735
736\size small
737Enable debugging during the actual compile time.
738\end_inset
739</cell>
740</row>
741<row topline="true">
742<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
743\begin_inset Text
744
745\layout Standard
746
747
748\series bold
Lev Walkinf47d0682005-03-05 01:07:04 +0000749\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000750Language Options
751\end_inset
752</cell>
753<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
754\begin_inset Text
755
756\layout Standard
757
758
759\series bold
Lev Walkinf47d0682005-03-05 01:07:04 +0000760\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000761Description
762\end_inset
763</cell>
764</row>
765<row topline="true">
766<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
767\begin_inset Text
768
769\layout Standard
770
Lev Walkinf47d0682005-03-05 01:07:04 +0000771
772\size small
Lev Walkindd32b592004-09-06 08:07:29 +0000773-fall-defs-global
774\end_inset
775</cell>
776<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
777\begin_inset Text
778
779\layout Standard
780
Lev Walkinf47d0682005-03-05 01:07:04 +0000781
782\size small
Lev Walkin1f756282004-09-29 13:29:17 +0000783Normally the compiler hides the definitions (asn_DEF_xxx) of the inner structure
784 elements (members of SEQUENCE, SET and other types).
Lev Walkindd32b592004-09-06 08:07:29 +0000785 This option makes all such definitions global.
Lev Walkin1f756282004-09-29 13:29:17 +0000786 Enabling this option may pollute the namespace by making lots of asn_DEF_xxx
Lev Walkindd32b592004-09-06 08:07:29 +0000787 structures globally visible, but will allow you to manipulate (encode and
788 decode) the individual members of any complex ASN.1 structure.
789\end_inset
790</cell>
791</row>
792<row topline="true">
793<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
794\begin_inset Text
795
796\layout Standard
797
Lev Walkinf47d0682005-03-05 01:07:04 +0000798
799\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000800-fbless-SIZE
801\end_inset
802</cell>
803<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
804\begin_inset Text
805
806\layout Standard
807
808
809\size small
810Allow SIZE() constraint for INTEGER, ENUMERATED, and other types for which
811 this constraint is normally prohibited by the standard.
812 This is a violation of an ASN.1 standard and compiler may fail to produce
813 the meaningful code.
814\end_inset
815</cell>
816</row>
817<row topline="true">
818<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
819\begin_inset Text
820
821\layout Standard
822
Lev Walkinf47d0682005-03-05 01:07:04 +0000823
824\size small
825-fcompound-names
826\end_inset
827</cell>
828<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
829\begin_inset Text
830
831\layout Standard
832
833
834\size small
835Use complex names for C structures.
836 Using complex names prevents name clashes in case the module reuses the
837 same identifiers in multiple contexts.
838\end_inset
839</cell>
840</row>
841<row topline="true">
842<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
843\begin_inset Text
844
845\layout Standard
846
847
848\size small
Lev Walkin99301892004-09-14 12:48:17 +0000849-fnative-types
Lev Walkin26587ab2004-08-23 15:12:04 +0000850\end_inset
851</cell>
852<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
853\begin_inset Text
854
855\layout Standard
856
857
858\size small
Lev Walkin99301892004-09-14 12:48:17 +0000859Use the native machine's data types (int, double) whenever possible, instead
Lev Walkind3630792004-09-30 22:26:19 +0000860 of the compound INTEGER_t, ENUMERATED_t and REAL_t types.
Lev Walkin26587ab2004-08-23 15:12:04 +0000861
862\end_inset
863</cell>
864</row>
865<row topline="true">
866<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
867\begin_inset Text
868
869\layout Standard
870
Lev Walkinf47d0682005-03-05 01:07:04 +0000871
872\size small
Lev Walkin4e940a02004-09-26 13:13:13 +0000873-fno-constraints
874\end_inset
875</cell>
876<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
877\begin_inset Text
878
879\layout Standard
880
Lev Walkinf47d0682005-03-05 01:07:04 +0000881
882\size small
Lev Walkin4e940a02004-09-26 13:13:13 +0000883Do not generate ASN.1 subtype constraint checking code.
Lev Walkinf47d0682005-03-05 01:07:04 +0000884 This may produce a shorter executable.
Lev Walkin4e940a02004-09-26 13:13:13 +0000885\end_inset
886</cell>
887</row>
888<row topline="true">
889<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
890\begin_inset Text
891
892\layout Standard
893
Lev Walkinf47d0682005-03-05 01:07:04 +0000894
895\size small
896-fno-include-deps
897\end_inset
898</cell>
899<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
900\begin_inset Text
901
902\layout Standard
903
904
905\size small
906Do not generate courtesy #include lines for non-critical dependencies.
907\end_inset
908</cell>
909</row>
910<row topline="true">
911<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
912\begin_inset Text
913
914\layout Standard
915
916
917\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000918-funnamed-unions
919\end_inset
920</cell>
921<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
922\begin_inset Text
923
924\layout Standard
925
926
927\size small
928Enable unnamed unions in the definitions of target language's structures.
929\end_inset
930</cell>
931</row>
932<row topline="true" bottomline="true">
933<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
934\begin_inset Text
935
936\layout Standard
937
Lev Walkinf47d0682005-03-05 01:07:04 +0000938
939\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000940-ftypes88
941\end_inset
942</cell>
943<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
944\begin_inset Text
945
946\layout Standard
947
948
949\size small
Lev Walkind3630792004-09-30 22:26:19 +0000950Pretend to support only ASN.1:1988 embedded types.
951 Certain reserved words, such as UniversalString and BMPString, become ordinary
952 type references and may be redefined by the specification.
Lev Walkin26587ab2004-08-23 15:12:04 +0000953\end_inset
954</cell>
955</row>
Lev Walkind3630792004-09-30 22:26:19 +0000956<row topline="true" newpage="true">
Lev Walkin26587ab2004-08-23 15:12:04 +0000957<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
958\begin_inset Text
959
960\layout Standard
961
962
963\series bold
Lev Walkinf47d0682005-03-05 01:07:04 +0000964\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000965Output Options
966\end_inset
967</cell>
968<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
969\begin_inset Text
970
971\layout Standard
972
973
974\series bold
Lev Walkinf47d0682005-03-05 01:07:04 +0000975\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000976Description
977\end_inset
978</cell>
979</row>
980<row topline="true">
981<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
982\begin_inset Text
983
984\layout Standard
985
Lev Walkinf47d0682005-03-05 01:07:04 +0000986
987\size small
Lev Walkin26587ab2004-08-23 15:12:04 +0000988-print-constraints
989\end_inset
990</cell>
991<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
992\begin_inset Text
993
994\layout Standard
995
996
997\size small
998When -EF are also specified, this option forces the compiler to explain
999 its internal understanding of subtype constraints.
1000\end_inset
1001</cell>
1002</row>
1003<row topline="true" bottomline="true">
1004<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1005\begin_inset Text
1006
1007\layout Standard
1008
Lev Walkinf47d0682005-03-05 01:07:04 +00001009
1010\size small
Lev Walkin26587ab2004-08-23 15:12:04 +00001011-print-lines
1012\end_inset
1013</cell>
1014<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1015\begin_inset Text
1016
1017\layout Standard
1018
1019
1020\size small
1021Generate "-- #line" comments in -E output.
1022\end_inset
1023</cell>
1024</row>
1025</lyxtabular>
1026
1027\end_inset
1028
1029
1030\layout Caption
1031
1032
1033\begin_inset LatexCommand \label{cap:asn1c-cmdopts}
1034
1035\end_inset
1036
1037The list of asn1c command line options
1038\end_inset
1039
1040
Lev Walkin09889622005-02-21 14:16:10 +00001041\layout Chapter
1042
1043Using the ASN.1 Compiler
Lev Walkin26587ab2004-08-23 15:12:04 +00001044\layout Section
Lev Walkin84291e02004-06-03 03:49:45 +00001045
Lev Walkin09889622005-02-21 14:16:10 +00001046Invoking the ASN.1 helper code
Lev Walkind3630792004-09-30 22:26:19 +00001047\begin_inset OptArg
1048collapsed true
1049
1050\layout Standard
1051
1052Invoking the helper code
1053\end_inset
1054
1055
Lev Walkin84291e02004-06-03 03:49:45 +00001056\layout Standard
1057
Lev Walkin7f70fe52005-02-22 07:28:26 +00001058First of all, you should include one or more header files into your application.
1059 Typically, it is enough to include the header file of the main PDU type.
1060 For our Rectangle module, including the Rectangle.h file is sufficient:
Lev Walkin84291e02004-06-03 03:49:45 +00001061\layout LyX-Code
1062
1063#include <Rectangle.h>
1064\layout Standard
1065
1066The header files defines the C structure corresponding to the ASN.1 definition
1067 of a rectangle and the declaration of the ASN.1 type descriptor, which is
1068 used as an argument to most of the functions provided by the ASN.1 module.
1069 For example, here is the code which frees the Rectangle_t structure:
1070\layout LyX-Code
1071
Lev Walkin26587ab2004-08-23 15:12:04 +00001072Rectangle_t *rect = ...;
1073\layout LyX-Code
1074
1075
Lev Walkin84291e02004-06-03 03:49:45 +00001076\layout LyX-Code
1077
Lev Walkin1f756282004-09-29 13:29:17 +00001078asn_DEF_Rectangle->free_struct(&asn_DEF_Rectangle,
Lev Walkin84291e02004-06-03 03:49:45 +00001079\layout LyX-Code
1080
1081 rect, 0);
1082\layout Standard
1083
1084This code defines a
1085\emph on
1086rect
1087\emph default
1088 pointer which points to the Rectangle_t structure which needs to be freed.
Lev Walkin7f70fe52005-02-22 07:28:26 +00001089 The second line invokes the generic
1090\emph on
1091free_struct()
1092\emph default
1093 routine created specifically for this Rectangle_t structure.
Lev Walkin84291e02004-06-03 03:49:45 +00001094 The
1095\emph on
Lev Walkin1f756282004-09-29 13:29:17 +00001096asn_DEF_Rectangle
Lev Walkin84291e02004-06-03 03:49:45 +00001097\emph default
Lev Walkin7f70fe52005-02-22 07:28:26 +00001098 is the type descriptor, which holds a collection of routines to deal with
1099 the Rectangle_t structure.
Lev Walkin84291e02004-06-03 03:49:45 +00001100\layout Standard
1101
Lev Walkin7f70fe52005-02-22 07:28:26 +00001102The following member functions of the asn_DEF_Rectangle type descriptor
1103 are of interest:
Lev Walkin84291e02004-06-03 03:49:45 +00001104\layout Description
1105
Lev Walkin84291e02004-06-03 03:49:45 +00001106ber_decoder This is the generic
1107\emph on
1108restartable
1109\begin_inset Foot
1110collapsed false
1111
1112\layout Standard
1113
1114Restartable means that if the decoder encounters the end of the buffer,
1115 it will fail, but may later be invoked again with the rest of the buffer
1116 to continue decoding.
1117\end_inset
1118
1119
1120\emph default
1121BER decoder (Basic Encoding Rules).
1122 This decoder would create and/or fill the target structure for you.
1123 Please refer to Section
Lev Walkin09889622005-02-21 14:16:10 +00001124\begin_inset LatexCommand \vref{sub:Decoding-BER}
Lev Walkin84291e02004-06-03 03:49:45 +00001125
1126\end_inset
1127
1128.
1129\layout Description
1130
1131der_encoder This is the generic DER encoder (Distinguished Encoding Rules).
Lev Walkin35d446b2004-09-24 20:56:07 +00001132 This encoder will take the target structure and encode it into a series
Lev Walkin84291e02004-06-03 03:49:45 +00001133 of bytes.
1134 Please refer to Section
Lev Walkin09889622005-02-21 14:16:10 +00001135\begin_inset LatexCommand \vref{sub:Encoding-DER}
Lev Walkin84291e02004-06-03 03:49:45 +00001136
1137\end_inset
1138
1139.
1140\layout Description
1141
Lev Walkin09889622005-02-21 14:16:10 +00001142xer_encoder This is the XER encoder (XML Encoding Rules).
Lev Walkin35d446b2004-09-24 20:56:07 +00001143 This encoder will take the target structure and represent it as an XML
Lev Walkin09889622005-02-21 14:16:10 +00001144 (text) document using either BASIC-XER or CANONICAL-XER encoding rules.
Lev Walkin35d446b2004-09-24 20:56:07 +00001145 Please refer to Section
Lev Walkin09889622005-02-21 14:16:10 +00001146\begin_inset LatexCommand \vref{sub:Encoding-XER}
1147
1148\end_inset
1149
1150.
1151\layout Description
1152
1153xer_decoder This is the generic XER decoder.
1154 It takes both BASIC-XER or CANONICAL-XER encodings and deserializes the
1155 data into a local, machine-dependent representation.
1156 Please refer to Section
1157\begin_inset LatexCommand \vref{sub:Decoding-XER}
Lev Walkin35d446b2004-09-24 20:56:07 +00001158
1159\end_inset
1160
1161.
1162\layout Description
1163
1164check_constraints Check that the contents of the target structure are semantical
1165ly valid and constrained to appropriate implicit or explicit subtype constraints.
1166 Please refer to Section
1167\begin_inset LatexCommand \vref{sub:Validating-the-target}
1168
1169\end_inset
1170
1171.
1172\layout Description
1173
Lev Walkin84291e02004-06-03 03:49:45 +00001174print_struct This function convert the contents of the passed target structure
1175 into human readable form.
1176 This form is not formal and cannot be converted back into the structure,
1177 but it may turn out to be useful for debugging or quick-n-dirty printing.
1178 Please refer to Section
Lev Walkin09889622005-02-21 14:16:10 +00001179\begin_inset LatexCommand \vref{sub:Printing-the-target}
Lev Walkin84291e02004-06-03 03:49:45 +00001180
1181\end_inset
1182
1183.
1184\layout Description
1185
1186free_struct This is a generic disposal which frees the target structure.
1187 Please refer to Section
Lev Walkin09889622005-02-21 14:16:10 +00001188\begin_inset LatexCommand \vref{sub:Freeing-the-target}
Lev Walkin35d446b2004-09-24 20:56:07 +00001189
1190\end_inset
1191
1192.
1193\layout Standard
1194
Lev Walkin84291e02004-06-03 03:49:45 +00001195Each of the above function takes the type descriptor (
1196\emph on
Lev Walkin1f756282004-09-29 13:29:17 +00001197asn_DEF_\SpecialChar \ldots{}
Lev Walkin84291e02004-06-03 03:49:45 +00001198
1199\emph default
1200) and the target structure (
1201\emph on
1202rect
1203\emph default
1204, in the above example).
Lev Walkin09889622005-02-21 14:16:10 +00001205\layout Subsection
1206
1207
1208\begin_inset LatexCommand \label{sub:Decoding-BER}
1209
1210\end_inset
1211
1212Decoding BER
Lev Walkin84291e02004-06-03 03:49:45 +00001213\layout Standard
1214
Lev Walkin09889622005-02-21 14:16:10 +00001215The Basic Encoding Rules describe the most widely used (by the ASN.1 community)
Lev Walkin7f70fe52005-02-22 07:28:26 +00001216 way to encode and decode a given structure in a machine-independent way.
Lev Walkin09889622005-02-21 14:16:10 +00001217 Several other encoding rules (CER, DER) define a more restrictive versions
1218 of BER, so the generic BER parser is also capable of decoding the data
1219 encoded by CER and DER encoders.
1220 The opposite is not true.
1221\layout Standard
1222
Lev Walkin7f70fe52005-02-22 07:28:26 +00001223
1224\emph on
Lev Walkin09889622005-02-21 14:16:10 +00001225The ASN.1 compiler provides the generic BER decoder which is implicitly capable
1226 of decoding BER, CER and DER encoded data.
1227\layout Standard
1228
1229The decoder is restartable (stream-oriented), which means that in case the
1230 buffer has less data than it is expected, the decoder will process whatever
1231 there is available and ask for more data to be provided.
1232 Please note that the decoder may actually process less data than it was
1233 given in the buffer, which means that you must be able to make the next
1234 buffer contain the unprocessed part of the previous buffer.
1235\layout Standard
1236
1237Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
1238\layout Itemize
1239
1240You may concatenate these buffers and feed the BER decoder with 300 bytes
1241 of data, or
1242\layout Itemize
1243
1244You may feed it the first buffer of 100 bytes of data, realize that the
1245 ber_decoder consumed only 95 bytes from it and later feed the decoder with
1246 205 bytes buffer which consists of 5 unprocessed bytes from the first buffer
Lev Walkin7f70fe52005-02-22 07:28:26 +00001247 and the additional 200 bytes from the second buffer.
Lev Walkin09889622005-02-21 14:16:10 +00001248\layout Standard
1249
Lev Walkin7f70fe52005-02-22 07:28:26 +00001250This is not as convenient as it could be (like, the BER encoder could consume
Lev Walkin09889622005-02-21 14:16:10 +00001251 the whole 100 bytes and keep these 5 bytes in some temporary storage),
Lev Walkin7f70fe52005-02-22 07:28:26 +00001252 but in case of existing stream based processing it might actually fit well
1253 into existing algorithm.
Lev Walkin09889622005-02-21 14:16:10 +00001254 Suggestions are welcome.
1255\layout Standard
1256
Lev Walkin7f70fe52005-02-22 07:28:26 +00001257Here is the simplest example of BER decoding.
Lev Walkin84291e02004-06-03 03:49:45 +00001258\layout LyX-Code
1259
1260Rectangle_t *
1261\layout LyX-Code
1262
Lev Walkin26587ab2004-08-23 15:12:04 +00001263simple_deserializer(const void *buffer, size_t buf_size) {
Lev Walkin84291e02004-06-03 03:49:45 +00001264\layout LyX-Code
1265
1266 Rectangle_t *rect = 0; /* Note this 0! */
1267\layout LyX-Code
1268
Lev Walkina646ccb2004-10-20 15:48:55 +00001269 asn_dec_rval_t rval;
Lev Walkin84291e02004-06-03 03:49:45 +00001270\layout LyX-Code
1271
1272
1273\layout LyX-Code
1274
Lev Walkin09889622005-02-21 14:16:10 +00001275 rval =
1276\series bold
1277asn_DEF_Rectangle->ber_decoder
1278\series default
1279(0,
Lev Walkin84291e02004-06-03 03:49:45 +00001280\layout LyX-Code
1281
Lev Walkin1f756282004-09-29 13:29:17 +00001282 &asn_DEF_Rectangle,
Lev Walkin84291e02004-06-03 03:49:45 +00001283\layout LyX-Code
1284
1285 (void **)&rect,
1286\layout LyX-Code
1287
1288 buffer, buf_size,
1289\layout LyX-Code
1290
1291 0);
1292\layout LyX-Code
1293
1294
1295\layout LyX-Code
1296
1297 if(rval
1298\series bold
1299.code
1300\series default
1301 == RC_OK) {
1302\layout LyX-Code
1303
1304 return rect; /* Decoding succeeded */
1305\layout LyX-Code
1306
1307 } else {
1308\layout LyX-Code
1309
Lev Walkin26587ab2004-08-23 15:12:04 +00001310 /* Free partially decoded rect */
1311\layout LyX-Code
1312
Lev Walkin1f756282004-09-29 13:29:17 +00001313 asn_DEF_Rectangle->free_struct(
Lev Walkin84291e02004-06-03 03:49:45 +00001314\layout LyX-Code
1315
Lev Walkin1f756282004-09-29 13:29:17 +00001316 &asn_DEF_Rectangle, rect, 0);
Lev Walkin84291e02004-06-03 03:49:45 +00001317\layout LyX-Code
1318
1319 return 0;
1320\layout LyX-Code
1321
1322 }
1323\layout LyX-Code
1324
1325}
1326\layout Standard
1327
1328The code above defines a function,
1329\emph on
1330simple_deserializer
1331\emph default
Lev Walkin7f70fe52005-02-22 07:28:26 +00001332, which takes a buffer and its length and is expected to return a pointer
1333 to the Rectangle_t structure.
Lev Walkin84291e02004-06-03 03:49:45 +00001334 Inside, it tries to convert the bytes passed into the target structure
Lev Walkin7f70fe52005-02-22 07:28:26 +00001335 (rect) using the BER decoder and returns the rect pointer afterwards.
Lev Walkin84291e02004-06-03 03:49:45 +00001336 If the structure cannot be deserialized, it frees the memory which might
1337 be left allocated by the unfinished
1338\emph on
1339ber_decoder
1340\emph default
Lev Walkin09889622005-02-21 14:16:10 +00001341 routine and returns 0 (no data).
Lev Walkin7f70fe52005-02-22 07:28:26 +00001342 (This
Lev Walkin84291e02004-06-03 03:49:45 +00001343\series bold
Lev Walkin09889622005-02-21 14:16:10 +00001344 freeing is necessary
Lev Walkin84291e02004-06-03 03:49:45 +00001345\series default
1346 because the ber_decoder is a restartable procedure, and may fail just because
Lev Walkin7f70fe52005-02-22 07:28:26 +00001347 there is more data needs to be provided before decoding could be finalized).
Lev Walkin84291e02004-06-03 03:49:45 +00001348 The code above obviously does not take into account the way the
1349\emph on
Lev Walkin7f70fe52005-02-22 07:28:26 +00001350ber_decoder()
Lev Walkin84291e02004-06-03 03:49:45 +00001351\emph default
1352 failed, so the freeing is necessary because the part of the buffer may
1353 already be decoded into the structure by the time something goes wrong.
1354\layout Standard
1355
Lev Walkin7f70fe52005-02-22 07:28:26 +00001356A little less wordy would be to invoke a globally available
Lev Walkin84291e02004-06-03 03:49:45 +00001357\emph on
Lev Walkin7f70fe52005-02-22 07:28:26 +00001358ber_decode()
Lev Walkin84291e02004-06-03 03:49:45 +00001359\emph default
Lev Walkin7f70fe52005-02-22 07:28:26 +00001360 function instead of dereferencing the asn_DEF_Rectangle type descriptor:
Lev Walkin84291e02004-06-03 03:49:45 +00001361\layout LyX-Code
1362
Lev Walkin1f756282004-09-29 13:29:17 +00001363rval = ber_decode(0, &asn_DEF_Rectangle, (void **)&rect,
Lev Walkin84291e02004-06-03 03:49:45 +00001364\layout LyX-Code
1365
1366 buffer, buf_size);
1367\layout Standard
1368
Lev Walkin1f756282004-09-29 13:29:17 +00001369Note that the initial (asn_DEF_Rectangle->ber_decoder) reference is gone,
Lev Walkin84291e02004-06-03 03:49:45 +00001370 and also the last argument (0) is no longer necessary.
1371\layout Standard
1372
Lev Walkin09889622005-02-21 14:16:10 +00001373These two ways of BER decoder invocations are fully equivalent.
Lev Walkin84291e02004-06-03 03:49:45 +00001374\layout Standard
1375
1376The BER de
1377\emph on
1378coder
1379\emph default
Lev Walkin1f756282004-09-29 13:29:17 +00001380 may fail because of (
Lev Walkin84291e02004-06-03 03:49:45 +00001381\emph on
1382the following RC_\SpecialChar \ldots{}
1383 codes are defined in ber_decoder.h
1384\emph default
1385):
1386\layout Itemize
1387
1388RC_WMORE: There is more data expected than it is provided (stream mode continuat
1389ion feature);
1390\layout Itemize
1391
1392RC_FAIL: General failure to decode the buffer;
1393\layout Itemize
1394
1395\SpecialChar \ldots{}
1396 other codes may be defined as well.
1397\layout Standard
1398
Lev Walkina646ccb2004-10-20 15:48:55 +00001399Together with the return code (.code) the asn_dec_rval_t type contains the
Lev Walkin84291e02004-06-03 03:49:45 +00001400 number of bytes which is consumed from the buffer.
1401 In the previous hypothetical example of two buffers (of 100 and 200 bytes),
1402 the first call to ber_decode() would return with .code = RC_WMORE and .consumed
1403 = 95.
1404 The .consumed field of the BER decoder return value is
1405\series bold
1406always
1407\series default
1408 valid, even if the decoder succeeds or fails with any other return code.
1409\layout Standard
1410
1411Please look into ber_decoder.h for the precise definition of ber_decode()
1412 and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00001413\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00001414
1415
1416\begin_inset LatexCommand \label{sub:Encoding-DER}
1417
1418\end_inset
1419
1420Encoding DER
1421\layout Standard
1422
Lev Walkin35d446b2004-09-24 20:56:07 +00001423The Distinguished Encoding Rules is the
1424\emph on
1425canonical
1426\emph default
1427 variant of BER encoding rules.
1428 The DER is best suited to encode the structures where all the lengths are
1429 known beforehand.
Lev Walkin84291e02004-06-03 03:49:45 +00001430 This is probably exactly how you want to encode: either after a BER decoding
1431 or after a manual fill-up, the target structure contains the data which
1432 size is implicitly known before encoding.
Lev Walkin09889622005-02-21 14:16:10 +00001433 Among other uses, the DER encoding is used to encode X.509 certificates.
Lev Walkin84291e02004-06-03 03:49:45 +00001434\layout Standard
1435
1436As with BER decoder, the DER encoder may be invoked either directly from
Lev Walkin1f756282004-09-29 13:29:17 +00001437 the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone function,
Lev Walkin84291e02004-06-03 03:49:45 +00001438 which is somewhat simpler:
1439\layout LyX-Code
1440
Lev Walkin84291e02004-06-03 03:49:45 +00001441
1442\layout LyX-Code
1443
1444/*
1445\layout LyX-Code
1446
1447 * This is the serializer itself,
1448\layout LyX-Code
1449
1450 * it supplies the DER encoder with the
1451\layout LyX-Code
1452
1453 * pointer to the custom output function.
1454\layout LyX-Code
1455
1456 */
1457\layout LyX-Code
1458
1459ssize_t
1460\layout LyX-Code
1461
1462simple_serializer(FILE *ostream, Rectangle_t *rect) {
1463\layout LyX-Code
1464
Lev Walkin35d446b2004-09-24 20:56:07 +00001465 asn_enc_rval_t er; /* Encoder return value */
Lev Walkin84291e02004-06-03 03:49:45 +00001466\layout LyX-Code
1467
1468
1469\layout LyX-Code
1470
Lev Walkin1f756282004-09-29 13:29:17 +00001471 er = der_encode(&asn_DEF_Rect, rect,
Lev Walkin84291e02004-06-03 03:49:45 +00001472\layout LyX-Code
1473
Lev Walkin26587ab2004-08-23 15:12:04 +00001474 write_stream, ostream);
Lev Walkin84291e02004-06-03 03:49:45 +00001475\layout LyX-Code
1476
Lev Walkin35d446b2004-09-24 20:56:07 +00001477 if(er.
Lev Walkin84291e02004-06-03 03:49:45 +00001478\series bold
Lev Walkin35d446b2004-09-24 20:56:07 +00001479encoded
Lev Walkin84291e02004-06-03 03:49:45 +00001480\series default
1481 == -1) {
1482\layout LyX-Code
1483
1484 /*
1485\layout LyX-Code
1486
Lev Walkin35d446b2004-09-24 20:56:07 +00001487 * Failed to encode the rectangle data.
Lev Walkin84291e02004-06-03 03:49:45 +00001488\layout LyX-Code
1489
1490 */
1491\layout LyX-Code
1492
1493 fprintf(stderr,
1494\begin_inset Quotes sld
1495\end_inset
1496
1497Cannot encode %s: %s
1498\backslash
1499n
1500\begin_inset Quotes srd
1501\end_inset
1502
1503,
1504\layout LyX-Code
1505
Lev Walkin35d446b2004-09-24 20:56:07 +00001506 er.
Lev Walkin84291e02004-06-03 03:49:45 +00001507\series bold
Lev Walkin35d446b2004-09-24 20:56:07 +00001508failed_type
Lev Walkin84291e02004-06-03 03:49:45 +00001509\series default
1510->name,
1511\layout LyX-Code
1512
1513 strerror(errno));
1514\layout LyX-Code
1515
1516 return -1;
1517\layout LyX-Code
1518
1519 } else {
1520\layout LyX-Code
1521
1522 /* Return the number of bytes */
1523\layout LyX-Code
1524
Lev Walkin35d446b2004-09-24 20:56:07 +00001525 return er.encoded;
Lev Walkin84291e02004-06-03 03:49:45 +00001526\layout LyX-Code
1527
1528 }
1529\layout LyX-Code
1530
1531}
1532\layout Standard
1533
1534As you see, the DER encoder does not write into some sort of buffer or something.
1535 It just invokes the custom function (possible, multiple times) which would
1536 save the data into appropriate storage.
1537 The optional argument
1538\emph on
1539app_key
1540\emph default
1541 is opaque for the DER encoder code and just used by
1542\emph on
1543_write_stream()
1544\emph default
1545 as the pointer to the appropriate output stream to be used.
1546\layout Standard
1547
1548If the custom write function is not given (passed as 0), then the DER encoder
1549 will essentially do the same thing (i.e., encode the data) but no callbacks
1550 will be invoked (so the data goes nowhere).
1551 It may prove useful to determine the size of the structure's encoding before
1552 actually doing the encoding
1553\begin_inset Foot
1554collapsed false
1555
1556\layout Standard
1557
1558It is actually faster too: the encoder might skip over some computations
1559 which aren't important for the size determination.
1560\end_inset
1561
1562.
1563\layout Standard
1564
1565Please look into der_encoder.h for the precise definition of der_encode()
1566 and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00001567\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00001568
1569
Lev Walkin35d446b2004-09-24 20:56:07 +00001570\begin_inset LatexCommand \label{sub:Encoding-XER}
1571
1572\end_inset
1573
1574Encoding XER
1575\layout Standard
1576
1577The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
1578 Markup Language, a text-based format for information exchange.
1579 The encoder routine API comes in two flavors: stdio-based and callback-based.
1580 With the callback-based encoder, the encoding process is very similar to
1581 the DER one, described in Section
1582\begin_inset LatexCommand \vref{sub:Encoding-DER}
1583
1584\end_inset
1585
1586.
1587 The following example uses the definition of write_stream() from up there.
1588\layout LyX-Code
1589
1590/*
1591\layout LyX-Code
1592
1593 * This procedure generates the XML document
1594\layout LyX-Code
1595
1596 * by invoking the XER encoder.
1597\layout LyX-Code
1598
1599 * NOTE: Do not copy this code verbatim!
1600\layout LyX-Code
1601
1602 * If the stdio output is necessary,
1603\layout LyX-Code
1604
1605 * use the xer_fprint() procedure instead.
1606\layout LyX-Code
1607
1608 * See Section
1609\begin_inset LatexCommand \vref{sub:Printing-the-target}
1610
1611\end_inset
1612
1613.
1614\layout LyX-Code
1615
1616 */
1617\layout LyX-Code
1618
1619int
1620\layout LyX-Code
1621
1622print_as_XML(FILE *ostream, Rectangle_t *rect) {
1623\layout LyX-Code
1624
1625 asn_enc_rval_t er; /* Encoder return value */
1626\layout LyX-Code
1627
1628
1629\layout LyX-Code
1630
Lev Walkin09889622005-02-21 14:16:10 +00001631 er = xer_encode(&asn_DEF_Rectangle, rect,
Lev Walkin35d446b2004-09-24 20:56:07 +00001632\layout LyX-Code
1633
1634 XER_F_BASIC, /* BASIC-XER or CANONICAL-XER */
1635\layout LyX-Code
1636
1637 write_stream, ostream);
1638\layout LyX-Code
1639
1640
1641\layout LyX-Code
1642
1643 return (er.encoded == -1) ? -1 : 0;
1644\layout LyX-Code
1645
1646}
1647\layout Standard
1648
1649Please look into xer_encoder.h for the precise definition of xer_encode()
1650 and related types.
1651\layout Standard
1652
1653See Section
1654\begin_inset LatexCommand \ref{sub:Printing-the-target}
1655
1656\end_inset
1657
1658 for the example of stdio-based XML encoder and other pretty-printing suggestion
1659s.
1660\layout Subsection
1661
1662
Lev Walkin09889622005-02-21 14:16:10 +00001663\begin_inset LatexCommand \label{sub:Decoding-XER}
1664
1665\end_inset
1666
1667Decoding XER
1668\layout Standard
1669
1670The data encoded using the XER rules can be subsequently decoded using the
1671 xer_decode() API call:
1672\layout LyX-Code
1673
1674Rectangle_t *
1675\layout LyX-Code
1676
1677XML_to_Rectangle(const void *buffer, size_t buf_size) {
1678\layout LyX-Code
1679
1680 Rectangle_t *rect = 0; /* Note this 0! */
1681\layout LyX-Code
1682
1683 asn_dec_rval_t rval;
1684\layout LyX-Code
1685
1686
1687\layout LyX-Code
1688
1689 rval = xer_decode(0, &asn_DEF_Rectangle, (void **)&rect,
1690\layout LyX-Code
1691
1692 buffer, buf_size);
1693\layout LyX-Code
1694
1695 if(rval
1696\series bold
1697.code
1698\series default
1699 == RC_OK) {
1700\layout LyX-Code
1701
1702 return rect; /* Decoding succeeded */
1703\layout LyX-Code
1704
1705 } else {
1706\layout LyX-Code
1707
1708 /* Free partially decoded rect */
1709\layout LyX-Code
1710
1711 asn_DEF_Rectangle->free_struct(
1712\layout LyX-Code
1713
1714 &asn_DEF_Rectangle, rect, 0);
1715\layout LyX-Code
1716
1717 return 0;
1718\layout LyX-Code
1719
1720 }
1721\layout LyX-Code
1722
1723}
1724\layout Standard
1725
1726The decoder takes both BASIC-XER and CANONICAL-XER encodings.
1727\layout Standard
1728
Lev Walkin7f70fe52005-02-22 07:28:26 +00001729The decoder shares its data consumption properties with BER decoder; please
1730 read the Section
1731\begin_inset LatexCommand \vref{sub:Decoding-BER}
1732
1733\end_inset
1734
1735 to know more.
1736\layout Standard
1737
Lev Walkin09889622005-02-21 14:16:10 +00001738Please look into xer_decoder.h for the precise definition of xer_decode()
1739 and related types.
1740\layout Subsection
1741
1742
Lev Walkin84291e02004-06-03 03:49:45 +00001743\begin_inset LatexCommand \label{sub:Validating-the-target}
1744
1745\end_inset
1746
1747Validating the target structure
1748\layout Standard
1749
1750Sometimes the target structure needs to be validated.
1751 For example, if the structure was created by the application (as opposed
1752 to being decoded from some external source), some important information
1753 required by the ASN.1 specification might be missing.
1754 On the other hand, the successful decoding of the data from some external
1755 source does not necessarily mean that the data is fully valid either.
1756 It might well be the case that the specification describes some subtype
1757 constraints that were not taken into account during decoding, and it would
1758 actually be useful to perform the last check when the data is ready to
1759 be encoded or when the data has just been decoded to ensure its validity
1760 according to some stricter rules.
1761\layout Standard
1762
1763The asn_check_constraints() function checks the type for various implicit
1764 and explicit constraints.
1765 It is recommended to use asn_check_constraints() function after each decoding
1766 and before each encoding.
1767\layout Standard
1768
1769Please look into constraints.h for the precise definition of asn_check_constraint
1770s() and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00001771\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00001772
1773
1774\begin_inset LatexCommand \label{sub:Printing-the-target}
1775
1776\end_inset
1777
1778Printing the target structure
1779\layout Standard
1780
1781There are two ways to print the target structure: either invoke the print_struct
1782 member of the ASN.1 type descriptor, or using the asn_fprint() function,
1783 which is a simpler wrapper of the former:
1784\layout LyX-Code
1785
Lev Walkin1f756282004-09-29 13:29:17 +00001786asn_fprint(stdout, &asn_DEF_Rectangle, rect);
Lev Walkin84291e02004-06-03 03:49:45 +00001787\layout Standard
1788
1789Please look into constr_TYPE.h for the precise definition of asn_fprint()
1790 and related types.
Lev Walkin35d446b2004-09-24 20:56:07 +00001791\layout Standard
1792
1793Another practical alternative to this custom format printing would be to
1794 invoke XER encoder.
1795 The default BASIC-XER encoder performs reasonable formatting for the output
1796 to be useful and human readable.
1797 To invoke the XER decoder in a manner similar to asn_fprint(), use the
1798 xer_fprint() call:
1799\layout LyX-Code
1800
Lev Walkin1f756282004-09-29 13:29:17 +00001801xer_fprint(stdout, &asn_DEF_Rectangle, rect);
Lev Walkin35d446b2004-09-24 20:56:07 +00001802\layout Standard
1803
1804See Section
1805\begin_inset LatexCommand \vref{sub:Encoding-XER}
1806
1807\end_inset
1808
1809 for XML-related details.
Lev Walkin26587ab2004-08-23 15:12:04 +00001810\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00001811
1812
1813\begin_inset LatexCommand \label{sub:Freeing-the-target}
1814
1815\end_inset
1816
1817Freeing the target structure
1818\layout Standard
1819
1820Freeing the structure is slightly more complex than it may seem to.
1821 When the ASN.1 structure is freed, all the members of the structure and
1822 their submembers etc etc are recursively freed too.
1823 But it might not be feasible to free the structure itself.
1824 Consider the following case:
1825\layout LyX-Code
1826
1827struct my_figure { /* The custom structure */
1828\layout LyX-Code
1829
1830 int flags; /* <some custom member> */
1831\layout LyX-Code
1832
1833 /* The type is generated by the ASN.1 compiler */
1834\layout LyX-Code
1835
1836
1837\emph on
1838Rectangle_t rect;
1839\layout LyX-Code
1840
1841 /* other members of the structure */
1842\layout LyX-Code
1843
1844};
1845\layout Standard
1846
1847In this example, the application programmer defined a custom structure with
1848 one ASN.1-derived member (rect).
1849 This member is not a reference to the Rectangle_t, but an in-place inclusion
1850 of the Rectangle_t structure.
1851 If the freeing is necessary, the usual procedure of freeing everything
1852 must not be applied to the &rect pointer itself, because it does not point
Lev Walkin7f70fe52005-02-22 07:28:26 +00001853 to the memory block directly allocated by the memory allocation routine,
1854 but instead lies within a block allocated for the my_figure structure.
Lev Walkin84291e02004-06-03 03:49:45 +00001855\layout Standard
1856
1857To solve this problem, the free_struct routine has the additional argument
Lev Walkin7f70fe52005-02-22 07:28:26 +00001858 (besides the obvious type descriptor and target structure pointers), which
1859 is the flag specifying whether the outer pointer itself must be freed (0,
1860 default) or it should be left intact (non-zero value).
Lev Walkin84291e02004-06-03 03:49:45 +00001861\layout LyX-Code
1862
Lev Walkin09889622005-02-21 14:16:10 +00001863
1864\series bold
1865/* 1.
1866 Rectangle_t is defined within my_figure */
Lev Walkin84291e02004-06-03 03:49:45 +00001867\layout LyX-Code
1868
Lev Walkin09889622005-02-21 14:16:10 +00001869struct my_figure {
1870\layout LyX-Code
1871
1872 Rectangle_t rect;
1873\layout LyX-Code
1874
1875} *mf =
Lev Walkin84291e02004-06-03 03:49:45 +00001876\series bold
1877...
1878\series default
1879;
1880\layout LyX-Code
1881
1882/*
1883\layout LyX-Code
1884
Lev Walkin09889622005-02-21 14:16:10 +00001885 * Freeing the Rectangle_t
Lev Walkin84291e02004-06-03 03:49:45 +00001886\layout LyX-Code
1887
Lev Walkin09889622005-02-21 14:16:10 +00001888 * without freeing the mf->rect area
Lev Walkin84291e02004-06-03 03:49:45 +00001889\layout LyX-Code
1890
1891 */
1892\layout LyX-Code
1893
Lev Walkin1f756282004-09-29 13:29:17 +00001894asn_DEF_Rectangle->free_struct(
Lev Walkin84291e02004-06-03 03:49:45 +00001895\layout LyX-Code
1896
Lev Walkin1f756282004-09-29 13:29:17 +00001897 &asn_DEF_Rectangle, &mf->rect,
Lev Walkin09889622005-02-21 14:16:10 +00001898\series bold
Lev Walkin84291e02004-06-03 03:49:45 +000018991
Lev Walkin09889622005-02-21 14:16:10 +00001900\emph on
Lev Walkin84291e02004-06-03 03:49:45 +00001901
Lev Walkin09889622005-02-21 14:16:10 +00001902\emph default
1903/* !free */
1904\series default
1905);
Lev Walkin84291e02004-06-03 03:49:45 +00001906\layout LyX-Code
1907
Lev Walkin09889622005-02-21 14:16:10 +00001908
1909\layout LyX-Code
1910
1911
1912\layout LyX-Code
1913
1914
1915\series bold
1916/* 2.
1917 Rectangle_t is a stand-alone pointer */
Lev Walkin84291e02004-06-03 03:49:45 +00001918\layout LyX-Code
1919
1920Rectangle_t *rect =
1921\series bold
1922...
1923\series default
1924;
1925\layout LyX-Code
1926
1927/*
1928\layout LyX-Code
1929
1930 * Freeing the Rectangle_t
1931\layout LyX-Code
1932
1933 * and freeing the rect pointer
1934\layout LyX-Code
1935
1936 */
1937\layout LyX-Code
1938
Lev Walkin1f756282004-09-29 13:29:17 +00001939asn_DEF_Rectangle->free_struct(
Lev Walkin84291e02004-06-03 03:49:45 +00001940\layout LyX-Code
1941
Lev Walkin1f756282004-09-29 13:29:17 +00001942 &asn_DEF_Rectangle, rect,
Lev Walkin09889622005-02-21 14:16:10 +00001943\series bold
Lev Walkin84291e02004-06-03 03:49:45 +000019440
Lev Walkin09889622005-02-21 14:16:10 +00001945\emph on
1946
Lev Walkin84291e02004-06-03 03:49:45 +00001947\emph default
Lev Walkin09889622005-02-21 14:16:10 +00001948/* free the pointer too */
1949\series default
1950);
Lev Walkin84291e02004-06-03 03:49:45 +00001951\layout Standard
1952
1953It is safe to invoke the
1954\emph on
1955free_struct
1956\emph default
1957 function with the target structure pointer set to 0 (NULL), the function
1958 will do nothing.
Lev Walkinfecbd1e2005-01-31 10:10:04 +00001959\layout Chapter
Lev Walkindba8ca02005-01-14 11:38:49 +00001960
Lev Walkindee61782005-02-02 09:53:46 +00001961
1962\begin_inset LatexCommand \label{cha:Step-by-step-examples}
1963
1964\end_inset
1965
Lev Walkinfecbd1e2005-01-31 10:10:04 +00001966Step by step examples
1967\layout Section
1968
1969A
1970\begin_inset Quotes sld
1971\end_inset
1972
1973Rectangle
1974\begin_inset Quotes srd
1975\end_inset
1976
1977 Encoder
1978\layout Standard
1979
Lev Walkindee61782005-02-02 09:53:46 +00001980This example will help you to create a simple BER and XER encoder of a
Lev Walkinfecbd1e2005-01-31 10:10:04 +00001981\begin_inset Quotes sld
1982\end_inset
1983
1984Rectangle
1985\begin_inset Quotes srd
1986\end_inset
1987
1988 type used throughout this document.
1989\layout Enumerate
1990
1991Create a file named
1992\series bold
1993rectangle.asn1
1994\series default
1995 with the following contents:
1996\begin_deeper
1997\layout LyX-Code
1998
1999RectangleModule1 DEFINITIONS ::=
2000\layout LyX-Code
2001
2002BEGIN
2003\layout LyX-Code
2004
2005
2006\layout LyX-Code
2007
2008Rectangle ::= SEQUENCE {
2009\layout LyX-Code
2010
2011 height INTEGER,
2012\layout LyX-Code
2013
2014 width INTEGER
2015\layout LyX-Code
2016
2017}
2018\layout LyX-Code
2019
2020
2021\layout LyX-Code
2022
2023END
2024\end_deeper
2025\layout Enumerate
2026
2027Compile it into the set of .c and .h files using asn1c compiler
2028\begin_inset LatexCommand \cite{ASN1C}
2029
2030\end_inset
2031
2032:
2033\begin_deeper
2034\layout LyX-Code
2035
2036
2037\emph on
2038asn1c -fnative-types
2039\series bold
2040\emph default
2041rectangle.asn1
2042\end_deeper
2043\layout Enumerate
2044
2045Alternatively, use the Online ASN.1 compiler
2046\begin_inset LatexCommand \cite{AONL}
2047
2048\end_inset
2049
2050 by uploading the
2051\series bold
2052rectangle.asn1
2053\series default
2054 file into the Web form and unpacking the produced archive on your computer.
2055\layout Enumerate
2056
2057By this time, you should have gotten multiple files in the current directory,
2058 including the
2059\series bold
2060Rectangle.c
2061\series default
2062 and
2063\series bold
2064Rectangle.h
2065\series default
2066.
2067\layout Enumerate
2068
2069Create a main() routine which creates the Rectangle_t structure in memory
2070 and encodes it using BER and XER encoding rules.
2071 Let's name the file
2072\series bold
2073main.c
2074\series default
2075:
2076\begin_inset ERT
2077status Open
2078
2079\layout Standard
2080
2081\backslash
2082clearpage{}
2083\end_inset
2084
2085
2086\begin_deeper
2087\layout LyX-Code
2088
2089
2090\size small
2091#include <stdio.h>
2092\layout LyX-Code
2093
2094
2095\size small
2096#include <sys/types.h>
2097\layout LyX-Code
2098
2099
2100\size small
2101#include <Rectangle.h> /* Rectangle ASN.1 type */
2102\layout LyX-Code
2103
2104
2105\layout LyX-Code
2106
2107
2108\size small
2109/*
2110\layout LyX-Code
2111
2112
2113\size small
2114 * This is a custom function which writes the
2115\layout LyX-Code
2116
2117
2118\size small
2119 * encoded output into some FILE stream.
2120\layout LyX-Code
2121
2122
2123\size small
2124 */
2125\layout LyX-Code
2126
2127
2128\size small
2129static int
2130\layout LyX-Code
2131
2132
2133\size small
2134write_out(const void *buffer, size_t size, void *app_key) {
2135\layout LyX-Code
2136
2137
2138\size small
2139 FILE *out_fp = app_key;
2140\layout LyX-Code
2141
2142
2143\size small
2144 size_t wrote;
2145\layout LyX-Code
2146
2147
2148\size small
2149
2150\layout LyX-Code
2151
2152
2153\size small
2154 wrote = fwrite(buffer, 1, size, out_fp);
2155\layout LyX-Code
2156
2157
2158\size small
2159
2160\layout LyX-Code
2161
2162
2163\size small
2164 return (wrote == size) ? 0 : -1;
2165\layout LyX-Code
2166
2167
2168\size small
2169}
2170\layout LyX-Code
2171
2172
2173\layout LyX-Code
2174
2175
2176\size small
Lev Walkin6b0df9f2005-01-31 15:06:32 +00002177int main(int ac, char **av) {
Lev Walkinfecbd1e2005-01-31 10:10:04 +00002178\layout LyX-Code
2179
2180
2181\size small
2182 Rectangle_t *rectangle; /* Type to encode */
2183\layout LyX-Code
2184
2185
2186\size small
2187 asn_enc_rval_t ec; /* Encoder return value */
2188\layout LyX-Code
2189
2190
2191\size small
2192
2193\layout LyX-Code
2194
2195
2196\size small
2197 /* Allocate the Rectangle_t */
2198\layout LyX-Code
2199
2200
2201\size small
Lev Walkindee61782005-02-02 09:53:46 +00002202 rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */
Lev Walkinfecbd1e2005-01-31 10:10:04 +00002203\layout LyX-Code
2204
2205
2206\size small
2207 if(!rectangle) {
2208\layout LyX-Code
2209
2210
2211\size small
2212 perror(
2213\begin_inset Quotes sld
2214\end_inset
2215
2216calloc() failed
2217\begin_inset Quotes srd
2218\end_inset
2219
2220);
2221\layout LyX-Code
2222
2223
2224\size small
2225 exit(71); /* better, EX_OSERR */
2226\layout LyX-Code
2227
2228
2229\size small
2230 }
2231\layout LyX-Code
2232
2233
2234\size small
2235
2236\layout LyX-Code
2237
2238
2239\size small
2240 /* Initialize the Rectangle members */
2241\layout LyX-Code
2242
2243
2244\size small
2245 rectangle->height = 42; /* any random value */
2246\layout LyX-Code
2247
2248
2249\size small
2250 rectangle->width = 23; /* any random value */
2251\layout LyX-Code
2252
2253
2254\size small
2255
2256\layout LyX-Code
2257
2258
2259\size small
2260 /* BER encode the data if filename is given */
2261\layout LyX-Code
2262
2263
2264\size small
2265 if(ac < 2) {
2266\layout LyX-Code
2267
2268
2269\size small
2270 fprintf(stderr,
2271\begin_inset Quotes sld
2272\end_inset
2273
2274Specify filename for BER output
2275\backslash
2276n
2277\begin_inset Quotes srd
2278\end_inset
2279
2280);
2281\layout LyX-Code
2282
2283
2284\size small
2285 } else {
2286\layout LyX-Code
2287
2288
2289\size small
2290 const char *filename = av[1];
2291\layout LyX-Code
2292
2293
2294\size small
2295 FILE *fp = fopen(filename,
2296\begin_inset Quotes sld
2297\end_inset
2298
2299w
2300\begin_inset Quotes srd
2301\end_inset
2302
2303); /* for BER output */
2304\layout LyX-Code
2305
2306
2307\layout LyX-Code
2308
2309
2310\size small
2311 if(!fp) {
2312\layout LyX-Code
2313
2314
2315\size small
2316 perror(filename);
2317\layout LyX-Code
2318
2319
2320\size small
2321 exit(71); /* better, EX_OSERR */
2322\layout LyX-Code
2323
2324
2325\size small
2326 }
2327\layout LyX-Code
2328
2329
2330\size small
2331
2332\layout LyX-Code
2333
2334
2335\size small
2336 /* Encode the Rectangle type as BER (DER) */
2337\layout LyX-Code
2338
2339
2340\size small
2341 ec = der_encode(&asn_DEF_Rectangle,
2342\layout LyX-Code
2343
2344
2345\size small
Lev Walkin6b0df9f2005-01-31 15:06:32 +00002346 rectangle, write_out, fp);
Lev Walkinfecbd1e2005-01-31 10:10:04 +00002347\layout LyX-Code
2348
2349
2350\size small
2351 fclose(fp);
2352\layout LyX-Code
2353
2354
2355\size small
2356 if(ec.encoded == -1) {
2357\layout LyX-Code
2358
2359
2360\size small
2361 fprintf(stderr,
2362\layout LyX-Code
2363
2364
2365\size small
2366
2367\begin_inset Quotes sld
2368\end_inset
2369
2370Could not encode Rectangle (at %s)
2371\backslash
2372n
2373\begin_inset Quotes srd
2374\end_inset
2375
2376,
2377\layout LyX-Code
2378
2379
2380\size small
2381 ec.failed_type ? ec.failed_type->name :
2382\begin_inset Quotes sld
2383\end_inset
2384
2385unknown
2386\begin_inset Quotes srd
2387\end_inset
2388
2389);
2390\layout LyX-Code
2391
2392
2393\size small
2394 exit(65); /* better, EX_DATAERR */
2395\layout LyX-Code
2396
2397
2398\size small
2399 } else {
2400\layout LyX-Code
2401
2402
2403\size small
2404 fprintf(stderr,
2405\begin_inset Quotes sld
2406\end_inset
2407
2408Created %s with BER encoded Rectangle
2409\backslash
2410n
2411\begin_inset Quotes srd
2412\end_inset
2413
2414,
2415\layout LyX-Code
2416
2417
2418\size small
2419 filename);
2420\layout LyX-Code
2421
2422
2423\size small
2424 }
2425\layout LyX-Code
2426
2427
2428\size small
2429 }
2430\layout LyX-Code
2431
2432
2433\size small
2434
2435\layout LyX-Code
2436
2437
2438\size small
2439 /* Also print the constructed Rectangle XER encoded (XML) */
2440\layout LyX-Code
2441
2442
2443\size small
2444 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
2445\layout LyX-Code
2446
2447
2448\size small
2449
2450\layout LyX-Code
2451
2452
2453\size small
2454 return 0; /* Encoding finished successfully */
2455\layout LyX-Code
2456
2457
2458\size small
2459}
2460\end_deeper
2461\layout Enumerate
2462
2463Compile all files together using C compiler (varies by platform):
2464\begin_deeper
2465\layout LyX-Code
2466
2467
2468\emph on
2469cc -I.
2470 -o
2471\series bold
2472rencode
2473\series default
2474 *.c
2475\end_deeper
2476\layout Enumerate
2477
2478Voila! You have just created the BER and XER encoder of a Rectangle type,
2479 named
2480\series bold
2481rencode
2482\series default
2483!
2484\layout Standard
2485
2486
2487\begin_inset ERT
2488status Collapsed
2489
2490\layout Standard
2491
2492\backslash
2493clearpage{}
2494\end_inset
2495
2496
2497\layout Section
2498
Lev Walkindee61782005-02-02 09:53:46 +00002499
2500\begin_inset LatexCommand \label{sec:A-Rectangle-Decoder}
2501
2502\end_inset
2503
Lev Walkinfecbd1e2005-01-31 10:10:04 +00002504A
Lev Walkindba8ca02005-01-14 11:38:49 +00002505\begin_inset Quotes sld
2506\end_inset
2507
2508Rectangle
2509\begin_inset Quotes srd
2510\end_inset
2511
2512 Decoder
2513\layout Standard
2514
Lev Walkindee61782005-02-02 09:53:46 +00002515This example will help you to create a simple BER decoder of a simple
Lev Walkindba8ca02005-01-14 11:38:49 +00002516\begin_inset Quotes sld
2517\end_inset
2518
2519Rectangle
2520\begin_inset Quotes srd
2521\end_inset
2522
2523 type used throughout this document.
2524\layout Enumerate
2525
2526Create a file named
2527\series bold
2528rectangle.asn1
2529\series default
2530 with the following contents:
2531\begin_deeper
2532\layout LyX-Code
2533
2534RectangleModule1 DEFINITIONS ::=
2535\layout LyX-Code
2536
2537BEGIN
2538\layout LyX-Code
2539
2540
2541\layout LyX-Code
2542
2543Rectangle ::= SEQUENCE {
2544\layout LyX-Code
2545
2546 height INTEGER,
2547\layout LyX-Code
2548
2549 width INTEGER
2550\layout LyX-Code
2551
2552}
2553\layout LyX-Code
2554
2555
2556\layout LyX-Code
2557
2558END
2559\end_deeper
2560\layout Enumerate
2561
2562Compile it into the set of .c and .h files using asn1c compiler
2563\begin_inset LatexCommand \cite{ASN1C}
2564
2565\end_inset
2566
2567:
2568\begin_deeper
2569\layout LyX-Code
2570
2571
2572\emph on
2573asn1c -fnative-types
2574\series bold
2575\emph default
2576rectangle.asn1
2577\end_deeper
2578\layout Enumerate
2579
2580Alternatively, use the Online ASN.1 compiler
2581\begin_inset LatexCommand \cite{AONL}
2582
2583\end_inset
2584
2585 by uploading the
2586\series bold
2587rectangle.asn1
2588\series default
Lev Walkinfc67f3e2005-01-17 11:05:04 +00002589 file into the Web form and unpacking the produced archive on your computer.
Lev Walkindba8ca02005-01-14 11:38:49 +00002590\layout Enumerate
2591
Lev Walkinfc67f3e2005-01-17 11:05:04 +00002592By this time, you should have gotten multiple files in the current directory,
2593 including the
2594\series bold
2595Rectangle.c
2596\series default
2597 and
2598\series bold
2599Rectangle.h
2600\series default
2601.
Lev Walkindba8ca02005-01-14 11:38:49 +00002602\layout Enumerate
2603
2604Create a main() routine which takes the binary input file, decodes it as
Lev Walkinfc67f3e2005-01-17 11:05:04 +00002605 it were a BER-encoded Rectangle type, and prints out the text (XML) representat
2606ion of the Rectangle type.
Lev Walkindba8ca02005-01-14 11:38:49 +00002607 Let's name the file
2608\series bold
2609main.c
2610\series default
2611:
Lev Walkinfc67f3e2005-01-17 11:05:04 +00002612\begin_inset ERT
Lev Walkindee61782005-02-02 09:53:46 +00002613status Collapsed
Lev Walkinfc67f3e2005-01-17 11:05:04 +00002614
2615\layout Standard
2616
2617\backslash
2618clearpage{}
2619\end_inset
2620
2621
Lev Walkindba8ca02005-01-14 11:38:49 +00002622\begin_deeper
2623\layout LyX-Code
2624
2625
2626\size small
2627#include <stdio.h>
2628\layout LyX-Code
2629
2630
2631\size small
2632#include <sys/types.h>
2633\layout LyX-Code
2634
2635
2636\size small
2637#include <Rectangle.h> /* Rectangle ASN.1 type */
2638\layout LyX-Code
2639
2640
2641\size small
2642
2643\layout LyX-Code
2644
2645
2646\size small
2647int main(int ac, char **av) {
2648\layout LyX-Code
2649
2650
2651\size small
2652 char buf[1024]; /* Temporary buffer */
2653\layout LyX-Code
2654
2655
2656\size small
2657 Rectangle_t *rectangle = 0; /* Type to decode */
2658\layout LyX-Code
2659
2660
2661\size small
2662 asn_dec_rval_t rval; /* Decoder return value */
2663\layout LyX-Code
2664
2665
2666\size small
2667 FILE *fp; /* Input file handler */
2668\layout LyX-Code
2669
2670
2671\size small
2672 size_t size; /* Number of bytes read */
2673\layout LyX-Code
2674
2675
2676\size small
2677 char *filename; /* Input file name */
2678\layout LyX-Code
2679
2680
2681\size small
2682
2683\layout LyX-Code
2684
2685
2686\size small
2687 /* Require a single filename argument */
2688\layout LyX-Code
2689
2690
2691\size small
2692 if(ac != 2) {
2693\layout LyX-Code
2694
2695
2696\size small
2697 fprintf(stderr,
2698\begin_inset Quotes sld
2699\end_inset
2700
2701Usage: %s <file.ber>
2702\backslash
2703n
2704\begin_inset Quotes srd
2705\end_inset
2706
2707, av[0]);
2708\layout LyX-Code
2709
2710
2711\size small
2712 exit(64); /* better, EX_USAGE */
2713\layout LyX-Code
2714
2715
2716\size small
2717 } else {
2718\layout LyX-Code
2719
2720
2721\size small
2722 filename = av[1];
2723\layout LyX-Code
2724
2725
2726\size small
2727 }
2728\layout LyX-Code
2729
2730
2731\size small
2732
2733\layout LyX-Code
2734
2735
2736\size small
2737 /* Open input file as read-only binary */
2738\layout LyX-Code
2739
2740
2741\size small
2742 fp = fopen(filename,
2743\begin_inset Quotes sld
2744\end_inset
2745
2746rb
2747\begin_inset Quotes srd
2748\end_inset
2749
2750);
2751\layout LyX-Code
2752
2753
2754\size small
2755 if(!fp) {
2756\layout LyX-Code
2757
2758
2759\size small
2760 perror(filename);
2761\layout LyX-Code
2762
2763
2764\size small
2765 exit(66); /* better, EX_NOINPUT */
2766\layout LyX-Code
2767
2768
2769\size small
2770 }
2771\layout LyX-Code
2772
2773
2774\size small
2775
2776\layout LyX-Code
2777
2778
2779\size small
2780 /* Read up to the buffer size */
2781\layout LyX-Code
2782
2783
2784\size small
2785 size = fread(buf, 1, sizeof(buf), fp);
2786\layout LyX-Code
2787
2788
2789\size small
2790 fclose(fp);
2791\layout LyX-Code
2792
2793
2794\size small
2795 if(!size) {
2796\layout LyX-Code
2797
2798
2799\size small
2800 fprintf(stderr,
2801\begin_inset Quotes sld
2802\end_inset
2803
2804%s: Empty or broken
2805\backslash
2806n
2807\begin_inset Quotes srd
2808\end_inset
2809
2810, filename);
2811\layout LyX-Code
2812
2813
2814\size small
2815 exit(65); /* better, EX_DATAERR */
2816\layout LyX-Code
2817
2818
2819\size small
2820 }
2821\layout LyX-Code
2822
2823
2824\size small
2825
2826\layout LyX-Code
2827
2828
2829\size small
2830 /* Decode the input buffer as Rectangle type */
2831\layout LyX-Code
2832
2833
2834\size small
2835 rval = ber_decode(0, &asn_DEF_Rectangle,
2836\layout LyX-Code
2837
2838
2839\size small
2840 (void **)&rectangle, buf, size);
2841\layout LyX-Code
2842
2843
2844\size small
2845 if(rval.code != RC_OK) {
2846\layout LyX-Code
2847
2848
2849\size small
2850 fprintf(stderr,
2851\layout LyX-Code
2852
2853
2854\size small
2855
2856\begin_inset Quotes sld
2857\end_inset
2858
2859%s: Broken Rectangle encoding at byte %ld
2860\backslash
2861n
2862\begin_inset Quotes srd
2863\end_inset
2864
2865,
2866\layout LyX-Code
2867
2868
2869\size small
2870 filename, (long)rval.consumed);
2871\layout LyX-Code
2872
2873
2874\size small
2875 exit(65); /* better, EX_DATAERR */
2876\layout LyX-Code
2877
2878
2879\size small
2880 }
2881\layout LyX-Code
2882
2883
2884\size small
2885
2886\layout LyX-Code
2887
2888
2889\size small
2890 /* Print the decoded Rectangle type as XML */
2891\layout LyX-Code
2892
2893
2894\size small
2895 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
2896\layout LyX-Code
2897
2898
2899\size small
2900
2901\layout LyX-Code
2902
2903
2904\size small
2905 return 0; /* Decoding finished successfully */
2906\layout LyX-Code
2907
2908
2909\size small
2910}
2911\end_deeper
2912\layout Enumerate
2913
Lev Walkinfc67f3e2005-01-17 11:05:04 +00002914Compile all files together using C compiler (varies by platform):
Lev Walkindba8ca02005-01-14 11:38:49 +00002915\begin_deeper
2916\layout LyX-Code
2917
2918
2919\emph on
2920cc -I.
Lev Walkinfc67f3e2005-01-17 11:05:04 +00002921 -o
2922\series bold
2923rdecode
2924\series default
2925 *.c
Lev Walkindba8ca02005-01-14 11:38:49 +00002926\end_deeper
2927\layout Enumerate
2928
Lev Walkinfecbd1e2005-01-31 10:10:04 +00002929Voila! You have just created the BER decoder of a Rectangle type, named
2930
Lev Walkindba8ca02005-01-14 11:38:49 +00002931\series bold
2932rdecode
2933\series default
2934!
Lev Walkindee61782005-02-02 09:53:46 +00002935\layout Chapter
2936
2937Constraint validation examples
2938\layout Standard
2939
2940This chapter shows how to define ASN.1 constraints and use the generated
2941 validation code.
2942\layout Section
2943
2944Adding constraints into
2945\begin_inset Quotes sld
2946\end_inset
2947
2948Rectangle
2949\begin_inset Quotes srd
2950\end_inset
2951
2952 type
2953\layout Standard
2954
2955This example shows how to add basic constraints to the ASN.1 specification
2956 and how to invoke the constraints validation code in your application.
2957\layout Enumerate
2958
2959Create a file named
2960\series bold
2961rectangle.asn1
2962\series default
2963 with the following contents:
2964\begin_deeper
2965\layout LyX-Code
2966
2967RectangleModuleWithConstraints DEFINITIONS ::=
2968\layout LyX-Code
2969
2970BEGIN
2971\layout LyX-Code
2972
2973
2974\layout LyX-Code
2975
2976Rectangle ::= SEQUENCE {
2977\layout LyX-Code
2978
2979 height INTEGER (0..100), -- Value range constraint
2980\layout LyX-Code
2981
2982 width INTEGER (0..MAX) -- Makes width non-negative
2983\layout LyX-Code
2984
2985}
2986\layout LyX-Code
2987
2988
2989\layout LyX-Code
2990
2991END
2992\end_deeper
2993\layout Enumerate
2994
2995Compile the file according to procedures shown in the previous chapter.
2996\layout Enumerate
2997
2998Modify the Rectangle type processing routine (you can start with the main()
2999 routine shown in the Section
3000\begin_inset LatexCommand \vref{sec:A-Rectangle-Decoder}
3001
3002\end_inset
3003
3004) by placing the following snippet of code
3005\emph on
3006before
3007\emph default
3008 encoding and/or
3009\emph on
3010after
3011\emph default
3012 decoding the Rectangle type
3013\begin_inset Foot
3014collapsed true
3015
3016\layout Standard
3017
3018Placing the constraint checking code
3019\emph on
3020before
3021\emph default
3022 encoding helps to make sure you know the data is correct and within constraints
3023 before sharing the data with anyone else.
3024\layout Standard
3025
3026Placing the constraint checking code
3027\emph on
3028after
3029\emph default
3030 decoding, but before any further action depending on the decoded data,
3031 helps to make sure the application got the valid contents before making
3032 use of it.
3033\end_inset
3034
3035:
3036\begin_inset ERT
3037status Collapsed
3038
3039\layout Standard
3040
3041\backslash
3042clearpage{}
3043\end_inset
3044
3045
3046\begin_deeper
3047\layout LyX-Code
3048
3049
3050\size small
3051int ret; /* Return value */
3052\layout LyX-Code
3053
3054
3055\size small
3056char errbuf[128]; /* Buffer for error message */
3057\layout LyX-Code
3058
3059
3060\size small
3061size_t errlen = sizeof(errbuf); /* Size of the buffer */
3062\layout LyX-Code
3063
3064
3065\size small
3066
3067\layout LyX-Code
3068
3069
3070\size small
3071/* ...
3072 here may go Rectangle decoding code ...
3073 */
3074\layout LyX-Code
3075
3076
3077\size small
3078
3079\layout LyX-Code
3080
3081
3082\size small
3083ret = asn_check_constraints(asn_DEF_Rectangle,
3084\layout LyX-Code
3085
3086
3087\size small
3088 rectangle, errbuf, &errlen);
3089\layout LyX-Code
3090
3091
3092\size small
3093/* assert(errlen < sizeof(errbuf)); // you may rely on that */
3094\layout LyX-Code
3095
3096
3097\size small
3098if(ret) {
3099\layout LyX-Code
3100
3101
3102\size small
3103 fprintf(stderr,
3104\begin_inset Quotes sld
3105\end_inset
3106
3107Constraint validation failed: %s
3108\backslash
3109n
3110\begin_inset Quotes srd
3111\end_inset
3112
3113,
3114\layout LyX-Code
3115
3116
3117\size small
3118 errbuf /* errbuf is properly nul-terminated */
3119\layout LyX-Code
3120
3121
3122\size small
3123 );
3124\layout LyX-Code
3125
3126
3127\size small
3128 /* exit(...); // Replace with appropriate action */
3129\layout LyX-Code
3130
3131
3132\size small
3133}
3134\layout LyX-Code
3135
3136
3137\size small
3138
3139\layout LyX-Code
3140
3141
3142\size small
3143/* ...
3144 here may go Rectangle encoding code ...
3145 */
3146\end_deeper
3147\layout Enumerate
3148
3149Compile the resulting C code as shown in the previous chapters.
3150\layout Enumerate
3151
Lev Walkin1411d052005-02-02 11:06:38 +00003152Try to test the constraints checking code by assigning integer value 101
3153 to the
3154\series bold
3155.height
3156\series default
3157 member of the Rectangle structure, or a negative value to the
3158\series bold
3159.width
3160\series default
3161 member.
3162 In either case, the program should print
3163\begin_inset Quotes sld
3164\end_inset
3165
3166Constraint validation failed
3167\begin_inset Quotes srd
3168\end_inset
3169
3170 message, followed by the short explanation why validation did not succeed.
3171\layout Enumerate
3172
Lev Walkindee61782005-02-02 09:53:46 +00003173Done.
Lev Walkin09889622005-02-21 14:16:10 +00003174\layout Part
3175
3176
3177\begin_inset LatexCommand \label{par:ASN.1-Basics}
3178
3179\end_inset
3180
3181ASN.1 Basics
3182\layout Chapter
3183
3184
3185\begin_inset LatexCommand \label{cha:Abstract-Syntax-Notation:}
3186
3187\end_inset
3188
3189Abstract Syntax Notation: ASN.1
3190\layout Standard
3191
3192
3193\emph on
3194This chapter defines some basic ASN.1 concepts and describes several most
3195 widely used types.
3196 It is by no means an authoritative or complete reference.
3197 For more complete ASN.1 description, please refer to Olivier Dubuisson's
3198 book
3199\begin_inset LatexCommand \cite{Dub00}
3200
3201\end_inset
3202
3203 or the ASN.1 body of standards itself
3204\begin_inset LatexCommand \cite{ITU-T/ASN.1}
3205
3206\end_inset
3207
3208.
3209\layout Standard
3210
3211The Abstract Syntax Notation One is used to formally describe the semantics
3212 of data transmitted across the network.
3213 Two communicating parties may have different formats of their native data
3214 types (i.e.
3215 number of bits in the integer type), thus it is important to have a way
3216 to describe the data in a manner which is independent from the particular
3217 machine's representation.
3218 The ASN.1 specifications are used to achieve the following:
3219\layout Itemize
3220
3221The specification expressed in the ASN.1 notation is a formal and precise
3222 way to communicate the data semantics to human readers;
3223\layout Itemize
3224
3225The ASN.1 specifications may be used as input for automatic compilers which
3226 produce the code for some target language (C, C++, Java, etc) to encode
3227 and decode the data according to some encoding rules (which are also defined
3228 by the ASN.1 standard).
3229\layout Standard
3230
3231Consider the following example:
3232\layout LyX-Code
3233
3234Rectangle ::= SEQUENCE {
3235\layout LyX-Code
3236
3237 height INTEGER,
3238\layout LyX-Code
3239
3240 width INTEGER
3241\layout LyX-Code
3242
3243}
3244\layout Standard
3245
3246This ASN.1 specification describes a constructed type,
3247\emph on
3248Rectangle
3249\emph default
3250, containing two integer fields.
3251 This specification may tell the reader that there exists this kind of data
3252 structure and that some entity may be prepared to send or receive it.
3253 The question on
3254\emph on
3255how
3256\emph default
3257 that entity is going to send or receive the
3258\emph on
3259encoded data
3260\emph default
3261 is outside the scope of ASN.1.
3262 For example, this data structure may be encoded according to some encoding
3263 rules and sent to the destination using the TCP protocol.
3264 The ASN.1 specifies several ways of encoding (or
3265\begin_inset Quotes sld
3266\end_inset
3267
3268serializing
3269\begin_inset Quotes srd
3270\end_inset
3271
3272, or
3273\begin_inset Quotes sld
3274\end_inset
3275
3276marshaling
3277\begin_inset Quotes srd
3278\end_inset
3279
3280) the data: BER, PER, XER and others, including CER and DER derivatives
3281 from BER.
3282\layout Standard
3283
3284The complete specification must be wrapped in a module, which looks like
3285 this:
3286\layout LyX-Code
3287
3288RectangleModule1
3289\layout LyX-Code
3290
3291 { iso org(3) dod(6) internet(1) private(4)
3292\layout LyX-Code
3293
3294 enterprise(1) spelio(9363) software(1)
3295\layout LyX-Code
3296
3297 asn1c(5) docs(2) rectangle(1) 1 }
3298\layout LyX-Code
3299
3300 DEFINITIONS AUTOMATIC TAGS ::=
3301\layout LyX-Code
3302
3303BEGIN
3304\layout LyX-Code
3305
3306
3307\layout LyX-Code
3308
3309-- This is a comment which describes nothing.
3310\layout LyX-Code
3311
3312Rectangle ::= SEQUENCE {
3313\layout LyX-Code
3314
3315 height INTEGER, -- Height of the rectangle
3316\layout LyX-Code
3317
3318 width INTEGER -- Width of the rectangle
3319\layout LyX-Code
3320
3321}
3322\layout LyX-Code
3323
3324
3325\layout LyX-Code
3326
3327END
3328\layout Standard
3329
3330The module header consists of module name (RectangleModule1), the module
3331 object identifier ({...}), a keyword
3332\begin_inset Quotes sld
3333\end_inset
3334
3335DEFINITIONS
3336\begin_inset Quotes srd
3337\end_inset
3338
3339, a set of module flags (AUTOMATIC TAGS) and
3340\begin_inset Quotes sld
3341\end_inset
3342
3343::= BEGIN
3344\begin_inset Quotes srd
3345\end_inset
3346
3347.
3348 The module ends with an
3349\begin_inset Quotes sld
3350\end_inset
3351
3352END
3353\begin_inset Quotes srd
3354\end_inset
3355
3356 statement.
3357\layout Section
3358
3359Some of the ASN.1 Basic Types
3360\layout Subsection
3361
3362The BOOLEAN type
3363\layout Standard
3364
3365The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF or
3366 a similar kind of two-way choice.
3367\layout Subsection
3368
3369The INTEGER type
3370\layout Standard
3371
3372The INTEGER type is a signed natural number type without any restrictions
3373 on its size.
3374 If the automatic checking on INTEGER value bounds are necessary, the subtype
3375 constraints must be used.
3376\layout LyX-Code
3377
3378SimpleInteger ::= INTEGER
3379\layout LyX-Code
3380
3381
3382\layout LyX-Code
3383
3384-- An integer with a very limited range
3385\layout LyX-Code
3386
3387SmallPositiveInt ::= INTEGER (0..127)
3388\layout LyX-Code
3389
3390
3391\layout LyX-Code
3392
3393-- Integer, negative
3394\layout LyX-Code
3395
3396NegativeInt ::= INTEGER (MIN..0)
3397\layout Subsection
3398
3399The ENUMERATED type
3400\layout Standard
3401
3402The ENUMERATED type is semantically equivalent to the INTEGER type with
3403 some integer values explicitly named.
3404\layout LyX-Code
3405
3406FruitId ::= ENUMERATED { apple(1), orange(2) }
3407\layout LyX-Code
3408
3409
3410\layout LyX-Code
3411
3412-- The numbers in braces are optional,
3413\layout LyX-Code
3414
3415-- the enumeration can be performed
3416\layout LyX-Code
3417
3418-- automatically by the compiler
3419\layout LyX-Code
3420
3421ComputerOSType ::= ENUMERATED {
3422\layout LyX-Code
3423
3424 FreeBSD, -- acquires value 0
3425\layout LyX-Code
3426
3427 Windows, -- acquires value 1
3428\layout LyX-Code
3429
3430 Solaris(5), -- remains 5
3431\layout LyX-Code
3432
3433 Linux, -- becomes 6
3434\layout LyX-Code
3435
3436 MacOS -- becomes 7
3437\layout LyX-Code
3438
3439}
3440\layout Subsection
3441
3442The OCTET STRING type
3443\layout Standard
3444
3445This type models the sequence of 8-bit bytes.
3446 This may be used to transmit some opaque data or data serialized by other
3447 types of encoders (i.e.
3448 video file, photo picture, etc).
3449\layout Subsection
3450
3451The OBJECT IDENTIFIER type
3452\layout Standard
3453
3454The OBJECT IDENTIFIER is used to represent the unique identifier of any
3455 object, starting from the very root of the registration tree.
3456 If your organization needs to uniquely identify something (a router, a
3457 room, a person, a standard, or whatever), you are encouraged to get your
3458 own identification subtree at
3459\begin_inset LatexCommand \htmlurl{http://www.iana.org/protocols/forms.htm}
3460
3461\end_inset
3462
3463.
3464\layout Standard
3465
3466For example, the very first ASN.1 module in this Chapter (RectangleModule1)
3467 has the following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
3468\layout LyX-Code
3469
3470ExampleOID ::= OBJECT IDENTIFIER
3471\layout LyX-Code
3472
3473
3474\layout LyX-Code
3475
3476rectangleModule1-oid ExampleOID
3477\layout LyX-Code
3478
3479 ::= { 1 3 6 1 4 1 9363 1 5 2 1 1 }
3480\layout LyX-Code
3481
3482
3483\layout LyX-Code
3484
3485-- An identifier of the Internet.
3486\layout LyX-Code
3487
3488internet-id OBJECT IDENTIFIER
3489\layout LyX-Code
3490
3491 ::= { iso(1) identified-organization(3)
3492\layout LyX-Code
3493
3494 dod(6) internet(1) }
3495\layout Standard
3496
3497As you see, names are optional.
3498\layout Subsection
3499
3500The RELATIVE-OID type
3501\layout Standard
3502
3503The RELATIVE-OID type has the semantics of a subtree of an OBJECT IDENTIFIER.
3504 There may be no need to repeat the whole sequence of numbers from the root
3505 of the registration tree where the only thing of interest is some of the
3506 tree's subsequence.
3507\layout LyX-Code
3508
3509this-document RELATIVE-OID ::= { docs(2) usage(1) }
3510\layout LyX-Code
3511
3512
3513\layout LyX-Code
3514
3515this-example RELATIVE-OID ::= {
3516\layout LyX-Code
3517
3518 this-document assorted-examples(0) this-example(1) }
3519\layout Section
3520
3521Some of the ASN.1 String Types
3522\layout Subsection
3523
3524The IA5String type
3525\layout Standard
3526
3527This is essentially the ASCII, with 128 character codes available (7 lower
3528 bits of an 8-bit byte).
3529\layout Subsection
3530
3531The UTF8String type
3532\layout Standard
3533
3534This is the character string which encodes the full Unicode range (4 bytes)
3535 using multibyte character sequences.
3536\layout Subsection
3537
3538The NumericString type
3539\layout Standard
3540
3541This type represents the character string with the alphabet consisting of
3542 numbers (
3543\begin_inset Quotes sld
3544\end_inset
3545
35460
3547\begin_inset Quotes srd
3548\end_inset
3549
3550 to
3551\begin_inset Quotes sld
3552\end_inset
3553
35549
3555\begin_inset Quotes srd
3556\end_inset
3557
3558) and a space.
3559\layout Subsection
3560
3561The PrintableString type
3562\layout Standard
3563
3564The character string with the following alphabet: space,
3565\begin_inset Quotes sld
3566\end_inset
3567
3568
3569\series bold
3570'
3571\series default
3572
3573\begin_inset Quotes srd
3574\end_inset
3575
3576 (single quote),
3577\begin_inset Quotes sld
3578\end_inset
3579
3580
3581\series bold
3582(
3583\series default
3584
3585\begin_inset Quotes sld
3586\end_inset
3587
3588,
3589\begin_inset Quotes sld
3590\end_inset
3591
3592
3593\series bold
3594)
3595\series default
3596
3597\begin_inset Quotes srd
3598\end_inset
3599
3600,
3601\begin_inset Quotes sld
3602\end_inset
3603
3604
3605\series bold
3606+
3607\series default
3608
3609\begin_inset Quotes srd
3610\end_inset
3611
3612,
3613\begin_inset Quotes sld
3614\end_inset
3615
3616
3617\series bold
3618,
3619\series default
3620
3621\begin_inset Quotes srd
3622\end_inset
3623
3624 (comma),
3625\begin_inset Quotes sld
3626\end_inset
3627
3628
3629\series bold
3630-
3631\series default
3632
3633\begin_inset Quotes srd
3634\end_inset
3635
3636,
3637\begin_inset Quotes sld
3638\end_inset
3639
3640
3641\series bold
3642.
3643\series default
3644
3645\begin_inset Quotes srd
3646\end_inset
3647
3648,
3649\begin_inset Quotes sld
3650\end_inset
3651
3652
3653\series bold
3654/
3655\series default
3656
3657\begin_inset Quotes srd
3658\end_inset
3659
3660, digits (
3661\begin_inset Quotes sld
3662\end_inset
3663
36640
3665\begin_inset Quotes srd
3666\end_inset
3667
3668 to
3669\begin_inset Quotes sld
3670\end_inset
3671
36729
3673\begin_inset Quotes srd
3674\end_inset
3675
3676),
3677\begin_inset Quotes sld
3678\end_inset
3679
3680
3681\series bold
3682:
3683\series default
3684
3685\begin_inset Quotes srd
3686\end_inset
3687
3688,
3689\begin_inset Quotes sld
3690\end_inset
3691
3692
3693\series bold
3694=
3695\series default
3696
3697\begin_inset Quotes srd
3698\end_inset
3699
3700,
3701\begin_inset Quotes sld
3702\end_inset
3703
3704
3705\series bold
3706?
3707\series default
3708
3709\begin_inset Quotes srd
3710\end_inset
3711
3712, upper-case and lower-case letters (
3713\begin_inset Quotes sld
3714\end_inset
3715
3716A
3717\begin_inset Quotes srd
3718\end_inset
3719
3720 to
3721\begin_inset Quotes sld
3722\end_inset
3723
3724Z
3725\begin_inset Quotes srd
3726\end_inset
3727
3728 and
3729\begin_inset Quotes sld
3730\end_inset
3731
3732a
3733\begin_inset Quotes srd
3734\end_inset
3735
3736 to
3737\begin_inset Quotes sld
3738\end_inset
3739
3740z
3741\begin_inset Quotes srd
3742\end_inset
3743
3744).
3745\layout Subsection
3746
3747The VisibleString type
3748\layout Standard
3749
3750The character string with the alphabet which is more or less a subset of
3751 ASCII between the space and the
3752\begin_inset Quotes sld
3753\end_inset
3754
3755
3756\series bold
3757~
3758\series default
3759
3760\begin_inset Quotes srd
3761\end_inset
3762
3763 symbol (tilde).
3764\layout Standard
3765
3766Alternatively, the alphabet may be described as the PrintableString alphabet
3767 presented earlier, plus the following characters:
3768\begin_inset Quotes sld
3769\end_inset
3770
3771
3772\series bold
3773!
3774\series default
3775
3776\begin_inset Quotes srd
3777\end_inset
3778
3779,
3780\begin_inset Quotes sld
3781\end_inset
3782
3783
3784\series bold
3785
3786\begin_inset Quotes srd
3787\end_inset
3788
3789
3790\series default
3791
3792\begin_inset Quotes srd
3793\end_inset
3794
3795,
3796\begin_inset Quotes sld
3797\end_inset
3798
3799
3800\series bold
3801#
3802\series default
3803
3804\begin_inset Quotes srd
3805\end_inset
3806
3807,
3808\begin_inset Quotes sld
3809\end_inset
3810
3811
3812\series bold
3813$
3814\series default
3815
3816\begin_inset Quotes srd
3817\end_inset
3818
3819,
3820\begin_inset Quotes sld
3821\end_inset
3822
3823
3824\series bold
3825%
3826\series default
3827
3828\begin_inset Quotes srd
3829\end_inset
3830
3831,
3832\begin_inset Quotes sld
3833\end_inset
3834
3835
3836\series bold
3837&
3838\series default
3839
3840\begin_inset Quotes srd
3841\end_inset
3842
3843,
3844\begin_inset Quotes sld
3845\end_inset
3846
3847
3848\series bold
3849*
3850\series default
3851
3852\begin_inset Quotes srd
3853\end_inset
3854
3855,
3856\begin_inset Quotes sld
3857\end_inset
3858
3859
3860\series bold
3861;
3862\series default
3863
3864\begin_inset Quotes srd
3865\end_inset
3866
3867,
3868\begin_inset Quotes sld
3869\end_inset
3870
3871
3872\series bold
3873<
3874\series default
3875
3876\begin_inset Quotes srd
3877\end_inset
3878
3879,
3880\begin_inset Quotes sld
3881\end_inset
3882
3883
3884\series bold
3885>
3886\series default
3887
3888\begin_inset Quotes srd
3889\end_inset
3890
3891,
3892\begin_inset Quotes sld
3893\end_inset
3894
3895
3896\series bold
3897[
3898\series default
3899
3900\begin_inset Quotes srd
3901\end_inset
3902
3903,
3904\begin_inset Quotes sld
3905\end_inset
3906
3907
3908\series bold
3909
3910\backslash
3911
3912\series default
3913
3914\begin_inset Quotes srd
3915\end_inset
3916
3917,
3918\begin_inset Quotes sld
3919\end_inset
3920
3921
3922\series bold
3923]
3924\series default
3925
3926\begin_inset Quotes srd
3927\end_inset
3928
3929,
3930\begin_inset Quotes sld
3931\end_inset
3932
3933
3934\series bold
3935^
3936\series default
3937
3938\begin_inset Quotes srd
3939\end_inset
3940
3941,
3942\begin_inset Quotes sld
3943\end_inset
3944
3945
3946\series bold
3947_
3948\series default
3949
3950\begin_inset Quotes srd
3951\end_inset
3952
3953,
3954\begin_inset Quotes sld
3955\end_inset
3956
3957
3958\series bold
3959`
3960\series default
3961
3962\begin_inset Quotes srd
3963\end_inset
3964
3965 (single left quote),
3966\begin_inset Quotes sld
3967\end_inset
3968
3969
3970\series bold
3971{
3972\series default
3973
3974\begin_inset Quotes srd
3975\end_inset
3976
3977,
3978\begin_inset Quotes sld
3979\end_inset
3980
3981
3982\series bold
3983|
3984\series default
3985
3986\begin_inset Quotes srd
3987\end_inset
3988
3989,
3990\begin_inset Quotes sld
3991\end_inset
3992
3993
3994\series bold
3995}
3996\series default
3997
3998\begin_inset Quotes srd
3999\end_inset
4000
4001,
4002\begin_inset Quotes sld
4003\end_inset
4004
4005
4006\series bold
4007~
4008\series default
4009
4010\begin_inset Quotes srd
4011\end_inset
4012
4013.
4014\layout Section
4015
4016ASN.1 Constructed Types
4017\layout Subsection
4018
4019The SEQUENCE type
4020\layout Standard
4021
4022This is an ordered collection of other simple or constructed types.
4023 The SEQUENCE constructed type resembles the C
4024\begin_inset Quotes sld
4025\end_inset
4026
4027struct
4028\begin_inset Quotes srd
4029\end_inset
4030
4031 statement.
4032\layout LyX-Code
4033
4034Address ::= SEQUENCE {
4035\layout LyX-Code
4036
4037 -- The apartment number may be omitted
4038\layout LyX-Code
4039
4040 apartmentNumber NumericString OPTIONAL,
4041\layout LyX-Code
4042
4043 streetName PrintableString,
4044\layout LyX-Code
4045
4046 cityName PrintableString,
4047\layout LyX-Code
4048
4049 stateName PrintableString,
4050\layout LyX-Code
4051
4052 -- This one may be omitted too
4053\layout LyX-Code
4054
4055 zipNo NumericString OPTIONAL
4056\layout LyX-Code
4057
4058}
4059\layout Subsection
4060
4061The SET type
4062\layout Standard
4063
4064This is a collection of other simple or constructed types.
4065 Ordering is not important.
4066 The data may arrive in the order which is different from the order of specifica
4067tion.
4068 Data is encoded in the order not necessarily corresponding to the order
4069 of specification.
4070\layout Subsection
4071
4072The CHOICE type
4073\layout Standard
4074
4075This type is just a choice between the subtypes specified in it.
4076 The CHOICE type contains at most one of the subtypes specified, and it
4077 is always implicitly known which choice is being decoded or encoded.
4078 This one resembles the C
4079\begin_inset Quotes sld
4080\end_inset
4081
4082union
4083\begin_inset Quotes srd
4084\end_inset
4085
4086 statement.
4087\layout Standard
4088
4089The following type defines a response code, which may be either an integer
4090 code or a boolean
4091\begin_inset Quotes sld
4092\end_inset
4093
4094true
4095\begin_inset Quotes srd
4096\end_inset
4097
4098/
4099\begin_inset Quotes srd
4100\end_inset
4101
4102false
4103\begin_inset Quotes srd
4104\end_inset
4105
4106 code.
4107\layout LyX-Code
4108
4109ResponseCode ::= CHOICE {
4110\layout LyX-Code
4111
4112 intCode INTEGER,
4113\layout LyX-Code
4114
4115 boolCode BOOLEAN
4116\layout LyX-Code
4117
4118}
4119\layout LyX-Code
4120
4121\layout Subsection
4122
4123The SEQUENCE OF type
4124\layout Standard
4125
4126This one is the list (array) of simple or constructed types:
4127\layout LyX-Code
4128
4129-- Example 1
4130\layout LyX-Code
4131
4132ManyIntegers ::= SEQUENCE OF INTEGER
4133\layout LyX-Code
4134
4135
4136\layout LyX-Code
4137
4138-- Example 2
4139\layout LyX-Code
4140
4141ManyRectangles ::= SEQUENCE OF Rectangle
4142\layout LyX-Code
4143
4144
4145\layout LyX-Code
4146
4147-- More complex example:
4148\layout LyX-Code
4149
4150-- an array of structures defined in place.
4151\layout LyX-Code
4152
4153ManyCircles ::= SEQUENCE OF SEQUENCE {
4154\layout LyX-Code
4155
4156 radius INTEGER
4157\layout LyX-Code
4158
4159 }
4160\layout Subsection
4161
4162The SET OF type
4163\layout Standard
4164
4165The SET OF type models the bag of structures.
4166 It resembles the SEQUENCE OF type, but the order is not important: i.e.
4167 the elements may arrive in the order which is not necessarily the same
4168 as the in-memory order on the remote machines.
4169\layout LyX-Code
4170
4171-- A set of structures defined elsewhere
4172\layout LyX-Code
4173
4174SetOfApples :: SET OF Apple
4175\layout LyX-Code
4176
4177
4178\layout LyX-Code
4179
4180-- Set of integers encoding the kind of a fruit
4181\layout LyX-Code
4182
4183FruitBag ::= SET OF ENUMERATED { apple, orange }
Lev Walkin84291e02004-06-03 03:49:45 +00004184\layout Bibliography
Lev Walkin26587ab2004-08-23 15:12:04 +00004185\bibitem [ASN1C]{ASN1C}
4186
Lev Walkindee61782005-02-02 09:53:46 +00004187The Open Source ASN.1 Compiler.
Lev Walkin26587ab2004-08-23 15:12:04 +00004188
Lev Walkindee61782005-02-02 09:53:46 +00004189\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c}
Lev Walkin26587ab2004-08-23 15:12:04 +00004190
4191\end_inset
4192
4193
4194\layout Bibliography
Lev Walkindba8ca02005-01-14 11:38:49 +00004195\bibitem [AONL]{AONL}
4196
4197Online ASN.1 Compiler.
4198
4199\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c/asn1c.cgi}
4200
4201\end_inset
4202
4203
4204\layout Bibliography
Lev Walkin84291e02004-06-03 03:49:45 +00004205\bibitem [Dub00]{Dub00}
4206
Lev Walkina646ccb2004-10-20 15:48:55 +00004207Olivier Dubuisson ---
Lev Walkin84291e02004-06-03 03:49:45 +00004208\emph on
4209ASN.1 Communication between heterogeneous systems
4210\emph default
Lev Walkina646ccb2004-10-20 15:48:55 +00004211 --- Morgan Kaufmann Publishers, 2000.
Lev Walkin84291e02004-06-03 03:49:45 +00004212
4213\begin_inset LatexCommand \htmlurl{http://asn1.elibel.tm.fr/en/book/}
4214
4215\end_inset
4216
4217.
4218 ISBN:0-12-6333361-0.
4219\layout Bibliography
4220\bibitem [ITU-T/ASN.1]{ITU-T/ASN.1}
4221
4222ITU-T Study Group 17 -- Languages for Telecommunication Systems
4223\begin_inset LatexCommand \url{http://www.itu.int/ITU-T/studygroups/com17/languages/}
4224
4225\end_inset
4226
4227
4228\the_end