blob: ad3d1217315e4e034e5defa32227e9955f52aba4 [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
5%\fancyhf{}
6%\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ #1\ sdf}{}}
7%\renewcommand{\sectionmark}[1]{\markright{\MakeUppercase{\thesection.\ #1}}}
8%\fancyhead[LE,RO]{\thepage}
9%\fancyhead[LO]{\rightmark}
10%\fancyhead[RE]]{\leftmark}
11\usepackage{extramarks}
12\lhead{\firstxmark}
13\rfoot{\lastxmark}
14\end_preamble
Lev Walkin84291e02004-06-03 03:49:45 +000015\language english
16\inputencoding latin1
Lev Walkin26587ab2004-08-23 15:12:04 +000017\fontscheme times
Lev Walkin84291e02004-06-03 03:49:45 +000018\graphics default
19\paperfontsize default
20\spacing single
21\papersize Default
22\paperpackage a4
23\use_geometry 0
24\use_amsmath 0
25\use_natbib 0
26\use_numerical_citations 0
27\paperorientation portrait
28\secnumdepth 3
29\tocdepth 3
30\paragraph_separation indent
31\defskip medskip
32\quotes_language swedish
33\quotes_times 2
34\papercolumns 1
Lev Walkin26587ab2004-08-23 15:12:04 +000035\papersides 2
36\paperpagestyle fancy
Lev Walkin84291e02004-06-03 03:49:45 +000037
38\layout Title
39
Lev Walkin26871b52004-09-08 03:01:06 +000040Using the Open ASN.1 Compiler
Lev Walkin84291e02004-06-03 03:49:45 +000041\layout Author
42
43Lev Walkin <
44\begin_inset LatexCommand \url{vlm@lionet.info}
45
46\end_inset
47
48>
49\layout Standard
50
51
Lev Walkin26587ab2004-08-23 15:12:04 +000052\begin_inset ERT
53status Open
54
55\layout Standard
56
57\backslash
Lev Walkin2cebad02004-09-05 10:42:51 +000058extramarks{$Revision$ -- describes asn1c-0.9.3}{}
Lev Walkin26587ab2004-08-23 15:12:04 +000059\end_inset
60
61
62\layout Standard
63
64
Lev Walkin84291e02004-06-03 03:49:45 +000065\begin_inset LatexCommand \tableofcontents{}
66
67\end_inset
68
69
Lev Walkin26587ab2004-08-23 15:12:04 +000070\layout Standard
71
72
73\begin_inset ERT
74status Collapsed
75
76\layout Standard
77
78\backslash
79pagestyle{headings}
80\end_inset
81
82
Lev Walkin4a83dd52004-08-07 05:00:07 +000083\layout Part
84
85ASN.1 Basics
Lev Walkin84291e02004-06-03 03:49:45 +000086\layout Chapter
87
Lev Walkin4a83dd52004-08-07 05:00:07 +000088Abstract Syntax Notation: ASN.1
Lev Walkin84291e02004-06-03 03:49:45 +000089\layout Standard
90
91
92\emph on
93This chapter defines some basic ASN.1 concepts and describes several most
94 widely used types.
95 It is by no means an authoritative or complete reference.
96 For more complete ASN.1 description, please refer to Olivier Dubuisson's
97 book
98\begin_inset LatexCommand \cite{Dub00}
99
100\end_inset
101
Lev Walkin4a83dd52004-08-07 05:00:07 +0000102 or the ASN.1 body of standards itself
Lev Walkin84291e02004-06-03 03:49:45 +0000103\begin_inset LatexCommand \cite{ITU-T/ASN.1}
104
105\end_inset
106
107.
108\layout Standard
109
110The Abstract Syntax Notation One is used to formally describe the semantics
111 of data transmitted across the network.
112 Two communicating parties may have different formats of their native data
113 types (i.e.
114 number of bits in the integer type), thus it is important to have a way
115 to describe the data in a manner which is independent from the particular
116 machine's representation.
117 The ASN.1 specifications is used to achieve one or more of the following:
118\layout Itemize
119
120The specification expressed in the ASN.1 notation is a formal and precise
121 way to communicate the data semantics to human readers;
122\layout Itemize
123
124The ASN.1 specifications may be used as input for automatic compilers which
125 produce the code for some target language (C, C++, Java, etc) to encode
126 and decode the data according to some encoding rules (which are also defined
127 by the ASN.1 standard).
128\layout Standard
129
130Consider the following example:
131\layout LyX-Code
132
133Rectangle ::= SEQUENCE {
134\layout LyX-Code
135
136 height INTEGER,
137\layout LyX-Code
138
139 width INTEGER
140\layout LyX-Code
141
142}
143\layout Standard
144
145This ASN.1 specification describes a constructed type,
146\emph on
147Rectangle
148\emph default
149, containing two integer fields.
150 This specification may tell the reader that there is this kind of data
151 structure and that some entity may be prepared to send or receive it.
152 The question on
153\emph on
154how
155\emph default
156 that entity is going to send or receive the
157\emph on
158encoded data
159\emph default
160 is outside the scope of ASN.1.
161 For example, this data structure may be encoded according to some encoding
162 rules and sent to the destination using the TCP protocol.
163 The ASN.1 specifies several ways of encoding (or
164\begin_inset Quotes sld
165\end_inset
166
167serializing
168\begin_inset Quotes srd
169\end_inset
170
171, or
172\begin_inset Quotes sld
173\end_inset
174
175marshaling
176\begin_inset Quotes srd
177\end_inset
178
179) the data: BER, CER, DER and XER, some of them which will be described
180 later.
181\layout Standard
182
183The complete specification must be wrapped in a module, which looks like
184 this:
185\layout LyX-Code
186
187UsageExampleModule1
188\layout LyX-Code
189
190 { iso org(3) dod(6) internet(1) private(4)
191\layout LyX-Code
192
193 enterprise(1) spelio(9363) software(1)
194\layout LyX-Code
195
196 asn1c(5) docs(2) usage(1) 1 }
197\layout LyX-Code
198
Lev Walkin26587ab2004-08-23 15:12:04 +0000199 DEFINITIONS AUTOMATIC TAGS ::=
Lev Walkin84291e02004-06-03 03:49:45 +0000200\layout LyX-Code
201
202BEGIN
203\layout LyX-Code
204
205
206\layout LyX-Code
207
208-- This is a comment which describes nothing.
209\layout LyX-Code
210
211Rectangle ::= SEQUENCE {
212\layout LyX-Code
213
214 height INTEGER, -- Height of the rectangle
215\layout LyX-Code
216
Lev Walkin26587ab2004-08-23 15:12:04 +0000217 width INTEGER -- Width of the rectangle
Lev Walkin84291e02004-06-03 03:49:45 +0000218\layout LyX-Code
219
220}
221\layout LyX-Code
222
223
224\layout LyX-Code
225
226END
227\layout Standard
228
229The module header consists of module name (UsageExampleModule1), the module
Lev Walkin26587ab2004-08-23 15:12:04 +0000230 object identifier ({...}), a keyword
Lev Walkin84291e02004-06-03 03:49:45 +0000231\begin_inset Quotes sld
232\end_inset
233
Lev Walkin26587ab2004-08-23 15:12:04 +0000234DEFINITIONS
235\begin_inset Quotes srd
236\end_inset
237
238, a set of module flags (AUTOMATIC TAGS) and
239\begin_inset Quotes sld
240\end_inset
241
242::= BEGIN
Lev Walkin84291e02004-06-03 03:49:45 +0000243\begin_inset Quotes srd
244\end_inset
245
246.
247 The module ends with an
248\begin_inset Quotes sld
249\end_inset
250
251END
252\begin_inset Quotes srd
253\end_inset
254
255 statement.
256\layout Section
257
258Some of the ASN.1 Basic Types
259\layout Subsection
260
261The BOOLEAN type
262\layout Standard
263
264The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF or
265 a similar kind of two-way choice.
266\layout Subsection
267
268The INTEGER type
269\layout Standard
270
271The INTEGER type is a signed natural number type without any restrictions
272 on its size.
273 If the automatic checking on INTEGER value bounds are necessary, the subtype
274 constraints must be used.
275\layout LyX-Code
276
277SimpleInteger ::= INTEGER
278\layout LyX-Code
279
Lev Walkin26587ab2004-08-23 15:12:04 +0000280
281\layout LyX-Code
282
Lev Walkin84291e02004-06-03 03:49:45 +0000283-- An integer with a very limited range
284\layout LyX-Code
285
286SmallInt ::= INTEGER (0..127)
287\layout LyX-Code
288
Lev Walkin26587ab2004-08-23 15:12:04 +0000289
290\layout LyX-Code
291
Lev Walkin84291e02004-06-03 03:49:45 +0000292-- Integer, negative
293\layout LyX-Code
294
295NegativeInt ::= INTEGER (MIN..0)
296\layout Subsection
297
298The ENUMERATED type
299\layout Standard
300
301The ENUMERATED type is semantically equivalent to the INTEGER type with
302 some integer values explicitly named.
303\layout LyX-Code
304
305FruitId ::= ENUMERATED { apple(1), orange(2) }
306\layout LyX-Code
307
Lev Walkin26587ab2004-08-23 15:12:04 +0000308
309\layout LyX-Code
310
Lev Walkin84291e02004-06-03 03:49:45 +0000311-- The numbers in braces are optional,
312\layout LyX-Code
313
Lev Walkin26587ab2004-08-23 15:12:04 +0000314-- the enumeration can be performed
Lev Walkin84291e02004-06-03 03:49:45 +0000315\layout LyX-Code
316
317-- automatically by the compiler
318\layout LyX-Code
319
320ComputerOSType ::= ENUMERATED {
321\layout LyX-Code
322
323 FreeBSD, -- will be 0
324\layout LyX-Code
325
326 Windows, -- will be 1
327\layout LyX-Code
328
329 Solaris(5), -- will remain 5
330\layout LyX-Code
331
332 Linux, -- will be 6
333\layout LyX-Code
334
335 MacOS -- will be 7
336\layout LyX-Code
337
338}
339\layout Subsection
340
341The OCTET STRING type
342\layout Standard
343
344This type models the sequence of 8-bit bytes.
345 This may be used to transmit some opaque data or data serialized by other
346 types of encoders (i.e.
347 video file, photo picture, etc).
348\layout Subsection
349
350The OBJECT IDENTIFIER type
351\layout Standard
352
353The OBJECT IDENTIFIER is used to represent the unique identifier of any
354 object, starting from the very root of the registration tree.
355 If your organization needs to uniquely identify something (a router, a
356 room, a person, a standard, or whatever), you are encouraged to get your
357 own identification subtree at
358\begin_inset LatexCommand \htmlurl{http://www.iana.org/protocols/forms.htm}
359
360\end_inset
361
362.
363\layout Standard
364
365For example, the very first ASN.1 module in this document has the following
366 OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
367\layout LyX-Code
368
369ExampleOID ::= OBJECT IDENTIFIER
370\layout LyX-Code
371
Lev Walkin26587ab2004-08-23 15:12:04 +0000372
373\layout LyX-Code
374
Lev Walkin84291e02004-06-03 03:49:45 +0000375usageExampleModule1-oid ExampleOID
376\layout LyX-Code
377
378 ::= { 1 3 6 1 4 1 9363 1 5 2 1 1 }
379\layout LyX-Code
380
Lev Walkin26587ab2004-08-23 15:12:04 +0000381
382\layout LyX-Code
383
Lev Walkin84291e02004-06-03 03:49:45 +0000384-- An identifier of the Internet.
385\layout LyX-Code
386
387internet-id OBJECT IDENTIFIER
388\layout LyX-Code
389
390 ::= { iso(1) identified-organization(3)
391\layout LyX-Code
392
393 dod(6) internet(1) }
394\layout Standard
395
396As you see, names are optional.
397\layout Subsection
398
399The RELATIVE-OID type
400\layout Standard
401
402The RELATIVE-OID type has the semantics of a subtree of an OBJECT IDENTIFIER.
403 There may be no need to repeat the whole sequence of numbers from the root
404 of the registration tree where the only thing of interest is some of the
405 tree's subsequence.
406\layout LyX-Code
407
408this-document RELATIVE-OID ::= { docs(2) usage(1) }
409\layout LyX-Code
410
Lev Walkin26587ab2004-08-23 15:12:04 +0000411
412\layout LyX-Code
413
Lev Walkin84291e02004-06-03 03:49:45 +0000414this-example RELATIVE-OID ::= {
415\layout LyX-Code
416
417 this-document assorted-examples(0) this-example(1) }
418\layout Section
419
420Some of the ASN.1 String Types
421\layout Subsection
422
423The IA5String type
424\layout Standard
425
426This is essentially the ASCII, with 128 character codes available (7 lower
Lev Walkin26587ab2004-08-23 15:12:04 +0000427 bits of an 8-bit byte).
Lev Walkin84291e02004-06-03 03:49:45 +0000428\layout Subsection
429
430The UTF8String type
431\layout Standard
432
433This is the character string which encodes the full Unicode range (4 bytes)
434 using multibyte character sequences.
435\layout Subsection
436
437The NumericString type
438\layout Standard
439
440This type represents the character string with the alphabet consisting of
441 numbers (
442\begin_inset Quotes sld
443\end_inset
444
4450
446\begin_inset Quotes srd
447\end_inset
448
449 to
450\begin_inset Quotes sld
451\end_inset
452
4539
454\begin_inset Quotes srd
455\end_inset
456
457) and a space.
458\layout Subsection
459
460The PrintableString type
461\layout Standard
462
463The character string with the following alphabet: space,
464\begin_inset Quotes sld
465\end_inset
466
467
468\series bold
469'
470\series default
471
472\begin_inset Quotes srd
473\end_inset
474
475 (single quote),
476\begin_inset Quotes sld
477\end_inset
478
479
480\series bold
481(
482\series default
483
484\begin_inset Quotes sld
485\end_inset
486
487,
488\begin_inset Quotes sld
489\end_inset
490
491
492\series bold
493)
494\series default
495
496\begin_inset Quotes srd
497\end_inset
498
499,
500\begin_inset Quotes sld
501\end_inset
502
503
504\series bold
505+
506\series default
507
508\begin_inset Quotes srd
509\end_inset
510
511,
512\begin_inset Quotes sld
513\end_inset
514
515,
516\begin_inset Quotes srd
517\end_inset
518
519 (comma),
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
535
536\series bold
537.
538\series default
539
540\begin_inset Quotes srd
541\end_inset
542
543,
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, digits (
556\begin_inset Quotes sld
557\end_inset
558
5590
560\begin_inset Quotes srd
561\end_inset
562
563 to
564\begin_inset Quotes sld
565\end_inset
566
5679
568\begin_inset Quotes srd
569\end_inset
570
571),
572\begin_inset Quotes sld
573\end_inset
574
575
576\series bold
577:
578\series default
579
580\begin_inset Quotes srd
581\end_inset
582
583,
584\begin_inset Quotes sld
585\end_inset
586
587
588\series bold
589=
590\series default
591
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, upper-case and lower-case letters (
608\begin_inset Quotes sld
609\end_inset
610
611A
612\begin_inset Quotes srd
613\end_inset
614
615 to
616\begin_inset Quotes sld
617\end_inset
618
619Z
620\begin_inset Quotes srd
621\end_inset
622
623 and
624\begin_inset Quotes sld
625\end_inset
626
627a
628\begin_inset Quotes srd
629\end_inset
630
631 to
632\begin_inset Quotes sld
633\end_inset
634
635z
636\begin_inset Quotes srd
637\end_inset
638
639)
640\layout Subsection
641
642The VisibleString type
643\layout Standard
644
645The character string with the alphabet which is more or less a subset of
646 ASCII between space and
647\begin_inset Quotes sld
648\end_inset
649
Lev Walkin26587ab2004-08-23 15:12:04 +0000650
651\series bold
Lev Walkin84291e02004-06-03 03:49:45 +0000652~
Lev Walkin26587ab2004-08-23 15:12:04 +0000653\series default
654
Lev Walkin84291e02004-06-03 03:49:45 +0000655\begin_inset Quotes srd
656\end_inset
657
658 (tilde).
Lev Walkin26587ab2004-08-23 15:12:04 +0000659 Alternatively, the alphabet may be described as the PrintableString alphabet
660 presented earlier, plus the following characters:
Lev Walkin84291e02004-06-03 03:49:45 +0000661\begin_inset Quotes sld
662\end_inset
663
664
665\series bold
666!
667\series default
668
669\begin_inset Quotes srd
670\end_inset
671
672,
673\begin_inset Quotes sld
674\end_inset
675
676
677\series bold
678
679\begin_inset Quotes srd
680\end_inset
681
682
683\series default
684
685\begin_inset Quotes srd
686\end_inset
687
688,
689\begin_inset Quotes sld
690\end_inset
691
692
693\series bold
694#
695\series default
696
697\begin_inset Quotes srd
698\end_inset
699
700,
701\begin_inset Quotes sld
702\end_inset
703
704
705\series bold
706$
707\series default
708
709\begin_inset Quotes srd
710\end_inset
711
712,
713\begin_inset Quotes sld
714\end_inset
715
716
717\series bold
718%
719\series default
720
721\begin_inset Quotes srd
722\end_inset
723
724,
725\begin_inset Quotes sld
726\end_inset
727
728
729\series bold
730&
731\series default
732
733\begin_inset Quotes srd
734\end_inset
735
736,
737\begin_inset Quotes sld
738\end_inset
739
740
741\series bold
742*
743\series default
744
745\begin_inset Quotes srd
746\end_inset
747
748,
749\begin_inset Quotes sld
750\end_inset
751
752
753\series bold
754;
755\series default
756
757\begin_inset Quotes srd
758\end_inset
759
760,
761\begin_inset Quotes sld
762\end_inset
763
764
765\series bold
766<
767\series default
768
769\begin_inset Quotes srd
770\end_inset
771
772,
773\begin_inset Quotes sld
774\end_inset
775
776
777\series bold
778>
779\series default
780
781\begin_inset Quotes srd
782\end_inset
783
784,
785\begin_inset Quotes sld
786\end_inset
787
788
789\series bold
790[
791\series default
792
793\begin_inset Quotes srd
794\end_inset
795
796,
797\begin_inset Quotes sld
798\end_inset
799
800
801\series bold
802
803\backslash
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\series default
830
831\begin_inset Quotes srd
832\end_inset
833
834,
835\begin_inset Quotes sld
836\end_inset
837
838
839\series bold
840_
841\series default
842
843\begin_inset Quotes srd
844\end_inset
845
846,
847\begin_inset Quotes sld
848\end_inset
849
850
851\series bold
852`
853\series default
854
855\begin_inset Quotes srd
856\end_inset
857
858 (single left quote),
859\begin_inset Quotes sld
860\end_inset
861
862
863\series bold
864{
865\series default
866
867\begin_inset Quotes srd
868\end_inset
869
870,
871\begin_inset Quotes sld
872\end_inset
873
874
875\series bold
876|
877\series default
878
879\begin_inset Quotes srd
880\end_inset
881
882,
883\begin_inset Quotes sld
884\end_inset
885
886
887\series bold
888}
889\series default
890
891\begin_inset Quotes srd
892\end_inset
893
894,
895\begin_inset Quotes sld
896\end_inset
897
Lev Walkin26587ab2004-08-23 15:12:04 +0000898
899\series bold
Lev Walkin84291e02004-06-03 03:49:45 +0000900~
Lev Walkin26587ab2004-08-23 15:12:04 +0000901\series default
902
Lev Walkin84291e02004-06-03 03:49:45 +0000903\begin_inset Quotes srd
904\end_inset
905
906.
907\layout Section
908
909ASN.1 Constructed Types
910\layout Subsection
911
912The SEQUENCE type
913\layout Standard
914
915This is an ordered collection of other simple or constructed types.
916 The SEQUENCE constructed type resembles the C
917\begin_inset Quotes sld
918\end_inset
919
920struct
921\begin_inset Quotes srd
922\end_inset
923
924 statement.
925\layout LyX-Code
926
927Address ::= SEQUENCE {
928\layout LyX-Code
929
930 -- The apartment number may be omitted
931\layout LyX-Code
932
933 apartmentNumber NumericString OPTIONAL,
934\layout LyX-Code
935
936 streetName PrintableString,
937\layout LyX-Code
938
939 cityName PrintableString,
940\layout LyX-Code
941
942 stateName PrintableString,
943\layout LyX-Code
944
945 -- This one may be omitted too
946\layout LyX-Code
947
948 zipNo NumericString OPTIONAL
949\layout LyX-Code
950
951}
952\layout Subsection
953
954The SET type
955\layout Standard
956
957This is a collection of other simple or constructed types.
958 Ordering is not important.
959 The data may arrive in the order which is different from the order of specifica
960tion.
961 Data is encoded in the order not necessarily corresponding to the order
962 of specification.
963\layout Subsection
964
965The CHOICE type
966\layout Standard
967
968This type is just a choice between the subtypes specified in it.
969 The CHOICE type contains at most one of the subtypes specified, and it
970 is always implicitly known which choice is being decoded or encoded.
971 This one resembles the C
972\begin_inset Quotes sld
973\end_inset
974
975union
976\begin_inset Quotes srd
977\end_inset
978
979 statement.
980\layout Standard
981
982The following type defines a response code, which may be either an integer
983 code or a boolean
984\begin_inset Quotes sld
985\end_inset
986
987true
988\begin_inset Quotes srd
989\end_inset
990
991/
992\begin_inset Quotes srd
993\end_inset
994
995false
996\begin_inset Quotes srd
997\end_inset
998
999 code.
1000\layout LyX-Code
1001
1002ResponseCode ::= CHOICE {
1003\layout LyX-Code
1004
1005 intCode INTEGER,
1006\layout LyX-Code
1007
1008 boolCode BOOLEAN
1009\layout LyX-Code
1010
1011}
1012\layout LyX-Code
1013
1014\layout Subsection
1015
1016The SEQUENCE OF type
1017\layout Standard
1018
1019This one is the list (array) of simple or constructed types:
1020\layout LyX-Code
1021
1022-- Example 1
1023\layout LyX-Code
1024
1025ManyIntegers ::= SEQUENCE OF INTEGER
1026\layout LyX-Code
1027
Lev Walkin26587ab2004-08-23 15:12:04 +00001028
1029\layout LyX-Code
1030
Lev Walkin84291e02004-06-03 03:49:45 +00001031-- Example 2
1032\layout LyX-Code
1033
1034ManyRectangles ::= SEQUENCE OF Rectangle
1035\layout LyX-Code
1036
Lev Walkin26587ab2004-08-23 15:12:04 +00001037
1038\layout LyX-Code
1039
Lev Walkin84291e02004-06-03 03:49:45 +00001040-- More complex example:
1041\layout LyX-Code
1042
1043-- an array of structures defined in place.
1044\layout LyX-Code
1045
1046ManyCircles ::= SEQUENCE OF SEQUENCE {
1047\layout LyX-Code
1048
1049 radius INTEGER
1050\layout LyX-Code
1051
1052 }
1053\layout Subsection
1054
1055The SET OF type
1056\layout Standard
1057
1058The SET OF type models the bag of structures.
1059 It resembles the SEQUENCE OF type, but the order is not important: i.e.
1060 the elements may arrive in the order which is not necessarily the same
1061 as the in-memory order on the remote machines.
1062\layout LyX-Code
1063
1064-- A set of structures defined elsewhere
1065\layout LyX-Code
1066
1067SetOfApples :: SET OF Apple
1068\layout LyX-Code
1069
Lev Walkin26587ab2004-08-23 15:12:04 +00001070
1071\layout LyX-Code
1072
Lev Walkin84291e02004-06-03 03:49:45 +00001073-- Set of integers encoding the kind of a fruit
1074\layout LyX-Code
1075
1076FruitBag ::= SET OF ENUMERATED { apple, orange }
Lev Walkin4a83dd52004-08-07 05:00:07 +00001077\layout Part
1078
1079Using the ASN.1 Compiler
Lev Walkin84291e02004-06-03 03:49:45 +00001080\layout Chapter
1081
Lev Walkin4a83dd52004-08-07 05:00:07 +00001082Introduction to the ASN.1 Compiler
Lev Walkin84291e02004-06-03 03:49:45 +00001083\layout Standard
1084
1085The purpose of the ASN.1 compiler, of which this document is part, is to
1086 convert the ASN.1 specifications to some other target language (currently,
1087 only C is supported
1088\begin_inset Foot
1089collapsed false
1090
1091\layout Standard
1092
1093C++ is
1094\begin_inset Quotes sld
1095\end_inset
1096
1097supported
1098\begin_inset Quotes srd
1099\end_inset
1100
Lev Walkin26587ab2004-08-23 15:12:04 +00001101 too, as long as an class-based approach is not a definitive factor.
Lev Walkin84291e02004-06-03 03:49:45 +00001102\end_inset
1103
1104).
1105 The compiler reads the specification and emits a series of target language
1106 structures and surrounding maintenance code.
1107 For example, the C structure which may be created by compiler to represent
1108 the simple
1109\emph on
1110Rectangle
1111\emph default
1112 specification defined earlier in this document, may look like this
1113\begin_inset Foot
1114collapsed false
1115
1116\layout Standard
1117
1118
1119\emph on
1120-fnative-integers
1121\emph default
1122 compiler option is used to produce basic C
1123\emph on
1124int
1125\emph default
Lev Walkin26587ab2004-08-23 15:12:04 +00001126 types instead of infinite width INTEGER_t structures.
1127 See Table
1128\begin_inset LatexCommand \vref{cap:asn1c-cmdopts}
1129
1130\end_inset
1131
1132.
Lev Walkin84291e02004-06-03 03:49:45 +00001133\end_inset
1134
1135:
1136\layout LyX-Code
1137
1138typedef struct Rectangle_s {
1139\layout LyX-Code
1140
1141 int height;
1142\layout LyX-Code
1143
1144 int width;
1145\layout LyX-Code
1146
1147} Rectangle_t;
1148\layout Standard
1149
1150This would not be of much value for such a simple specification, so the
1151 compiler goes further and actually produces the code which fills in this
Lev Walkin26587ab2004-08-23 15:12:04 +00001152 structure by parsing the opaque binary
Lev Walkin84291e02004-06-03 03:49:45 +00001153\begin_inset Foot
Lev Walkin26587ab2004-08-23 15:12:04 +00001154collapsed true
Lev Walkin84291e02004-06-03 03:49:45 +00001155
1156\layout Standard
1157
1158BER, CER and DER encodings are binary.
1159 However, the XER encoding is text (XML) based.
1160\end_inset
1161
1162 data provided in some buffer.
1163 It also produces the code that takes this structure as an argument and
1164 performs structure serialization by emitting a series of bytes.
Lev Walkin26587ab2004-08-23 15:12:04 +00001165\layout Chapter
Lev Walkin84291e02004-06-03 03:49:45 +00001166
1167Quick start
1168\layout Standard
1169
Lev Walkin26587ab2004-08-23 15:12:04 +00001170After building and installing the compiler, the
1171\emph on
1172asn1c
1173\begin_inset Foot
1174collapsed false
1175
1176\layout Standard
1177
1178The 1 symbol in asn
1179\series bold
11801
1181\series default
1182c is a digit, not an
1183\begin_inset Quotes sld
1184\end_inset
1185
1186ell
1187\begin_inset Quotes srd
1188\end_inset
1189
1190 letter.
1191\end_inset
1192
1193
1194\emph default
1195 command may be used to compile the ASN.1 specification
Lev Walkin84291e02004-06-03 03:49:45 +00001196\begin_inset Foot
1197collapsed false
1198
1199\layout Standard
1200
1201This is probably
1202\series bold
1203not
1204\series default
1205 what you want to try out right now -- read through the rest of this chapter
Lev Walkin26587ab2004-08-23 15:12:04 +00001206 to find out about
1207\series bold
1208-P
1209\series default
1210 and
1211\series bold
1212-R
1213\series default
1214 options.
Lev Walkin84291e02004-06-03 03:49:45 +00001215\end_inset
1216
1217:
1218\layout LyX-Code
1219
1220asn1c
1221\emph on
1222<spec.asn1>
1223\layout Standard
1224
Lev Walkin26587ab2004-08-23 15:12:04 +00001225If several specifications contain interdependencies, all of the files must
1226 be specified altogether:
Lev Walkin84291e02004-06-03 03:49:45 +00001227\layout LyX-Code
1228
1229asn1c
1230\emph on
1231<spec1.asn1> <spec2.asn1> ...
1232\layout Standard
1233
Lev Walkin26587ab2004-08-23 15:12:04 +00001234The compiler
1235\series bold
1236-E
1237\series default
1238 and
1239\series bold
1240-EF
1241\series default
1242 options are used for testing the parser and the semantic fixer, respectively.
1243 These options will instruct the compiler to dump out the parsed (and fixed,
1244 if
1245\series bold
1246-F
1247\series default
1248 is involved) ASN.1 specification as it was "understood" by the compiler.
1249 It might be useful to check whether a particular syntactic construction
Lev Walkin84291e02004-06-03 03:49:45 +00001250 is properly supported by the compiler.
1251\layout LyX-Code
1252
Lev Walkin26587ab2004-08-23 15:12:04 +00001253asn1c
1254\series bold
1255-EF
1256\series default
1257
Lev Walkin84291e02004-06-03 03:49:45 +00001258\emph on
1259<spec-to-test.asn1>
1260\layout Standard
1261
Lev Walkin26587ab2004-08-23 15:12:04 +00001262The
1263\series bold
1264-P
1265\series default
1266 option is used to dump the compiled output on the screen instead of creating
1267 a bunch of .c and .h files on disk in the current directory.
1268 You would probably want to start with
1269\series bold
1270-P
1271\series default
1272 option instead of creating a mess in your current directory.
1273 Another option,
1274\series bold
1275-R
1276\series default
1277, asks compiler to only generate the files which need to be generated, and
1278 supress linking in the numerous support files.
1279\layout Standard
1280
1281Print the compiled output instead of creating multiple source files:
1282\layout LyX-Code
1283
1284asn1c
1285\series bold
1286-P
1287\series default
1288
1289\emph on
1290<spec-to-compile-and-print.asn1>
1291\layout LyX-Code
1292
1293\layout Chapter
1294
1295Using the ASN.1 Compiler
Lev Walkin84291e02004-06-03 03:49:45 +00001296\layout Section
1297
Lev Walkin26587ab2004-08-23 15:12:04 +00001298Command-line options
1299\layout Standard
1300
1301The Table
1302\begin_inset LatexCommand \vref{cap:asn1c-cmdopts}
1303
1304\end_inset
1305
1306 summarizes various options affecting the compiler's behavior.
1307\layout Standard
1308
1309
1310\begin_inset Float table
1311wide false
Lev Walkindd32b592004-09-06 08:07:29 +00001312collapsed false
Lev Walkin26587ab2004-08-23 15:12:04 +00001313
1314\layout Standard
1315
1316
1317\begin_inset Tabular
Lev Walkindd32b592004-09-06 08:07:29 +00001318<lyxtabular version="3" rows="21" columns="2">
Lev Walkin26587ab2004-08-23 15:12:04 +00001319<features>
1320<column alignment="left" valignment="top" leftline="true" width="0">
1321<column alignment="block" valignment="top" leftline="true" rightline="true" width="3in">
1322<row topline="true" bottomline="true">
1323<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1324\begin_inset Text
1325
1326\layout Standard
1327
1328
1329\series bold
1330Overall Options
1331\end_inset
1332</cell>
1333<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1334\begin_inset Text
1335
1336\layout Standard
1337
1338
1339\series bold
1340Description
1341\end_inset
1342</cell>
1343</row>
1344<row topline="true">
1345<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1346\begin_inset Text
1347
1348\layout Standard
1349
1350-E
1351\end_inset
1352</cell>
1353<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1354\begin_inset Text
1355
1356\layout Standard
1357
1358
1359\size small
1360Stop after the parsing stage and print the reconstructed ASN.1 specification
1361 code to the standard output.
1362\end_inset
1363</cell>
1364</row>
1365<row topline="true">
1366<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1367\begin_inset Text
1368
1369\layout Standard
1370
1371-F
1372\end_inset
1373</cell>
1374<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1375\begin_inset Text
1376
1377\layout Standard
1378
1379
1380\size small
1381Used together with -E, instructs the compiler to stop after the ASN.1 syntax
1382 tree fixing stage and dump the reconstructed ASN.1 specification to the
1383 standard output.
1384\end_inset
1385</cell>
1386</row>
1387<row topline="true">
1388<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1389\begin_inset Text
1390
1391\layout Standard
1392
1393-P
1394\end_inset
1395</cell>
1396<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1397\begin_inset Text
1398
1399\layout Standard
1400
1401
1402\size small
1403Dump the compiled output to the standard output instead of cre- ating the
1404 target language files on disk.
1405\end_inset
1406</cell>
1407</row>
1408<row topline="true">
1409<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1410\begin_inset Text
1411
1412\layout Standard
1413
1414-R
1415\end_inset
1416</cell>
1417<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1418\begin_inset Text
1419
1420\layout Standard
1421
1422
1423\size small
1424Restrict the compiler to generate only the ASN.1 tables, omit- ting the usual
1425 support code.
1426\end_inset
1427</cell>
1428</row>
1429<row topline="true">
1430<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1431\begin_inset Text
1432
1433\layout Standard
1434
1435-S
1436\emph on
1437<directory>
1438\end_inset
1439</cell>
1440<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1441\begin_inset Text
1442
1443\layout Standard
1444
1445
1446\size small
1447Use the specified directory with ASN.1 skeleton files.
1448\end_inset
1449</cell>
1450</row>
1451<row topline="true" bottomline="true">
1452<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1453\begin_inset Text
1454
1455\layout Standard
1456
1457-t
1458\emph on
1459<data-string>
1460\end_inset
1461</cell>
1462<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1463\begin_inset Text
1464
1465\layout Standard
1466
1467
1468\size small
1469Interpret the data-string as a sequence of hexadecimal values representing
1470 the start of BER TLV encoding.
1471 Print the human readable explanation.
1472\end_inset
1473</cell>
1474</row>
1475<row topline="true">
1476<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1477\begin_inset Text
1478
1479\layout Standard
1480
1481
1482\series bold
1483Warning Options
1484\end_inset
1485</cell>
1486<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1487\begin_inset Text
1488
1489\layout Standard
1490
1491
1492\series bold
1493Description
1494\end_inset
1495</cell>
1496</row>
1497<row topline="true">
1498<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1499\begin_inset Text
1500
1501\layout Standard
1502
1503-Werror
1504\end_inset
1505</cell>
1506<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1507\begin_inset Text
1508
1509\layout Standard
1510
1511
1512\size small
1513Treat warnings as errors; abort if any warning is produced.
1514\end_inset
1515</cell>
1516</row>
1517<row topline="true">
1518<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1519\begin_inset Text
1520
1521\layout Standard
1522
1523-Wdebug-lexer
1524\end_inset
1525</cell>
1526<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1527\begin_inset Text
1528
1529\layout Standard
1530
1531
1532\size small
1533Enable lexer debugging during the ASN.1 parsing stage.
1534\end_inset
1535</cell>
1536</row>
1537<row topline="true">
1538<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1539\begin_inset Text
1540
1541\layout Standard
1542
1543-Wdebug-fixer
1544\end_inset
1545</cell>
1546<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1547\begin_inset Text
1548
1549\layout Standard
1550
1551
1552\size small
1553 Enable ASN.1 syntax tree fixer debugging during the fixing stage.
1554\end_inset
1555</cell>
1556</row>
1557<row topline="true" bottomline="true">
1558<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1559\begin_inset Text
1560
1561\layout Standard
1562
1563-Wdebug-compiler
1564\end_inset
1565</cell>
1566<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1567\begin_inset Text
1568
1569\layout Standard
1570
1571
1572\size small
1573Enable debugging during the actual compile time.
1574\end_inset
1575</cell>
1576</row>
1577<row topline="true">
1578<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1579\begin_inset Text
1580
1581\layout Standard
1582
1583
1584\series bold
1585Language Options
1586\end_inset
1587</cell>
1588<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1589\begin_inset Text
1590
1591\layout Standard
1592
1593
1594\series bold
1595Description
1596\end_inset
1597</cell>
1598</row>
1599<row topline="true">
1600<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1601\begin_inset Text
1602
1603\layout Standard
1604
Lev Walkindd32b592004-09-06 08:07:29 +00001605-fall-defs-global
1606\end_inset
1607</cell>
1608<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1609\begin_inset Text
1610
1611\layout Standard
1612
1613Normally the compiler hides the definitions (asn1_DEF_xxx) of the inner
1614 structure elements (members of SEQUENCE, SET and other types).
1615 This option makes all such definitions global.
1616 Enabling this option may pollute the namespace by making lots of asn1_DEF_xxx
1617 structures globally visible, but will allow you to manipulate (encode and
1618 decode) the individual members of any complex ASN.1 structure.
1619\end_inset
1620</cell>
1621</row>
1622<row topline="true">
1623<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1624\begin_inset Text
1625
1626\layout Standard
1627
Lev Walkin26587ab2004-08-23 15:12:04 +00001628-fbless-SIZE
1629\end_inset
1630</cell>
1631<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1632\begin_inset Text
1633
1634\layout Standard
1635
1636
1637\size small
1638Allow SIZE() constraint for INTEGER, ENUMERATED, and other types for which
1639 this constraint is normally prohibited by the standard.
1640 This is a violation of an ASN.1 standard and compiler may fail to produce
1641 the meaningful code.
1642\end_inset
1643</cell>
1644</row>
1645<row topline="true">
1646<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1647\begin_inset Text
1648
1649\layout Standard
1650
1651-fnative-integers
1652\end_inset
1653</cell>
1654<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1655\begin_inset Text
1656
1657\layout Standard
1658
1659
1660\size small
1661Use native machine's integer types whenever possible, instead of the complex
1662 ASN.1 INTEGER and ENUMERATED types.
1663
1664\end_inset
1665</cell>
1666</row>
1667<row topline="true">
1668<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1669\begin_inset Text
1670
1671\layout Standard
1672
1673-funnamed-unions
1674\end_inset
1675</cell>
1676<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1677\begin_inset Text
1678
1679\layout Standard
1680
1681
1682\size small
1683Enable unnamed unions in the definitions of target language's structures.
1684\end_inset
1685</cell>
1686</row>
1687<row topline="true" bottomline="true">
1688<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1689\begin_inset Text
1690
1691\layout Standard
1692
1693-ftypes88
1694\end_inset
1695</cell>
1696<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1697\begin_inset Text
1698
1699\layout Standard
1700
1701
1702\size small
1703Use only ASN.1:1988 embedded types.
1704\end_inset
1705</cell>
1706</row>
1707<row topline="true">
1708<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1709\begin_inset Text
1710
1711\layout Standard
1712
1713
1714\series bold
1715Output Options
1716\end_inset
1717</cell>
1718<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1719\begin_inset Text
1720
1721\layout Standard
1722
1723
1724\series bold
1725Description
1726\end_inset
1727</cell>
1728</row>
1729<row topline="true">
1730<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1731\begin_inset Text
1732
1733\layout Standard
1734
1735-print-constraints
1736\end_inset
1737</cell>
1738<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1739\begin_inset Text
1740
1741\layout Standard
1742
1743
1744\size small
1745When -EF are also specified, this option forces the compiler to explain
1746 its internal understanding of subtype constraints.
1747\end_inset
1748</cell>
1749</row>
1750<row topline="true" bottomline="true">
1751<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1752\begin_inset Text
1753
1754\layout Standard
1755
1756-print-lines
1757\end_inset
1758</cell>
1759<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1760\begin_inset Text
1761
1762\layout Standard
1763
1764
1765\size small
1766Generate "-- #line" comments in -E output.
1767\end_inset
1768</cell>
1769</row>
1770</lyxtabular>
1771
1772\end_inset
1773
1774
1775\layout Caption
1776
1777
1778\begin_inset LatexCommand \label{cap:asn1c-cmdopts}
1779
1780\end_inset
1781
1782The list of asn1c command line options
1783\end_inset
1784
1785
1786\layout Section
Lev Walkin84291e02004-06-03 03:49:45 +00001787
1788Recognizing compiler output
1789\layout Standard
1790
1791After compiling, the following entities will be created in your current
1792 directory:
1793\layout Itemize
1794
1795A set of .c and .h files, generally a single pair for each type defined in
1796 the ASN.1 specifications.
1797 These files will be named similarly to the ASN.1 types (
1798\emph on
1799Rectangle.c
1800\emph default
1801 and
1802\emph on
1803Rectangle.h
1804\emph default
1805 for the specification defined in the beginning of this document).
1806\layout Itemize
1807
1808A set of helper .c and .h files which contain generic encoders, decoders and
1809 other useful routines.
Lev Walkin26587ab2004-08-23 15:12:04 +00001810 There will be quite a few of them, some of them even are not always necessary,
1811 but the overall amount of code after compiling will be rather small anyway.
Lev Walkin84291e02004-06-03 03:49:45 +00001812\layout Standard
1813
1814It is your responsibility to create .c file with the
1815\emph on
1816 int main()
1817\emph default
1818 routine and the Makefile (if needed).
1819 Compiler helps you with the latter by creating the Makefile.am.sample, containing
1820 the skeleton definition for the automake, should you want to use autotools.
1821\layout Standard
1822
1823In other words, after compiling the Rectangle module, you have the following
1824 set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h,
1825\series bold
1826\SpecialChar \ldots{}
1827
1828\series default
1829 }, where
1830\series bold
1831
1832\begin_inset Quotes sld
1833\end_inset
1834
1835\SpecialChar \ldots{}
1836
1837\begin_inset Quotes srd
1838\end_inset
1839
1840
1841\series default
1842 stands for the set of additional
1843\begin_inset Quotes sld
1844\end_inset
1845
1846helper
1847\begin_inset Quotes srd
1848\end_inset
1849
1850 files created by the compiler.
1851 If you add the simple file with the
1852\emph on
1853int main()
1854\emph default
1855 routine, it would even be possible to compile everything with the single
1856 instruction:
1857\layout LyX-Code
1858
1859cc -o rectangle *.c # It could be
1860\emph on
1861that
1862\emph default
1863 simple
1864\begin_inset Foot
1865collapsed false
1866
1867\layout Standard
1868
1869Provided that you've also created a .c file with the
1870\emph on
1871int main()
1872\emph default
1873 routine.
1874\end_inset
1875
1876
Lev Walkin26587ab2004-08-23 15:12:04 +00001877\layout Section
Lev Walkin84291e02004-06-03 03:49:45 +00001878
1879Invoking the ASN.1 helper code from the application
1880\layout Standard
1881
Lev Walkin26587ab2004-08-23 15:12:04 +00001882First of all, you should to include one or more header files into your applicati
1883on.
1884 For our Rectangle module, including the Rectangle.h file is enough:
Lev Walkin84291e02004-06-03 03:49:45 +00001885\layout LyX-Code
1886
1887#include <Rectangle.h>
1888\layout Standard
1889
1890The header files defines the C structure corresponding to the ASN.1 definition
1891 of a rectangle and the declaration of the ASN.1 type descriptor, which is
1892 used as an argument to most of the functions provided by the ASN.1 module.
1893 For example, here is the code which frees the Rectangle_t structure:
1894\layout LyX-Code
1895
Lev Walkin26587ab2004-08-23 15:12:04 +00001896Rectangle_t *rect = ...;
1897\layout LyX-Code
1898
1899
Lev Walkin84291e02004-06-03 03:49:45 +00001900\layout LyX-Code
1901
1902asn1_DEF_Rectangle->free_struct(&asn1_DEF_Rectangle,
1903\layout LyX-Code
1904
1905 rect, 0);
1906\layout Standard
1907
1908This code defines a
1909\emph on
1910rect
1911\emph default
1912 pointer which points to the Rectangle_t structure which needs to be freed.
1913 The second line invokes the generic free_struct routine created specifically
1914 for this Rectangle_t structure.
1915 The
1916\emph on
1917asn1_DEF_Rectangle
1918\emph default
1919 is the type descriptor, which holds a collection of generic routines to
1920 deal with the Rectangle_t structure.
1921\layout Standard
1922
1923There are several generic functions available:
1924\layout Description
1925
1926check_constraints Check that the contents of the target structure are semantical
1927ly valid and constrained to appropriate implicit or explicit subtype constraints.
1928 Please refer to Section
1929\begin_inset LatexCommand \vref{sub:Validating-the-target}
1930
1931\end_inset
1932
1933.
1934\layout Description
1935
1936ber_decoder This is the generic
1937\emph on
1938restartable
1939\begin_inset Foot
1940collapsed false
1941
1942\layout Standard
1943
1944Restartable means that if the decoder encounters the end of the buffer,
1945 it will fail, but may later be invoked again with the rest of the buffer
1946 to continue decoding.
1947\end_inset
1948
1949
1950\emph default
1951BER decoder (Basic Encoding Rules).
1952 This decoder would create and/or fill the target structure for you.
1953 Please refer to Section
1954\begin_inset LatexCommand \ref{sub:Decoding-BER}
1955
1956\end_inset
1957
1958.
1959\layout Description
1960
1961der_encoder This is the generic DER encoder (Distinguished Encoding Rules).
1962 This decoder will take the target structure and encode it into a series
1963 of bytes.
1964 Please refer to Section
1965\begin_inset LatexCommand \ref{sub:Encoding-DER}
1966
1967\end_inset
1968
1969.
1970\layout Description
1971
1972print_struct This function convert the contents of the passed target structure
1973 into human readable form.
1974 This form is not formal and cannot be converted back into the structure,
1975 but it may turn out to be useful for debugging or quick-n-dirty printing.
1976 Please refer to Section
1977\begin_inset LatexCommand \ref{sub:Printing-the-target}
1978
1979\end_inset
1980
1981.
1982\layout Description
1983
1984free_struct This is a generic disposal which frees the target structure.
1985 Please refer to Section
1986\begin_inset LatexCommand \ref{sub:Freeing-the-target}
1987
1988\end_inset
1989
1990.
1991\layout Standard
1992
1993Each of the above function takes the type descriptor (
1994\emph on
1995asn1_DEF_\SpecialChar \ldots{}
1996
1997\emph default
1998) and the target structure (
1999\emph on
2000rect
2001\emph default
2002, in the above example).
2003 The target structure is typically created by the generic BER decoder or
2004 by the application itself.
2005\layout Standard
2006
2007Here is how the buffer can be deserialized into the structure:
2008\layout LyX-Code
2009
2010Rectangle_t *
2011\layout LyX-Code
2012
Lev Walkin26587ab2004-08-23 15:12:04 +00002013simple_deserializer(const void *buffer, size_t buf_size) {
Lev Walkin84291e02004-06-03 03:49:45 +00002014\layout LyX-Code
2015
2016 Rectangle_t *rect = 0; /* Note this 0! */
2017\layout LyX-Code
2018
2019 ber_dec_rval_t rval;
2020\layout LyX-Code
2021
2022
2023\layout LyX-Code
2024
2025 rval = asn1_DEF_Rectangle->ber_decoder(
2026\layout LyX-Code
2027
2028 &asn1_DEF_Rectangle,
2029\layout LyX-Code
2030
2031 (void **)&rect,
2032\layout LyX-Code
2033
2034 buffer, buf_size,
2035\layout LyX-Code
2036
2037 0);
2038\layout LyX-Code
2039
2040
2041\layout LyX-Code
2042
2043 if(rval
2044\series bold
2045.code
2046\series default
2047 == RC_OK) {
2048\layout LyX-Code
2049
2050 return rect; /* Decoding succeeded */
2051\layout LyX-Code
2052
2053 } else {
2054\layout LyX-Code
2055
Lev Walkin26587ab2004-08-23 15:12:04 +00002056 /* Free partially decoded rect */
2057\layout LyX-Code
2058
Lev Walkin84291e02004-06-03 03:49:45 +00002059 asn1_DEF_Rectangle->free_struct(
2060\layout LyX-Code
2061
2062 &asn1_DEF_Rectangle, rect, 0);
2063\layout LyX-Code
2064
2065 return 0;
2066\layout LyX-Code
2067
2068 }
2069\layout LyX-Code
2070
2071}
2072\layout Standard
2073
2074The code above defines a function,
2075\emph on
2076simple_deserializer
2077\emph default
2078, which takes a buffer and its length and expected to return a pointer to
2079 the Rectangle_t structure.
2080 Inside, it tries to convert the bytes passed into the target structure
2081 (rect) using the generic BER decoder and returns the rect pointer afterwards.
2082 If the structure cannot be deserialized, it frees the memory which might
2083 be left allocated by the unfinished
2084\emph on
2085ber_decoder
2086\emph default
2087 routine and returns NULL.
2088
2089\series bold
2090This freeing is necessary
2091\series default
2092 because the ber_decoder is a restartable procedure, and may fail just because
2093 there is more data needs to be provided before decoding could be finalized.
2094 The code above obviously does not take into account the way the
2095\emph on
2096ber_decoder
2097\emph default
2098 failed, so the freeing is necessary because the part of the buffer may
2099 already be decoded into the structure by the time something goes wrong.
2100\layout Standard
2101
2102Restartable decoding is a little bit trickier: you need to provide the old
2103 target structure pointer (which might be already half-decoded) and react
2104 on RC_WMORE return code.
2105 This will be explained later in Section
2106\begin_inset LatexCommand \vref{sub:Decoding-BER}
2107
2108\end_inset
2109
2110
Lev Walkin26587ab2004-08-23 15:12:04 +00002111\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002112
2113
2114\begin_inset LatexCommand \label{sub:Decoding-BER}
2115
2116\end_inset
2117
2118Decoding BER
2119\layout Standard
2120
2121The Basic Encoding Rules describe the basic way how the structure can be
2122 encoded and decoded.
2123 Several other encoding rules (CER, DER) define a more restrictive versions
2124 of BER, so the generic BER parser is also capable of decoding the data
2125 encoded by CER and DER encoders.
2126 The opposite is not true.
2127\layout Standard
2128
2129The ASN.1 compiler provides the generic BER decoder which is implicitly capable
2130 of decoding BER, CER and DER encoded data.
2131\layout Standard
2132
2133The decoder is restartable (stream-oriented), which means that in case the
2134 buffer has less data than it is expected, the decoder will process whatever
2135 it is available and ask for more data to be provided.
2136 Please note that the decoder may actually process less data than it is
2137 given in the buffer, which means that you should be able to make the next
2138 buffer contain the unprocessed part of the previous buffer.
2139\layout Standard
2140
2141Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
2142\layout Itemize
2143
2144You may concatenate these buffers and feed the BER decoder with 300 bytes
2145 of data, or
2146\layout Itemize
2147
2148You may feed it the first buffer of 100 bytes of data, realize that the
2149 ber_decoder consumed only 95 bytes from it and later feed the decoder with
2150 205 bytes buffer which consists of 5 unprocessed bytes from the first buffer
2151 and the latter 200 bytes from the second buffer.
2152\layout Standard
2153
2154This is not as convenient as it could be (like, the BER encoder would consume
2155 the whole 100 bytes and keep these 5 bytes in some temporary storage),
2156 but in case of stream-based processing it might actually be OK.
2157 Suggestions are welcome.
2158\layout Standard
2159
2160There are two ways to invoke a BER decoder.
2161 The first one is a direct reference of the type-specific decoder.
2162 This way was shown in the previous example of
2163\emph on
2164simple_deserializer
2165\emph default
2166 function.
2167 The second way is to invoke a
2168\emph on
2169ber_decode
2170\emph default
2171 function, which is just a simple wrapper of the former approach into a
2172 less wordy notation:
2173\layout LyX-Code
2174
2175rval = ber_decode(&asn1_DEF_Rectangle, (void **)&rect,
2176\layout LyX-Code
2177
2178 buffer, buf_size);
2179\layout Standard
2180
2181Note that the initial (asn1_DEF_Rectangle->ber_decoder) reference is gone,
2182 and also the last argument (0) is no longer necessary.
2183\layout Standard
2184
2185These two ways of invocations are fully equivalent.
2186\layout Standard
2187
2188The BER de
2189\emph on
2190coder
2191\emph default
2192 may fail because (
2193\emph on
2194the following RC_\SpecialChar \ldots{}
2195 codes are defined in ber_decoder.h
2196\emph default
2197):
2198\layout Itemize
2199
2200RC_WMORE: There is more data expected than it is provided (stream mode continuat
2201ion feature);
2202\layout Itemize
2203
2204RC_FAIL: General failure to decode the buffer;
2205\layout Itemize
2206
2207\SpecialChar \ldots{}
2208 other codes may be defined as well.
2209\layout Standard
2210
2211Together with the return code (.code) the ber_dec_rval_t type contains the
2212 number of bytes which is consumed from the buffer.
2213 In the previous hypothetical example of two buffers (of 100 and 200 bytes),
2214 the first call to ber_decode() would return with .code = RC_WMORE and .consumed
2215 = 95.
2216 The .consumed field of the BER decoder return value is
2217\series bold
2218always
2219\series default
2220 valid, even if the decoder succeeds or fails with any other return code.
2221\layout Standard
2222
2223Please look into ber_decoder.h for the precise definition of ber_decode()
2224 and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00002225\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002226
2227
2228\begin_inset LatexCommand \label{sub:Encoding-DER}
2229
2230\end_inset
2231
2232Encoding DER
2233\layout Standard
2234
2235The Distinguished Encoding Rules is the variant of BER encoding rules which
2236 is oriented on representing the structures with length known beforehand.
2237 This is probably exactly how you want to encode: either after a BER decoding
2238 or after a manual fill-up, the target structure contains the data which
2239 size is implicitly known before encoding.
2240 The DER encoding is used, for example, to encode X.509 certificates.
2241\layout Standard
2242
2243As with BER decoder, the DER encoder may be invoked either directly from
2244 the ASN.1 type descriptor (asn1_DEF_Rectangle) or from the stand-alone function,
2245 which is somewhat simpler:
2246\layout LyX-Code
2247
2248/*
2249\layout LyX-Code
2250
2251 * This is a custom function which writes the
2252\layout LyX-Code
2253
2254 * encoded output into some FILE stream.
2255\layout LyX-Code
2256
2257 */
2258\layout LyX-Code
2259
Lev Walkin26587ab2004-08-23 15:12:04 +00002260static int
2261\layout LyX-Code
2262
2263write_stream(const void *buffer, size_t size, void *app_key) {
Lev Walkin84291e02004-06-03 03:49:45 +00002264\layout LyX-Code
2265
2266 FILE *ostream = app_key;
2267\layout LyX-Code
2268
2269 size_t wrote;
2270\layout LyX-Code
2271
2272
2273\layout LyX-Code
2274
2275 wrote = fwrite(buffer, 1, size, ostream);
2276\layout LyX-Code
2277
2278
2279\layout LyX-Code
2280
2281 return (wrote == size) ? 0 : -1;
2282\layout LyX-Code
2283
2284}
2285\layout LyX-Code
2286
2287
2288\layout LyX-Code
2289
2290/*
2291\layout LyX-Code
2292
2293 * This is the serializer itself,
2294\layout LyX-Code
2295
2296 * it supplies the DER encoder with the
2297\layout LyX-Code
2298
2299 * pointer to the custom output function.
2300\layout LyX-Code
2301
2302 */
2303\layout LyX-Code
2304
2305ssize_t
2306\layout LyX-Code
2307
2308simple_serializer(FILE *ostream, Rectangle_t *rect) {
2309\layout LyX-Code
2310
2311 der_enc_rval_t rval; /* Return value */
2312\layout LyX-Code
2313
2314
2315\layout LyX-Code
2316
2317 rval = der_encode(&asn1_DEF_Rect, rect,
2318\layout LyX-Code
2319
Lev Walkin26587ab2004-08-23 15:12:04 +00002320 write_stream, ostream);
Lev Walkin84291e02004-06-03 03:49:45 +00002321\layout LyX-Code
2322
2323 if(rval
2324\series bold
2325.encoded
2326\series default
2327 == -1) {
2328\layout LyX-Code
2329
2330 /*
2331\layout LyX-Code
2332
2333 * Failure to encode the rectangle data.
2334\layout LyX-Code
2335
2336 */
2337\layout LyX-Code
2338
2339 fprintf(stderr,
2340\begin_inset Quotes sld
2341\end_inset
2342
2343Cannot encode %s: %s
2344\backslash
2345n
2346\begin_inset Quotes srd
2347\end_inset
2348
2349,
2350\layout LyX-Code
2351
2352 rval
2353\series bold
2354.failed_type
2355\series default
2356->name,
2357\layout LyX-Code
2358
2359 strerror(errno));
2360\layout LyX-Code
2361
2362 return -1;
2363\layout LyX-Code
2364
2365 } else {
2366\layout LyX-Code
2367
2368 /* Return the number of bytes */
2369\layout LyX-Code
2370
2371 return rval.encoded;
2372\layout LyX-Code
2373
2374 }
2375\layout LyX-Code
2376
2377}
2378\layout Standard
2379
2380As you see, the DER encoder does not write into some sort of buffer or something.
2381 It just invokes the custom function (possible, multiple times) which would
2382 save the data into appropriate storage.
2383 The optional argument
2384\emph on
2385app_key
2386\emph default
2387 is opaque for the DER encoder code and just used by
2388\emph on
2389_write_stream()
2390\emph default
2391 as the pointer to the appropriate output stream to be used.
2392\layout Standard
2393
2394If the custom write function is not given (passed as 0), then the DER encoder
2395 will essentially do the same thing (i.e., encode the data) but no callbacks
2396 will be invoked (so the data goes nowhere).
2397 It may prove useful to determine the size of the structure's encoding before
2398 actually doing the encoding
2399\begin_inset Foot
2400collapsed false
2401
2402\layout Standard
2403
2404It is actually faster too: the encoder might skip over some computations
2405 which aren't important for the size determination.
2406\end_inset
2407
2408.
2409\layout Standard
2410
2411Please look into der_encoder.h for the precise definition of der_encode()
2412 and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00002413\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002414
2415
2416\begin_inset LatexCommand \label{sub:Validating-the-target}
2417
2418\end_inset
2419
2420Validating the target structure
2421\layout Standard
2422
2423Sometimes the target structure needs to be validated.
2424 For example, if the structure was created by the application (as opposed
2425 to being decoded from some external source), some important information
2426 required by the ASN.1 specification might be missing.
2427 On the other hand, the successful decoding of the data from some external
2428 source does not necessarily mean that the data is fully valid either.
2429 It might well be the case that the specification describes some subtype
2430 constraints that were not taken into account during decoding, and it would
2431 actually be useful to perform the last check when the data is ready to
2432 be encoded or when the data has just been decoded to ensure its validity
2433 according to some stricter rules.
2434\layout Standard
2435
2436The asn_check_constraints() function checks the type for various implicit
2437 and explicit constraints.
2438 It is recommended to use asn_check_constraints() function after each decoding
2439 and before each encoding.
2440\layout Standard
2441
2442Please look into constraints.h for the precise definition of asn_check_constraint
2443s() and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00002444\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002445
2446
2447\begin_inset LatexCommand \label{sub:Printing-the-target}
2448
2449\end_inset
2450
2451Printing the target structure
2452\layout Standard
2453
2454There are two ways to print the target structure: either invoke the print_struct
2455 member of the ASN.1 type descriptor, or using the asn_fprint() function,
2456 which is a simpler wrapper of the former:
2457\layout LyX-Code
2458
2459asn_fprint(stdout, &asn1_DEF_Rectangle, rect);
2460\layout Standard
2461
2462Please look into constr_TYPE.h for the precise definition of asn_fprint()
2463 and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00002464\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002465
2466
2467\begin_inset LatexCommand \label{sub:Freeing-the-target}
2468
2469\end_inset
2470
2471Freeing the target structure
2472\layout Standard
2473
2474Freeing the structure is slightly more complex than it may seem to.
2475 When the ASN.1 structure is freed, all the members of the structure and
2476 their submembers etc etc are recursively freed too.
2477 But it might not be feasible to free the structure itself.
2478 Consider the following case:
2479\layout LyX-Code
2480
2481struct my_figure { /* The custom structure */
2482\layout LyX-Code
2483
2484 int flags; /* <some custom member> */
2485\layout LyX-Code
2486
2487 /* The type is generated by the ASN.1 compiler */
2488\layout LyX-Code
2489
2490
2491\emph on
2492Rectangle_t rect;
2493\layout LyX-Code
2494
2495 /* other members of the structure */
2496\layout LyX-Code
2497
2498};
2499\layout Standard
2500
2501In this example, the application programmer defined a custom structure with
2502 one ASN.1-derived member (rect).
2503 This member is not a reference to the Rectangle_t, but an in-place inclusion
2504 of the Rectangle_t structure.
2505 If the freeing is necessary, the usual procedure of freeing everything
2506 must not be applied to the &rect pointer itself, because it does not point
2507 to the memory block directly allocated by memory allocation routine, but
2508 instead lies within such a block allocated for my_figure structure.
2509\layout Standard
2510
2511To solve this problem, the free_struct routine has the additional argument
2512 (besides the intuitive type descriptor and target structure pointers),
2513 which is the flag specifying whether the outer pointer itself must be freed
2514 (0, default) or it should be left intact (non-zero value).
2515\layout LyX-Code
2516
2517/* Rectangle_t is defined within my_figure */
2518\layout LyX-Code
2519
2520struct my_figure *mf =
2521\series bold
2522...
2523\series default
2524;
2525\layout LyX-Code
2526
2527/*
2528\layout LyX-Code
2529
2530 * Freeing the Rectangle_td
2531\layout LyX-Code
2532
2533 * without freeing the mf->rect pointer
2534\layout LyX-Code
2535
2536 */
2537\layout LyX-Code
2538
2539asn1_DEF_Rectangle->free_struct(
2540\layout LyX-Code
2541
2542 &asn1_DEF_Rectangle, &mf->rect,
2543\emph on
25441
2545\emph default
2546 /* !free */);
2547\layout LyX-Code
2548
2549
2550\layout LyX-Code
2551
2552/* Rectangle_t is a stand-alone pointer */
2553\layout LyX-Code
2554
2555Rectangle_t *rect =
2556\series bold
2557...
2558\series default
2559;
2560\layout LyX-Code
2561
2562/*
2563\layout LyX-Code
2564
2565 * Freeing the Rectangle_t
2566\layout LyX-Code
2567
2568 * and freeing the rect pointer
2569\layout LyX-Code
2570
2571 */
2572\layout LyX-Code
2573
2574asn1_DEF_Rectangle->free_struct(
2575\layout LyX-Code
2576
2577 &asn1_DEF_Rectangle, rect,
2578\emph on
25790
2580\emph default
2581 /* free the pointer too */);
2582\layout Standard
2583
2584It is safe to invoke the
2585\emph on
2586free_struct
2587\emph default
2588 function with the target structure pointer set to 0 (NULL), the function
2589 will do nothing.
2590\layout Bibliography
Lev Walkin26587ab2004-08-23 15:12:04 +00002591\bibitem [ASN1C]{ASN1C}
2592
Lev Walkin26871b52004-09-08 03:01:06 +00002593The OpenSource ASN.1 Compiler.
Lev Walkin26587ab2004-08-23 15:12:04 +00002594
2595\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1/}
2596
2597\end_inset
2598
2599
2600\layout Bibliography
Lev Walkin84291e02004-06-03 03:49:45 +00002601\bibitem [Dub00]{Dub00}
2602
2603Olivier Dubuisson --
2604\emph on
2605ASN.1 Communication between heterogeneous systems
2606\emph default
2607 -- Morgan Kaufmann Publishers, 2000.
2608
2609\begin_inset LatexCommand \htmlurl{http://asn1.elibel.tm.fr/en/book/}
2610
2611\end_inset
2612
2613.
2614 ISBN:0-12-6333361-0.
2615\layout Bibliography
2616\bibitem [ITU-T/ASN.1]{ITU-T/ASN.1}
2617
2618ITU-T Study Group 17 -- Languages for Telecommunication Systems
2619\begin_inset LatexCommand \url{http://www.itu.int/ITU-T/studygroups/com17/languages/}
2620
2621\end_inset
2622
2623
2624\the_end