diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slip.c | 28 | ||||
| -rw-r--r-- | src/test.c | 87 |
2 files changed, 101 insertions, 14 deletions
@@ -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 new file mode 100644 index 0000000..82da068 --- /dev/null +++ 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); +} |
