1 //
2 // Copyright (C) 2016 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/payload_generator/xz.h"
18
19 #include <base/logging.h>
20 #include <lzma.h>
21
22 namespace chromeos_update_engine {
23
XzCompressInit()24 void XzCompressInit() {}
25
XzCompress(const brillo::Blob & in,brillo::Blob * out)26 bool XzCompress(const brillo::Blob& in, brillo::Blob* out) {
27 out->clear();
28 if (in.empty())
29 return true;
30
31 // Resize the output buffer to get enough memory for writing the compressed
32 // data.
33 out->resize(lzma_stream_buffer_bound(in.size()));
34
35 const uint32_t kLzmaPreset = 6;
36 size_t out_pos = 0;
37 int rc = lzma_easy_buffer_encode(kLzmaPreset,
38 LZMA_CHECK_NONE, // We do not need CRC.
39 nullptr,
40 in.data(),
41 in.size(),
42 out->data(),
43 &out_pos,
44 out->size());
45 if (rc != LZMA_OK) {
46 LOG(ERROR) << "Failed to compress data to LZMA stream with return code: "
47 << rc;
48 return false;
49 }
50 out->resize(out_pos);
51 return true;
52 }
53
54 } // namespace chromeos_update_engine
55