cobs 1.2.0__tar.gz → 1.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cobs-1.2.2/MANIFEST.in +2 -0
- cobs-1.2.2/PKG-INFO +312 -0
- {cobs-1.2.0 → cobs-1.2.2}/README.rst +10 -2
- cobs-1.2.2/cobs.egg-info/PKG-INFO +312 -0
- cobs-1.2.2/cobs.egg-info/SOURCES.txt +23 -0
- cobs-1.2.2/pyproject.toml +26 -0
- cobs-1.2.2/setup.cfg +4 -0
- cobs-1.2.2/setup.py +29 -0
- {cobs-1.2.0 → cobs-1.2.2/src/cobs}/_version/__init__.py +1 -1
- {cobs-1.2.0/python3 → cobs-1.2.2/src}/cobs/cobs/__init__.py +13 -0
- {cobs-1.2.0/python3 → cobs-1.2.2/src}/cobs/cobs/_cobs_py.py +5 -4
- {cobs-1.2.0/python3 → cobs-1.2.2/src}/cobs/cobs/test.py +19 -1
- {cobs-1.2.0/python3 → cobs-1.2.2/src}/cobs/cobsr/__init__.py +13 -0
- {cobs-1.2.0/python3 → cobs-1.2.2/src}/cobs/cobsr/_cobsr_py.py +6 -5
- {cobs-1.2.0/python3 → cobs-1.2.2/src}/cobs/cobsr/test.py +19 -0
- cobs-1.2.0/MANIFEST.in +0 -2
- cobs-1.2.0/PKG-INFO +0 -283
- cobs-1.2.0/cobs.egg-info/PKG-INFO +0 -283
- cobs-1.2.0/cobs.egg-info/SOURCES.txt +0 -21
- cobs-1.2.0/setup.cfg +0 -7
- cobs-1.2.0/setup.py +0 -52
- {cobs-1.2.0 → cobs-1.2.2}/LICENSE.txt +0 -0
- {cobs-1.2.0 → cobs-1.2.2}/cobs.egg-info/dependency_links.txt +0 -0
- {cobs-1.2.0 → cobs-1.2.2}/cobs.egg-info/top_level.txt +0 -0
- {cobs-1.2.0/python3 → cobs-1.2.2/src}/cobs/__init__.py +0 -0
- {cobs-1.2.0/python3/src → cobs-1.2.2/src/ext}/_cobs_ext.c +0 -0
- {cobs-1.2.0/python3/src → cobs-1.2.2/src/ext}/_cobsr_ext.c +0 -0
- {cobs-1.2.0 → cobs-1.2.2}/test/test_cobs.py +0 -0
- {cobs-1.2.0 → cobs-1.2.2}/test/test_cobsr.py +0 -0
cobs-1.2.2/MANIFEST.in
ADDED
cobs-1.2.2/PKG-INFO
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cobs
|
|
3
|
+
Version: 1.2.2
|
|
4
|
+
Summary: Consistent Overhead Byte Stuffing (COBS)
|
|
5
|
+
Author-email: Craig McQueen <python@craig.mcqueen.id.au>
|
|
6
|
+
License: ----------------------------------------------------------------------------
|
|
7
|
+
Copyright (c) 2010 Craig McQueen
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in
|
|
17
|
+
all copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
----------------------------------------------------------------------------
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
Project-URL: Homepage, https://github.com/cmcqueen/cobs-python
|
|
30
|
+
Project-URL: Bug Tracker, https://github.com/cmcqueen/cobs-python/issues
|
|
31
|
+
Keywords: byte stuffing
|
|
32
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
33
|
+
Classifier: Intended Audience :: Developers
|
|
34
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
35
|
+
Classifier: Programming Language :: Python :: 3
|
|
36
|
+
Classifier: Topic :: Communications
|
|
37
|
+
Requires-Python: >=3.6
|
|
38
|
+
Description-Content-Type: text/x-rst
|
|
39
|
+
License-File: LICENSE.txt
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
========================================
|
|
43
|
+
Consistent Overhead Byte Stuffing (COBS)
|
|
44
|
+
========================================
|
|
45
|
+
|
|
46
|
+
:Author: Craig McQueen
|
|
47
|
+
:Contact: http://craig.mcqueen.id.au/
|
|
48
|
+
:Copyright: 2010 Craig McQueen
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
Python functions for encoding and decoding COBS.
|
|
52
|
+
|
|
53
|
+
-----
|
|
54
|
+
Intro
|
|
55
|
+
-----
|
|
56
|
+
|
|
57
|
+
The ``cobs`` package is provided, which contains modules containing functions
|
|
58
|
+
for encoding and decoding according to COBS methods.
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
What Is COBS?
|
|
62
|
+
`````````````
|
|
63
|
+
|
|
64
|
+
COBS is a method of encoding a packet of bytes into a form that contains no
|
|
65
|
+
bytes with value zero (0x00). The input packet of bytes can contain bytes
|
|
66
|
+
in the full range of 0x00 to 0xFF. The COBS encoded packet is guaranteed to
|
|
67
|
+
generate packets with bytes only in the range 0x01 to 0xFF. Thus, in a
|
|
68
|
+
communication protocol, packet boundaries can be reliably delimited with 0x00
|
|
69
|
+
bytes.
|
|
70
|
+
|
|
71
|
+
The COBS encoding does have to increase the packet size to achieve this
|
|
72
|
+
encoding. However, compared to other byte-stuffing methods, the packet size
|
|
73
|
+
increase is reasonable and predictable. COBS always adds 1 byte to the
|
|
74
|
+
message length. Additionally, for longer packets of length *n*, it *may* add
|
|
75
|
+
n/254 (rounded down) additional bytes to the encoded packet size.
|
|
76
|
+
|
|
77
|
+
For example, compare to the PPP protocol, which uses 0x7E bytes to delimit
|
|
78
|
+
PPP packets. The PPP protocol uses an "escape" style of byte stuffing,
|
|
79
|
+
replacing all occurences of 0x7E bytes in the packet with 0x7D 0x5E. But that
|
|
80
|
+
byte-stuffing method can potentially double the size of the packet in the
|
|
81
|
+
worst case. COBS uses a different method for byte-stuffing, which has a much
|
|
82
|
+
more reasonable worst-case overhead.
|
|
83
|
+
|
|
84
|
+
For more details about COBS, see the references [#ieeeton]_ [#ppp]_.
|
|
85
|
+
|
|
86
|
+
I have included a variant on COBS, `COBS/R`_, which slightly modifies COBS to
|
|
87
|
+
often avoid the +1 byte overhead of COBS. So in many cases, especially for
|
|
88
|
+
smaller packets, the size of a COBS/R encoded packet is the same size as the
|
|
89
|
+
original packet. See below for more details about `COBS/R`_.
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
References
|
|
93
|
+
``````````
|
|
94
|
+
|
|
95
|
+
.. [#ieeeton] | `Consistent Overhead Byte Stuffing`__
|
|
96
|
+
| Stuart Cheshire and Mary Baker
|
|
97
|
+
| IEEE/ACM Transations on Networking, Vol. 7, No. 2, April 1999
|
|
98
|
+
|
|
99
|
+
.. __:
|
|
100
|
+
.. _Consistent Overhead Byte Stuffing (for IEEE):
|
|
101
|
+
http://www.stuartcheshire.org/papers/COBSforToN.pdf
|
|
102
|
+
|
|
103
|
+
.. [#ppp] | `PPP Consistent Overhead Byte Stuffing (COBS)`_
|
|
104
|
+
| PPP Working Group Internet Draft
|
|
105
|
+
| James Carlson, IronBridge Networks
|
|
106
|
+
| Stuart Cheshire and Mary Baker, Stanford University
|
|
107
|
+
| November 1997
|
|
108
|
+
|
|
109
|
+
.. _PPP Consistent Overhead Byte Stuffing (COBS):
|
|
110
|
+
http://tools.ietf.org/html/draft-ietf-pppext-cobs-00
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
----------------
|
|
114
|
+
Modules Provided
|
|
115
|
+
----------------
|
|
116
|
+
|
|
117
|
+
================== ================== ===============================================================
|
|
118
|
+
Module Short Name Long Name
|
|
119
|
+
================== ================== ===============================================================
|
|
120
|
+
``cobs.cobs`` COBS Consistent Overhead Byte Stuffing (basic method) [#ieeeton]_
|
|
121
|
+
``cobs.cobsr`` `COBS/R`_ `Consistent Overhead Byte Stuffing--Reduced`_
|
|
122
|
+
================== ================== ===============================================================
|
|
123
|
+
|
|
124
|
+
"`Consistent Overhead Byte Stuffing--Reduced`_" (`COBS/R`_) is my own invention,
|
|
125
|
+
a modification of basic COBS encoding, and is described in more detail below.
|
|
126
|
+
|
|
127
|
+
The following are not implemented:
|
|
128
|
+
|
|
129
|
+
================== ======================================================================
|
|
130
|
+
Short Name Long Name
|
|
131
|
+
================== ======================================================================
|
|
132
|
+
COBS/ZPE Consistent Overhead Byte Stuffing--Zero Pair Elimination [#ieeeton]_
|
|
133
|
+
COBS/ZRE Consistent Overhead Byte Stuffing--Zero Run Elimination [#ppp]_
|
|
134
|
+
================== ======================================================================
|
|
135
|
+
|
|
136
|
+
A pure Python implementation and a C extension implementation are provided. If
|
|
137
|
+
the C extension is not available for some reason, the pure Python version will
|
|
138
|
+
be used.
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
-----
|
|
142
|
+
Usage
|
|
143
|
+
-----
|
|
144
|
+
|
|
145
|
+
The modules provide an ``encode`` and a ``decode`` function.
|
|
146
|
+
|
|
147
|
+
The input should be a byte string, not a Unicode string. Basic usage::
|
|
148
|
+
|
|
149
|
+
>>> from cobs import cobs
|
|
150
|
+
>>> encoded = cobs.encode(b'Hello world\x00This is a test')
|
|
151
|
+
>>> encoded
|
|
152
|
+
b'\x0cHello world\x0fThis is a test'
|
|
153
|
+
>>> cobs.decode(encoded)
|
|
154
|
+
b'Hello world\x00This is a test'
|
|
155
|
+
|
|
156
|
+
`COBS/R`_ usage is almost identical::
|
|
157
|
+
|
|
158
|
+
>>> from cobs import cobsr
|
|
159
|
+
>>> encoded = cobsr.encode(b'Hello world\x00This is a test')
|
|
160
|
+
>>> encoded
|
|
161
|
+
b'\x0cHello worldtThis is a tes'
|
|
162
|
+
>>> cobsr.decode(encoded)
|
|
163
|
+
b'Hello world\x00This is a test'
|
|
164
|
+
|
|
165
|
+
Any type that implements the buffer protocol, providing a single block of
|
|
166
|
+
bytes, is also acceptable as input::
|
|
167
|
+
|
|
168
|
+
>>> from cobs import cobs
|
|
169
|
+
>>> encoded = cobs.encode(bytearray(b'Hello world\x00This is a test'))
|
|
170
|
+
>>> encoded
|
|
171
|
+
b'\x0cHello world\x0fThis is a test'
|
|
172
|
+
>>> cobs.decode(encoded)
|
|
173
|
+
b'Hello world\x00This is a test'
|
|
174
|
+
|
|
175
|
+
Note that the ``encode`` function does not add any framing ``0x00`` byte at
|
|
176
|
+
the end (or beginning) of the encoded data. Similarly, the ``decode`` function
|
|
177
|
+
does not strip or split on any framing ``0x00`` bytes, but treats any ``0x00``
|
|
178
|
+
bytes as a data input error. The details of data framing is
|
|
179
|
+
application-specific, so it is the user's application's responsibility to
|
|
180
|
+
implement the framing and deframing that is suitable for the needs of the
|
|
181
|
+
application.
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
-------------------------
|
|
185
|
+
Supported Python Versions
|
|
186
|
+
-------------------------
|
|
187
|
+
|
|
188
|
+
Python >= 3.10 are supported, and have both a C extension and a pure Python
|
|
189
|
+
implementation.
|
|
190
|
+
|
|
191
|
+
Python versions < 3.10 might work, but have not been tested.
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
------------
|
|
195
|
+
Installation
|
|
196
|
+
------------
|
|
197
|
+
|
|
198
|
+
The cobs package is installed using ``distutils``. If you have the tools
|
|
199
|
+
installed to build a Python extension module, run the following command::
|
|
200
|
+
|
|
201
|
+
python setup.py install
|
|
202
|
+
|
|
203
|
+
If you cannot build the C extension, you may install just the pure Python
|
|
204
|
+
implementation, using the following command::
|
|
205
|
+
|
|
206
|
+
python setup.py build_py install --skip-build
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
------------
|
|
210
|
+
Unit Testing
|
|
211
|
+
------------
|
|
212
|
+
|
|
213
|
+
Basic unit testing is in the ``test`` sub-module, e.g. ``cobs.cobs.test``. To run it::
|
|
214
|
+
|
|
215
|
+
python -m cobs.cobs.test
|
|
216
|
+
python -m cobs.cobsr.test
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
-------------
|
|
220
|
+
Documentation
|
|
221
|
+
-------------
|
|
222
|
+
|
|
223
|
+
Documentation is written with Sphinx. Source files are provided in the ``doc``
|
|
224
|
+
directory. It can be built using Sphinx 0.6.5. It uses the ``pngmath`` Sphinx
|
|
225
|
+
extension, which requires Latex and ``dvipng`` to be installed.
|
|
226
|
+
|
|
227
|
+
The documentation is available online at: http://packages.python.org/cobs/
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
-------
|
|
231
|
+
License
|
|
232
|
+
-------
|
|
233
|
+
|
|
234
|
+
The code is released under the MIT license. See LICENSE.txt for details.
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
.. _COBS/R:
|
|
238
|
+
.. _Consistent Overhead Byte Stuffing--Reduced:
|
|
239
|
+
|
|
240
|
+
---------------------------------------------------
|
|
241
|
+
Consistent Overhead Byte Stuffing--Reduced (COBS/R)
|
|
242
|
+
---------------------------------------------------
|
|
243
|
+
|
|
244
|
+
A modification of COBS, which I'm calling "Consistent Overhead Byte
|
|
245
|
+
Stuffing--Reduced" (COBS/R), is provided in the ``cobs.cobsr`` module. Its
|
|
246
|
+
purpose is to save one byte from the encoded form in some cases. Plain COBS
|
|
247
|
+
encoding always has a +1 byte encoding overhead. See the references for
|
|
248
|
+
details [#ieeeton]_. COBS/R can often avoid the +1 byte, which can be a useful
|
|
249
|
+
savings if it is mostly small messages that are being encoded.
|
|
250
|
+
|
|
251
|
+
In plain COBS, the last length code byte in the message has some inherent
|
|
252
|
+
redundancy: if it is greater than the number of remaining bytes, this is
|
|
253
|
+
detected as an error.
|
|
254
|
+
|
|
255
|
+
In COBS/R, instead we opportunistically replace the final length code byte with
|
|
256
|
+
the final data byte, whenever the value of the final data byte is greater than
|
|
257
|
+
or equal to what the final length value would normally be. This variation can be
|
|
258
|
+
unambiguously decoded: the decoder notices that the length code is greater than
|
|
259
|
+
the number of remaining bytes.
|
|
260
|
+
|
|
261
|
+
Examples
|
|
262
|
+
````````
|
|
263
|
+
|
|
264
|
+
The byte values in the examples are in hex.
|
|
265
|
+
|
|
266
|
+
First example:
|
|
267
|
+
|
|
268
|
+
Input:
|
|
269
|
+
|
|
270
|
+
====== ====== ====== ====== ====== ======
|
|
271
|
+
2F A2 00 92 73 02
|
|
272
|
+
====== ====== ====== ====== ====== ======
|
|
273
|
+
|
|
274
|
+
This example is encoded the same in COBS and COBS/R. Encoded (length code bytes
|
|
275
|
+
are bold):
|
|
276
|
+
|
|
277
|
+
====== ====== ====== ====== ====== ====== ======
|
|
278
|
+
**03** 2F A2 **04** 92 73 02
|
|
279
|
+
====== ====== ====== ====== ====== ====== ======
|
|
280
|
+
|
|
281
|
+
Second example:
|
|
282
|
+
|
|
283
|
+
The second example is almost the same, except the final data byte value is
|
|
284
|
+
greater than what the length byte would be.
|
|
285
|
+
|
|
286
|
+
Input:
|
|
287
|
+
|
|
288
|
+
====== ====== ====== ====== ====== ======
|
|
289
|
+
2F A2 00 92 73 26
|
|
290
|
+
====== ====== ====== ====== ====== ======
|
|
291
|
+
|
|
292
|
+
Encoded in plain COBS (length code bytes are bold):
|
|
293
|
+
|
|
294
|
+
====== ====== ====== ====== ====== ====== ======
|
|
295
|
+
**03** 2F A2 **04** 92 73 26
|
|
296
|
+
====== ====== ====== ====== ====== ====== ======
|
|
297
|
+
|
|
298
|
+
Encoded in COBS/R:
|
|
299
|
+
|
|
300
|
+
====== ====== ====== ====== ====== ======
|
|
301
|
+
**03** 2F A2 **26** 92 73
|
|
302
|
+
====== ====== ====== ====== ====== ======
|
|
303
|
+
|
|
304
|
+
Because the last data byte (**26**) is greater than the usual length code
|
|
305
|
+
(**04**), the last data byte can be inserted in place of the length code, and
|
|
306
|
+
removed from the end of the sequence. This avoids the usual +1 byte overhead of
|
|
307
|
+
the COBS encoding.
|
|
308
|
+
|
|
309
|
+
The decoder detects this variation on the encoding simply by detecting that the
|
|
310
|
+
length code is greater than the number of remaining bytes. That situation would
|
|
311
|
+
be a decoding error in regular COBS, but in COBS/R it is used to save one byte
|
|
312
|
+
in the encoded message.
|
|
@@ -131,15 +131,23 @@ bytes, is also acceptable as input::
|
|
|
131
131
|
>>> cobs.decode(encoded)
|
|
132
132
|
b'Hello world\x00This is a test'
|
|
133
133
|
|
|
134
|
+
Note that the ``encode`` function does not add any framing ``0x00`` byte at
|
|
135
|
+
the end (or beginning) of the encoded data. Similarly, the ``decode`` function
|
|
136
|
+
does not strip or split on any framing ``0x00`` bytes, but treats any ``0x00``
|
|
137
|
+
bytes as a data input error. The details of data framing is
|
|
138
|
+
application-specific, so it is the user's application's responsibility to
|
|
139
|
+
implement the framing and deframing that is suitable for the needs of the
|
|
140
|
+
application.
|
|
141
|
+
|
|
134
142
|
|
|
135
143
|
-------------------------
|
|
136
144
|
Supported Python Versions
|
|
137
145
|
-------------------------
|
|
138
146
|
|
|
139
|
-
Python >= 3.
|
|
147
|
+
Python >= 3.10 are supported, and have both a C extension and a pure Python
|
|
140
148
|
implementation.
|
|
141
149
|
|
|
142
|
-
Python versions < 3.
|
|
150
|
+
Python versions < 3.10 might work, but have not been tested.
|
|
143
151
|
|
|
144
152
|
|
|
145
153
|
------------
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cobs
|
|
3
|
+
Version: 1.2.2
|
|
4
|
+
Summary: Consistent Overhead Byte Stuffing (COBS)
|
|
5
|
+
Author-email: Craig McQueen <python@craig.mcqueen.id.au>
|
|
6
|
+
License: ----------------------------------------------------------------------------
|
|
7
|
+
Copyright (c) 2010 Craig McQueen
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in
|
|
17
|
+
all copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
----------------------------------------------------------------------------
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
Project-URL: Homepage, https://github.com/cmcqueen/cobs-python
|
|
30
|
+
Project-URL: Bug Tracker, https://github.com/cmcqueen/cobs-python/issues
|
|
31
|
+
Keywords: byte stuffing
|
|
32
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
33
|
+
Classifier: Intended Audience :: Developers
|
|
34
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
35
|
+
Classifier: Programming Language :: Python :: 3
|
|
36
|
+
Classifier: Topic :: Communications
|
|
37
|
+
Requires-Python: >=3.6
|
|
38
|
+
Description-Content-Type: text/x-rst
|
|
39
|
+
License-File: LICENSE.txt
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
========================================
|
|
43
|
+
Consistent Overhead Byte Stuffing (COBS)
|
|
44
|
+
========================================
|
|
45
|
+
|
|
46
|
+
:Author: Craig McQueen
|
|
47
|
+
:Contact: http://craig.mcqueen.id.au/
|
|
48
|
+
:Copyright: 2010 Craig McQueen
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
Python functions for encoding and decoding COBS.
|
|
52
|
+
|
|
53
|
+
-----
|
|
54
|
+
Intro
|
|
55
|
+
-----
|
|
56
|
+
|
|
57
|
+
The ``cobs`` package is provided, which contains modules containing functions
|
|
58
|
+
for encoding and decoding according to COBS methods.
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
What Is COBS?
|
|
62
|
+
`````````````
|
|
63
|
+
|
|
64
|
+
COBS is a method of encoding a packet of bytes into a form that contains no
|
|
65
|
+
bytes with value zero (0x00). The input packet of bytes can contain bytes
|
|
66
|
+
in the full range of 0x00 to 0xFF. The COBS encoded packet is guaranteed to
|
|
67
|
+
generate packets with bytes only in the range 0x01 to 0xFF. Thus, in a
|
|
68
|
+
communication protocol, packet boundaries can be reliably delimited with 0x00
|
|
69
|
+
bytes.
|
|
70
|
+
|
|
71
|
+
The COBS encoding does have to increase the packet size to achieve this
|
|
72
|
+
encoding. However, compared to other byte-stuffing methods, the packet size
|
|
73
|
+
increase is reasonable and predictable. COBS always adds 1 byte to the
|
|
74
|
+
message length. Additionally, for longer packets of length *n*, it *may* add
|
|
75
|
+
n/254 (rounded down) additional bytes to the encoded packet size.
|
|
76
|
+
|
|
77
|
+
For example, compare to the PPP protocol, which uses 0x7E bytes to delimit
|
|
78
|
+
PPP packets. The PPP protocol uses an "escape" style of byte stuffing,
|
|
79
|
+
replacing all occurences of 0x7E bytes in the packet with 0x7D 0x5E. But that
|
|
80
|
+
byte-stuffing method can potentially double the size of the packet in the
|
|
81
|
+
worst case. COBS uses a different method for byte-stuffing, which has a much
|
|
82
|
+
more reasonable worst-case overhead.
|
|
83
|
+
|
|
84
|
+
For more details about COBS, see the references [#ieeeton]_ [#ppp]_.
|
|
85
|
+
|
|
86
|
+
I have included a variant on COBS, `COBS/R`_, which slightly modifies COBS to
|
|
87
|
+
often avoid the +1 byte overhead of COBS. So in many cases, especially for
|
|
88
|
+
smaller packets, the size of a COBS/R encoded packet is the same size as the
|
|
89
|
+
original packet. See below for more details about `COBS/R`_.
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
References
|
|
93
|
+
``````````
|
|
94
|
+
|
|
95
|
+
.. [#ieeeton] | `Consistent Overhead Byte Stuffing`__
|
|
96
|
+
| Stuart Cheshire and Mary Baker
|
|
97
|
+
| IEEE/ACM Transations on Networking, Vol. 7, No. 2, April 1999
|
|
98
|
+
|
|
99
|
+
.. __:
|
|
100
|
+
.. _Consistent Overhead Byte Stuffing (for IEEE):
|
|
101
|
+
http://www.stuartcheshire.org/papers/COBSforToN.pdf
|
|
102
|
+
|
|
103
|
+
.. [#ppp] | `PPP Consistent Overhead Byte Stuffing (COBS)`_
|
|
104
|
+
| PPP Working Group Internet Draft
|
|
105
|
+
| James Carlson, IronBridge Networks
|
|
106
|
+
| Stuart Cheshire and Mary Baker, Stanford University
|
|
107
|
+
| November 1997
|
|
108
|
+
|
|
109
|
+
.. _PPP Consistent Overhead Byte Stuffing (COBS):
|
|
110
|
+
http://tools.ietf.org/html/draft-ietf-pppext-cobs-00
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
----------------
|
|
114
|
+
Modules Provided
|
|
115
|
+
----------------
|
|
116
|
+
|
|
117
|
+
================== ================== ===============================================================
|
|
118
|
+
Module Short Name Long Name
|
|
119
|
+
================== ================== ===============================================================
|
|
120
|
+
``cobs.cobs`` COBS Consistent Overhead Byte Stuffing (basic method) [#ieeeton]_
|
|
121
|
+
``cobs.cobsr`` `COBS/R`_ `Consistent Overhead Byte Stuffing--Reduced`_
|
|
122
|
+
================== ================== ===============================================================
|
|
123
|
+
|
|
124
|
+
"`Consistent Overhead Byte Stuffing--Reduced`_" (`COBS/R`_) is my own invention,
|
|
125
|
+
a modification of basic COBS encoding, and is described in more detail below.
|
|
126
|
+
|
|
127
|
+
The following are not implemented:
|
|
128
|
+
|
|
129
|
+
================== ======================================================================
|
|
130
|
+
Short Name Long Name
|
|
131
|
+
================== ======================================================================
|
|
132
|
+
COBS/ZPE Consistent Overhead Byte Stuffing--Zero Pair Elimination [#ieeeton]_
|
|
133
|
+
COBS/ZRE Consistent Overhead Byte Stuffing--Zero Run Elimination [#ppp]_
|
|
134
|
+
================== ======================================================================
|
|
135
|
+
|
|
136
|
+
A pure Python implementation and a C extension implementation are provided. If
|
|
137
|
+
the C extension is not available for some reason, the pure Python version will
|
|
138
|
+
be used.
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
-----
|
|
142
|
+
Usage
|
|
143
|
+
-----
|
|
144
|
+
|
|
145
|
+
The modules provide an ``encode`` and a ``decode`` function.
|
|
146
|
+
|
|
147
|
+
The input should be a byte string, not a Unicode string. Basic usage::
|
|
148
|
+
|
|
149
|
+
>>> from cobs import cobs
|
|
150
|
+
>>> encoded = cobs.encode(b'Hello world\x00This is a test')
|
|
151
|
+
>>> encoded
|
|
152
|
+
b'\x0cHello world\x0fThis is a test'
|
|
153
|
+
>>> cobs.decode(encoded)
|
|
154
|
+
b'Hello world\x00This is a test'
|
|
155
|
+
|
|
156
|
+
`COBS/R`_ usage is almost identical::
|
|
157
|
+
|
|
158
|
+
>>> from cobs import cobsr
|
|
159
|
+
>>> encoded = cobsr.encode(b'Hello world\x00This is a test')
|
|
160
|
+
>>> encoded
|
|
161
|
+
b'\x0cHello worldtThis is a tes'
|
|
162
|
+
>>> cobsr.decode(encoded)
|
|
163
|
+
b'Hello world\x00This is a test'
|
|
164
|
+
|
|
165
|
+
Any type that implements the buffer protocol, providing a single block of
|
|
166
|
+
bytes, is also acceptable as input::
|
|
167
|
+
|
|
168
|
+
>>> from cobs import cobs
|
|
169
|
+
>>> encoded = cobs.encode(bytearray(b'Hello world\x00This is a test'))
|
|
170
|
+
>>> encoded
|
|
171
|
+
b'\x0cHello world\x0fThis is a test'
|
|
172
|
+
>>> cobs.decode(encoded)
|
|
173
|
+
b'Hello world\x00This is a test'
|
|
174
|
+
|
|
175
|
+
Note that the ``encode`` function does not add any framing ``0x00`` byte at
|
|
176
|
+
the end (or beginning) of the encoded data. Similarly, the ``decode`` function
|
|
177
|
+
does not strip or split on any framing ``0x00`` bytes, but treats any ``0x00``
|
|
178
|
+
bytes as a data input error. The details of data framing is
|
|
179
|
+
application-specific, so it is the user's application's responsibility to
|
|
180
|
+
implement the framing and deframing that is suitable for the needs of the
|
|
181
|
+
application.
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
-------------------------
|
|
185
|
+
Supported Python Versions
|
|
186
|
+
-------------------------
|
|
187
|
+
|
|
188
|
+
Python >= 3.10 are supported, and have both a C extension and a pure Python
|
|
189
|
+
implementation.
|
|
190
|
+
|
|
191
|
+
Python versions < 3.10 might work, but have not been tested.
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
------------
|
|
195
|
+
Installation
|
|
196
|
+
------------
|
|
197
|
+
|
|
198
|
+
The cobs package is installed using ``distutils``. If you have the tools
|
|
199
|
+
installed to build a Python extension module, run the following command::
|
|
200
|
+
|
|
201
|
+
python setup.py install
|
|
202
|
+
|
|
203
|
+
If you cannot build the C extension, you may install just the pure Python
|
|
204
|
+
implementation, using the following command::
|
|
205
|
+
|
|
206
|
+
python setup.py build_py install --skip-build
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
------------
|
|
210
|
+
Unit Testing
|
|
211
|
+
------------
|
|
212
|
+
|
|
213
|
+
Basic unit testing is in the ``test`` sub-module, e.g. ``cobs.cobs.test``. To run it::
|
|
214
|
+
|
|
215
|
+
python -m cobs.cobs.test
|
|
216
|
+
python -m cobs.cobsr.test
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
-------------
|
|
220
|
+
Documentation
|
|
221
|
+
-------------
|
|
222
|
+
|
|
223
|
+
Documentation is written with Sphinx. Source files are provided in the ``doc``
|
|
224
|
+
directory. It can be built using Sphinx 0.6.5. It uses the ``pngmath`` Sphinx
|
|
225
|
+
extension, which requires Latex and ``dvipng`` to be installed.
|
|
226
|
+
|
|
227
|
+
The documentation is available online at: http://packages.python.org/cobs/
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
-------
|
|
231
|
+
License
|
|
232
|
+
-------
|
|
233
|
+
|
|
234
|
+
The code is released under the MIT license. See LICENSE.txt for details.
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
.. _COBS/R:
|
|
238
|
+
.. _Consistent Overhead Byte Stuffing--Reduced:
|
|
239
|
+
|
|
240
|
+
---------------------------------------------------
|
|
241
|
+
Consistent Overhead Byte Stuffing--Reduced (COBS/R)
|
|
242
|
+
---------------------------------------------------
|
|
243
|
+
|
|
244
|
+
A modification of COBS, which I'm calling "Consistent Overhead Byte
|
|
245
|
+
Stuffing--Reduced" (COBS/R), is provided in the ``cobs.cobsr`` module. Its
|
|
246
|
+
purpose is to save one byte from the encoded form in some cases. Plain COBS
|
|
247
|
+
encoding always has a +1 byte encoding overhead. See the references for
|
|
248
|
+
details [#ieeeton]_. COBS/R can often avoid the +1 byte, which can be a useful
|
|
249
|
+
savings if it is mostly small messages that are being encoded.
|
|
250
|
+
|
|
251
|
+
In plain COBS, the last length code byte in the message has some inherent
|
|
252
|
+
redundancy: if it is greater than the number of remaining bytes, this is
|
|
253
|
+
detected as an error.
|
|
254
|
+
|
|
255
|
+
In COBS/R, instead we opportunistically replace the final length code byte with
|
|
256
|
+
the final data byte, whenever the value of the final data byte is greater than
|
|
257
|
+
or equal to what the final length value would normally be. This variation can be
|
|
258
|
+
unambiguously decoded: the decoder notices that the length code is greater than
|
|
259
|
+
the number of remaining bytes.
|
|
260
|
+
|
|
261
|
+
Examples
|
|
262
|
+
````````
|
|
263
|
+
|
|
264
|
+
The byte values in the examples are in hex.
|
|
265
|
+
|
|
266
|
+
First example:
|
|
267
|
+
|
|
268
|
+
Input:
|
|
269
|
+
|
|
270
|
+
====== ====== ====== ====== ====== ======
|
|
271
|
+
2F A2 00 92 73 02
|
|
272
|
+
====== ====== ====== ====== ====== ======
|
|
273
|
+
|
|
274
|
+
This example is encoded the same in COBS and COBS/R. Encoded (length code bytes
|
|
275
|
+
are bold):
|
|
276
|
+
|
|
277
|
+
====== ====== ====== ====== ====== ====== ======
|
|
278
|
+
**03** 2F A2 **04** 92 73 02
|
|
279
|
+
====== ====== ====== ====== ====== ====== ======
|
|
280
|
+
|
|
281
|
+
Second example:
|
|
282
|
+
|
|
283
|
+
The second example is almost the same, except the final data byte value is
|
|
284
|
+
greater than what the length byte would be.
|
|
285
|
+
|
|
286
|
+
Input:
|
|
287
|
+
|
|
288
|
+
====== ====== ====== ====== ====== ======
|
|
289
|
+
2F A2 00 92 73 26
|
|
290
|
+
====== ====== ====== ====== ====== ======
|
|
291
|
+
|
|
292
|
+
Encoded in plain COBS (length code bytes are bold):
|
|
293
|
+
|
|
294
|
+
====== ====== ====== ====== ====== ====== ======
|
|
295
|
+
**03** 2F A2 **04** 92 73 26
|
|
296
|
+
====== ====== ====== ====== ====== ====== ======
|
|
297
|
+
|
|
298
|
+
Encoded in COBS/R:
|
|
299
|
+
|
|
300
|
+
====== ====== ====== ====== ====== ======
|
|
301
|
+
**03** 2F A2 **26** 92 73
|
|
302
|
+
====== ====== ====== ====== ====== ======
|
|
303
|
+
|
|
304
|
+
Because the last data byte (**26**) is greater than the usual length code
|
|
305
|
+
(**04**), the last data byte can be inserted in place of the length code, and
|
|
306
|
+
removed from the end of the sequence. This avoids the usual +1 byte overhead of
|
|
307
|
+
the COBS encoding.
|
|
308
|
+
|
|
309
|
+
The decoder detects this variation on the encoding simply by detecting that the
|
|
310
|
+
length code is greater than the number of remaining bytes. That situation would
|
|
311
|
+
be a decoding error in regular COBS, but in COBS/R it is used to save one byte
|
|
312
|
+
in the encoded message.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
LICENSE.txt
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.rst
|
|
4
|
+
pyproject.toml
|
|
5
|
+
setup.py
|
|
6
|
+
cobs.egg-info/PKG-INFO
|
|
7
|
+
cobs.egg-info/SOURCES.txt
|
|
8
|
+
cobs.egg-info/dependency_links.txt
|
|
9
|
+
cobs.egg-info/top_level.txt
|
|
10
|
+
src/ext/_cobs_ext.c
|
|
11
|
+
src/ext/_cobsr_ext.c
|
|
12
|
+
src/cobs/__init__.py
|
|
13
|
+
src/cobs/_version/__init__.py
|
|
14
|
+
src/cobs/cobs/__init__.py
|
|
15
|
+
src/cobs/cobs/_cobs_py.py
|
|
16
|
+
src/cobs/cobs/test.py
|
|
17
|
+
src/cobs/cobsr/__init__.py
|
|
18
|
+
src/cobs/cobsr/_cobsr_py.py
|
|
19
|
+
src/cobs/cobsr/test.py
|
|
20
|
+
src/ext/_cobs_ext.c
|
|
21
|
+
src/ext/_cobsr_ext.c
|
|
22
|
+
test/test_cobs.py
|
|
23
|
+
test/test_cobsr.py
|