Skip to content
Extraits de code Groupes Projets
Vérifiée Valider b4656680 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Move the #if defined for big and little endian into the source code (not

this in the headers)
parent 75c4ee13
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!101Corrections
...@@ -27,36 +27,15 @@ double be_double_t(const uint8_t bytes[]); ...@@ -27,36 +27,15 @@ double be_double_t(const uint8_t bytes[]);
be_uint32_t defined function. */ be_uint32_t defined function. */
float be_float_t(const uint8_t bytes[]); float be_float_t(const uint8_t bytes[]);
#if defined (_LITTLE_ENDIAN)
/* Destination: Little-endian */
#define be_uint32_t be_uint32_t_to_le
#define be_uint64_t be_uint64_t_to_le
/* Read `bytes` as the big endian representation of a 32-bit unsigned integer. /* Read `bytes` as the big endian representation of a 32-bit unsigned integer.
`n` is the number of bytes in `bytes`. The result is a Little-endian. `n` is the number of bytes in `bytes`. The result is a 'correct'-endian.
Returns 0 if n is 0. Returns 0 if n is 0.
Restriction: n <= 4 */ Restriction: n <= 4 */
uint32_t be_uint32_t_to_le(const uint8_t bytes[], size_t n); uint32_t be_uint32_t(const uint8_t bytes[], size_t n);
/* Same as `be_uint32_t` but for 64-bit unsigned integers. The result is a /* Same as `be_uint32_t` but for 64-bit unsigned integers. The result is a
Little-endian. Restriction: n <= 8 */ 'correct'-endian. Restriction: n <= 8 */
uint64_t be_uint64_t_to_le(const uint8_t bytes[], size_t n); uint64_t be_uint64_t(const uint8_t bytes[], size_t n);
#else
#if defined(_BIG_ENDIAN)
/* Destination: Big-endian */
#error "You must define functions to read big endian and store them as big endian."
#define be_uint32_t be_uint32_t_to_be
#define be_uint64_t be_uint64_t_to_be
/* Same as the be_to_le versions */
uint32_t be_uint32_t_to_be(const uint8_t bytes[], size_t n);
uint64_t be_uint64_t_to_be(const uint8_t bytes[], size_t n);
#endif /* _BIG_ENDIAN */
#endif /* _LITTLE_ENDIAN */
/* Trim the string `str` of the char `c` from the right and the left. /* Trim the string `str` of the char `c` from the right and the left.
The string may not be null terminated. The string may not be null terminated.
......
...@@ -26,15 +26,6 @@ __unused(void *dummy, ...) ...@@ -26,15 +26,6 @@ __unused(void *dummy, ...)
/* Endian stuff */ /* Endian stuff */
uint32_t
be_uint32_t_to_le(const uint8_t bytes[], size_t n)
{
uint32_t res = 0;
for (size_t i = 0; i < n; i++)
res = (res << 8u) | bytes[i];
return res;
}
float float
be_float_t(const uint8_t bytes[]) be_float_t(const uint8_t bytes[])
{ {
...@@ -47,11 +38,20 @@ be_float_t(const uint8_t bytes[]) ...@@ -47,11 +38,20 @@ be_float_t(const uint8_t bytes[])
} }
uint64_t uint64_t
be_uint64_t_to_le(const uint8_t bytes[], size_t n) be_uint64_t(const uint8_t bytes[], size_t n)
{ {
uint64_t res = 0; uint64_t res = 0;
for (size_t i = 0; i < n; i++) for (size_t i = 0; i < n; i++) {
#if defined (_LITTLE_ENDIAN)
res = (res << 8u) | bytes[i]; res = (res << 8u) | bytes[i];
#else
#if defined(_BIG_ENDIAN)
res = (res >> 8u) | bytes[i];
#else
#error "Nor BIG nor LITTLE endian"
#endif /* _BIG_ENDIAN */
#endif /* _LITTLE_ENDIAN */
}
return res; return res;
} }
...@@ -67,20 +67,20 @@ be_double_t(const uint8_t bytes[]) ...@@ -67,20 +67,20 @@ be_double_t(const uint8_t bytes[])
} }
uint32_t uint32_t
be_uint32_t_to_be(const uint8_t bytes[], size_t n) be_uint32_t(const uint8_t bytes[], size_t n)
{ {
uint32_t res = 0; uint32_t res = 0;
for (size_t i = 0; i < n; i++) for (size_t i = 0; i < n; i++) {
res = (res >> 8u) | bytes[i]; #if defined (_LITTLE_ENDIAN)
return res; res = (res << 8u) | bytes[i];
} #else
#if defined(_BIG_ENDIAN)
uint64_t
be_uint64_t_to_be(const uint8_t bytes[], size_t n)
{
uint64_t res = 0;
for (size_t i = 0; i < n; i++)
res = (res >> 8u) | bytes[i]; res = (res >> 8u) | bytes[i];
#else
#error "Nor BIG nor LITTLE endian"
#endif /* _BIG_ENDIAN */
#endif /* _LITTLE_ENDIAN */
}
return res; return res;
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter