big-endian convert: Use stdint.h
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 5s

Replace "unsigned short" with uint16_t, "unsigned int" with uint32_t,
and "unsigned long long" with uint64_t.
This commit is contained in:
Stephen Seo 2024-07-15 16:59:15 +09:00
parent cb79e4e45c
commit 4670f0f3c1
3 changed files with 12 additions and 10 deletions

View file

@ -20,14 +20,14 @@
int simple_archiver_helper_is_big_endian(void) { int simple_archiver_helper_is_big_endian(void) {
union { union {
unsigned int i; uint32_t i;
char c[4]; char c[4];
} bint = {0x01020304}; } bint = {0x01020304};
return bint.c[0] == 1 ? 1 : 0; return bint.c[0] == 1 ? 1 : 0;
} }
void simple_archiver_helper_16_bit_be(unsigned short *value) { void simple_archiver_helper_16_bit_be(uint16_t *value) {
if (simple_archiver_helper_is_big_endian() == 0) { if (simple_archiver_helper_is_big_endian() == 0) {
unsigned char c = ((unsigned char *)value)[0]; unsigned char c = ((unsigned char *)value)[0];
((unsigned char *)value)[0] = ((unsigned char *)value)[1]; ((unsigned char *)value)[0] = ((unsigned char *)value)[1];
@ -35,7 +35,7 @@ void simple_archiver_helper_16_bit_be(unsigned short *value) {
} }
} }
void simple_archiver_helper_32_bit_be(unsigned int *value) { void simple_archiver_helper_32_bit_be(uint32_t *value) {
if (simple_archiver_helper_is_big_endian() == 0) { if (simple_archiver_helper_is_big_endian() == 0) {
for (unsigned int i = 0; i < 2; ++i) { for (unsigned int i = 0; i < 2; ++i) {
unsigned char c = ((unsigned char *)value)[i]; unsigned char c = ((unsigned char *)value)[i];
@ -45,7 +45,7 @@ void simple_archiver_helper_32_bit_be(unsigned int *value) {
} }
} }
void simple_archiver_helper_64_bit_be(unsigned long long *value) { void simple_archiver_helper_64_bit_be(uint64_t *value) {
if (simple_archiver_helper_is_big_endian() == 0) { if (simple_archiver_helper_is_big_endian() == 0) {
for (unsigned int i = 0; i < 4; ++i) { for (unsigned int i = 0; i < 4; ++i) {
unsigned char c = ((unsigned char *)value)[i]; unsigned char c = ((unsigned char *)value)[i];

View file

@ -19,18 +19,20 @@
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_ #ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_ #define SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_
#include <stdint.h>
static const unsigned int MAX_SYMBOLIC_LINK_SIZE = 512; static const unsigned int MAX_SYMBOLIC_LINK_SIZE = 512;
/// Returns non-zero if this system is big-endian. /// Returns non-zero if this system is big-endian.
int simple_archiver_helper_is_big_endian(void); int simple_archiver_helper_is_big_endian(void);
/// Swaps value from/to big-endian. Nop on big-endian systems. /// Swaps value from/to big-endian. Nop on big-endian systems.
void simple_archiver_helper_16_bit_be(unsigned short *value); void simple_archiver_helper_16_bit_be(uint16_t *value);
/// Swaps value from/to big-endian. Nop on big-endian systems. /// Swaps value from/to big-endian. Nop on big-endian systems.
void simple_archiver_helper_32_bit_be(unsigned int *value); void simple_archiver_helper_32_bit_be(uint32_t *value);
/// Swaps value from/to big-endian. Nop on big-endian systems. /// Swaps value from/to big-endian. Nop on big-endian systems.
void simple_archiver_helper_64_bit_be(unsigned long long *value); void simple_archiver_helper_64_bit_be(uint64_t *value);
#endif #endif

View file

@ -114,7 +114,7 @@ int main(void) {
{ {
// Only if system is little-endian. // Only if system is little-endian.
if (simple_archiver_helper_is_big_endian() == 0) { if (simple_archiver_helper_is_big_endian() == 0) {
unsigned short u16 = 0x0102; uint16_t u16 = 0x0102;
CHECK_TRUE(((unsigned char *)&u16)[0] == 2); CHECK_TRUE(((unsigned char *)&u16)[0] == 2);
CHECK_TRUE(((unsigned char *)&u16)[1] == 1); CHECK_TRUE(((unsigned char *)&u16)[1] == 1);
simple_archiver_helper_16_bit_be(&u16); simple_archiver_helper_16_bit_be(&u16);
@ -124,7 +124,7 @@ int main(void) {
CHECK_TRUE(((unsigned char *)&u16)[0] == 2); CHECK_TRUE(((unsigned char *)&u16)[0] == 2);
CHECK_TRUE(((unsigned char *)&u16)[1] == 1); CHECK_TRUE(((unsigned char *)&u16)[1] == 1);
unsigned int u32 = 0x01020304; uint32_t u32 = 0x01020304;
CHECK_TRUE(((unsigned char *)&u32)[0] == 4); CHECK_TRUE(((unsigned char *)&u32)[0] == 4);
CHECK_TRUE(((unsigned char *)&u32)[1] == 3); CHECK_TRUE(((unsigned char *)&u32)[1] == 3);
CHECK_TRUE(((unsigned char *)&u32)[2] == 2); CHECK_TRUE(((unsigned char *)&u32)[2] == 2);
@ -140,7 +140,7 @@ int main(void) {
CHECK_TRUE(((unsigned char *)&u32)[2] == 2); CHECK_TRUE(((unsigned char *)&u32)[2] == 2);
CHECK_TRUE(((unsigned char *)&u32)[3] == 1); CHECK_TRUE(((unsigned char *)&u32)[3] == 1);
unsigned long long u64 = 0x010203040a0b0c0d; uint64_t u64 = 0x010203040a0b0c0d;
CHECK_TRUE(((unsigned char *)&u64)[0] == 0xd); CHECK_TRUE(((unsigned char *)&u64)[0] == 0xd);
CHECK_TRUE(((unsigned char *)&u64)[1] == 0xc); CHECK_TRUE(((unsigned char *)&u64)[1] == 0xc);
CHECK_TRUE(((unsigned char *)&u64)[2] == 0xb); CHECK_TRUE(((unsigned char *)&u64)[2] == 0xb);