Compare commits

...

7 Commits

5 changed files with 45 additions and 88 deletions

View File

@ -1,76 +1,18 @@
include config.mk
include sources.mk
ifeq (${CC},clang)
CFLAGS =\
-std=c99\
-Weverything\
-Wno-padded\
-Wno-disabled-macro-expansion\
-pedantic
ifeq (${DEBUG},1)
CFLAGS += -g
else
CFLAGS += -O2 -Werror
endif
else ifeq (${CC},gcc)
CFLAGS =\
-std=c99\
-Wall\
-Wextra\
-Wformat-overflow=2\
-Wformat-security\
-Winit-self\
-Wstrict-overflow=2\
-Wstringop-overflow=2\
-Walloc-zero\
-Wduplicated-branches\
-Wduplicated-cond\
-Wtrampolines\
-Wfloat-equal\
-Wshadow\
-Wunsafe-loop-optimizations\
-Wparentheses\
-pedantic
ifeq (${DEBUG},1)
CFLAGS += -g
else
CFLAGS += -O2 -Werror
endif
else ifeq (${CC},tcc)
CFLAGS =\
-Wall
ifeq (${DEBUG},1)
CFLAGS += -g
else
CFLAGS += -Werror
endif
endif
CFLAGS += ${EXTRA_CFLAGS}
OBJ = ${SRC:.c=.o}
all: ${OBJ}
all: ${OBJ} ${BIN}
%.o: %.c
${CC} -c ${CFLAGS} -o $@ $<
${CC} -o $@ -c ${CFLAGS} $<
clean:
rm -f ${OBJ}
rm -f ${OBJ} ${BIN}
install: all
mkdir -p "${DESTDIR}${PREFIX}/lib/corelibs"
cp -f ${OBJ} "${DESTDIR}${PREFIX}/lib/corelibs"
mkdir -p "${DESTDIR}${PREFIX}/include/corelibs"
cp -f ${HDR} "${DESTDIR}${PREFIX}/include/corelibs"
ifneq (${BIN},)
${BIN}: ${OBJ}
${CC} -o "$@" ${LDFLAGS} $^
endif
.PHONY: all clean install
.PHONY: all clean

View File

@ -1,3 +1,20 @@
PREFIX = /usr/local
SRC = src/baseven.c
BIN =
CC = clang
DEBUG = 1
LDFLAGS =\
-static
CFLAGS =\
-std=c99\
-Weverything\
-Wno-padded\
-Wno-disabled-macro-expansion\
-pedantic
ifeq (${DEBUG},1)
CFLAGS += -g
else
CFLAGS += -O2 -Werror
endif

View File

@ -1,2 +0,0 @@
SRC = baseven.c
HDR = baseven.h

View File

@ -42,27 +42,25 @@ static uint8_t corelibs_baseven_bits_nth (uint8_t, uint8_t);
enum { // Avoid collision in error numbers
CORELIBS_BASEVEN_ERR_OK = 0,
CORELIBS_BASEVEN_ERR_UNKOWN,
CORELIBS_BASEVEN_ERR_MEM_ALLOC,
CORELIBS_BASEVEN_ERR_MEM_OVERFLOW,
CORELIBS_BASEVEN_ERR_VAR_NCOMPAT,
CORELIBS_BASEVEN_ERR_VAR_INVAL,
CORELIBS_BASEVEN_ERR_VAR_UNDEF,
};
const struct corelibs_baseven_interface cl_baseven = {
.encode = corelibs_baseven_encode,
.decode = corelibs_baseven_decode,
.err = {
.size = {
.decoded = corelibs_baseven_size_decoded,
.encoded = corelibs_baseven_size_encoded,
},
.err = {
.ok = CORELIBS_BASEVEN_ERR_OK,
.unknown = CORELIBS_BASEVEN_ERR_UNKOWN,
.mem = {
.alloc = CORELIBS_BASEVEN_ERR_MEM_ALLOC,
.overflow = CORELIBS_BASEVEN_ERR_MEM_OVERFLOW,
},
.var = {
.undef = CORELIBS_BASEVEN_ERR_VAR_UNDEF,
.inval = CORELIBS_BASEVEN_ERR_VAR_INVAL,
.ncompat = CORELIBS_BASEVEN_ERR_VAR_NCOMPAT,
.inval = CORELIBS_BASEVEN_ERR_VAR_INVAL,
},
},
};
@ -110,17 +108,17 @@ corelibs_baseven_decode (const uint8_t *src, size_t len, uint8_t *dest, size_t m
if (corelibs_baseven_size_decoded (len) > max) err = CORELIBS_BASEVEN_ERR_MEM_OVERFLOW;
if (!err) {
const uint8_t *const end = src + len;
while (1) {
const uint8_t con = (src + DECODED < end) ? DECODED : (uint8_t) (end - src); // Count of bytes to the extra post-encoding byte
const uint8_t con = ((ENCODED - 1) < len) ? (ENCODED - 1) : (uint8_t) len - 1; // Count of bytes to the extra post-encoding byte as OFFSET (8 -> 7)
for (uint8_t p = 0; p < con; p++) {
dest[p] = (uint8_t) ((corelibs_baseven_bits_nth (src[con], p + 1) << DECODED) // Get n-th bit from last byte and shift it to the beginning
| (src[p] ^ RESERVEDBIT) // Discard reserved bit and merge it with bit from last
dest[p] = (uint8_t) ((corelibs_baseven_bits_nth (src[con], p + 1) << (ENCODED - 1)) // Get n-th bit from last byte and shift it to the beginning
| (src[p] ^ RESERVEDBIT) // Discard reserved bit and merge it with bit from last
);
}
if (src + DECODED < end) {
if ((ENCODED - 1) < len) {
dest += con;
len -= con;
src += ENCODED;
} else {
break;

View File

@ -31,6 +31,11 @@ struct corelibs_baseven_interface {
cl_baseven_err (*const encode) (const uint8_t *src, size_t len, uint8_t *dest, size_t cap);
cl_baseven_err (*const decode) (const uint8_t *src, size_t len, uint8_t *dest, size_t cap);
const struct {
uintmax_t (*const decoded) (uintmax_t enc_size);
uintmax_t (*const encoded) (uintmax_t dec_size);
} size;
// Errors - functions return them on run-time problems
const struct {
@ -38,14 +43,11 @@ struct corelibs_baseven_interface {
unknown; // Unknown error
const struct {
const cl_baseven_err alloc, // Memory allocation failed
overflow; // Not enough memory to run
const cl_baseven_err overflow; // Not enough memory to run
} mem;
const struct {
const cl_baseven_err undef, // Undefined variable
inval, // Invalid variable
ncompat; // Incompatible variable
const cl_baseven_err inval; // Invalid variable
} var;
} err;