summaryrefslogtreecommitdiff
path: root/src/kmrandgen.cpp
diff options
context:
space:
mode:
authortmccolm <tmccolm@cinecert.com>2006-06-23 19:48:03 +0000
committertmccolm <>2006-06-23 19:48:03 +0000
commitc589ee9d47d9f00aa4be32c5832a44ce466f014d (patch)
treed285585caea07384dc75c5cb2a697df821437f72 /src/kmrandgen.cpp
parent5e91ca52284adc91a42d6fe389c9cc70a33126a6 (diff)
2006/06/23 tmccolm
Diffstat (limited to 'src/kmrandgen.cpp')
-rw-r--r--src/kmrandgen.cpp248
1 files changed, 248 insertions, 0 deletions
diff --git a/src/kmrandgen.cpp b/src/kmrandgen.cpp
new file mode 100644
index 0000000..c953244
--- /dev/null
+++ b/src/kmrandgen.cpp
@@ -0,0 +1,248 @@
+/*
+Copyright (c) 2005-2006, John Hurst
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+ /*! \file kmrandgen.cpp
+ \version $Id$
+ \brief psuedo-random number generation utility
+ */
+
+#include "AS_DCP.h"
+#include <KM_fileio.h>
+#include <KM_prng.h>
+#include <ctype.h>
+
+using namespace Kumu;
+
+const ui32_t RandBlockSize = 16;
+const char* PACKAGE = "kmrandgen";
+
+// Increment the iterator, test for an additional non-option command line argument.
+// Causes the caller to return if there are no remaining arguments or if the next
+// argument begins with '-'.
+#define TEST_EXTRA_ARG(i,c) if ( ++i >= argc || argv[(i)][0] == '-' ) \
+ { \
+ fprintf(stderr, "Argument not found for option -%c.\n", (c)); \
+ return; \
+ }
+
+//
+void
+banner(FILE* stream = stdout)
+{
+ fprintf(stream, "\n\
+%s (asdcplib %s)\n\n\
+Copyright (c) 2003-2006 John Hurst\n\n\
+%s is part of the asdcp DCP tools package.\n\
+asdcplib may be copied only under the terms of the license found at\n\
+the top of every file in the asdcplib distribution kit.\n\n\
+Specify the -h (help) option for further information about %s\n\n",
+ PACKAGE, ASDCP::Version(), PACKAGE, PACKAGE);
+}
+
+//
+void
+usage(FILE* stream = stdout)
+{
+ fprintf(stream, "\
+USAGE: %s [-b|-c] [-n] [-s <size>]\n\
+\n\
+ %s [-h|-help] [-V]\n\
+\n\
+ -b - Output a stream of binary data\n\
+ -c - Output a C-language struct containing the values\n\
+ -h | -help - Show help\n\
+ -n - Suppress newlines\n\
+ -s <size> - Number of random bytes to generate (default 32, supplied value\n\
+ is rounded up to nearest multiple of 16)\n\
+ -v - Verbose. Prints informative messages to stderr\n\
+ -V - Show version information\n\
+\n\
+ NOTES: o There is no option grouping, all options must be distinct arguments.\n\
+ o All option arguments must be separated from the option by whitespace.\n\
+\n", PACKAGE, PACKAGE);
+}
+
+//
+class CommandOptions
+{
+ CommandOptions();
+
+public:
+ bool error_flag; // true if the given options are in error or not complete
+ bool no_newline_flag; //
+ bool c_array_flag; //
+ bool binary_flag; //
+ bool verbose_flag; // true if the verbose option was selected
+ bool version_flag; // true if the version display option was selected
+ bool help_flag; // true if the help display option was selected
+ ui32_t request_size;
+
+ //
+ CommandOptions(int argc, const char** argv) :
+ error_flag(true), no_newline_flag(false), c_array_flag(false), binary_flag(false),
+ verbose_flag(false), version_flag(false), help_flag(false), request_size(32)
+ {
+ ui32_t tmp_size = 0, diff = 0;
+
+ for ( int i = 1; i < argc; i++ )
+ {
+
+ if ( (strcmp( argv[i], "-help") == 0) )
+ {
+ help_flag = true;
+ continue;
+ }
+
+ if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
+ {
+ switch ( argv[i][1] )
+ {
+ case 'b': binary_flag = true; break;
+ case 'c': c_array_flag = true; break;
+ case 'n': no_newline_flag = true; break;
+ case 'h': help_flag = true; break;
+
+ case 's':
+ TEST_EXTRA_ARG(i, 's');
+ tmp_size = atoi(argv[i]);
+ diff = tmp_size % RandBlockSize;
+
+ if ( diff != 0 )
+ tmp_size += RandBlockSize - diff;
+
+ request_size = tmp_size;
+ break;
+
+ case 'v': verbose_flag = true; break;
+ case 'V': version_flag = true; break;
+
+ default:
+ fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
+ return;
+ }
+ }
+ else
+ {
+ fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
+ return;
+ }
+ }
+
+ if ( help_flag || version_flag )
+ return;
+
+ if ( binary_flag && c_array_flag )
+ {
+ fprintf(stderr, "Error, must use only one of -b and -c options.\n");
+ return;
+ }
+
+ error_flag = false;
+ }
+};
+
+
+//
+int
+main(int argc, const char** argv)
+{
+ CommandOptions Options(argc, argv);
+
+ if ( Options.version_flag )
+ banner();
+
+ if ( Options.help_flag )
+ usage();
+
+ if ( Options.version_flag || Options.help_flag )
+ return 0;
+
+ if ( Options.error_flag )
+ {
+ fprintf(stderr, "There was a problem. Type %s -h for help.\n", PACKAGE);
+ return 3;
+ }
+
+ FortunaRNG RandGen;
+ ByteString Buf(Kumu::Kilobyte);
+
+ if ( Options.verbose_flag )
+ fprintf(stderr, "Creating %d random values.\n", Options.request_size);
+
+ if ( Options.binary_flag )
+ {
+ for ( ui32_t i = 0; i < Options.request_size; i += Kumu::Kilobyte )
+ {
+ RandGen.FillRandom(Buf);
+ ui32_t write_size = ((i + Kumu::Kilobyte) > Options.request_size) ? Options.request_size - i : Kumu::Kilobyte;
+ fwrite((byte_t*)Buf.Data(), 1, write_size, stdout);
+ }
+ }
+ else if ( Options.c_array_flag )
+ {
+ byte_t* p = Buf.Data();
+ printf("byte_t rand_buf[%u] = {\n", Options.request_size);
+
+ while ( Options.request_size > 0 )
+ {
+ RandGen.FillRandom(p, RandBlockSize);
+ fputc(' ', stdout);
+
+ for ( ui32_t i = 0; i < RandBlockSize; i++ )
+ printf(" 0x%02x,", p[i]);
+
+ fputc('\n', stdout);
+ Options.request_size -= RandBlockSize;
+ }
+
+ fputs("};", stdout);
+
+ if ( ! Options.no_newline_flag )
+ fputc('\n', stdout);
+ }
+ else
+ {
+ char hex_buf[64];
+ byte_t* p = Buf.Data();
+
+ for ( ui32_t i = 0; i < Options.request_size; i += RandBlockSize )
+ {
+ RandGen.FillRandom(p, RandBlockSize);
+ bin2hex(p, RandBlockSize, hex_buf, 64);
+ fputs(hex_buf, stdout);
+
+ if ( ! Options.no_newline_flag )
+ fputc('\n', stdout);
+ }
+ }
+
+ return 0;
+}
+
+
+//
+// end kmrandgen.cpp
+//