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