taken over dle encoder from upstream master

This commit is contained in:
Robin Müller 2020-08-26 17:43:20 +02:00
parent 2748a8b93b
commit a8f2cf910b
2 changed files with 120 additions and 203 deletions

View File

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

View File

@ -1,79 +1,25 @@
#ifndef FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ #ifndef DLEENCODER_H_
#define FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ #define DLEENCODER_H_
#include "../returnvalues/HasReturnvaluesIF.h" #include "../returnvalues/HasReturnvaluesIF.h"
#include <cstddef>
class DleEncoder: public HasReturnvaluesIF {
/** private:
* @brief This DLE Encoder (Data Link Encoder) can be used to encode and DleEncoder();
* decode arbitrary data with ASCII control characters virtual ~DleEncoder();
* @details
* List of control codes: public:
* https://en.wikipedia.org/wiki/C0_and_C1_control_codes static const uint8_t STX = 0x02;
* static const uint8_t ETX = 0x03;
* This encoder can be used to achieve a basic transport layer when using static const uint8_t DLE = 0x10;
* char based transmission systems.
* The passed source strean is converted into a encoded stream by adding static ReturnValue_t decode(const uint8_t *sourceStream,
* a STX marker at the start of the stream and an ETX marker at the end of uint32_t sourceStreamLen, uint32_t *readLen, uint8_t *destStream,
* the stream. Any STX, ETX, DLE and CR occurences in the source stream are uint32_t maxDestStreamlen, uint32_t *decodedLen);
* escaped by a DLE character. The encoder also replaces escaped control chars
* by another char, so STX, ETX and CR should not appear anywhere in the actual static ReturnValue_t encode(const uint8_t *sourceStream, uint32_t sourceLen,
* encoded data stream. uint8_t *destStream, uint32_t maxDestLen, uint32_t *encodedLen,
* bool addStxEtx = true);
* When using a strictly char based reception of packets enoded with DLE, };
* STX can be used to notify a reader that actual data will start to arrive
* while ETX can be used to notify the reader that the data has ended. #endif /* DLEENCODER_H_ */
*/
class DleEncoder: public HasReturnvaluesIF {
private:
DleEncoder();
virtual ~DleEncoder();
public:
static constexpr uint8_t INTERFACE_ID = CLASS_ID::DLE_ENCODER;
static constexpr ReturnValue_t STREAM_TOO_SHORT = MAKE_RETURN_CODE(0x01);
static constexpr ReturnValue_t DECODING_ERROR = MAKE_RETURN_CODE(0x02);
//! Start Of Text character. First character is encoded stream
static constexpr uint8_t STX_CHAR = 0x02;
//! End Of Text character. Last character in encoded stream
static constexpr uint8_t ETX_CHAR = 0x03;
//! Data Link Escape character. Used to escape STX, ETX and DLE occurences
//! in the source stream.
static constexpr uint8_t DLE_CHAR = 0x10;
static constexpr uint8_t CARRIAGE_RETURN = 0x0D;
/**
* Encodes the give data stream by preceding it with the STX marker
* and ending it with an ETX marker. STX, ETX and DLE characters inside
* the stream are escaped by DLE characters and also replaced by adding
* 0x40 (which is reverted in the decoing process).
* @param sourceStream
* @param sourceLen
* @param destStream
* @param maxDestLen
* @param encodedLen
* @param addStxEtx
* Adding STX and ETX can be omitted, if they are added manually.
* @return
*/
static ReturnValue_t encode(const uint8_t *sourceStream, size_t sourceLen,
uint8_t *destStream, size_t maxDestLen, size_t *encodedLen,
bool addStxEtx = true);
/**
* Converts an encoded stream back.
* @param sourceStream
* @param sourceStreamLen
* @param readLen
* @param destStream
* @param maxDestStreamlen
* @param decodedLen
* @return
*/
static ReturnValue_t decode(const uint8_t *sourceStream,
size_t sourceStreamLen, size_t *readLen, uint8_t *destStream,
size_t maxDestStreamlen, size_t *decodedLen);
};
#endif /* FRAMEWORK_GLOBALFUNCTIONS_DLEENCODER_H_ */