From 44548df52f8f5e1dcfdc7b3cfdd7024d9d3bf50a Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 26 Nov 2020 17:42:05 +0100 Subject: Add COBS encoder --- prototype/fw/src/crc32_test.c | 20 --------- prototype/fw/src/microcobs.c | 99 +++++++++++++++++++++++++++++++++++++++++++ prototype/fw/src/microcobs.h | 15 +++++++ 3 files changed, 114 insertions(+), 20 deletions(-) delete mode 100644 prototype/fw/src/crc32_test.c create mode 100644 prototype/fw/src/microcobs.c create mode 100644 prototype/fw/src/microcobs.h (limited to 'prototype/fw/src') diff --git a/prototype/fw/src/crc32_test.c b/prototype/fw/src/crc32_test.c deleted file mode 100644 index e23bb37..0000000 --- a/prototype/fw/src/crc32_test.c +++ /dev/null @@ -1,20 +0,0 @@ - -#include - -#include "crc32.h" - -int main(void) { - crc32_t state = crc32_reset(); - - do { - char c; - if (fread(&c, 1, 1, stdin) != 1) - break; - - state = crc32_update(state, (uint8_t)c); - } while(23); - - state = crc32_finalize(state); - printf("%08x\n", state); - return 0; -} diff --git a/prototype/fw/src/microcobs.c b/prototype/fw/src/microcobs.c new file mode 100644 index 0000000..58d5e7b --- /dev/null +++ b/prototype/fw/src/microcobs.c @@ -0,0 +1,99 @@ + +#include +#include "microcobs.h" +#include + +ssize_t cobs_encode_sg(const struct sg_entry input[], uint8_t *output, size_t output_len) +{ + size_t idx_pos = 0; + size_t out_pos = 1; + size_t sg_idx = 0; + + const struct sg_entry *e = input; + fprintf(stderr, "\nsg_entry %016x %zd\n", e->target, e->size); + while (e->size >= 0) { + if (sg_idx == e->size) { + sg_idx = 0; + e++; + fprintf(stderr, "\nsg_entry %016x %zd\n", e->target, e->size); + continue; + } + + if (out_pos >= output_len) + return -1; + + uint8_t inbyte = e->target[sg_idx]; + fprintf(stderr, "%02x ", inbyte); + sg_idx += 1; + + if (out_pos - idx_pos >= 255) { + output[idx_pos] = 255; + idx_pos = out_pos; + out_pos += 1; + + if (out_pos >= output_len) + return -1; + } + + if (inbyte) { + output[out_pos] = inbyte; + out_pos += 1; + } else { + output[idx_pos] = out_pos - idx_pos; + idx_pos = out_pos; + out_pos += 1; + } + } + + if (out_pos >= output_len) + return -1; + output[idx_pos] = out_pos - idx_pos; + output[out_pos] = 0x00; + fprintf(stderr, "\n"); + fprintf(stderr, "Finishing %d %d %d\n", idx_pos, out_pos, out_pos - idx_pos); + + return out_pos + 1; +} + +ssize_t cobs_encode(const uint8_t *input, size_t input_len, uint8_t *output, size_t output_len) +{ + size_t idx_pos = 0; + size_t out_pos = 1; + size_t in_pos = 0; + + while (in_pos < input_len) { + if (out_pos >= output_len) + return -1; + + uint8_t inbyte = input[in_pos]; + fprintf(stderr, "%02x ", inbyte); + in_pos += 1; + + if (out_pos - idx_pos >= 255) { + output[idx_pos] = 255; + idx_pos = out_pos; + out_pos += 1; + + if (out_pos >= output_len) + return -1; + } + + if (inbyte) { + output[out_pos] = inbyte; + out_pos += 1; + } else { + output[idx_pos] = out_pos - idx_pos; + idx_pos = out_pos; + out_pos += 1; + } + } + + if (out_pos >= output_len) + return -1; + output[idx_pos] = out_pos - idx_pos; + output[out_pos] = 0x00; + fprintf(stderr, "\n"); + fprintf(stderr, "Finishing %d %d %d\n", idx_pos, out_pos, out_pos - idx_pos); + + return out_pos + 1; +} diff --git a/prototype/fw/src/microcobs.h b/prototype/fw/src/microcobs.h new file mode 100644 index 0000000..774a27b --- /dev/null +++ b/prototype/fw/src/microcobs.h @@ -0,0 +1,15 @@ +#ifndef __MICROCOBS_H__ +#define __MICROCOBS_H__ + +#include +#include + +struct sg_entry { + uint8_t *target; + ssize_t size; +}; + +ssize_t cobs_encode_sg(const struct sg_entry input[], uint8_t *output, size_t output_len); +ssize_t cobs_encode(const uint8_t *input, size_t input_len, uint8_t *output, size_t output_len); + +#endif /* __MICROCOBS_H__ */ -- cgit