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