2016-06-15 23:48:41 +02:00
|
|
|
#include <framework/globalfunctions/DleEncoder.h>
|
|
|
|
|
2020-07-07 16:36:41 +02:00
|
|
|
DleEncoder::DleEncoder() {}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-07-07 16:36:41 +02:00
|
|
|
DleEncoder::~DleEncoder() {}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
ReturnValue_t DleEncoder::decode(const uint8_t *sourceStream,
|
2020-07-07 16:36:41 +02:00
|
|
|
size_t sourceStreamLen, size_t *readLen, uint8_t *destStream,
|
|
|
|
size_t maxDestStreamlen, size_t *decodedLen) {
|
|
|
|
size_t encodedIndex = 0, decodedIndex = 0;
|
2016-06-15 23:48:41 +02:00
|
|
|
uint8_t nextByte;
|
|
|
|
if (*sourceStream != STX) {
|
2020-07-07 16:36:41 +02:00
|
|
|
return DECODING_ERROR;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
++encodedIndex;
|
|
|
|
while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen)
|
|
|
|
&& (sourceStream[encodedIndex] != ETX)
|
|
|
|
&& (sourceStream[encodedIndex] != STX)) {
|
|
|
|
if (sourceStream[encodedIndex] == DLE) {
|
|
|
|
nextByte = sourceStream[encodedIndex + 1];
|
2020-07-07 16:36:41 +02:00
|
|
|
// The next byte is a DLE character that was escaped by another
|
|
|
|
// DLE character, so we can write it to the destination stream.
|
|
|
|
if (nextByte == DLE) {
|
2016-06-15 23:48:41 +02:00
|
|
|
destStream[decodedIndex] = nextByte;
|
|
|
|
} else {
|
2020-07-07 16:36:41 +02:00
|
|
|
// The next byte is a STX, DTX or 0x0D character which
|
|
|
|
// was escaped by a DLE character
|
2016-06-15 23:48:41 +02:00
|
|
|
if ((nextByte == 0x42) || (nextByte == 0x43)
|
|
|
|
|| (nextByte == 0x4D)) {
|
|
|
|
destStream[decodedIndex] = nextByte - 0x40;
|
|
|
|
} else {
|
2020-07-07 16:36:41 +02:00
|
|
|
return DECODING_ERROR;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++encodedIndex;
|
|
|
|
} else {
|
|
|
|
destStream[decodedIndex] = sourceStream[encodedIndex];
|
|
|
|
}
|
|
|
|
++encodedIndex;
|
|
|
|
++decodedIndex;
|
|
|
|
}
|
|
|
|
if (sourceStream[encodedIndex] != ETX) {
|
2020-07-07 16:36:41 +02:00
|
|
|
return DECODING_ERROR;
|
2016-06-15 23:48:41 +02:00
|
|
|
} else {
|
|
|
|
*readLen = ++encodedIndex;
|
|
|
|
*decodedLen = decodedIndex;
|
|
|
|
return RETURN_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
2020-07-07 16:36:41 +02:00
|
|
|
size_t sourceLen, uint8_t* destStream, size_t maxDestLen,
|
|
|
|
size_t* encodedLen, bool addStxEtx) {
|
2016-06-15 23:48:41 +02:00
|
|
|
if (maxDestLen < 2) {
|
2020-07-07 16:36:41 +02:00
|
|
|
return STREAM_TOO_SHORT;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-07-07 16:36:41 +02:00
|
|
|
size_t encodedIndex = 0, sourceIndex = 0;
|
2016-06-15 23:48:41 +02:00
|
|
|
uint8_t nextByte;
|
|
|
|
if (addStxEtx) {
|
|
|
|
destStream[0] = STX;
|
|
|
|
++encodedIndex;
|
|
|
|
}
|
|
|
|
while ((encodedIndex < maxDestLen) && (sourceIndex < sourceLen)) {
|
|
|
|
nextByte = sourceStream[sourceIndex];
|
|
|
|
if ((nextByte == STX) || (nextByte == ETX) || (nextByte == 0x0D)) {
|
|
|
|
if (encodedIndex + 1 >= maxDestLen) {
|
2020-07-07 16:36:41 +02:00
|
|
|
return STREAM_TOO_SHORT;
|
2016-06-15 23:48:41 +02:00
|
|
|
} else {
|
|
|
|
destStream[encodedIndex] = DLE;
|
|
|
|
++encodedIndex;
|
2020-07-07 16:36:41 +02:00
|
|
|
// Escaped byte will be actual byte + 0x40.
|
2016-06-15 23:48:41 +02:00
|
|
|
destStream[encodedIndex] = nextByte + 0x40;
|
|
|
|
}
|
|
|
|
} else if (nextByte == DLE) {
|
|
|
|
if (encodedIndex + 1 >= maxDestLen) {
|
2020-07-07 16:36:41 +02:00
|
|
|
return STREAM_TOO_SHORT;
|
2016-06-15 23:48:41 +02:00
|
|
|
} else {
|
|
|
|
destStream[encodedIndex] = DLE;
|
|
|
|
++encodedIndex;
|
|
|
|
destStream[encodedIndex] = DLE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
destStream[encodedIndex] = nextByte;
|
|
|
|
}
|
|
|
|
++encodedIndex;
|
|
|
|
++sourceIndex;
|
|
|
|
}
|
|
|
|
if ((sourceIndex == sourceLen) && (encodedIndex < maxDestLen)) {
|
|
|
|
if (addStxEtx) {
|
|
|
|
destStream[encodedIndex] = ETX;
|
|
|
|
++encodedIndex;
|
|
|
|
}
|
|
|
|
*encodedLen = encodedIndex;
|
|
|
|
return RETURN_OK;
|
|
|
|
} else {
|
2020-07-07 16:36:41 +02:00
|
|
|
return STREAM_TOO_SHORT;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|