/src/php-src/ext/hash/hash_adler32.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright (c) The PHP Group | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to version 3.01 of the PHP license, | |
6 | | | that is bundled with this package in the file LICENSE, and is | |
7 | | | available through the world-wide-web at the following url: | |
8 | | | https://www.php.net/license/3_01.txt | |
9 | | | If you did not receive a copy of the PHP license and are unable to | |
10 | | | obtain it through the world-wide-web, please send a note to | |
11 | | | license@php.net so we can mail you a copy immediately. | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Michael Wallner <mike@php.net> | |
14 | | | Sara Golemon <pollita@php.net> | |
15 | | +----------------------------------------------------------------------+ |
16 | | */ |
17 | | |
18 | | #include "php_hash.h" |
19 | | #include "php_hash_adler32.h" |
20 | | |
21 | | PHP_HASH_API void PHP_ADLER32Init(PHP_ADLER32_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) |
22 | 32 | { |
23 | 32 | context->state = 1; |
24 | 32 | } |
25 | | |
26 | | PHP_HASH_API void PHP_ADLER32Update(PHP_ADLER32_CTX *context, const unsigned char *input, size_t len) |
27 | 32 | { |
28 | 32 | uint32_t s[2]; |
29 | | |
30 | 32 | s[0] = context->state & 0xffff; |
31 | 32 | s[1] = (context->state >> 16) & 0xffff; |
32 | 1.30M | for (size_t i = 0; i < len; ++i) { |
33 | 1.30M | s[0] += input[i]; |
34 | 1.30M | s[1] += s[0]; |
35 | 1.30M | if (s[1]>=0x7fffffff) |
36 | 245 | { |
37 | 245 | s[0] = s[0] % 65521; |
38 | 245 | s[1] = s[1] % 65521; |
39 | 245 | } |
40 | 1.30M | } |
41 | 32 | s[0] = s[0] % 65521; |
42 | 32 | s[1] = s[1] % 65521; |
43 | 32 | context->state = s[0] + (s[1] << 16); |
44 | 32 | } |
45 | | |
46 | | PHP_HASH_API void PHP_ADLER32Final(unsigned char digest[4], PHP_ADLER32_CTX *context) |
47 | 32 | { |
48 | 32 | digest[0] = (unsigned char) ((context->state >> 24) & 0xff); |
49 | 32 | digest[1] = (unsigned char) ((context->state >> 16) & 0xff); |
50 | 32 | digest[2] = (unsigned char) ((context->state >> 8) & 0xff); |
51 | 32 | digest[3] = (unsigned char) (context->state & 0xff); |
52 | 32 | context->state = 0; |
53 | 32 | } |
54 | | |
55 | | PHP_HASH_API zend_result PHP_ADLER32Copy(const php_hash_ops *ops, const PHP_ADLER32_CTX *orig_context, PHP_ADLER32_CTX *copy_context) |
56 | 0 | { |
57 | 0 | copy_context->state = orig_context->state; |
58 | 0 | return SUCCESS; |
59 | 0 | } |
60 | | |
61 | | const php_hash_ops php_hash_adler32_ops = { |
62 | | "adler32", |
63 | | (php_hash_init_func_t) PHP_ADLER32Init, |
64 | | (php_hash_update_func_t) PHP_ADLER32Update, |
65 | | (php_hash_final_func_t) PHP_ADLER32Final, |
66 | | (php_hash_copy_func_t) PHP_ADLER32Copy, |
67 | | php_hash_serialize, |
68 | | php_hash_unserialize, |
69 | | PHP_ADLER32_SPEC, |
70 | | 4, /* what to say here? */ |
71 | | 4, |
72 | | sizeof(PHP_ADLER32_CTX), |
73 | | 0 |
74 | | }; |