| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | #include "wizard/studio.h" |
|---|
| 44 | #include "wizard/bzip.h" |
|---|
| 45 | #include "wizard/exception.h" |
|---|
| 46 | #include "wizard/exception-private.h" |
|---|
| 47 | #include "wizard/memory_.h" |
|---|
| 48 | #include "bzlib.h" |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | struct _BZIPInfo |
|---|
| 54 | { |
|---|
| 55 | bz_stream |
|---|
| 56 | stream; |
|---|
| 57 | |
|---|
| 58 | StringInfo |
|---|
| 59 | *chaos; |
|---|
| 60 | |
|---|
| 61 | unsigned long |
|---|
| 62 | level; |
|---|
| 63 | |
|---|
| 64 | time_t |
|---|
| 65 | timestamp; |
|---|
| 66 | |
|---|
| 67 | unsigned long |
|---|
| 68 | signature; |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | WizardExport BZIPInfo *AcquireBZIPInfo(const unsigned long level) |
|---|
| 94 | { |
|---|
| 95 | BZIPInfo |
|---|
| 96 | *bzip_info; |
|---|
| 97 | |
|---|
| 98 | bzip_info=(BZIPInfo *) AcquireWizardMemory(sizeof(*bzip_info)); |
|---|
| 99 | if (bzip_info == (BZIPInfo *) NULL) |
|---|
| 100 | ThrowWizardFatalError(EntropyDomain,MemoryError); |
|---|
| 101 | (void) ResetWizardMemory(bzip_info,0,sizeof(*bzip_info)); |
|---|
| 102 | bzip_info->chaos=AcquireStringInfo(1); |
|---|
| 103 | bzip_info->level=level; |
|---|
| 104 | bzip_info->timestamp=time((time_t *) NULL); |
|---|
| 105 | bzip_info->signature=WizardSignature; |
|---|
| 106 | return(bzip_info); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | WizardExport BZIPInfo *DestroyBZIPInfo(BZIPInfo *bzip_info) |
|---|
| 132 | { |
|---|
| 133 | (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); |
|---|
| 134 | WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL); |
|---|
| 135 | WizardAssert(EntropyDomain,bzip_info->signature == WizardSignature); |
|---|
| 136 | if (bzip_info->chaos != (StringInfo *) NULL) |
|---|
| 137 | bzip_info->chaos=DestroyStringInfo(bzip_info->chaos); |
|---|
| 138 | bzip_info->signature=(~WizardSignature); |
|---|
| 139 | bzip_info=(BZIPInfo *) RelinquishWizardMemory(bzip_info); |
|---|
| 140 | return(bzip_info); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | WizardExport const StringInfo *GetBZIPChaos(const BZIPInfo *bzip_info) |
|---|
| 166 | { |
|---|
| 167 | (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); |
|---|
| 168 | WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL); |
|---|
| 169 | WizardAssert(EntropyDomain,bzip_info->signature == WizardSignature); |
|---|
| 170 | return(bzip_info->chaos); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | static void *AcquireBZIPMemory(void *context,int items,int size) |
|---|
| 203 | { |
|---|
| 204 | return((void *) AcquireQuantumMemory((size_t) items,(size_t) size)); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | WizardExport void bz_internal_error(int error) |
|---|
| 208 | { |
|---|
| 209 | ThrowWizardFatalError(EntropyDomain,AssertError); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | static void RelinquishBZIPMemory(void *context,void *memory) |
|---|
| 213 | { |
|---|
| 214 | memory=RelinquishWizardMemory(memory); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | WizardExport WizardBooleanType IncreaseBZIP(BZIPInfo *bzip_info, |
|---|
| 218 | const StringInfo *message,ExceptionInfo *exception) |
|---|
| 219 | { |
|---|
| 220 | int |
|---|
| 221 | status; |
|---|
| 222 | |
|---|
| 223 | bz_stream |
|---|
| 224 | stream; |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); |
|---|
| 230 | WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL); |
|---|
| 231 | WizardAssert(EntropyDomain,bzip_info->signature == WizardSignature); |
|---|
| 232 | WizardAssert(EntropyDomain,message != (const StringInfo *) NULL); |
|---|
| 233 | stream.bzalloc=AcquireBZIPMemory; |
|---|
| 234 | stream.bzfree=RelinquishBZIPMemory; |
|---|
| 235 | stream.opaque=(void *) NULL; |
|---|
| 236 | status=BZ2_bzCompressInit(&stream,(int) bzip_info->level,0,0); |
|---|
| 237 | if (status != BZ_OK) |
|---|
| 238 | { |
|---|
| 239 | (void) ThrowWizardException(exception,GetWizardModule(),EntropyError, |
|---|
| 240 | "unable to increase entropy `%s'",strerror(errno)); |
|---|
| 241 | return(WizardFalse); |
|---|
| 242 | } |
|---|
| 243 | stream.next_in=(char *) GetStringInfoDatum(message); |
|---|
| 244 | stream.avail_in=(unsigned int) GetStringInfoLength(message); |
|---|
| 245 | SetStringInfoLength(bzip_info->chaos,(size_t) |
|---|
| 246 | (GetStringInfoLength(message)+GetStringInfoLength(message)/100+600)); |
|---|
| 247 | stream.next_out=(char *) GetStringInfoDatum(bzip_info->chaos); |
|---|
| 248 | stream.avail_out=(unsigned int) GetStringInfoLength(bzip_info->chaos); |
|---|
| 249 | status=BZ2_bzCompress(&stream,BZ_FINISH); |
|---|
| 250 | if (status != BZ_STREAM_END) |
|---|
| 251 | { |
|---|
| 252 | (void) ThrowWizardException(exception,GetWizardModule(),EntropyError, |
|---|
| 253 | "unable to increase entropy `%s'",strerror(errno)); |
|---|
| 254 | return(WizardFalse); |
|---|
| 255 | } |
|---|
| 256 | SetStringInfoLength(bzip_info->chaos,(size_t) stream.total_out_lo32); |
|---|
| 257 | status=BZ2_bzCompressEnd(&stream); |
|---|
| 258 | if (status != BZ_OK) |
|---|
| 259 | { |
|---|
| 260 | (void) ThrowWizardException(exception,GetWizardModule(),EntropyError, |
|---|
| 261 | "unable to increase entropy `%s'",strerror(errno)); |
|---|
| 262 | return(WizardFalse); |
|---|
| 263 | } |
|---|
| 264 | return(WizardTrue); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | WizardExport WizardBooleanType RestoreBZIP(BZIPInfo *bzip_info, |
|---|
| 298 | const size_t length,const StringInfo *message,ExceptionInfo *exception) |
|---|
| 299 | { |
|---|
| 300 | int |
|---|
| 301 | status; |
|---|
| 302 | |
|---|
| 303 | bz_stream |
|---|
| 304 | stream; |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL); |
|---|
| 310 | (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); |
|---|
| 311 | WizardAssert(EntropyDomain,bzip_info->signature == WizardSignature); |
|---|
| 312 | WizardAssert(EntropyDomain,message != (const StringInfo *) NULL); |
|---|
| 313 | stream.bzalloc=AcquireBZIPMemory; |
|---|
| 314 | stream.bzfree=RelinquishBZIPMemory; |
|---|
| 315 | stream.opaque=(void *) NULL; |
|---|
| 316 | status=BZ2_bzDecompressInit(&stream,0,0); |
|---|
| 317 | if (status != BZ_OK) |
|---|
| 318 | { |
|---|
| 319 | (void) ThrowWizardException(exception,GetWizardModule(),EntropyError, |
|---|
| 320 | "unable to restore entropy `%s'",strerror(errno)); |
|---|
| 321 | return(WizardFalse); |
|---|
| 322 | } |
|---|
| 323 | stream.next_in=(char *) GetStringInfoDatum(message); |
|---|
| 324 | stream.avail_in=(unsigned int) GetStringInfoLength(message); |
|---|
| 325 | SetStringInfoLength(bzip_info->chaos,length); |
|---|
| 326 | stream.next_out=(char *) GetStringInfoDatum(bzip_info->chaos); |
|---|
| 327 | stream.avail_out=(unsigned int) GetStringInfoLength(bzip_info->chaos); |
|---|
| 328 | status=BZ2_bzDecompress(&stream); |
|---|
| 329 | if (status != BZ_STREAM_END) |
|---|
| 330 | { |
|---|
| 331 | (void) ThrowWizardException(exception,GetWizardModule(),EntropyError, |
|---|
| 332 | "unable to restore entropy `%s'",strerror(errno)); |
|---|
| 333 | return(WizardFalse); |
|---|
| 334 | } |
|---|
| 335 | SetStringInfoLength(bzip_info->chaos,(size_t) stream.total_out_lo32); |
|---|
| 336 | status=BZ2_bzDecompressEnd(&stream); |
|---|
| 337 | if (status != BZ_OK) |
|---|
| 338 | { |
|---|
| 339 | (void) ThrowWizardException(exception,GetWizardModule(),EntropyError, |
|---|
| 340 | "unable to restore entropy `%s'",strerror(errno)); |
|---|
| 341 | return(WizardFalse); |
|---|
| 342 | } |
|---|
| 343 | return(WizardTrue); |
|---|
| 344 | } |
|---|