Skip to content

Commit

Permalink
libzc: fix mingw build
Browse files Browse the repository at this point in the history
  • Loading branch information
mferland committed Dec 7, 2023
1 parent abd8e44 commit 739fcfb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docker/mingw64/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# yazc static executable for Windows 64-bits

FROM ubuntu:22.10
FROM ubuntu:22.04
MAINTAINER Marc Ferland <marc.ferland@gmail.com>

RUN apt update \
Expand Down
39 changes: 37 additions & 2 deletions lib/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,41 @@
#define CD_ENTRY64_LEN 56 /* Zip64 central directory entry length */
#define CD_BUF_LEN (MAX_COM_LEN + EOCD_LEN + EOCD64_LOC_LEN)

/*
* Taken from:
* https://opensource.apple.com/source/Libc/Libc-825.25/string/FreeBSD/memmem.c.auto.html
*/
#ifdef __MINGW64__
void *
memmem(const void *l, size_t l_len, const void *s, size_t s_len)
{
register char *cur, *last;
const char *cl = (const char *)l;
const char *cs = (const char *)s;

/* we need something to compare */
if (l_len == 0 || s_len == 0)
return NULL;

/* "s" must be smaller or equal to "l" */
if (l_len < s_len)
return NULL;

/* special case where s_len == 1 */
if (s_len == 1)
return memchr(l, (int)*cs, l_len);

/* the last position where its possible to find "s" in "l" */
last = (char *)cl + l_len - s_len;

for (cur = (char *)cl; cur <= last; cur++)
if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
return cur;

return NULL;
}
#endif

/**
* SECTION:file
* @short_description: libzc zip file
Expand Down Expand Up @@ -852,7 +887,7 @@ static int read_all_entries_at(struct zc_file *f, off_t cd_offset, uint64_t nben
if (!info)
goto err;

dbg(f->ctx, "Reading entry: %ld\n", i);
dbg(f->ctx, "Reading entry: %"PRIu64"\n", i);

ret = read_single_entry_at(f, cd_offset, &info->header,
&info->extra, &cd_offset);
Expand Down Expand Up @@ -1034,7 +1069,7 @@ static int fill_info_list_central_directory(struct zc_file *f)

/* basic sanity checks */
if (!entries_in_cd || cd_offset > sb.st_size) {
err(f->ctx, "detected invalid zip file: entries_in_cd: %ld, cd_offset: 0x%016jx\n",
err(f->ctx, "detected invalid zip file: entries_in_cd: %"PRIu64", cd_offset: 0x%016jx\n",
entries_in_cd, cd_offset);
to_read = rem;
from++;
Expand Down
12 changes: 9 additions & 3 deletions lib/libzc_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,24 @@
#define LOG_DEBUG 7 /* debug-level messages */
#endif

static inline void __attribute__((always_inline, format(printf, 2, 3)))
#ifdef __MINGW64__
#define __ZC_PRINTF_FORMAT __MINGW_PRINTF_FORMAT
#else
#define __ZC_PRINTF_FORMAT printf
#endif

static inline void __attribute__((always_inline, format(__ZC_PRINTF_FORMAT, 2, 3)))
zc_log_null(struct zc_ctx *ctx __attribute__((__unused__)),
const char *format __attribute__((__unused__)), ...)
{
}

void zc_log(struct zc_ctx *ctx, int priority, const char *file, int line,
const char *fn, const char *format, ...)
__attribute__((format(printf, 6, 7)));
__attribute__((format(__ZC_PRINTF_FORMAT, 6, 7)));

void zc_trace(const char *file, int line, const char *fn, const char *format,
...) __attribute__((format(printf, 4, 5)));
...) __attribute__((format(__ZC_PRINTF_FORMAT, 4, 5)));

#define zc_log_cond(ctx, prio, arg...) \
do { \
Expand Down
4 changes: 2 additions & 2 deletions scripts/build-mingw64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export PTHREAD_CFLAGS="-I${MINGW_LOCAL_PATH}/include"
../configure --host=x86_64-w64-mingw32 \
--enable-static \
--disable-shared \
CFLAGS="-Ofast" \
CPPFLAGS="-D_FILE_OFFSET_BITS=64" \
LDFLAGS="-L${MINGW_LIB_PATH} -lmman ${PTHREAD_LIBS}"
make -j12
make V=1

V=$(../configure -V | grep 'zc configure' | cut -d' ' -f3)
ARCHIVE=yazc-v${V}-win64.zip
Expand Down

0 comments on commit 739fcfb

Please sign in to comment.