diff --git a/oss-internship-2020/openjpeg/examples/decompress_example.cc b/oss-internship-2020/openjpeg/examples/decompress_example.cc index 0c9ce22..f19ce0e 100644 --- a/oss-internship-2020/openjpeg/examples/decompress_example.cc +++ b/oss-internship-2020/openjpeg/examples/decompress_example.cc @@ -17,20 +17,18 @@ #include #include -#include #include #include +#include +#include "absl/algorithm/container.h" #include "convert_helper.h" #include "openjp2_sapi.sapi.h" -#include "sandboxed_api/util/flag.h" - -class Parameters : public sapi::v::Struct {}; -class Opj_image_t : public sapi::v::Struct {}; class Openjp2SapiSandbox : public Openjp2Sandbox { public: - Openjp2SapiSandbox(const std::string& in_file) : in_file_(in_file) {} + Openjp2SapiSandbox(const std::string in_file) + : in_file_(std::move(in_file)) {} std::unique_ptr ModifyPolicy( sandbox2::PolicyBuilder*) override { @@ -57,6 +55,7 @@ class Openjp2SapiSandbox : public Openjp2Sandbox { int main(int argc, char* argv[]) { gflags::ParseCommandLineFlags(&argc, &argv, true); + google::InitGoogleLogging(argv[0]); if (argc != 3) { std::cerr << "usage: " << basename(argv[0]) << " absolute/path/to/INPUT.jp2" @@ -69,59 +68,82 @@ int main(int argc, char* argv[]) { // initialize sandbox Openjp2SapiSandbox sandbox(in_file); absl::Status status = sandbox.Init(); - assert(status.ok()); + if (!status.ok()) { + LOG(FATAL) << "sandbox initialization status: " << status; + return EXIT_FAILURE; + } Openjp2Api api(&sandbox); sapi::v::ConstCStr in_file_v(in_file.c_str()); // initialize library's main data-holders - sapi::StatusOr stream_status = + sapi::StatusOr stream = api.opj_stream_create_default_file_stream(in_file_v.PtrBefore(), 1); - assert(stream_status.ok()); - void* stream_status_value = stream_status.value(); - sapi::v::RemotePtr stream_pointer(stream_status_value); + if (!stream.ok()) { + LOG(FATAL) << "opj_stream initialization failed: " << stream.status(); + return EXIT_FAILURE; + } + sapi::v::RemotePtr stream_pointer(stream.value()); - sapi::StatusOr codec_status = - api.opj_create_decompress(OPJ_CODEC_JP2); - assert(codec_status.ok()); - void* codec_status_value = codec_status.value(); - sapi::v::RemotePtr codec_pointer(codec_status_value); + sapi::StatusOr codec = api.opj_create_decompress(OPJ_CODEC_JP2); + if (!codec.ok()) { + LOG(FATAL) << "opj_codec initialization failed: " << codec.status(); + return EXIT_FAILURE; + } + sapi::v::RemotePtr codec_pointer(codec.value()); - Parameters parameters; + sapi::v::Struct parameters; status = api.opj_set_default_decoder_parameters(parameters.PtrBoth()); - assert(status.ok()); + if (!status.ok()) { + LOG(FATAL) << "parameters initialization failed: " << status; + return EXIT_FAILURE; + } sapi::StatusOr bool_status = api.opj_setup_decoder(&codec_pointer, parameters.PtrBefore()); - assert(bool_status.ok()); - assert(bool_status.value()); + if (!bool_status.ok() || !bool_status.value()) { + LOG(FATAL) << "decoder setup failed"; + return EXIT_FAILURE; + } // start reading image from the input file sapi::v::GenericPtr image_pointer; bool_status = api.opj_read_header(&stream_pointer, &codec_pointer, image_pointer.PtrAfter()); - assert(bool_status.ok()); - assert(bool_status.value()); + if (!bool_status.ok() || !bool_status.value()) { + LOG(FATAL) << "reading image header failed"; + return EXIT_FAILURE; + } - Opj_image_t image; + sapi::v::Struct image; image.SetRemote((void*)image_pointer.GetValue()); - assert(sandbox.TransferFromSandboxee(&image).ok()); + if (!sandbox.TransferFromSandboxee(&image).ok()) { + LOG(FATAL) << "transfer from sandboxee failed"; + return EXIT_FAILURE; + } bool_status = api.opj_decode(&codec_pointer, &stream_pointer, (sapi::v::Ptr*)&image_pointer); - assert(bool_status.ok()); - assert(bool_status.value()); + if (!bool_status.ok() || !bool_status.value()) { + LOG(FATAL) << "decoding failed"; + return EXIT_FAILURE; + } bool_status = api.opj_end_decompress(&codec_pointer, &stream_pointer); - assert(bool_status.ok()); - assert(bool_status.value()); + if (!bool_status.ok() || !bool_status.value()) { + LOG(FATAL) << "ending decompress failed"; + return EXIT_FAILURE; + } int components = image.data().numcomps; // transfer the read data to the main process sapi::v::Array image_components(components); image_components.SetRemote(image.data().comps); - assert(sandbox.TransferFromSandboxee(&image_components).ok()); + if (!sandbox.TransferFromSandboxee(&image_components).ok()) { + LOG(FATAL) << "transfer from sandboxee failed"; + return EXIT_FAILURE; + } image.mutable_data()->comps = (opj_image_comp_t*)image_components.GetLocal(); @@ -131,9 +153,12 @@ int main(int argc, char* argv[]) { OPJ_INT32 data[components][width * height]; sapi::v::Array image_components_data(width * height); - for (int i = 0; i < components; i++) { + for (int i = 0; i < components; ++i) { image_components_data.SetRemote(image.data().comps[i].data); - assert(sandbox.TransferFromSandboxee(&image_components_data).ok()); + if (!sandbox.TransferFromSandboxee(&image_components_data).ok()) { + LOG(FATAL) << "transfer from sandboxee failed"; + return EXIT_FAILURE; + } for (int j = 0; j < width * height; j++) { data[i][j] = image_components_data[j]; } @@ -142,18 +167,17 @@ int main(int argc, char* argv[]) { // convert the image to the desired format and save it to the file int error = imagetopnm((opj_image_t*)image.GetLocal(), argv[2], 0); - assert(error == 0); + if (error) LOG(FATAL) << "image convert failed"; // cleanup - sapi::v::RemotePtr remote_image_pointer(image.GetRemote()); - status = api.opj_image_destroy(&remote_image_pointer); - assert(status.ok()); + status = api.opj_image_destroy(image.PtrNone()); + if (!status.ok()) LOG(FATAL) << "image destroy failed: " << status; status = api.opj_stream_destroy(&stream_pointer); - assert(status.ok()); + if (!status.ok()) LOG(FATAL) << "stream destroy failed: " << status; status = api.opj_destroy_codec(&codec_pointer); - assert(status.ok()); + if (!status.ok()) LOG(FATAL) << "codec destroy failed: " << status; return EXIT_SUCCESS; }