| 1 |
/* Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License, version 2.0, |
| 5 |
as published by the Free Software Foundation. |
| 6 |
|
| 7 |
This program is designed to work with certain software (including |
| 8 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 9 |
as designated in a particular file or component or in included license |
| 10 |
documentation. The authors of MySQL hereby grant you an additional |
| 11 |
permission to link the program and your derivative works with the |
| 12 |
separately licensed software that they have either included with |
| 13 |
the program or referenced in the documentation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License, version 2.0, for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 |
|
| 24 |
/** |
| 25 |
@file libbinlogevents/include/byteorder.h |
| 26 |
|
| 27 |
@brief The file contains functions to convert the byte encoding of integer |
| 28 |
values to and from little-endian and big-endian byte order. |
| 29 |
*/ |
| 30 |
|
| 31 |
#ifndef BYTEORDER_INCLUDED |
| 32 |
#define BYTEORDER_INCLUDED |
| 33 |
|
| 34 |
#include <stdint.h> |
| 35 |
#include "binlog_config.h" |
| 36 |
#include "my_compiler.h" |
| 37 |
#include "my_config.h" |
| 38 |
#ifndef STANDALONE_BINLOG |
| 39 |
#define HAVE_MYSYS 1 |
| 40 |
#endif |
| 41 |
|
| 42 |
/* |
| 43 |
Methods for reading and storing in machine independent |
| 44 |
format (low byte first). |
| 45 |
*/ |
| 46 |
|
| 47 |
/* |
| 48 |
Checking le16toh is required because the machine may have the header |
| 49 |
but the functions might not be defined if the version of glibc < 2.9 |
| 50 |
*/ |
| 51 |
#ifdef HAVE_ENDIAN_CONVERSION_MACROS |
| 52 |
#include <endian.h> |
| 53 |
#endif |
| 54 |
|
| 55 |
#if !defined(le16toh) |
| 56 |
/** |
| 57 |
Converting a 16 bit integer from little-endian byte order to host byteorder |
| 58 |
|
| 59 |
@param x 16-bit integer in little endian byte order |
| 60 |
@return 16-bit integer in host byte order |
| 61 |
*/ |
| 62 |
uint16_t inline le16toh(uint16_t x) { |
| 63 |
#ifndef WORDS_BIGENDIAN |
| 64 |
return x; |
| 65 |
#else |
| 66 |
return ((x >> 8) | (x << 8)); |
| 67 |
#endif |
| 68 |
} |
| 69 |
#endif |
| 70 |
|
| 71 |
#if !defined(le32toh) |
| 72 |
/** |
| 73 |
Converting a 32 bit integer from little-endian byte order to host byteorder |
| 74 |
|
| 75 |
@param x 32-bit integer in little endian byte order |
| 76 |
@return 32-bit integer in host byte order |
| 77 |
*/ |
| 78 |
uint32_t inline le32toh(uint32_t x) { |
| 79 |
#ifndef WORDS_BIGENDIAN |
| 80 |
return x; |
| 81 |
#else |
| 82 |
return (((x >> 24) & 0xff) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) | |
| 83 |
((x << 24) & 0xff000000)); |
| 84 |
#endif |
| 85 |
} |
| 86 |
#endif |
| 87 |
|
| 88 |
#if !defined(be32toh) |
| 89 |
/** |
| 90 |
Converting a 32 bit integer from big-endian byte order to host byteorder |
| 91 |
|
| 92 |
@param x 32-bit integer in big endian byte order |
| 93 |
@return 32-bit integer in host byte order |
| 94 |
*/ |
| 95 |
uint32_t inline be32toh(uint32_t x) { |
| 96 |
#ifndef WORDS_BIGENDIAN |
| 97 |
return (((x >> 24) & 0xff) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) | |
| 98 |
((x << 24) & 0xff000000)); |
| 99 |
#else |
| 100 |
return x; |
| 101 |
#endif |
| 102 |
} |
| 103 |
#endif |
| 104 |
|
| 105 |
#if !defined(le64toh) |
| 106 |
/** |
| 107 |
Converting a 64 bit integer from little-endian byte order to host byteorder |
| 108 |
|
| 109 |
@param x 64-bit integer in little endian byte order |
| 110 |
@return 64-bit integer in host byte order |
| 111 |
*/ |
| 112 |
uint64_t inline le64toh(uint64_t x) { |
| 113 |
#ifndef WORDS_BIGENDIAN |
| 114 |
return x; |
| 115 |
#else |
| 116 |
x = ((x << 8) & 0xff00ff00ff00ff00ULL) | ((x >> 8) & 0x00ff00ff00ff00ffULL); |
| 117 |
x = ((x << 16) & 0xffff0000ffff0000ULL) | ((x >> 16) & 0x0000ffff0000ffffULL); |
| 118 |
return (x << 32) | (x >> 32); |
| 119 |
#endif |
| 120 |
} |
| 121 |
#endif |
| 122 |
|
| 123 |
#endif // BYTEORDER_INCLUDED |