more cleanup
[m6w6/libmemcached] / src / libmemcached / byteorder.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #if HAVE_HTONLL && HAVE_ARPA_INET_H
19 # include <arpa/inet.h>
20 #endif
21
22 #include "libmemcached/byteorder.h"
23
24 /* Byte swap a 64-bit number. */
25 #ifndef swap64
26 # ifndef HAVE_HTONLL
27 static inline uint64_t swap64(uint64_t in) {
28 # if !WORDS_BIGENDIAN
29 /* Little endian, flip the bytes around until someone makes a faster/better
30 * way to do this. */
31 uint64_t rv = 0;
32 for (uint8_t x = 0; x < 8; ++x) {
33 rv = (rv << 8) | (in & 0xff);
34 in >>= 8;
35 }
36 return rv;
37 # else
38 /* big-endian machines don't need byte swapping */
39 return in;
40 # endif // WORDS_BIGENDIAN
41 }
42 # endif
43 #endif
44
45 #include <sys/types.h>
46
47 uint64_t memcached_ntohll(uint64_t value) {
48 #ifdef HAVE_HTONLL
49 return ntohll(value);
50 #else
51 return swap64(value);
52 #endif
53 }
54
55 uint64_t memcached_htonll(uint64_t value) {
56 #ifdef HAVE_HTONLL
57 return htonll(value);
58 #else
59 return swap64(value);
60 #endif
61 }