]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Added some network byte ordering functions and macros
authorTimo Sirainen <tss@iki.fi>
Wed, 6 Aug 2003 19:56:20 +0000 (22:56 +0300)
committerTimo Sirainen <tss@iki.fi>
Wed, 6 Aug 2003 19:56:20 +0000 (22:56 +0300)
--HG--
branch : HEAD

src/lib/Makefile.am
src/lib/byteorder.c [new file with mode: 0644]
src/lib/byteorder.h [new file with mode: 0644]

index 366bc52ee53ab89539179ecf0350502531baf992..5b597eed06fef43a7138c81a75d7a298fafcffcd 100644 (file)
@@ -4,6 +4,7 @@ liblib_a_SOURCES = \
        alarm-hup.c \
        base64.c \
        buffer.c \
+       byteorder.c \
        compat.c \
        data-stack.c \
        env-util.c \
@@ -62,6 +63,7 @@ noinst_HEADERS = \
        alarm-hup.h \
        base64.h \
        buffer.h \
+       byteorder.h \
        compat.h \
        data-stack.h \
        env-util.h \
diff --git a/src/lib/byteorder.c b/src/lib/byteorder.c
new file mode 100644 (file)
index 0000000..77312de
--- /dev/null
@@ -0,0 +1,38 @@
+/* Copyright (C) 2003 Timo Sirainen */
+
+#include "lib.h"
+#include "byteorder.h"
+
+#ifndef WORDS_BIGENDIAN
+void nbo_to_host(void *data, size_t size)
+{
+       if (size == sizeof(uint32_t)) {
+               uint32_t *num = (uint32_t *) data;
+
+               *num = ntohl(*num);
+       } else if (size == sizeof(uint32_t)*2) {
+               uint32_t *num = (uint32_t *) data;
+               uint32_t temp;
+
+               temp = ntohl(num[0]);
+               num[0] = ntohl(num[1]);
+               num[1] = temp;
+       }
+}
+
+void host_to_nbo(void *data, size_t size)
+{
+       if (size == sizeof(uint32_t)) {
+               uint32_t *num = (uint32_t *) data;
+
+               *num = htonl(*num);
+       } else if (size == sizeof(uint32_t)*2) {
+               uint32_t *num = (uint32_t *) data;
+               uint32_t temp;
+
+               temp = htonl(num[0]);
+               num[0] = htonl(num[1]);
+               num[1] = temp;
+       }
+}
+#endif
diff --git a/src/lib/byteorder.h b/src/lib/byteorder.h
new file mode 100644 (file)
index 0000000..e1c0cc0
--- /dev/null
@@ -0,0 +1,91 @@
+#ifndef __BYTEORDER_H
+#define __BYTEORDER_H
+
+#ifdef WORDS_BIGENDIAN
+
+/* Bits in network byte order */
+#  define NBO32_BIT0   0x00000001
+#  define NBO32_BIT1   0x00000002
+#  define NBO32_BIT2   0x00000004
+#  define NBO32_BIT3   0x00000008
+#  define NBO32_BIT4   0x00000010
+#  define NBO32_BIT5   0x00000020
+#  define NBO32_BIT6   0x00000040
+#  define NBO32_BIT7   0x00000080
+#  define NBO32_BIT8   0x00000100
+#  define NBO32_BIT9   0x00000200
+#  define NBO32_BIT10  0x00000400
+#  define NBO32_BIT11  0x00000800
+#  define NBO32_BIT12  0x00001000
+#  define NBO32_BIT13  0x00002000
+#  define NBO32_BIT14  0x00004000
+#  define NBO32_BIT15  0x00008000
+#  define NBO32_BIT16  0x00010000
+#  define NBO32_BIT17  0x00020000
+#  define NBO32_BIT18  0x00040000
+#  define NBO32_BIT19  0x00080000
+#  define NBO32_BIT20  0x00100000
+#  define NBO32_BIT21  0x00200000
+#  define NBO32_BIT22  0x00400000
+#  define NBO32_BIT23  0x00800000
+#  define NBO32_BIT24  0x01000000
+#  define NBO32_BIT25  0x02000000
+#  define NBO32_BIT26  0x04000000
+#  define NBO32_BIT27  0x08000000
+#  define NBO32_BIT28  0x10000000
+#  define NBO32_BIT29  0x20000000
+#  define NBO32_BIT30  0x40000000
+#  define NBO32_BIT31  0x80000000
+
+#  define nbo_to_host(data, size)
+#  define host_to_nbo(data, size)
+#  define nbo_to_uint32(num) (num)
+#  define uint32_to_nbo(num) (num)
+#else
+
+/* We support only big endian and little endian. AFAIK PDP-endian is the
+   only third used one but I don't think I need PDP-support for now :) */
+#include <arpa/inet.h>
+
+/* Bits in network byte order */
+#  define NBO32_BIT0   0x01000000
+#  define NBO32_BIT1   0x02000000
+#  define NBO32_BIT2   0x04000000
+#  define NBO32_BIT3   0x08000000
+#  define NBO32_BIT4   0x10000000
+#  define NBO32_BIT5   0x20000000
+#  define NBO32_BIT6   0x40000000
+#  define NBO32_BIT7   0x80000000
+#  define NBO32_BIT8   0x00010000
+#  define NBO32_BIT9   0x00020000
+#  define NBO32_BIT10  0x00040000
+#  define NBO32_BIT11  0x00080000
+#  define NBO32_BIT12  0x00100000
+#  define NBO32_BIT13  0x00200000
+#  define NBO32_BIT14  0x00400000
+#  define NBO32_BIT15  0x00800000
+#  define NBO32_BIT16  0x00000100
+#  define NBO32_BIT17  0x00000200
+#  define NBO32_BIT18  0x00000400
+#  define NBO32_BIT19  0x00000800
+#  define NBO32_BIT20  0x00001000
+#  define NBO32_BIT21  0x00002000
+#  define NBO32_BIT22  0x00004000
+#  define NBO32_BIT23  0x00008000
+#  define NBO32_BIT24  0x00000001
+#  define NBO32_BIT25  0x00000002
+#  define NBO32_BIT26  0x00000004
+#  define NBO32_BIT27  0x00000008
+#  define NBO32_BIT28  0x00000010
+#  define NBO32_BIT29  0x00000020
+#  define NBO32_BIT30  0x00000040
+#  define NBO32_BIT31  0x00000080
+
+void nbo_to_host(void *data, size_t size);
+void host_to_nbo(void *data, size_t size);
+
+#  define nbo_to_uint32(num) ntohl(num)
+#  define uint32_to_nbo(num) htonl(num)
+#endif
+
+#endif