libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2021-01-25 23:32:10
Commit9f6b39c028bf2eb00657f7626bead56ece55d387
Parent7a809d7cf4099b9cfe16fa2387d01800e476d28d

endian: Add endian

Diffstat

M README.md | 1 +
A src/lib/endian.h | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 154 insertions, 0 deletions

diff --git a/README.md b/README.md
index 827e073..98d4e8f 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,7 @@ OMKOV lib is a lightweight generic library to be included locally in projects.
 
 | Component        | Description                                         |
 | ---------------- | --------------------------------------------------- |
+| endian           | Endianness related functions                        |
 | error            | Error reporting functions                           |
 | mode             | Parse numeric or symbolic POSIX modes               |
 | optget           | Parse command line options                          |
diff --git a/src/lib/endian.h b/src/lib/endian.h
new file mode 100644
index 0000000..d7b7044
--- /dev/null
+++ b/src/lib/endian.h
@@ -0,0 +1,153 @@
+// endian.h, version 1.0.0
+// Endian header file for OMKOV lib
+// Copyright (C) 2021, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+#ifndef OMKOV_LIB_ENDIAN_H_G7AID2RQ
+#define OMKOV_LIB_ENDIAN_H_G7AID2RQ
+
+#include <stdint.h>
+
+/* Byte swap a 16-bit integer */
+#define BSWAP16(x) ( \
+	(((uint16_t)(x) & 0xFF00u) >> 8) | \
+	(((uint16_t)(x) & 0x00FFu) << 8))
+
+/* Byte swap a 32-bit integer */
+#define BSWAP32(x) ( \
+	(((uint32_t)(x) & 0xFF000000uL) >> 24) | \
+	(((uint32_t)(x) & 0x00FF0000uL) >>  8) | \
+	(((uint32_t)(x) & 0x0000FF00uL) <<  8) | \
+	(((uint32_t)(x) & 0x000000FFuL) << 24))
+
+/* Byte swap a 64-bit integer */
+#define BSWAP64(x) ( \
+	(((uint64_t)(x) & 0xFF00000000000000uLL) >> 56) | \
+	(((uint64_t)(x) & 0x00FF000000000000uLL) >> 40) | \
+	(((uint64_t)(x) & 0x0000FF0000000000uLL) >> 24) | \
+	(((uint64_t)(x) & 0x000000FF00000000uLL) >>  8) | \
+	(((uint64_t)(x) & 0x00000000FF000000uLL) <<  8) | \
+	(((uint64_t)(x) & 0x0000000000FF0000uLL) << 24) | \
+	(((uint64_t)(x) & 0x000000000000FF00uLL) << 40) | \
+	(((uint64_t)(x) & 0x00000000000000FFuLL) << 56))
+
+/* Load a 16-bit little endian integer */
+#define LD16LE(p) ( \
+	((uint16_t)(((uint8_t *)(p))[0]) << 0) | \
+	((uint16_t)(((uint8_t *)(p))[1]) << 8))
+
+/* Load a 16-bit big endian integer */
+#define LD16BE(p) ( \
+	((uint16_t)(((uint8_t *)(p))[0]) << 8) | \
+	((uint16_t)(((uint8_t *)(p))[1]) << 0))
+
+/* Load a 32-bit little endian integer */
+#define LD32LE(p) ( \
+	((uint32_t)(((uint8_t *)(p))[0]) <<  0) | \
+	((uint32_t)(((uint8_t *)(p))[1]) <<  8) | \
+	((uint32_t)(((uint8_t *)(p))[2]) << 16) | \
+	((uint32_t)(((uint8_t *)(p))[3]) << 24))
+
+/* Load a 32-bit big endian integer */
+#define LD32BE(p) ( \
+	((uint32_t)(((uint8_t *)(p))[0]) << 24) | \
+	((uint32_t)(((uint8_t *)(p))[1]) << 16) | \
+	((uint32_t)(((uint8_t *)(p))[2]) <<  8) | \
+	((uint32_t)(((uint8_t *)(p))[3]) <<  0))
+
+/* Load a 64-bit little endian integer */
+#define LD64LE(p) ( \
+	((uint64_t)(((uint8_t *)(p))[0]) <<  0) | \
+	((uint64_t)(((uint8_t *)(p))[1]) <<  8) | \
+	((uint64_t)(((uint8_t *)(p))[2]) << 16) | \
+	((uint64_t)(((uint8_t *)(p))[3]) << 24) | \
+	((uint64_t)(((uint8_t *)(p))[4]) << 32) | \
+	((uint64_t)(((uint8_t *)(p))[5]) << 40) | \
+	((uint64_t)(((uint8_t *)(p))[6]) << 48) | \
+	((uint64_t)(((uint8_t *)(p))[7]) << 56))
+
+/* Load a 64-bit big endian integer */
+#define LD64BE(p) ( \
+	((uint64_t)(((uint8_t *)(p))[0]) << 56) | \
+	((uint64_t)(((uint8_t *)(p))[1]) << 48) | \
+	((uint64_t)(((uint8_t *)(p))[2]) << 40) | \
+	((uint64_t)(((uint8_t *)(p))[3]) << 32) | \
+	((uint64_t)(((uint8_t *)(p))[4]) << 24) | \
+	((uint64_t)(((uint8_t *)(p))[5]) << 16) | \
+	((uint64_t)(((uint8_t *)(p))[6]) <<  8) | \
+	((uint64_t)(((uint8_t *)(p))[7]) <<  0))
+
+/* Store a 16-bit little endian integer */
+#define ST16LE(p, x) \
+	((uint8_t *)(p))[0] = ((uint16_t)(x) >> 0) & 0xFFu; \
+	((uint8_t *)(p))[1] = ((uint16_t)(x) >> 8) & 0xFFu
+
+/* Store a 16-bit big endian integer */
+#define ST16BE(p, x) \
+	((uint8_t *)(p))[0] = ((uint16_t)(x) >> 8) & 0xFFu; \
+	((uint8_t *)(p))[1] = ((uint16_t)(x) >> 0) & 0xFFu
+
+/* Store a 32-bit little endian integer */
+#define ST32LE(p, x) \
+	((uint8_t *)(p))[0] = ((uint32_t)(x) >>  0) & 0xFFu; \
+	((uint8_t *)(p))[1] = ((uint32_t)(x) >>  8) & 0xFFu; \
+	((uint8_t *)(p))[2] = ((uint32_t)(x) >> 16) & 0xFFu; \
+	((uint8_t *)(p))[3] = ((uint32_t)(x) >> 24) & 0xFFu
+
+/* Store a 32-bit big endian integer */
+#define ST32BE(p, x) \
+	((uint8_t *)(p))[0] = ((uint32_t)(x) >> 24) & 0xFFu; \
+	((uint8_t *)(p))[1] = ((uint32_t)(x) >> 16) & 0xFFu; \
+	((uint8_t *)(p))[2] = ((uint32_t)(x) >>  8) & 0xFFu; \
+	((uint8_t *)(p))[3] = ((uint32_t)(x) >>  0) & 0xFFu
+
+/* Store a 64-bit little endian integer */
+#define ST64LE(p, x) \
+	((uint8_t *)(p))[0] = ((uint64_t)(x) >>  0) & 0xFFu; \
+	((uint8_t *)(p))[1] = ((uint64_t)(x) >>  8) & 0xFFu; \
+	((uint8_t *)(p))[2] = ((uint64_t)(x) >> 16) & 0xFFu; \
+	((uint8_t *)(p))[3] = ((uint64_t)(x) >> 24) & 0xFFu; \
+	((uint8_t *)(p))[4] = ((uint64_t)(x) >> 32) & 0xFFu; \
+	((uint8_t *)(p))[5] = ((uint64_t)(x) >> 40) & 0xFFu; \
+	((uint8_t *)(p))[6] = ((uint64_t)(x) >> 48) & 0xFFu; \
+	((uint8_t *)(p))[7] = ((uint64_t)(x) >> 56) & 0xFFu
+
+/* Store a 64-bit big endian integer */
+#define ST64BE(p, x) \
+	((uint8_t *)(p))[0] = ((uint64_t)(x) >> 56) & 0xFFu; \
+	((uint8_t *)(p))[1] = ((uint64_t)(x) >> 48) & 0xFFu; \
+	((uint8_t *)(p))[2] = ((uint64_t)(x) >> 40) & 0xFFu; \
+	((uint8_t *)(p))[3] = ((uint64_t)(x) >> 32) & 0xFFu; \
+	((uint8_t *)(p))[4] = ((uint64_t)(x) >> 24) & 0xFFu; \
+	((uint8_t *)(p))[5] = ((uint64_t)(x) >> 16) & 0xFFu; \
+	((uint8_t *)(p))[6] = ((uint64_t)(x) >>  8) & 0xFFu; \
+	((uint8_t *)(p))[7] = ((uint64_t)(x) >>  0) & 0xFFu
+
+#endif // OMKOV_LIB_ENDIAN_H_G7AID2RQ