00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
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
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
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
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
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
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
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
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
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
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
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
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
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
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 }