root/WizardsToolkit/trunk/wizard/crc64.c

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


Line 
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%                               John Cristy                                   %
16%                               March  2003                                   %
17%                                                                             %
18%                                                                             %
19%  Copyright 1999-2009 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%    http://www.wizards-toolkit.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"
44#include "wizard/exception-private.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*/
56struct _CRC64Info
57{   
58  unsigned int
59    digestsize,
60    blocksize;
61
62  StringInfo
63    *digest;
64
65  WizardSizeType
66    *crc_xor,
67    crc;
68
69  time_t
70    timestamp;
71
72  unsigned long
73    signature;
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*/
94WizardExport CRC64Info *AcquireCRC64Info(void)
95{
96  CRC64Info
97    *crc_info;
98
99  crc_info=(CRC64Info *) AcquireWizardMemory(sizeof(*crc_info));
100  if (crc_info == (CRC64Info *) NULL)
101    ThrowWizardFatalError(HashDomain,MemoryError);
102  (void) ResetWizardMemory(crc_info,0,sizeof(*crc_info));
103  crc_info->digestsize=CRC64Digestsize;
104  crc_info->blocksize=CRC64Blocksize;
105  crc_info->digest=AcquireStringInfo(CRC64Digestsize);
106  crc_info->crc_xor=(WizardSizeType *) AcquireQuantumMemory(256UL,
107    sizeof(*crc_info->crc_xor));
108  if (crc_info->crc_xor == (WizardSizeType *) NULL)
109    ThrowWizardFatalError(HashDomain,MemoryError);
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*/
137WizardExport CRC64Info *DestroyCRC64Info(CRC64Info *crc_info)
138{
139  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
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 *)
146      RelinquishWizardMemory(crc_info->crc_xor);
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%      void 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%
174*/
175WizardExport void FinalizeCRC64(CRC64Info *crc_info)
176{
177  unsigned char
178    *datum;
179
180  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
181  assert(crc_info != (CRC64Info *) NULL);
182  assert(crc_info->signature == WizardSignature);
183  datum=GetStringInfoDatum(crc_info->digest);
184  datum[0]=(unsigned char) (crc_info->crc >> 56);
185  datum[1]=(unsigned char) (crc_info->crc >> 48);
186  datum[2]=(unsigned char) (crc_info->crc >> 40);
187  datum[3]=(unsigned char) (crc_info->crc >> 32);
188  datum[4]=(unsigned char) (crc_info->crc >> 24);
189  datum[5]=(unsigned char) (crc_info->crc >> 16);
190  datum[6]=(unsigned char) (crc_info->crc >> 8);
191  datum[7]=(unsigned char) (crc_info->crc >> 0);
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{
218  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
219  WizardAssert(CipherDomain,crc64_info != (CRC64Info *) NULL);
220  WizardAssert(CipherDomain,crc64_info->signature == WizardSignature);
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*/
247WizardExport WizardSizeType GetCRC64CyclicRedundancyCheck(
248  const CRC64Info *crc64_info)
249{
250  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
251  WizardAssert(CipherDomain,crc64_info != (CRC64Info *) NULL);
252  WizardAssert(CipherDomain,crc64_info->signature == WizardSignature);
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*/
278WizardExport const StringInfo *GetCRC64Digest(const CRC64Info *crc64_info)
279{
280  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
281  WizardAssert(HashDomain,crc64_info != (CRC64Info *) NULL);
282  WizardAssert(HashDomain,crc64_info->signature == WizardSignature);
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{
310  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
311  WizardAssert(CipherDomain,crc64_info != (CRC64Info *) NULL);
312  WizardAssert(CipherDomain,crc64_info->signature == WizardSignature);
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%      void 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%
338*/
339WizardExport void InitializeCRC64(CRC64Info *crc_info)
340{
341  register long
342    i,
343    j;
344
345  WizardSizeType
346    alpha;
347
348  /*
349    Load magic initialization constants.
350  */
351  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
352  assert(crc_info != (CRC64Info *) NULL);
353  assert(crc_info->signature == WizardSignature);
354  crc_info->crc=0;
355  for (i=0; i < 256; i++)
356  {
357    alpha=(WizardSizeType) i;
358    for (j=0; j < 8; j++)
359      if ((alpha & 0x01) != 0)
360        alpha=(WizardSizeType) ((alpha >> 1) ^
361          WizardULLConstant(0xd800000000000000));
362      else
363        alpha>>=1;
364    crc_info->crc_xor[i]=alpha;
365  }
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%      UpdateCRC64(CRC64Info *crc_info,const StringInfo *message)
384%
385%  A description of each parameter follows:
386%
387%    o crc_info: The address of a structure of type CRC64Info.
388%
389*/
390WizardExport void UpdateCRC64(CRC64Info *crc_info,const StringInfo *message)
391{
392  register const unsigned char
393    *p;
394
395  register size_t
396    i;
397
398  /*
399    Update the CRC64 accumulator.
400  */
401  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
402  assert(crc_info != (CRC64Info *) NULL);
403  assert(crc_info->signature == WizardSignature);
404  p=GetStringInfoDatum(message);
405  for (i=0; i < GetStringInfoLength(message); i++)
406  {
407    crc_info->crc=(crc_info->crc >> 8) ^
408      crc_info->crc_xor[(crc_info->crc ^ (WizardSizeType) *p) & 0xff];
409    p++;
410  }
411}
Note: See TracBrowser for help on using the browser.