33
44#ifdef __cplusplus
55extern "C" {
6- #endif
6+ #endif /* __cplusplus */
77
88#include "acquire_handle.h"
99#include "libacquire_export.h"
@@ -93,112 +93,101 @@ int acquire_verify_async_start(struct acquire_handle *handle,
9393 return -1 ;
9494 }
9595
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-
96+ /* duplicate labels so can't have a 'global' switch/case */
97+ #if defined(LIBACQUIRE_USE_COMMON_CRYPTO ) || defined(LIBACQUIRE_USE_OPENSSL ) || defined(LIBACQUIRE_USE_LIBRESSL )
98+ switch (algorithm ) {
99+ case LIBACQUIRE_SHA256 :
100+ case LIBACQUIRE_SHA512 :
101+ return _openssl_verify_async_start (handle , filepath , algorithm ,
102+ expected_hash );
103+ default : break ;
104+ }
105+ #endif /* defined(LIBACQUIRE_USE_COMMON_CRYPTO) || defined(LIBACQUIRE_USE_OPENSSL) || defined(LIBACQUIRE_USE_LIBRESSL) */
103106#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-
107+ switch (algorithm ) {
108+ case LIBACQUIRE_SHA256 :
109+ case LIBACQUIRE_SHA512 :
110+ return _wincrypt_verify_async_start (handle , filepath , algorithm ,
111+ expected_hash );
112+ default : break ;
113+ }
114+ #endif /* LIBACQUIRE_USE_WINCRYPT */
109115#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 ,
116+ switch (algorithm ) {
117+ case LIBACQUIRE_CRC32C :
118+ case LIBACQUIRE_SHA256 :
119+ case LIBACQUIRE_SHA512 :
120+ return _librhash_verify_async_start (handle , filepath , algorithm ,
113121 expected_hash );
114- #endif
115-
122+ default :
123+ break ;
124+ }
125+ #endif /* LIBACQUIRE_USE_LIBRHASH */
116126#ifdef LIBACQUIRE_USE_CRC32C
117- if (algorithm == LIBACQUIRE_CRC32C )
118- return _crc32c_verify_async_start (handle , filepath , algorithm ,
127+ switch (algorithm ) {
128+ case LIBACQUIRE_CRC32C :
129+ return _crc32c_verify_async_start (handle , filepath , algorithm ,
119130 expected_hash );
120- #endif
131+ default : break ;
132+ }
133+ #endif /* LIBACQUIRE_USE_CRC32C */
121134
122- acquire_handle_set_error (handle , ACQUIRE_ERROR_UNSUPPORTED_ARCHIVE_FORMAT ,
123- "Unsupported checksum algorithm" );
135+ acquire_handle_set_error (
136+ handle , ACQUIRE_ERROR_UNSUPPORTED_ARCHIVE_FORMAT ,
137+ "Unsupported checksum algorithm or no backend for it" );
124138 return -1 ;
125139}
126140
127141enum acquire_status acquire_verify_async_poll (struct acquire_handle * handle ) {
128- if (!handle )
142+ if (!handle || ! handle -> backend_handle )
129143 return ACQUIRE_ERROR ;
130144
131145#if defined(LIBACQUIRE_USE_COMMON_CRYPTO ) || \
132146 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-
147+ return _openssl_verify_async_poll (handle );
148+ #elif defined(LIBACQUIRE_USE_WINCRYPT )
149+ return _wincrypt_verify_async_poll (handle );
150+ #elif defined(LIBACQUIRE_USE_LIBRHASH )
151+ return _librhash_verify_async_poll (handle );
152+ #elif defined(LIBACQUIRE_USE_CRC32C )
153+ return _crc32c_verify_async_poll (handle );
154+ #else
155+ acquire_handle_set_error (handle , ACQUIRE_ERROR_UNKNOWN ,
156+ "No checksum backend compiled" );
152157 return ACQUIRE_ERROR ;
158+ #endif
153159}
154160
155161void acquire_verify_async_cancel (struct acquire_handle * handle ) {
156- if (!handle )
162+ if (!handle || ! handle -> backend_handle )
157163 return ;
158164
159165#if defined(LIBACQUIRE_USE_COMMON_CRYPTO ) || \
160166 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- }
167+ _openssl_verify_async_cancel (handle );
168+ #elif defined(LIBACQUIRE_USE_WINCRYPT )
169+ _wincrypt_verify_async_cancel (handle );
170+ #elif defined(LIBACQUIRE_USE_LIBRHASH )
171+ _librhash_verify_async_cancel (handle );
172+ #elif defined(LIBACQUIRE_USE_CRC32C )
173+ _crc32c_verify_async_cancel (handle );
186174#endif
187175}
188176
189177int acquire_verify_sync (struct acquire_handle * handle , const char * filepath ,
190178 enum Checksum algorithm , const char * expected_hash ) {
179+ enum acquire_status status ;
191180 if (!handle || !filepath || !expected_hash )
192181 return -1 ;
193182
194183 if (acquire_verify_async_start (handle , filepath , algorithm , expected_hash ) !=
195184 0 )
196185 return -1 ;
197186
198- while (acquire_verify_async_poll (handle ) == ACQUIRE_IN_PROGRESS )
187+ while (( status = acquire_verify_async_poll (handle ) ) == ACQUIRE_IN_PROGRESS )
199188 ;
200189
201- return (handle -> status == ACQUIRE_COMPLETE ) ? 0 : -1 ;
190+ return (status == ACQUIRE_COMPLETE ) ? 0 : -1 ;
202191}
203192
204193enum Checksum string2checksum (const char * const s ) {
@@ -217,6 +206,6 @@ enum Checksum string2checksum(const char *const s) {
217206
218207#ifdef __cplusplus
219208}
220- #endif
209+ #endif /* __cplusplus */
221210
222211#endif /* LIBACQUIRE_ACQUIRE_CHECKSUMS_H */
0 commit comments