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