2 Copyright (c) 2005-2006, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /*! \file kmrandgen.cpp
29 \brief psuedo-random number generation utility
33 #include <KM_fileio.h>
39 const ui32_t RandBlockSize = 16;
40 const char* PACKAGE = "kmrandgen";
42 // Increment the iterator, test for an additional non-option command line argument.
43 // Causes the caller to return if there are no remaining arguments or if the next
44 // argument begins with '-'.
45 #define TEST_EXTRA_ARG(i,c) if ( ++i >= argc || argv[(i)][0] == '-' ) \
47 fprintf(stderr, "Argument not found for option -%c.\n", (c)); \
53 banner(FILE* stream = stdout)
57 Copyright (c) 2003-2006 John Hurst\n\n\
58 %s is part of the asdcp DCP tools package.\n\
59 asdcplib may be copied only under the terms of the license found at\n\
60 the top of every file in the asdcplib distribution kit.\n\n\
61 Specify the -h (help) option for further information about %s\n\n",
62 PACKAGE, ASDCP::Version(), PACKAGE, PACKAGE);
67 usage(FILE* stream = stdout)
70 USAGE: %s [-b|-c] [-n] [-s <size>]\n\
74 -b - Output a stream of binary data\n\
75 -c - Output a C-language struct containing the values\n\
76 -h | -help - Show help\n\
77 -n - Suppress newlines\n\
78 -s <size> - Number of random bytes to generate (default 32, supplied value\n\
79 is rounded up to nearest multiple of 16)\n\
80 -v - Verbose. Prints informative messages to stderr\n\
81 -V - Show version information\n\
83 NOTES: o There is no option grouping, all options must be distinct arguments.\n\
84 o All option arguments must be separated from the option by whitespace.\n\
85 \n", PACKAGE, PACKAGE);
94 bool error_flag; // true if the given options are in error or not complete
95 bool no_newline_flag; //
98 bool verbose_flag; // true if the verbose option was selected
99 bool version_flag; // true if the version display option was selected
100 bool help_flag; // true if the help display option was selected
104 CommandOptions(int argc, const char** argv) :
105 error_flag(true), no_newline_flag(false), c_array_flag(false), binary_flag(false),
106 verbose_flag(false), version_flag(false), help_flag(false), request_size(32)
108 ui32_t tmp_size = 0, diff = 0;
110 for ( int i = 1; i < argc; i++ )
113 if ( (strcmp( argv[i], "-help") == 0) )
119 if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
121 switch ( argv[i][1] )
123 case 'b': binary_flag = true; break;
124 case 'c': c_array_flag = true; break;
125 case 'n': no_newline_flag = true; break;
126 case 'h': help_flag = true; break;
129 TEST_EXTRA_ARG(i, 's');
130 tmp_size = atoi(argv[i]);
131 diff = tmp_size % RandBlockSize;
134 tmp_size += RandBlockSize - diff;
136 request_size = tmp_size;
139 case 'v': verbose_flag = true; break;
140 case 'V': version_flag = true; break;
143 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
149 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
154 if ( help_flag || version_flag )
157 if ( binary_flag && c_array_flag )
159 fprintf(stderr, "Error, must use only one of -b and -c options.\n");
170 main(int argc, const char** argv)
172 CommandOptions Options(argc, argv);
174 if ( Options.version_flag )
177 if ( Options.help_flag )
180 if ( Options.version_flag || Options.help_flag )
183 if ( Options.error_flag )
185 fprintf(stderr, "There was a problem. Type %s -h for help.\n", PACKAGE);
190 ByteString Buf(Kumu::Kilobyte);
192 if ( Options.verbose_flag )
193 fprintf(stderr, "Creating %d random values.\n", Options.request_size);
195 if ( Options.binary_flag )
197 for ( ui32_t i = 0; i < Options.request_size; i += Kumu::Kilobyte )
199 RandGen.FillRandom(Buf);
200 ui32_t write_size = ((i + Kumu::Kilobyte) > Options.request_size) ? Options.request_size - i : Kumu::Kilobyte;
201 fwrite((byte_t*)Buf.Data(), 1, write_size, stdout);
204 else if ( Options.c_array_flag )
206 byte_t* p = Buf.Data();
207 printf("byte_t rand_buf[%u] = {\n", Options.request_size);
209 while ( Options.request_size > 0 )
211 RandGen.FillRandom(p, RandBlockSize);
214 for ( ui32_t i = 0; i < RandBlockSize; i++ )
215 printf(" 0x%02x,", p[i]);
218 Options.request_size -= RandBlockSize;
223 if ( ! Options.no_newline_flag )
229 byte_t* p = Buf.Data();
231 for ( ui32_t i = 0; i < Options.request_size; i += RandBlockSize )
233 RandGen.FillRandom(p, RandBlockSize);
234 bin2hex(p, RandBlockSize, hex_buf, 64);
235 fputs(hex_buf, stdout);
237 if ( ! Options.no_newline_flag )