-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add common folder for packet decomutation
- Loading branch information
1 parent
351b86a
commit e6b9de2
Showing
29 changed files
with
689 additions
and
601 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.vscode | ||
|
||
.codegpt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include "CobsEncoder.hpp" | ||
#include <cstdint> | ||
|
||
namespace Device | ||
{ | ||
std::size_t CobsEncoder::encode(const std::uint8_t *input, | ||
std::size_t length, | ||
std::uint8_t *output, | ||
std::size_t maxOutputLength) | ||
{ | ||
if (input == nullptr || output == nullptr || length == 0 || maxOutputLength < length + 1) | ||
{ | ||
return 0; // Invalid input or insufficient output buffer size | ||
} | ||
|
||
std::size_t writeIndex = 0; // Position to write in the output | ||
std::size_t readIndex = 0; // Position to read in the input | ||
std::size_t codeIndex = 0; // Position of the code byte in the output | ||
std::uint8_t code = 1; // Initialize the code byte | ||
|
||
output[writeIndex++] = 0; // Placeholder for the code byte | ||
|
||
while (readIndex < length) | ||
{ | ||
if (input[readIndex] == 0) | ||
{ | ||
// Write the code byte to its position | ||
output[codeIndex] = code; | ||
|
||
// Update the code byte's position and reset code | ||
codeIndex = writeIndex++; | ||
code = 1; | ||
} | ||
else | ||
{ | ||
// Copy non-zero byte to the output | ||
output[writeIndex++] = input[readIndex]; | ||
code++; | ||
|
||
// If code reaches the max value (255), write it and reset | ||
if (code == 0xFF) | ||
{ | ||
output[codeIndex] = code; | ||
codeIndex = writeIndex++; | ||
code = 1; | ||
} | ||
} | ||
|
||
readIndex++; | ||
} | ||
|
||
// Write the final code byte | ||
output[codeIndex] = code; | ||
|
||
// Return the total number of bytes written | ||
return writeIndex; | ||
} | ||
|
||
std::size_t CobsEncoder::decode(const uint8_t *input, std::size_t length, | ||
uint8_t *output, std::size_t maxOutputLength) | ||
{ | ||
if (input == nullptr || output == nullptr || length == 0 || maxOutputLength < length - 1) | ||
{ | ||
return 0; // Invalid input or insufficient output buffer size | ||
} | ||
|
||
std::size_t readIndex = 0; // Position to read in the input | ||
std::size_t writeIndex = 0; // Position to write in the output | ||
|
||
while (readIndex < length) | ||
{ | ||
std::uint8_t code = input[readIndex++]; | ||
|
||
if (code == 0 || readIndex + code - 1 > length) | ||
{ | ||
return 0; // Malformed input | ||
} | ||
|
||
for (std::uint8_t i = 1; i < code; ++i) | ||
{ | ||
if (writeIndex >= maxOutputLength) | ||
{ | ||
return 0; // Output buffer overflow | ||
} | ||
|
||
output[writeIndex++] = input[readIndex++]; | ||
} | ||
|
||
if (code < 0xFF && writeIndex < maxOutputLength) | ||
{ | ||
// Insert a zero if the code is less than 0xFF | ||
output[writeIndex++] = 0; | ||
} | ||
} | ||
|
||
// Return the total number of bytes written | ||
return writeIndex; | ||
} | ||
} |
File renamed without changes.
18 changes: 14 additions & 4 deletions
18
...F103RBTx/Application/Device/Src/Crc32.cpp → Software/Common/Crc32.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.