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