fix base64 test failed.
Some checks failed
linux-arm-gcc / linux-gcc-armhf (push) Failing after 3m21s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 2m11s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 1m58s
linux-arm-gcc / linux-gcc-arm (push) Failing after 4m41s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 1m53s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m42s

This commit is contained in:
tqcq 2024-04-01 10:47:59 +08:00
parent 8af0855224
commit 21e6fd80f7
4 changed files with 80 additions and 81 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
out/
build/
compile_commands.json
build-*/

View File

@ -19,9 +19,9 @@ template<typename T, typename U>
struct BufferCompat {
using RawU = typename std::remove_const<U>::type;
static constexpr bool value = !std::is_volatile<U>::value
&& ((std::is_integral<T>::value && sizeof(T) == 1)
? (std::is_integral<U>::value && sizeof(U) == 1)
static constexpr bool value
= !std::is_volatile<U>::value
&& ((std::is_integral<T>::value && sizeof(T) == 1) ? (std::is_integral<U>::value && sizeof(U) == 1)
: (std::is_same<T, RawU>::value)
);
@ -42,10 +42,7 @@ public:
BufferT(const BufferT &) = delete;
BufferT &operator=(const BufferT &) = delete;
BufferT(BufferT &&buf)
: size_(buf.size()),
capacity_(buf.capacity()),
data_(std::move(buf.data_))
BufferT(BufferT &&buf) : size_(buf.size()), capacity_(buf.capacity()), data_(std::move(buf.data_))
{
IsConsistent();
}
@ -60,41 +57,30 @@ public:
IsConsistent();
}
template<typename U,
typename std::enable_if<internal::BufferCompat<T, U>::value>::type
* = nullptr>
template<typename U, typename std::enable_if<internal::BufferCompat<T, U>::value>::type * = nullptr>
BufferT(const U *data, size_t size) : BufferT(data, size, size)
{}
template<typename U,
typename std::enable_if<internal::BufferCompat<T, U>::value>::type
* = nullptr>
template<typename U, typename std::enable_if<internal::BufferCompat<T, U>::value>::type * = nullptr>
BufferT(U *data, size_t size, size_t capacity) : BufferT(size, capacity)
{
static_assert(sizeof(T) == sizeof(U), "");
if (size > 0) { std::memcpy(data_.get(), data, size * sizeof(U)); }
}
template<typename U,
size_t N,
typename std::enable_if<internal::BufferCompat<T, U>::value>::type
* = nullptr>
template<typename U, size_t N, typename std::enable_if<internal::BufferCompat<T, U>::value>::type * = nullptr>
BufferT(U (&array)[N]) : BufferT(array, N)
{}
~BufferT() {}
template<typename U,
typename std::enable_if<internal::BufferCompat<T, U>::value>::type
* = nullptr>
template<typename U = T, typename std::enable_if<internal::BufferCompat<T, U>::value>::type * = nullptr>
const U *data() const
{
return reinterpret_cast<const U *>(data_.get());
}
template<typename U,
typename std::enable_if<internal::BufferCompat<T, U>::value>::type
* = nullptr>
template<typename U = T, typename std::enable_if<internal::BufferCompat<T, U>::value>::type * = nullptr>
U *data()
{
return reinterpret_cast<U *>(data_.get());
@ -120,10 +106,7 @@ public:
bool operator==(const BufferT &buf) const
{
if (size_ != buf.size_) { return false; }
if (std::is_integral<T>::value) {
return std::memcmp(data_.get(), buf.data_.get(), size_ * sizeof(T))
== 0;
}
if (std::is_integral<T>::value) { return std::memcmp(data_.get(), buf.data_.get(), size_ * sizeof(T)) == 0; }
for (size_t i = 0; i < size_; i++) {
if (data_[i] != buf.data_[i]) { return false; }
}
@ -148,32 +131,23 @@ public:
const T *cend() const { return data() + size(); }
template<typename U,
typename std::enable_if<internal::BufferCompat<T, U>::value>::type
* = nullptr>
template<typename U, typename std::enable_if<internal::BufferCompat<T, U>::value>::type * = nullptr>
void SetData(const U *data, size_t size)
{
const size_t old_size = size_;
size_ = 0;
AppentData(data, size);
if (ZeroOnFree && size_ < old_size) {
ZeroTrailingData(old_size - size_);
}
if (ZeroOnFree && size_ < old_size) { ZeroTrailingData(old_size - size_); }
}
template<typename U,
size_t N,
typename std::enable_if<internal::BufferCompat<T, U>::value>::type
* = nullptr>
template<typename U, size_t N, typename std::enable_if<internal::BufferCompat<T, U>::value>::type * = nullptr>
void SetData(const U (&array)[N])
{
SetData(array, N);
}
template<typename U,
typename std::enable_if<internal::BufferCompat<T, U>::value>::type
* = nullptr>
void AppentData(const U *data, size_t size)
template<typename U, typename std::enable_if<internal::BufferCompat<T, U>::value>::type * = nullptr>
void AppendData(const U *data, size_t size)
{
if (size == 0) { return; }
@ -184,10 +158,7 @@ public:
size_ = new_size;
}
template<typename U,
size_t N,
typename std::enable_if<internal::BufferCompat<T, U>::value>::type
* = nullptr>
template<typename U, size_t N, typename std::enable_if<internal::BufferCompat<T, U>::value>::type * = nullptr>
void AppendData(const U (&array)[N])
{
AppendData(array, N);
@ -201,9 +172,7 @@ public:
// AppendData(w.data(), w.size());
// }
template<typename U,
typename std::enable_if<internal::BufferCompat<T, U>::value>::type
* = nullptr>
template<typename U, typename std::enable_if<internal::BufferCompat<T, U>::value>::type * = nullptr>
void AppendData(const U &item)
{
AppendData(&item, 1);
@ -214,15 +183,10 @@ public:
const size_t old_size = size_;
EnsureCapacityWithHeadroom(size, true);
size_ = size;
if (ZeroOnFree && size_ < old_size) {
ZeroTrailingData(old_size - size_);
}
if (ZeroOnFree && size_ < old_size) { ZeroTrailingData(old_size - size_); }
}
void EnsureCapacity(size_t capacity)
{
EnsureCapacityWithHeadroom(capacity, false);
}
void EnsureCapacity(size_t capacity) { EnsureCapacityWithHeadroom(capacity, false); }
void Clear() { size_ = 0; }
@ -239,13 +203,9 @@ private:
{
if (capacity <= capacity_) { return; }
const size_t new_capacity = extra_headroom
? std::max(capacity, capacity_ + capacity_ / 2)
: capacity;
const size_t new_capacity = extra_headroom ? std::max(capacity, capacity_ + capacity_ / 2) : capacity;
std::unique_ptr<T[]> new_data(new T[new_capacity]);
if (data_ != nullptr) {
std::memcpy(new_data.get(), data_.get(), size_ * sizeof(T));
}
if (data_ != nullptr) { std::memcpy(new_data.get(), data_.get(), size_ * sizeof(T)); }
data_ = std::move(new_data);
capacity_ = new_capacity;
@ -253,10 +213,7 @@ private:
void ZeroTrailingData(size_t count) {}
bool IsConsistent() const
{
return (data_ || capacity_ == 0) && capacity_ >= size_;
}
bool IsConsistent() const { return (data_ || capacity_ == 0) && capacity_ >= size_; }
void OnMovedFrom()
{

View File

@ -1,4 +1,5 @@
#include "sled/strings/base64.h"
#include "sled/buffer.h"
#include "sled/log/log.h"
#include "sled/synchronization/call_once.h"
#include <array>
@ -108,24 +109,36 @@ Base64::Decode(const uint8_t *ptr, size_t len)
{
if (len == 0) { return std::string(); }
// FIXME: 修复快速解码在arm平台错误问题
/*
base64_decodestate state;
base64_init_decodestate(&state);
std::stringstream ss;
sled::BufferT<char> buffer;
char plaintext[kBufferSize];
int codelength = 0;
int plainlength = 0;
do {
codelength = std::min(kBufferSize, static_cast<int>(len));
// 如果长度不足4需要填充=
if (codelength < 4) {
sled::BufferT<char> temp(reinterpret_cast<const char *>(ptr), codelength);
while (temp.size() % 4) { temp.AppendData('='); }
plainlength = base64_decode_block(temp.data(), temp.size(), plaintext, &state);
} else {
// 如果长度不足4则将这部分留到下一次处理
codelength -= codelength % 4;
plainlength = base64_decode_block(reinterpret_cast<const char *>(ptr), codelength, plaintext, &state);
ss.write(plaintext, plainlength);
}
buffer.AppendData(static_cast<const char *>(plaintext), plainlength);
ptr += codelength;
len -= codelength;
} while (len > 0 && codelength > 0);
return ss.str();
/*
return std::string(buffer.data(), buffer.data() + buffer.size());
*/
CallOnce(once_flag, [&] {
std::fill(kInvBase64Chars.begin(), kInvBase64Chars.end(), -1);
@ -161,8 +174,7 @@ Base64::Decode(const uint8_t *ptr, size_t len)
}
while (write_idx < data.size()) data.pop_back();
return make_status_or<std::string>(data);
*/
return MakeStatusOr<std::string>(data);
}
}// namespace sled

View File

@ -0,0 +1,29 @@
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_C_COMPILER "aarch64-linux-gnu-gcc")
set(CMAKE_CXX_COMPILER "aarch64-linux-gnu-g++")
if(NOT CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
endif()
if(NOT CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
endif()
if(NOT CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
endif()
if(NOT CMAKE_FIND_ROOT_PATH_MODE_PACKAGE)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
endif()
set(CMAKE_C_FLAGS "-march=armv8-a")
set(CMAKE_CXX_FLAGS "-march=armv8-a")
# cache flags
set(CMAKE_C_FLAGS
"${CMAKE_C_FLAGS}"
CACHE STRING "c flags")
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS}"
CACHE STRING "c++ flags")