Skip to content

Commit 459b068

Browse files
committed
Add / fix documentation, change variable names to be clearer, use nullptr rather than 0.
1 parent 49f5f23 commit 459b068

File tree

3 files changed

+206
-105
lines changed

3 files changed

+206
-105
lines changed

src/Encoding/COBS.h

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,22 @@
2323
/// additional byte per 254 bytes of data). For messages smaller than 254 bytes,
2424
/// the overhead is constant.
2525
///
26-
/// (via http://www.jacquesf.com/2011/03/consistent-overhead-byte-stuffing/)
27-
///
2826
/// \sa http://conferences.sigcomm.org/sigcomm/1997/papers/p062.pdf
2927
/// \sa http://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
3028
/// \sa https://github.com/jacquesf/COBS-Consistent-Overhead-Byte-Stuffing
31-
/// \sa http://www.jacquesf.com/2011/03/consistent-overhead-byte-stuffing/
29+
/// \sa http://www.jacquesf.com/2011/03/consistent-overhead-byte-stuffing
3230
class COBS
3331
{
3432
public:
3533
/// \brief Encode a byte buffer with the COBS encoder.
36-
/// \param source The buffer to encode.
37-
/// \param size The size of the buffer to encode.
38-
/// \param destination The target buffer for the encoded bytes.
39-
/// \returns The number of bytes in the encoded buffer.
40-
/// \warning destination must have a minimum capacity of
41-
/// (size + size / 254 + 1).
42-
static size_t encode(const uint8_t* source, size_t size, uint8_t* destination)
34+
/// \param buffer A pointer to the unencoded buffer to encode.
35+
/// \param size The number of bytes in the \p buffer.
36+
/// \param encodedBuffer The buffer for the encoded bytes.
37+
/// \returns The number of bytes written to the \p encodedBuffer.
38+
/// \warning The encodedBuffer must have at least getEncodedBufferSize() allocated.
39+
static size_t encode(const uint8_t* buffer,
40+
size_t size,
41+
uint8_t* encodedBuffer)
4342
{
4443
size_t read_index = 0;
4544
size_t write_index = 1;
@@ -48,51 +47,54 @@ class COBS
4847

4948
while (read_index < size)
5049
{
51-
if (source[read_index] == 0)
50+
if (buffer[read_index] == 0)
5251
{
53-
destination[code_index] = code;
52+
encodedBuffer[code_index] = code;
5453
code = 1;
5554
code_index = write_index++;
5655
read_index++;
5756
}
5857
else
5958
{
60-
destination[write_index++] = source[read_index++];
59+
encodedBuffer[write_index++] = buffer[read_index++];
6160
code++;
6261

6362
if (code == 0xFF)
6463
{
65-
destination[code_index] = code;
64+
encodedBuffer[code_index] = code;
6665
code = 1;
6766
code_index = write_index++;
6867
}
6968
}
7069
}
7170

72-
destination[code_index] = code;
71+
encodedBuffer[code_index] = code;
7372

7473
return write_index;
7574
}
7675

76+
7777
/// \brief Decode a COBS-encoded buffer.
78-
/// \param source The COBS-encoded buffer to decode.
79-
/// \param size The size of the COBS-encoded buffer.
80-
/// \param destination The target buffer for the decoded bytes.
81-
/// \returns The number of bytes in the decoded buffer.
82-
/// \warning destination must have a minimum capacity of size.
83-
static size_t decode(const uint8_t* source, size_t size, uint8_t* destination)
78+
/// \param encodedBuffer A pointer to the \p encodedBuffer to decode.
79+
/// \param size The number of bytes in the \p encodedBuffer.
80+
/// \param decodedBuffer The target buffer for the decoded bytes.
81+
/// \returns The number of bytes written to the \p decodedBuffer.
82+
/// \warning decodedBuffer must have a minimum capacity of size.
83+
static size_t decode(const uint8_t* encodedBuffer,
84+
size_t size,
85+
uint8_t* decodedBuffer)
8486
{
8587
if (size == 0)
8688
return 0;
8789

8890
size_t read_index = 0;
8991
size_t write_index = 0;
90-
uint8_t code;
91-
uint8_t i;
92+
uint8_t code = 0;
93+
uint8_t i = 0;
9294

9395
while (read_index < size)
9496
{
95-
code = source[read_index];
97+
code = encodedBuffer[read_index];
9698

9799
if (read_index + code > size && code != 1)
98100
{
@@ -103,24 +105,24 @@ class COBS
103105

104106
for (i = 1; i < code; i++)
105107
{
106-
destination[write_index++] = source[read_index++];
108+
decodedBuffer[write_index++] = encodedBuffer[read_index++];
107109
}
108110

109111
if (code != 0xFF && read_index != size)
110112
{
111-
destination[write_index++] = '\0';
113+
decodedBuffer[write_index++] = '\0';
112114
}
113115
}
114116

115117
return write_index;
116118
}
117119

118-
/// \brief Get the maximum encoded buffer size needed for a given source size.
119-
/// \param sourceSize The size of the buffer to be encoded.
120+
/// \brief Get the maximum encoded buffer size needed for a given unencoded buffer size.
121+
/// \param unencodedBufferSize The size of the buffer to be encoded.
120122
/// \returns the maximum size of the required encoded buffer.
121-
static size_t getEncodedBufferSize(size_t sourceSize)
123+
static size_t getEncodedBufferSize(size_t unencodedBufferSize)
122124
{
123-
return sourceSize + sourceSize / 254 + 1;
125+
return unencodedBufferSize + unencodedBufferSize / 254 + 1;
124126
}
125127

126128
};

src/Encoding/SLIP.h

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,25 @@
1414

1515
/// \brief A Serial Line IP (SLIP) Encoder.
1616
///
17-
/// Serial Line IP (SLIP) is a packet framing protocol: SLIP defines a
18-
/// sequence of characters that frame IP packets on a serial line and
19-
/// nothing more. It provides no addressing, packet type identification,
20-
/// error detection/correction or compression mechanisms. Because the
21-
/// protocol does so little, though, it is usually very easy to
22-
/// implement.
17+
/// Serial Line IP (SLIP) is a packet framing protocol: SLIP defines a sequence
18+
/// of characters that frame IP packets on a serial line and nothing more. It
19+
/// provides no addressing, packet type identification, error detection,
20+
/// correction or compression mechanisms. Because the protocol does so little
21+
/// its implementation is trivial and fast.
2322
///
2423
/// \sa http://tools.ietf.org/html/rfc1055
2524
class SLIP
2625
{
2726
public:
28-
/// \brief Encode the given buffer.
29-
/// \param buffer A pointer to the buffer to be encoded.
30-
/// \param size The size of the buffer pointed to by \p buffer.
31-
/// \param encoded A pointer to the target encoded buffer. This buffer must have sufficient memory allocated.
32-
/// \returns The number of bytes written to the encoded \p buffer.
33-
/// \warning The encoded buffer must have at least getEncodedBufferSize() allocated.
34-
static size_t encode(const uint8_t* buffer, size_t size, uint8_t* encoded)
27+
/// \brief Encode a byte buffer with the SLIP encoder.
28+
/// \param buffer A pointer to the unencoded buffer to encode.
29+
/// \param size The number of bytes in the \p buffer.
30+
/// \param encodedBuffer The buffer for the encoded bytes.
31+
/// \returns The number of bytes written to the \p encodedBuffer.
32+
/// \warning The encodedBuffer must have at least getEncodedBufferSize() allocated.
33+
static size_t encode(const uint8_t* buffer,
34+
size_t size,
35+
uint8_t* encodedBuffer)
3536
{
3637
if (size == 0)
3738
return 0;
@@ -40,38 +41,40 @@ class SLIP
4041
size_t write_index = 0;
4142

4243
// double-ENDed, flush any data that may have accumulated due to line noise
43-
encoded[write_index++] = END;
44+
encodedBuffer[write_index++] = END;
4445

4546
while (read_index < size)
4647
{
4748
if(buffer[read_index] == END)
4849
{
49-
encoded[write_index++] = ESC;
50-
encoded[write_index++] = ESC_END;
50+
encodedBuffer[write_index++] = ESC;
51+
encodedBuffer[write_index++] = ESC_END;
5152
read_index++;
5253
}
5354
else if(buffer[read_index] == ESC)
5455
{
55-
encoded[write_index++] = ESC;
56-
encoded[write_index++] = ESC_ESC;
56+
encodedBuffer[write_index++] = ESC;
57+
encodedBuffer[write_index++] = ESC_ESC;
5758
read_index++;
5859
}
5960
else
6061
{
61-
encoded[write_index++] = buffer[read_index++];
62+
encodedBuffer[write_index++] = buffer[read_index++];
6263
}
6364
}
6465

6566
return write_index;
6667
}
6768

6869
/// \brief Decode a SLIP-encoded buffer.
69-
/// \param source The SLIP-encoded buffer to decode.
70-
/// \param size The size of the SLIP-encoded buffer.
71-
/// \param destination The target buffer for the decoded bytes.
72-
/// \returns The number of bytes in the decoded buffer.
73-
/// \warning destination must have a minimum capacity of size.
74-
static size_t decode(const uint8_t* buffer, size_t size, uint8_t* decoded)
70+
/// \param encodedBuffer A pointer to the \p encodedBuffer to decode.
71+
/// \param size The number of bytes in the \p encodedBuffer.
72+
/// \param decodedBuffer The target buffer for the decoded bytes.
73+
/// \returns The number of bytes written to the \p decodedBuffer.
74+
/// \warning decodedBuffer must have a minimum capacity of size.
75+
static size_t decode(const uint8_t* encodedBuffer,
76+
size_t size,
77+
uint8_t* decodedBuffer)
7578
{
7679
if (size == 0)
7780
return 0;
@@ -81,21 +84,21 @@ class SLIP
8184

8285
while (read_index < size)
8386
{
84-
if (buffer[read_index] == END)
87+
if (encodedBuffer[read_index] == END)
8588
{
8689
// flush or done
8790
read_index++;
8891
}
89-
else if (buffer[read_index] == ESC)
92+
else if (encodedBuffer[read_index] == ESC)
9093
{
91-
if (buffer[read_index+1] == ESC_END)
94+
if (encodedBuffer[read_index+1] == ESC_END)
9295
{
93-
decoded[write_index++] = END;
96+
decodedBuffer[write_index++] = END;
9497
read_index += 2;
9598
}
96-
else if (buffer[read_index+1] == ESC_ESC)
99+
else if (encodedBuffer[read_index+1] == ESC_ESC)
97100
{
98-
decoded[write_index++] = ESC;
101+
decodedBuffer[write_index++] = ESC;
99102
read_index += 2;
100103
}
101104
else
@@ -105,24 +108,25 @@ class SLIP
105108
}
106109
else
107110
{
108-
decoded[write_index++] = buffer[read_index++];
111+
decodedBuffer[write_index++] = encodedBuffer[read_index++];
109112
}
110113
}
111114

112115
return write_index;
113116
}
114117

115-
/// \brief Get the maximum encoded buffer size needed for a given source size.
118+
/// \brief Get the maximum encoded buffer size needed for a given unencoded buffer size.
116119
///
117-
/// SLIP has a start and a end markers (192 and 219).
118-
/// Marker value is replaced by 2 bytes in the encoded buffer.
119-
/// So in the worst case of sending a buffer with only '192' or '219',
120-
/// the encoded buffer length will be 2 * buffer.size() + 2
121-
/// \param sourceSize The size of the buffer to be encoded.
120+
/// SLIP has a start and a end markers (192 and 219). Marker value is
121+
/// replaced by 2 bytes in the encoded buffer. So in the worst case of
122+
/// sending a buffer with only '192' or '219', the encoded buffer length
123+
/// will be 2 * buffer.size() + 2
124+
///
125+
/// \param unencodedBufferSize The size of the buffer to be encoded.
122126
/// \returns the maximum size of the required encoded buffer.
123-
static size_t getEncodedBufferSize(size_t sourceSize)
127+
static size_t getEncodedBufferSize(size_t unencodedBufferSize)
124128
{
125-
return sourceSize * 2 + 2;
129+
return unencodedBufferSize * 2 + 2;
126130
}
127131

128132
/// \brief Key constants used in the SLIP protocol.

0 commit comments

Comments
 (0)