Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 1 | \documentclass[english,oneside,12pt]{book} |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 2 | \usepackage[no-math]{fontspec} |
| 3 | \usepackage{MnSymbol} |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 4 | \usepackage{xunicode} |
| 5 | \usepackage{xltxtra} |
| 6 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 7 | \usepackage[hmargin={1in,1in},vmargin={1.5in,1.5in}]{geometry} |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 8 | |
| 9 | \defaultfontfeatures{Mapping=tex-text} |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 10 | \setmainfont{PT Sans} |
| 11 | \setsansfont{PT Sans} |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 12 | \setmonofont{Consolas} |
| 13 | |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 14 | \usepackage{fancyhdr} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 15 | \usepackage{longtable} |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 16 | \usepackage{booktabs} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 17 | \usepackage{varioref} |
| 18 | \usepackage{url} |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 19 | \usepackage{xcolor} |
| 20 | \usepackage{listings} |
| 21 | \usepackage{setspace} |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 22 | \usepackage{unicode-math} |
| 23 | \usepackage{perpage} |
| 24 | \MakePerPage{footnote} |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 25 | |
| 26 | \setstretch{1.1} |
| 27 | |
| 28 | % Courier 10 Pitch |
| 29 | \def\courierFont{Courier10 BT WGL4} |
| 30 | %\def\courierFont{Consolas} |
| 31 | \setmonofont[Scale=1.05]{\courierFont} |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 32 | \setmathfont[Scale=1.05]{Cambria Math} |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 33 | |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 34 | |
| 35 | \makeatletter |
| 36 | |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 37 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands. |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 38 | \lstloadlanguages{C,bash} |
| 39 | \newfontfamily\listingfont[Scale=1.05]{\courierFont} |
| 40 | \newfontfamily\inlinelistingfont[Scale=1.05]{\courierFont} |
| 41 | \definecolor{clrlcomment}{gray}{0.3} |
| 42 | \definecolor{clrlkeyword}{rgb}{0.588,0.145,0.18} |
| 43 | \newcommand{\listingkeyword}[1]{\color{clrlkeyword}{#1}} |
| 44 | \newcommand{\listingstring}[1]{\color{clrlcomment}{#1}} |
| 45 | \newcommand{\listingcomment}[1]{\color{clrlcomment}{#1}} |
| 46 | \lstset{tabsize=4, |
| 47 | showstringspaces=false, |
| 48 | showtabs=false, |
| 49 | showspaces=false, |
| 50 | keywordstyle=\listingkeyword, |
| 51 | stringstyle=\listingstring, |
| 52 | commentstyle=\listingcomment, |
| 53 | xleftmargin=\parindent, |
| 54 | columns=fixed, |
| 55 | escapechar=\%, |
| 56 | texcl |
| 57 | } |
| 58 | \lstdefinestyle{listingStyle}{ |
| 59 | basicstyle=\small\listingfont, |
| 60 | stringstyle=\listingstring, |
| 61 | breaklines=true, |
| 62 | breakatwhitespace=true, |
| 63 | flexiblecolumns=false |
| 64 | } |
| 65 | \lstdefinelanguage{asn1}{ |
| 66 | morekeywords={DEFINITIONS,BEGIN,END,SEQUENCE,SET,OF,CHOICE,OPTIONAL}, |
| 67 | morecomment=[l]{--}, |
| 68 | morecomment=[n]{/*}{*/} |
| 69 | } |
| 70 | |
| 71 | \lstnewenvironment{codesample}[1][]{\lstset{style=listingStyle,language=C,#1}}{} |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 72 | \lstnewenvironment{bash}[1][]{\lstset{style=listingStyle,language=bash,#1}}{} |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 73 | \lstnewenvironment{asn}[1][]{\lstset{style=listingStyle,language=asn1,#1}}{} |
| 74 | \def\code{lstinline} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 75 | |
| 76 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands. |
| 77 | \usepackage{extramarks} |
| 78 | \lhead{\firstxmark} |
| 79 | \rfoot{\lastxmark} |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 80 | \definecolor{clrlink}{rgb}{0,0.4,0} |
| 81 | \definecolor{clrurl}{rgb}{0,0,.6} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 82 | \usepackage[colorlinks=true, |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 83 | linkcolor={clrlink}, |
| 84 | citecolor={clrlink}, |
| 85 | urlcolor={clrurl}, |
| 86 | pdfauthor={Lev Walkin}, |
| 87 | pdftitle={Using the Open Source ASN.1 Compiler}, |
| 88 | pdfkeywords={ASN.1,asn1c,compiler}, |
| 89 | bookmarksopen,bookmarksopenlevel=1, |
| 90 | pdffitwindow, |
| 91 | xetex |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 92 | ]{hyperref} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 93 | |
| 94 | \makeatother |
| 95 | |
| 96 | \usepackage{babel} |
| 97 | |
| 98 | \begin{document} |
| 99 | |
| 100 | \title{Using the Open Source ASN.1 Compiler} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 101 | \author{Lev Walkin <\href{mailto:vlm@lionet.info?Subject=asn1c}{vlm@lionet.info}>} |
| 102 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 103 | \pagestyle{fancy} |
| 104 | \fancyhead[L]{\leftmark} |
| 105 | \fancyhead[R]{\href{http://lionet.info/asn1c}{asn1c-0.9.25}} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 106 | \maketitle |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 107 | |
| 108 | \tableofcontents{} |
| 109 | |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 110 | \part{Using the ASN.1 Compiler} |
| 111 | |
| 112 | |
| 113 | \chapter{Introduction to the ASN.1 Compiler} |
| 114 | |
| 115 | The purpose of the ASN.1 compiler is to convert the specifications |
| 116 | in ASN.1 notation into some other language. At this moment, only C |
| 117 | and C++ target languages are supported, the latter is in upward compatibility |
| 118 | mode. |
| 119 | |
| 120 | The compiler reads the specification and emits a series of target |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 121 | language structures (C structs, unions, enums) describing the corresponding |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 122 | ASN.1 types. The compiler also creates the code which allows automatic |
| 123 | serialization and deserialization of these structures using several |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 124 | standardized encoding rules (BER, DER, XER, PER). |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 125 | |
| 126 | For example, suppose the following ASN.1 module is given% |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 127 | \footnote{Part \ref{par:ASN.1-Basics} provides a quick reference |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 128 | on the ASN.1 notation.}: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 129 | \begin{asn} |
| 130 | RectangleTest DEFINITIONS ::= BEGIN |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 131 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 132 | Rectangle ::= SEQUENCE { |
| 133 | height INTEGER, -- Height of the rectangle |
| 134 | width INTEGER -- Width of the rectangle |
| 135 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 136 | |
| 137 | END |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 138 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 139 | The compiler would read this ASN.1 definition and produce the following |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 140 | C type: |
| 141 | \begin{codesample} |
| 142 | typedef struct Rectangle_s { |
| 143 | long height; |
| 144 | long width; |
| 145 | } Rectangle_t; |
| 146 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 147 | It would also create the code for converting this structure into platform-independent |
| 148 | wire representation (a serializer API) and the decoder of such wire |
| 149 | representation back into local, machine-specific type (a deserializer |
| 150 | API). |
| 151 | |
| 152 | |
| 153 | \section{Quick start with asn1c} |
| 154 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 155 | After building and installing the compiler, the \emph{asn1c} |
| 156 | command may be used to compile the ASN.1 modules% |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 157 | \footnote{This is probably \textbf{not} what you want to try out right now. Read through the rest of this chapter and check the Section~\ref{sec:Command-line-options} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 158 | to find out about \textbf{-P} and \textbf{-R} options.% |
| 159 | }: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 160 | \begin{bash} |
| 161 | asn1c %\emph{<modules.asn1>}% |
| 162 | \end{bash} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 163 | If several ASN.1 modules contain interdependencies, all of the files |
| 164 | must be specified altogether: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 165 | \begin{bash} |
| 166 | asn1c %\emph{<module1.asn1> <module2.asn1> ...}% |
| 167 | \end{bash} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 168 | The compiler \textbf{-E} and \textbf{-EF} options are used for testing |
| 169 | the parser and the semantic fixer, respectively. These options will |
| 170 | instruct the compiler to dump out the parsed (and fixed, if \textbf{-F} |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 171 | is involved) ASN.1 specification as it was understood |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 172 | by the compiler. It might be useful to check whether a particular |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 173 | syntactic construct is properly supported by the compiler. |
| 174 | \begin{bash} |
| 175 | asn1c %\textbf{-EF} \emph{<module-to-test.asn1>}% |
| 176 | \end{bash} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 177 | The \textbf{-P} option is used to dump the compiled output on the |
| 178 | screen instead of creating a bunch of .c and .h files on disk in the |
| 179 | current directory. You would probably want to start with \textbf{-P} |
| 180 | option instead of creating a mess in your current directory. Another |
| 181 | option, \textbf{-R}, asks compiler to only generate the files which |
| 182 | need to be generated, and supress linking in the numerous support |
| 183 | files. |
| 184 | |
| 185 | Print the compiled output instead of creating multiple source files: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 186 | \begin{bash} |
| 187 | asn1c %\textbf{-P} \emph{<module-to-compile-and-print.asn1>}% |
| 188 | \end{bash} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 189 | |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 190 | \clearpage{} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 191 | \section{Recognizing compiler output} |
| 192 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 193 | The asn1c compiler produces a number of files: |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 194 | \begin{itemize} |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 195 | \item A set of .c and .h files for each type defined |
| 196 | in the ASN.1 specification. These files will be named similarly to |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 197 | the ASN.1 types (\emph{Rectangle.c} and \emph{Rectangle.h} for the |
| 198 | RectangleTest ASN.1 module defined in the beginning of this document). |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 199 | \item A set of helper .c and .h files which contain the generic encoders, |
| 200 | decoders and other useful routines. There will be quite a few of them, some |
| 201 | of them are not even always necessary, but the overall amount of code |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 202 | after compilation will be rather small anyway. |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 203 | \item A \emph{converter-sample.c} file containing the \emph{int main()} function with a fully functioning decoder. It can convert a given PDU between BER, XER and possibly PER (if -gen-PER option to asn1c was in effect). At some point you will want to replace this file with your own file containing the \emph{int main()} function. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 204 | \item A \emph{Makefile.am.sample} file mentioning all the files created |
| 205 | at the earlier steps. This file is suitable for either automake suite |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 206 | or the plain `make` utility. Just rename it into \emph{Makefile}. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 207 | \end{itemize} |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 208 | It is possible to compile everything with just a couple of instructions: |
| 209 | \begin{bash} |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 210 | asn1c -pdu=%\emph{Rectangle}% *.asn1 |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 211 | make -f Makefile.am.sample # If you use `make` |
| 212 | \end{bash} |
| 213 | or |
| 214 | \begin{bash} |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 215 | asn1c *.asn1 |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 216 | cc -I. -DPDU=%\emph{Rectangle}% -o rectangle.exe *.c # ... or like this |
| 217 | \end{bash} |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 218 | Refer to the Chapter \ref{cha:Step-by-step-examples} for a sample |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 219 | \emph{int main()} function if you want some custom logic and not satisfied |
| 220 | with the supplied \emph{converter-sample.c}. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 221 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 222 | \clearpage{} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 223 | \section{\label{sec:Command-line-options}Command line options} |
| 224 | |
| 225 | The following table summarizes the asn1c command line options. |
| 226 | |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 227 | \renewcommand{\arraystretch}{1.33} |
| 228 | \begin{longtable}{lp{4in}} |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 229 | \textbf{Overall Options} & \textbf{Description}\\ |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 230 | \midrule |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 231 | {-E} & {\small Stop after the parsing stage and print the reconstructed ASN.1 |
| 232 | specification code to the standard output.}\\ |
| 233 | {-F} & {\small Used together with -E, instructs the compiler to stop after |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 234 | the ASN.1 syntax tree fixing stage and dump the reconstructed ASN.1 |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 235 | specification to the standard output.}\\ |
| 236 | {-P} & {\small Dump the compiled output to the standard output instead of |
| 237 | creating the target language files on disk.}\\ |
| 238 | {-R} & {\small Restrict the compiler to generate only the ASN.1 tables, omitting the usual support code.}\\ |
| 239 | {-S}~\emph{<directory>} & {\small Use the specified directory with ASN.1 skeleton files.}\\ |
| 240 | {-X} & {\small Generate the XML DTD for the specified ASN.1 modules.}\\\\ |
| 241 | \textbf{Warning Options} & \textbf{Description}\\ |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 242 | \midrule |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 243 | {-Werror} & {\small Treat warnings as errors; abort if any warning is produced.}\\ |
| 244 | {-Wdebug-lexer} & {\small Enable lexer debugging during the ASN.1 parsing stage.}\\ |
| 245 | {-Wdebug-fixer} & {\small Enable ASN.1 syntax tree fixer debugging during the |
| 246 | fixing stage.}\\ |
| 247 | {-Wdebug-compiler} & {\small Enable debugging during the actual compile time.}\\ \\ |
| 248 | \textbf{Language Options} & \textbf{Description}\\ |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 249 | \midrule |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 250 | {-fbless-SIZE} & {\small Allow SIZE() constraint for INTEGER, ENUMERATED, and other |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 251 | types for which this constraint is normally prohibited by the standard. |
| 252 | This is a violation of an ASN.1 standard and compiler may fail to |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 253 | produce the meaningful code.}\\ |
| 254 | {-fcompound-names} & {\small Use complex names for C structures. Using complex names prevents |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 255 | name clashes in case the module reuses the same identifiers in multiple |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 256 | contexts.}\\ |
| 257 | {-findirect-choice} & {\small When generating code for a CHOICE type, compile the CHOICE |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 258 | members as indirect pointers instead of declaring them inline. Consider |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 259 | using this option together with \textbf{-fno-include-deps} |
| 260 | to prevent circular references. }\\ |
| 261 | {-fknown-extern-type=}\emph{<name>} & {\small Pretend the specified type is known. The compiler will assume |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 262 | the target language source files for the given type have been provided |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 263 | manually. }\\ |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 264 | {-fno-constraints} & {\small Do not generate ASN.1 subtype constraint checking code. This |
| 265 | may produce a shorter executable.}\\ |
| 266 | {-fno-include-deps} & {\small Do not generate courtesy \#include lines for non-critical |
| 267 | dependencies.}\\ |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 268 | {-funnamed-unions} & {\small Enable unnamed unions in the definitions of target language's structures.}\\ |
| 269 | {-fwide-types} & {\small Use the wide integer types (INTEGER\_t, REAL\_t) instead of machine's native data types (long, double). }\\\\ |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 270 | \textbf{Codecs Generation Options} & \textbf{Description}\\ |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 271 | \midrule |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 272 | {-gen-PER} & {\small Generate Packed Encoding Rules (PER) support code.}\\ |
| 273 | {-pdu=}\emph{auto} & {\small Generate PDU tables by discovering Protocol Data Units automatically. Also accepts a special keyword \emph{all} or a particular type to be used as a PDU.}\\ \\ |
| 274 | \textbf{Output Options} & \textbf{Description}\\ |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 275 | \midrule |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 276 | {-print-constraints} & {\small When -EF are also specified, this option forces the compiler |
| 277 | to explain its internal understanding of subtype constraints.}\\ |
| 278 | {-print-lines} & {\small Generate ``-{}- \#line'' comments |
| 279 | in -E output.}\\ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 280 | \end{longtable} |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 281 | \renewcommand{\arraystretch}{1} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 282 | |
| 283 | |
| 284 | \chapter{Using the ASN.1 Compiler} |
| 285 | |
| 286 | |
| 287 | \section[Invoking the helper code]{Invoking the ASN.1 helper code} |
| 288 | |
| 289 | First of all, you should include one or more header files into your |
| 290 | application. Typically, it is enough to include the header file of |
| 291 | the main PDU type. For our Rectangle module, including the Rectangle.h |
| 292 | file is sufficient: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 293 | \begin{codesample} |
| 294 | #include <Rectangle.h> |
| 295 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 296 | The header files defines the C structure corresponding to the ASN.1 |
| 297 | definition of a rectangle and the declaration of the ASN.1 type descriptor, |
| 298 | which is used as an argument to most of the functions provided by |
| 299 | the ASN.1 module. For example, here is the code which frees the Rectangle\_t |
| 300 | structure: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 301 | \begin{codesample} |
| 302 | Rectangle_t *rect = ...; |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 303 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 304 | asn_DEF_Rectangle.free_struct(&asn_DEF_Rectangle, rect, 0); |
| 305 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 306 | This code defines a \emph{rect} pointer which points to the Rectangle\_t |
| 307 | structure which needs to be freed. The second line invokes the generic |
| 308 | \emph{free\_struct()} routine created specifically for this Rectangle\_t |
| 309 | structure. The \emph{asn\_DEF\_Rectangle} is the type descriptor, |
| 310 | which holds a collection of routines to deal with the Rectangle\_t |
| 311 | structure. |
| 312 | |
| 313 | The following member functions of the asn\_DEF\_Rectangle type descriptor |
| 314 | are of interest: |
| 315 | \begin{description} |
| 316 | \item [{ber\_decoder}] This is the generic \emph{restartable}% |
| 317 | \footnote{Restartable means that if the decoder encounters the end of the buffer, |
| 318 | it will fail, but may later be invoked again with the rest of the |
| 319 | buffer to continue decoding.% |
| 320 | } BER decoder (Basic Encoding Rules). This decoder would create and/or |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 321 | fill the target structure for you. See Section~\ref{sub:Decoding-BER}. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 322 | \item [{der\_encoder}] This is the generic DER encoder (Distinguished Encoding |
| 323 | Rules). This encoder will take the target structure and encode it |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 324 | into a series of bytes. See Section~\ref{sub:Encoding-DER}. NOTE: |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 325 | DER encoding is a subset of BER. Any BER decoder should be able to |
| 326 | handle DER input. |
| 327 | \item [{xer\_decoder}] This is the generic XER decoder. It takes both BASIC-XER |
| 328 | or CANONICAL-XER encodings and deserializes the data into a local, |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 329 | machine-dependent representation. See Section~\ref{sub:Decoding-XER}. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 330 | \item [{xer\_encoder}] This is the XER encoder (XML Encoding Rules). This |
| 331 | encoder will take the target structure and represent it as an XML |
| 332 | (text) document using either BASIC-XER or CANONICAL-XER encoding rules. |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 333 | See Section~\ref{sub:Encoding-XER}. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 334 | \item [{uper\_decoder}] This is the Unaligned PER decoder. |
| 335 | \item [{uper\_encoder}] This is the Unaligned Basic PER encoder. This encoder |
| 336 | will take the target structure and encode it into a series of bytes. |
| 337 | \item [{check\_constraints}] Check that the contents of the target structure |
| 338 | are semantically valid and constrained to appropriate implicit or |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 339 | explicit subtype constraints. See Section~\ref{sub:Validating-the-target}. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 340 | \item [{print\_struct}] This function convert the contents of the passed |
| 341 | target structure into human readable form. This form is not formal |
| 342 | and cannot be converted back into the structure, but it may turn out |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 343 | to be useful for debugging or quick-n-dirty printing. See Section~\ref{sub:Printing-the-target}. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 344 | \item [{free\_struct}] This is a generic disposal which frees the target |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 345 | structure. See Section~\ref{sub:Freeing-the-target}. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 346 | \end{description} |
| 347 | Each of the above function takes the type descriptor (\emph{asn\_DEF\_\ldots{}}) |
| 348 | and the target structure (\emph{rect}, in the above example). |
| 349 | |
| 350 | |
| 351 | \subsection{\label{sub:Decoding-BER}Decoding BER} |
| 352 | |
| 353 | The Basic Encoding Rules describe the most widely used (by the ASN.1 |
| 354 | community) way to encode and decode a given structure in a machine-independent |
| 355 | way. Several other encoding rules (CER, DER) define a more restrictive |
| 356 | versions of BER, so the generic BER parser is also capable of decoding |
| 357 | the data encoded by CER and DER encoders. The opposite is not true. |
| 358 | |
| 359 | \emph{The ASN.1 compiler provides the generic BER decoder which is |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 360 | capable of decoding BER, CER and DER encoded data.} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 361 | |
| 362 | The decoder is restartable (stream-oriented), which means that in |
| 363 | case the buffer has less data than it is expected, the decoder will |
| 364 | process whatever there is available and ask for more data to be provided. |
| 365 | Please note that the decoder may actually process less data than it |
| 366 | was given in the buffer, which means that you must be able to make |
| 367 | the next buffer contain the unprocessed part of the previous buffer. |
| 368 | |
| 369 | Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes. |
| 370 | \begin{itemize} |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 371 | \item You can concatenate these buffers and feed the BER decoder with 300 |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 372 | bytes of data, or |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 373 | \item You can feed it the first buffer of 100 bytes of data, realize that |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 374 | the ber\_decoder consumed only 95 bytes from it and later feed the |
| 375 | decoder with 205 bytes buffer which consists of 5 unprocessed bytes |
| 376 | from the first buffer and the additional 200 bytes from the second |
| 377 | buffer. |
| 378 | \end{itemize} |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 379 | This is not as convenient as it could be (the BER encoder could |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 380 | consume the whole 100 bytes and keep these 5 bytes in some temporary |
| 381 | storage), but in case of existing stream based processing it might |
| 382 | actually fit well into existing algorithm. Suggestions are welcome. |
| 383 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 384 | Here is the simplest example of BER decoding: |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 385 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 386 | \begin{codesample} |
| 387 | Rectangle_t * |
| 388 | simple_deserializer(const void *buffer, size_t buf_size) { |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 389 | asn_dec_rval_t rval; |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 390 | Rectangle_t *%$\underbracket{\textrm{\listingfont rect = 0}}$%; /* %\textbf{\color{red}Note this 0\footnote{Forgetting to properly initialize the pointer to a destination structure is a major source of support requests.}!}% */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 391 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 392 | rval = %\textbf{asn\_DEF\_Rectangle.ber\_decoder}%(0, |
| 393 | &asn_DEF_Rectangle, |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 394 | (void **) %$\underbracket{\textrm{\listingfont \&rect}}$%, /* Decoder %\emph{moves}% the pointer */ |
| 395 | buffer, buf_size, 0); |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 396 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 397 | if(rval%\textbf{.code}% == RC_OK) { |
| 398 | return rect; /* Decoding succeeded */ |
| 399 | } else { |
| 400 | /* Free partially decoded rect */ |
| 401 | asn_DEF_Rectangle.free_struct(&asn_DEF_Rectangle, rect, 0); |
| 402 | return 0; |
| 403 | } |
| 404 | } |
| 405 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 406 | The code above defines a function, \emph{simple\_deserializer}, which |
| 407 | takes a buffer and its length and is expected to return a pointer |
| 408 | to the Rectangle\_t structure. Inside, it tries to convert the bytes |
| 409 | passed into the target structure (rect) using the BER decoder and |
| 410 | returns the rect pointer afterwards. If the structure cannot be deserialized, |
| 411 | it frees the memory which might be left allocated by the unfinished |
| 412 | \emph{ber\_decoder} routine and returns 0 (no data). (This \textbf{freeing |
| 413 | is necessary} because the ber\_decoder is a restartable procedure, |
| 414 | and may fail just because there is more data needs to be provided |
| 415 | before decoding could be finalized). The code above obviously does |
| 416 | not take into account the way the \emph{ber\_decoder()} failed, so |
| 417 | the freeing is necessary because the part of the buffer may already |
| 418 | be decoded into the structure by the time something goes wrong. |
| 419 | |
| 420 | A little less wordy would be to invoke a globally available \emph{ber\_decode()} |
| 421 | function instead of dereferencing the asn\_DEF\_Rectangle type descriptor: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 422 | \begin{codesample} |
| 423 | rval = ber_decode(0, &asn_DEF_Rectangle, (void **)&rect, buffer, buf_size); |
| 424 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 425 | Note that the initial (asn\_DEF\_Rectangle.ber\_decoder) reference |
| 426 | is gone, and also the last argument (0) is no longer necessary. |
| 427 | |
| 428 | These two ways of BER decoder invocations are fully equivalent. |
| 429 | |
| 430 | The BER de\emph{coder} may fail because of (\emph{the following RC\_\ldots{} |
| 431 | codes are defined in ber\_decoder.h}): |
| 432 | \begin{itemize} |
| 433 | \item RC\_WMORE: There is more data expected than it is provided (stream |
| 434 | mode continuation feature); |
| 435 | \item RC\_FAIL: General failure to decode the buffer; |
| 436 | \item \ldots{} other codes may be defined as well. |
| 437 | \end{itemize} |
| 438 | Together with the return code (.code) the asn\_dec\_rval\_t type contains |
| 439 | the number of bytes which is consumed from the buffer. In the previous |
| 440 | hypothetical example of two buffers (of 100 and 200 bytes), the first |
| 441 | call to ber\_decode() would return with .code = RC\_WMORE and .consumed |
| 442 | = 95. The .consumed field of the BER decoder return value is \textbf{always} |
| 443 | valid, even if the decoder succeeds or fails with any other return |
| 444 | code. |
| 445 | |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 446 | Look into ber\_decoder.h for the precise definition of ber\_decode() |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 447 | and related types. |
| 448 | |
| 449 | |
| 450 | \subsection{\label{sub:Encoding-DER}Encoding DER} |
| 451 | |
| 452 | The Distinguished Encoding Rules is the \emph{canonical} variant of |
| 453 | BER encoding rules. The DER is best suited to encode the structures |
| 454 | where all the lengths are known beforehand. This is probably exactly |
| 455 | how you want to encode: either after a BER decoding or after a manual |
| 456 | fill-up, the target structure contains the data which size is implicitly |
| 457 | known before encoding. Among other uses, the DER encoding is used |
| 458 | to encode X.509 certificates. |
| 459 | |
| 460 | As with BER decoder, the DER encoder may be invoked either directly |
| 461 | from the ASN.1 type descriptor (asn\_DEF\_Rectangle) or from the stand-alone |
| 462 | function, which is somewhat simpler: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 463 | \begin{codesample} |
| 464 | /* |
| 465 | * This is the serializer itself. |
| 466 | * It supplies the DER encoder with the |
| 467 | * pointer to the custom output function. |
| 468 | */ |
| 469 | ssize_t |
| 470 | simple_serializer(FILE *ostream, Rectangle_t *rect) { |
| 471 | asn_enc_rval_t er; /* Encoder return value */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 472 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 473 | er = der_encode(&asn_DEF_Rect, rect, write_stream, ostream); |
| 474 | if(er%\textbf{.encoded}% == -1) { |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 475 | fprintf(stderr, "Cannot encode %\%%s: %\%%s\n", |
| 476 | er%\textbf{.failed\_type}%->name, strerror(errno)); |
| 477 | return -1; |
| 478 | } else { |
| 479 | /* Return the number of bytes */ |
| 480 | return er.encoded; |
| 481 | } |
| 482 | } |
| 483 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 484 | As you see, the DER encoder does not write into some sort of buffer |
| 485 | or something. It just invokes the custom function (possible, multiple |
| 486 | times) which would save the data into appropriate storage. The optional |
| 487 | argument \emph{app\_key} is opaque for the DER encoder code and just |
| 488 | used by \emph{\_write\_stream()} as the pointer to the appropriate |
| 489 | output stream to be used. |
| 490 | |
| 491 | If the custom write function is not given (passed as 0), then the |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 492 | DER encoder will essentially do the same thing (i.~e., encode the data) |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 493 | but no callbacks will be invoked (so the data goes nowhere). It may |
| 494 | prove useful to determine the size of the structure's encoding before |
| 495 | actually doing the encoding% |
| 496 | \footnote{It is actually faster too: the encoder might skip over some computations |
| 497 | which aren't important for the size determination.% |
| 498 | }. |
| 499 | |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 500 | Look into der\_encoder.h for the precise definition of der\_encode() |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 501 | and related types. |
| 502 | |
| 503 | |
| 504 | \subsection{\label{sub:Encoding-XER}Encoding XER} |
| 505 | |
| 506 | The XER stands for XML Encoding Rules, where XML, in turn, is eXtensible |
| 507 | Markup Language, a text-based format for information exchange. The |
| 508 | encoder routine API comes in two flavors: stdio-based and callback-based. |
| 509 | With the callback-based encoder, the encoding process is very similar |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 510 | to the DER one, described in Section~\ref{sub:Encoding-DER}. The |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 511 | following example uses the definition of write\_stream() from up there. |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 512 | \begin{codesample} |
| 513 | /* |
| 514 | * This procedure generates the XML document |
| 515 | * by invoking the XER encoder. |
| 516 | * NOTE: Do not copy this code verbatim! |
| 517 | * If the stdio output is necessary, |
| 518 | * use the xer_fprint() procedure instead. |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 519 | * See Section~%\ref{sub:Printing-the-target}%. |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 520 | */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 521 | int |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 522 | print_as_XML(FILE *ostream, Rectangle_t *rect) { |
| 523 | asn_enc_rval_t er; /* Encoder return value */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 524 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 525 | er = xer_encode(&asn_DEF_Rectangle, rect, |
| 526 | XER_F_BASIC, /* BASIC-XER or CANONICAL-XER */ |
| 527 | write_stream, ostream); |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 528 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 529 | return (er.encoded == -1) ? -1 : 0; |
| 530 | } |
| 531 | \end{codesample} |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 532 | Look into xer\_encoder.h for the precise definition of xer\_encode() |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 533 | and related types. |
| 534 | |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 535 | See Section~\ref{sub:Printing-the-target} for the example of stdio-based |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 536 | XML encoder and other pretty-printing suggestions. |
| 537 | |
| 538 | |
| 539 | \subsection{\label{sub:Decoding-XER}Decoding XER} |
| 540 | |
| 541 | The data encoded using the XER rules can be subsequently decoded using |
| 542 | the xer\_decode() API call: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 543 | \begin{codesample} |
| 544 | Rectangle_t * |
| 545 | XML_to_Rectangle(const void *buffer, size_t buf_size) { |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 546 | asn_dec_rval_t rval; |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 547 | Rectangle_t *%$\underbracket{\textrm{\listingfont rect = 0}}$%; /* %\textbf{\color{red}Note this 0\footnote{Forgetting to properly initialize the pointer to a destination structure is a major source of support requests.}!}% */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 548 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 549 | rval = xer_decode(0, &asn_DEF_Rectangle, (void **)&rect, buffer, buf_size); |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 550 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 551 | if(rval%\textbf{.code}% == RC_OK) { |
| 552 | return rect; /* Decoding succeeded */ |
| 553 | } else { |
| 554 | /* Free partially decoded rect */ |
| 555 | asn_DEF_Rectangle.free_struct(&asn_DEF_Rectangle, rect, 0); |
| 556 | return 0; |
| 557 | } |
| 558 | } |
| 559 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 560 | The decoder takes both BASIC-XER and CANONICAL-XER encodings. |
| 561 | |
| 562 | The decoder shares its data consumption properties with BER decoder; |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 563 | please read the Section~\ref{sub:Decoding-BER} to know more. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 564 | |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 565 | Look into xer\_decoder.h for the precise definition of xer\_decode() |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 566 | and related types. |
| 567 | |
| 568 | |
| 569 | \subsection{\label{sub:Validating-the-target}Validating the target structure} |
| 570 | |
| 571 | Sometimes the target structure needs to be validated. For example, |
| 572 | if the structure was created by the application (as opposed to being |
| 573 | decoded from some external source), some important information required |
| 574 | by the ASN.1 specification might be missing. On the other hand, the |
| 575 | successful decoding of the data from some external source does not |
| 576 | necessarily mean that the data is fully valid either. It might well |
| 577 | be the case that the specification describes some subtype constraints |
| 578 | that were not taken into account during decoding, and it would actually |
| 579 | be useful to perform the last check when the data is ready to be encoded |
| 580 | or when the data has just been decoded to ensure its validity according |
| 581 | to some stricter rules. |
| 582 | |
| 583 | The asn\_check\_constraints() function checks the type for various |
| 584 | implicit and explicit constraints. It is recommended to use asn\_check\_constraints() |
| 585 | function after each decoding and before each encoding. |
| 586 | |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 587 | Look into constraints.h for the precise definition of asn\_check\_constraints() |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 588 | and related types. |
| 589 | |
| 590 | |
| 591 | \subsection{\label{sub:Printing-the-target}Printing the target structure} |
| 592 | |
| 593 | There are two ways to print the target structure: either invoke the |
| 594 | print\_struct member of the ASN.1 type descriptor, or using the asn\_fprint() |
| 595 | function, which is a simpler wrapper of the former: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 596 | \begin{codesample} |
| 597 | asn_fprint(stdout, &asn_DEF_Rectangle, rect); |
| 598 | \end{codesample} |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 599 | Look into constr\_TYPE.h for the precise definition of asn\_fprint() |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 600 | and related types. |
| 601 | |
| 602 | Another practical alternative to this custom format printing would |
| 603 | be to invoke XER encoder. The default BASIC-XER encoder performs reasonable |
| 604 | formatting for the output to be useful and human readable. To invoke |
| 605 | the XER decoder in a manner similar to asn\_fprint(), use the xer\_fprint() |
| 606 | call: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 607 | \begin{codesample} |
| 608 | xer_fprint(stdout, &asn_DEF_Rectangle, rect); |
| 609 | \end{codesample} |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 610 | See Section~\ref{sub:Encoding-XER} for XML-related details. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 611 | |
| 612 | |
| 613 | \subsection{\label{sub:Freeing-the-target}Freeing the target structure} |
| 614 | |
| 615 | Freeing the structure is slightly more complex than it may seem to. |
| 616 | When the ASN.1 structure is freed, all the members of the structure |
| 617 | and their submembers are recursively freed as well. But it might not |
| 618 | be feasible to free the structure itself. Consider the following case: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 619 | \begin{codesample} |
| 620 | struct my_figure { /* The custom structure */ |
| 621 | int flags; /* <some custom member> */ |
| 622 | /* The type is generated by the ASN.1 compiler */ |
| 623 | Rectangle_t rect; |
| 624 | /* other members of the structure */ |
| 625 | }; |
| 626 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 627 | In this example, the application programmer defined a custom structure |
| 628 | with one ASN.1-derived member (rect). This member is not a reference |
| 629 | to the Rectangle\_t, but an in-place inclusion of the Rectangle\_t |
| 630 | structure. If the freeing is necessary, the usual procedure of freeing |
| 631 | everything must not be applied to the \&rect pointer itself, because |
| 632 | it does not point to the memory block directly allocated by the memory |
| 633 | allocation routine, but instead lies within a block allocated for |
| 634 | the my\_figure structure. |
| 635 | |
| 636 | To solve this problem, the free\_struct routine has the additional |
| 637 | argument (besides the obvious type descriptor and target structure |
| 638 | pointers), which is the flag specifying whether the outer pointer |
| 639 | itself must be freed (0, default) or it should be left intact (non-zero |
| 640 | value). |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 641 | \begin{codesample} |
| 642 | /* %\textbf{1. Rectangle\_t is defined within my\_figure}% */ |
| 643 | struct my_figure { |
| 644 | Rectangle_t rect; |
| 645 | } *mf = ...; |
| 646 | /* |
| 647 | * Freeing the Rectangle_t |
| 648 | * without freeing the mf->rect area. |
| 649 | */ |
| 650 | asn_DEF_Rectangle.free_struct( |
| 651 | &asn_DEF_Rectangle, &mf->rect, %\textbf{1 /* !free */}%); |
| 652 | |
| 653 | /* %\textbf{2. Rectangle\_t is a stand-alone pointer}% */ |
| 654 | Rectangle_t *rect = ...; |
| 655 | /* |
| 656 | * Freeing the Rectangle_t |
| 657 | * and freeing the rect pointer. |
| 658 | */ |
| 659 | asn_DEF_Rectangle.free_struct( |
| 660 | &asn_DEF_Rectangle, rect, %\textbf{0 /* free the pointer too */}%); |
| 661 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 662 | It is safe to invoke the \emph{free\_struct} function with the target |
| 663 | structure pointer set to 0 (NULL), the function will do nothing. |
| 664 | |
| 665 | For the programmer's convenience, the following macros are available: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 666 | \begin{codesample} |
| 667 | ASN_STRUCT_FREE(asn_DEF, ptr); |
| 668 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF, ptr); |
| 669 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 670 | These macros bear the same semantics as the \emph{free\_struct} function |
| 671 | invocation, discussed above. |
| 672 | |
| 673 | |
| 674 | \chapter{\label{cha:Step-by-step-examples}Step by step examples} |
| 675 | |
| 676 | |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 677 | \section{A ``Rectangle'' Encoder} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 678 | |
| 679 | This example will help you create a simple BER and XER encoder of |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 680 | a ``Rectangle'' type used throughout this document. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 681 | \begin{enumerate} |
| 682 | \item Create a file named \textbf{rectangle.asn1} with the following contents: |
| 683 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 684 | \begin{asn} |
| 685 | RectangleModule1 DEFINITIONS ::= BEGIN |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 686 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 687 | Rectangle ::= SEQUENCE { |
| 688 | height INTEGER, |
| 689 | width INTEGER |
| 690 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 691 | |
| 692 | END |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 693 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 694 | \item Compile it into the set of .c and .h files using asn1c compiler \cite{ASN1C}: |
| 695 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 696 | \begin{bash} |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 697 | asn1c %\textbf{rectangle.asn1}% |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 698 | \end{bash} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 699 | \item Alternatively, use the Online ASN.1 compiler \cite{AONL} by uploading |
| 700 | the \textbf{rectangle.asn1} file into the Web form and unpacking the |
| 701 | produced archive on your computer. |
| 702 | \item By this time, you should have gotten multiple files in the current |
| 703 | directory, including the \textbf{Rectangle.c} and \textbf{Rectangle.h}. |
| 704 | \item Create a main() routine which creates the Rectangle\_t structure in |
| 705 | memory and encodes it using BER and XER encoding rules. Let's name |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 706 | the file \textbf{main.c}: |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 707 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 708 | \begin{codesample}[basicstyle=\scriptsize\listingfont] |
| 709 | #include <stdio.h> |
| 710 | #include <sys/types.h> |
| 711 | #include <Rectangle.h> /* Rectangle ASN.1 type */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 712 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 713 | /* Write the encoded output into some FILE stream. */ |
| 714 | static int write_out(const void *buffer, size_t size, void *app_key) { |
| 715 | FILE *out_fp = app_key; |
| 716 | size_t wrote = fwrite(buffer, 1, size, out_fp); |
| 717 | return (wrote == size) ? 0 : -1; |
| 718 | } |
| 719 | |
| 720 | int main(int ac, char **av) { |
| 721 | Rectangle_t *rectangle; /* Type to encode */ |
| 722 | asn_enc_rval_t ec; /* Encoder return value */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 723 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 724 | /* Allocate the Rectangle_t */ |
| 725 | rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */ |
| 726 | if(!rectangle) { |
| 727 | perror("calloc() failed"); |
| 728 | exit(1); |
| 729 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 730 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 731 | /* Initialize the Rectangle members */ |
| 732 | rectangle->height = 42; /* any random value */ |
| 733 | rectangle->width = 23; /* any random value */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 734 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 735 | /* BER encode the data if filename is given */ |
| 736 | if(ac < 2) { |
| 737 | fprintf(stderr, "Specify filename for BER output\n"); |
| 738 | } else { |
| 739 | const char *filename = av[1]; |
| 740 | FILE *fp = fopen(filename, "wb"); /* for BER output */ |
| 741 | |
| 742 | if(!fp) { |
| 743 | perror(filename); |
| 744 | exit(1); |
| 745 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 746 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 747 | /* Encode the Rectangle type as BER (DER) */ |
| 748 | ec = der_encode(&asn_DEF_Rectangle, rectangle, write_out, fp); |
| 749 | fclose(fp); |
| 750 | if(ec.encoded == -1) { |
| 751 | fprintf(stderr, "Could not encode Rectangle (at %\%%s)\n", |
| 752 | ec.failed_type ? ec.failed_type->name : "unknown"); |
| 753 | exit(1); |
| 754 | } else { |
| 755 | fprintf(stderr, "Created %\%%s with BER encoded Rectangle\n", filename); |
| 756 | } |
| 757 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 758 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 759 | /* Also print the constructed Rectangle XER encoded (XML) */ |
| 760 | xer_fprint(stdout, &asn_DEF_Rectangle, rectangle); |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 761 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 762 | return 0; /* Encoding finished successfully */ |
| 763 | } |
| 764 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 765 | \item Compile all files together using C compiler (varies by platform): |
| 766 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 767 | \begin{bash} |
| 768 | cc -I. -o %\textbf{\emph{rencode}} \emph{*.c}% |
| 769 | \end{bash} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 770 | \item Voila! You have just created the BER and XER encoder of a Rectangle |
| 771 | type, named \textbf{rencode}! |
| 772 | \end{enumerate} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 773 | |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 774 | \section{\label{sec:A-Rectangle-Decoder}A ``Rectangle'' Decoder} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 775 | |
| 776 | This example will help you to create a simple BER decoder of a simple |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 777 | ``Rectangle'' type used throughout this document. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 778 | \begin{enumerate} |
| 779 | \item Create a file named \textbf{rectangle.asn1} with the following contents: |
| 780 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 781 | \begin{asn} |
| 782 | RectangleModule1 DEFINITIONS ::= BEGIN |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 783 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 784 | Rectangle ::= SEQUENCE { |
| 785 | height INTEGER, |
| 786 | width INTEGER |
| 787 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 788 | |
| 789 | END |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 790 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 791 | \item Compile it into the set of .c and .h files using asn1c compiler \cite{ASN1C}: |
| 792 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 793 | \begin{bash} |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 794 | asn1c %\textbf{rectangle.asn1}% |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 795 | \end{bash} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 796 | \item Alternatively, use the Online ASN.1 compiler \cite{AONL} by uploading |
| 797 | the \textbf{rectangle.asn1} file into the Web form and unpacking the |
| 798 | produced archive on your computer. |
| 799 | \item By this time, you should have gotten multiple files in the current |
| 800 | directory, including the \textbf{Rectangle.c} and \textbf{Rectangle.h}. |
| 801 | \item Create a main() routine which takes the binary input file, decodes |
| 802 | it as it were a BER-encoded Rectangle type, and prints out the text |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 803 | (XML) representation of the Rectangle type. Let's name the file \textbf{main.c}: |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 804 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 805 | \begin{codesample}[basicstyle=\scriptsize\listingfont] |
| 806 | #include <stdio.h> |
| 807 | #include <sys/types.h> |
| 808 | #include <Rectangle.h> /* Rectangle ASN.1 type */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 809 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 810 | int main(int ac, char **av) { |
| 811 | char buf[1024]; /* Temporary buffer */ |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 812 | asn_dec_rval_t rval; /* Decoder return value */ |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 813 | Rectangle_t *%$\underbracket{\textrm{\listingfont rectangle = 0}}$%; /* Type to decode. %\textbf{\color{red}Note this 0\footnote{Forgetting to properly initialize the pointer to a destination structure is a major source of support requests.}!}% */ |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 814 | FILE *fp; /* Input file handler */ |
| 815 | size_t size; /* Number of bytes read */ |
| 816 | char *filename; /* Input file name */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 817 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 818 | /* Require a single filename argument */ |
| 819 | if(ac != 2) { |
| 820 | fprintf(stderr, "Usage: %\%%s <file.ber>\n", av[0]); |
| 821 | exit(1); |
| 822 | } else { |
| 823 | filename = av[1]; |
| 824 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 825 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 826 | /* Open input file as read-only binary */ |
| 827 | fp = fopen(filename, "rb"); |
| 828 | if(!fp) { |
| 829 | perror(filename); |
| 830 | exit(1); |
| 831 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 832 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 833 | /* Read up to the buffer size */ |
| 834 | size = fread(buf, 1, sizeof(buf), fp); |
| 835 | fclose(fp); |
| 836 | if(!size) { |
| 837 | fprintf(stderr, "%\%%s: Empty or broken\n", filename); |
| 838 | exit(1); |
| 839 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 840 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 841 | /* Decode the input buffer as Rectangle type */ |
| 842 | rval = ber_decode(0, &asn_DEF_Rectangle, (void **)&rectangle, buf, size); |
| 843 | if(rval.code != RC_OK) { |
| 844 | fprintf(stderr, "%\%%s: Broken Rectangle encoding at byte %\%%ld\n", filename, (long)rval.consumed); |
| 845 | exit(1); |
| 846 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 847 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 848 | /* Print the decoded Rectangle type as XML */ |
| 849 | xer_fprint(stdout, &asn_DEF_Rectangle, rectangle); |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 850 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 851 | return 0; /* Decoding finished successfully */ |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 852 | } |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 853 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 854 | \item Compile all files together using C compiler (varies by platform): |
| 855 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 856 | \begin{bash} |
| 857 | cc -I. -o %\textbf{\emph{rdecode}} \emph{*.c}% |
| 858 | \end{bash} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 859 | \item Voila! You have just created the BER decoder of a Rectangle type, |
| 860 | named \textbf{rdecode}! |
| 861 | \end{enumerate} |
| 862 | |
| 863 | \chapter{Constraint validation examples} |
| 864 | |
| 865 | This chapter shows how to define ASN.1 constraints and use the generated |
| 866 | validation code. |
| 867 | |
| 868 | |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 869 | \section{Adding constraints into ``Rectangle'' type} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 870 | |
| 871 | This example shows how to add basic constraints to the ASN.1 specification |
| 872 | and how to invoke the constraints validation code in your application. |
| 873 | \begin{enumerate} |
| 874 | \item Create a file named \textbf{rectangle.asn1} with the following contents: |
| 875 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 876 | \begin{asn} |
| 877 | RectangleModuleWithConstraints DEFINITIONS ::= BEGIN |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 878 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 879 | Rectangle ::= SEQUENCE { |
| 880 | height INTEGER (0..100), -- Value range constraint |
| 881 | width INTEGER (0..MAX) -- Makes width non-negative |
| 882 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 883 | |
| 884 | END |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 885 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 886 | \item Compile the file according to procedures shown in the previous chapter. |
| 887 | \item Modify the Rectangle type processing routine (you can start with the |
Lev Walkin | 194b210 | 2013-03-28 01:29:06 -0700 | [diff] [blame] | 888 | main() routine shown in the Section~\ref{sec:A-Rectangle-Decoder}) |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 889 | by placing the following snippet of code \emph{before} encoding and/or |
| 890 | \emph{after} decoding the Rectangle type% |
| 891 | \footnote{Placing the constraint checking code \emph{before} encoding helps |
| 892 | to make sure you know the data is correct and within constraints before |
| 893 | sharing the data with anyone else. |
| 894 | |
| 895 | Placing the constraint checking code \emph{after} decoding, but before |
| 896 | any further action depending on the decoded data, helps to make sure |
| 897 | the application got the valid contents before making use of it.% |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 898 | }: |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 899 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 900 | \begin{codesample} |
| 901 | int ret; /* Return value */ |
| 902 | char errbuf[128]; /* Buffer for error message */ |
| 903 | size_t errlen = sizeof(errbuf); /* Size of the buffer */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 904 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 905 | /* ... here may go Rectangle %\emph{decoding}% code ... */ |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 906 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 907 | ret = asn_check_constraints(&asn_DEF_Rectangle, rectangle, errbuf, &errlen); |
| 908 | /* assert(errlen < sizeof(errbuf)); // you may rely on that */ |
| 909 | if(ret) { |
| 910 | fprintf(stderr, "Constraint validation failed: %\%%s\n", |
| 911 | errbuf /* errbuf is properly nul-terminated */ |
| 912 | ); |
| 913 | /* exit(...); // Replace with appropriate action */ |
| 914 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 915 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 916 | /* ... here may go Rectangle %\emph{encoding}% code ... */ |
| 917 | \end{codesample} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 918 | \item Compile the resulting C code as shown in the previous chapters. |
| 919 | \item Try to test the constraints checking code by assigning integer value |
| 920 | 101 to the \textbf{.height} member of the Rectangle structure, or |
| 921 | a negative value to the \textbf{.width} member. In either case, the |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 922 | program should print ``Constraint validation failed'' message, followed |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 923 | by a short explanation why validation did not succeed. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 924 | \item Done. |
| 925 | \end{enumerate} |
| 926 | |
| 927 | \part{\label{par:ASN.1-Basics}ASN.1 Basics} |
| 928 | |
| 929 | |
| 930 | \chapter{\label{cha:Abstract-Syntax-Notation:}Abstract Syntax Notation: ASN.1} |
| 931 | |
| 932 | \emph{This chapter defines some basic ASN.1 concepts and describes |
| 933 | several most widely used types. It is by no means an authoritative |
| 934 | or complete reference. For more complete ASN.1 description, please |
| 935 | refer to Olivier Dubuisson's book \cite{Dub00} or the ASN.1 body |
| 936 | of standards itself \cite{ITU-T/ASN.1}.} |
| 937 | |
| 938 | The Abstract Syntax Notation One is used to formally describe the |
| 939 | semantics of data transmitted across the network. Two communicating |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 940 | parties may have different formats of their native data types (i.~e. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 941 | number of bits in the integer type), thus it is important to have |
| 942 | a way to describe the data in a manner which is independent from the |
| 943 | particular machine's representation. The ASN.1 specifications are |
| 944 | used to achieve the following: |
| 945 | \begin{itemize} |
| 946 | \item The specification expressed in the ASN.1 notation is a formal and |
| 947 | precise way to communicate the data semantics to human readers; |
| 948 | \item The ASN.1 specifications may be used as input for automatic compilers |
| 949 | which produce the code for some target language (C, C++, Java, etc) |
| 950 | to encode and decode the data according to some encoding rules (which |
| 951 | are also defined by the ASN.1 standard). |
| 952 | \end{itemize} |
| 953 | Consider the following example: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 954 | \begin{asn} |
| 955 | Rectangle ::= SEQUENCE { |
| 956 | height INTEGER, |
| 957 | width INTEGER |
| 958 | } |
| 959 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 960 | This ASN.1 specification describes a constructed type, \emph{Rectangle}, |
| 961 | containing two integer fields. This specification may tell the reader |
| 962 | that there exists this kind of data structure and that some entity |
| 963 | may be prepared to send or receive it. The question on \emph{how} |
| 964 | that entity is going to send or receive the \emph{encoded data} is |
| 965 | outside the scope of ASN.1. For example, this data structure may be |
| 966 | encoded according to some encoding rules and sent to the destination |
| 967 | using the TCP protocol. The ASN.1 specifies several ways of encoding |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 968 | (or ``serializing'', or ``marshaling'') the data: BER, PER, XER |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 969 | and others, including CER and DER derivatives from BER. |
| 970 | |
| 971 | The complete specification must be wrapped in a module, which looks |
| 972 | like this: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 973 | \begin{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 974 | RectangleModule1 |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 975 | { iso org(3) dod(6) internet(1) private(4) |
| 976 | enterprise(1) spelio(9363) software(1) |
| 977 | asn1c(5) docs(2) rectangle(1) 1 } |
| 978 | DEFINITIONS AUTOMATIC TAGS ::= |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 979 | BEGIN |
| 980 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 981 | -- This is a comment which describes nothing. |
| 982 | Rectangle ::= SEQUENCE { |
| 983 | height INTEGER, -- Height of the rectangle |
| 984 | width INTEGER -- Width of the rectangle |
| 985 | } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 986 | |
| 987 | END |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 988 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 989 | The module header consists of module name (RectangleModule1), the |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 990 | module object identifier (\{...\}), a keyword ``DEFINITIONS'', a |
| 991 | set of module flags (AUTOMATIC TAGS) and ``::= BEGIN''. The module |
| 992 | ends with an ``END'' statement. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 993 | |
| 994 | |
| 995 | \section{Some of the ASN.1 Basic Types} |
| 996 | |
| 997 | |
| 998 | \subsection{The BOOLEAN type} |
| 999 | |
| 1000 | The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF |
| 1001 | or a similar kind of two-way choice. |
| 1002 | |
| 1003 | |
| 1004 | \subsection{The INTEGER type} |
| 1005 | |
| 1006 | The INTEGER type is a signed natural number type without any restrictions |
| 1007 | on its size. If the automatic checking on INTEGER value bounds are |
| 1008 | necessary, the subtype constraints must be used. |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1009 | \begin{asn} |
| 1010 | SimpleInteger ::= INTEGER |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1011 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1012 | -- An integer with a very limited range |
| 1013 | SmallPositiveInt ::= INTEGER (0..127) |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1014 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1015 | -- Integer, negative |
| 1016 | NegativeInt ::= INTEGER (MIN..0) |
| 1017 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1018 | |
| 1019 | \subsection{The ENUMERATED type} |
| 1020 | |
| 1021 | The ENUMERATED type is semantically equivalent to the INTEGER type |
| 1022 | with some integer values explicitly named. |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1023 | \begin{asn} |
| 1024 | FruitId ::= ENUMERATED { apple(1), orange(2) } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1025 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1026 | -- The numbers in braces are optional, |
| 1027 | -- the enumeration can be performed |
| 1028 | -- automatically by the compiler |
| 1029 | ComputerOSType ::= ENUMERATED { |
| 1030 | FreeBSD, -- acquires value 0 |
| 1031 | Windows, -- acquires value 1 |
| 1032 | Solaris(5), -- remains 5 |
| 1033 | Linux, -- becomes 6 |
| 1034 | MacOS -- becomes 7 |
| 1035 | } |
| 1036 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1037 | |
| 1038 | \subsection{The OCTET STRING type} |
| 1039 | |
| 1040 | This type models the sequence of 8-bit bytes. This may be used to |
| 1041 | transmit some opaque data or data serialized by other types of encoders |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1042 | (i.~e., video file, photo picture, etc). |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1043 | |
| 1044 | \subsection{The OBJECT IDENTIFIER type} |
| 1045 | |
| 1046 | The OBJECT IDENTIFIER is used to represent the unique identifier of |
| 1047 | any object, starting from the very root of the registration tree. |
| 1048 | If your organization needs to uniquely identify something (a router, |
| 1049 | a room, a person, a standard, or whatever), you are encouraged to |
| 1050 | get your own identification subtree at \url{http://www.iana.org/protocols/forms.htm}. |
| 1051 | |
| 1052 | For example, the very first ASN.1 module in this Chapter (RectangleModule1) |
| 1053 | has the following OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1. |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1054 | \begin{asn} |
| 1055 | ExampleOID ::= OBJECT IDENTIFIER |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1056 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1057 | rectangleModule1-oid ExampleOID |
| 1058 | ::= { 1 3 6 1 4 1 9363 1 5 2 1 1 } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1059 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1060 | -- An identifier of the Internet. |
| 1061 | internet-id OBJECT IDENTIFIER |
| 1062 | ::= { iso(1) identified-organization(3) |
| 1063 | dod(6) internet(1) } |
| 1064 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1065 | As you see, names are optional. |
| 1066 | |
| 1067 | |
| 1068 | \subsection{The RELATIVE-OID type} |
| 1069 | |
| 1070 | The RELATIVE-OID type has the semantics of a subtree of an OBJECT |
| 1071 | IDENTIFIER. There may be no need to repeat the whole sequence of numbers |
| 1072 | from the root of the registration tree where the only thing of interest |
| 1073 | is some of the tree's subsequence. |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1074 | \begin{asn} |
| 1075 | this-document RELATIVE-OID ::= { docs(2) usage(1) } |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1076 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1077 | this-example RELATIVE-OID ::= { |
| 1078 | this-document assorted-examples(0) this-example(1) } |
| 1079 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1080 | |
| 1081 | \section{Some of the ASN.1 String Types} |
| 1082 | |
| 1083 | |
| 1084 | \subsection{The IA5String type} |
| 1085 | |
| 1086 | This is essentially the ASCII, with 128 character codes available |
| 1087 | (7 lower bits of an 8-bit byte). |
| 1088 | |
| 1089 | |
| 1090 | \subsection{The UTF8String type} |
| 1091 | |
| 1092 | This is the character string which encodes the full Unicode range |
| 1093 | (4 bytes) using multibyte character sequences. |
| 1094 | |
| 1095 | |
| 1096 | \subsection{The NumericString type} |
| 1097 | |
| 1098 | This type represents the character string with the alphabet consisting |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 1099 | of numbers (``0'' to ``9'') and a space. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1100 | |
| 1101 | |
| 1102 | \subsection{The PrintableString type} |
| 1103 | |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 1104 | The character string with the following alphabet: space, ``\textbf{'}'' |
| 1105 | (single quote), ``\textbf{(}'', ``\textbf{)}'', ``\textbf{+}'', |
| 1106 | ``\textbf{,}'' (comma), ``\textbf{-}'', ``\textbf{.}'', ``\textbf{/}'', |
| 1107 | digits (``0'' to ``9''), ``\textbf{:}'', ``\textbf{=}'', ``\textbf{?}'', |
| 1108 | upper-case and lower-case letters (``A'' to ``Z'' and ``a'' |
| 1109 | to ``z''). |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1110 | |
| 1111 | |
| 1112 | \subsection{The VisibleString type} |
| 1113 | |
| 1114 | The character string with the alphabet which is more or less a subset |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 1115 | of ASCII between the space and the ``\textbf{\textasciitilde{}}'' |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1116 | symbol (tilde). |
| 1117 | |
| 1118 | Alternatively, the alphabet may be described as the PrintableString |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 1119 | alphabet presented earlier, plus the following characters: ``\textbf{!}'', |
| 1120 | ``\textbf{``}'', ``\textbf{\#}'', ``\textbf{\$}'', ``\textbf{\%}'', |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1121 | ``\textbf{\&}'', ``\textbf{*}'', ``\textbf{;}'', ``\textbf{<}'', |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 1122 | ``\textbf{>}'', ``\textbf{{[}}'', ``\textbf{\textbackslash{}}'', |
| 1123 | ``\textbf{{]}}'', ``\textbf{\textasciicircum{}}'', ``\textbf{\_}'', |
| 1124 | ``\textbf{`}`` (single left quote), ``\textbf{\{}'', ``\textbf{|}'', |
| 1125 | ``\textbf{\}}'', ``\textbf{\textasciitilde{}}''. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1126 | |
| 1127 | |
| 1128 | \section{ASN.1 Constructed Types} |
| 1129 | |
| 1130 | |
| 1131 | \subsection{The SEQUENCE type} |
| 1132 | |
| 1133 | This is an ordered collection of other simple or constructed types. |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 1134 | The SEQUENCE constructed type resembles the C ``struct'' statement. |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1135 | \begin{asn} |
| 1136 | Address ::= SEQUENCE { |
| 1137 | -- The apartment number may be omitted |
| 1138 | apartmentNumber NumericString OPTIONAL, |
| 1139 | streetName PrintableString, |
| 1140 | cityName PrintableString, |
| 1141 | stateName PrintableString, |
| 1142 | -- This one may be omitted too |
| 1143 | zipNo NumericString OPTIONAL |
| 1144 | } |
| 1145 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1146 | |
| 1147 | \subsection{The SET type} |
| 1148 | |
| 1149 | This is a collection of other simple or constructed types. Ordering |
| 1150 | is not important. The data may arrive in the order which is different |
| 1151 | from the order of specification. Data is encoded in the order not |
| 1152 | necessarily corresponding to the order of specification. |
| 1153 | |
| 1154 | |
| 1155 | \subsection{The CHOICE type} |
| 1156 | |
| 1157 | This type is just a choice between the subtypes specified in it. The |
| 1158 | CHOICE type contains at most one of the subtypes specified, and it |
| 1159 | is always implicitly known which choice is being decoded or encoded. |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 1160 | This one resembles the C ``union'' statement. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1161 | |
| 1162 | The following type defines a response code, which may be either an |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 1163 | integer code or a boolean ``true''/``false'' code. |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1164 | \begin{asn} |
| 1165 | ResponseCode ::= CHOICE { |
| 1166 | intCode INTEGER, |
| 1167 | boolCode BOOLEAN |
| 1168 | } |
| 1169 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1170 | |
| 1171 | \subsection{The SEQUENCE OF type} |
| 1172 | |
| 1173 | This one is the list (array) of simple or constructed types: |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1174 | \begin{asn} |
| 1175 | -- Example 1 |
| 1176 | ManyIntegers ::= SEQUENCE OF INTEGER |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1177 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1178 | -- Example 2 |
| 1179 | ManyRectangles ::= SEQUENCE OF Rectangle |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1180 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1181 | -- More complex example: |
| 1182 | -- an array of structures defined in place. |
| 1183 | ManyCircles ::= SEQUENCE OF SEQUENCE { |
| 1184 | radius INTEGER |
| 1185 | } |
| 1186 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1187 | |
| 1188 | \subsection{The SET OF type} |
| 1189 | |
| 1190 | The SET OF type models the bag of structures. It resembles the SEQUENCE |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1191 | OF type, but the order is not important: i.~e. the elements may arrive |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1192 | in the order which is not necessarily the same as the in-memory order |
| 1193 | on the remote machines. |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1194 | \begin{asn} |
| 1195 | -- A set of structures defined elsewhere |
| 1196 | SetOfApples :: SET OF Apple |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1197 | |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1198 | -- Set of integers encoding the kind of a fruit |
| 1199 | FruitBag ::= SET OF ENUMERATED { apple, orange } |
| 1200 | \end{asn} |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1201 | \begin{thebibliography}{ITU-T/ASN.1} |
| 1202 | \bibitem[ASN1C]{ASN1C}The Open Source ASN.1 Compiler. \url{http://lionet.info/asn1c} |
| 1203 | |
| 1204 | \bibitem[AONL]{AONL}Online ASN.1 Compiler. \url{http://lionet.info/asn1c/asn1c.cgi} |
| 1205 | |
| 1206 | \bibitem[Dub00]{Dub00}Olivier Dubuisson --- \emph{ASN.1 Communication |
Lev Walkin | 11c9a8c | 2013-03-26 00:46:55 -0700 | [diff] [blame] | 1207 | between heterogeneous systems} --- Morgan Kaufmann Publishers, 2000. |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1208 | \url{http://asn1.elibel.tm.fr/en/book/}. ISBN:0-12-6333361-0. |
| 1209 | |
Lev Walkin | 464166c | 2010-11-09 08:34:38 -0800 | [diff] [blame] | 1210 | \bibitem[ITU-T/ASN.1]{ITU-T/ASN.1}ITU-T Study Group 17 --- Languages |
Lev Walkin | ed44bf4 | 2010-11-08 02:04:55 -0800 | [diff] [blame] | 1211 | for Telecommunication Systems \url{http://www.itu.int/ITU-T/studygroups/com17/languages/} |
| 1212 | \end{thebibliography} |
| 1213 | |
| 1214 | \end{document} |