bzip.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %                         BBBB   ZZZZZ  IIIII  PPPP                           %
00007 %                         B   B     ZZ    I    P   P                          %
00008 %                         BBBB    ZZZ     I    PPPP                           %
00009 %                         B   B  ZZ       I    P                              %
00010 %                         BBBB   ZZZZZ  IIIII  P                              %
00011 %                                                                             %
00012 %                                                                             %
00013 %                    Wizard's Toolkit BZip 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/exception.h"
00046 #include "wizard/exception-private.h"
00047 #include "wizard/memory_.h"
00048 #include "bzlib.h"
00049 
00050 /*
00051   Typedef declaractions;
00052 */
00053 struct _BZIPInfo
00054 {
00055   bz_stream
00056     stream;
00057 
00058   StringInfo
00059     *chaos;
00060 
00061   size_t
00062     level;
00063 
00064   time_t
00065     timestamp;
00066 
00067   size_t
00068     signature;
00069 };
00070 
00071 /*
00072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00073 %                                                                             %
00074 %                                                                             %
00075 %                                                                             %
00076 %   A c q u i r e B Z I P I n f o                                             %
00077 %                                                                             %
00078 %                                                                             %
00079 %                                                                             %
00080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00081 %
00082 %  AcquireBZIPInfo() allocates the ZIPInfo structure.
00083 %
00084 %  The format of the AcquireBZIPInfo method is:
00085 %
00086 %      BZIPInfo *AcquireBZIPInfo(const size_t level)
00087 %
00088 %  A description of each parameter follows:
00089 %
00090 %    o level: entropy level: 1 is best speed, 9 is more entropy.
00091 %
00092 */
00093 WizardExport BZIPInfo *AcquireBZIPInfo(const size_t level)
00094 {
00095   BZIPInfo
00096     *bzip_info;
00097 
00098   bzip_info=(BZIPInfo *) AcquireAlignedMemory(1,sizeof(*bzip_info));
00099   if (bzip_info == (BZIPInfo *) NULL)
00100     ThrowWizardFatalError(EntropyDomain,MemoryError);
00101   (void) ResetWizardMemory(bzip_info,0,sizeof(*bzip_info));
00102   bzip_info->chaos=AcquireStringInfo(1);
00103   bzip_info->level=level;
00104   bzip_info->timestamp=time((time_t *) NULL);
00105   bzip_info->signature=WizardSignature;
00106   return(bzip_info);
00107 }
00108 
00109 /*
00110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00111 %                                                                             %
00112 %                                                                             %
00113 %                                                                             %
00114 %   D e s t r o y B Z I P I n f o                                             %
00115 %                                                                             %
00116 %                                                                             %
00117 %                                                                             %
00118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00119 %
00120 %  DestroyBZIPInfo() zeros memory associated with the ZIPInfo structure.
00121 %
00122 %  The format of the DestroyBZIPInfo method is:
00123 %
00124 %      BZIPInfo *DestroyBZIPInfo(BZIPInfo *bzip_info)
00125 %
00126 %  A description of each parameter follows:
00127 %
00128 %    o bzip_info: The bzip info.
00129 %
00130 */
00131 WizardExport BZIPInfo *DestroyBZIPInfo(BZIPInfo *bzip_info)
00132 {
00133   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00134   WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL);
00135   WizardAssert(EntropyDomain,bzip_info->signature == WizardSignature);
00136   if (bzip_info->chaos != (StringInfo *) NULL)
00137     bzip_info->chaos=DestroyStringInfo(bzip_info->chaos);
00138   bzip_info->signature=(~WizardSignature);
00139   bzip_info=(BZIPInfo *) RelinquishWizardMemory(bzip_info);
00140   return(bzip_info);
00141 }
00142 
00143 /*
00144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00145 %                                                                             %
00146 %                                                                             %
00147 %                                                                             %
00148 %   G e t B Z I P C h a o s                                                   %
00149 %                                                                             %
00150 %                                                                             %
00151 %                                                                             %
00152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00153 %
00154 %  GetBZIPChaos() returns BZIP chaos.
00155 %
00156 %  The format of the GetBZIPChaos method is:
00157 %
00158 %      const StringInfo *GetBZIPChaos(const BZIPInfo *bzip_info)
00159 %
00160 %  A description of each parameter follows:
00161 %
00162 %    o bzip_info: The bzip info.
00163 %
00164 */
00165 WizardExport const StringInfo *GetBZIPChaos(const BZIPInfo *bzip_info)
00166 {
00167   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00168   WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL);
00169   WizardAssert(EntropyDomain,bzip_info->signature == WizardSignature);
00170   return(bzip_info->chaos);
00171 }
00172 
00173 /*
00174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00175 %                                                                             %
00176 %                                                                             %
00177 %                                                                             %
00178 %   I n c r e a s e B Z I P                                                   %
00179 %                                                                             %
00180 %                                                                             %
00181 %                                                                             %
00182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00183 %
00184 
00185 %  IncreaseBZIP() compresses the message to increase its entropy.
00186 %
00187 %  The format of the IncreaseBZIP method is:
00188 %
00189 %      WizardBooleanType IncreaseBZIP(BZIPInfo *bzip_info,
00190 %        const StringInfo *message,ExceptionInfo *exception)
00191 %
00192 %  A description of each parameter follows:
00193 %
00194 %    o bzip_info: The address of a structure of type BZIPInfo.
00195 %
00196 %    o message: The message.
00197 %
00198 %    o exception: Return any errors or warnings in this structure.
00199 %
00200 */
00201 
00202 static void *AcquireBZIPMemory(void *context,int items,int size)
00203 {
00204   return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
00205 }
00206 
00207 WizardExport void bz_internal_error(int error)
00208 {
00209   ThrowWizardFatalError(EntropyDomain,AssertError);
00210 }
00211 
00212 static void RelinquishBZIPMemory(void *context,void *memory)
00213 {
00214   memory=RelinquishWizardMemory(memory);
00215 }
00216 
00217 WizardExport WizardBooleanType IncreaseBZIP(BZIPInfo *bzip_info,
00218   const StringInfo *message,ExceptionInfo *exception)
00219 {
00220   int
00221     status;
00222 
00223   bz_stream
00224     stream;
00225 
00226   /*
00227     Increase the message entropy.
00228   */
00229   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00230   WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL);
00231   WizardAssert(EntropyDomain,bzip_info->signature == WizardSignature);
00232   WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
00233   stream.bzalloc=AcquireBZIPMemory;
00234   stream.bzfree=RelinquishBZIPMemory;
00235   stream.opaque=(void *) NULL;
00236   status=BZ2_bzCompressInit(&stream,(int) bzip_info->level,0,0);
00237   if (status != BZ_OK)
00238     {
00239       (void) ThrowWizardException(exception,GetWizardModule(),EntropyError,
00240         "unable to increase entropy `%s'",strerror(errno));
00241       return(WizardFalse);
00242     }
00243   stream.next_in=(char *) GetStringInfoDatum(message);
00244   stream.avail_in=(unsigned int) GetStringInfoLength(message);
00245   SetStringInfoLength(bzip_info->chaos,(size_t)
00246     (GetStringInfoLength(message)+GetStringInfoLength(message)/100+600));
00247   stream.next_out=(char *) GetStringInfoDatum(bzip_info->chaos);
00248   stream.avail_out=(unsigned int) GetStringInfoLength(bzip_info->chaos);
00249   status=BZ2_bzCompress(&stream,BZ_FINISH);
00250   if (status != BZ_STREAM_END)
00251     {
00252       (void) ThrowWizardException(exception,GetWizardModule(),EntropyError,
00253         "unable to increase entropy `%s'",strerror(errno));
00254       return(WizardFalse);
00255     }
00256   SetStringInfoLength(bzip_info->chaos,(size_t) stream.total_out_lo32);
00257   status=BZ2_bzCompressEnd(&stream);
00258   if (status != BZ_OK)
00259     {
00260       (void) ThrowWizardException(exception,GetWizardModule(),EntropyError,
00261         "unable to increase entropy `%s'",strerror(errno));
00262       return(WizardFalse);
00263     }
00264   return(WizardTrue);
00265 }
00266 
00267 /*
00268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00269 %                                                                             %
00270 %                                                                             %
00271 %                                                                             %
00272 %   R e s t o r e B Z I P                                                     %
00273 %                                                                             %
00274 %                                                                             %
00275 %                                                                             %
00276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00277 %
00278 %  RestoreBZIP() uncompresses the message to restore its original entropy.
00279 %
00280 %  The format of the RestoreBZIP method is:
00281 %
00282 %      WizardBooleanType RestoreBZIP(BZIPInfo *bzip_info,const size_t length,
00283 %        const StringInfo *message,ExceptionInfo *exception)
00284 %
00285 %  A description of each parameter follows:
00286 %
00287 %    o bzip_info: The address of a structure of type BZIPInfo.
00288 %
00289 %    o length: The total size of the destination buffer, which must be large
00290 %      enough to hold the entire uncompressed data.
00291 %
00292 %    o message: The message.
00293 %
00294 %    o exception: Return any errors or warnings in this structure.
00295 %
00296 */
00297 WizardExport WizardBooleanType RestoreBZIP(BZIPInfo *bzip_info,
00298   const size_t length,const StringInfo *message,ExceptionInfo *exception)
00299 {
00300   int
00301     status;
00302 
00303   bz_stream
00304     stream;
00305 
00306   /*
00307     Restore the message entropy.
00308   */
00309   WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL);
00310   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00311   WizardAssert(EntropyDomain,bzip_info->signature == WizardSignature);
00312   WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
00313   stream.bzalloc=AcquireBZIPMemory;
00314   stream.bzfree=RelinquishBZIPMemory;
00315   stream.opaque=(void *) NULL;
00316   status=BZ2_bzDecompressInit(&stream,0,0);
00317   if (status != BZ_OK)
00318     {
00319       (void) ThrowWizardException(exception,GetWizardModule(),EntropyError,
00320         "unable to restore entropy `%s'",strerror(errno));
00321       return(WizardFalse);
00322     }
00323   stream.next_in=(char *) GetStringInfoDatum(message);
00324   stream.avail_in=(unsigned int) GetStringInfoLength(message);
00325   SetStringInfoLength(bzip_info->chaos,length);
00326   stream.next_out=(char *) GetStringInfoDatum(bzip_info->chaos);
00327   stream.avail_out=(unsigned int) GetStringInfoLength(bzip_info->chaos);
00328   status=BZ2_bzDecompress(&stream);
00329   if (status != BZ_STREAM_END)
00330     {
00331       (void) ThrowWizardException(exception,GetWizardModule(),EntropyError,
00332         "unable to restore entropy `%s'",strerror(errno));
00333       return(WizardFalse);
00334     }
00335   SetStringInfoLength(bzip_info->chaos,(size_t) stream.total_out_lo32);
00336   status=BZ2_bzDecompressEnd(&stream);
00337   if (status != BZ_OK)
00338     {
00339       (void) ThrowWizardException(exception,GetWizardModule(),EntropyError,
00340         "unable to restore entropy `%s'",strerror(errno));
00341       return(WizardFalse);
00342     }
00343   return(WizardTrue);
00344 }
Generated by  doxygen 1.6.2-20100208