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