xref: /aosp_15_r20/external/bsdiff/bsdiff_arguments.h (revision a3a45f308bd90ef1a6e6a5e8fb92fe449b895909)
1*a3a45f30SXin Li // Copyright 2017 The Chromium OS Authors. All rights reserved.
2*a3a45f30SXin Li // Use of this source code is governed by a BSD-style license that can be
3*a3a45f30SXin Li // found in the LICENSE file.
4*a3a45f30SXin Li 
5*a3a45f30SXin Li #ifndef _BSDIFF_BSDIFF_ARGUMENTS_H_
6*a3a45f30SXin Li #define _BSDIFF_BSDIFF_ARGUMENTS_H_
7*a3a45f30SXin Li 
8*a3a45f30SXin Li #include <stdint.h>
9*a3a45f30SXin Li 
10*a3a45f30SXin Li #include <set>
11*a3a45f30SXin Li #include <string>
12*a3a45f30SXin Li #include <vector>
13*a3a45f30SXin Li 
14*a3a45f30SXin Li #include "bsdiff/constants.h"
15*a3a45f30SXin Li #include "bsdiff/patch_writer_interface.h"
16*a3a45f30SXin Li 
17*a3a45f30SXin Li namespace bsdiff {
18*a3a45f30SXin Li 
19*a3a45f30SXin Li // Class to store the patch writer options about format, type and
20*a3a45f30SXin Li // brotli_quality.
21*a3a45f30SXin Li class BsdiffArguments {
22*a3a45f30SXin Li  public:
BsdiffArguments()23*a3a45f30SXin Li   BsdiffArguments() : format_(BsdiffFormat::kLegacy), brotli_quality_(-1) {
24*a3a45f30SXin Li     compressor_types_.emplace(CompressorType::kBZ2);
25*a3a45f30SXin Li   }
26*a3a45f30SXin Li 
BsdiffArguments(BsdiffFormat format,std::set<CompressorType> types,int brotli_quality)27*a3a45f30SXin Li   BsdiffArguments(BsdiffFormat format,
28*a3a45f30SXin Li                   std::set<CompressorType> types,
29*a3a45f30SXin Li                   int brotli_quality)
30*a3a45f30SXin Li       : format_(format),
31*a3a45f30SXin Li         compressor_types_(std::move(types)),
32*a3a45f30SXin Li         brotli_quality_(brotli_quality) {}
33*a3a45f30SXin Li 
34*a3a45f30SXin Li   // Check if the compressor type is compatible with the bsdiff format.
35*a3a45f30SXin Li   bool IsValid() const;
36*a3a45f30SXin Li 
37*a3a45f30SXin Li   // Getter functions.
format()38*a3a45f30SXin Li   BsdiffFormat format() const { return format_; }
39*a3a45f30SXin Li 
min_length()40*a3a45f30SXin Li   int min_length() const { return min_length_; }
41*a3a45f30SXin Li 
42*a3a45f30SXin Li   std::vector<CompressorType> compressor_types() const;
43*a3a45f30SXin Li 
brotli_quality()44*a3a45f30SXin Li   int brotli_quality() const { return brotli_quality_; }
45*a3a45f30SXin Li 
46*a3a45f30SXin Li   // Parse the command line arguments of the main function and set all the
47*a3a45f30SXin Li   // fields accordingly.
48*a3a45f30SXin Li   bool ParseCommandLine(int argc, char** argv);
49*a3a45f30SXin Li 
50*a3a45f30SXin Li   // Parse the compression type from string.
51*a3a45f30SXin Li   static bool ParseCompressorTypes(const std::string& str,
52*a3a45f30SXin Li                                    std::set<CompressorType>* types);
53*a3a45f30SXin Li 
54*a3a45f30SXin Li   // Parse the minimum length parameter from string.
55*a3a45f30SXin Li   static bool ParseMinLength(const std::string& str, size_t* len);
56*a3a45f30SXin Li 
57*a3a45f30SXin Li   // Parse the bsdiff format from string.
58*a3a45f30SXin Li   static bool ParseBsdiffFormat(const std::string& str, BsdiffFormat* format);
59*a3a45f30SXin Li 
60*a3a45f30SXin Li   // Parse the compression quality (for brotli) from string; also check if the
61*a3a45f30SXin Li   // value is within the valid range.
62*a3a45f30SXin Li   static bool ParseQuality(const std::string& str,
63*a3a45f30SXin Li                            int* quality,
64*a3a45f30SXin Li                            int min,
65*a3a45f30SXin Li                            int max);
66*a3a45f30SXin Li 
67*a3a45f30SXin Li  private:
68*a3a45f30SXin Li   bool IsCompressorSupported(CompressorType type) const;
69*a3a45f30SXin Li 
70*a3a45f30SXin Li   // Current format supported are the legacy "BSDIFF40" or "BSDF2".
71*a3a45f30SXin Li   BsdiffFormat format_;
72*a3a45f30SXin Li 
73*a3a45f30SXin Li   // The algorithms to compress the patch, e.g. bz2, brotli.
74*a3a45f30SXin Li   std::set<CompressorType> compressor_types_;
75*a3a45f30SXin Li 
76*a3a45f30SXin Li   // The quality of brotli compressor.
77*a3a45f30SXin Li   int brotli_quality_;
78*a3a45f30SXin Li 
79*a3a45f30SXin Li   size_t min_length_{0};
80*a3a45f30SXin Li };
81*a3a45f30SXin Li 
82*a3a45f30SXin Li }  // namespace bsdiff
83*a3a45f30SXin Li 
84*a3a45f30SXin Li #endif  // _BSDIFF_BSDIFF_ARGUMENTS_H_
85