This example_checksum page provides an example of librnr support for various checksum algorithms.
#include <stdio.h>
#include <libgen.h>
#include <time.h>
#include <string.h>
{
.
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."
};
{
{
.has_arg = no_argument,
.has_default = true,
.opt_desc = "Do [not] print poem."
},
};
static int init(
int argc,
char *argv[])
{
Argv0 = basename(argv[0]);
argv =
OptsGet(Argv0, &
PkgInfo, &PgmInfo, OptsInfo,
true, &argc, argv);
return 1;
}
"Jabberwocky -- Lewis Carroll\n"
"\n"
"'Twas brillig, and the slithy toves\n"
"Did gyre and gimble in the wabe;\n"
"All mimsy were the borogoves,\n"
"And the mome raths outgrabe.\n"
"\n"
"\"Beware the Jabberwock, my son!\n"
"The jaws that bite, the claws that catch!\n"
"Beware the Jubjub bird, and shun\n"
"The frumious Bandersnatch!\"\n"
"\n"
"He took his vorpal sword in hand:\n"
"Long time the manxome foe he sought—\n"
"So rested he by the Tumtum tree,\n"
"And stood awhile in thought.\n"
"\n"
"And as in uffish thought he stood,\n"
"The Jabberwock, with eyes of flame,\n"
"Came whiffling through the tulgey wood,\n"
"And burbled as it came!\n"
"\n"
"One, two! One, two! and through and through\n"
"The vorpal blade went snicker-snack!\n"
"He left it dead, and with its head\n"
"He went galumphing back.\n"
"\n"
"\"And hast thou slain the Jabberwock?\n"
"Come to my arms, my beamish boy!\n"
"O frabjous day! Callooh! Callay!\"\n"
"He chortled in his joy.\n"
"\n"
"'Twas brillig, and the slithy toves\n"
"Did gyre and gimble in the wabe;\n"
"All mimsy were the borogoves,\n"
"And the mome raths outgrabe.\n";
{
clock_gettime(CLOCK_REALTIME, pstart);
}
static long stoptimer(
struct timespec *pstart)
{
struct timespec tstop;
long nsec;
clock_gettime(CLOCK_REALTIME, &tstop);
nsec = (tstop.tv_sec - pstart->tv_sec) * 1000000000;
if( tstop.tv_nsec >= pstart->tv_nsec )
{
nsec += (tstop.tv_nsec - pstart->tv_nsec);
}
else
{
nsec += (1000000000 - pstart->tv_nsec + tstop.tv_nsec);
}
return nsec;
}
{
struct timespec t;
long nsec;
printf("Algorithm Result nSecs\n");
printf(" 8-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
printf("16-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
printf("32-bit chksum %10u 0x%08x %6ld\n", chksum, chksum, nsec);
printf("32-bit crc %10u 0x%08x %6ld\n", chksum, chksum, nsec);
}
int main(
int argc,
char *argv[])
{
char *sText;
char c;
int k;
int i;
{
return 2;
}
if( OptsPrintPoem )
{
printf("\n%s\n", Jabberwocky);
}
printf("Text: Jabberwocky -- Lewis Carroll\n");
printf("Length: %zu bytes\n", strlen(Jabberwocky));
printf("\n");
c = sText[0];
sText[0] = sText[1];
sText[1] = c;
printf("Text: Jabberwocky, 1 byte swapped -- Lewis Carroll\n");
printf("Length: %zu bytes\n", strlen(sText));
printf("\n");
delete(sText);
k = 25;
sText = new(strlen(Jabberwocky) * (size_t)k + (size_t)1);
for(i=0, *sText=0; i<k; ++i)
{
strcat(sText, Jabberwocky);
}
printf("Text: Jabberwocky * %d -- Lewis Carroll\n", k);
printf("Length: %zu bytes\n", strlen(sText));
printf("\n");
delete(sText);
return 0;
}