blob: 27736a2d5f6fbad5e2be0f32214fd7adef01454f [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 Walkin4a83dd52004-08-07 05:00:07 +000040Using the Free 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
1312collapsed true
1313
1314\layout Standard
1315
1316
1317\begin_inset Tabular
1318<lyxtabular version="3" rows="20" columns="2">
1319<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
1605-fbless-SIZE
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
1613
1614\size small
1615Allow SIZE() constraint for INTEGER, ENUMERATED, and other types for which
1616 this constraint is normally prohibited by the standard.
1617 This is a violation of an ASN.1 standard and compiler may fail to produce
1618 the meaningful code.
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
1628-fnative-integers
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
1638Use native machine's integer types whenever possible, instead of the complex
1639 ASN.1 INTEGER and ENUMERATED types.
1640
1641\end_inset
1642</cell>
1643</row>
1644<row topline="true">
1645<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1646\begin_inset Text
1647
1648\layout Standard
1649
1650-funnamed-unions
1651\end_inset
1652</cell>
1653<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1654\begin_inset Text
1655
1656\layout Standard
1657
1658
1659\size small
1660Enable unnamed unions in the definitions of target language's structures.
1661\end_inset
1662</cell>
1663</row>
1664<row topline="true" bottomline="true">
1665<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1666\begin_inset Text
1667
1668\layout Standard
1669
1670-ftypes88
1671\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
1680Use only ASN.1:1988 embedded types.
1681\end_inset
1682</cell>
1683</row>
1684<row topline="true">
1685<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1686\begin_inset Text
1687
1688\layout Standard
1689
1690
1691\series bold
1692Output Options
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
1700
1701\series bold
1702Description
1703\end_inset
1704</cell>
1705</row>
1706<row topline="true">
1707<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1708\begin_inset Text
1709
1710\layout Standard
1711
1712-print-constraints
1713\end_inset
1714</cell>
1715<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1716\begin_inset Text
1717
1718\layout Standard
1719
1720
1721\size small
1722When -EF are also specified, this option forces the compiler to explain
1723 its internal understanding of subtype constraints.
1724\end_inset
1725</cell>
1726</row>
1727<row topline="true" bottomline="true">
1728<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1729\begin_inset Text
1730
1731\layout Standard
1732
1733-print-lines
1734\end_inset
1735</cell>
1736<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1737\begin_inset Text
1738
1739\layout Standard
1740
1741
1742\size small
1743Generate "-- #line" comments in -E output.
1744\end_inset
1745</cell>
1746</row>
1747</lyxtabular>
1748
1749\end_inset
1750
1751
1752\layout Caption
1753
1754
1755\begin_inset LatexCommand \label{cap:asn1c-cmdopts}
1756
1757\end_inset
1758
1759The list of asn1c command line options
1760\end_inset
1761
1762
1763\layout Section
Lev Walkin84291e02004-06-03 03:49:45 +00001764
1765Recognizing compiler output
1766\layout Standard
1767
1768After compiling, the following entities will be created in your current
1769 directory:
1770\layout Itemize
1771
1772A set of .c and .h files, generally a single pair for each type defined in
1773 the ASN.1 specifications.
1774 These files will be named similarly to the ASN.1 types (
1775\emph on
1776Rectangle.c
1777\emph default
1778 and
1779\emph on
1780Rectangle.h
1781\emph default
1782 for the specification defined in the beginning of this document).
1783\layout Itemize
1784
1785A set of helper .c and .h files which contain generic encoders, decoders and
1786 other useful routines.
Lev Walkin26587ab2004-08-23 15:12:04 +00001787 There will be quite a few of them, some of them even are not always necessary,
1788 but the overall amount of code after compiling will be rather small anyway.
Lev Walkin84291e02004-06-03 03:49:45 +00001789\layout Standard
1790
1791It is your responsibility to create .c file with the
1792\emph on
1793 int main()
1794\emph default
1795 routine and the Makefile (if needed).
1796 Compiler helps you with the latter by creating the Makefile.am.sample, containing
1797 the skeleton definition for the automake, should you want to use autotools.
1798\layout Standard
1799
1800In other words, after compiling the Rectangle module, you have the following
1801 set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h,
1802\series bold
1803\SpecialChar \ldots{}
1804
1805\series default
1806 }, where
1807\series bold
1808
1809\begin_inset Quotes sld
1810\end_inset
1811
1812\SpecialChar \ldots{}
1813
1814\begin_inset Quotes srd
1815\end_inset
1816
1817
1818\series default
1819 stands for the set of additional
1820\begin_inset Quotes sld
1821\end_inset
1822
1823helper
1824\begin_inset Quotes srd
1825\end_inset
1826
1827 files created by the compiler.
1828 If you add the simple file with the
1829\emph on
1830int main()
1831\emph default
1832 routine, it would even be possible to compile everything with the single
1833 instruction:
1834\layout LyX-Code
1835
1836cc -o rectangle *.c # It could be
1837\emph on
1838that
1839\emph default
1840 simple
1841\begin_inset Foot
1842collapsed false
1843
1844\layout Standard
1845
1846Provided that you've also created a .c file with the
1847\emph on
1848int main()
1849\emph default
1850 routine.
1851\end_inset
1852
1853
Lev Walkin26587ab2004-08-23 15:12:04 +00001854\layout Section
Lev Walkin84291e02004-06-03 03:49:45 +00001855
1856Invoking the ASN.1 helper code from the application
1857\layout Standard
1858
Lev Walkin26587ab2004-08-23 15:12:04 +00001859First of all, you should to include one or more header files into your applicati
1860on.
1861 For our Rectangle module, including the Rectangle.h file is enough:
Lev Walkin84291e02004-06-03 03:49:45 +00001862\layout LyX-Code
1863
1864#include <Rectangle.h>
1865\layout Standard
1866
1867The header files defines the C structure corresponding to the ASN.1 definition
1868 of a rectangle and the declaration of the ASN.1 type descriptor, which is
1869 used as an argument to most of the functions provided by the ASN.1 module.
1870 For example, here is the code which frees the Rectangle_t structure:
1871\layout LyX-Code
1872
Lev Walkin26587ab2004-08-23 15:12:04 +00001873Rectangle_t *rect = ...;
1874\layout LyX-Code
1875
1876
Lev Walkin84291e02004-06-03 03:49:45 +00001877\layout LyX-Code
1878
1879asn1_DEF_Rectangle->free_struct(&asn1_DEF_Rectangle,
1880\layout LyX-Code
1881
1882 rect, 0);
1883\layout Standard
1884
1885This code defines a
1886\emph on
1887rect
1888\emph default
1889 pointer which points to the Rectangle_t structure which needs to be freed.
1890 The second line invokes the generic free_struct routine created specifically
1891 for this Rectangle_t structure.
1892 The
1893\emph on
1894asn1_DEF_Rectangle
1895\emph default
1896 is the type descriptor, which holds a collection of generic routines to
1897 deal with the Rectangle_t structure.
1898\layout Standard
1899
1900There are several generic functions available:
1901\layout Description
1902
1903check_constraints Check that the contents of the target structure are semantical
1904ly valid and constrained to appropriate implicit or explicit subtype constraints.
1905 Please refer to Section
1906\begin_inset LatexCommand \vref{sub:Validating-the-target}
1907
1908\end_inset
1909
1910.
1911\layout Description
1912
1913ber_decoder This is the generic
1914\emph on
1915restartable
1916\begin_inset Foot
1917collapsed false
1918
1919\layout Standard
1920
1921Restartable means that if the decoder encounters the end of the buffer,
1922 it will fail, but may later be invoked again with the rest of the buffer
1923 to continue decoding.
1924\end_inset
1925
1926
1927\emph default
1928BER decoder (Basic Encoding Rules).
1929 This decoder would create and/or fill the target structure for you.
1930 Please refer to Section
1931\begin_inset LatexCommand \ref{sub:Decoding-BER}
1932
1933\end_inset
1934
1935.
1936\layout Description
1937
1938der_encoder This is the generic DER encoder (Distinguished Encoding Rules).
1939 This decoder will take the target structure and encode it into a series
1940 of bytes.
1941 Please refer to Section
1942\begin_inset LatexCommand \ref{sub:Encoding-DER}
1943
1944\end_inset
1945
1946.
1947\layout Description
1948
1949print_struct This function convert the contents of the passed target structure
1950 into human readable form.
1951 This form is not formal and cannot be converted back into the structure,
1952 but it may turn out to be useful for debugging or quick-n-dirty printing.
1953 Please refer to Section
1954\begin_inset LatexCommand \ref{sub:Printing-the-target}
1955
1956\end_inset
1957
1958.
1959\layout Description
1960
1961free_struct This is a generic disposal which frees the target structure.
1962 Please refer to Section
1963\begin_inset LatexCommand \ref{sub:Freeing-the-target}
1964
1965\end_inset
1966
1967.
1968\layout Standard
1969
1970Each of the above function takes the type descriptor (
1971\emph on
1972asn1_DEF_\SpecialChar \ldots{}
1973
1974\emph default
1975) and the target structure (
1976\emph on
1977rect
1978\emph default
1979, in the above example).
1980 The target structure is typically created by the generic BER decoder or
1981 by the application itself.
1982\layout Standard
1983
1984Here is how the buffer can be deserialized into the structure:
1985\layout LyX-Code
1986
1987Rectangle_t *
1988\layout LyX-Code
1989
Lev Walkin26587ab2004-08-23 15:12:04 +00001990simple_deserializer(const void *buffer, size_t buf_size) {
Lev Walkin84291e02004-06-03 03:49:45 +00001991\layout LyX-Code
1992
1993 Rectangle_t *rect = 0; /* Note this 0! */
1994\layout LyX-Code
1995
1996 ber_dec_rval_t rval;
1997\layout LyX-Code
1998
1999
2000\layout LyX-Code
2001
2002 rval = asn1_DEF_Rectangle->ber_decoder(
2003\layout LyX-Code
2004
2005 &asn1_DEF_Rectangle,
2006\layout LyX-Code
2007
2008 (void **)&rect,
2009\layout LyX-Code
2010
2011 buffer, buf_size,
2012\layout LyX-Code
2013
2014 0);
2015\layout LyX-Code
2016
2017
2018\layout LyX-Code
2019
2020 if(rval
2021\series bold
2022.code
2023\series default
2024 == RC_OK) {
2025\layout LyX-Code
2026
2027 return rect; /* Decoding succeeded */
2028\layout LyX-Code
2029
2030 } else {
2031\layout LyX-Code
2032
Lev Walkin26587ab2004-08-23 15:12:04 +00002033 /* Free partially decoded rect */
2034\layout LyX-Code
2035
Lev Walkin84291e02004-06-03 03:49:45 +00002036 asn1_DEF_Rectangle->free_struct(
2037\layout LyX-Code
2038
2039 &asn1_DEF_Rectangle, rect, 0);
2040\layout LyX-Code
2041
2042 return 0;
2043\layout LyX-Code
2044
2045 }
2046\layout LyX-Code
2047
2048}
2049\layout Standard
2050
2051The code above defines a function,
2052\emph on
2053simple_deserializer
2054\emph default
2055, which takes a buffer and its length and expected to return a pointer to
2056 the Rectangle_t structure.
2057 Inside, it tries to convert the bytes passed into the target structure
2058 (rect) using the generic BER decoder and returns the rect pointer afterwards.
2059 If the structure cannot be deserialized, it frees the memory which might
2060 be left allocated by the unfinished
2061\emph on
2062ber_decoder
2063\emph default
2064 routine and returns NULL.
2065
2066\series bold
2067This freeing is necessary
2068\series default
2069 because the ber_decoder is a restartable procedure, and may fail just because
2070 there is more data needs to be provided before decoding could be finalized.
2071 The code above obviously does not take into account the way the
2072\emph on
2073ber_decoder
2074\emph default
2075 failed, so the freeing is necessary because the part of the buffer may
2076 already be decoded into the structure by the time something goes wrong.
2077\layout Standard
2078
2079Restartable decoding is a little bit trickier: you need to provide the old
2080 target structure pointer (which might be already half-decoded) and react
2081 on RC_WMORE return code.
2082 This will be explained later in Section
2083\begin_inset LatexCommand \vref{sub:Decoding-BER}
2084
2085\end_inset
2086
2087
Lev Walkin26587ab2004-08-23 15:12:04 +00002088\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002089
2090
2091\begin_inset LatexCommand \label{sub:Decoding-BER}
2092
2093\end_inset
2094
2095Decoding BER
2096\layout Standard
2097
2098The Basic Encoding Rules describe the basic way how the structure can be
2099 encoded and decoded.
2100 Several other encoding rules (CER, DER) define a more restrictive versions
2101 of BER, so the generic BER parser is also capable of decoding the data
2102 encoded by CER and DER encoders.
2103 The opposite is not true.
2104\layout Standard
2105
2106The ASN.1 compiler provides the generic BER decoder which is implicitly capable
2107 of decoding BER, CER and DER encoded data.
2108\layout Standard
2109
2110The decoder is restartable (stream-oriented), which means that in case the
2111 buffer has less data than it is expected, the decoder will process whatever
2112 it is available and ask for more data to be provided.
2113 Please note that the decoder may actually process less data than it is
2114 given in the buffer, which means that you should be able to make the next
2115 buffer contain the unprocessed part of the previous buffer.
2116\layout Standard
2117
2118Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
2119\layout Itemize
2120
2121You may concatenate these buffers and feed the BER decoder with 300 bytes
2122 of data, or
2123\layout Itemize
2124
2125You may feed it the first buffer of 100 bytes of data, realize that the
2126 ber_decoder consumed only 95 bytes from it and later feed the decoder with
2127 205 bytes buffer which consists of 5 unprocessed bytes from the first buffer
2128 and the latter 200 bytes from the second buffer.
2129\layout Standard
2130
2131This is not as convenient as it could be (like, the BER encoder would consume
2132 the whole 100 bytes and keep these 5 bytes in some temporary storage),
2133 but in case of stream-based processing it might actually be OK.
2134 Suggestions are welcome.
2135\layout Standard
2136
2137There are two ways to invoke a BER decoder.
2138 The first one is a direct reference of the type-specific decoder.
2139 This way was shown in the previous example of
2140\emph on
2141simple_deserializer
2142\emph default
2143 function.
2144 The second way is to invoke a
2145\emph on
2146ber_decode
2147\emph default
2148 function, which is just a simple wrapper of the former approach into a
2149 less wordy notation:
2150\layout LyX-Code
2151
2152rval = ber_decode(&asn1_DEF_Rectangle, (void **)&rect,
2153\layout LyX-Code
2154
2155 buffer, buf_size);
2156\layout Standard
2157
2158Note that the initial (asn1_DEF_Rectangle->ber_decoder) reference is gone,
2159 and also the last argument (0) is no longer necessary.
2160\layout Standard
2161
2162These two ways of invocations are fully equivalent.
2163\layout Standard
2164
2165The BER de
2166\emph on
2167coder
2168\emph default
2169 may fail because (
2170\emph on
2171the following RC_\SpecialChar \ldots{}
2172 codes are defined in ber_decoder.h
2173\emph default
2174):
2175\layout Itemize
2176
2177RC_WMORE: There is more data expected than it is provided (stream mode continuat
2178ion feature);
2179\layout Itemize
2180
2181RC_FAIL: General failure to decode the buffer;
2182\layout Itemize
2183
2184\SpecialChar \ldots{}
2185 other codes may be defined as well.
2186\layout Standard
2187
2188Together with the return code (.code) the ber_dec_rval_t type contains the
2189 number of bytes which is consumed from the buffer.
2190 In the previous hypothetical example of two buffers (of 100 and 200 bytes),
2191 the first call to ber_decode() would return with .code = RC_WMORE and .consumed
2192 = 95.
2193 The .consumed field of the BER decoder return value is
2194\series bold
2195always
2196\series default
2197 valid, even if the decoder succeeds or fails with any other return code.
2198\layout Standard
2199
2200Please look into ber_decoder.h for the precise definition of ber_decode()
2201 and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00002202\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002203
2204
2205\begin_inset LatexCommand \label{sub:Encoding-DER}
2206
2207\end_inset
2208
2209Encoding DER
2210\layout Standard
2211
2212The Distinguished Encoding Rules is the variant of BER encoding rules which
2213 is oriented on representing the structures with length known beforehand.
2214 This is probably exactly how you want to encode: either after a BER decoding
2215 or after a manual fill-up, the target structure contains the data which
2216 size is implicitly known before encoding.
2217 The DER encoding is used, for example, to encode X.509 certificates.
2218\layout Standard
2219
2220As with BER decoder, the DER encoder may be invoked either directly from
2221 the ASN.1 type descriptor (asn1_DEF_Rectangle) or from the stand-alone function,
2222 which is somewhat simpler:
2223\layout LyX-Code
2224
2225/*
2226\layout LyX-Code
2227
2228 * This is a custom function which writes the
2229\layout LyX-Code
2230
2231 * encoded output into some FILE stream.
2232\layout LyX-Code
2233
2234 */
2235\layout LyX-Code
2236
Lev Walkin26587ab2004-08-23 15:12:04 +00002237static int
2238\layout LyX-Code
2239
2240write_stream(const void *buffer, size_t size, void *app_key) {
Lev Walkin84291e02004-06-03 03:49:45 +00002241\layout LyX-Code
2242
2243 FILE *ostream = app_key;
2244\layout LyX-Code
2245
2246 size_t wrote;
2247\layout LyX-Code
2248
2249
2250\layout LyX-Code
2251
2252 wrote = fwrite(buffer, 1, size, ostream);
2253\layout LyX-Code
2254
2255
2256\layout LyX-Code
2257
2258 return (wrote == size) ? 0 : -1;
2259\layout LyX-Code
2260
2261}
2262\layout LyX-Code
2263
2264
2265\layout LyX-Code
2266
2267/*
2268\layout LyX-Code
2269
2270 * This is the serializer itself,
2271\layout LyX-Code
2272
2273 * it supplies the DER encoder with the
2274\layout LyX-Code
2275
2276 * pointer to the custom output function.
2277\layout LyX-Code
2278
2279 */
2280\layout LyX-Code
2281
2282ssize_t
2283\layout LyX-Code
2284
2285simple_serializer(FILE *ostream, Rectangle_t *rect) {
2286\layout LyX-Code
2287
2288 der_enc_rval_t rval; /* Return value */
2289\layout LyX-Code
2290
2291
2292\layout LyX-Code
2293
2294 rval = der_encode(&asn1_DEF_Rect, rect,
2295\layout LyX-Code
2296
Lev Walkin26587ab2004-08-23 15:12:04 +00002297 write_stream, ostream);
Lev Walkin84291e02004-06-03 03:49:45 +00002298\layout LyX-Code
2299
2300 if(rval
2301\series bold
2302.encoded
2303\series default
2304 == -1) {
2305\layout LyX-Code
2306
2307 /*
2308\layout LyX-Code
2309
2310 * Failure to encode the rectangle data.
2311\layout LyX-Code
2312
2313 */
2314\layout LyX-Code
2315
2316 fprintf(stderr,
2317\begin_inset Quotes sld
2318\end_inset
2319
2320Cannot encode %s: %s
2321\backslash
2322n
2323\begin_inset Quotes srd
2324\end_inset
2325
2326,
2327\layout LyX-Code
2328
2329 rval
2330\series bold
2331.failed_type
2332\series default
2333->name,
2334\layout LyX-Code
2335
2336 strerror(errno));
2337\layout LyX-Code
2338
2339 return -1;
2340\layout LyX-Code
2341
2342 } else {
2343\layout LyX-Code
2344
2345 /* Return the number of bytes */
2346\layout LyX-Code
2347
2348 return rval.encoded;
2349\layout LyX-Code
2350
2351 }
2352\layout LyX-Code
2353
2354}
2355\layout Standard
2356
2357As you see, the DER encoder does not write into some sort of buffer or something.
2358 It just invokes the custom function (possible, multiple times) which would
2359 save the data into appropriate storage.
2360 The optional argument
2361\emph on
2362app_key
2363\emph default
2364 is opaque for the DER encoder code and just used by
2365\emph on
2366_write_stream()
2367\emph default
2368 as the pointer to the appropriate output stream to be used.
2369\layout Standard
2370
2371If the custom write function is not given (passed as 0), then the DER encoder
2372 will essentially do the same thing (i.e., encode the data) but no callbacks
2373 will be invoked (so the data goes nowhere).
2374 It may prove useful to determine the size of the structure's encoding before
2375 actually doing the encoding
2376\begin_inset Foot
2377collapsed false
2378
2379\layout Standard
2380
2381It is actually faster too: the encoder might skip over some computations
2382 which aren't important for the size determination.
2383\end_inset
2384
2385.
2386\layout Standard
2387
2388Please look into der_encoder.h for the precise definition of der_encode()
2389 and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00002390\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002391
2392
2393\begin_inset LatexCommand \label{sub:Validating-the-target}
2394
2395\end_inset
2396
2397Validating the target structure
2398\layout Standard
2399
2400Sometimes the target structure needs to be validated.
2401 For example, if the structure was created by the application (as opposed
2402 to being decoded from some external source), some important information
2403 required by the ASN.1 specification might be missing.
2404 On the other hand, the successful decoding of the data from some external
2405 source does not necessarily mean that the data is fully valid either.
2406 It might well be the case that the specification describes some subtype
2407 constraints that were not taken into account during decoding, and it would
2408 actually be useful to perform the last check when the data is ready to
2409 be encoded or when the data has just been decoded to ensure its validity
2410 according to some stricter rules.
2411\layout Standard
2412
2413The asn_check_constraints() function checks the type for various implicit
2414 and explicit constraints.
2415 It is recommended to use asn_check_constraints() function after each decoding
2416 and before each encoding.
2417\layout Standard
2418
2419Please look into constraints.h for the precise definition of asn_check_constraint
2420s() and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00002421\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002422
2423
2424\begin_inset LatexCommand \label{sub:Printing-the-target}
2425
2426\end_inset
2427
2428Printing the target structure
2429\layout Standard
2430
2431There are two ways to print the target structure: either invoke the print_struct
2432 member of the ASN.1 type descriptor, or using the asn_fprint() function,
2433 which is a simpler wrapper of the former:
2434\layout LyX-Code
2435
2436asn_fprint(stdout, &asn1_DEF_Rectangle, rect);
2437\layout Standard
2438
2439Please look into constr_TYPE.h for the precise definition of asn_fprint()
2440 and related types.
Lev Walkin26587ab2004-08-23 15:12:04 +00002441\layout Subsection
Lev Walkin84291e02004-06-03 03:49:45 +00002442
2443
2444\begin_inset LatexCommand \label{sub:Freeing-the-target}
2445
2446\end_inset
2447
2448Freeing the target structure
2449\layout Standard
2450
2451Freeing the structure is slightly more complex than it may seem to.
2452 When the ASN.1 structure is freed, all the members of the structure and
2453 their submembers etc etc are recursively freed too.
2454 But it might not be feasible to free the structure itself.
2455 Consider the following case:
2456\layout LyX-Code
2457
2458struct my_figure { /* The custom structure */
2459\layout LyX-Code
2460
2461 int flags; /* <some custom member> */
2462\layout LyX-Code
2463
2464 /* The type is generated by the ASN.1 compiler */
2465\layout LyX-Code
2466
2467
2468\emph on
2469Rectangle_t rect;
2470\layout LyX-Code
2471
2472 /* other members of the structure */
2473\layout LyX-Code
2474
2475};
2476\layout Standard
2477
2478In this example, the application programmer defined a custom structure with
2479 one ASN.1-derived member (rect).
2480 This member is not a reference to the Rectangle_t, but an in-place inclusion
2481 of the Rectangle_t structure.
2482 If the freeing is necessary, the usual procedure of freeing everything
2483 must not be applied to the &rect pointer itself, because it does not point
2484 to the memory block directly allocated by memory allocation routine, but
2485 instead lies within such a block allocated for my_figure structure.
2486\layout Standard
2487
2488To solve this problem, the free_struct routine has the additional argument
2489 (besides the intuitive type descriptor and target structure pointers),
2490 which is the flag specifying whether the outer pointer itself must be freed
2491 (0, default) or it should be left intact (non-zero value).
2492\layout LyX-Code
2493
2494/* Rectangle_t is defined within my_figure */
2495\layout LyX-Code
2496
2497struct my_figure *mf =
2498\series bold
2499...
2500\series default
2501;
2502\layout LyX-Code
2503
2504/*
2505\layout LyX-Code
2506
2507 * Freeing the Rectangle_td
2508\layout LyX-Code
2509
2510 * without freeing the mf->rect pointer
2511\layout LyX-Code
2512
2513 */
2514\layout LyX-Code
2515
2516asn1_DEF_Rectangle->free_struct(
2517\layout LyX-Code
2518
2519 &asn1_DEF_Rectangle, &mf->rect,
2520\emph on
25211
2522\emph default
2523 /* !free */);
2524\layout LyX-Code
2525
2526
2527\layout LyX-Code
2528
2529/* Rectangle_t is a stand-alone pointer */
2530\layout LyX-Code
2531
2532Rectangle_t *rect =
2533\series bold
2534...
2535\series default
2536;
2537\layout LyX-Code
2538
2539/*
2540\layout LyX-Code
2541
2542 * Freeing the Rectangle_t
2543\layout LyX-Code
2544
2545 * and freeing the rect pointer
2546\layout LyX-Code
2547
2548 */
2549\layout LyX-Code
2550
2551asn1_DEF_Rectangle->free_struct(
2552\layout LyX-Code
2553
2554 &asn1_DEF_Rectangle, rect,
2555\emph on
25560
2557\emph default
2558 /* free the pointer too */);
2559\layout Standard
2560
2561It is safe to invoke the
2562\emph on
2563free_struct
2564\emph default
2565 function with the target structure pointer set to 0 (NULL), the function
2566 will do nothing.
2567\layout Bibliography
Lev Walkin26587ab2004-08-23 15:12:04 +00002568\bibitem [ASN1C]{ASN1C}
2569
2570Free ASN.1 Compiler.
2571
2572\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1/}
2573
2574\end_inset
2575
2576
2577\layout Bibliography
Lev Walkin84291e02004-06-03 03:49:45 +00002578\bibitem [Dub00]{Dub00}
2579
2580Olivier Dubuisson --
2581\emph on
2582ASN.1 Communication between heterogeneous systems
2583\emph default
2584 -- Morgan Kaufmann Publishers, 2000.
2585
2586\begin_inset LatexCommand \htmlurl{http://asn1.elibel.tm.fr/en/book/}
2587
2588\end_inset
2589
2590.
2591 ISBN:0-12-6333361-0.
2592\layout Bibliography
2593\bibitem [ITU-T/ASN.1]{ITU-T/ASN.1}
2594
2595ITU-T Study Group 17 -- Languages for Telecommunication Systems
2596\begin_inset LatexCommand \url{http://www.itu.int/ITU-T/studygroups/com17/languages/}
2597
2598\end_inset
2599
2600
2601\the_end