-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcobs.c
151 lines (128 loc) · 3.47 KB
/
cobs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "cobs.h"
#define COBS_ISV COBS_INPLACE_SENTINEL_VALUE
typedef unsigned char cobs_byte_t;
cobs_ret_t cobs_encode_inplace(void *buf, unsigned len) {
if (!buf || (len < 2)) {
return COBS_RET_ERR_BAD_ARG;
}
cobs_byte_t *cur = (cobs_byte_t *)buf;
cobs_byte_t const *const end = cur + len - 1;
if ((*cur != COBS_ISV) || (*end != COBS_ISV)) {
return COBS_RET_ERR_BAD_PAYLOAD;
}
#define COBS__ENCODE_INPLACE_PATCH() \
do { \
unsigned const ofs = (unsigned)(cur - patch); \
if (ofs > 255) { return COBS_RET_ERR_BAD_PAYLOAD; } \
*patch = (cobs_byte_t)ofs; \
} while (0)
cobs_byte_t *patch = cur++;
while (cur != end) {
if (*cur == 0) {
COBS__ENCODE_INPLACE_PATCH();
patch = cur;
}
++cur;
}
COBS__ENCODE_INPLACE_PATCH();
#undef COBS__ENCODE_INPLACE_PATCH
*cur = 0;
return COBS_RET_SUCCESS;
}
cobs_ret_t cobs_decode_inplace(void *buf, unsigned len) {
if (!buf || (len < 2)) {
return COBS_RET_ERR_BAD_ARG;
}
cobs_byte_t *cur = (cobs_byte_t *)buf;
cobs_byte_t const *const end = cur + len - 1;
if ((*cur == 0) || (*end != 0)) {
return COBS_RET_ERR_BAD_PAYLOAD;
}
while (*cur) {
unsigned const ofs = *cur;
*cur = 0;
cur += ofs;
if (cur > end) {
return COBS_RET_ERR_BAD_PAYLOAD;
}
}
*(cobs_byte_t *)buf = COBS_ISV;
*cur = COBS_ISV;
return COBS_RET_SUCCESS;
}
cobs_ret_t cobs_encode(void const *dec,
unsigned dec_len,
void *out_enc,
unsigned enc_max,
unsigned *out_enc_len) {
if (!dec || !out_enc || !out_enc_len) {
return COBS_RET_ERR_BAD_ARG;
}
if ((enc_max < 2) || (enc_max < dec_len)) {
return COBS_RET_ERR_BAD_ARG;
}
cobs_byte_t const *src = (cobs_byte_t const *)dec;
cobs_byte_t *dst = (cobs_byte_t *)out_enc;
cobs_byte_t *code_dst = dst++;
cobs_byte_t code = 1;
unsigned enc_len = 1;
while (dec_len--) {
cobs_byte_t const byte = *src;
if (byte) {
if (++enc_len > enc_max) { return COBS_RET_ERR_EXHAUSTED; }
*dst++ = byte;
++code;
}
if ((byte == 0) || (code == 0xFF)) {
*code_dst = code;
code_dst = dst;
code = 1;
if ((byte == 0) || dec_len) {
if (++enc_len > enc_max) { return COBS_RET_ERR_EXHAUSTED; }
++dst;
}
}
++src;
}
*code_dst = code;
if (++enc_len > enc_max) { return COBS_RET_ERR_EXHAUSTED; }
*dst++ = 0;
*out_enc_len = enc_len;
return COBS_RET_SUCCESS;
}
cobs_ret_t cobs_decode(void const *enc,
unsigned enc_len,
void *out_dec,
unsigned dec_max,
unsigned *out_dec_len) {
if (!enc || !out_dec || !out_dec_len) {
return COBS_RET_ERR_BAD_ARG;
}
if ((enc_len < 2) || (dec_max < enc_len)) {
return COBS_RET_ERR_BAD_ARG;
}
cobs_byte_t const *src = (cobs_byte_t const *)enc;
cobs_byte_t const *const end = src + enc_len - 1;
cobs_byte_t *dst = (cobs_byte_t *)out_dec;
unsigned block_len = 0;
unsigned code = 0xFF;
unsigned dec_len = 0;
while (src < end) {
if (block_len) {
if (++dec_len > dec_max) { return COBS_RET_ERR_EXHAUSTED; }
*dst++ = *src++;
} else {
if (code != 0xFF) {
if (++dec_len > dec_max) { return COBS_RET_ERR_EXHAUSTED; }
*dst++ = 0;
}
block_len = code = *src++;
if (code == 0) {
break;
}
}
--block_len;
}
*out_dec_len = dec_len;
return COBS_RET_SUCCESS;
}