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-2010 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/memory_.h"
00049 #include "wizard/zip.h"
00050 
00051 /*
00052   Typedef declarations.
00053 */
00054 struct _EntropyInfo
00055 {
00056   EntropyType
00057     entropy;
00058 
00059   void
00060     *handle;
00061 
00062   time_t
00063     timestamp;
00064 
00065   size_t
00066     signature;
00067 };
00068 
00069 /*
00070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00071 %                                                                             %
00072 %                                                                             %
00073 %                                                                             %
00074 %   A c q u i r e E n t r o p y I n f o                                       %
00075 %                                                                             %
00076 %                                                                             %
00077 %                                                                             %
00078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00079 %
00080 %  AcquireEntropyInfo() allocates the EntropyInfo structure.
00081 %
00082 %  The format of the AcquireEntropyInfo method is:
00083 %
00084 %      EntropyInfo *AcquireEntropyInfo(const EntropyType entropy,
00085 %        const size_t level)
00086 %
00087 %  A description of each parameter follows:
00088 %
00089 %    o entropy: The entropy type.
00090 %
00091 %    o level: entropy level: 1 is best speed, 9 is more entropy.
00092 %
00093 */
00094 WizardExport EntropyInfo *AcquireEntropyInfo(const EntropyType entropy,
00095   const size_t level)
00096 {
00097   EntropyInfo
00098     *entropy_info;
00099 
00100   entropy_info=(EntropyInfo *) AcquireAlignedMemory(1,sizeof(*entropy_info));
00101   if (entropy_info == (EntropyInfo *) NULL)
00102     ThrowWizardFatalError(EntropyDomain,MemoryError);
00103   (void) ResetWizardMemory(entropy_info,0,sizeof(*entropy_info));
00104   entropy_info->entropy=entropy;
00105   switch (entropy_info->entropy)
00106   {
00107     case BZIPEntropy:
00108     {
00109       entropy_info->handle=(EntropyInfo *) AcquireBZIPInfo(level);
00110       break;
00111     }
00112     case ZIPEntropy:
00113     {
00114       entropy_info->handle=(EntropyInfo *) AcquireZIPInfo(level);
00115       break;
00116     }
00117     default:
00118       ThrowWizardFatalError(EntropyDomain,EnumerateError);
00119   }
00120   entropy_info->timestamp=time((time_t *) NULL);
00121   entropy_info->signature=WizardSignature;
00122   return(entropy_info);
00123 }
00124 
00125 /*
00126 e%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00127 %                                                                             %
00128 %                                                                             %
00129 %                                                                             %
00130 %   D e s t r o y E n t r o p y I n f o                                       %
00131 %                                                                             %
00132 %                                                                             %
00133 %                                                                             %
00134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00135 %
00136 %  DestroyEntropyInfo() zeros memory associated with the EntropyInfo
00137 %  structure.
00138 %
00139 %  The format of the DestroyEntropyInfo method is:
00140 %
00141 %      EntropyInfo *DestroyEntropyInfo(EntropyInfo *entropy_info)
00142 %
00143 %  A description of each parameter follows:
00144 %
00145 %    o entropy_info: The entropy info.
00146 %
00147 */
00148 WizardExport EntropyInfo *DestroyEntropyInfo(EntropyInfo *entropy_info)
00149 {
00150   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00151   WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
00152   WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
00153   if (entropy_info->handle != (EntropyInfo *) NULL)
00154     switch (entropy_info->entropy)
00155     {
00156       case BZIPEntropy:
00157       {
00158         entropy_info->handle=(void *) DestroyBZIPInfo((BZIPInfo *)
00159           entropy_info->handle);
00160         break;
00161       }
00162       case ZIPEntropy:
00163       {
00164         entropy_info->handle=(void *) DestroyZIPInfo((ZIPInfo *)
00165           entropy_info->handle);
00166         break;
00167       }
00168       default:
00169         break;
00170     }
00171   entropy_info->signature=(~WizardSignature);
00172   entropy_info=(EntropyInfo *) RelinquishWizardMemory(entropy_info);
00173   return(entropy_info);
00174 }
00175 
00176 /*
00177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00178 %                                                                             %
00179 %                                                                             %
00180 %                                                                             %
00181 %   G e t E n t r o p y C h a o s                                             %
00182 %                                                                             %
00183 %                                                                             %
00184 %                                                                             %
00185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00186 %
00187 %  GetEntropyChaos() returns Entropy chaos.
00188 %
00189 %  The format of the GetEntropyChaos method is:
00190 %
00191 %      const StringInfo *GetEntropyChaos(const EntropyInfo *entropy_info)
00192 %
00193 %  A description of each parameter follows:
00194 %
00195 %    o entropy_info: The entropy info.
00196 %
00197 */
00198 WizardExport const StringInfo *GetEntropyChaos(const EntropyInfo *entropy_info)
00199 {
00200   const StringInfo
00201     *chaos;
00202 
00203   /*
00204     Increase the message entropy.
00205   */
00206   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00207   WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
00208   WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
00209   switch (entropy_info->entropy)
00210   {
00211     case BZIPEntropy:
00212     {
00213       BZIPInfo
00214         *bzip_info;
00215 
00216       bzip_info=(BZIPInfo *) entropy_info->handle;
00217       chaos=GetBZIPChaos(bzip_info);
00218       break;
00219     }
00220     case ZIPEntropy:
00221     {
00222       ZIPInfo
00223         *zip_info;
00224 
00225       zip_info=(ZIPInfo *) entropy_info->handle;
00226       chaos=GetZIPChaos(zip_info);
00227       break;
00228     }
00229     default:
00230       ThrowWizardFatalError(EntropyDomain,EnumerateError);
00231   }
00232   return(chaos);
00233 }
00234 
00235 /*
00236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00237 %                                                                             %
00238 %                                                                             %
00239 %                                                                             %
00240 %   I n c r e a s e E n t r o p y                                             %
00241 %                                                                             %
00242 %                                                                             %
00243 %                                                                             %
00244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00245 %
00246 %  IncreaseEntropy() increases the entropy of a message.
00247 %
00248 %  The format of the IncreaseEntropy method is:
00249 %
00250 %      WizardBooleanType IncreaseEntropy(EntropyInfo *entropy_info,
00251 %        const StringInfo *message,ExceptionInfo *exception)
00252 %
00253 %  A description of each parameter follows:
00254 %
00255 %    o entropy_info: The address of a structure of type EntropyInfo.
00256 %
00257 %    o message: The message.
00258 %
00259 %    o exception: Return any errors or warnings in this structure.
00260 %
00261 */
00262 WizardExport WizardBooleanType IncreaseEntropy(EntropyInfo *entropy_info,
00263   const StringInfo *message,ExceptionInfo *exception)
00264 {
00265   WizardBooleanType
00266     status;
00267 
00268   /*
00269     Increase the message entropy.
00270   */
00271   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00272   WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
00273   WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
00274   WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
00275   status=WizardFalse;
00276   switch (entropy_info->entropy)
00277   {
00278     case BZIPEntropy:
00279     {
00280       BZIPInfo
00281         *bzip_info;
00282 
00283       bzip_info=(BZIPInfo *) entropy_info->handle;
00284       status=IncreaseBZIP(bzip_info,message,exception);
00285       break;
00286     }
00287     case ZIPEntropy:
00288     {
00289       ZIPInfo
00290         *zip_info;
00291 
00292       zip_info=(ZIPInfo *) entropy_info->handle;
00293       status=IncreaseZIP(zip_info,message,exception);
00294       break;
00295     }
00296     default:
00297       ThrowWizardFatalError(EntropyDomain,EnumerateError);
00298   }
00299   return(status);
00300 }
00301 
00302 /*
00303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00304 %                                                                             %
00305 %                                                                             %
00306 %                                                                             %
00307 %   R e s t o r e E n t r o p y                                               %
00308 %                                                                             %
00309 %                                                                             %
00310 %                                                                             %
00311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00312 %
00313 %  RestoreEntropy() restores the messages to its original entropy.
00314 %
00315 %  The format of the RestoreEntropy method is:
00316 %
00317 %      unsigned ing RestoreEntropy(EntropyInfo *entropy_info,
00318 %        const size_t length,const StringInfo *message,ExceptionInfo *exception)
00319 %
00320 %  A description of each parameter follows:
00321 %
00322 %    o entropy_info: The address of a structure of type EntropyInfo.
00323 %
00324 %    o length: The  the total size of the destination buffer, which must be
00325 %      large enough to hold the entire uncompressed data.
00326 %
00327 %    o message: The message.
00328 %
00329 %    o exception: Return any errors or warnings in this structure.
00330 %
00331 */
00332 WizardExport WizardBooleanType RestoreEntropy(EntropyInfo *entropy_info,
00333   const size_t length,const StringInfo *message,ExceptionInfo *exception)
00334 {
00335   WizardBooleanType
00336     status;
00337 
00338   /*
00339     Restore the message entropy.
00340   */
00341   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00342   WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
00343   WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
00344   WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
00345   status=WizardFalse;
00346   switch (entropy_info->entropy)
00347   {
00348     case BZIPEntropy:
00349     {
00350       BZIPInfo
00351         *bzip_info;
00352 
00353       bzip_info=(BZIPInfo *) entropy_info->handle;
00354       status=RestoreBZIP(bzip_info,length,message,exception);
00355       break;
00356     }
00357     case ZIPEntropy:
00358     {
00359       ZIPInfo
00360         *zip_info;
00361 
00362       zip_info=(ZIPInfo *) entropy_info->handle;
00363       status=RestoreZIP(zip_info,length,message,exception);
00364       break;
00365     }
00366     default:
00367       ThrowWizardFatalError(EntropyDomain,EnumerateError);
00368   }
00369   return(status);
00370 }
Generated by  doxygen 1.6.2-20100208