[Support] Reduce Dependence on Host.h

The intention behind this commit is to reduce the use of Host.h/Host.cpp
in Support, to where it is only necessary.

In this case, the endian-detection and support functionality needed by
these implementations can be provided by `Support/SwapByteOrder.h` in a
cleaner manner.

This patch also changes the byte swap in SHA256.cpp to use the byte swap
function from that header, rather than an inlined implementation.

Differential Revision: https://reviews.llvm.org/D137834
This commit is contained in:
Archibald Elliott 2022-11-21 18:26:49 +00:00
parent ac93b61165
commit 53d234eab6
4 changed files with 31 additions and 48 deletions

View File

@ -17,7 +17,6 @@
#include "llvm/Support/Allocator.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>

View File

@ -15,8 +15,8 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/SwapByteOrder.h"
#include <cassert>
#include <cstring>
using namespace llvm;

View File

@ -18,15 +18,11 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/SwapByteOrder.h"
#include <string.h>
using namespace llvm;
#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
#define SHA_BIG_ENDIAN
#endif
static inline uint32_t rol(uint32_t Number, int Bits) {
return (Number << Bits) | (Number >> (32 - Bits));
}
@ -192,11 +188,10 @@ void SHA1::hashBlock() {
}
void SHA1::addUncounted(uint8_t Data) {
#ifdef SHA_BIG_ENDIAN
if constexpr (sys::IsBigEndianHost)
InternalState.Buffer.C[InternalState.BufferOffset] = Data;
#else
else
InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
#endif
InternalState.BufferOffset++;
if (InternalState.BufferOffset == BLOCK_LENGTH) {
@ -267,20 +262,17 @@ void SHA1::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) {
// Pad to complete the last block
pad();
#ifdef SHA_BIG_ENDIAN
if constexpr (sys::IsBigEndianHost) {
// Just copy the current state
for (int i = 0; i < 5; i++) {
HashResult[i] = InternalState.State[i];
}
#else
} else {
// Swap byte order back
for (int i = 0; i < 5; i++) {
HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
(((InternalState.State[i]) << 8) & 0x00ff0000) |
(((InternalState.State[i]) >> 8) & 0x0000ff00) |
(((InternalState.State[i]) >> 24) & 0x000000ff);
HashResult[i] = sys::getSwappedBytes(InternalState.State[i]);
}
}
#endif
}
std::array<uint8_t, 20> SHA1::final() {

View File

@ -23,15 +23,11 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/SwapByteOrder.h"
#include <string.h>
namespace llvm {
#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
#define SHA_BIG_ENDIAN
#endif
#define SHR(x, c) ((x) >> (c))
#define ROTR(x, n) (((x) >> n) | ((x) << (32 - (n))))
@ -171,11 +167,10 @@ void SHA256::hashBlock() {
}
void SHA256::addUncounted(uint8_t Data) {
#ifdef SHA_BIG_ENDIAN
if constexpr (sys::IsBigEndianHost)
InternalState.Buffer.C[InternalState.BufferOffset] = Data;
#else
else
InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
#endif
InternalState.BufferOffset++;
if (InternalState.BufferOffset == BLOCK_LENGTH) {
@ -247,20 +242,17 @@ void SHA256::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) {
// Pad to complete the last block
pad();
#ifdef SHA_BIG_ENDIAN
if constexpr (sys::IsBigEndianHost) {
// Just copy the current state
for (int i = 0; i < 8; i++) {
HashResult[i] = InternalState.State[i];
}
#else
} else {
// Swap byte order back
for (int i = 0; i < 8; i++) {
HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
(((InternalState.State[i]) << 8) & 0x00ff0000) |
(((InternalState.State[i]) >> 8) & 0x0000ff00) |
(((InternalState.State[i]) >> 24) & 0x000000ff);
HashResult[i] = sys::getSwappedBytes(InternalState.State[i]);
}
}
#endif
}
std::array<uint8_t, 32> SHA256::final() {