MagickCore 7.0.10
bzip.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% BBBB ZZZZZ IIIII PPPP %
7% B B ZZ I P P %
8% BBBB ZZZ I PPPP %
9% B B ZZ I P %
10% BBBB ZZZZZ IIIII P %
11% %
12% %
13% Wizard's Toolkit BZip Entropy Methods %
14% %
15% Software Design %
16% Cristy %
17% March 2003 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
43#include "wizard/studio.h"
44#include "wizard/bzip.h"
45#include "wizard/exception.h"
47#include "wizard/memory_.h"
48#include "bzlib.h"
49
50/*
51 Typedef declaractions;
52*/
54{
55 bz_stream
57
60
61 size_t
63
64 time_t
66
67 size_t
69};
70
71/*
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73% %
74% %
75% %
76% A c q u i r e B Z I P I n f o %
77% %
78% %
79% %
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81%
82% AcquireBZIPInfo() allocates the ZIPInfo structure.
83%
84% The format of the AcquireBZIPInfo method is:
85%
86% BZIPInfo *AcquireBZIPInfo(const size_t level)
87%
88% A description of each parameter follows:
89%
90% o level: entropy level: 1 is best speed, 9 is more entropy.
91%
92*/
94{
96 *bzip_info;
97
98 bzip_info=(BZIPInfo *) AcquireWizardMemory(sizeof(*bzip_info));
99 if (bzip_info == (BZIPInfo *) NULL)
101 (void) memset(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% D e s t r o y B Z I P I n f o %
115% %
116% %
117% %
118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119%
120% DestroyBZIPInfo() zeros memory associated with the ZIPInfo structure.
121%
122% The format of the DestroyBZIPInfo method is:
123%
124% BZIPInfo *DestroyBZIPInfo(BZIPInfo *bzip_info)
125%
126% A description of each parameter follows:
127%
128% o bzip_info: The bzip info.
129%
130*/
132{
134 WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL);
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% G e t B Z I P C h a o s %
149% %
150% %
151% %
152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153%
154% GetBZIPChaos() returns BZIP chaos.
155%
156% The format of the GetBZIPChaos method is:
157%
158% const StringInfo *GetBZIPChaos(const BZIPInfo *bzip_info)
159%
160% A description of each parameter follows:
161%
162% o bzip_info: The bzip info.
163%
164*/
166{
168 WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL);
170 return(bzip_info->chaos);
171}
172
173/*
174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175% %
176% %
177% %
178% I n c r e a s e B Z I P %
179% %
180% %
181% %
182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183%
184
185% IncreaseBZIP() compresses the message to increase its entropy.
186%
187% The format of the IncreaseBZIP method is:
188%
189% WizardBooleanType IncreaseBZIP(BZIPInfo *bzip_info,
190% const StringInfo *message,ExceptionInfo *exception)
191%
192% A description of each parameter follows:
193%
194% o bzip_info: The address of a structure of type BZIPInfo.
195%
196% o message: The message.
197%
198% o exception: Return any errors or warnings in this structure.
199%
200*/
201
202static void *AcquireBZIPMemory(void *context,int items,int size)
203{
204 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
205}
206
208{
210}
211
212static void RelinquishBZIPMemory(void *context,void *memory)
213{
214 memory=RelinquishWizardMemory(memory);
215}
216
218 const StringInfo *message,ExceptionInfo *exception)
219{
220 int
221 status;
222
223 bz_stream
224 stream;
225
226 /*
227 Increase the message entropy.
228 */
230 WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL);
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 {
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 {
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 {
261 "unable to increase entropy `%s'",strerror(errno));
262 return(WizardFalse);
263 }
264 return(WizardTrue);
265}
266
267/*
268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269% %
270% %
271% %
272% R e s t o r e B Z I P %
273% %
274% %
275% %
276%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277%
278% RestoreBZIP() uncompresses the message to restore its original entropy.
279%
280% The format of the RestoreBZIP method is:
281%
282% WizardBooleanType RestoreBZIP(BZIPInfo *bzip_info,const size_t length,
283% const StringInfo *message,ExceptionInfo *exception)
284%
285% A description of each parameter follows:
286%
287% o bzip_info: The address of a structure of type BZIPInfo.
288%
289% o length: The total size of the destination buffer, which must be large
290% enough to hold the entire uncompressed data.
291%
292% o message: The message.
293%
294% o exception: Return any errors or warnings in this structure.
295%
296*/
298 const size_t length,const StringInfo *message,ExceptionInfo *exception)
299{
300 int
301 status;
302
303 bz_stream
304 stream;
305
306 /*
307 Restore the message entropy.
308 */
309 WizardAssert(EntropyDomain,bzip_info != (BZIPInfo *) NULL);
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 {
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 {
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 {
340 "unable to restore entropy `%s'",strerror(errno));
341 return(WizardFalse);
342 }
343 return(WizardTrue);
344}
WizardExport const StringInfo * GetBZIPChaos(const BZIPInfo *bzip_info)
Definition bzip.c:165
WizardExport BZIPInfo * DestroyBZIPInfo(BZIPInfo *bzip_info)
Definition bzip.c:131
WizardExport WizardBooleanType IncreaseBZIP(BZIPInfo *bzip_info, const StringInfo *message, ExceptionInfo *exception)
Definition bzip.c:217
WizardExport WizardBooleanType RestoreBZIP(BZIPInfo *bzip_info, const size_t length, const StringInfo *message, ExceptionInfo *exception)
Definition bzip.c:297
static void RelinquishBZIPMemory(void *context, void *memory)
Definition bzip.c:212
WizardExport BZIPInfo * AcquireBZIPInfo(const size_t level)
Definition bzip.c:93
WizardExport void bz_internal_error(int error)
Definition bzip.c:207
static void * AcquireBZIPMemory(void *context, int items, int size)
Definition bzip.c:202
#define WizardAssert(domain, predicate)
#define ThrowWizardFatalError(domain, error)
WizardExport WizardBooleanType ThrowWizardException(ExceptionInfo *exception, const char *module, const char *function, const size_t line, const ExceptionType severity, const char *format,...)
Definition exception.c:1029
@ EntropyDomain
Definition exception.h:31
@ EntropyError
Definition exception.h:87
@ AssertError
Definition exception.h:56
@ MemoryError
Definition exception.h:49
WizardBooleanType LogWizardEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition log.c:1390
@ TraceEvent
Definition log.h:39
#define GetWizardModule()
Definition log.h:30
WizardExport void * AcquireWizardMemory(const size_t size)
Definition memory.c:586
WizardExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition memory.c:657
WizardExport void * RelinquishWizardMemory(void *memory)
Definition memory.c:1039
#define WizardExport
#define WizardSignature
WizardExport void SetStringInfoLength(StringInfo *string_info, const size_t length)
Definition string.c:1865
WizardExport size_t GetStringInfoLength(const StringInfo *string_info)
Definition string.c:1280
WizardExport StringInfo * AcquireStringInfo(const size_t length)
Definition string.c:179
WizardExport unsigned char * GetStringInfoDatum(const StringInfo *string_info)
Definition string.c:1251
WizardExport StringInfo * DestroyStringInfo(StringInfo *string_info)
Definition string.c:857
time_t timestamp
Definition bzip.c:65
size_t signature
Definition bzip.c:68
size_t level
Definition bzip.c:62
StringInfo * chaos
Definition bzip.c:59
bz_stream stream
Definition bzip.c:56
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28
@ WizardFalse
Definition wizard-type.h:27