root/WizardsToolkit/trunk/wizard/bzip.c

Revision 1, 13.3 KB (checked in by cristy, 3 months ago)


Line 
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%                               John Cristy                                   %
17%                               March 2003                                    %
18%                                                                             %
19%                                                                             %
20%  Copyright 1999-2009 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%    http://www.wizards-toolkit.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"
46#include "wizard/exception-private.h"
47#include "wizard/memory_.h"
48#include "bzlib.h"
49
50/*
51  Typedef declaractions;
52*/
53struct _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%   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 unsigned long 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*/
93WizardExport 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%   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*/
131WizardExport 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%   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*/
165WizardExport 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%   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
207WizardExport void bz_internal_error(int error)
208{
209  ThrowWizardFatalError(EntropyDomain,AssertError);
210}
211
212static void RelinquishBZIPMemory(void *context,void *memory)
213{
214  memory=RelinquishWizardMemory(memory);
215}
216
217WizardExport 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    Increase the message entropy.
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%   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*/
297WizardExport 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    Restore the message entropy.
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}
Note: See TracBrowser for help on using the browser.