mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
Merge pull request #95 from oshogbo:zstd
PiperOrigin-RevId: 425893255 Change-Id: I6760fe1ab7734f1a27dd65e4c761c57961306a85
This commit is contained in:
commit
570898542b
|
@ -15,6 +15,7 @@
|
||||||
# Append to this list whenever a new sandboxed library is added to `contrib/`.
|
# Append to this list whenever a new sandboxed library is added to `contrib/`.
|
||||||
set(SAPI_CONTRIB_SANDBOXES
|
set(SAPI_CONTRIB_SANDBOXES
|
||||||
jsonnet
|
jsonnet
|
||||||
|
zstd
|
||||||
)
|
)
|
||||||
|
|
||||||
foreach(_contrib IN LISTS SAPI_CONTRIB_SANDBOXES)
|
foreach(_contrib IN LISTS SAPI_CONTRIB_SANDBOXES)
|
||||||
|
|
|
@ -6,8 +6,9 @@ libraries.
|
||||||
## Projects Sandboxed
|
## Projects Sandboxed
|
||||||
|
|
||||||
Directory | Project | Home Page | Integration
|
Directory | Project | Home Page | Integration
|
||||||
---------- | -------------------------------------- | -------------------------------------------------------------- | -----------
|
---------- | ------------------------------------------------ | -------------------------------------------------------------- | -----------
|
||||||
`jsonnet/` | Jsonnet - The Data Templating Language | [github.com/google/jsonnet](https://github.com/google/jsonnet) | CMake
|
`jsonnet/` | Jsonnet - The Data Templating Language | [github.com/google/jsonnet](https://github.com/google/jsonnet) | CMake
|
||||||
|
`zstd/` | Zstandard - Fast real-time compression algorithm | [github.com/facebook/zstd](https://github.com/facebook/zstd) | CMake
|
||||||
|
|
||||||
## Projects Shipping with Sandboxed API Sandboxes
|
## Projects Shipping with Sandboxed API Sandboxes
|
||||||
|
|
||||||
|
|
90
contrib/zstd/CMakeLists.txt
Normal file
90
contrib/zstd/CMakeLists.txt
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
# Copyright 2022 Google LLC
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.13)
|
||||||
|
|
||||||
|
project(sapi_zstd CXX)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||||
|
|
||||||
|
if(NOT TARGET sapi::sapi)
|
||||||
|
set(SAPI_ROOT "../.." CACHE PATH "Path to the Sandboxed API source tree")
|
||||||
|
add_subdirectory("${SAPI_ROOT}"
|
||||||
|
"${CMAKE_BINARY_DIR}/sandboxed-api-build"
|
||||||
|
EXCLUDE_FROM_ALL)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
FetchContent_Declare(libzstd
|
||||||
|
GIT_REPOSITORY https://github.com/facebook/zstd.git
|
||||||
|
GIT_TAG 4dfc4eca9a166b474b09542a9fc8e2da162eea99
|
||||||
|
SOURCE_SUBDIR build/cmake
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(libzstd)
|
||||||
|
|
||||||
|
set(libzstd_INCLUDE_DIR "${libzstd_SOURCE_DIR}/lib")
|
||||||
|
add_sapi_library(
|
||||||
|
sapi_zstd
|
||||||
|
|
||||||
|
FUNCTIONS
|
||||||
|
ZSTD_versionNumber
|
||||||
|
ZSTD_versionString
|
||||||
|
|
||||||
|
ZSTD_compressBound
|
||||||
|
ZSTD_isError
|
||||||
|
ZSTD_getErrorName
|
||||||
|
ZSTD_minCLevel
|
||||||
|
ZSTD_maxCLevel
|
||||||
|
ZSTD_defaultCLevel
|
||||||
|
|
||||||
|
ZSTD_createDCtx
|
||||||
|
ZSTD_freeDCtx
|
||||||
|
|
||||||
|
ZSTD_CCtx_setParameter
|
||||||
|
|
||||||
|
ZSTD_compressBound
|
||||||
|
ZSTD_compress
|
||||||
|
ZSTD_compressStream
|
||||||
|
ZSTD_compressStream2
|
||||||
|
ZSTD_flushStream
|
||||||
|
ZSTD_endStream
|
||||||
|
|
||||||
|
ZSTD_CStreamInSize
|
||||||
|
ZSTD_CStreamOutSize
|
||||||
|
|
||||||
|
ZSTD_decompress
|
||||||
|
ZSTD_decompressStream
|
||||||
|
|
||||||
|
ZSTD_getFrameContentSize
|
||||||
|
|
||||||
|
INPUTS
|
||||||
|
${libzstd_INCLUDE_DIR}/zstd.h
|
||||||
|
|
||||||
|
LIBRARY libzstd_static
|
||||||
|
LIBRARY_NAME Zstd
|
||||||
|
NAMESPACE ""
|
||||||
|
)
|
||||||
|
add_library(sapi_contrib::zstd ALIAS sapi_zstd)
|
||||||
|
target_include_directories(sapi_zstd INTERFACE
|
||||||
|
"${PROJECT_BINARY_DIR}"
|
||||||
|
"${SAPI_SOURCE_DIR}"
|
||||||
|
)
|
||||||
|
|
||||||
|
if(SAPI_ENABLE_EXAMPLES)
|
||||||
|
add_subdirectory(example)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(SAPI_ENABLE_TESTS)
|
||||||
|
add_subdirectory(test)
|
||||||
|
endif()
|
28
contrib/zstd/example/CMakeLists.txt
Normal file
28
contrib/zstd/example/CMakeLists.txt
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
# Copyright 2022 Google LLC
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
add_executable(
|
||||||
|
sapi_minizstd
|
||||||
|
|
||||||
|
main.cc
|
||||||
|
../utils/utils_zstd.cc
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(
|
||||||
|
sapi_minizstd PRIVATE
|
||||||
|
|
||||||
|
sapi_zstd
|
||||||
|
sapi::sapi
|
||||||
|
absl::flags_parse
|
||||||
|
)
|
80
contrib/zstd/example/main.cc
Normal file
80
contrib/zstd/example/main.cc
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
// Copyright 2022 Google LLC
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "contrib/zstd/sandboxed.h"
|
||||||
|
#include "contrib/zstd/utils/utils_zstd.h"
|
||||||
|
|
||||||
|
ABSL_FLAG(bool, decompress, false, "decompress");
|
||||||
|
ABSL_FLAG(bool, memory_mode, false, "in memory operations");
|
||||||
|
ABSL_FLAG(uint32_t, level, 0, "compression level");
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
std::string prog_name(argv[0]);
|
||||||
|
google::InitGoogleLogging(argv[0]);
|
||||||
|
std::vector<char*> args = absl::ParseCommandLine(argc, argv);
|
||||||
|
|
||||||
|
if (args.size() != 3) {
|
||||||
|
std::cerr << "Usage:\n " << prog_name << " INPUT OUTPUT\n";
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ifstream infile(args[1], std::ios::binary);
|
||||||
|
if (!infile.is_open()) {
|
||||||
|
std::cerr << "Unable to open " << args[1] << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
std::ofstream outfile(args[2], std::ios::binary);
|
||||||
|
if (!outfile.is_open()) {
|
||||||
|
std::cerr << "Unable to open " << args[2] << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
ZstdSapiSandbox sandbox;
|
||||||
|
if (!sandbox.Init().ok()) {
|
||||||
|
std::cerr << "Unable to start sandbox\n";
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
ZstdApi api(&sandbox);
|
||||||
|
|
||||||
|
absl::Status status;
|
||||||
|
if (absl::GetFlag(FLAGS_memory_mode) && absl::GetFlag(FLAGS_decompress)) {
|
||||||
|
status = DecompressInMemory(api, infile, outfile);
|
||||||
|
} else if (absl::GetFlag(FLAGS_memory_mode) &&
|
||||||
|
!absl::GetFlag(FLAGS_decompress)) {
|
||||||
|
status = CompressInMemory(api, infile, outfile, absl::GetFlag(FLAGS_level));
|
||||||
|
} else if (!absl::GetFlag(FLAGS_memory_mode) &&
|
||||||
|
absl::GetFlag(FLAGS_decompress)) {
|
||||||
|
status = DecompressStream(api, infile, outfile);
|
||||||
|
} else {
|
||||||
|
status = CompressStream(api, infile, outfile, absl::GetFlag(FLAGS_level));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!status.ok()) {
|
||||||
|
std::cerr << "Unable to ";
|
||||||
|
std::cerr << (absl::GetFlag(FLAGS_decompress) ? "decompress" : "compress");
|
||||||
|
std::cerr << " file.\n" << status << "\n";
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
59
contrib/zstd/files/text
Normal file
59
contrib/zstd/files/text
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam id ultricies neque, id blandit nisl. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas pharetra urna purus, a finibus erat sodales ac. Donec sed convallis felis, ac rutrum velit. Suspendisse dapibus ultrices euismod. Donec non erat scelerisque, pretium dui id, dignissim dolor. Vivamus id orci maximus, maximus sapien sed, tincidunt nunc.
|
||||||
|
|
||||||
|
Praesent fringilla lectus lobortis dui posuere, ac mollis neque ultrices. Fusce nec quam eget neque feugiat mollis quis sit amet ipsum. Suspendisse hendrerit elit tincidunt neque tincidunt mattis. Nulla vitae neque sit amet metus consequat ultrices a ac augue. Nunc viverra lacinia ultrices. Vivamus in cursus ex, a sollicitudin purus. Curabitur et consectetur enim. Duis euismod magna eget velit tristique, at varius lorem posuere. Quisque non erat at diam blandit sollicitudin sit amet ac lectus. Maecenas nec consectetur augue, vel dignissim orci. Etiam rutrum ac massa at accumsan.
|
||||||
|
|
||||||
|
Nullam congue lacus et eros ornare, et malesuada nunc varius. Mauris facilisis suscipit nisl non tristique. Proin lobortis diam vel mi tempus, sit amet iaculis eros iaculis. Phasellus a metus ac purus pellentesque fermentum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Suspendisse venenatis quam vel sapien scelerisque commodo. Donec nec urna feugiat, semper ipsum non, dapibus neque. Aenean egestas tortor sit amet interdum convallis.
|
||||||
|
|
||||||
|
Cras sed enim scelerisque orci hendrerit porta. Duis egestas erat sollicitudin vestibulum convallis. Proin eget eros in neque ultricies posuere. Quisque sodales nisi nulla, non sollicitudin ipsum sodales in. Phasellus feugiat sollicitudin enim, in faucibus massa cursus nec. Donec arcu erat, ultricies eget nisi scelerisque, pharetra fringilla nisl. Maecenas et consectetur neque. Duis vel rhoncus lacus. Nulla nec lorem iaculis, consequat sapien a, semper leo.
|
||||||
|
|
||||||
|
Praesent vehicula sem in tempor pulvinar. Sed porttitor volutpat nulla eget tempor. Phasellus sit amet quam justo. Donec sit amet lacus dolor. Ut finibus sollicitudin dui vitae tincidunt. Mauris commodo nec orci convallis accumsan. In dapibus urna ex, in convallis purus ornare vel.
|
||||||
|
|
||||||
|
Suspendisse potenti. Nulla mi lorem, dictum accumsan mollis vitae, commodo vitae purus. Nullam accumsan lectus elit, vel volutpat lectus varius eget. Sed in condimentum neque. Mauris nibh arcu, dignissim sed ipsum a, imperdiet mattis neque. Nam eu pretium orci, a semper mauris. Proin dui lacus, auctor nec nibh sed, eleifend interdum libero. Vestibulum venenatis gravida risus in pulvinar. Sed sit amet leo vehicula, lacinia nisi et, dignissim ex. Fusce consectetur sollicitudin nisi. Pellentesque nec mauris vitae arcu iaculis semper eu eu odio. Ut facilisis erat a hendrerit egestas. Nullam tristique augue nunc, et ornare risus efficitur et. Quisque placerat risus metus, eu rutrum dui egestas et. Donec elementum sapien leo, vitae porttitor metus pharetra luctus.
|
||||||
|
|
||||||
|
Ut non neque vel nibh accumsan luctus at eu ipsum. Ut vitae lacus vestibulum, sodales purus quis, elementum neque. Pellentesque a justo non massa finibus iaculis in sit amet mi. Nunc vel justo libero. Proin mollis ex quis nulla sollicitudin, quis porttitor justo imperdiet. Aenean vulputate semper consectetur. Aenean nibh tortor, viverra commodo magna sed, condimentum efficitur tellus. Maecenas eros mauris, tempor at porta ut, laoreet quis est. Proin eu purus a dolor aliquet bibendum nec ac velit. Phasellus quis tortor risus. Aliquam non felis quis massa scelerisque ullamcorper. Nulla tristique nunc ligula, quis semper lacus tincidunt ut. Maecenas in aliquet magna. Phasellus sed dolor id felis blandit efficitur a at nulla.
|
||||||
|
|
||||||
|
Curabitur condimentum est non felis luctus, in tempus tellus tempus. Curabitur at quam nec mi vulputate sollicitudin quis a arcu. Phasellus lectus lorem, feugiat sit amet accumsan in, pretium ut leo. Integer vulputate ante ac nunc elementum varius. Nunc interdum tellus auctor, rutrum urna ut, imperdiet quam. Maecenas non accumsan lorem. Donec sem augue, tincidunt vel aliquam in, viverra et tellus. Duis lobortis, arcu quis scelerisque rhoncus, velit enim lacinia sem, sed commodo lorem ex cursus ligula. Integer nec auctor erat. Proin feugiat vel odio a fringilla. Nam dignissim augue elit. Nam sed lectus consequat, lacinia est vel, maximus eros. Aenean egestas ultricies odio. Mauris ut cursus ipsum. Fusce porttitor, eros sed sollicitudin porttitor, quam ipsum blandit nunc, ut sagittis metus diam tristique tortor. In sagittis odio odio, et ullamcorper augue gravida vel.
|
||||||
|
|
||||||
|
Nam vulputate nulla ut faucibus pulvinar. Praesent in purus non orci semper imperdiet. Vivamus vel diam ornare, eleifend tellus sit amet, viverra orci. Proin porttitor ipsum et odio laoreet, quis efficitur nisl dictum. Vestibulum mollis, arcu ut semper iaculis, nulla sapien vehicula enim, quis vulputate dolor leo hendrerit felis. Vestibulum mi purus, tristique vel vulputate et, efficitur ac turpis. Aenean facilisis sed nisl ac iaculis. Proin scelerisque justo a diam commodo, ac volutpat nisi sodales. Aliquam hendrerit blandit sapien, eu pretium risus dictum nec. Aliquam ultrices tincidunt magna nec bibendum. Vivamus dapibus et lectus at suscipit. Phasellus maximus, nibh sed tempor convallis, arcu erat condimentum ex, id laoreet ex leo nec mi. Aenean ultricies eget quam non ultrices. Donec sollicitudin ex non elit mattis rutrum.
|
||||||
|
|
||||||
|
Aliquam feugiat, elit a lobortis aliquam, orci augue rhoncus urna, et condimentum nibh est at est. Morbi eu diam quis nisi tristique tempus id ac tortor. Praesent a aliquet sem. Donec a mollis tellus, in dapibus ligula. Nullam quis dictum leo. Nam turpis diam, imperdiet id felis non, rutrum vehicula dui. Donec eleifend augue eu mauris tristique, non condimentum sapien dapibus. Fusce congue scelerisque suscipit. In in vulputate enim, malesuada mollis augue. Vestibulum tristique tempus ipsum vel venenatis. Quisque mauris urna, congue in lorem sed, dapibus convallis lorem. In eget arcu varius, tempor nisi quis, fermentum ipsum. Cras faucibus lacus sed massa pulvinar, vel viverra felis rhoncus.
|
||||||
|
|
||||||
|
Nulla in nisi fringilla, molestie nisi at, dapibus arcu. Aenean ut justo eget neque tincidunt elementum. Ut varius, neque eu tempor pellentesque, magna turpis fermentum neque, eget pulvinar diam purus sit amet augue. Curabitur suscipit pharetra nisi egestas convallis. Aliquam et ex pulvinar eros malesuada viverra sed in libero. Quisque molestie dictum arcu at ultricies. Nulla id metus non lacus posuere vestibulum vel ut sem. Proin pulvinar nisl nisl, a imperdiet enim ultrices vel. Etiam malesuada posuere dignissim. Donec mollis nulla ut enim sollicitudin, nec commodo ante malesuada. Quisque sit amet augue eu quam malesuada mollis sed at ipsum.
|
||||||
|
|
||||||
|
Aliquam quam est, maximus sed porta ut, placerat eu nunc. Vivamus ultrices ultricies tristique. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. In et mi eu lacus rhoncus condimentum ultrices non velit. Suspendisse imperdiet, ipsum eu commodo euismod, est dolor sollicitudin augue, eu blandit risus tortor sit amet eros. Aliquam rutrum feugiat magna, sit amet aliquam tortor rutrum non. Fusce tempor rhoncus lectus vitae molestie. Sed ut tellus sagittis, congue enim mollis, malesuada ligula. Sed id tellus vel dolor commodo posuere malesuada et felis.
|
||||||
|
|
||||||
|
Morbi quis risus nec lectus maximus tristique vehicula a lectus. Suspendisse ipsum urna, accumsan sit amet dui id, malesuada pellentesque diam. Sed sit amet tellus rutrum, placerat urna nec, tristique libero. Etiam luctus sit amet dui in rutrum. Etiam iaculis, tortor ut vulputate blandit, enim ligula euismod tortor, a porttitor leo ex sit amet lorem. Proin fermentum, est et fermentum laoreet, ante risus aliquam odio, nec consectetur dui leo in est. Etiam dapibus risus sit amet quam viverra gravida. Aenean at libero quis dolor consectetur ultricies. Maecenas rutrum mattis dui, ut auctor elit fringilla vitae. Nulla consequat tincidunt ligula ac dapibus. Vestibulum nisl erat, pharetra ac cursus sed, molestie sit amet justo. Vestibulum lobortis id ligula in elementum. Mauris non purus a magna feugiat tempus et at erat. In tincidunt diam vitae rutrum molestie. Pellentesque ut nisi laoreet, semper mi aliquet, rhoncus dolor.
|
||||||
|
|
||||||
|
Pellentesque lacinia quis felis ac fringilla. Cras ante enim, efficitur non erat in, gravida mollis diam. Aliquam accumsan nulla nec eros aliquam fringilla. Mauris scelerisque aliquam fringilla. Aliquam lacinia mi orci, sit amet dignissim lorem lacinia at. Duis elit arcu, iaculis id risus at, porta sollicitudin sapien. Duis ante eros, consequat vitae massa at, auctor interdum nisi. Vivamus sit amet dui neque. Mauris tincidunt vestibulum arcu, consequat condimentum urna tincidunt vel. Sed a urna magna. Nullam in pharetra nulla.
|
||||||
|
|
||||||
|
Nunc id purus at ipsum blandit consectetur sagittis non lorem. In sit amet aliquam ante. Integer convallis lectus eu lorem finibus vulputate. Morbi feugiat turpis id neque posuere, non elementum turpis volutpat. Nullam gravida, turpis vitae varius placerat, neque nisl bibendum velit, at suscipit eros ipsum et lorem. Integer arcu eros, gravida eu mauris condimentum, tincidunt euismod mi. Sed id elit vitae nulla aliquet laoreet. Sed pulvinar sollicitudin fermentum. Fusce semper ullamcorper mollis. Aenean laoreet, sem non cursus volutpat, metus elit scelerisque nisi, eget maximus neque enim non orci.
|
||||||
|
|
||||||
|
Curabitur lacinia suscipit rutrum. Curabitur convallis eu lorem nec accumsan. Morbi et turpis sit amet erat mollis vehicula et eu risus. Nulla vel urna mollis, laoreet risus vel, auctor risus. Cras rutrum tempor ligula, sed fringilla justo tempor gravida. Nam tincidunt elementum dolor in sollicitudin. Donec pretium diam sed libero varius efficitur. Vivamus non nunc vulputate, accumsan ipsum sit amet, facilisis urna. Suspendisse suscipit, diam et ultrices maximus, enim sapien facilisis ligula, ut lacinia metus nisl eget felis. Proin egestas sollicitudin est ac auctor. Cras sapien turpis, interdum vitae laoreet sed, aliquam sed ex. Sed ullamcorper mi eu nisi varius facilisis. Nam fringilla at augue sit amet finibus. Mauris vitae facilisis purus.
|
||||||
|
|
||||||
|
Suspendisse hendrerit lacus eget nisi bibendum sodales. In scelerisque sem eget est suscipit, in sollicitudin urna ultricies. Aenean ac mi non mauris congue viverra. Nam fermentum, purus ut consectetur volutpat, augue arcu luctus eros, quis efficitur felis mi nec ligula. Nullam eget feugiat ante. Donec rutrum, ex sit amet sodales venenatis, leo nibh euismod lacus, nec auctor purus urna in enim. Aliquam erat volutpat. Ut a ante rhoncus, cursus massa quis, porttitor mi. Nullam non lectus et mauris mollis lacinia. Curabitur euismod euismod nisi vitae malesuada. Donec vel finibus justo. Suspendisse lobortis efficitur ligula sed bibendum.
|
||||||
|
|
||||||
|
Integer mauris nulla, auctor vel imperdiet vitae, commodo vel arcu. Nulla sit amet tempor ante, ac elementum dolor. Nam fermentum euismod est nec pulvinar. Etiam eleifend blandit nisl non mollis. Vestibulum porta sed nunc at aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec id sem eu eros lobortis malesuada. Maecenas cursus neque a purus rhoncus, fermentum sagittis enim viverra. Proin blandit laoreet est eget feugiat.
|
||||||
|
|
||||||
|
Nulla nec nulla pellentesque, ullamcorper felis eget, bibendum augue. Sed lectus felis, tempor ac est non, commodo viverra augue. Ut bibendum arcu eget dapibus consectetur. Morbi id lectus vulputate, maximus leo sed, tincidunt leo. Nunc id nibh a lacus sagittis tristique at ac dolor. Nunc sed iaculis dolor. Cras maximus vulputate metus, a finibus magna sagittis non. Morbi porta fringilla enim, non pellentesque elit pellentesque eu. Vivamus pharetra dictum fermentum. Etiam consequat iaculis consequat. Morbi pharetra ante quam, quis efficitur purus finibus quis. Nulla at volutpat arcu. Curabitur augue lorem, lacinia eget augue vel, dignissim fringilla augue. Duis facilisis nulla turpis, ut fringilla quam efficitur ut. Cras tincidunt, tellus et dignissim ultricies, ligula ex pulvinar enim, vitae convallis erat ex ac neque.
|
||||||
|
|
||||||
|
Mauris sed auctor lorem. Vestibulum non ligula viverra, rhoncus quam at, pulvinar tellus. Phasellus dictum tellus eu nisl dignissim, id vulputate odio tincidunt. Mauris ornare ipsum nec maximus maximus. Nulla non sagittis turpis, in blandit magna. Donec at leo gravida, cursus diam ac, ornare velit. Praesent ipsum lorem, fermentum id lacinia ac, ultrices ac lectus. Vestibulum efficitur eget ipsum vitae vulputate. Aenean placerat magna sed ligula efficitur, ut consequat eros vestibulum. Proin placerat sapien sed arcu pharetra volutpat. Nunc diam lectus, tincidunt id eleifend vel, lobortis a lectus. In id orci sed purus venenatis pulvinar quis eget odio. Praesent quis nisi porta, blandit nisi a, hendrerit lacus. Morbi mollis libero non commodo consequat. Nullam id erat mi.
|
||||||
|
|
||||||
|
Vestibulum sit amet ante turpis. Duis in cursus eros. Praesent luctus eget augue tristique porttitor. Sed scelerisque facilisis sem sed interdum. Donec tristique augue ex, sit amet auctor ex elementum id. Sed lacinia ultrices dui, vitae scelerisque leo fringilla quis. Proin condimentum facilisis justo. Integer nulla mi, laoreet vel magna elementum, dignissim volutpat turpis. Aliquam erat volutpat.
|
||||||
|
|
||||||
|
Ut at quam nulla. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam maximus felis quis tincidunt fringilla. Fusce hendrerit pharetra pharetra. Nullam venenatis augue eget neque venenatis varius. Aenean posuere posuere nibh, sed vulputate purus tristique a. Sed nec lacus egestas, rhoncus nunc varius, euismod metus.
|
||||||
|
|
||||||
|
Aliquam pellentesque vel turpis ut aliquam. Fusce aliquam fermentum eros, gravida finibus arcu lacinia ut. Morbi vehicula nulla vitae viverra imperdiet. Curabitur eu euismod dui. Nullam congue congue odio, vel varius libero volutpat ut. Curabitur ante urna, vehicula nec sodales vel, lacinia eget arcu. Ut mollis rhoncus orci et facilisis. Aenean feugiat, purus a porttitor feugiat, ex nunc convallis leo, vel semper metus dolor id neque. Proin placerat orci ut purus cursus vehicula. Praesent molestie gravida ipsum, ac sagittis augue viverra vitae. Donec mi purus, congue vitae blandit ut, sollicitudin eu lorem. Vivamus dapibus nibh sed placerat mollis. Fusce egestas nunc lorem, sed interdum turpis gravida eget. Pellentesque enim ante, accumsan pulvinar dignissim ut, faucibus ac orci.
|
||||||
|
|
||||||
|
Fusce tempor fringilla bibendum. Nulla eu suscipit dui. In hac habitasse platea dictumst. Vivamus at lobortis orci, sit amet aliquet nunc. Nunc lacus mauris, sagittis in ornare sit amet, venenatis et dolor. Donec pellentesque nisl blandit, facilisis urna ac, dapibus nisl. Integer sollicitudin commodo euismod. Maecenas vulputate consequat ligula a accumsan. Nunc id risus magna. Nullam pulvinar blandit ante quis elementum.
|
||||||
|
|
||||||
|
Aliquam cursus lectus dui, facilisis tincidunt libero euismod id. Integer id placerat ligula, sit amet mattis mi. Nullam sed nunc orci. Etiam ac dolor sit amet purus consectetur hendrerit. Sed facilisis venenatis ullamcorper. Maecenas tincidunt nunc scelerisque vehicula eleifend. Nullam tincidunt tristique felis sed tempus. Donec nec fermentum dui, in tempus libero. Nam viverra mauris ornare mi finibus, sit amet convallis dui aliquam. Cras ut ultrices tellus. Pellentesque scelerisque placerat vehicula. Quisque cursus malesuada nunc, ut ultricies metus imperdiet eget.
|
||||||
|
|
||||||
|
Proin tristique gravida justo eget hendrerit. Donec id tincidunt sapien. Fusce elementum metus eget risus fermentum, eget venenatis purus rutrum. Maecenas rhoncus pharetra eros a iaculis. Aliquam vestibulum ipsum sed vehicula tempor. Sed convallis tellus sed augue pellentesque, eget suscipit ex iaculis. Integer nisl sapien, vulputate vel congue eget, viverra id ante. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nunc blandit aliquet ex nec vehicula. Sed tristique gravida tellus eget egestas. Quisque bibendum ligula velit, sed egestas diam rutrum in. Maecenas hendrerit augue nec elit lacinia mattis.
|
||||||
|
|
||||||
|
Phasellus ultrices ex augue, vel venenatis turpis laoreet in. Suspendisse a imperdiet lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nulla quis nisl finibus, dictum urna eget, fermentum purus. Nam fermentum magna id nisi semper, nec tempor lorem blandit. Sed dignissim fringilla dui id lacinia. Phasellus vitae viverra erat. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque vel malesuada lorem, pharetra consequat diam. Quisque tempor dui eu quam aliquam porta. Donec tortor tortor, tristique vitae sem eget, euismod viverra justo. Morbi id mollis ex. Suspendisse potenti. Donec rutrum, orci id molestie malesuada, massa ex fermentum nisi, vitae facilisis nisi turpis eget enim. Sed eu mi at augue maximus rhoncus. Cras vehicula tellus non gravida efficitur.
|
||||||
|
|
||||||
|
Mauris eros justo, finibus lacinia sodales ut, tempus a ligula. Nulla sit amet sagittis mauris, non fermentum magna. Quisque tristique sagittis porta. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In ultricies, lectus in rutrum lobortis, elit nisi bibendum turpis, a gravida ligula mi id dolor. In rhoncus suscipit lectus ac maximus. Nunc vulputate aliquam laoreet. Morbi eget purus sit amet odio suscipit rutrum. Suspendisse id enim eget justo fringilla fringilla. Maecenas et nulla consectetur, feugiat orci ac, scelerisque est. Pellentesque iaculis justo dui, nec volutpat elit tincidunt eget. Curabitur finibus est a nisl interdum, ut rutrum lorem luctus.
|
||||||
|
|
||||||
|
Nam dolor ipsum, egestas eget luctus nec, congue vitae eros. Donec libero massa, molestie nec augue lacinia, auctor iaculis tortor. Proin tellus mi, vestibulum in lacus at, fringilla commodo nulla. Ut ullamcorper, quam at blandit congue, mauris purus volutpat nisl, quis fermentum justo ipsum id mi. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Etiam vestibulum augue eget sagittis porta. Curabitur eget purus placerat, fringilla lorem non, pretium mauris. Suspendisse potenti. Ut cursus erat augue, quis laoreet felis posuere sit amet. Suspendisse potenti. Sed aliquet semper quam quis ultrices. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||||
|
|
||||||
|
Donec placerat massa et ipsum malesuada, in fringilla mi mollis. Curabitur vel condimentum enim. Donec luctus libero eget dapibus sagittis. Cras vulputate nibh non mauris accumsan, vel lobortis elit hendrerit. Praesent hendrerit ligula purus, non consequat libero accumsan sed. Suspendisse fringilla, tortor in fermentum congue, arcu tellus mollis sem, ut tempus enim dui non leo. Sed in augue diam. Maecenas vel neque eget mi lacinia ornare. Maecenas pharetra consectetur erat non feugiat. Aliquam lacinia augue eget elit maximus fringilla. Suspendisse risus lorem, porta eget nunc a, porta lobortis sapien.
|
BIN
contrib/zstd/files/text.blob.zstd
Normal file
BIN
contrib/zstd/files/text.blob.zstd
Normal file
Binary file not shown.
BIN
contrib/zstd/files/text.stream.zstd
Normal file
BIN
contrib/zstd/files/text.stream.zstd
Normal file
Binary file not shown.
40
contrib/zstd/sandboxed.h
Normal file
40
contrib/zstd/sandboxed.h
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
// Copyright 2022 Google LLC
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#ifndef CONTRIB_ZSTD_SANDBOXED_H_
|
||||||
|
#define CONTRIB_ZSTD_SANDBOXED_H_
|
||||||
|
|
||||||
|
#include <libgen.h>
|
||||||
|
#include <syscall.h>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "sapi_zstd.sapi.h" // NOLINT(build/include)
|
||||||
|
#include "sandboxed_api/util/flag.h"
|
||||||
|
#include "absl/flags/parse.h"
|
||||||
|
|
||||||
|
class ZstdSapiSandbox : public ZstdSandbox {
|
||||||
|
public:
|
||||||
|
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
||||||
|
sandbox2::PolicyBuilder*) override {
|
||||||
|
return sandbox2::PolicyBuilder()
|
||||||
|
.AllowRead()
|
||||||
|
.AllowWrite()
|
||||||
|
.AllowSystemMalloc()
|
||||||
|
.AllowExit()
|
||||||
|
.BuildOrDie();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CONTRIB_ZSTD_SANDBOXED_H_
|
33
contrib/zstd/test/CMakeLists.txt
Normal file
33
contrib/zstd/test/CMakeLists.txt
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
# Copyright 2022 Google LLC
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
include(GoogleTest)
|
||||||
|
|
||||||
|
add_executable(
|
||||||
|
sapi_zstd_test
|
||||||
|
|
||||||
|
zstd_test.cc
|
||||||
|
../utils/utils_zstd.cc
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
target_link_libraries(
|
||||||
|
sapi_zstd_test PRIVATE
|
||||||
|
|
||||||
|
sapi_zstd
|
||||||
|
sapi::test_main
|
||||||
|
sapi::temp_file
|
||||||
|
)
|
||||||
|
|
||||||
|
gtest_discover_tests(sapi_zstd_test PROPERTIES ENVIRONMENT "TEST_FILES_DIR=${PROJECT_SOURCE_DIR}/files")
|
286
contrib/zstd/test/zstd_test.cc
Normal file
286
contrib/zstd/test/zstd_test.cc
Normal file
|
@ -0,0 +1,286 @@
|
||||||
|
// Copyright 2022 Google LLC
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "gmock/gmock.h"
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "contrib/zstd/sandboxed.h"
|
||||||
|
#include "contrib/zstd/utils/utils_zstd.h"
|
||||||
|
#include "sandboxed_api/util/path.h"
|
||||||
|
#include "sandboxed_api/util/status_matchers.h"
|
||||||
|
#include "sandboxed_api/util/temp_file.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::sapi::IsOk;
|
||||||
|
|
||||||
|
std::string GetTestFilePath(const std::string& filename) {
|
||||||
|
return sapi::file::JoinPath(getenv("TEST_FILES_DIR"), filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CompareFiles(const std::string& name1, const std::string& name2) {
|
||||||
|
std::ifstream f1(name1, std::ios::binary);
|
||||||
|
if (!f1.is_open()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ifstream f2(name2, std::ios::binary);
|
||||||
|
if (!f2.is_open()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!f1.eof() && !f2.eof()) {
|
||||||
|
char buf1[128];
|
||||||
|
char buf2[128];
|
||||||
|
|
||||||
|
f1.read(buf1, sizeof(buf1));
|
||||||
|
f2.read(buf2, sizeof(buf2));
|
||||||
|
|
||||||
|
if (f1.gcount() != f2.gcount()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (memcmp(&buf1, &buf2, f2.gcount()) != 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return f1.eof() == f2.eof();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SandboxTest, CheckVersion) {
|
||||||
|
ZstdSapiSandbox sandbox;
|
||||||
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
|
||||||
|
ZstdApi api = ZstdApi(&sandbox);
|
||||||
|
|
||||||
|
absl::StatusOr<unsigned> version = api.ZSTD_versionNumber();
|
||||||
|
ASSERT_THAT(version, IsOk())
|
||||||
|
<< "fatal error when invoking ZSTD_versionNumber";
|
||||||
|
|
||||||
|
ASSERT_GE(*version, 10000);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SandboxTest, CheckMinCLevel) {
|
||||||
|
ZstdSapiSandbox sandbox;
|
||||||
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
|
||||||
|
ZstdApi api = ZstdApi(&sandbox);
|
||||||
|
|
||||||
|
absl::StatusOr<int> level = api.ZSTD_minCLevel();
|
||||||
|
ASSERT_THAT(level, IsOk()) << "fatal error when invoking ZSTD_minCLevel";
|
||||||
|
|
||||||
|
ASSERT_LT(*level, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SandboxTest, CheckMaxCLevel) {
|
||||||
|
ZstdSapiSandbox sandbox;
|
||||||
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
|
||||||
|
ZstdApi api = ZstdApi(&sandbox);
|
||||||
|
|
||||||
|
absl::StatusOr<int> level = api.ZSTD_maxCLevel();
|
||||||
|
ASSERT_THAT(level, IsOk()) << "fatal error when invoking ZSTD_maxCLevel";
|
||||||
|
|
||||||
|
ASSERT_GT(*level, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SandboxTest, CheckCompressInMemory) {
|
||||||
|
ZstdSapiSandbox sandbox;
|
||||||
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
|
||||||
|
ZstdApi api = ZstdApi(&sandbox);
|
||||||
|
|
||||||
|
std::string infile_s = GetTestFilePath("text");
|
||||||
|
|
||||||
|
absl::StatusOr<std::string> path =
|
||||||
|
sapi::CreateNamedTempFileAndClose("out.zstd");
|
||||||
|
ASSERT_THAT(path, IsOk()) << "Could not create temp output file";
|
||||||
|
std::string outfile_s =
|
||||||
|
sapi::file::JoinPath(sapi::file_util::fileops::GetCWD(), *path);
|
||||||
|
|
||||||
|
std::ifstream infile(infile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(infile.is_open());
|
||||||
|
|
||||||
|
std::ofstream outfile(outfile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(outfile.is_open());
|
||||||
|
|
||||||
|
absl::Status status = CompressInMemory(api, infile, outfile, 0);
|
||||||
|
ASSERT_THAT(status, IsOk()) << "Unable to compress file in memory";
|
||||||
|
|
||||||
|
ASSERT_LT(outfile.tellp(), infile.tellg());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SandboxTest, CheckDecompressInMemory) {
|
||||||
|
ZstdSapiSandbox sandbox;
|
||||||
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
|
||||||
|
ZstdApi api = ZstdApi(&sandbox);
|
||||||
|
|
||||||
|
std::string infile_s = GetTestFilePath("text.blob.zstd");
|
||||||
|
|
||||||
|
absl::StatusOr<std::string> path = sapi::CreateNamedTempFileAndClose("out");
|
||||||
|
ASSERT_THAT(path, IsOk()) << "Could not create temp output file";
|
||||||
|
std::string outfile_s =
|
||||||
|
sapi::file::JoinPath(sapi::file_util::fileops::GetCWD(), *path);
|
||||||
|
|
||||||
|
std::ifstream infile(infile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(infile.is_open());
|
||||||
|
|
||||||
|
std::ofstream outfile(outfile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(outfile.is_open());
|
||||||
|
|
||||||
|
absl::Status status = DecompressInMemory(api, infile, outfile);
|
||||||
|
ASSERT_THAT(status, IsOk()) << "Unable to decompress file in memory";
|
||||||
|
|
||||||
|
ASSERT_GT(outfile.tellp(), infile.tellg());
|
||||||
|
|
||||||
|
ASSERT_TRUE(CompareFiles(GetTestFilePath("text"), outfile_s));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SandboxTest, CheckCompressAndDecompressInMemory) {
|
||||||
|
ZstdSapiSandbox sandbox;
|
||||||
|
absl::Status status;
|
||||||
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
|
||||||
|
ZstdApi api = ZstdApi(&sandbox);
|
||||||
|
|
||||||
|
std::string infile_s = GetTestFilePath("text");
|
||||||
|
|
||||||
|
absl::StatusOr<std::string> path_middle =
|
||||||
|
sapi::CreateNamedTempFileAndClose("middle.zstd");
|
||||||
|
ASSERT_THAT(path_middle, IsOk()) << "Could not create temp output file";
|
||||||
|
std::string middle_s =
|
||||||
|
sapi::file::JoinPath(sapi::file_util::fileops::GetCWD(), *path_middle);
|
||||||
|
|
||||||
|
absl::StatusOr<std::string> path = sapi::CreateNamedTempFileAndClose("out");
|
||||||
|
ASSERT_THAT(path, IsOk()) << "Could not create temp output file";
|
||||||
|
std::string outfile_s =
|
||||||
|
sapi::file::JoinPath(sapi::file_util::fileops::GetCWD(), *path);
|
||||||
|
|
||||||
|
std::ifstream infile(infile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(infile.is_open());
|
||||||
|
|
||||||
|
std::ofstream outmiddle(middle_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(outmiddle.is_open());
|
||||||
|
|
||||||
|
status = CompressInMemory(api, infile, outmiddle, 0);
|
||||||
|
ASSERT_THAT(status, IsOk()) << "Unable to compress file in memory";
|
||||||
|
|
||||||
|
ASSERT_LT(outmiddle.tellp(), infile.tellg());
|
||||||
|
|
||||||
|
std::ifstream inmiddle(middle_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(inmiddle.is_open());
|
||||||
|
|
||||||
|
std::ofstream outfile(outfile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(outfile.is_open());
|
||||||
|
|
||||||
|
status = DecompressInMemory(api, inmiddle, outfile);
|
||||||
|
ASSERT_THAT(status, IsOk()) << "Unable to decompress file in memory";
|
||||||
|
|
||||||
|
ASSERT_TRUE(CompareFiles(infile_s, outfile_s));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SandboxTest, CheckCompressStream) {
|
||||||
|
ZstdSapiSandbox sandbox;
|
||||||
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
|
||||||
|
ZstdApi api = ZstdApi(&sandbox);
|
||||||
|
|
||||||
|
std::string infile_s = GetTestFilePath("text");
|
||||||
|
|
||||||
|
absl::StatusOr<std::string> path =
|
||||||
|
sapi::CreateNamedTempFileAndClose("out.zstd");
|
||||||
|
ASSERT_THAT(path, IsOk()) << "Could not create temp output file";
|
||||||
|
std::string outfile_s =
|
||||||
|
sapi::file::JoinPath(sapi::file_util::fileops::GetCWD(), *path);
|
||||||
|
|
||||||
|
std::ifstream infile(infile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(infile.is_open());
|
||||||
|
|
||||||
|
std::ofstream outfile(outfile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(outfile.is_open());
|
||||||
|
|
||||||
|
absl::Status status = CompressStream(api, infile, outfile, 0);
|
||||||
|
ASSERT_THAT(status, IsOk()) << "Unable to compress file in memory";
|
||||||
|
|
||||||
|
infile.clear();
|
||||||
|
|
||||||
|
ASSERT_LT(outfile.tellp(), infile.tellg());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SandboxTest, CheckDecompressStream) {
|
||||||
|
ZstdSapiSandbox sandbox;
|
||||||
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
|
||||||
|
ZstdApi api = ZstdApi(&sandbox);
|
||||||
|
|
||||||
|
std::string infile_s = GetTestFilePath("text.stream.zstd");
|
||||||
|
|
||||||
|
absl::StatusOr<std::string> path = sapi::CreateNamedTempFileAndClose("out");
|
||||||
|
ASSERT_THAT(path, IsOk()) << "Could not create temp output file";
|
||||||
|
std::string outfile_s =
|
||||||
|
sapi::file::JoinPath(sapi::file_util::fileops::GetCWD(), *path);
|
||||||
|
|
||||||
|
std::ifstream infile(infile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(infile.is_open());
|
||||||
|
|
||||||
|
std::ofstream outfile(outfile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(outfile.is_open());
|
||||||
|
|
||||||
|
absl::Status status = DecompressStream(api, infile, outfile);
|
||||||
|
ASSERT_THAT(status, IsOk()) << "Unable to decompress file in memory";
|
||||||
|
|
||||||
|
ASSERT_GT(outfile.tellp(), infile.tellg());
|
||||||
|
|
||||||
|
ASSERT_TRUE(CompareFiles(GetTestFilePath("text"), outfile_s));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SandboxTest, CheckCompressAndDecompressStream) {
|
||||||
|
ZstdSapiSandbox sandbox;
|
||||||
|
absl::Status status;
|
||||||
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
|
||||||
|
ZstdApi api = ZstdApi(&sandbox);
|
||||||
|
|
||||||
|
std::string infile_s = GetTestFilePath("text");
|
||||||
|
|
||||||
|
absl::StatusOr<std::string> path_middle =
|
||||||
|
sapi::CreateNamedTempFileAndClose("middle.zstd");
|
||||||
|
ASSERT_THAT(path_middle, IsOk()) << "Could not create temp output file";
|
||||||
|
std::string middle_s =
|
||||||
|
sapi::file::JoinPath(sapi::file_util::fileops::GetCWD(), *path_middle);
|
||||||
|
|
||||||
|
absl::StatusOr<std::string> path = sapi::CreateNamedTempFileAndClose("out");
|
||||||
|
ASSERT_THAT(path, IsOk()) << "Could not create temp output file";
|
||||||
|
std::string outfile_s =
|
||||||
|
sapi::file::JoinPath(sapi::file_util::fileops::GetCWD(), *path);
|
||||||
|
|
||||||
|
std::ifstream infile(infile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(infile.is_open());
|
||||||
|
|
||||||
|
std::ofstream outmiddle(middle_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(outmiddle.is_open());
|
||||||
|
|
||||||
|
status = CompressStream(api, infile, outmiddle, 0);
|
||||||
|
ASSERT_THAT(status, IsOk()) << "Unable to compress file in memory";
|
||||||
|
|
||||||
|
infile.clear();
|
||||||
|
ASSERT_LT(outmiddle.tellp(), infile.tellg());
|
||||||
|
|
||||||
|
std::ifstream inmiddle(middle_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(inmiddle.is_open());
|
||||||
|
|
||||||
|
std::ofstream outfile(outfile_s, std::ios::binary);
|
||||||
|
ASSERT_TRUE(outfile.is_open());
|
||||||
|
|
||||||
|
status = DecompressStream(api, inmiddle, outfile);
|
||||||
|
ASSERT_THAT(status, IsOk()) << "Unable to decompress file in memory";
|
||||||
|
|
||||||
|
ASSERT_TRUE(CompareFiles(infile_s, outfile_s));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
241
contrib/zstd/utils/utils_zstd.cc
Normal file
241
contrib/zstd/utils/utils_zstd.cc
Normal file
|
@ -0,0 +1,241 @@
|
||||||
|
// Copyright 2022 Google LLC
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "contrib/zstd/sandboxed.h"
|
||||||
|
|
||||||
|
static const size_t kFileMaxSize = 1024 * 1024 * 1024; // 1GB
|
||||||
|
|
||||||
|
std::streamsize GetStreamSize(std::ifstream& stream) {
|
||||||
|
stream.seekg(0, std::ios_base::end);
|
||||||
|
std::streamsize ssize = stream.tellg();
|
||||||
|
stream.seekg(0, std::ios_base::beg);
|
||||||
|
|
||||||
|
return ssize;
|
||||||
|
}
|
||||||
|
|
||||||
|
absl::Status CompressInMemory(ZstdApi& api, std::ifstream& in_file,
|
||||||
|
std::ofstream& out_stream, int level) {
|
||||||
|
std::streamsize ssize = GetStreamSize(in_file);
|
||||||
|
sapi::v::Array<uint8_t> inbuf(ssize);
|
||||||
|
in_file.read(reinterpret_cast<char*>(inbuf.GetData()), ssize);
|
||||||
|
if (in_file.gcount() != ssize) {
|
||||||
|
return absl::UnavailableError("Unable to read file");
|
||||||
|
}
|
||||||
|
|
||||||
|
SAPI_ASSIGN_OR_RETURN(size_t size, api.ZSTD_compressBound(inbuf.GetSize()));
|
||||||
|
sapi::v::Array<uint8_t> outbuf(size);
|
||||||
|
|
||||||
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
|
size_t outsize,
|
||||||
|
api.ZSTD_compress(outbuf.PtrAfter(), size, inbuf.PtrBefore(),
|
||||||
|
inbuf.GetSize(), level));
|
||||||
|
SAPI_ASSIGN_OR_RETURN(int iserr, api.ZSTD_isError(outsize))
|
||||||
|
if (iserr) {
|
||||||
|
return absl::UnavailableError("Unable to compress file");
|
||||||
|
}
|
||||||
|
|
||||||
|
out_stream.write(reinterpret_cast<char*>(outbuf.GetData()), outsize);
|
||||||
|
if (!out_stream.good()) {
|
||||||
|
return absl::UnavailableError("Unable to write file");
|
||||||
|
}
|
||||||
|
|
||||||
|
return absl::OkStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
absl::Status DecompressInMemory(ZstdApi& api, std::ifstream& in_file,
|
||||||
|
std::ofstream& out_stream) {
|
||||||
|
int iserr;
|
||||||
|
std::streamsize ssize = GetStreamSize(in_file);
|
||||||
|
sapi::v::Array<uint8_t> inbuf(ssize);
|
||||||
|
in_file.read(reinterpret_cast<char*>(inbuf.GetData()), ssize);
|
||||||
|
if (in_file.gcount() != ssize) {
|
||||||
|
return absl::UnavailableError("Unable to read file");
|
||||||
|
}
|
||||||
|
|
||||||
|
SAPI_ASSIGN_OR_RETURN(size_t size, api.ZSTD_getFrameContentSize(
|
||||||
|
inbuf.PtrBefore(), inbuf.GetSize()));
|
||||||
|
if (size > kFileMaxSize) {
|
||||||
|
return absl::UnavailableError("File to large");
|
||||||
|
}
|
||||||
|
SAPI_ASSIGN_OR_RETURN(iserr, api.ZSTD_isError(size));
|
||||||
|
if (iserr) {
|
||||||
|
return absl::UnavailableError("Unable to decompress file");
|
||||||
|
}
|
||||||
|
sapi::v::Array<uint8_t> outbuf(size);
|
||||||
|
|
||||||
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
|
size_t desize, api.ZSTD_decompress(outbuf.PtrAfter(), size,
|
||||||
|
inbuf.PtrBefore(), inbuf.GetSize()));
|
||||||
|
SAPI_ASSIGN_OR_RETURN(iserr, api.ZSTD_isError(desize));
|
||||||
|
if (iserr) {
|
||||||
|
return absl::UnavailableError("Unable to decompress file");
|
||||||
|
}
|
||||||
|
|
||||||
|
out_stream.write(reinterpret_cast<char*>(outbuf.GetData()), desize);
|
||||||
|
if (!out_stream.good()) {
|
||||||
|
return absl::UnavailableError("Unable to write file");
|
||||||
|
}
|
||||||
|
|
||||||
|
return absl::OkStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
absl::Status CompressStream(ZstdApi& api, std::ifstream& in_file,
|
||||||
|
std::ofstream& out_stream, int level) {
|
||||||
|
int iserr;
|
||||||
|
|
||||||
|
// Create necessary buffers.
|
||||||
|
SAPI_ASSIGN_OR_RETURN(size_t inbuf_size, api.ZSTD_CStreamInSize());
|
||||||
|
SAPI_ASSIGN_OR_RETURN(size_t outbuf_size, api.ZSTD_CStreamOutSize());
|
||||||
|
sapi::v::Array<uint8_t> inbuf(inbuf_size);
|
||||||
|
sapi::v::Array<uint8_t> outbuf(outbuf_size);
|
||||||
|
|
||||||
|
if (!api.GetSandbox()->Allocate(&inbuf).ok() ||
|
||||||
|
!api.GetSandbox()->Allocate(&outbuf).ok()) {
|
||||||
|
return absl::UnavailableError("Unable to allocate buffors");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Zstd context.
|
||||||
|
SAPI_ASSIGN_OR_RETURN(ZSTD_DCtx * dctx, api.ZSTD_createDCtx());
|
||||||
|
sapi::v::RemotePtr rdctx(dctx);
|
||||||
|
|
||||||
|
SAPI_ASSIGN_OR_RETURN(iserr, api.ZSTD_CCtx_setParameter(
|
||||||
|
&rdctx, ZSTD_c_compressionLevel, level));
|
||||||
|
if (!iserr) {
|
||||||
|
return absl::UnavailableError("Unable to set parameter");
|
||||||
|
}
|
||||||
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
|
iserr, api.ZSTD_CCtx_setParameter(&rdctx, ZSTD_c_checksumFlag, 1));
|
||||||
|
if (!iserr) {
|
||||||
|
return absl::UnavailableError("Unable to set parameter");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compress.
|
||||||
|
while (in_file) {
|
||||||
|
in_file.read(reinterpret_cast<char*>(inbuf.GetData()), inbuf_size);
|
||||||
|
|
||||||
|
if (!api.GetSandbox()->TransferToSandboxee(&inbuf).ok()) {
|
||||||
|
return absl::UnavailableError("Unable to transfer data");
|
||||||
|
}
|
||||||
|
|
||||||
|
sapi::v::Struct<ZSTD_inBuffer_s> struct_in;
|
||||||
|
struct_in.mutable_data()->src = static_cast<uint8_t*>(inbuf.GetRemote());
|
||||||
|
struct_in.mutable_data()->pos = 0;
|
||||||
|
struct_in.mutable_data()->size = in_file.gcount();
|
||||||
|
|
||||||
|
ZSTD_EndDirective mode = ZSTD_e_continue;
|
||||||
|
if (in_file.gcount() < inbuf_size) {
|
||||||
|
mode = ZSTD_e_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isdone = false;
|
||||||
|
while (!isdone) {
|
||||||
|
sapi::v::Struct<ZSTD_outBuffer_s> struct_out;
|
||||||
|
struct_out.mutable_data()->dst =
|
||||||
|
static_cast<uint8_t*>(outbuf.GetRemote());
|
||||||
|
struct_out.mutable_data()->pos = 0;
|
||||||
|
struct_out.mutable_data()->size = outbuf.GetSize();
|
||||||
|
|
||||||
|
SAPI_ASSIGN_OR_RETURN(size_t remaining, api.ZSTD_compressStream2(
|
||||||
|
&rdctx, struct_out.PtrBoth(),
|
||||||
|
struct_in.PtrBoth(), mode));
|
||||||
|
SAPI_ASSIGN_OR_RETURN(int iserr, api.ZSTD_isError(remaining))
|
||||||
|
if (iserr) {
|
||||||
|
return absl::UnavailableError("Unable to decompress file");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!api.GetSandbox()->TransferFromSandboxee(&outbuf).ok()) {
|
||||||
|
return absl::UnavailableError("Unable to transfer data from");
|
||||||
|
}
|
||||||
|
out_stream.write(reinterpret_cast<char*>(outbuf.GetData()),
|
||||||
|
struct_out.mutable_data()->pos);
|
||||||
|
if (!out_stream.good()) {
|
||||||
|
return absl::UnavailableError("Unable to write file");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode == ZSTD_e_continue) {
|
||||||
|
isdone = (struct_in.mutable_data()->pos == in_file.gcount());
|
||||||
|
} else {
|
||||||
|
isdone = (remaining == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
api.ZSTD_freeDCtx(&rdctx).IgnoreError();
|
||||||
|
|
||||||
|
return absl::OkStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
absl::Status DecompressStream(ZstdApi& api, std::ifstream& in_file,
|
||||||
|
std::ofstream& out_stream) {
|
||||||
|
// Create necessary buffers.
|
||||||
|
SAPI_ASSIGN_OR_RETURN(size_t inbuf_size, api.ZSTD_CStreamInSize());
|
||||||
|
SAPI_ASSIGN_OR_RETURN(size_t outbuf_size, api.ZSTD_CStreamOutSize());
|
||||||
|
sapi::v::Array<uint8_t> inbuf(inbuf_size);
|
||||||
|
sapi::v::Array<uint8_t> outbuf(outbuf_size);
|
||||||
|
|
||||||
|
if (!api.GetSandbox()->Allocate(&inbuf).ok() ||
|
||||||
|
!api.GetSandbox()->Allocate(&outbuf).ok()) {
|
||||||
|
return absl::UnavailableError("Unable to allocate buffors");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Zstd context.
|
||||||
|
SAPI_ASSIGN_OR_RETURN(ZSTD_DCtx * dctx, api.ZSTD_createDCtx());
|
||||||
|
sapi::v::RemotePtr rdctx(dctx);
|
||||||
|
|
||||||
|
// Decompress.
|
||||||
|
while (in_file) {
|
||||||
|
in_file.read(reinterpret_cast<char*>(inbuf.GetData()), inbuf_size);
|
||||||
|
|
||||||
|
if (!api.GetSandbox()->TransferToSandboxee(&inbuf).ok()) {
|
||||||
|
return absl::UnavailableError("Unable to transfer data");
|
||||||
|
}
|
||||||
|
|
||||||
|
sapi::v::Struct<ZSTD_inBuffer_s> struct_in;
|
||||||
|
*struct_in.mutable_data() = {static_cast<uint8_t*>(inbuf.GetRemote()),
|
||||||
|
(size_t)in_file.gcount(), 0};
|
||||||
|
|
||||||
|
bool isdone = false;
|
||||||
|
while (struct_in.mutable_data()->pos < in_file.gcount()) {
|
||||||
|
sapi::v::Struct<ZSTD_outBuffer_s> struct_out;
|
||||||
|
*struct_out.mutable_data() = {static_cast<uint8_t*>(outbuf.GetRemote()),
|
||||||
|
(size_t)outbuf.GetSize(), 0};
|
||||||
|
|
||||||
|
SAPI_ASSIGN_OR_RETURN(
|
||||||
|
size_t ret, api.ZSTD_decompressStream(&rdctx, struct_out.PtrBoth(),
|
||||||
|
struct_in.PtrBoth()));
|
||||||
|
SAPI_ASSIGN_OR_RETURN(int iserr, api.ZSTD_isError(ret))
|
||||||
|
if (iserr) {
|
||||||
|
return absl::UnavailableError("Unable to decompress file");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!api.GetSandbox()->TransferFromSandboxee(&outbuf).ok()) {
|
||||||
|
return absl::UnavailableError("Unable to transfer data from");
|
||||||
|
}
|
||||||
|
|
||||||
|
out_stream.write(reinterpret_cast<char*>(outbuf.GetData()),
|
||||||
|
struct_out.mutable_data()->pos);
|
||||||
|
if (!out_stream.good()) {
|
||||||
|
return absl::UnavailableError("Unable to write file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
api.ZSTD_freeDCtx(&rdctx).IgnoreError();
|
||||||
|
|
||||||
|
return absl::OkStatus();
|
||||||
|
}
|
33
contrib/zstd/utils/utils_zstd.h
Normal file
33
contrib/zstd/utils/utils_zstd.h
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
// Copyright 2022 Google LLC
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#ifndef CONTRIB_ZSTD_UTILS_UTILS_ZSTD_H_
|
||||||
|
#define CONTRIB_ZSTD_UTILS_UTILS_ZSTD_H_
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "contrib/zstd/sandboxed.h"
|
||||||
|
|
||||||
|
absl::Status CompressInMemory(ZstdApi& api, std::ifstream& in_file,
|
||||||
|
std::ofstream& out_file, int level);
|
||||||
|
absl::Status DecompressInMemory(ZstdApi& api, std::ifstream& in_file,
|
||||||
|
std::ofstream& out_file);
|
||||||
|
|
||||||
|
absl::Status CompressStream(ZstdApi& api, std::ifstream& in_file,
|
||||||
|
std::ofstream& out_file, int level);
|
||||||
|
absl::Status DecompressStream(ZstdApi& api, std::ifstream& in_file,
|
||||||
|
std::ofstream& out_file);
|
||||||
|
|
||||||
|
#endif // CONTRIB_ZSTD_UTILS_UTILS_ZSTD_H_
|
Loading…
Reference in New Issue
Block a user