blob: 695a5e09490fff667f8aa006a6e85037fbdb8dd0 [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},
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}
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
vlmc3bfd812004-09-30 22:26:19 +000049Using the OpenSource 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
vlm60e7ef02004-10-13 09:13:56 +000074href{http://lionet.info/asn1c}{asn1c-0.9.8}}
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
105ASN.1 Basics
vlme18adea2004-06-03 03:49:45 +0000106\layout Chapter
107
vlmcaba97b2004-08-07 05:00:07 +0000108Abstract Syntax Notation: ASN.1
vlme18adea2004-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
vlmcaba97b2004-08-07 05:00:07 +0000122 or the ASN.1 body of standards itself
vlme18adea2004-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.
vlm4cb0edf2005-01-14 11:38:49 +0000137 The ASN.1 specifications are used to achieve the following:
vlme18adea2004-06-03 03:49:45 +0000138\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.
vlm4cb0edf2005-01-14 11:38:49 +0000170 This specification may tell the reader that there exists this kind of data
vlme18adea2004-06-03 03:49:45 +0000171 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
vlm4cb0edf2005-01-14 11:38:49 +0000199) the data: BER, CER, DER and XER, some of them which will be outlined later.
vlme18adea2004-06-03 03:49:45 +0000200\layout Standard
201
202The complete specification must be wrapped in a module, which looks like
203 this:
204\layout LyX-Code
205
vlm4cb0edf2005-01-14 11:38:49 +0000206RectangleModule1
vlme18adea2004-06-03 03:49:45 +0000207\layout LyX-Code
208
209 { iso org(3) dod(6) internet(1) private(4)
210\layout LyX-Code
211
212 enterprise(1) spelio(9363) software(1)
213\layout LyX-Code
214
vlm4cb0edf2005-01-14 11:38:49 +0000215 asn1c(5) docs(2) rectangle(1) 1 }
vlme18adea2004-06-03 03:49:45 +0000216\layout LyX-Code
217
vlmfcec8e52004-08-23 15:12:04 +0000218 DEFINITIONS AUTOMATIC TAGS ::=
vlme18adea2004-06-03 03:49:45 +0000219\layout LyX-Code
220
221BEGIN
222\layout LyX-Code
223
224
225\layout LyX-Code
226
227-- This is a comment which describes nothing.
228\layout LyX-Code
229
230Rectangle ::= SEQUENCE {
231\layout LyX-Code
232
233 height INTEGER, -- Height of the rectangle
234\layout LyX-Code
235
vlmfcec8e52004-08-23 15:12:04 +0000236 width INTEGER -- Width of the rectangle
vlme18adea2004-06-03 03:49:45 +0000237\layout LyX-Code
238
239}
240\layout LyX-Code
241
242
243\layout LyX-Code
244
245END
246\layout Standard
247
vlm4cb0edf2005-01-14 11:38:49 +0000248The module header consists of module name (RectangleModule1), the module
vlmfcec8e52004-08-23 15:12:04 +0000249 object identifier ({...}), a keyword
vlme18adea2004-06-03 03:49:45 +0000250\begin_inset Quotes sld
251\end_inset
252
vlmfcec8e52004-08-23 15:12:04 +0000253DEFINITIONS
254\begin_inset Quotes srd
255\end_inset
256
257, a set of module flags (AUTOMATIC TAGS) and
258\begin_inset Quotes sld
259\end_inset
260
261::= BEGIN
vlme18adea2004-06-03 03:49:45 +0000262\begin_inset Quotes srd
263\end_inset
264
265.
266 The module ends with an
267\begin_inset Quotes sld
268\end_inset
269
270END
271\begin_inset Quotes srd
272\end_inset
273
274 statement.
275\layout Section
276
277Some of the ASN.1 Basic Types
278\layout Subsection
279
280The BOOLEAN type
281\layout Standard
282
283The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF or
284 a similar kind of two-way choice.
285\layout Subsection
286
287The INTEGER type
288\layout Standard
289
290The INTEGER type is a signed natural number type without any restrictions
291 on its size.
292 If the automatic checking on INTEGER value bounds are necessary, the subtype
293 constraints must be used.
294\layout LyX-Code
295
296SimpleInteger ::= INTEGER
297\layout LyX-Code
298
vlmfcec8e52004-08-23 15:12:04 +0000299
300\layout LyX-Code
301
vlme18adea2004-06-03 03:49:45 +0000302-- An integer with a very limited range
303\layout LyX-Code
304
vlmba56d852004-09-29 13:29:17 +0000305SmallPositiveInt ::= INTEGER (0..127)
vlme18adea2004-06-03 03:49:45 +0000306\layout LyX-Code
307
vlmfcec8e52004-08-23 15:12:04 +0000308
309\layout LyX-Code
310
vlme18adea2004-06-03 03:49:45 +0000311-- Integer, negative
312\layout LyX-Code
313
314NegativeInt ::= INTEGER (MIN..0)
315\layout Subsection
316
317The ENUMERATED type
318\layout Standard
319
320The ENUMERATED type is semantically equivalent to the INTEGER type with
321 some integer values explicitly named.
322\layout LyX-Code
323
324FruitId ::= ENUMERATED { apple(1), orange(2) }
325\layout LyX-Code
326
vlmfcec8e52004-08-23 15:12:04 +0000327
328\layout LyX-Code
329
vlme18adea2004-06-03 03:49:45 +0000330-- The numbers in braces are optional,
331\layout LyX-Code
332
vlmfcec8e52004-08-23 15:12:04 +0000333-- the enumeration can be performed
vlme18adea2004-06-03 03:49:45 +0000334\layout LyX-Code
335
336-- automatically by the compiler
337\layout LyX-Code
338
339ComputerOSType ::= ENUMERATED {
340\layout LyX-Code
341
vlmc3bfd812004-09-30 22:26:19 +0000342 FreeBSD, -- acquires value 0
vlme18adea2004-06-03 03:49:45 +0000343\layout LyX-Code
344
vlmc3bfd812004-09-30 22:26:19 +0000345 Windows, -- acquires value 1
vlme18adea2004-06-03 03:49:45 +0000346\layout LyX-Code
347
vlmc3bfd812004-09-30 22:26:19 +0000348 Solaris(5), -- remains 5
vlme18adea2004-06-03 03:49:45 +0000349\layout LyX-Code
350
vlmc3bfd812004-09-30 22:26:19 +0000351 Linux, -- becomes 6
vlme18adea2004-06-03 03:49:45 +0000352\layout LyX-Code
353
vlmc3bfd812004-09-30 22:26:19 +0000354 MacOS -- becomes 7
vlme18adea2004-06-03 03:49:45 +0000355\layout LyX-Code
356
357}
358\layout Subsection
359
360The OCTET STRING type
361\layout Standard
362
363This type models the sequence of 8-bit bytes.
364 This may be used to transmit some opaque data or data serialized by other
365 types of encoders (i.e.
366 video file, photo picture, etc).
367\layout Subsection
368
369The OBJECT IDENTIFIER type
370\layout Standard
371
372The OBJECT IDENTIFIER is used to represent the unique identifier of any
373 object, starting from the very root of the registration tree.
374 If your organization needs to uniquely identify something (a router, a
375 room, a person, a standard, or whatever), you are encouraged to get your
376 own identification subtree at
377\begin_inset LatexCommand \htmlurl{http://www.iana.org/protocols/forms.htm}
378
379\end_inset
380
381.
382\layout Standard
383
384For example, the very first ASN.1 module in this document has the following
385 OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
386\layout LyX-Code
387
388ExampleOID ::= OBJECT IDENTIFIER
389\layout LyX-Code
390
vlmfcec8e52004-08-23 15:12:04 +0000391
392\layout LyX-Code
393
vlm4cb0edf2005-01-14 11:38:49 +0000394rectangleModule1-oid ExampleOID
vlme18adea2004-06-03 03:49:45 +0000395\layout LyX-Code
396
397 ::= { 1 3 6 1 4 1 9363 1 5 2 1 1 }
398\layout LyX-Code
399
vlmfcec8e52004-08-23 15:12:04 +0000400
401\layout LyX-Code
402
vlme18adea2004-06-03 03:49:45 +0000403-- An identifier of the Internet.
404\layout LyX-Code
405
406internet-id OBJECT IDENTIFIER
407\layout LyX-Code
408
409 ::= { iso(1) identified-organization(3)
410\layout LyX-Code
411
412 dod(6) internet(1) }
413\layout Standard
414
415As you see, names are optional.
416\layout Subsection
417
418The RELATIVE-OID type
419\layout Standard
420
421The RELATIVE-OID type has the semantics of a subtree of an OBJECT IDENTIFIER.
422 There may be no need to repeat the whole sequence of numbers from the root
423 of the registration tree where the only thing of interest is some of the
424 tree's subsequence.
425\layout LyX-Code
426
427this-document RELATIVE-OID ::= { docs(2) usage(1) }
428\layout LyX-Code
429
vlmfcec8e52004-08-23 15:12:04 +0000430
431\layout LyX-Code
432
vlme18adea2004-06-03 03:49:45 +0000433this-example RELATIVE-OID ::= {
434\layout LyX-Code
435
436 this-document assorted-examples(0) this-example(1) }
437\layout Section
438
439Some of the ASN.1 String Types
440\layout Subsection
441
442The IA5String type
443\layout Standard
444
445This is essentially the ASCII, with 128 character codes available (7 lower
vlmfcec8e52004-08-23 15:12:04 +0000446 bits of an 8-bit byte).
vlme18adea2004-06-03 03:49:45 +0000447\layout Subsection
448
449The UTF8String type
450\layout Standard
451
452This is the character string which encodes the full Unicode range (4 bytes)
453 using multibyte character sequences.
454\layout Subsection
455
456The NumericString type
457\layout Standard
458
459This type represents the character string with the alphabet consisting of
460 numbers (
461\begin_inset Quotes sld
462\end_inset
463
4640
465\begin_inset Quotes srd
466\end_inset
467
468 to
469\begin_inset Quotes sld
470\end_inset
471
4729
473\begin_inset Quotes srd
474\end_inset
475
476) and a space.
477\layout Subsection
478
479The PrintableString type
480\layout Standard
481
482The character string with the following alphabet: space,
483\begin_inset Quotes sld
484\end_inset
485
486
487\series bold
488'
489\series default
490
491\begin_inset Quotes srd
492\end_inset
493
494 (single quote),
495\begin_inset Quotes sld
496\end_inset
497
498
499\series bold
500(
501\series default
502
503\begin_inset Quotes sld
504\end_inset
505
506,
507\begin_inset Quotes sld
508\end_inset
509
510
511\series bold
512)
513\series default
514
515\begin_inset Quotes srd
516\end_inset
517
518,
519\begin_inset Quotes sld
520\end_inset
521
522
523\series bold
524+
525\series default
526
527\begin_inset Quotes srd
528\end_inset
529
530,
531\begin_inset Quotes sld
532\end_inset
533
vlmba56d852004-09-29 13:29:17 +0000534
535\series bold
vlme18adea2004-06-03 03:49:45 +0000536,
vlmba56d852004-09-29 13:29:17 +0000537\series default
538
vlme18adea2004-06-03 03:49:45 +0000539\begin_inset Quotes srd
540\end_inset
541
542 (comma),
543\begin_inset Quotes sld
544\end_inset
545
546
547\series bold
548-
549\series default
550
551\begin_inset Quotes srd
552\end_inset
553
554,
555\begin_inset Quotes sld
556\end_inset
557
558
559\series bold
560.
561\series default
562
563\begin_inset Quotes srd
564\end_inset
565
566,
567\begin_inset Quotes sld
568\end_inset
569
570
571\series bold
572/
573\series default
574
575\begin_inset Quotes srd
576\end_inset
577
578, digits (
579\begin_inset Quotes sld
580\end_inset
581
5820
583\begin_inset Quotes srd
584\end_inset
585
586 to
587\begin_inset Quotes sld
588\end_inset
589
5909
591\begin_inset Quotes srd
592\end_inset
593
594),
595\begin_inset Quotes sld
596\end_inset
597
598
599\series bold
600:
601\series default
602
603\begin_inset Quotes srd
604\end_inset
605
606,
607\begin_inset Quotes sld
608\end_inset
609
610
611\series bold
612=
613\series default
614
615\begin_inset Quotes srd
616\end_inset
617
618,
619\begin_inset Quotes sld
620\end_inset
621
622
623\series bold
624?
625\series default
626
627\begin_inset Quotes srd
628\end_inset
629
630, upper-case and lower-case letters (
631\begin_inset Quotes sld
632\end_inset
633
634A
635\begin_inset Quotes srd
636\end_inset
637
638 to
639\begin_inset Quotes sld
640\end_inset
641
642Z
643\begin_inset Quotes srd
644\end_inset
645
646 and
647\begin_inset Quotes sld
648\end_inset
649
650a
651\begin_inset Quotes srd
652\end_inset
653
654 to
655\begin_inset Quotes sld
656\end_inset
657
658z
659\begin_inset Quotes srd
660\end_inset
661
vlmba56d852004-09-29 13:29:17 +0000662).
vlme18adea2004-06-03 03:49:45 +0000663\layout Subsection
664
665The VisibleString type
666\layout Standard
667
668The character string with the alphabet which is more or less a subset of
vlmba56d852004-09-29 13:29:17 +0000669 ASCII between the space and the
vlme18adea2004-06-03 03:49:45 +0000670\begin_inset Quotes sld
671\end_inset
672
vlmfcec8e52004-08-23 15:12:04 +0000673
674\series bold
vlme18adea2004-06-03 03:49:45 +0000675~
vlmfcec8e52004-08-23 15:12:04 +0000676\series default
677
vlme18adea2004-06-03 03:49:45 +0000678\begin_inset Quotes srd
679\end_inset
680
vlmba56d852004-09-29 13:29:17 +0000681 symbol (tilde).
682\layout Standard
683
684Alternatively, the alphabet may be described as the PrintableString alphabet
vlmfcec8e52004-08-23 15:12:04 +0000685 presented earlier, plus the following characters:
vlme18adea2004-06-03 03:49:45 +0000686\begin_inset Quotes sld
687\end_inset
688
689
690\series bold
691!
692\series default
693
694\begin_inset Quotes srd
695\end_inset
696
697,
698\begin_inset Quotes sld
699\end_inset
700
701
702\series bold
703
704\begin_inset Quotes srd
705\end_inset
706
707
708\series default
709
710\begin_inset Quotes srd
711\end_inset
712
713,
714\begin_inset Quotes sld
715\end_inset
716
717
718\series bold
719#
720\series default
721
722\begin_inset Quotes srd
723\end_inset
724
725,
726\begin_inset Quotes sld
727\end_inset
728
729
730\series bold
731$
732\series default
733
734\begin_inset Quotes srd
735\end_inset
736
737,
738\begin_inset Quotes sld
739\end_inset
740
741
742\series bold
743%
744\series default
745
746\begin_inset Quotes srd
747\end_inset
748
749,
750\begin_inset Quotes sld
751\end_inset
752
753
754\series bold
755&
756\series default
757
758\begin_inset Quotes srd
759\end_inset
760
761,
762\begin_inset Quotes sld
763\end_inset
764
765
766\series bold
767*
768\series default
769
770\begin_inset Quotes srd
771\end_inset
772
773,
774\begin_inset Quotes sld
775\end_inset
776
777
778\series bold
779;
780\series default
781
782\begin_inset Quotes srd
783\end_inset
784
785,
786\begin_inset Quotes sld
787\end_inset
788
789
790\series bold
791<
792\series default
793
794\begin_inset Quotes srd
795\end_inset
796
797,
798\begin_inset Quotes sld
799\end_inset
800
801
802\series bold
803>
804\series default
805
806\begin_inset Quotes srd
807\end_inset
808
809,
810\begin_inset Quotes sld
811\end_inset
812
813
814\series bold
815[
816\series default
817
818\begin_inset Quotes srd
819\end_inset
820
821,
822\begin_inset Quotes sld
823\end_inset
824
825
826\series bold
827
828\backslash
829
830\series default
831
832\begin_inset Quotes srd
833\end_inset
834
835,
836\begin_inset Quotes sld
837\end_inset
838
839
840\series bold
841]
842\series default
843
844\begin_inset Quotes srd
845\end_inset
846
847,
848\begin_inset Quotes sld
849\end_inset
850
851
852\series bold
853^
854\series default
855
856\begin_inset Quotes srd
857\end_inset
858
859,
860\begin_inset Quotes sld
861\end_inset
862
863
864\series bold
865_
866\series default
867
868\begin_inset Quotes srd
869\end_inset
870
871,
872\begin_inset Quotes sld
873\end_inset
874
875
876\series bold
877`
878\series default
879
880\begin_inset Quotes srd
881\end_inset
882
883 (single left quote),
884\begin_inset Quotes sld
885\end_inset
886
887
888\series bold
889{
890\series default
891
892\begin_inset Quotes srd
893\end_inset
894
895,
896\begin_inset Quotes sld
897\end_inset
898
899
900\series bold
901|
902\series default
903
904\begin_inset Quotes srd
905\end_inset
906
907,
908\begin_inset Quotes sld
909\end_inset
910
911
912\series bold
913}
914\series default
915
916\begin_inset Quotes srd
917\end_inset
918
919,
920\begin_inset Quotes sld
921\end_inset
922
vlmfcec8e52004-08-23 15:12:04 +0000923
924\series bold
vlme18adea2004-06-03 03:49:45 +0000925~
vlmfcec8e52004-08-23 15:12:04 +0000926\series default
927
vlme18adea2004-06-03 03:49:45 +0000928\begin_inset Quotes srd
929\end_inset
930
931.
932\layout Section
933
934ASN.1 Constructed Types
935\layout Subsection
936
937The SEQUENCE type
938\layout Standard
939
940This is an ordered collection of other simple or constructed types.
941 The SEQUENCE constructed type resembles the C
942\begin_inset Quotes sld
943\end_inset
944
945struct
946\begin_inset Quotes srd
947\end_inset
948
949 statement.
950\layout LyX-Code
951
952Address ::= SEQUENCE {
953\layout LyX-Code
954
955 -- The apartment number may be omitted
956\layout LyX-Code
957
vlmc3bfd812004-09-30 22:26:19 +0000958 apartmentNumber NumericString OPTIONAL,
vlme18adea2004-06-03 03:49:45 +0000959\layout LyX-Code
960
961 streetName PrintableString,
962\layout LyX-Code
963
964 cityName PrintableString,
965\layout LyX-Code
966
967 stateName PrintableString,
968\layout LyX-Code
969
970 -- This one may be omitted too
971\layout LyX-Code
972
973 zipNo NumericString OPTIONAL
974\layout LyX-Code
975
976}
977\layout Subsection
978
979The SET type
980\layout Standard
981
982This is a collection of other simple or constructed types.
983 Ordering is not important.
984 The data may arrive in the order which is different from the order of specifica
985tion.
986 Data is encoded in the order not necessarily corresponding to the order
987 of specification.
988\layout Subsection
989
990The CHOICE type
991\layout Standard
992
993This type is just a choice between the subtypes specified in it.
994 The CHOICE type contains at most one of the subtypes specified, and it
995 is always implicitly known which choice is being decoded or encoded.
996 This one resembles the C
997\begin_inset Quotes sld
998\end_inset
999
1000union
1001\begin_inset Quotes srd
1002\end_inset
1003
1004 statement.
1005\layout Standard
1006
1007The following type defines a response code, which may be either an integer
1008 code or a boolean
1009\begin_inset Quotes sld
1010\end_inset
1011
1012true
1013\begin_inset Quotes srd
1014\end_inset
1015
1016/
1017\begin_inset Quotes srd
1018\end_inset
1019
1020false
1021\begin_inset Quotes srd
1022\end_inset
1023
1024 code.
1025\layout LyX-Code
1026
1027ResponseCode ::= CHOICE {
1028\layout LyX-Code
1029
1030 intCode INTEGER,
1031\layout LyX-Code
1032
1033 boolCode BOOLEAN
1034\layout LyX-Code
1035
1036}
1037\layout LyX-Code
1038
1039\layout Subsection
1040
1041The SEQUENCE OF type
1042\layout Standard
1043
1044This one is the list (array) of simple or constructed types:
1045\layout LyX-Code
1046
1047-- Example 1
1048\layout LyX-Code
1049
1050ManyIntegers ::= SEQUENCE OF INTEGER
1051\layout LyX-Code
1052
vlmfcec8e52004-08-23 15:12:04 +00001053
1054\layout LyX-Code
1055
vlme18adea2004-06-03 03:49:45 +00001056-- Example 2
1057\layout LyX-Code
1058
1059ManyRectangles ::= SEQUENCE OF Rectangle
1060\layout LyX-Code
1061
vlmfcec8e52004-08-23 15:12:04 +00001062
1063\layout LyX-Code
1064
vlme18adea2004-06-03 03:49:45 +00001065-- More complex example:
1066\layout LyX-Code
1067
1068-- an array of structures defined in place.
1069\layout LyX-Code
1070
1071ManyCircles ::= SEQUENCE OF SEQUENCE {
1072\layout LyX-Code
1073
1074 radius INTEGER
1075\layout LyX-Code
1076
1077 }
1078\layout Subsection
1079
1080The SET OF type
1081\layout Standard
1082
1083The SET OF type models the bag of structures.
1084 It resembles the SEQUENCE OF type, but the order is not important: i.e.
1085 the elements may arrive in the order which is not necessarily the same
1086 as the in-memory order on the remote machines.
1087\layout LyX-Code
1088
1089-- A set of structures defined elsewhere
1090\layout LyX-Code
1091
1092SetOfApples :: SET OF Apple
1093\layout LyX-Code
1094
vlmfcec8e52004-08-23 15:12:04 +00001095
1096\layout LyX-Code
1097
vlme18adea2004-06-03 03:49:45 +00001098-- Set of integers encoding the kind of a fruit
1099\layout LyX-Code
1100
1101FruitBag ::= SET OF ENUMERATED { apple, orange }
vlmcaba97b2004-08-07 05:00:07 +00001102\layout Part
1103
1104Using the ASN.1 Compiler
vlme18adea2004-06-03 03:49:45 +00001105\layout Chapter
1106
vlmcaba97b2004-08-07 05:00:07 +00001107Introduction to the ASN.1 Compiler
vlme18adea2004-06-03 03:49:45 +00001108\layout Standard
1109
1110The purpose of the ASN.1 compiler, of which this document is part, is to
1111 convert the ASN.1 specifications to some other target language (currently,
1112 only C is supported
1113\begin_inset Foot
1114collapsed false
1115
1116\layout Standard
1117
1118C++ is
1119\begin_inset Quotes sld
1120\end_inset
1121
1122supported
1123\begin_inset Quotes srd
1124\end_inset
1125
vlmfcec8e52004-08-23 15:12:04 +00001126 too, as long as an class-based approach is not a definitive factor.
vlme18adea2004-06-03 03:49:45 +00001127\end_inset
1128
1129).
1130 The compiler reads the specification and emits a series of target language
1131 structures and surrounding maintenance code.
1132 For example, the C structure which may be created by compiler to represent
1133 the simple
1134\emph on
1135Rectangle
1136\emph default
1137 specification defined earlier in this document, may look like this
1138\begin_inset Foot
1139collapsed false
1140
1141\layout Standard
1142
1143
1144\emph on
vlm03af0f02004-09-14 12:48:17 +00001145-fnative-types
vlme18adea2004-06-03 03:49:45 +00001146\emph default
1147 compiler option is used to produce basic C
1148\emph on
1149int
1150\emph default
vlmfcec8e52004-08-23 15:12:04 +00001151 types instead of infinite width INTEGER_t structures.
1152 See Table
1153\begin_inset LatexCommand \vref{cap:asn1c-cmdopts}
1154
1155\end_inset
1156
1157.
vlme18adea2004-06-03 03:49:45 +00001158\end_inset
1159
1160:
1161\layout LyX-Code
1162
1163typedef struct Rectangle_s {
1164\layout LyX-Code
1165
1166 int height;
1167\layout LyX-Code
1168
1169 int width;
1170\layout LyX-Code
1171
1172} Rectangle_t;
1173\layout Standard
1174
1175This would not be of much value for such a simple specification, so the
1176 compiler goes further and actually produces the code which fills in this
vlmfcec8e52004-08-23 15:12:04 +00001177 structure by parsing the opaque binary
vlme18adea2004-06-03 03:49:45 +00001178\begin_inset Foot
vlmfcec8e52004-08-23 15:12:04 +00001179collapsed true
vlme18adea2004-06-03 03:49:45 +00001180
1181\layout Standard
1182
1183BER, CER and DER encodings are binary.
1184 However, the XER encoding is text (XML) based.
1185\end_inset
1186
1187 data provided in some buffer.
1188 It also produces the code that takes this structure as an argument and
1189 performs structure serialization by emitting a series of bytes.
vlmfcec8e52004-08-23 15:12:04 +00001190\layout Chapter
vlme18adea2004-06-03 03:49:45 +00001191
1192Quick start
1193\layout Standard
1194
vlmfcec8e52004-08-23 15:12:04 +00001195After building and installing the compiler, the
1196\emph on
1197asn1c
1198\begin_inset Foot
1199collapsed false
1200
1201\layout Standard
1202
1203The 1 symbol in asn
1204\series bold
12051
1206\series default
1207c is a digit, not an
1208\begin_inset Quotes sld
1209\end_inset
1210
1211ell
1212\begin_inset Quotes srd
1213\end_inset
1214
1215 letter.
1216\end_inset
1217
1218
1219\emph default
1220 command may be used to compile the ASN.1 specification
vlme18adea2004-06-03 03:49:45 +00001221\begin_inset Foot
1222collapsed false
1223
1224\layout Standard
1225
1226This is probably
1227\series bold
1228not
1229\series default
1230 what you want to try out right now -- read through the rest of this chapter
vlmfcec8e52004-08-23 15:12:04 +00001231 to find out about
1232\series bold
1233-P
1234\series default
1235 and
1236\series bold
1237-R
1238\series default
1239 options.
vlme18adea2004-06-03 03:49:45 +00001240\end_inset
1241
1242:
1243\layout LyX-Code
1244
1245asn1c
1246\emph on
1247<spec.asn1>
1248\layout Standard
1249
vlmfcec8e52004-08-23 15:12:04 +00001250If several specifications contain interdependencies, all of the files must
1251 be specified altogether:
vlme18adea2004-06-03 03:49:45 +00001252\layout LyX-Code
1253
1254asn1c
1255\emph on
1256<spec1.asn1> <spec2.asn1> ...
1257\layout Standard
1258
vlmfcec8e52004-08-23 15:12:04 +00001259The compiler
1260\series bold
1261-E
1262\series default
1263 and
1264\series bold
1265-EF
1266\series default
1267 options are used for testing the parser and the semantic fixer, respectively.
1268 These options will instruct the compiler to dump out the parsed (and fixed,
1269 if
1270\series bold
1271-F
1272\series default
1273 is involved) ASN.1 specification as it was "understood" by the compiler.
1274 It might be useful to check whether a particular syntactic construction
vlme18adea2004-06-03 03:49:45 +00001275 is properly supported by the compiler.
1276\layout LyX-Code
1277
vlmfcec8e52004-08-23 15:12:04 +00001278asn1c
1279\series bold
1280-EF
1281\series default
1282
vlme18adea2004-06-03 03:49:45 +00001283\emph on
1284<spec-to-test.asn1>
1285\layout Standard
1286
vlmfcec8e52004-08-23 15:12:04 +00001287The
1288\series bold
1289-P
1290\series default
1291 option is used to dump the compiled output on the screen instead of creating
1292 a bunch of .c and .h files on disk in the current directory.
1293 You would probably want to start with
1294\series bold
1295-P
1296\series default
1297 option instead of creating a mess in your current directory.
1298 Another option,
1299\series bold
1300-R
1301\series default
1302, asks compiler to only generate the files which need to be generated, and
1303 supress linking in the numerous support files.
1304\layout Standard
1305
1306Print the compiled output instead of creating multiple source files:
1307\layout LyX-Code
1308
1309asn1c
1310\series bold
1311-P
1312\series default
1313
1314\emph on
1315<spec-to-compile-and-print.asn1>
1316\layout LyX-Code
1317
1318\layout Chapter
1319
1320Using the ASN.1 Compiler
vlme18adea2004-06-03 03:49:45 +00001321\layout Section
1322
vlmfcec8e52004-08-23 15:12:04 +00001323Command-line options
1324\layout Standard
1325
1326The Table
1327\begin_inset LatexCommand \vref{cap:asn1c-cmdopts}
1328
1329\end_inset
1330
1331 summarizes various options affecting the compiler's behavior.
1332\layout Standard
1333
1334
1335\begin_inset Float table
1336wide false
vlm12c8f692004-09-06 08:07:29 +00001337collapsed false
vlmfcec8e52004-08-23 15:12:04 +00001338
1339\layout Standard
1340
1341
1342\begin_inset Tabular
vlm60e7ef02004-10-13 09:13:56 +00001343<lyxtabular version="3" rows="22" columns="2">
vlmc3bfd812004-09-30 22:26:19 +00001344<features islongtable="true">
vlmfcec8e52004-08-23 15:12:04 +00001345<column alignment="left" valignment="top" leftline="true" width="0">
1346<column alignment="block" valignment="top" leftline="true" rightline="true" width="3in">
1347<row topline="true" bottomline="true">
1348<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1349\begin_inset Text
1350
1351\layout Standard
1352
1353
1354\series bold
1355Overall Options
1356\end_inset
1357</cell>
1358<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1359\begin_inset Text
1360
1361\layout Standard
1362
1363
1364\series bold
1365Description
1366\end_inset
1367</cell>
1368</row>
1369<row topline="true">
1370<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1371\begin_inset Text
1372
1373\layout Standard
1374
1375-E
1376\end_inset
1377</cell>
1378<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1379\begin_inset Text
1380
1381\layout Standard
1382
1383
1384\size small
1385Stop after the parsing stage and print the reconstructed ASN.1 specification
1386 code to the standard output.
1387\end_inset
1388</cell>
1389</row>
1390<row topline="true">
1391<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1392\begin_inset Text
1393
1394\layout Standard
1395
1396-F
1397\end_inset
1398</cell>
1399<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1400\begin_inset Text
1401
1402\layout Standard
1403
1404
1405\size small
1406Used together with -E, instructs the compiler to stop after the ASN.1 syntax
1407 tree fixing stage and dump the reconstructed ASN.1 specification to the
1408 standard output.
1409\end_inset
1410</cell>
1411</row>
1412<row topline="true">
1413<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1414\begin_inset Text
1415
1416\layout Standard
1417
1418-P
1419\end_inset
1420</cell>
1421<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1422\begin_inset Text
1423
1424\layout Standard
1425
1426
1427\size small
1428Dump the compiled output to the standard output instead of cre- ating the
1429 target language files on disk.
1430\end_inset
1431</cell>
1432</row>
1433<row topline="true">
1434<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1435\begin_inset Text
1436
1437\layout Standard
1438
1439-R
1440\end_inset
1441</cell>
1442<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1443\begin_inset Text
1444
1445\layout Standard
1446
1447
1448\size small
1449Restrict the compiler to generate only the ASN.1 tables, omit- ting the usual
1450 support code.
1451\end_inset
1452</cell>
1453</row>
vlm60e7ef02004-10-13 09:13:56 +00001454<row topline="true">
vlmfcec8e52004-08-23 15:12:04 +00001455<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1456\begin_inset Text
1457
1458\layout Standard
1459
1460-S
1461\emph on
1462<directory>
1463\end_inset
1464</cell>
1465<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1466\begin_inset Text
1467
1468\layout Standard
1469
1470
1471\size small
1472Use the specified directory with ASN.1 skeleton files.
1473\end_inset
1474</cell>
1475</row>
vlm60e7ef02004-10-13 09:13:56 +00001476<row topline="true" bottomline="true">
1477<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1478\begin_inset Text
1479
1480\layout Standard
1481
1482-X
1483\end_inset
1484</cell>
1485<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1486\begin_inset Text
1487
1488\layout Standard
1489
1490Generate the XML DTD for the specified ASN.1 modules.
1491\end_inset
1492</cell>
1493</row>
vlmfcec8e52004-08-23 15:12:04 +00001494<row topline="true">
1495<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1496\begin_inset Text
1497
1498\layout Standard
1499
1500
1501\series bold
1502Warning Options
1503\end_inset
1504</cell>
1505<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1506\begin_inset Text
1507
1508\layout Standard
1509
1510
1511\series bold
1512Description
1513\end_inset
1514</cell>
1515</row>
1516<row topline="true">
1517<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1518\begin_inset Text
1519
1520\layout Standard
1521
1522-Werror
1523\end_inset
1524</cell>
1525<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1526\begin_inset Text
1527
1528\layout Standard
1529
1530
1531\size small
1532Treat warnings as errors; abort if any warning is produced.
1533\end_inset
1534</cell>
1535</row>
1536<row topline="true">
1537<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1538\begin_inset Text
1539
1540\layout Standard
1541
1542-Wdebug-lexer
1543\end_inset
1544</cell>
1545<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1546\begin_inset Text
1547
1548\layout Standard
1549
1550
1551\size small
1552Enable lexer debugging during the ASN.1 parsing stage.
1553\end_inset
1554</cell>
1555</row>
1556<row topline="true">
1557<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1558\begin_inset Text
1559
1560\layout Standard
1561
1562-Wdebug-fixer
1563\end_inset
1564</cell>
1565<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1566\begin_inset Text
1567
1568\layout Standard
1569
1570
1571\size small
1572 Enable ASN.1 syntax tree fixer debugging during the fixing stage.
1573\end_inset
1574</cell>
1575</row>
1576<row topline="true" bottomline="true">
1577<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1578\begin_inset Text
1579
1580\layout Standard
1581
1582-Wdebug-compiler
1583\end_inset
1584</cell>
1585<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1586\begin_inset Text
1587
1588\layout Standard
1589
1590
1591\size small
1592Enable debugging during the actual compile time.
1593\end_inset
1594</cell>
1595</row>
1596<row topline="true">
1597<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1598\begin_inset Text
1599
1600\layout Standard
1601
1602
1603\series bold
1604Language Options
1605\end_inset
1606</cell>
1607<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1608\begin_inset Text
1609
1610\layout Standard
1611
1612
1613\series bold
1614Description
1615\end_inset
1616</cell>
1617</row>
1618<row topline="true">
1619<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1620\begin_inset Text
1621
1622\layout Standard
1623
vlm12c8f692004-09-06 08:07:29 +00001624-fall-defs-global
1625\end_inset
1626</cell>
1627<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1628\begin_inset Text
1629
1630\layout Standard
1631
vlmba56d852004-09-29 13:29:17 +00001632Normally the compiler hides the definitions (asn_DEF_xxx) of the inner structure
1633 elements (members of SEQUENCE, SET and other types).
vlm12c8f692004-09-06 08:07:29 +00001634 This option makes all such definitions global.
vlmba56d852004-09-29 13:29:17 +00001635 Enabling this option may pollute the namespace by making lots of asn_DEF_xxx
vlm12c8f692004-09-06 08:07:29 +00001636 structures globally visible, but will allow you to manipulate (encode and
1637 decode) the individual members of any complex ASN.1 structure.
1638\end_inset
1639</cell>
1640</row>
1641<row topline="true">
1642<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1643\begin_inset Text
1644
1645\layout Standard
1646
vlmfcec8e52004-08-23 15:12:04 +00001647-fbless-SIZE
1648\end_inset
1649</cell>
1650<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1651\begin_inset Text
1652
1653\layout Standard
1654
1655
1656\size small
1657Allow SIZE() constraint for INTEGER, ENUMERATED, and other types for which
1658 this constraint is normally prohibited by the standard.
1659 This is a violation of an ASN.1 standard and compiler may fail to produce
1660 the meaningful code.
1661\end_inset
1662</cell>
1663</row>
1664<row topline="true">
1665<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1666\begin_inset Text
1667
1668\layout Standard
1669
vlm03af0f02004-09-14 12:48:17 +00001670-fnative-types
vlmfcec8e52004-08-23 15:12:04 +00001671\end_inset
1672</cell>
1673<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1674\begin_inset Text
1675
1676\layout Standard
1677
1678
1679\size small
vlm03af0f02004-09-14 12:48:17 +00001680Use the native machine's data types (int, double) whenever possible, instead
vlmc3bfd812004-09-30 22:26:19 +00001681 of the compound INTEGER_t, ENUMERATED_t and REAL_t types.
vlmfcec8e52004-08-23 15:12:04 +00001682
1683\end_inset
1684</cell>
1685</row>
1686<row topline="true">
1687<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1688\begin_inset Text
1689
1690\layout Standard
1691
vlmbfc49bd2004-09-26 13:13:13 +00001692-fno-constraints
1693\end_inset
1694</cell>
1695<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1696\begin_inset Text
1697
1698\layout Standard
1699
1700Do not generate ASN.1 subtype constraint checking code.
1701 This may make a shorter executable.
1702\end_inset
1703</cell>
1704</row>
1705<row topline="true">
1706<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1707\begin_inset Text
1708
1709\layout Standard
1710
vlmfcec8e52004-08-23 15:12:04 +00001711-funnamed-unions
1712\end_inset
1713</cell>
1714<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1715\begin_inset Text
1716
1717\layout Standard
1718
1719
1720\size small
1721Enable unnamed unions in the definitions of target language's structures.
1722\end_inset
1723</cell>
1724</row>
1725<row topline="true" bottomline="true">
1726<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1727\begin_inset Text
1728
1729\layout Standard
1730
1731-ftypes88
1732\end_inset
1733</cell>
1734<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1735\begin_inset Text
1736
1737\layout Standard
1738
1739
1740\size small
vlmc3bfd812004-09-30 22:26:19 +00001741Pretend to support only ASN.1:1988 embedded types.
1742 Certain reserved words, such as UniversalString and BMPString, become ordinary
1743 type references and may be redefined by the specification.
vlmfcec8e52004-08-23 15:12:04 +00001744\end_inset
1745</cell>
1746</row>
vlmc3bfd812004-09-30 22:26:19 +00001747<row topline="true" newpage="true">
vlmfcec8e52004-08-23 15:12:04 +00001748<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1749\begin_inset Text
1750
1751\layout Standard
1752
1753
1754\series bold
1755Output Options
1756\end_inset
1757</cell>
1758<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1759\begin_inset Text
1760
1761\layout Standard
1762
1763
1764\series bold
1765Description
1766\end_inset
1767</cell>
1768</row>
1769<row topline="true">
1770<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1771\begin_inset Text
1772
1773\layout Standard
1774
1775-print-constraints
1776\end_inset
1777</cell>
1778<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1779\begin_inset Text
1780
1781\layout Standard
1782
1783
1784\size small
1785When -EF are also specified, this option forces the compiler to explain
1786 its internal understanding of subtype constraints.
1787\end_inset
1788</cell>
1789</row>
1790<row topline="true" bottomline="true">
1791<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1792\begin_inset Text
1793
1794\layout Standard
1795
1796-print-lines
1797\end_inset
1798</cell>
1799<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1800\begin_inset Text
1801
1802\layout Standard
1803
1804
1805\size small
1806Generate "-- #line" comments in -E output.
1807\end_inset
1808</cell>
1809</row>
1810</lyxtabular>
1811
1812\end_inset
1813
1814
1815\layout Caption
1816
1817
1818\begin_inset LatexCommand \label{cap:asn1c-cmdopts}
1819
1820\end_inset
1821
1822The list of asn1c command line options
1823\end_inset
1824
1825
1826\layout Section
vlme18adea2004-06-03 03:49:45 +00001827
1828Recognizing compiler output
1829\layout Standard
1830
1831After compiling, the following entities will be created in your current
1832 directory:
1833\layout Itemize
1834
1835A set of .c and .h files, generally a single pair for each type defined in
1836 the ASN.1 specifications.
1837 These files will be named similarly to the ASN.1 types (
1838\emph on
1839Rectangle.c
1840\emph default
1841 and
1842\emph on
1843Rectangle.h
1844\emph default
1845 for the specification defined in the beginning of this document).
1846\layout Itemize
1847
1848A set of helper .c and .h files which contain generic encoders, decoders and
1849 other useful routines.
vlmfcec8e52004-08-23 15:12:04 +00001850 There will be quite a few of them, some of them even are not always necessary,
1851 but the overall amount of code after compiling will be rather small anyway.
vlme18adea2004-06-03 03:49:45 +00001852\layout Standard
1853
1854It is your responsibility to create .c file with the
1855\emph on
1856 int main()
1857\emph default
1858 routine and the Makefile (if needed).
1859 Compiler helps you with the latter by creating the Makefile.am.sample, containing
1860 the skeleton definition for the automake, should you want to use autotools.
1861\layout Standard
1862
1863In other words, after compiling the Rectangle module, you have the following
1864 set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h,
1865\series bold
1866\SpecialChar \ldots{}
1867
1868\series default
1869 }, where
1870\series bold
1871
1872\begin_inset Quotes sld
1873\end_inset
1874
1875\SpecialChar \ldots{}
1876
1877\begin_inset Quotes srd
1878\end_inset
1879
1880
1881\series default
1882 stands for the set of additional
1883\begin_inset Quotes sld
1884\end_inset
1885
1886helper
1887\begin_inset Quotes srd
1888\end_inset
1889
1890 files created by the compiler.
1891 If you add the simple file with the
1892\emph on
1893int main()
1894\emph default
1895 routine, it would even be possible to compile everything with the single
1896 instruction:
1897\layout LyX-Code
1898
1899cc -o rectangle *.c # It could be
1900\emph on
1901that
1902\emph default
1903 simple
1904\begin_inset Foot
1905collapsed false
1906
1907\layout Standard
1908
1909Provided that you've also created a .c file with the
1910\emph on
1911int main()
1912\emph default
1913 routine.
1914\end_inset
1915
1916
vlmfcec8e52004-08-23 15:12:04 +00001917\layout Section
vlme18adea2004-06-03 03:49:45 +00001918
vlmc3bfd812004-09-30 22:26:19 +00001919Invoking the ASN.1 helper code from an application
1920\begin_inset OptArg
1921collapsed true
1922
1923\layout Standard
1924
1925Invoking the helper code
1926\end_inset
1927
1928
vlme18adea2004-06-03 03:49:45 +00001929\layout Standard
1930
vlmfcec8e52004-08-23 15:12:04 +00001931First of all, you should to include one or more header files into your applicati
1932on.
1933 For our Rectangle module, including the Rectangle.h file is enough:
vlme18adea2004-06-03 03:49:45 +00001934\layout LyX-Code
1935
1936#include <Rectangle.h>
1937\layout Standard
1938
1939The header files defines the C structure corresponding to the ASN.1 definition
1940 of a rectangle and the declaration of the ASN.1 type descriptor, which is
1941 used as an argument to most of the functions provided by the ASN.1 module.
1942 For example, here is the code which frees the Rectangle_t structure:
1943\layout LyX-Code
1944
vlmfcec8e52004-08-23 15:12:04 +00001945Rectangle_t *rect = ...;
1946\layout LyX-Code
1947
1948
vlme18adea2004-06-03 03:49:45 +00001949\layout LyX-Code
1950
vlmba56d852004-09-29 13:29:17 +00001951asn_DEF_Rectangle->free_struct(&asn_DEF_Rectangle,
vlme18adea2004-06-03 03:49:45 +00001952\layout LyX-Code
1953
1954 rect, 0);
1955\layout Standard
1956
1957This code defines a
1958\emph on
1959rect
1960\emph default
1961 pointer which points to the Rectangle_t structure which needs to be freed.
1962 The second line invokes the generic free_struct routine created specifically
1963 for this Rectangle_t structure.
1964 The
1965\emph on
vlmba56d852004-09-29 13:29:17 +00001966asn_DEF_Rectangle
vlme18adea2004-06-03 03:49:45 +00001967\emph default
1968 is the type descriptor, which holds a collection of generic routines to
1969 deal with the Rectangle_t structure.
1970\layout Standard
1971
1972There are several generic functions available:
1973\layout Description
1974
vlme18adea2004-06-03 03:49:45 +00001975ber_decoder This is the generic
1976\emph on
1977restartable
1978\begin_inset Foot
1979collapsed false
1980
1981\layout Standard
1982
1983Restartable means that if the decoder encounters the end of the buffer,
1984 it will fail, but may later be invoked again with the rest of the buffer
1985 to continue decoding.
1986\end_inset
1987
1988
1989\emph default
1990BER decoder (Basic Encoding Rules).
1991 This decoder would create and/or fill the target structure for you.
1992 Please refer to Section
1993\begin_inset LatexCommand \ref{sub:Decoding-BER}
1994
1995\end_inset
1996
1997.
1998\layout Description
1999
2000der_encoder This is the generic DER encoder (Distinguished Encoding Rules).
vlm68d43e92004-09-24 20:56:07 +00002001 This encoder will take the target structure and encode it into a series
vlme18adea2004-06-03 03:49:45 +00002002 of bytes.
2003 Please refer to Section
2004\begin_inset LatexCommand \ref{sub:Encoding-DER}
2005
2006\end_inset
2007
2008.
2009\layout Description
2010
vlm68d43e92004-09-24 20:56:07 +00002011xer_encoder This is the generic XER encoder (XML Encoding Rules).
2012 This encoder will take the target structure and represent it as an XML
2013 (text) document.
2014 Please refer to Section
2015\begin_inset LatexCommand \ref{sub:Encoding-XER}
2016
2017\end_inset
2018
2019.
2020\layout Description
2021
2022check_constraints Check that the contents of the target structure are semantical
2023ly valid and constrained to appropriate implicit or explicit subtype constraints.
2024 Please refer to Section
2025\begin_inset LatexCommand \vref{sub:Validating-the-target}
2026
2027\end_inset
2028
2029.
2030\layout Description
2031
vlme18adea2004-06-03 03:49:45 +00002032print_struct This function convert the contents of the passed target structure
2033 into human readable form.
2034 This form is not formal and cannot be converted back into the structure,
2035 but it may turn out to be useful for debugging or quick-n-dirty printing.
2036 Please refer to Section
2037\begin_inset LatexCommand \ref{sub:Printing-the-target}
2038
2039\end_inset
2040
2041.
2042\layout Description
2043
2044free_struct This is a generic disposal which frees the target structure.
2045 Please refer to Section
2046\begin_inset LatexCommand \ref{sub:Freeing-the-target}
2047
2048\end_inset
2049
2050.
2051\layout Standard
2052
vlm68d43e92004-09-24 20:56:07 +00002053check_constraints Check that the contents of the target structure are semantical
2054ly valid and constrained to appropriate implicit or explicit subtype constraints.
2055 Please refer to Section
2056\begin_inset LatexCommand \vref{sub:Validating-the-target}
2057
2058\end_inset
2059
2060.
2061\layout Standard
2062
vlme18adea2004-06-03 03:49:45 +00002063Each of the above function takes the type descriptor (
2064\emph on
vlmba56d852004-09-29 13:29:17 +00002065asn_DEF_\SpecialChar \ldots{}
vlme18adea2004-06-03 03:49:45 +00002066
2067\emph default
2068) and the target structure (
2069\emph on
2070rect
2071\emph default
2072, in the above example).
2073 The target structure is typically created by the generic BER decoder or
2074 by the application itself.
2075\layout Standard
2076
2077Here is how the buffer can be deserialized into the structure:
2078\layout LyX-Code
2079
2080Rectangle_t *
2081\layout LyX-Code
2082
vlmfcec8e52004-08-23 15:12:04 +00002083simple_deserializer(const void *buffer, size_t buf_size) {
vlme18adea2004-06-03 03:49:45 +00002084\layout LyX-Code
2085
2086 Rectangle_t *rect = 0; /* Note this 0! */
2087\layout LyX-Code
2088
vlmaf2155e2004-10-20 15:48:55 +00002089 asn_dec_rval_t rval;
vlme18adea2004-06-03 03:49:45 +00002090\layout LyX-Code
2091
2092
2093\layout LyX-Code
2094
vlmba56d852004-09-29 13:29:17 +00002095 rval = asn_DEF_Rectangle->ber_decoder(0,
vlme18adea2004-06-03 03:49:45 +00002096\layout LyX-Code
2097
vlmba56d852004-09-29 13:29:17 +00002098 &asn_DEF_Rectangle,
vlme18adea2004-06-03 03:49:45 +00002099\layout LyX-Code
2100
2101 (void **)&rect,
2102\layout LyX-Code
2103
2104 buffer, buf_size,
2105\layout LyX-Code
2106
2107 0);
2108\layout LyX-Code
2109
2110
2111\layout LyX-Code
2112
2113 if(rval
2114\series bold
2115.code
2116\series default
2117 == RC_OK) {
2118\layout LyX-Code
2119
2120 return rect; /* Decoding succeeded */
2121\layout LyX-Code
2122
2123 } else {
2124\layout LyX-Code
2125
vlmfcec8e52004-08-23 15:12:04 +00002126 /* Free partially decoded rect */
2127\layout LyX-Code
2128
vlmba56d852004-09-29 13:29:17 +00002129 asn_DEF_Rectangle->free_struct(
vlme18adea2004-06-03 03:49:45 +00002130\layout LyX-Code
2131
vlmba56d852004-09-29 13:29:17 +00002132 &asn_DEF_Rectangle, rect, 0);
vlme18adea2004-06-03 03:49:45 +00002133\layout LyX-Code
2134
2135 return 0;
2136\layout LyX-Code
2137
2138 }
2139\layout LyX-Code
2140
2141}
2142\layout Standard
2143
2144The code above defines a function,
2145\emph on
2146simple_deserializer
2147\emph default
2148, which takes a buffer and its length and expected to return a pointer to
2149 the Rectangle_t structure.
2150 Inside, it tries to convert the bytes passed into the target structure
2151 (rect) using the generic BER decoder and returns the rect pointer afterwards.
2152 If the structure cannot be deserialized, it frees the memory which might
2153 be left allocated by the unfinished
2154\emph on
2155ber_decoder
2156\emph default
2157 routine and returns NULL.
2158
2159\series bold
2160This freeing is necessary
2161\series default
2162 because the ber_decoder is a restartable procedure, and may fail just because
2163 there is more data needs to be provided before decoding could be finalized.
2164 The code above obviously does not take into account the way the
2165\emph on
2166ber_decoder
2167\emph default
2168 failed, so the freeing is necessary because the part of the buffer may
2169 already be decoded into the structure by the time something goes wrong.
2170\layout Standard
2171
2172Restartable decoding is a little bit trickier: you need to provide the old
2173 target structure pointer (which might be already half-decoded) and react
2174 on RC_WMORE return code.
2175 This will be explained later in Section
2176\begin_inset LatexCommand \vref{sub:Decoding-BER}
2177
2178\end_inset
2179
2180
vlmfcec8e52004-08-23 15:12:04 +00002181\layout Subsection
vlme18adea2004-06-03 03:49:45 +00002182
2183
2184\begin_inset LatexCommand \label{sub:Decoding-BER}
2185
2186\end_inset
2187
2188Decoding BER
2189\layout Standard
2190
2191The Basic Encoding Rules describe the basic way how the structure can be
2192 encoded and decoded.
2193 Several other encoding rules (CER, DER) define a more restrictive versions
2194 of BER, so the generic BER parser is also capable of decoding the data
2195 encoded by CER and DER encoders.
2196 The opposite is not true.
2197\layout Standard
2198
2199The ASN.1 compiler provides the generic BER decoder which is implicitly capable
2200 of decoding BER, CER and DER encoded data.
2201\layout Standard
2202
2203The decoder is restartable (stream-oriented), which means that in case the
2204 buffer has less data than it is expected, the decoder will process whatever
2205 it is available and ask for more data to be provided.
2206 Please note that the decoder may actually process less data than it is
2207 given in the buffer, which means that you should be able to make the next
2208 buffer contain the unprocessed part of the previous buffer.
2209\layout Standard
2210
2211Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
2212\layout Itemize
2213
2214You may concatenate these buffers and feed the BER decoder with 300 bytes
2215 of data, or
2216\layout Itemize
2217
2218You may feed it the first buffer of 100 bytes of data, realize that the
2219 ber_decoder consumed only 95 bytes from it and later feed the decoder with
2220 205 bytes buffer which consists of 5 unprocessed bytes from the first buffer
2221 and the latter 200 bytes from the second buffer.
2222\layout Standard
2223
2224This is not as convenient as it could be (like, the BER encoder would consume
2225 the whole 100 bytes and keep these 5 bytes in some temporary storage),
2226 but in case of stream-based processing it might actually be OK.
2227 Suggestions are welcome.
2228\layout Standard
2229
2230There are two ways to invoke a BER decoder.
2231 The first one is a direct reference of the type-specific decoder.
2232 This way was shown in the previous example of
2233\emph on
2234simple_deserializer
2235\emph default
2236 function.
2237 The second way is to invoke a
2238\emph on
2239ber_decode
2240\emph default
2241 function, which is just a simple wrapper of the former approach into a
2242 less wordy notation:
2243\layout LyX-Code
2244
vlmba56d852004-09-29 13:29:17 +00002245rval = ber_decode(0, &asn_DEF_Rectangle, (void **)&rect,
vlme18adea2004-06-03 03:49:45 +00002246\layout LyX-Code
2247
2248 buffer, buf_size);
2249\layout Standard
2250
vlmba56d852004-09-29 13:29:17 +00002251Note that the initial (asn_DEF_Rectangle->ber_decoder) reference is gone,
vlme18adea2004-06-03 03:49:45 +00002252 and also the last argument (0) is no longer necessary.
2253\layout Standard
2254
2255These two ways of invocations are fully equivalent.
2256\layout Standard
2257
2258The BER de
2259\emph on
2260coder
2261\emph default
vlmba56d852004-09-29 13:29:17 +00002262 may fail because of (
vlme18adea2004-06-03 03:49:45 +00002263\emph on
2264the following RC_\SpecialChar \ldots{}
2265 codes are defined in ber_decoder.h
2266\emph default
2267):
2268\layout Itemize
2269
2270RC_WMORE: There is more data expected than it is provided (stream mode continuat
2271ion feature);
2272\layout Itemize
2273
2274RC_FAIL: General failure to decode the buffer;
2275\layout Itemize
2276
2277\SpecialChar \ldots{}
2278 other codes may be defined as well.
2279\layout Standard
2280
vlmaf2155e2004-10-20 15:48:55 +00002281Together with the return code (.code) the asn_dec_rval_t type contains the
vlme18adea2004-06-03 03:49:45 +00002282 number of bytes which is consumed from the buffer.
2283 In the previous hypothetical example of two buffers (of 100 and 200 bytes),
2284 the first call to ber_decode() would return with .code = RC_WMORE and .consumed
2285 = 95.
2286 The .consumed field of the BER decoder return value is
2287\series bold
2288always
2289\series default
2290 valid, even if the decoder succeeds or fails with any other return code.
2291\layout Standard
2292
2293Please look into ber_decoder.h for the precise definition of ber_decode()
2294 and related types.
vlmfcec8e52004-08-23 15:12:04 +00002295\layout Subsection
vlme18adea2004-06-03 03:49:45 +00002296
2297
2298\begin_inset LatexCommand \label{sub:Encoding-DER}
2299
2300\end_inset
2301
2302Encoding DER
2303\layout Standard
2304
vlm68d43e92004-09-24 20:56:07 +00002305The Distinguished Encoding Rules is the
2306\emph on
2307canonical
2308\emph default
2309 variant of BER encoding rules.
2310 The DER is best suited to encode the structures where all the lengths are
2311 known beforehand.
vlme18adea2004-06-03 03:49:45 +00002312 This is probably exactly how you want to encode: either after a BER decoding
2313 or after a manual fill-up, the target structure contains the data which
2314 size is implicitly known before encoding.
2315 The DER encoding is used, for example, to encode X.509 certificates.
2316\layout Standard
2317
2318As with BER decoder, the DER encoder may be invoked either directly from
vlmba56d852004-09-29 13:29:17 +00002319 the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone function,
vlme18adea2004-06-03 03:49:45 +00002320 which is somewhat simpler:
2321\layout LyX-Code
2322
2323/*
2324\layout LyX-Code
2325
2326 * This is a custom function which writes the
2327\layout LyX-Code
2328
2329 * encoded output into some FILE stream.
2330\layout LyX-Code
2331
2332 */
2333\layout LyX-Code
2334
vlmfcec8e52004-08-23 15:12:04 +00002335static int
2336\layout LyX-Code
2337
2338write_stream(const void *buffer, size_t size, void *app_key) {
vlme18adea2004-06-03 03:49:45 +00002339\layout LyX-Code
2340
2341 FILE *ostream = app_key;
2342\layout LyX-Code
2343
2344 size_t wrote;
2345\layout LyX-Code
2346
2347
2348\layout LyX-Code
2349
2350 wrote = fwrite(buffer, 1, size, ostream);
2351\layout LyX-Code
2352
2353
2354\layout LyX-Code
2355
2356 return (wrote == size) ? 0 : -1;
2357\layout LyX-Code
2358
2359}
2360\layout LyX-Code
2361
2362
2363\layout LyX-Code
2364
2365/*
2366\layout LyX-Code
2367
2368 * This is the serializer itself,
2369\layout LyX-Code
2370
2371 * it supplies the DER encoder with the
2372\layout LyX-Code
2373
2374 * pointer to the custom output function.
2375\layout LyX-Code
2376
2377 */
2378\layout LyX-Code
2379
2380ssize_t
2381\layout LyX-Code
2382
2383simple_serializer(FILE *ostream, Rectangle_t *rect) {
2384\layout LyX-Code
2385
vlm68d43e92004-09-24 20:56:07 +00002386 asn_enc_rval_t er; /* Encoder return value */
vlme18adea2004-06-03 03:49:45 +00002387\layout LyX-Code
2388
2389
2390\layout LyX-Code
2391
vlmba56d852004-09-29 13:29:17 +00002392 er = der_encode(&asn_DEF_Rect, rect,
vlme18adea2004-06-03 03:49:45 +00002393\layout LyX-Code
2394
vlmfcec8e52004-08-23 15:12:04 +00002395 write_stream, ostream);
vlme18adea2004-06-03 03:49:45 +00002396\layout LyX-Code
2397
vlm68d43e92004-09-24 20:56:07 +00002398 if(er.
vlme18adea2004-06-03 03:49:45 +00002399\series bold
vlm68d43e92004-09-24 20:56:07 +00002400encoded
vlme18adea2004-06-03 03:49:45 +00002401\series default
2402 == -1) {
2403\layout LyX-Code
2404
2405 /*
2406\layout LyX-Code
2407
vlm68d43e92004-09-24 20:56:07 +00002408 * Failed to encode the rectangle data.
vlme18adea2004-06-03 03:49:45 +00002409\layout LyX-Code
2410
2411 */
2412\layout LyX-Code
2413
2414 fprintf(stderr,
2415\begin_inset Quotes sld
2416\end_inset
2417
2418Cannot encode %s: %s
2419\backslash
2420n
2421\begin_inset Quotes srd
2422\end_inset
2423
2424,
2425\layout LyX-Code
2426
vlm68d43e92004-09-24 20:56:07 +00002427 er.
vlme18adea2004-06-03 03:49:45 +00002428\series bold
vlm68d43e92004-09-24 20:56:07 +00002429failed_type
vlme18adea2004-06-03 03:49:45 +00002430\series default
2431->name,
2432\layout LyX-Code
2433
2434 strerror(errno));
2435\layout LyX-Code
2436
2437 return -1;
2438\layout LyX-Code
2439
2440 } else {
2441\layout LyX-Code
2442
2443 /* Return the number of bytes */
2444\layout LyX-Code
2445
vlm68d43e92004-09-24 20:56:07 +00002446 return er.encoded;
vlme18adea2004-06-03 03:49:45 +00002447\layout LyX-Code
2448
2449 }
2450\layout LyX-Code
2451
2452}
2453\layout Standard
2454
2455As you see, the DER encoder does not write into some sort of buffer or something.
2456 It just invokes the custom function (possible, multiple times) which would
2457 save the data into appropriate storage.
2458 The optional argument
2459\emph on
2460app_key
2461\emph default
2462 is opaque for the DER encoder code and just used by
2463\emph on
2464_write_stream()
2465\emph default
2466 as the pointer to the appropriate output stream to be used.
2467\layout Standard
2468
2469If the custom write function is not given (passed as 0), then the DER encoder
2470 will essentially do the same thing (i.e., encode the data) but no callbacks
2471 will be invoked (so the data goes nowhere).
2472 It may prove useful to determine the size of the structure's encoding before
2473 actually doing the encoding
2474\begin_inset Foot
2475collapsed false
2476
2477\layout Standard
2478
2479It is actually faster too: the encoder might skip over some computations
2480 which aren't important for the size determination.
2481\end_inset
2482
2483.
2484\layout Standard
2485
2486Please look into der_encoder.h for the precise definition of der_encode()
2487 and related types.
vlmfcec8e52004-08-23 15:12:04 +00002488\layout Subsection
vlme18adea2004-06-03 03:49:45 +00002489
2490
vlm68d43e92004-09-24 20:56:07 +00002491\begin_inset LatexCommand \label{sub:Encoding-XER}
2492
2493\end_inset
2494
2495Encoding XER
2496\layout Standard
2497
2498The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible
2499 Markup Language, a text-based format for information exchange.
2500 The encoder routine API comes in two flavors: stdio-based and callback-based.
2501 With the callback-based encoder, the encoding process is very similar to
2502 the DER one, described in Section
2503\begin_inset LatexCommand \vref{sub:Encoding-DER}
2504
2505\end_inset
2506
2507.
2508 The following example uses the definition of write_stream() from up there.
2509\layout LyX-Code
2510
2511/*
2512\layout LyX-Code
2513
2514 * This procedure generates the XML document
2515\layout LyX-Code
2516
2517 * by invoking the XER encoder.
2518\layout LyX-Code
2519
2520 * NOTE: Do not copy this code verbatim!
2521\layout LyX-Code
2522
2523 * If the stdio output is necessary,
2524\layout LyX-Code
2525
2526 * use the xer_fprint() procedure instead.
2527\layout LyX-Code
2528
2529 * See Section
2530\begin_inset LatexCommand \vref{sub:Printing-the-target}
2531
2532\end_inset
2533
2534.
2535\layout LyX-Code
2536
2537 */
2538\layout LyX-Code
2539
2540int
2541\layout LyX-Code
2542
2543print_as_XML(FILE *ostream, Rectangle_t *rect) {
2544\layout LyX-Code
2545
2546 asn_enc_rval_t er; /* Encoder return value */
2547\layout LyX-Code
2548
2549
2550\layout LyX-Code
2551
vlmba56d852004-09-29 13:29:17 +00002552 er = xer_encode(&asn_DEF_Rect, rect,
vlm68d43e92004-09-24 20:56:07 +00002553\layout LyX-Code
2554
2555 XER_F_BASIC, /* BASIC-XER or CANONICAL-XER */
2556\layout LyX-Code
2557
2558 write_stream, ostream);
2559\layout LyX-Code
2560
2561
2562\layout LyX-Code
2563
2564 return (er.encoded == -1) ? -1 : 0;
2565\layout LyX-Code
2566
2567}
2568\layout Standard
2569
2570Please look into xer_encoder.h for the precise definition of xer_encode()
2571 and related types.
2572\layout Standard
2573
2574See Section
2575\begin_inset LatexCommand \ref{sub:Printing-the-target}
2576
2577\end_inset
2578
2579 for the example of stdio-based XML encoder and other pretty-printing suggestion
2580s.
2581\layout Subsection
2582
2583
vlme18adea2004-06-03 03:49:45 +00002584\begin_inset LatexCommand \label{sub:Validating-the-target}
2585
2586\end_inset
2587
2588Validating the target structure
2589\layout Standard
2590
2591Sometimes the target structure needs to be validated.
2592 For example, if the structure was created by the application (as opposed
2593 to being decoded from some external source), some important information
2594 required by the ASN.1 specification might be missing.
2595 On the other hand, the successful decoding of the data from some external
2596 source does not necessarily mean that the data is fully valid either.
2597 It might well be the case that the specification describes some subtype
2598 constraints that were not taken into account during decoding, and it would
2599 actually be useful to perform the last check when the data is ready to
2600 be encoded or when the data has just been decoded to ensure its validity
2601 according to some stricter rules.
2602\layout Standard
2603
2604The asn_check_constraints() function checks the type for various implicit
2605 and explicit constraints.
2606 It is recommended to use asn_check_constraints() function after each decoding
2607 and before each encoding.
2608\layout Standard
2609
2610Please look into constraints.h for the precise definition of asn_check_constraint
2611s() and related types.
vlmfcec8e52004-08-23 15:12:04 +00002612\layout Subsection
vlme18adea2004-06-03 03:49:45 +00002613
2614
2615\begin_inset LatexCommand \label{sub:Printing-the-target}
2616
2617\end_inset
2618
2619Printing the target structure
2620\layout Standard
2621
2622There are two ways to print the target structure: either invoke the print_struct
2623 member of the ASN.1 type descriptor, or using the asn_fprint() function,
2624 which is a simpler wrapper of the former:
2625\layout LyX-Code
2626
vlmba56d852004-09-29 13:29:17 +00002627asn_fprint(stdout, &asn_DEF_Rectangle, rect);
vlme18adea2004-06-03 03:49:45 +00002628\layout Standard
2629
2630Please look into constr_TYPE.h for the precise definition of asn_fprint()
2631 and related types.
vlm68d43e92004-09-24 20:56:07 +00002632\layout Standard
2633
2634Another practical alternative to this custom format printing would be to
2635 invoke XER encoder.
2636 The default BASIC-XER encoder performs reasonable formatting for the output
2637 to be useful and human readable.
2638 To invoke the XER decoder in a manner similar to asn_fprint(), use the
2639 xer_fprint() call:
2640\layout LyX-Code
2641
vlmba56d852004-09-29 13:29:17 +00002642xer_fprint(stdout, &asn_DEF_Rectangle, rect);
vlm68d43e92004-09-24 20:56:07 +00002643\layout Standard
2644
2645See Section
2646\begin_inset LatexCommand \vref{sub:Encoding-XER}
2647
2648\end_inset
2649
2650 for XML-related details.
vlmfcec8e52004-08-23 15:12:04 +00002651\layout Subsection
vlme18adea2004-06-03 03:49:45 +00002652
2653
2654\begin_inset LatexCommand \label{sub:Freeing-the-target}
2655
2656\end_inset
2657
2658Freeing the target structure
2659\layout Standard
2660
2661Freeing the structure is slightly more complex than it may seem to.
2662 When the ASN.1 structure is freed, all the members of the structure and
2663 their submembers etc etc are recursively freed too.
2664 But it might not be feasible to free the structure itself.
2665 Consider the following case:
2666\layout LyX-Code
2667
2668struct my_figure { /* The custom structure */
2669\layout LyX-Code
2670
2671 int flags; /* <some custom member> */
2672\layout LyX-Code
2673
2674 /* The type is generated by the ASN.1 compiler */
2675\layout LyX-Code
2676
2677
2678\emph on
2679Rectangle_t rect;
2680\layout LyX-Code
2681
2682 /* other members of the structure */
2683\layout LyX-Code
2684
2685};
2686\layout Standard
2687
2688In this example, the application programmer defined a custom structure with
2689 one ASN.1-derived member (rect).
2690 This member is not a reference to the Rectangle_t, but an in-place inclusion
2691 of the Rectangle_t structure.
2692 If the freeing is necessary, the usual procedure of freeing everything
2693 must not be applied to the &rect pointer itself, because it does not point
2694 to the memory block directly allocated by memory allocation routine, but
2695 instead lies within such a block allocated for my_figure structure.
2696\layout Standard
2697
2698To solve this problem, the free_struct routine has the additional argument
2699 (besides the intuitive type descriptor and target structure pointers),
2700 which is the flag specifying whether the outer pointer itself must be freed
2701 (0, default) or it should be left intact (non-zero value).
2702\layout LyX-Code
2703
2704/* Rectangle_t is defined within my_figure */
2705\layout LyX-Code
2706
2707struct my_figure *mf =
2708\series bold
2709...
2710\series default
2711;
2712\layout LyX-Code
2713
2714/*
2715\layout LyX-Code
2716
2717 * Freeing the Rectangle_td
2718\layout LyX-Code
2719
2720 * without freeing the mf->rect pointer
2721\layout LyX-Code
2722
2723 */
2724\layout LyX-Code
2725
vlmba56d852004-09-29 13:29:17 +00002726asn_DEF_Rectangle->free_struct(
vlme18adea2004-06-03 03:49:45 +00002727\layout LyX-Code
2728
vlmba56d852004-09-29 13:29:17 +00002729 &asn_DEF_Rectangle, &mf->rect,
vlme18adea2004-06-03 03:49:45 +00002730\emph on
27311
2732\emph default
2733 /* !free */);
2734\layout LyX-Code
2735
2736
2737\layout LyX-Code
2738
2739/* Rectangle_t is a stand-alone pointer */
2740\layout LyX-Code
2741
2742Rectangle_t *rect =
2743\series bold
2744...
2745\series default
2746;
2747\layout LyX-Code
2748
2749/*
2750\layout LyX-Code
2751
2752 * Freeing the Rectangle_t
2753\layout LyX-Code
2754
2755 * and freeing the rect pointer
2756\layout LyX-Code
2757
2758 */
2759\layout LyX-Code
2760
vlmba56d852004-09-29 13:29:17 +00002761asn_DEF_Rectangle->free_struct(
vlme18adea2004-06-03 03:49:45 +00002762\layout LyX-Code
2763
vlmba56d852004-09-29 13:29:17 +00002764 &asn_DEF_Rectangle, rect,
vlme18adea2004-06-03 03:49:45 +00002765\emph on
27660
2767\emph default
2768 /* free the pointer too */);
2769\layout Standard
2770
2771It is safe to invoke the
2772\emph on
2773free_struct
2774\emph default
2775 function with the target structure pointer set to 0 (NULL), the function
2776 will do nothing.
vlm4cb0edf2005-01-14 11:38:49 +00002777\layout Part
2778
2779Examples
vlmabd51e22005-01-17 11:05:04 +00002780\layout Chapter*
vlm4cb0edf2005-01-14 11:38:49 +00002781
2782Step-by-step: A
2783\begin_inset Quotes sld
2784\end_inset
2785
2786Rectangle
2787\begin_inset Quotes srd
2788\end_inset
2789
2790 Decoder
2791\layout Standard
2792
2793This chapter will help you to create a simple decoder of a simple
2794\begin_inset Quotes sld
2795\end_inset
2796
2797Rectangle
2798\begin_inset Quotes srd
2799\end_inset
2800
2801 type used throughout this document.
2802\layout Enumerate
2803
2804Create a file named
2805\series bold
2806rectangle.asn1
2807\series default
2808 with the following contents:
2809\begin_deeper
2810\layout LyX-Code
2811
2812RectangleModule1 DEFINITIONS ::=
2813\layout LyX-Code
2814
2815BEGIN
2816\layout LyX-Code
2817
2818
2819\layout LyX-Code
2820
2821Rectangle ::= SEQUENCE {
2822\layout LyX-Code
2823
2824 height INTEGER,
2825\layout LyX-Code
2826
2827 width INTEGER
2828\layout LyX-Code
2829
2830}
2831\layout LyX-Code
2832
2833
2834\layout LyX-Code
2835
2836END
2837\end_deeper
2838\layout Enumerate
2839
2840Compile it into the set of .c and .h files using asn1c compiler
2841\begin_inset LatexCommand \cite{ASN1C}
2842
2843\end_inset
2844
2845:
2846\begin_deeper
2847\layout LyX-Code
2848
2849
2850\emph on
2851asn1c -fnative-types
2852\series bold
2853\emph default
2854rectangle.asn1
2855\end_deeper
2856\layout Enumerate
2857
2858Alternatively, use the Online ASN.1 compiler
2859\begin_inset LatexCommand \cite{AONL}
2860
2861\end_inset
2862
2863 by uploading the
2864\series bold
2865rectangle.asn1
2866\series default
vlmabd51e22005-01-17 11:05:04 +00002867 file into the Web form and unpacking the produced archive on your computer.
vlm4cb0edf2005-01-14 11:38:49 +00002868\layout Enumerate
2869
vlmabd51e22005-01-17 11:05:04 +00002870By this time, you should have gotten multiple files in the current directory,
2871 including the
2872\series bold
2873Rectangle.c
2874\series default
2875 and
2876\series bold
2877Rectangle.h
2878\series default
2879.
vlm4cb0edf2005-01-14 11:38:49 +00002880\layout Enumerate
2881
2882Create a main() routine which takes the binary input file, decodes it as
vlmabd51e22005-01-17 11:05:04 +00002883 it were a BER-encoded Rectangle type, and prints out the text (XML) representat
2884ion of the Rectangle type.
vlm4cb0edf2005-01-14 11:38:49 +00002885 Let's name the file
2886\series bold
2887main.c
2888\series default
2889:
vlmabd51e22005-01-17 11:05:04 +00002890\begin_inset ERT
2891status Open
2892
2893\layout Standard
2894
2895\backslash
2896clearpage{}
2897\end_inset
2898
2899
vlm4cb0edf2005-01-14 11:38:49 +00002900\begin_deeper
2901\layout LyX-Code
2902
2903
2904\size small
2905#include <stdio.h>
2906\layout LyX-Code
2907
2908
2909\size small
2910#include <sys/types.h>
2911\layout LyX-Code
2912
2913
2914\size small
2915#include <Rectangle.h> /* Rectangle ASN.1 type */
2916\layout LyX-Code
2917
2918
2919\size small
2920
2921\layout LyX-Code
2922
2923
2924\size small
2925int main(int ac, char **av) {
2926\layout LyX-Code
2927
2928
2929\size small
2930 char buf[1024]; /* Temporary buffer */
2931\layout LyX-Code
2932
2933
2934\size small
2935 Rectangle_t *rectangle = 0; /* Type to decode */
2936\layout LyX-Code
2937
2938
2939\size small
2940 asn_dec_rval_t rval; /* Decoder return value */
2941\layout LyX-Code
2942
2943
2944\size small
2945 FILE *fp; /* Input file handler */
2946\layout LyX-Code
2947
2948
2949\size small
2950 size_t size; /* Number of bytes read */
2951\layout LyX-Code
2952
2953
2954\size small
2955 char *filename; /* Input file name */
2956\layout LyX-Code
2957
2958
2959\size small
2960
2961\layout LyX-Code
2962
2963
2964\size small
2965 /* Require a single filename argument */
2966\layout LyX-Code
2967
2968
2969\size small
2970 if(ac != 2) {
2971\layout LyX-Code
2972
2973
2974\size small
2975 fprintf(stderr,
2976\begin_inset Quotes sld
2977\end_inset
2978
2979Usage: %s <file.ber>
2980\backslash
2981n
2982\begin_inset Quotes srd
2983\end_inset
2984
2985, av[0]);
2986\layout LyX-Code
2987
2988
2989\size small
2990 exit(64); /* better, EX_USAGE */
2991\layout LyX-Code
2992
2993
2994\size small
2995 } else {
2996\layout LyX-Code
2997
2998
2999\size small
3000 filename = av[1];
3001\layout LyX-Code
3002
3003
3004\size small
3005 }
3006\layout LyX-Code
3007
3008
3009\size small
3010
3011\layout LyX-Code
3012
3013
3014\size small
3015 /* Open input file as read-only binary */
3016\layout LyX-Code
3017
3018
3019\size small
3020 fp = fopen(filename,
3021\begin_inset Quotes sld
3022\end_inset
3023
3024rb
3025\begin_inset Quotes srd
3026\end_inset
3027
3028);
3029\layout LyX-Code
3030
3031
3032\size small
3033 if(!fp) {
3034\layout LyX-Code
3035
3036
3037\size small
3038 perror(filename);
3039\layout LyX-Code
3040
3041
3042\size small
3043 exit(66); /* better, EX_NOINPUT */
3044\layout LyX-Code
3045
3046
3047\size small
3048 }
3049\layout LyX-Code
3050
3051
3052\size small
3053
3054\layout LyX-Code
3055
3056
3057\size small
3058 /* Read up to the buffer size */
3059\layout LyX-Code
3060
3061
3062\size small
3063 size = fread(buf, 1, sizeof(buf), fp);
3064\layout LyX-Code
3065
3066
3067\size small
3068 fclose(fp);
3069\layout LyX-Code
3070
3071
3072\size small
3073 if(!size) {
3074\layout LyX-Code
3075
3076
3077\size small
3078 fprintf(stderr,
3079\begin_inset Quotes sld
3080\end_inset
3081
3082%s: Empty or broken
3083\backslash
3084n
3085\begin_inset Quotes srd
3086\end_inset
3087
3088, filename);
3089\layout LyX-Code
3090
3091
3092\size small
3093 exit(65); /* better, EX_DATAERR */
3094\layout LyX-Code
3095
3096
3097\size small
3098 }
3099\layout LyX-Code
3100
3101
3102\size small
3103
3104\layout LyX-Code
3105
3106
3107\size small
3108 /* Decode the input buffer as Rectangle type */
3109\layout LyX-Code
3110
3111
3112\size small
3113 rval = ber_decode(0, &asn_DEF_Rectangle,
3114\layout LyX-Code
3115
3116
3117\size small
3118 (void **)&rectangle, buf, size);
3119\layout LyX-Code
3120
3121
3122\size small
3123 if(rval.code != RC_OK) {
3124\layout LyX-Code
3125
3126
3127\size small
3128 fprintf(stderr,
3129\layout LyX-Code
3130
3131
3132\size small
3133
3134\begin_inset Quotes sld
3135\end_inset
3136
3137%s: Broken Rectangle encoding at byte %ld
3138\backslash
3139n
3140\begin_inset Quotes srd
3141\end_inset
3142
3143,
3144\layout LyX-Code
3145
3146
3147\size small
3148 filename, (long)rval.consumed);
3149\layout LyX-Code
3150
3151
3152\size small
3153 exit(65); /* better, EX_DATAERR */
3154\layout LyX-Code
3155
3156
3157\size small
3158 }
3159\layout LyX-Code
3160
3161
3162\size small
3163
3164\layout LyX-Code
3165
3166
3167\size small
3168 /* Print the decoded Rectangle type as XML */
3169\layout LyX-Code
3170
3171
3172\size small
3173 xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);
3174\layout LyX-Code
3175
3176
3177\size small
3178
3179\layout LyX-Code
3180
3181
3182\size small
3183 return 0; /* Decoding finished successfully */
3184\layout LyX-Code
3185
3186
3187\size small
3188}
3189\end_deeper
3190\layout Enumerate
3191
vlmabd51e22005-01-17 11:05:04 +00003192Compile all files together using C compiler (varies by platform):
vlm4cb0edf2005-01-14 11:38:49 +00003193\begin_deeper
3194\layout LyX-Code
3195
3196
3197\emph on
3198cc -I.
vlmabd51e22005-01-17 11:05:04 +00003199 -o
3200\series bold
3201rdecode
3202\series default
3203 *.c
vlm4cb0edf2005-01-14 11:38:49 +00003204\end_deeper
3205\layout Enumerate
3206
3207Voila! You have just created the Rectangle type decoder named
3208\series bold
3209rdecode
3210\series default
3211!
vlme18adea2004-06-03 03:49:45 +00003212\layout Bibliography
vlmfcec8e52004-08-23 15:12:04 +00003213\bibitem [ASN1C]{ASN1C}
3214
vlm89d219b2004-09-08 03:01:06 +00003215The OpenSource ASN.1 Compiler.
vlmfcec8e52004-08-23 15:12:04 +00003216
vlmc3bfd812004-09-30 22:26:19 +00003217\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c/}
vlmfcec8e52004-08-23 15:12:04 +00003218
3219\end_inset
3220
3221
3222\layout Bibliography
vlm4cb0edf2005-01-14 11:38:49 +00003223\bibitem [AONL]{AONL}
3224
3225Online ASN.1 Compiler.
3226
3227\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c/asn1c.cgi}
3228
3229\end_inset
3230
3231
3232\layout Bibliography
vlme18adea2004-06-03 03:49:45 +00003233\bibitem [Dub00]{Dub00}
3234
vlmaf2155e2004-10-20 15:48:55 +00003235Olivier Dubuisson ---
vlme18adea2004-06-03 03:49:45 +00003236\emph on
3237ASN.1 Communication between heterogeneous systems
3238\emph default
vlmaf2155e2004-10-20 15:48:55 +00003239 --- Morgan Kaufmann Publishers, 2000.
vlme18adea2004-06-03 03:49:45 +00003240
3241\begin_inset LatexCommand \htmlurl{http://asn1.elibel.tm.fr/en/book/}
3242
3243\end_inset
3244
3245.
3246 ISBN:0-12-6333361-0.
3247\layout Bibliography
3248\bibitem [ITU-T/ASN.1]{ITU-T/ASN.1}
3249
3250ITU-T Study Group 17 -- Languages for Telecommunication Systems
3251\begin_inset LatexCommand \url{http://www.itu.int/ITU-T/studygroups/com17/languages/}
3252
3253\end_inset
3254
3255
3256\the_end