blob: b330834fca76e3aa1959dabe073ebf4813ba0cc5 [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
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080015namespace bsdiff {
16
17// Class to store the patch writer options about format, type and quality.
18class 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 Deymo383f6772018-02-08 15:50:11 +010036 int min_length() const { return min_length_; }
37
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080038 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 Deymo89439c52018-02-13 11:27:30 +010047 static bool ParseCompressorType(const std::string& str, CompressorType* type);
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080048
Alex Deymo383f6772018-02-08 15:50:11 +010049 // Parse the minimum length parameter from string.
Alex Deymo89439c52018-02-13 11:27:30 +010050 static bool ParseMinLength(const std::string& str, size_t* len);
Alex Deymo383f6772018-02-08 15:50:11 +010051
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080052 // Parse the bsdiff format from string.
Alex Deymo89439c52018-02-13 11:27:30 +010053 static bool ParseBsdiffFormat(const std::string& str, BsdiffFormat* format);
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080054
55 // Parse the compression quality (for brotli) from string.
Alex Deymo89439c52018-02-13 11:27:30 +010056 static bool ParseQuality(const std::string& str, int* quality);
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080057
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 Deymo383f6772018-02-08 15:50:11 +010068
69 size_t min_length_{0};
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080070};
71
72} // namespace bsdiff
73
Alex Deymo383f6772018-02-08 15:50:11 +010074#endif // _BSDIFF_BSDIFF_ARGUMENTS_H_