| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 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 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | static 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 | |
|---|
| 118 | WizardExport 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 282 | |
|---|
| 283 | DestroyCipherInfo(); |
|---|
| 284 | return(status); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | int 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 | } |
|---|