crc64.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                            CCCC  RRRR    CCCC                               %
00006 %                           C      R   R  C                                   %
00007 %                           C      RRRR   C                                   %
00008 %                           C      R R    C                                   %
00009 %                            CCCC  R  R    CCCC                               %
00010 %                                                                             %
00011 %                                                                             %
00012 %               Wizard's Toolkit Cyclic Redunancy Checksum Methods            %
00013 %                                                                             %
00014 %                             Software Design                                 %
00015 %                               John Cristy                                   %
00016 %                               March  2003                                   %
00017 %                                                                             %
00018 %                                                                             %
00019 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
00020 %  dedicated to making software imaging solutions freely available.           %
00021 %                                                                             %
00022 %  You may not use this file except in compliance with the License.  You may  %
00023 %  obtain a copy of the License at                                            %
00024 %                                                                             %
00025 %    http://www.wizards-toolkit.org/script/license.php                        %
00026 %                                                                             %
00027 %  Unless required by applicable law or agreed to in writing, software        %
00028 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00029 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00030 %  See the License for the specific language governing permissions and        %
00031 %  limitations under the License.                                             %
00032 %                                                                             %
00033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00034 %
00035 %
00036 */
00037 
00038 /*
00039   Include declarations.
00040 */
00041 #include "wizard/studio.h"
00042 #include "wizard/crc64.h"
00043 #include "wizard/exception.h"
00044 #include "wizard/exception-private.h"
00045 #include "wizard/memory_.h"
00046 
00047 /*
00048   Define declarations.
00049 */
00050 #define CRC64Blocksize  32
00051 #define CRC64Digestsize  8
00052 
00053 /*
00054   Typedef declarations.
00055 */
00056 struct _CRC64Info
00057 {   
00058   unsigned int
00059     digestsize,
00060     blocksize;
00061 
00062   StringInfo
00063     *digest;
00064 
00065   WizardSizeType
00066     *crc_xor,
00067     crc;
00068 
00069   time_t
00070     timestamp;
00071 
00072   size_t
00073     signature;
00074 };
00075 
00076 /*
00077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00078 %                                                                             %
00079 %                                                                             %
00080 %                                                                             %
00081 %   A c q u i r e C R C 6 4 I n f o                                           %
00082 %                                                                             %
00083 %                                                                             %
00084 %                                                                             %
00085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00086 %
00087 %  AcquireCRC64Info() allocate the CRC64Info structure.
00088 %
00089 %  The format of the AcquireCRC64Info method is:
00090 %
00091 %      CRC64Info *AcquireCRC64Info(void)
00092 %
00093 */
00094 WizardExport CRC64Info *AcquireCRC64Info(void)
00095 {
00096   CRC64Info
00097     *crc_info;
00098 
00099   crc_info=(CRC64Info *) AcquireAlignedMemory(1,sizeof(*crc_info));
00100   if (crc_info == (CRC64Info *) NULL)
00101     ThrowWizardFatalError(HashDomain,MemoryError);
00102   (void) ResetWizardMemory(crc_info,0,sizeof(*crc_info));
00103   crc_info->digestsize=CRC64Digestsize;
00104   crc_info->blocksize=CRC64Blocksize;
00105   crc_info->digest=AcquireStringInfo(CRC64Digestsize);
00106   crc_info->crc_xor=(WizardSizeType *) AcquireQuantumMemory(256UL,
00107     sizeof(*crc_info->crc_xor));
00108   if (crc_info->crc_xor == (WizardSizeType *) NULL)
00109     ThrowWizardFatalError(HashDomain,MemoryError);
00110   crc_info->timestamp=time((time_t *) NULL);
00111   crc_info->signature=WizardSignature;
00112   return(crc_info);
00113 }
00114 
00115 /*
00116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00117 %                                                                             %
00118 %                                                                             %
00119 %                                                                             %
00120 %   D e s t r o y C R C 6 4 I n f o                                           %
00121 %                                                                             %
00122 %                                                                             %
00123 %                                                                             %
00124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00125 %
00126 %  DestroyCRC64Info() zeros memory associated with the CRC64Info structure.
00127 %
00128 %  The format of the DestroyCRC64Info method is:
00129 %
00130 %      CRC64Info *DestroyCRC64Info(CRC64Info *crc_info)
00131 %
00132 %  A description of each parameter follows:
00133 %
00134 %    o crc_info: The cipher crc_info.
00135 %
00136 */
00137 WizardExport CRC64Info *DestroyCRC64Info(CRC64Info *crc_info)
00138 {
00139   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00140   assert(crc_info != (CRC64Info *) NULL);
00141   assert(crc_info->signature == WizardSignature);
00142   if (crc_info->digest != (StringInfo *) NULL)
00143     crc_info->digest=DestroyStringInfo(crc_info->digest);
00144   if (crc_info->crc_xor != (WizardSizeType *) NULL)
00145     crc_info->crc_xor=(WizardSizeType *)
00146       RelinquishWizardMemory(crc_info->crc_xor);
00147   crc_info->signature=(~WizardSignature);
00148   crc_info=(CRC64Info *) RelinquishWizardMemory(crc_info);
00149   return(crc_info);
00150 }
00151 
00152 /*
00153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00154 %                                                                             %
00155 %                                                                             %
00156 %                                                                             %
00157 %   F i n a l i z e C R C 6 4                                                 %
00158 %                                                                             %
00159 %                                                                             %
00160 %                                                                             %
00161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00162 %
00163 %  FinalizeCRC64() finalizes the CRC64 message digest computation.
00164 %
00165 %  The format of the FinalizeCRC64 method is:
00166 %
00167 %      void FinalizeCRC64(CRC64Info *crc_info)
00168 %
00169 %  A description of each parameter follows:
00170 %
00171 %    o crc_info: The address of a structure of type CRC64Info.
00172 %
00173 %
00174 */
00175 WizardExport void FinalizeCRC64(CRC64Info *crc_info)
00176 {
00177   unsigned char
00178     *datum;
00179 
00180   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00181   assert(crc_info != (CRC64Info *) NULL);
00182   assert(crc_info->signature == WizardSignature);
00183   datum=GetStringInfoDatum(crc_info->digest);
00184   datum[0]=(unsigned char) (crc_info->crc >> 56);
00185   datum[1]=(unsigned char) (crc_info->crc >> 48);
00186   datum[2]=(unsigned char) (crc_info->crc >> 40);
00187   datum[3]=(unsigned char) (crc_info->crc >> 32);
00188   datum[4]=(unsigned char) (crc_info->crc >> 24);
00189   datum[5]=(unsigned char) (crc_info->crc >> 16);
00190   datum[6]=(unsigned char) (crc_info->crc >> 8);
00191   datum[7]=(unsigned char) (crc_info->crc >> 0);
00192 }
00193 
00194 /*
00195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00196 %                                                                             %
00197 %                                                                             %
00198 %                                                                             %
00199 %   G e t C R C 6 4 B l o c k s i z e                                         %
00200 %                                                                             %
00201 %                                                                             %
00202 %                                                                             %
00203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00204 %
00205 %  GetCRC64Blocksize() returns the CRC64 blocksize.
00206 %
00207 %  The format of the GetCRC64Blocksize method is:
00208 %
00209 %      unsigned int *GetCRC64Blocksize(const CRC64Info *crc64_info)
00210 %
00211 %  A description of each parameter follows:
00212 %
00213 %    o crc64_info: The crc64 info.
00214 %
00215 */
00216 WizardExport unsigned int GetCRC64Blocksize(const CRC64Info *crc64_info)
00217 {
00218   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00219   WizardAssert(CipherDomain,crc64_info != (CRC64Info *) NULL);
00220   WizardAssert(CipherDomain,crc64_info->signature == WizardSignature);
00221   return(crc64_info->blocksize);
00222 }
00223 
00224 /*
00225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00226 %                                                                             %
00227 %                                                                             %
00228 %                                                                             %
00229 %   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                 %
00230 %                                                                             %
00231 %                                                                             %
00232 %                                                                             %
00233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00234 %
00235 %  GetCRC64CyclicRedundancyCheck() returns the CRC64 cyclic redunancy check.
00236 %
00237 %  The format of the GetCRC64CyclicRedundancyCheck method is:
00238 %
00239 %      WizardSizeType *GetCRC64CyclicRedundancyCheck(
00240 %        const CRC64Info *crc64_info)
00241 %
00242 %  A description of each parameter follows:
00243 %
00244 %    o crc64_info: The crc64 info.
00245 %
00246 */
00247 WizardExport WizardSizeType GetCRC64CyclicRedundancyCheck(
00248   const CRC64Info *crc64_info)
00249 {
00250   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00251   WizardAssert(CipherDomain,crc64_info != (CRC64Info *) NULL);
00252   WizardAssert(CipherDomain,crc64_info->signature == WizardSignature);
00253   return(crc64_info->crc);
00254 }
00255 
00256 /*
00257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00258 %                                                                             %
00259 %                                                                             %
00260 %                                                                             %
00261 %   G e t C R C 6 4 D i g e s t                                               %
00262 %                                                                             %
00263 %                                                                             %
00264 %                                                                             %
00265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00266 %
00267 %  GetCRC64Digest() returns the CRC64 digest.
00268 %
00269 %  The format of the GetCRC64Digest method is:
00270 %
00271 %      const StringInfo *GetCRC64Digest(const CRC64Info *crc64_info)
00272 %
00273 %  A description of each parameter follows:
00274 %
00275 %    o crc64_info: The crc64 info.
00276 %
00277 */
00278 WizardExport const StringInfo *GetCRC64Digest(const CRC64Info *crc64_info)
00279 {
00280   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00281   WizardAssert(HashDomain,crc64_info != (CRC64Info *) NULL);
00282   WizardAssert(HashDomain,crc64_info->signature == WizardSignature);
00283   return(crc64_info->digest);
00284 }
00285 
00286 /*
00287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00288 %                                                                             %
00289 %                                                                             %
00290 %                                                                             %
00291 %   G e t C R C 6 4 D i g e s t s i z e                                       %
00292 %                                                                             %
00293 %                                                                             %
00294 %                                                                             %
00295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00296 %
00297 %  GetCRC64Digestsize() returns the CRC64 digest size.
00298 %
00299 %  The format of the GetCRC64Digestsize method is:
00300 %
00301 %      unsigned int *GetCRC64Digestsize(const CRC64Info *crc64_info)
00302 %
00303 %  A description of each parameter follows:
00304 %
00305 %    o crc64_info: The crc64 info.
00306 %
00307 */
00308 WizardExport unsigned int GetCRC64Digestsize(const CRC64Info *crc64_info)
00309 {
00310   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00311   WizardAssert(CipherDomain,crc64_info != (CRC64Info *) NULL);
00312   WizardAssert(CipherDomain,crc64_info->signature == WizardSignature);
00313   return(crc64_info->digestsize);
00314 }
00315 
00316 /*
00317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00318 %                                                                             %
00319 %                                                                             %
00320 %                                                                             %
00321 %   I n i t i a l i z e C R C 6 4                                             %
00322 %                                                                             %
00323 %                                                                             %
00324 %                                                                             %
00325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00326 %
00327 %  IntializeCRC64() intializes the CRC64 digest.
00328 %
00329 %  The format of the InitializeCRC64 method is:
00330 %
00331 %      void InitializeCRC64(crc_info)
00332 %
00333 %  A description of each parameter follows:
00334 %
00335 %    o crc_info: The address of a structure of type CRC64Info.
00336 %
00337 %
00338 */
00339 WizardExport void InitializeCRC64(CRC64Info *crc_info)
00340 {
00341   register ssize_t
00342     i,
00343     j;
00344 
00345   WizardSizeType
00346     alpha;
00347 
00348   /*
00349     Load magic initialization constants.
00350   */
00351   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00352   assert(crc_info != (CRC64Info *) NULL);
00353   assert(crc_info->signature == WizardSignature);
00354   crc_info->crc=0;
00355   for (i=0; i < 256; i++)
00356   {
00357     alpha=(WizardSizeType) i;
00358     for (j=0; j < 8; j++)
00359       if ((alpha & 0x01) != 0)
00360         alpha=(WizardSizeType) ((alpha >> 1) ^
00361           WizardULLConstant(0xd800000000000000));
00362       else
00363         alpha>>=1;
00364     crc_info->crc_xor[i]=alpha;
00365   }
00366 }
00367 
00368 /*
00369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00370 %                                                                             %
00371 %                                                                             %
00372 %                                                                             %
00373 %   U p d a t e C R C 6 4                                                     %
00374 %                                                                             %
00375 %                                                                             %
00376 %                                                                             %
00377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00378 %
00379 %  UpdateCRC64() updates the CRC64 message digest
00380 %
00381 %  The format of the UpdateCRC64 method is:
00382 %
00383 %      UpdateCRC64(CRC64Info *crc_info,const StringInfo *message)
00384 %
00385 %  A description of each parameter follows:
00386 %
00387 %    o crc_info: The address of a structure of type CRC64Info.
00388 %
00389 */
00390 WizardExport void UpdateCRC64(CRC64Info *crc_info,const StringInfo *message)
00391 {
00392   register const unsigned char
00393     *p;
00394 
00395   register size_t
00396     i;
00397 
00398   /*
00399     Update the CRC64 accumulator.
00400   */
00401   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00402   assert(crc_info != (CRC64Info *) NULL);
00403   assert(crc_info->signature == WizardSignature);
00404   p=GetStringInfoDatum(message);
00405   for (i=0; i < GetStringInfoLength(message); i++)
00406   {
00407     crc_info->crc=(crc_info->crc >> 8) ^
00408       crc_info->crc_xor[(crc_info->crc ^ (WizardSizeType) *p) & 0xff];
00409     p++;
00410   }
00411 }
Generated by  doxygen 1.6.2-20100208