vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1 | |
| 2 | <!--Converted with LaTeX2HTML 2002-2-1 (1.70) |
| 3 | original version by: Nikos Drakos, CBLU, University of Leeds |
| 4 | * revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan |
| 5 | * with significant contributions from: |
| 6 | Jens Lippmann, Marek Rouchal, Martin Wilck and others --> |
| 7 | <HTML> |
| 8 | <HEAD> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 9 | <TITLE>Using the Open Source ASN.1 Compiler</TITLE> |
| 10 | <META NAME="description" CONTENT="Using the Open Source ASN.1 Compiler"> |
| 11 | <META NAME="ASN.1, asn1c, compiler, BER, DER, XER\"> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 12 | |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 13 | |
| 14 | |
| 15 | </HEAD> |
| 16 | |
| 17 | <BODY > |
| 18 | |
| 19 | <P> |
| 20 | |
| 21 | <P> |
| 22 | |
| 23 | <P> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 24 | <H1 ALIGN="CENTER">Using the Open Source ASN.1 Compiler</H1><DIV> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 25 | |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 26 | <P ALIGN="CENTER"><STRONG>Lev Walkin <<A HREF=mailto:vlm@lionet.info?Subject=asn1c>vlm@lionet.info</A>></STRONG></P> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 27 | </DIV> |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 28 | |
| 29 | <P> |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 30 | <!-- MATH |
| 31 | $Revision$ |
| 32 | --> |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 33 | <FONT COLOR=red><B>Download the <A HREF=asn1c-usage.pdf>PDF</A> version</B></FONT> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 34 | |
| 35 | <P> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 36 | <BR> |
| 37 | |
| 38 | <H2><A NAME="SECTION01000000000000000000"> |
| 39 | Contents</A> |
| 40 | </H2> |
| 41 | <!--Table of Contents--> |
| 42 | |
| 43 | <UL> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 44 | <LI><A NAME="tex2html53" |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 45 | HREF="asn1c-usage.html#SECTION02000000000000000000">ASN.1 Basics</A> |
| 46 | <UL> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 47 | <LI><A NAME="tex2html54" |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 48 | HREF="asn1c-usage.html#SECTION02100000000000000000">Abstract Syntax Notation: ASN.1</A> |
| 49 | <UL> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 50 | <LI><A NAME="tex2html55" |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 51 | HREF="asn1c-usage.html#SECTION02110000000000000000">Some of the ASN.1 Basic Types</A> |
| 52 | <UL> |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 53 | <LI><A NAME="tex2html56" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 54 | HREF="asn1c-usage.html#SECTION02111000000000000000">The BOOLEAN type</A> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 55 | <LI><A NAME="tex2html57" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 56 | HREF="asn1c-usage.html#SECTION02112000000000000000">The INTEGER type</A> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 57 | <LI><A NAME="tex2html58" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 58 | HREF="asn1c-usage.html#SECTION02113000000000000000">The ENUMERATED type</A> |
| 59 | <LI><A NAME="tex2html59" |
| 60 | HREF="asn1c-usage.html#SECTION02114000000000000000">The OCTET STRING type</A> |
| 61 | <LI><A NAME="tex2html60" |
| 62 | HREF="asn1c-usage.html#SECTION02115000000000000000">The OBJECT IDENTIFIER type</A> |
| 63 | <LI><A NAME="tex2html61" |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 64 | HREF="asn1c-usage.html#SECTION02116000000000000000">The RELATIVE-OID type</A> |
| 65 | </UL> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 66 | <LI><A NAME="tex2html62" |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 67 | HREF="asn1c-usage.html#SECTION02120000000000000000">Some of the ASN.1 String Types</A> |
| 68 | <UL> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 69 | <LI><A NAME="tex2html63" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 70 | HREF="asn1c-usage.html#SECTION02121000000000000000">The IA5String type</A> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 71 | <LI><A NAME="tex2html64" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 72 | HREF="asn1c-usage.html#SECTION02122000000000000000">The UTF8String type</A> |
| 73 | <LI><A NAME="tex2html65" |
| 74 | HREF="asn1c-usage.html#SECTION02123000000000000000">The NumericString type</A> |
| 75 | <LI><A NAME="tex2html66" |
| 76 | HREF="asn1c-usage.html#SECTION02124000000000000000">The PrintableString type</A> |
| 77 | <LI><A NAME="tex2html67" |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 78 | HREF="asn1c-usage.html#SECTION02125000000000000000">The VisibleString type</A> |
| 79 | </UL> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 80 | <LI><A NAME="tex2html68" |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 81 | HREF="asn1c-usage.html#SECTION02130000000000000000">ASN.1 Constructed Types</A> |
| 82 | <UL> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 83 | <LI><A NAME="tex2html69" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 84 | HREF="asn1c-usage.html#SECTION02131000000000000000">The SEQUENCE type</A> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 85 | <LI><A NAME="tex2html70" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 86 | HREF="asn1c-usage.html#SECTION02132000000000000000">The SET type</A> |
| 87 | <LI><A NAME="tex2html71" |
| 88 | HREF="asn1c-usage.html#SECTION02133000000000000000">The CHOICE type</A> |
| 89 | <LI><A NAME="tex2html72" |
| 90 | HREF="asn1c-usage.html#SECTION02134000000000000000">The SEQUENCE OF type</A> |
| 91 | <LI><A NAME="tex2html73" |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 92 | HREF="asn1c-usage.html#SECTION02135000000000000000">The SET OF type</A> |
| 93 | </UL> |
| 94 | </UL> |
| 95 | </UL><BR> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 96 | <LI><A NAME="tex2html74" |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 97 | HREF="asn1c-usage.html#SECTION03000000000000000000">Using the ASN.1 Compiler</A> |
| 98 | <UL> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 99 | <LI><A NAME="tex2html75" |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 100 | HREF="asn1c-usage.html#SECTION03100000000000000000">Introduction to the ASN.1 Compiler</A> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 101 | <LI><A NAME="tex2html76" |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 102 | HREF="asn1c-usage.html#SECTION03200000000000000000">Quick start</A> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 103 | <LI><A NAME="tex2html77" |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 104 | HREF="asn1c-usage.html#SECTION03300000000000000000">Using the ASN.1 Compiler</A> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 105 | <UL> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 106 | <LI><A NAME="tex2html78" |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 107 | HREF="asn1c-usage.html#SECTION03310000000000000000">Command-line options</A> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 108 | <LI><A NAME="tex2html79" |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 109 | HREF="asn1c-usage.html#SECTION03320000000000000000">Recognizing compiler output</A> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 110 | <LI><A NAME="tex2html80" |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 111 | HREF="asn1c-usage.html#SECTION03330000000000000000">Invoking the ASN.1 helper code from an application</A> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 112 | <UL> |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 113 | <LI><A NAME="tex2html81" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 114 | HREF="asn1c-usage.html#SECTION03331000000000000000">Decoding BER</A> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 115 | <LI><A NAME="tex2html82" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 116 | HREF="asn1c-usage.html#SECTION03332000000000000000">Encoding DER</A> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 117 | <LI><A NAME="tex2html83" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 118 | HREF="asn1c-usage.html#SECTION03333000000000000000">Encoding XER</A> |
| 119 | <LI><A NAME="tex2html84" |
| 120 | HREF="asn1c-usage.html#SECTION03334000000000000000">Validating the target structure</A> |
| 121 | <LI><A NAME="tex2html85" |
| 122 | HREF="asn1c-usage.html#SECTION03335000000000000000">Printing the target structure</A> |
| 123 | <LI><A NAME="tex2html86" |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 124 | HREF="asn1c-usage.html#SECTION03336000000000000000">Freeing the target structure</A> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 125 | </UL> |
| 126 | </UL> |
| 127 | </UL><BR> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 128 | <LI><A NAME="tex2html87" |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 129 | HREF="asn1c-usage.html#SECTION04000000000000000000">Examples</A> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 130 | <UL> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 131 | <LI><A NAME="tex2html88" |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 132 | HREF="asn1c-usage.html#SECTION04100000000000000000">Step by step examples</A> |
| 133 | <UL> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 134 | <LI><A NAME="tex2html89" |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 135 | HREF="asn1c-usage.html#SECTION04110000000000000000">A ''Rectangle'' Encoder</A> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 136 | <LI><A NAME="tex2html90" |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 137 | HREF="asn1c-usage.html#SECTION04120000000000000000">A ''Rectangle'' Decoder</A> |
| 138 | </UL> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 139 | <LI><A NAME="tex2html91" |
| 140 | HREF="asn1c-usage.html#SECTION04200000000000000000">Constraint validation examples</A> |
| 141 | <UL> |
| 142 | <LI><A NAME="tex2html92" |
| 143 | HREF="asn1c-usage.html#SECTION04210000000000000000">Adding constraints into ''Rectangle'' type</A> |
| 144 | </UL> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 145 | </UL><BR> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 146 | <LI><A NAME="tex2html93" |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 147 | HREF="asn1c-usage.html#SECTION05000000000000000000">Bibliography</A> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 148 | </UL> |
| 149 | <!--End of Table of Contents--> |
| 150 | |
| 151 | <P> |
| 152 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 153 | <P> |
| 154 | |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 155 | <H1><A NAME="SECTION02000000000000000000"> |
| 156 | ASN.1 Basics</A> |
| 157 | </H1> |
| 158 | |
| 159 | <P> |
| 160 | |
| 161 | <H1><A NAME="SECTION02100000000000000000"> |
| 162 | Abstract Syntax Notation: ASN.1</A> |
| 163 | </H1> |
| 164 | |
| 165 | <P> |
| 166 | <I>This chapter defines some basic ASN.1 concepts and describes |
| 167 | several most widely used types. It is by no means an authoritative |
| 168 | or complete reference. For more complete ASN.1 description, please |
| 169 | refer to Olivier Dubuisson's book [<A |
| 170 | HREF="asn1c-usage.html#Dub00">Dub00</A>] or the ASN.1 body |
| 171 | of standards itself [<A |
| 172 | HREF="asn1c-usage.html#ITU-T_ASN.1">ITU-T/ASN.1</A>].</I> |
| 173 | |
| 174 | <P> |
| 175 | The Abstract Syntax Notation One is used to formally describe the |
| 176 | semantics of data transmitted across the network. Two communicating |
| 177 | parties may have different formats of their native data types (i.e. |
| 178 | number of bits in the integer type), thus it is important to have |
| 179 | a way to describe the data in a manner which is independent from the |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 180 | particular machine's representation. The ASN.1 specifications are |
| 181 | used to achieve the following: |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 182 | |
| 183 | <P> |
| 184 | |
| 185 | <UL> |
| 186 | <LI>The specification expressed in the ASN.1 notation is a formal and |
| 187 | precise way to communicate the data semantics to human readers; |
| 188 | </LI> |
| 189 | <LI>The ASN.1 specifications may be used as input for automatic compilers |
| 190 | which produce the code for some target language (C, C++, Java, etc) |
| 191 | to encode and decode the data according to some encoding rules (which |
| 192 | are also defined by the ASN.1 standard). |
| 193 | </LI> |
| 194 | </UL> |
| 195 | Consider the following example: |
| 196 | |
| 197 | <P> |
| 198 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 199 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 200 | Rectangle ::= SEQUENCE { |
| 201 | height INTEGER, |
| 202 | width INTEGER |
| 203 | } |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 204 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 205 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 206 | This ASN.1 specification describes a constructed type, <I>Rectangle</I>, |
| 207 | containing two integer fields. This specification may tell the reader |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 208 | that there exists this kind of data structure and that some entity |
| 209 | may be prepared to send or receive it. The question on <I>how</I> |
| 210 | that entity is going to send or receive the <I>encoded data</I> is |
| 211 | outside the scope of ASN.1. For example, this data structure may be |
| 212 | encoded according to some encoding rules and sent to the destination |
| 213 | using the TCP protocol. The ASN.1 specifies several ways of encoding |
| 214 | (or ''serializing'', or ''marshaling'') the data: BER, CER, DER |
| 215 | and XER, some of them which will be outlined later. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 216 | |
| 217 | <P> |
| 218 | The complete specification must be wrapped in a module, which looks |
| 219 | like this: |
| 220 | |
| 221 | <P> |
| 222 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 223 | <BLOCKQUOTE><PRE> |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 224 | RectangleModule1 |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 225 | { iso org(3) dod(6) internet(1) private(4) |
| 226 | enterprise(1) spelio(9363) software(1) |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 227 | asn1c(5) docs(2) rectangle(1) 1 } |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 228 | DEFINITIONS AUTOMATIC TAGS ::= |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 229 | BEGIN |
| 230 | |
| 231 | -- This is a comment which describes nothing. |
| 232 | Rectangle ::= SEQUENCE { |
| 233 | height INTEGER, -- Height of the rectangle |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 234 | width INTEGER -- Width of the rectangle |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | END |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 238 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 239 | </BLOCKQUOTE> |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 240 | The module header consists of module name (RectangleModule1), the |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 241 | module object identifier ({...}), a keyword ''DEFINITIONS'', a |
| 242 | set of module flags (AUTOMATIC TAGS) and ''::= BEGIN''. The module |
| 243 | ends with an ''END'' statement. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 244 | |
| 245 | <P> |
| 246 | |
| 247 | <H1><A NAME="SECTION02110000000000000000"> |
| 248 | Some of the ASN.1 Basic Types</A> |
| 249 | </H1> |
| 250 | |
| 251 | <P> |
| 252 | |
| 253 | <H2><A NAME="SECTION02111000000000000000"> |
| 254 | The BOOLEAN type</A> |
| 255 | </H2> |
| 256 | |
| 257 | <P> |
| 258 | The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF |
| 259 | or a similar kind of two-way choice. |
| 260 | |
| 261 | <P> |
| 262 | |
| 263 | <H2><A NAME="SECTION02112000000000000000"> |
| 264 | The INTEGER type</A> |
| 265 | </H2> |
| 266 | |
| 267 | <P> |
| 268 | The INTEGER type is a signed natural number type without any restrictions |
| 269 | on its size. If the automatic checking on INTEGER value bounds are |
| 270 | necessary, the subtype constraints must be used. |
| 271 | |
| 272 | <P> |
| 273 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 274 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 275 | SimpleInteger ::= INTEGER |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 276 | |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 277 | -- An integer with a very limited range |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 278 | SmallPositiveInt ::= INTEGER (0..127) |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 279 | |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 280 | -- Integer, negative |
| 281 | NegativeInt ::= INTEGER (MIN..0) |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 282 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 283 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 284 | |
| 285 | <P> |
| 286 | |
| 287 | <H2><A NAME="SECTION02113000000000000000"> |
| 288 | The ENUMERATED type</A> |
| 289 | </H2> |
| 290 | |
| 291 | <P> |
| 292 | The ENUMERATED type is semantically equivalent to the INTEGER type |
| 293 | with some integer values explicitly named. |
| 294 | |
| 295 | <P> |
| 296 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 297 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 298 | FruitId ::= ENUMERATED { apple(1), orange(2) } |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 299 | |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 300 | -- The numbers in braces are optional, |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 301 | -- the enumeration can be performed |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 302 | -- automatically by the compiler |
| 303 | ComputerOSType ::= ENUMERATED { |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 304 | FreeBSD, -- acquires value 0 |
| 305 | Windows, -- acquires value 1 |
| 306 | Solaris(5), -- remains 5 |
| 307 | Linux, -- becomes 6 |
| 308 | MacOS -- becomes 7 |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 309 | } |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 310 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 311 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 312 | |
| 313 | <P> |
| 314 | |
| 315 | <H2><A NAME="SECTION02114000000000000000"> |
| 316 | The OCTET STRING type</A> |
| 317 | </H2> |
| 318 | |
| 319 | <P> |
| 320 | This type models the sequence of 8-bit bytes. This may be used to |
| 321 | transmit some opaque data or data serialized by other types of encoders |
| 322 | (i.e. video file, photo picture, etc). |
| 323 | |
| 324 | <P> |
| 325 | |
| 326 | <H2><A NAME="SECTION02115000000000000000"> |
| 327 | The OBJECT IDENTIFIER type</A> |
| 328 | </H2> |
| 329 | |
| 330 | <P> |
| 331 | The OBJECT IDENTIFIER is used to represent the unique identifier of |
| 332 | any object, starting from the very root of the registration tree. |
| 333 | If your organization needs to uniquely identify something (a router, |
| 334 | a room, a person, a standard, or whatever), you are encouraged to |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 335 | get your own identification subtree at <A HREF=http://www.iana.org/protocols/forms.htm>http://www.iana.org/protocols/forms.htm</A>. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 336 | |
| 337 | <P> |
| 338 | For example, the very first ASN.1 module in this document has the |
| 339 | following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1. |
| 340 | |
| 341 | <P> |
| 342 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 343 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 344 | ExampleOID ::= OBJECT IDENTIFIER |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 345 | |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 346 | rectangleModule1-oid ExampleOID |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 347 | ::= { 1 3 6 1 4 1 9363 1 5 2 1 1 } |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 348 | |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 349 | -- An identifier of the Internet. |
| 350 | internet-id OBJECT IDENTIFIER |
| 351 | ::= { iso(1) identified-organization(3) |
| 352 | dod(6) internet(1) } |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 353 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 354 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 355 | As you see, names are optional. |
| 356 | |
| 357 | <P> |
| 358 | |
| 359 | <H2><A NAME="SECTION02116000000000000000"> |
| 360 | The RELATIVE-OID type</A> |
| 361 | </H2> |
| 362 | |
| 363 | <P> |
| 364 | The RELATIVE-OID type has the semantics of a subtree of an OBJECT |
| 365 | IDENTIFIER. There may be no need to repeat the whole sequence of numbers |
| 366 | from the root of the registration tree where the only thing of interest |
| 367 | is some of the tree's subsequence. |
| 368 | |
| 369 | <P> |
| 370 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 371 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 372 | this-document RELATIVE-OID ::= { docs(2) usage(1) } |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 373 | |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 374 | this-example RELATIVE-OID ::= { |
| 375 | this-document assorted-examples(0) this-example(1) } |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 376 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 377 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 378 | |
| 379 | <P> |
| 380 | |
| 381 | <H1><A NAME="SECTION02120000000000000000"> |
| 382 | Some of the ASN.1 String Types</A> |
| 383 | </H1> |
| 384 | |
| 385 | <P> |
| 386 | |
| 387 | <H2><A NAME="SECTION02121000000000000000"> |
| 388 | The IA5String type</A> |
| 389 | </H2> |
| 390 | |
| 391 | <P> |
| 392 | This is essentially the ASCII, with 128 character codes available |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 393 | (7 lower bits of an 8-bit byte). |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 394 | |
| 395 | <P> |
| 396 | |
| 397 | <H2><A NAME="SECTION02122000000000000000"> |
| 398 | The UTF8String type</A> |
| 399 | </H2> |
| 400 | |
| 401 | <P> |
| 402 | This is the character string which encodes the full Unicode range |
| 403 | (4 bytes) using multibyte character sequences. |
| 404 | |
| 405 | <P> |
| 406 | |
| 407 | <H2><A NAME="SECTION02123000000000000000"> |
| 408 | The NumericString type</A> |
| 409 | </H2> |
| 410 | |
| 411 | <P> |
| 412 | This type represents the character string with the alphabet consisting |
| 413 | of numbers (''0'' to ''9'') and a space. |
| 414 | |
| 415 | <P> |
| 416 | |
| 417 | <H2><A NAME="SECTION02124000000000000000"> |
| 418 | The PrintableString type</A> |
| 419 | </H2> |
| 420 | |
| 421 | <P> |
| 422 | The character string with the following alphabet: space, ''<B>'</B>'' |
| 423 | (single quote), ''<B>(</B>'', ''<B>)</B>'', ''<B>+</B>'', |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 424 | ''<B>,</B>'' (comma), ''<B>-</B>'', ''<B>.</B>'', ''<B>/</B>'', |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 425 | digits (''0'' to ''9''), ''<B>:</B>'', ''<B>=</B>'', ''<B>?</B>'', |
| 426 | upper-case and lower-case letters (''A'' to ''Z'' and ''a'' |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 427 | to ''z''). |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 428 | |
| 429 | <P> |
| 430 | |
| 431 | <H2><A NAME="SECTION02125000000000000000"> |
| 432 | The VisibleString type</A> |
| 433 | </H2> |
| 434 | |
| 435 | <P> |
| 436 | The character string with the alphabet which is more or less a subset |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 437 | of ASCII between the space and the ''<B>~</B>'' |
| 438 | symbol (tilde). |
| 439 | |
| 440 | <P> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 441 | Alternatively, the alphabet may be described as the PrintableString |
| 442 | alphabet presented earlier, plus the following characters: ''<B>!</B>'', |
| 443 | ''<B>''</B>'', ''<B>#</B>'', ''<B>$</B>'', ''<B>%</B>'', |
| 444 | ''<B>&</B>'', ''<B>*</B>'', ''<B>;</B>'', ''<B><</B>'', |
| 445 | ''<B>></B>'', ''<B>[</B>'', ''<B>\</B>'', |
| 446 | ''<B>]</B>'', ''<B>^</B>'', ''<B>_</B>'', |
| 447 | ''<B>`</B>'' (single left quote), ''<B>{</B>'', ''<B>|</B>'', |
| 448 | ''<B>}</B>'', ''<B>~</B>''. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 449 | |
| 450 | <P> |
| 451 | |
| 452 | <H1><A NAME="SECTION02130000000000000000"> |
| 453 | ASN.1 Constructed Types</A> |
| 454 | </H1> |
| 455 | |
| 456 | <P> |
| 457 | |
| 458 | <H2><A NAME="SECTION02131000000000000000"> |
| 459 | The SEQUENCE type</A> |
| 460 | </H2> |
| 461 | |
| 462 | <P> |
| 463 | This is an ordered collection of other simple or constructed types. |
| 464 | The SEQUENCE constructed type resembles the C ''struct'' statement. |
| 465 | |
| 466 | <P> |
| 467 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 468 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 469 | Address ::= SEQUENCE { |
| 470 | -- The apartment number may be omitted |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 471 | apartmentNumber NumericString OPTIONAL, |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 472 | streetName PrintableString, |
| 473 | cityName PrintableString, |
| 474 | stateName PrintableString, |
| 475 | -- This one may be omitted too |
| 476 | zipNo NumericString OPTIONAL |
| 477 | } |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 478 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 479 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 480 | |
| 481 | <P> |
| 482 | |
| 483 | <H2><A NAME="SECTION02132000000000000000"> |
| 484 | The SET type</A> |
| 485 | </H2> |
| 486 | |
| 487 | <P> |
| 488 | This is a collection of other simple or constructed types. Ordering |
| 489 | is not important. The data may arrive in the order which is different |
| 490 | from the order of specification. Data is encoded in the order not |
| 491 | necessarily corresponding to the order of specification. |
| 492 | |
| 493 | <P> |
| 494 | |
| 495 | <H2><A NAME="SECTION02133000000000000000"> |
| 496 | The CHOICE type</A> |
| 497 | </H2> |
| 498 | |
| 499 | <P> |
| 500 | This type is just a choice between the subtypes specified in it. The |
| 501 | CHOICE type contains at most one of the subtypes specified, and it |
| 502 | is always implicitly known which choice is being decoded or encoded. |
| 503 | This one resembles the C ''union'' statement. |
| 504 | |
| 505 | <P> |
| 506 | The following type defines a response code, which may be either an |
| 507 | integer code or a boolean ''true''/''false'' code. |
| 508 | |
| 509 | <P> |
| 510 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 511 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 512 | ResponseCode ::= CHOICE { |
| 513 | intCode INTEGER, |
| 514 | boolCode BOOLEAN |
| 515 | } |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 516 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 517 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 518 | |
| 519 | <P> |
| 520 | |
| 521 | <H2><A NAME="SECTION02134000000000000000"> |
| 522 | The SEQUENCE OF type</A> |
| 523 | </H2> |
| 524 | |
| 525 | <P> |
| 526 | This one is the list (array) of simple or constructed types: |
| 527 | |
| 528 | <P> |
| 529 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 530 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 531 | -- Example 1 |
| 532 | ManyIntegers ::= SEQUENCE OF INTEGER |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 533 | |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 534 | -- Example 2 |
| 535 | ManyRectangles ::= SEQUENCE OF Rectangle |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 536 | |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 537 | -- More complex example: |
| 538 | -- an array of structures defined in place. |
| 539 | ManyCircles ::= SEQUENCE OF SEQUENCE { |
| 540 | radius INTEGER |
| 541 | } |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 542 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 543 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 544 | |
| 545 | <P> |
| 546 | |
| 547 | <H2><A NAME="SECTION02135000000000000000"> |
| 548 | The SET OF type</A> |
| 549 | </H2> |
| 550 | |
| 551 | <P> |
| 552 | The SET OF type models the bag of structures. It resembles the SEQUENCE |
| 553 | OF type, but the order is not important: i.e. the elements may arrive |
| 554 | in the order which is not necessarily the same as the in-memory order |
| 555 | on the remote machines. |
| 556 | |
| 557 | <P> |
| 558 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 559 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 560 | -- A set of structures defined elsewhere |
| 561 | SetOfApples :: SET OF Apple |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 562 | |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 563 | -- Set of integers encoding the kind of a fruit |
| 564 | FruitBag ::= SET OF ENUMERATED { apple, orange } |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 565 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 566 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 567 | |
| 568 | <P> |
| 569 | |
| 570 | <H1><A NAME="SECTION03000000000000000000"> |
| 571 | Using the ASN.1 Compiler</A> |
| 572 | </H1> |
| 573 | |
| 574 | <P> |
| 575 | |
| 576 | <H1><A NAME="SECTION03100000000000000000"> |
| 577 | Introduction to the ASN.1 Compiler</A> |
| 578 | </H1> |
| 579 | |
| 580 | <P> |
| 581 | The purpose of the ASN.1 compiler, of which this document is part, |
| 582 | is to convert the ASN.1 specifications to some other target language |
| 583 | (currently, only C is supported<A NAME="tex2html1" |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 584 | HREF="#foot159"><SUP>2.1</SUP></A>). The compiler reads the specification and emits a series of target |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 585 | language structures and surrounding maintenance code. For example, |
| 586 | the C structure which may be created by compiler to represent the |
| 587 | simple <I>Rectangle</I> specification defined earlier in this document, |
| 588 | may look like this<A NAME="tex2html2" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 589 | HREF="#foot801"><SUP>2.2</SUP></A>: |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 590 | |
| 591 | <P> |
| 592 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 593 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 594 | typedef struct Rectangle_s { |
| 595 | int height; |
| 596 | int width; |
| 597 | } Rectangle_t; |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 598 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 599 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 600 | This would not be of much value for such a simple specification, so |
| 601 | the compiler goes further and actually produces the code which fills |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 602 | in this structure by parsing the opaque binary<A NAME="tex2html3" |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 603 | HREF="#foot166"><SUP>2.3</SUP></A> data provided in some buffer. It also produces the code that takes |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 604 | this structure as an argument and performs structure serialization |
| 605 | by emitting a series of bytes. |
| 606 | |
| 607 | <P> |
| 608 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 609 | <H1><A NAME="SECTION03200000000000000000"> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 610 | Quick start</A> |
| 611 | </H1> |
| 612 | |
| 613 | <P> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 614 | After building and installing the compiler, the <I>asn1c</I><A NAME="tex2html4" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 615 | HREF="#foot802"><SUP>3.1</SUP></A> command may be used to compile the ASN.1 specification<A NAME="tex2html5" |
| 616 | HREF="#foot803"><SUP>3.2</SUP></A>: |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 617 | |
| 618 | <P> |
| 619 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 620 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 621 | asn1c <I><spec.asn1></I> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 622 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 623 | </BLOCKQUOTE> |
| 624 | If several specifications contain interdependencies, all of the files |
| 625 | must be specified altogether: |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 626 | |
| 627 | <P> |
| 628 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 629 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 630 | asn1c <I><spec1.asn1> <spec2.asn1> ...</I> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 631 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 632 | </BLOCKQUOTE> |
| 633 | The compiler <B>-E</B> and <B>-EF</B> options are used for testing |
| 634 | the parser and the semantic fixer, respectively. These options will |
| 635 | instruct the compiler to dump out the parsed (and fixed, if <B>-F</B> |
| 636 | is involved) ASN.1 specification as it was "understood" |
| 637 | by the compiler. It might be useful to check whether a particular |
| 638 | syntactic construction is properly supported by the compiler. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 639 | |
| 640 | <P> |
| 641 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 642 | <BLOCKQUOTE><PRE> |
| 643 | asn1c <B>-EF</B> <I><spec-to-test.asn1></I> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 644 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 645 | </BLOCKQUOTE> |
| 646 | The <B>-P</B> option is used to dump the compiled output on the |
| 647 | screen instead of creating a bunch of .c and .h files on disk in the |
| 648 | current directory. You would probably want to start with <B>-P</B> |
| 649 | option instead of creating a mess in your current directory. Another |
| 650 | option, <B>-R</B>, asks compiler to only generate the files which |
| 651 | need to be generated, and supress linking in the numerous support |
| 652 | files. |
| 653 | |
| 654 | <P> |
| 655 | Print the compiled output instead of creating multiple source files: |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 656 | |
| 657 | <P> |
| 658 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 659 | <BLOCKQUOTE><PRE> |
| 660 | asn1c <B>-P</B> <I><spec-to-compile-and-print.asn1></I> |
| 661 | </PRE> |
| 662 | </BLOCKQUOTE> |
| 663 | |
| 664 | <P> |
| 665 | |
| 666 | <H1><A NAME="SECTION03300000000000000000"> |
| 667 | Using the ASN.1 Compiler</A> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 668 | </H1> |
| 669 | |
| 670 | <P> |
| 671 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 672 | <H1><A NAME="SECTION03310000000000000000"> |
| 673 | Command-line options</A> |
| 674 | </H1> |
| 675 | |
| 676 | <P> |
| 677 | The <A HREF=#Table1>Table 1</A> summarizes various options affecting |
| 678 | the compiler's behavior. |
| 679 | |
| 680 | <P> |
| 681 | <BR><P></P> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 682 | <DIV ALIGN="CENTER"><A NAME="806"></A> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 683 | <TABLE> |
| 684 | <CAPTION><STRONG><A NAME=Table1>Table 1:</A></STRONG> |
| 685 | The list of asn1c command line options</CAPTION> |
| 686 | <TR><TD><TABLE COLS=2 BORDER FRAME=BOX RULES=GROUPS> |
| 687 | <COLGROUP><COL ALIGN=LEFT><COLGROUP><COL ALIGN=JUSTIFY WIDTH="3in"> |
| 688 | <TBODY> |
| 689 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> |
| 690 | <B>Overall Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 691 | <B>Description</B></TD></TR> |
| 692 | </TBODY><TBODY> |
| 693 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-E</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 694 | <FONT SIZE="-1">Stop after the parsing stage and print the reconstructed ASN.1 |
| 695 | specification code to the standard output.</FONT></TD></TR> |
| 696 | </TBODY><TBODY> |
| 697 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-F</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 698 | <FONT SIZE="-1">Used together with -E, instructs the compiler to stop after |
| 699 | the ASN.1 syntax tree fixing stage and dump the reconstructed ASN.1 |
| 700 | specification to the standard output.</FONT></TD></TR> |
| 701 | </TBODY><TBODY> |
| 702 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-P</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 703 | <FONT SIZE="-1">Dump the compiled output to the standard output instead of |
| 704 | cre- ating the target language files on disk.</FONT></TD></TR> |
| 705 | </TBODY><TBODY> |
| 706 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-R</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 707 | <FONT SIZE="-1">Restrict the compiler to generate only the ASN.1 tables, omit- |
| 708 | ting the usual support code.</FONT></TD></TR> |
| 709 | </TBODY><TBODY> |
| 710 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-S <I><directory></I></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 711 | <FONT SIZE="-1">Use the specified directory with ASN.1 skeleton files.</FONT></TD></TR> |
| 712 | </TBODY><TBODY> |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 713 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-X</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 714 | Generate the XML DTD for the specified ASN.1 modules.</TD></TR> |
vlm | 60e7ef0 | 2004-10-13 09:13:56 +0000 | [diff] [blame] | 715 | </TBODY><TBODY> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 716 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Warning Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 717 | <B>Description</B></TD></TR> |
| 718 | </TBODY><TBODY> |
| 719 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Werror</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 720 | <FONT SIZE="-1">Treat warnings as errors; abort if any warning is produced.</FONT></TD></TR> |
| 721 | </TBODY><TBODY> |
| 722 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-lexer</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 723 | <FONT SIZE="-1">Enable lexer debugging during the ASN.1 parsing stage.</FONT></TD></TR> |
| 724 | </TBODY><TBODY> |
| 725 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-fixer</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 726 | <FONT SIZE="-1">Enable ASN.1 syntax tree fixer debugging during the |
| 727 | fixing stage.</FONT></TD></TR> |
| 728 | </TBODY><TBODY> |
| 729 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-Wdebug-compiler</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 730 | <FONT SIZE="-1">Enable debugging during the actual compile time.</FONT></TD></TR> |
| 731 | </TBODY><TBODY> |
| 732 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Language Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 733 | <B>Description</B></TD></TR> |
| 734 | </TBODY><TBODY> |
vlm | 03eb4e1 | 2004-09-08 03:10:54 +0000 | [diff] [blame] | 735 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fall-defs-global</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 736 | Normally the compiler hides the definitions (asn_DEF_xxx) of the |
vlm | 03eb4e1 | 2004-09-08 03:10:54 +0000 | [diff] [blame] | 737 | inner structure elements (members of SEQUENCE, SET and other types). |
| 738 | This option makes all such definitions global. Enabling this option |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 739 | may pollute the namespace by making lots of asn_DEF_xxx structures |
vlm | 03eb4e1 | 2004-09-08 03:10:54 +0000 | [diff] [blame] | 740 | globally visible, but will allow you to manipulate (encode and decode) |
| 741 | the individual members of any complex ASN.1 structure.</TD></TR> |
| 742 | </TBODY><TBODY> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 743 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fbless-SIZE</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 744 | <FONT SIZE="-1">Allow SIZE() constraint for INTEGER, ENUMERATED, and other |
| 745 | types for which this constraint is normally prohibited by the standard. |
| 746 | This is a violation of an ASN.1 standard and compiler may fail to |
| 747 | produce the meaningful code.</FONT></TD></TR> |
| 748 | </TBODY><TBODY> |
vlm | 1111525 | 2004-09-17 08:35:02 +0000 | [diff] [blame] | 749 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fnative-types</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 750 | <FONT SIZE="-1">Use the native machine's data types (int, double) whenever |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 751 | possible, instead of the compound INTEGER_t, ENUMERATED_t and REAL_t |
| 752 | types. </FONT></TD></TR> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 753 | </TBODY><TBODY> |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 754 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-fno-constraints</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 755 | Do not generate ASN.1 subtype constraint checking code. This may make |
| 756 | a shorter executable.</TD></TR> |
| 757 | </TBODY><TBODY> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 758 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-funnamed-unions</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 759 | <FONT SIZE="-1">Enable unnamed unions in the definitions of target language's |
| 760 | structures.</FONT></TD></TR> |
| 761 | </TBODY><TBODY> |
| 762 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-ftypes88</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 763 | <FONT SIZE="-1">Pretend to support only ASN.1:1988 embedded types. Certain |
| 764 | reserved words, such as UniversalString and BMPString, become ordinary |
| 765 | type references and may be redefined by the specification.</FONT></TD></TR> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 766 | </TBODY><TBODY> |
| 767 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP><B>Output Options</B></TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 768 | <B>Description</B></TD></TR> |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 769 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP> |
| 770 | |
| 771 | -print-constraints</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 772 | <FONT SIZE="-1">When -EF are also specified, this option forces the compiler |
| 773 | to explain its internal understanding of subtype constraints.</FONT></TD></TR> |
| 774 | </TBODY><TBODY> |
| 775 | <TR><TD VALIGN=BASELINE ALIGN=LEFT NOWRAP>-print-lines</TD><TD VALIGN=BASELINE ALIGN=LEFT WIDTH="216"> |
| 776 | <FONT SIZE="-1">Generate "- #line" comments in -E output.</FONT></TD></TR> |
| 777 | </TBODY> |
| 778 | </TABLE> |
| 779 | |
| 780 | <P> |
| 781 | </TD></TR> |
| 782 | </TABLE> |
| 783 | </DIV><P></P><BR> |
| 784 | |
| 785 | <P> |
| 786 | |
| 787 | <H1><A NAME="SECTION03320000000000000000"> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 788 | Recognizing compiler output</A> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 789 | </H1> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 790 | |
| 791 | <P> |
| 792 | After compiling, the following entities will be created in your current |
| 793 | directory: |
| 794 | |
| 795 | <P> |
| 796 | |
| 797 | <UL> |
| 798 | <LI>A set of .c and .h files, generally a single pair for each type defined |
| 799 | in the ASN.1 specifications. These files will be named similarly to |
| 800 | the ASN.1 types (<I>Rectangle.c</I> and <I>Rectangle.h</I> for the |
| 801 | specification defined in the beginning of this document). |
| 802 | </LI> |
| 803 | <LI>A set of helper .c and .h files which contain generic encoders, decoders |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 804 | and other useful routines. There will be quite a few of them, some |
| 805 | of them even are not always necessary, but the overall amount of code |
| 806 | after compiling will be rather small anyway. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 807 | </LI> |
| 808 | </UL> |
| 809 | It is your responsibility to create .c file with the <I>int main()</I> |
| 810 | routine and the Makefile (if needed). Compiler helps you with the |
| 811 | latter by creating the Makefile.am.sample, containing the skeleton |
| 812 | definition for the automake, should you want to use autotools. |
| 813 | |
| 814 | <P> |
| 815 | In other words, after compiling the Rectangle module, you have the |
| 816 | following set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h, |
| 817 | <B>...</B> }, where <B>''...''</B> stands for the |
| 818 | set of additional ''helper'' files created by the compiler. If you |
| 819 | add the simple file with the <I>int main()</I> routine, it would even |
| 820 | be possible to compile everything with the single instruction: |
| 821 | |
| 822 | <P> |
| 823 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 824 | <BLOCKQUOTE><PRE> |
| 825 | cc -o rectangle *.c # It could be <I>that</I> simple<A NAME="tex2html7" |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 826 | HREF="#foot809"><SUP>4.1</SUP></A> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 827 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 828 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 829 | |
| 830 | <P> |
| 831 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 832 | <H1><A NAME="SECTION03330000000000000000"> |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 833 | Invoking the ASN.1 helper code from an application</A> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 834 | </H1> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 835 | |
| 836 | <P> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 837 | First of all, you should to include one or more header files into |
| 838 | your application. For our Rectangle module, including the Rectangle.h |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 839 | file is enough: |
| 840 | |
| 841 | <P> |
| 842 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 843 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 844 | #include <Rectangle.h> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 845 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 846 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 847 | The header files defines the C structure corresponding to the ASN.1 |
| 848 | definition of a rectangle and the declaration of the ASN.1 type descriptor, |
| 849 | which is used as an argument to most of the functions provided by |
| 850 | the ASN.1 module. For example, here is the code which frees the Rectangle_t |
| 851 | structure: |
| 852 | |
| 853 | <P> |
| 854 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 855 | <BLOCKQUOTE><PRE> |
| 856 | Rectangle_t *rect = ...; |
| 857 | |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 858 | asn_DEF_Rectangle->free_struct(&asn_DEF_Rectangle, |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 859 | rect, 0); |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 860 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 861 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 862 | This code defines a <I>rect</I> pointer which points to the Rectangle_t |
| 863 | structure which needs to be freed. The second line invokes the generic |
| 864 | free_struct routine created specifically for this Rectangle_t structure. |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 865 | The <I>asn_DEF_Rectangle</I> is the type descriptor, which holds |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 866 | a collection of generic routines to deal with the Rectangle_t structure. |
| 867 | |
| 868 | <P> |
| 869 | There are several generic functions available: |
| 870 | |
| 871 | <P> |
| 872 | <DL> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 873 | <DT><STRONG>ber_decoder</STRONG></DT> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 874 | <DD>This is the generic <I>restartable</I><A NAME="tex2html8" |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 875 | HREF="#foot253"><SUP>4.2</SUP></A> BER decoder (Basic Encoding Rules). This decoder would create |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 876 | and/or fill the target structure for you. Please refer to Section |
| 877 | [<A HREF="#sub:Decoding-BER">Decoding-BER</A>]. |
| 878 | </DD> |
| 879 | <DT><STRONG>der_encoder</STRONG></DT> |
| 880 | <DD>This is the generic DER encoder (Distinguished Encoding |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 881 | Rules). This encoder will take the target structure and encode it |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 882 | into a series of bytes. Please refer to Section [<A HREF="#sub:Encoding-DER">Encoding-DER</A>]. |
| 883 | </DD> |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 884 | <DT><STRONG>xer_encoder</STRONG></DT> |
| 885 | <DD>This is the generic XER encoder (XML Encoding Rules). |
| 886 | This encoder will take the target structure and represent it as an |
| 887 | XML (text) document. Please refer to Section [<A HREF="#sub:Encoding-XER">Encoding-XER</A>]. |
| 888 | </DD> |
| 889 | <DT><STRONG>check_constraints</STRONG></DT> |
| 890 | <DD>Check that the contents of the target structure |
| 891 | are semantically valid and constrained to appropriate implicit or |
| 892 | explicit subtype constraints. Please refer to Section sub:Validating-the-target. |
| 893 | </DD> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 894 | <DT><STRONG>print_struct</STRONG></DT> |
| 895 | <DD>This function convert the contents of the passed target |
| 896 | structure into human readable form. This form is not formal and cannot |
| 897 | be converted back into the structure, but it may turn out to be useful |
| 898 | for debugging or quick-n-dirty printing. Please refer to Section [<A HREF="#sub:Printing-the-target">Printing-the-target</A>]. |
| 899 | </DD> |
| 900 | <DT><STRONG>free_struct</STRONG></DT> |
| 901 | <DD>This is a generic disposal which frees the target structure. |
| 902 | Please refer to Section [<A HREF="#sub:Freeing-the-target">Freeing-the-target</A>]. |
| 903 | </DD> |
| 904 | </DL> |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 905 | check_constraints Check that the contents of the target structure |
| 906 | are semantically valid and constrained to appropriate implicit or |
| 907 | explicit subtype constraints. Please refer to Section sub:Validating-the-target. |
| 908 | |
| 909 | <P> |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 910 | Each of the above function takes the type descriptor (<I>asn_DEF_...</I>) |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 911 | and the target structure (<I>rect</I>, in the above example). The |
| 912 | target structure is typically created by the generic BER decoder or |
| 913 | by the application itself. |
| 914 | |
| 915 | <P> |
| 916 | Here is how the buffer can be deserialized into the structure: |
| 917 | |
| 918 | <P> |
| 919 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 920 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 921 | Rectangle_t * |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 922 | simple_deserializer(const void *buffer, size_t buf_size) { |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 923 | Rectangle_t *rect = 0; /* Note this 0! */ |
vlm | af2155e | 2004-10-20 15:48:55 +0000 | [diff] [blame] | 924 | asn_dec_rval_t rval; |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 925 | |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 926 | rval = asn_DEF_Rectangle->ber_decoder(0, |
| 927 | &asn_DEF_Rectangle, |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 928 | (void **)&rect, |
| 929 | buffer, buf_size, |
| 930 | 0); |
| 931 | |
| 932 | if(rval<B>.code</B> == RC_OK) { |
| 933 | return rect; /* Decoding succeeded */ |
| 934 | } else { |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 935 | /* Free partially decoded rect */ |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 936 | asn_DEF_Rectangle->free_struct( |
| 937 | &asn_DEF_Rectangle, rect, 0); |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 938 | return 0; |
| 939 | } |
| 940 | } |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 941 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 942 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 943 | The code above defines a function, <I>simple_deserializer</I>, which |
| 944 | takes a buffer and its length and expected to return a pointer to |
| 945 | the Rectangle_t structure. Inside, it tries to convert the bytes |
| 946 | passed into the target structure (rect) using the generic BER decoder |
| 947 | and returns the rect pointer afterwards. If the structure cannot be |
| 948 | deserialized, it frees the memory which might be left allocated by |
| 949 | the unfinished <I>ber_decoder</I> routine and returns NULL. <B>This |
| 950 | freeing is necessary</B> because the ber_decoder is a restartable procedure, |
| 951 | and may fail just because there is more data needs to be provided |
| 952 | before decoding could be finalized. The code above obviously does |
| 953 | not take into account the way the <I>ber_decoder</I> failed, so the |
| 954 | freeing is necessary because the part of the buffer may already be |
| 955 | decoded into the structure by the time something goes wrong. |
| 956 | |
| 957 | <P> |
| 958 | Restartable decoding is a little bit trickier: you need to provide |
| 959 | the old target structure pointer (which might be already half-decoded) |
| 960 | and react on RC_WMORE return code. This will be explained later in |
| 961 | Section sub:Decoding-BER |
| 962 | |
| 963 | <P> |
| 964 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 965 | <H2><A NAME="SECTION03331000000000000000"></A><A NAME="sub:Decoding-BER"></A><BR> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 966 | Decoding BER |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 967 | </H2> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 968 | |
| 969 | <P> |
| 970 | The Basic Encoding Rules describe the basic way how the structure |
| 971 | can be encoded and decoded. Several other encoding rules (CER, DER) |
| 972 | define a more restrictive versions of BER, so the generic BER parser |
| 973 | is also capable of decoding the data encoded by CER and DER encoders. |
| 974 | The opposite is not true. |
| 975 | |
| 976 | <P> |
| 977 | The ASN.1 compiler provides the generic BER decoder which is implicitly |
| 978 | capable of decoding BER, CER and DER encoded data. |
| 979 | |
| 980 | <P> |
| 981 | The decoder is restartable (stream-oriented), which means that in |
| 982 | case the buffer has less data than it is expected, the decoder will |
| 983 | process whatever it is available and ask for more data to be provided. |
| 984 | Please note that the decoder may actually process less data than it |
| 985 | is given in the buffer, which means that you should be able to make |
| 986 | the next buffer contain the unprocessed part of the previous buffer. |
| 987 | |
| 988 | <P> |
| 989 | Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes. |
| 990 | |
| 991 | <P> |
| 992 | |
| 993 | <UL> |
| 994 | <LI>You may concatenate these buffers and feed the BER decoder with 300 |
| 995 | bytes of data, or |
| 996 | </LI> |
| 997 | <LI>You may feed it the first buffer of 100 bytes of data, realize that |
| 998 | the ber_decoder consumed only 95 bytes from it and later feed the |
| 999 | decoder with 205 bytes buffer which consists of 5 unprocessed bytes |
| 1000 | from the first buffer and the latter 200 bytes from the second buffer. |
| 1001 | </LI> |
| 1002 | </UL> |
| 1003 | This is not as convenient as it could be (like, the BER encoder would |
| 1004 | consume the whole 100 bytes and keep these 5 bytes in some temporary |
| 1005 | storage), but in case of stream-based processing it might actually |
| 1006 | be OK. Suggestions are welcome. |
| 1007 | |
| 1008 | <P> |
| 1009 | There are two ways to invoke a BER decoder. The first one is a direct |
| 1010 | reference of the type-specific decoder. This way was shown in the |
| 1011 | previous example of <I>simple_deserializer</I> function. The second |
| 1012 | way is to invoke a <I>ber_decode</I> function, which is just a simple |
| 1013 | wrapper of the former approach into a less wordy notation: |
| 1014 | |
| 1015 | <P> |
| 1016 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1017 | <BLOCKQUOTE><PRE> |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 1018 | rval = ber_decode(0, &asn_DEF_Rectangle, (void **)&rect, |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1019 | buffer, buf_size); |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1020 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1021 | </BLOCKQUOTE> |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 1022 | Note that the initial (asn_DEF_Rectangle->ber_decoder) reference |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1023 | is gone, and also the last argument (0) is no longer necessary. |
| 1024 | |
| 1025 | <P> |
| 1026 | These two ways of invocations are fully equivalent. |
| 1027 | |
| 1028 | <P> |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 1029 | The BER de<I>coder</I> may fail because of (<I>the following RC_... |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1030 | codes are defined in ber_decoder.h</I>): |
| 1031 | |
| 1032 | <P> |
| 1033 | |
| 1034 | <UL> |
| 1035 | <LI>RC_WMORE: There is more data expected than it is provided (stream |
| 1036 | mode continuation feature); |
| 1037 | </LI> |
| 1038 | <LI>RC_FAIL: General failure to decode the buffer; |
| 1039 | </LI> |
| 1040 | <LI>... other codes may be defined as well. |
| 1041 | </LI> |
| 1042 | </UL> |
vlm | af2155e | 2004-10-20 15:48:55 +0000 | [diff] [blame] | 1043 | Together with the return code (.code) the asn_dec_rval_t type contains |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1044 | the number of bytes which is consumed from the buffer. In the previous |
| 1045 | hypothetical example of two buffers (of 100 and 200 bytes), the first |
| 1046 | call to ber_decode() would return with .code = RC_WMORE and .consumed |
| 1047 | = 95. The .consumed field of the BER decoder return value is <B>always</B> |
| 1048 | valid, even if the decoder succeeds or fails with any other return |
| 1049 | code. |
| 1050 | |
| 1051 | <P> |
| 1052 | Please look into ber_decoder.h for the precise definition of ber_decode() |
| 1053 | and related types. |
| 1054 | |
| 1055 | <P> |
| 1056 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1057 | <H2><A NAME="SECTION03332000000000000000"></A><A NAME="sub:Encoding-DER"></A><BR> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1058 | Encoding DER |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1059 | </H2> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1060 | |
| 1061 | <P> |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1062 | The Distinguished Encoding Rules is the <I>canonical</I> variant of |
| 1063 | BER encoding rules. The DER is best suited to encode the structures |
| 1064 | where all the lengths are known beforehand. This is probably exactly |
| 1065 | how you want to encode: either after a BER decoding or after a manual |
| 1066 | fill-up, the target structure contains the data which size is implicitly |
| 1067 | known before encoding. The DER encoding is used, for example, to encode |
| 1068 | X.509 certificates. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1069 | |
| 1070 | <P> |
| 1071 | As with BER decoder, the DER encoder may be invoked either directly |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 1072 | from the ASN.1 type descriptor (asn_DEF_Rectangle) or from the stand-alone |
| 1073 | function, which is somewhat simpler: |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1074 | |
| 1075 | <P> |
| 1076 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1077 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1078 | |
| 1079 | /* |
| 1080 | * This is the serializer itself, |
| 1081 | * it supplies the DER encoder with the |
| 1082 | * pointer to the custom output function. |
| 1083 | */ |
| 1084 | ssize_t |
| 1085 | simple_serializer(FILE *ostream, Rectangle_t *rect) { |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1086 | asn_enc_rval_t er; /* Encoder return value */ |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1087 | |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 1088 | er = der_encode(&asn_DEF_Rect, rect, |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1089 | write_stream, ostream); |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1090 | if(er.<B>encoded</B> == -1) { |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1091 | /* |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1092 | * Failed to encode the rectangle data. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1093 | */ |
| 1094 | fprintf(stderr, ''Cannot encode %s: %s\n'', |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1095 | er.<B>failed_type</B>->name, |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1096 | strerror(errno)); |
| 1097 | return -1; |
| 1098 | } else { |
| 1099 | /* Return the number of bytes */ |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1100 | return er.encoded; |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1101 | } |
| 1102 | } |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1103 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1104 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1105 | As you see, the DER encoder does not write into some sort of buffer |
| 1106 | or something. It just invokes the custom function (possible, multiple |
| 1107 | times) which would save the data into appropriate storage. The optional |
| 1108 | argument <I>app_key</I> is opaque for the DER encoder code and just |
| 1109 | used by <I>_write_stream()</I> as the pointer to the appropriate |
| 1110 | output stream to be used. |
| 1111 | |
| 1112 | <P> |
| 1113 | If the custom write function is not given (passed as 0), then the |
| 1114 | DER encoder will essentially do the same thing (i.e., encode the data) |
| 1115 | but no callbacks will be invoked (so the data goes nowhere). It may |
| 1116 | prove useful to determine the size of the structure's encoding before |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1117 | actually doing the encoding<A NAME="tex2html9" |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 1118 | HREF="#foot322"><SUP>4.3</SUP></A>. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1119 | |
| 1120 | <P> |
| 1121 | Please look into der_encoder.h for the precise definition of der_encode() |
| 1122 | and related types. |
| 1123 | |
| 1124 | <P> |
| 1125 | |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1126 | <H2><A NAME="SECTION03333000000000000000"></A><A NAME="sub:Encoding-XER"></A><BR> |
| 1127 | Encoding XER |
| 1128 | </H2> |
| 1129 | |
| 1130 | <P> |
| 1131 | The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible |
| 1132 | Markup Language, a text-based format for information exchange. The |
| 1133 | encoder routine API comes in two flavors: stdio-based and callback-based. |
| 1134 | With the callback-based encoder, the encoding process is very similar |
| 1135 | to the DER one, described in Section sub:Encoding-DER. The |
| 1136 | following example uses the definition of write_stream() from up there. |
| 1137 | |
| 1138 | <P> |
| 1139 | |
| 1140 | <BLOCKQUOTE><PRE> |
| 1141 | /* |
| 1142 | * This procedure generates the XML document |
| 1143 | * by invoking the XER encoder. |
| 1144 | * NOTE: Do not copy this code verbatim! |
| 1145 | * If the stdio output is necessary, |
| 1146 | * use the xer_fprint() procedure instead. |
| 1147 | * See Section sub:Printing-the-target. |
| 1148 | */ |
| 1149 | int |
| 1150 | print_as_XML(FILE *ostream, Rectangle_t *rect) { |
| 1151 | asn_enc_rval_t er; /* Encoder return value */ |
| 1152 | |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 1153 | er = xer_encode(&asn_DEF_Rect, rect, |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1154 | XER_F_BASIC, /* BASIC-XER or CANONICAL-XER */ |
| 1155 | write_stream, ostream); |
| 1156 | |
| 1157 | return (er.encoded == -1) ? -1 : 0; |
| 1158 | } |
| 1159 | </PRE> |
| 1160 | </BLOCKQUOTE> |
| 1161 | Please look into xer_encoder.h for the precise definition of xer_encode() |
| 1162 | and related types. |
| 1163 | |
| 1164 | <P> |
| 1165 | See Section [<A HREF="#sub:Printing-the-target">Printing-the-target</A>] for the example of stdio-based |
| 1166 | XML encoder and other pretty-printing suggestions. |
| 1167 | |
| 1168 | <P> |
| 1169 | |
| 1170 | <H2><A NAME="SECTION03334000000000000000"></A><A NAME="sub:Validating-the-target"></A><BR> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1171 | Validating the target structure |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1172 | </H2> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1173 | |
| 1174 | <P> |
| 1175 | Sometimes the target structure needs to be validated. For example, |
| 1176 | if the structure was created by the application (as opposed to being |
| 1177 | decoded from some external source), some important information required |
| 1178 | by the ASN.1 specification might be missing. On the other hand, the |
| 1179 | successful decoding of the data from some external source does not |
| 1180 | necessarily mean that the data is fully valid either. It might well |
| 1181 | be the case that the specification describes some subtype constraints |
| 1182 | that were not taken into account during decoding, and it would actually |
| 1183 | be useful to perform the last check when the data is ready to be encoded |
| 1184 | or when the data has just been decoded to ensure its validity according |
| 1185 | to some stricter rules. |
| 1186 | |
| 1187 | <P> |
| 1188 | The asn_check_constraints() function checks the type for various |
| 1189 | implicit and explicit constraints. It is recommended to use asn_check_constraints() |
| 1190 | function after each decoding and before each encoding. |
| 1191 | |
| 1192 | <P> |
| 1193 | Please look into constraints.h for the precise definition of asn_check_constraints() |
| 1194 | and related types. |
| 1195 | |
| 1196 | <P> |
| 1197 | |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1198 | <H2><A NAME="SECTION03335000000000000000"></A><A NAME="sub:Printing-the-target"></A><BR> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1199 | Printing the target structure |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1200 | </H2> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1201 | |
| 1202 | <P> |
| 1203 | There are two ways to print the target structure: either invoke the |
| 1204 | print_struct member of the ASN.1 type descriptor, or using the asn_fprint() |
| 1205 | function, which is a simpler wrapper of the former: |
| 1206 | |
| 1207 | <P> |
| 1208 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1209 | <BLOCKQUOTE><PRE> |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 1210 | asn_fprint(stdout, &asn_DEF_Rectangle, rect); |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1211 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1212 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1213 | Please look into constr_TYPE.h for the precise definition of asn_fprint() |
| 1214 | and related types. |
| 1215 | |
| 1216 | <P> |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1217 | Another practical alternative to this custom format printing would |
| 1218 | be to invoke XER encoder. The default BASIC-XER encoder performs reasonable |
| 1219 | formatting for the output to be useful and human readable. To invoke |
| 1220 | the XER decoder in a manner similar to asn_fprint(), use the xer_fprint() |
| 1221 | call: |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1222 | |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1223 | <P> |
| 1224 | |
| 1225 | <BLOCKQUOTE><PRE> |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 1226 | xer_fprint(stdout, &asn_DEF_Rectangle, rect); |
vlm | 763ac41 | 2004-09-26 13:41:45 +0000 | [diff] [blame] | 1227 | </PRE> |
| 1228 | </BLOCKQUOTE> |
| 1229 | See Section sub:Encoding-XER for XML-related details. |
| 1230 | |
| 1231 | <P> |
| 1232 | |
| 1233 | <H2><A NAME="SECTION03336000000000000000"></A><A NAME="sub:Freeing-the-target"></A><BR> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1234 | Freeing the target structure |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1235 | </H2> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1236 | |
| 1237 | <P> |
| 1238 | Freeing the structure is slightly more complex than it may seem to. |
| 1239 | When the ASN.1 structure is freed, all the members of the structure |
| 1240 | and their submembers etc etc are recursively freed too. But it might |
| 1241 | not be feasible to free the structure itself. Consider the following |
| 1242 | case: |
| 1243 | |
| 1244 | <P> |
| 1245 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1246 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1247 | struct my_figure { /* The custom structure */ |
| 1248 | int flags; /* <some custom member> */ |
| 1249 | /* The type is generated by the ASN.1 compiler */ |
| 1250 | <I>Rectangle_t rect;</I> |
| 1251 | /* other members of the structure */ |
| 1252 | }; |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1253 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1254 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1255 | In this example, the application programmer defined a custom structure |
| 1256 | with one ASN.1-derived member (rect). This member is not a reference |
| 1257 | to the Rectangle_t, but an in-place inclusion of the Rectangle_t |
| 1258 | structure. If the freeing is necessary, the usual procedure of freeing |
| 1259 | everything must not be applied to the &rect pointer itself, because |
| 1260 | it does not point to the memory block directly allocated by memory |
| 1261 | allocation routine, but instead lies within such a block allocated |
| 1262 | for my_figure structure. |
| 1263 | |
| 1264 | <P> |
| 1265 | To solve this problem, the free_struct routine has the additional |
| 1266 | argument (besides the intuitive type descriptor and target structure |
| 1267 | pointers), which is the flag specifying whether the outer pointer |
| 1268 | itself must be freed (0, default) or it should be left intact (non-zero |
| 1269 | value). |
| 1270 | |
| 1271 | <P> |
| 1272 | |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1273 | <BLOCKQUOTE><PRE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1274 | /* Rectangle_t is defined within my_figure */ |
| 1275 | struct my_figure *mf = <B>...</B>; |
| 1276 | /* |
| 1277 | * Freeing the Rectangle_td |
| 1278 | * without freeing the mf->rect pointer |
| 1279 | */ |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 1280 | asn_DEF_Rectangle->free_struct( |
| 1281 | &asn_DEF_Rectangle, &mf->rect, <I>1</I> /* !free */); |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1282 | |
| 1283 | /* Rectangle_t is a stand-alone pointer */ |
| 1284 | Rectangle_t *rect = <B>...</B>; |
| 1285 | /* |
| 1286 | * Freeing the Rectangle_t |
| 1287 | * and freeing the rect pointer |
| 1288 | */ |
vlm | 4a168ae | 2004-09-29 13:37:15 +0000 | [diff] [blame] | 1289 | asn_DEF_Rectangle->free_struct( |
| 1290 | &asn_DEF_Rectangle, rect, <I>0</I> /* free the pointer too */); |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1291 | </PRE> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1292 | </BLOCKQUOTE> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1293 | It is safe to invoke the <I>free_struct</I> function with the target |
| 1294 | structure pointer set to 0 (NULL), the function will do nothing. |
| 1295 | |
| 1296 | <P> |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 1297 | |
| 1298 | <H1><A NAME="SECTION04000000000000000000"> |
| 1299 | Examples</A> |
| 1300 | </H1> |
| 1301 | |
| 1302 | <P> |
| 1303 | |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1304 | <H1><A NAME="SECTION04100000000000000000"></A><A NAME="cha:Step-by-step-examples"></A><BR> |
| 1305 | Step by step examples |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 1306 | </H1> |
| 1307 | |
| 1308 | <P> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 1309 | |
| 1310 | <H1><A NAME="SECTION04110000000000000000"> |
| 1311 | A ''Rectangle'' Encoder</A> |
| 1312 | </H1> |
| 1313 | |
| 1314 | <P> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1315 | This example will help you to create a simple BER and XER encoder |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 1316 | of a ''Rectangle'' type used throughout this document. |
| 1317 | |
| 1318 | <P> |
| 1319 | |
| 1320 | <OL> |
| 1321 | <LI>Create a file named <B>rectangle.asn1</B> with the following contents: |
| 1322 | |
| 1323 | <P> |
| 1324 | |
| 1325 | <BLOCKQUOTE><PRE> |
| 1326 | RectangleModule1 DEFINITIONS ::= |
| 1327 | BEGIN |
| 1328 | |
| 1329 | Rectangle ::= SEQUENCE { |
| 1330 | height INTEGER, |
| 1331 | width INTEGER |
| 1332 | } |
| 1333 | |
| 1334 | END |
| 1335 | </PRE> |
| 1336 | </BLOCKQUOTE> |
| 1337 | |
| 1338 | </LI> |
| 1339 | <LI>Compile it into the set of .c and .h files using asn1c compiler [<A |
| 1340 | HREF="asn1c-usage.html#ASN1C">ASN1C</A>]: |
| 1341 | |
| 1342 | <P> |
| 1343 | |
| 1344 | <BLOCKQUOTE><PRE> |
| 1345 | <I>asn1c -fnative-types</I> <B>rectangle.asn1</B> |
| 1346 | </PRE> |
| 1347 | </BLOCKQUOTE> |
| 1348 | |
| 1349 | </LI> |
| 1350 | <LI>Alternatively, use the Online ASN.1 compiler [<A |
| 1351 | HREF="asn1c-usage.html#AONL">AONL</A>] by uploading |
| 1352 | the <B>rectangle.asn1</B> file into the Web form and unpacking the |
| 1353 | produced archive on your computer. |
| 1354 | </LI> |
| 1355 | <LI>By this time, you should have gotten multiple files in the current |
| 1356 | directory, including the <B>Rectangle.c</B> and <B>Rectangle.h</B>. |
| 1357 | </LI> |
| 1358 | <LI>Create a main() routine which creates the Rectangle_t structure in |
| 1359 | memory and encodes it using BER and XER encoding rules. Let's name |
| 1360 | the file <B>main.c</B>: |
| 1361 | |
| 1362 | <P> |
| 1363 | |
| 1364 | <BLOCKQUOTE><PRE> |
| 1365 | <FONT SIZE="-1">#include <stdio.h></FONT> |
| 1366 | <FONT SIZE="-1">#include <sys/types.h></FONT> |
| 1367 | <FONT SIZE="-1">#include <Rectangle.h> /* Rectangle ASN.1 type */</FONT> |
| 1368 | |
| 1369 | <FONT SIZE="-1">/*</FONT> |
| 1370 | <FONT SIZE="-1">* This is a custom function which writes the</FONT> |
| 1371 | <FONT SIZE="-1">* encoded output into some FILE stream.</FONT> |
| 1372 | <FONT SIZE="-1">*/</FONT> |
| 1373 | <FONT SIZE="-1">static int</FONT> |
| 1374 | <FONT SIZE="-1">write_out(const void *buffer, size_t size, void *app_key) {</FONT> |
| 1375 | <FONT SIZE="-1"> FILE *out_fp = app_key;</FONT> |
| 1376 | <FONT SIZE="-1"> size_t wrote;</FONT> |
| 1377 | |
| 1378 | <FONT SIZE="-1"> wrote = fwrite(buffer, 1, size, out_fp);</FONT> |
| 1379 | |
| 1380 | <FONT SIZE="-1"> return (wrote == size) ? 0 : -1;</FONT> |
| 1381 | <FONT SIZE="-1">}</FONT> |
| 1382 | |
vlm | 6c37dac | 2005-01-31 15:06:32 +0000 | [diff] [blame] | 1383 | <FONT SIZE="-1">int main(int ac, char **av) {</FONT> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 1384 | <FONT SIZE="-1"> Rectangle_t *rectangle; /* Type to encode */</FONT> |
| 1385 | <FONT SIZE="-1"> asn_enc_rval_t ec; /* Encoder return value */</FONT> |
| 1386 | |
| 1387 | <FONT SIZE="-1"> /* Allocate the Rectangle_t */</FONT> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1388 | <FONT SIZE="-1"> rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */</FONT> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 1389 | <FONT SIZE="-1"> if(!rectangle) {</FONT> |
| 1390 | <FONT SIZE="-1"> perror(''calloc() failed'');</FONT> |
| 1391 | <FONT SIZE="-1"> exit(71); /* better, EX_OSERR */</FONT> |
| 1392 | <FONT SIZE="-1"> }</FONT> |
| 1393 | |
| 1394 | <FONT SIZE="-1"> /* Initialize the Rectangle members */</FONT> |
| 1395 | <FONT SIZE="-1"> rectangle->height = 42; /* any random value */</FONT> |
| 1396 | <FONT SIZE="-1"> rectangle->width = 23; /* any random value */</FONT> |
| 1397 | <FONT SIZE="-1"> </FONT> |
| 1398 | <FONT SIZE="-1"> /* BER encode the data if filename is given */</FONT> |
| 1399 | <FONT SIZE="-1"> if(ac < 2) {</FONT> |
| 1400 | <FONT SIZE="-1"> fprintf(stderr, ''Specify filename for BER output\n'');</FONT> |
| 1401 | <FONT SIZE="-1"> } else {</FONT> |
| 1402 | <FONT SIZE="-1"> const char *filename = av[1];</FONT> |
| 1403 | <FONT SIZE="-1"> FILE *fp = fopen(filename, ''w''); /* for BER output */</FONT> |
| 1404 | |
| 1405 | <FONT SIZE="-1"> if(!fp) {</FONT> |
| 1406 | <FONT SIZE="-1"> perror(filename);</FONT> |
| 1407 | <FONT SIZE="-1"> exit(71); /* better, EX_OSERR */</FONT> |
| 1408 | <FONT SIZE="-1"> }</FONT> |
| 1409 | <FONT SIZE="-1"> </FONT> |
| 1410 | <FONT SIZE="-1"> /* Encode the Rectangle type as BER (DER) */</FONT> |
| 1411 | <FONT SIZE="-1"> ec = der_encode(&asn_DEF_Rectangle,</FONT> |
vlm | 6c37dac | 2005-01-31 15:06:32 +0000 | [diff] [blame] | 1412 | <FONT SIZE="-1"> rectangle, write_out, fp);</FONT> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 1413 | <FONT SIZE="-1"> fclose(fp);</FONT> |
| 1414 | <FONT SIZE="-1"> if(ec.encoded == -1) {</FONT> |
| 1415 | <FONT SIZE="-1"> fprintf(stderr,</FONT> |
| 1416 | <FONT SIZE="-1"> ''Could not encode Rectangle (at %s)\n'',</FONT> |
| 1417 | <FONT SIZE="-1"> ec.failed_type ? ec.failed_type->name : ''unknown'');</FONT> |
| 1418 | <FONT SIZE="-1"> exit(65); /* better, EX_DATAERR */</FONT> |
| 1419 | <FONT SIZE="-1"> } else {</FONT> |
| 1420 | <FONT SIZE="-1"> fprintf(stderr, ''Created %s with BER encoded Rectangle\n'',</FONT> |
| 1421 | <FONT SIZE="-1"> filename);</FONT> |
| 1422 | <FONT SIZE="-1"> }</FONT> |
| 1423 | <FONT SIZE="-1"> }</FONT> |
| 1424 | |
| 1425 | <FONT SIZE="-1"> /* Also print the constructed Rectangle XER encoded (XML) */</FONT> |
| 1426 | <FONT SIZE="-1"> xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);</FONT> |
| 1427 | |
| 1428 | <FONT SIZE="-1"> return 0; /* Encoding finished successfully */</FONT> |
| 1429 | <FONT SIZE="-1">}</FONT> |
| 1430 | </PRE> |
| 1431 | </BLOCKQUOTE> |
| 1432 | |
| 1433 | </LI> |
| 1434 | <LI>Compile all files together using C compiler (varies by platform): |
| 1435 | |
| 1436 | <P> |
| 1437 | |
| 1438 | <BLOCKQUOTE><PRE> |
| 1439 | <I>cc -I. -o</I> <B><I>rencode</I></B> <I>*.c</I> |
| 1440 | </PRE> |
| 1441 | </BLOCKQUOTE> |
| 1442 | |
| 1443 | </LI> |
| 1444 | <LI>Voila! You have just created the BER and XER encoder of a Rectangle |
| 1445 | type, named <B>rencode</B>! |
| 1446 | </LI> |
| 1447 | </OL> |
| 1448 | |
| 1449 | |
| 1450 | <P> |
| 1451 | |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1452 | <H1><A NAME="SECTION04120000000000000000"></A><A NAME="sec:A-Rectangle-Decoder"></A><BR> |
| 1453 | A ''Rectangle'' Decoder |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 1454 | </H1> |
| 1455 | |
| 1456 | <P> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1457 | This example will help you to create a simple BER decoder of a simple |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 1458 | ''Rectangle'' type used throughout this document. |
| 1459 | |
| 1460 | <P> |
| 1461 | |
| 1462 | <OL> |
| 1463 | <LI>Create a file named <B>rectangle.asn1</B> with the following contents: |
| 1464 | |
| 1465 | <P> |
| 1466 | |
| 1467 | <BLOCKQUOTE><PRE> |
| 1468 | RectangleModule1 DEFINITIONS ::= |
| 1469 | BEGIN |
| 1470 | |
| 1471 | Rectangle ::= SEQUENCE { |
| 1472 | height INTEGER, |
| 1473 | width INTEGER |
| 1474 | } |
| 1475 | |
| 1476 | END |
| 1477 | </PRE> |
| 1478 | </BLOCKQUOTE> |
| 1479 | |
| 1480 | </LI> |
| 1481 | <LI>Compile it into the set of .c and .h files using asn1c compiler [<A |
| 1482 | HREF="asn1c-usage.html#ASN1C">ASN1C</A>]: |
| 1483 | |
| 1484 | <P> |
| 1485 | |
| 1486 | <BLOCKQUOTE><PRE> |
| 1487 | <I>asn1c -fnative-types</I> <B>rectangle.asn1</B> |
| 1488 | </PRE> |
| 1489 | </BLOCKQUOTE> |
| 1490 | |
| 1491 | </LI> |
| 1492 | <LI>Alternatively, use the Online ASN.1 compiler [<A |
| 1493 | HREF="asn1c-usage.html#AONL">AONL</A>] by uploading |
| 1494 | the <B>rectangle.asn1</B> file into the Web form and unpacking the |
| 1495 | produced archive on your computer. |
| 1496 | </LI> |
| 1497 | <LI>By this time, you should have gotten multiple files in the current |
| 1498 | directory, including the <B>Rectangle.c</B> and <B>Rectangle.h</B>. |
| 1499 | </LI> |
| 1500 | <LI>Create a main() routine which takes the binary input file, decodes |
| 1501 | it as it were a BER-encoded Rectangle type, and prints out the text |
| 1502 | (XML) representation of the Rectangle type. Let's name the file <B>main.c</B>: |
| 1503 | |
| 1504 | <P> |
| 1505 | |
| 1506 | <BLOCKQUOTE><PRE> |
| 1507 | <FONT SIZE="-1">#include <stdio.h></FONT> |
| 1508 | <FONT SIZE="-1">#include <sys/types.h></FONT> |
| 1509 | <FONT SIZE="-1">#include <Rectangle.h> /* Rectangle ASN.1 type */</FONT> |
| 1510 | |
| 1511 | <FONT SIZE="-1">int main(int ac, char **av) {</FONT> |
| 1512 | <FONT SIZE="-1"> char buf[1024]; /* Temporary buffer */</FONT> |
| 1513 | <FONT SIZE="-1"> Rectangle_t *rectangle = 0; /* Type to decode */</FONT> |
| 1514 | <FONT SIZE="-1"> asn_dec_rval_t rval; /* Decoder return value */</FONT> |
| 1515 | <FONT SIZE="-1"> FILE *fp; /* Input file handler */</FONT> |
| 1516 | <FONT SIZE="-1"> size_t size; /* Number of bytes read */</FONT> |
| 1517 | <FONT SIZE="-1"> char *filename; /* Input file name */</FONT> |
| 1518 | |
| 1519 | <FONT SIZE="-1"> /* Require a single filename argument */</FONT> |
| 1520 | <FONT SIZE="-1"> if(ac != 2) {</FONT> |
| 1521 | <FONT SIZE="-1"> fprintf(stderr, ''Usage: %s <file.ber>\n'', av[0]);</FONT> |
| 1522 | <FONT SIZE="-1"> exit(64); /* better, EX_USAGE */</FONT> |
| 1523 | <FONT SIZE="-1"> } else {</FONT> |
| 1524 | <FONT SIZE="-1"> filename = av[1];</FONT> |
| 1525 | <FONT SIZE="-1"> }</FONT> |
| 1526 | |
| 1527 | <FONT SIZE="-1"> /* Open input file as read-only binary */</FONT> |
| 1528 | <FONT SIZE="-1"> fp = fopen(filename, ''rb'');</FONT> |
| 1529 | <FONT SIZE="-1"> if(!fp) {</FONT> |
| 1530 | <FONT SIZE="-1"> perror(filename);</FONT> |
| 1531 | <FONT SIZE="-1"> exit(66); /* better, EX_NOINPUT */</FONT> |
| 1532 | <FONT SIZE="-1"> }</FONT> |
| 1533 | <FONT SIZE="-1"> </FONT> |
| 1534 | <FONT SIZE="-1"> /* Read up to the buffer size */</FONT> |
| 1535 | <FONT SIZE="-1"> size = fread(buf, 1, sizeof(buf), fp);</FONT> |
| 1536 | <FONT SIZE="-1"> fclose(fp);</FONT> |
| 1537 | <FONT SIZE="-1"> if(!size) {</FONT> |
| 1538 | <FONT SIZE="-1"> fprintf(stderr, ''%s: Empty or broken\n'', filename);</FONT> |
| 1539 | <FONT SIZE="-1"> exit(65); /* better, EX_DATAERR */</FONT> |
| 1540 | <FONT SIZE="-1"> }</FONT> |
| 1541 | |
| 1542 | <FONT SIZE="-1"> /* Decode the input buffer as Rectangle type */</FONT> |
| 1543 | <FONT SIZE="-1"> rval = ber_decode(0, &asn_DEF_Rectangle,</FONT> |
| 1544 | <FONT SIZE="-1"> (void **)&rectangle, buf, size);</FONT> |
| 1545 | <FONT SIZE="-1"> if(rval.code != RC_OK) {</FONT> |
| 1546 | <FONT SIZE="-1"> fprintf(stderr,</FONT> |
| 1547 | <FONT SIZE="-1"> ''%s: Broken Rectangle encoding at byte %ld\n'',</FONT> |
| 1548 | <FONT SIZE="-1"> filename, (long)rval.consumed);</FONT> |
| 1549 | <FONT SIZE="-1"> exit(65); /* better, EX_DATAERR */</FONT> |
| 1550 | <FONT SIZE="-1"> }</FONT> |
| 1551 | |
| 1552 | <FONT SIZE="-1"> /* Print the decoded Rectangle type as XML */</FONT> |
| 1553 | <FONT SIZE="-1"> xer_fprint(stdout, &asn_DEF_Rectangle, rectangle);</FONT> |
| 1554 | |
| 1555 | <FONT SIZE="-1"> return 0; /* Decoding finished successfully */</FONT> |
| 1556 | <FONT SIZE="-1">}</FONT> |
| 1557 | </PRE> |
| 1558 | </BLOCKQUOTE> |
| 1559 | |
| 1560 | </LI> |
| 1561 | <LI>Compile all files together using C compiler (varies by platform): |
| 1562 | |
| 1563 | <P> |
| 1564 | |
| 1565 | <BLOCKQUOTE><PRE> |
| 1566 | <I>cc -I. -o</I> <B><I>rdecode</I></B> <I>*.c</I> |
| 1567 | </PRE> |
| 1568 | </BLOCKQUOTE> |
| 1569 | |
| 1570 | </LI> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 1571 | <LI>Voila! You have just created the BER decoder of a Rectangle type, |
| 1572 | named <B>rdecode</B>! |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 1573 | </LI> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1574 | </OL> |
| 1575 | |
| 1576 | <P> |
| 1577 | |
| 1578 | <H1><A NAME="SECTION04200000000000000000"> |
| 1579 | Constraint validation examples</A> |
| 1580 | </H1> |
| 1581 | |
| 1582 | <P> |
| 1583 | This chapter shows how to define ASN.1 constraints and use the generated |
| 1584 | validation code. |
| 1585 | |
| 1586 | <P> |
| 1587 | |
| 1588 | <H1><A NAME="SECTION04210000000000000000"> |
| 1589 | Adding constraints into ''Rectangle'' type</A> |
| 1590 | </H1> |
| 1591 | |
| 1592 | <P> |
| 1593 | This example shows how to add basic constraints to the ASN.1 specification |
| 1594 | and how to invoke the constraints validation code in your application. |
| 1595 | |
| 1596 | <P> |
| 1597 | |
| 1598 | <OL> |
| 1599 | <LI>Create a file named <B>rectangle.asn1</B> with the following contents: |
| 1600 | |
| 1601 | <P> |
| 1602 | |
| 1603 | <BLOCKQUOTE><PRE> |
| 1604 | RectangleModuleWithConstraints DEFINITIONS ::= |
| 1605 | BEGIN |
| 1606 | |
| 1607 | Rectangle ::= SEQUENCE { |
| 1608 | height INTEGER (0..100), -- Value range constraint |
| 1609 | width INTEGER (0..MAX) -- Makes width non-negative |
| 1610 | } |
| 1611 | |
| 1612 | END |
| 1613 | </PRE> |
| 1614 | </BLOCKQUOTE> |
| 1615 | |
| 1616 | </LI> |
| 1617 | <LI>Compile the file according to procedures shown in the previous chapter. |
| 1618 | </LI> |
| 1619 | <LI>Modify the Rectangle type processing routine (you can start with the |
| 1620 | main() routine shown in the Section sec:A-Rectangle-Decoder) |
| 1621 | by placing the following snippet of code <I>before</I> encoding and/or |
| 1622 | <I>after</I> decoding the Rectangle type<A NAME="tex2html10" |
| 1623 | HREF="#foot874"><SUP>6.1</SUP></A>: |
| 1624 | |
| 1625 | <P> |
| 1626 | |
| 1627 | <BLOCKQUOTE><PRE> |
| 1628 | <FONT SIZE="-1">int ret; /* Return value */</FONT> |
| 1629 | <FONT SIZE="-1">char errbuf[128]; /* Buffer for error message */</FONT> |
| 1630 | <FONT SIZE="-1">size_t errlen = sizeof(errbuf); /* Size of the buffer */</FONT> |
| 1631 | <FONT SIZE="-1"> </FONT> |
| 1632 | <FONT SIZE="-1">/* ... here may go Rectangle decoding code ... */</FONT> |
| 1633 | |
| 1634 | <FONT SIZE="-1">ret = asn_check_constraints(asn_DEF_Rectangle,</FONT> |
| 1635 | <FONT SIZE="-1"> rectangle, errbuf, &errlen);</FONT> |
| 1636 | <FONT SIZE="-1">/* assert(errlen < sizeof(errbuf)); // you may rely on that */</FONT> |
| 1637 | <FONT SIZE="-1">if(ret) {</FONT> |
| 1638 | <FONT SIZE="-1"> fprintf(stderr, ''Constraint validation failed: %s\n'',</FONT> |
| 1639 | <FONT SIZE="-1"> errbuf /* errbuf is properly nul-terminated */</FONT> |
| 1640 | <FONT SIZE="-1"> );</FONT> |
| 1641 | <FONT SIZE="-1"> /* exit(...); // Replace with appropriate action */</FONT> |
| 1642 | <FONT SIZE="-1">}</FONT> |
| 1643 | |
| 1644 | <FONT SIZE="-1">/* ... here may go Rectangle encoding code ... */</FONT> |
| 1645 | </PRE> |
| 1646 | </BLOCKQUOTE> |
| 1647 | |
| 1648 | </LI> |
| 1649 | <LI>Compile the resulting C code as shown in the previous chapters. |
| 1650 | </LI> |
| 1651 | <LI>Done. |
| 1652 | </LI> |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 1653 | </OL> |
| 1654 | <H2><A NAME="SECTION05000000000000000000"> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1655 | Bibliography</A> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1656 | </H2><DL COMPACT><DD><P></P><DT><A NAME="ASN1C">ASN1C</A> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1657 | <DD>The Open Source ASN.1 Compiler. <A HREF=http://lionet.info/>http://lionet.info/</A>asn1c |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 1658 | <P></P><DT><A NAME="AONL">AONL</A> |
| 1659 | <DD>Online ASN.1 Compiler. <A HREF=http://lionet.info/asn1c/>http://lionet.info/asn1c/</A>asn1c.cgi |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1660 | <P></P><DT><A NAME="Dub00">Dub00</A> |
vlm | 2914a80 | 2005-01-17 11:09:32 +0000 | [diff] [blame] | 1661 | <DD>Olivier Dubuisson -- <I>ASN.1 Communication between heterogeneous |
| 1662 | systems</I> -- Morgan Kaufmann Publishers, 2000. <A HREF=http://asn1.elibel.tm.fr/en/book/>http://asn1.elibel.tm.fr/en/book/</A>. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1663 | ISBN:0-12-6333361-0. |
| 1664 | <P></P><DT><A NAME="ITU-T_ASN.1">ITU-T/ASN.1</A> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1665 | <DD>ITU-T Study Group 17 - Languages for Telecommunication Systems <A HREF=http://www.itu.int/ITU-T/studygroups/com17/languages/>http://www.itu.int/ITU-T/studygroups/com17/languages/</A> |
| 1666 | </DL> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1667 | |
| 1668 | <P> |
| 1669 | <BR><HR><H4>Footnotes</H4> |
| 1670 | <DL> |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 1671 | <DT><A NAME="foot159">... supported</A><A |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1672 | HREF="asn1c-usage.html#tex2html1"><SUP>2.1</SUP></A></DT> |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1673 | <DD>C++ is ''supported'' too, as long as an class-based approach is |
| 1674 | not a definitive factor. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1675 | |
| 1676 | </DD> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1677 | <DT><A NAME="foot801">... this</A><A |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1678 | HREF="asn1c-usage.html#tex2html2"><SUP>2.2</SUP></A></DT> |
vlm | 1111525 | 2004-09-17 08:35:02 +0000 | [diff] [blame] | 1679 | <DD><I>-fnative-types</I> compiler option is used to produce basic C <I>int</I> |
| 1680 | types instead of infinite width INTEGER_t structures. See <A HREF=#Table1>Table 1</A>. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1681 | |
| 1682 | </DD> |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 1683 | <DT><A NAME="foot166">... binary</A><A |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1684 | HREF="asn1c-usage.html#tex2html3"><SUP>2.3</SUP></A></DT> |
| 1685 | <DD>BER, CER and DER encodings are binary. However, the XER encoding is |
| 1686 | text (XML) based. |
| 1687 | |
| 1688 | </DD> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1689 | <DT><A NAME="foot802">...asn1c</A><A |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1690 | HREF="asn1c-usage.html#tex2html4"><SUP>3.1</SUP></A></DT> |
| 1691 | <DD>The 1 symbol in asn<B>1</B>c is a digit, not an ''ell'' letter. |
| 1692 | |
| 1693 | </DD> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1694 | <DT><A NAME="foot803">... specification</A><A |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1695 | HREF="asn1c-usage.html#tex2html5"><SUP>3.2</SUP></A></DT> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1696 | <DD>This is probably <B>not</B> what you want to try out right now - |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1697 | read through the rest of this chapter to find out about <B>-P</B> |
| 1698 | and <B>-R</B> options. |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1699 | |
| 1700 | </DD> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1701 | <DT><A NAME="foot809">...that simple</A><A |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1702 | HREF="asn1c-usage.html#tex2html7"><SUP>4.1</SUP></A></DT> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1703 | <DD>Provided that you've also created a .c file with the <I>int main()</I> |
| 1704 | routine. |
| 1705 | |
| 1706 | </DD> |
vlm | c55095f | 2004-09-30 22:45:58 +0000 | [diff] [blame] | 1707 | <DT><A NAME="foot253">...restartable</A><A |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1708 | HREF="asn1c-usage.html#tex2html8"><SUP>4.2</SUP></A></DT> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1709 | <DD>Restartable means that if the decoder encounters the end of the buffer, |
| 1710 | it will fail, but may later be invoked again with the rest of the |
| 1711 | buffer to continue decoding. |
| 1712 | |
| 1713 | </DD> |
vlm | 37731e9 | 2005-01-31 10:18:53 +0000 | [diff] [blame] | 1714 | <DT><A NAME="foot322">... encoding</A><A |
vlm | fcec8e5 | 2004-08-23 15:12:04 +0000 | [diff] [blame] | 1715 | HREF="asn1c-usage.html#tex2html9"><SUP>4.3</SUP></A></DT> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1716 | <DD>It is actually faster too: the encoder might skip over some computations |
| 1717 | which aren't important for the size determination. |
| 1718 | |
| 1719 | </DD> |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1720 | <DT><A NAME="foot874">... type</A><A |
| 1721 | HREF="asn1c-usage.html#tex2html10"><SUP>6.1</SUP></A></DT> |
| 1722 | <DD>Placing the constraint checking code <I>before</I> encoding helps |
| 1723 | to make sure you know the data is correct and within constraints before |
| 1724 | sharing the data with anyone else. |
| 1725 | |
| 1726 | <P> |
| 1727 | Placing the constraint checking code <I>after</I> decoding, but before |
| 1728 | any further action depending on the decoded data, helps to make sure |
| 1729 | the application got the valid contents before making use of it. |
| 1730 | |
| 1731 | </DD> |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1732 | </DL><BR><HR> |
| 1733 | <ADDRESS> |
| 1734 | Lev Walkin |
vlm | c7737f6 | 2005-02-02 09:53:46 +0000 | [diff] [blame^] | 1735 | 2005-02-02 |
vlm | 9ca1504 | 2004-08-07 06:03:15 +0000 | [diff] [blame] | 1736 | </ADDRESS> |
| 1737 | </BODY> |
| 1738 | </HTML> |