/src/php-src/ext/hash/hash_adler32.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Michael Wallner <mike@php.net> | |
12 | | | Sara Golemon <pollita@php.net> | |
13 | | +----------------------------------------------------------------------+ |
14 | | */ |
15 | | |
16 | | #include "php_hash.h" |
17 | | #include "php_hash_adler32.h" |
18 | | |
19 | | PHP_HASH_API void PHP_ADLER32Init(PHP_ADLER32_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args) |
20 | 0 | { |
21 | 0 | context->state = 1; |
22 | 0 | } |
23 | | |
24 | | PHP_HASH_API void PHP_ADLER32Update(PHP_ADLER32_CTX *context, const unsigned char *input, size_t len) |
25 | 0 | { |
26 | 0 | uint32_t s[2]; |
27 | |
|
28 | 0 | s[0] = context->state & 0xffff; |
29 | 0 | s[1] = (context->state >> 16) & 0xffff; |
30 | 0 | for (size_t i = 0; i < len; ++i) { |
31 | 0 | s[0] += input[i]; |
32 | 0 | s[1] += s[0]; |
33 | 0 | if (s[1]>=0x7fffffff) |
34 | 0 | { |
35 | 0 | s[0] = s[0] % 65521; |
36 | 0 | s[1] = s[1] % 65521; |
37 | 0 | } |
38 | 0 | } |
39 | 0 | s[0] = s[0] % 65521; |
40 | 0 | s[1] = s[1] % 65521; |
41 | 0 | context->state = s[0] + (s[1] << 16); |
42 | 0 | } |
43 | | |
44 | | PHP_HASH_API void PHP_ADLER32Final(unsigned char digest[4], PHP_ADLER32_CTX *context) |
45 | 0 | { |
46 | 0 | digest[0] = (unsigned char) ((context->state >> 24) & 0xff); |
47 | 0 | digest[1] = (unsigned char) ((context->state >> 16) & 0xff); |
48 | 0 | digest[2] = (unsigned char) ((context->state >> 8) & 0xff); |
49 | 0 | digest[3] = (unsigned char) (context->state & 0xff); |
50 | 0 | context->state = 0; |
51 | 0 | } |
52 | | |
53 | | PHP_HASH_API zend_result PHP_ADLER32Copy(const php_hash_ops *ops, const PHP_ADLER32_CTX *orig_context, PHP_ADLER32_CTX *copy_context) |
54 | 0 | { |
55 | 0 | copy_context->state = orig_context->state; |
56 | 0 | return SUCCESS; |
57 | 0 | } |
58 | | |
59 | | const php_hash_ops php_hash_adler32_ops = { |
60 | | "adler32", |
61 | | (php_hash_init_func_t) PHP_ADLER32Init, |
62 | | (php_hash_update_func_t) PHP_ADLER32Update, |
63 | | (php_hash_final_func_t) PHP_ADLER32Final, |
64 | | (php_hash_copy_func_t) PHP_ADLER32Copy, |
65 | | php_hash_serialize, |
66 | | php_hash_unserialize, |
67 | | PHP_ADLER32_SPEC, |
68 | | 4, /* what to say here? */ |
69 | | 4, |
70 | | sizeof(PHP_ADLER32_CTX), |
71 | | 0 |
72 | | }; |