From 21e6fd80f7957f76b71d65e3719d1a42e4510059 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Mon, 1 Apr 2024 10:47:59 +0800 Subject: [PATCH] fix base64 test failed. --- .gitignore | 1 + src/sled/buffer.h | 103 ++++++------------- src/sled/strings/base64.cc | 28 +++-- toolchains/aarch64-linux-gnu.toolchain.cmake | 29 ++++++ 4 files changed, 80 insertions(+), 81 deletions(-) create mode 100644 toolchains/aarch64-linux-gnu.toolchain.cmake diff --git a/.gitignore b/.gitignore index c7e98b2..dfdfe3e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ out/ build/ compile_commands.json +build-*/ diff --git a/src/sled/buffer.h b/src/sled/buffer.h index ee03a1a..dd4d6e0 100644 --- a/src/sled/buffer.h +++ b/src/sled/buffer.h @@ -19,12 +19,12 @@ template struct BufferCompat { using RawU = typename std::remove_const::type; - static constexpr bool value = !std::is_volatile::value - && ((std::is_integral::value && sizeof(T) == 1) - ? (std::is_integral::value && sizeof(U) == 1) - : (std::is_same::value) + static constexpr bool value + = !std::is_volatile::value + && ((std::is_integral::value && sizeof(T) == 1) ? (std::is_integral::value && sizeof(U) == 1) + : (std::is_same::value) - ); + ); }; }// namespace internal @@ -34,18 +34,15 @@ class BufferT { static_assert(!std::is_const::value, "T may not be const"); public: - using value_type = T; + using value_type = T; using const_iterator = const T *; BufferT() : size_(0), capacity_(0), data_(nullptr) { IsConsistent(); } - BufferT(const BufferT &) = delete; + 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::value>::type - * = nullptr> + template::value>::type * = nullptr> BufferT(const U *data, size_t size) : BufferT(data, size, size) {} - template::value>::type - * = nullptr> + template::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::value>::type - * = nullptr> + template::value>::type * = nullptr> BufferT(U (&array)[N]) : BufferT(array, N) {} ~BufferT() {} - template::value>::type - * = nullptr> + template::value>::type * = nullptr> const U *data() const { return reinterpret_cast(data_.get()); } - template::value>::type - * = nullptr> + template::value>::type * = nullptr> U *data() { return reinterpret_cast(data_.get()); @@ -108,7 +94,7 @@ public: BufferT &operator=(BufferT &&buf) { - size_ = buf.size_; + size_ = buf.size_; capacity_ = buf.capacity_; using std::swap; swap(data_, buf.data_); @@ -120,10 +106,7 @@ public: bool operator==(const BufferT &buf) const { if (size_ != buf.size_) { return false; } - if (std::is_integral::value) { - return std::memcmp(data_.get(), buf.data_.get(), size_ * sizeof(T)) - == 0; - } + if (std::is_integral::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::value>::type - * = nullptr> + template::value>::type * = nullptr> void SetData(const U *data, size_t size) { const size_t old_size = size_; - size_ = 0; + size_ = 0; AppentData(data, size); - if (ZeroOnFree && size_ < old_size) { - ZeroTrailingData(old_size - size_); - } + if (ZeroOnFree && size_ < old_size) { ZeroTrailingData(old_size - size_); } } - template::value>::type - * = nullptr> + template::value>::type * = nullptr> void SetData(const U (&array)[N]) { SetData(array, N); } - template::value>::type - * = nullptr> - void AppentData(const U *data, size_t size) + template::value>::type * = nullptr> + void AppendData(const U *data, size_t size) { if (size == 0) { return; } @@ -184,10 +158,7 @@ public: size_ = new_size; } - template::value>::type - * = nullptr> + template::value>::type * = nullptr> void AppendData(const U (&array)[N]) { AppendData(array, N); @@ -201,9 +172,7 @@ public: // AppendData(w.data(), w.size()); // } - template::value>::type - * = nullptr> + template::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,28 +203,21 @@ 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 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); + data_ = std::move(new_data); capacity_ = new_capacity; } 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() { - size_ = 0; + size_ = 0; capacity_ = 0; } diff --git a/src/sled/strings/base64.cc b/src/sled/strings/base64.cc index 1724671..6173158 100644 --- a/src/sled/strings/base64.cc +++ b/src/sled/strings/base64.cc @@ -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 @@ -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 buffer; + char plaintext[kBufferSize]; int codelength = 0; int plainlength = 0; do { - codelength = std::min(kBufferSize, static_cast(len)); - plainlength = base64_decode_block(reinterpret_cast(ptr), codelength, plaintext, &state); - ss.write(plaintext, plainlength); + codelength = std::min(kBufferSize, static_cast(len)); + // 如果长度不足4,需要填充= + if (codelength < 4) { + sled::BufferT temp(reinterpret_cast(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(ptr), codelength, plaintext, &state); + } + buffer.AppendData(static_cast(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(data); - */ + return MakeStatusOr(data); } }// namespace sled diff --git a/toolchains/aarch64-linux-gnu.toolchain.cmake b/toolchains/aarch64-linux-gnu.toolchain.cmake new file mode 100644 index 0000000..bab72a9 --- /dev/null +++ b/toolchains/aarch64-linux-gnu.toolchain.cmake @@ -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")