blob: 012503c0bf7e8454d67ac019a615049b212bd8d1 [file] [log] [blame]
Lev Walkin84291e02004-06-03 03:49:45 +00001#LyX 1.3 created this file. For more info see http://www.lyx.org/
2\lyxformat 221
3\textclass book
Lev Walkin26587ab2004-08-23 15:12:04 +00004\begin_preamble
Lev Walkin26587ab2004-08-23 15:12:04 +00005\usepackage{extramarks}
6\lhead{\firstxmark}
7\rfoot{\lastxmark}
Lev Walkind3630792004-09-30 22:26:19 +00008\usepackage{color}
9\definecolor{gray40}{gray}{.4}
10\definecolor{urlblue}{rgb}{0,0,.6}
11\usepackage[colorlinks=true,
12linkcolor={gray40},
13urlcolor={urlblue},
14pdfauthor={Lev Walkin},
15pdftitle={Using the OpenSource ASN.1 Compiler},
16pdfkeywords={ASN.1,asn1c,compiler}
17]{hyperref}
18%\fancyhf{}
19%\fancyhead[LE,RO]{\thepage}
20%\fancyhead[LO]{\rightmark}
21%\fancyhead[RE]{\leftmark}
22%\fancyfoot[R]{\lastxmark}
Lev Walkin26587ab2004-08-23 15:12:04 +000023\end_preamble
Lev Walkin84291e02004-06-03 03:49:45 +000024\language english
25\inputencoding latin1
Lev Walkin26587ab2004-08-23 15:12:04 +000026\fontscheme times
Lev Walkin84291e02004-06-03 03:49:45 +000027\graphics default
28\paperfontsize default
29\spacing single
30\papersize Default
31\paperpackage a4
32\use_geometry 0
33\use_amsmath 0
34\use_natbib 0
35\use_numerical_citations 0
36\paperorientation portrait
Lev Walkind3630792004-09-30 22:26:19 +000037\secnumdepth 2
38\tocdepth 2
Lev Walkin84291e02004-06-03 03:49:45 +000039\paragraph_separation indent
40\defskip medskip
41\quotes_language swedish
42\quotes_times 2
43\papercolumns 1
Lev Walkin26587ab2004-08-23 15:12:04 +000044\papersides 2
45\paperpagestyle fancy
Lev Walkin84291e02004-06-03 03:49:45 +000046
47\layout Title
48
Lev Walkind3630792004-09-30 22:26:19 +000049Using the OpenSource ASN.1 Compiler
Lev Walkin84291e02004-06-03 03:49:45 +000050\layout Author
51
52Lev Walkin <
Lev Walkind3630792004-09-30 22:26:19 +000053\begin_inset ERT
54status Collapsed
Lev Walkin84291e02004-06-03 03:49:45 +000055
Lev Walkind3630792004-09-30 22:26:19 +000056\layout Standard
57
58\backslash
59href{mailto:vlm@lionet.info?Subject=asn1c}{vlm@lionet.info}
Lev Walkin84291e02004-06-03 03:49:45 +000060\end_inset
61
62>
63\layout Standard
64
65
Lev Walkin26587ab2004-08-23 15:12:04 +000066\begin_inset ERT
67status Open
68
69\layout Standard
70
71\backslash
Lev Walkind3630792004-09-30 22:26:19 +000072lhead{Document describes
73\backslash
74href{http://lionet.info/soft/asn1c-0.9.6.tar.gz}{asn1c-0.9.6}}
75\layout Standard
76
77\backslash
78rhead{$Revision$}
Lev Walkin26587ab2004-08-23 15:12:04 +000079\end_inset
80
81
82\layout Standard
83
84
Lev Walkin84291e02004-06-03 03:49:45 +000085\begin_inset LatexCommand \tableofcontents{}
86
87\end_inset
88
89
Lev Walkin26587ab2004-08-23 15:12:04 +000090\layout Standard
91
92
93\begin_inset ERT
Lev Walkind3630792004-09-30 22:26:19 +000094status Open
Lev Walkin26587ab2004-08-23 15:12:04 +000095
96\layout Standard
97
98\backslash
99pagestyle{headings}
100\end_inset
101
102
Lev Walkin4a83dd52004-08-07 05:00:07 +0000103\layout Part
104
105ASN.1 Basics
Lev Walkin84291e02004-06-03 03:49:45 +0000106\layout Chapter
107
Lev Walkin4a83dd52004-08-07 05:00:07 +0000108Abstract Syntax Notation: ASN.1
Lev Walkin84291e02004-06-03 03:49:45 +0000109\layout Standard
110
111
112\emph on
113This chapter defines some basic ASN.1 concepts and describes several most
114 widely used types.
115 It is by no means an authoritative or complete reference.
116 For more complete ASN.1 description, please refer to Olivier Dubuisson's
117 book
118\begin_inset LatexCommand \cite{Dub00}
119
120\end_inset
121
Lev Walkin4a83dd52004-08-07 05:00:07 +0000122 or the ASN.1 body of standards itself
Lev Walkin84291e02004-06-03 03:49:45 +0000123\begin_inset LatexCommand \cite{ITU-T/ASN.1}
124
125\end_inset
126
127.
128\layout Standard
129
130The Abstract Syntax Notation One is used to formally describe the semantics
131 of data transmitted across the network.
132 Two communicating parties may have different formats of their native data
133 types (i.e.
134 number of bits in the integer type), thus it is important to have a way
135 to describe the data in a manner which is independent from the particular
136 machine's representation.
137 The ASN.1 specifications is used to achieve one or more of the following:
138\layout Itemize
139
140The specification expressed in the ASN.1 notation is a formal and precise
141 way to communicate the data semantics to human readers;
142\layout Itemize
143
144The ASN.1 specifications may be used as input for automatic compilers which
145 produce the code for some target language (C, C++, Java, etc) to encode
146 and decode the data according to some encoding rules (which are also defined
147 by the ASN.1 standard).
148\layout Standard
149
150Consider the following example:
151\layout LyX-Code
152
153Rectangle ::= SEQUENCE {
154\layout LyX-Code
155
156 height INTEGER,
157\layout LyX-Code
158
159 width INTEGER
160\layout LyX-Code
161
162}
163\layout Standard
164
165This ASN.1 specification describes a constructed type,
166\emph on
167Rectangle
168\emph default
169, containing two integer fields.
170 This specification may tell the reader that there is this kind of data
171 structure and that some entity may be prepared to send or receive it.
172 The question on
173\emph on
174how
175\emph default
176 that entity is going to send or receive the
177\emph on
178encoded data
179\emph default
180 is outside the scope of ASN.1.
181 For example, this data structure may be encoded according to some encoding
182 rules and sent to the destination using the TCP protocol.
183 The ASN.1 specifies several ways of encoding (or
184\begin_inset Quotes sld
185\end_inset
186
187serializing
188\begin_inset Quotes srd
189\end_inset
190
191, or
192\begin_inset Quotes sld
193\end_inset
194
195marshaling
196\begin_inset Quotes srd
197\end_inset
198
199) the data: BER, CER, DER and XER, some of them which will be described
200 later.
201\layout Standard
202
203The complete specification must be wrapped in a module, which looks like
204 this:
205\layout LyX-Code
206
207UsageExampleModule1
208\layout LyX-Code
209
210 { iso org(3) dod(6) internet(1) private(4)
211\layout LyX-Code
212
213 enterprise(1) spelio(9363) software(1)
214\layout LyX-Code
215
216 asn1c(5) docs(2) usage(1) 1 }
217\layout LyX-Code
218
Lev Walkin26587ab2004-08-23 15:12:04 +0000219 DEFINITIONS AUTOMATIC TAGS ::=
Lev Walkin84291e02004-06-03 03:49:45 +0000220\layout LyX-Code
221
222BEGIN
223\layout LyX-Code
224
225
226\layout LyX-Code
227
228-- This is a comment which describes nothing.
229\layout LyX-Code
230
231Rectangle ::= SEQUENCE {
232\layout LyX-Code
233
234 height INTEGER, -- Height of the rectangle
235\layout LyX-Code
236
Lev Walkin26587ab2004-08-23 15:12:04 +0000237 width INTEGER -- Width of the rectangle
Lev Walkin84291e02004-06-03 03:49:45 +0000238\layout LyX-Code
239
240}
241\layout LyX-Code
242
243
244\layout LyX-Code
245
246END
247\layout Standard
248
249The module header consists of module name (UsageExampleModule1), the module
Lev Walkin26587ab2004-08-23 15:12:04 +0000250 object identifier ({...}), a keyword
Lev Walkin84291e02004-06-03 03:49:45 +0000251\begin_inset Quotes sld
252\end_inset
253
Lev Walkin26587ab2004-08-23 15:12:04 +0000254DEFINITIONS
255\begin_inset Quotes srd
256\end_inset
257
258, a set of module flags (AUTOMATIC TAGS) and
259\begin_inset Quotes sld
260\end_inset
261
262::= BEGIN
Lev Walkin84291e02004-06-03 03:49:45 +0000263\begin_inset Quotes srd
264\end_inset
265
266.
267 The module ends with an
268\begin_inset Quotes sld
269\end_inset
270
271END
272\begin_inset Quotes srd
273\end_inset
274
275 statement.
276\layout Section
277
278Some of the ASN.1 Basic Types
279\layout Subsection
280
281The BOOLEAN type
282\layout Standard
283
284The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF or
285 a similar kind of two-way choice.
286\layout Subsection
287
288The INTEGER type
289\layout Standard
290
291The INTEGER type is a signed natural number type without any restrictions
292 on its size.
293 If the automatic checking on INTEGER value bounds are necessary, the subtype
294 constraints must be used.
295\layout LyX-Code
296
297SimpleInteger ::= INTEGER
298\layout LyX-Code
299
Lev Walkin26587ab2004-08-23 15:12:04 +0000300
301\layout LyX-Code
302
Lev Walkin84291e02004-06-03 03:49:45 +0000303-- An integer with a very limited range
304\layout LyX-Code
305
Lev Walkin1f756282004-09-29 13:29:17 +0000306SmallPositiveInt ::= INTEGER (0..127)
Lev Walkin84291e02004-06-03 03:49:45 +0000307\layout LyX-Code
308
Lev Walkin26587ab2004-08-23 15:12:04 +0000309
310\layout LyX-Code
311
Lev Walkin84291e02004-06-03 03:49:45 +0000312-- Integer, negative
313\layout LyX-Code
314
315NegativeInt ::= INTEGER (MIN..0)
316\layout Subsection
317
318The ENUMERATED type
319\layout Standard
320
321The ENUMERATED type is semantically equivalent to the INTEGER type with
322 some integer values explicitly named.
323\layout LyX-Code
324
325FruitId ::= ENUMERATED { apple(1), orange(2) }
326\layout LyX-Code
327
Lev Walkin26587ab2004-08-23 15:12:04 +0000328
329\layout LyX-Code
330
Lev Walkin84291e02004-06-03 03:49:45 +0000331-- The numbers in braces are optional,
332\layout LyX-Code
333
Lev Walkin26587ab2004-08-23 15:12:04 +0000334-- the enumeration can be performed
Lev Walkin84291e02004-06-03 03:49:45 +0000335\layout LyX-Code
336
337-- automatically by the compiler
338\layout LyX-Code
339
340ComputerOSType ::= ENUMERATED {
341\layout LyX-Code
342
Lev Walkind3630792004-09-30 22:26:19 +0000343 FreeBSD, -- acquires value 0
Lev Walkin84291e02004-06-03 03:49:45 +0000344\layout LyX-Code
345
Lev Walkind3630792004-09-30 22:26:19 +0000346 Windows, -- acquires value 1
Lev Walkin84291e02004-06-03 03:49:45 +0000347\layout LyX-Code
348
Lev Walkind3630792004-09-30 22:26:19 +0000349 Solaris(5), -- remains 5
Lev Walkin84291e02004-06-03 03:49:45 +0000350\layout LyX-Code
351
Lev Walkind3630792004-09-30 22:26:19 +0000352 Linux, -- becomes 6
Lev Walkin84291e02004-06-03 03:49:45 +0000353\layout LyX-Code
354
Lev Walkind3630792004-09-30 22:26:19 +0000355 MacOS -- becomes 7
Lev Walkin84291e02004-06-03 03:49:45 +0000356\layout LyX-Code
357
358}
359\layout Subsection
360
361The OCTET STRING type
362\layout Standard
363
364This type models the sequence of 8-bit bytes.
365 This may be used to transmit some opaque data or data serialized by other
366 types of encoders (i.e.
367 video file, photo picture, etc).
368\layout Subsection
369
370The OBJECT IDENTIFIER type
371\layout Standard
372
373The OBJECT IDENTIFIER is used to represent the unique identifier of any
374 object, starting from the very root of the registration tree.
375 If your organization needs to uniquely identify something (a router, a
376 room, a person, a standard, or whatever), you are encouraged to get your
377 own identification subtree at
378\begin_inset LatexCommand \htmlurl{http://www.iana.org/protocols/forms.htm}
379
380\end_inset
381
382.
383\layout Standard
384
385For example, the very first ASN.1 module in this document has the following
386 OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
387\layout LyX-Code
388
389ExampleOID ::= OBJECT IDENTIFIER
390\layout LyX-Code
391
Lev Walkin26587ab2004-08-23 15:12:04 +0000392
393\layout LyX-Code
394
Lev Walkin84291e02004-06-03 03:49:45 +0000395usageExampleModule1-oid ExampleOID
396\layout LyX-Code
397
398 ::= { 1 3 6 1 4 1 9363 1 5 2 1 1 }
399\layout LyX-Code
400
Lev Walkin26587ab2004-08-23 15:12:04 +0000401
402\layout LyX-Code
403
Lev Walkin84291e02004-06-03 03:49:45 +0000404-- An identifier of the Internet.
405\layout LyX-Code
406
407internet-id OBJECT IDENTIFIER
408\layout LyX-Code
409
410 ::= { iso(1) identified-organization(3)
411\layout LyX-Code
412
413 dod(6) internet(1) }
414\layout Standard
415
416As you see, names are optional.
417\layout Subsection
418
419The RELATIVE-OID type
420\layout Standard
421
422The RELATIVE-OID type has the semantics of a subtree of an OBJECT IDENTIFIER.
423 There may be no need to repeat the whole sequence of numbers from the root
424 of the registration tree where the only thing of interest is some of the
425 tree's subsequence.
426\layout LyX-Code
427
428this-document RELATIVE-OID ::= { docs(2) usage(1) }
429\layout LyX-Code
430
Lev Walkin26587ab2004-08-23 15:12:04 +0000431
432\layout LyX-Code
433
Lev Walkin84291e02004-06-03 03:49:45 +0000434this-example RELATIVE-OID ::= {
435\layout LyX-Code
436
437 this-document assorted-examples(0) this-example(1) }
438\layout Section
439
440Some of the ASN.1 String Types
441\layout Subsection
442
443The IA5String type
444\layout Standard
445
446This is essentially the ASCII, with 128 character codes available (7 lower
Lev Walkin26587ab2004-08-23 15:12:04 +0000447 bits of an 8-bit byte).
Lev Walkin84291e02004-06-03 03:49:45 +0000448\layout Subsection
449
450The UTF8String type
451\layout Standard
452
453This is the character string which encodes the full Unicode range (4 bytes)
454 using multibyte character sequences.
455\layout Subsection
456
457The NumericString type
458\layout Standard
459
460This type represents the character string with the alphabet consisting of
461 numbers (
462\begin_inset Quotes sld
463\end_inset
464
4650
466\begin_inset Quotes srd
467\end_inset
468
469 to
470\begin_inset Quotes sld
471\end_inset
472
4739
474\begin_inset Quotes srd
475\end_inset
476
477) and a space.
478\layout Subsection
479
480The PrintableString type
481\layout Standard
482
483The character string with the following alphabet: space,
484\begin_inset Quotes sld
485\end_inset
486
487
488\series bold
489'
490\series default
491
492\begin_inset Quotes srd
493\end_inset
494
495 (single quote),
496\begin_inset Quotes sld
497\end_inset
498
499
500\series bold
501(
502\series default
503
504\begin_inset Quotes sld
505\end_inset
506
507,
508\begin_inset Quotes sld
509\end_inset
510
511
512\series bold
513)
514\series default
515
516\begin_inset Quotes srd
517\end_inset
518
519,
520\begin_inset Quotes sld
521\end_inset
522
523
524\series bold
525+
526\series default
527
528\begin_inset Quotes srd
529\end_inset
530
531,
532\begin_inset Quotes sld
533\end_inset
534
Lev Walkin1f756282004-09-29 13:29:17 +0000535
536\series bold
Lev Walkin84291e02004-06-03 03:49:45 +0000537,
Lev Walkin1f756282004-09-29 13:29:17 +0000538\series default
539
Lev Walkin84291e02004-06-03 03:49:45 +0000540\begin_inset Quotes srd
541\end_inset
542
543 (comma),
544\begin_inset Quotes sld
545\end_inset
546
547
548\series bold
549-
550\series default
551
552\begin_inset Quotes srd
553\end_inset
554
555,
556\begin_inset Quotes sld
557\end_inset
558
559
560\series bold
561.
562\series default
563
564\begin_inset Quotes srd
565\end_inset
566
567,
568\begin_inset Quotes sld
569\end_inset
570
571
572\series bold
573/
574\series default
575
576\begin_inset Quotes srd
577\end_inset
578
579, digits (
580\begin_inset Quotes sld
581\end_inset
582
5830
584\begin_inset Quotes srd
585\end_inset
586
587 to
588\begin_inset Quotes sld
589\end_inset
590
5919
592\begin_inset Quotes srd
593\end_inset
594
595),
596\begin_inset Quotes sld
597\end_inset
598
599
600\series bold
601:
602\series default
603
604\begin_inset Quotes srd
605\end_inset
606
607,
608\begin_inset Quotes sld
609\end_inset
610
611
612\series bold
613=
614\series default
615
616\begin_inset Quotes srd
617\end_inset
618
619,
620\begin_inset Quotes sld
621\end_inset
622
623
624\series bold
625?
626\series default
627
628\begin_inset Quotes srd
629\end_inset
630
631, upper-case and lower-case letters (
632\begin_inset Quotes sld
633\end_inset
634
635A
636\begin_inset Quotes srd
637\end_inset
638
639 to
640\begin_inset Quotes sld
641\end_inset
642
643Z
644\begin_inset Quotes srd
645\end_inset
646
647 and
648\begin_inset Quotes sld
649\end_inset
650
651a
652\begin_inset Quotes srd
653\end_inset
654
655 to
656\begin_inset Quotes sld
657\end_inset
658
659z
660\begin_inset Quotes srd
661\end_inset
662
Lev Walkin1f756282004-09-29 13:29:17 +0000663).
Lev Walkin84291e02004-06-03 03:49:45 +0000664\layout Subsection
665
666The VisibleString type
667\layout Standard
668
669The character string with the alphabet which is more or less a subset of
Lev Walkin1f756282004-09-29 13:29:17 +0000670 ASCII between the space and the
Lev Walkin84291e02004-06-03 03:49:45 +0000671\begin_inset Quotes sld
672\end_inset
673
Lev Walkin26587ab2004-08-23 15:12:04 +0000674
675\series bold
Lev Walkin84291e02004-06-03 03:49:45 +0000676~
Lev Walkin26587ab2004-08-23 15:12:04 +0000677\series default
678
Lev Walkin84291e02004-06-03 03:49:45 +0000679\begin_inset Quotes srd
680\end_inset
681
Lev Walkin1f756282004-09-29 13:29:17 +0000682 symbol (tilde).
683\layout Standard
684
685Alternatively, the alphabet may be described as the PrintableString alphabet
Lev Walkin26587ab2004-08-23 15:12:04 +0000686 presented earlier, plus the following characters:
Lev Walkin84291e02004-06-03 03:49:45 +0000687\begin_inset Quotes sld
688\end_inset
689
690
691\series bold
692!
693\series default
694
695\begin_inset Quotes srd
696\end_inset
697
698,
699\begin_inset Quotes sld
700\end_inset
701
702
703\series bold
704
705\begin_inset Quotes srd
706\end_inset
707
708
709\series default
710
711\begin_inset Quotes srd
712\end_inset
713
714,
715\begin_inset Quotes sld
716\end_inset
717
718
719\series bold
720#
721\series default
722
723\begin_inset Quotes srd
724\end_inset
725
726,
727\begin_inset Quotes sld
728\end_inset
729
730
731\series bold
732$
733\series default
734
735\begin_inset Quotes srd
736\end_inset
737
738,
739\begin_inset Quotes sld
740\end_inset
741
742
743\series bold
744%
745\series default
746
747\begin_inset Quotes srd
748\end_inset
749
750,
751\begin_inset Quotes sld
752\end_inset
753
754
755\series bold
756&
757\series default
758
759\begin_inset Quotes srd
760\end_inset
761
762,
763\begin_inset Quotes sld
764\end_inset
765
766
767\series bold
768*
769\series default
770
771\begin_inset Quotes srd
772\end_inset
773
774,
775\begin_inset Quotes sld
776\end_inset
777
778
779\series bold
780;
781\series default
782
783\begin_inset Quotes srd
784\end_inset
785
786,
787\begin_inset Quotes sld
788\end_inset
789
790
791\series bold
792<
793\series default
794
795\begin_inset Quotes srd
796\end_inset
797
798,
799\begin_inset Quotes sld
800\end_inset
801
802
803\series bold
804>
805\series default
806
807\begin_inset Quotes srd
808\end_inset
809
810,
811\begin_inset Quotes sld
812\end_inset
813
814
815\series bold
816[
817\series default
818
819\begin_inset Quotes srd
820\end_inset
821
822,
823\begin_inset Quotes sld
824\end_inset
825
826
827\series bold
828
829\backslash
830
831\series default
832
833\begin_inset Quotes srd
834\end_inset
835
836,
837\begin_inset Quotes sld
838\end_inset
839
840
841\series bold
842]
843\series default
844
845\begin_inset Quotes srd
846\end_inset
847
848,
849\begin_inset Quotes sld
850\end_inset
851
852
853\series bold
854^
855\series default
856
857\begin_inset Quotes srd
858\end_inset
859
860,
861\begin_inset Quotes sld
862\end_inset
863
864
865\series bold
866_
867\series default
868
869\begin_inset Quotes srd
870\end_inset
871
872,
873\begin_inset Quotes sld
874\end_inset
875
876
877\series bold
878`
879\series default
880
881\begin_inset Quotes srd
882\end_inset
883
884 (single left quote),
885\begin_inset Quotes sld
886\end_inset
887
888
889\series bold
890{
891\series default
892
893\begin_inset Quotes srd
894\end_inset
895
896,
897\begin_inset Quotes sld
898\end_inset
899
900
901\series bold
902|
903\series default
904
905\begin_inset Quotes srd
906\end_inset
907
908,
909\begin_inset Quotes sld
910\end_inset
911
912
913\series bold
914}
915\series default
916
917\begin_inset Quotes srd
918\end_inset
919
920,
921\begin_inset Quotes sld
922\end_inset
923
Lev Walkin26587ab2004-08-23 15:12:04 +0000924
925\series bold
Lev Walkin84291e02004-06-03 03:49:45 +0000926~
Lev Walkin26587ab2004-08-23 15:12:04 +0000927\series default
928
Lev Walkin84291e02004-06-03 03:49:45 +0000929\begin_inset Quotes srd
930\end_inset
931
932.
933\layout Section
934
935ASN.1 Constructed Types
936\layout Subsection
937
938The SEQUENCE type
939\layout Standard
940
941This is an ordered collection of other simple or constructed types.
942 The SEQUENCE constructed type resembles the C
943\begin_inset Quotes sld
944\end_inset
945
946struct
947\begin_inset Quotes srd
948\end_inset
949
950 statement.
951\layout LyX-Code
952
953Address ::= SEQUENCE {
954\layout LyX-Code
955
956 -- The apartment number may be omitted
957\layout LyX-Code
958
Lev Walkind3630792004-09-30 22:26:19 +0000959 apartmentNumber NumericString OPTIONAL,
Lev Walkin84291e02004-06-03 03:49:45 +0000960\layout LyX-Code
961
962 streetName PrintableString,
963\layout LyX-Code
964
965 cityName PrintableString,
966\layout LyX-Code
967
968 stateName PrintableString,
969\layout LyX-Code
970
971 -- This one may be omitted too
972\layout LyX-Code
973
974 zipNo NumericString OPTIONAL
975\layout LyX-Code
976
977}
978\layout Subsection
979
980The SET type
981\layout Standard
982
983This is a collection of other simple or constructed types.
984 Ordering is not important.
985 The data may arrive in the order which is different from the order of specifica
986tion.
987 Data is encoded in the order not necessarily corresponding to the order
988 of specification.
989\layout Subsection
990
991The CHOICE type
992\layout Standard
993
994This type is just a choice between the subtypes specified in it.
995 The CHOICE type contains at most one of the subtypes specified, and it
996 is always implicitly known which choice is being decoded or encoded.
997 This one resembles the C
998\begin_inset Quotes sld
999\end_inset
1000
1001union
1002\begin_inset Quotes srd
1003\end_inset
1004
1005 statement.
1006\layout Standard
1007
1008The following type defines a response code, which may be either an integer
1009 code or a boolean
1010\begin_inset Quotes sld
1011\end_inset
1012
1013true
1014\begin_inset Quotes srd
1015\end_inset
1016
1017/
1018\begin_inset Quotes srd
1019\end_inset
1020
1021false
1022\begin_inset Quotes srd
1023\end_inset
1024
1025 code.
1026\layout LyX-Code
1027
1028ResponseCode ::= CHOICE {
1029\layout LyX-Code
1030
1031 intCode INTEGER,
1032\layout LyX-Code
1033
1034 boolCode BOOLEAN
1035\layout LyX-Code
1036
1037}
1038\layout LyX-Code
1039
1040\layout Subsection
1041
1042The SEQUENCE OF type
1043\layout Standard
1044
1045This one is the list (array) of simple or constructed types:
1046\layout LyX-Code
1047
1048-- Example 1
1049\layout LyX-Code
1050
1051ManyIntegers ::= SEQUENCE OF INTEGER
1052\layout LyX-Code
1053
Lev Walkin26587ab2004-08-23 15:12:04 +00001054
1055\layout LyX-Code
1056
Lev Walkin84291e02004-06-03 03:49:45 +00001057-- Example 2
1058\layout LyX-Code
1059
1060ManyRectangles ::= SEQUENCE OF Rectangle
1061\layout LyX-Code
1062
Lev Walkin26587ab2004-08-23 15:12:04 +00001063
1064\layout LyX-Code
1065
Lev Walkin84291e02004-06-03 03:49:45 +00001066-- More complex example:
1067\layout LyX-Code
1068
1069-- an array of structures defined in place.
1070\layout LyX-Code
1071
1072ManyCircles ::= SEQUENCE OF SEQUENCE {
1073\layout LyX-Code
1074
1075 radius INTEGER
1076\layout LyX-Code
1077
1078 }
1079\layout Subsection
1080
1081The SET OF type
1082\layout Standard
1083
1084The SET OF type models the bag of structures.
1085 It resembles the SEQUENCE OF type, but the order is not important: i.e.
1086 the elements may arrive in the order which is not necessarily the same
1087 as the in-memory order on the remote machines.
1088\layout LyX-Code
1089
1090-- A set of structures defined elsewhere
1091\layout LyX-Code
1092
1093SetOfApples :: SET OF Apple
1094\layout LyX-Code
1095
Lev Walkin26587ab2004-08-23 15:12:04 +00001096
1097\layout LyX-Code
1098
Lev Walkin84291e02004-06-03 03:49:45 +00001099-- Set of integers encoding the kind of a fruit
1100\layout LyX-Code
1101
1102FruitBag ::= SET OF ENUMERATED { apple, orange }
Lev Walkin4a83dd52004-08-07 05:00:07 +00001103\layout Part
1104
1105Using the ASN.1 Compiler
Lev Walkin84291e02004-06-03 03:49:45 +00001106\layout Chapter
1107
Lev Walkin4a83dd52004-08-07 05:00:07 +00001108Introduction to the ASN.1 Compiler
Lev Walkin84291e02004-06-03 03:49:45 +00001109\layout Standard
1110
1111The purpose of the ASN.1 compiler, of which this document is part, is to
1112 convert the ASN.1 specifications to some other target language (currently,
1113 only C is supported
1114\begin_inset Foot
1115collapsed false
1116
1117\layout Standard
1118
1119C++ is
1120\begin_inset Quotes sld
1121\end_inset
1122
1123supported
1124\begin_inset Quotes srd
1125\end_inset
1126
Lev Walkin26587ab2004-08-23 15:12:04 +00001127 too, as long as an class-based approach is not a definitive factor.
Lev Walkin84291e02004-06-03 03:49:45 +00001128\end_inset
1129
1130).
1131 The compiler reads the specification and emits a series of target language
1132 structures and surrounding maintenance code.
1133 For example, the C structure which may be created by compiler to represent
1134 the simple
1135\emph on
1136Rectangle
1137\emph default
1138 specification defined earlier in this document, may look like this
1139\begin_inset Foot
1140collapsed false
1141
1142\layout Standard
1143
1144
1145\emph on
Lev Walkin99301892004-09-14 12:48:17 +00001146-fnative-types
Lev Walkin84291e02004-06-03 03:49:45 +00001147\emph default
1148 compiler option is used to produce basic C
1149\emph on
1150int
1151\emph default
Lev Walkin26587ab2004-08-23 15:12:04 +00001152 types instead of infinite width INTEGER_t structures.
1153 See Table
1154\begin_inset LatexCommand \vref{cap:asn1c-cmdopts}
1155
1156\end_inset
1157
1158.
Lev Walkin84291e02004-06-03 03:49:45 +00001159\end_inset
1160
1161:
1162\layout LyX-Code
1163
1164typedef struct Rectangle_s {
1165\layout LyX-Code
1166
1167 int height;
1168\layout LyX-Code
1169
1170 int width;
1171\layout LyX-Code
1172
1173} Rectangle_t;
1174\layout Standard
1175
1176This would not be of much value for such a simple specification, so the
1177 compiler goes further and actually produces the code which fills in this
Lev Walkin26587ab2004-08-23 15:12:04 +00001178 structure by parsing the opaque binary
Lev Walkin84291e02004-06-03 03:49:45 +00001179\begin_inset Foot
Lev Walkin26587ab2004-08-23 15:12:04 +00001180collapsed true
Lev Walkin84291e02004-06-03 03:49:45 +00001181
1182\layout Standard
1183
1184BER, CER and DER encodings are binary.
1185 However, the XER encoding is text (XML) based.
1186\end_inset
1187
1188 data provided in some buffer.
1189 It also produces the code that takes this structure as an argument and
1190 performs structure serialization by emitting a series of bytes.
Lev Walkin26587ab2004-08-23 15:12:04 +00001191\layout Chapter
Lev Walkin84291e02004-06-03 03:49:45 +00001192
1193Quick start
1194\layout Standard
1195
Lev Walkin26587ab2004-08-23 15:12:04 +00001196After building and installing the compiler, the
1197\emph on
1198asn1c
1199\begin_inset Foot
1200collapsed false
1201
1202\layout Standard
1203
1204The 1 symbol in asn
1205\series bold
12061
1207\series default
1208c is a digit, not an
1209\begin_inset Quotes sld
1210\end_inset
1211
1212ell
1213\begin_inset Quotes srd
1214\end_inset
1215
1216 letter.
1217\end_inset
1218
1219
1220\emph default
1221 command may be used to compile the ASN.1 specification
Lev Walkin84291e02004-06-03 03:49:45 +00001222\begin_inset Foot
1223collapsed false
1224
1225\layout Standard
1226
1227This is probably
1228\series bold
1229not
1230\series default
1231 what you want to try out right now -- read through the rest of this chapter
Lev Walkin26587ab2004-08-23 15:12:04 +00001232 to find out about
1233\series bold
1234-P
1235\series default
1236 and
1237\series bold
1238-R
1239\series default
1240 options.
Lev Walkin84291e02004-06-03 03:49:45 +00001241\end_inset
1242
1243:
1244\layout LyX-Code
1245
1246asn1c
1247\emph on
1248<spec.asn1>
1249\layout Standard
1250
Lev Walkin26587ab2004-08-23 15:12:04 +00001251If several specifications contain interdependencies, all of the files must
1252 be specified altogether:
Lev Walkin84291e02004-06-03 03:49:45 +00001253\layout LyX-Code
1254
1255asn1c
1256\emph on
1257<spec1.asn1> <spec2.asn1> ...
1258\layout Standard
1259
Lev Walkin26587ab2004-08-23 15:12:04 +00001260The compiler
1261\series bold
1262-E
1263\series default
1264 and
1265\series bold
1266-EF
1267\series default
1268 options are used for testing the parser and the semantic fixer, respectively.
1269 These options will instruct the compiler to dump out the parsed (and fixed,
1270 if
1271\series bold
1272-F
1273\series default
1274 is involved) ASN.1 specification as it was "understood" by the compiler.
1275 It might be useful to check whether a particular syntactic construction
Lev Walkin84291e02004-06-03 03:49:45 +00001276 is properly supported by the compiler.
1277\layout LyX-Code
1278
Lev Walkin26587ab2004-08-23 15:12:04 +00001279asn1c
1280\series bold
1281-EF
1282\series default
1283
Lev Walkin84291e02004-06-03 03:49:45 +00001284\emph on
1285<spec-to-test.asn1>
1286\layout Standard
1287
Lev Walkin26587ab2004-08-23 15:12:04 +00001288The
1289\series bold
1290-P
1291\series default
1292 option is used to dump the compiled output on the screen instead of creating
1293 a bunch of .c and .h files on disk in the current directory.
1294 You would probably want to start with
1295\series bold
1296-P
1297\series default
1298 option instead of creating a mess in your current directory.
1299 Another option,
1300\series bold
1301-R
1302\series default
1303, asks compiler to only generate the files which need to be generated, and
1304 supress linking in the numerous support files.
1305\layout Standard
1306
1307Print the compiled output instead of creating multiple source files:
1308\layout LyX-Code
1309
1310asn1c
1311\series bold
1312-P
1313\series default
1314
1315\emph on
1316<spec-to-compile-and-print.asn1>
1317\layout LyX-Code
1318
1319\layout Chapter
1320
1321Using the ASN.1 Compiler
Lev Walkin84291e02004-06-03 03:49:45 +00001322\layout Section
1323
Lev Walkin26587ab2004-08-23 15:12:04 +00001324Command-line options
1325\layout Standard
1326
1327The Table
1328\begin_inset LatexCommand \vref{cap:asn1c-cmdopts}
1329
1330\end_inset
1331
1332 summarizes various options affecting the compiler's behavior.
1333\layout Standard
1334
1335
1336\begin_inset Float table
1337wide false
Lev Walkindd32b592004-09-06 08:07:29 +00001338collapsed false
Lev Walkin26587ab2004-08-23 15:12:04 +00001339
1340\layout Standard
1341
1342
1343\begin_inset Tabular
Lev Walkin4e940a02004-09-26 13:13:13 +00001344<lyxtabular version="3" rows="21" columns="2">
Lev Walkind3630792004-09-30 22:26:19 +00001345<features islongtable="true">
Lev Walkin26587ab2004-08-23 15:12:04 +00001346<column alignment="left" valignment="top" leftline="true" width="0">
1347<column alignment="block" valignment="top" leftline="true" rightline="true" width="3in">
1348<row topline="true" bottomline="true">
1349<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1350\begin_inset Text
1351
1352\layout Standard
1353
1354
1355\series bold
1356Overall Options
1357\end_inset
1358</cell>
1359<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1360\begin_inset Text
1361
1362\layout Standard
1363
1364
1365\series bold
1366Description
1367\end_inset
1368</cell>
1369</row>
1370<row topline="true">
1371<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1372\begin_inset Text
1373
1374\layout Standard
1375
1376-E
1377\end_inset
1378</cell>
1379<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1380\begin_inset Text
1381
1382\layout Standard
1383
1384
1385\size small
1386Stop after the parsing stage and print the reconstructed ASN.1 specification
1387 code to the standard output.
1388\end_inset
1389</cell>
1390</row>
1391<row topline="true">
1392<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1393\begin_inset Text
1394
1395\layout Standard
1396
1397-F
1398\end_inset
1399</cell>
1400<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1401\begin_inset Text
1402
1403\layout Standard
1404
1405
1406\size small
1407Used together with -E, instructs the compiler to stop after the ASN.1 syntax
1408 tree fixing stage and dump the reconstructed ASN.1 specification to the
1409 standard output.
1410\end_inset
1411</cell>
1412</row>
1413<row topline="true">
1414<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1415\begin_inset Text
1416
1417\layout Standard
1418
1419-P
1420\end_inset
1421</cell>
1422<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1423\begin_inset Text
1424
1425\layout Standard
1426
1427
1428\size small
1429Dump the compiled output to the standard output instead of cre- ating the
1430 target language files on disk.
1431\end_inset
1432</cell>
1433</row>
1434<row topline="true">
1435<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1436\begin_inset Text
1437
1438\layout Standard
1439
1440-R
1441\end_inset
1442</cell>
1443<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1444\begin_inset Text
1445
1446\layout Standard
1447
1448
1449\size small
1450Restrict the compiler to generate only the ASN.1 tables, omit- ting the usual
1451 support code.
1452\end_inset
1453</cell>
1454</row>
Lev Walkin35d446b2004-09-24 20:56:07 +00001455<row topline="true" bottomline="true">
Lev Walkin26587ab2004-08-23 15:12:04 +00001456<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1457\begin_inset Text
1458
1459\layout Standard
1460
1461-S
1462\emph on
1463<directory>
1464\end_inset
1465</cell>
1466<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1467\begin_inset Text
1468
1469\layout Standard
1470
1471
1472\size small
1473Use the specified directory with ASN.1 skeleton files.
1474\end_inset
1475</cell>
1476</row>
Lev Walkin26587ab2004-08-23 15:12:04 +00001477<row topline="true">
1478<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1479\begin_inset Text
1480
1481\layout Standard
1482
1483
1484\series bold
1485Warning Options
1486\end_inset
1487</cell>
1488<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1489\begin_inset Text
1490
1491\layout Standard
1492
1493
1494\series bold
1495Description
1496\end_inset
1497</cell>
1498</row>
1499<row topline="true">
1500<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1501\begin_inset Text
1502
1503\layout Standard
1504
1505-Werror
1506\end_inset
1507</cell>
1508<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1509\begin_inset Text
1510
1511\layout Standard
1512
1513
1514\size small
1515Treat warnings as errors; abort if any warning is produced.
1516\end_inset
1517</cell>
1518</row>
1519<row topline="true">
1520<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1521\begin_inset Text
1522
1523\layout Standard
1524
1525-Wdebug-lexer
1526\end_inset
1527</cell>
1528<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1529\begin_inset Text
1530
1531\layout Standard
1532
1533
1534\size small
1535Enable lexer debugging during the ASN.1 parsing stage.
1536\end_inset
1537</cell>
1538</row>
1539<row topline="true">
1540<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1541\begin_inset Text
1542
1543\layout Standard
1544
1545-Wdebug-fixer
1546\end_inset
1547</cell>
1548<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1549\begin_inset Text
1550
1551\layout Standard
1552
1553
1554\size small
1555 Enable ASN.1 syntax tree fixer debugging during the fixing stage.
1556\end_inset
1557</cell>
1558</row>
1559<row topline="true" bottomline="true">
1560<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1561\begin_inset Text
1562
1563\layout Standard
1564
1565-Wdebug-compiler
1566\end_inset
1567</cell>
1568<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1569\begin_inset Text
1570
1571\layout Standard
1572
1573
1574\size small
1575Enable debugging during the actual compile time.
1576\end_inset
1577</cell>
1578</row>
1579<row topline="true">
1580<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1581\begin_inset Text
1582
1583\layout Standard
1584
1585
1586\series bold
1587Language Options
1588\end_inset
1589</cell>
1590<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1591\begin_inset Text
1592
1593\layout Standard
1594
1595
1596\series bold
1597Description
1598\end_inset
1599</cell>
1600</row>
1601<row topline="true">
1602<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1603\begin_inset Text
1604
1605\layout Standard
1606
Lev Walkindd32b592004-09-06 08:07:29 +00001607-fall-defs-global
1608\end_inset
1609</cell>
1610<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1611\begin_inset Text
1612
1613\layout Standard
1614
Lev Walkin1f756282004-09-29 13:29:17 +00001615Normally the compiler hides the definitions (asn_DEF_xxx) of the inner structure
1616 elements (members of SEQUENCE, SET and other types).
Lev Walkindd32b592004-09-06 08:07:29 +00001617 This option makes all such definitions global.
Lev Walkin1f756282004-09-29 13:29:17 +00001618 Enabling this option may pollute the namespace by making lots of asn_DEF_xxx
Lev Walkindd32b592004-09-06 08:07:29 +00001619 structures globally visible, but will allow you to manipulate (encode and
1620 decode) the individual members of any complex ASN.1 structure.
1621\end_inset
1622</cell>
1623</row>
1624<row topline="true">
1625<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1626\begin_inset Text
1627
1628\layout Standard
1629
Lev Walkin26587ab2004-08-23 15:12:04 +00001630-fbless-SIZE
1631\end_inset
1632</cell>
1633<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1634\begin_inset Text
1635
1636\layout Standard
1637
1638
1639\size small
1640Allow SIZE() constraint for INTEGER, ENUMERATED, and other types for which
1641 this constraint is normally prohibited by the standard.
1642 This is a violation of an ASN.1 standard and compiler may fail to produce
1643 the meaningful code.
1644\end_inset
1645</cell>
1646</row>
1647<row topline="true">
1648<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1649\begin_inset Text
1650
1651\layout Standard
1652
Lev Walkin99301892004-09-14 12:48:17 +00001653-fnative-types
Lev Walkin26587ab2004-08-23 15:12:04 +00001654\end_inset
1655</cell>
1656<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1657\begin_inset Text
1658
1659\layout Standard
1660
1661
1662\size small
Lev Walkin99301892004-09-14 12:48:17 +00001663Use the native machine's data types (int, double) whenever possible, instead
Lev Walkind3630792004-09-30 22:26:19 +00001664 of the compound INTEGER_t, ENUMERATED_t and REAL_t types.
Lev Walkin26587ab2004-08-23 15:12:04 +00001665
1666\end_inset
1667</cell>
1668</row>
1669<row topline="true">
1670<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1671\begin_inset Text
1672
1673\layout Standard
1674
Lev Walkin4e940a02004-09-26 13:13:13 +00001675-fno-constraints
1676\end_inset
1677</cell>
1678<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1679\begin_inset Text
1680
1681\layout Standard
1682
1683Do not generate ASN.1 subtype constraint checking code.
1684 This may make a shorter executable.
1685\end_inset
1686</cell>
1687</row>
1688<row topline="true">
1689<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1690\begin_inset Text
1691
1692\layout Standard
1693
Lev Walkin26587ab2004-08-23 15:12:04 +00001694-funnamed-unions
1695\end_inset
1696</cell>
1697<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1698\begin_inset Text
1699
1700\layout Standard
1701
1702
1703\size small
1704Enable unnamed unions in the definitions of target language's structures.
1705\end_inset
1706</cell>
1707</row>
1708<row topline="true" bottomline="true">
1709<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1710\begin_inset Text
1711
1712\layout Standard
1713
1714-ftypes88
1715\end_inset
1716</cell>
1717<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1718\begin_inset Text
1719
1720\layout Standard
1721
1722
1723\size small
Lev Walkind3630792004-09-30 22:26:19 +00001724Pretend to support only ASN.1:1988 embedded types.
1725 Certain reserved words, such as UniversalString and BMPString, become ordinary
1726 type references and may be redefined by the specification.
Lev Walkin26587ab2004-08-23 15:12:04 +00001727\end_inset
1728</cell>
1729</row>
Lev Walkind3630792004-09-30 22:26:19 +00001730<row topline="true" newpage="true">
Lev Walkin26587ab2004-08-23 15:12:04 +00001731<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1732\begin_inset Text
1733
1734\layout Standard
1735
1736
1737\series bold
1738Output Options
1739\end_inset
1740</cell>
1741<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1742\begin_inset Text
1743
1744\layout Standard
1745
1746
1747\series bold
1748Description
1749\end_inset
1750</cell>
1751</row>
1752<row topline="true">
1753<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1754\begin_inset Text
1755
1756\layout Standard
1757
1758-print-constraints
1759\end_inset
1760</cell>
1761<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1762\begin_inset Text
1763
1764\layout Standard
1765
1766
1767\size small
1768When -EF are also specified, this option forces the compiler to explain
1769 its internal understanding of subtype constraints.
1770\end_inset
1771</cell>
1772</row>
1773<row topline="true" bottomline="true">
1774<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1775\begin_inset Text
1776
1777\layout Standard
1778
1779-print-lines
1780\end_inset
1781</cell>
1782<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1783\begin_inset Text
1784
1785\layout Standard
1786
1787
1788\size small
1789Generate "-- #line" comments in -E output.
1790\end_inset
1791</cell>
1792</row>
1793</lyxtabular>
1794
1795\end_inset
1796
1797
1798\layout Caption
1799
1800
1801\begin_inset LatexCommand \label{cap:asn1c-cmdopts}
1802
1803\end_inset
1804
1805The list of asn1c command line options
1806\end_inset
1807
1808
1809\layout Section
Lev Walkin84291e02004-06-03 03:49:45 +00001810
1811Recognizing compiler output
1812\layout Standard
1813
1814After compiling, the following entities will be created in your current
1815 directory:
1816\layout Itemize
1817
1818A set of .c and .h files, generally a single pair for each type defined in
1819 the ASN.1 specifications.
1820 These files will be named similarly to the ASN.1 types (
1821\emph on
1822Rectangle.c
1823\emph default
1824 and
1825\emph on
1826Rectangle.h
1827\emph default
1828 for the specification defined in the beginning of this document).
1829\layout Itemize
1830
1831A set of helper .c and .h files which contain generic encoders, decoders and
1832 other useful routines.
Lev Walkin26587ab2004-08-23 15:12:04 +00001833 There will be quite a few of them, some of them even are not always necessary,
1834 but the overall amount of code after compiling will be rather small anyway.
Lev Walkin84291e02004-06-03 03:49:45 +00001835\layout Standard
1836
1837It is your responsibility to create .c file with the
1838\emph on
1839 int main()
1840\emph default
1841 routine and the Makefile (if needed).
1842 Compiler helps you with the latter by creating the Makefile.am.sample, containing
1843 the skeleton definition for the automake, should you want to use autotools.
1844\layout Standard
1845
1846In other words, after compiling the Rectangle module, you have the following
1847 set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h,
1848\series bold
1849\SpecialChar \ldots{}
1850
1851\series default
1852 }, where
1853\series bold
1854
1855\begin_inset Quotes sld
1856\end_inset
1857
1858\SpecialChar \ldots{}
1859
1860\begin_inset Quotes srd
1861\end_inset
1862
1863
1864\series default
1865 stands for the set of additional
1866\begin_inset Quotes sld
1867\end_inset
1868
1869helper
1870\begin_inset Quotes srd
1871\end_inset
1872
1873 files created by the compiler.
1874 If you add the simple file with the
1875\emph on
1876int main()
1877\emph default
1878 routine, it would even be possible to compile everything with the single
1879 instruction:
1880\layout LyX-Code
1881
1882cc -o rectangle *.c # It could be
1883\emph on
1884that
1885\emph default
1886 simple
1887\begin_inset Foot
1888collapsed false
1889
1890\layout Standard
1891
1892Provided that you've also created a .c file with the
1893\emph on
1894int main()
1895\emph default
1896 routine.
1897\end_inset
1898
1899
Lev Walkin26587ab2004-08-23 15:12:04 +00001900\layout Section
Lev Walkin84291e02004-06-03 03:49:45 +00001901
Lev Walkind3630792004-09-30 22:26:19 +00001902Invoking the ASN.1 helper code from an application
1903\begin_inset OptArg
1904collapsed true
1905
1906\layout Standard
1907
1908Invoking the helper code
1909\end_inset
1910
1911
Lev Walkin84291e02004-06-03 03:49:45 +00001912\layout Standard
1913
Lev Walkin26587ab2004-08-23 15:12:04 +00001914First of all, you should to include one or more header files into your applicati
1915on.
1916 For our Rectangle module, including the Rectangle.h file is enough:
Lev Walkin84291e02004-06-03 03:49:45 +00001917\layout LyX-Code
1918
1919#include <Rectangle.h>
1920\layout Standard
1921
1922The header files defines the C structure corresponding to the ASN.1 definition
1923 of a rectangle and the declaration of the ASN.1 type descriptor, which is
1924 used as an argument to most of the functions provided by the ASN.1 module.
1925 For example, here is the code which frees the Rectangle_t structure:
1926\layout LyX-Code
1927
Lev Walkin26587ab2004-08-23 15:12:04 +00001928Rectangle_t *rect = ...;
1929\layout LyX-Code
1930
1931
Lev Walkin84291e02004-06-03 03:49:45 +00001932\layout LyX-Code
1933
Lev Walkin1f756282004-09-29 13:29:17 +00001934asn_DEF_Rectangle->free_struct(&asn_DEF_Rectangle,
Lev Walkin84291e02004-06-03 03:49:45 +00001935\layout LyX-Code
1936
1937 rect, 0);
1938\layout Standard
1939
1940This code defines a
1941\emph on
1942rect
1943\emph default
1944 pointer which points to the Rectangle_t structure which needs to be freed.
1945 The second line invokes the generic free_struct routine created specifically
1946 for this Rectangle_t structure.
1947 The
1948\emph on
Lev Walkin1f756282004-09-29 13:29:17 +00001949asn_DEF_Rectangle
Lev Walkin84291e02004-06-03 03:49:45 +00001950\emph default
1951 is the type descriptor, which holds a collection of generic routines to
1952 deal with the Rectangle_t structure.
1953\layout Standard
1954
1955There are several generic functions available:
1956\layout Description
1957
Lev Walkin84291e02004-06-03 03:49:45 +00001958ber_decoder This is the generic
1959\emph on
1960restartable
1961\begin_inset Foot
1962collapsed false
1963
1964\layout Standard
1965
1966Restartable means that if the decoder encounters the end of the buffer,
1967 it will fail, but may later be invoked again with the rest of the buffer
1968 to continue decoding.
1969\end_inset
1970
1971
1972\emph default
1973BER decoder (Basic Encoding Rules).
1974 This decoder would create and/or fill the target structure for you.
1975 Please refer to Section
1976\begin_inset LatexCommand \ref{sub:Decoding-BER}
1977
1978\end_inset
1979
1980.
1981\layout Description
1982
1983der_encoder This is the generic DER encoder (Distinguished Encoding Rules).
Lev Walkin35d446b2004-09-24 20:56:07 +00001984 This encoder will take the target structure and encode it into a series
Lev Walkin84291e02004-06-03 03:49:45 +00001985 of bytes.
1986 Please refer to Section
1987\begin_inset LatexCommand \ref{sub:Encoding-DER}
1988
1989\end_inset
1990
1991.
1992\layout Description
1993
Lev Walkin35d446b2004-09-24 20:56:07 +00001994xer_encoder This is the generic XER encoder (XML Encoding Rules).
1995 This encoder will take the target structure and represent it as an XML
1996 (text) document.
1997 Please refer to Section
1998\begin_inset LatexCommand \ref{sub:Encoding-XER}
1999
2000\end_inset
2001
2002.
2003\layout Description
2004
2005check_constraints Check that the contents of the target structure are semantical
2006ly valid and constrained to appropriate implicit or explicit subtype constraints.
2007 Please refer to Section
2008\begin_inset LatexCommand \vref{sub:Validating-the-target}
2009
2010\end_inset
2011
2012.
2013\layout Description
2014
Lev Walkin84291e02004-06-03 03:49:45 +00002015print_struct This function convert the contents of the passed target structure
2016 into human readable form.
2017 This form is not formal and cannot be converted back into the structure,
2018 but it may turn out to be useful for debugging or quick-n-dirty printing.
2019 Please refer to Section
2020\begin_inset LatexCommand \ref{sub:Printing-the-target}
2021
2022\end_inset
2023
2024.
2025\layout Description
2026
2027free_struct This is a generic disposal which frees the target structure.
2028 Please refer to Section
2029\begin_inset LatexCommand \ref{sub:Freeing-the-target}
2030
2031\end_inset
2032
2033.
2034\layout Standard
2035
Lev Walkin35d446b2004-09-24 20:56:07 +00002036check_constraints Check that the contents of the target structure are semantical
2037ly valid and constrained to appropriate implicit or explicit subtype constraints.
2038 Please refer to Section
2039\begin_inset LatexCommand \vref{sub:Validating-the-target}
2040
2041\end_inset
2042
2043.
2044\layout Standard
2045
Lev Walkin84291e02004-06-03 03:49:45 +00002046Each of the above function takes the type descriptor (
2047\emph on
Lev Walkin1f756282004-09-29 13:29:17 +00002048asn_DEF_\SpecialChar \ldots{}
Lev Walkin84291e02004-06-03 03:49:45 +00002049
2050\emph default
2051) and the target structure (
2052\emph on
2053rect
2054\emph default
2055, in the above example).
2056 The target structure is typically created by the generic BER decoder or
2057 by the application itself.
2058\layout Standard
2059
2060Here is how the buffer can be deserialized into the structure:
2061\layout LyX-Code
2062
2063Rectangle_t *
2064\layout LyX-Code
2065
Lev Walkin26587ab2004-08-23 15:12:04 +00002066simple_deserializer(const void *buffer, size_t buf_size) {
Lev Walkin84291e02004-06-03 03:49:45 +00002067\layout LyX-Code
2068
2069 Rectangle_t *rect = 0; /* Note this 0! */
2070\layout LyX-Code
2071
2072 ber_dec_rval_t rval;
2073\layout LyX-Code
2074
2075
2076\layout LyX-Code
2077
Lev Walkin1f756282004-09-29 13:29:17 +00002078 rval = asn_DEF_Rectangle->ber_decoder(0,
Lev Walkin84291e02004-06-03 03:49:45 +00002079\layout LyX-Code
2080
Lev Walkin1f756282004-09-29 13:29:17 +00002081 &asn_DEF_Rectangle,
Lev Walkin84291e02004-06-03 03:49:45 +00002082\layout LyX-Code
2083
2084 (void **)&rect,
2085\layout LyX-Code
2086
2087 buffer, buf_size,
2088\layout LyX-Code
2089
2090 0);
2091\layout LyX-Code
2092
2093
2094\layout LyX-Code
2095
2096 if(rval
2097\series bold
2098.code
2099\series default
2100 == RC_OK) {
2101\layout LyX-Code
2102
2103 return rect; /* Decoding succeeded */
2104\layout LyX-Code
2105
2106 } else {
2107\layout LyX-Code
2108
Lev Walkin26587ab2004-08-23 15:12:04 +00002109 /* Free partially decoded rect */
2110\layout LyX-Code
2111
Lev Walkin1f756282004-09-29 13:29:17 +00002112 asn_DEF_Rectangle->free_struct(
Lev Walkin84291e02004-06-03 03:49:45 +00002113\layout LyX-Code
2114
Lev Walkin1f756282004-09-29 13:29:17 +00002115 &asn_DEF_Rectangle, rect, 0);
Lev Walkin84291e02004-06-03 03:49:45 +00002116\layout LyX-Code
2117
2118 return 0;
2119\layout LyX-Code
2120
2121 }
2122\layout LyX-Code
2123
2124}
2125\layout Standard
2126
2127The code above defines a function,
2128\emph on
2129simple_deserializer
2130\emph default
2131, which takes a buffer and its length and expected to return a pointer to
2132 the Rectangle_t structure.
2133 Inside, it tries to convert the bytes passed into the target structure
2134 (rect) using the generic BER decoder and returns the rect pointer afterwards.
2135 If the structure cannot be deserialized, it frees the memory which might
2136 be left allocated by the unfinished
2137\emph on
2138ber_decoder
2139\emph default
2140 routine and returns NULL.
2141
2142\series bold
2143This freeing is necessary
2144\series default
2145 because the ber_decoder is a restartable procedure, and may fail just because
2146 there is more data needs to be provided before decoding could be finalized.
2147 The code above obviously does not take into account the way the
2148\emph on
2149ber_decoder
2150\emph default
2151 failed, so the freeing is necessary because the part of the buffer may
2152 already be decoded into the structure by the time something goes wrong.
2153\layout Standard
2154
2155Restartable decoding is a little bit trickier: you need to provide the old
2156 target structure pointer (which might be already half-decoded) and react
2157 on RC_WMORE return code.
2158 This will be explained later in Section
2159\begin_inset LatexCommand \vref{sub:Decoding-BER}
2160
2161\end_inset
2162
2163
Lev Walkin26587ab2004-08-23 15:12:04 +00002164\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002165
2166
2167\begin_inset LatexCommand \label{sub:Decoding-BER}
2168
2169\end_inset
2170
2171Decoding BER
2172\layout Standard
2173
2174The Basic Encoding Rules describe the basic way how the structure can be
2175 encoded and decoded.
2176 Several other encoding rules (CER, DER) define a more restrictive versions
2177 of BER, so the generic BER parser is also capable of decoding the data
2178 encoded by CER and DER encoders.
2179 The opposite is not true.
2180\layout Standard
2181
2182The ASN.1 compiler provides the generic BER decoder which is implicitly capable
2183 of decoding BER, CER and DER encoded data.
2184\layout Standard
2185
2186The decoder is restartable (stream-oriented), which means that in case the
2187 buffer has less data than it is expected, the decoder will process whatever
2188 it is available and ask for more data to be provided.
2189 Please note that the decoder may actually process less data than it is
2190 given in the buffer, which means that you should be able to make the next
2191 buffer contain the unprocessed part of the previous buffer.
2192\layout Standard
2193
2194Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
2195\layout Itemize
2196
2197You may concatenate these buffers and feed the BER decoder with 300 bytes
2198 of data, or
2199\layout Itemize
2200
2201You may feed it the first buffer of 100 bytes of data, realize that the
2202 ber_decoder consumed only 95 bytes from it and later feed the decoder with
2203 205 bytes buffer which consists of 5 unprocessed bytes from the first buffer
2204 and the latter 200 bytes from the second buffer.
2205\layout Standard
2206
2207This is not as convenient as it could be (like, the BER encoder would consume
2208 the whole 100 bytes and keep these 5 bytes in some temporary storage),
2209 but in case of stream-based processing it might actually be OK.
2210 Suggestions are welcome.
2211\layout Standard
2212
2213There are two ways to invoke a BER decoder.
2214 The first one is a direct reference of the type-specific decoder.
2215 This way was shown in the previous example of
2216\emph on
2217simple_deserializer
2218\emph default
2219 function.
2220 The second way is to invoke a
2221\emph on
2222ber_decode
2223\emph default
2224 function, which is just a simple wrapper of the former approach into a
2225 less wordy notation:
2226\layout LyX-Code
2227
Lev Walkin1f756282004-09-29 13:29:17 +00002228rval = ber_decode(0, &asn_DEF_Rectangle, (void **)&rect,
Lev Walkin84291e02004-06-03 03:49:45 +00002229\layout LyX-Code
2230
2231 buffer, buf_size);
2232\layout Standard
2233
Lev Walkin1f756282004-09-29 13:29:17 +00002234Note that the initial (asn_DEF_Rectangle->ber_decoder) reference is gone,
Lev Walkin84291e02004-06-03 03:49:45 +00002235 and also the last argument (0) is no longer necessary.
2236\layout Standard
2237
2238These two ways of invocations are fully equivalent.
2239\layout Standard
2240
2241The BER de
2242\emph on
2243coder
2244\emph default
Lev Walkin1f756282004-09-29 13:29:17 +00002245 may fail because of (
Lev Walkin84291e02004-06-03 03:49:45 +00002246\emph on
2247the following RC_\SpecialChar \ldots{}
2248 codes are defined in ber_decoder.h
2249\emph default
2250):
2251\layout Itemize
2252
2253RC_WMORE: There is more data expected than it is provided (stream mode continuat
2254ion feature);
2255\layout Itemize
2256
2257RC_FAIL: General failure to decode the buffer;
2258\layout Itemize
2259
2260\SpecialChar \ldots{}
2261 other codes may be defined as well.
2262\layout Standard
2263
2264Together with the return code (.code) the ber_dec_rval_t type contains the
2265 number of bytes which is consumed from the buffer.
2266 In the previous hypothetical example of two buffers (of 100 and 200 bytes),
2267 the first call to ber_decode() would return with .code = RC_WMORE and .consumed
2268 = 95.
2269 The .consumed field of the BER decoder return value is
2270\series bold
2271always
2272\series default
2273 valid, even if the decoder succeeds or fails with any other return code.
2274\layout Standard
2275
2276Please look into ber_decoder.h for the precise definition of ber_decode()
2277 and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00002278\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002279
2280
2281\begin_inset LatexCommand \label{sub:Encoding-DER}
2282
2283\end_inset
2284
2285Encoding DER
2286\layout Standard
2287
Lev Walkin35d446b2004-09-24 20:56:07 +00002288The Distinguished Encoding Rules is the
2289\emph on
2290canonical
2291\emph default
2292 variant of BER encoding rules.
2293 The DER is best suited to encode the structures where all the lengths are
2294 known beforehand.
Lev Walkin84291e02004-06-03 03:49:45 +00002295 This is probably exactly how you want to encode: either after a BER decoding
2296 or after a manual fill-up, the target structure contains the data which
2297 size is implicitly known before encoding.
2298 The DER encoding is used, for example, to encode X.509 certificates.
2299\layout Standard
2300
2301As with BER decoder, the DER encoder may be invoked either directly from
Lev Walkin1f756282004-09-29 13:29:17 +00002302 the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone function,
Lev Walkin84291e02004-06-03 03:49:45 +00002303 which is somewhat simpler:
2304\layout LyX-Code
2305
2306/*
2307\layout LyX-Code
2308
2309 * This is a custom function which writes the
2310\layout LyX-Code
2311
2312 * encoded output into some FILE stream.
2313\layout LyX-Code
2314
2315 */
2316\layout LyX-Code
2317
Lev Walkin26587ab2004-08-23 15:12:04 +00002318static int
2319\layout LyX-Code
2320
2321write_stream(const void *buffer, size_t size, void *app_key) {
Lev Walkin84291e02004-06-03 03:49:45 +00002322\layout LyX-Code
2323
2324 FILE *ostream = app_key;
2325\layout LyX-Code
2326
2327 size_t wrote;
2328\layout LyX-Code
2329
2330
2331\layout LyX-Code
2332
2333 wrote = fwrite(buffer, 1, size, ostream);
2334\layout LyX-Code
2335
2336
2337\layout LyX-Code
2338
2339 return (wrote == size) ? 0 : -1;
2340\layout LyX-Code
2341
2342}
2343\layout LyX-Code
2344
2345
2346\layout LyX-Code
2347
2348/*
2349\layout LyX-Code
2350
2351 * This is the serializer itself,
2352\layout LyX-Code
2353
2354 * it supplies the DER encoder with the
2355\layout LyX-Code
2356
2357 * pointer to the custom output function.
2358\layout LyX-Code
2359
2360 */
2361\layout LyX-Code
2362
2363ssize_t
2364\layout LyX-Code
2365
2366simple_serializer(FILE *ostream, Rectangle_t *rect) {
2367\layout LyX-Code
2368
Lev Walkin35d446b2004-09-24 20:56:07 +00002369 asn_enc_rval_t er; /* Encoder return value */
Lev Walkin84291e02004-06-03 03:49:45 +00002370\layout LyX-Code
2371
2372
2373\layout LyX-Code
2374
Lev Walkin1f756282004-09-29 13:29:17 +00002375 er = der_encode(&asn_DEF_Rect, rect,
Lev Walkin84291e02004-06-03 03:49:45 +00002376\layout LyX-Code
2377
Lev Walkin26587ab2004-08-23 15:12:04 +00002378 write_stream, ostream);
Lev Walkin84291e02004-06-03 03:49:45 +00002379\layout LyX-Code
2380
Lev Walkin35d446b2004-09-24 20:56:07 +00002381 if(er.
Lev Walkin84291e02004-06-03 03:49:45 +00002382\series bold
Lev Walkin35d446b2004-09-24 20:56:07 +00002383encoded
Lev Walkin84291e02004-06-03 03:49:45 +00002384\series default
2385 == -1) {
2386\layout LyX-Code
2387
2388 /*
2389\layout LyX-Code
2390
Lev Walkin35d446b2004-09-24 20:56:07 +00002391 * Failed to encode the rectangle data.
Lev Walkin84291e02004-06-03 03:49:45 +00002392\layout LyX-Code
2393
2394 */
2395\layout LyX-Code
2396
2397 fprintf(stderr,
2398\begin_inset Quotes sld
2399\end_inset
2400
2401Cannot encode %s: %s
2402\backslash
2403n
2404\begin_inset Quotes srd
2405\end_inset
2406
2407,
2408\layout LyX-Code
2409
Lev Walkin35d446b2004-09-24 20:56:07 +00002410 er.
Lev Walkin84291e02004-06-03 03:49:45 +00002411\series bold
Lev Walkin35d446b2004-09-24 20:56:07 +00002412failed_type
Lev Walkin84291e02004-06-03 03:49:45 +00002413\series default
2414->name,
2415\layout LyX-Code
2416
2417 strerror(errno));
2418\layout LyX-Code
2419
2420 return -1;
2421\layout LyX-Code
2422
2423 } else {
2424\layout LyX-Code
2425
2426 /* Return the number of bytes */
2427\layout LyX-Code
2428
Lev Walkin35d446b2004-09-24 20:56:07 +00002429 return er.encoded;
Lev Walkin84291e02004-06-03 03:49:45 +00002430\layout LyX-Code
2431
2432 }
2433\layout LyX-Code
2434
2435}
2436\layout Standard
2437
2438As you see, the DER encoder does not write into some sort of buffer or something.
2439 It just invokes the custom function (possible, multiple times) which would
2440 save the data into appropriate storage.
2441 The optional argument
2442\emph on
2443app_key
2444\emph default
2445 is opaque for the DER encoder code and just used by
2446\emph on
2447_write_stream()
2448\emph default
2449 as the pointer to the appropriate output stream to be used.
2450\layout Standard
2451
2452If the custom write function is not given (passed as 0), then the DER encoder
2453 will essentially do the same thing (i.e., encode the data) but no callbacks
2454 will be invoked (so the data goes nowhere).
2455 It may prove useful to determine the size of the structure's encoding before
2456 actually doing the encoding
2457\begin_inset Foot
2458collapsed false
2459
2460\layout Standard
2461
2462It is actually faster too: the encoder might skip over some computations
2463 which aren't important for the size determination.
2464\end_inset
2465
2466.
2467\layout Standard
2468
2469Please look into der_encoder.h for the precise definition of der_encode()
2470 and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00002471\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002472
2473
Lev Walkin35d446b2004-09-24 20:56:07 +00002474\begin_inset LatexCommand \label{sub:Encoding-XER}
2475
2476\end_inset
2477
2478Encoding XER
2479\layout Standard
2480
2481The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
2482 Markup Language, a text-based format for information exchange.
2483 The encoder routine API comes in two flavors: stdio-based and callback-based.
2484 With the callback-based encoder, the encoding process is very similar to
2485 the DER one, described in Section
2486\begin_inset LatexCommand \vref{sub:Encoding-DER}
2487
2488\end_inset
2489
2490.
2491 The following example uses the definition of write_stream() from up there.
2492\layout LyX-Code
2493
2494/*
2495\layout LyX-Code
2496
2497 * This procedure generates the XML document
2498\layout LyX-Code
2499
2500 * by invoking the XER encoder.
2501\layout LyX-Code
2502
2503 * NOTE: Do not copy this code verbatim!
2504\layout LyX-Code
2505
2506 * If the stdio output is necessary,
2507\layout LyX-Code
2508
2509 * use the xer_fprint() procedure instead.
2510\layout LyX-Code
2511
2512 * See Section
2513\begin_inset LatexCommand \vref{sub:Printing-the-target}
2514
2515\end_inset
2516
2517.
2518\layout LyX-Code
2519
2520 */
2521\layout LyX-Code
2522
2523int
2524\layout LyX-Code
2525
2526print_as_XML(FILE *ostream, Rectangle_t *rect) {
2527\layout LyX-Code
2528
2529 asn_enc_rval_t er; /* Encoder return value */
2530\layout LyX-Code
2531
2532
2533\layout LyX-Code
2534
Lev Walkin1f756282004-09-29 13:29:17 +00002535 er = xer_encode(&asn_DEF_Rect, rect,
Lev Walkin35d446b2004-09-24 20:56:07 +00002536\layout LyX-Code
2537
2538 XER_F_BASIC, /* BASIC-XER or CANONICAL-XER */
2539\layout LyX-Code
2540
2541 write_stream, ostream);
2542\layout LyX-Code
2543
2544
2545\layout LyX-Code
2546
2547 return (er.encoded == -1) ? -1 : 0;
2548\layout LyX-Code
2549
2550}
2551\layout Standard
2552
2553Please look into xer_encoder.h for the precise definition of xer_encode()
2554 and related types.
2555\layout Standard
2556
2557See Section
2558\begin_inset LatexCommand \ref{sub:Printing-the-target}
2559
2560\end_inset
2561
2562 for the example of stdio-based XML encoder and other pretty-printing suggestion
2563s.
2564\layout Subsection
2565
2566
Lev Walkin84291e02004-06-03 03:49:45 +00002567\begin_inset LatexCommand \label{sub:Validating-the-target}
2568
2569\end_inset
2570
2571Validating the target structure
2572\layout Standard
2573
2574Sometimes the target structure needs to be validated.
2575 For example, if the structure was created by the application (as opposed
2576 to being decoded from some external source), some important information
2577 required by the ASN.1 specification might be missing.
2578 On the other hand, the successful decoding of the data from some external
2579 source does not necessarily mean that the data is fully valid either.
2580 It might well be the case that the specification describes some subtype
2581 constraints that were not taken into account during decoding, and it would
2582 actually be useful to perform the last check when the data is ready to
2583 be encoded or when the data has just been decoded to ensure its validity
2584 according to some stricter rules.
2585\layout Standard
2586
2587The asn_check_constraints() function checks the type for various implicit
2588 and explicit constraints.
2589 It is recommended to use asn_check_constraints() function after each decoding
2590 and before each encoding.
2591\layout Standard
2592
2593Please look into constraints.h for the precise definition of asn_check_constraint
2594s() and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00002595\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002596
2597
2598\begin_inset LatexCommand \label{sub:Printing-the-target}
2599
2600\end_inset
2601
2602Printing the target structure
2603\layout Standard
2604
2605There are two ways to print the target structure: either invoke the print_struct
2606 member of the ASN.1 type descriptor, or using the asn_fprint() function,
2607 which is a simpler wrapper of the former:
2608\layout LyX-Code
2609
Lev Walkin1f756282004-09-29 13:29:17 +00002610asn_fprint(stdout, &asn_DEF_Rectangle, rect);
Lev Walkin84291e02004-06-03 03:49:45 +00002611\layout Standard
2612
2613Please look into constr_TYPE.h for the precise definition of asn_fprint()
2614 and related types.
Lev Walkin35d446b2004-09-24 20:56:07 +00002615\layout Standard
2616
2617Another practical alternative to this custom format printing would be to
2618 invoke XER encoder.
2619 The default BASIC-XER encoder performs reasonable formatting for the output
2620 to be useful and human readable.
2621 To invoke the XER decoder in a manner similar to asn_fprint(), use the
2622 xer_fprint() call:
2623\layout LyX-Code
2624
Lev Walkin1f756282004-09-29 13:29:17 +00002625xer_fprint(stdout, &asn_DEF_Rectangle, rect);
Lev Walkin35d446b2004-09-24 20:56:07 +00002626\layout Standard
2627
2628See Section
2629\begin_inset LatexCommand \vref{sub:Encoding-XER}
2630
2631\end_inset
2632
2633 for XML-related details.
Lev Walkin26587ab2004-08-23 15:12:04 +00002634\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002635
2636
2637\begin_inset LatexCommand \label{sub:Freeing-the-target}
2638
2639\end_inset
2640
2641Freeing the target structure
2642\layout Standard
2643
2644Freeing the structure is slightly more complex than it may seem to.
2645 When the ASN.1 structure is freed, all the members of the structure and
2646 their submembers etc etc are recursively freed too.
2647 But it might not be feasible to free the structure itself.
2648 Consider the following case:
2649\layout LyX-Code
2650
2651struct my_figure { /* The custom structure */
2652\layout LyX-Code
2653
2654 int flags; /* <some custom member> */
2655\layout LyX-Code
2656
2657 /* The type is generated by the ASN.1 compiler */
2658\layout LyX-Code
2659
2660
2661\emph on
2662Rectangle_t rect;
2663\layout LyX-Code
2664
2665 /* other members of the structure */
2666\layout LyX-Code
2667
2668};
2669\layout Standard
2670
2671In this example, the application programmer defined a custom structure with
2672 one ASN.1-derived member (rect).
2673 This member is not a reference to the Rectangle_t, but an in-place inclusion
2674 of the Rectangle_t structure.
2675 If the freeing is necessary, the usual procedure of freeing everything
2676 must not be applied to the &rect pointer itself, because it does not point
2677 to the memory block directly allocated by memory allocation routine, but
2678 instead lies within such a block allocated for my_figure structure.
2679\layout Standard
2680
2681To solve this problem, the free_struct routine has the additional argument
2682 (besides the intuitive type descriptor and target structure pointers),
2683 which is the flag specifying whether the outer pointer itself must be freed
2684 (0, default) or it should be left intact (non-zero value).
2685\layout LyX-Code
2686
2687/* Rectangle_t is defined within my_figure */
2688\layout LyX-Code
2689
2690struct my_figure *mf =
2691\series bold
2692...
2693\series default
2694;
2695\layout LyX-Code
2696
2697/*
2698\layout LyX-Code
2699
2700 * Freeing the Rectangle_td
2701\layout LyX-Code
2702
2703 * without freeing the mf->rect pointer
2704\layout LyX-Code
2705
2706 */
2707\layout LyX-Code
2708
Lev Walkin1f756282004-09-29 13:29:17 +00002709asn_DEF_Rectangle->free_struct(
Lev Walkin84291e02004-06-03 03:49:45 +00002710\layout LyX-Code
2711
Lev Walkin1f756282004-09-29 13:29:17 +00002712 &asn_DEF_Rectangle, &mf->rect,
Lev Walkin84291e02004-06-03 03:49:45 +00002713\emph on
27141
2715\emph default
2716 /* !free */);
2717\layout LyX-Code
2718
2719
2720\layout LyX-Code
2721
2722/* Rectangle_t is a stand-alone pointer */
2723\layout LyX-Code
2724
2725Rectangle_t *rect =
2726\series bold
2727...
2728\series default
2729;
2730\layout LyX-Code
2731
2732/*
2733\layout LyX-Code
2734
2735 * Freeing the Rectangle_t
2736\layout LyX-Code
2737
2738 * and freeing the rect pointer
2739\layout LyX-Code
2740
2741 */
2742\layout LyX-Code
2743
Lev Walkin1f756282004-09-29 13:29:17 +00002744asn_DEF_Rectangle->free_struct(
Lev Walkin84291e02004-06-03 03:49:45 +00002745\layout LyX-Code
2746
Lev Walkin1f756282004-09-29 13:29:17 +00002747 &asn_DEF_Rectangle, rect,
Lev Walkin84291e02004-06-03 03:49:45 +00002748\emph on
27490
2750\emph default
2751 /* free the pointer too */);
2752\layout Standard
2753
2754It is safe to invoke the
2755\emph on
2756free_struct
2757\emph default
2758 function with the target structure pointer set to 0 (NULL), the function
2759 will do nothing.
2760\layout Bibliography
Lev Walkin26587ab2004-08-23 15:12:04 +00002761\bibitem [ASN1C]{ASN1C}
2762
Lev Walkin26871b52004-09-08 03:01:06 +00002763The OpenSource ASN.1 Compiler.
Lev Walkin26587ab2004-08-23 15:12:04 +00002764
Lev Walkind3630792004-09-30 22:26:19 +00002765\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c/}
Lev Walkin26587ab2004-08-23 15:12:04 +00002766
2767\end_inset
2768
2769
2770\layout Bibliography
Lev Walkin84291e02004-06-03 03:49:45 +00002771\bibitem [Dub00]{Dub00}
2772
2773Olivier Dubuisson --
2774\emph on
2775ASN.1 Communication between heterogeneous systems
2776\emph default
2777 -- Morgan Kaufmann Publishers, 2000.
2778
2779\begin_inset LatexCommand \htmlurl{http://asn1.elibel.tm.fr/en/book/}
2780
2781\end_inset
2782
2783.
2784 ISBN:0-12-6333361-0.
2785\layout Bibliography
2786\bibitem [ITU-T/ASN.1]{ITU-T/ASN.1}
2787
2788ITU-T Study Group 17 -- Languages for Telecommunication Systems
2789\begin_inset LatexCommand \url{http://www.itu.int/ITU-T/studygroups/com17/languages/}
2790
2791\end_inset
2792
2793
2794\the_end