c953244c502323bbed048500cf5e9e7b2f699448
[asdcplib.git] / src / kmrandgen.cpp
1 /*
2 Copyright (c) 2005-2006, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
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.
15
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.
26 */
27   /*! \file    kmrandgen.cpp
28     \version $Id$
29     \brief   psuedo-random number generation utility
30   */
31
32 #include "AS_DCP.h"
33 #include <KM_fileio.h>
34 #include <KM_prng.h>
35 #include <ctype.h>
36
37 using namespace Kumu;
38
39 const ui32_t RandBlockSize = 16;
40 const char* PACKAGE = "kmrandgen";
41
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] == '-' ) \
46                                  { \
47                                    fprintf(stderr, "Argument not found for option -%c.\n", (c)); \
48                                    return; \
49                                  }
50
51 //
52 void
53 banner(FILE* stream = stdout)
54 {
55   fprintf(stream, "\n\
56 %s (asdcplib %s)\n\n\
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);
63 }
64
65 //
66 void
67 usage(FILE* stream = stdout)
68 {
69   fprintf(stream, "\
70 USAGE: %s [-b|-c] [-n] [-s <size>]\n\
71 \n\
72        %s [-h|-help] [-V]\n\
73 \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\
82 \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);
86 }
87
88 //
89 class CommandOptions
90 {
91   CommandOptions();
92
93 public:
94   bool   error_flag;      // true if the given options are in error or not complete
95   bool   no_newline_flag; // 
96   bool   c_array_flag;    // 
97   bool   binary_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
101   ui32_t request_size;
102
103  //
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)
107   {
108     ui32_t tmp_size = 0, diff = 0;
109
110     for ( int i = 1; i < argc; i++ )
111       {
112
113          if ( (strcmp( argv[i], "-help") == 0) )
114            {
115              help_flag = true;
116              continue;
117            }
118      
119         if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
120           {
121             switch ( argv[i][1] )
122               {
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;
127
128               case 's':
129                 TEST_EXTRA_ARG(i, 's');
130                 tmp_size = atoi(argv[i]);
131                 diff = tmp_size % RandBlockSize;
132
133                 if ( diff != 0 )
134                   tmp_size += RandBlockSize - diff;
135
136                 request_size = tmp_size;
137                 break;
138
139               case 'v': verbose_flag = true; break;
140               case 'V': version_flag = true; break;
141
142               default:
143                 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
144                 return;
145               }
146           }
147         else
148           {
149             fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
150             return;
151           }
152       }
153
154         if ( help_flag || version_flag )
155           return;
156
157         if ( binary_flag && c_array_flag )
158           {
159             fprintf(stderr, "Error, must use only one of -b and -c options.\n");
160             return;
161           }
162
163     error_flag = false;
164   }
165 };
166
167
168 //
169 int
170 main(int argc, const char** argv)
171 {
172   CommandOptions Options(argc, argv);
173
174    if ( Options.version_flag )
175     banner();
176
177   if ( Options.help_flag )
178     usage();
179
180   if ( Options.version_flag || Options.help_flag )
181     return 0;
182
183   if ( Options.error_flag )
184     {
185       fprintf(stderr, "There was a problem. Type %s -h for help.\n", PACKAGE);
186       return 3;
187     }
188
189   FortunaRNG    RandGen;
190   ByteString    Buf(Kumu::Kilobyte);
191
192   if ( Options.verbose_flag )
193     fprintf(stderr, "Creating %d random values.\n", Options.request_size);
194
195   if ( Options.binary_flag )
196     {
197       for ( ui32_t i = 0; i < Options.request_size; i += Kumu::Kilobyte )
198         {
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);
202         }
203     }
204   else if ( Options.c_array_flag )
205     {
206       byte_t* p = Buf.Data();
207       printf("byte_t rand_buf[%u] = {\n", Options.request_size);
208
209       while ( Options.request_size > 0 )
210         {
211           RandGen.FillRandom(p, RandBlockSize);
212           fputc(' ', stdout);
213
214           for ( ui32_t i = 0; i < RandBlockSize; i++ )
215             printf(" 0x%02x,", p[i]);
216
217           fputc('\n', stdout);
218           Options.request_size -= RandBlockSize;
219         }
220
221       fputs("};", stdout);
222
223       if ( ! Options.no_newline_flag )
224         fputc('\n', stdout);
225     }
226   else
227     {
228       char hex_buf[64];
229       byte_t* p = Buf.Data();
230
231       for ( ui32_t i = 0; i < Options.request_size; i += RandBlockSize )
232         {
233           RandGen.FillRandom(p, RandBlockSize);
234           bin2hex(p, RandBlockSize, hex_buf, 64);
235           fputs(hex_buf, stdout);
236
237           if ( ! Options.no_newline_flag )
238             fputc('\n', stdout);
239         }
240     }
241
242   return 0;
243 }
244
245
246 //
247 // end kmrandgen.cpp
248 //