diff options
author | John Mark Bell <jmb@netsurf-browser.org> | 2009-07-02 17:53:52 +0000 |
---|---|---|
committer | John Mark Bell <jmb@netsurf-browser.org> | 2009-07-02 17:53:52 +0000 |
commit | 040e428e08eedabed3994941a1b098a56827ca1d (patch) | |
tree | 4e1eae3df2dd261ac2e800355d4599e2141d3ffc /src/utils/endian.h | |
parent | 7f77907ab1f44a0d8b7c0fa14f2c9df69f42500b (diff) | |
download | libparserutils-040e428e08eedabed3994941a1b098a56827ca1d.tar.gz libparserutils-040e428e08eedabed3994941a1b098a56827ca1d.tar.bz2 |
Remove dependency on a library providing ntohl/htonl.
svn path=/trunk/libparserutils/; revision=8267
Diffstat (limited to 'src/utils/endian.h')
-rw-r--r-- | src/utils/endian.h | 40 |
1 files changed, 40 insertions, 0 deletions
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 |