From 9744a40f7aa71f874b9f3eeb3c5e7d0899c8d409 Mon Sep 17 00:00:00 2001 From: David Goulet Date: Mon, 11 Jan 2016 11:13:04 +0100 Subject: [PATCH] Add tor_htonll/ntohll functions Signed-off-by: David Goulet --- src/common/util.c | 21 +++++++++++++++++++++ src/common/util.h | 2 ++ 2 files changed, 23 insertions(+) diff --git a/src/common/util.c b/src/common/util.c index 4b6df81b7d..3c5341d460 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -5587,3 +5587,24 @@ clamp_double_to_int64(double number) return signbit(number) ? INT64_MIN : INT64_MAX; } +/** Return a uint64_t value from a in network byte order. */ +uint64_t +tor_htonll(uint64_t a) +{ +#ifdef WORDS_BIGENDIAN + /* Big endian. */ + return a; +#else /* WORDS_BIGENDIAN */ + /* Little endian. The worst... */ + return htonl((uint32_t)(a>>32)) | + (((uint64_t)htonl((uint32_t)a))<<32); +#endif /* WORDS_BIGENDIAN */ +} + +/** Return a uint64_t value from a in host byte order. */ +uint64_t +tor_ntohll(uint64_t a) +{ + return tor_htonll(a); +} + diff --git a/src/common/util.h b/src/common/util.h index 7cb33dc680..fdf1c03b6e 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -61,6 +61,8 @@ void *tor_memdup_(const void *mem, size_t len DMALLOC_PARAMS) void *tor_memdup_nulterm_(const void *mem, size_t len DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1)); void tor_free_(void *mem); +uint64_t tor_htonll(uint64_t a); +uint64_t tor_ntohll(uint64_t a); #ifdef USE_DMALLOC extern int dmalloc_free(const char *file, const int line, void *pnt, const int func_id);