2 Copyright (c) 2005-2008, 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-2008 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|-B|-c|-x] [-n] [-s <size>] [-v]\n\
74 -b - Output a stream of binary data\n\
75 -B - Output a Base64 string\n\
76 -c - Output a C-language struct containing the values\n\
77 -h | -help - Show help\n\
78 -n - Suppress newlines\n\
79 -s <size> - Number of random bytes to generate (default 32)\n\
80 -v - Verbose. Prints informative messages to stderr\n\
81 -V - Show version information\n\
82 -x - Output hexadecimal (default)\n\
84 NOTES: o There is no option grouping, all options must be distinct arguments.\n\
85 o All option arguments must be separated from the option by whitespace.\n\
86 \n", PACKAGE, PACKAGE);
102 bool error_flag; // true if the given options are in error or not complete
103 bool no_newline_flag; //
104 bool verbose_flag; // true if the verbose option was selected
105 bool version_flag; // true if the version display option was selected
106 bool help_flag; // true if the help display option was selected
107 OutputFormat_t format; //
111 CommandOptions(int argc, const char** argv) :
112 error_flag(true), no_newline_flag(false), verbose_flag(false),
113 version_flag(false), help_flag(false), format(OF_HEX), request_size(RandBlockSize*2)
115 for ( int i = 1; i < argc; i++ )
118 if ( (strcmp( argv[i], "-help") == 0) )
124 if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
126 switch ( argv[i][1] )
128 case 'b': format = OF_BINARY; break;
129 case 'B': format = OF_BASE64; break;
130 case 'c': format = OF_CSTRUCT; break;
131 case 'n': no_newline_flag = true; break;
132 case 'h': help_flag = true; break;
135 TEST_EXTRA_ARG(i, 's');
136 request_size = abs(atoi(argv[i]));
139 case 'v': verbose_flag = true; break;
140 case 'V': version_flag = true; break;
141 case 'x': format = OF_HEX; break;
144 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
150 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
155 if ( help_flag || version_flag )
158 if ( request_size == 0 )
160 fprintf(stderr, "Please use a non-zero request size\n");
171 main(int argc, const char** argv)
173 CommandOptions Options(argc, argv);
175 if ( Options.version_flag )
178 if ( Options.help_flag )
181 if ( Options.version_flag || Options.help_flag )
184 if ( Options.error_flag )
186 fprintf(stderr, "There was a problem. Type %s -h for help.\n", PACKAGE);
191 ByteString Buf(Kumu::Kilobyte);
193 if ( Options.verbose_flag )
194 fprintf(stderr, "Generating %d random byte%s.\n", Options.request_size, (Options.request_size == 1 ? "" : "s"));
196 if ( Options.format == OF_BINARY )
198 if ( KM_FAILURE(Buf.Capacity(Options.request_size)) )
200 fprintf(stderr, "randbuf: %s\n", RESULT_ALLOC.Label());
204 RandGen.FillRandom(Buf.Data(), Options.request_size);
205 fwrite((byte_t*)Buf.Data(), 1, Options.request_size, stdout);
207 else if ( Options.format == OF_CSTRUCT )
209 ui32_t line_count = 0;
210 byte_t* p = Buf.Data();
211 printf("byte_t rand_buf[%u] = {\n", Options.request_size);
213 if ( Options.request_size > 128 )
214 fputs(" // 0x00000000\n", stdout);
216 while ( Options.request_size > 0 )
218 if ( line_count > 0 && (line_count % (RandBlockSize*8)) == 0 )
219 fprintf(stdout, " // 0x%08x\n", line_count);
221 RandGen.FillRandom(p, RandBlockSize);
224 for ( ui32_t i = 0; i < RandBlockSize && Options.request_size > 0; i++, Options.request_size-- )
225 printf(" 0x%02x,", p[i]);
228 line_count += RandBlockSize;
233 if ( ! Options.no_newline_flag )
236 else if ( Options.format == OF_BASE64 )
238 if ( KM_FAILURE(Buf.Capacity(Options.request_size)) )
240 fprintf(stderr, "randbuf: %s\n", RESULT_ALLOC.Label());
245 ui32_t e_len = base64_encode_length(Options.request_size) + 1;
247 if ( KM_FAILURE(Strbuf.Capacity(e_len)) )
249 fprintf(stderr, "strbuf: %s\n", RESULT_ALLOC.Label());
253 RandGen.FillRandom(Buf.Data(), Options.request_size);
255 if ( base64encode(Buf.RoData(), Options.request_size, (char*)Strbuf.Data(), Strbuf.Capacity()) == 0 )
257 fprintf(stderr, "encode error\n");
261 fputs((const char*)Strbuf.RoData(), stdout);
263 if ( ! Options.no_newline_flag )
268 byte_t* p = Buf.Data();
271 while ( Options.request_size > 0 )
273 ui32_t x_len = xmin(Options.request_size, RandBlockSize);
274 RandGen.FillRandom(p, RandBlockSize);
275 bin2hex(p, x_len, hex_buf, 64);
276 fputs(hex_buf, stdout);
278 if ( ! Options.no_newline_flag )
281 Options.request_size -= x_len;