-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
include: util: Add mem_xor functions #67159
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -639,6 +639,45 @@ char *utf8_lcpy(char *dst, const char *src, size_t n); | |
(((buflen) != 0) && \ | ||
((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1)))) | ||
|
||
/** | ||
* @brief XOR n bytes | ||
* | ||
* @param dst Destination of where to store result. Shall be @p len bytes. | ||
* @param src1 First source. Shall be @p len bytes. | ||
* @param src2 Second source. Shall be @p len bytes. | ||
* @param len Number of bytes to XOR. | ||
*/ | ||
static inline void mem_xor_n(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, size_t len) | ||
{ | ||
while (len--) { | ||
*dst++ = *src1++ ^ *src2++; | ||
} | ||
} | ||
|
||
/** | ||
* @brief XOR 32 bits | ||
* | ||
* @param dst Destination of where to store result. Shall be 32 bits. | ||
* @param src1 First source. Shall be 32 bits. | ||
* @param src2 Second source. Shall be 32 bits. | ||
*/ | ||
static inline void mem_xor_32(uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4]) | ||
{ | ||
mem_xor_n(dst, src1, src2, 4U); | ||
} | ||
|
||
/** | ||
* @brief XOR 128 bits | ||
* | ||
* @param dst Destination of where to store result. Shall be 128 bits. | ||
* @param src1 First source. Shall be 128 bits. | ||
* @param src2 Second source. Shall be 128 bits. | ||
*/ | ||
static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16]) | ||
{ | ||
mem_xor_n(dst, src1, src2, 16); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here have a small loop and call the 32 variant instead. |
||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One possible optimization here would be to cast to uint32_t and then xor directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a bad suggestion. Since the code was copied from implementations from @cvinayak I'd like his input as to why it was not done like that in the first place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to support CPUs needing 4-byte aligned memory read/write operations (ARM cortex-m0 variants etc...).
Fun fact, was my very first contribution to Zephyr Project! de999a8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably solvable by using
UNALIGNED_PUT/UNALIGNED_GET
macros.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry look at your comment on UNALIGNED_PUT/UNALIGNED_GET now. The implementation in our toolchain header has workarounds for compiler version, and my feeling goes towards sticking to simple old while loop for now.