libslip-c

C SLIP-encoding library
git clone https://git.sinitax.com/sinitax/libslip-c
Log | Files | Refs | LICENSE | sfeed.txt

commit 4cb68fb41e7109f1f8c293b5ff08ae818f45cebe
parent e940cc8f0a57f04299bdc1b995981bab4c24f971
Author: Louis Burda <quent.burda@gmail.com>
Date:   Wed, 21 Jun 2023 03:04:54 +0200

Add build.jst, fixup slip_init and add test

Diffstat:
M.gitignore | 1+
Abuild.jst.tmpl | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aconfigure | 3+++
Minclude/slip.h | 4++--
Msrc/slip.c | 28++++++++++++++--------------
Asrc/test.c | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 167 insertions(+), 16 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ build +build.jst compile_commands.json .cache diff --git a/build.jst.tmpl b/build.jst.tmpl @@ -0,0 +1,60 @@ +#default PREFIX /usr/local +#default INCLDIR /include +#default LIBDIR /lib +#default CC gcc + +#ifdef DEBUG +#define OPT_CFLAGS -Og -g +#else +#define OPT_CFLAGS -O2 +#endif + +cflags = -Wunused-function -Wunused-variable -Wconversion -Wformat + -I include -fPIC #{OPT_CFLAGS} #{EXTRA_CFLAGS} + +rule liba + gcc -o $out.tmp.o $in $cflags -r + objcopy --keep-global-symbols=libslip.api $out.tmp.o $out.fixed.o + ar rcs $out $out.fixed.o + rm $out.tmp.o $out.fixed.o + +rule libso + gcc -o $out $in $cflags -shared -Wl,-version-script libslip.lds + +rule cc + gcc -o $out $in $cflags + +rule mkdir + mkdir $out + +target build + mkdir + +target build/libslip.a + liba src/slip.c | include/slip.h build + +target build/libslip.so + libso src/slip.c | include/slip.h build + +target build/test + cc src/test.c build/libslip.a | build + +command clean + rm -rf build + +command cleanall + just clean + +command install + install -m755 build/libslip.a -t "#{DESTDIR}#{PREFIX}#{LIBDIR}" + install -m755 build/libslip.so -t "#{DESTDIR}#{PREFIX}#{LIBDIR}" + install -m644 include/slip.h -t "#{DESTDIR}#{PREFIX}#{INCLDIR}" + +command uninstall + rm -f "#{DESTDIR}#{PREFIX}#{LIBDIR}/libslip.a" + rm -f "#{DESTDIR}#{PREFIX}#{LIBDIR}/libslip.so" + rm -f "#{DESTDIR}#{PREFIX}#{INCLDIR}/slip.h" + +command all + just build/libslip.a build/libslip.so build/test + diff --git a/configure b/configure @@ -0,0 +1,3 @@ +#!/bin/sh + +tmpl "$@" build.jst.tmpl > build.jst diff --git a/include/slip.h b/include/slip.h @@ -12,8 +12,8 @@ struct slip { uint8_t *rx_buf; void (*rx_restart)(struct slip *slip, uint8_t err_byte); - void (*rx_packet)(struct slip *slip, void *user, - uint8_t *data, size_t size); + void (*rx_packet)(struct slip *slip, + uint8_t *data, size_t size, void *user); void *rx_packet_userdata; int (*realloc)(void **buffer, size_t size); diff --git a/src/slip.c b/src/slip.c @@ -37,8 +37,8 @@ slip_decode_inframe(struct slip *slip, uint8_t rx_byte) if (rx_byte == slip->end) { /* end of packet */ - slip->rx_packet(slip, slip->rx_packet_userdata, - slip->rx_buf, slip->rx_index); + slip->rx_packet(slip, slip->rx_buf, slip->rx_index, + slip->rx_packet_userdata); slip->rx_state = SLIPDEC_START_STATE; } else if (rx_byte == slip->start) { /* malformed packet */ @@ -65,16 +65,16 @@ slip_decode_escseq(struct slip *slip, uint8_t rx_byte) int rc; dec = slip->esc_dec[rx_byte]; - if (!slip->esc_active[dec]) { - /* invalid contents, restart */ + if (slip->esc_active[dec]) { + rc = slip_decode_store(slip, dec); + if (rc) return rc; + slip->rx_state = SLIPDEC_IN_FRAME_STATE; + } else { + /* invalid contents */ if (slip->rx_restart) slip->rx_restart(slip, rx_byte); slip->rx_state = SLIPDEC_START_STATE; slip->rx_index = 0; - } else { - rc = slip_decode_store(slip, dec); - if (rc) return rc; - slip->rx_state = SLIPDEC_IN_FRAME_STATE; } return 0; @@ -91,14 +91,14 @@ slip_init(struct slip *slip) slip->rx_buflen = 0; slip->rx_buf = NULL; - if (slip->rx_packet) return -EINVAL; - if (slip->realloc) return -EINVAL; - if (slip->esc != slip->start) return -EINVAL; - if (slip->esc != slip->end) return -EINVAL; + if (!slip->rx_packet) return -EINVAL; + if (!slip->realloc) return -EINVAL; + if (slip->esc == slip->start) return -EINVAL; + if (slip->esc == slip->end) return -EINVAL; /* build decode table */ for (i = 0; i < 256; i++) - slip->esc_dec[slip->esc_enc[i]] = i; + slip->esc_dec[slip->esc_enc[i]] = (uint8_t) i; return 0; } @@ -181,7 +181,7 @@ slip_encode(struct slip *slip, uint8_t* out, size_t *outlen, size_t outcap, if (pos >= end) return -ENOBUFS; - *outlen = pos - out; + *outlen = (size_t) (pos - out); return 0; } diff --git a/src/test.c b/src/test.c @@ -0,0 +1,87 @@ +#include "slip.h" + +#include <string.h> +#include <stdarg.h> +#include <stdio.h> + +void +die(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + fputs("libslip-test: ", stderr); + vfprintf(stderr, fmt, ap); + if (*fmt && fmt[strlen(fmt) - 1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } + va_end(ap); + + exit(1); +} + +int +slip_realloc(void **buf, size_t size) +{ + *buf = realloc(*buf, size); + if (!*buf) die("realloc:"); + + return 0; +} + +void +slip_restart(struct slip *slip, uint8_t b) +{ + printf("RESTART due to '%02x' byte\n", b); +} + +void +slip_rx(struct slip *slip, uint8_t *data, size_t len, void *user) +{ + printf("PACKET %.*s\n", (int) len, data); +} + +int +main(int argc, const char **argv) +{ + uint8_t iobuf[BUFSIZ]; + uint8_t encbuf[BUFSIZ * 2 + 2]; + struct slip slip = { 0 }; + size_t enclen, nread; + int rc; + + slip.realloc = slip_realloc; + slip.rx_packet = slip_rx; + slip.rx_restart = slip_restart; + slip.esc = '\\'; + slip.start = '<'; + slip.end = '>'; + slip.esc_active['\\'] = true; + slip.esc_enc['\\'] = '\\'; + slip.esc_active['<'] = true; + slip.esc_enc['<'] = '<'; + slip.esc_active['>'] = true; + slip.esc_enc['>'] = '>'; + + rc = slip_init(&slip); + if (rc) die("slip_init"); + + if (argc == 2 && !strcmp(argv[1], "-d")) { + while ((nread = fread(iobuf, 1, sizeof(iobuf), stdin))) { + rc = slip_decode(&slip, iobuf, nread); + if (rc) die("slip_decode"); + } + } else { + while ((nread = fread(iobuf, 1, sizeof(iobuf), stdin))) { + rc = slip_encode(&slip, encbuf, &enclen, + sizeof(encbuf), iobuf, nread); + if (rc) die("slip_encode"); + fwrite(encbuf, 1, enclen, stdout); + } + } + + slip_deinit(&slip); +}