0d9ed4c961db3b96b3d7160ed357533ad403c787
[libdcp.git] / asdcplib / src / asdcp-util.cpp
1 /*
2 Copyright (c) 2003-2012, 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    asdcp-util.cpp
28     \version $Id: asdcp-util.cpp,v 1.1 2012/02/03 19:49:57 jhurst Exp $       
29     \brief   Utility functions for working with AS-DCP files
30
31   This program provides utility features commonly useful in DCP workflows.
32
33   For more information about asdcplib, please refer to the header file AS_DCP.h
34 */
35
36 #include <KM_fileio.h>
37 #include <KM_prng.h>
38 #include <AS_DCP.h>
39 #include <openssl/sha.h>
40
41 using namespace Kumu;
42
43
44 //------------------------------------------------------------------------------------------
45 //
46 // command line option parser class
47
48 static const char* PROGRAM_NAME = "asdcp-util";  // program name for messages
49
50 // Increment the iterator, test for an additional non-option command line argument.
51 // Causes the caller to return if there are no remaining arguments or if the next
52 // argument begins with '-'.
53 #define TEST_EXTRA_ARG(i,c)                                             \
54   if ( ++i >= argc || argv[(i)][0] == '-' ) {                           \
55     fprintf(stderr, "Argument not found for option -%c.\n", (c));       \
56     return;                                                             \
57   }
58
59 //
60 void
61 banner(FILE* stream = stdout)
62 {
63   fprintf(stream, "\n\
64 %s (asdcplib %s)\n\n\
65 Copyright (c) 2003-2012 John Hurst\n\n\
66 asdcplib may be copied only under the terms of the license found at\n\
67 the top of every file in the asdcplib distribution kit.\n\n\
68 Specify the -h (help) option for further information about %s\n\n",
69           PROGRAM_NAME, ASDCP::Version(), PROGRAM_NAME);
70 }
71
72 //
73 void
74 usage(FILE* stream = stdout)
75 {
76   fprintf(stream, "\
77 USAGE: %s [-h|-help] [-V]\n\
78 \n\
79        %s -d <input-file>\n\
80 \n\
81        %s -g | -u\n\
82 \n\
83        %s -u\n\n",
84           PROGRAM_NAME, PROGRAM_NAME, PROGRAM_NAME, PROGRAM_NAME);
85
86   fprintf(stream, "\
87 Major modes:\n\
88   -d                - Calculate message digest of input file\n\
89   -g                - Generate a random 16 byte value to stdout\n\
90   -h | -help        - Show help\n\
91   -u                - Generate a random UUID value to stdout\n\
92   -V                - Show version information\n\
93 \n\
94   NOTES: o There is no option grouping, all options must be distinct arguments.\n\
95          o All option arguments must be separated from the option by whitespace.\n\n");
96 }
97
98 //
99 enum MajorMode_t
100 {
101   MMT_NONE,
102   MMT_GEN_ID,
103   MMT_GEN_KEY,
104   MMT_DIGEST,
105 };
106
107 //
108 class CommandOptions
109 {
110   CommandOptions();
111
112 public:
113   MajorMode_t mode;
114   bool   error_flag;     // true if the given options are in error or not complete
115   bool   version_flag;   // true if the version display option was selected
116   bool   help_flag;      // true if the help display option was selected
117   PathList_t filenames;  // list of filenames to be processed
118
119   //
120   CommandOptions(int argc, const char** argv) :
121     mode(MMT_NONE), error_flag(true), version_flag(false), help_flag(false)
122   {
123     for ( int i = 1; i < argc; ++i )
124       {
125
126         if ( (strcmp( argv[i], "-help") == 0) )
127           {
128             help_flag = true;
129             continue;
130           }
131          
132         if ( argv[i][0] == '-'
133              && ( isalpha(argv[i][1]) || isdigit(argv[i][1]) )
134              && argv[i][2] == 0 )
135           {
136             switch ( argv[i][1] )
137               {
138               case 'd': mode = MMT_DIGEST; break;
139               case 'g': mode = MMT_GEN_KEY; break;
140               case 'h': help_flag = true; break;
141               case 'u': mode = MMT_GEN_ID; break;
142               case 'V': version_flag = true; break;
143
144               default:
145                 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
146                 return;
147               }
148           }
149         else
150           {
151             if ( argv[i][0] != '-' )
152               {
153                 filenames.push_back(argv[i]);
154               }
155             else
156               {
157                 fprintf(stderr, "Unrecognized argument: %s\n", argv[i]);
158                 return;
159               }
160           }
161       }
162
163     if ( help_flag || version_flag )
164       return;
165     
166     if ( ( mode == MMT_DIGEST ) && filenames.empty() )
167       {
168         fputs("Option requires at least one filename argument.\n", stderr);
169         return;
170       }
171
172     if ( mode == MMT_NONE && ! help_flag && ! version_flag )
173       {
174         fputs("No operation selected (use one of -[dgu] or -h for help).\n", stderr);
175         return;
176       }
177
178     error_flag = false;
179   }
180 };
181
182 //
183 Result_t
184 digest_file(const std::string& filename)
185 {
186   FileReader Reader;
187   SHA_CTX Ctx;
188   SHA1_Init(&Ctx);
189   ByteString Buf(8192);
190
191   Result_t result = Reader.OpenRead(filename.c_str());
192
193   while ( ASDCP_SUCCESS(result) )
194     {
195       ui32_t read_count = 0;
196       result = Reader.Read(Buf.Data(), Buf.Capacity(), &read_count);
197
198       if ( result == RESULT_ENDOFFILE )
199         {
200           result = RESULT_OK;
201           break;
202         }
203
204       if ( ASDCP_SUCCESS(result) )
205         SHA1_Update(&Ctx, Buf.Data(), read_count);
206     }
207
208   if ( ASDCP_SUCCESS(result) )
209     {
210       const ui32_t sha_len = 20;
211       byte_t bin_buf[sha_len];
212       char sha_buf[64];
213       SHA1_Final(bin_buf, &Ctx);
214
215       fprintf(stdout, "%s %s\n",
216               base64encode(bin_buf, sha_len, sha_buf, 64),
217               filename.c_str());
218     }
219
220   return result;
221 }
222
223 //
224 int
225 main(int argc, const char** argv)
226 {
227   Result_t result = RESULT_OK;
228   char     str_buf[64];
229   CommandOptions Options(argc, argv);
230
231   if ( Options.version_flag )
232     banner();
233
234   if ( Options.help_flag )
235     usage();
236
237   if ( Options.version_flag || Options.help_flag )
238     return 0;
239
240   if ( Options.error_flag )
241     {
242       fprintf(stderr, "There was a problem. Type %s -h for help.\n",
243               PROGRAM_NAME);
244       return 3;
245     }
246
247   if ( Options.mode == MMT_GEN_KEY )
248     {
249       Kumu::FortunaRNG RNG;
250       byte_t bin_buf[ASDCP::KeyLen];
251
252       RNG.FillRandom(bin_buf, ASDCP::KeyLen);
253       printf("%s\n", Kumu::bin2hex(bin_buf, ASDCP::KeyLen, str_buf, 64));
254     }
255   else if ( Options.mode == MMT_GEN_ID )
256     {
257       UUID TmpID;
258       Kumu::GenRandomValue(TmpID);
259       printf("%s\n", TmpID.EncodeHex(str_buf, 64));
260     }
261   else if ( Options.mode == MMT_DIGEST )
262     {
263       PathList_t::iterator i;
264
265       for ( i = Options.filenames.begin();
266             i != Options.filenames.end() && ASDCP_SUCCESS(result); ++i )
267         result = digest_file(*i);
268     }
269   else
270     {
271       fprintf(stderr, "Unhandled mode: %d.\n", Options.mode);
272       return 6;
273     }
274
275   if ( ASDCP_FAILURE(result) )
276     {
277       fputs("Program stopped on error.\n", stderr);
278
279       if ( result != RESULT_FAIL )
280         {
281           fputs(result, stderr);
282           fputc('\n', stderr);
283         }
284
285       return 1;
286     }
287
288   return 0;
289 }
290
291
292 //
293 // end asdcp-util.cpp
294 //