MagickCore 7.0.10
entropy.c
Go to the documentation of this file.
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% 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/entropy.h"
46#include "wizard/exception.h"
48#include "wizard/lzma.h"
49#include "wizard/memory_.h"
50#include "wizard/zip.h"
51
52/*
53 Typedef declarations.
54*/
56{
59
60 void
62
63 time_t
65
66 size_t
68};
69
70/*
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72% %
73% %
74% %
75% A c q u i r e E n t r o p y I n f o %
76% %
77% %
78% %
79%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80%
81% AcquireEntropyInfo() allocates the EntropyInfo structure.
82%
83% The format of the AcquireEntropyInfo method is:
84%
85% EntropyInfo *AcquireEntropyInfo(const EntropyType entropy,
86% const size_t level)
87%
88% A description of each parameter follows:
89%
90% o entropy: The entropy type.
91%
92% o level: entropy level: 1 is best speed, 9 is more entropy.
93%
94*/
96 const size_t level)
97{
99 *entropy_info;
100
101 entropy_info=(EntropyInfo *) AcquireWizardMemory(sizeof(*entropy_info));
102 if (entropy_info == (EntropyInfo *) NULL)
104 (void) memset(entropy_info,0,sizeof(*entropy_info));
105 entropy_info->entropy=entropy;
106 switch (entropy_info->entropy)
107 {
108 case BZIPEntropy:
109 {
110 entropy_info->handle=(EntropyInfo *) AcquireBZIPInfo(level);
111 break;
112 }
113 case LZMAEntropy:
114 {
115 entropy_info->handle=(EntropyInfo *) AcquireLZMAInfo(level);
116 break;
117 }
118 case ZIPEntropy:
119 {
120 entropy_info->handle=(EntropyInfo *) AcquireZIPInfo(level);
121 break;
122 }
123 default:
125 }
126 entropy_info->timestamp=time((time_t *) NULL);
127 entropy_info->signature=WizardSignature;
128 return(entropy_info);
129}
130
131/*
132e%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133% %
134% %
135% %
136% D e s t r o y E n t r o p y I n f o %
137% %
138% %
139% %
140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141%
142% DestroyEntropyInfo() zeros memory associated with the EntropyInfo
143% structure.
144%
145% The format of the DestroyEntropyInfo method is:
146%
147% EntropyInfo *DestroyEntropyInfo(EntropyInfo *entropy_info)
148%
149% A description of each parameter follows:
150%
151% o entropy_info: The entropy info.
152%
153*/
155{
157 WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
159 if (entropy_info->handle != (EntropyInfo *) NULL)
160 switch (entropy_info->entropy)
161 {
162 case BZIPEntropy:
163 {
164 entropy_info->handle=(void *) DestroyBZIPInfo((BZIPInfo *)
165 entropy_info->handle);
166 break;
167 }
168 case LZMAEntropy:
169 {
170 entropy_info->handle=(void *) DestroyLZMAInfo((LZMAInfo *)
171 entropy_info->handle);
172 break;
173 }
174 case ZIPEntropy:
175 {
176 entropy_info->handle=(void *) DestroyZIPInfo((ZIPInfo *)
177 entropy_info->handle);
178 break;
179 }
180 default:
181 break;
182 }
183 entropy_info->signature=(~WizardSignature);
184 entropy_info=(EntropyInfo *) RelinquishWizardMemory(entropy_info);
185 return(entropy_info);
186}
187
188/*
189%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190% %
191% %
192% %
193% G e t E n t r o p y C h a o s %
194% %
195% %
196% %
197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198%
199% GetEntropyChaos() returns Entropy chaos.
200%
201% The format of the GetEntropyChaos method is:
202%
203% const StringInfo *GetEntropyChaos(const EntropyInfo *entropy_info)
204%
205% A description of each parameter follows:
206%
207% o entropy_info: The entropy info.
208%
209*/
211{
212 const StringInfo
213 *chaos;
214
215 /*
216 Increase the message entropy.
217 */
219 WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
221 switch (entropy_info->entropy)
222 {
223 case BZIPEntropy:
224 {
226 *bzip_info;
227
228 bzip_info=(BZIPInfo *) entropy_info->handle;
229 chaos=GetBZIPChaos(bzip_info);
230 break;
231 }
232 case LZMAEntropy:
233 {
235 *lzma_info;
236
237 lzma_info=(LZMAInfo *) entropy_info->handle;
238 chaos=GetLZMAChaos(lzma_info);
239 break;
240 }
241 case ZIPEntropy:
242 {
243 ZIPInfo
244 *zip_info;
245
246 zip_info=(ZIPInfo *) entropy_info->handle;
247 chaos=GetZIPChaos(zip_info);
248 break;
249 }
250 default:
252 }
253 return(chaos);
254}
255
256/*
257%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258% %
259% %
260% %
261% I n c r e a s e E n t r o p y %
262% %
263% %
264% %
265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266%
267% IncreaseEntropy() increases the entropy of a message.
268%
269% The format of the IncreaseEntropy method is:
270%
271% WizardBooleanType IncreaseEntropy(EntropyInfo *entropy_info,
272% const StringInfo *message,ExceptionInfo *exception)
273%
274% A description of each parameter follows:
275%
276% o entropy_info: The address of a structure of type EntropyInfo.
277%
278% o message: The message.
279%
280% o exception: Return any errors or warnings in this structure.
281%
282*/
284 const StringInfo *message,ExceptionInfo *exception)
285{
287 status;
288
289 /*
290 Increase the message entropy.
291 */
293 WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
295 WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
296 status=WizardFalse;
297 switch (entropy_info->entropy)
298 {
299 case BZIPEntropy:
300 {
302 *bzip_info;
303
304 bzip_info=(BZIPInfo *) entropy_info->handle;
305 status=IncreaseBZIP(bzip_info,message,exception);
306 break;
307 }
308 case LZMAEntropy:
309 {
311 *lzma_info;
312
313 lzma_info=(LZMAInfo *) entropy_info->handle;
314 status=IncreaseLZMA(lzma_info,message,exception);
315 break;
316 }
317 case ZIPEntropy:
318 {
319 ZIPInfo
320 *zip_info;
321
322 zip_info=(ZIPInfo *) entropy_info->handle;
323 status=IncreaseZIP(zip_info,message,exception);
324 break;
325 }
326 default:
328 }
329 return(status);
330}
331
332/*
333%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334% %
335% %
336% %
337% R e s t o r e E n t r o p y %
338% %
339% %
340% %
341%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342%
343% RestoreEntropy() restores the messages to its original entropy.
344%
345% The format of the RestoreEntropy method is:
346%
347% unsigned ing RestoreEntropy(EntropyInfo *entropy_info,
348% const size_t length,const StringInfo *message,ExceptionInfo *exception)
349%
350% A description of each parameter follows:
351%
352% o entropy_info: The address of a structure of type EntropyInfo.
353%
354% o length: The the total size of the destination buffer, which must be
355% large enough to hold the entire uncompressed data.
356%
357% o message: The message.
358%
359% o exception: Return any errors or warnings in this structure.
360%
361*/
363 const size_t length,const StringInfo *message,ExceptionInfo *exception)
364{
366 status;
367
368 /*
369 Restore the message entropy.
370 */
372 WizardAssert(EntropyDomain,entropy_info != (EntropyInfo *) NULL);
374 WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
375 status=WizardFalse;
376 switch (entropy_info->entropy)
377 {
378 case BZIPEntropy:
379 {
381 *bzip_info;
382
383 bzip_info=(BZIPInfo *) entropy_info->handle;
384 status=RestoreBZIP(bzip_info,length,message,exception);
385 break;
386 }
387 case LZMAEntropy:
388 {
390 *lzma_info;
391
392 lzma_info=(LZMAInfo *) entropy_info->handle;
393 status=RestoreLZMA(lzma_info,length,message,exception);
394 break;
395 }
396 case ZIPEntropy:
397 {
398 ZIPInfo
399 *zip_info;
400
401 zip_info=(ZIPInfo *) entropy_info->handle;
402 status=RestoreZIP(zip_info,length,message,exception);
403 break;
404 }
405 default:
407 }
408 return(status);
409}
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
WizardExport BZIPInfo * AcquireBZIPInfo(const size_t level)
Definition bzip.c:93
WizardExport EntropyInfo * AcquireEntropyInfo(const EntropyType entropy, const size_t level)
Definition entropy.c:95
WizardExport EntropyInfo * DestroyEntropyInfo(EntropyInfo *entropy_info)
Definition entropy.c:154
WizardExport WizardBooleanType IncreaseEntropy(EntropyInfo *entropy_info, const StringInfo *message, ExceptionInfo *exception)
Definition entropy.c:283
WizardExport WizardBooleanType RestoreEntropy(EntropyInfo *entropy_info, const size_t length, const StringInfo *message, ExceptionInfo *exception)
Definition entropy.c:362
WizardExport const StringInfo * GetEntropyChaos(const EntropyInfo *entropy_info)
Definition entropy.c:210
EntropyType
Definition entropy.h:29
@ BZIPEntropy
Definition entropy.h:33
@ ZIPEntropy
Definition entropy.h:32
@ LZMAEntropy
Definition entropy.h:34
#define WizardAssert(domain, predicate)
#define ThrowWizardFatalError(domain, error)
@ EntropyDomain
Definition exception.h:31
@ EnumerateError
Definition exception.h:50
@ 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 WizardBooleanType IncreaseLZMA(LZMAInfo *lzma_info, const StringInfo *message, ExceptionInfo *exception)
Definition lzma.c:213
WizardExport WizardBooleanType RestoreLZMA(LZMAInfo *lzma_info, const size_t length, const StringInfo *message, ExceptionInfo *exception)
Definition lzma.c:311
WizardExport const StringInfo * GetLZMAChaos(const LZMAInfo *lzma_info)
Definition lzma.c:134
WizardExport LZMAInfo * DestroyLZMAInfo(LZMAInfo *lzma_info)
Definition lzma.c:164
WizardExport LZMAInfo * AcquireLZMAInfo(const size_t level)
Definition lzma.c:96
WizardExport void * AcquireWizardMemory(const size_t size)
Definition memory.c:586
WizardExport void * RelinquishWizardMemory(void *memory)
Definition memory.c:1039
#define WizardExport
#define WizardSignature
EntropyType entropy
Definition entropy.c:58
time_t timestamp
Definition entropy.c:64
size_t signature
Definition entropy.c:67
void * handle
Definition entropy.c:61
Definition zip.c:54
WizardBooleanType
Definition wizard-type.h:26
@ WizardFalse
Definition wizard-type.h:27
WizardExport ZIPInfo * DestroyZIPInfo(ZIPInfo *zip_info)
Definition zip.c:161
WizardExport ZIPInfo * AcquireZIPInfo(const size_t level)
Definition zip.c:93
WizardExport WizardBooleanType RestoreZIP(ZIPInfo *zip_info, const size_t length, const StringInfo *message, ExceptionInfo *exception)
Definition zip.c:291
WizardExport const StringInfo * GetZIPChaos(const ZIPInfo *zip_info)
Definition zip.c:131
WizardExport WizardBooleanType IncreaseZIP(ZIPInfo *zip_info, const StringInfo *message, ExceptionInfo *exception)
Definition zip.c:210