MagickCore 7.0.10
crc64.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% CCCC RRRR CCCC %
6% C R R C %
7% C RRRR C %
8% C R R C %
9% CCCC R R CCCC %
10% %
11% %
12% Wizard's Toolkit Cyclic Redunancy Checksum 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 Include declarations.
40*/
41#include "wizard/studio.h"
42#include "wizard/crc64.h"
43#include "wizard/exception.h"
45#include "wizard/memory_.h"
46
47/*
48 Define declarations.
49*/
50#define CRC64Blocksize 32
51#define CRC64Digestsize 8
52
53/*
54 Typedef declarations.
55*/
57{
58 unsigned int
61
64
68
69 time_t
71
72 size_t
74};
75
76/*
77%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78% %
79% %
80% %
81% A c q u i r e C R C 6 4 I n f o %
82% %
83% %
84% %
85%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86%
87% AcquireCRC64Info() allocate the CRC64Info structure.
88%
89% The format of the AcquireCRC64Info method is:
90%
91% CRC64Info *AcquireCRC64Info(void)
92%
93*/
95{
97 *crc_info;
98
99 crc_info=(CRC64Info *) AcquireWizardMemory(sizeof(*crc_info));
100 if (crc_info == (CRC64Info *) NULL)
102 (void) memset(crc_info,0,sizeof(*crc_info));
103 crc_info->digestsize=CRC64Digestsize;
104 crc_info->blocksize=CRC64Blocksize;
106 crc_info->crc_xor=(WizardSizeType *) AcquireQuantumMemory(256UL,
107 sizeof(*crc_info->crc_xor));
108 if (crc_info->crc_xor == (WizardSizeType *) NULL)
110 crc_info->timestamp=time((time_t *) NULL);
111 crc_info->signature=WizardSignature;
112 return(crc_info);
113}
114
115/*
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117% %
118% %
119% %
120% D e s t r o y C R C 6 4 I n f o %
121% %
122% %
123% %
124%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125%
126% DestroyCRC64Info() zeros memory associated with the CRC64Info structure.
127%
128% The format of the DestroyCRC64Info method is:
129%
130% CRC64Info *DestroyCRC64Info(CRC64Info *crc_info)
131%
132% A description of each parameter follows:
133%
134% o crc_info: The cipher crc_info.
135%
136*/
138{
140 assert(crc_info != (CRC64Info *) NULL);
141 assert(crc_info->signature == WizardSignature);
142 if (crc_info->digest != (StringInfo *) NULL)
143 crc_info->digest=DestroyStringInfo(crc_info->digest);
144 if (crc_info->crc_xor != (WizardSizeType *) NULL)
145 crc_info->crc_xor=(WizardSizeType *)
147 crc_info->signature=(~WizardSignature);
148 crc_info=(CRC64Info *) RelinquishWizardMemory(crc_info);
149 return(crc_info);
150}
151
152/*
153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154% %
155% %
156% %
157% F i n a l i z e C R C 6 4 %
158% %
159% %
160% %
161%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162%
163% FinalizeCRC64() finalizes the CRC64 message digest computation.
164%
165% The format of the FinalizeCRC64 method is:
166%
167% WizardBooleanType FinalizeCRC64(CRC64Info *crc_info)
168%
169% A description of each parameter follows:
170%
171% o crc_info: The address of a structure of type CRC64Info.
172%
173*/
175{
176 unsigned char
177 *datum;
178
180 assert(crc_info != (CRC64Info *) NULL);
181 assert(crc_info->signature == WizardSignature);
182 datum=GetStringInfoDatum(crc_info->digest);
183 datum[0]=(unsigned char) (crc_info->crc >> 56);
184 datum[1]=(unsigned char) (crc_info->crc >> 48);
185 datum[2]=(unsigned char) (crc_info->crc >> 40);
186 datum[3]=(unsigned char) (crc_info->crc >> 32);
187 datum[4]=(unsigned char) (crc_info->crc >> 24);
188 datum[5]=(unsigned char) (crc_info->crc >> 16);
189 datum[6]=(unsigned char) (crc_info->crc >> 8);
190 datum[7]=(unsigned char) (crc_info->crc >> 0);
191 return(WizardTrue);
192}
193
194/*
195%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196% %
197% %
198% %
199% G e t C R C 6 4 B l o c k s i z e %
200% %
201% %
202% %
203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204%
205% GetCRC64Blocksize() returns the CRC64 blocksize.
206%
207% The format of the GetCRC64Blocksize method is:
208%
209% unsigned int *GetCRC64Blocksize(const CRC64Info *crc64_info)
210%
211% A description of each parameter follows:
212%
213% o crc64_info: The crc64 info.
214%
215*/
216WizardExport unsigned int GetCRC64Blocksize(const CRC64Info *crc64_info)
217{
219 WizardAssert(CipherDomain,crc64_info != (CRC64Info *) NULL);
221 return(crc64_info->blocksize);
222}
223
224/*
225%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226% %
227% %
228% %
229% G e t C R C 6 4 C y c l i c R e d u n d a n c y C h e c k %
230% %
231% %
232% %
233%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234%
235% GetCRC64CyclicRedundancyCheck() returns the CRC64 cyclic redunancy check.
236%
237% The format of the GetCRC64CyclicRedundancyCheck method is:
238%
239% WizardSizeType *GetCRC64CyclicRedundancyCheck(
240% const CRC64Info *crc64_info)
241%
242% A description of each parameter follows:
243%
244% o crc64_info: The crc64 info.
245%
246*/
248 const CRC64Info *crc64_info)
249{
251 WizardAssert(CipherDomain,crc64_info != (CRC64Info *) NULL);
253 return(crc64_info->crc);
254}
255
256/*
257%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258% %
259% %
260% %
261% G e t C R C 6 4 D i g e s t %
262% %
263% %
264% %
265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266%
267% GetCRC64Digest() returns the CRC64 digest.
268%
269% The format of the GetCRC64Digest method is:
270%
271% const StringInfo *GetCRC64Digest(const CRC64Info *crc64_info)
272%
273% A description of each parameter follows:
274%
275% o crc64_info: The crc64 info.
276%
277*/
279{
281 WizardAssert(HashDomain,crc64_info != (CRC64Info *) NULL);
283 return(crc64_info->digest);
284}
285
286/*
287%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288% %
289% %
290% %
291% G e t C R C 6 4 D i g e s t s i z e %
292% %
293% %
294% %
295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296%
297% GetCRC64Digestsize() returns the CRC64 digest size.
298%
299% The format of the GetCRC64Digestsize method is:
300%
301% unsigned int *GetCRC64Digestsize(const CRC64Info *crc64_info)
302%
303% A description of each parameter follows:
304%
305% o crc64_info: The crc64 info.
306%
307*/
308WizardExport unsigned int GetCRC64Digestsize(const CRC64Info *crc64_info)
309{
311 WizardAssert(CipherDomain,crc64_info != (CRC64Info *) NULL);
313 return(crc64_info->digestsize);
314}
315
316/*
317%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318% %
319% %
320% %
321% I n i t i a l i z e C R C 6 4 %
322% %
323% %
324% %
325%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326%
327% IntializeCRC64() intializes the CRC64 digest.
328%
329% The format of the InitializeCRC64 method is:
330%
331% WizardBooleanType InitializeCRC64(crc_info)
332%
333% A description of each parameter follows:
334%
335% o crc_info: The address of a structure of type CRC64Info.
336%
337*/
339{
340 ssize_t
341 i,
342 j;
343
345 alpha;
346
347 /*
348 Load magic initialization constants.
349 */
351 assert(crc_info != (CRC64Info *) NULL);
352 assert(crc_info->signature == WizardSignature);
353 crc_info->crc=0;
354 for (i=0; i < 256; i++)
355 {
356 alpha=(WizardSizeType) i;
357 for (j=0; j < 8; j++)
358 if ((alpha & 0x01) != 0)
359 alpha=(WizardSizeType) ((alpha >> 1) ^
360 WizardULLConstant(0xd800000000000000));
361 else
362 alpha>>=1;
363 crc_info->crc_xor[i]=alpha;
364 }
365 return(WizardTrue);
366}
367
368/*
369%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370% %
371% %
372% %
373% U p d a t e C R C 6 4 %
374% %
375% %
376% %
377%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378%
379% UpdateCRC64() updates the CRC64 message digest
380%
381% The format of the UpdateCRC64 method is:
382%
383% WizardBooleanType UpdateCRC64(CRC64Info *crc_info,
384% const StringInfo *message)
385%
386% A description of each parameter follows:
387%
388% o crc_info: The address of a structure of type CRC64Info.
389%
390*/
392 const StringInfo *message)
393{
394 const unsigned char
395 *p;
396
397 size_t
398 i;
399
400 /*
401 Update the CRC64 accumulator.
402 */
404 assert(crc_info != (CRC64Info *) NULL);
405 assert(crc_info->signature == WizardSignature);
406 p=GetStringInfoDatum(message);
407 for (i=0; i < GetStringInfoLength(message); i++)
408 {
409 crc_info->crc=(crc_info->crc >> 8) ^
410 crc_info->crc_xor[(crc_info->crc ^ (WizardSizeType) *p) & 0xff];
411 p++;
412 }
413 return(WizardTrue);
414}
WizardExport unsigned int GetCRC64Digestsize(const CRC64Info *crc64_info)
Definition crc64.c:308
WizardExport unsigned int GetCRC64Blocksize(const CRC64Info *crc64_info)
Definition crc64.c:216
WizardExport CRC64Info * DestroyCRC64Info(CRC64Info *crc_info)
Definition crc64.c:137
WizardExport WizardBooleanType InitializeCRC64(CRC64Info *crc_info)
Definition crc64.c:338
WizardExport CRC64Info * AcquireCRC64Info(void)
Definition crc64.c:94
WizardExport WizardSizeType GetCRC64CyclicRedundancyCheck(const CRC64Info *crc64_info)
Definition crc64.c:247
#define CRC64Blocksize
Definition crc64.c:50
WizardExport const StringInfo * GetCRC64Digest(const CRC64Info *crc64_info)
Definition crc64.c:278
WizardExport WizardBooleanType FinalizeCRC64(CRC64Info *crc_info)
Definition crc64.c:174
#define CRC64Digestsize
Definition crc64.c:51
WizardExport WizardBooleanType UpdateCRC64(CRC64Info *crc_info, const StringInfo *message)
Definition crc64.c:391
#define WizardAssert(domain, predicate)
#define ThrowWizardFatalError(domain, error)
@ HashDomain
Definition exception.h:30
@ CipherDomain
Definition exception.h:34
@ 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 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
unsigned int blocksize
Definition crc64.c:60
WizardSizeType crc
Definition crc64.c:67
size_t signature
Definition crc64.c:73
unsigned int digestsize
Definition crc64.c:59
StringInfo * digest
Definition crc64.c:63
time_t timestamp
Definition crc64.c:70
WizardSizeType * crc_xor
Definition crc64.c:66
#define WizardULLConstant(c)
Definition wizard-type.h:36
size_t WizardSizeType
Definition wizard-type.h:51
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28