diff options
author | jaseg <git@jaseg.de> | 2020-11-26 17:42:05 +0100 |
---|---|---|
committer | jaseg <git@jaseg.de> | 2020-11-26 17:42:05 +0100 |
commit | 44548df52f8f5e1dcfdc7b3cfdd7024d9d3bf50a (patch) | |
tree | 9198f87b0f41ef96aa4f51db1595b9156b7955a1 /prototype/fw/src | |
parent | 9b5625f12ae067a1583252d41febef848f055588 (diff) | |
download | ihsm-44548df52f8f5e1dcfdc7b3cfdd7024d9d3bf50a.tar.gz ihsm-44548df52f8f5e1dcfdc7b3cfdd7024d9d3bf50a.tar.bz2 ihsm-44548df52f8f5e1dcfdc7b3cfdd7024d9d3bf50a.zip |
Add COBS encoder
Diffstat (limited to 'prototype/fw/src')
-rw-r--r-- | prototype/fw/src/crc32_test.c | 20 | ||||
-rw-r--r-- | prototype/fw/src/microcobs.c | 99 | ||||
-rw-r--r-- | prototype/fw/src/microcobs.h | 15 |
3 files changed, 114 insertions, 20 deletions
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 <stdio.h> - -#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 <stdint.h> +#include "microcobs.h" +#include <stdio.h> + +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 <stdint.h> +#include <unistd.h> + +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__ */ |