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