1 /*
2  * Copyright (C) 2021 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 "lz4_legacy.h"
18 
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 
22 #include <vector>
23 
24 #include <android-base/file.h>
25 #include <android-base/result.h>
26 #include <android-base/unique_fd.h>
27 #include <lz4.h>
28 #include <storage_literals/storage_literals.h>
29 
30 using android::base::ErrnoError;
31 using android::base::Error;
32 using android::base::ReadFully;
33 using android::base::unique_fd;
34 using android::base::WriteFully;
35 using android::storage_literals::operator""_MiB;
36 
37 namespace android {
38 
Lz4DecompressLegacy(const char * input,const char * output)39 android::base::Result<void> Lz4DecompressLegacy(const char* input,
40                                                 const char* output) {
41   constexpr uint32_t lz4_legacy_magic = 0x184C2102;
42   constexpr auto lz4_legacy_block_size = 8_MiB;
43 
44   unique_fd ifd(TEMP_FAILURE_RETRY(open(input, O_RDONLY | O_CLOEXEC)));
45   unique_fd ofd(TEMP_FAILURE_RETRY(
46       open(output, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, 0640)));
47 
48   uint32_t magic;
49   if (!ReadFully(ifd, &magic, sizeof(magic))) {
50     return ErrnoError() << "read magic";
51   }
52   // Android is little-endian. No need to convert magic.
53   if (magic != lz4_legacy_magic) {
54     return Error() << "magic is " << std::hex << magic << " but expected "
55                    << lz4_legacy_magic;
56   }
57 
58   std::vector<char> ibuf(LZ4_compressBound(lz4_legacy_block_size));
59   std::vector<char> obuf(lz4_legacy_block_size);
60 
61   while (true) {
62     uint32_t block_size;
63     ssize_t read_bytes =
64         TEMP_FAILURE_RETRY(read(ifd.get(), &block_size, sizeof(block_size)));
65     if (read_bytes == 0) break;
66     if (read_bytes < 0) {
67       return ErrnoError() << "read block size";
68     }
69     if (static_cast<size_t>(read_bytes) != sizeof(block_size)) {
70       return Error() << "Cannot read " << sizeof(block_size)
71                      << " bytes for block size";
72     }
73 
74     // Android is little-endian. No need to convert block_size.
75     if (block_size == lz4_legacy_magic) {
76       return Error() << "Found another lz4 compressed stream";
77     }
78     if (block_size > ibuf.size()) {
79       return Error() << "Block size is " << block_size
80                      << " but compress bound is " << ibuf.size();
81     }
82 
83     if (!ReadFully(ifd, ibuf.data(), block_size)) {
84       return ErrnoError() << "read " << block_size << " bytes";
85     }
86 
87     int decoded_size =
88         LZ4_decompress_safe(ibuf.data(), obuf.data(), block_size, obuf.size());
89     if (decoded_size < 0) {
90       return Error() << "LZ4_decompress_safe returns " << decoded_size;
91     }
92 
93     if (!WriteFully(ofd, obuf.data(), decoded_size)) {
94       return ErrnoError() << "write " << decoded_size << " bytes";
95     }
96   }
97 
98   return {};
99 }
100 }  // namespace android
101