encoder functions split up
This commit is contained in:
parent
3cec9f5f80
commit
5dcf0e44b6
@ -8,26 +8,47 @@ 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) {
|
size_t minAllowedLen = 0;
|
||||||
|
if(escapeStxEtx) {
|
||||||
|
minAllowedLen = 2;
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
minAllowedLen = 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
if(maxDestLen < minAllowedLen) {
|
||||||
return STREAM_TOO_SHORT;
|
return STREAM_TOO_SHORT;
|
||||||
}
|
}
|
||||||
size_t encodedIndex = 0, sourceIndex = 0;
|
|
||||||
uint8_t nextByte;
|
|
||||||
if (addStxEtx) {
|
if (addStxEtx) {
|
||||||
if(not escapeStxEtx) {
|
if(not escapeStxEtx) {
|
||||||
destStream[0] = DLE_CHAR;
|
destStream[0] = DLE_CHAR;
|
||||||
++encodedIndex;
|
|
||||||
}
|
}
|
||||||
destStream[0] = STX_CHAR;
|
destStream[0] = STX_CHAR;
|
||||||
++encodedIndex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(escapeStxEtx) {
|
||||||
|
return encodeStreamEscaped(sourceStream, sourceLen,
|
||||||
|
destStream, maxDestLen, encodedLen, addStxEtx);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return encodeStreamNonEscaped(sourceStream, sourceLen,
|
||||||
|
destStream, maxDestLen, encodedLen, addStxEtx);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnValue_t DleEncoder::encodeStreamEscaped(const uint8_t *sourceStream, size_t sourceLen,
|
||||||
|
uint8_t *destStream, size_t maxDestLen, size_t *encodedLen,
|
||||||
|
bool addStxEtx) {
|
||||||
|
size_t encodedIndex = 2;
|
||||||
|
size_t sourceIndex = 0;
|
||||||
|
uint8_t nextByte = 0;
|
||||||
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
|
if ((nextByte == STX_CHAR or nextByte == ETX_CHAR) or
|
||||||
(this->escapeCr and nextByte == CARRIAGE_RETURN)) {
|
(this->escapeCr and nextByte == CARRIAGE_RETURN)) {
|
||||||
if(this->escapeStxEtx) {
|
|
||||||
if (encodedIndex + 1 >= maxDestLen) {
|
if (encodedIndex + 1 >= maxDestLen) {
|
||||||
return STREAM_TOO_SHORT;
|
return STREAM_TOO_SHORT;
|
||||||
}
|
}
|
||||||
@ -44,7 +65,6 @@ ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
|||||||
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) {
|
||||||
@ -65,10 +85,45 @@ ReturnValue_t DleEncoder::encode(const uint8_t* sourceStream,
|
|||||||
|
|
||||||
if (sourceIndex == sourceLen and encodedIndex < maxDestLen) {
|
if (sourceIndex == sourceLen and encodedIndex < maxDestLen) {
|
||||||
if (addStxEtx) {
|
if (addStxEtx) {
|
||||||
if(not escapeStxEtx) {
|
destStream[encodedIndex] = ETX_CHAR;
|
||||||
destStream[encodedIndex] = DLE_CHAR;
|
|
||||||
++encodedIndex;
|
++encodedIndex;
|
||||||
}
|
}
|
||||||
|
*encodedLen = encodedIndex;
|
||||||
|
return RETURN_OK;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return STREAM_TOO_SHORT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnValue_t DleEncoder::encodeStreamNonEscaped(const uint8_t *sourceStream, size_t sourceLen,
|
||||||
|
uint8_t *destStream, size_t maxDestLen, size_t *encodedLen,
|
||||||
|
bool addStxEtx) {
|
||||||
|
size_t encodedIndex = 1;
|
||||||
|
size_t sourceIndex = 0;
|
||||||
|
uint8_t nextByte = 0;
|
||||||
|
while (encodedIndex < maxDestLen and sourceIndex < sourceLen) {
|
||||||
|
nextByte = sourceStream[sourceIndex];
|
||||||
|
// DLE characters are simply escaped with DLE.
|
||||||
|
if (nextByte == DLE_CHAR) {
|
||||||
|
if (encodedIndex + 1 >= maxDestLen) {
|
||||||
|
return STREAM_TOO_SHORT;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
destStream[encodedIndex] = DLE_CHAR;
|
||||||
|
++encodedIndex;
|
||||||
|
destStream[encodedIndex] = DLE_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
destStream[encodedIndex] = nextByte;
|
||||||
|
}
|
||||||
|
++encodedIndex;
|
||||||
|
++sourceIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sourceIndex == sourceLen and encodedIndex < maxDestLen) {
|
||||||
|
if (addStxEtx) {
|
||||||
destStream[encodedIndex] = ETX_CHAR;
|
destStream[encodedIndex] = ETX_CHAR;
|
||||||
++encodedIndex;
|
++encodedIndex;
|
||||||
}
|
}
|
||||||
|
@ -89,6 +89,14 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
ReturnValue_t encodeStreamEscaped(const uint8_t *sourceStream, size_t sourceLen,
|
||||||
|
uint8_t *destStream, size_t maxDestLen, size_t *encodedLen,
|
||||||
|
bool addStxEtx = true);
|
||||||
|
|
||||||
|
ReturnValue_t encodeStreamNonEscaped(const uint8_t *sourceStream, size_t sourceLen,
|
||||||
|
uint8_t *destStream, size_t maxDestLen, size_t *encodedLen,
|
||||||
|
bool addStxEtx = true);
|
||||||
|
|
||||||
ReturnValue_t decodeStreamEscaped(const uint8_t *sourceStream, size_t sourceStreamLen,
|
ReturnValue_t decodeStreamEscaped(const uint8_t *sourceStream, size_t sourceStreamLen,
|
||||||
size_t *readLen, uint8_t *destStream, size_t maxDestStreamlen, size_t *decodedLen);
|
size_t *readLen, uint8_t *destStream, size_t maxDestStreamlen, size_t *decodedLen);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user