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