Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sandbox libxls #160

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contrib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(SAPI_CONTRIB_SANDBOXES
hunspell
jsonnet
libidn2
libxls
libzip
pffft
turbojpeg
Expand Down
1 change: 1 addition & 0 deletions contrib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Directory | Project
`hunspell/` | Hunspell - The most popular spellchecking library | [github.com/hunspell/hunspell](https://github.com/hunspell/hunspell) | CMake
`jsonnet/` | Jsonnet - The Data Templating Language | [github.com/google/jsonnet](https://github.com/google/jsonnet) | CMake
`libidn2/` | libidn2 - GNU IDN library | [www.gnu.org/software/libidn/#libidn2](https://www.gnu.org/software/libidn/#libidn2) | CMake
`libxls/` | libxls- Read binary Excel files from C/C++ | [https://github.com/libxls/libxls](https://github.com/libxls/libxls) | CMake
`libzip/` | libzip - operations on zip archives | [github.com/nih-at/libzip](https://github.com/nih-at/libzip) | CMake
`pffft/` | PFFFT - a pretty fast Fourier Transform | [bitbucket.org/jpommier/pffft.git](https://bitbucket.org/jpommier/pffft.git) | CMake
`turbojpeg/` | High-level JPEG library | [libjpeg-turbo.org/About/TurboJPEG](https://libjpeg-turbo.org/About/TurboJPEG) | CMake
Expand Down
117 changes: 117 additions & 0 deletions contrib/libxls/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# 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..3.22)

project(sapi_libxls)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(SAPI_ROOT "" CACHE PATH "Path to the Sandboxed API source tree")

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(libxls
GIT_REPOSITORY https://github.com/libxls/libxls.git
GIT_TAG 448240067919707eb95fb009f76f3fdb439b1427
)

FetchContent_GetProperties(libxls)
if(NOT libxls_POPULATED)
FetchContent_Populate(libxls)
set(libxls_STATUS_FILE "${libxls_SOURCE_DIR}/config.status")
if(EXISTS "${libxls_STATUS_FILE}")
file(SHA256 "${libxls_STATUS_FILE}" _sapi_CONFIG_STATUS)
endif()
if(NOT _sapi_CONFIG_STATUS STREQUAL "${libxls_CONFIG_STATUS}")
message("-- Configuring libxls...")
execute_process(
COMMAND autoreconf -i
WORKING_DIRECTORY "${libxls_SOURCE_DIR}"
RESULT_VARIABLE _sapi_libxls_autoreconf_result
)
if(NOT _sapi_libxls_autoreconf_result EQUAL "0")
message(FATAL_ERROR "Configuration for libxls failed: "
"${_sapi_libxls_autoreconf_result}")
endif()
execute_process(
COMMAND ./configure --disable-dependency-tracking
WORKING_DIRECTORY "${libxls_SOURCE_DIR}"
RESULT_VARIABLE _sapi_libxls_config_result
)
if(NOT _sapi_libxls_config_result EQUAL "0")
message(FATAL_ERROR "Configuration for libxls failed: "
"${_sapi_libxls_config_result}")
endif()
file(SHA256 "${libxls_SOURCE_DIR}/config.status" _sapi_CONFIG_STATUS)
set(libxls_CONFIG_STATUS "${_sapi_CONFIG_STATUS}" CACHE INTERNAL "")
endif()
endif()

add_library(libxls STATIC
"${libxls_SOURCE_DIR}/src/endian.c"
"${libxls_SOURCE_DIR}/src/locale.c"
"${libxls_SOURCE_DIR}/src/ole.c"
"${libxls_SOURCE_DIR}/src/xls.c"
"${libxls_SOURCE_DIR}/src/xlstool.c"
)

target_include_directories(libxls PUBLIC
"${libxls_SOURCE_DIR}"
"${libxls_SOURCE_DIR}/include"
)

configure_file(xls.gen.h.in xls.gen.h)

add_sapi_library(
sapi_libxls

FUNCTIONS
xls_open_file

xls_getWorkSheet
xls_parseWorkSheet

xls_cell

xls_close_WS
xls_close_WB

xls_getError

INPUTS
"${CMAKE_BINARY_DIR}/xls.gen.h"

LIBRARY libxls
LIBRARY_NAME Libxls
NAMESPACE ""
)
add_library(sapi_contrib::libxls ALIAS sapi_libxls)
target_include_directories(sapi_libxls 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/libxls/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_minixls

main.cc
../utils/utils_libxls.cc
)

target_link_libraries(
sapi_minixls PRIVATE

sapi_libxls
sapi::sapi
absl::flags_parse
)
89 changes: 89 additions & 0 deletions contrib/libxls/example/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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 <fcntl.h>
#include <unistd.h>

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "contrib/libxls/sandboxed.h"
#include "contrib/libxls/utils/utils_libxls.h"

ABSL_FLAG(uint32_t, sheet, 0, "sheet number");

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() != 2) {
std::cerr << "Usage:\n " << prog_name << " INPUT\n";
return EXIT_FAILURE;
}

LibxlsSapiSandbox sandbox(args[1]);
if (!sandbox.Init().ok()) {
std::cerr << "Unable to start sandbox\n";
return EXIT_FAILURE;
}

absl::StatusOr<LibXlsWorkbook> wb = LibXlsWorkbook::Open(&sandbox, args[1]);

uint32_t nr_sheet = absl::GetFlag(FLAGS_sheet);

absl::StatusOr<LibXlsSheet> sheet = wb->OpenSheet(nr_sheet);
if (!sheet.ok()) {
std::cerr << "Unable to switch sheet: ";
std::cerr << sheet.status() << "\n";
return EXIT_FAILURE;
}

for (size_t row = 0; row < sheet->GetRowCount(); ++row) {
for (size_t col = 0; col < sheet->GetColCount(); ++col) {
absl::StatusOr<LibXlsCell> cell = sheet->GetCell(row, col);
if (!cell.ok()) {
std::cerr << "Unable to get cell: ";
std::cerr << cell.status() << "\n";
return EXIT_FAILURE;
}
switch (cell->type) {
case XLS_RECORD_NUMBER:
std::cout << std::setw(16) << std::get<double>(cell->value) << " | ";
break;
case XLS_RECORD_STRING:
std::cout << std::setw(16) << std::get<std::string>(cell->value)
<< " | ";
break;
case XLS_RECORD_BOOL:
std::cout << std::setw(16) << std::get<bool>(cell->value) << " | ";
break;
case XLS_RECORD_BLANK:
std::cout << std::setw(16) << " | ";
break;
case XLS_RECORD_ERROR:
std::cout << "error"
<< "\n";
break;
}
}
std::cout << "\n";
}
return EXIT_SUCCESS;
}
Binary file added contrib/libxls/files/t1.xls
Binary file not shown.
Binary file added contrib/libxls/files/t2.xls
Binary file not shown.
Binary file added contrib/libxls/files/t3.xls
Binary file not shown.
48 changes: 48 additions & 0 deletions contrib/libxls/sandboxed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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_libxls.sapi.h" // NOLINT(build/include)

class LibxlsSapiSandbox : public LibxlsSandbox {
public:
explicit LibxlsSapiSandbox(std::string filename)
: filename_(std::move(filename)) {}

std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
return sandbox2::PolicyBuilder()
.AllowDynamicStartup()
.AllowOpen()
.AllowRead()
.AllowWrite()
.AllowSystemMalloc()
.AllowExit()
.AllowSyscall(__NR_recvmsg)
.AddFile(filename_)
.BuildOrDie();
}

private:
std::string filename_;
};

#endif // CONTRIB_LIBXLS_SANDBOXED_H_
33 changes: 33 additions & 0 deletions contrib/libxls/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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_libxls_test

libxls_test.cc
../utils/utils_libxls.cc
)


target_link_libraries(
sapi_libxls_test PRIVATE

sapi_libxls
sapi::test_main
sapi::temp_file
)

gtest_discover_tests(sapi_libxls_test PROPERTIES ENVIRONMENT "TEST_FILES_DIR=${PROJECT_SOURCE_DIR}/files")
Loading