diff --git a/oss-internship-2020/libtiff/CMakeLists.txt b/oss-internship-2020/libtiff/CMakeLists.txt index b659fd3..59175d1 100644 --- a/oss-internship-2020/libtiff/CMakeLists.txt +++ b/oss-internship-2020/libtiff/CMakeLists.txt @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.12) project(sandboxed_libtiff CXX) @@ -120,10 +120,10 @@ target_include_directories(tiff_sapi INTERFACE "${PROJECT_BINARY_DIR}" # To find the generated SAPI header ) -if (TIFF_SAPI_ENABLE_EXAMPLES) +#if (TIFF_SAPI_ENABLE_EXAMPLES) add_subdirectory(example) -endif() +#endif() -if (TIFF_SAPI_ENABLE_TESTS) +#if (TIFF_SAPI_ENABLE_TESTS) add_subdirectory(test) -endif() +#endif() diff --git a/oss-internship-2020/libtiff/README.md b/oss-internship-2020/libtiff/README.md index 4e253db..75ba011 100644 --- a/oss-internship-2020/libtiff/README.md +++ b/oss-internship-2020/libtiff/README.md @@ -19,4 +19,4 @@ You should make sure the libtiff submodule is cloned. #### to run tests: `./test/tests` -you also can use sandbox flags `sandbox2_danger_danger_permit_all` and `sandbox2_danger_danger_permit_all_and_log` +You also can use sandbox flags `sandbox2_danger_danger_permit_all` and `sandbox2_danger_danger_permit_all_and_log`. diff --git a/oss-internship-2020/libtiff/example/main_sandboxed.cc b/oss-internship-2020/libtiff/example/main_sandboxed.cc index 5b61acd..fe59e8b 100644 --- a/oss-internship-2020/libtiff/example/main_sandboxed.cc +++ b/oss-internship-2020/libtiff/example/main_sandboxed.cc @@ -12,36 +12,39 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include -#include +#include +#include #include "../sandboxed.h" #include "sandboxed_api/sandbox2/util/fileops.h" #include "sandboxed_api/sandbox2/util/path.h" -#include "tiffio.h" +#include "sandboxed_api/vars.h" +#include "tiffio.h" // NOLINT(build/include) -/* sapi functions: - * TIFFTileSize - * TIFFOpen - * TIFFReadEncodedTile - * TIFFSetField - * TIFFClose - * TIFFReadRGBATile - * TIFFGetField - */ +// sapi functions: +// TIFFTileSize +// TIFFOpen +// TIFFReadEncodedTile +// TIFFSetField +// TIFFClose +// TIFFReadRGBATile +// TIFFGetField -static const std::vector cluster_0 = {0, 0, 2, 0, 138, 139}; -static const std::vector cluster_64 = {0, 0, 9, 6, 134, 119}; -static const std::vector cluster_128 = {44, 40, 63, 59, 230, 95}; +constexpr std::array kCluster0 = {0, 0, 2, 0, 138, 139}; +constexpr std::array kCluster64 = {0, 0, 9, 6, 134, 119}; +constexpr std::array kCluster128 = {44, 40, 63, 59, 230, 95}; -static int CheckCluster(int cluster, - const sapi::v::Array &buffer, - const std::vector &expected_cluster) { - unsigned char* target = buffer.GetData() + cluster * 6; +namespace { + +absl::Status CheckCluster(int cluster, const sapi::v::Array& buffer, + const std::array& expected_cluster) { + uint8_t* target = buffer.GetData() + cluster * 6; if (!std::memcmp(target, expected_cluster.data(), 6)) { - return 0; + return absl::OkStatus(); } std::cerr << "Cluster " << cluster << " did not match expected results.\n" @@ -53,31 +56,36 @@ static int CheckCluster(int cluster, << "\t" << target[5] << "\t" << target[2] << "\t" << target[3] << '\n'; - return 1; + return absl::InternalError(absl::StrCat( + "Cluster ", cluster, " did not match expected results.\n", "Expect: ", + expected_cluster[0], "\t", expected_cluster[1], "\t", expected_cluster[4], + "\t", expected_cluster[5], "\t", expected_cluster[2], "\t", + expected_cluster[3], "\n" + )); } -static int CheckRgbPixel(int pixel, int min_red, int max_red, int min_green, - int max_green, int min_blue, int max_blue, - const sapi::v::Array &buffer) { - unsigned char* rgb = buffer.GetData() + 3 * pixel; +absl::Status CheckRgbPixel(int pixel, int min_red, int max_red, int min_green, + int max_green, int min_blue, int max_blue, + const sapi::v::Array& buffer) { + uint8_t* rgb = buffer.GetData() + 3 * pixel; if (rgb[0] >= min_red && rgb[0] <= max_red && rgb[1] >= min_green && rgb[1] <= max_green && rgb[2] >= min_blue && rgb[2] <= max_blue) { - return 0; + return absl::OkStatus(); } - std::cerr << "Pixel " << pixel << " did not match expected results.\n" - << "Got R=" << rgb[0] << " (expected " << min_red << ".." << max_red - << "), G=" << rgb[1] << " (expected " << min_green << ".." - << max_green << "), B=" << rgb[2] << " (expected " << min_blue - << ".." << max_blue << ")\n"; - return 1; + return absl::InternalError(absl::StrCat( + "Pixel ", pixel, " did not match expected results.\n", "Got R=", rgb[0], + " (expected ", min_red, "..", max_red, "), G=", rgb[1], " (expected ", + min_green, "..", max_green, "), B=", rgb[2], " (expected ", min_blue, "..", + max_blue, ")\n" + )); } -static int CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green, - int max_green, int min_blue, int max_blue, - int min_alpha, int max_alpha, - const sapi::v::Array &buffer) { +absl::Status CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green, + int max_green, int min_blue, int max_blue, + int min_alpha, int max_alpha, + const sapi::v::Array& buffer) { // RGBA images are upside down - adjust for normal ordering int adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128; uint32 rgba = buffer[adjusted_pixel]; @@ -89,37 +97,33 @@ static int CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green, TIFFGetB(rgba) <= (uint32)max_blue && TIFFGetA(rgba) >= (uint32)min_alpha && TIFFGetA(rgba) <= (uint32)max_alpha) { - return 0; + return absl::OkStatus(); } - std::cerr << "Pixel " << pixel << " did not match expected results.\n" - << "Got R=" << TIFFGetR(rgba) << " (expected " << min_red << ".." - << max_red << "), G=" << TIFFGetG(rgba) << " (expected " - << min_green << ".." << max_green << "), B=" << TIFFGetB(rgba) - << " (expected " << min_blue << ".." << max_blue - << "), A=" << TIFFGetA(rgba) << " (expected " << min_alpha << ".." - << max_alpha << ")\n"; - return 1; + return absl::InternalError(absl::StrCat( + "Pixel ", pixel, " did not match expected results.\n", "Got R=", + TIFFGetR(rgba), " (expected ", min_red, "..", max_red, "), G=", + TIFFGetG(rgba), " (expected ", min_green, "..", max_green, "), B=", + TIFFGetB(rgba), " (expected ", min_blue, "..", max_blue, "), A=", + TIFFGetA(rgba), " (expected ", min_alpha, "..", max_alpha, ")\n" + )); } -std::string GetFilePath(const std::string &dir, const std::string &filename) { + +} // namespace + +std::string GetFilePath(const std::string& dir, const std::string& filename) { return sandbox2::file::JoinPath(dir, "test", "images", filename); } -std::string GetCWD() { - char cwd[PATH_MAX]; - getcwd(cwd, sizeof(cwd)); - return cwd; -} - std::string GetFilePath(const std::string filename) { - std::string cwd = GetCWD(); + std::string cwd = sandbox2::file_util::fileops::GetCWD(); auto find = cwd.rfind("build"); std::string project_path; if (find == std::string::npos) { - std::cerr << "Something went wrong: CWD don't contain build dir. " - << "Please run tests from build dir or send project dir as a " - << "parameter: ./sandboxed /absolute/path/to/project/dir \n"; + LOG(ERROR) << "Something went wrong: CWD don't contain build dir. " + << "Please run tests from build dir or send project dir as a " + << "parameter: ./sandboxed /absolute/path/to/project/dir \n"; project_path = cwd; } else { project_path = cwd.substr(0, find); @@ -128,6 +132,173 @@ std::string GetFilePath(const std::string filename) { return sandbox2::file::JoinPath(project_path, "test", "images", filename); } +absl::Status LibTIFFMain(const std::string& srcfile) { + // without addDir to sandbox. to add dir use + // sandbox(absolute_path_to_dir, srcfile) or + // sandbox(absolute_path_to_dir). file and dir should be exists. + // srcfile must also be absolute_path_to_file + TiffSapiSandbox sandbox("", srcfile); + + // initialize sapi vars after constructing TiffSapiSandbox + sapi::v::UShort h, v; + sapi::StatusOr status_or_tif; + sapi::StatusOr status_or_int; + sapi::StatusOr status_or_long; + absl::Status status; + + status = sandbox.Init(); + + SAPI_RETURN_IF_ERROR(sandbox.Init()); + + TiffApi api(&sandbox); + sapi::v::ConstCStr srcfile_var(srcfile.c_str()); + sapi::v::ConstCStr r_var("r"); + + SAPI_ASSIGN_OR_RETURN( + status_or_tif, api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore())); + + sapi::v::RemotePtr tif(status_or_tif.value()); + if (!tif.GetValue()) { + // tif is NULL + return absl::InternalError(absl::StrCat("Could not open ", srcfile)); + } + + SAPI_ASSIGN_OR_RETURN( + status_or_int, api.TIFFGetField2(&tif, TIFFTAG_YCBCRSUBSAMPLING, + h.PtrBoth(), v.PtrBoth())); + if (status_or_int.value() == 0 || h.GetValue() != 2 || v.GetValue() != 2) { + return absl::InternalError("Could not retrieve subsampling tag"); + } + + SAPI_ASSIGN_OR_RETURN( + status_or_long, api.TIFFTileSize(&tif)); + if (status_or_long.value() != 24576) { + return absl::InternalError(absl::StrCat("tiles are ", + status_or_long.value(), + " bytes\n")); + } + tsize_t sz = status_or_long.value(); + + sapi::v::Array buffer_(sz); + SAPI_ASSIGN_OR_RETURN( + status_or_long, api.TIFFReadEncodedTile(&tif, 9, buffer_.PtrBoth(), sz)); + if (status_or_long.value() != sz) { + return absl::InternalError(absl::StrCat( + "Did not get expected result code from TIFFReadEncodedTile(): ", + status_or_long.value(), " instead of ", sz + )); + } + + unsigned pixel_status = 1; + if (status = CheckCluster(0, buffer_, kCluster0); !status.ok()) { + LOG(ERROR) << "CheckCluster failed:\n" << status.ToString(); + } + pixel_status &= status.ok(); + + if (status = CheckCluster(64, buffer_, kCluster64); !status.ok()) { + LOG(ERROR) << "CheckCluster failed:\n" << status.ToString(); + } + pixel_status &= status.ok(); + + if (status = CheckCluster(128, buffer_, kCluster128); !status.ok()) { + LOG(ERROR) << "CheckCluster failed:\n" << status.ToString(); + } + pixel_status &= status.ok(); + + if (!pixel_status) { + return absl::InternalError("unexpected pixel_status value"); + } + + SAPI_ASSIGN_OR_RETURN( + status_or_int, api.TIFFSetFieldU1(&tif, TIFFTAG_JPEGCOLORMODE, + JPEGCOLORMODE_RGB)); + if (!status_or_int.value()) { + return absl::InternalError("TIFFSetFieldU1 not available"); + } + + SAPI_ASSIGN_OR_RETURN( + status_or_long, api.TIFFTileSize(&tif)); + if (status_or_long.value() != 128 * 128 * 3) { + return absl::InternalError(absl::StrCat("tiles are ", + status_or_long.value(), + " bytes")); + } + sz = status_or_long.value(); + + sapi::v::Array buffer2_(sz); + + SAPI_ASSIGN_OR_RETURN( + status_or_long, api.TIFFReadEncodedTile(&tif, 9, buffer2_.PtrBoth(), sz)); + if (status_or_long.value() != sz) { + return absl::InternalError(absl::StrCat( + "Did not get expected result code from TIFFReadEncodedTile(): ", + status_or_long.value(), " instead of ", sz + )); + } + + pixel_status = 1; + if (status = CheckRgbPixel(0, 15, 18, 0, 0, 18, 41, buffer2_); + !status.ok()) { + LOG(ERROR) << "CheckRgbPixel failed:\n" << status.ToString(); + } + pixel_status &= status.ok(); + + if (status = CheckRgbPixel(64, 0, 0, 0, 0, 0, 2, buffer2_); !status.ok()) { + LOG(ERROR) << "CheckRgbPixel failed:\n" << status.ToString(); + } + pixel_status &= status.ok(); + + if (status = CheckRgbPixel(512, 5, 6, 34, 36, 182, 196, buffer2_); + !status.ok()) { + LOG(ERROR) << "CheckRgbPixel failed:\n" << status.ToString(); + } + pixel_status &= status.ok(); + + SAPI_RETURN_IF_ERROR(api.TIFFClose(&tif)); + + SAPI_ASSIGN_OR_RETURN( + status_or_tif, api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore())); + + sapi::v::RemotePtr tif2(status_or_tif.value()); + if (!tif2.GetValue()) { // tif is NULL + return absl::InternalError(absl::StrCat("Could not reopen ", srcfile)); + } + + sapi::v::Array rgba_buffer_(128 * 128); + + SAPI_ASSIGN_OR_RETURN(status_or_int, + api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer_.PtrBoth())); + if (!status_or_int.value()) { + return absl::InternalError("TIFFReadRGBATile() returned failure code"); + } + + if (status = CheckRgbaPixel(0, 15, 18, 0, 0, 18, 41, 255, 255, rgba_buffer_); + !status.ok()) { + LOG(ERROR) << "CheckRgbaPixel failed:\n" << status.ToString(); + } + pixel_status &= status.ok(); + + if (status = CheckRgbaPixel(64, 0, 0, 0, 0, 0, 2, 255, 255, rgba_buffer_); + !status.ok()) { + LOG(ERROR) << "CheckRgbaPixel failed:\n" << status.ToString(); + } + pixel_status &= status.ok(); + + if (status = CheckRgbaPixel(512, 5, 6, 34, 36, 182, 196, 255, 255, + rgba_buffer_); !status.ok()) { + LOG(ERROR) << "CheckRgbaPixel failed:\n" << status.ToString(); + } + pixel_status &= status.ok(); + + SAPI_RETURN_IF_ERROR(api.TIFFClose(&tif2)); + + if (!pixel_status) { + return absl::InternalError("unexpected pixel_status value"); + } + + return absl::OkStatus(); +} + int main(int argc, char** argv) { gflags::ParseCommandLineFlags(&argc, &argv, true); @@ -141,137 +312,12 @@ int main(int argc, char** argv) { srcfile = GetFilePath(argv[1], srcfilerel); } - // without addDir to sandbox. to add dir use - // sandbox(absolute_path_to_dir, srcfile) or - // sandbox(absolute_path_to_dir). file and dir should be exists. - // srcfile must also be absolute_path_to_file - TiffSapiSandbox sandbox("", srcfile); - - // initialize sapi vars after constructing TiffSapiSandbox - sapi::v::UShort h, v; - sapi::StatusOr status_or_tif; - sapi::StatusOr status_or_int; - sapi::StatusOr status_or_long; - - auto status = sandbox.Init(); + auto status = LibTIFFMain(srcfile); if (!status.ok()) { - fprintf(stderr, "Couldn't initialize Sandboxed API: %s\n", status); + LOG(ERROR) + << "LibTIFFMain failed with error:\n" << status.ToString() << '\n'; + return EXIT_FAILURE; } - TiffApi api(&sandbox); - sapi::v::ConstCStr srcfile_var(srcfile.c_str()); - sapi::v::ConstCStr r_var("r"); - - status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore()); - if (!status_or_tif.ok()) { - std::cerr << "Could not open " << srcfile << ", TIFFError\n"; - return 1; - } - - sapi::v::RemotePtr tif(status_or_tif.value()); - if (!tif.GetValue()) { - // tif is NULL - std::cerr << "Could not open " << srcfile << "\n"; - return 1; - } - - status_or_int = api.TIFFGetField2(&tif, TIFFTAG_YCBCRSUBSAMPLING, h.PtrBoth(), - v.PtrBoth()); - if (!status_or_int.ok() || status_or_int.value() == 0 || h.GetValue() != 2 || - v.GetValue() != 2) { - std::cerr << "Could not retrieve subsampling tag\n"; - return 1; - } - - status_or_long = api.TIFFTileSize(&tif); - if (!status_or_int.ok() || status_or_long.value() != 24576) { - std::cerr << "tiles are " << status_or_long.value() << " bytes\n"; - exit(1); - } - tsize_t sz = status_or_long.value(); - - sapi::v::Array buffer_(sz); - status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer_.PtrBoth(), sz); - if (!status_or_long.ok() || status_or_long.value() != sz) { - std::cerr << "Did not get expected result code from" - << "TIFFReadEncodedTile(): (" << status_or_long.value() - << " instead of " << sz << ")\n"; - return 1; - } - - if (CheckCluster(0, buffer_, cluster_0) || - CheckCluster(64, buffer_, cluster_64) || - CheckCluster(128, buffer_, cluster_128)) { - return 1; - } - - status_or_int = - api.TIFFSetFieldU1(&tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); - if (!status_or_int.ok() || !status_or_int.value()) { - std::cerr << "TIFFSetFieldU1 not available\n"; - } - - status_or_long = api.TIFFTileSize(&tif); - if (!status_or_long.ok() || status_or_long.value() != 128 * 128 * 3) { - std::cerr << "tiles are " << status_or_long.value() << " bytes\n"; - return 1; - } - sz = status_or_long.value(); - - sapi::v::Array buffer2_(sz); - - status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer2_.PtrBoth(), sz); - if (!status_or_long.ok() || status_or_long.value() != sz) { - std::cerr << "Did not get expected result code from " - << "TIFFReadEncodedTile(): (" << status_or_long.value() - << " instead of " << sz << ")\n"; - return 1; - } - - unsigned pixel_status = 0; - pixel_status |= CheckRgbPixel(0, 15, 18, 0, 0, 18, 41, buffer2_); - pixel_status |= CheckRgbPixel(64, 0, 0, 0, 0, 0, 2, buffer2_); - pixel_status |= CheckRgbPixel(512, 5, 6, 34, 36, 182, 196, buffer2_); - - if (!api.TIFFClose(&tif).ok()) { - std::cerr << "TIFFClose error\n"; - } - - status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore()); - if (!status_or_tif.ok()) { - std::cerr << "Could not reopen " << srcfile << "\n"; - return 1; - } - - sapi::v::RemotePtr tif2(status_or_tif.value()); - if (!tif2.GetValue()) { // tif is NULL - std::cerr << "Could not reopen " << srcfile << "\n"; - return 1; - } - - sapi::v::Array rgba_buffer_(128 * 128); - - status_or_int = - api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer_.PtrBoth()); - if (!status_or_int.ok() || !status_or_int.value()) { - fprintf(stderr, "TIFFReadRGBATile() returned failure code.\n"); - return 1; - } - - pixel_status |= - CheckRgbaPixel(0, 15, 18, 0, 0, 18, 41, 255, 255, rgba_buffer_); - pixel_status |= CheckRgbaPixel(64, 0, 0, 0, 0, 0, 2, 255, 255, rgba_buffer_); - pixel_status |= - CheckRgbaPixel(512, 5, 6, 34, 36, 182, 196, 255, 255, rgba_buffer_); - - if (!api.TIFFClose(&tif2).ok()) { - std::cerr << "TIFFClose erro\n"; - } - - if (pixel_status) { - std::cerr << "pixel_status is true, expected false"; - return 1; - } - - return 0; + return EXIT_SUCCESS; } diff --git a/oss-internship-2020/libtiff/sandboxed.h b/oss-internship-2020/libtiff/sandboxed.h index 70dbdff..168740b 100644 --- a/oss-internship-2020/libtiff/sandboxed.h +++ b/oss-internship-2020/libtiff/sandboxed.h @@ -41,6 +41,7 @@ class TiffSapiSandbox : public TiffSandbox { .AllowOpen() .AllowExit() .AllowStat() + .AllowMmap() .AllowSystemMalloc() .AllowSyscalls({ __NR_futex, @@ -48,7 +49,6 @@ class TiffSapiSandbox : public TiffSandbox { __NR_lseek, __NR_gettid, __NR_sysinfo, - __NR_mmap, __NR_munmap, }); @@ -63,7 +63,8 @@ class TiffSapiSandbox : public TiffSandbox { return builder.get()->BuildOrDie(); } - std::string dir_, file_; + std::string dir_; + std::string file_; }; } // namespace diff --git a/oss-internship-2020/libtiff/test/check_tag.cc b/oss-internship-2020/libtiff/test/check_tag.cc index 72240e5..0f48835 100644 --- a/oss-internship-2020/libtiff/test/check_tag.cc +++ b/oss-internship-2020/libtiff/test/check_tag.cc @@ -14,12 +14,11 @@ #include "check_tag.h" -/* sapi functions: - * TIFFGetField - */ +// sapi functions: +// TIFFGetField void CheckShortField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field, - const unsigned short value) { + const uint16_t value) { sapi::v::UShort tmp(123); sapi::StatusOr status_or_int; @@ -33,7 +32,7 @@ void CheckShortField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field, void CheckShortPairedField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field, - const std::array& values) { + const std::array& values) { sapi::v::UShort tmp0(123); sapi::v::UShort tmp1(456); sapi::StatusOr status_or_int; diff --git a/oss-internship-2020/libtiff/test/check_tag.h b/oss-internship-2020/libtiff/test/check_tag.h index fd48e39..18a029c 100644 --- a/oss-internship-2020/libtiff/test/check_tag.h +++ b/oss-internship-2020/libtiff/test/check_tag.h @@ -12,13 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "helper.h" #include "tiffio.h" void CheckShortField(TiffApi&, sapi::v::RemotePtr& tif, const ttag_t field, - const unsigned short value); + const uint16_t value); void CheckShortPairedField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field, - const std::array& values); + const std::array& values); void CheckLongField(TiffApi&, sapi::v::RemotePtr& tif, const ttag_t field, const unsigned value); diff --git a/oss-internship-2020/libtiff/test/defer_strile_writing.cc b/oss-internship-2020/libtiff/test/defer_strile_writing.cc index cbaab14..67ff3c3 100644 --- a/oss-internship-2020/libtiff/test/defer_strile_writing.cc +++ b/oss-internship-2020/libtiff/test/defer_strile_writing.cc @@ -12,33 +12,36 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "helper.h" #include "tiffio.h" -/* sapi functions: - * TIFFOpen - * TIFFClose - * TIFFGetField - * TIFFSetField - * TIFFWriteCheck - * TIFFSetDirectory - * TIFFFreeDirectory - * TIFFWriteScanline - * TIFFWriteDirectory - * TIFFCreateDirectory - * TIFFReadEncodedTile - * TIFFReadEncodedStrip - * TIFFWriteEncodedTile - * TIFFWriteEncodedStrip - * TIFFDeferStrileArrayWriting - * TIFFForceStrileArrayWriting - */ +// sapi functions: +// TIFFOpen +// TIFFClose +// TIFFGetField +// TIFFSetField +// TIFFWriteCheck +// TIFFSetDirectory +// TIFFFreeDirectory +// TIFFWriteScanline +// TIFFWriteDirectory +// TIFFCreateDirectory +// TIFFReadEncodedTile +// TIFFReadEncodedStrip +// TIFFWriteEncodedTile +// TIFFWriteEncodedStrip +// TIFFDeferStrileArrayWriting +// TIFFForceStrileArrayWriting + +namespace { #define TBS 256 // kTileBufferSize -static const unsigned short kWidth = 1; -static const unsigned short kBps = 8; -static const unsigned short kRowsPerStrip = 1; -static const unsigned short kSamplePerPixel = 1; +constexpr uint16_t kWidth = 1; +constexpr uint16_t kBps = 8; +constexpr uint16_t kRowsPerStrip = 1; +constexpr uint16_t kSamplePerPixel = 1; void TestWriting(const char* mode, int tiled, int height) { sapi::StatusOr status_or_path = @@ -320,3 +323,5 @@ TEST(SandboxTest, DeferStrileWriting) { TestWriting("wD", tiled, 1); } } + +} // namespace diff --git a/oss-internship-2020/libtiff/test/helper.cc b/oss-internship-2020/libtiff/test/helper.cc index 964c15d..b50fc1d 100644 --- a/oss-internship-2020/libtiff/test/helper.cc +++ b/oss-internship-2020/libtiff/test/helper.cc @@ -16,16 +16,10 @@ #include -std::string inDir; - -std::string GetCWD() { - char cwd[PATH_MAX]; - getcwd(cwd, sizeof(cwd)); - return cwd; -} +static auto* g_in_dir = new std::string(); std::string GetImagesDir() { - std::string cwd = GetCWD(); + std::string cwd = sandbox2::file_util::fileops::GetCWD(); auto find = cwd.rfind("/build"); if (find == std::string::npos) { std::cerr << "Something went wrong: CWD don't contain build dir. " @@ -38,8 +32,8 @@ std::string GetImagesDir() { } std::string GetFilePath(const std::string& filename) { - if (inDir.empty()) { - inDir = GetImagesDir(); + if (g_in_dir->empty()) { + *g_in_dir = GetImagesDir(); } - return sandbox2::file::JoinPath(inDir, filename); + return sandbox2::file::JoinPath(*g_in_dir, filename); } diff --git a/oss-internship-2020/libtiff/test/long_tag.cc b/oss-internship-2020/libtiff/test/long_tag.cc index a065204..e575ded 100644 --- a/oss-internship-2020/libtiff/test/long_tag.cc +++ b/oss-internship-2020/libtiff/test/long_tag.cc @@ -18,13 +18,14 @@ #include "check_tag.h" #include "tiffio.h" -/* sapi functions: - * TIFFWriteScanline - * TIFFOpen - * TIFFClose - * TIFFGetField (from check_tag.c) - * TIFFSetField - */ +// sapi functions: +// TIFFWriteScanline +// TIFFOpen +// TIFFClose +// TIFFGetField (from check_tag.c) +// TIFFSetField + +namespace { struct LongTag { ttag_t tag; @@ -32,14 +33,13 @@ struct LongTag { unsigned value; }; -const std::vector long_tags = { - {TIFFTAG_SUBFILETYPE, 1, - FILETYPE_REDUCEDIMAGE | FILETYPE_PAGE | FILETYPE_MASK}}; +constexpr std::array kLongTags = { + {TIFFTAG_SUBFILETYPE, 1, FILETYPE_REDUCEDIMAGE | FILETYPE_PAGE | FILETYPE_MASK}}; #define SPP 3 // kSamplePerPixel -static const unsigned kWidth = 1; -static const unsigned kLength = 1; -static const unsigned kBps = 8; -static const unsigned kRowsPerStrip = 1; +constexpr unsigned kWidth = 1; +constexpr unsigned kLength = 1; +constexpr unsigned kBps = 8; +constexpr unsigned kRowsPerStrip = 1; TEST(SandboxTest, LongTag) { sapi::StatusOr status_or_path = @@ -52,8 +52,8 @@ TEST(SandboxTest, LongTag) { TiffSapiSandbox sandbox("", srcfile); ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API"; - std::array buffer = {0, 127, 255}; - sapi::v::Array buffer_(buffer.data(), SPP); + std::array buffer = {0, 127, 255}; + sapi::v::Array buffer_(buffer.data(), SPP); sapi::StatusOr status_or_int; sapi::StatusOr status_or_tif; @@ -102,7 +102,7 @@ TEST(SandboxTest, LongTag) { EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set PhotometricInterpretation tag"; - for (auto& tag : long_tags) { + for (auto& tag : kLongTags) { status_or_int = api.TIFFSetFieldU1(&tif, tag.tag, tag.value); ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort1 fatal error"; EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set tag " << tag.tag; @@ -126,10 +126,12 @@ TEST(SandboxTest, LongTag) { CheckLongField(api, tif2, TIFFTAG_IMAGELENGTH, kLength); CheckLongField(api, tif2, TIFFTAG_ROWSPERSTRIP, kRowsPerStrip); - for (auto& tag : long_tags) { + for (auto& tag : kLongTags) { CheckLongField(api, tif2, tag.tag, tag.value); } ASSERT_THAT(api.TIFFClose(&tif2), IsOk()) << "TIFFClose fatal error"; unlink(srcfile.c_str()); } + +} // namespace diff --git a/oss-internship-2020/libtiff/test/raw_decode.cc b/oss-internship-2020/libtiff/test/raw_decode.cc index 0a3f58e..d359316 100644 --- a/oss-internship-2020/libtiff/test/raw_decode.cc +++ b/oss-internship-2020/libtiff/test/raw_decode.cc @@ -12,30 +12,31 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include "gtest/gtest.h" #include "helper.h" #include "tiffio.h" -/* sapi functions: - * TIFFOpen - * TIFFClose - * TIFFGetField - * TIFFSetField - * TIFFTileSize - * TIFFReadRGBATile - * TIFFReadEncodedTile - */ +// sapi functions: +// TIFFOpen +// TIFFClose +// TIFFGetField +// TIFFSetField +// TIFFTileSize +// TIFFReadRGBATile +// TIFFReadEncodedTile -static const std::vector cluster_0 = {0, 0, 2, 0, 138, 139}; -static const std::vector cluster_64 = {0, 0, 9, 6, 134, 119}; -static const std::vector cluster_128 = {44, 40, 63, 59, 230, 95}; +namespace { -static int CheckCluster(int cluster, - const sapi::v::Array &buffer, - const std::vector &expected_cluster) { - unsigned char* target = buffer.GetData() + cluster * 6; +constexpr std::array kCluster0 = {0, 0, 2, 0, 138, 139}; +constexpr std::array kCluster64 = {0, 0, 9, 6, 134, 119}; +constexpr std::array kCluster128 = {44, 40, 63, 59, 230, 95}; + +int CheckCluster(int cluster, const sapi::v::Array &buffer, + const std::array &expected_cluster) { + uint8_t* target = buffer.GetData() + cluster * 6; bool comp = !(std::memcmp(target, expected_cluster.data(), 6) == 0); @@ -50,10 +51,10 @@ static int CheckCluster(int cluster, return comp; } -static int CheckRgbPixel(int pixel, int min_red, int max_red, int min_green, - int max_green, int min_blue, int max_blue, - const sapi::v::Array &buffer) { - unsigned char* rgb = buffer.GetData() + 3 * pixel; +int CheckRgbPixel(int pixel, int min_red, int max_red, int min_green, + int max_green, int min_blue, int max_blue, + const sapi::v::Array &buffer) { + uint8_t* rgb = buffer.GetData() + 3 * pixel; bool comp = !(rgb[0] >= min_red && rgb[0] <= max_red && rgb[1] >= min_green && @@ -68,10 +69,9 @@ static int CheckRgbPixel(int pixel, int min_red, int max_red, int min_green, return comp; } -static int CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green, - int max_green, int min_blue, int max_blue, - int min_alpha, int max_alpha, - const sapi::v::Array &buffer) { +int CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green, + int max_green, int min_blue, int max_blue, int min_alpha, + int max_alpha, const sapi::v::Array &buffer) { // RGBA images are upside down - adjust for normal ordering int adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128; unsigned rgba = buffer[adjusted_pixel]; @@ -133,16 +133,16 @@ TEST(SandboxTest, RawDecode) { << "tiles are " << status_or_long.value() << " bytes"; sz = status_or_long.value(); - sapi::v::Array buffer_(sz); + sapi::v::Array buffer_(sz); status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer_.PtrBoth(), sz); ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error"; EXPECT_THAT(status_or_long.value(), Eq(sz)) << "Did not get expected result code from TIFFReadEncodedTile()(" << (int)status_or_long.value() << " instead of " << (int)sz << ")"; - ASSERT_FALSE(CheckCluster(0, buffer_, cluster_0) || - CheckCluster(64, buffer_, cluster_64) || - CheckCluster(128, buffer_, cluster_128)) + ASSERT_FALSE(CheckCluster(0, buffer_, kCluster0) || + CheckCluster(64, buffer_, kCluster64) || + CheckCluster(128, buffer_, kCluster128)) << "Clusters did not match expected results"; status_or_int = @@ -157,7 +157,7 @@ TEST(SandboxTest, RawDecode) { << "tiles are " << status_or_long.value() << " bytes"; sz = status_or_long.value(); - sapi::v::Array buffer2_(sz); + sapi::v::Array buffer2_(sz); status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer2_.PtrBoth(), sz); ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error"; EXPECT_THAT(status_or_long.value(), Eq(sz)) @@ -194,4 +194,6 @@ TEST(SandboxTest, RawDecode) { EXPECT_THAT(api.TIFFClose(&tif2), IsOk()) << "TIFFClose fatal error"; EXPECT_THAT(pixel_status, IsFalse()) << "wrong encoding"; -} \ No newline at end of file +} + +} // namespace diff --git a/oss-internship-2020/libtiff/test/short_tag.cc b/oss-internship-2020/libtiff/test/short_tag.cc index d5a6bb1..5913d51 100644 --- a/oss-internship-2020/libtiff/test/short_tag.cc +++ b/oss-internship-2020/libtiff/test/short_tag.cc @@ -13,38 +13,40 @@ // limitations under the License. #include +#include #include #include "check_tag.h" #include "tiffio.h" -/* sapi functions: - * TIFFWriteScanline - * TIFFOpen - * TIFFClose - * TIFFGetField (from check_tag.c) - * TIFFSetField - */ +// sapi functions: +// TIFFWriteScanline +// TIFFOpen +// TIFFClose +// TIFFGetField (from check_tag.c) +// TIFFSetField + +namespace { #define SPP 3 // kSamplePerPixel -static const unsigned short kWidth = 1; -static const unsigned short kLength = 1; -static const unsigned short kBps = 8; -static const unsigned short kPhotometric = PHOTOMETRIC_RGB; -static const unsigned short kRowsPerStrip = 1; -static const unsigned short kPlanarConfig = PLANARCONFIG_CONTIG; +constexpr uint16_t kWidth = 1; +constexpr uint16_t kLength = 1; +constexpr uint16_t kBps = 8; +constexpr uint16_t kPhotometric = PHOTOMETRIC_RGB; +constexpr uint16_t kRowsPerStrip = 1; +constexpr uint16_t kPlanarConfig = PLANARCONFIG_CONTIG; struct SingleTag { const ttag_t tag; - const unsigned short value; + const uint16_t value; }; struct PairedTag { const ttag_t tag; - const std::array values; + const std::array values; }; -static const std::vector short_single_tags = { +constexpr std::array kShortSingleTags = {{ {TIFFTAG_COMPRESSION, COMPRESSION_NONE}, {TIFFTAG_FILLORDER, FILLORDER_MSB2LSB}, {TIFFTAG_ORIENTATION, ORIENTATION_BOTRIGHT}, @@ -53,13 +55,13 @@ static const std::vector short_single_tags = { {TIFFTAG_MAXSAMPLEVALUE, 241}, {TIFFTAG_INKSET, INKSET_MULTIINK}, {TIFFTAG_NUMBEROFINKS, SPP}, - {TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT}}; + {TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT}}}; -static const std::vector short_paired_tags = { +constexpr std::array kShortPairedTags = {{ {TIFFTAG_PAGENUMBER, {1, 1}}, {TIFFTAG_HALFTONEHINTS, {0, 255}}, {TIFFTAG_DOTRANGE, {8, 16}}, - {TIFFTAG_YCBCRSUBSAMPLING, {2, 1}}}; + {TIFFTAG_YCBCRSUBSAMPLING, {2, 1}}}}; TEST(SandboxTest, ShortTag) { sapi::StatusOr status_or_path = @@ -72,8 +74,8 @@ TEST(SandboxTest, ShortTag) { TiffSapiSandbox sandbox("", srcfile); ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API"; - std::array buffer = {0, 127, 255}; - sapi::v::Array buffer_(buffer.data(), SPP); + std::array buffer = {0, 127, 255}; + sapi::v::Array buffer_(buffer.data(), SPP); sapi::StatusOr status_or_int; sapi::StatusOr status_or_tif; @@ -123,13 +125,13 @@ TEST(SandboxTest, ShortTag) { EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set PhotometricInterpretation tag"; - for (auto& tag : short_single_tags) { + for (auto& tag : kShortSingleTags) { status_or_int = api.TIFFSetFieldUShort1(&tif, tag.tag, tag.value); ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort1 fatal error"; EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set tag " << tag.tag; } - for (auto& tag : short_paired_tags) { + for (auto& tag : kShortPairedTags) { status_or_int = api.TIFFSetFieldUShort2(&tif, tag.tag, tag.values[0], tag.values[1]); ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort2 fatal error"; @@ -158,13 +160,15 @@ TEST(SandboxTest, ShortTag) { CheckLongField(api, tif2, TIFFTAG_ROWSPERSTRIP, kRowsPerStrip); CheckShortField(api, tif2, TIFFTAG_PLANARCONFIG, kPlanarConfig); - for (auto& tag : short_single_tags) { + for (auto& tag : kShortSingleTags) { CheckShortField(api, tif2, tag.tag, tag.value); } - for (auto& tag : short_paired_tags) { + for (auto& tag : kShortPairedTags) { CheckShortPairedField(api, tif2, tag.tag, tag.values); } ASSERT_THAT(api.TIFFClose(&tif2), IsOk()) << "TIFFClose fatal error"; } + +} // namespace diff --git a/oss-internship-2020/libtiff/wrapper/CMakeLists.txt b/oss-internship-2020/libtiff/wrapper/CMakeLists.txt index 0be8ee7..6627455 100644 --- a/oss-internship-2020/libtiff/wrapper/CMakeLists.txt +++ b/oss-internship-2020/libtiff/wrapper/CMakeLists.txt @@ -26,4 +26,4 @@ target_link_libraries(wrapped_tiff sandbox2::temp_file sapi::sapi tiff -) \ No newline at end of file +) diff --git a/oss-internship-2020/libtiff/wrapper/func.h b/oss-internship-2020/libtiff/wrapper/func.h index 4d841dc..3115ddb 100644 --- a/oss-internship-2020/libtiff/wrapper/func.h +++ b/oss-internship-2020/libtiff/wrapper/func.h @@ -17,10 +17,9 @@ #include "tiffio.h" -/* s - signed - * u - unsigned - * wrapper for variadic functions TIFFGetField and TIFFSetField - */ +// wrapper for variadic functions TIFFGetField and TIFFSetField +// s - signed +// u - unsigned extern "C" { int TIFFGetField1(TIFF* tif, unsigned tag, void* param);