"Fossies" - the Fresh Open Source Software Archive

Member "john-1.9.0/src/crc32.c" (29 May 2013, 1774 Bytes) of package /linux/privat/john-1.9.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "crc32.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.8.0_vs_1.9.0.

    1 /*
    2  * This is a tiny implementation of CRC-32.
    3  *
    4  * This software was written by Solar Designer in 1998 and revised in 2005.
    5  * No copyright is claimed, and the software is hereby placed in the public
    6  * domain.
    7  * In case this attempt to disclaim copyright and place the software in the
    8  * public domain is deemed null and void, then the software is
    9  * Copyright (c) 1998,2005 by Solar Designer and it is hereby released to the
   10  * general public under the following terms:
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted.
   14  *
   15  * There's ABSOLUTELY NO WARRANTY, express or implied.
   16  *
   17  * (This is a heavily cut-down "BSD license".)
   18  */
   19 
   20 #include <stdio.h>
   21 
   22 #include "memory.h"
   23 #include "crc32.h"
   24 
   25 #define POLY 0xEDB88320
   26 #define ALL1 0xFFFFFFFF
   27 
   28 static CRC32_t *table = NULL;
   29 
   30 void CRC32_Init(CRC32_t *value)
   31 {
   32     unsigned int index, bit;
   33     CRC32_t entry;
   34 
   35     *value = ALL1;
   36 
   37     if (table) return;
   38 /* mem_alloc() doesn't return on failure.  If replacing this with plain
   39  * malloc(3), error checking would need to be added. */
   40     table = mem_alloc(sizeof(*table) * 0x100);
   41 
   42     for (index = 0; index < 0x100; index++) {
   43         entry = index;
   44 
   45         for (bit = 0; bit < 8; bit++)
   46         if (entry & 1) {
   47             entry >>= 1;
   48             entry ^= POLY;
   49         } else
   50             entry >>= 1;
   51 
   52         table[index] = entry;
   53     }
   54 }
   55 
   56 void CRC32_Update(CRC32_t *value, void *data, unsigned int size)
   57 {
   58     unsigned char *ptr;
   59     unsigned int count;
   60     CRC32_t result;
   61 
   62     result = *value;
   63     ptr = data;
   64     count = size;
   65 
   66     if (count)
   67     do {
   68         result = (result >> 8) ^ table[(result ^ *ptr++) & 0xFF];
   69     } while (--count);
   70 
   71     *value = result;
   72 }
   73 
   74 void CRC32_Final(unsigned char *out, CRC32_t value)
   75 {
   76     value = ~value;
   77     out[0] = value;
   78     out[1] = value >> 8;
   79     out[2] = value >> 16;
   80     out[3] = value >> 24;
   81 }