1 /*
2  * Copyright (C) 2018 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 "xz_utils.h"
18 
19 #include <vector>
20 #include <mutex>
21 
22 #include "base/array_ref.h"
23 #include "base/bit_utils.h"
24 #include "base/leb128.h"
25 #include "dwarf/writer.h"
26 
27 // liblzma.
28 #include "7zCrc.h"
29 #include "Xz.h"
30 #include "XzCrc64.h"
31 #include "XzEnc.h"
32 
33 namespace art {
34 
XzInitCrc()35 static void XzInitCrc() {
36   static std::once_flag crc_initialized;
37   std::call_once(crc_initialized, []() {
38     CrcGenerateTable();
39     Crc64GenerateTable();
40   });
41 }
42 
XzCompress(ArrayRef<const uint8_t> src,std::vector<uint8_t> * dst,int level,size_t block_size)43 void XzCompress(ArrayRef<const uint8_t> src,
44                 std::vector<uint8_t>* dst,
45                 int level,
46                 size_t block_size) {
47   // Configure the compression library.
48   XzInitCrc();
49   CLzma2EncProps lzma2Props;
50   Lzma2EncProps_Init(&lzma2Props);
51   lzma2Props.lzmaProps.level = level;
52   lzma2Props.lzmaProps.reduceSize = src.size();  // Size of data that will be compressed.
53   lzma2Props.blockSize = block_size;
54   Lzma2EncProps_Normalize(&lzma2Props);
55   CXzProps props;
56   XzProps_Init(&props);
57   props.lzma2Props = lzma2Props;
58   // Implement the required interface for communication (written in C so no virtual methods).
59   struct XzCallbacks : public ISeqInStream, public ISeqOutStream, public ICompressProgress {
60     static SRes ReadImpl(const ISeqInStream* p, void* buf, size_t* size) {
61       auto* ctx = static_cast<XzCallbacks*>(const_cast<ISeqInStream*>(p));
62       *size = std::min(*size, ctx->src_.size() - ctx->src_pos_);
63       memcpy(buf, ctx->src_.data() + ctx->src_pos_, *size);
64       ctx->src_pos_ += *size;
65       return SZ_OK;
66     }
67     static size_t WriteImpl(const ISeqOutStream* p, const void* buf, size_t size) {
68       auto* ctx = static_cast<const XzCallbacks*>(p);
69       const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf);
70       ctx->dst_->insert(ctx->dst_->end(), buffer, buffer + size);
71       return size;
72     }
73     static SRes ProgressImpl(const ICompressProgress* , UInt64, UInt64) {
74       return SZ_OK;
75     }
76     size_t src_pos_;
77     ArrayRef<const uint8_t> src_;
78     std::vector<uint8_t>* dst_;
79   };
80   XzCallbacks callbacks;
81   callbacks.Read = XzCallbacks::ReadImpl;
82   callbacks.Write = XzCallbacks::WriteImpl;
83   callbacks.Progress = XzCallbacks::ProgressImpl;
84   callbacks.src_pos_ = 0;
85   callbacks.src_ = src;
86   callbacks.dst_ = dst;
87   // Compress.
88   SRes res = Xz_Encode(&callbacks, &callbacks, &props, &callbacks);
89   CHECK_EQ(res, SZ_OK);
90 
91   // Decompress the data back and check that we get the original.
92   if (kIsDebugBuild) {
93     std::vector<uint8_t> decompressed;
94     XzDecompress(ArrayRef<const uint8_t>(*dst), &decompressed);
95     DCHECK_EQ(decompressed.size(), src.size());
96     DCHECK_EQ(memcmp(decompressed.data(), src.data(), src.size()), 0);
97   }
98 }
99 
XzDecompress(ArrayRef<const uint8_t> src,std::vector<uint8_t> * dst)100 void XzDecompress(ArrayRef<const uint8_t> src, std::vector<uint8_t>* dst) {
101   XzInitCrc();
102   std::unique_ptr<CXzUnpacker> state(new CXzUnpacker());
103   ISzAlloc alloc;
104   alloc.Alloc = [](ISzAllocPtr, size_t size) { return malloc(size); };
105   alloc.Free = [](ISzAllocPtr, void* ptr) { return free(ptr); };
106   XzUnpacker_Construct(state.get(), &alloc);
107 
108   size_t src_offset = 0;
109   size_t dst_offset = 0;
110   ECoderStatus status;
111   do {
112     dst->resize(RoundUp(dst_offset + kPageSize / 4, kPageSize));
113     size_t src_remaining = src.size() - src_offset;
114     size_t dst_remaining = dst->size() - dst_offset;
115     int return_val = XzUnpacker_Code(state.get(),
116                                      dst->data() + dst_offset,
117                                      &dst_remaining,
118                                      src.data() + src_offset,
119                                      &src_remaining,
120                                      true,
121                                      CODER_FINISH_ANY,
122                                      &status);
123     CHECK_EQ(return_val, SZ_OK);
124     src_offset += src_remaining;
125     dst_offset += dst_remaining;
126   } while (status == CODER_STATUS_NOT_FINISHED);
127   CHECK_EQ(src_offset, src.size());
128   CHECK(XzUnpacker_IsStreamWasFinished(state.get()));
129   XzUnpacker_Free(state.get());
130   dst->resize(dst_offset);
131 }
132 
133 }  // namespace art
134