1 // Copyright 2017 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef SRC_INCLUDE_PUFFIN_UTILS_H_
6 #define SRC_INCLUDE_PUFFIN_UTILS_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "puffin/common.h"
12 #include "puffin/stream.h"
13
14 namespace puffin {
15
16 // Counts the number of bytes in a list of |ByteExtent|s.
17 PUFFIN_EXPORT
18 uint64_t BytesInByteExtents(const std::vector<ByteExtent>& extents);
19
20 // Converts an array of |ByteExtens| or |BitExtents| to a string. Each extent
21 // has format "offset:length" and are comma separated.
22 template <typename T>
ExtentsToString(const T & extents)23 PUFFIN_EXPORT std::string ExtentsToString(const T& extents) {
24 std::string str;
25 for (const auto& extent : extents) {
26 str += std::to_string(extent.offset) + ":" + std::to_string(extent.length) +
27 ",";
28 }
29 return str;
30 }
31
32 // Locates deflate locations for a zlib buffer |data|. It locates by removing
33 // header and footer bytes from the zlib stream.
34 bool LocateDeflatesInZlib(const Buffer& data,
35 std::vector<ByteExtent>* deflate_blocks);
36
37 // Similar to the function above, except that it accepts the file path to the
38 // source and a list of zlib blocks and returns the deflate addresses in bit
39 // extents.
40 PUFFIN_EXPORT
41 bool LocateDeflatesInZlibBlocks(const std::string& file_path,
42 const std::vector<ByteExtent>& zlibs,
43 std::vector<BitExtent>* deflates);
44
45 // Searches for deflate locations in a gzip file. The results are
46 // saved in |deflate_blocks|.
47 bool LocateDeflatesInGzip(const Buffer& data,
48 std::vector<ByteExtent>* deflate_blocks);
49
50 // Search for the deflates in a zip archive, and put the result in
51 // |deflate_blocks|.
52 bool LocateDeflatesInZipArchive(const Buffer& data,
53 std::vector<ByteExtent>* deflate_blocks);
54
55 PUFFIN_EXPORT
56 // Create a list of deflate subblock locations from the deflate blocks in a
57 // zip archive.
58 bool LocateDeflateSubBlocksInZipArchive(const Buffer& data,
59 std::vector<BitExtent>* deflates);
60
61 // Reads the deflates in from |deflates| and returns a list of its subblock
62 // locations. Each subblock in practice is a deflate stream by itself.
63 // Assumption is that the first subblock in each deflate in |deflates| start in
64 // byte boundary.
65 bool FindDeflateSubBlocks(const UniqueStreamPtr& src,
66 const std::vector<ByteExtent>& deflates,
67 std::vector<BitExtent>* subblock_deflates);
68
69 // Finds the location of puffs in the deflate stream |src| based on the location
70 // of |deflates| and populates the |puffs|. We assume |deflates| are sorted by
71 // their offset value. |out_puff_size| will be the size of the puff stream.
72 bool FindPuffLocations(const UniqueStreamPtr& src,
73 const std::vector<BitExtent>& deflates,
74 std::vector<ByteExtent>* puffs,
75 uint64_t* out_puff_size);
76
77 } // namespace puffin
78
79 #endif // SRC_INCLUDE_PUFFIN_UTILS_H_
80