1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "contrib/c-blosc/utils/utils_blosc.h"
16
17 #include <fstream>
18 #include <iostream>
19 #include <string>
20
21 #include "contrib/c-blosc/sandboxed.h"
22
23 constexpr size_t kFileMaxSize = 1024 * 1024 * 1024; // 1GB
24
GetStreamSize(std::ifstream & stream)25 std::streamsize GetStreamSize(std::ifstream& stream) {
26 stream.seekg(0, std::ios_base::end);
27 std::streamsize ssize = stream.tellg();
28 stream.seekg(0, std::ios_base::beg);
29
30 return ssize;
31 }
32
Compress(CbloscApi & api,std::ifstream & in_stream,std::ofstream & out_stream,int clevel,std::string & compressor,int nthreads)33 absl::Status Compress(CbloscApi& api, std::ifstream& in_stream,
34 std::ofstream& out_stream, int clevel,
35 std::string& compressor, int nthreads) {
36 std::streamsize ssize = GetStreamSize(in_stream);
37 sapi::v::Array<uint8_t> inbuf(ssize);
38 sapi::v::Array<uint8_t> outbuf(ssize);
39
40 in_stream.read(reinterpret_cast<char*>(inbuf.GetData()), ssize);
41 if (in_stream.gcount() != ssize) {
42 return absl::UnavailableError("Unable to read file");
43 }
44
45 int ret;
46 SAPI_ASSIGN_OR_RETURN(
47 ret, api.blosc_set_compressor(
48 sapi::v::ConstCStr(compressor.c_str()).PtrBefore()));
49 if (ret < 0) {
50 return absl::UnavailableError("Unable to set compressor");
51 }
52
53 SAPI_ASSIGN_OR_RETURN(ret, api.blosc_set_nthreads(nthreads));
54 if (ret < 0) {
55 return absl::UnavailableError("Unable to set nthreads");
56 }
57
58 SAPI_ASSIGN_OR_RETURN(
59 ssize_t outsize, api.blosc_compress(clevel, 1, sizeof(uint8_t),
60 inbuf.GetSize(), inbuf.PtrBefore(),
61 outbuf.PtrAfter(), outbuf.GetSize()));
62 if (outsize <= 0) {
63 return absl::UnavailableError("Unable to compress file.");
64 }
65
66 out_stream.write(reinterpret_cast<char*>(outbuf.GetData()), outsize);
67 if (!out_stream.good()) {
68 return absl::UnavailableError("Unable to write file");
69 }
70
71 return absl::OkStatus();
72 }
73
Decompress(CbloscApi & api,std::ifstream & in_stream,std::ofstream & out_stream,int nthreads)74 absl::Status Decompress(CbloscApi& api, std::ifstream& in_stream,
75 std::ofstream& out_stream, int nthreads) {
76 std::streamsize ssize = GetStreamSize(in_stream);
77 sapi::v::Array<uint8_t> inbuf(ssize);
78
79 in_stream.read(reinterpret_cast<char*>(inbuf.GetData()), ssize);
80 if (in_stream.gcount() != ssize) {
81 return absl::UnavailableError("Unable to read file");
82 }
83
84 int ret;
85 SAPI_ASSIGN_OR_RETURN(ret, api.blosc_set_nthreads(nthreads));
86 if (ret < 0) {
87 return absl::UnavailableError("Unable to set nthreads");
88 }
89
90 // To not transfer memory twice (for blosc_cbuffer_sizes and decopmress),
91 // tranfer memory before using it.
92 SAPI_RETURN_IF_ERROR(api.GetSandbox()->Allocate(&inbuf, true));
93 SAPI_RETURN_IF_ERROR(api.GetSandbox()->TransferToSandboxee(&inbuf));
94
95 sapi::v::IntBase<size_t> nbytes;
96 sapi::v::IntBase<size_t> cbytes;
97 sapi::v::IntBase<size_t> blocksize;
98 SAPI_RETURN_IF_ERROR(
99 api.blosc_cbuffer_sizes(inbuf.PtrNone(), nbytes.PtrAfter(),
100 cbytes.PtrAfter(), blocksize.PtrAfter()));
101 if (nbytes.GetValue() == 0) {
102 return absl::UnavailableError("Unable to get size");
103 }
104 if (nbytes.GetValue() > kFileMaxSize) {
105 return absl::UnavailableError("The file is to large");
106 }
107
108 sapi::v::Array<uint8_t> outbuf(nbytes.GetValue());
109 SAPI_ASSIGN_OR_RETURN(ssize_t outsize,
110 api.blosc_decompress(inbuf.PtrNone(), outbuf.PtrAfter(),
111 outbuf.GetSize()));
112 if (outsize <= 0) {
113 return absl::UnavailableError("Unable to decompress file");
114 }
115
116 out_stream.write(reinterpret_cast<char*>(outbuf.GetData()), outsize);
117 if (!out_stream.good()) {
118 return absl::UnavailableError("Unable to write file");
119 }
120
121 return absl::OkStatus();
122 }
123