Skip to content

Commit eb3a541

Browse files
committed
[*.h,*.c,CMakeLists.txt] Improve checksum handling through dedicated dispatch
1 parent 3c70b44 commit eb3a541

20 files changed

+782
-386
lines changed

USAGE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ This example shows a non-blocking download with a progress indicator, perfect fo
8080
8181
```c
8282
#include <stdio.h>
83+
8384
#include <acquire_handle.h>
8485
#include <acquire_download.h>
8586

acquire/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,23 @@ else ()
171171
endif ()
172172
list(APPEND header_impls "${NETWORK_LIB}")
173173

174+
#############
175+
# Checksums #
176+
#############
177+
if (LIBACQUIRE_USE_LIBRHASH)
178+
set(CHECKSUM_LIB "acquire_librhash.h")
179+
elseif (LIBACQUIRE_USE_CRC32C)
180+
set(CHECKSUM_LIB "acquire_crc32c.h")
181+
endif ()
182+
list(APPEND header_impls "${CHECKSUM_LIB}")
183+
174184
message(STATUS "header_impls = ${header_impls}")
175185

176186
foreach (header_file IN LISTS header_impls)
177187
if (header_file STREQUAL "")
178188
continue()
179189
endif (header_file STREQUAL "")
190+
message(STATUS "Header: ${header_file}")
180191
get_filename_component(name_no_ext "${header_file}" NAME_WE)
181192
set(gen_header_file "${CMAKE_BINARY_DIR}/gen/gen_${name_no_ext}.h")
182193
set(gen_source_file "${CMAKE_BINARY_DIR}/gen/gen_${name_no_ext}.c")
@@ -188,6 +199,8 @@ else ()
188199
# set(associated_header "gen_acquire_checksums.h")
189200
elseif (header_file STREQUAL CHECKSUM_SRC_CRC32c)
190201
#
202+
elseif (header_file STREQUAL CHECKSUM_LIB)
203+
set(associated_header "gen_acquire_checksums.h")
191204
elseif (header_file STREQUAL ARCHIVE_LIB)
192205
set(associated_header "gen_acquire_extract.h")
193206
endif ()

acquire/acquire_checksums.h

Lines changed: 175 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33

44
#ifdef __cplusplus
55
extern "C" {
6-
#endif /* __cplusplus */
6+
#endif
77

88
#include "acquire_handle.h"
99
#include "libacquire_export.h"
10+
#include <string.h> /* strncasecmp */
1011

11-
/**
12-
* @brief Enumeration of supported checksum algorithms.
13-
*/
12+
/** Checksum algorithms supported */
1413
enum Checksum {
1514
LIBACQUIRE_CRC32C,
1615
LIBACQUIRE_SHA256,
@@ -19,68 +18,205 @@ enum Checksum {
1918
};
2019

2120
/**
22-
* @brief Converts a string name (e.g., "sha256") to a Checksum enum.
23-
* @return The corresponding enum, or LIBACQUIRE_UNSUPPORTED_CHECKSUM.
21+
* @brief Convert string to checksum enum.
22+
*
23+
* Supports at least "CRC32C", "SHA256", "SHA512".
24+
*
25+
* @param s String of algorithm name (case-insensitive)
26+
* @return Enum value or LIBACQUIRE_UNSUPPORTED_CHECKSUM if unknown.
2427
*/
25-
extern LIBACQUIRE_EXPORT enum Checksum string2checksum(const char *);
28+
extern LIBACQUIRE_EXPORT enum Checksum string2checksum(const char *s);
2629

27-
/* --- Asynchronous Verification API --- */
30+
/* --- Asynchronous API --- */
2831

29-
/**
30-
* @brief Begins an asynchronous checksum verification (non-blocking).
31-
* @param handle An initialized acquire handle.
32-
* @param filepath The path to the file to verify.
33-
* @param algorithm The checksum algorithm to use.
34-
* @param expected_hash The expected hexadecimal checksum string.
35-
* @return 0 on success, non-zero on failure.
36-
*/
3732
extern LIBACQUIRE_EXPORT int
3833
acquire_verify_async_start(struct acquire_handle *handle, const char *filepath,
3934
enum Checksum algorithm, const char *expected_hash);
4035

41-
/**
42-
* @brief Polls the status of an asynchronous verification, processing a chunk.
43-
* @param handle The handle for the in-progress operation.
44-
* @return The current status of the operation.
45-
*/
4636
extern LIBACQUIRE_EXPORT enum acquire_status
4737
acquire_verify_async_poll(struct acquire_handle *handle);
4838

49-
/**
50-
* @brief Requests cancellation of an asynchronous verification.
51-
* @param handle The handle for the operation to be cancelled.
52-
*/
5339
extern LIBACQUIRE_EXPORT void
5440
acquire_verify_async_cancel(struct acquire_handle *handle);
5541

56-
/* --- Synchronous Verification API --- */
42+
/* --- Synchronous API --- */
5743

58-
/**
59-
* @brief Verifies a file's checksum synchronously (blocking).
60-
* @return 0 if the checksum matches, -1 on failure or mismatch.
61-
*/
6244
extern LIBACQUIRE_EXPORT int acquire_verify_sync(struct acquire_handle *handle,
6345
const char *filepath,
6446
enum Checksum algorithm,
6547
const char *expected_hash);
6648

49+
/* --- Backend internal function declarations --- */
50+
51+
#if defined(LIBACQUIRE_USE_COMMON_CRYPTO) || \
52+
defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL)
53+
int _openssl_verify_async_start(struct acquire_handle *handle,
54+
const char *filepath, enum Checksum algorithm,
55+
const char *expected_hash);
56+
enum acquire_status _openssl_verify_async_poll(struct acquire_handle *handle);
57+
void _openssl_verify_async_cancel(struct acquire_handle *handle);
58+
#endif
59+
60+
#ifdef LIBACQUIRE_USE_WINCRYPT
61+
int _wincrypt_verify_async_start(struct acquire_handle *handle,
62+
const char *filepath, enum Checksum algorithm,
63+
const char *expected_hash);
64+
enum acquire_status _wincrypt_verify_async_poll(struct acquire_handle *handle);
65+
void _wincrypt_verify_async_cancel(struct acquire_handle *handle);
66+
#endif
67+
68+
#ifdef LIBACQUIRE_USE_LIBRHASH
69+
int _librhash_verify_async_start(struct acquire_handle *handle,
70+
const char *filepath, enum Checksum algorithm,
71+
const char *expected_hash);
72+
enum acquire_status _librhash_verify_async_poll(struct acquire_handle *handle);
73+
void _librhash_verify_async_cancel(struct acquire_handle *handle);
74+
#endif
75+
76+
#ifdef LIBACQUIRE_USE_CRC32C
77+
int _crc32c_verify_async_start(struct acquire_handle *handle,
78+
const char *filepath, enum Checksum algorithm,
79+
const char *expected_hash);
80+
enum acquire_status _crc32c_verify_async_poll(struct acquire_handle *handle);
81+
void _crc32c_verify_async_cancel(struct acquire_handle *handle);
82+
#endif
83+
6784
#if defined(LIBACQUIRE_IMPLEMENTATION) && defined(LIBACQUIRE_CHECKSUMS_IMPL)
85+
86+
int acquire_verify_async_start(struct acquire_handle *handle,
87+
const char *filepath, enum Checksum algorithm,
88+
const char *expected_hash) {
89+
if (!handle || !filepath || !expected_hash) {
90+
if (handle)
91+
acquire_handle_set_error(handle, ACQUIRE_ERROR_INVALID_ARGUMENT,
92+
"Invalid arguments");
93+
return -1;
94+
}
95+
96+
#if defined(LIBACQUIRE_USE_COMMON_CRYPTO) || \
97+
defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL)
98+
if (algorithm == LIBACQUIRE_SHA256 || algorithm == LIBACQUIRE_SHA512)
99+
return _openssl_verify_async_start(handle, filepath, algorithm,
100+
expected_hash);
101+
#endif
102+
103+
#ifdef LIBACQUIRE_USE_WINCRYPT
104+
if (algorithm == LIBACQUIRE_SHA256 || algorithm == LIBACQUIRE_SHA512)
105+
return _wincrypt_verify_async_start(handle, filepath, algorithm,
106+
expected_hash);
107+
#endif
108+
109+
#ifdef LIBACQUIRE_USE_LIBRHASH
110+
if (algorithm == LIBACQUIRE_CRC32C || algorithm == LIBACQUIRE_SHA256 ||
111+
algorithm == LIBACQUIRE_SHA512)
112+
return _librhash_verify_async_start(handle, filepath, algorithm,
113+
expected_hash);
114+
#endif
115+
116+
#ifdef LIBACQUIRE_USE_CRC32C
117+
if (algorithm == LIBACQUIRE_CRC32C)
118+
return _crc32c_verify_async_start(handle, filepath, algorithm,
119+
expected_hash);
120+
#endif
121+
122+
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNSUPPORTED_ARCHIVE_FORMAT,
123+
"Unsupported checksum algorithm");
124+
return -1;
125+
}
126+
127+
enum acquire_status acquire_verify_async_poll(struct acquire_handle *handle) {
128+
if (!handle)
129+
return ACQUIRE_ERROR;
130+
131+
#if defined(LIBACQUIRE_USE_COMMON_CRYPTO) || \
132+
defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL)
133+
if (handle->backend_handle)
134+
return _openssl_verify_async_poll(handle);
135+
#endif
136+
137+
#ifdef LIBACQUIRE_USE_WINCRYPT
138+
if (handle->backend_handle)
139+
return _wincrypt_verify_async_poll(handle);
140+
#endif
141+
142+
#ifdef LIBACQUIRE_USE_LIBRHASH
143+
if (handle->backend_handle)
144+
return _librhash_verify_async_poll(handle);
145+
#endif
146+
147+
#ifdef LIBACQUIRE_USE_CRC32C
148+
if (handle->backend_handle)
149+
return _crc32c_verify_async_poll(handle);
150+
#endif
151+
152+
return ACQUIRE_ERROR;
153+
}
154+
155+
void acquire_verify_async_cancel(struct acquire_handle *handle) {
156+
if (!handle)
157+
return;
158+
159+
#if defined(LIBACQUIRE_USE_COMMON_CRYPTO) || \
160+
defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL)
161+
if (handle->backend_handle) {
162+
_openssl_verify_async_cancel(handle);
163+
return;
164+
}
165+
#endif
166+
167+
#ifdef LIBACQUIRE_USE_WINCRYPT
168+
if (handle->backend_handle) {
169+
_wincrypt_verify_async_cancel(handle);
170+
return;
171+
}
172+
#endif
173+
174+
#ifdef LIBACQUIRE_USE_LIBRHASH
175+
if (handle->backend_handle) {
176+
_librhash_verify_async_cancel(handle);
177+
return;
178+
}
179+
#endif
180+
181+
#ifdef LIBACQUIRE_USE_CRC32C
182+
if (handle->backend_handle) {
183+
_crc32c_verify_async_cancel(handle);
184+
return;
185+
}
186+
#endif
187+
}
188+
189+
int acquire_verify_sync(struct acquire_handle *handle, const char *filepath,
190+
enum Checksum algorithm, const char *expected_hash) {
191+
if (!handle || !filepath || !expected_hash)
192+
return -1;
193+
194+
if (acquire_verify_async_start(handle, filepath, algorithm, expected_hash) !=
195+
0)
196+
return -1;
197+
198+
while (acquire_verify_async_poll(handle) == ACQUIRE_IN_PROGRESS)
199+
;
200+
201+
return (handle->status == ACQUIRE_COMPLETE) ? 0 : -1;
202+
}
203+
68204
enum Checksum string2checksum(const char *const s) {
69205
if (s == NULL)
70206
return LIBACQUIRE_UNSUPPORTED_CHECKSUM;
71-
else if (strncasecmp(s, "CRC32C", 6) == 0)
207+
if (strncasecmp(s, "CRC32C", 6) == 0)
72208
return LIBACQUIRE_CRC32C;
73-
else if (strncasecmp(s, "SHA256", 6) == 0)
209+
if (strncasecmp(s, "SHA256", 6) == 0)
74210
return LIBACQUIRE_SHA256;
75-
else if (strncasecmp(s, "SHA512", 6) == 0)
211+
if (strncasecmp(s, "SHA512", 6) == 0)
76212
return LIBACQUIRE_SHA512;
77-
else
78-
return LIBACQUIRE_UNSUPPORTED_CHECKSUM;
213+
return LIBACQUIRE_UNSUPPORTED_CHECKSUM;
79214
}
80-
#endif /* defined(LIBACQUIRE_IMPLEMENTATION) && \
81-
defined(LIBACQUIRE_CHECKSUMS_IMPL) */
215+
216+
#endif /* LIBACQUIRE_IMPLEMENTATION && LIBACQUIRE_CHECKSUMS_IMPL */
82217

83218
#ifdef __cplusplus
84219
}
85-
#endif /* __cplusplus */
86-
#endif /* !LIBACQUIRE_ACQUIRE_CHECKSUMS_H */
220+
#endif
221+
222+
#endif /* LIBACQUIRE_ACQUIRE_CHECKSUMS_H */

0 commit comments

Comments
 (0)