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