blob: 8e43df2af6507c5ef7f0f99fb850ddd17d9b2fc8 [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
vlmc3bfd812004-09-30 22:26:19 +000072lhead{Document describes
73\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
112 convert the ASN.1 specifications into some other target language.
113 At this moment, only C and C++ target languages are supported, the latter
114 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.
120 Also, it creates the code which allows automatic serialization and deserializat
121ion of these structures using several standardized encoding rules (BER,
122 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
vlm856978f2005-02-21 14:16:10 +0000252 and check the table
253\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
vlmfcec8e52004-08-23 15:12:04 +0000962First of all, you should to include one or more header files into your applicati
963on.
964 For our Rectangle module, including the Rectangle.h file is enough:
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.
993 The second line invokes the generic free_struct routine created specifically
994 for this Rectangle_t structure.
995 The
996\emph on
vlmba56d852004-09-29 13:29:17 +0000997asn_DEF_Rectangle
vlme18adea2004-06-03 03:49:45 +0000998\emph default
999 is the type descriptor, which holds a collection of generic routines to
1000 deal with the Rectangle_t structure.
1001\layout Standard
1002
1003There are several generic functions available:
1004\layout Description
1005
vlme18adea2004-06-03 03:49:45 +00001006ber_decoder This is the generic
1007\emph on
1008restartable
1009\begin_inset Foot
1010collapsed false
1011
1012\layout Standard
1013
1014Restartable means that if the decoder encounters the end of the buffer,
1015 it will fail, but may later be invoked again with the rest of the buffer
1016 to continue decoding.
1017\end_inset
1018
1019
1020\emph default
1021BER decoder (Basic Encoding Rules).
1022 This decoder would create and/or fill the target structure for you.
1023 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001024\begin_inset LatexCommand \vref{sub:Decoding-BER}
vlme18adea2004-06-03 03:49:45 +00001025
1026\end_inset
1027
1028.
1029\layout Description
1030
1031der_encoder This is the generic DER encoder (Distinguished Encoding Rules).
vlm68d43e92004-09-24 20:56:07 +00001032 This encoder will take the target structure and encode it into a series
vlme18adea2004-06-03 03:49:45 +00001033 of bytes.
1034 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001035\begin_inset LatexCommand \vref{sub:Encoding-DER}
vlme18adea2004-06-03 03:49:45 +00001036
1037\end_inset
1038
1039.
1040\layout Description
1041
vlm856978f2005-02-21 14:16:10 +00001042xer_encoder This is the XER encoder (XML Encoding Rules).
vlm68d43e92004-09-24 20:56:07 +00001043 This encoder will take the target structure and represent it as an XML
vlm856978f2005-02-21 14:16:10 +00001044 (text) document using either BASIC-XER or CANONICAL-XER encoding rules.
vlm68d43e92004-09-24 20:56:07 +00001045 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001046\begin_inset LatexCommand \vref{sub:Encoding-XER}
1047
1048\end_inset
1049
1050.
1051\layout Description
1052
1053xer_decoder This is the generic XER decoder.
1054 It takes both BASIC-XER or CANONICAL-XER encodings and deserializes the
1055 data into a local, machine-dependent representation.
1056 Please refer to Section
1057\begin_inset LatexCommand \vref{sub:Decoding-XER}
vlm68d43e92004-09-24 20:56:07 +00001058
1059\end_inset
1060
1061.
1062\layout Description
1063
1064check_constraints Check that the contents of the target structure are semantical
1065ly valid and constrained to appropriate implicit or explicit subtype constraints.
1066 Please refer to Section
1067\begin_inset LatexCommand \vref{sub:Validating-the-target}
1068
1069\end_inset
1070
1071.
1072\layout Description
1073
vlme18adea2004-06-03 03:49:45 +00001074print_struct This function convert the contents of the passed target structure
1075 into human readable form.
1076 This form is not formal and cannot be converted back into the structure,
1077 but it may turn out to be useful for debugging or quick-n-dirty printing.
1078 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001079\begin_inset LatexCommand \vref{sub:Printing-the-target}
vlme18adea2004-06-03 03:49:45 +00001080
1081\end_inset
1082
1083.
1084\layout Description
1085
1086free_struct This is a generic disposal which frees the target structure.
1087 Please refer to Section
vlm856978f2005-02-21 14:16:10 +00001088\begin_inset LatexCommand \vref{sub:Freeing-the-target}
vlm68d43e92004-09-24 20:56:07 +00001089
1090\end_inset
1091
1092.
1093\layout Standard
1094
vlme18adea2004-06-03 03:49:45 +00001095Each of the above function takes the type descriptor (
1096\emph on
vlmba56d852004-09-29 13:29:17 +00001097asn_DEF_\SpecialChar \ldots{}
vlme18adea2004-06-03 03:49:45 +00001098
1099\emph default
1100) and the target structure (
1101\emph on
1102rect
1103\emph default
1104, in the above example).
vlm856978f2005-02-21 14:16:10 +00001105\layout Subsection
1106
1107
1108\begin_inset LatexCommand \label{sub:Decoding-BER}
1109
1110\end_inset
1111
1112Decoding BER
vlme18adea2004-06-03 03:49:45 +00001113\layout Standard
1114
vlm856978f2005-02-21 14:16:10 +00001115The Basic Encoding Rules describe the most widely used (by the ASN.1 community)
1116 way how the structure can be encoded and decoded.
1117 Several other encoding rules (CER, DER) define a more restrictive versions
1118 of BER, so the generic BER parser is also capable of decoding the data
1119 encoded by CER and DER encoders.
1120 The opposite is not true.
1121\layout Standard
1122
1123The ASN.1 compiler provides the generic BER decoder which is implicitly capable
1124 of decoding BER, CER and DER encoded data.
1125\layout Standard
1126
1127The decoder is restartable (stream-oriented), which means that in case the
1128 buffer has less data than it is expected, the decoder will process whatever
1129 there is available and ask for more data to be provided.
1130 Please note that the decoder may actually process less data than it was
1131 given in the buffer, which means that you must be able to make the next
1132 buffer contain the unprocessed part of the previous buffer.
1133\layout Standard
1134
1135Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
1136\layout Itemize
1137
1138You may concatenate these buffers and feed the BER decoder with 300 bytes
1139 of data, or
1140\layout Itemize
1141
1142You may feed it the first buffer of 100 bytes of data, realize that the
1143 ber_decoder consumed only 95 bytes from it and later feed the decoder with
1144 205 bytes buffer which consists of 5 unprocessed bytes from the first buffer
1145 and the latter 200 bytes from the second buffer.
1146\layout Standard
1147
1148This is not as convenient as it could be (like, the BER encoder would consume
1149 the whole 100 bytes and keep these 5 bytes in some temporary storage),
1150 but in case of stream-based processing it might actually be OK.
1151 Suggestions are welcome.
1152\layout Standard
1153
1154Here is the simplest example which shows how to invoke a BER decoder.
vlme18adea2004-06-03 03:49:45 +00001155\layout LyX-Code
1156
1157Rectangle_t *
1158\layout LyX-Code
1159
vlmfcec8e52004-08-23 15:12:04 +00001160simple_deserializer(const void *buffer, size_t buf_size) {
vlme18adea2004-06-03 03:49:45 +00001161\layout LyX-Code
1162
1163 Rectangle_t *rect = 0; /* Note this 0! */
1164\layout LyX-Code
1165
vlmaf2155e2004-10-20 15:48:55 +00001166 asn_dec_rval_t rval;
vlme18adea2004-06-03 03:49:45 +00001167\layout LyX-Code
1168
1169
1170\layout LyX-Code
1171
vlm856978f2005-02-21 14:16:10 +00001172 rval =
1173\series bold
1174asn_DEF_Rectangle->ber_decoder
1175\series default
1176(0,
vlme18adea2004-06-03 03:49:45 +00001177\layout LyX-Code
1178
vlmba56d852004-09-29 13:29:17 +00001179 &asn_DEF_Rectangle,
vlme18adea2004-06-03 03:49:45 +00001180\layout LyX-Code
1181
1182 (void **)&rect,
1183\layout LyX-Code
1184
1185 buffer, buf_size,
1186\layout LyX-Code
1187
1188 0);
1189\layout LyX-Code
1190
1191
1192\layout LyX-Code
1193
1194 if(rval
1195\series bold
1196.code
1197\series default
1198 == RC_OK) {
1199\layout LyX-Code
1200
1201 return rect; /* Decoding succeeded */
1202\layout LyX-Code
1203
1204 } else {
1205\layout LyX-Code
1206
vlmfcec8e52004-08-23 15:12:04 +00001207 /* Free partially decoded rect */
1208\layout LyX-Code
1209
vlmba56d852004-09-29 13:29:17 +00001210 asn_DEF_Rectangle->free_struct(
vlme18adea2004-06-03 03:49:45 +00001211\layout LyX-Code
1212
vlmba56d852004-09-29 13:29:17 +00001213 &asn_DEF_Rectangle, rect, 0);
vlme18adea2004-06-03 03:49:45 +00001214\layout LyX-Code
1215
1216 return 0;
1217\layout LyX-Code
1218
1219 }
1220\layout LyX-Code
1221
1222}
1223\layout Standard
1224
1225The code above defines a function,
1226\emph on
1227simple_deserializer
1228\emph default
1229, which takes a buffer and its length and expected to return a pointer to
1230 the Rectangle_t structure.
1231 Inside, it tries to convert the bytes passed into the target structure
1232 (rect) using the generic BER decoder and returns the rect pointer afterwards.
1233 If the structure cannot be deserialized, it frees the memory which might
1234 be left allocated by the unfinished
1235\emph on
1236ber_decoder
1237\emph default
vlm856978f2005-02-21 14:16:10 +00001238 routine and returns 0 (no data).
1239 This
vlme18adea2004-06-03 03:49:45 +00001240\series bold
vlm856978f2005-02-21 14:16:10 +00001241 freeing is necessary
vlme18adea2004-06-03 03:49:45 +00001242\series default
1243 because the ber_decoder is a restartable procedure, and may fail just because
1244 there is more data needs to be provided before decoding could be finalized.
1245 The code above obviously does not take into account the way the
1246\emph on
1247ber_decoder
1248\emph default
1249 failed, so the freeing is necessary because the part of the buffer may
1250 already be decoded into the structure by the time something goes wrong.
1251\layout Standard
1252
vlm856978f2005-02-21 14:16:10 +00001253A little less wordy would be to invoke a
vlme18adea2004-06-03 03:49:45 +00001254\emph on
1255ber_decode
1256\emph default
vlm856978f2005-02-21 14:16:10 +00001257 function instead of dereferencing the asn_DEF_Rectangle:
vlme18adea2004-06-03 03:49:45 +00001258\layout LyX-Code
1259
vlmba56d852004-09-29 13:29:17 +00001260rval = ber_decode(0, &asn_DEF_Rectangle, (void **)&rect,
vlme18adea2004-06-03 03:49:45 +00001261\layout LyX-Code
1262
1263 buffer, buf_size);
1264\layout Standard
1265
vlmba56d852004-09-29 13:29:17 +00001266Note that the initial (asn_DEF_Rectangle->ber_decoder) reference is gone,
vlme18adea2004-06-03 03:49:45 +00001267 and also the last argument (0) is no longer necessary.
1268\layout Standard
1269
vlm856978f2005-02-21 14:16:10 +00001270These two ways of BER decoder invocations are fully equivalent.
vlme18adea2004-06-03 03:49:45 +00001271\layout Standard
1272
1273The BER de
1274\emph on
1275coder
1276\emph default
vlmba56d852004-09-29 13:29:17 +00001277 may fail because of (
vlme18adea2004-06-03 03:49:45 +00001278\emph on
1279the following RC_\SpecialChar \ldots{}
1280 codes are defined in ber_decoder.h
1281\emph default
1282):
1283\layout Itemize
1284
1285RC_WMORE: There is more data expected than it is provided (stream mode continuat
1286ion feature);
1287\layout Itemize
1288
1289RC_FAIL: General failure to decode the buffer;
1290\layout Itemize
1291
1292\SpecialChar \ldots{}
1293 other codes may be defined as well.
1294\layout Standard
1295
vlmaf2155e2004-10-20 15:48:55 +00001296Together with the return code (.code) the asn_dec_rval_t type contains the
vlme18adea2004-06-03 03:49:45 +00001297 number of bytes which is consumed from the buffer.
1298 In the previous hypothetical example of two buffers (of 100 and 200 bytes),
1299 the first call to ber_decode() would return with .code = RC_WMORE and .consumed
1300 = 95.
1301 The .consumed field of the BER decoder return value is
1302\series bold
1303always
1304\series default
1305 valid, even if the decoder succeeds or fails with any other return code.
1306\layout Standard
1307
1308Please look into ber_decoder.h for the precise definition of ber_decode()
1309 and related types.
vlmfcec8e52004-08-23 15:12:04 +00001310\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001311
1312
1313\begin_inset LatexCommand \label{sub:Encoding-DER}
1314
1315\end_inset
1316
1317Encoding DER
1318\layout Standard
1319
vlm68d43e92004-09-24 20:56:07 +00001320The Distinguished Encoding Rules is the
1321\emph on
1322canonical
1323\emph default
1324 variant of BER encoding rules.
1325 The DER is best suited to encode the structures where all the lengths are
1326 known beforehand.
vlme18adea2004-06-03 03:49:45 +00001327 This is probably exactly how you want to encode: either after a BER decoding
1328 or after a manual fill-up, the target structure contains the data which
1329 size is implicitly known before encoding.
vlm856978f2005-02-21 14:16:10 +00001330 Among other uses, the DER encoding is used to encode X.509 certificates.
vlme18adea2004-06-03 03:49:45 +00001331\layout Standard
1332
1333As with BER decoder, the DER encoder may be invoked either directly from
vlmba56d852004-09-29 13:29:17 +00001334 the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone function,
vlme18adea2004-06-03 03:49:45 +00001335 which is somewhat simpler:
1336\layout LyX-Code
1337
vlme18adea2004-06-03 03:49:45 +00001338
1339\layout LyX-Code
1340
1341/*
1342\layout LyX-Code
1343
1344 * This is the serializer itself,
1345\layout LyX-Code
1346
1347 * it supplies the DER encoder with the
1348\layout LyX-Code
1349
1350 * pointer to the custom output function.
1351\layout LyX-Code
1352
1353 */
1354\layout LyX-Code
1355
1356ssize_t
1357\layout LyX-Code
1358
1359simple_serializer(FILE *ostream, Rectangle_t *rect) {
1360\layout LyX-Code
1361
vlm68d43e92004-09-24 20:56:07 +00001362 asn_enc_rval_t er; /* Encoder return value */
vlme18adea2004-06-03 03:49:45 +00001363\layout LyX-Code
1364
1365
1366\layout LyX-Code
1367
vlmba56d852004-09-29 13:29:17 +00001368 er = der_encode(&asn_DEF_Rect, rect,
vlme18adea2004-06-03 03:49:45 +00001369\layout LyX-Code
1370
vlmfcec8e52004-08-23 15:12:04 +00001371 write_stream, ostream);
vlme18adea2004-06-03 03:49:45 +00001372\layout LyX-Code
1373
vlm68d43e92004-09-24 20:56:07 +00001374 if(er.
vlme18adea2004-06-03 03:49:45 +00001375\series bold
vlm68d43e92004-09-24 20:56:07 +00001376encoded
vlme18adea2004-06-03 03:49:45 +00001377\series default
1378 == -1) {
1379\layout LyX-Code
1380
1381 /*
1382\layout LyX-Code
1383
vlm68d43e92004-09-24 20:56:07 +00001384 * Failed to encode the rectangle data.
vlme18adea2004-06-03 03:49:45 +00001385\layout LyX-Code
1386
1387 */
1388\layout LyX-Code
1389
1390 fprintf(stderr,
1391\begin_inset Quotes sld
1392\end_inset
1393
1394Cannot encode %s: %s
1395\backslash
1396n
1397\begin_inset Quotes srd
1398\end_inset
1399
1400,
1401\layout LyX-Code
1402
vlm68d43e92004-09-24 20:56:07 +00001403 er.
vlme18adea2004-06-03 03:49:45 +00001404\series bold
vlm68d43e92004-09-24 20:56:07 +00001405failed_type
vlme18adea2004-06-03 03:49:45 +00001406\series default
1407->name,
1408\layout LyX-Code
1409
1410 strerror(errno));
1411\layout LyX-Code
1412
1413 return -1;
1414\layout LyX-Code
1415
1416 } else {
1417\layout LyX-Code
1418
1419 /* Return the number of bytes */
1420\layout LyX-Code
1421
vlm68d43e92004-09-24 20:56:07 +00001422 return er.encoded;
vlme18adea2004-06-03 03:49:45 +00001423\layout LyX-Code
1424
1425 }
1426\layout LyX-Code
1427
1428}
1429\layout Standard
1430
1431As you see, the DER encoder does not write into some sort of buffer or something.
1432 It just invokes the custom function (possible, multiple times) which would
1433 save the data into appropriate storage.
1434 The optional argument
1435\emph on
1436app_key
1437\emph default
1438 is opaque for the DER encoder code and just used by
1439\emph on
1440_write_stream()
1441\emph default
1442 as the pointer to the appropriate output stream to be used.
1443\layout Standard
1444
1445If the custom write function is not given (passed as 0), then the DER encoder
1446 will essentially do the same thing (i.e., encode the data) but no callbacks
1447 will be invoked (so the data goes nowhere).
1448 It may prove useful to determine the size of the structure's encoding before
1449 actually doing the encoding
1450\begin_inset Foot
1451collapsed false
1452
1453\layout Standard
1454
1455It is actually faster too: the encoder might skip over some computations
1456 which aren't important for the size determination.
1457\end_inset
1458
1459.
1460\layout Standard
1461
1462Please look into der_encoder.h for the precise definition of der_encode()
1463 and related types.
vlmfcec8e52004-08-23 15:12:04 +00001464\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001465
1466
vlm68d43e92004-09-24 20:56:07 +00001467\begin_inset LatexCommand \label{sub:Encoding-XER}
1468
1469\end_inset
1470
1471Encoding XER
1472\layout Standard
1473
1474The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
1475 Markup Language, a text-based format for information exchange.
1476 The encoder routine API comes in two flavors: stdio-based and callback-based.
1477 With the callback-based encoder, the encoding process is very similar to
1478 the DER one, described in Section
1479\begin_inset LatexCommand \vref{sub:Encoding-DER}
1480
1481\end_inset
1482
1483.
1484 The following example uses the definition of write_stream() from up there.
1485\layout LyX-Code
1486
1487/*
1488\layout LyX-Code
1489
1490 * This procedure generates the XML document
1491\layout LyX-Code
1492
1493 * by invoking the XER encoder.
1494\layout LyX-Code
1495
1496 * NOTE: Do not copy this code verbatim!
1497\layout LyX-Code
1498
1499 * If the stdio output is necessary,
1500\layout LyX-Code
1501
1502 * use the xer_fprint() procedure instead.
1503\layout LyX-Code
1504
1505 * See Section
1506\begin_inset LatexCommand \vref{sub:Printing-the-target}
1507
1508\end_inset
1509
1510.
1511\layout LyX-Code
1512
1513 */
1514\layout LyX-Code
1515
1516int
1517\layout LyX-Code
1518
1519print_as_XML(FILE *ostream, Rectangle_t *rect) {
1520\layout LyX-Code
1521
1522 asn_enc_rval_t er; /* Encoder return value */
1523\layout LyX-Code
1524
1525
1526\layout LyX-Code
1527
vlm856978f2005-02-21 14:16:10 +00001528 er = xer_encode(&asn_DEF_Rectangle, rect,
vlm68d43e92004-09-24 20:56:07 +00001529\layout LyX-Code
1530
1531 XER_F_BASIC, /* BASIC-XER or CANONICAL-XER */
1532\layout LyX-Code
1533
1534 write_stream, ostream);
1535\layout LyX-Code
1536
1537
1538\layout LyX-Code
1539
1540 return (er.encoded == -1) ? -1 : 0;
1541\layout LyX-Code
1542
1543}
1544\layout Standard
1545
1546Please look into xer_encoder.h for the precise definition of xer_encode()
1547 and related types.
1548\layout Standard
1549
1550See Section
1551\begin_inset LatexCommand \ref{sub:Printing-the-target}
1552
1553\end_inset
1554
1555 for the example of stdio-based XML encoder and other pretty-printing suggestion
1556s.
1557\layout Subsection
1558
1559
vlm856978f2005-02-21 14:16:10 +00001560\begin_inset LatexCommand \label{sub:Decoding-XER}
1561
1562\end_inset
1563
1564Decoding XER
1565\layout Standard
1566
1567The data encoded using the XER rules can be subsequently decoded using the
1568 xer_decode() API call:
1569\layout LyX-Code
1570
1571Rectangle_t *
1572\layout LyX-Code
1573
1574XML_to_Rectangle(const void *buffer, size_t buf_size) {
1575\layout LyX-Code
1576
1577 Rectangle_t *rect = 0; /* Note this 0! */
1578\layout LyX-Code
1579
1580 asn_dec_rval_t rval;
1581\layout LyX-Code
1582
1583
1584\layout LyX-Code
1585
1586 rval = xer_decode(0, &asn_DEF_Rectangle, (void **)&rect,
1587\layout LyX-Code
1588
1589 buffer, buf_size);
1590\layout LyX-Code
1591
1592 if(rval
1593\series bold
1594.code
1595\series default
1596 == RC_OK) {
1597\layout LyX-Code
1598
1599 return rect; /* Decoding succeeded */
1600\layout LyX-Code
1601
1602 } else {
1603\layout LyX-Code
1604
1605 /* Free partially decoded rect */
1606\layout LyX-Code
1607
1608 asn_DEF_Rectangle->free_struct(
1609\layout LyX-Code
1610
1611 &asn_DEF_Rectangle, rect, 0);
1612\layout LyX-Code
1613
1614 return 0;
1615\layout LyX-Code
1616
1617 }
1618\layout LyX-Code
1619
1620}
1621\layout Standard
1622
1623The decoder takes both BASIC-XER and CANONICAL-XER encodings.
1624\layout Standard
1625
1626Please look into xer_decoder.h for the precise definition of xer_decode()
1627 and related types.
1628\layout Subsection
1629
1630
vlme18adea2004-06-03 03:49:45 +00001631\begin_inset LatexCommand \label{sub:Validating-the-target}
1632
1633\end_inset
1634
1635Validating the target structure
1636\layout Standard
1637
1638Sometimes the target structure needs to be validated.
1639 For example, if the structure was created by the application (as opposed
1640 to being decoded from some external source), some important information
1641 required by the ASN.1 specification might be missing.
1642 On the other hand, the successful decoding of the data from some external
1643 source does not necessarily mean that the data is fully valid either.
1644 It might well be the case that the specification describes some subtype
1645 constraints that were not taken into account during decoding, and it would
1646 actually be useful to perform the last check when the data is ready to
1647 be encoded or when the data has just been decoded to ensure its validity
1648 according to some stricter rules.
1649\layout Standard
1650
1651The asn_check_constraints() function checks the type for various implicit
1652 and explicit constraints.
1653 It is recommended to use asn_check_constraints() function after each decoding
1654 and before each encoding.
1655\layout Standard
1656
1657Please look into constraints.h for the precise definition of asn_check_constraint
1658s() and related types.
vlmfcec8e52004-08-23 15:12:04 +00001659\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001660
1661
1662\begin_inset LatexCommand \label{sub:Printing-the-target}
1663
1664\end_inset
1665
1666Printing the target structure
1667\layout Standard
1668
1669There are two ways to print the target structure: either invoke the print_struct
1670 member of the ASN.1 type descriptor, or using the asn_fprint() function,
1671 which is a simpler wrapper of the former:
1672\layout LyX-Code
1673
vlmba56d852004-09-29 13:29:17 +00001674asn_fprint(stdout, &asn_DEF_Rectangle, rect);
vlme18adea2004-06-03 03:49:45 +00001675\layout Standard
1676
1677Please look into constr_TYPE.h for the precise definition of asn_fprint()
1678 and related types.
vlm68d43e92004-09-24 20:56:07 +00001679\layout Standard
1680
1681Another practical alternative to this custom format printing would be to
1682 invoke XER encoder.
1683 The default BASIC-XER encoder performs reasonable formatting for the output
1684 to be useful and human readable.
1685 To invoke the XER decoder in a manner similar to asn_fprint(), use the
1686 xer_fprint() call:
1687\layout LyX-Code
1688
vlmba56d852004-09-29 13:29:17 +00001689xer_fprint(stdout, &asn_DEF_Rectangle, rect);
vlm68d43e92004-09-24 20:56:07 +00001690\layout Standard
1691
1692See Section
1693\begin_inset LatexCommand \vref{sub:Encoding-XER}
1694
1695\end_inset
1696
1697 for XML-related details.
vlmfcec8e52004-08-23 15:12:04 +00001698\layout Subsection
vlme18adea2004-06-03 03:49:45 +00001699
1700
1701\begin_inset LatexCommand \label{sub:Freeing-the-target}
1702
1703\end_inset
1704
1705Freeing the target structure
1706\layout Standard
1707
1708Freeing the structure is slightly more complex than it may seem to.
1709 When the ASN.1 structure is freed, all the members of the structure and
1710 their submembers etc etc are recursively freed too.
1711 But it might not be feasible to free the structure itself.
1712 Consider the following case:
1713\layout LyX-Code
1714
1715struct my_figure { /* The custom structure */
1716\layout LyX-Code
1717
1718 int flags; /* <some custom member> */
1719\layout LyX-Code
1720
1721 /* The type is generated by the ASN.1 compiler */
1722\layout LyX-Code
1723
1724
1725\emph on
1726Rectangle_t rect;
1727\layout LyX-Code
1728
1729 /* other members of the structure */
1730\layout LyX-Code
1731
1732};
1733\layout Standard
1734
1735In this example, the application programmer defined a custom structure with
1736 one ASN.1-derived member (rect).
1737 This member is not a reference to the Rectangle_t, but an in-place inclusion
1738 of the Rectangle_t structure.
1739 If the freeing is necessary, the usual procedure of freeing everything
1740 must not be applied to the &rect pointer itself, because it does not point
1741 to the memory block directly allocated by memory allocation routine, but
1742 instead lies within such a block allocated for my_figure structure.
1743\layout Standard
1744
1745To solve this problem, the free_struct routine has the additional argument
1746 (besides the intuitive type descriptor and target structure pointers),
1747 which is the flag specifying whether the outer pointer itself must be freed
1748 (0, default) or it should be left intact (non-zero value).
1749\layout LyX-Code
1750
vlm856978f2005-02-21 14:16:10 +00001751
1752\series bold
1753/* 1.
1754 Rectangle_t is defined within my_figure */
vlme18adea2004-06-03 03:49:45 +00001755\layout LyX-Code
1756
vlm856978f2005-02-21 14:16:10 +00001757struct my_figure {
1758\layout LyX-Code
1759
1760 Rectangle_t rect;
1761\layout LyX-Code
1762
1763} *mf =
vlme18adea2004-06-03 03:49:45 +00001764\series bold
1765...
1766\series default
1767;
1768\layout LyX-Code
1769
1770/*
1771\layout LyX-Code
1772
vlm856978f2005-02-21 14:16:10 +00001773 * Freeing the Rectangle_t
vlme18adea2004-06-03 03:49:45 +00001774\layout LyX-Code
1775
vlm856978f2005-02-21 14:16:10 +00001776 * without freeing the mf->rect area
vlme18adea2004-06-03 03:49:45 +00001777\layout LyX-Code
1778
1779 */
1780\layout LyX-Code
1781
vlmba56d852004-09-29 13:29:17 +00001782asn_DEF_Rectangle->free_struct(
vlme18adea2004-06-03 03:49:45 +00001783\layout LyX-Code
1784
vlmba56d852004-09-29 13:29:17 +00001785 &asn_DEF_Rectangle, &mf->rect,
vlm856978f2005-02-21 14:16:10 +00001786\series bold
vlme18adea2004-06-03 03:49:45 +000017871
vlm856978f2005-02-21 14:16:10 +00001788\emph on
vlme18adea2004-06-03 03:49:45 +00001789
vlm856978f2005-02-21 14:16:10 +00001790\emph default
1791/* !free */
1792\series default
1793);
vlme18adea2004-06-03 03:49:45 +00001794\layout LyX-Code
1795
vlm856978f2005-02-21 14:16:10 +00001796
1797\layout LyX-Code
1798
1799
1800\layout LyX-Code
1801
1802
1803\series bold
1804/* 2.
1805 Rectangle_t is a stand-alone pointer */
vlme18adea2004-06-03 03:49:45 +00001806\layout LyX-Code
1807
1808Rectangle_t *rect =
1809\series bold
1810...
1811\series default
1812;
1813\layout LyX-Code
1814
1815/*
1816\layout LyX-Code
1817
1818 * Freeing the Rectangle_t
1819\layout LyX-Code
1820
1821 * and freeing the rect pointer
1822\layout LyX-Code
1823
1824 */
1825\layout LyX-Code
1826
vlmba56d852004-09-29 13:29:17 +00001827asn_DEF_Rectangle->free_struct(
vlme18adea2004-06-03 03:49:45 +00001828\layout LyX-Code
1829
vlmba56d852004-09-29 13:29:17 +00001830 &asn_DEF_Rectangle, rect,
vlm856978f2005-02-21 14:16:10 +00001831\series bold
vlme18adea2004-06-03 03:49:45 +000018320
vlm856978f2005-02-21 14:16:10 +00001833\emph on
1834
vlme18adea2004-06-03 03:49:45 +00001835\emph default
vlm856978f2005-02-21 14:16:10 +00001836/* free the pointer too */
1837\series default
1838);
vlme18adea2004-06-03 03:49:45 +00001839\layout Standard
1840
1841It is safe to invoke the
1842\emph on
1843free_struct
1844\emph default
1845 function with the target structure pointer set to 0 (NULL), the function
1846 will do nothing.
vlma1fd7f92005-01-31 10:10:04 +00001847\layout Chapter
vlm4cb0edf2005-01-14 11:38:49 +00001848
vlmc7737f62005-02-02 09:53:46 +00001849
1850\begin_inset LatexCommand \label{cha:Step-by-step-examples}
1851
1852\end_inset
1853
vlma1fd7f92005-01-31 10:10:04 +00001854Step by step examples
1855\layout Section
1856
1857A
1858\begin_inset Quotes sld
1859\end_inset
1860
1861Rectangle
1862\begin_inset Quotes srd
1863\end_inset
1864
1865 Encoder
1866\layout Standard
1867
vlmc7737f62005-02-02 09:53:46 +00001868This example will help you to create a simple BER and XER encoder of a
vlma1fd7f92005-01-31 10:10:04 +00001869\begin_inset Quotes sld
1870\end_inset
1871
1872Rectangle
1873\begin_inset Quotes srd
1874\end_inset
1875
1876 type used throughout this document.
1877\layout Enumerate
1878
1879Create a file named
1880\series bold
1881rectangle.asn1
1882\series default
1883 with the following contents:
1884\begin_deeper
1885\layout LyX-Code
1886
1887RectangleModule1 DEFINITIONS ::=
1888\layout LyX-Code
1889
1890BEGIN
1891\layout LyX-Code
1892
1893
1894\layout LyX-Code
1895
1896Rectangle ::= SEQUENCE {
1897\layout LyX-Code
1898
1899 height INTEGER,
1900\layout LyX-Code
1901
1902 width INTEGER
1903\layout LyX-Code
1904
1905}
1906\layout LyX-Code
1907
1908
1909\layout LyX-Code
1910
1911END
1912\end_deeper
1913\layout Enumerate
1914
1915Compile it into the set of .c and .h files using asn1c compiler
1916\begin_inset LatexCommand \cite{ASN1C}
1917
1918\end_inset
1919
1920:
1921\begin_deeper
1922\layout LyX-Code
1923
1924
1925\emph on
1926asn1c -fnative-types
1927\series bold
1928\emph default
1929rectangle.asn1
1930\end_deeper
1931\layout Enumerate
1932
1933Alternatively, use the Online ASN.1 compiler
1934\begin_inset LatexCommand \cite{AONL}
1935
1936\end_inset
1937
1938 by uploading the
1939\series bold
1940rectangle.asn1
1941\series default
1942 file into the Web form and unpacking the produced archive on your computer.
1943\layout Enumerate
1944
1945By this time, you should have gotten multiple files in the current directory,
1946 including the
1947\series bold
1948Rectangle.c
1949\series default
1950 and
1951\series bold
1952Rectangle.h
1953\series default
1954.
1955\layout Enumerate
1956
1957Create a main() routine which creates the Rectangle_t structure in memory
1958 and encodes it using BER and XER encoding rules.
1959 Let's name the file
1960\series bold
1961main.c
1962\series default
1963:
1964\begin_inset ERT
1965status Open
1966
1967\layout Standard
1968
1969\backslash
1970clearpage{}
1971\end_inset
1972
1973
1974\begin_deeper
1975\layout LyX-Code
1976
1977
1978\size small
1979#include <stdio.h>
1980\layout LyX-Code
1981
1982
1983\size small
1984#include <sys/types.h>
1985\layout LyX-Code
1986
1987
1988\size small
1989#include <Rectangle.h> /* Rectangle ASN.1 type */
1990\layout LyX-Code
1991
1992
1993\layout LyX-Code
1994
1995
1996\size small
1997/*
1998\layout LyX-Code
1999
2000
2001\size small
2002 * This is a custom function which writes the
2003\layout LyX-Code
2004
2005
2006\size small
2007 * encoded output into some FILE stream.
2008\layout LyX-Code
2009
2010
2011\size small
2012 */
2013\layout LyX-Code
2014
2015
2016\size small
2017static int
2018\layout LyX-Code
2019
2020
2021\size small
2022write_out(const void *buffer, size_t size, void *app_key) {
2023\layout LyX-Code
2024
2025
2026\size small
2027 FILE *out_fp = app_key;
2028\layout LyX-Code
2029
2030
2031\size small
2032 size_t wrote;
2033\layout LyX-Code
2034
2035
2036\size small
2037
2038\layout LyX-Code
2039
2040
2041\size small
2042 wrote = fwrite(buffer, 1, size, out_fp);
2043\layout LyX-Code
2044
2045
2046\size small
2047
2048\layout LyX-Code
2049
2050
2051\size small
2052 return (wrote == size) ? 0 : -1;
2053\layout LyX-Code
2054
2055
2056\size small
2057}
2058\layout LyX-Code
2059
2060
2061\layout LyX-Code
2062
2063
2064\size small
vlm6c37dac2005-01-31 15:06:32 +00002065int main(int ac, char **av) {
vlma1fd7f92005-01-31 10:10:04 +00002066\layout LyX-Code
2067
2068
2069\size small
2070 Rectangle_t *rectangle; /* Type to encode */
2071\layout LyX-Code
2072
2073
2074\size small
2075 asn_enc_rval_t ec; /* Encoder return value */
2076\layout LyX-Code
2077
2078
2079\size small
2080
2081\layout LyX-Code
2082
2083
2084\size small
2085 /* Allocate the Rectangle_t */
2086\layout LyX-Code
2087
2088
2089\size small
vlmc7737f62005-02-02 09:53:46 +00002090 rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */
vlma1fd7f92005-01-31 10:10:04 +00002091\layout LyX-Code
2092
2093
2094\size small
2095 if(!rectangle) {
2096\layout LyX-Code
2097
2098
2099\size small
2100 perror(
2101\begin_inset Quotes sld
2102\end_inset
2103
2104calloc() failed
2105\begin_inset Quotes srd
2106\end_inset
2107
2108);
2109\layout LyX-Code
2110
2111
2112\size small
2113 exit(71); /* better, EX_OSERR */
2114\layout LyX-Code
2115
2116
2117\size small
2118 }
2119\layout LyX-Code
2120
2121
2122\size small
2123
2124\layout LyX-Code
2125
2126
2127\size small
2128 /* Initialize the Rectangle members */
2129\layout LyX-Code
2130
2131
2132\size small
2133 rectangle->height = 42; /* any random value */
2134\layout LyX-Code
2135
2136
2137\size small
2138 rectangle->width = 23; /* any random value */
2139\layout LyX-Code
2140
2141
2142\size small
2143
2144\layout LyX-Code
2145
2146
2147\size small
2148 /* BER encode the data if filename is given */
2149\layout LyX-Code
2150
2151
2152\size small
2153 if(ac < 2) {
2154\layout LyX-Code
2155
2156
2157\size small
2158 fprintf(stderr,
2159\begin_inset Quotes sld
2160\end_inset
2161
2162Specify filename for BER output
2163\backslash
2164n
2165\begin_inset Quotes srd
2166\end_inset
2167
2168);
2169\layout LyX-Code
2170
2171
2172\size small
2173 } else {
2174\layout LyX-Code
2175
2176
2177\size small
2178 const char *filename = av[1];
2179\layout LyX-Code
2180
2181
2182\size small
2183 FILE *fp = fopen(filename,
2184\begin_inset Quotes sld
2185\end_inset
2186
2187w
2188\begin_inset Quotes srd
2189\end_inset
2190
2191); /* for BER output */
2192\layout LyX-Code
2193
2194
2195\layout LyX-Code
2196
2197
2198\size small
2199 if(!fp) {
2200\layout LyX-Code
2201
2202
2203\size small
2204 perror(filename);
2205\layout LyX-Code
2206
2207
2208\size small
2209 exit(71); /* better, EX_OSERR */
2210\layout LyX-Code
2211
2212
2213\size small
2214 }
2215\layout LyX-Code
2216
2217
2218\size small
2219
2220\layout LyX-Code
2221
2222
2223\size small
2224 /* Encode the Rectangle type as BER (DER) */
2225\layout LyX-Code
2226
2227
2228\size small
2229 ec = der_encode(&asn_DEF_Rectangle,
2230\layout LyX-Code
2231
2232
2233\size small
vlm6c37dac2005-01-31 15:06:32 +00002234 rectangle, write_out, fp);
vlma1fd7f92005-01-31 10:10:04 +00002235\layout LyX-Code
2236
2237
2238\size small
2239 fclose(fp);
2240\layout LyX-Code
2241
2242
2243\size small
2244 if(ec.encoded == -1) {
2245\layout LyX-Code
2246
2247
2248\size small
2249 fprintf(stderr,
2250\layout LyX-Code
2251
2252
2253\size small
2254
2255\begin_inset Quotes sld
2256\end_inset
2257
2258Could not encode Rectangle (at %s)
2259\backslash
2260n
2261\begin_inset Quotes srd
2262\end_inset
2263
2264,
2265\layout LyX-Code
2266
2267
2268\size small
2269 ec.failed_type ? ec.failed_type->name :
2270\begin_inset Quotes sld
2271\end_inset
2272
2273unknown
2274\begin_inset Quotes srd
2275\end_inset
2276
2277);
2278\layout LyX-Code
2279
2280
2281\size small
2282 exit(65); /* better, EX_DATAERR */
2283\layout LyX-Code
2284
2285
2286\size small
2287 } else {
2288\layout LyX-Code
2289
2290
2291\size small
2292 fprintf(stderr,
2293\begin_inset Quotes sld
2294\end_inset
2295
2296Created %s with BER encoded Rectangle
2297\backslash
2298n
2299\begin_inset Quotes srd
2300\end_inset
2301
2302,
2303\layout LyX-Code
2304
2305
2306\size small
2307 filename);
2308\layout LyX-Code
2309
2310
2311\size small
2312 }
2313\layout LyX-Code
2314
2315
2316\size small
2317 }
2318\layout LyX-Code
2319
2320
2321\size small
2322
2323\layout LyX-Code
2324
2325
2326\size small
2327 /* Also print the constructed Rectangle XER encoded (XML) */
2328\layout LyX-Code
2329
2330
2331\size small
2332 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
2333\layout LyX-Code
2334
2335
2336\size small
2337
2338\layout LyX-Code
2339
2340
2341\size small
2342 return 0; /* Encoding finished successfully */
2343\layout LyX-Code
2344
2345
2346\size small
2347}
2348\end_deeper
2349\layout Enumerate
2350
2351Compile all files together using C compiler (varies by platform):
2352\begin_deeper
2353\layout LyX-Code
2354
2355
2356\emph on
2357cc -I.
2358 -o
2359\series bold
2360rencode
2361\series default
2362 *.c
2363\end_deeper
2364\layout Enumerate
2365
2366Voila! You have just created the BER and XER encoder of a Rectangle type,
2367 named
2368\series bold
2369rencode
2370\series default
2371!
2372\layout Standard
2373
2374
2375\begin_inset ERT
2376status Collapsed
2377
2378\layout Standard
2379
2380\backslash
2381clearpage{}
2382\end_inset
2383
2384
2385\layout Section
2386
vlmc7737f62005-02-02 09:53:46 +00002387
2388\begin_inset LatexCommand \label{sec:A-Rectangle-Decoder}
2389
2390\end_inset
2391
vlma1fd7f92005-01-31 10:10:04 +00002392A
vlm4cb0edf2005-01-14 11:38:49 +00002393\begin_inset Quotes sld
2394\end_inset
2395
2396Rectangle
2397\begin_inset Quotes srd
2398\end_inset
2399
2400 Decoder
2401\layout Standard
2402
vlmc7737f62005-02-02 09:53:46 +00002403This example will help you to create a simple BER decoder of a simple
vlm4cb0edf2005-01-14 11:38:49 +00002404\begin_inset Quotes sld
2405\end_inset
2406
2407Rectangle
2408\begin_inset Quotes srd
2409\end_inset
2410
2411 type used throughout this document.
2412\layout Enumerate
2413
2414Create a file named
2415\series bold
2416rectangle.asn1
2417\series default
2418 with the following contents:
2419\begin_deeper
2420\layout LyX-Code
2421
2422RectangleModule1 DEFINITIONS ::=
2423\layout LyX-Code
2424
2425BEGIN
2426\layout LyX-Code
2427
2428
2429\layout LyX-Code
2430
2431Rectangle ::= SEQUENCE {
2432\layout LyX-Code
2433
2434 height INTEGER,
2435\layout LyX-Code
2436
2437 width INTEGER
2438\layout LyX-Code
2439
2440}
2441\layout LyX-Code
2442
2443
2444\layout LyX-Code
2445
2446END
2447\end_deeper
2448\layout Enumerate
2449
2450Compile it into the set of .c and .h files using asn1c compiler
2451\begin_inset LatexCommand \cite{ASN1C}
2452
2453\end_inset
2454
2455:
2456\begin_deeper
2457\layout LyX-Code
2458
2459
2460\emph on
2461asn1c -fnative-types
2462\series bold
2463\emph default
2464rectangle.asn1
2465\end_deeper
2466\layout Enumerate
2467
2468Alternatively, use the Online ASN.1 compiler
2469\begin_inset LatexCommand \cite{AONL}
2470
2471\end_inset
2472
2473 by uploading the
2474\series bold
2475rectangle.asn1
2476\series default
vlmabd51e22005-01-17 11:05:04 +00002477 file into the Web form and unpacking the produced archive on your computer.
vlm4cb0edf2005-01-14 11:38:49 +00002478\layout Enumerate
2479
vlmabd51e22005-01-17 11:05:04 +00002480By this time, you should have gotten multiple files in the current directory,
2481 including the
2482\series bold
2483Rectangle.c
2484\series default
2485 and
2486\series bold
2487Rectangle.h
2488\series default
2489.
vlm4cb0edf2005-01-14 11:38:49 +00002490\layout Enumerate
2491
2492Create a main() routine which takes the binary input file, decodes it as
vlmabd51e22005-01-17 11:05:04 +00002493 it were a BER-encoded Rectangle type, and prints out the text (XML) representat
2494ion of the Rectangle type.
vlm4cb0edf2005-01-14 11:38:49 +00002495 Let's name the file
2496\series bold
2497main.c
2498\series default
2499:
vlmabd51e22005-01-17 11:05:04 +00002500\begin_inset ERT
vlmc7737f62005-02-02 09:53:46 +00002501status Collapsed
vlmabd51e22005-01-17 11:05:04 +00002502
2503\layout Standard
2504
2505\backslash
2506clearpage{}
2507\end_inset
2508
2509
vlm4cb0edf2005-01-14 11:38:49 +00002510\begin_deeper
2511\layout LyX-Code
2512
2513
2514\size small
2515#include <stdio.h>
2516\layout LyX-Code
2517
2518
2519\size small
2520#include <sys/types.h>
2521\layout LyX-Code
2522
2523
2524\size small
2525#include <Rectangle.h> /* Rectangle ASN.1 type */
2526\layout LyX-Code
2527
2528
2529\size small
2530
2531\layout LyX-Code
2532
2533
2534\size small
2535int main(int ac, char **av) {
2536\layout LyX-Code
2537
2538
2539\size small
2540 char buf[1024]; /* Temporary buffer */
2541\layout LyX-Code
2542
2543
2544\size small
2545 Rectangle_t *rectangle = 0; /* Type to decode */
2546\layout LyX-Code
2547
2548
2549\size small
2550 asn_dec_rval_t rval; /* Decoder return value */
2551\layout LyX-Code
2552
2553
2554\size small
2555 FILE *fp; /* Input file handler */
2556\layout LyX-Code
2557
2558
2559\size small
2560 size_t size; /* Number of bytes read */
2561\layout LyX-Code
2562
2563
2564\size small
2565 char *filename; /* Input file name */
2566\layout LyX-Code
2567
2568
2569\size small
2570
2571\layout LyX-Code
2572
2573
2574\size small
2575 /* Require a single filename argument */
2576\layout LyX-Code
2577
2578
2579\size small
2580 if(ac != 2) {
2581\layout LyX-Code
2582
2583
2584\size small
2585 fprintf(stderr,
2586\begin_inset Quotes sld
2587\end_inset
2588
2589Usage: %s <file.ber>
2590\backslash
2591n
2592\begin_inset Quotes srd
2593\end_inset
2594
2595, av[0]);
2596\layout LyX-Code
2597
2598
2599\size small
2600 exit(64); /* better, EX_USAGE */
2601\layout LyX-Code
2602
2603
2604\size small
2605 } else {
2606\layout LyX-Code
2607
2608
2609\size small
2610 filename = av[1];
2611\layout LyX-Code
2612
2613
2614\size small
2615 }
2616\layout LyX-Code
2617
2618
2619\size small
2620
2621\layout LyX-Code
2622
2623
2624\size small
2625 /* Open input file as read-only binary */
2626\layout LyX-Code
2627
2628
2629\size small
2630 fp = fopen(filename,
2631\begin_inset Quotes sld
2632\end_inset
2633
2634rb
2635\begin_inset Quotes srd
2636\end_inset
2637
2638);
2639\layout LyX-Code
2640
2641
2642\size small
2643 if(!fp) {
2644\layout LyX-Code
2645
2646
2647\size small
2648 perror(filename);
2649\layout LyX-Code
2650
2651
2652\size small
2653 exit(66); /* better, EX_NOINPUT */
2654\layout LyX-Code
2655
2656
2657\size small
2658 }
2659\layout LyX-Code
2660
2661
2662\size small
2663
2664\layout LyX-Code
2665
2666
2667\size small
2668 /* Read up to the buffer size */
2669\layout LyX-Code
2670
2671
2672\size small
2673 size = fread(buf, 1, sizeof(buf), fp);
2674\layout LyX-Code
2675
2676
2677\size small
2678 fclose(fp);
2679\layout LyX-Code
2680
2681
2682\size small
2683 if(!size) {
2684\layout LyX-Code
2685
2686
2687\size small
2688 fprintf(stderr,
2689\begin_inset Quotes sld
2690\end_inset
2691
2692%s: Empty or broken
2693\backslash
2694n
2695\begin_inset Quotes srd
2696\end_inset
2697
2698, filename);
2699\layout LyX-Code
2700
2701
2702\size small
2703 exit(65); /* better, EX_DATAERR */
2704\layout LyX-Code
2705
2706
2707\size small
2708 }
2709\layout LyX-Code
2710
2711
2712\size small
2713
2714\layout LyX-Code
2715
2716
2717\size small
2718 /* Decode the input buffer as Rectangle type */
2719\layout LyX-Code
2720
2721
2722\size small
2723 rval = ber_decode(0, &asn_DEF_Rectangle,
2724\layout LyX-Code
2725
2726
2727\size small
2728 (void **)&rectangle, buf, size);
2729\layout LyX-Code
2730
2731
2732\size small
2733 if(rval.code != RC_OK) {
2734\layout LyX-Code
2735
2736
2737\size small
2738 fprintf(stderr,
2739\layout LyX-Code
2740
2741
2742\size small
2743
2744\begin_inset Quotes sld
2745\end_inset
2746
2747%s: Broken Rectangle encoding at byte %ld
2748\backslash
2749n
2750\begin_inset Quotes srd
2751\end_inset
2752
2753,
2754\layout LyX-Code
2755
2756
2757\size small
2758 filename, (long)rval.consumed);
2759\layout LyX-Code
2760
2761
2762\size small
2763 exit(65); /* better, EX_DATAERR */
2764\layout LyX-Code
2765
2766
2767\size small
2768 }
2769\layout LyX-Code
2770
2771
2772\size small
2773
2774\layout LyX-Code
2775
2776
2777\size small
2778 /* Print the decoded Rectangle type as XML */
2779\layout LyX-Code
2780
2781
2782\size small
2783 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
2784\layout LyX-Code
2785
2786
2787\size small
2788
2789\layout LyX-Code
2790
2791
2792\size small
2793 return 0; /* Decoding finished successfully */
2794\layout LyX-Code
2795
2796
2797\size small
2798}
2799\end_deeper
2800\layout Enumerate
2801
vlmabd51e22005-01-17 11:05:04 +00002802Compile all files together using C compiler (varies by platform):
vlm4cb0edf2005-01-14 11:38:49 +00002803\begin_deeper
2804\layout LyX-Code
2805
2806
2807\emph on
2808cc -I.
vlmabd51e22005-01-17 11:05:04 +00002809 -o
2810\series bold
2811rdecode
2812\series default
2813 *.c
vlm4cb0edf2005-01-14 11:38:49 +00002814\end_deeper
2815\layout Enumerate
2816
vlma1fd7f92005-01-31 10:10:04 +00002817Voila! You have just created the BER decoder of a Rectangle type, named
2818
vlm4cb0edf2005-01-14 11:38:49 +00002819\series bold
2820rdecode
2821\series default
2822!
vlmc7737f62005-02-02 09:53:46 +00002823\layout Chapter
2824
2825Constraint validation examples
2826\layout Standard
2827
2828This chapter shows how to define ASN.1 constraints and use the generated
2829 validation code.
2830\layout Section
2831
2832Adding constraints into
2833\begin_inset Quotes sld
2834\end_inset
2835
2836Rectangle
2837\begin_inset Quotes srd
2838\end_inset
2839
2840 type
2841\layout Standard
2842
2843This example shows how to add basic constraints to the ASN.1 specification
2844 and how to invoke the constraints validation code in your application.
2845\layout Enumerate
2846
2847Create a file named
2848\series bold
2849rectangle.asn1
2850\series default
2851 with the following contents:
2852\begin_deeper
2853\layout LyX-Code
2854
2855RectangleModuleWithConstraints DEFINITIONS ::=
2856\layout LyX-Code
2857
2858BEGIN
2859\layout LyX-Code
2860
2861
2862\layout LyX-Code
2863
2864Rectangle ::= SEQUENCE {
2865\layout LyX-Code
2866
2867 height INTEGER (0..100), -- Value range constraint
2868\layout LyX-Code
2869
2870 width INTEGER (0..MAX) -- Makes width non-negative
2871\layout LyX-Code
2872
2873}
2874\layout LyX-Code
2875
2876
2877\layout LyX-Code
2878
2879END
2880\end_deeper
2881\layout Enumerate
2882
2883Compile the file according to procedures shown in the previous chapter.
2884\layout Enumerate
2885
2886Modify the Rectangle type processing routine (you can start with the main()
2887 routine shown in the Section
2888\begin_inset LatexCommand \vref{sec:A-Rectangle-Decoder}
2889
2890\end_inset
2891
2892) by placing the following snippet of code
2893\emph on
2894before
2895\emph default
2896 encoding and/or
2897\emph on
2898after
2899\emph default
2900 decoding the Rectangle type
2901\begin_inset Foot
2902collapsed true
2903
2904\layout Standard
2905
2906Placing the constraint checking code
2907\emph on
2908before
2909\emph default
2910 encoding helps to make sure you know the data is correct and within constraints
2911 before sharing the data with anyone else.
2912\layout Standard
2913
2914Placing the constraint checking code
2915\emph on
2916after
2917\emph default
2918 decoding, but before any further action depending on the decoded data,
2919 helps to make sure the application got the valid contents before making
2920 use of it.
2921\end_inset
2922
2923:
2924\begin_inset ERT
2925status Collapsed
2926
2927\layout Standard
2928
2929\backslash
2930clearpage{}
2931\end_inset
2932
2933
2934\begin_deeper
2935\layout LyX-Code
2936
2937
2938\size small
2939int ret; /* Return value */
2940\layout LyX-Code
2941
2942
2943\size small
2944char errbuf[128]; /* Buffer for error message */
2945\layout LyX-Code
2946
2947
2948\size small
2949size_t errlen = sizeof(errbuf); /* Size of the buffer */
2950\layout LyX-Code
2951
2952
2953\size small
2954
2955\layout LyX-Code
2956
2957
2958\size small
2959/* ...
2960 here may go Rectangle decoding code ...
2961 */
2962\layout LyX-Code
2963
2964
2965\size small
2966
2967\layout LyX-Code
2968
2969
2970\size small
2971ret = asn_check_constraints(asn_DEF_Rectangle,
2972\layout LyX-Code
2973
2974
2975\size small
2976 rectangle, errbuf, &errlen);
2977\layout LyX-Code
2978
2979
2980\size small
2981/* assert(errlen < sizeof(errbuf)); // you may rely on that */
2982\layout LyX-Code
2983
2984
2985\size small
2986if(ret) {
2987\layout LyX-Code
2988
2989
2990\size small
2991 fprintf(stderr,
2992\begin_inset Quotes sld
2993\end_inset
2994
2995Constraint validation failed: %s
2996\backslash
2997n
2998\begin_inset Quotes srd
2999\end_inset
3000
3001,
3002\layout LyX-Code
3003
3004
3005\size small
3006 errbuf /* errbuf is properly nul-terminated */
3007\layout LyX-Code
3008
3009
3010\size small
3011 );
3012\layout LyX-Code
3013
3014
3015\size small
3016 /* exit(...); // Replace with appropriate action */
3017\layout LyX-Code
3018
3019
3020\size small
3021}
3022\layout LyX-Code
3023
3024
3025\size small
3026
3027\layout LyX-Code
3028
3029
3030\size small
3031/* ...
3032 here may go Rectangle encoding code ...
3033 */
3034\end_deeper
3035\layout Enumerate
3036
3037Compile the resulting C code as shown in the previous chapters.
3038\layout Enumerate
3039
vlm602b7262005-02-02 11:06:38 +00003040Try to test the constraints checking code by assigning integer value 101
3041 to the
3042\series bold
3043.height
3044\series default
3045 member of the Rectangle structure, or a negative value to the
3046\series bold
3047.width
3048\series default
3049 member.
3050 In either case, the program should print
3051\begin_inset Quotes sld
3052\end_inset
3053
3054Constraint validation failed
3055\begin_inset Quotes srd
3056\end_inset
3057
3058 message, followed by the short explanation why validation did not succeed.
3059\layout Enumerate
3060
vlmc7737f62005-02-02 09:53:46 +00003061Done.
vlm856978f2005-02-21 14:16:10 +00003062\layout Part
3063
3064
3065\begin_inset LatexCommand \label{par:ASN.1-Basics}
3066
3067\end_inset
3068
3069ASN.1 Basics
3070\layout Chapter
3071
3072
3073\begin_inset LatexCommand \label{cha:Abstract-Syntax-Notation:}
3074
3075\end_inset
3076
3077Abstract Syntax Notation: ASN.1
3078\layout Standard
3079
3080
3081\emph on
3082This chapter defines some basic ASN.1 concepts and describes several most
3083 widely used types.
3084 It is by no means an authoritative or complete reference.
3085 For more complete ASN.1 description, please refer to Olivier Dubuisson's
3086 book
3087\begin_inset LatexCommand \cite{Dub00}
3088
3089\end_inset
3090
3091 or the ASN.1 body of standards itself
3092\begin_inset LatexCommand \cite{ITU-T/ASN.1}
3093
3094\end_inset
3095
3096.
3097\layout Standard
3098
3099The Abstract Syntax Notation One is used to formally describe the semantics
3100 of data transmitted across the network.
3101 Two communicating parties may have different formats of their native data
3102 types (i.e.
3103 number of bits in the integer type), thus it is important to have a way
3104 to describe the data in a manner which is independent from the particular
3105 machine's representation.
3106 The ASN.1 specifications are used to achieve the following:
3107\layout Itemize
3108
3109The specification expressed in the ASN.1 notation is a formal and precise
3110 way to communicate the data semantics to human readers;
3111\layout Itemize
3112
3113The ASN.1 specifications may be used as input for automatic compilers which
3114 produce the code for some target language (C, C++, Java, etc) to encode
3115 and decode the data according to some encoding rules (which are also defined
3116 by the ASN.1 standard).
3117\layout Standard
3118
3119Consider the following example:
3120\layout LyX-Code
3121
3122Rectangle ::= SEQUENCE {
3123\layout LyX-Code
3124
3125 height INTEGER,
3126\layout LyX-Code
3127
3128 width INTEGER
3129\layout LyX-Code
3130
3131}
3132\layout Standard
3133
3134This ASN.1 specification describes a constructed type,
3135\emph on
3136Rectangle
3137\emph default
3138, containing two integer fields.
3139 This specification may tell the reader that there exists this kind of data
3140 structure and that some entity may be prepared to send or receive it.
3141 The question on
3142\emph on
3143how
3144\emph default
3145 that entity is going to send or receive the
3146\emph on
3147encoded data
3148\emph default
3149 is outside the scope of ASN.1.
3150 For example, this data structure may be encoded according to some encoding
3151 rules and sent to the destination using the TCP protocol.
3152 The ASN.1 specifies several ways of encoding (or
3153\begin_inset Quotes sld
3154\end_inset
3155
3156serializing
3157\begin_inset Quotes srd
3158\end_inset
3159
3160, or
3161\begin_inset Quotes sld
3162\end_inset
3163
3164marshaling
3165\begin_inset Quotes srd
3166\end_inset
3167
3168) the data: BER, PER, XER and others, including CER and DER derivatives
3169 from BER.
3170\layout Standard
3171
3172The complete specification must be wrapped in a module, which looks like
3173 this:
3174\layout LyX-Code
3175
3176RectangleModule1
3177\layout LyX-Code
3178
3179 { iso org(3) dod(6) internet(1) private(4)
3180\layout LyX-Code
3181
3182 enterprise(1) spelio(9363) software(1)
3183\layout LyX-Code
3184
3185 asn1c(5) docs(2) rectangle(1) 1 }
3186\layout LyX-Code
3187
3188 DEFINITIONS AUTOMATIC TAGS ::=
3189\layout LyX-Code
3190
3191BEGIN
3192\layout LyX-Code
3193
3194
3195\layout LyX-Code
3196
3197-- This is a comment which describes nothing.
3198\layout LyX-Code
3199
3200Rectangle ::= SEQUENCE {
3201\layout LyX-Code
3202
3203 height INTEGER, -- Height of the rectangle
3204\layout LyX-Code
3205
3206 width INTEGER -- Width of the rectangle
3207\layout LyX-Code
3208
3209}
3210\layout LyX-Code
3211
3212
3213\layout LyX-Code
3214
3215END
3216\layout Standard
3217
3218The module header consists of module name (RectangleModule1), the module
3219 object identifier ({...}), a keyword
3220\begin_inset Quotes sld
3221\end_inset
3222
3223DEFINITIONS
3224\begin_inset Quotes srd
3225\end_inset
3226
3227, a set of module flags (AUTOMATIC TAGS) and
3228\begin_inset Quotes sld
3229\end_inset
3230
3231::= BEGIN
3232\begin_inset Quotes srd
3233\end_inset
3234
3235.
3236 The module ends with an
3237\begin_inset Quotes sld
3238\end_inset
3239
3240END
3241\begin_inset Quotes srd
3242\end_inset
3243
3244 statement.
3245\layout Section
3246
3247Some of the ASN.1 Basic Types
3248\layout Subsection
3249
3250The BOOLEAN type
3251\layout Standard
3252
3253The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF or
3254 a similar kind of two-way choice.
3255\layout Subsection
3256
3257The INTEGER type
3258\layout Standard
3259
3260The INTEGER type is a signed natural number type without any restrictions
3261 on its size.
3262 If the automatic checking on INTEGER value bounds are necessary, the subtype
3263 constraints must be used.
3264\layout LyX-Code
3265
3266SimpleInteger ::= INTEGER
3267\layout LyX-Code
3268
3269
3270\layout LyX-Code
3271
3272-- An integer with a very limited range
3273\layout LyX-Code
3274
3275SmallPositiveInt ::= INTEGER (0..127)
3276\layout LyX-Code
3277
3278
3279\layout LyX-Code
3280
3281-- Integer, negative
3282\layout LyX-Code
3283
3284NegativeInt ::= INTEGER (MIN..0)
3285\layout Subsection
3286
3287The ENUMERATED type
3288\layout Standard
3289
3290The ENUMERATED type is semantically equivalent to the INTEGER type with
3291 some integer values explicitly named.
3292\layout LyX-Code
3293
3294FruitId ::= ENUMERATED { apple(1), orange(2) }
3295\layout LyX-Code
3296
3297
3298\layout LyX-Code
3299
3300-- The numbers in braces are optional,
3301\layout LyX-Code
3302
3303-- the enumeration can be performed
3304\layout LyX-Code
3305
3306-- automatically by the compiler
3307\layout LyX-Code
3308
3309ComputerOSType ::= ENUMERATED {
3310\layout LyX-Code
3311
3312 FreeBSD, -- acquires value 0
3313\layout LyX-Code
3314
3315 Windows, -- acquires value 1
3316\layout LyX-Code
3317
3318 Solaris(5), -- remains 5
3319\layout LyX-Code
3320
3321 Linux, -- becomes 6
3322\layout LyX-Code
3323
3324 MacOS -- becomes 7
3325\layout LyX-Code
3326
3327}
3328\layout Subsection
3329
3330The OCTET STRING type
3331\layout Standard
3332
3333This type models the sequence of 8-bit bytes.
3334 This may be used to transmit some opaque data or data serialized by other
3335 types of encoders (i.e.
3336 video file, photo picture, etc).
3337\layout Subsection
3338
3339The OBJECT IDENTIFIER type
3340\layout Standard
3341
3342The OBJECT IDENTIFIER is used to represent the unique identifier of any
3343 object, starting from the very root of the registration tree.
3344 If your organization needs to uniquely identify something (a router, a
3345 room, a person, a standard, or whatever), you are encouraged to get your
3346 own identification subtree at
3347\begin_inset LatexCommand \htmlurl{http://www.iana.org/protocols/forms.htm}
3348
3349\end_inset
3350
3351.
3352\layout Standard
3353
3354For example, the very first ASN.1 module in this Chapter (RectangleModule1)
3355 has the following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
3356\layout LyX-Code
3357
3358ExampleOID ::= OBJECT IDENTIFIER
3359\layout LyX-Code
3360
3361
3362\layout LyX-Code
3363
3364rectangleModule1-oid ExampleOID
3365\layout LyX-Code
3366
3367 ::= { 1 3 6 1 4 1 9363 1 5 2 1 1 }
3368\layout LyX-Code
3369
3370
3371\layout LyX-Code
3372
3373-- An identifier of the Internet.
3374\layout LyX-Code
3375
3376internet-id OBJECT IDENTIFIER
3377\layout LyX-Code
3378
3379 ::= { iso(1) identified-organization(3)
3380\layout LyX-Code
3381
3382 dod(6) internet(1) }
3383\layout Standard
3384
3385As you see, names are optional.
3386\layout Subsection
3387
3388The RELATIVE-OID type
3389\layout Standard
3390
3391The RELATIVE-OID type has the semantics of a subtree of an OBJECT IDENTIFIER.
3392 There may be no need to repeat the whole sequence of numbers from the root
3393 of the registration tree where the only thing of interest is some of the
3394 tree's subsequence.
3395\layout LyX-Code
3396
3397this-document RELATIVE-OID ::= { docs(2) usage(1) }
3398\layout LyX-Code
3399
3400
3401\layout LyX-Code
3402
3403this-example RELATIVE-OID ::= {
3404\layout LyX-Code
3405
3406 this-document assorted-examples(0) this-example(1) }
3407\layout Section
3408
3409Some of the ASN.1 String Types
3410\layout Subsection
3411
3412The IA5String type
3413\layout Standard
3414
3415This is essentially the ASCII, with 128 character codes available (7 lower
3416 bits of an 8-bit byte).
3417\layout Subsection
3418
3419The UTF8String type
3420\layout Standard
3421
3422This is the character string which encodes the full Unicode range (4 bytes)
3423 using multibyte character sequences.
3424\layout Subsection
3425
3426The NumericString type
3427\layout Standard
3428
3429This type represents the character string with the alphabet consisting of
3430 numbers (
3431\begin_inset Quotes sld
3432\end_inset
3433
34340
3435\begin_inset Quotes srd
3436\end_inset
3437
3438 to
3439\begin_inset Quotes sld
3440\end_inset
3441
34429
3443\begin_inset Quotes srd
3444\end_inset
3445
3446) and a space.
3447\layout Subsection
3448
3449The PrintableString type
3450\layout Standard
3451
3452The character string with the following alphabet: space,
3453\begin_inset Quotes sld
3454\end_inset
3455
3456
3457\series bold
3458'
3459\series default
3460
3461\begin_inset Quotes srd
3462\end_inset
3463
3464 (single quote),
3465\begin_inset Quotes sld
3466\end_inset
3467
3468
3469\series bold
3470(
3471\series default
3472
3473\begin_inset Quotes sld
3474\end_inset
3475
3476,
3477\begin_inset Quotes sld
3478\end_inset
3479
3480
3481\series bold
3482)
3483\series default
3484
3485\begin_inset Quotes srd
3486\end_inset
3487
3488,
3489\begin_inset Quotes sld
3490\end_inset
3491
3492
3493\series bold
3494+
3495\series default
3496
3497\begin_inset Quotes srd
3498\end_inset
3499
3500,
3501\begin_inset Quotes sld
3502\end_inset
3503
3504
3505\series bold
3506,
3507\series default
3508
3509\begin_inset Quotes srd
3510\end_inset
3511
3512 (comma),
3513\begin_inset Quotes sld
3514\end_inset
3515
3516
3517\series bold
3518-
3519\series default
3520
3521\begin_inset Quotes srd
3522\end_inset
3523
3524,
3525\begin_inset Quotes sld
3526\end_inset
3527
3528
3529\series bold
3530.
3531\series default
3532
3533\begin_inset Quotes srd
3534\end_inset
3535
3536,
3537\begin_inset Quotes sld
3538\end_inset
3539
3540
3541\series bold
3542/
3543\series default
3544
3545\begin_inset Quotes srd
3546\end_inset
3547
3548, digits (
3549\begin_inset Quotes sld
3550\end_inset
3551
35520
3553\begin_inset Quotes srd
3554\end_inset
3555
3556 to
3557\begin_inset Quotes sld
3558\end_inset
3559
35609
3561\begin_inset Quotes srd
3562\end_inset
3563
3564),
3565\begin_inset Quotes sld
3566\end_inset
3567
3568
3569\series bold
3570:
3571\series default
3572
3573\begin_inset Quotes srd
3574\end_inset
3575
3576,
3577\begin_inset Quotes sld
3578\end_inset
3579
3580
3581\series bold
3582=
3583\series default
3584
3585\begin_inset Quotes srd
3586\end_inset
3587
3588,
3589\begin_inset Quotes sld
3590\end_inset
3591
3592
3593\series bold
3594?
3595\series default
3596
3597\begin_inset Quotes srd
3598\end_inset
3599
3600, upper-case and lower-case letters (
3601\begin_inset Quotes sld
3602\end_inset
3603
3604A
3605\begin_inset Quotes srd
3606\end_inset
3607
3608 to
3609\begin_inset Quotes sld
3610\end_inset
3611
3612Z
3613\begin_inset Quotes srd
3614\end_inset
3615
3616 and
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).
3633\layout Subsection
3634
3635The VisibleString type
3636\layout Standard
3637
3638The character string with the alphabet which is more or less a subset of
3639 ASCII between the space and the
3640\begin_inset Quotes sld
3641\end_inset
3642
3643
3644\series bold
3645~
3646\series default
3647
3648\begin_inset Quotes srd
3649\end_inset
3650
3651 symbol (tilde).
3652\layout Standard
3653
3654Alternatively, the alphabet may be described as the PrintableString alphabet
3655 presented earlier, plus the following characters:
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,
3668\begin_inset Quotes sld
3669\end_inset
3670
3671
3672\series bold
3673
3674\begin_inset Quotes srd
3675\end_inset
3676
3677
3678\series default
3679
3680\begin_inset Quotes srd
3681\end_inset
3682
3683,
3684\begin_inset Quotes sld
3685\end_inset
3686
3687
3688\series bold
3689#
3690\series default
3691
3692\begin_inset Quotes srd
3693\end_inset
3694
3695,
3696\begin_inset Quotes sld
3697\end_inset
3698
3699
3700\series bold
3701$
3702\series default
3703
3704\begin_inset Quotes srd
3705\end_inset
3706
3707,
3708\begin_inset Quotes sld
3709\end_inset
3710
3711
3712\series bold
3713%
3714\series default
3715
3716\begin_inset Quotes srd
3717\end_inset
3718
3719,
3720\begin_inset Quotes sld
3721\end_inset
3722
3723
3724\series bold
3725&
3726\series default
3727
3728\begin_inset Quotes srd
3729\end_inset
3730
3731,
3732\begin_inset Quotes sld
3733\end_inset
3734
3735
3736\series bold
3737*
3738\series default
3739
3740\begin_inset Quotes srd
3741\end_inset
3742
3743,
3744\begin_inset Quotes sld
3745\end_inset
3746
3747
3748\series bold
3749;
3750\series default
3751
3752\begin_inset Quotes srd
3753\end_inset
3754
3755,
3756\begin_inset Quotes sld
3757\end_inset
3758
3759
3760\series bold
3761<
3762\series default
3763
3764\begin_inset Quotes srd
3765\end_inset
3766
3767,
3768\begin_inset Quotes sld
3769\end_inset
3770
3771
3772\series bold
3773>
3774\series default
3775
3776\begin_inset Quotes srd
3777\end_inset
3778
3779,
3780\begin_inset Quotes sld
3781\end_inset
3782
3783
3784\series bold
3785[
3786\series default
3787
3788\begin_inset Quotes srd
3789\end_inset
3790
3791,
3792\begin_inset Quotes sld
3793\end_inset
3794
3795
3796\series bold
3797
3798\backslash
3799
3800\series default
3801
3802\begin_inset Quotes srd
3803\end_inset
3804
3805,
3806\begin_inset Quotes sld
3807\end_inset
3808
3809
3810\series bold
3811]
3812\series default
3813
3814\begin_inset Quotes srd
3815\end_inset
3816
3817,
3818\begin_inset Quotes sld
3819\end_inset
3820
3821
3822\series bold
3823^
3824\series default
3825
3826\begin_inset Quotes srd
3827\end_inset
3828
3829,
3830\begin_inset Quotes sld
3831\end_inset
3832
3833
3834\series bold
3835_
3836\series default
3837
3838\begin_inset Quotes srd
3839\end_inset
3840
3841,
3842\begin_inset Quotes sld
3843\end_inset
3844
3845
3846\series bold
3847`
3848\series default
3849
3850\begin_inset Quotes srd
3851\end_inset
3852
3853 (single left quote),
3854\begin_inset Quotes sld
3855\end_inset
3856
3857
3858\series bold
3859{
3860\series default
3861
3862\begin_inset Quotes srd
3863\end_inset
3864
3865,
3866\begin_inset Quotes sld
3867\end_inset
3868
3869
3870\series bold
3871|
3872\series default
3873
3874\begin_inset Quotes srd
3875\end_inset
3876
3877,
3878\begin_inset Quotes sld
3879\end_inset
3880
3881
3882\series bold
3883}
3884\series default
3885
3886\begin_inset Quotes srd
3887\end_inset
3888
3889,
3890\begin_inset Quotes sld
3891\end_inset
3892
3893
3894\series bold
3895~
3896\series default
3897
3898\begin_inset Quotes srd
3899\end_inset
3900
3901.
3902\layout Section
3903
3904ASN.1 Constructed Types
3905\layout Subsection
3906
3907The SEQUENCE type
3908\layout Standard
3909
3910This is an ordered collection of other simple or constructed types.
3911 The SEQUENCE constructed type resembles the C
3912\begin_inset Quotes sld
3913\end_inset
3914
3915struct
3916\begin_inset Quotes srd
3917\end_inset
3918
3919 statement.
3920\layout LyX-Code
3921
3922Address ::= SEQUENCE {
3923\layout LyX-Code
3924
3925 -- The apartment number may be omitted
3926\layout LyX-Code
3927
3928 apartmentNumber NumericString OPTIONAL,
3929\layout LyX-Code
3930
3931 streetName PrintableString,
3932\layout LyX-Code
3933
3934 cityName PrintableString,
3935\layout LyX-Code
3936
3937 stateName PrintableString,
3938\layout LyX-Code
3939
3940 -- This one may be omitted too
3941\layout LyX-Code
3942
3943 zipNo NumericString OPTIONAL
3944\layout LyX-Code
3945
3946}
3947\layout Subsection
3948
3949The SET type
3950\layout Standard
3951
3952This is a collection of other simple or constructed types.
3953 Ordering is not important.
3954 The data may arrive in the order which is different from the order of specifica
3955tion.
3956 Data is encoded in the order not necessarily corresponding to the order
3957 of specification.
3958\layout Subsection
3959
3960The CHOICE type
3961\layout Standard
3962
3963This type is just a choice between the subtypes specified in it.
3964 The CHOICE type contains at most one of the subtypes specified, and it
3965 is always implicitly known which choice is being decoded or encoded.
3966 This one resembles the C
3967\begin_inset Quotes sld
3968\end_inset
3969
3970union
3971\begin_inset Quotes srd
3972\end_inset
3973
3974 statement.
3975\layout Standard
3976
3977The following type defines a response code, which may be either an integer
3978 code or a boolean
3979\begin_inset Quotes sld
3980\end_inset
3981
3982true
3983\begin_inset Quotes srd
3984\end_inset
3985
3986/
3987\begin_inset Quotes srd
3988\end_inset
3989
3990false
3991\begin_inset Quotes srd
3992\end_inset
3993
3994 code.
3995\layout LyX-Code
3996
3997ResponseCode ::= CHOICE {
3998\layout LyX-Code
3999
4000 intCode INTEGER,
4001\layout LyX-Code
4002
4003 boolCode BOOLEAN
4004\layout LyX-Code
4005
4006}
4007\layout LyX-Code
4008
4009\layout Subsection
4010
4011The SEQUENCE OF type
4012\layout Standard
4013
4014This one is the list (array) of simple or constructed types:
4015\layout LyX-Code
4016
4017-- Example 1
4018\layout LyX-Code
4019
4020ManyIntegers ::= SEQUENCE OF INTEGER
4021\layout LyX-Code
4022
4023
4024\layout LyX-Code
4025
4026-- Example 2
4027\layout LyX-Code
4028
4029ManyRectangles ::= SEQUENCE OF Rectangle
4030\layout LyX-Code
4031
4032
4033\layout LyX-Code
4034
4035-- More complex example:
4036\layout LyX-Code
4037
4038-- an array of structures defined in place.
4039\layout LyX-Code
4040
4041ManyCircles ::= SEQUENCE OF SEQUENCE {
4042\layout LyX-Code
4043
4044 radius INTEGER
4045\layout LyX-Code
4046
4047 }
4048\layout Subsection
4049
4050The SET OF type
4051\layout Standard
4052
4053The SET OF type models the bag of structures.
4054 It resembles the SEQUENCE OF type, but the order is not important: i.e.
4055 the elements may arrive in the order which is not necessarily the same
4056 as the in-memory order on the remote machines.
4057\layout LyX-Code
4058
4059-- A set of structures defined elsewhere
4060\layout LyX-Code
4061
4062SetOfApples :: SET OF Apple
4063\layout LyX-Code
4064
4065
4066\layout LyX-Code
4067
4068-- Set of integers encoding the kind of a fruit
4069\layout LyX-Code
4070
4071FruitBag ::= SET OF ENUMERATED { apple, orange }
vlme18adea2004-06-03 03:49:45 +00004072\layout Bibliography
vlmfcec8e52004-08-23 15:12:04 +00004073\bibitem [ASN1C]{ASN1C}
4074
vlmc7737f62005-02-02 09:53:46 +00004075The Open Source ASN.1 Compiler.
vlmfcec8e52004-08-23 15:12:04 +00004076
vlmc7737f62005-02-02 09:53:46 +00004077\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c}
vlmfcec8e52004-08-23 15:12:04 +00004078
4079\end_inset
4080
4081
4082\layout Bibliography
vlm4cb0edf2005-01-14 11:38:49 +00004083\bibitem [AONL]{AONL}
4084
4085Online ASN.1 Compiler.
4086
4087\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c/asn1c.cgi}
4088
4089\end_inset
4090
4091
4092\layout Bibliography
vlme18adea2004-06-03 03:49:45 +00004093\bibitem [Dub00]{Dub00}
4094
vlmaf2155e2004-10-20 15:48:55 +00004095Olivier Dubuisson ---
vlme18adea2004-06-03 03:49:45 +00004096\emph on
4097ASN.1 Communication between heterogeneous systems
4098\emph default
vlmaf2155e2004-10-20 15:48:55 +00004099 --- Morgan Kaufmann Publishers, 2000.
vlme18adea2004-06-03 03:49:45 +00004100
4101\begin_inset LatexCommand \htmlurl{http://asn1.elibel.tm.fr/en/book/}
4102
4103\end_inset
4104
4105.
4106 ISBN:0-12-6333361-0.
4107\layout Bibliography
4108\bibitem [ITU-T/ASN.1]{ITU-T/ASN.1}
4109
4110ITU-T Study Group 17 -- Languages for Telecommunication Systems
4111\begin_inset LatexCommand \url{http://www.itu.int/ITU-T/studygroups/com17/languages/}
4112
4113\end_inset
4114
4115
4116\the_end