WizardsToolkit  1.0.7
entropy.c
Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %              EEEEE  N   N  TTTTT  RRRR    OOO   PPPP    Y   Y               %
00007 %              E      NN  N    T    R   R  O   O  P   P    Y Y                %
00008 %              EEE    N N N    T    RRRR   O   O  PPPP      Y                 %
00009 %              E      N  NN    T    R R    O   O  P         Y                 %
00010 %              EEEEE  N   N    T    R  R    OOO   P         Y                 %
00011 %                                                                             %
00012 %                                                                             %
00013 %                       Wizard's Toolkit Entropy Methods                      %
00014 %                                                                             %
00015 %                             Software Design                                 %
00016 %                               John Cristy                                   %
00017 %                               March 2003                                    %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
00021 %  dedicated to making software imaging solutions freely available.           %
00022 %                                                                             %
00023 %  You may not use this file except in compliance with the License.  You may  %
00024 %  obtain a copy of the License at                                            %
00025 %                                                                             %
00026 %    http://www.wizards-toolkit.org/script/license.php                        %
00027 %                                                                             %
00028 %  Unless required by applicable law or agreed to in writing, software        %
00029 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00030 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00031 %  See the License for the specific language governing permissions and        %
00032 %  limitations under the License.                                             %
00033 %                                                                             %
00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00035 %
00036 %
00037 %
00038 */
00039 
00040 /*
00041   Include declarations.
00042 */
00043 #include "wizard/studio.h"
00044 #include "wizard/bzip.h"
00045 #include "wizard/entropy.h"
00046 #include "wizard/exception.h"
00047 #include "wizard/exception-private.h"
00048 #include "wizard/lzma.h"
00049 #include "wizard/memory_.h"
00050 #include "wizard/zip.h"
00051 
00052 /*
00053   Typedef declarations.
00054 */
00055 struct _EntropyInfo
00056 {
00057   EntropyType
00058     entropy;
00059 
00060   void
00061     *handle;
00062 
00063   time_t
00064     timestamp;
00065 
00066   size_t
00067     signature;
00068 };
00069 
00070 /*
00071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00072 %                                                                             %
00073 %                                                                             %
00074 %                                                                             %
00075 %   A c q u i r e E n t r o p y I n f o                                       %
00076 %                                                                             %
00077 %                                                                             %
00078 %                                                                             %
00079 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00080 %
00081 %  AcquireEntropyInfo() allocates the EntropyInfo structure.
00082 %
00083 %  The format of the AcquireEntropyInfo method is:
00084 %
00085 %      EntropyInfo *AcquireEntropyInfo(const EntropyType entropy,
00086 %        const size_t level)
00087 %
00088 %  A description of each parameter follows:
00089 %
00090 %    o entropy: The entropy type.
00091 %
00092 %    o level: entropy level: 1 is best speed, 9 is more entropy.
00093 %
00094 */
00095 WizardExport EntropyInfo *AcquireEntropyInfo(const EntropyType entropy,
00096   const size_t level)
00097 {
00098   EntropyInfo
00099     *entropy_info;
00100 
00101   entropy_info=(EntropyInfo *) AcquireWizardMemory(sizeof(*entropy_info));
00102   if (entropy_info == (EntropyInfo *) NULL)
00103     ThrowWizardFatalError(EntropyDomain,MemoryError);
00104   (void) ResetWizardMemory(entropy_info,0,sizeof(*entropy_info));
00105   entropy_info->entropy=entropy;
00106   switch (entropy_info->entropy)
00107   {
00108     case BZIPEntropy:
00109     {
00110       entropy_info->handle=(EntropyInfo *) AcquireBZIPInfo(level);
00111       break;
00112     }
00113     case LZMAEntropy:
00114     {
00115       entropy_info->handle=(EntropyInfo *) AcquireLZMAInfo(level);
00116       break;
00117     }
00118     case ZIPEntropy:
00119     {
00120       entropy_info->handle=(EntropyInfo *) AcquireZIPInfo(level);
00121       break;
00122     }
00123     default:
00124       ThrowWizardFatalError(EntropyDomain,EnumerateError);
00125   }
00126   entropy_info->timestamp=time((time_t *) NULL);
00127   entropy_info->signature=WizardSignature;
00128   return(entropy_info);
00129 }
00130 
00131 /*
00132 e%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00133 %                                                                             %
00134 %                                                                             %
00135 %                                                                             %
00136 %   D e s t r o y E n t r o p y I n f o                                       %
00137 %                                                                             %
00138 %                                                                             %
00139 %                                                                             %
00140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00141 %
00142 %  DestroyEntropyInfo() zeros memory associated with the EntropyInfo
00143 %  structure.
00144 %
00145 %  The format of the DestroyEntropyInfo method is:
00146 %
00147 %      EntropyInfo *DestroyEntropyInfo(EntropyInfo *entropy_info)
00148 %
00149 %  A description of each parameter follows:
00150 %
00151 %    o entropy_info: The entropy info.
00152 %
00153 */
00154 WizardExport EntropyInfo *DestroyEntropyInfo(EntropyInfo *entropy_info)
00155 {
00156   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00157   WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
00158   WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
00159   if (entropy_info->handle != (EntropyInfo *) NULL)
00160     switch (entropy_info->entropy)
00161     {
00162       case BZIPEntropy:
00163       {
00164         entropy_info->handle=(void *) DestroyBZIPInfo((BZIPInfo *)
00165           entropy_info->handle);
00166         break;
00167       }
00168       case LZMAEntropy:
00169       {
00170         entropy_info->handle=(void *) DestroyLZMAInfo((LZMAInfo *)
00171           entropy_info->handle);
00172         break;
00173       }
00174       case ZIPEntropy:
00175       {
00176         entropy_info->handle=(void *) DestroyZIPInfo((ZIPInfo *)
00177           entropy_info->handle);
00178         break;
00179       }
00180       default:
00181         break;
00182     }
00183   entropy_info->signature=(~WizardSignature);
00184   entropy_info=(EntropyInfo *) RelinquishWizardMemory(entropy_info);
00185   return(entropy_info);
00186 }
00187 
00188 /*
00189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00190 %                                                                             %
00191 %                                                                             %
00192 %                                                                             %
00193 %   G e t E n t r o p y C h a o s                                             %
00194 %                                                                             %
00195 %                                                                             %
00196 %                                                                             %
00197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00198 %
00199 %  GetEntropyChaos() returns Entropy chaos.
00200 %
00201 %  The format of the GetEntropyChaos method is:
00202 %
00203 %      const StringInfo *GetEntropyChaos(const EntropyInfo *entropy_info)
00204 %
00205 %  A description of each parameter follows:
00206 %
00207 %    o entropy_info: The entropy info.
00208 %
00209 */
00210 WizardExport const StringInfo *GetEntropyChaos(const EntropyInfo *entropy_info)
00211 {
00212   const StringInfo
00213     *chaos;
00214 
00215   /*
00216     Increase the message entropy.
00217   */
00218   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00219   WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
00220   WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
00221   switch (entropy_info->entropy)
00222   {
00223     case BZIPEntropy:
00224     {
00225       BZIPInfo
00226         *bzip_info;
00227 
00228       bzip_info=(BZIPInfo *) entropy_info->handle;
00229       chaos=GetBZIPChaos(bzip_info);
00230       break;
00231     }
00232     case LZMAEntropy:
00233     {
00234       LZMAInfo
00235         *lzma_info;
00236 
00237       lzma_info=(LZMAInfo *) entropy_info->handle;
00238       chaos=GetLZMAChaos(lzma_info);
00239       break;
00240     }
00241     case ZIPEntropy:
00242     {
00243       ZIPInfo
00244         *zip_info;
00245 
00246       zip_info=(ZIPInfo *) entropy_info->handle;
00247       chaos=GetZIPChaos(zip_info);
00248       break;
00249     }
00250     default:
00251       ThrowWizardFatalError(EntropyDomain,EnumerateError);
00252   }
00253   return(chaos);
00254 }
00255 
00256 /*
00257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00258 %                                                                             %
00259 %                                                                             %
00260 %                                                                             %
00261 %   I n c r e a s e E n t r o p y                                             %
00262 %                                                                             %
00263 %                                                                             %
00264 %                                                                             %
00265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00266 %
00267 %  IncreaseEntropy() increases the entropy of a message.
00268 %
00269 %  The format of the IncreaseEntropy method is:
00270 %
00271 %      WizardBooleanType IncreaseEntropy(EntropyInfo *entropy_info,
00272 %        const StringInfo *message,ExceptionInfo *exception)
00273 %
00274 %  A description of each parameter follows:
00275 %
00276 %    o entropy_info: The address of a structure of type EntropyInfo.
00277 %
00278 %    o message: The message.
00279 %
00280 %    o exception: Return any errors or warnings in this structure.
00281 %
00282 */
00283 WizardExport WizardBooleanType IncreaseEntropy(EntropyInfo *entropy_info,
00284   const StringInfo *message,ExceptionInfo *exception)
00285 {
00286   WizardBooleanType
00287     status;
00288 
00289   /*
00290     Increase the message entropy.
00291   */
00292   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00293   WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
00294   WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
00295   WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
00296   status=WizardFalse;
00297   switch (entropy_info->entropy)
00298   {
00299     case BZIPEntropy:
00300     {
00301       BZIPInfo
00302         *bzip_info;
00303 
00304       bzip_info=(BZIPInfo *) entropy_info->handle;
00305       status=IncreaseBZIP(bzip_info,message,exception);
00306       break;
00307     }
00308     case LZMAEntropy:
00309     {
00310       LZMAInfo
00311         *lzma_info;
00312 
00313       lzma_info=(LZMAInfo *) entropy_info->handle;
00314       status=IncreaseLZMA(lzma_info,message,exception);
00315       break;
00316     }
00317     case ZIPEntropy:
00318     {
00319       ZIPInfo
00320         *zip_info;
00321 
00322       zip_info=(ZIPInfo *) entropy_info->handle;
00323       status=IncreaseZIP(zip_info,message,exception);
00324       break;
00325     }
00326     default:
00327       ThrowWizardFatalError(EntropyDomain,EnumerateError);
00328   }
00329   return(status);
00330 }
00331 
00332 /*
00333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00334 %                                                                             %
00335 %                                                                             %
00336 %                                                                             %
00337 %   R e s t o r e E n t r o p y                                               %
00338 %                                                                             %
00339 %                                                                             %
00340 %                                                                             %
00341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00342 %
00343 %  RestoreEntropy() restores the messages to its original entropy.
00344 %
00345 %  The format of the RestoreEntropy method is:
00346 %
00347 %      unsigned ing RestoreEntropy(EntropyInfo *entropy_info,
00348 %        const size_t length,const StringInfo *message,ExceptionInfo *exception)
00349 %
00350 %  A description of each parameter follows:
00351 %
00352 %    o entropy_info: The address of a structure of type EntropyInfo.
00353 %
00354 %    o length: The  the total size of the destination buffer, which must be
00355 %      large enough to hold the entire uncompressed data.
00356 %
00357 %    o message: The message.
00358 %
00359 %    o exception: Return any errors or warnings in this structure.
00360 %
00361 */
00362 WizardExport WizardBooleanType RestoreEntropy(EntropyInfo *entropy_info,
00363   const size_t length,const StringInfo *message,ExceptionInfo *exception)
00364 {
00365   WizardBooleanType
00366     status;
00367 
00368   /*
00369     Restore the message entropy.
00370   */
00371   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00372   WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
00373   WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
00374   WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
00375   status=WizardFalse;
00376   switch (entropy_info->entropy)
00377   {
00378     case BZIPEntropy:
00379     {
00380       BZIPInfo
00381         *bzip_info;
00382 
00383       bzip_info=(BZIPInfo *) entropy_info->handle;
00384       status=RestoreBZIP(bzip_info,length,message,exception);
00385       break;
00386     }
00387     case LZMAEntropy:
00388     {
00389       LZMAInfo
00390         *lzma_info;
00391 
00392       lzma_info=(LZMAInfo *) entropy_info->handle;
00393       status=RestoreLZMA(lzma_info,length,message,exception);
00394       break;
00395     }
00396     case ZIPEntropy:
00397     {
00398       ZIPInfo
00399         *zip_info;
00400 
00401       zip_info=(ZIPInfo *) entropy_info->handle;
00402       status=RestoreZIP(zip_info,length,message,exception);
00403       break;
00404     }
00405     default:
00406       ThrowWizardFatalError(EntropyDomain,EnumerateError);
00407   }
00408   return(status);
00409 }