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