Skip to content

Commit 302e08c

Browse files
committed
[acquire/*.h,**CMakeLists.txt] Progress on fixing Windows (MSVC) builds
1 parent 9808a82 commit 302e08c

16 files changed

+357
-179
lines changed

acquire/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ else ()
184184

185185
message(STATUS "header_impls = ${header_impls}")
186186

187+
if (WIN32)
188+
list(APPEND header_impls "acquire_windows.h")
189+
endif (WIN32)
190+
187191
foreach (header_file IN LISTS header_impls)
188192
if (header_file STREQUAL "")
189193
continue()
@@ -306,12 +310,12 @@ else ()
306310
if (HAS_LIBBSD)
307311
set_source_files_properties(
308312
${src} PROPERTIES
309-
COMPILE_DEFINITIONS "LIBACQUIRE_IMPLEMENTATION=1;LIBACQUIRE_STRCASESTR_IMPL=1;LIBACQUIRE_STRERRORLEN_IMPL=1"
313+
COMPILE_DEFINITIONS "LIBACQUIRE_IMPLEMENTATION=1;STRCASESTR_IMPL=1;STRERRORLEN_IMPL=1"
310314
)
311315
else ()
312316
set_source_files_properties(
313317
${src} PROPERTIES
314-
COMPILE_DEFINITIONS "LIBACQUIRE_IMPLEMENTATION=1;LIBACQUIRE_STRCASESTR_IMPL=1;LIBACQUIRE_STRERRORLEN_IMPL=1;STRNSTR_IMPL=1"
318+
COMPILE_DEFINITIONS "LIBACQUIRE_IMPLEMENTATION=1;STRCASESTR_IMPL=1;STRERRORLEN_IMPL=1;STRNSTR_IMPL=1"
315319
)
316320
endif ()
317321
elseif (src MATCHES "/gen_acquire_url_utils.c$")

acquire/acquire_checksums.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,23 @@ extern "C" {
88
#include "acquire_handle.h"
99
#include "libacquire_export.h"
1010

11+
#if defined(LIBACQUIRE_USE_CRC32C) && LIBACQUIRE_USE_CRC32C
12+
#include "acquire_crc32c.h"
13+
#endif /* defined(LIBACQUIRE_USE_CRC32C) && LIBACQUIRE_USE_CRC32C */
14+
15+
#if defined(LIBACQUIRE_USE_LIBRHASH) && LIBACQUIRE_USE_LIBRHASH
1116
#include "acquire_librhash.h"
17+
#endif
18+
19+
#if (defined(LIBACQUIRE_USE_OPENSSL) && LIBACQUIRE_USE_OPENSSL) || \
20+
(defined(LIBACQUIRE_USE_COMMON_CRYPTO) && LIBACQUIRE_USE_COMMON_CRYPTO) || \
21+
(defined(LIBACQUIRE_USE_LIBRESSL) && LIBACQUIRE_USE_LIBRESSL)
1222
#include "acquire_openssl.h"
23+
#endif
24+
25+
#if defined(LIBACQUIRE_USE_WINCRYPT) && LIBACQUIRE_USE_WINCRYPT
26+
#include "acquire_wincrypt.h"
27+
#endif
1328

1429
extern LIBACQUIRE_EXPORT enum Checksum string2checksum(const char *s);
1530
extern LIBACQUIRE_EXPORT int
@@ -33,6 +48,7 @@ extern LIBACQUIRE_EXPORT int acquire_verify_sync(struct acquire_handle *handle,
3348
#else
3449
#include <unistd.h> /* For usleep() */
3550
#endif
51+
#include <acquire_string_extras.h>
3652

3753
enum Checksum string2checksum(const char *const s) {
3854
if (s == NULL)

acquire/acquire_crc32c.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ void _crc32c_verify_async_cancel(struct acquire_handle *handle);
2020
#if defined(LIBACQUIRE_IMPLEMENTATION) && defined(LIBACQUIRE_USE_CRC32C) && \
2121
LIBACQUIRE_USE_CRC32C
2222

23-
#include "acquire_handle.h"
2423
#include <errno.h>
2524
#include <stdint.h>
2625
#include <stdlib.h>
2726
#include <string.h>
2827

28+
#include "acquire_handle.h"
29+
#include "acquire_string_extras.h"
30+
2931
#ifndef CHUNK_SIZE
3032
#define CHUNK_SIZE 4096
3133
#endif /* !CHUNK_SIZE */
@@ -116,15 +118,33 @@ int _crc32c_verify_async_start(struct acquire_handle *handle,
116118
"Out of memory");
117119
return -1;
118120
}
121+
#if defined(_MSC_VER) || defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__
122+
{
123+
const errno_t err = fopen_s(&be->file, filepath, "rb");
124+
if (err != 0 || be->file == NULL) {
125+
fprintf(stderr, "couldn't open file for reading %s\n", filepath);
126+
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_OPEN_FAILED,
127+
"Cannot open file: %s", filepath);
128+
free(be);
129+
return -1;
130+
}
131+
}
132+
#else
119133
be->file = fopen(filepath, "rb");
120134
if (!be->file) {
121135
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_OPEN_FAILED,
122136
"Cannot open file: %s", strerror(errno));
123137
free(be);
124138
return -1;
125139
}
140+
#endif
126141
be->crc = crc32c_init();
142+
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) || \
143+
defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__
144+
strncpy_s(be->expected_hash, sizeof be->expected_hash, expected_hash, 8);
145+
#else
127146
strncpy(be->expected_hash, expected_hash, 8);
147+
#endif
128148
be->expected_hash[8] = '\0';
129149
handle->backend_handle = be;
130150
handle->status = ACQUIRE_IN_PROGRESS;
@@ -149,12 +169,20 @@ enum acquire_status _crc32c_verify_async_poll(struct acquire_handle *handle) {
149169
bytes_read = fread(buffer, 1, sizeof(buffer), be->file);
150170
if (bytes_read > 0) {
151171
be->crc = crc32c_update(be->crc, buffer, bytes_read);
152-
handle->bytes_processed += bytes_read;
172+
handle->bytes_processed += (off_t)bytes_read;
153173
return ACQUIRE_IN_PROGRESS;
154174
}
155175
if (ferror(be->file)) {
176+
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) || \
177+
defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__
178+
char error_code[256];
179+
strerror_s(error_code, sizeof(error_code), errno);
180+
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_READ_FAILED,
181+
"File read error: %s", error_code);
182+
#else
156183
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_READ_FAILED,
157184
"File read error: %s", strerror(errno));
185+
#endif
158186
} else {
159187
char computed_hex[9];
160188
uint32_t final_crc = crc32c_finalize(be->crc);

acquire/acquire_libfetch.h

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,6 @@
2222
const char *get_download_dir(void) { return ".downloads"; }
2323
#endif /* LIBACQUIRE_DOWNLOAD_DIR_IMPL */
2424

25-
/* --- Common Handle Management --- */
26-
27-
struct acquire_handle *acquire_handle_init(void) {
28-
struct acquire_handle *handle =
29-
(struct acquire_handle *)calloc(1, sizeof(struct acquire_handle));
30-
if (handle) {
31-
handle->total_size = -1;
32-
handle->status = ACQUIRE_IDLE;
33-
}
34-
return handle;
35-
}
36-
37-
void acquire_handle_free(struct acquire_handle *handle) {
38-
if (!handle)
39-
return;
40-
if (handle->output_file)
41-
fclose(handle->output_file);
42-
free(handle);
43-
}
44-
45-
const char *acquire_handle_get_error_string(struct acquire_handle *handle) {
46-
return handle ? handle->error_message : "Invalid handle.";
47-
}
48-
4925
/* --- Synchronous API --- */
5026

5127
/**
@@ -96,7 +72,7 @@ int acquire_download_sync(struct acquire_handle *handle, const char *url,
9672
break;
9773
}
9874
fwrite(buffer, 1, bytes_read, handle->output_file);
99-
handle->bytes_processed += bytes_read;
75+
handle->bytes_processed += (off_t)bytes_read;
10076
}
10177

10278
fclose(handle->output_file);

acquire/acquire_librhash.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern "C" {
1212
#include "acquire_common_defs.h"
1313
#include "acquire_handle.h"
1414
#include "libacquire_export.h"
15+
#include <acquire_string_extras.h>
1516

1617
struct rhash_backend {
1718
rhash handle;
@@ -98,12 +99,15 @@ int _librhash_verify_async_start(struct acquire_handle *handle,
9899
"rhash backend allocation failed");
99100
return -1;
100101
} /* LCOV_EXCL_STOP */
101-
be->file = fopen(filepath, "rb");
102-
if (!be->file) {
103-
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_OPEN_FAILED, "%s",
104-
strerror(errno));
105-
free(be);
106-
return -1;
102+
{
103+
const errno_t err = fopen_s(&be->file, filepath, "rb");
104+
if (err != 0 || be->file == NULL) {
105+
fprintf(stderr, "couldn't open file for reading %s\n", filepath);
106+
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_OPEN_FAILED,
107+
"Cannot open file: %s", filepath);
108+
free(be);
109+
return -1;
110+
}
107111
}
108112
be->handle = rhash_init(rhash_algo_id);
109113
if (!be->handle) { /* LCOV_EXCL_START */
@@ -113,7 +117,17 @@ int _librhash_verify_async_start(struct acquire_handle *handle,
113117
return -1;
114118
} /* LCOV_EXCL_STOP */
115119
be->algorithm_id = rhash_algo_id;
120+
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) || \
121+
defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__
122+
{
123+
const errno_t e = strncpy_s(be->expected_hash, sizeof(be->expected_hash),
124+
expected_hash, sizeof(be->expected_hash) - 1);
125+
if (e)
126+
be->expected_hash[0] = '\0';
127+
}
128+
#else
116129
strncpy(be->expected_hash, expected_hash, sizeof(be->expected_hash) - 1);
130+
#endif
117131
handle->backend_handle = be;
118132
handle->status = ACQUIRE_IN_PROGRESS;
119133
return 0;
@@ -146,13 +160,21 @@ enum acquire_status _librhash_verify_async_poll(struct acquire_handle *handle) {
146160
acquire_handle_set_error(handle, ACQUIRE_ERROR_UNKNOWN,
147161
"rhash_update failed");
148162
} else { /* LCOV_EXCL_STOP */
149-
handle->bytes_processed += bytes_read;
163+
handle->bytes_processed += (off_t)bytes_read;
150164
return ACQUIRE_IN_PROGRESS;
151165
}
152166
}
153167
if (ferror(be->file)) { /* LCOV_EXCL_START */
154-
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_READ_FAILED, "%s",
155-
strerror(errno));
168+
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) || \
169+
defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__
170+
char error_code[256];
171+
strerror_s(error_code, sizeof(error_code), errno);
172+
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_READ_FAILED,
173+
"File read error: %s", error_code);
174+
#else
175+
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_READ_FAILED,
176+
"File read error: %s", strerror(errno));
177+
#endif
156178
} else { /* LCOV_EXCL_STOP */
157179
unsigned char hash[64];
158180
char computed_hex[130];

acquire/acquire_openssl.h

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void _openssl_verify_async_cancel(struct acquire_handle *handle);
3737
#endif /* !EVP_MAX_MD_SIZE */
3838

3939
#include "acquire_handle.h"
40+
#include <acquire_string_extras.h>
4041
#include <errno.h>
4142
#include <stdlib.h>
4243
#include <string.h>
@@ -103,13 +104,26 @@ int _openssl_verify_async_start(struct acquire_handle *handle,
103104
"openssl backend memory allocation failed");
104105
return -1;
105106
}
107+
#if defined(_MSC_VER) || defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__
108+
{
109+
const errno_t err = fopen_s(&be->file, filepath, "rb");
110+
if (err != 0 || be->file == NULL) {
111+
fprintf(stderr, "couldn't open file for reading %s\n", filepath);
112+
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_OPEN_FAILED,
113+
"Cannot open file: %s", filepath);
114+
free(be);
115+
return -1;
116+
}
117+
}
118+
#else
106119
be->file = fopen(filepath, "rb");
107120
if (!be->file) {
108121
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_OPEN_FAILED, "%s",
109122
strerror(errno));
110123
free(be);
111124
return -1;
112125
}
126+
#endif
113127

114128
#if defined(LIBACQUIRE_USE_COMMON_CRYPTO) && LIBACQUIRE_USE_COMMON_CRYPTO
115129
be->algorithm = algorithm;
@@ -135,7 +149,17 @@ int _openssl_verify_async_start(struct acquire_handle *handle,
135149
}
136150
#endif
137151

152+
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) || \
153+
defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__
154+
{
155+
const errno_t e = strncpy_s(be->expected_hash, sizeof(be->expected_hash),
156+
expected_hash, sizeof(be->expected_hash) - 1);
157+
if (e)
158+
be->expected_hash[0] = '\0';
159+
}
160+
#else
138161
strncpy(be->expected_hash, expected_hash, sizeof(be->expected_hash) - 1);
162+
#endif
139163
handle->backend_handle = be;
140164
handle->status = ACQUIRE_IN_PROGRESS;
141165
return 0;
@@ -181,13 +205,21 @@ enum acquire_status _openssl_verify_async_poll(struct acquire_handle *handle) {
181205
}
182206
#endif
183207
if (handle->error.code == ACQUIRE_OK) {
184-
handle->bytes_processed += bytes_read;
208+
handle->bytes_processed += (off_t)bytes_read;
185209
return ACQUIRE_IN_PROGRESS;
186210
}
187211
}
188212
if (ferror(be->file)) {
189-
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_READ_FAILED, "%s",
190-
strerror(errno));
213+
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) || \
214+
defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__
215+
char error_code[256];
216+
strerror_s(error_code, sizeof(error_code), errno);
217+
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_READ_FAILED,
218+
"File read error: %s", error_code);
219+
#else
220+
acquire_handle_set_error(handle, ACQUIRE_ERROR_FILE_READ_FAILED,
221+
"File read error: %s", strerror(errno));
222+
#endif
191223
} else {
192224
unsigned char hash[EVP_MAX_MD_SIZE];
193225
char computed_hex[EVP_MAX_MD_SIZE * 2 + 1];

0 commit comments

Comments
 (0)