-
Notifications
You must be signed in to change notification settings - Fork 17
/
cobsr.c
278 lines (246 loc) · 9.42 KB
/
cobsr.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* cobsr.c
*
* Consistent Overhead Byte Stuffing--Reduced (COBS/R)
*/
#include <stdlib.h>
#include "cobsr.h"
/*****************************************************************************
* Defines
****************************************************************************/
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif
/*****************************************************************************
* Functions
****************************************************************************/
/* COBS/R-encode a string of input bytes, which may save one byte of output.
*
* dst_buf_ptr: The buffer into which the result will be written
* dst_buf_len: Length of the buffer into which the result will be written
* src_ptr: The byte string to be encoded
* src_len Length of the byte string to be encoded
*
* returns: A struct containing the success status of the encoding
* operation and the length of the result (that was written to
* dst_buf_ptr)
*/
cobsr_encode_result cobsr_encode(uint8_t * dst_buf_ptr, size_t dst_buf_len,
const uint8_t * src_ptr, size_t src_len)
{
cobsr_encode_result result = { 0, COBSR_ENCODE_OK };
const uint8_t * src_read_ptr = src_ptr;
const uint8_t * src_end_ptr = src_ptr + src_len;
uint8_t * dst_buf_start_ptr = dst_buf_ptr;
uint8_t * dst_buf_end_ptr = dst_buf_ptr + dst_buf_len;
uint8_t * dst_code_write_ptr = dst_buf_ptr;
uint8_t * dst_write_ptr = dst_code_write_ptr + 1;
uint8_t src_byte = 0;
uint8_t src_byte_next = *src_read_ptr;
uint8_t search_len = 1;
/* First, do a NULL pointer check and return immediately if it fails. */
if ((dst_buf_ptr == NULL) || (src_ptr == NULL))
{
result.status = COBSR_ENCODE_NULL_POINTER;
return result;
}
if (src_len != 0)
{
/* Iterate over the source bytes */
for (;;)
{
/* Check for running out of output buffer space */
if (dst_write_ptr >= dst_buf_end_ptr)
{
result.status |= COBSR_ENCODE_OUT_BUFFER_OVERFLOW;
break;
}
src_byte = src_byte_next;
if ( (src_read_ptr+1) <= src_end_ptr)
{
src_byte_next = *(src_read_ptr++ + 1);
}
if (src_byte == 0)
{
/* We found a zero byte */
*dst_code_write_ptr = search_len;
dst_code_write_ptr = dst_write_ptr++;
search_len = 1;
if (src_read_ptr >= src_end_ptr)
{
break;
}
}
else
{
/* Copy the non-zero byte to the destination buffer */
*dst_write_ptr++ = src_byte;
search_len++;
if (src_read_ptr >= src_end_ptr)
{
break;
}
if (search_len == 0xFF)
{
/* We have a long string of non-zero bytes, so we need
* to write out a length code of 0xFF. */
*dst_code_write_ptr = search_len;
dst_code_write_ptr = dst_write_ptr++;
search_len = 1;
}
}
}
}
/* We've reached the end of the source data (or possibly run out of output buffer)
* Finalise the remaining output. In particular, write the code (length) byte.
*
* For COBS/R, the final code (length) byte is special: if the final data byte is
* greater than or equal to what would normally be the final code (length) byte,
* then replace the final code byte with the final data byte, and remove the final
* data byte from the end of the sequence. This saves one byte in the output.
*
* Update the pointer to calculate the final output length.
*/
if (dst_code_write_ptr >= dst_buf_end_ptr)
{
/* We've run out of output buffer to write the code byte. */
result.status |= COBSR_ENCODE_OUT_BUFFER_OVERFLOW;
dst_write_ptr = dst_buf_end_ptr;
}
else
{
if (src_byte < search_len)
{
/* Encoding same as plain COBS */
*dst_code_write_ptr = search_len;
}
else
{
/* Special COBS/R encoding: length code is final byte,
* and final byte is removed from data sequence. */
*dst_code_write_ptr = src_byte;
dst_write_ptr--;
}
}
/* Calculate the output length, from the value of dst_code_write_ptr */
result.out_len = dst_write_ptr - dst_buf_start_ptr;
return result;
}
/* Decode a COBS/R byte string.
*
* dst_buf_ptr: The buffer into which the result will be written
* dst_buf_len: Length of the buffer into which the result will be written
* src_ptr: The byte string to be decoded
* src_len Length of the byte string to be decoded
*
* returns: A struct containing the success status of the decoding
* operation and the length of the result (that was written to
* dst_buf_ptr)
*/
cobsr_decode_result cobsr_decode(uint8_t * dst_buf_ptr, size_t dst_buf_len,
const uint8_t * src_ptr, size_t src_len)
{
cobsr_decode_result result = { 0, COBSR_DECODE_OK };
const uint8_t * src_read_ptr = src_ptr;
const uint8_t * src_end_ptr = src_ptr + src_len;
uint8_t * dst_buf_start_ptr = dst_buf_ptr;
uint8_t * dst_buf_end_ptr = dst_buf_ptr + dst_buf_len;
uint8_t * dst_write_ptr = dst_buf_ptr;
size_t remaining_input_bytes;
size_t remaining_output_bytes;
size_t num_output_bytes;
uint8_t src_byte;
uint8_t i;
uint8_t len_code;
/* First, do a NULL pointer check and return immediately if it fails. */
if ((dst_buf_ptr == NULL) || (src_ptr == NULL))
{
result.status = COBSR_DECODE_NULL_POINTER;
return result;
}
if (src_len != 0)
{
for (;;)
{
len_code = *src_read_ptr++;
if (len_code == 0)
{
result.status |= COBSR_DECODE_ZERO_BYTE_IN_INPUT;
break;
}
/* Calculate remaining input bytes */
remaining_input_bytes = src_end_ptr - src_read_ptr;
if ((len_code - 1u) < remaining_input_bytes)
{
num_output_bytes = len_code - 1;
/* Check length code against remaining output buffer space */
remaining_output_bytes = dst_buf_end_ptr - dst_write_ptr;
if (num_output_bytes > remaining_output_bytes)
{
result.status |= COBSR_DECODE_OUT_BUFFER_OVERFLOW;
num_output_bytes = remaining_output_bytes;
}
for (i = num_output_bytes; i != 0; i--)
{
src_byte = *src_read_ptr++;
if (src_byte == 0)
{
result.status |= COBSR_DECODE_ZERO_BYTE_IN_INPUT;
}
*dst_write_ptr++ = src_byte;
}
/* Add a zero to the end */
if (len_code != 0xFF)
{
if (dst_write_ptr >= dst_buf_end_ptr)
{
result.status |= COBSR_DECODE_OUT_BUFFER_OVERFLOW;
break;
}
*dst_write_ptr++ = 0;
}
}
else
{
/* We've reached the last length code, so write the remaining
* bytes and then exit the loop. */
num_output_bytes = remaining_input_bytes;
/* Check length code against remaining output buffer space */
remaining_output_bytes = dst_buf_end_ptr - dst_write_ptr;
if (num_output_bytes > remaining_output_bytes)
{
result.status |= COBSR_DECODE_OUT_BUFFER_OVERFLOW;
num_output_bytes = remaining_output_bytes;
}
for (i = num_output_bytes; i != 0; i--)
{
src_byte = *src_read_ptr++;
if (src_byte == 0)
{
result.status |= COBSR_DECODE_ZERO_BYTE_IN_INPUT;
}
*dst_write_ptr++ = src_byte;
}
/* Write final data byte, if applicable for COBS/R encoding. */
if (len_code - 1u > remaining_input_bytes)
{
if (dst_write_ptr >= dst_buf_end_ptr)
{
result.status |= COBSR_DECODE_OUT_BUFFER_OVERFLOW;
}
else
{
*dst_write_ptr++ = len_code;
}
}
/* Exit the loop */
break;
}
}
result.out_len = dst_write_ptr - dst_buf_start_ptr;
}
return result;
}