WizardsToolkit  1.0.7
option.c
Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                   OOO   PPPP   TTTTT  IIIII   OOO   N   N                   %
00006 %                  O   O  P   P    T      I    O   O  NN  N                   %
00007 %                  O   O  PPPP     T      I    O   O  N N N                   %
00008 %                  O   O  P        T      I    O   O  N  NN                   %
00009 %                   OOO   P        T    IIIII   OOO   N   N                   %
00010 %                                                                             %
00011 %                                                                             %
00012 %                       Wizard's Toolkit Option Methods                       %
00013 %                                                                             %
00014 %                             Software Design                                 %
00015 %                               John Cristy                                   %
00016 %                               March  2003                                   %
00017 %                                                                             %
00018 %                                                                             %
00019 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
00020 %  dedicated to making software imaging solutions freely available.           %
00021 %                                                                             %
00022 %  You may not use this file except in compliance with the License.  You may  %
00023 %  obtain a copy of the License at                                            %
00024 %                                                                             %
00025 %    http://www.wizards-toolkit.org/script/license.php                        %
00026 %                                                                             %
00027 %  Unless required by applicable law or agreed to in writing, software        %
00028 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00029 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00030 %  See the License for the specific language governing permissions and        %
00031 %  limitations under the License.                                             %
00032 %                                                                             %
00033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00034 %
00035 %
00036 */
00037 
00038 /*
00039   Include declarations.
00040 */
00041 #include "wizard/studio.h"
00042 #include "wizard/authenticate.h"
00043 #include "wizard/cipher.h"
00044 #include "wizard/entropy.h"
00045 #include "wizard/exception-private.h"
00046 #include "wizard/memory_.h"
00047 #include "wizard/mime.h"
00048 #include "wizard/mime-private.h"
00049 #include "wizard/option.h"
00050 #include "wizard/resource_.h"
00051 #include "wizard/string_.h"
00052 #include "wizard/token.h"
00053 
00054 /*
00055   Option declarations.
00056 */
00057 static const OptionInfo
00058   AuthenticateOptions[] =
00059   {
00060     { "Undefined", (ssize_t) UndefinedAuthenticate },
00061     { "Public", (ssize_t) PublicAuthenticateMethod },
00062     { "Secret", (ssize_t) SecretAuthenticateMethod },
00063     { (char *) NULL, UndefinedAuthenticate }
00064   },
00065   CipherOptions[] =
00066   {
00067     { "Undefined", (ssize_t) UndefinedCipher },
00068     { "AES", (ssize_t) AESCipher },
00069     { "None", (ssize_t) NoCipher },
00070     { "Serpent", (ssize_t) SerpentCipher },
00071     { "Twofish", (ssize_t) TwofishCipher },
00072     { (char *) NULL, UndefinedCipher }
00073   },
00074   CommandOptions[] =
00075   {
00076     { "+authenticate", 1L },
00077     { "-authenticate", 1L },
00078     { "+chunksize", 1L },
00079     { "-chunksize", 1L },
00080     { "+decipher", 0L },
00081     { "-decipher", 0L },
00082     { "+debug", 1L },
00083     { "-debug", 1L },
00084     { "+entropy", 1L },
00085     { "-entropy", 1L },
00086     { "+hash", 1L },
00087     { "-hash", 1L },
00088     { "+help", 0L },
00089     { "-help", 0L },
00090     { "+key", 1L },
00091     { "-key", 1L },
00092     { "+key-length", 1L },
00093     { "-key-length", 1L },
00094     { "+mac", 1L },
00095     { "-mac", 1L },
00096     { "+level", 1L },
00097     { "-level", 1L },
00098     { "+list", 1L },
00099     { "-list", 1L },
00100     { "+log", 1L },
00101     { "-log", 1L },
00102     { "+mode", 1L },
00103     { "-mode", 1L },
00104     { "+properties", 1L },
00105     { "-properties", 1L },
00106     { "+random", 1L },
00107     { "-random", 1L },
00108     { "+verbose", 0L },
00109     { "-verbose", 0L },
00110     { "+version", 1L },
00111     { "-version", 1L },
00112     { (char *) NULL, 0L }
00113   },
00114   DataTypeOptions[] =
00115   {
00116     { "Undefined", (ssize_t) UndefinedData },
00117     { "Byte", (ssize_t) ByteData },
00118     { "Long", (ssize_t) LongData },
00119     { "Short", (ssize_t) ShortData },
00120     { "String", (ssize_t) StringData },
00121     { (char *) NULL, (ssize_t) UndefinedData }
00122   },
00123   EndianOptions[] =
00124   {
00125     { "Undefined", (ssize_t) UndefinedEndian },
00126     { "LSB", (ssize_t) LSBEndian },
00127     { "MSB", (ssize_t) MSBEndian },
00128     { (char *) NULL, (ssize_t) UndefinedEndian }
00129   },
00130   EntropyLevelOptions[] =
00131   {
00132     { "0", 0L },
00133     { "1", 1L },
00134     { "2", 2L },
00135     { "3", 3L },
00136     { "4", 4L },
00137     { "5", 5L },
00138     { "6", 6L },
00139     { "7", 7L },
00140     { "8", 8L },
00141     { "9", 9L },
00142     { (char *) NULL, 0L }
00143   },
00144   EntropyOptions[] =
00145   {
00146     { "Undefined", (ssize_t) UndefinedEntropy },
00147     { "None", (ssize_t) NoEntropy },
00148     { "BZip", (ssize_t) BZIPEntropy },
00149     { "LZMA", (ssize_t) LZMAEntropy },
00150     { "Zip", (ssize_t) ZIPEntropy },
00151     { (char *) NULL, UndefinedEntropy }
00152   },
00153   KeyLengthOptions[] =
00154   {
00155     { "256", 256L },
00156     { "512", 512L },
00157     { "1024", 1024L },
00158     { "2048", 2048L },
00159     { (char *) NULL, 0L }
00160   },
00161   HashOptions[] =
00162   {
00163     { "Undefined", (ssize_t) UndefinedHash },
00164     { "CRC64", (ssize_t) CRC64Hash },
00165     { "MD5", (ssize_t) MD5Hash },
00166     { "None", (ssize_t) NoHash },
00167     { "SHA1", (ssize_t) SHA1Hash },
00168     { "SHA224", (ssize_t) SHA224Hash },
00169     { "SHA256", (ssize_t) SHA256Hash },
00170     { "SHA384", (ssize_t) SHA384Hash },
00171     { "SHA512", (ssize_t) SHA512Hash },
00172     { (char *) NULL, UndefinedHash }
00173   },
00174   ListOptions[] =
00175   {
00176     { "Authenticate", (ssize_t) WizardAuthenticateOptions },
00177     { "Cipher", (ssize_t) WizardCipherOptions },
00178     { "Command", (ssize_t) WizardCommandOptions },
00179     { "DataType", (ssize_t) WizardDataTypeOptions },
00180     { "Debug", (ssize_t) WizardDebugOptions },
00181     { "Endian", (ssize_t) WizardEndianOptions },
00182     { "Entropy", (ssize_t) WizardEntropyOptions },
00183     { "EntropyLevel", (ssize_t) WizardEntropyLevelOptions },
00184     { "Hash", (ssize_t) WizardHashOptions },
00185     { "KeyLength", (ssize_t) WizardKeyLengthOptions },
00186     { "List", (ssize_t) WizardListOptions },
00187     { "Mode", (ssize_t) WizardModeOptions },
00188     { (char *) NULL, (ssize_t) WizardUndefinedOptions }
00189   },
00190   LogWizardEventOptions[] =
00191   {
00192     { "All", (ssize_t) (AllEvents &~ TraceEvent) },
00193     { "Blob", (ssize_t) BlobEvent },
00194     { "Deprecate", (ssize_t) DeprecateEvent },
00195     { "Configure", (ssize_t) ConfigureEvent },
00196     { "Exception", (ssize_t) ExceptionEvent },
00197     { "Locale", (ssize_t) LocaleEvent },
00198     { "None", (ssize_t) NoEvents },
00199     { "Resource", (ssize_t) ResourceEvent },
00200     { "Trace", (ssize_t) TraceEvent },
00201     { "User", (ssize_t) UserEvent },
00202     { "Warning", (ssize_t) WarningEvent },
00203     { (char *) NULL, UndefinedEvents }
00204   },
00205   ModeOptions[] =
00206   {
00207     { "Undefined", (ssize_t) UndefinedMode },
00208     { "CBC", (ssize_t) CBCMode },
00209     { "CFB", (ssize_t) CFBMode },
00210     { "CTR", (ssize_t) CTRMode },
00211     { "ECB", (ssize_t) ECBMode },
00212     { "OFB", (ssize_t) OFBMode },
00213     { (char *) NULL, UndefinedMode }
00214   },
00215   ResourceOptions[] =
00216   {
00217     { "Undefined", (ssize_t) UndefinedResource },
00218     { "Area", (ssize_t) AreaResource },
00219     { "Disk", (ssize_t) DiskResource },
00220     { "File", (ssize_t) FileResource },
00221     { "Map", (ssize_t) MapResource },
00222     { "Memory", (ssize_t) MemoryResource },
00223     { (char *) NULL, (ssize_t) UndefinedResource }
00224   };
00225 
00226 /*
00227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00228 %                                                                             %
00229 %                                                                             %
00230 %                                                                             %
00231 %   G e t W i z a r d O p t i o n s                                           %
00232 %                                                                             %
00233 %                                                                             %
00234 %                                                                             %
00235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00236 %
00237 %  GetWizardOptions() returns a list of options.
00238 %
00239 %  The format of the GetWizardOptions method is:
00240 %
00241 %      const char **GetWizardOptions(const WizardOption option)
00242 %
00243 %  A description of each parameter follows:
00244 %
00245 %    o option: The option.
00246 %
00247 */
00248 
00249 static const OptionInfo *GetOptionInfo(const WizardOption option)
00250 {
00251   switch (option)
00252   {
00253     case WizardAuthenticateOptions: return(AuthenticateOptions);
00254     case WizardCipherOptions: return(CipherOptions);
00255     case WizardCommandOptions: return(CommandOptions);
00256     case WizardDataTypeOptions: return(DataTypeOptions);
00257     case WizardDebugOptions: return(LogWizardEventOptions);
00258     case WizardEndianOptions: return(EndianOptions);
00259     case WizardEntropyOptions: return(EntropyOptions);
00260     case WizardEntropyLevelOptions: return(EntropyLevelOptions);
00261     case WizardKeyLengthOptions: return(KeyLengthOptions);
00262     case WizardHashOptions: return(HashOptions);
00263     case WizardListOptions: return(ListOptions);
00264     case WizardLogEventOptions: return(LogWizardEventOptions);
00265     case WizardModeOptions: return(ModeOptions);
00266     case WizardResourceOptions: return(ResourceOptions);
00267     default: break;
00268   }
00269   return((const OptionInfo *) NULL);
00270 }
00271 
00272 WizardExport char **GetWizardOptions(const WizardOption option)
00273 {
00274   char
00275     **options;
00276 
00277   const OptionInfo
00278     *option_info;
00279 
00280   register ssize_t
00281     i;
00282 
00283   option_info=GetOptionInfo(option);
00284   if (option_info == (const OptionInfo *) NULL)
00285     return((char **) NULL);
00286   for (i=0; option_info[i].mnemonic != (const char *) NULL; i++) ;
00287   options=(char **) AcquireQuantumMemory((size_t) i+1UL,sizeof(*options));
00288   if (options == (char **) NULL)
00289     ThrowFatalException(ResourceFatalError,"unable to acquire string `%s'");
00290   for (i=0; option_info[i].mnemonic != (const char *) NULL; i++)
00291     options[i]=AcquireString(option_info[i].mnemonic);
00292   options[i]=(char *) NULL;
00293   return(options);
00294 }
00295 
00296 /*
00297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00298 %                                                                             %
00299 %                                                                             %
00300 %                                                                             %
00301 %     I s W i z a r d O p t i o n                                             %
00302 %                                                                             %
00303 %                                                                             %
00304 %                                                                             %
00305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00306 %
00307 %  IsWizardOption() returns WizardTrue if the option begins with a - or + and
00308 %  the first character that follows is alphanumeric.
00309 %
00310 %  The format of the IsWizardOption method is:
00311 %
00312 %      WizardBooleanType IsWizardOption(const char *option)
00313 %
00314 %  A description of each parameter follows:
00315 %
00316 %    o option: The option.
00317 %
00318 */
00319 WizardExport WizardBooleanType IsWizardOption(const char *option)
00320 {
00321   assert(option != (const char *) NULL);
00322   if ((*option != '-') && (*option != '+'))
00323     return(WizardFalse);
00324   if (strlen(option) == 1)
00325     return(WizardFalse);
00326   option++;
00327   if (isalpha((int) ((unsigned char) *option)) == 0)
00328     return(WizardFalse);
00329   return(WizardTrue);
00330 }
00331 
00332 /*
00333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00334 %                                                                             %
00335 %                                                                             %
00336 %                                                                             %
00337 %   L i s t W i z a r d O p t i o n s                                         %
00338 %                                                                             %
00339 %                                                                             %
00340 %                                                                             %
00341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00342 %
00343 %  ListWizardOptions() lists the contents of enumerated option type(s).
00344 %
00345 %  The format of the ListWizardOptions method is:
00346 %
00347 %      WizardBooleanType ListWizardOptions(FILE *file,const WizardOption option,
00348 %        ExceptionInfo *exception)
00349 %
00350 %  A description of each parameter follows:
00351 %
00352 %    o file:  list options to this file handle.
00353 %
00354 %    o option:  which option list to display.
00355 %
00356 %    o exception:  return any errors or warnings in this structure.
00357 %
00358 */
00359 WizardExport WizardBooleanType ListWizardOptions(FILE *file,
00360   const WizardOption option,ExceptionInfo *wizard_unused(exception))
00361 {
00362   const OptionInfo
00363     *option_info;
00364 
00365   register ssize_t
00366     i;
00367 
00368   if (file == (FILE *) NULL)
00369     file=stdout;
00370   option_info=GetOptionInfo(option);
00371   if (option_info == (const OptionInfo *) NULL)
00372     return(WizardFalse);
00373   for (i=0; option_info[i].mnemonic != (char *) NULL; i++)
00374   {
00375     if ((i == 0) && (strcmp(option_info[i].mnemonic,"Undefined") == 0))
00376       continue;
00377     (void) fprintf(file,"%s\n",option_info[i].mnemonic);
00378   }
00379   return(WizardTrue);
00380 }
00381 
00382 /*
00383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00384 %                                                                             %
00385 %                                                                             %
00386 %                                                                             %
00387 %   P a r s e W i z a r d O p t i o n                                         %
00388 %                                                                             %
00389 %                                                                             %
00390 %                                                                             %
00391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00392 %
00393 %  ParseWizardOption() parses an option string and returns an enumerated option
00394 %  type(s).
00395 %
00396 %  The format of the ParseWizardOption method is:
00397 %
00398 %      ssize_t ParseWizardOption(const WizardOption option,
00399 %        const WizardBooleanType list,const char *options)
00400 %
00401 %  A description of each parameter follows:
00402 %
00403 %    o option: The option.
00404 %
00405 %    o list: A option other than zero permits more than one option separated by
00406 %      commas.
00407 %   
00408 %    o options: One or more options separated by commas.
00409 %
00410 */
00411 WizardExport ssize_t ParseWizardOption(const WizardOption option,
00412   const WizardBooleanType list,const char *options)
00413 {
00414   char
00415     token[MaxTextExtent];
00416 
00417   const OptionInfo
00418     *option_info;
00419 
00420   ssize_t
00421     option_types;
00422 
00423   register char
00424     *q;
00425 
00426   register const char
00427     *p;
00428 
00429   register ssize_t
00430     i;
00431 
00432   WizardBooleanType
00433     negate;
00434 
00435   option_info=GetOptionInfo(option);
00436   if (option_info == (const OptionInfo *) NULL)
00437     return(-1);
00438   option_types=0;
00439   for (p=options; p != (char *) NULL; p=strchr(p,','))
00440   {
00441     while (((isspace((int) ((unsigned char) *p)) != 0) || (*p == ',')) &&
00442            (*p != '\0'))
00443       p++;
00444     if ((*p == '-') || (*p == '+'))
00445       p++;
00446     negate=(*p == '!') ? WizardTrue : WizardFalse;
00447     if (negate != WizardFalse)
00448       p++;
00449     q=token;
00450     while (((isspace((int) ((unsigned char) *p)) == 0) && (*p != ',')) &&
00451            (*p != '\0'))
00452     {
00453       if ((q-token) >= (MaxTextExtent-1))
00454         break;
00455       *q++=(*p++);
00456     }
00457     *q='\0';
00458     for (i=0; option_info[i].mnemonic != (char *) NULL; i++)
00459     {
00460       if (LocaleCompare(token,option_info[i].mnemonic) == 0)
00461         {
00462           if (*token == '!')
00463             option_types=option_types &~ option_info[i].type;
00464           else
00465             option_types=option_types | option_info[i].type;
00466           break;
00467         }
00468     }
00469     if ((option_info[i].mnemonic == (char *) NULL) &&
00470         ((strchr(token+1,'-') != (char *) NULL) || 
00471          (strchr(token+1,'_') != (char *) NULL)))
00472       {
00473         while ((q=strchr(token+1,'-')) != (char *) NULL)
00474           (void) CopyWizardString(q,q+1,MaxTextExtent-strlen(q));
00475         while ((q=strchr(token+1,'_')) != (char *) NULL)
00476           (void) CopyWizardString(q,q+1,MaxTextExtent-strlen(q));
00477         for (i=0; option_info[i].mnemonic != (char *) NULL; i++)
00478           if (LocaleCompare(token,option_info[i].mnemonic) == 0)
00479             {
00480               if (*token == '!')
00481                 option_types=option_types &~ option_info[i].type;
00482               else
00483                 option_types=option_types | option_info[i].type;
00484               break;
00485             }
00486       }
00487     if (option_info[i].mnemonic == (char *) NULL)
00488       return(-1);
00489     if (list == WizardFalse)
00490       break;
00491   }
00492   return(option_types);
00493 }
00494 
00495 /*
00496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00497 %                                                                             %
00498 %                                                                             %
00499 %                                                                             %
00500 %   W i z a r d O p t i o n T o M n e m o n i c                               %
00501 %                                                                             %
00502 %                                                                             %
00503 %                                                                             %
00504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00505 %
00506 %  WizardOptionToMnemonic() returns an enumerated option as a mnemonic.
00507 %
00508 %  The format of the WizardOptionToMnemonic method is:
00509 %
00510 %      const char *WizardOptionToMnemonic(const WizardOption option,
00511 %        const ssize_t type)
00512 %
00513 %  A description of each parameter follows:
00514 %
00515 %    o option: the option.
00516 %
00517 %    o type: one or more options separated by commas.
00518 %
00519 */
00520 WizardExport const char *WizardOptionToMnemonic(const WizardOption option,
00521   const ssize_t type)
00522 {
00523   const OptionInfo
00524     *option_info;
00525 
00526   register ssize_t
00527     i;
00528 
00529   option_info=GetOptionInfo(option);
00530   if (option_info == (const OptionInfo *) NULL)
00531     return((const char *) NULL);
00532   for (i=0; option_info[i].mnemonic != (const char *) NULL; i++)
00533     if (type == option_info[i].type)
00534       break;
00535   if (option_info[i].mnemonic == (const char *) NULL)
00536     return("undefined");
00537   return(option_info[i].mnemonic);
00538 }