summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/charset/codecs/codec_8859.c16
-rw-r--r--src/charset/codecs/codec_ascii.c16
-rw-r--r--src/charset/codecs/codec_ext8.c16
-rw-r--r--src/charset/codecs/codec_utf16.c16
-rw-r--r--src/charset/codecs/codec_utf8.c16
-rw-r--r--src/utils/endian.h40
6 files changed, 60 insertions, 60 deletions
diff --git a/src/charset/codecs/codec_8859.c b/src/charset/codecs/codec_8859.c
index fec034d..d323ca0 100644
--- a/src/charset/codecs/codec_8859.c
+++ b/src/charset/codecs/codec_8859.c
@@ -9,18 +9,10 @@
#include <stdlib.h>
#include <string.h>
-#ifdef BUILD_TARGET_windows
-#include <winsock.h>
-#else
-/* These three are for htonl / ntohl */
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#endif
-
#include <parserutils/charset/mibenum.h>
#include "charset/codecs/codec_impl.h"
+#include "utils/endian.h"
#include "utils/utils.h"
#include "charset/codecs/8859_tables.h"
@@ -255,7 +247,7 @@ parserutils_error charset_8859_codec_encode(parserutils_charset_codec *codec,
/* Now process the characters for this call */
while (*sourcelen > 0) {
- ucs4 = (uint32_t) ntohl(*((uint32_t *) (void *) *source));
+ ucs4 = endian_big_to_host(*((uint32_t *) (void *) *source));
towrite = &ucs4;
towritelen = 1;
@@ -353,7 +345,7 @@ parserutils_error charset_8859_codec_decode(parserutils_charset_codec *codec,
while (c->read_len > 0 && *destlen >= c->read_len * 4) {
*((uint32_t *) (void *) *dest) =
- (uint32_t) htonl(pread[0]);
+ endian_host_to_big(pread[0]);
*dest += 4;
*destlen -= 4;
@@ -502,7 +494,7 @@ parserutils_error charset_8859_codec_output_decoded_char(charset_8859_codec *c,
return PARSERUTILS_NOMEM;
}
- *((uint32_t *) (void *) *dest) = (uint32_t) htonl(ucs4);
+ *((uint32_t *) (void *) *dest) = endian_host_to_big(ucs4);
*dest += 4;
*destlen -= 4;
diff --git a/src/charset/codecs/codec_ascii.c b/src/charset/codecs/codec_ascii.c
index 46bfe5f..b73280c 100644
--- a/src/charset/codecs/codec_ascii.c
+++ b/src/charset/codecs/codec_ascii.c
@@ -9,18 +9,10 @@
#include <stdlib.h>
#include <string.h>
-#ifdef BUILD_TARGET_windows
-#include <winsock.h>
-#else
-/* These three are for htonl / ntohl */
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#endif
-
#include <parserutils/charset/mibenum.h>
#include "charset/codecs/codec_impl.h"
+#include "utils/endian.h"
#include "utils/utils.h"
/**
@@ -209,7 +201,7 @@ parserutils_error charset_ascii_codec_encode(parserutils_charset_codec *codec,
/* Now process the characters for this call */
while (*sourcelen > 0) {
- ucs4 = (uint32_t) ntohl(*((uint32_t *) (void *) *source));
+ ucs4 = endian_big_to_host(*((uint32_t *) (void *) *source));
towrite = &ucs4;
towritelen = 1;
@@ -307,7 +299,7 @@ parserutils_error charset_ascii_codec_decode(parserutils_charset_codec *codec,
while (c->read_len > 0 && *destlen >= c->read_len * 4) {
*((uint32_t *) (void *) *dest) =
- (uint32_t) htonl(pread[0]);
+ endian_host_to_big(pread[0]);
*dest += 4;
*destlen -= 4;
@@ -457,7 +449,7 @@ parserutils_error charset_ascii_codec_output_decoded_char(
return PARSERUTILS_NOMEM;
}
- *((uint32_t *) (void *) *dest) = (uint32_t) htonl(ucs4);
+ *((uint32_t *) (void *) *dest) = endian_host_to_big(ucs4);
*dest += 4;
*destlen -= 4;
diff --git a/src/charset/codecs/codec_ext8.c b/src/charset/codecs/codec_ext8.c
index cbde1c4..5e7cb8a 100644
--- a/src/charset/codecs/codec_ext8.c
+++ b/src/charset/codecs/codec_ext8.c
@@ -9,18 +9,10 @@
#include <stdlib.h>
#include <string.h>
-#ifdef BUILD_TARGET_windows
-#include <winsock.h>
-#else
-/* These three are for htonl / ntohl */
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#endif
-
#include <parserutils/charset/mibenum.h>
#include "charset/codecs/codec_impl.h"
+#include "utils/endian.h"
#include "utils/utils.h"
#include "charset/codecs/ext8_tables.h"
@@ -249,7 +241,7 @@ parserutils_error charset_ext8_codec_encode(parserutils_charset_codec *codec,
/* Now process the characters for this call */
while (*sourcelen > 0) {
- ucs4 = (uint32_t) ntohl(*((uint32_t *) (void *) *source));
+ ucs4 = endian_big_to_host(*((uint32_t *) (void *) *source));
towrite = &ucs4;
towritelen = 1;
@@ -347,7 +339,7 @@ parserutils_error charset_ext8_codec_decode(parserutils_charset_codec *codec,
while (c->read_len > 0 && *destlen >= c->read_len * 4) {
*((uint32_t *) (void *) *dest) =
- (uint32_t) htonl(pread[0]);
+ endian_host_to_big(pread[0]);
*dest += 4;
*destlen -= 4;
@@ -496,7 +488,7 @@ parserutils_error charset_ext8_codec_output_decoded_char(charset_ext8_codec *c,
return PARSERUTILS_NOMEM;
}
- *((uint32_t *) (void *) *dest) = (uint32_t) htonl(ucs4);
+ *((uint32_t *) (void *) *dest) = endian_host_to_big(ucs4);
*dest += 4;
*destlen -= 4;
diff --git a/src/charset/codecs/codec_utf16.c b/src/charset/codecs/codec_utf16.c
index 6ec7878..8dbcae1 100644
--- a/src/charset/codecs/codec_utf16.c
+++ b/src/charset/codecs/codec_utf16.c
@@ -8,19 +8,11 @@
#include <stdlib.h>
#include <string.h>
-#ifdef BUILD_TARGET_windows
-#include <winsock.h>
-#else
-/* These three are for htonl / ntohl */
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#endif
-
#include <parserutils/charset/mibenum.h>
#include <parserutils/charset/utf16.h>
#include "charset/codecs/codec_impl.h"
+#include "utils/endian.h"
#include "utils/utils.h"
/**
@@ -211,7 +203,7 @@ parserutils_error charset_utf16_codec_encode(parserutils_charset_codec *codec,
/* Now process the characters for this call */
while (*sourcelen > 0) {
- ucs4 = (uint32_t) ntohl(*((uint32_t *) (void *) *source));
+ ucs4 = endian_big_to_host(*((uint32_t *) (void *) *source));
towrite = &ucs4;
towritelen = 1;
@@ -315,7 +307,7 @@ parserutils_error charset_utf16_codec_decode(parserutils_charset_codec *codec,
while (c->read_len > 0 && *destlen >= c->read_len * 4) {
*((uint32_t *) (void *) *dest) =
- (uint32_t) htonl(pread[0]);
+ endian_host_to_big(pread[0]);
*dest += 4;
*destlen -= 4;
@@ -546,7 +538,7 @@ parserutils_error charset_utf16_codec_output_decoded_char(charset_utf16_codec *c
return PARSERUTILS_NOMEM;
}
- *((uint32_t *) (void *) *dest) = (uint32_t) htonl(ucs4);
+ *((uint32_t *) (void *) *dest) = endian_host_to_big(ucs4);
*dest += 4;
*destlen -= 4;
diff --git a/src/charset/codecs/codec_utf8.c b/src/charset/codecs/codec_utf8.c
index 7ef7fb8..ceae951 100644
--- a/src/charset/codecs/codec_utf8.c
+++ b/src/charset/codecs/codec_utf8.c
@@ -8,19 +8,11 @@
#include <stdlib.h>
#include <string.h>
-#ifdef BUILD_TARGET_windows
-#include <winsock.h>
-#else
-/* These three are for htonl / ntohl */
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#endif
-
#include <parserutils/charset/mibenum.h>
#include "charset/codecs/codec_impl.h"
#include "charset/encodings/utf8impl.h"
+#include "utils/endian.h"
#include "utils/utils.h"
/**
@@ -206,7 +198,7 @@ parserutils_error charset_utf8_codec_encode(parserutils_charset_codec *codec,
/* Now process the characters for this call */
while (*sourcelen > 0) {
- ucs4 = (uint32_t) ntohl(*((uint32_t *) (void *) *source));
+ ucs4 = endian_big_to_host(*((uint32_t *) (void *) *source));
towrite = &ucs4;
towritelen = 1;
@@ -302,7 +294,7 @@ parserutils_error charset_utf8_codec_decode(parserutils_charset_codec *codec,
while (c->read_len > 0 && *destlen >= c->read_len * 4) {
*((uint32_t *) (void *) *dest) =
- (uint32_t) htonl(pread[0]);
+ endian_host_to_big(pread[0]);
*dest += 4;
*destlen -= 4;
@@ -548,7 +540,7 @@ parserutils_error charset_utf8_codec_output_decoded_char(charset_utf8_codec *c,
return PARSERUTILS_NOMEM;
}
- *((uint32_t *) (void *) *dest) = (uint32_t) htonl(ucs4);
+ *((uint32_t *) (void *) *dest) = endian_host_to_big(ucs4);
*dest += 4;
*destlen -= 4;
diff --git a/src/utils/endian.h b/src/utils/endian.h
new file mode 100644
index 0000000..6dc36a0
--- /dev/null
+++ b/src/utils/endian.h
@@ -0,0 +1,40 @@
+/*
+ * This file is part of LibParserUtils.
+ * Licensed under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ * Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
+ */
+
+#ifndef parserutils_endian_h_
+#define parserutils_endian_h_
+
+static inline bool endian_host_is_le(void)
+{
+ uint32_t magic = 0x01020304;
+
+ return (((uint8_t *) &magic)[0] == 0x04);
+}
+
+static inline uint32_t endian_swap(uint32_t val)
+{
+ return ((val & 0xff000000) >> 24) | ((val & 0x00ff0000) >> 8) |
+ ((val & 0x0000ff00) << 8) | ((val & 0x000000ff) << 24);
+}
+
+static inline uint32_t endian_host_to_big(uint32_t host)
+{
+ if (endian_host_is_le())
+ return endian_swap(host);
+
+ return host;
+}
+
+static inline uint32_t endian_big_to_host(uint32_t big)
+{
+ if (endian_host_is_le())
+ return endian_swap(big);
+
+ return big;
+}
+
+#endif