Back to index

php5  5.3.10
hash_crc32.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 1997-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Authors: Michael Wallner <mike@php.net>                              |
00016   |          Sara Golemon <pollita@php.net>                              |
00017   +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: hash_crc32.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "php_hash.h"
00023 #include "php_hash_crc32.h"
00024 #include "php_hash_crc32_tables.h"
00025 
00026 PHP_HASH_API void PHP_CRC32Init(PHP_CRC32_CTX *context)
00027 {
00028        context->state = ~0;
00029 }
00030 
00031 PHP_HASH_API void PHP_CRC32Update(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
00032 {
00033        size_t i;
00034        
00035        for (i = 0; i < len; ++i) {
00036               context->state = (context->state << 8) ^ crc32_table[(context->state >> 24) ^ (input[i] & 0xff)];
00037        }
00038 }
00039 
00040 PHP_HASH_API void PHP_CRC32BUpdate(PHP_CRC32_CTX *context, const unsigned char *input, size_t len)
00041 {
00042        size_t i;
00043        
00044        for (i = 0; i < len; ++i) {
00045               context->state = (context->state >> 8) ^ crc32b_table[(context->state ^ input[i]) & 0xff];
00046        }
00047 }
00048 
00049 PHP_HASH_API void PHP_CRC32Final(unsigned char digest[4], PHP_CRC32_CTX *context)
00050 {
00051        context->state=~context->state;
00052        digest[3] = (unsigned char) ((context->state >> 24) & 0xff);
00053        digest[2] = (unsigned char) ((context->state >> 16) & 0xff);
00054        digest[1] = (unsigned char) ((context->state >> 8) & 0xff);
00055        digest[0] = (unsigned char) (context->state & 0xff);
00056        context->state = 0;
00057 }
00058 
00059 PHP_HASH_API void PHP_CRC32BFinal(unsigned char digest[4], PHP_CRC32_CTX *context)
00060 {
00061        context->state=~context->state;
00062        digest[0] = (unsigned char) ((context->state >> 24) & 0xff);
00063        digest[1] = (unsigned char) ((context->state >> 16) & 0xff);
00064        digest[2] = (unsigned char) ((context->state >> 8) & 0xff);
00065        digest[3] = (unsigned char) (context->state & 0xff);
00066        context->state = 0;
00067 }
00068 
00069 PHP_HASH_API int PHP_CRC32Copy(const php_hash_ops *ops, PHP_CRC32_CTX *orig_context, PHP_CRC32_CTX *copy_context)
00070 {
00071        copy_context->state = orig_context->state;
00072        return SUCCESS;
00073 }
00074 
00075 const php_hash_ops php_hash_crc32_ops = {
00076        (php_hash_init_func_t) PHP_CRC32Init,
00077        (php_hash_update_func_t) PHP_CRC32Update,
00078        (php_hash_final_func_t) PHP_CRC32Final,
00079        (php_hash_copy_func_t) PHP_CRC32Copy,
00080        4, /* what to say here? */
00081        4,
00082        sizeof(PHP_CRC32_CTX)
00083 };
00084 
00085 const php_hash_ops php_hash_crc32b_ops = {
00086        (php_hash_init_func_t) PHP_CRC32Init,
00087        (php_hash_update_func_t) PHP_CRC32BUpdate,
00088        (php_hash_final_func_t) PHP_CRC32BFinal,
00089        (php_hash_copy_func_t) PHP_CRC32Copy,
00090        4, /* what to say here? */
00091        4,
00092        sizeof(PHP_CRC32_CTX)
00093 };
00094 
00095 /*
00096  * Local variables:
00097  * tab-width: 4
00098  * c-basic-offset: 4
00099  * End:
00100  * vim600: sw=4 ts=4 fdm=marker
00101  * vim<600: sw=4 ts=4
00102  */