Skip to content

Commit

Permalink
Sandbox ZStandard
Browse files Browse the repository at this point in the history
  • Loading branch information
oshogbo committed Feb 2, 2022
1 parent 87177de commit 12b71e7
Show file tree
Hide file tree
Showing 12 changed files with 887 additions and 3 deletions.
13 changes: 10 additions & 3 deletions contrib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
This directory contains reusable Sandboxed API integrations with external
libraries.

## Projects Sandboxed

Project | Home Page | Directory
------------------------------------------------ | ---------------------------------------------------------------- | -----------
Zstandard - Fast real-time compression algorithm | [github.com/facebook/zstd](https://github.com/facebook/zstd) | zstd

## Projects Shipping with Sandboxed API Sandboxes

Project | Home Page | Integration
--------------------------------------- | ---------------------------------------------------------------- | -----------
YARA - The pattern matching swiss knife | [github.com/VirusTotal/yara](https://github.com/VirusTotal/yara) | Bazel
Project | Home Page | Integration
------------------------------------------------ | ---------------------------------------------------------------- | -----------
YARA - The pattern matching swiss knife | [github.com/VirusTotal/yara](https://github.com/VirusTotal/yara) | Bazel

92 changes: 92 additions & 0 deletions contrib/zstd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# 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)

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
)

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 ""
)

target_include_directories(sapi_zstd INTERFACE
"${PROJECT_BINARY_DIR}"
)

if(SAPI_ENABLE_EXAMPLES)
add_subdirectory(example)
endif()

if(SAPI_ENABLE_TESTS)
add_subdirectory(test)
endif()
28 changes: 28 additions & 0 deletions contrib/zstd/example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
78 changes: 78 additions & 0 deletions contrib/zstd/example/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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 <fstream>
#include <iostream>
#include <string>

#include "../sandboxed.h"
#include "../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;
}
Loading

0 comments on commit 12b71e7

Please sign in to comment.