| 1 | /* |
| 2 | This file is part of Mtproto-proxy Library. |
| 3 | |
| 4 | Mtproto-proxy Library is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published by |
| 6 | the Free Software Foundation, either version 2 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | Mtproto-proxy Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with Mtproto-proxy Library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | Copyright 2009-2012 Vkontakte Ltd |
| 18 | 2009-2012 Nikolai Durov |
| 19 | 2009-2012 Andrey Lopatin |
| 20 | 2012 Anton Maydell |
| 21 | |
| 22 | Copyright 2014 Telegram Messenger Inc |
| 23 | 2014 Anton Maydell |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include <stdint.h> |
| 29 | |
| 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
| 34 | typedef unsigned (*crc32_partial_func_t) (const void *data, long len, unsigned crc); |
| 35 | typedef unsigned (*crc32_combine_func_t) (unsigned crc1, unsigned crc2, int64_t len2); |
| 36 | typedef uint64_t (*crc64_partial_func_t) (const void *data, long len, uint64_t crc); |
| 37 | typedef uint64_t (*crc64_combine_func_t) (uint64_t crc1, uint64_t crc2, int64_t len2); |
| 38 | |
| 39 | extern crc32_partial_func_t crc32_partial; |
| 40 | extern crc64_partial_func_t crc64_partial; |
| 41 | extern crc32_combine_func_t compute_crc32_combine; |
| 42 | extern crc64_combine_func_t compute_crc64_combine; |
| 43 | |
| 44 | static inline unsigned compute_crc32 (const void *data, long len) { |
| 45 | return crc32_partial (data, len, -1) ^ -1; |
| 46 | } |
| 47 | |
| 48 | static inline uint64_t compute_crc64 (const void *data, long len) { |
| 49 | return crc64_partial (data, len, -1LL) ^ -1LL; |
| 50 | } |
| 51 | |
| 52 | /* crc32_check_and_repair returns |
| 53 | 0 : Cyclic redundancy check is ok |
| 54 | 1 : Cyclic redundancy check fails, but we fix one bit in input |
| 55 | 2 : Cyclic redundancy check fails, but we fix one bit in input_crc32 |
| 56 | -1 : Cyclic redundancy check fails, no repair possible. |
| 57 | In this case *input_crc32 will be equal crc32 (input, l) |
| 58 | |
| 59 | Case force_exit == 1 (case 1, 2: kprintf call, case -1: assert fail). |
| 60 | */ |
| 61 | int crc32_check_and_repair (void *input, int l, unsigned *input_crc32, int force_exit); |
| 62 | int crc32_find_corrupted_bit (int size, unsigned d); |
| 63 | int crc32_repair_bit (unsigned char *input, int l, int k); |
| 64 | |
| 65 | /* these functions are exported only for testing purpose */ |
| 66 | unsigned crc32_partial_generic (const void *data, long len, unsigned crc); |
| 67 | unsigned crc32_partial_clmul (const void *data, long len, unsigned crc); |
| 68 | uint64_t crc64_partial_one_table (const void *data, long len, uint64_t crc); |
| 69 | uint64_t crc64_partial_clmul (const void *data, long len, uint64_t crc); |
| 70 | |
| 71 | uint64_t crc64_feed_byte (uint64_t crc, unsigned char b); |
| 72 | |
| 73 | |
| 74 | void gf32_compute_powers_generic (unsigned *P, int size, unsigned poly); |
| 75 | void gf32_compute_powers_clmul (unsigned *P, unsigned poly); |
| 76 | unsigned gf32_combine_generic (unsigned *powers, unsigned crc1, int64_t len2); |
| 77 | uint64_t gf32_combine_clmul (unsigned *powers, unsigned crc1, int64_t len2); |
| 78 | |
| 79 | |
| 80 | #ifdef __cplusplus |
| 81 | } |
| 82 | #endif |
| 83 | |