WIP
[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 #include "libmemcached/byteorder.h"
18
19 /* Byte swap a 64-bit number. */
20 #ifndef swap64
21 # ifndef HAVE_HTONLL
22 static inline uint64_t swap64(uint64_t in) {
23 # if !WORDS_BIGENDIAN
24 /* Little endian, flip the bytes around until someone makes a faster/better
25 * way to do this. */
26 uint64_t rv = 0;
27 for (uint8_t x = 0; x < 8; ++x) {
28 rv = (rv << 8) | (in & 0xff);
29 in >>= 8;
30 }
31 return rv;
32 # else
33 /* big-endian machines don't need byte swapping */
34 return in;
35 # endif // WORDS_BIGENDIAN
36 }
37 # endif
38 #endif
39
40 #include <sys/types.h>
41
42 uint64_t memcached_ntohll(uint64_t value) {
43 #ifdef HAVE_HTONLL
44 return ntohll(value);
45 #else
46 return swap64(value);
47 #endif
48 }
49
50 uint64_t memcached_htonll(uint64_t value) {
51 #ifdef HAVE_HTONLL
52 return htonll(value);
53 #else
54 return swap64(value);
55 #endif
56 }