added setting of readLen according to review

This commit is contained in:
Ulrich Mohr 2021-09-13 10:38:36 +02:00
parent dae27a8e10
commit abacfbf2d5

View File

@ -154,6 +154,11 @@ ReturnValue_t DleEncoder::decodeStreamEscaped(const uint8_t *sourceStream, size_
size_t encodedIndex = 0; size_t encodedIndex = 0;
size_t decodedIndex = 0; size_t decodedIndex = 0;
uint8_t nextByte; uint8_t nextByte;
//init to 0 so that we can just return in the first checks (which do not consume anything from
//the source stream)
*readLen = 0;
if(maxDestStreamlen < 1) { if(maxDestStreamlen < 1) {
return STREAM_TOO_SHORT; return STREAM_TOO_SHORT;
} }
@ -166,6 +171,8 @@ ReturnValue_t DleEncoder::decodeStreamEscaped(const uint8_t *sourceStream, size_
and (sourceStream[encodedIndex] != STX_CHAR)) { and (sourceStream[encodedIndex] != STX_CHAR)) {
if (sourceStream[encodedIndex] == DLE_CHAR) { if (sourceStream[encodedIndex] == DLE_CHAR) {
if(encodedIndex + 1 >= sourceStreamLen) { if(encodedIndex + 1 >= sourceStreamLen) {
//reached the end of the sourceStream
*readLen = sourceStreamLen;
return DECODING_ERROR; return DECODING_ERROR;
} }
nextByte = sourceStream[encodedIndex + 1]; nextByte = sourceStream[encodedIndex + 1];
@ -200,6 +207,8 @@ ReturnValue_t DleEncoder::decodeStreamEscaped(const uint8_t *sourceStream, size_
} }
if (sourceStream[encodedIndex] != ETX_CHAR) { if (sourceStream[encodedIndex] != ETX_CHAR) {
if(decodedIndex == maxDestStreamlen) { if(decodedIndex == maxDestStreamlen) {
//so far we did not find anything wrong here, so let user try again
*readLen = 0;
return STREAM_TOO_SHORT; return STREAM_TOO_SHORT;
} }
else { else {
@ -220,6 +229,11 @@ ReturnValue_t DleEncoder::decodeStreamNonEscaped(const uint8_t *sourceStream,
size_t encodedIndex = 0; size_t encodedIndex = 0;
size_t decodedIndex = 0; size_t decodedIndex = 0;
uint8_t nextByte; uint8_t nextByte;
//init to 0 so that we can just return in the first checks (which do not consume anything from
//the source stream)
*readLen = 0;
if(maxDestStreamlen < 2) { if(maxDestStreamlen < 2) {
return STREAM_TOO_SHORT; return STREAM_TOO_SHORT;
} }
@ -227,6 +241,7 @@ ReturnValue_t DleEncoder::decodeStreamNonEscaped(const uint8_t *sourceStream,
return DECODING_ERROR; return DECODING_ERROR;
} }
if (sourceStream[encodedIndex++] != STX_CHAR) { if (sourceStream[encodedIndex++] != STX_CHAR) {
*readLen = 1;
return DECODING_ERROR; return DECODING_ERROR;
} }
while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen)) { while ((encodedIndex < sourceStreamLen) && (decodedIndex < maxDestStreamlen)) {
@ -265,11 +280,13 @@ ReturnValue_t DleEncoder::decodeStreamNonEscaped(const uint8_t *sourceStream,
++encodedIndex; ++encodedIndex;
++decodedIndex; ++decodedIndex;
} }
*readLen = encodedIndex;
if(decodedIndex == maxDestStreamlen) { if(decodedIndex == maxDestStreamlen) {
//so far we did not find anything wrong here, so let user try again
*readLen = 0;
return STREAM_TOO_SHORT; return STREAM_TOO_SHORT;
} } else {
else { *readLen = encodedIndex;
return DECODING_ERROR; return DECODING_ERROR;
} }
} }