Example of librnr support for checksum algorithms.
More...
#include <stdio.h>
#include <libgen.h>
#include <time.h>
#include <string.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/new.h"
#include "rnr/opts.h"
#include "rnr/checksum.h"
#include "version.h"
Go to the source code of this file.
|
| static int | init (int argc, char *argv[]) |
| | Main initialization. More...
|
| |
| static void | starttimer (struct timespec *pstart) |
| | Start nano second timer. More...
|
| |
| static long | stoptimer (struct timespec *pstart) |
| | Stop nano second timer. More...
|
| |
| static void | profile (char *sText) |
| | Profile the various checksum algorithms. More...
|
| |
| int | main (int argc, char *argv[]) |
| | Example main. More...
|
| |
Example of librnr support for checksum algorithms.
- LastChangedDate
- 2013-03-05 09:45:13 -0700 (Tue, 05 Mar 2013)
- Rev
- 2729
- Author
- Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)
- Copyright
- © 2013-2018. RoadNarrows LLC..
http://www.roadnarrows.com
All Rights Reserved
Definition in file example_checksum.c.
| static int init |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
|
static |
Main initialization.
- Parameters
-
| argc | Command-line argument count. |
| argv | Command-line argument list. |
- Returns
- Returns 1 on success, exits on failure.
Definition at line 104 of file example_checksum.c.
References Argv0, OptsGet(), and PkgInfo.
Referenced by main().
107 Argv0 = basename(argv[0]);
static char * Argv0
the command
static OptsPgmInfo_T PgmInfo
Program information.
static const PkgInfo_T PkgInfo
static OptsInfo_T OptsInfo[]
Command line options information.
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.
| int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
Example main.
- Parameters
-
| argc | Command-line argument count. |
| argv | Command-line argument list. |
- Exit Status:
- Program exits with 0 success, > 0 on failure.
Definition at line 237 of file example_checksum.c.
References init(), Jabberwocky, new_strdup(), OptsPrintPoem, and profile().
244 if( !
init(argc, argv) )
257 printf(
"Text: Jabberwocky -- Lewis Carroll\n");
258 printf(
"Length: %zu bytes\n", strlen(
Jabberwocky));
273 printf(
"Text: Jabberwocky, 1 byte swapped -- Lewis Carroll\n");
274 printf(
"Length: %zu bytes\n", strlen(sText));
287 sText =
new(strlen(
Jabberwocky) * (size_t)k + (
size_t)1);
289 for(i=0, *sText=0; i<k; ++i)
294 printf(
"Text: Jabberwocky * %d -- Lewis Carroll\n", k);
295 printf(
"Length: %zu bytes\n", strlen(sText));
char * new_strdup(const char *s)
Duplicate a string.
static int init(int argc, char *argv[])
Main initialization.
static bool_t OptsPrintPoem
do [not] print poem
static char * Jabberwocky
Jabberwocky by Lewis Carroll.
static void profile(char *sText)
Profile the various checksum algorithms.
| static void profile |
( |
char * |
sText | ) |
|
|
static |
Profile the various checksum algorithms.
- Parameters
-
| sText | Null-terminated text string. |
Definition at line 199 of file example_checksum.c.
References generate_checksum16(), generate_checksum32(), generate_checksum8(), generate_crc32(), starttimer(), and stoptimer().
Referenced by main().
205 printf(
"Algorithm Result nSecs\n");
210 printf(
" 8-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
215 printf(
"16-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
220 printf(
"32-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
225 printf(
"32-bit crc %10u 0x%08x %6ld\n", chksum, chksum, nsec);
static void starttimer(struct timespec *pstart)
Start nano second timer.
u32_t generate_checksum32(byte_t buf[], size_t len)
Computes the modular 32-bit checksum over buffer.
u32_t generate_crc32(byte_t buf[], size_t len)
Computes the 32-bit cyclic redundance check over buffer.
u32_t uint_t
32-bit unsigned integer
u16_t generate_checksum16(byte_t buf[], size_t len)
Computes the modular 16-bit checksum over buffer.
u8_t generate_checksum8(byte_t buf[], size_t len)
Computes the modular 8-bit checksum over buffer.
static long stoptimer(struct timespec *pstart)
Stop nano second timer.
| static void starttimer |
( |
struct timespec * |
pstart | ) |
|
|
static |
Start nano second timer.
- Parameters
-
| [out] | pstart | Pointer to start timespec. |
Definition at line 161 of file example_checksum.c.
Referenced by profile().
163 clock_gettime(CLOCK_REALTIME, pstart);
| static long stoptimer |
( |
struct timespec * |
pstart | ) |
|
|
static |
Stop nano second timer.
- Parameters
-
| [in] | pstart | Pointer to start timespec. |
- Returns
- Elaspse time in nano seconds.
Definition at line 173 of file example_checksum.c.
Referenced by profile().
175 struct timespec tstop;
178 clock_gettime(CLOCK_REALTIME, &tstop);
180 nsec = (tstop.tv_sec - pstart->tv_sec) * 1000000000;
182 if( tstop.tv_nsec >= pstart->tv_nsec )
184 nsec += (tstop.tv_nsec - pstart->tv_nsec);
188 nsec += (1000000000 - pstart->tv_nsec + tstop.tv_nsec);
Initial value:=
{
{
.long_opt = "poem",
.has_arg = no_argument,
.has_default = true,
.opt_desc = "Do [not] print poem."
},
}
int OptsCvtArgBool(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options boolean argument to bool_t.
#define OPTS_NO_SHORT
no short option equivalent
char * OptsFmtBool(char *buf, size_t buflen, void *pOptVal)
Boolean option value string formatter.
static bool_t OptsPrintPoem
do [not] print poem
Command line options information.
Definition at line 78 of file example_checksum.c.
Initial value:=
{
.synopsis = "Example of librnr checksum algorithms.",
.long_desc =
"The %P command runs serveral checksum algorithms over a fixed body of "
"text and prints the results to stdout."
}
Program information.
Definition at line 66 of file example_checksum.c.