Skip to content

Commit a62b5a1

Browse files
committed
[*.h] Expand documentation ; [acquire/tests/test_url_utils.h] New test suite ; [acquire/tests/test_checksum.h] Massively expand test suite ; [acquire/tests/CMakeLists.txt] Ensure included test headers are also explicitly referenced here
1 parent 7af3ef7 commit a62b5a1

12 files changed

+566
-81
lines changed

acquire/acquire_checksums.h

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
/*
2-
* Prototype for checksum API
3-
*
4-
* Always `#include` this when adding new checksum implementations,
5-
* to ensure the implementation matches the prototype.
6-
* */
7-
81
#ifndef LIBACQUIRE_ACQUIRE_CHECKSUMS_H
92
#define LIBACQUIRE_ACQUIRE_CHECKSUMS_H
103

4+
/**
5+
* @file acquire_checksums.h
6+
* @brief Collection of checksum function declarations and interface.
7+
*
8+
* This header declares typedefs and functions for computing and verifying
9+
* various checksum algorithms like crc32c, md5, sha1, etc.
10+
* Also provides functionality to get checksum function by name.
11+
*
12+
* NOTE: Always `#include` this when adding new checksum implementations,
13+
* to ensure the implementation matches the prototype.
14+
*/
15+
1116
#ifdef __cplusplus
1217
extern "C" {
1318
#endif /* __cplusplus */
@@ -21,11 +26,42 @@ extern "C" {
2126
#include "acquire_config.h"
2227
#include "acquire_string_extras.h"
2328

24-
extern LIBACQUIRE_LIB_EXPORT bool crc32c(const char *, const char *);
29+
/**
30+
* @brief Boolean result type alias for checksum validation functions.
31+
*/
2532

26-
extern LIBACQUIRE_LIB_EXPORT bool sha256(const char *, const char *);
33+
/**
34+
* @brief Verify file using CRC32C checksum.
35+
*
36+
* @param filename Path to file.
37+
* @param hash Expected CRC32C checksum string.
38+
*
39+
* @return true if valid.
40+
*/
41+
extern LIBACQUIRE_LIB_EXPORT bool crc32c(const char *filename,
42+
const char *hash);
2743

28-
extern LIBACQUIRE_LIB_EXPORT bool sha512(const char *, const char *);
44+
/**
45+
* @brief Verify file using SHA256 checksum.
46+
*
47+
* @param filename Path to file.
48+
* @param hash Expected checksum string.
49+
*
50+
* @return true if valid.
51+
*/
52+
extern LIBACQUIRE_LIB_EXPORT bool sha256(const char *filename,
53+
const char *hash);
54+
55+
/**
56+
* @brief Verify file using SHA256 checksum.
57+
*
58+
* @param filename Path to file.
59+
* @param hash Expected checksum string.
60+
*
61+
* @return true if valid.
62+
*/
63+
extern LIBACQUIRE_LIB_EXPORT bool sha512(const char *filename,
64+
const char *hash);
2965

3066
enum Checksum {
3167
LIBACQUIRE_CRC32C,
@@ -36,8 +72,19 @@ enum Checksum {
3672

3773
extern LIBACQUIRE_LIB_EXPORT enum Checksum string2checksum(const char *);
3874

39-
extern LIBACQUIRE_LIB_EXPORT bool (*get_checksum_function(enum Checksum))(
40-
const char *, const char *);
75+
/**
76+
* @brief Obtain a pointer to a checksum function by name.
77+
*
78+
* Returns a pointer to a function matching the
79+
* signature of checksum_func_t, or NULL if not found.
80+
*
81+
* @param checksum Discriminant from `enum Checksum` representing checksum
82+
* algorithm
83+
*
84+
* @return Function pointer on success; NULL on failure.
85+
*/
86+
extern LIBACQUIRE_LIB_EXPORT bool (
87+
*get_checksum_function(enum Checksum checksum))(const char *, const char *);
4188

4289
#ifdef LIBACQUIRE_IMPLEMENTATION
4390

acquire/acquire_common_defs.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
/*
2-
* Common definitions used through libacquire
3-
*
4-
* Defines things like `PATH_SEP`, `MAX_FILENAME`, and `NAME_MAX`
5-
* */
6-
71
#ifndef LIBACQUIRE_ACQUIRE_COMMON_DEFS_H
82
#define LIBACQUIRE_ACQUIRE_COMMON_DEFS_H
93

4+
/**
5+
* @file acquire_common_defs.h
6+
* @brief Common macros, constants, and definitions used throughout libacquire.
7+
*
8+
* This header defines frequently used constants, macros for error handling,
9+
* log level definitions, and other common values.
10+
*/
11+
1012
#ifdef __cplusplus
1113
extern "C" {
1214
#endif /* __cplusplus */

acquire/acquire_crc32c.h

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
/*
2-
* Taken from FreeBSD implementation of CRC32C, specifically:
3-
* - usr.bin\cksum\crc32_algo.c @ d91d2b513eb30a226e87f0e52e2f9f232a2e1ca3
4-
*
5-
* Actually taken from:
6-
* - https://gist.github.com/Jessidhia/1484174
7-
*
8-
* Alternatives are to use `zlib`'s CRC32C impl conditionally or RHash or miniz
9-
* */
10-
11-
#if !defined(LIBACQUIRE_ACQUIRE_CRC32C_H) && \
12-
defined(LIBACQUIRE_IMPLEMENTATION) && defined(USE_CRC32C)
1+
#ifndef LIBACQUIRE_ACQUIRE_CRC32C_H
132
#define LIBACQUIRE_ACQUIRE_CRC32C_H
143

4+
/**
5+
* @file acquire_crc32c.h
6+
* @brief Header declaring the CRC32C checksum verification function.
7+
*
8+
* This header provides the interface and implementation of a function
9+
* to compute and verify the CRC32C checksum of a file against a known hash.
10+
*/
11+
1512
#ifdef __cplusplus
1613
extern "C" {
1714
#elif defined(HAS_STDBOOL) && !defined(bool)
@@ -20,6 +17,33 @@ extern "C" {
2017
#include "acquire_stdbool.h"
2118
#endif /* __cplusplus */
2219

20+
#include <stdio.h>
21+
22+
/**
23+
* @brief Verify the CRC32C checksum of a given file against an expected hash
24+
* string.
25+
*
26+
* Opens the file, computes its CRC32C checksum, then compares it with the input
27+
* hash string case-insensitively.
28+
*
29+
* @param filename Path to the file to be validated.
30+
* @param hash Expected CRC32C checksum string in hexadecimal (8 characters).
31+
*
32+
* @return true if computed checksum matches input hash, false otherwise or on
33+
* error.
34+
*/
35+
extern bool crc32c(const char *filename, const char *hash);
36+
37+
/*
38+
* Taken from FreeBSD implementation of CRC32C, specifically:
39+
* - usr.bin\cksum\crc32_algo.c @ d91d2b513eb30a226e87f0e52e2f9f232a2e1ca3
40+
*
41+
* Actually taken from:
42+
* - https://gist.github.com/Jessidhia/1484174
43+
*
44+
* Alternatives are to use `zlib`'s CRC32C impl conditionally or RHash or miniz
45+
* */
46+
2347
#define CHUNK_SIZE 4096
2448

2549
#include <stdio.h>
@@ -33,7 +57,7 @@ extern "C" {
3357

3458
#ifndef O_BINARY
3559
#define O_BINARY 0
36-
#endif
60+
#endif /* !O_BINARY */
3761

3862
unsigned int crc32_file(FILE *file);
3963
unsigned int crc32_algo(unsigned int iv, unsigned char *buf, long long len);
@@ -101,40 +125,43 @@ unsigned int crc32_file(FILE *file) {
101125
return crc;
102126
}
103127

128+
#ifdef LIBACQUIRE_IMPLEMENTATION
129+
#ifdef USE_CRC32C
130+
131+
/**
132+
* @brief Implementation of CRC32C checksum verification.
133+
*
134+
* Reads the file and computes its CRC32C checksum, then compares with
135+
* the hash string ignoring character cases.
136+
*/
104137
bool crc32c(const char *filename, const char *hash) {
105138
unsigned int crc32_res;
106139
FILE *fh;
107-
/* Format computed CRC32C in hex into buffer 9 chars, zero terminated */
108140
char computed[9];
109-
#if defined(_MSC_VER) || defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__
141+
142+
#if defined(_MSC_VER) || (defined(__STDC_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__)
110143
fopen_s(&fh, filename, "rb");
111144
#else
112145
fh = fopen(filename, "rb");
113-
#endif
114-
if (fh != NULL) {
115-
crc32_res = crc32_file(fh);
116-
printf("crc32_res: %x\n", crc32_res);
117-
fclose(fh);
118-
}
119-
return true;
146+
#endif /* defined(_MSC_VER) || (defined(__STDC_LIB_EXT1__) && \
147+
__STDC_WANT_LIB_EXT1__) */
148+
if (fh == NULL)
149+
return false;
120150

121151
crc32_res = crc32_file(fh);
122152
fclose(fh);
123153

124-
/* Verify CRC32C checksum: input hash string expected to be hex digits */
125154
snprintf(computed, sizeof(computed), "%08x", crc32_res);
126155

127-
/* Case insensitive compare */
128156
{
129157
int i;
130158
for (i = 0; computed[i] && hash[i]; i++) {
131159
char c1 = computed[i];
132160
char c2 = hash[i];
133-
/* convert both to lowercase */
134161
if (c1 >= 'A' && c1 <= 'F')
135-
c1 += ('a' - 'A');
162+
c1 += 'a' - 'A';
136163
if (c2 >= 'A' && c2 <= 'F')
137-
c2 += ('a' - 'A');
164+
c2 += 'a' - 'A';
138165
if (c1 != c2)
139166
return false;
140167
}
@@ -144,9 +171,11 @@ bool crc32c(const char *filename, const char *hash) {
144171
return true;
145172
}
146173

174+
#endif /* USE_CRC32C */
175+
#endif /* LIBACQUIRE_IMPLEMENTATION */
176+
147177
#ifdef __cplusplus
148178
}
149179
#endif /* __cplusplus */
150180

151-
#endif /* !defined(LIBACQUIRE_ACQUIRE_CRC32C_H) && \
152-
defined(LIBACQUIRE_IMPLEMENTATION) && defined(USE_CRC32C) */
181+
#endif /* !LIBACQUIRE_ACQUIRE_CRC32C_H */

acquire/acquire_download.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
/*
2-
* Prototype for download API
3-
*
4-
* Always `#include` this when adding new download implementations,
5-
* to ensure the implementation matches the prototype.
6-
* */
7-
81
#ifndef LIBACQUIRE_ACQUIRE_DOWNLOAD_H
92
#define LIBACQUIRE_ACQUIRE_DOWNLOAD_H
103

4+
/**
5+
* @file acquire_download.h
6+
* @brief Functionality to download files from network locations.
7+
*
8+
* This module handles downloading files via protocols like HTTP(S),
9+
* providing progress monitoring and retry logic support.
10+
*
11+
* NOTE: Always `#include` this when adding new download implementations,
12+
* to ensure the implementation matches the prototype.
13+
*/
14+
1115
#include <stdlib.h>
1216

1317
#ifdef __cplusplus

acquire/acquire_extract.h

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
/*
22
* Prototype for extract API
33
*
4-
* Always `#include` this when adding new extraction implementations,
5-
* to ensure the implementation matches the prototype.
64
*
7-
* The extract API is for expanding the contents of archives
85
* */
96

107
#ifndef LIBACQUIRE_ACQUIRE_EXTRACT_H
118
#define LIBACQUIRE_ACQUIRE_EXTRACT_H
129

10+
/**
11+
* @file acquire_extract.h
12+
* @brief Extraction of compressed archives.
13+
*
14+
* Provides functions to decompress and extract files from archives,
15+
* supporting common formats like ZIP, TAR, GZ, etc.
16+
* NOTE: Always `#include` this when adding new extraction
17+
* implementations, to ensure implementation matches the prototype.
18+
*
19+
* The extract API is for expanding the contents of archives
20+
*/
21+
1322
#ifdef __cplusplus
1423
extern "C" {
1524
#endif /* __cplusplus */
@@ -22,10 +31,34 @@ enum LIBACQUIRE_LIB_EXPORT Archive {
2231
LIBACQUIRE_UNSUPPORTED_ARCHIVE
2332
};
2433

25-
extern LIBACQUIRE_LIB_EXPORT int extract_archive(enum Archive, const char *,
26-
const char *);
34+
/**
35+
* @brief Extract an archive file into a specified directory.
36+
*
37+
* Supports formats based on `enum Archive` discriminant.
38+
*
39+
* @param archive `enum Archive` discriminant type of archive.
40+
* @param archive_filepath Path to the archive file.
41+
* @param output_folder Directory path where contents should be extracted.
42+
*
43+
* @return `EXIT_SUCCESS` if extraction succeeds;
44+
* `EXIT_FAILURE` or non-`EXIT_SUCCESS` otherwise.
45+
*/
46+
extern LIBACQUIRE_LIB_EXPORT int extract_archive(enum Archive archive,
47+
const char *archive_filepath,
48+
const char *output_folder);
2749

28-
extern LIBACQUIRE_LIB_EXPORT enum Archive extension2archive(const char *);
50+
/**
51+
* @brief Determine archive type based on extension
52+
*
53+
* TODO: magic bytes whence `LIBACQUIRE_INFER`
54+
*
55+
* @param extension Simple end of path, like ".zip" or ".tar.gz".
56+
*
57+
* @return `enum Archive` discriminant; including potential values of
58+
* `LIBACQUIRE_UNSUPPORTED_ARCHIVE` xor `LIBACQUIRE_INFER`.
59+
*/
60+
extern LIBACQUIRE_LIB_EXPORT enum Archive
61+
extension2archive(const char *extension);
2962

3063
#ifdef LIBACQUIRE_IMPLEMENTATION
3164

0 commit comments

Comments
 (0)