[coreboot-gerrit] Patch set updated for coreboot: 390099d libpayload/endian.h: Provide alignment-agnostic enc/dec bytestreams.

Edward O'Callaghan (eocallaghan@alterapraxis.com) gerrit at coreboot.org
Wed Feb 12 13:09:15 CET 2014


Edward O'Callaghan (eocallaghan at alterapraxis.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/5198

-gerrit

commit 390099d7f8843071c09ae53648df41dff4ecd3cc
Author: Edward O'Callaghan <eocallaghan at alterapraxis.com>
Date:   Wed Feb 12 21:53:44 2014 +1100

    libpayload/endian.h: Provide alignment-agnostic enc/dec bytestreams.
    
    Alignment-agnostic encode/decode bytestream to/from little/big endian.
    
    The le16enc(), le16dec(), le32enc(), le32dec() functions encode and
    decode integers to/from byte strings on any alignment in big/little
    endian format. See BYTEORDER(9).
    
    Change-Id: I73a174b9c02c467bc60590c5cd894dac58b8683a
    Signed-off-by: Edward O'Callaghan <eocallaghan at alterapraxis.com>
---
 payloads/libpayload/include/endian.h | 44 ++++++++++++++++++++++++++++++++++--
 1 file changed, 42 insertions(+), 2 deletions(-)

diff --git a/payloads/libpayload/include/endian.h b/payloads/libpayload/include/endian.h
index cb50443..1ac2b63 100644
--- a/payloads/libpayload/include/endian.h
+++ b/payloads/libpayload/include/endian.h
@@ -69,7 +69,7 @@ static inline uint64_t swap_bytes64(uint64_t in)
 
 #error Cant tell if the CPU is little or big endian.
 
-#endif
+#endif /* CONFIG_*_ENDIAN */
 
 #define be16toh(in) htobe16(in)
 #define be32toh(in) htobe32(in)
@@ -87,6 +87,46 @@ static inline uint64_t swap_bytes64(uint64_t in)
 #define ntohl(in) be32toh(in)
 #define ntohll(in) be64toh(in)
 
+/* Alignment-agnostic encode/decode bytestream to/from little/big endian.
+ * Note: Trimmed down from FreeBSD 10.0 (sys/endian.h).
+ */
+
+static inline uint16_t
+le16dec(const void *pp)
+{
+	unsigned char const *p = (unsigned char const *)pp;
+
+	return ((p[1] << 8) | p[0]);
+}
+
+static inline uint32_t
+le32dec(const void *pp)
+{
+	unsigned char const *p = (unsigned char const *)pp;
+
+	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
+}
+
+static inline void
+le16enc(void *pp, uint16_t u)
+{
+	unsigned char *p = (unsigned char *)pp;
+
+	p[0] = u & 0xff;
+	p[1] = (u >> 8) & 0xff;
+}
+
+static inline void
+le32enc(void *pp, uint32_t u)
+{
+	unsigned char *p = (unsigned char *)pp;
+
+	p[0] = u & 0xff;
+	p[1] = (u >> 8) & 0xff;
+	p[2] = (u >> 16) & 0xff;
+	p[3] = (u >> 24) & 0xff;
+}
+
 /* Deprecated names (not in glibc / BSD) */
 #define htobew(in) htobe16(in)
 #define htobel(in) htobe32(in)
@@ -101,4 +141,4 @@ static inline uint64_t swap_bytes64(uint64_t in)
 #define letohl(in) le32toh(in)
 #define letohll(in) le64toh(in)
 
-#endif
+#endif /* _ENDIAN_H_ */



More information about the coreboot-gerrit mailing list