1 % file: .../doc/intro-1.1.tex
3 % $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/doc/intro-1.1.tex,v 1.1.1.1 2001/05/18 23:14:10 mb Exp $
4 % $Log: intro-1.1.tex,v $
5 % Revision 1.1.1.1 2001/05/18 23:14:10 mb
6 % Move from private repository to open source repository
8 % Revision 1.1.1.1 1999/03/16 18:05:53 aram
9 % Originals from SMIME Free Library.
11 % Revision 1.1 1997/01/01 22:47:28 rj
15 \chapter{\label{intro-
1.1}Introduction to Snacc Release~
1.1}
17 Snacc compiles ASN
.1 \cite{X
.208} (Abstract Syntax Notation One)
18 modules into C, C++ or type tables. The generated C or C++ code
19 contains equivalent data structures and routines to convert values
20 between the internal (C or C++) representation and the corresponding
21 BER
\cite{X
.209} (Basic Encoding Rules) format. The name ``snacc'' is
22 an acronym for ``Sample Neufeld ASN
.1 to C/C++ Compiler''.
24 This compiler was written so I could do some encoding performance
25 research for my M.Sc. See
\cite{Sample93-
1}, or
\cite{Sample93-
2} for
26 the results of that research. A techreport will soo be available
27 via ftp from UBC, in the same directory as snacc.
29 The ASN
.1 data structure language can specify complex types such as
30 lists and recursively defined types. BER data values are defined
31 independently of any computer architecture, providing a universal data
32 value representation that is useful for sharing data in heterogeneous
35 The process of converting an ASN
.1 value from its C or C++
36 representation into an equivalent BER data value is called encoding
37 and the reverse process is called decoding. This
document was written
38 assuming that the reader is familiar ASN
.1 and BER. Further
39 information on ASN
.1 and BER can be found in
\cite{ASN
.1Book
},
40 \cite{ASN
.1Overview
},
\cite{X
.208} and
\cite{X
.209}.
42 Compiling ASN
.1 into C is not a new idea but many other tools such as
43 UBC's CASN1
\cite{CASN1
}, ISODE's PEPY/POSY
\cite{ISODE
}, and
44 commercial tools either do not parse ASN
.1 '
90, produce slow encoders
45 and decoders or are outrageously expensive. The aim of this tool is to
46 provide an ASN
.1 compiler that parses ASN
.1 '
90, produces efficient
47 encoding and decoding routines and is freely available. Effort has
48 been made to make the generated encoders and decoders relatively easy
49 to fit into different software environments.
51 The table driven encoders are useful for certain applications such as
52 protocol testing. They are also useful if you need to dynamically
53 load new ASN
.1 definitions. It is also fairly simple to write your
54 own special ASN
.1 tools based on tables (e.g. a protocol tester that
55 verifies that values conform to a given ASN
.1 type definition). The
56 price of the flexibility is speed; they are slower (~
4 times) than the
57 compiled C and C++ versions.
59 Some of snacc's features include:
61 \item {parses CCITT ASN
.1 '
90 including subtype notation
}
62 \item {can compile and link inter-dependent ASN
.1 modules (IMPORTS/EXPORTS)
}
63 \item {some X
.400 and SNMP macros are parsed
}
65 \item {macro
{\em definitions
} do not generate syntax errors but are
66 are not processed. The macro definitions are retained as a string
67 internally (if you want to modify the compiler to process them).
}
69 \item {value notation is parsed. OBJECT IDENTIFIERs, INTEGERs
70 and BOOLEANs are translated to C/C++ values. Any other value in \
{\
}'s
71 is kept as a string internally (if you want to modify the compiler to
74 \item {optionally supports ``;'' separated type or value definitions
75 in the ASN
.1 source. This is useful for dealing with some macros and
76 other language ambiguities that introduce parsing problems.
}
77 \item {ANY DEFINED BY types are supported via the SNMP OBJECT-TYPE macro
}
80 \section{\label{old-install-section
}Installing snacc
}
82 First of all, if you haven't already done so, un-archive snacc to
83 produce the directory
{\em snacc
} and its contents. The following tools
84 are required to compile snacc:
86 \item {\verb$lex$ or GNU's
\verb$flex$ (
\verb$flex$ is recommended)
}
87 \item {\verb$yacc$ or GNU's
\verb$bison$ (
\verb$bison$ is recommended)
}
88 \item {a C compiler and
\verb$make$
}
91 Some versions of
\verb$yacc$ will choke due to the large size of the
92 \verb$asn1.yacc$ file, however, I have had no problems with
\verb$bison$.
93 Our
\verb$yacc$ grammar for ASN
.1 has
61 shift/reduce errors and
2
94 reduce/reduce errors. Most of these errors were introduced when
95 certain macros were added to the compiler. Some of the shift/reduce
96 errors will require you to follow the offending macro in the ASN
.1
97 module with a semi-colon. The reduce/reduce errors were introduced by
98 macros that have ``Type or Value Lists'' because the NULL Type and
99 NULL values use the same symbol, ``NULL''. This is not a problem
100 since no real processing is done with the macros in question at the
103 \verb$Lex$ will work for the
\verb$asn1.lex$ file but
\verb$flex$ will typically
104 produce a smaller executable. Most versions of
\verb$lex$ have a small
105 maximum token size that will cause problems for long tokens in the
106 ASN
.1 source files, such as quoted strings. To avoid this problem,
107 increase the
\verb$YYLMAX$ value in the generated
{\em lex.yy.c
} file to at least
108 2048.
\verb$Flex$ does not seem to have this problem.
110 The compiler and library C code has been written to support ANSI or
111 non-ANSI C\@. ANSI C is used by default; this can be configured in
112 \verb$snacc/c_include/asn_config.h$.
114 By default, the compiler's makefiles use
\verb$flex$,
\verb$bison$ and
115 \verb$gcc$. If you wish to change these, edit the following files:
119 snacc/src/c_lib/makefile
120 snacc/src/back_ends/c_gen/makefile
121 snacc/src/back_ends/c++_gen/makefile
124 The C runtime library uses
\verb$gcc$, and its makefile is
125 \verb$snacc/c_lib/makefile$. The C++ runtime library uses
\verb$g++$
126 (
\verb$gcc-
2.2.3$) and its makefile is
\verb$snacc/c++_lib/makefile$.
127 The type table library makefile uses
\verb$gcc$ and is
128 \verb$snacc/tbl_lib/makefile$.
130 Finally, to compile
\verb$snacc$ and the C and C++ runtime libraries,
131 type the following at the shell prompt:
138 If you wish to install only the C (including type tables) or only the
139 C++ versions of the library, type
\verb$make c$ or
\verb$make c++$,
140 respectively, instead of
\verb$make all$. If the make succeeds, the
141 snacc binary,
{\em snacc
}, should be in the
\verb$snacc/bin/$
142 directory, the C runtime libraries,
{\em libasn1csbuf.a
},
{\em
143 libasn1cebuf.a
}, and
{\em libasn1cmbuf.a
}, should be in the
144 \verb$snacc/c_lib$ and the C++ runtime library,
{\em libasn1c++.a
}
145 should be in the
\verb$snacc/c++_lib$. The type table library,
{\em
146 libasn1tbl.a
} will be in
\verb$snacc/tbl_lib$. The type table tools,
147 {\em ptbl
},
{\em pval
} and
{\em mkchdr
} will be in
\verb$snacc/bin$.
148 The
\verb$.o$ and other junk files will have been removed.
150 After compiling the libraries, you can test the library routines with
151 \linebreak \verb$snacc/c_examples/test_lib/test_lib$ or
\linebreak
152 \verb$snacc/c++_examples/test_lib/test_lib$. These programs run
153 simple encoding and decoding tests on all of the library types. You
154 can test the snacc compiler with the other examples.
156 A manual page that contains information on running snacc can be found in
157 \verb$snacc/doc/snacc
.1$. This should be installed in section~
1 of
158 the manual. You can use
\verb$nroff -man snacc
.1$ to view it if you
159 don't want to install it.
162 \section{\label{old-run-section
}Running snacc
}
164 Snacc is typically invoked from the shell command line and has the synopsis:
166 snacc
[-h
] [-P
] [-t
] [-e
] [-d
] [-p
] [-f
]
167 [ -c | -C | -T <table output file>
]
168 [-u <useful types ASN
.1 file>
]
169 [-mf <max file name len>
]
174 Snacc generates C or C++ source code for BER encode and decode
175 routines as well as print and free routines for each type in the given
176 ASN
.1 modules. Alternatively, snacc can produce type tables that can
177 be used for table based/interpreted encoding and decoding. The type
178 table based methods tend to be slower than their C or C++ counterparts
179 but they tend use less memory (table size vs. C/C++ object code).
181 Most of the
1990~ASN
.1 features are parsed although some do not affect
182 the generated code. Fairly rigourous error checking is performed on
183 the ASN
.1 source; any errors detected will be reported (printed to
186 Each file in the ASN
.1 file list should contain a complete ASN
.1
187 module. ASN
.1 modules that use the IMPORTS feature must be compiled
188 together (specify all necessary modules in the ASN
.1 file list). The
189 generated source files will include each module's header file in the
190 command line order. This makes it important to order the modules from
191 least dependent to most dependent on the command line to avoid type
192 ordering problems. Currently, snacc assumes that each ASN
.1 file
193 given on the command line depends on all of the others on the command
194 line. No attempt is made to only include the header files from
195 modules referenced in the import list for that module.
197 If the target language is C, snacc will generate a
\verb$.h$ and
198 \verb$.c$ file for each specified ASN
.1 module. If the target
199 language is C++, snacc will generate a
\verb$.h$ and
\verb$.C$ file
200 for each module. The generated file names will be derived from the
203 The command line options are:
204 % zap bullet for items
208 \item[-h
] {short for ``help'', prints a synopsis of snacc
211 \item[-c
] {causes snacc to generate C source code.
212 This is the default behaviour of snacc if neither of the
\verb$-c$ or
213 \verb$-C$ options are given. Only one of the
\verb$-c$,
\verb$-C$ or
214 \verb$-T$ options should be specified.
}
216 \item[-C
] {causes snacc to generate C++ source code.
}
218 \item[-T
{\em file
}] {causes snacc to generate type tables and
219 write them to the given file
{\em file
}.
}
221 \item[-P
] {causes snacc to print the parsed ASN
.1
222 modules to
\verb$stdout$ after the types have been linked, sorted, and
223 processed. This option is useful for debugging snacc and observing
224 the modifications snacc performs on the types to make code generation
228 The options,
\verb$-t, -v, -e, -d, -p,$ and
\verb$-f$ affect
229 what types and routines go into the generated source code.
230 These options do not affect type table generation. If none of
231 them are given on the command line, snacc assumes that all of them are
232 in effect. For example, if you do not need the Free or Print
233 routines, you should give the
\verb$-t -v -e -d$ options to snacc.
234 This lets you trim the size of the generated code by removing
235 unnecessary routines; the code generated from large ASN
.1
236 specifications can produce very large binaries.
239 \item[-t
] {causes snacc to generate type definitions in the
240 target language for each ASN
.1 type.
}
242 \item[-v
] {causes snacc to generate value definitions in the
243 target language for each ASN
.1 value. Currently value definitions are
244 limited to INTEGERs, BOOLEANs and OBJECT IDENTIFIERs.
}
246 \item[-e
] {causes snacc to generate encode routines in the
247 target language for each ASN
.1 type.
}
249 \item[-d
] {causes snacc to generate decode routines in the
250 target language for each ASN
.1 type.
}
252 \item[-p
] {causes snacc to generate print routines in the
253 target language for each ASN
.1 type.
}
255 \item[-f
] {causes snacc to generate free routines in the
256 target language for each ASN
.1 type. This option only works when the
257 target language is C\@. The free routines hierarchically free C values.
258 A more efficient approach is to use the provided nibble-memory system.
259 The nibble memory permits freeing an entire decoded value without
260 traversing the decoded value. This is the default memory allocator
261 used by snacc generated decoders. See file
262 \verb$snacc/c_include/asn_config.h$ to change the default memory
263 system. For more information on the memory management see Section~
\ref{lib-mem-C-section
}.
}
265 \item[-u
{\em file
}] {causes snacc to read the useful types
266 definitions from the ASN
.1 module in file
{\em file
}
267 for linking purposes. For some ASN
.1 specifications, such as SNMP,
268 the useful types are not needed. The types in the given useful types
269 file are globally available to all modules; a useful type definition
270 is overridden by a local or explicitly imported type with the same
271 name. The useful type module can be found in
272 \verb$snacc/asn1specs/asn-useful.asn1$ and contains:
276 \setlength{\itemsep}{0pt
}
277 \setlength{\parsep}{0pt
}
279 \item ObjectDesccriptor
281 \item PrintableString
290 \item GeneralizedTime
295 \item[-mf
{\em number
}] {causes the names of the generated source
296 files to have a maximum length of
{\em number
} characters, including
297 their suffix. The
{\em number
} argument must be at least~
3. This option
298 is useful for supporting operating systems that only support short
299 file names. A better solution is to shorten the module name of each
302 \item[-l
{\em number
}] {this is fairly obscure but may be useful. Each
303 error that the decoders can
report is given an id number. The number
304 {\em number
} is where the error ids start decreasing from as they are
305 assigned to errors. The default is -
100 if this option is not given.
306 Avoid using a number in the range -
100 to
0 since they may conflict
307 with the library routines' error ids. If you are re-compiling the
308 useful types for the library use -
50. Another use of this option is
309 to integrate newly generated code with older code; if done correctly,
310 the error ids will not conflict.
}
314 Since ASN
.1 has different scoping rules than C and C++, some name munging
315 is done for types, named-numbers etc. to eliminate conflicts.
316 Some capitalization schemes were chosen to fit common C programming
317 style. For all names, dashes in the ASN
.1 source are converted to
318 underscores. See Sections
\ref{naming-C-section
} and
\ref{naming-C++-section
}
319 for more naming information.
321 The module name is used as a base name for the generated source file
322 names. It will be put into lowercase and dashes will be replaced with
323 underscores. Module names that result in file names longer than
324 specified with the
\verb$-mf$ option will be truncated. If the
325 \verb$-mf$ option was not given, file names will be truncated if they
326 are too long for the target file system. You may want to shorten long
327 module names to meaningful abbreviations. This will avoid file name
328 conflicts for module names that are truncated to the same substring.
329 Any module name and file name conflicts will be reported.
331 If your ASN
.1 modules have syntactic or semantic errors, each error
332 will be printed to
\verb$stderr$ along with the file name and line number of
333 where it occurred. These errors are usable by GNU emacs compiling
334 tools. See the next chapter for more information on the types of
335 errors snacc can detect.
337 More errors can be detected and reported in a single compile if type
338 and value definitions are separated by semi-colons. Separating type
339 and value definitions with semi-colons is not required, and if used,
340 need not be used to separate all type and value definitions.
341 Semi-colons are necessary after some macros that introduce ambiguity.
342 In general, if you get a parse error you can't figure out, try
343 separating the surrounding type/value definitions with semicolons.
346 \subsection{Known Bugs
}
348 Snacc has problems with the following case:
354 val ANY DEFINED BY id
364 The error checking pass will print an error to the effect that the id
365 type must be INTEGER or OBJECT IDENTIFER\@. To fix this you must modify
366 the error checking pass as well as the code generation pass. To be
367 cheap about it, disable/fix the error checking and hand modify the
370 The hashing code used for handling ANY DEFINED BY id to type mappings
371 will encounter problems if the hash table goes more than four levels
372 deep (I think this is unlikely). To fix this just add linear chaining
375 On the deficiency side of things, the C++ classes really need to have
376 free methods defined for them. (Unless you have replaced new with
377 something like nibble memory)
379 \section{\label{old-bug-section
}Reporting Bugs and Your Own Improvements
}
381 This (
1.1) is the final release of snacc (I have finished my M.Sc).
382 Gerald Neufeld
\verb$<neufeld@cs.ubc.ca>$ was my supervisor but he
383 does not have time to deal with support (it is all my code anyway).
384 Luckily, a colleague has kindly offered to receive the bug reports and
385 to coordinate work done by others (i.e. you). His name is Barry
386 Brachman
\verb$<brachman@cs.ubc.ca>$. He did not write the code
387 (
35,
000+ lines of C) but he has used snacc for X
.500 work. He may be
388 able to point you to someone who has fixed or encountered the same
389 bug. Anyway, be nice to him, it's not his job.
391 Even though this is the second release of snacc, bugs are still
392 likely. In fact, this release was quite rushed so there are probably
393 lots of stupid installation bugs etc. If you find some bugs or have
394 other comments, please send email to
\verb$snacc-bugs@cs.ubc.ca$
395 (these will get to Barry and Gerald). Please include the offending
396 ASN
.1 source, the command line options you were using and the hardware
397 and operating system configuration.
399 If you are really keen and hack in new goodies, please share. Send
400 them to Barry or
\verb$snacc-bugs@cs.ubc.ca$. Look in
401 \verb$snacc/README.future$ for things you could work on.
403 As I mentioned, I have entered the real world. I am now working with
404 Open Systems Solutions (based in New Jersey). If your application
405 needs a commercially developed and supported ASN
.1 compiler, try
406 calling
1-
609-
987-
9073 (Yeah, I know this is a plug, but it's a good