root/WizardsToolkit/trunk/wizard/entropy.c

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


Line 
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3%                                                                             %
4%                                                                             %
5%                                                                             %
6%              EEEEE  N   N  TTTTT  RRRR    OOO   PPPP    Y   Y               %
7%              E      NN  N    T    R   R  O   O  P   P    Y Y                %
8%              EEE    N N N    T    RRRR   O   O  PPPP      Y                 %
9%              E      N  NN    T    R R    O   O  P         Y                 %
10%              EEEEE  N   N    T    R  R    OOO   P         Y                 %
11%                                                                             %
12%                                                                             %
13%                       Wizard's Toolkit 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/entropy.h"
46#include "wizard/exception.h"
47#include "wizard/exception-private.h"
48#include "wizard/memory_.h"
49#include "wizard/zip.h"
50
51/*
52  Typedef declarations.
53*/
54struct _EntropyInfo
55{
56  EntropyType
57    entropy;
58
59  void
60    *handle;
61
62  time_t
63    timestamp;
64
65  unsigned long
66    signature;
67};
68
69/*
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71%                                                                             %
72%                                                                             %
73%                                                                             %
74%   A c q u i r e E n t r o p y I n f o                                       %
75%                                                                             %
76%                                                                             %
77%                                                                             %
78%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79%
80%  AcquireEntropyInfo() allocates the EntropyInfo structure.
81%
82%  The format of the AcquireEntropyInfo method is:
83%
84%      EntropyInfo *AcquireEntropyInfo(const EntropyType entropy,
85%        const unsigned long level)
86%
87%  A description of each parameter follows:
88%
89%    o entropy: The entropy type.
90%
91%    o level: entropy level: 1 is best speed, 9 is more entropy.
92%
93*/
94WizardExport EntropyInfo *AcquireEntropyInfo(const EntropyType entropy,
95  const unsigned long level)
96{
97  EntropyInfo
98    *entropy_info;
99
100  entropy_info=(EntropyInfo *) AcquireWizardMemory(sizeof(*entropy_info));
101  if (entropy_info == (EntropyInfo *) NULL)
102    ThrowWizardFatalError(EntropyDomain,MemoryError);
103  (void) ResetWizardMemory(entropy_info,0,sizeof(*entropy_info));
104  entropy_info->entropy=entropy;
105  switch (entropy_info->entropy)
106  {
107    case BZIPEntropy:
108    {
109      entropy_info->handle=(EntropyInfo *) AcquireBZIPInfo(level);
110      break;
111    }
112    case ZIPEntropy:
113    {
114      entropy_info->handle=(EntropyInfo *) AcquireZIPInfo(level);
115      break;
116    }
117    default:
118      ThrowWizardFatalError(EntropyDomain,EnumerateError);
119  }
120  entropy_info->timestamp=time((time_t *) NULL);
121  entropy_info->signature=WizardSignature;
122  return(entropy_info);
123}
124
125/*
126e%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127%                                                                             %
128%                                                                             %
129%                                                                             %
130%   D e s t r o y E n t r o p y I n f o                                       %
131%                                                                             %
132%                                                                             %
133%                                                                             %
134%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135%
136%  DestroyEntropyInfo() zeros memory associated with the EntropyInfo
137%  structure.
138%
139%  The format of the DestroyEntropyInfo method is:
140%
141%      EntropyInfo *DestroyEntropyInfo(EntropyInfo *entropy_info)
142%
143%  A description of each parameter follows:
144%
145%    o entropy_info: The entropy info.
146%
147*/
148WizardExport EntropyInfo *DestroyEntropyInfo(EntropyInfo *entropy_info)
149{
150  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
151  WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
152  WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
153  if (entropy_info->handle != (EntropyInfo *) NULL)
154    switch (entropy_info->entropy)
155    {
156      case BZIPEntropy:
157      {
158        entropy_info->handle=(void *) DestroyBZIPInfo((BZIPInfo *)
159          entropy_info->handle);
160        break;
161      }
162      case ZIPEntropy:
163      {
164        entropy_info->handle=(void *) DestroyZIPInfo((ZIPInfo *)
165          entropy_info->handle);
166        break;
167      }
168      default:
169        break;
170    }
171  entropy_info->signature=(~WizardSignature);
172  entropy_info=(EntropyInfo *) RelinquishWizardMemory(entropy_info);
173  return(entropy_info);
174}
175
176/*
177%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178%                                                                             %
179%                                                                             %
180%                                                                             %
181%   G e t E n t r o p y C h a o s                                             %
182%                                                                             %
183%                                                                             %
184%                                                                             %
185%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186%
187%  GetEntropyChaos() returns Entropy chaos.
188%
189%  The format of the GetEntropyChaos method is:
190%
191%      const StringInfo *GetEntropyChaos(const EntropyInfo *entropy_info)
192%
193%  A description of each parameter follows:
194%
195%    o entropy_info: The entropy info.
196%
197*/
198WizardExport const StringInfo *GetEntropyChaos(const EntropyInfo *entropy_info)
199{
200  const StringInfo
201    *chaos;
202
203  /*
204    Increase the message entropy.
205  */
206  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
207  WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
208  WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
209  switch (entropy_info->entropy)
210  {
211    case BZIPEntropy:
212    {
213      BZIPInfo
214        *bzip_info;
215
216      bzip_info=(BZIPInfo *) entropy_info->handle;
217      chaos=GetBZIPChaos(bzip_info);
218      break;
219    }
220    case ZIPEntropy:
221    {
222      ZIPInfo
223        *zip_info;
224
225      zip_info=(ZIPInfo *) entropy_info->handle;
226      chaos=GetZIPChaos(zip_info);
227      break;
228    }
229    default:
230      ThrowWizardFatalError(EntropyDomain,EnumerateError);
231  }
232  return(chaos);
233}
234
235/*
236%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237%                                                                             %
238%                                                                             %
239%                                                                             %
240%   I n c r e a s e E n t r o p y                                             %
241%                                                                             %
242%                                                                             %
243%                                                                             %
244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245%
246%  IncreaseEntropy() increases the entropy of a message.
247%
248%  The format of the IncreaseEntropy method is:
249%
250%      WizardBooleanType IncreaseEntropy(EntropyInfo *entropy_info,
251%        const StringInfo *message,ExceptionInfo *exception)
252%
253%  A description of each parameter follows:
254%
255%    o entropy_info: The address of a structure of type EntropyInfo.
256%
257%    o message: The message.
258%
259%    o exception: Return any errors or warnings in this structure.
260%
261*/
262WizardExport WizardBooleanType IncreaseEntropy(EntropyInfo *entropy_info,
263  const StringInfo *message,ExceptionInfo *exception)
264{
265  WizardBooleanType
266    status;
267
268  /*
269    Increase the message entropy.
270  */
271  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
272  WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
273  WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
274  WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
275  status=WizardFalse;
276  switch (entropy_info->entropy)
277  {
278    case BZIPEntropy:
279    {
280      BZIPInfo
281        *bzip_info;
282
283      bzip_info=(BZIPInfo *) entropy_info->handle;
284      status=IncreaseBZIP(bzip_info,message,exception);
285      break;
286    }
287    case ZIPEntropy:
288    {
289      ZIPInfo
290        *zip_info;
291
292      zip_info=(ZIPInfo *) entropy_info->handle;
293      status=IncreaseZIP(zip_info,message,exception);
294      break;
295    }
296    default:
297      ThrowWizardFatalError(EntropyDomain,EnumerateError);
298  }
299  return(status);
300}
301
302/*
303%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304%                                                                             %
305%                                                                             %
306%                                                                             %
307%   R e s t o r e E n t r o p y                                               %
308%                                                                             %
309%                                                                             %
310%                                                                             %
311%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312%
313%  RestoreEntropy() restores the messages to its original entropy.
314%
315%  The format of the RestoreEntropy method is:
316%
317%      unsigned ing RestoreEntropy(EntropyInfo *entropy_info,
318%        const size_t length,const StringInfo *message,ExceptionInfo *exception)
319%
320%  A description of each parameter follows:
321%
322%    o entropy_info: The address of a structure of type EntropyInfo.
323%
324%    o length: The  the total size of the destination buffer, which must be
325%      large enough to hold the entire uncompressed data.
326%
327%    o message: The message.
328%
329%    o exception: Return any errors or warnings in this structure.
330%
331*/
332WizardExport WizardBooleanType RestoreEntropy(EntropyInfo *entropy_info,
333  const size_t length,const StringInfo *message,ExceptionInfo *exception)
334{
335  WizardBooleanType
336    status;
337
338  /*
339    Restore the message entropy.
340  */
341  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
342  WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
343  WizardAssert(EntropyDomain,entropy_info->signature == WizardSignature);
344  WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
345  status=WizardFalse;
346  switch (entropy_info->entropy)
347  {
348    case BZIPEntropy:
349    {
350      BZIPInfo
351        *bzip_info;
352
353      bzip_info=(BZIPInfo *) entropy_info->handle;
354      status=RestoreBZIP(bzip_info,length,message,exception);
355      break;
356    }
357    case ZIPEntropy:
358    {
359      ZIPInfo
360        *zip_info;
361
362      zip_info=(ZIPInfo *) entropy_info->handle;
363      status=RestoreZIP(zip_info,length,message,exception);
364      break;
365    }
366    default:
367      ThrowWizardFatalError(EntropyDomain,EnumerateError);
368  }
369  return(status);
370}
Note: See TracBrowser for help on using the browser.