-
Notifications
You must be signed in to change notification settings - Fork 0
/
cobssi.m
41 lines (35 loc) · 1.37 KB
/
cobssi.m
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
classdef cobssi < handle
%COBSSI Consistent Overhead Byte Stuffing Streaming decoder
% Byte-wise (streaming) COBS decode - see
% http://www.stuartcheshire.org/papers/cobsforton.pdf for algorithm
% details
properties (Access = private)
data = uint8.empty;
code = 0; % used to decide whether to add '0' following EOB
count = 0; % number of values before EOB
end
methods
function obj = push(obj, data)
if obj.count == 0 % read a new stuffing code
obj.code = data;
obj.count = obj.code - 1; % code, n, indicates n-1 values before EOB
else
obj.data(end+1) = data;
obj.count = obj.count - 1; % one less value before EOB
end
if obj.count == 0 && obj.code ~= 255 % 254 = n-1 = maximum block length.
obj.data(end+1) = 0; % no '0' after 254 block length
end
end
function data = peek(obj)
data = obj.data;
end
function data = get(obj)
if obj.count ~= 0 % if count not zero then current message would be invalid
error('Decoded message is invalid or incomplete.');
else
data = obj.data(1:end-1); % remove the trailing zero
end
end
end
end