forked from cmcqueen/cobs-c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCobs.java
More file actions
217 lines (175 loc) · 6.51 KB
/
Cobs.java
File metadata and controls
217 lines (175 loc) · 6.51 KB
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
public class Cobs {
public static int encodeDstBufMaxLen(int srcLen){
return ((srcLen) + (((srcLen) + 253)/254));
}
public static int decodeDstBufMaxLen(int srcLen){
return (((srcLen) == 0) ? 0 : ((srcLen) - 1));
}
public static enum EncodeStatus {
OK,
NULL_POINTER,
OUT_BUFFER_OVERFLOW;
}
public static class EncodeResult {
int outLen;
EncodeStatus status;
}
public static enum DecodeStatus {
OK,
NULL_POINTER,
OUT_BUFFER_OVERFLOW,
ZERO_BYTE_IN_INPUT,
INPUT_TOO_SHORT;
}
public static class DecodeResult {
int outLen;
DecodeStatus status;
}
public static EncodeResult encode(char[] dst_buf_ptr, int dst_buf_len, char[] src_ptr, int src_len){
EncodeResult result = new EncodeResult();
result.outLen = 0;
result.status = EncodeStatus.OK;
int dst_write_counter = 1;
int dst_code_write_counter = 0;
int dst_buf_end_counter = dst_buf_len;
int src_ptr_counter = 0;
int src_end_counter = src_len;
int search_len = 1;
if(dst_buf_ptr == null || src_ptr == null){
result.status = EncodeStatus.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_counter >= dst_buf_end_counter)
{
result.status = EncodeStatus.OUT_BUFFER_OVERFLOW;
break;
}
int src_byte = (int) src_ptr[src_ptr_counter++];
if (src_byte == 0)
{
/* We found a zero byte */
dst_buf_ptr[dst_code_write_counter] = (char) (search_len & 0xFF);
dst_code_write_counter = dst_write_counter++;
search_len = 1;
if (src_ptr_counter >= src_end_counter)
{
break;
}
}
else
{
/* Copy the non-zero byte to the destination buffer */
dst_buf_ptr[dst_write_counter++] = (char) (src_byte & 0xFF);
search_len++;
if (src_ptr_counter >= src_end_counter)
{
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_buf_ptr[dst_code_write_counter] = (char) (search_len & 0xFF);
dst_code_write_counter = dst_write_counter++;
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.
* Update the pointer to calculate the final output length.
*/
if (dst_code_write_counter >= dst_buf_end_counter)
{
/* We've run out of output buffer to write the code byte. */
result.status = EncodeStatus.OUT_BUFFER_OVERFLOW;
dst_write_counter = dst_buf_end_counter;
}
else
{
/* Write the last code (length) byte. */
dst_buf_ptr[dst_code_write_counter] = (char) (search_len & 0xFF);
}
/* Calculate the output length, from the value of dst_code_write_ptr */
result.outLen = dst_write_counter;
return result;
}
public static DecodeResult decode(char[] dst_buf_ptr, int dst_buf_len, char[] src_ptr, int src_len){
DecodeResult result = new DecodeResult();
result.outLen = 0;
result.status = DecodeStatus.OK;
int src_ptr_counter = 0;
int src_end_counter = src_len;
int dst_buf_end_counter = dst_buf_len;
int dst_write_counter = 0;
int remaining_bytes;
int src_byte;
int i;
int len_code;
/* First, do a NULL pointer check and return immediately if it fails. */
if ((dst_buf_ptr == null) || (src_ptr == null))
{
result.status = DecodeStatus.NULL_POINTER;
return result;
}
if (src_len != 0)
{
for (;;)
{
len_code = (int) src_ptr[src_ptr_counter++];
if (len_code == 0)
{
result.status = DecodeStatus.ZERO_BYTE_IN_INPUT;
break;
}
len_code--;
/* Check length code against remaining input bytes */
remaining_bytes = src_end_counter - src_ptr_counter;
if (len_code > remaining_bytes)
{
result.status = DecodeStatus.INPUT_TOO_SHORT;
len_code = remaining_bytes;
}
/* Check length code against remaining output buffer space */
remaining_bytes = dst_buf_end_counter - dst_write_counter;
if (len_code > remaining_bytes)
{
result.status = DecodeStatus.OUT_BUFFER_OVERFLOW;
len_code = remaining_bytes;
}
for (i = len_code; i != 0; i--)
{
src_byte = (char) (src_ptr[src_ptr_counter++] & 0xFF);
if (src_byte == 0)
{
result.status = DecodeStatus.ZERO_BYTE_IN_INPUT;
}
dst_buf_ptr[dst_write_counter++] = (char) src_byte;
}
if (src_ptr_counter >= src_end_counter)
{
break;
}
/* Add a zero to the end */
if (len_code != 0xFE)
{
if (dst_write_counter >= dst_buf_end_counter)
{
result.status = DecodeStatus.OUT_BUFFER_OVERFLOW;
break;
}
dst_buf_ptr[dst_write_counter++] = 0;
}
}
}
result.outLen = dst_write_counter;
return result;
}
}