librnr  1.14.5
RoadNarrows Robotics Common Library 1
example_checksum.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Common Library 1
4 //
5 // File: example_checksum.c
6 //
7 /*! \file
8  *
9  * $LastChangedDate: 2013-03-05 09:45:13 -0700 (Tue, 05 Mar 2013) $
10  * $Rev: 2729 $
11  *
12  * \brief Example of librnr support for checksum algorithms.
13  *
14  * \author Robin Knight (robin.knight@roadnarrows.com)
15  *
16  * \pkgcopyright{2013-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
17  */
18 // Permission is hereby granted, without written agreement and without
19 // license or royalty fees, to use, copy, modify, and distribute this
20 // software and its documentation for any purpose, provided that
21 // (1) The above copyright notice and the following two paragraphs
22 // appear in all copies of the source code and (2) redistributions
23 // including binaries reproduces these notices in the supporting
24 // documentation. Substantial modifications to this software may be
25 // copyrighted by their authors and need not follow the licensing terms
26 // described here, provided that the new terms are clearly indicated in
27 // all files where they apply.
28 //
29 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
30 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
31 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
32 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
33 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
34 // THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
37 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
38 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
39 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
40 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
41 //
42 ////////////////////////////////////////////////////////////////////////////////
43 
44 #include <stdio.h>
45 #include <libgen.h>
46 #include <time.h>
47 #include <string.h>
48 
49 #include "rnr/rnrconfig.h"
50 #include "rnr/log.h"
51 #include "rnr/new.h"
52 #include "rnr/opts.h"
53 #include "rnr/checksum.h"
54 
55 #include "version.h"
56 
57 //
58 // The command with option and argument values.
59 //
60 static char *Argv0; ///< the command
61 static bool_t OptsPrintPoem = false; ///< do [not] print poem
62 
63 /*!
64  * \brief Program information.
65  */
67 {
68  .synopsis = "Example of librnr checksum algorithms.",
69 
70  .long_desc =
71  "The %P command runs serveral checksum algorithms over a fixed body of "
72  "text and prints the results to stdout."
73 };
74 
75 /*!
76  * \brief Command line options information.
77  */
78 static OptsInfo_T OptsInfo[] =
79 {
80  // --poem
81  {
82  .long_opt = "poem",
83  .short_opt = OPTS_NO_SHORT,
84  .has_arg = no_argument,
85  .has_default = true,
86  .opt_addr = &OptsPrintPoem,
87  .fn_cvt = OptsCvtArgBool,
88  .fn_fmt = OptsFmtBool,
89  .arg_name = NULL,
90  .opt_desc = "Do [not] print poem."
91  },
92 
93  {NULL, }
94 };
95 
96 /*!
97  * \brief Main initialization.
98  *
99  * \param argc Command-line argument count.
100  * \param argv Command-line argument list.
101  *
102  * \return Returns 1 on success, exits on failure.
103  */
104 static int init(int argc, char *argv[])
105 {
106  // name of this process
107  Argv0 = basename(argv[0]);
108 
109  // parse input options
110  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
111 
112  return 1;
113 }
114 
115 /*!
116  * \brief Jabberwocky by Lewis Carroll
117  */
118 static char *Jabberwocky =
119  "Jabberwocky -- Lewis Carroll\n"
120  "\n"
121  "'Twas brillig, and the slithy toves\n"
122  "Did gyre and gimble in the wabe;\n"
123  "All mimsy were the borogoves,\n"
124  "And the mome raths outgrabe.\n"
125  "\n"
126  "\"Beware the Jabberwock, my son!\n"
127  "The jaws that bite, the claws that catch!\n"
128  "Beware the Jubjub bird, and shun\n"
129  "The frumious Bandersnatch!\"\n"
130  "\n"
131  "He took his vorpal sword in hand:\n"
132  "Long time the manxome foe he sought—\n"
133  "So rested he by the Tumtum tree,\n"
134  "And stood awhile in thought.\n"
135  "\n"
136  "And as in uffish thought he stood,\n"
137  "The Jabberwock, with eyes of flame,\n"
138  "Came whiffling through the tulgey wood,\n"
139  "And burbled as it came!\n"
140  "\n"
141  "One, two! One, two! and through and through\n"
142  "The vorpal blade went snicker-snack!\n"
143  "He left it dead, and with its head\n"
144  "He went galumphing back.\n"
145  "\n"
146  "\"And hast thou slain the Jabberwock?\n"
147  "Come to my arms, my beamish boy!\n"
148  "O frabjous day! Callooh! Callay!\"\n"
149  "He chortled in his joy.\n"
150  "\n"
151  "'Twas brillig, and the slithy toves\n"
152  "Did gyre and gimble in the wabe;\n"
153  "All mimsy were the borogoves,\n"
154  "And the mome raths outgrabe.\n";
155 
156 /*!
157  * \brief Start nano second timer.
158  *
159  * \param [out] pstart Pointer to start timespec.
160  */
161 static void starttimer(struct timespec *pstart)
162 {
163  clock_gettime(CLOCK_REALTIME, pstart);
164 }
165 
166 /*!
167  * \brief Stop nano second timer.
168  *
169  * \param [in] pstart Pointer to start timespec.
170  *
171  * \return Elaspse time in nano seconds.
172  */
173 static long stoptimer(struct timespec *pstart)
174 {
175  struct timespec tstop;
176  long nsec;
177 
178  clock_gettime(CLOCK_REALTIME, &tstop);
179 
180  nsec = (tstop.tv_sec - pstart->tv_sec) * 1000000000;
181 
182  if( tstop.tv_nsec >= pstart->tv_nsec )
183  {
184  nsec += (tstop.tv_nsec - pstart->tv_nsec);
185  }
186  else
187  {
188  nsec += (1000000000 - pstart->tv_nsec + tstop.tv_nsec);
189  }
190 
191  return nsec;
192 }
193 
194 /*!
195  * \brief Profile the various checksum algorithms.
196  *
197  * \param sText Null-terminated text string.
198  */
199 static void profile(char *sText)
200 {
201  struct timespec t;
202  uint_t chksum;
203  long nsec;
204 
205  printf("Algorithm Result nSecs\n");
206 
207  starttimer(&t);
208  chksum = (uint_t)generate_checksum8((byte_t *)sText, strlen(sText));
209  nsec = stoptimer(&t);
210  printf(" 8-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
211 
212  starttimer(&t);
213  chksum = (uint_t)generate_checksum16((byte_t *)sText, strlen(sText));
214  nsec = stoptimer(&t);
215  printf("16-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
216 
217  starttimer(&t);
218  chksum = (uint_t)generate_checksum32((byte_t *)sText, strlen(sText));
219  nsec = stoptimer(&t);
220  printf("32-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
221 
222  starttimer(&t);
223  chksum = (uint_t)generate_crc32((byte_t *)sText, strlen(sText));
224  nsec = stoptimer(&t);
225  printf("32-bit crc %10u 0x%08x %6ld\n", chksum, chksum, nsec);
226 }
227 
228 /*!
229  * \brief Example main.
230  *
231  * \param argc Command-line argument count.
232  * \param argv Command-line argument list.
233  *
234  * \par Exit Status:
235  * Program exits with 0 success, \h_gt 0 on failure.
236  */
237 int main(int argc, char *argv[])
238 {
239  char *sText;
240  char c;
241  int k;
242  int i;
243 
244  if( !init(argc, argv) )
245  {
246  return 2;
247  }
248 
249  if( OptsPrintPoem )
250  {
251  printf("\n%s\n", Jabberwocky);
252  }
253 
254  //
255  // --- Profile 1
256  //
257  printf("Text: Jabberwocky -- Lewis Carroll\n");
258  printf("Length: %zu bytes\n", strlen(Jabberwocky));
259 
261 
262  printf("\n");
263 
264  //
265  // --- Profile 2
266  //
267  sText = new_strdup(Jabberwocky);
268 
269  c = sText[0];
270  sText[0] = sText[1];
271  sText[1] = c;
272 
273  printf("Text: Jabberwocky, 1 byte swapped -- Lewis Carroll\n");
274  printf("Length: %zu bytes\n", strlen(sText));
275 
276  profile(sText);
277 
278  printf("\n");
279 
280  delete(sText);
281 
282  //
283  // --- Profile 3
284  //
285  k = 25;
286 
287  sText = new(strlen(Jabberwocky) * (size_t)k + (size_t)1);
288 
289  for(i=0, *sText=0; i<k; ++i)
290  {
291  strcat(sText, Jabberwocky);
292  }
293 
294  printf("Text: Jabberwocky * %d -- Lewis Carroll\n", k);
295  printf("Length: %zu bytes\n", strlen(sText));
296 
297  profile(sText);
298 
299  printf("\n");
300 
301  delete(sText);
302 
303  return 0;
304 }
static void starttimer(struct timespec *pstart)
Start nano second timer.
int OptsCvtArgBool(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options boolean argument to bool_t.
Definition: opts.c:1018
u32_t generate_checksum32(byte_t buf[], size_t len)
Computes the modular 32-bit checksum over buffer.
Definition: checksum.c:126
char * new_strdup(const char *s)
Duplicate a string.
Definition: new.c:176
Standard command-line options options and parsing.
Program Description Strings Info Structure.
Definition: opts.h:226
#define NULL
null pointer
Definition: rnrconfig.h:199
u32_t generate_crc32(byte_t buf[], size_t len)
Computes the 32-bit cyclic redundance check over buffer.
Definition: checksum.c:139
Memory allocation and deallocation declarations.
u32_t uint_t
32-bit unsigned integer
Definition: rnrconfig.h:181
static char * Argv0
the command
u16_t generate_checksum16(byte_t buf[], size_t len)
Computes the modular 16-bit checksum over buffer.
Definition: checksum.c:113
const char * synopsis
Simple program synopsis string.
Definition: opts.h:240
static OptsPgmInfo_T PgmInfo
Program information.
static const PkgInfo_T PkgInfo
Definition: version.h:45
#define OPTS_NO_SHORT
no short option equivalent
Definition: opts.h:99
static OptsInfo_T OptsInfo[]
Command line options information.
int bool_t
"boolean" T/F
Definition: rnrconfig.h:187
RoadNarrows Robotics common configuration file.
char ** OptsGet(const char *argv0, const PkgInfo_T *pPkgInfo, OptsPgmInfo_T *pPgmInfo, OptsInfo_T *pOptsInfo, bool_t bHasLogging, int *pargc, char *argv[])
Gets, validates, and sets all command line options.
Definition: opts.c:861
u8_t byte_t
8-bit byte
Definition: rnrconfig.h:177
Package version information.
static int init(int argc, char *argv[])
Main initialization.
int main(int argc, char *argv[])
Example main.
u8_t generate_checksum8(byte_t buf[], size_t len)
Computes the modular 8-bit checksum over buffer.
Definition: checksum.c:100
char * OptsFmtBool(char *buf, size_t buflen, void *pOptVal)
Boolean option value string formatter.
Definition: opts.c:1247
Checksum algorithms.
static bool_t OptsPrintPoem
do [not] print poem
Logger declarations.
static long stoptimer(struct timespec *pstart)
Stop nano second timer.
const char * long_opt
Long option string name.
Definition: opts.h:137
Short and Long Options Info.
Definition: opts.h:134
static char * Jabberwocky
Jabberwocky by Lewis Carroll.
static void profile(char *sText)
Profile the various checksum algorithms.