Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
COBS.cpp@0:736db4334c89, 2015-02-10 (annotated)
- Committer:
- glansberry
- Date:
- Tue Feb 10 02:29:49 2015 +0000
- Revision:
- 0:736db4334c89
- Child:
- 1:2b84a7482df6
COBS encoding, initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| glansberry | 0:736db4334c89 | 1 | /* Consistent Overhead Byte Stuffing (COBS) | 
| glansberry | 0:736db4334c89 | 2 | by Stuart Cheshire and Mary Baker | 
| glansberry | 0:736db4334c89 | 3 | |
| glansberry | 0:736db4334c89 | 4 | reference: http://conferences.sigcomm.org/sigcomm/1997/papers/p062.pdf | 
| glansberry | 0:736db4334c89 | 5 | |
| glansberry | 0:736db4334c89 | 6 | |
| glansberry | 0:736db4334c89 | 7 | COBS is a byte stuffing algorithm that encodes data bytes into a sequence that is easily | 
| glansberry | 0:736db4334c89 | 8 | decoded, but simple to identify when blocks of data start. The idea is elegant. The sequence starts with | 
| glansberry | 0:736db4334c89 | 9 | a zero, followed by the number of bytes until the next zero, in that location is the number of bytes again, etc. | 
| glansberry | 0:736db4334c89 | 10 | To keep a data stream synchronized, all one must do is watch for the zeros, which will never occur except when | 
| glansberry | 0:736db4334c89 | 11 | a new byte sequence is starting. | 
| glansberry | 0:736db4334c89 | 12 | |
| glansberry | 0:736db4334c89 | 13 | */ | 
| glansberry | 0:736db4334c89 | 14 | #include "COBS.h" | 
| glansberry | 0:736db4334c89 | 15 | /* | 
| glansberry | 0:736db4334c89 | 16 | * StuffData byte stuffs “length” bytes of | 
| glansberry | 0:736db4334c89 | 17 | * data at the location pointed to by “ptr”, | 
| glansberry | 0:736db4334c89 | 18 | * writing the output to the location pointed | 
| glansberry | 0:736db4334c89 | 19 | * to by “dst”. | 
| glansberry | 0:736db4334c89 | 20 | */ | 
| glansberry | 0:736db4334c89 | 21 | #define FinishBlock(X) \ | 
| glansberry | 0:736db4334c89 | 22 | (*code_ptr = (X), \ | 
| glansberry | 0:736db4334c89 | 23 | code_ptr = dst++, \ | 
| glansberry | 0:736db4334c89 | 24 | code = 0x01) | 
| glansberry | 0:736db4334c89 | 25 | |
| glansberry | 0:736db4334c89 | 26 | /* Stuff the data into an array */ | 
| glansberry | 0:736db4334c89 | 27 | void COBS::StuffData(unsigned char *ptr, unsigned long length, unsigned char *dst) | 
| glansberry | 0:736db4334c89 | 28 | { | 
| glansberry | 0:736db4334c89 | 29 | const unsigned char *end = ptr + length; | 
| glansberry | 0:736db4334c89 | 30 | unsigned char *code_ptr = dst++; | 
| glansberry | 0:736db4334c89 | 31 | unsigned char code = 0x01; | 
| glansberry | 0:736db4334c89 | 32 | while (ptr < end) | 
| glansberry | 0:736db4334c89 | 33 | { | 
| glansberry | 0:736db4334c89 | 34 | if (*ptr == 0) FinishBlock(code); | 
| glansberry | 0:736db4334c89 | 35 | else | 
| glansberry | 0:736db4334c89 | 36 | { | 
| glansberry | 0:736db4334c89 | 37 | *dst++ = *ptr; | 
| glansberry | 0:736db4334c89 | 38 | code++; | 
| glansberry | 0:736db4334c89 | 39 | if (code == 0xFF) FinishBlock(code); | 
| glansberry | 0:736db4334c89 | 40 | } | 
| glansberry | 0:736db4334c89 | 41 | ptr++; | 
| glansberry | 0:736db4334c89 | 42 | } | 
| glansberry | 0:736db4334c89 | 43 | |
| glansberry | 0:736db4334c89 | 44 | FinishBlock(code); | 
| glansberry | 0:736db4334c89 | 45 | } | 
| glansberry | 0:736db4334c89 | 46 | |
| glansberry | 0:736db4334c89 | 47 | /* | 
| glansberry | 0:736db4334c89 | 48 | * UnStuffData decodes “length” bytes of | 
| glansberry | 0:736db4334c89 | 49 | * data at the location pointed to by “ptr”, | 
| glansberry | 0:736db4334c89 | 50 | * writing the output to the location pointed | 
| glansberry | 0:736db4334c89 | 51 | * to by “dst”. | 
| glansberry | 0:736db4334c89 | 52 | */ | 
| glansberry | 0:736db4334c89 | 53 | void COBS::UnStuffData(unsigned char *ptr, unsigned long length, unsigned char *dst) | 
| glansberry | 0:736db4334c89 | 54 | { | 
| glansberry | 0:736db4334c89 | 55 | const unsigned char *end = ptr + length; | 
| glansberry | 0:736db4334c89 | 56 | while (ptr < end) | 
| glansberry | 0:736db4334c89 | 57 | { | 
| glansberry | 0:736db4334c89 | 58 | int i, code = *ptr++; | 
| glansberry | 0:736db4334c89 | 59 | for (i=1; i<code; i++) *dst++ = *ptr++; | 
| glansberry | 0:736db4334c89 | 60 | if (code < 0xFF) *dst++ = 0; | 
| glansberry | 0:736db4334c89 | 61 | } | 
| glansberry | 0:736db4334c89 | 62 | } |