tests
3 years ago
config.m4
3 years ago
hash_crc32c.c
3 years ago
install_crc32c.sh
3 years ago
php_crc32c.c
3 years ago
php_crc32c.h
3 years ago
php_crc32c.c
175 lines
| 1 | /** |
| 2 | * Copyright 2019 Google Inc. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * crc32c extension for PHP |
| 19 | * |
| 20 | * This file sets up the crc32c module, and provide the 'crc32c' function. |
| 21 | */ |
| 22 | |
| 23 | #include "php_crc32c.h" |
| 24 | |
| 25 | #include "ext/hash/php_hash.h" |
| 26 | #include "ext/standard/info.h" |
| 27 | |
| 28 | #include "crc32c/crc32c.h" |
| 29 | |
| 30 | extern const php_hash_ops crc32_ops; |
| 31 | |
| 32 | static uint32_t byte2int(const uint8_t hash[4]) { |
| 33 | return (hash[0] << 24) | (hash[1] << 16) | (hash[2] << 8) | hash[3]; |
| 34 | } |
| 35 | |
| 36 | /* {{{ int crc32c( string $data [, int $crc ] ) |
| 37 | */ |
| 38 | PHP_FUNCTION(crc32c) |
| 39 | { |
| 40 | char *data_arg = NULL; |
| 41 | size_t data_len = 0; |
| 42 | char *crc_arg = NULL; |
| 43 | size_t crc_len = 0; |
| 44 | |
| 45 | #if PHP_API_VERSION >= 20151012 /* >= PHP 7.0 */ |
| 46 | // fast_zpp is a faster way to parse paramters. |
| 47 | ZEND_PARSE_PARAMETERS_START(1, 2) |
| 48 | Z_PARAM_STRING(data_arg, data_len) |
| 49 | Z_PARAM_OPTIONAL |
| 50 | Z_PARAM_STRING_EX(crc_arg, crc_len, /* check_null */ 1, 0) |
| 51 | ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); |
| 52 | #else |
| 53 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", &data_arg, &data_len, &crc_arg, &crc_len) == FAILURE) { |
| 54 | RETURN_BOOL(false); |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | uint32_t crc = 0; |
| 59 | |
| 60 | if (crc_len == 4) { |
| 61 | crc = byte2int((uint8_t *)crc_arg); |
| 62 | |
| 63 | } else if (crc_arg != NULL) { |
| 64 | zend_error(E_WARNING, "crc32c(): Supplied crc must be a 4 byte string"); |
| 65 | RETURN_BOOL(false); |
| 66 | } |
| 67 | |
| 68 | crc = crc32c_extend(crc, (const uint8_t *)data_arg, data_len); |
| 69 | |
| 70 | uint8_t hash[4]; |
| 71 | int2byte(crc, hash); |
| 72 | |
| 73 | #if PHP_API_VERSION >= 20151012 /* >= PHP 7.0 */ |
| 74 | RETURN_STRINGL((const char *)hash, sizeof(hash)); |
| 75 | #else |
| 76 | RETURN_STRINGL((const char *)hash, sizeof(hash), /* dup */ 1); |
| 77 | #endif |
| 78 | } |
| 79 | /* }}}*/ |
| 80 | |
| 81 | |
| 82 | /* {{{ PHP_RINIT_FUNCTION |
| 83 | */ |
| 84 | PHP_RINIT_FUNCTION(crc32c) |
| 85 | { |
| 86 | #if PHP_VERSION_ID >= 70000 |
| 87 | # if defined(ZTS) && defined(COMPILE_DL_CRC32C) |
| 88 | ZEND_TSRMLS_CACHE_UPDATE(); |
| 89 | # endif |
| 90 | #endif |
| 91 | |
| 92 | return SUCCESS; |
| 93 | } |
| 94 | /* }}} */ |
| 95 | |
| 96 | /* {{{ PHP_MINIT_FUNCTION |
| 97 | */ |
| 98 | PHP_MINIT_FUNCTION(crc32c) |
| 99 | { |
| 100 | php_hash_register_algo("crc32c", &crc32_ops); |
| 101 | return SUCCESS; |
| 102 | } |
| 103 | /* }}} */ |
| 104 | |
| 105 | /* {{{ PHP_MSHUTDOWN_FUNCTION |
| 106 | */ |
| 107 | PHP_MSHUTDOWN_FUNCTION(crc32c) |
| 108 | { |
| 109 | // TODO Unregister php_hash_register_algo |
| 110 | return SUCCESS; |
| 111 | } |
| 112 | /* }}} */ |
| 113 | |
| 114 | /* {{{ PHP_MINFO_FUNCTION |
| 115 | */ |
| 116 | PHP_MINFO_FUNCTION(crc32c) |
| 117 | { |
| 118 | php_info_print_table_start(); |
| 119 | php_info_print_table_header(2, "Google CRC32C support", "enabled"); |
| 120 | php_info_print_table_end(); |
| 121 | } |
| 122 | /* }}} */ |
| 123 | |
| 124 | /* {{{ arginfo |
| 125 | */ |
| 126 | ZEND_BEGIN_ARG_INFO_EX(arginfo_crc32c, 0, 0, 1) |
| 127 | ZEND_ARG_INFO(0, str) |
| 128 | ZEND_ARG_INFO(0, crc) |
| 129 | ZEND_END_ARG_INFO() |
| 130 | /* }}} */ |
| 131 | |
| 132 | /* {{{ crc32c_functions[] |
| 133 | */ |
| 134 | static const zend_function_entry crc32c_functions[] = { |
| 135 | PHP_FE(crc32c, arginfo_crc32c) |
| 136 | PHP_FE_END |
| 137 | }; |
| 138 | /* }}} */ |
| 139 | |
| 140 | /* {{{ crc32c_deps |
| 141 | */ |
| 142 | static const zend_module_dep crc32c_deps[] = { |
| 143 | ZEND_MOD_REQUIRED("hash") |
| 144 | ZEND_MOD_END |
| 145 | }; |
| 146 | /* }}} */ |
| 147 | |
| 148 | /* {{{ crc32c_module_entry |
| 149 | */ |
| 150 | zend_module_entry crc32c_module_entry = { |
| 151 | STANDARD_MODULE_HEADER_EX, NULL, |
| 152 | crc32c_deps, /* Module dependencies */ |
| 153 | "crc32c", /* Extension name */ |
| 154 | crc32c_functions, /* zend_function_entry */ |
| 155 | PHP_MINIT(crc32c), /* PHP_MINIT - Module initialization */ |
| 156 | PHP_MSHUTDOWN(crc32c), /* PHP_MSHUTDOWN - Module shutdown */ |
| 157 | PHP_RINIT(crc32c), /* PHP_RINIT - Request initialization */ |
| 158 | NULL, /* PHP_RSHUTDOWN - Request shutdown */ |
| 159 | PHP_MINFO(crc32c), /* PHP_MINFO - Module info */ |
| 160 | PHP_CRC32C_VERSION, /* Version */ |
| 161 | STANDARD_MODULE_PROPERTIES |
| 162 | }; |
| 163 | /* }}} */ |
| 164 | |
| 165 | #ifdef COMPILE_DL_CRC32C |
| 166 | |
| 167 | # if PHP_VERSION_ID >= 70000 |
| 168 | # ifdef ZTS |
| 169 | ZEND_TSRMLS_CACHE_DEFINE() |
| 170 | # endif |
| 171 | # endif /* PHP_VERSION_ID >= 70000 */ |
| 172 | |
| 173 | ZEND_GET_MODULE(crc32c) |
| 174 | #endif /* COMPILE_DL_CRC32C */ |
| 175 |