| 1 |
/* crc_fast library C/C++ API - Copyright 2025 Don MacAskill */ |
| 2 |
/* This header is auto-generated. Do not edit directly. */ |
| 3 |
|
| 4 |
#ifndef CRC_FAST_H |
| 5 |
#define CRC_FAST_H |
| 6 |
|
| 7 |
#include <stdarg.h> |
| 8 |
#include <stdbool.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include <stdlib.h> |
| 11 |
|
| 12 |
/** |
| 13 |
* Error codes for FFI operations |
| 14 |
*/ |
| 15 |
typedef enum CrcFastError { |
| 16 |
/** |
| 17 |
* Operation completed successfully |
| 18 |
*/ |
| 19 |
Success = 0, |
| 20 |
/** |
| 21 |
* Lock was poisoned (thread panicked while holding lock) |
| 22 |
*/ |
| 23 |
LockPoisoned = 1, |
| 24 |
/** |
| 25 |
* Null pointer was passed where non-null required |
| 26 |
*/ |
| 27 |
NullPointer = 2, |
| 28 |
/** |
| 29 |
* Invalid key count for CRC parameters |
| 30 |
*/ |
| 31 |
InvalidKeyCount = 3, |
| 32 |
/** |
| 33 |
* Unsupported CRC width (must be 32 or 64) |
| 34 |
*/ |
| 35 |
UnsupportedWidth = 4, |
| 36 |
/** |
| 37 |
* Invalid UTF-8 string |
| 38 |
*/ |
| 39 |
InvalidUtf8 = 5, |
| 40 |
/** |
| 41 |
* File I/O error |
| 42 |
*/ |
| 43 |
IoError = 6, |
| 44 |
/** |
| 45 |
* Internal string conversion error |
| 46 |
*/ |
| 47 |
StringConversionError = 7, |
| 48 |
} CrcFastError; |
| 49 |
|
| 50 |
/** |
| 51 |
* The supported CRC algorithms |
| 52 |
*/ |
| 53 |
typedef enum CrcFastAlgorithm { |
| 54 |
CrcCustom, |
| 55 |
Crc16Arc, |
| 56 |
Crc16Cdma2000, |
| 57 |
Crc16Cms, |
| 58 |
Crc16Dds110, |
| 59 |
Crc16DectR, |
| 60 |
Crc16DectX, |
| 61 |
Crc16Dnp, |
| 62 |
Crc16En13757, |
| 63 |
Crc16Genibus, |
| 64 |
Crc16Gsm, |
| 65 |
Crc16Ibm3740, |
| 66 |
Crc16IbmSdlc, |
| 67 |
Crc16IsoIec144433A, |
| 68 |
Crc16Kermit, |
| 69 |
Crc16Lj1200, |
| 70 |
Crc16M17, |
| 71 |
Crc16MaximDow, |
| 72 |
Crc16Mcrf4xx, |
| 73 |
Crc16Modbus, |
| 74 |
Crc16Nrsc5, |
| 75 |
Crc16OpensafetyA, |
| 76 |
Crc16OpensafetyB, |
| 77 |
Crc16Profibus, |
| 78 |
Crc16Riello, |
| 79 |
Crc16SpiFujitsu, |
| 80 |
Crc16T10Dif, |
| 81 |
Crc16Teledisk, |
| 82 |
Crc16Tms37157, |
| 83 |
Crc16Umts, |
| 84 |
Crc16Usb, |
| 85 |
Crc16Xmodem, |
| 86 |
Crc32Aixm, |
| 87 |
Crc32Autosar, |
| 88 |
Crc32Base91D, |
| 89 |
Crc32Bzip2, |
| 90 |
Crc32CdRomEdc, |
| 91 |
Crc32Cksum, |
| 92 |
Crc32Custom, |
| 93 |
Crc32Iscsi, |
| 94 |
Crc32IsoHdlc, |
| 95 |
Crc32Jamcrc, |
| 96 |
Crc32Mef, |
| 97 |
Crc32Mpeg2, |
| 98 |
Crc32Xfer, |
| 99 |
Crc64Custom, |
| 100 |
Crc64Ecma182, |
| 101 |
Crc64GoIso, |
| 102 |
Crc64Ms, |
| 103 |
Crc64Nvme, |
| 104 |
Crc64Redis, |
| 105 |
Crc64We, |
| 106 |
Crc64Xz, |
| 107 |
} CrcFastAlgorithm; |
| 108 |
|
| 109 |
/** |
| 110 |
* Represents a CRC Digest, which is used to compute CRC checksums. |
| 111 |
* |
| 112 |
* The `Digest` struct maintains the state of the CRC computation, including |
| 113 |
* the current state, the amount of data processed, the CRC parameters, and |
| 114 |
* the calculator function used to perform the CRC calculation. |
| 115 |
*/ |
| 116 |
typedef struct CrcFastDigest CrcFastDigest; |
| 117 |
|
| 118 |
/** |
| 119 |
* A handle to the Digest object |
| 120 |
*/ |
| 121 |
typedef struct CrcFastDigestHandle { |
| 122 |
struct CrcFastDigest *_0; |
| 123 |
} CrcFastDigestHandle; |
| 124 |
|
| 125 |
/** |
| 126 |
* Custom CRC parameters |
| 127 |
*/ |
| 128 |
typedef struct CrcFastParams { |
| 129 |
enum CrcFastAlgorithm algorithm; |
| 130 |
uint8_t width; |
| 131 |
uint64_t poly; |
| 132 |
uint64_t init; |
| 133 |
bool refin; |
| 134 |
bool refout; |
| 135 |
uint64_t xorout; |
| 136 |
uint64_t check; |
| 137 |
uint32_t key_count; |
| 138 |
const uint64_t *keys; |
| 139 |
} CrcFastParams; |
| 140 |
|
| 141 |
#ifdef __cplusplus |
| 142 |
extern "C" { |
| 143 |
#endif // __cplusplus |
| 144 |
|
| 145 |
/** |
| 146 |
* Gets the last error that occurred in the current thread |
| 147 |
* Returns CrcFastError::Success if no error has occurred |
| 148 |
*/ |
| 149 |
enum CrcFastError crc_fast_get_last_error(void); |
| 150 |
|
| 151 |
/** |
| 152 |
* Clears the last error for the current thread |
| 153 |
*/ |
| 154 |
void crc_fast_clear_error(void); |
| 155 |
|
| 156 |
/** |
| 157 |
* Gets a human-readable error message for the given error code |
| 158 |
* Returns a pointer to a static string (do not free) |
| 159 |
*/ |
| 160 |
const char *crc_fast_error_message(enum CrcFastError error); |
| 161 |
|
| 162 |
/** |
| 163 |
* Creates a new Digest to compute CRC checksums using algorithm |
| 164 |
*/ |
| 165 |
struct CrcFastDigestHandle *crc_fast_digest_new(enum CrcFastAlgorithm algorithm); |
| 166 |
|
| 167 |
/** |
| 168 |
* Creates a new Digest with a custom initial state |
| 169 |
*/ |
| 170 |
struct CrcFastDigestHandle *crc_fast_digest_new_with_init_state(enum CrcFastAlgorithm algorithm, |
| 171 |
uint64_t init_state); |
| 172 |
|
| 173 |
/** |
| 174 |
* Creates a new Digest to compute CRC checksums using custom parameters |
| 175 |
* Returns NULL if parameters are invalid (invalid key count or null pointer) |
| 176 |
* Call crc_fast_get_last_error() to get the specific error code |
| 177 |
*/ |
| 178 |
struct CrcFastDigestHandle *crc_fast_digest_new_with_params(struct CrcFastParams params); |
| 179 |
|
| 180 |
/** |
| 181 |
* Updates the Digest with data |
| 182 |
*/ |
| 183 |
void crc_fast_digest_update(struct CrcFastDigestHandle *handle, const char *data, uintptr_t len); |
| 184 |
|
| 185 |
/** |
| 186 |
* Calculates the CRC checksum for data that's been written to the Digest |
| 187 |
* Returns 0 on error (e.g. null handle) |
| 188 |
*/ |
| 189 |
uint64_t crc_fast_digest_finalize(struct CrcFastDigestHandle *handle); |
| 190 |
|
| 191 |
/** |
| 192 |
* Free the Digest resources without finalizing |
| 193 |
*/ |
| 194 |
void crc_fast_digest_free(struct CrcFastDigestHandle *handle); |
| 195 |
|
| 196 |
/** |
| 197 |
* Reset the Digest state |
| 198 |
*/ |
| 199 |
void crc_fast_digest_reset(struct CrcFastDigestHandle *handle); |
| 200 |
|
| 201 |
/** |
| 202 |
* Finalize and reset the Digest in one operation |
| 203 |
* Returns 0 on error (e.g. null handle) |
| 204 |
*/ |
| 205 |
uint64_t crc_fast_digest_finalize_reset(struct CrcFastDigestHandle *handle); |
| 206 |
|
| 207 |
/** |
| 208 |
* Combine two Digest checksums |
| 209 |
*/ |
| 210 |
void crc_fast_digest_combine(struct CrcFastDigestHandle *handle1, |
| 211 |
struct CrcFastDigestHandle *handle2); |
| 212 |
|
| 213 |
/** |
| 214 |
* Gets the amount of data processed by the Digest so far |
| 215 |
* Returns 0 on error (e.g. null handle) |
| 216 |
*/ |
| 217 |
uint64_t crc_fast_digest_get_amount(struct CrcFastDigestHandle *handle); |
| 218 |
|
| 219 |
/** |
| 220 |
* Gets the current state of the Digest |
| 221 |
* Returns 0 on error (e.g. null handle) |
| 222 |
*/ |
| 223 |
uint64_t crc_fast_digest_get_state(struct CrcFastDigestHandle *handle); |
| 224 |
|
| 225 |
/** |
| 226 |
* Helper method to calculate a CRC checksum directly for a string using algorithm |
| 227 |
* Returns 0 on error (e.g. null data pointer) |
| 228 |
*/ |
| 229 |
uint64_t crc_fast_checksum(enum CrcFastAlgorithm algorithm, const char *data, uintptr_t len); |
| 230 |
|
| 231 |
/** |
| 232 |
* Helper method to calculate a CRC checksum directly for data using custom parameters |
| 233 |
* Returns 0 if parameters are invalid or data is null |
| 234 |
* Call crc_fast_get_last_error() to get the specific error code |
| 235 |
*/ |
| 236 |
uint64_t crc_fast_checksum_with_params(struct CrcFastParams params, |
| 237 |
const char *data, |
| 238 |
uintptr_t len); |
| 239 |
|
| 240 |
/** |
| 241 |
* Helper method to just calculate a CRC checksum directly for a file using algorithm |
| 242 |
* Returns 0 if path is null or file I/O fails |
| 243 |
* Call crc_fast_get_last_error() to get the specific error code |
| 244 |
*/ |
| 245 |
uint64_t crc_fast_checksum_file(enum CrcFastAlgorithm algorithm, |
| 246 |
const uint8_t *path_ptr, |
| 247 |
uintptr_t path_len); |
| 248 |
|
| 249 |
/** |
| 250 |
* Helper method to calculate a CRC checksum directly for a file using custom parameters |
| 251 |
* Returns 0 if parameters are invalid, path is null, or file I/O fails |
| 252 |
* Call crc_fast_get_last_error() to get the specific error code |
| 253 |
*/ |
| 254 |
uint64_t crc_fast_checksum_file_with_params(struct CrcFastParams params, |
| 255 |
const uint8_t *path_ptr, |
| 256 |
uintptr_t path_len); |
| 257 |
|
| 258 |
/** |
| 259 |
* Combine two CRC checksums using algorithm |
| 260 |
*/ |
| 261 |
uint64_t crc_fast_checksum_combine(enum CrcFastAlgorithm algorithm, |
| 262 |
uint64_t checksum1, |
| 263 |
uint64_t checksum2, |
| 264 |
uint64_t checksum2_len); |
| 265 |
|
| 266 |
/** |
| 267 |
* Combine two CRC checksums using custom parameters |
| 268 |
* Returns 0 if parameters are invalid |
| 269 |
* Call crc_fast_get_last_error() to get the specific error code |
| 270 |
*/ |
| 271 |
uint64_t crc_fast_checksum_combine_with_params(struct CrcFastParams params, |
| 272 |
uint64_t checksum1, |
| 273 |
uint64_t checksum2, |
| 274 |
uint64_t checksum2_len); |
| 275 |
|
| 276 |
/** |
| 277 |
* Returns the custom CRC parameters for a given set of Rocksoft CRC parameters |
| 278 |
* If width is not 32 or 64, sets error to UnsupportedWidth |
| 279 |
*/ |
| 280 |
struct CrcFastParams crc_fast_get_custom_params(const char *name_ptr, |
| 281 |
uint8_t width, |
| 282 |
uint64_t poly, |
| 283 |
uint64_t init, |
| 284 |
bool reflected, |
| 285 |
uint64_t xorout, |
| 286 |
uint64_t check); |
| 287 |
|
| 288 |
/** |
| 289 |
* Gets the target build properties (CPU architecture and fine-tuning parameters) for this algorithm |
| 290 |
* Returns NULL if string conversion fails |
| 291 |
* Call crc_fast_get_last_error() to get the specific error code |
| 292 |
*/ |
| 293 |
const char *crc_fast_get_calculator_target(enum CrcFastAlgorithm algorithm); |
| 294 |
|
| 295 |
/** |
| 296 |
* Gets the version of this library |
| 297 |
* Returns a pointer to "unknown" if version string is invalid |
| 298 |
*/ |
| 299 |
const char *crc_fast_get_version(void); |
| 300 |
|
| 301 |
/** |
| 302 |
* Calculates the CRC-32/ISCSI checksum (commonly called "crc32c" in many, but not all, |
| 303 |
* implementations). |
| 304 |
* |
| 305 |
* https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-32-iscsi |
| 306 |
* |
| 307 |
* Returns 0 on error (e.g. null data pointer) |
| 308 |
*/ |
| 309 |
uint32_t crc_fast_crc32_iscsi(const char *data, uintptr_t len); |
| 310 |
|
| 311 |
/** |
| 312 |
* Calculates the CRC-32/ISO-HDLC checksum (commonly called "crc32" in many, but not all, |
| 313 |
* implementations). |
| 314 |
* |
| 315 |
* https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-32-iso-hdlc |
| 316 |
* |
| 317 |
* Returns 0 on error (e.g. null data pointer) |
| 318 |
*/ |
| 319 |
uint32_t crc_fast_crc32_iso_hdlc(const char *data, uintptr_t len); |
| 320 |
|
| 321 |
/** |
| 322 |
* Calculates the CRC-64/NVME checksum. |
| 323 |
* |
| 324 |
* https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-64-nvme |
| 325 |
* |
| 326 |
* Returns 0 on error (e.g. null data pointer) |
| 327 |
*/ |
| 328 |
uint64_t crc_fast_crc64_nvme(const char *data, uintptr_t len); |
| 329 |
|
| 330 |
#ifdef __cplusplus |
| 331 |
} // extern "C" |
| 332 |
#endif // __cplusplus |
| 333 |
|
| 334 |
#endif /* CRC_FAST_H */ |