forked from awesomized/crc-fast-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibcrc_fast.h
More file actions
273 lines (240 loc) · 7.66 KB
/
libcrc_fast.h
File metadata and controls
273 lines (240 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* crc_fast library C/C++ API - Copyright 2025 Don MacAskill */
/* This header is auto-generated. Do not edit directly. */
#ifndef CRC_FAST_H
#define CRC_FAST_H
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* Error codes for FFI operations
*/
typedef enum CrcFastError {
/**
* Operation completed successfully
*/
Success = 0,
/**
* Lock was poisoned (thread panicked while holding lock)
*/
LockPoisoned = 1,
/**
* Null pointer was passed where non-null required
*/
NullPointer = 2,
/**
* Invalid key count for CRC parameters
*/
InvalidKeyCount = 3,
/**
* Unsupported CRC width (must be 32 or 64)
*/
UnsupportedWidth = 4,
/**
* Invalid UTF-8 string
*/
InvalidUtf8 = 5,
/**
* File I/O error
*/
IoError = 6,
/**
* Internal string conversion error
*/
StringConversionError = 7,
} CrcFastError;
/**
* The supported CRC algorithms
*/
typedef enum CrcFastAlgorithm {
Crc32Aixm,
Crc32Autosar,
Crc32Base91D,
Crc32Bzip2,
Crc32CdRomEdc,
Crc32Cksum,
Crc32Custom,
Crc32Iscsi,
Crc32IsoHdlc,
Crc32Jamcrc,
Crc32Mef,
Crc32Mpeg2,
Crc32Xfer,
Crc64Custom,
Crc64Ecma182,
Crc64GoIso,
Crc64Ms,
Crc64Nvme,
Crc64Redis,
Crc64We,
Crc64Xz,
} CrcFastAlgorithm;
/**
* Represents a CRC Digest, which is used to compute CRC checksums.
*
* The `Digest` struct maintains the state of the CRC computation, including
* the current state, the amount of data processed, the CRC parameters, and
* the calculator function used to perform the CRC calculation.
*/
typedef struct CrcFastDigest CrcFastDigest;
/**
* A handle to the Digest object
*/
typedef struct CrcFastDigestHandle {
struct CrcFastDigest *_0;
} CrcFastDigestHandle;
/**
* Custom CRC parameters
*/
typedef struct CrcFastParams {
enum CrcFastAlgorithm algorithm;
uint8_t width;
uint64_t poly;
uint64_t init;
bool refin;
bool refout;
uint64_t xorout;
uint64_t check;
uint32_t key_count;
const uint64_t *keys;
} CrcFastParams;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/**
* Gets the last error that occurred in the current thread
* Returns CrcFastError::Success if no error has occurred
*/
enum CrcFastError crc_fast_get_last_error(void);
/**
* Clears the last error for the current thread
*/
void crc_fast_clear_error(void);
/**
* Gets a human-readable error message for the given error code
* Returns a pointer to a static string (do not free)
*/
const char *crc_fast_error_message(enum CrcFastError error);
/**
* Creates a new Digest to compute CRC checksums using algorithm
*/
struct CrcFastDigestHandle *crc_fast_digest_new(enum CrcFastAlgorithm algorithm);
/**
* Creates a new Digest with a custom initial state
*/
struct CrcFastDigestHandle *crc_fast_digest_new_with_init_state(enum CrcFastAlgorithm algorithm,
uint64_t init_state);
/**
* Creates a new Digest to compute CRC checksums using custom parameters
* Returns NULL if parameters are invalid (invalid key count or null pointer)
* Call crc_fast_get_last_error() to get the specific error code
*/
struct CrcFastDigestHandle *crc_fast_digest_new_with_params(struct CrcFastParams params);
/**
* Updates the Digest with data
*/
void crc_fast_digest_update(struct CrcFastDigestHandle *handle, const char *data, uintptr_t len);
/**
* Calculates the CRC checksum for data that's been written to the Digest
* Returns 0 on error (e.g. null handle)
*/
uint64_t crc_fast_digest_finalize(struct CrcFastDigestHandle *handle);
/**
* Free the Digest resources without finalizing
*/
void crc_fast_digest_free(struct CrcFastDigestHandle *handle);
/**
* Reset the Digest state
*/
void crc_fast_digest_reset(struct CrcFastDigestHandle *handle);
/**
* Finalize and reset the Digest in one operation
* Returns 0 on error (e.g. null handle)
*/
uint64_t crc_fast_digest_finalize_reset(struct CrcFastDigestHandle *handle);
/**
* Combine two Digest checksums
*/
void crc_fast_digest_combine(struct CrcFastDigestHandle *handle1,
struct CrcFastDigestHandle *handle2);
/**
* Gets the amount of data processed by the Digest so far
* Returns 0 on error (e.g. null handle)
*/
uint64_t crc_fast_digest_get_amount(struct CrcFastDigestHandle *handle);
/**
* Gets the current state of the Digest
* Returns 0 on error (e.g. null handle)
*/
uint64_t crc_fast_digest_get_state(struct CrcFastDigestHandle *handle);
/**
* Helper method to calculate a CRC checksum directly for a string using algorithm
* Returns 0 on error (e.g. null data pointer)
*/
uint64_t crc_fast_checksum(enum CrcFastAlgorithm algorithm, const char *data, uintptr_t len);
/**
* Helper method to calculate a CRC checksum directly for data using custom parameters
* Returns 0 if parameters are invalid or data is null
* Call crc_fast_get_last_error() to get the specific error code
*/
uint64_t crc_fast_checksum_with_params(struct CrcFastParams params,
const char *data,
uintptr_t len);
/**
* Helper method to just calculate a CRC checksum directly for a file using algorithm
* Returns 0 if path is null or file I/O fails
* Call crc_fast_get_last_error() to get the specific error code
*/
uint64_t crc_fast_checksum_file(enum CrcFastAlgorithm algorithm,
const uint8_t *path_ptr,
uintptr_t path_len);
/**
* Helper method to calculate a CRC checksum directly for a file using custom parameters
* Returns 0 if parameters are invalid, path is null, or file I/O fails
* Call crc_fast_get_last_error() to get the specific error code
*/
uint64_t crc_fast_checksum_file_with_params(struct CrcFastParams params,
const uint8_t *path_ptr,
uintptr_t path_len);
/**
* Combine two CRC checksums using algorithm
*/
uint64_t crc_fast_checksum_combine(enum CrcFastAlgorithm algorithm,
uint64_t checksum1,
uint64_t checksum2,
uint64_t checksum2_len);
/**
* Combine two CRC checksums using custom parameters
* Returns 0 if parameters are invalid
* Call crc_fast_get_last_error() to get the specific error code
*/
uint64_t crc_fast_checksum_combine_with_params(struct CrcFastParams params,
uint64_t checksum1,
uint64_t checksum2,
uint64_t checksum2_len);
/**
* Returns the custom CRC parameters for a given set of Rocksoft CRC parameters
* If width is not 32 or 64, sets error to UnsupportedWidth
*/
struct CrcFastParams crc_fast_get_custom_params(const char *name_ptr,
uint8_t width,
uint64_t poly,
uint64_t init,
bool reflected,
uint64_t xorout,
uint64_t check);
/**
* Gets the target build properties (CPU architecture and fine-tuning parameters) for this algorithm
* Returns NULL if string conversion fails
* Call crc_fast_get_last_error() to get the specific error code
*/
const char *crc_fast_get_calculator_target(enum CrcFastAlgorithm algorithm);
/**
* Gets the version of this library
* Returns a pointer to "unknown" if version string is invalid
*/
const char *crc_fast_get_version(void);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif /* CRC_FAST_H */