cobs.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*****************************************************************************
  2. *
  3. * cobs.h
  4. *
  5. * Consistent Overhead Byte Stuffing
  6. *
  7. * https://github.com/cmcqueen/cobs-c
  8. *
  9. * The COBS encoding does have to increase the packet size to achieve this encoding. However, compared to other byte-stuffing methods,
  10. * the packet size increase is reasonable and predictable. COBS always adds 1 byte to the message length. Additionally,
  11. * for longer packets of length n, it may add n/254 (rounded down) additional bytes to the encoded packet size.
  12. ****************************************************************************/
  13. #ifndef COBS_H_
  14. #define COBS_H_
  15. /*****************************************************************************
  16. * Includes
  17. ****************************************************************************/
  18. #include "ac780x.h"
  19. /*****************************************************************************
  20. * Defines
  21. ****************************************************************************/
  22. #define COBS_ENCODE_DST_BUF_LEN_MAX(SRC_LEN) ((SRC_LEN) + (((SRC_LEN) + 253u)/254u))
  23. #define COBS_DECODE_DST_BUF_LEN_MAX(SRC_LEN) (((SRC_LEN) == 0) ? 0u : ((SRC_LEN) - 1u))
  24. /*****************************************************************************
  25. * Typedefs
  26. ****************************************************************************/
  27. typedef enum
  28. {
  29. COBS_ENCODE_OK = 0x00,
  30. COBS_ENCODE_NULL_POINTER = 0x01,
  31. COBS_ENCODE_OUT_BUFFER_OVERFLOW = 0x02
  32. } cobs_encode_status;
  33. typedef struct
  34. {
  35. size_t out_len;
  36. cobs_encode_status status;
  37. } cobs_encode_result;
  38. typedef enum
  39. {
  40. COBS_DECODE_OK = 0x00,
  41. COBS_DECODE_NULL_POINTER = 0x01,
  42. COBS_DECODE_OUT_BUFFER_OVERFLOW = 0x02,
  43. COBS_DECODE_ZERO_BYTE_IN_INPUT = 0x04,
  44. COBS_DECODE_INPUT_TOO_SHORT = 0x08
  45. } cobs_decode_status;
  46. typedef struct
  47. {
  48. size_t out_len;
  49. cobs_decode_status status;
  50. } cobs_decode_result;
  51. /*****************************************************************************
  52. * Function prototypes
  53. ****************************************************************************/
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. cobs_encode_result cobs_encode(uint8_t *dst_buf_ptr, size_t dst_buf_len, const uint8_t * src_ptr, size_t src_len);
  58. cobs_decode_result cobs_decode(uint8_t *dst_buf_ptr, size_t dst_buf_len, const uint8_t * src_ptr, size_t src_len);
  59. #ifdef __cplusplus
  60. } /* extern "C" */
  61. #endif
  62. #endif /* COBS_H_ */