MagickCore 7.0.10
lzma.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% L ZZZZZ M M AAA %
6% L ZZ MM MM A A %
7% L ZZZ M M M AAAAA %
8% L ZZ M M A A %
9% LLLLL ZZZZZ M M A A %
10% %
11% %
12% Wizard's Toolkit LZMA Entropy Methods %
13% %
14% Software Design %
15% Cristy %
16% March 2003 %
17% %
18% %
19% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
20% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
25% https://imagemagick.org/script/license.php %
26% %
27% Unless required by applicable law or agreed to in writing, software %
28% distributed under the License is distributed on an "AS IS" BASIS, %
29% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30% See the License for the specific language governing permissions and %
31% limitations under the License. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "wizard/studio.h"
43#include "wizard/exception.h"
45#include "wizard/memory_.h"
46#include "wizard/lzma.h"
47#if defined(WIZARDSTOOLKIT_LZMA_DELEGATE)
48#include <lzma.h>
49#endif
50
51/*
52 Typedef declarations.
53*/
55{
56#if defined(WIZARDSTOOLKIT_LZMA_DELEGATE)
57 lzma_stream
58 stream;
59#endif
60
63
64 size_t
66
67 ssize_t
69
70 size_t
72};
73
74/*
75%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76% %
77% %
78% %
79% A c q u i r e L Z M A I n f o %
80% %
81% %
82% %
83%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84%
85% AcquireLZMAInfo() allocates the LZMAInfo structure.
86%
87% The format of the AcquireLZMAInfo method is:
88%
89% LZMAInfo *AcquireLZMAInfo(const size_t level)
90%
91% A description of each parameter follows:
92%
93% o level: entropy level: 1 is best speed, 9 is more entropy.
94%
95*/
97{
99 *lzma_info;
100
101 lzma_info=(LZMAInfo *) AcquireWizardMemory(sizeof(*lzma_info));
102 if (lzma_info == (LZMAInfo *) NULL)
104 (void) memset(lzma_info,0,sizeof(*lzma_info));
105 lzma_info->chaos=AcquireStringInfo(1);
106 lzma_info->level=level;
107 lzma_info->timestamp=(ssize_t) (time((time_t *) NULL)-WizardEpoch);
108 lzma_info->signature=WizardSignature;
109 return(lzma_info);
110}
111
112/*
113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114% %
115% %
116% %
117% G e t L Z M A C h a o s %
118% %
119% %
120% %
121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122%
123% GetLZMAChaos() returns LZMA chaos.
124%
125% The format of the GetLZMAChaos method is:
126%
127% const StringInfo *GetLZMAChaos(const LZMAInfo *lzma_info)
128%
129% A description of each parameter follows:
130%
131% o lzma_info: The lzma info.
132%
133*/
135{
137 WizardAssert(EntropyDomain,lzma_info != (LZMAInfo *) NULL);
139 return(lzma_info->chaos);
140}
141
142/*
143%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144% %
145% %
146% %
147% D e s t r o y L Z M A I n f o %
148% %
149% %
150% %
151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152%
153% DestroyLZMAInfo() zeros memory associated with the LZMAInfo structure.
154%
155% The format of the DestroyLZMAInfo method is:
156%
157% LZMAInfo *DestroyLZMAInfo(LZMAInfo *lzma_info)
158%
159% A description of each parameter follows:
160%
161% o lzma_info: The lzma info.
162%
163*/
165{
167 WizardAssert(EntropyDomain,lzma_info != (LZMAInfo *) NULL);
169 if (lzma_info->chaos != (StringInfo *) NULL)
170 lzma_info->chaos=DestroyStringInfo(lzma_info->chaos);
171 lzma_info=(LZMAInfo *) RelinquishWizardMemory(lzma_info);
172 return(lzma_info);
173}
174
175/*
176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177% %
178% %
179% %
180% I n c r e a s e L Z M A %
181% %
182% %
183% %
184%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185%
186% IncreaseLZMA() compresses the message to increase its entropy.
187%
188% The format of the IncreaseLZMA method is:
189%
190% WizardBooleanType IncreaseLZMA(LZMAInfo *lzma_info,
191% const StringInfo *message,ExceptionInfo *exception)
192%
193% A description of each parameter follows:
194%
195% o lzma_info: The address of a structure of type LZMAInfo.
196%
197% o message: The message.
198%
199% o exception: Return any errors or warnings in this structure.
200%
201*/
202
203static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
204{
205 return((void *) AcquireQuantumMemory(items,size));
206}
207
208static void RelinquishLZMAMemory(void *context,void *memory)
209{
210 memory=RelinquishWizardMemory(memory);
211}
212
214 const StringInfo *message,ExceptionInfo *exception)
215{
216#if defined(WIZARDSTOOLKIT_LZMA_DELEGATE)
217#define LZMAMaxExtent(x) ((x)+((x)/3)+128)
218
219 int
220 status;
221
222 lzma_allocator
223 allocator;
224
225 lzma_stream
226 initialize_lzma = LZMA_STREAM_INIT,
227 stream;
228
229 /*
230 Increase the message entropy.
231 */
233 WizardAssert(EntropyDomain,lzma_info != (LZMAInfo *) NULL);
235 WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
236 (void) memset(&allocator,0,sizeof(allocator));
237 allocator.alloc=AcquireLZMAMemory;
238 allocator.free=RelinquishLZMAMemory;
239 stream=initialize_lzma;
240 stream.allocator=&allocator;
241 stream.next_in=GetStringInfoDatum(message);
242 stream.avail_in=GetStringInfoLength(message);
243 SetStringInfoLength(lzma_info->chaos,(size_t) LZMAMaxExtent(
244 GetStringInfoLength(message)));
245 stream.next_out=GetStringInfoDatum(lzma_info->chaos);
246 stream.avail_out=GetStringInfoLength(lzma_info->chaos);
247 status=lzma_easy_encoder(&stream,lzma_info->level,LZMA_CHECK_SHA256);
248 if (status != LZMA_OK)
249 {
251 "unable to increase entropy `%s'",strerror(errno));
252 lzma_end(&stream);
253 return(WizardFalse);
254 }
255 status=lzma_code(&stream,LZMA_RUN);
256 if (status != LZMA_OK)
257 {
259 "unable to increase entropy `%s'",strerror(errno));
260 lzma_end(&stream);
261 return(WizardFalse);
262 }
263 status=lzma_code(&stream,LZMA_FINISH);
264 if ((status != LZMA_STREAM_END) && (status != LZMA_OK))
265 {
267 "unable to restore entropy `%s'",strerror(errno));
268 lzma_end(&stream);
269 return(WizardFalse);
270 }
271 SetStringInfoLength(lzma_info->chaos,(size_t) stream.total_out);
272 lzma_end(&stream);
273 return(WizardTrue);
274#else
276 "unable to increase entropy `%s'","LZMA delegate support not builtin");
277 return(WizardFalse);
278#endif
279}
280
281/*
282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283% %
284% %
285% %
286% R e s t o r e L Z M A %
287% %
288% %
289% %
290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291%
292% RestoreLZMA() uncompresses the message to restore its original entropy.
293%
294% The format of the RestoreLZMA method is:
295%
296% WizardBooleanType RestoreLZMA(LZMAInfo *lzma_info,const size_t length,
297% const StringInfo *message,ExceptionInfo *exception)
298%
299% A description of each parameter follows:
300%
301% o lzma_info: The address of a structure of type LZMAInfo.
302%
303% o length: The total size of the destination buffer, which must be large
304% enough to hold the entire uncompressed data.
305%
306% o message: The message.
307%
308% o exception: Return any errors or warnings in this structure.
309%
310*/
312 const size_t length,const StringInfo *message,ExceptionInfo *exception)
313{
314#if defined(WIZARDSTOOLKIT_LZMA_DELEGATE)
315 int
316 status;
317
318 lzma_allocator
319 allocator;
320
321 lzma_stream
322 initialize_lzma = LZMA_STREAM_INIT,
323 stream;
324
325 /*
326 Restore the message entropy.
327 */
328 WizardAssert(EntropyDomain,lzma_info != (LZMAInfo *) NULL);
331 WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
332 (void) memset(&allocator,0,sizeof(allocator));
333 allocator.alloc=AcquireLZMAMemory;
334 allocator.free=RelinquishLZMAMemory;
335 stream=initialize_lzma;
336 stream.allocator=&allocator;
337 stream.next_in=GetStringInfoDatum(message);
338 stream.avail_in=GetStringInfoLength(message);
339 SetStringInfoLength(lzma_info->chaos,length);
340 stream.next_out=GetStringInfoDatum(lzma_info->chaos);
341 stream.avail_out=GetStringInfoLength(lzma_info->chaos);
342 status=lzma_auto_decoder(&stream,-1,0);
343 if (status != LZMA_OK)
344 {
346 "unable to restore entropy `%s'",strerror(errno));
347 lzma_end(&stream);
348 return(WizardFalse);
349 }
350 status=lzma_code(&stream,LZMA_RUN);
351 if (status < 0)
352 {
354 "unable to restore entropy `%s'",strerror(errno));
355 lzma_end(&stream);
356 return(WizardFalse);
357 }
358 status=lzma_code(&stream,LZMA_FINISH);
359 if ((status != LZMA_STREAM_END) && (status != LZMA_OK))
360 {
362 "unable to restore entropy `%s'",strerror(errno));
363 lzma_end(&stream);
364 return(WizardFalse);
365 }
366 SetStringInfoLength(lzma_info->chaos,(size_t) stream.total_out);
367 lzma_end(&stream);
368 return(WizardTrue);
369#else
371 "unable to restore entropy `%s'","LZMA delegate support not builtin");
372 return(WizardFalse);
373#endif
374}
#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
@ 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
static void RelinquishLZMAMemory(void *context, void *memory)
Definition lzma.c:208
WizardExport const StringInfo * GetLZMAChaos(const LZMAInfo *lzma_info)
Definition lzma.c:134
static void * AcquireLZMAMemory(void *context, size_t items, size_t size)
Definition lzma.c:203
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 * 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
StringInfo * chaos
Definition lzma.c:62
size_t signature
Definition lzma.c:71
size_t level
Definition lzma.c:65
ssize_t timestamp
Definition lzma.c:68
#define WizardEpoch
Definition studio.h:305
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28
@ WizardFalse
Definition wizard-type.h:27