constraint checking example

This commit is contained in:
Lev Walkin 2005-02-02 09:53:46 +00:00
parent 6b0df9f7af
commit dee61785fa
3 changed files with 404 additions and 78 deletions

View File

@ -6,9 +6,9 @@ original version by: Nikos Drakos, CBLU, University of Leeds
Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>Using the OpenSource ASN.1 Compiler</TITLE>
<META NAME="description" CONTENT="Using the OpenSource ASN.1 Compiler">
<META NAME="asn1c, ASN.1, free, compiler, BER, DER, PER, XER\">
<TITLE>Using the Open Source ASN.1 Compiler</TITLE>
<META NAME="description" CONTENT="Using the Open Source ASN.1 Compiler">
<META NAME="ASN.1, asn1c, compiler, BER, DER, XER\">
@ -21,7 +21,7 @@ original version by: Nikos Drakos, CBLU, University of Leeds
<P>
<P>
<H1 ALIGN="CENTER">Using the OpenSource ASN.1 Compiler</H1><DIV>
<H1 ALIGN="CENTER">Using the Open Source ASN.1 Compiler</H1><DIV>
<P ALIGN="CENTER"><STRONG>Lev Walkin &lt;<A HREF=mailto:vlm@lionet.info?Subject=asn1c>vlm@lionet.info</A>&gt;</STRONG></P>
</DIV>
@ -41,103 +41,109 @@ Contents</A>
<!--Table of Contents-->
<UL>
<LI><A NAME="tex2html50"
<LI><A NAME="tex2html53"
HREF="asn1c-usage.html#SECTION02000000000000000000">ASN.1 Basics</A>
<UL>
<LI><A NAME="tex2html51"
<LI><A NAME="tex2html54"
HREF="asn1c-usage.html#SECTION02100000000000000000">Abstract Syntax Notation: ASN.1</A>
<UL>
<LI><A NAME="tex2html52"
<LI><A NAME="tex2html55"
HREF="asn1c-usage.html#SECTION02110000000000000000">Some of the ASN.1 Basic Types</A>
<UL>
<LI><A NAME="tex2html53"
HREF="asn1c-usage.html#SECTION02111000000000000000">The BOOLEAN type</A>
<LI><A NAME="tex2html54"
HREF="asn1c-usage.html#SECTION02112000000000000000">The INTEGER type</A>
<LI><A NAME="tex2html55"
HREF="asn1c-usage.html#SECTION02113000000000000000">The ENUMERATED type</A>
<LI><A NAME="tex2html56"
HREF="asn1c-usage.html#SECTION02114000000000000000">The OCTET STRING type</A>
HREF="asn1c-usage.html#SECTION02111000000000000000">The BOOLEAN type</A>
<LI><A NAME="tex2html57"
HREF="asn1c-usage.html#SECTION02115000000000000000">The OBJECT IDENTIFIER type</A>
HREF="asn1c-usage.html#SECTION02112000000000000000">The INTEGER type</A>
<LI><A NAME="tex2html58"
HREF="asn1c-usage.html#SECTION02113000000000000000">The ENUMERATED type</A>
<LI><A NAME="tex2html59"
HREF="asn1c-usage.html#SECTION02114000000000000000">The OCTET STRING type</A>
<LI><A NAME="tex2html60"
HREF="asn1c-usage.html#SECTION02115000000000000000">The OBJECT IDENTIFIER type</A>
<LI><A NAME="tex2html61"
HREF="asn1c-usage.html#SECTION02116000000000000000">The RELATIVE-OID type</A>
</UL>
<LI><A NAME="tex2html59"
<LI><A NAME="tex2html62"
HREF="asn1c-usage.html#SECTION02120000000000000000">Some of the ASN.1 String Types</A>
<UL>
<LI><A NAME="tex2html60"
HREF="asn1c-usage.html#SECTION02121000000000000000">The IA5String type</A>
<LI><A NAME="tex2html61"
HREF="asn1c-usage.html#SECTION02122000000000000000">The UTF8String type</A>
<LI><A NAME="tex2html62"
HREF="asn1c-usage.html#SECTION02123000000000000000">The NumericString type</A>
<LI><A NAME="tex2html63"
HREF="asn1c-usage.html#SECTION02124000000000000000">The PrintableString type</A>
HREF="asn1c-usage.html#SECTION02121000000000000000">The IA5String type</A>
<LI><A NAME="tex2html64"
HREF="asn1c-usage.html#SECTION02122000000000000000">The UTF8String type</A>
<LI><A NAME="tex2html65"
HREF="asn1c-usage.html#SECTION02123000000000000000">The NumericString type</A>
<LI><A NAME="tex2html66"
HREF="asn1c-usage.html#SECTION02124000000000000000">The PrintableString type</A>
<LI><A NAME="tex2html67"
HREF="asn1c-usage.html#SECTION02125000000000000000">The VisibleString type</A>
</UL>
<LI><A NAME="tex2html65"
<LI><A NAME="tex2html68"
HREF="asn1c-usage.html#SECTION02130000000000000000">ASN.1 Constructed Types</A>
<UL>
<LI><A NAME="tex2html66"
HREF="asn1c-usage.html#SECTION02131000000000000000">The SEQUENCE type</A>
<LI><A NAME="tex2html67"
HREF="asn1c-usage.html#SECTION02132000000000000000">The SET type</A>
<LI><A NAME="tex2html68"
HREF="asn1c-usage.html#SECTION02133000000000000000">The CHOICE type</A>
<LI><A NAME="tex2html69"
HREF="asn1c-usage.html#SECTION02134000000000000000">The SEQUENCE OF type</A>
HREF="asn1c-usage.html#SECTION02131000000000000000">The SEQUENCE type</A>
<LI><A NAME="tex2html70"
HREF="asn1c-usage.html#SECTION02132000000000000000">The SET type</A>
<LI><A NAME="tex2html71"
HREF="asn1c-usage.html#SECTION02133000000000000000">The CHOICE type</A>
<LI><A NAME="tex2html72"
HREF="asn1c-usage.html#SECTION02134000000000000000">The SEQUENCE OF type</A>
<LI><A NAME="tex2html73"
HREF="asn1c-usage.html#SECTION02135000000000000000">The SET OF type</A>
</UL>
</UL>
</UL><BR>
<LI><A NAME="tex2html71"
<LI><A NAME="tex2html74"
HREF="asn1c-usage.html#SECTION03000000000000000000">Using the ASN.1 Compiler</A>
<UL>
<LI><A NAME="tex2html72"
<LI><A NAME="tex2html75"
HREF="asn1c-usage.html#SECTION03100000000000000000">Introduction to the ASN.1 Compiler</A>
<LI><A NAME="tex2html73"
<LI><A NAME="tex2html76"
HREF="asn1c-usage.html#SECTION03200000000000000000">Quick start</A>
<LI><A NAME="tex2html74"
<LI><A NAME="tex2html77"
HREF="asn1c-usage.html#SECTION03300000000000000000">Using the ASN.1 Compiler</A>
<UL>
<LI><A NAME="tex2html75"
<LI><A NAME="tex2html78"
HREF="asn1c-usage.html#SECTION03310000000000000000">Command-line options</A>
<LI><A NAME="tex2html76"
<LI><A NAME="tex2html79"
HREF="asn1c-usage.html#SECTION03320000000000000000">Recognizing compiler output</A>
<LI><A NAME="tex2html77"
<LI><A NAME="tex2html80"
HREF="asn1c-usage.html#SECTION03330000000000000000">Invoking the ASN.1 helper code from an application</A>
<UL>
<LI><A NAME="tex2html78"
HREF="asn1c-usage.html#SECTION03331000000000000000">Decoding BER</A>
<LI><A NAME="tex2html79"
HREF="asn1c-usage.html#SECTION03332000000000000000">Encoding DER</A>
<LI><A NAME="tex2html80"
HREF="asn1c-usage.html#SECTION03333000000000000000">Encoding XER</A>
<LI><A NAME="tex2html81"
HREF="asn1c-usage.html#SECTION03334000000000000000">Validating the target structure</A>
HREF="asn1c-usage.html#SECTION03331000000000000000">Decoding BER</A>
<LI><A NAME="tex2html82"
HREF="asn1c-usage.html#SECTION03335000000000000000">Printing the target structure</A>
HREF="asn1c-usage.html#SECTION03332000000000000000">Encoding DER</A>
<LI><A NAME="tex2html83"
HREF="asn1c-usage.html#SECTION03333000000000000000">Encoding XER</A>
<LI><A NAME="tex2html84"
HREF="asn1c-usage.html#SECTION03334000000000000000">Validating the target structure</A>
<LI><A NAME="tex2html85"
HREF="asn1c-usage.html#SECTION03335000000000000000">Printing the target structure</A>
<LI><A NAME="tex2html86"
HREF="asn1c-usage.html#SECTION03336000000000000000">Freeing the target structure</A>
</UL>
</UL>
</UL><BR>
<LI><A NAME="tex2html84"
<LI><A NAME="tex2html87"
HREF="asn1c-usage.html#SECTION04000000000000000000">Examples</A>
<UL>
<LI><A NAME="tex2html85"
<LI><A NAME="tex2html88"
HREF="asn1c-usage.html#SECTION04100000000000000000">Step by step examples</A>
<UL>
<LI><A NAME="tex2html86"
<LI><A NAME="tex2html89"
HREF="asn1c-usage.html#SECTION04110000000000000000">A ''Rectangle'' Encoder</A>
<LI><A NAME="tex2html87"
<LI><A NAME="tex2html90"
HREF="asn1c-usage.html#SECTION04120000000000000000">A ''Rectangle'' Decoder</A>
</UL>
<LI><A NAME="tex2html91"
HREF="asn1c-usage.html#SECTION04200000000000000000">Constraint validation examples</A>
<UL>
<LI><A NAME="tex2html92"
HREF="asn1c-usage.html#SECTION04210000000000000000">Adding constraints into ''Rectangle'' type</A>
</UL>
</UL><BR>
<LI><A NAME="tex2html88"
<LI><A NAME="tex2html93"
HREF="asn1c-usage.html#SECTION05000000000000000000">Bibliography</A>
</UL>
<!--End of Table of Contents-->
@ -580,7 +586,7 @@ language structures and surrounding maintenance code. For example,
the C structure which may be created by compiler to represent the
simple <I>Rectangle</I> specification defined earlier in this document,
may look like this<A NAME="tex2html2"
HREF="#foot739"><SUP>2.2</SUP></A>:
HREF="#foot801"><SUP>2.2</SUP></A>:
<P>
@ -606,8 +612,8 @@ Quick start</A>
<P>
After building and installing the compiler, the <I>asn1c</I><A NAME="tex2html4"
HREF="#foot740"><SUP>3.1</SUP></A> command may be used to compile the ASN.1 specification<A NAME="tex2html5"
HREF="#foot741"><SUP>3.2</SUP></A>:
HREF="#foot802"><SUP>3.1</SUP></A> command may be used to compile the ASN.1 specification<A NAME="tex2html5"
HREF="#foot803"><SUP>3.2</SUP></A>:
<P>
@ -673,7 +679,7 @@ the compiler's behavior.
<P>
<BR><P></P>
<DIV ALIGN="CENTER"><A NAME="744"></A>
<DIV ALIGN="CENTER"><A NAME="806"></A>
<TABLE>
<CAPTION><STRONG><A NAME=Table1>Table 1:</A></STRONG>
The list of asn1c command line options</CAPTION>
@ -817,7 +823,7 @@ be possible to compile everything with the single instruction:
<BLOCKQUOTE><PRE>
cc&nbsp;-o&nbsp;rectangle&nbsp;*.c&nbsp;&nbsp;&nbsp;#&nbsp;It&nbsp;could&nbsp;be&nbsp;<I>that</I>&nbsp;simple<A NAME="tex2html7"
HREF="#foot747"><SUP>4.1</SUP></A>
HREF="#foot809"><SUP>4.1</SUP></A>
</PRE>
</BLOCKQUOTE>
@ -1295,8 +1301,8 @@ Examples</A>
<P>
<H1><A NAME="SECTION04100000000000000000">
Step by step examples</A>
<H1><A NAME="SECTION04100000000000000000"></A><A NAME="cha:Step-by-step-examples"></A><BR>
Step by step examples
</H1>
<P>
@ -1306,7 +1312,7 @@ A ''Rectangle'' Encoder</A>
</H1>
<P>
This chapter will help you to create a simple BER and XER encoder
This example will help you to create a simple BER and XER encoder
of a ''Rectangle'' type used throughout this document.
<P>
@ -1379,7 +1385,7 @@ the file <B>main.c</B>:
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;asn_enc_rval_t&nbsp;ec;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Encoder&nbsp;return&nbsp;value&nbsp;&nbsp;*/</FONT>
&nbsp;
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;/*&nbsp;Allocate&nbsp;the&nbsp;Rectangle_t&nbsp;*/</FONT>
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;rectangle&nbsp;=&nbsp;calloc(1,&nbsp;sizeof(Rectangle_t);&nbsp;/*&nbsp;not&nbsp;malloc!&nbsp;*/</FONT>
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;rectangle&nbsp;=&nbsp;calloc(1,&nbsp;sizeof(Rectangle_t));&nbsp;/*&nbsp;not&nbsp;malloc!&nbsp;*/</FONT>
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;if(!rectangle)&nbsp;{</FONT>
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(''calloc()&nbsp;failed'');</FONT>
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(71);&nbsp;/*&nbsp;better,&nbsp;EX_OSERR&nbsp;*/</FONT>
@ -1443,12 +1449,12 @@ type, named <B>rencode</B>!
<P>
<H1><A NAME="SECTION04120000000000000000">
A ''Rectangle'' Decoder</A>
<H1><A NAME="SECTION04120000000000000000"></A><A NAME="sec:A-Rectangle-Decoder"></A><BR>
A ''Rectangle'' Decoder
</H1>
<P>
This chapter will help you to create a simple BER decoder of a simple
This example will help you to create a simple BER decoder of a simple
''Rectangle'' type used throughout this document.
<P>
@ -1565,11 +1571,90 @@ it as it were a BER-encoded Rectangle type, and prints out the text
<LI>Voila! You have just created the BER decoder of a Rectangle type,
named <B>rdecode</B>!
</LI>
</OL>
<P>
<H1><A NAME="SECTION04200000000000000000">
Constraint validation examples</A>
</H1>
<P>
This chapter shows how to define ASN.1 constraints and use the generated
validation code.
<P>
<H1><A NAME="SECTION04210000000000000000">
Adding constraints into ''Rectangle'' type</A>
</H1>
<P>
This example shows how to add basic constraints to the ASN.1 specification
and how to invoke the constraints validation code in your application.
<P>
<OL>
<LI>Create a file named <B>rectangle.asn1</B> with the following contents:
<P>
<BLOCKQUOTE><PRE>
RectangleModuleWithConstraints&nbsp;DEFINITIONS&nbsp;::=
BEGIN
&nbsp;
Rectangle&nbsp;::=&nbsp;SEQUENCE&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;height&nbsp;&nbsp;INTEGER&nbsp;(0..100),&nbsp;--&nbsp;Value&nbsp;range&nbsp;constraint
&nbsp;&nbsp;&nbsp;&nbsp;width&nbsp;&nbsp;&nbsp;INTEGER&nbsp;(0..MAX)&nbsp;&nbsp;--&nbsp;Makes&nbsp;width&nbsp;non-negative&nbsp;
}
&nbsp;
END
</PRE>
</BLOCKQUOTE>
</LI>
<LI>Compile the file according to procedures shown in the previous chapter.
</LI>
<LI>Modify the Rectangle type processing routine (you can start with the
main() routine shown in the Section sec:A-Rectangle-Decoder)
by placing the following snippet of code <I>before</I> encoding and/or
<I>after</I> decoding the Rectangle type<A NAME="tex2html10"
HREF="#foot874"><SUP>6.1</SUP></A>:
<P>
<BLOCKQUOTE><PRE>
<FONT SIZE="-1">int&nbsp;ret;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Return&nbsp;value&nbsp;*/</FONT>
<FONT SIZE="-1">char&nbsp;errbuf[128];&nbsp;&nbsp;/*&nbsp;Buffer&nbsp;for&nbsp;error&nbsp;message&nbsp;*/</FONT>
<FONT SIZE="-1">size_t&nbsp;errlen&nbsp;=&nbsp;sizeof(errbuf);&nbsp;&nbsp;/*&nbsp;Size&nbsp;of&nbsp;the&nbsp;buffer&nbsp;*/</FONT>
&nbsp;<FONT SIZE="-1">&nbsp;</FONT>
<FONT SIZE="-1">/*&nbsp;...&nbsp;here&nbsp;may&nbsp;go&nbsp;Rectangle&nbsp;decoding&nbsp;code&nbsp;...&nbsp;*/</FONT>
&nbsp;
<FONT SIZE="-1">ret&nbsp;=&nbsp;asn_check_constraints(asn_DEF_Rectangle,</FONT>
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rectangle,&nbsp;errbuf,&nbsp;&amp;errlen);</FONT>
<FONT SIZE="-1">/*&nbsp;assert(errlen&nbsp;&lt;&nbsp;sizeof(errbuf));&nbsp;//&nbsp;you&nbsp;may&nbsp;rely&nbsp;on&nbsp;that&nbsp;*/</FONT>
<FONT SIZE="-1">if(ret)&nbsp;{</FONT>
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;''Constraint&nbsp;validation&nbsp;failed:&nbsp;%s&#92;n'',</FONT>
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errbuf&nbsp;&nbsp;&nbsp;/*&nbsp;errbuf&nbsp;is&nbsp;properly&nbsp;nul-terminated&nbsp;*/</FONT>
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);</FONT>
&nbsp;<FONT SIZE="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;exit(...);&nbsp;//&nbsp;Replace&nbsp;with&nbsp;appropriate&nbsp;action&nbsp;*/</FONT>
<FONT SIZE="-1">}</FONT>
&nbsp;
<FONT SIZE="-1">/*&nbsp;...&nbsp;here&nbsp;may&nbsp;go&nbsp;Rectangle&nbsp;encoding&nbsp;code&nbsp;...&nbsp;*/</FONT>
</PRE>
</BLOCKQUOTE>
</LI>
<LI>Compile the resulting C code as shown in the previous chapters.
</LI>
<LI>Done.
</LI>
</OL>
<H2><A NAME="SECTION05000000000000000000">
Bibliography</A>
</H2><DL COMPACT><DD><P></P><DT><A NAME="ASN1C">ASN1C</A>
<DD>The OpenSource ASN.1 Compiler. <A HREF=http://lionet.info/asn1c/>http://lionet.info/asn1c/</A>
<DD>The Open Source ASN.1 Compiler. <A HREF=http://lionet.info/>http://lionet.info/</A>asn1c
<P></P><DT><A NAME="AONL">AONL</A>
<DD>Online ASN.1 Compiler. <A HREF=http://lionet.info/asn1c/>http://lionet.info/asn1c/</A>asn1c.cgi
<P></P><DT><A NAME="Dub00">Dub00</A>
@ -1577,7 +1662,8 @@ Bibliography</A>
systems</I> -- Morgan Kaufmann Publishers, 2000. <A HREF=http://asn1.elibel.tm.fr/en/book/>http://asn1.elibel.tm.fr/en/book/</A>.
ISBN:0-12-6333361-0.
<P></P><DT><A NAME="ITU-T_ASN.1">ITU-T/ASN.1</A>
<DD>ITU-T Study Group 17 - Languages for Telecommunication Systems <A HREF=http://www.itu.int/ITU-T/studygroups/com17/languages/>http://www.itu.int/ITU-T/studygroups/com17/languages/</A></DL>
<DD>ITU-T Study Group 17 - Languages for Telecommunication Systems <A HREF=http://www.itu.int/ITU-T/studygroups/com17/languages/>http://www.itu.int/ITU-T/studygroups/com17/languages/</A>
</DL>
<P>
<BR><HR><H4>Footnotes</H4>
@ -1588,7 +1674,7 @@ ISBN:0-12-6333361-0.
not a definitive factor.
</DD>
<DT><A NAME="foot739">... this</A><A
<DT><A NAME="foot801">... this</A><A
HREF="asn1c-usage.html#tex2html2"><SUP>2.2</SUP></A></DT>
<DD><I>-fnative-types</I> compiler option is used to produce basic C <I>int</I>
types instead of infinite width INTEGER_t structures. See <A HREF=#Table1>Table 1</A>.
@ -1600,19 +1686,19 @@ types instead of infinite width INTEGER_t structures. See <A HREF=#Table1>Table
text (XML) based.
</DD>
<DT><A NAME="foot740">...asn1c</A><A
<DT><A NAME="foot802">...asn1c</A><A
HREF="asn1c-usage.html#tex2html4"><SUP>3.1</SUP></A></DT>
<DD>The 1 symbol in asn<B>1</B>c is a digit, not an ''ell'' letter.
</DD>
<DT><A NAME="foot741">... specification</A><A
<DT><A NAME="foot803">... specification</A><A
HREF="asn1c-usage.html#tex2html5"><SUP>3.2</SUP></A></DT>
<DD>This is probably <B>not</B> what you want to try out right now -
read through the rest of this chapter to find out about <B>-P</B>
and <B>-R</B> options.
</DD>
<DT><A NAME="foot747">...that&nbsp;simple</A><A
<DT><A NAME="foot809">...that&nbsp;simple</A><A
HREF="asn1c-usage.html#tex2html7"><SUP>4.1</SUP></A></DT>
<DD>Provided that you've also created a .c file with the <I>int main()</I>
routine.
@ -1630,11 +1716,23 @@ buffer to continue decoding.
<DD>It is actually faster too: the encoder might skip over some computations
which aren't important for the size determination.
</DD>
<DT><A NAME="foot874">... type</A><A
HREF="asn1c-usage.html#tex2html10"><SUP>6.1</SUP></A></DT>
<DD>Placing the constraint checking code <I>before</I> encoding helps
to make sure you know the data is correct and within constraints before
sharing the data with anyone else.
<P>
Placing the constraint checking code <I>after</I> decoding, but before
any further action depending on the decoded data, helps to make sure
the application got the valid contents before making use of it.
</DD>
</DL><BR><HR>
<ADDRESS>
Lev Walkin
2005-01-31
2005-02-02
</ADDRESS>
</BODY>
</HTML>

View File

@ -12,7 +12,7 @@
linkcolor={gray40},
urlcolor={urlblue},
pdfauthor={Lev Walkin},
pdftitle={Using the OpenSource ASN.1 Compiler},
pdftitle={Using the Open Source ASN.1 Compiler},
pdfkeywords={ASN.1,asn1c,compiler}
]{hyperref}
%\fancyhf{}
@ -46,7 +46,7 @@ pdfkeywords={ASN.1,asn1c,compiler}
\layout Title
Using the OpenSource ASN.1 Compiler
Using the Open Source ASN.1 Compiler
\layout Author
Lev Walkin <
@ -2740,6 +2740,11 @@ free_struct
Examples
\layout Chapter
\begin_inset LatexCommand \label{cha:Step-by-step-examples}
\end_inset
Step by step examples
\layout Section
@ -2754,7 +2759,7 @@ Rectangle
Encoder
\layout Standard
This chapter will help you to create a simple BER and XER encoder of a
This example will help you to create a simple BER and XER encoder of a
\begin_inset Quotes sld
\end_inset
@ -2976,7 +2981,7 @@ int main(int ac, char **av) {
\size small
rectangle = calloc(1, sizeof(Rectangle_t); /* not malloc! */
rectangle = calloc(1, sizeof(Rectangle_t)); /* not malloc! */
\layout LyX-Code
@ -3273,6 +3278,11 @@ clearpage{}
\layout Section
\begin_inset LatexCommand \label{sec:A-Rectangle-Decoder}
\end_inset
A
\begin_inset Quotes sld
\end_inset
@ -3284,7 +3294,7 @@ Rectangle
Decoder
\layout Standard
This chapter will help you to create a simple BER decoder of a simple
This example will help you to create a simple BER decoder of a simple
\begin_inset Quotes sld
\end_inset
@ -3382,7 +3392,7 @@ main.c
\series default
:
\begin_inset ERT
status Open
status Collapsed
\layout Standard
@ -3704,12 +3714,230 @@ Voila! You have just created the BER decoder of a Rectangle type, named
rdecode
\series default
!
\layout Chapter
Constraint validation examples
\layout Standard
This chapter shows how to define ASN.1 constraints and use the generated
validation code.
\layout Section
Adding constraints into
\begin_inset Quotes sld
\end_inset
Rectangle
\begin_inset Quotes srd
\end_inset
type
\layout Standard
This example shows how to add basic constraints to the ASN.1 specification
and how to invoke the constraints validation code in your application.
\layout Enumerate
Create a file named
\series bold
rectangle.asn1
\series default
with the following contents:
\begin_deeper
\layout LyX-Code
RectangleModuleWithConstraints DEFINITIONS ::=
\layout LyX-Code
BEGIN
\layout LyX-Code
\layout LyX-Code
Rectangle ::= SEQUENCE {
\layout LyX-Code
height INTEGER (0..100), -- Value range constraint
\layout LyX-Code
width INTEGER (0..MAX) -- Makes width non-negative
\layout LyX-Code
}
\layout LyX-Code
\layout LyX-Code
END
\end_deeper
\layout Enumerate
Compile the file according to procedures shown in the previous chapter.
\layout Enumerate
Modify the Rectangle type processing routine (you can start with the main()
routine shown in the Section
\begin_inset LatexCommand \vref{sec:A-Rectangle-Decoder}
\end_inset
) by placing the following snippet of code
\emph on
before
\emph default
encoding and/or
\emph on
after
\emph default
decoding the Rectangle type
\begin_inset Foot
collapsed true
\layout Standard
Placing the constraint checking code
\emph on
before
\emph default
encoding helps to make sure you know the data is correct and within constraints
before sharing the data with anyone else.
\layout Standard
Placing the constraint checking code
\emph on
after
\emph default
decoding, but before any further action depending on the decoded data,
helps to make sure the application got the valid contents before making
use of it.
\end_inset
:
\begin_inset ERT
status Collapsed
\layout Standard
\backslash
clearpage{}
\end_inset
\begin_deeper
\layout LyX-Code
\size small
int ret; /* Return value */
\layout LyX-Code
\size small
char errbuf[128]; /* Buffer for error message */
\layout LyX-Code
\size small
size_t errlen = sizeof(errbuf); /* Size of the buffer */
\layout LyX-Code
\size small
\layout LyX-Code
\size small
/* ...
here may go Rectangle decoding code ...
*/
\layout LyX-Code
\size small
\layout LyX-Code
\size small
ret = asn_check_constraints(asn_DEF_Rectangle,
\layout LyX-Code
\size small
rectangle, errbuf, &errlen);
\layout LyX-Code
\size small
/* assert(errlen < sizeof(errbuf)); // you may rely on that */
\layout LyX-Code
\size small
if(ret) {
\layout LyX-Code
\size small
fprintf(stderr,
\begin_inset Quotes sld
\end_inset
Constraint validation failed: %s
\backslash
n
\begin_inset Quotes srd
\end_inset
,
\layout LyX-Code
\size small
errbuf /* errbuf is properly nul-terminated */
\layout LyX-Code
\size small
);
\layout LyX-Code
\size small
/* exit(...); // Replace with appropriate action */
\layout LyX-Code
\size small
}
\layout LyX-Code
\size small
\layout LyX-Code
\size small
/* ...
here may go Rectangle encoding code ...
*/
\end_deeper
\layout Enumerate
Compile the resulting C code as shown in the previous chapters.
\layout Enumerate
Done.
\layout Bibliography
\bibitem [ASN1C]{ASN1C}
The OpenSource ASN.1 Compiler.
The Open Source ASN.1 Compiler.
\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c/}
\begin_inset LatexCommand \htmlurl{http://lionet.info/asn1c}
\end_inset

Binary file not shown.