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 <set>
11 #include <string>
12 #include <vector>
13 
14 #include "bsdiff/constants.h"
15 #include "bsdiff/patch_writer_interface.h"
16 
17 namespace bsdiff {
18 
19 // Class to store the patch writer options about format, type and
20 // brotli_quality.
21 class BsdiffArguments {
22  public:
23   BsdiffArguments() : format_(BsdiffFormat::kLegacy), brotli_quality_(-1) {
24     compressor_types_.emplace(CompressorType::kBZ2);
25   }
26 
27   BsdiffArguments(BsdiffFormat format,
28                   std::set<CompressorType> types,
29                   int brotli_quality)
30       : format_(format),
31         compressor_types_(std::move(types)),
32         brotli_quality_(brotli_quality) {}
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 
40   int min_length() const { return min_length_; }
41 
42   std::vector<CompressorType> compressor_types() const;
43 
44   int brotli_quality() const { return brotli_quality_; }
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.
51   static bool ParseCompressorTypes(const std::string& str,
52                                    std::set<CompressorType>* types);
53 
54   // Parse the minimum length parameter from string.
55   static bool ParseMinLength(const std::string& str, size_t* len);
56 
57   // Parse the bsdiff format from string.
58   static bool ParseBsdiffFormat(const std::string& str, BsdiffFormat* format);
59 
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);
66 
67  private:
68   bool IsCompressorSupported(CompressorType type) const;
69 
70   // Current format supported are the legacy "BSDIFF40" or "BSDF2".
71   BsdiffFormat format_;
72 
73   // The algorithms to compress the patch, e.g. bz2, brotli.
74   std::set<CompressorType> compressor_types_;
75 
76   // The quality of brotli compressor.
77   int brotli_quality_;
78 
79   size_t min_length_{0};
80 };
81 
82 }  // namespace bsdiff
83 
84 #endif  // _BSDIFF_BSDIFF_ARGUMENTS_H_
85