Compare commits
6 Commits
Author | SHA1 | Date |
---|---|---|
Alex D. | 530fa5ff22 | |
Alex D. | 91807c2b45 | |
Alex D. | cc742932a2 | |
Alex D. | 94892fa5dd | |
Alex D. | 7a6093f175 | |
Alex D. | b38f9156ce |
28
Makefile
28
Makefile
|
@ -1,7 +1,8 @@
|
|||
include config.mk
|
||||
|
||||
OBJ = ${SRC:.c=.o}
|
||||
all: ${TARGETS}
|
||||
|
||||
all: ${OBJ} ${BIN}
|
||||
|
||||
%.o: %.c
|
||||
${CC} -o $@ -c ${CFLAGS} $<
|
||||
|
@ -9,30 +10,9 @@ all: ${TARGETS}
|
|||
clean:
|
||||
rm -f ${OBJ} ${BIN}
|
||||
|
||||
|
||||
ifneq (${OBJ},)
|
||||
installobj: ${OBJ}
|
||||
mkdir -p "${OBJDEST}"
|
||||
cp -f ${OBJ} "${OBJDEST}"
|
||||
endif
|
||||
|
||||
ifneq (${HDR},)
|
||||
installhdr: ${HDR}
|
||||
mkdir -p "${HDRDEST}"
|
||||
cp -f ${HDR} "${HDRDEST}"
|
||||
endif
|
||||
|
||||
ifneq (${BIN},)
|
||||
|
||||
${BIN}: ${OBJ}
|
||||
${CC} -o "$@" ${LDFLAGS} $?
|
||||
|
||||
installbin: ${BIN}
|
||||
mkdir -p "${BINDEST}"
|
||||
cp -f ${BIN} "${BINDEST}"
|
||||
|
||||
${CC} -o "$@" ${LDFLAGS} $^
|
||||
endif
|
||||
|
||||
install: installhdr installobj installbin
|
||||
|
||||
.PHONY: all clean installhdr installobj installbin install
|
||||
.PHONY: all clean
|
||||
|
|
10
config.mk
10
config.mk
|
@ -1,10 +1,6 @@
|
|||
SRC = baseven.c
|
||||
HDR = baseven.h
|
||||
SRC = src/baseven.c
|
||||
BIN =
|
||||
|
||||
TARGETS = baseven.o
|
||||
|
||||
PREFIX = /usr/local
|
||||
CC = clang
|
||||
DEBUG = 1
|
||||
|
||||
|
@ -22,7 +18,3 @@ CFLAGS += -g
|
|||
else
|
||||
CFLAGS += -O2 -Werror
|
||||
endif
|
||||
|
||||
BINDEST = ${DESTDIR}/${PREFIX}/bin/corelibs
|
||||
OBJDEST = ${DESTDIR}/${PREFIX}/lib/corelibs
|
||||
HDRDEST = ${DESTDIR}/${PREFIX}/include/corelibs
|
||||
|
|
|
@ -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;
|
|
@ -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;
|
Reference in New Issue