Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 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 _BSDIFF_BSDIFF_ARGUMENTS_H_ |
| 6 | #define _BSDIFF_BSDIFF_ARGUMENTS_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 10 | #include <set> |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 11 | #include <string> |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 12 | #include <vector> |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 13 | |
| 14 | #include "bsdiff/constants.h" |
| 15 | #include "bsdiff/patch_writer_interface.h" |
| 16 | |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 17 | namespace bsdiff { |
| 18 | |
Tianjie Xu | 2e70b55 | 2018-03-02 16:22:10 -0800 | [diff] [blame] | 19 | // Class to store the patch writer options about format, type and |
| 20 | // brotli_quality. |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 21 | class BsdiffArguments { |
| 22 | public: |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 23 | BsdiffArguments() : format_(BsdiffFormat::kLegacy), brotli_quality_(-1) { |
| 24 | compressor_types_.emplace(CompressorType::kBZ2); |
| 25 | } |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 26 | |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 27 | BsdiffArguments(BsdiffFormat format, |
| 28 | std::set<CompressorType> types, |
| 29 | int brotli_quality) |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 30 | : format_(format), |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 31 | compressor_types_(std::move(types)), |
Tianjie Xu | 2e70b55 | 2018-03-02 16:22:10 -0800 | [diff] [blame] | 32 | brotli_quality_(brotli_quality) {} |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 33 | |
| 34 | // Check if the compressor type is compatible with the bsdiff format. |
| 35 | bool IsValid() const; |
| 36 | |
| 37 | // Getter functions. |
| 38 | BsdiffFormat format() const { return format_; } |
| 39 | |
Alex Deymo | 383f677 | 2018-02-08 15:50:11 +0100 | [diff] [blame] | 40 | int min_length() const { return min_length_; } |
| 41 | |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 42 | std::vector<CompressorType> compressor_types() const; |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 43 | |
Tianjie Xu | 2e70b55 | 2018-03-02 16:22:10 -0800 | [diff] [blame] | 44 | int brotli_quality() const { return brotli_quality_; } |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 45 | |
| 46 | // Parse the command line arguments of the main function and set all the |
| 47 | // fields accordingly. |
| 48 | bool ParseCommandLine(int argc, char** argv); |
| 49 | |
| 50 | // Parse the compression type from string. |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 51 | static bool ParseCompressorTypes(const std::string& str, |
| 52 | std::set<CompressorType>* types); |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 53 | |
Alex Deymo | 383f677 | 2018-02-08 15:50:11 +0100 | [diff] [blame] | 54 | // Parse the minimum length parameter from string. |
Alex Deymo | 89439c5 | 2018-02-13 11:27:30 +0100 | [diff] [blame] | 55 | static bool ParseMinLength(const std::string& str, size_t* len); |
Alex Deymo | 383f677 | 2018-02-08 15:50:11 +0100 | [diff] [blame] | 56 | |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 57 | // Parse the bsdiff format from string. |
Alex Deymo | 89439c5 | 2018-02-13 11:27:30 +0100 | [diff] [blame] | 58 | static bool ParseBsdiffFormat(const std::string& str, BsdiffFormat* format); |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 59 | |
Tianjie Xu | 2e70b55 | 2018-03-02 16:22:10 -0800 | [diff] [blame] | 60 | // Parse the compression quality (for brotli) from string; also check if the |
| 61 | // value is within the valid range. |
| 62 | static bool ParseQuality(const std::string& str, |
| 63 | int* quality, |
| 64 | int min, |
| 65 | int max); |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 66 | |
| 67 | private: |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 68 | bool IsCompressorSupported(CompressorType type) const; |
| 69 | |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 70 | // Current format supported are the legacy "BSDIFF40" or "BSDF2". |
| 71 | BsdiffFormat format_; |
| 72 | |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 73 | // The algorithms to compress the patch, e.g. bz2, brotli. |
| 74 | std::set<CompressorType> compressor_types_; |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 75 | |
Tianjie Xu | 2e70b55 | 2018-03-02 16:22:10 -0800 | [diff] [blame] | 76 | // The quality of brotli compressor. |
| 77 | int brotli_quality_; |
Alex Deymo | 383f677 | 2018-02-08 15:50:11 +0100 | [diff] [blame] | 78 | |
| 79 | size_t min_length_{0}; |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | } // namespace bsdiff |
| 83 | |
Alex Deymo | 383f677 | 2018-02-08 15:50:11 +0100 | [diff] [blame] | 84 | #endif // _BSDIFF_BSDIFF_ARGUMENTS_H_ |