MySQL 8.3.0
Source Code Documentation
byteorder.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/**
24 @file mysql/binlog/event/byteorder.h
25
26 @brief The file contains functions to convert the byte encoding of integer
27 values to and from little-endian and big-endian byte order.
28*/
29
30#ifndef MYSQL_BINLOG_EVENT_BYTEORDER_H
31#define MYSQL_BINLOG_EVENT_BYTEORDER_H
32
33#include <stdint.h>
34#include "my_compiler.h"
35#include "my_config.h"
37#ifndef STANDALONE_BINLOG
38#define HAVE_MYSYS 1
39#endif
40
41/*
42 Methods for reading and storing in machine independent
43 format (low byte first).
44*/
45
46/*
47 Checking le16toh is required because the machine may have the header
48 but the functions might not be defined if the version of glibc < 2.9
49*/
50#ifdef HAVE_ENDIAN_CONVERSION_MACROS
51#include <endian.h>
52#endif
53
54#if !defined(le16toh)
55/**
56 Converting a 16 bit integer from little-endian byte order to host byteorder
57
58 @param x 16-bit integer in little endian byte order
59 @return 16-bit integer in host byte order
60*/
61uint16_t inline le16toh(uint16_t x) {
62#ifndef WORDS_BIGENDIAN
63 return x;
64#else
65 return ((x >> 8) | (x << 8));
66#endif
67}
68#endif
69
70#if !defined(le32toh)
71/**
72 Converting a 32 bit integer from little-endian byte order to host byteorder
73
74 @param x 32-bit integer in little endian byte order
75 @return 32-bit integer in host byte order
76*/
77uint32_t inline le32toh(uint32_t x) {
78#ifndef WORDS_BIGENDIAN
79 return x;
80#else
81 return (((x >> 24) & 0xff) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
82 ((x << 24) & 0xff000000));
83#endif
84}
85#endif
86
87#if !defined(be32toh)
88/**
89 Converting a 32 bit integer from big-endian byte order to host byteorder
90
91 @param x 32-bit integer in big endian byte order
92 @return 32-bit integer in host byte order
93*/
94uint32_t inline be32toh(uint32_t x) {
95#ifndef WORDS_BIGENDIAN
96 return (((x >> 24) & 0xff) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) |
97 ((x << 24) & 0xff000000));
98#else
99 return x;
100#endif
101}
102#endif
103
104#endif // MYSQL_BINLOG_EVENT_BYTEORDER_H
Experimental API header Conversions between different number representations.
Include only the necessary part of Sun RPC for Windows builds.
uint16_t le16toh(uint16_t x)
Converting a 16 bit integer from little-endian byte order to host byteorder.
Definition: byteorder.h:61
uint32_t le32toh(uint32_t x)
Converting a 32 bit integer from little-endian byte order to host byteorder.
Definition: byteorder.h:77
uint32_t be32toh(uint32_t x)
Converting a 32 bit integer from big-endian byte order to host byteorder.
Definition: byteorder.h:94
Header for compiler-dependent features.