MagickCore 7.0.10
zip.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% ZZZZZ IIIII PPPP %
7% ZZ I P P %
8% ZZZ I PPPP %
9% ZZ I P %
10% ZZZZZ IIIII P %
11% %
12% %
13% Wizard's Toolkit Zip 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/exception.h"
46#include "wizard/memory_.h"
47#include "wizard/zip.h"
48#include "zlib.h"
49
50/*
51 Typedef declarations.
52*/
54{
55 z_stream
57
60
61 size_t
63
64 ssize_t
66
67 size_t
69};
70
71/*
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73% %
74% %
75% %
76% A c q u i r e Z I P I n f o %
77% %
78% %
79% %
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81%
82% AcquireZIPInfo() allocates the ZIPInfo structure.
83%
84% The format of the AcquireZIPInfo method is:
85%
86% ZIPInfo *AcquireZIPInfo(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 *zip_info;
97
98 zip_info=(ZIPInfo *) AcquireWizardMemory(sizeof(*zip_info));
99 if (zip_info == (ZIPInfo *) NULL)
101 (void) memset(zip_info,0,sizeof(*zip_info));
102 zip_info->chaos=AcquireStringInfo(1);
103 zip_info->level=level;
104 zip_info->timestamp=(ssize_t) (time((time_t *) NULL)-WizardEpoch);
105 zip_info->signature=WizardSignature;
106 return(zip_info);
107}
108
109/*
110%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111% %
112% %
113% %
114% G e t Z I P C h a o s %
115% %
116% %
117% %
118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119%
120% GetZIPChaos() returns ZIP chaos.
121%
122% The format of the GetZIPChaos method is:
123%
124% const StringInfo *GetZIPChaos(const ZIPInfo *zip_info)
125%
126% A description of each parameter follows:
127%
128% o zip_info: The zip info.
129%
130*/
132{
134 WizardAssert(EntropyDomain,zip_info != (ZIPInfo *) NULL);
136 return(zip_info->chaos);
137}
138
139/*
140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141% %
142% %
143% %
144% D e s t r o y Z I P I n f o %
145% %
146% %
147% %
148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149%
150% DestroyZIPInfo() zeros memory associated with the ZIPInfo structure.
151%
152% The format of the DestroyZIPInfo method is:
153%
154% ZIPInfo *DestroyZIPInfo(ZIPInfo *zip_info)
155%
156% A description of each parameter follows:
157%
158% o zip_info: The zip info.
159%
160*/
162{
164 WizardAssert(EntropyDomain,zip_info != (ZIPInfo *) NULL);
166 if (zip_info->chaos != (StringInfo *) NULL)
167 zip_info->chaos=DestroyStringInfo(zip_info->chaos);
168 zip_info=(ZIPInfo *) RelinquishWizardMemory(zip_info);
169 return(zip_info);
170}
171
172/*
173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174% %
175% %
176% %
177% I n c r e a s e Z i p %
178% %
179% %
180% %
181%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182%
183% IncreaseZIP() compresses the message to increase its entropy.
184%
185% The format of the IncreaseZIP method is:
186%
187% WizardBooleanType IncreaseZIP(ZIPInfo *zip_info,
188% const StringInfo *message,ExceptionInfo *exception)
189%
190% A description of each parameter follows:
191%
192% o zip_info: The address of a structure of type ZIPInfo.
193%
194% o message: The message.
195%
196% o exception: Return any errors or warnings in this structure.
197%
198*/
199
200static voidpf AcquireZIPMemory(voidpf context,uInt items,uInt size)
201{
202 return((voidpf) AcquireQuantumMemory(items,size));
203}
204
205static void RelinquishZIPMemory(voidpf context,voidpf memory)
206{
207 memory=RelinquishWizardMemory(memory);
208}
209
211 const StringInfo *message,ExceptionInfo *exception)
212{
213 int
214 status;
215
216 z_stream
217 stream;
218
219 /*
220 Increase the message entropy.
221 */
223 WizardAssert(EntropyDomain,zip_info != (ZIPInfo *) NULL);
225 WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
226 (void) memset(&stream,0,sizeof(stream));
227 stream.zalloc=AcquireZIPMemory;
228 stream.zfree=RelinquishZIPMemory;
229 stream.opaque=(voidpf) NULL;
230 status=deflateInit(&stream,(int) zip_info->level);
231 if (status != Z_OK)
232 {
234 "unable to increase entropy `%s'",strerror(errno));
235 return(WizardFalse);
236 }
237 stream.next_in=(Bytef *) GetStringInfoDatum(message);
238 stream.avail_in=(uInt) GetStringInfoLength(message);
239 SetStringInfoLength(zip_info->chaos,(size_t) deflateBound(&stream,
240 (unsigned long) GetStringInfoLength(message)));
241 stream.next_out=(Bytef *) GetStringInfoDatum(zip_info->chaos);
242 stream.avail_out=(uInt) GetStringInfoLength(zip_info->chaos);
243 status=deflate(&stream,Z_FINISH);
244 if (status != Z_STREAM_END)
245 {
247 "unable to increase entropy `%s'",strerror(errno));
248 return(WizardFalse);
249 }
250 SetStringInfoLength(zip_info->chaos,(size_t) stream.total_out);
251 status=deflateEnd(&stream);
252 if (status != Z_OK)
253 {
255 "unable to increase entropy `%s'",strerror(errno));
256 return(WizardFalse);
257 }
258 return(WizardTrue);
259}
260
261/*
262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263% %
264% %
265% %
266% R e s t o r e Z i p %
267% %
268% %
269% %
270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271%
272% RestoreZIP() uncompresses the message to restore its original entropy.
273%
274% The format of the RestoreZIP method is:
275%
276% WizardBooleanType RestoreZIP(ZIPInfo *zip_info,const size_t length,
277% const StringInfo *message,ExceptionInfo *exception)
278%
279% A description of each parameter follows:
280%
281% o zip_info: The address of a structure of type ZIPInfo.
282%
283% o length: The total size of the destination buffer, which must be large
284% enough to hold the entire uncompressed data.
285%
286% o message: The message.
287%
288% o exception: Return any errors or warnings in this structure.
289%
290*/
291WizardExport WizardBooleanType RestoreZIP(ZIPInfo *zip_info,const size_t length,
292 const StringInfo *message,ExceptionInfo *exception)
293{
294 int
295 status;
296
297 z_stream
298 stream;
299
300 /*
301 Restore the message entropy.
302 */
303 WizardAssert(EntropyDomain,zip_info != (ZIPInfo *) NULL);
306 WizardAssert(EntropyDomain,message != (const StringInfo *) NULL);
307 (void) memset(&stream,0,sizeof(stream));
308 stream.zalloc=AcquireZIPMemory;
309 stream.zfree=RelinquishZIPMemory;
310 stream.opaque=(voidpf) NULL;
311 status=inflateInit(&stream);
312 if (status != Z_OK)
313 {
315 "unable to restore entropy `%s'",strerror(errno));
316 return(WizardFalse);
317 }
318 stream.next_in=(Bytef *) GetStringInfoDatum(message);
319 stream.avail_in=(uInt) GetStringInfoLength(message);
320 SetStringInfoLength(zip_info->chaos,length);
321 stream.next_out=(Bytef *) GetStringInfoDatum(zip_info->chaos);
322 stream.avail_out=(uInt) GetStringInfoLength(zip_info->chaos);
323 status=inflate(&stream,Z_FINISH);
324 if (status != Z_STREAM_END)
325 {
327 "unable to restore entropy `%s'",strerror(errno));
328 return(WizardFalse);
329 }
330 SetStringInfoLength(zip_info->chaos,(size_t) stream.total_out);
331 status=inflateEnd(&stream);
332 if (status != Z_OK)
333 {
335 "unable to restore entropy `%s'",strerror(errno));
336 return(WizardFalse);
337 }
338 return(WizardTrue);
339}
#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 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
Definition zip.c:54
size_t signature
Definition zip.c:68
ssize_t timestamp
Definition zip.c:65
z_stream stream
Definition zip.c:56
StringInfo * chaos
Definition zip.c:59
size_t level
Definition zip.c:62
#define WizardEpoch
Definition studio.h:305
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28
@ 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
static void RelinquishZIPMemory(voidpf context, voidpf memory)
Definition zip.c:205
static voidpf AcquireZIPMemory(voidpf context, uInt items, uInt size)
Definition zip.c:200