Makefile rules + fix some integer comparisons between different sizes.

This commit is contained in:
Philippe Pittoli 2025-12-01 00:52:14 +01:00
parent a992bb8f3f
commit aabe00b58b
3 changed files with 28 additions and 7 deletions

View file

@ -1,6 +1,27 @@
# DESTDIR envvar is a way to install the application in a different root (for building packages for example).
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
SHAREDIR ?= $(PREFIX)/share
DOCDIR ?= $(SHAREDIR)/doc/dnsmanagerd
MANDIR ?= $(SHAREDIR)/man
CFLAGS ?= -Wall -Wextra
all: build all: build
build: install: build
[ -d ./bin ] || mkdir bin $(Q)install -D -m 555 bin/* --target-directory=$(DESTDIR)$(BINDIR)/
cc -o bin/b64-lbl-encode src/b64-lbl-encode.c
cc -o bin/b64-lbl-decode src/b64-lbl-decode.c build: bin bin/b64-lbl-encode bin/b64-lbl-decode
bin:
[ -d bin ] || mkdir bin
bin/b64-lbl-encode: src/b64-lbl-encode.c
cc -o bin/b64-lbl-encode src/b64-lbl-encode.c $(CFLAGS)
bin/b64-lbl-decode: src/b64-lbl-decode.c
cc -o bin/b64-lbl-decode src/b64-lbl-decode.c $(CFLAGS)
clean:
rm bin/*

View file

@ -10,7 +10,7 @@ static const char *base64_chars =
"0123456789+/"; "0123456789+/";
int base64_decode(const char *input, unsigned char *output, size_t *output_length) { int base64_decode(const char *input, unsigned char *output, size_t *output_length) {
int input_length = strlen(input); size_t input_length = strlen(input);
if (input_length % 4 != 0) return -1; // Base64 input must be a multiple of 4. if (input_length % 4 != 0) return -1; // Base64 input must be a multiple of 4.
// Calculate output length // Calculate output length
@ -18,7 +18,7 @@ int base64_decode(const char *input, unsigned char *output, size_t *output_lengt
if (input[input_length - 1] == '=') (*output_length)--; if (input[input_length - 1] == '=') (*output_length)--;
if (input[input_length - 2] == '=') (*output_length)--; if (input[input_length - 2] == '=') (*output_length)--;
for (int i = 0, j = 0; i < input_length; ) { for (size_t i = 0, j = 0; i < input_length; ) {
unsigned int a = input[i] == '=' ? 0 : strchr(base64_chars, input[i]) - base64_chars; i++; unsigned int a = input[i] == '=' ? 0 : strchr(base64_chars, input[i]) - base64_chars; i++;
unsigned int b = input[i] == '=' ? 0 : strchr(base64_chars, input[i]) - base64_chars; i++; unsigned int b = input[i] == '=' ? 0 : strchr(base64_chars, input[i]) - base64_chars; i++;
unsigned int c = input[i] == '=' ? 0 : strchr(base64_chars, input[i]) - base64_chars; i++; unsigned int c = input[i] == '=' ? 0 : strchr(base64_chars, input[i]) - base64_chars; i++;

View file

@ -10,7 +10,7 @@ static const char *base64_chars =
"0123456789+/"; "0123456789+/";
void base64_encode(const unsigned char *input, size_t input_length, char *output, size_t *output_length) { void base64_encode(const unsigned char *input, size_t input_length, char *output, size_t *output_length) {
int i, j; size_t i, j;
*output_length = 4 * ((input_length + 2) / 3); // Calculate output size *output_length = 4 * ((input_length + 2) / 3); // Calculate output size
for (i = 0, j = 0; i < input_length;) { for (i = 0, j = 0; i < input_length;) {