Skip to content

Commit 90e6645

Browse files
committed
[acquire/acquire_net_common.h] Experimentally improve header file ; [acquire/acquire_string_extras.h] clang-format ; [README.md] Improve formatting
1 parent e94df59 commit 90e6645

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Supports:
2727

2828
- Extremely portable C code (ANSI C: C89)
2929
- link with different libraries, including those built in to your OS
30-
- very small executable size (thanks to above)
30+
- _very_ small executable size (thanks to above)
3131
- fast
3232
- simple API, easy to integrate in your C project (or really any language, they all have nice FFI back to C or a C intermediary language)
3333
- default cipher selection >= TLS 1.2
@@ -82,9 +82,10 @@ Note that most checksum libraries are crypto libraries so working with these API
8282
## Docker
8383

8484
`Dockerfile`s are provided for convenience, try them out, e.g., by running:
85-
86-
docker build . -f Dockerfile.alpine --tag libacquire
87-
docker run libacquire
85+
```sh
86+
docker build . -f Dockerfile.alpine --tag libacquire
87+
docker run libacquire
88+
```
8889

8990
## Shell script equivalent (UNIX with `grep`, `curl`, and `tar`)
9091

acquire/acquire_net_common.h

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
2-
* API that is common between all network libraries… however requires network
3-
* libs included first
2+
* Networking helper API
3+
*
4+
* Provides minimal networking-related helpers used throughout libacquire.
5+
* Does NOT implement fileutils functions (moved to acquire_fileutils.h).
46
* */
57

68
#ifndef LIBACQUIRE_ACQUIRE_NET_COMMON_H
@@ -10,11 +12,18 @@
1012
extern "C" {
1113
#endif /* __cplusplus */
1214

13-
#include "acquire_download.h"
14-
#include "acquire_string_extras.h"
1515
#include "acquire_common_defs.h"
1616

17-
bool LIBACQUIRE_LIB_EXPORT is_downloaded(const char *url,
17+
/**
18+
* Check if a file represented by URL is already downloaded locally and matches the checksum.
19+
*
20+
* @param url URL or filename
21+
* @param checksum checksum enum type, e.g., LIBACQUIRE_SHA256
22+
* @param hash expected hash string to verify
23+
* @param target_location directory to check in (if NULL, defaults to ".downloads")
24+
* @return true if already downloaded and checksum verifies, else false
25+
*/
26+
extern LIBACQUIRE_LIB_EXPORT bool is_downloaded(const char *url,
1827
enum Checksum checksum,
1928
const char *hash,
2029
const char *target_location);
@@ -26,34 +35,43 @@ bool LIBACQUIRE_LIB_EXPORT is_downloaded(const char *url,
2635
const char *get_download_dir() { return ".downloads"; }
2736
#endif /* !DOWNLOAD_DIR_IMPL */
2837

38+
/**
39+
* Implementation of is_downloaded.
40+
* Uses fileutils functions for existence checks and checksum functions for verification.
41+
*/
2942
bool is_downloaded(const char *url, enum Checksum checksum, const char *hash,
3043
const char *target_location) {
3144
char full_local_fname[NAME_MAX + 1];
3245
const char *filename = is_url(url) ? get_path_from_url(url) : url;
3346

34-
if (target_location == NULL)
47+
if (target_location == NULL) {
3548
target_location = get_download_dir();
49+
}
3650

3751
if (filename == NULL || strlen(filename) == 0 ||
38-
!is_directory(target_location))
52+
!is_directory(target_location)) {
3953
return false;
54+
}
4055

4156
snprintf(full_local_fname, NAME_MAX + 1, "%s" PATH_SEP "%s", target_location,
4257
filename);
4358

44-
if (!is_file(full_local_fname))
59+
if (!is_file(full_local_fname)) {
4560
return false;
61+
}
4662

4763
switch (checksum) {
4864
case LIBACQUIRE_SHA256:
4965
return sha256(full_local_fname, hash);
5066
case LIBACQUIRE_SHA512:
51-
/* return sha512(full_local_fname, hash); */
67+
// TODO: can add sha512 once implemented and enabled
68+
return false;
5269
case LIBACQUIRE_UNSUPPORTED_CHECKSUM:
5370
default:
5471
return false;
5572
}
5673
}
74+
5775
#endif /* LIBACQUIRE_IMPLEMENTATION */
5876

5977
#ifdef __cplusplus

acquire/acquire_string_extras.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
extern "C" {
1111
#endif /* __cplusplus */
1212

13+
#include <ctype.h>
1314
#include <stdarg.h>
1415
#include <stdio.h>
15-
#include <ctype.h>
1616
#ifdef HAVE_LIBBSD
1717
#include <bsd/string.h>
1818
#else
@@ -162,13 +162,15 @@ extern LIBACQUIRE_LIB_EXPORT int strcasecmp(const char *, const char *);
162162
#define strcasecmp _stricmp
163163
#else
164164
/* from MIT licensed musl @ e0ef93c20de1a9e0a6b8f4a4a951a8e61a1a2973 */
165-
int strncasecmp(const char *_l, const char *_r, size_t n)
166-
{
167-
const unsigned char *l=(void *)_l, *r=(void *)_r;
168-
if (!n--) return 0;
169-
for (; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r)); l++, r++, n--);
170-
return tolower(*l) - tolower(*r);
171-
}
165+
int strncasecmp(const char *_l, const char *_r, size_t n) {
166+
const unsigned char *l = (void *)_l, *r = (void *)_r;
167+
if (!n--)
168+
return 0;
169+
for (; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r));
170+
l++, r++, n--)
171+
;
172+
return tolower(*l) - tolower(*r);
173+
}
172174
#endif /* defined(_MSC_VER) && !defined(__INTEL_COMPILER) */
173175

174176
#endif /* LIBACQUIRE_IMPLEMENTATION */

0 commit comments

Comments
 (0)