refactoring, code more understandable

This commit is contained in:
Robin Müller 2021-09-30 16:49:30 +02:00
parent f76f462022
commit afb472996c

View File

@ -165,11 +165,9 @@ ReturnValue_t DleEncoder::decodeStreamEscaped(const uint8_t *sourceStream, size_
if (sourceStream[encodedIndex++] != STX_CHAR) { if (sourceStream[encodedIndex++] != STX_CHAR) {
return DECODING_ERROR; return DECODING_ERROR;
} }
while ((encodedIndex < sourceStreamLen - 1) while ((encodedIndex < sourceStreamLen) and (decodedIndex < maxDestStreamlen)) {
and (decodedIndex < maxDestStreamlen) switch(sourceStream[encodedIndex]) {
and (sourceStream[encodedIndex] != ETX_CHAR) case(DLE_CHAR): {
and (sourceStream[encodedIndex] != STX_CHAR)) {
if (sourceStream[encodedIndex] == DLE_CHAR) {
if(encodedIndex + 1 >= sourceStreamLen) { if(encodedIndex + 1 >= sourceStreamLen) {
//reached the end of the sourceStream //reached the end of the sourceStream
*readLen = sourceStreamLen; *readLen = sourceStreamLen;
@ -197,30 +195,34 @@ ReturnValue_t DleEncoder::decodeStreamEscaped(const uint8_t *sourceStream, size_
} }
} }
++encodedIndex; ++encodedIndex;
break;
} }
else { case(STX_CHAR): {
*readLen = ++encodedIndex;
return DECODING_ERROR;
}
case(ETX_CHAR): {
*readLen = ++encodedIndex;
*decodedLen = decodedIndex;
return RETURN_OK;
}
default: {
destStream[decodedIndex] = sourceStream[encodedIndex]; destStream[decodedIndex] = sourceStream[encodedIndex];
break;
}
} }
++encodedIndex; ++encodedIndex;
++decodedIndex; ++decodedIndex;
} }
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 //so far we did not find anything wrong here, so let user try again
*readLen = 0; *readLen = 0;
return STREAM_TOO_SHORT; return STREAM_TOO_SHORT;
} } else {
else { *readLen = encodedIndex;
*readLen = ++encodedIndex;
return DECODING_ERROR; return DECODING_ERROR;
} }
}
else {
*readLen = ++encodedIndex;
*decodedLen = decodedIndex;
return RETURN_OK;
}
} }
ReturnValue_t DleEncoder::decodeStreamNonEscaped(const uint8_t *sourceStream, ReturnValue_t DleEncoder::decodeStreamNonEscaped(const uint8_t *sourceStream,