root/WizardsToolkit/trunk/utilities/cipher-info.c

Revision 245, 13.0 KB (checked in by cristy, 8 weeks ago)
Line 
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3%                                                                             %
4%                                                                             %
5%                                                                             %
6%                   CCCC  IIIII  PPPP   H   H  EEEEE  RRRR                    %
7%                  C        I    P   P  H   H  E      R   R                   %
8%                  C        I    PPPP   HHHHH  EEE    RRRR                    %
9%                  C        I    P      H   H  E      R R                     %
10%                   CCCC  IIIII  P      H   H  EEEEE  R  R                    %
11%                                                                             %
12%                                                                             %
13%                         IIIII  N   N  FFFFF   OOO                           %
14%                           I    NN  N  F      O   O                          %
15%                           I    N N N  FFF    O   O                          %
16%                           I    N  NN  F      O   O                          %
17%                         IIIII  N   N  F       OOO                           %
18%                                                                             %
19%                                                                             %
20%                       Identify Ciphertext Properties.                       %
21%                                                                             %
22%                              Software Design                                %
23%                                John Cristy                                  %
24%                                January 2008                                 %
25%                                                                             %
26%                                                                             %
27%  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
28%  dedicated to making software imaging solutions freely available.           %
29%                                                                             %
30%  You may not use this file except in compliance with the License.  You may  %
31%  obtain a copy of the License at                                            %
32%                                                                             %
33%    http://www.wizards-toolkit.org/script/license.php                        %
34%                                                                             %
35%  Unless required by applicable law or agreed to in writing, software        %
36%  distributed under the License is distributed on an "AS IS" BASIS,          %
37%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
38%  See the License for the specific language governing permissions and        %
39%  limitations under the License.                                             %
40%                                                                             %
41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42%
43%
44*/
45
46/*
47  Include declarations.
48*/
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <time.h>
53#include "wizard/studio.h"
54#include "wizard/WizardsToolkit.h"
55#include "wizard/blob-private.h"
56#include "wizard/exception-private.h"
57#if defined(__WINDOWS__)
58#include <windows.h>
59#endif
60#include "content.h"
61#include "utility_.h"
62
63/*
64%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65%                                                                             %
66%                                                                             %
67%                                                                             %
68+   C i p h e r I n f o C o m m a n d                                         %
69%                                                                             %
70%                                                                             %
71%                                                                             %
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73%
74%  CipherInfoCommand() reads one or more files and displays its cipher
75%  properties (i.e. enciphering algorithm, hash, etc.).
76%
77%  The format of the CipherInfoCommand method is:
78%
79%      WizardBooleanType CipherInfoCommand(int argc,char **argv,
80%        ExceptionInfo *exception)
81%
82%  A description of each parameter follows:
83%
84%    o argc: The number of elements in the argument vector.
85%
86%    o argv: A text array containing the command line arguments.
87%
88%    o exception: Return any errors or warnings in this structure.
89%
90*/
91
92static void CipherInfoUsage(void)
93{
94  static const char
95    *options[]=
96    {
97      "-debug events        display copious debugging information",
98      "-help                print program options",
99      "-list type           print a list of supported option arguments",
100      "-version             print version information",
101      (char *) NULL
102    };
103
104  const char
105    **p;
106
107  (void) fprintf(stdout,"Version: %s\n",GetWizardVersion(
108    (unsigned long *) NULL));
109  (void) fprintf(stdout,"Copyright: %s\n\n",GetWizardCopyright());
110  (void) fprintf(stdout,"Usage: %s [options ...] ciphertext [ ciphertext ... "
111    "]\n",GetClientName());
112  (void) fprintf(stdout,"\nWhere options include:\n");
113  for (p=options; *p != (char *) NULL; p++)
114    (void) fprintf(stdout,"  %s\n",*p);
115  Exit(0);
116}
117
118WizardExport WizardBooleanType CipherInfoCommand(int argc,char **argv,
119  ExceptionInfo *exception)
120{
121#define DestroyCipherInfo() \
122{ \
123  content_info=DestroyContentInfo(content_info); \
124  for (i=0; i < (long) argc; i++) \
125    argv[i]=DestroyString(argv[i]); \
126  argv=(char **) RelinquishWizardMemory(argv); \
127}
128#define ThrowCipherException(asperity,tag,context) \
129{ \
130  (void) ThrowWizardException(exception,GetWizardModule(),asperity,tag, \
131    context); \
132  DestroyCipherInfo(); \
133  return(WizardFalse); \
134}
135#define ThrowInvalidArgumentException(option,argument) \
136{ \
137  (void) ThrowWizardException(exception,GetWizardModule(),OptionError, \
138    "invalid argument: `%s': %s",argument,option); \
139  DestroyCipherInfo(); \
140  return(WizardFalse); \
141}
142
143  char
144    *option;
145
146  ContentInfo
147    *content_info;
148
149  register long
150    i;
151
152  WizardBooleanType
153    status;
154
155  /*
156    Parse command-line options.
157  */
158  if (argc < 2)
159    CipherInfoUsage();
160  status=ExpandFilenames(&argc,&argv);
161  content_info=AcquireContentInfo();
162  if (status == WizardFalse)
163    ThrowCipherException(ResourceError,"memory allocation failed: `%s'",
164      strerror(errno));
165  for (i=1; i < argc; i++)
166  {
167    option=argv[i];
168    if (*option == '-')
169      {
170        switch(*(option+1))
171        {
172          case 'd':
173          {
174            if (strcasecmp(option,"-debug") == 0)
175              {
176                LogEventType
177                  event_mask;
178
179                i++;
180                if (i == argc)
181                  ThrowCipherException(OptionError,"missing log event mask: "
182                    "`%s'",option);
183                event_mask=SetLogEventMask(argv[i]);
184                if (event_mask == UndefinedEvents)
185                  ThrowCipherException(OptionFatalError,"unrecognized log "
186                    "event type: `%s'",argv[i]);
187                break;
188              }
189            ThrowCipherException(OptionFatalError,"unrecognized option: "
190              "`%s'",option);
191            break;
192          }
193          case 'h':
194          {
195            if ((LocaleCompare("help",option+1) == 0) ||
196                (LocaleCompare("-help",option+1) == 0))
197              CipherInfoUsage();
198            ThrowCipherException(OptionFatalError,"unrecognized option: `%s'",
199              option);
200            break;
201          }
202          case 'l':
203          {
204            if (LocaleCompare(option,"-list") == 0)
205              {
206                long
207                  list;
208
209                if (*option == '+')
210                  break;
211                i++;
212                if (i == (long) argc)
213                  ThrowCipherException(OptionError,"missing list type: `%s'",
214                    option);
215                if (LocaleCompare(argv[i],"configure") == 0)
216                  {
217                    (void) ListConfigureInfo((FILE *) NULL,exception);
218                    Exit(0);
219                  }
220                list=ParseWizardOption(WizardListOptions,WizardFalse,argv[i]);
221                if (list < 0)
222                  ThrowCipherException(OptionFatalError,"unrecognized list "
223                    "type: `%s'",argv[i]);
224                (void) ListWizardOptions((FILE *) NULL,(WizardOption) list,
225                  exception);
226                Exit(0);
227              }
228            if (LocaleCompare("log",option+1) == 0)
229              {
230                if (*option == '+')
231                  break;
232                i++;
233                if ((i == (long) argc) ||
234                    (strchr(argv[i],'%') == (char *) NULL))
235                  ThrowCipherException(OptionFatalError,"missing argument: "
236                    "`%s'",option);
237                break;
238              }
239            ThrowCipherException(OptionFatalError,"unrecognized option: `%s'",
240              option);
241            break;
242          }
243          case 'v':
244          {
245            if (strcasecmp(option,"-version") == 0)
246              {
247                (void) fprintf(stdout,"Version: %s\n",GetWizardVersion(
248                  (unsigned long *) NULL));
249                (void) fprintf(stdout,"Copyright: %s\n\n",GetWizardCopyright());
250                exit(0);
251              }
252            ThrowCipherException(OptionFatalError,"unrecognized option: `%s'",
253              option);
254            break;
255          }
256          default:
257          {
258            ThrowCipherException(OptionFatalError,"unrecognized option: `%s'",
259              option);
260            break;
261          }
262        }
263        continue;
264      }
265    /*
266      Identify cipher properties.
267    */
268    content_info->content=ConstantString(argv[i]);
269    content_info->cipherblob=OpenBlob(content_info->content,ReadBinaryBlobMode,
270      WizardTrue,exception);
271    if (content_info->cipherblob != (BlobInfo *) NULL)
272      {
273        status=GetContentInfo(content_info,content_info->cipherblob,exception);
274        if (status != WizardFalse)
275          (void) PrintCipherProperties(content_info,stdout);
276        if (CloseBlob(content_info->cipherblob) != WizardFalse)
277          ThrowFileException(exception,FileError,content_info->content);
278      }
279  }
280  /*
281    Free resources.
282  */
283  DestroyCipherInfo();
284  return(status);
285}
286
287/*
288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289%                                                                             %
290%                                                                             %
291%                                                                             %
292%  M a i n                                                                    %
293%                                                                             %
294%                                                                             %
295%                                                                             %
296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297%
298%
299*/
300int main(int argc,char **argv)
301{
302  char
303    *option;
304
305  double
306    elapsed_time,
307    user_time;
308
309  ExceptionInfo
310    *exception;
311
312  register long
313    i;
314
315  TimerInfo
316    *timer;
317
318  unsigned int
319    iterations;
320
321  WizardBooleanType
322    regard_warnings,
323    status;
324
325  WizardsToolkitGenesis(*argv);
326  exception=AcquireExceptionInfo();
327  iterations=1;
328  status=WizardTrue;
329  regard_warnings=WizardFalse;
330  for (i=1; i < (long) (argc-1); i++)
331  {
332    option=argv[i];
333    if ((strlen(option) == 1) || ((*option != '-') && (*option != '+')))
334      continue;
335    if (LocaleCompare("bench",option+1) == 0)
336      iterations=(unsigned int) atol(argv[++i]);
337    if (LocaleCompare("debug",option+1) == 0)
338      (void) SetLogEventMask(argv[++i]);
339    if (LocaleCompare("regard-warnings",option+1) == 0)
340      regard_warnings=WizardTrue;
341  }
342  timer=(TimerInfo *) NULL;
343  if (iterations > 1)
344    timer=AcquireTimerInfo();
345  for (i=0; i < (long) iterations; i++)
346  {
347    status=CipherInfoCommand(argc,argv,exception);
348    if ((status == WizardFalse) ||
349        (GetExceptionSeverity(exception) != UndefinedException))
350      {
351        if ((GetExceptionSeverity(exception) < ErrorException) &&
352            (regard_warnings == WizardFalse))
353          status=WizardTrue;
354        CatchException(exception);
355      }
356  }
357  if (iterations > 1)
358    {
359      elapsed_time=GetElapsedTime(timer);
360      user_time=GetUserTime(timer);
361      (void) fprintf(stderr,"Performance: %ui %gips %0.3fu %ld:%02ld.%03ld\n",
362        iterations,1.0*iterations/elapsed_time,user_time,(long)
363        (elapsed_time/60.0+0.5),(long) floor(fmod(elapsed_time,60.0)),
364        (long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
365      timer=DestroyTimerInfo(timer);
366    }
367  exception=DestroyExceptionInfo(exception);
368  WizardsToolkitTerminus();
369  return(status == WizardFalse ? 1 : 0);
370}
Note: See TracBrowser for help on using the browser.