Compare commits

..

1 Commits

Author SHA1 Message Date
MechaCat02
77c78fad94 crossbuild: target-aware POSIX gate in generated headers (Sylpheed fork)
Some checks failed
ci / CI ubuntu-latest avx clang debug (push) Failing after 17s
ci / CI ubuntu-latest baseline clang debug (push) Failing after 8s
ci / CI ubuntu-latest baseline clang release (push) Failing after 9s
ci / CI ubuntu-latest avx gcc debug (push) Failing after 8s
ci / CI ubuntu-latest avx gcc release (push) Failing after 8s
ci / CI ubuntu-latest avx2 gcc debug (push) Failing after 10s
ci / CI ubuntu-latest avx2 gcc release (push) Failing after 8s
ci / CI ubuntu-latest baseline gcc debug (push) Failing after 8s
ci / CI ubuntu-latest baseline gcc release (push) Failing after 8s
ci / CI ubuntu-latest avx clang release (push) Failing after 9s
ci / CI ubuntu-latest avx2 clang debug (push) Failing after 8s
ci / CI ubuntu-latest avx2 clang release (push) Failing after 8s
ci / CI windows-latest avx msvc debug (push) Has been cancelled
ci / CI windows-latest avx msvc release (push) Has been cancelled
ci / CI windows-latest avx2 msvc debug (push) Has been cancelled
ci / CI windows-latest avx2 msvc release (push) Has been cancelled
ci / CI windows-latest baseline msvc debug (push) Has been cancelled
ci / CI windows-latest baseline msvc release (push) Has been cancelled
ci / CI macos-latest avx clang debug (push) Has been cancelled
ci / CI macos-latest avx clang release (push) Has been cancelled
ci / CI macos-latest baseline clang debug (push) Has been cancelled
ci / CI macos-latest baseline clang release (push) Has been cancelled
Pre-generated snappy-stubs-public.h / config.h baked HAVE_SYS_UIO_H=1 from
a Linux cmake host, breaking clang-cl cross-compiles to Windows. Gate the
sys/uio.h include on !_WIN32 so the same checked-in headers build on both
hosts. See xenia-rs docs/CROSS_BUILD_SETUP.md §7.3.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-05 07:22:08 +02:00
16 changed files with 293 additions and 53 deletions

View File

@@ -1,2 +1,4 @@
# googletest requires C++14 or above # googletest requires C++14 or above
build --cxxopt='-std=c++17' build --cxxopt='-std=c++17'
# Enable Bzlmod for every Bazel command
common --enable_bzlmod

1
.gitignore vendored
View File

@@ -6,4 +6,5 @@
# Build directory. # Build directory.
build/ build/
/bazel-* /bazel-*
MODULE.bazel.lock
out/ out/

View File

@@ -30,7 +30,7 @@ package(default_visibility = ["//visibility:public"])
licenses(["notice"]) licenses(["notice"])
SNAPPY_VERSION = (1, 1, 10) SNAPPY_VERSION = (1, 2, 2)
config_setting( config_setting(
name = "windows", name = "windows",
@@ -40,7 +40,7 @@ config_setting(
cc_library( cc_library(
name = "config", name = "config",
hdrs = ["config.h"], hdrs = ["config.h"],
defines = ["HAVE_CONFIG_H"] defines = ["HAVE_CONFIG_H"],
) )
cc_library( cc_library(
@@ -70,10 +70,11 @@ cc_library(
"snappy-sinksource.h", "snappy-sinksource.h",
], ],
copts = select({ copts = select({
":windows": [], ":windows": [],
"//conditions:default": [ "//conditions:default": [
"-Wno-sign-compare", "-Wno-sign-compare",
]}), ],
}),
deps = [ deps = [
":config", ":config",
":snappy-stubs-internal", ":snappy-stubs-internal",
@@ -114,7 +115,7 @@ cc_test(
deps = [ deps = [
":snappy", ":snappy",
":snappy-test", ":snappy-test",
"//third_party/benchmark:benchmark_main", "@com_google_benchmark//:benchmark_main",
], ],
) )
@@ -127,7 +128,7 @@ cc_test(
deps = [ deps = [
":snappy", ":snappy",
":snappy-test", ":snappy-test",
"//third_party/googletest:gtest_main", "@com_google_googletest//:gtest_main",
], ],
) )

View File

@@ -26,8 +26,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.10)
project(Snappy VERSION 1.1.10 LANGUAGES C CXX) project(Snappy VERSION 1.2.2 LANGUAGES C CXX)
# C++ standard can be overridden when this is used as a sub-project. # C++ standard can be overridden when this is used as a sub-project.
if(NOT CMAKE_CXX_STANDARD) if(NOT CMAKE_CXX_STANDARD)
@@ -38,7 +38,7 @@ if(NOT CMAKE_CXX_STANDARD)
endif(NOT CMAKE_CXX_STANDARD) endif(NOT CMAKE_CXX_STANDARD)
# https://github.com/izenecloud/cmake/blob/master/SetCompilerWarningAll.cmake # https://github.com/izenecloud/cmake/blob/master/SetCompilerWarningAll.cmake
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") if(MSVC)
# Use the highest warning level for Visual Studio. # Use the highest warning level for Visual Studio.
set(CMAKE_CXX_WARNING_LEVEL 4) set(CMAKE_CXX_WARNING_LEVEL 4)
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
@@ -55,7 +55,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# Disable RTTI. # Disable RTTI.
string(REGEX REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") string(REGEX REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")
else(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") else(MSVC)
# Use -Wall for clang and gcc. # Use -Wall for clang and gcc.
if(NOT CMAKE_CXX_FLAGS MATCHES "-Wall") if(NOT CMAKE_CXX_FLAGS MATCHES "-Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
@@ -85,7 +85,7 @@ else(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# Disable RTTI. # Disable RTTI.
string(REGEX REPLACE "-frtti" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") string(REGEX REPLACE "-frtti" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") endif(MSVC)
# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to make # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to make
# it prominent in the GUI. # it prominent in the GUI.
@@ -206,10 +206,13 @@ int main() {
check_cxx_source_compiles(" check_cxx_source_compiles("
#include <arm_neon.h> #include <arm_neon.h>
#include <stdint.h>
int main() { int main() {
uint8_t val = 3, dup[8]; uint8_t val = 3, dup[8];
uint8x16_t v = vld1q_dup_u8(&val); uint8x16_t v1 = vld1q_dup_u8(&val);
vst1q_u8(dup, v); uint8x16_t v2 = vqtbl1q_u8(v1, v1);
vst1q_u8(dup, v1);
vst1q_u8(dup, v2);
return 0; return 0;
}" SNAPPY_HAVE_NEON) }" SNAPPY_HAVE_NEON)
@@ -258,9 +261,7 @@ target_sources(snappy
"snappy-stubs-internal.cc" "snappy-stubs-internal.cc"
"snappy.cc" "snappy.cc"
"${PROJECT_BINARY_DIR}/config.h" "${PROJECT_BINARY_DIR}/config.h"
PUBLIC
# Only CMake 3.3+ supports PUBLIC sources in targets exported by "install".
$<$<VERSION_GREATER:CMAKE_VERSION,3.2>:PUBLIC>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/snappy-c.h> $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/snappy-c.h>
$<INSTALL_INTERFACE:include/snappy-c.h> $<INSTALL_INTERFACE:include/snappy-c.h>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/snappy-sinksource.h> $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/snappy-sinksource.h>
@@ -297,6 +298,9 @@ if(SNAPPY_BUILD_TESTS OR SNAPPY_BUILD_BENCHMARKS)
# Test files include snappy-test.h, HAVE_CONFIG_H must be defined. # Test files include snappy-test.h, HAVE_CONFIG_H must be defined.
target_compile_definitions(snappy_test_support PUBLIC -DHAVE_CONFIG_H) target_compile_definitions(snappy_test_support PUBLIC -DHAVE_CONFIG_H)
if(BUILD_SHARED_LIBS)
set_target_properties(snappy_test_support PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif(BUILD_SHARED_LIBS)
target_link_libraries(snappy_test_support snappy) target_link_libraries(snappy_test_support snappy)

23
MODULE.bazel Normal file
View File

@@ -0,0 +1,23 @@
module(
name = "snappy",
version = "1.2.2",
compatibility_level = 1,
)
bazel_dep(
name = "googletest",
version = "1.14.0.bcr.1",
dev_dependency = True,
repo_name = "com_google_googletest",
)
bazel_dep(
name = "google_benchmark",
version = "1.9.0",
dev_dependency = True,
repo_name = "com_google_benchmark",
)
bazel_dep(
name = "platforms",
version = "0.0.9",
)

15
NEWS
View File

@@ -1,3 +1,18 @@
Snappy v1.2.2, Mar 26th 2025:
* We added a new compression level in v1.2.1 which compresses a bit
denser but slower. Decompression speed should be even faster with it.
* We fixed a very old issue of data corruption when compressed size
exceeds 4GB. This can happen when you compress data close to 4GB
and it's incompressible, for example, random data.
* Started to use minimum CMake 3.10 because older ones are not
planned to be supported.
* Various other small fixes and performance improvements (especially
for clang).
Snappy v1.1.10, Mar 8th 2023: Snappy v1.1.10, Mar 8th 2023:
* Performance improvements * Performance improvements

View File

@@ -140,10 +140,10 @@ explicitly supports the following:
1. C++11 1. C++11
2. Clang (gcc and MSVC are best-effort). 2. Clang (gcc and MSVC are best-effort).
3. Low level optimizations (e.g. assembly or equivalent intrinsics) for: 3. Low level optimizations (e.g. assembly or equivalent intrinsics) for:
1. [x86](https://en.wikipedia.org/wiki/X86) - [x86](https://en.wikipedia.org/wiki/X86)
2. [x86-64](https://en.wikipedia.org/wiki/X86-64) - [x86-64](https://en.wikipedia.org/wiki/X86-64)
3. ARMv7 (32-bit) - ARMv7 (32-bit)
4. ARMv8 (AArch64) - ARMv8 (AArch64)
4. Supports only the Snappy compression scheme as described in 4. Supports only the Snappy compression scheme as described in
[format_description.txt](format_description.txt). [format_description.txt](format_description.txt).
5. CMake for building 5. CMake for building

0
WORKSPACE.bzlmod Normal file
View File

69
config.h Normal file
View File

@@ -0,0 +1,69 @@
#ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_CMAKE_CONFIG_H_
#define THIRD_PARTY_SNAPPY_OPENSOURCE_CMAKE_CONFIG_H_
/* Define to 1 if the compiler supports __attribute__((always_inline)). */
#define HAVE_ATTRIBUTE_ALWAYS_INLINE 1
/* Define to 1 if the compiler supports __builtin_ctz and friends. */
#define HAVE_BUILTIN_CTZ 1
/* Define to 1 if the compiler supports __builtin_expect. */
#define HAVE_BUILTIN_EXPECT 1
/* Define to 1 if the compiler supports __builtin_prefetch. */
#define HAVE_BUILTIN_PREFETCH 1
/* Define to 1 if you have a definition for mmap() in <sys/mman.h>. */
#define HAVE_FUNC_MMAP 1
/* Define to 1 if you have a definition for sysconf() in <unistd.h>. */
#define HAVE_FUNC_SYSCONF 1
/* Define to 1 if you have the `lzo2' library (-llzo2). */
#define HAVE_LIBLZO2 0
/* Define to 1 if you have the `z' library (-lz). */
#define HAVE_LIBZ 1
/* Define to 1 if you have the `lz4' library (-llz4). */
#define HAVE_LIBLZ4 0
/* Define to 1 if you have the <sys/mman.h> header file. */
#define HAVE_SYS_MMAN_H 1
/* Define to 1 if you have the <sys/resource.h> header file. */
#define HAVE_SYS_RESOURCE_H 1
/* Define to 1 if you have the <sys/time.h> header file. */
#define HAVE_SYS_TIME_H 1
/* Define to 1 if you have the <sys/uio.h> header file. */
#define HAVE_SYS_UIO_H 1
/* Define to 1 if you have the <unistd.h> header file. */
#define HAVE_UNISTD_H 1
/* Define to 1 if you have the <windows.h> header file. */
#define HAVE_WINDOWS_H 0
/* Define to 1 if you target processors with SSSE3+ and have <tmmintrin.h>. */
#define SNAPPY_HAVE_SSSE3 1
/* Define to 1 if you target processors with SSE4.2 and have <crc32intrin.h>. */
#define SNAPPY_HAVE_X86_CRC32 1
/* Define to 1 if you target processors with BMI2+ and have <bmi2intrin.h>. */
#define SNAPPY_HAVE_BMI2 0
/* Define to 1 if you target processors with NEON and have <arm_neon.h>. */
#define SNAPPY_HAVE_NEON 0
/* Define to 1 if you have <arm_neon.h> and <arm_acle.h> and want to optimize
compression speed by using __crc32cw from <arm_acle.h>. */
#define SNAPPY_HAVE_NEON_CRC32 0
/* Define to 1 if your processor stores words with the most significant byte
first (like Motorola and SPARC, unlike Intel and VAX). */
#define SNAPPY_IS_BIG_ENDIAN 0
#endif // THIRD_PARTY_SNAPPY_OPENSOURCE_CMAKE_CONFIG_H_

66
snappy-stubs-public.h Normal file
View File

@@ -0,0 +1,66 @@
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Various type stubs for the open-source version of Snappy.
//
// This file cannot include config.h, as it is included from snappy.h,
// which is a public header. Instead, snappy-stubs-public.h is generated by
// from snappy-stubs-public.h.in at configure time.
#ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_
#define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_
#include <cstddef>
// Original cmake configure baked HAVE_SYS_UIO_H=1 on the Linux host that
// pre-generated this header; gate it on _WIN32 so cross-compiles to Windows
// fall back to the iovec definition below.
#if !defined(_WIN32) // HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif // HAVE_SYS_UIO_H
#define SNAPPY_MAJOR 1
#define SNAPPY_MINOR 2
#define SNAPPY_PATCHLEVEL 2
#define SNAPPY_VERSION \
((SNAPPY_MAJOR << 16) | (SNAPPY_MINOR << 8) | SNAPPY_PATCHLEVEL)
namespace snappy {
#if defined(_WIN32) // !HAVE_SYS_UIO_H
// Windows does not have an iovec type, yet the concept is universally useful.
// It is simple to define it ourselves, so we put it inside our own namespace.
struct iovec {
void* iov_base;
size_t iov_len;
};
#endif // !HAVE_SYS_UIO_H
} // namespace snappy
#endif // THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_

View File

@@ -74,6 +74,7 @@
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <functional>
#include <memory> #include <memory>
#include <string> #include <string>
#include <utility> #include <utility>
@@ -959,6 +960,8 @@ emit_remainder:
char* CompressFragmentDoubleHash(const char* input, size_t input_size, char* op, char* CompressFragmentDoubleHash(const char* input, size_t input_size, char* op,
uint16_t* table, const int table_size, uint16_t* table, const int table_size,
uint16_t* table2, const int table_size2) { uint16_t* table2, const int table_size2) {
(void)table_size2;
assert(table_size == table_size2);
// "ip" is the input pointer, and "op" is the output pointer. // "ip" is the input pointer, and "op" is the output pointer.
const char* ip = input; const char* ip = input;
assert(input_size <= kBlockSize); assert(input_size <= kBlockSize);
@@ -1496,7 +1499,7 @@ class SnappyDecompressor {
// If ip < ip_limit_min_maxtaglen_ it's safe to read kMaxTagLength from // If ip < ip_limit_min_maxtaglen_ it's safe to read kMaxTagLength from
// buffer. // buffer.
const char* ip_limit_min_maxtaglen_; const char* ip_limit_min_maxtaglen_;
uint32_t peeked_; // Bytes peeked from reader (need to skip) uint64_t peeked_; // Bytes peeked from reader (need to skip)
bool eof_; // Hit end of input without an error? bool eof_; // Hit end of input without an error?
char scratch_[kMaximumTagLength]; // See RefillTag(). char scratch_[kMaximumTagLength]; // See RefillTag().
@@ -1687,7 +1690,8 @@ constexpr uint32_t CalculateNeeded(uint8_t tag) {
#if __cplusplus >= 201402L #if __cplusplus >= 201402L
constexpr bool VerifyCalculateNeeded() { constexpr bool VerifyCalculateNeeded() {
for (int i = 0; i < 1; i++) { for (int i = 0; i < 1; i++) {
if (CalculateNeeded(i) != (char_table[i] >> 11) + 1) return false; if (CalculateNeeded(i) != static_cast<uint32_t>((char_table[i] >> 11)) + 1)
return false;
} }
return true; return true;
} }
@@ -1723,7 +1727,7 @@ bool SnappyDecompressor::RefillTag() {
assert(needed <= sizeof(scratch_)); assert(needed <= sizeof(scratch_));
// Read more bytes from reader if needed // Read more bytes from reader if needed
uint32_t nbuf = ip_limit_ - ip; uint64_t nbuf = ip_limit_ - ip;
if (nbuf < needed) { if (nbuf < needed) {
// Stitch together bytes from ip and reader to form the word // Stitch together bytes from ip and reader to form the word
// contents. We store the needed bytes in "scratch_". They // contents. We store the needed bytes in "scratch_". They
@@ -1736,7 +1740,7 @@ bool SnappyDecompressor::RefillTag() {
size_t length; size_t length;
const char* src = reader_->Peek(&length); const char* src = reader_->Peek(&length);
if (length == 0) return false; if (length == 0) return false;
uint32_t to_add = std::min<uint32_t>(needed - nbuf, length); uint64_t to_add = std::min<uint64_t>(needed - nbuf, length);
std::memcpy(scratch_ + nbuf, src, to_add); std::memcpy(scratch_ + nbuf, src, to_add);
nbuf += to_add; nbuf += to_add;
reader_->Skip(to_add); reader_->Skip(to_add);
@@ -1790,11 +1794,16 @@ bool GetUncompressedLength(Source* source, uint32_t* result) {
return decompressor.ReadUncompressedLength(result); return decompressor.ReadUncompressedLength(result);
} }
size_t Compress(Source* reader, Sink* writer) {
return Compress(reader, writer, CompressionOptions{});
}
size_t Compress(Source* reader, Sink* writer, CompressionOptions options) { size_t Compress(Source* reader, Sink* writer, CompressionOptions options) {
assert(options.level == 1 || options.level == 2); assert(options.level == 1 || options.level == 2);
int token = 0; int token = 0;
size_t written = 0; size_t written = 0;
size_t N = reader->Available(); size_t N = reader->Available();
assert(N <= 0xFFFFFFFFu);
const size_t uncompressed_size = N; const size_t uncompressed_size = N;
char ulength[Varint::kMax32]; char ulength[Varint::kMax32];
char* p = Varint::Encode32(ulength, N); char* p = Varint::Encode32(ulength, N);
@@ -1886,16 +1895,16 @@ class SnappyIOVecReader : public Source {
if (total_size > 0 && curr_size_remaining_ == 0) Advance(); if (total_size > 0 && curr_size_remaining_ == 0) Advance();
} }
~SnappyIOVecReader() = default; ~SnappyIOVecReader() override = default;
size_t Available() const { return total_size_remaining_; } size_t Available() const override { return total_size_remaining_; }
const char* Peek(size_t* len) { const char* Peek(size_t* len) override {
*len = curr_size_remaining_; *len = curr_size_remaining_;
return curr_pos_; return curr_pos_;
} }
void Skip(size_t n) { void Skip(size_t n) override {
while (n >= curr_size_remaining_ && n > 0) { while (n >= curr_size_remaining_ && n > 0) {
n -= curr_size_remaining_; n -= curr_size_remaining_;
Advance(); Advance();
@@ -2296,6 +2305,12 @@ bool IsValidCompressed(Source* compressed) {
return InternalUncompress(compressed, &writer); return InternalUncompress(compressed, &writer);
} }
void RawCompress(const char* input, size_t input_length, char* compressed,
size_t* compressed_length) {
RawCompress(input, input_length, compressed, compressed_length,
CompressionOptions{});
}
void RawCompress(const char* input, size_t input_length, char* compressed, void RawCompress(const char* input, size_t input_length, char* compressed,
size_t* compressed_length, CompressionOptions options) { size_t* compressed_length, CompressionOptions options) {
ByteArraySource reader(input, input_length); ByteArraySource reader(input, input_length);
@@ -2306,6 +2321,12 @@ void RawCompress(const char* input, size_t input_length, char* compressed,
*compressed_length = (writer.CurrentDestination() - compressed); *compressed_length = (writer.CurrentDestination() - compressed);
} }
void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length,
char* compressed, size_t* compressed_length) {
RawCompressFromIOVec(iov, uncompressed_length, compressed, compressed_length,
CompressionOptions{});
}
void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length, void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length,
char* compressed, size_t* compressed_length, char* compressed, size_t* compressed_length,
CompressionOptions options) { CompressionOptions options) {
@@ -2317,6 +2338,11 @@ void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length,
*compressed_length = writer.CurrentDestination() - compressed; *compressed_length = writer.CurrentDestination() - compressed;
} }
size_t Compress(const char* input, size_t input_length,
std::string* compressed) {
return Compress(input, input_length, compressed, CompressionOptions{});
}
size_t Compress(const char* input, size_t input_length, std::string* compressed, size_t Compress(const char* input, size_t input_length, std::string* compressed,
CompressionOptions options) { CompressionOptions options) {
// Pre-grow the buffer to the max length of the compressed output // Pre-grow the buffer to the max length of the compressed output
@@ -2329,6 +2355,11 @@ size_t Compress(const char* input, size_t input_length, std::string* compressed,
return compressed_length; return compressed_length;
} }
size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt,
std::string* compressed) {
return CompressFromIOVec(iov, iov_cnt, compressed, CompressionOptions{});
}
size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt, size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt,
std::string* compressed, CompressionOptions options) { std::string* compressed, CompressionOptions options) {
// Compute the number of bytes to be compressed. // Compute the number of bytes to be compressed.
@@ -2533,7 +2564,6 @@ bool SnappyScatteredWriter<Allocator>::SlowAppendFromSelf(size_t offset,
class SnappySinkAllocator { class SnappySinkAllocator {
public: public:
explicit SnappySinkAllocator(Sink* dest) : dest_(dest) {} explicit SnappySinkAllocator(Sink* dest) : dest_(dest) {}
~SnappySinkAllocator() {}
char* Allocate(int size) { char* Allocate(int size) {
Datablock block(new char[size], size); Datablock block(new char[size], size);

View File

@@ -64,6 +64,9 @@ namespace snappy {
// faster decompression speeds than snappy:1 and zstd:-3. // faster decompression speeds than snappy:1 and zstd:-3.
int level = DefaultCompressionLevel(); int level = DefaultCompressionLevel();
constexpr CompressionOptions() = default;
constexpr CompressionOptions(int compression_level)
: level(compression_level) {}
static constexpr int MinCompressionLevel() { return 1; } static constexpr int MinCompressionLevel() { return 1; }
static constexpr int MaxCompressionLevel() { return 2; } static constexpr int MaxCompressionLevel() { return 2; }
static constexpr int DefaultCompressionLevel() { return 1; } static constexpr int DefaultCompressionLevel() { return 1; }
@@ -75,8 +78,10 @@ namespace snappy {
// Compress the bytes read from "*reader" and append to "*writer". Return the // Compress the bytes read from "*reader" and append to "*writer". Return the
// number of bytes written. // number of bytes written.
// First version is to preserve ABI.
size_t Compress(Source* reader, Sink* writer);
size_t Compress(Source* reader, Sink* writer, size_t Compress(Source* reader, Sink* writer,
CompressionOptions options = {}); CompressionOptions options);
// Find the uncompressed length of the given stream, as given by the header. // Find the uncompressed length of the given stream, as given by the header.
// Note that the true length could deviate from this; the stream could e.g. // Note that the true length could deviate from this; the stream could e.g.
@@ -95,16 +100,22 @@ namespace snappy {
// Original contents of *compressed are lost. // Original contents of *compressed are lost.
// //
// REQUIRES: "input[]" is not an alias of "*compressed". // REQUIRES: "input[]" is not an alias of "*compressed".
// First version is to preserve ABI.
size_t Compress(const char* input, size_t input_length, size_t Compress(const char* input, size_t input_length,
std::string* compressed, CompressionOptions options = {}); std::string* compressed);
size_t Compress(const char* input, size_t input_length,
std::string* compressed, CompressionOptions options);
// Same as `Compress` above but taking an `iovec` array as input. Note that // Same as `Compress` above but taking an `iovec` array as input. Note that
// this function preprocesses the inputs to compute the sum of // this function preprocesses the inputs to compute the sum of
// `iov[0..iov_cnt-1].iov_len` before reading. To avoid this, use // `iov[0..iov_cnt-1].iov_len` before reading. To avoid this, use
// `RawCompressFromIOVec` below. // `RawCompressFromIOVec` below.
// First version is to preserve ABI.
size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt,
std::string* compressed);
size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt, size_t CompressFromIOVec(const struct iovec* iov, size_t iov_cnt,
std::string* compressed, std::string* compressed,
CompressionOptions options = {}); CompressionOptions options);
// Decompresses "compressed[0..compressed_length-1]" to "*uncompressed". // Decompresses "compressed[0..compressed_length-1]" to "*uncompressed".
// Original contents of "*uncompressed" are lost. // Original contents of "*uncompressed" are lost.
@@ -148,14 +159,18 @@ namespace snappy {
// ... Process(output, output_length) ... // ... Process(output, output_length) ...
// delete [] output; // delete [] output;
void RawCompress(const char* input, size_t input_length, char* compressed, void RawCompress(const char* input, size_t input_length, char* compressed,
size_t* compressed_length, CompressionOptions options = {}); size_t* compressed_length);
void RawCompress(const char* input, size_t input_length, char* compressed,
size_t* compressed_length, CompressionOptions options);
// Same as `RawCompress` above but taking an `iovec` array as input. Note that // Same as `RawCompress` above but taking an `iovec` array as input. Note that
// `uncompressed_length` is the total number of bytes to be read from the // `uncompressed_length` is the total number of bytes to be read from the
// elements of `iov` (_not_ the number of elements in `iov`). // elements of `iov` (_not_ the number of elements in `iov`).
void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length,
char* compressed, size_t* compressed_length);
void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length, void RawCompressFromIOVec(const struct iovec* iov, size_t uncompressed_length,
char* compressed, size_t* compressed_length, char* compressed, size_t* compressed_length,
CompressionOptions options = {}); CompressionOptions options);
// Given data in "compressed[0..compressed_length-1]" generated by // Given data in "compressed[0..compressed_length-1]" generated by
// calling the Snappy::Compress routine, this routine // calling the Snappy::Compress routine, this routine

View File

@@ -31,12 +31,10 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "snappy-test.h"
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
#include "snappy-internal.h" #include "snappy-internal.h"
#include "snappy-sinksource.h" #include "snappy-sinksource.h"
#include "snappy-test.h"
#include "snappy.h" #include "snappy.h"
#include "snappy_test_data.h" #include "snappy_test_data.h"
@@ -44,7 +42,7 @@ namespace snappy {
namespace { namespace {
void FilesAndLevels(::testing::Benchmark* benchmark) { void FilesAndLevels(benchmark::internal::Benchmark* benchmark) {
for (int i = 0; i < ARRAYSIZE(kTestDataFiles); ++i) { for (int i = 0; i < ARRAYSIZE(kTestDataFiles); ++i) {
for (int level = snappy::CompressionOptions::MinCompressionLevel(); for (int level = snappy::CompressionOptions::MinCompressionLevel();
level <= snappy::CompressionOptions::MaxCompressionLevel(); ++level) { level <= snappy::CompressionOptions::MaxCompressionLevel(); ++level) {
@@ -64,8 +62,9 @@ void BM_UFlat(benchmark::State& state) {
kTestDataFiles[file_index].size_limit); kTestDataFiles[file_index].size_limit);
std::string zcontents; std::string zcontents;
snappy::Compress(contents.data(), contents.size(), &zcontents, snappy::Compress(
snappy::CompressionOptions{.level = state.range(1)}); contents.data(), contents.size(), &zcontents,
snappy::CompressionOptions{/*level=*/static_cast<int>(state.range(1))});
char* dst = new char[contents.size()]; char* dst = new char[contents.size()];
for (auto s : state) { for (auto s : state) {
@@ -129,8 +128,9 @@ void BM_UValidate(benchmark::State& state) {
kTestDataFiles[file_index].size_limit); kTestDataFiles[file_index].size_limit);
std::string zcontents; std::string zcontents;
snappy::Compress(contents.data(), contents.size(), &zcontents, snappy::Compress(
snappy::CompressionOptions{.level = state.range(1)}); contents.data(), contents.size(), &zcontents,
snappy::CompressionOptions{/*level=*/static_cast<int>(state.range(1))});
for (auto s : state) { for (auto s : state) {
CHECK(snappy::IsValidCompressedBuffer(zcontents.data(), zcontents.size())); CHECK(snappy::IsValidCompressedBuffer(zcontents.data(), zcontents.size()));
@@ -193,7 +193,7 @@ void BM_UIOVecSource(benchmark::State& state) {
size_t zsize = 0; size_t zsize = 0;
for (auto s : state) { for (auto s : state) {
snappy::RawCompressFromIOVec(iov, contents.size(), dst, &zsize, snappy::RawCompressFromIOVec(iov, contents.size(), dst, &zsize,
snappy::CompressionOptions{.level = level}); snappy::CompressionOptions{/*level=*/level});
benchmark::DoNotOptimize(iov); benchmark::DoNotOptimize(iov);
} }
state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) * state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
@@ -226,7 +226,7 @@ void BM_UIOVecSink(benchmark::State& state) {
// Uncompress into an iovec containing ten entries. // Uncompress into an iovec containing ten entries.
const int kNumEntries = 10; const int kNumEntries = 10;
struct iovec iov[kNumEntries]; struct iovec iov[kNumEntries];
char *dst = new char[contents.size()]; char* dst = new char[contents.size()];
size_t used_so_far = 0; size_t used_so_far = 0;
for (int i = 0; i < kNumEntries; ++i) { for (int i = 0; i < kNumEntries; ++i) {
iov[i].iov_base = dst + used_so_far; iov[i].iov_base = dst + used_so_far;
@@ -267,8 +267,9 @@ void BM_UFlatSink(benchmark::State& state) {
kTestDataFiles[file_index].size_limit); kTestDataFiles[file_index].size_limit);
std::string zcontents; std::string zcontents;
snappy::Compress(contents.data(), contents.size(), &zcontents, snappy::Compress(
snappy::CompressionOptions{.level = state.range(1)}); contents.data(), contents.size(), &zcontents,
snappy::CompressionOptions{/*level=*/static_cast<int>(state.range(1))});
char* dst = new char[contents.size()]; char* dst = new char[contents.size()];
for (auto s : state) { for (auto s : state) {
@@ -304,7 +305,7 @@ void BM_ZFlat(benchmark::State& state) {
size_t zsize = 0; size_t zsize = 0;
for (auto s : state) { for (auto s : state) {
snappy::RawCompress(contents.data(), contents.size(), dst, &zsize, snappy::RawCompress(contents.data(), contents.size(), dst, &zsize,
snappy::CompressionOptions{.level = level}); snappy::CompressionOptions{/*level=*/level});
benchmark::DoNotOptimize(dst); benchmark::DoNotOptimize(dst);
} }
state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) * state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
@@ -340,7 +341,7 @@ void BM_ZFlatAll(benchmark::State& state) {
for (auto s : state) { for (auto s : state) {
for (int i = 0; i < num_files; ++i) { for (int i = 0; i < num_files; ++i) {
snappy::RawCompress(contents[i].data(), contents[i].size(), dst[i], snappy::RawCompress(contents[i].data(), contents[i].size(), dst[i],
&zsize, snappy::CompressionOptions{.level = level}); &zsize, snappy::CompressionOptions{/*level=*/level});
benchmark::DoNotOptimize(dst); benchmark::DoNotOptimize(dst);
} }
} }
@@ -377,7 +378,7 @@ void BM_ZFlatIncreasingTableSize(benchmark::State& state) {
for (auto s : state) { for (auto s : state) {
for (size_t i = 0; i < contents.size(); ++i) { for (size_t i = 0; i < contents.size(); ++i) {
snappy::RawCompress(contents[i].data(), contents[i].size(), dst[i], snappy::RawCompress(contents[i].data(), contents[i].size(), dst[i],
&zsize, snappy::CompressionOptions{.level = level}); &zsize, snappy::CompressionOptions{/*level=*/level});
benchmark::DoNotOptimize(dst); benchmark::DoNotOptimize(dst);
} }
} }

View File

@@ -44,7 +44,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
std::string compressed; std::string compressed;
size_t compressed_size = size_t compressed_size =
snappy::Compress(input.data(), input.size(), &compressed, snappy::Compress(input.data(), input.size(), &compressed,
snappy::CompressionOptions{.level = level}); snappy::CompressionOptions{/*level=*/level});
(void)compressed_size; // Variable only used in debug builds. (void)compressed_size; // Variable only used in debug builds.
assert(compressed_size == compressed.size()); assert(compressed_size == compressed.size());

View File

@@ -27,6 +27,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <algorithm> #include <algorithm>
#include <cinttypes>
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
#include <random> #include <random>
@@ -484,6 +485,18 @@ TEST(Snappy, MaxBlowup) {
Verify(input); Verify(input);
} }
// Issue #201, when output is more than 4GB, we had a data corruption bug.
// We cannot run this test always because of CI constraints.
TEST(Snappy, DISABLED_MoreThan4GB) {
std::mt19937 rng;
std::uniform_int_distribution<int> uniform_byte(0, 255);
std::string input;
input.resize((1ull << 32) - 1);
for (uint64_t i = 0; i < ((1ull << 32) - 1); ++i)
input[i] = static_cast<char>(uniform_byte(rng));
Verify(input);
}
TEST(Snappy, RandomData) { TEST(Snappy, RandomData) {
std::minstd_rand0 rng(snappy::GetFlag(FLAGS_test_random_seed)); std::minstd_rand0 rng(snappy::GetFlag(FLAGS_test_random_seed));
std::uniform_int_distribution<int> uniform_0_to_3(0, 3); std::uniform_int_distribution<int> uniform_0_to_3(0, 3);