-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement utility functions, improve p04
- Loading branch information
1 parent
607170f
commit df36e44
Showing
5 changed files
with
201 additions
and
41 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
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,138 @@ | ||
/* SPDX-FileCopyrightText: 2022-present Artur Drogunow <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT */ | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
|
||
// copy uint16_t into buffer in littleendian byte order | ||
void uint16_to_littleendian(uint8_t *target, uint16_t value) | ||
{ | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
target[i] = (uint8_t)((value >> (i * 8)) & 0xffu); | ||
} | ||
} | ||
|
||
// copy uint32_t into buffer in littleendian byte order | ||
void uint32_to_littleendian(uint8_t *target, uint32_t value) | ||
{ | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
target[i] = (uint8_t)((value >> (i * 8)) & 0xffu); | ||
} | ||
} | ||
|
||
// copy uint64_t into buffer in littleendian byte order | ||
void uint64_to_littleendian(uint8_t *target, uint64_t value) | ||
{ | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
target[i] = (uint8_t)((value >> (i * 8)) & 0xffu); | ||
} | ||
} | ||
|
||
// read uint16_t from littleendian byte order buffer | ||
uint16_t littleendian_to_uint16(uint8_t *source) | ||
{ | ||
uint16_t value = 0; | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
value += (uint16_t)source[i] << (i * 8); | ||
} | ||
return value; | ||
} | ||
|
||
// read uint32_t from littleendian byte order buffer | ||
uint32_t littleendian_to_uint32(uint8_t *source) | ||
{ | ||
uint32_t value = 0; | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
value += (uint32_t)source[i] << (i * 8); | ||
} | ||
return value; | ||
} | ||
|
||
// read uint64_t from littleendian byte order buffer | ||
uint64_t littleendian_to_uint64(uint8_t *source) | ||
{ | ||
uint64_t value = 0; | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
value += (uint64_t)source[i] << (i * 8); | ||
} | ||
return value; | ||
} | ||
|
||
// copy uint16_t into buffer in bigendian byte order | ||
void uint16_to_bigendian(uint8_t *target, uint16_t value) | ||
{ | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
target[i] = (uint8_t)((value >> ((len - 1 - i) * 8)) & 0xffu); | ||
} | ||
} | ||
|
||
// copy uint32_t into buffer in bigendian byte order | ||
void uint32_to_bigendian(uint8_t *target, uint32_t value) | ||
{ | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
target[i] = (uint8_t)((value >> ((len - 1 - i) * 8)) & 0xffu); | ||
} | ||
} | ||
|
||
// copy uint64_t into buffer in bigendian byte order | ||
void uint64_to_bigendian(uint8_t *target, uint64_t value) | ||
{ | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
target[i] = (uint8_t)((value >> ((len - 1 - i) * 8)) & 0xffu); | ||
} | ||
} | ||
|
||
// read uint16_t from bigendian byte order buffer | ||
uint16_t bigendian_to_uint16(uint8_t *source) | ||
{ | ||
uint16_t value = 0; | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
value += (uint16_t)source[i] << ((len - 1 - i) * 8); | ||
} | ||
return value; | ||
} | ||
|
||
// read uint32_t from bigendian byte order buffer | ||
uint32_t bigendian_to_uint32(uint8_t *source) | ||
{ | ||
uint32_t value = 0; | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
value += (uint32_t)source[i] << ((len - 1 - i) * 8); | ||
} | ||
return value; | ||
} | ||
|
||
// read uint64_t from bigendian byte order buffer | ||
uint64_t bigendian_to_uint64(uint8_t *source) | ||
{ | ||
uint64_t value = 0; | ||
size_t len = sizeof(value); | ||
for (size_t i = 0; i < len; ++i) | ||
{ | ||
value += (uint64_t)source[i] << ((len - 1 - i) * 8); | ||
} | ||
return value; | ||
} |
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,26 @@ | ||
/* SPDX-FileCopyrightText: 2022-present Artur Drogunow <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT */ | ||
|
||
#ifndef UTIL_H | ||
#define UTIL_H | ||
|
||
#include <stdint.h> | ||
|
||
void uint16_to_littleendian(uint8_t *target, uint16_t value); | ||
void uint32_to_littleendian(uint8_t *target, uint32_t value); | ||
void uint64_to_littleendian(uint8_t *target, uint64_t value); | ||
|
||
uint16_t littleendian_to_uint16(uint8_t *source); | ||
uint32_t littleendian_to_uint32(uint8_t *source); | ||
uint64_t littleendian_to_uint64(uint8_t *source); | ||
|
||
void uint16_to_bigendian(uint8_t *target, uint16_t value); | ||
void uint32_to_bigendian(uint8_t *target, uint32_t value); | ||
void uint64_to_bigendian(uint8_t *target, uint64_t value); | ||
|
||
uint16_t bigendian_to_uint16(uint8_t *source); | ||
uint32_t bigendian_to_uint32(uint8_t *source); | ||
uint64_t bigendian_to_uint64(uint8_t *source); | ||
|
||
#endif |