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