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 #ifndef UPDATE_ENGINE_LZ4DIFF_LZ4DIFF_FORMAT_H_
18 #define UPDATE_ENGINE_LZ4DIFF_LZ4DIFF_FORMAT_H_
19 
20 #include <string_view>
21 #include <vector>
22 
23 #include <lz4diff/lz4diff.pb.h>
24 
25 namespace chromeos_update_engine {
26 
27 using Blob = std::vector<unsigned char>;
28 
29 // Format of LZ4diff patch:
30 // struct lz4diff_header {
31 //     char magic[8] = kLz4diffMagic;
32 //     uint32_t version;
33 //     uint32_t pb_header_size;         // size of protobuf message
34 //     char pf_header[pb_header_size];
35 // }
36 
37 constexpr std::string_view kLz4diffMagic = "LZ4DIFF";
38 
39 // 8 bytes magic + 4 bytes version + 4 bytes pb_header_size
40 constexpr size_t kLz4diffHeaderSize = 8 + 4 + 4;
41 
42 constexpr uint32_t kLz4diffVersion = 1;
43 
44 struct CompressedBlock {
CompressedBlockCompressedBlock45   constexpr CompressedBlock() : CompressedBlock(0, 0, 0) {}
CompressedBlockCompressedBlock46   constexpr CompressedBlock(uint64_t offset,
47                             uint64_t length,
48                             uint64_t uncompressed_length)
49       : uncompressed_offset(offset),
50         compressed_length(length),
51         uncompressed_length(uncompressed_length) {}
IsCompressedCompressedBlock52   constexpr bool IsCompressed() const noexcept {
53     return compressed_length < uncompressed_length;
54   }
55   uint64_t uncompressed_offset;
56   uint64_t compressed_length;
57   uint64_t uncompressed_length;
58 };
59 
60 struct CompressedFile {
61   // Extents in this array should be in range [0, file_size]. It represents
62   // which bytes inside this file are compressed. Useful for compressed file
63   // systems like EROFS.
64   std::vector<CompressedBlock> blocks;
65   CompressionAlgorithm algo;
66   // Whether the EROFS zero padding feature is enabled
67   bool zero_padding_enabled{};
68 };
69 
70 }  // namespace chromeos_update_engine
71 
72 #endif
73