Skip to content

Commit 10c6f06

Browse files
asyntsawesomekling
authored andcommitted
AK: Add Endian.h header to replace NetworkOrdered.h.
1 parent ecf6cbb commit 10c6f06

File tree

20 files changed

+195
-106
lines changed

20 files changed

+195
-106
lines changed

AK/Endian.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright (c) 2020, the SerenityOS developers.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#pragma once
28+
29+
#include <AK/Platform.h>
30+
31+
namespace AK {
32+
33+
template<typename T>
34+
ALWAYS_INLINE T convert_between_host_and_little_endian(T value)
35+
{
36+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
37+
return value;
38+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
39+
if constexpr (sizeof(T) == 8)
40+
return __builtin_bswap64(value);
41+
if constexpr (sizeof(T) == 4)
42+
return __builtin_bswap32(value);
43+
if constexpr (sizeof(T) == 2)
44+
return __builtin_bswap16(value);
45+
if constexpr (sizeof(T) == 1)
46+
return value;
47+
#endif
48+
}
49+
50+
template<typename T>
51+
ALWAYS_INLINE T convert_between_host_and_big_endian(T value)
52+
{
53+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
54+
if constexpr (sizeof(T) == 8)
55+
return __builtin_bswap64(value);
56+
if constexpr (sizeof(T) == 4)
57+
return __builtin_bswap32(value);
58+
if constexpr (sizeof(T) == 2)
59+
return __builtin_bswap16(value);
60+
if constexpr (sizeof(T) == 1)
61+
return value;
62+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
63+
return value;
64+
#endif
65+
}
66+
67+
template<typename T>
68+
ALWAYS_INLINE T convert_between_host_and_network_endian(T value)
69+
{
70+
return convert_between_host_and_big_endian(value);
71+
}
72+
73+
template<typename T>
74+
class [[gnu::packed]] LittleEndian
75+
{
76+
public:
77+
LittleEndian() { }
78+
79+
LittleEndian(T value)
80+
: m_value(convert_between_host_and_little_endian(value))
81+
{
82+
}
83+
84+
operator T() const { return convert_between_host_and_little_endian(m_value); }
85+
86+
private:
87+
T m_value { 0 };
88+
};
89+
90+
template<typename T>
91+
class [[gnu::packed]] BigEndian
92+
{
93+
public:
94+
BigEndian() { }
95+
96+
BigEndian(T value)
97+
: m_value(convert_between_host_and_big_endian(value))
98+
{
99+
}
100+
101+
operator T() const { return convert_between_host_and_big_endian(m_value); }
102+
103+
private:
104+
T m_value { 0 };
105+
};
106+
107+
template<typename T>
108+
using NetworkOrdered = BigEndian<T>;
109+
110+
}
111+
112+
using AK::BigEndian;
113+
using AK::LittleEndian;
114+
using AK::NetworkOrdered;

AK/IPv4Address.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
#pragma once
2828

29+
#include <AK/Endian.h>
2930
#include <AK/LogStream.h>
30-
#include <AK/NetworkOrdered.h>
3131
#include <AK/Optional.h>
3232
#include <AK/String.h>
3333
#include <AK/StringView.h>
@@ -106,7 +106,6 @@ class [[gnu::packed]] IPv4Address
106106
return {};
107107
}
108108

109-
110109
if (a > 255 || b > 255 || c > 255 || d > 255)
111110
return {};
112111
return IPv4Address((u8)a, (u8)b, (u8)c, (u8)d);

AK/NetworkOrdered.h

Lines changed: 0 additions & 46 deletions
This file was deleted.

AK/Platform.h

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@
3737
#define ARCH(arch) (defined(AK_ARCH_##arch) && AK_ARCH_##arch)
3838

3939
#ifdef ALWAYS_INLINE
40-
#undef ALWAYS_INLINE
40+
# undef ALWAYS_INLINE
4141
#endif
4242
#define ALWAYS_INLINE [[gnu::always_inline]] inline
4343

4444
#ifdef NEVER_INLINE
45-
#undef NEVER_INLINE
45+
# undef NEVER_INLINE
4646
#endif
4747
#define NEVER_INLINE [[gnu::noinline]]
4848

4949
#ifdef FLATTEN
50-
#undef FLATTEN
50+
# undef FLATTEN
5151
#endif
5252
#define FLATTEN [[gnu::flatten]]
5353

@@ -71,33 +71,16 @@ inline int open_with_path_length(const char* path, size_t path_length, int optio
7171
}
7272
#endif
7373

74-
template<typename T>
75-
ALWAYS_INLINE T convert_between_host_and_network(T value)
76-
{
77-
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
78-
if constexpr (sizeof(T) == 8)
79-
return __builtin_bswap64(value);
80-
if constexpr (sizeof(T) == 4)
81-
return __builtin_bswap32(value);
82-
if constexpr (sizeof(T) == 2)
83-
return __builtin_bswap16(value);
84-
if constexpr (sizeof(T) == 1)
85-
return value;
86-
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
87-
return value;
88-
#endif
89-
}
90-
9174
ALWAYS_INLINE int count_trailing_zeroes_32(unsigned int val)
9275
{
9376
#if defined(__GNUC__) || defined(__clang__)
94-
return __builtin_ctz(val);
77+
return __builtin_ctz(val);
9578
#else
96-
for (u8 i = 0; i < 32; ++i) {
97-
if ((val >> i) & 1) {
98-
return i;
99-
}
79+
for (u8 i = 0; i < 32; ++i) {
80+
if ((val >> i) & 1) {
81+
return i;
10082
}
101-
return 0;
83+
}
84+
return 0;
10285
#endif
10386
}

AK/Stream.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <AK/ByteBuffer.h>
3030
#include <AK/Concepts.h>
31+
#include <AK/Endian.h>
3132
#include <AK/Forward.h>
3233
#include <AK/MemMem.h>
3334
#include <AK/Span.h>
@@ -74,6 +75,35 @@ class DuplexStream
7475
, public OutputStream {
7576
};
7677

78+
template<typename T>
79+
InputStream& operator>>(InputStream& stream, LittleEndian<T>& value)
80+
{
81+
T temporary;
82+
stream >> temporary;
83+
value = temporary;
84+
return stream;
85+
}
86+
template<typename T>
87+
InputStream& operator<<(InputStream& stream, LittleEndian<T> value)
88+
{
89+
stream << static_cast<T>(value);
90+
return stream;
91+
}
92+
template<typename T>
93+
InputStream& operator>>(InputStream& stream, BigEndian<T>& value)
94+
{
95+
T temporary;
96+
stream >> temporary;
97+
value = temporary;
98+
return stream;
99+
}
100+
template<typename T>
101+
InputStream& operator<<(InputStream& stream, BigEndian<T> value)
102+
{
103+
stream << static_cast<T>(value);
104+
return stream;
105+
}
106+
77107
#if defined(__cpp_concepts) && !defined(__COVERITY__)
78108
template<Concepts::Integral Integral>
79109
#else

Kernel/Net/ARP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
#pragma once
2828

29+
#include <AK/Endian.h>
2930
#include <AK/MACAddress.h>
30-
#include <AK/NetworkOrdered.h>
3131
#include <Kernel/Net/EtherType.h>
3232
#include <Kernel/Net/IPv4.h>
3333

Kernel/Net/EthernetFrameHeader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626

2727
#pragma once
2828

29+
#include <AK/Endian.h>
2930
#include <AK/MACAddress.h>
30-
#include <AK/NetworkOrdered.h>
3131

3232
#pragma GCC diagnostic ignored "-Warray-bounds"
3333

3434
class [[gnu::packed]] EthernetFrameHeader
3535
{
3636
public:
37-
EthernetFrameHeader() {}
38-
~EthernetFrameHeader() {}
37+
EthernetFrameHeader() { }
38+
~EthernetFrameHeader() { }
3939

4040
MACAddress destination() const { return m_destination; }
4141
void set_destination(const MACAddress& address) { m_destination = address; }

Kernel/Net/IPv4.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
#pragma once
2828

2929
#include <AK/Assertions.h>
30+
#include <AK/Endian.h>
3031
#include <AK/IPv4Address.h>
31-
#include <AK/NetworkOrdered.h>
3232
#include <AK/String.h>
3333
#include <AK/Types.h>
3434

@@ -131,7 +131,7 @@ inline NetworkOrdered<u16> internet_checksum(const void* ptr, size_t count)
131131
u32 checksum = 0;
132132
auto* w = (const u16*)ptr;
133133
while (count > 1) {
134-
checksum += convert_between_host_and_network(*w++);
134+
checksum += AK::convert_between_host_and_network_endian(*w++);
135135
if (checksum & 0x80000000)
136136
checksum = (checksum & 0xffff) | (checksum >> 16);
137137
count -= 2;

Libraries/LibCrypto/Hash/SHA1.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
*/
2626

27+
#include <AK/Endian.h>
2728
#include <AK/Types.h>
2829
#include <LibCrypto/Hash/SHA1.h>
2930

@@ -39,7 +40,7 @@ inline void SHA1::transform(const u8* data)
3940
{
4041
u32 blocks[80];
4142
for (size_t i = 0; i < 16; ++i)
42-
blocks[i] = convert_between_host_and_network(((const u32*)data)[i]);
43+
blocks[i] = AK::convert_between_host_and_network_endian(((const u32*)data)[i]);
4344

4445
// w[i] = (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1
4546
for (size_t i = 16; i < Rounds; ++i)

Libraries/LibGfx/PBMLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
*/
2626

2727
#include "PBMLoader.h"
28+
#include <AK/Endian.h>
2829
#include <AK/LexicalPath.h>
2930
#include <AK/MappedFile.h>
30-
#include <AK/NetworkOrdered.h>
3131
#include <AK/StringBuilder.h>
3232
#include <string.h>
3333

0 commit comments

Comments
 (0)