99#include <stdint.h>
1010#include <stdlib.h>
1111
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+
1250/**
1351 * The supported CRC algorithms
1452 */
@@ -72,6 +110,23 @@ typedef struct CrcFastParams {
72110extern "C" {
73111#endif // __cplusplus
74112
113+ /**
114+ * Gets the last error that occurred in the current thread
115+ * Returns CrcFastError::Success if no error has occurred
116+ */
117+ enum CrcFastError crc_fast_get_last_error (void );
118+
119+ /**
120+ * Clears the last error for the current thread
121+ */
122+ void crc_fast_clear_error (void );
123+
124+ /**
125+ * Gets a human-readable error message for the given error code
126+ * Returns a pointer to a static string (do not free)
127+ */
128+ const char * crc_fast_error_message (enum CrcFastError error );
129+
75130/**
76131 * Creates a new Digest to compute CRC checksums using algorithm
77132 */
@@ -86,6 +141,7 @@ struct CrcFastDigestHandle *crc_fast_digest_new_with_init_state(enum CrcFastAlgo
86141/**
87142 * Creates a new Digest to compute CRC checksums using custom parameters
88143 * Returns NULL if parameters are invalid (invalid key count or null pointer)
144+ * Call crc_fast_get_last_error() to get the specific error code
89145 */
90146struct CrcFastDigestHandle * crc_fast_digest_new_with_params (struct CrcFastParams params );
91147
@@ -96,6 +152,7 @@ void crc_fast_digest_update(struct CrcFastDigestHandle *handle, const char *data
96152
97153/**
98154 * Calculates the CRC checksum for data that's been written to the Digest
155+ * Returns 0 on error (e.g. null handle)
99156 */
100157uint64_t crc_fast_digest_finalize (struct CrcFastDigestHandle * handle );
101158
@@ -111,6 +168,7 @@ void crc_fast_digest_reset(struct CrcFastDigestHandle *handle);
111168
112169/**
113170 * Finalize and reset the Digest in one operation
171+ * Returns 0 on error (e.g. null handle)
114172 */
115173uint64_t crc_fast_digest_finalize_reset (struct CrcFastDigestHandle * handle );
116174
@@ -122,22 +180,26 @@ void crc_fast_digest_combine(struct CrcFastDigestHandle *handle1,
122180
123181/**
124182 * Gets the amount of data processed by the Digest so far
183+ * Returns 0 on error (e.g. null handle)
125184 */
126185uint64_t crc_fast_digest_get_amount (struct CrcFastDigestHandle * handle );
127186
128187/**
129188 * Gets the current state of the Digest
189+ * Returns 0 on error (e.g. null handle)
130190 */
131191uint64_t crc_fast_digest_get_state (struct CrcFastDigestHandle * handle );
132192
133193/**
134194 * Helper method to calculate a CRC checksum directly for a string using algorithm
195+ * Returns 0 on error (e.g. null data pointer)
135196 */
136197uint64_t crc_fast_checksum (enum CrcFastAlgorithm algorithm , const char * data , uintptr_t len );
137198
138199/**
139200 * Helper method to calculate a CRC checksum directly for data using custom parameters
140201 * Returns 0 if parameters are invalid or data is null
202+ * Call crc_fast_get_last_error() to get the specific error code
141203 */
142204uint64_t crc_fast_checksum_with_params (struct CrcFastParams params ,
143205 const char * data ,
@@ -146,6 +208,7 @@ uint64_t crc_fast_checksum_with_params(struct CrcFastParams params,
146208/**
147209 * Helper method to just calculate a CRC checksum directly for a file using algorithm
148210 * Returns 0 if path is null or file I/O fails
211+ * Call crc_fast_get_last_error() to get the specific error code
149212 */
150213uint64_t crc_fast_checksum_file (enum CrcFastAlgorithm algorithm ,
151214 const uint8_t * path_ptr ,
@@ -154,6 +217,7 @@ uint64_t crc_fast_checksum_file(enum CrcFastAlgorithm algorithm,
154217/**
155218 * Helper method to calculate a CRC checksum directly for a file using custom parameters
156219 * Returns 0 if parameters are invalid, path is null, or file I/O fails
220+ * Call crc_fast_get_last_error() to get the specific error code
157221 */
158222uint64_t crc_fast_checksum_file_with_params (struct CrcFastParams params ,
159223 const uint8_t * path_ptr ,
@@ -170,6 +234,7 @@ uint64_t crc_fast_checksum_combine(enum CrcFastAlgorithm algorithm,
170234/**
171235 * Combine two CRC checksums using custom parameters
172236 * Returns 0 if parameters are invalid
237+ * Call crc_fast_get_last_error() to get the specific error code
173238 */
174239uint64_t crc_fast_checksum_combine_with_params (struct CrcFastParams params ,
175240 uint64_t checksum1 ,
@@ -178,6 +243,7 @@ uint64_t crc_fast_checksum_combine_with_params(struct CrcFastParams params,
178243
179244/**
180245 * Returns the custom CRC parameters for a given set of Rocksoft CRC parameters
246+ * If width is not 32 or 64, sets error to UnsupportedWidth
181247 */
182248struct CrcFastParams crc_fast_get_custom_params (const char * name_ptr ,
183249 uint8_t width ,
@@ -189,7 +255,8 @@ struct CrcFastParams crc_fast_get_custom_params(const char *name_ptr,
189255
190256/**
191257 * Gets the target build properties (CPU architecture and fine-tuning parameters) for this algorithm
192- * Returns NULL if string conversion fails (should never happen)
258+ * Returns NULL if string conversion fails
259+ * Call crc_fast_get_last_error() to get the specific error code
193260 */
194261const char * crc_fast_get_calculator_target (enum CrcFastAlgorithm algorithm );
195262
0 commit comments