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