Update 'globalfunctions/DleEncoder.cpp'
Just a missing char
This commit is contained in:
parent
281f13e27b
commit
76f145ddcf
@ -1,124 +1,124 @@
|
|||||||
#include "../globalfunctions/DleEncoder.h"
|
#include "../globalfunctions/DleEncoder.h"
|
||||||
|
|
||||||
DleEncoder::DleEncoder() {}
|
DleEncoder::DleEncoder() {}
|
||||||
|
|
||||||
DleEncoder::~DleEncoder() {}
|
DleEncoder::~DleEncoder() {}
|
||||||
|
|
||||||
ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
||||||
size_t sourceLen, uint8_t* destStream, size_t maxDestLen,
|
size_t sourceLen, uint8_t* destStream, size_t maxDestLen,
|
||||||
size_t* encodedLen, bool addStxEtx) {
|
size_t* encodedLen, bool addStxEtx) {
|
||||||
if (maxDestLen < 2) {
|
if (maxDestLen < 2) {
|
||||||
return STREAM_TOO_SHORT;
|
return STREAM_TOO_SHORT;
|
||||||
}
|
}
|
||||||
size_t encodedIndex = 0, sourceIndex = 0;
|
size_t encodedIndex = 0, sourceIndex = 0;
|
||||||
uint8_t nextByte;
|
uint8_t nextByte;
|
||||||
if (addStxEtx) {
|
if (addStxEtx) {
|
||||||
destStream[0] = STX_CHAR;
|
destStream[0] = STX_CHAR;
|
||||||
++encodedIndex;
|
++encodedIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (encodedIndex < maxDestLen and sourceIndex < sourceLen)
|
while (encodedIndex < maxDestLen and sourceIndex < sourceLen)
|
||||||
{
|
{
|
||||||
nextByte = sourceStream[sourceIndex];
|
nextByte = sourceStream[sourceIndex];
|
||||||
// STX, ETX and CR characters in the stream need to be escaped with DLE
|
// STX, ETX and CR characters in the stream need to be escaped with DLE
|
||||||
if (nextByte == STX_CHAR or nextByte == ETX_CHAR or nextByte == CARRIAGE_RETURN) {
|
if (nextByte == STX_CHAR or nextByte == ETX_CHAR or nextByte == CARRIAGE_RETURN) {
|
||||||
if (encodedIndex + 1 >= maxDestLen) {
|
if (encodedIndex + 1 >= maxDestLen) {
|
||||||
return STREAM_TOO_SHORT;
|
return STREAM_TOO_SHORT;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
destStream[encodedIndex] = DLE_CHAR;
|
destStream[encodedIndex] = DLE_CHAR;
|
||||||
++encodedIndex;
|
++encodedIndex;
|
||||||
/* Escaped byte will be actual byte + 0x40. This prevents
|
/* Escaped byte will be actual byte + 0x40. This prevents
|
||||||
* STX, ETX, and carriage return characters from appearing
|
* STX, ETX, and carriage return characters from appearing
|
||||||
* in the encoded data stream at all, so when polling an
|
* in the encoded data stream at all, so when polling an
|
||||||
* encoded stream, the transmission can be stopped at ETX.
|
* encoded stream, the transmission can be stopped at ETX.
|
||||||
* 0x40 was chosen at random with special requirements:
|
* 0x40 was chosen at random with special requirements:
|
||||||
* - Prevent going from one control char to another
|
* - Prevent going from one control char to another
|
||||||
* - Prevent overflow for common characters */
|
* - Prevent overflow for common characters */
|
||||||
destStream[encodedIndex] = nextByte + 0x40;
|
destStream[encodedIndex] = nextByte + 0x40;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// DLE characters are simply escaped with DLE.
|
// DLE characters are simply escaped with DLE.
|
||||||
else if (nextByte == DLE_CHAR) {
|
else if (nextByte == DLE_CHAR) {
|
||||||
if (encodedIndex + 1 >= maxDestLen) {
|
if (encodedIndex + 1 >= maxDestLen) {
|
||||||
return STREAM_TOO_SHORT;
|
return STREAM_TOO_SHORT;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
destStream[encodedIndex] = DLE_CHAR;
|
destStream[encodedIndex] = DLE_CHAR;
|
||||||
++encodedIndex;
|
++encodedIndex;
|
||||||
destStream[encodedIndex] = DLE_CHAR;
|
destStream[encodedIndex] = DLE_CHAR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
destStream[encodedIndex] = nextByte;
|
destStream[encodedIndex] = nextByte;
|
||||||
}
|
}
|
||||||
++encodedIndex;
|
++encodedIndex;
|
||||||
++sourceIndex;
|
++sourceIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sourceIndex == sourceLen and encodedIndex < maxDestLen) {
|
if (sourceIndex == sourceLen and encodedIndex < maxDestLen) {
|
||||||
if (addStxEtx) {
|
if (addStxEtx) {
|
||||||
destStream[encodedIndex] = ETX_CHAR;
|
destStream[encodedIndex] = ETX_CHAR;
|
||||||
++encodedIndex;
|
++encodedIndex;
|
||||||
}
|
}
|
||||||
*encodedLen = encodedIndex;
|
*encodedLen = encodedIndex;
|
||||||
return RETURN_OK;
|
return RETURN_OK;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return STREAM_TOO_SHORT;
|
return STREAM_TOO_SHORT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream,
|
ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream,
|
||||||
size_t sourceStreamLen, size_t *readLen, uint8_t *destStream,
|
size_t sourceStreamLen, size_t *readLen, uint8_t *destStream,
|
||||||
size_t maxDestStreamlen, size_t *decodedLen) {
|
size_t maxDestStreamlen, size_t *decodedLen) {
|
||||||
size_t encodedIndex = 0, decodedIndex = 0;
|
size_t encodedIndex = 0, decodedIndex = 0;
|
||||||
uint8_t nextByte;
|
uint8_t nextByte;
|
||||||
if (*sourceStream != STX_CHAR) {
|
if (*sourceStream != STX_CHAR) {
|
||||||
return DECODING_ERROR;
|
return DECODING_ERROR;
|
||||||
}
|
}
|
||||||
++encodedIndex;
|
++encodedIndex;
|
||||||
|
|
||||||
while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen)
|
while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen)
|
||||||
&& (sourceStream[encodedIndex] != ETX_CHAR)
|
&& (sourceStream[encodedIndex] != ETX_CHAR)
|
||||||
&& (sourceStream[encodedIndex] != STX_CHAR)) {
|
&& (sourceStream[encodedIndex] != STX_CHAR)) {
|
||||||
if (sourceStream[encodedIndex] == DLE_CHAR) {
|
if (sourceStream[encodedIndex] == DLE_CHAR) {
|
||||||
nextByte = sourceStream[encodedIndex + 1];
|
nextByte = sourceStream[encodedIndex + 1];
|
||||||
// The next byte is a DLE character that was escaped by another
|
// The next byte is a DLE character that was escaped by another
|
||||||
// DLE character, so we can write it to the destination stream.
|
// DLE character, so we can write it to the destination stream.
|
||||||
if (nextByte == DLE_CHAR) {
|
if (nextByte == DLE_CHAR) {
|
||||||
destStream[decodedIndex] = nextByte;
|
destStream[decodedIndex] = nextByte;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* The next byte is a STX, DTX or 0x0D character which
|
/* The next byte is a STX, DTX or 0x0D character which
|
||||||
* was escaped by a DLE character. The actual byte was
|
* was escaped by a DLE character. The actual byte was
|
||||||
* also encoded by adding + 0x40 to preven having control chars,
|
* also encoded by adding + 0x40 to prevent having control chars,
|
||||||
* in the stream at all, so we convert it back. */
|
* in the stream at all, so we convert it back. */
|
||||||
if (nextByte == 0x42 or nextByte == 0x43 or nextByte == 0x4D) {
|
if (nextByte == 0x42 or nextByte == 0x43 or nextByte == 0x4D) {
|
||||||
destStream[decodedIndex] = nextByte - 0x40;
|
destStream[decodedIndex] = nextByte - 0x40;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return DECODING_ERROR;
|
return DECODING_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
++encodedIndex;
|
++encodedIndex;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
destStream[decodedIndex] = sourceStream[encodedIndex];
|
destStream[decodedIndex] = sourceStream[encodedIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
++encodedIndex;
|
++encodedIndex;
|
||||||
++decodedIndex;
|
++decodedIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sourceStream[encodedIndex] != ETX_CHAR) {
|
if (sourceStream[encodedIndex] != ETX_CHAR) {
|
||||||
*readLen = ++encodedIndex;
|
*readLen = ++encodedIndex;
|
||||||
return DECODING_ERROR;
|
return DECODING_ERROR;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
*readLen = ++encodedIndex;
|
*readLen = ++encodedIndex;
|
||||||
*decodedLen = decodedIndex;
|
*decodedLen = decodedIndex;
|
||||||
return RETURN_OK;
|
return RETURN_OK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user