blob: 1907e73189bfbef59dd9e498adc5a53490f550fa [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
Tianjie Xu2e70b552018-03-02 16:22:10 -080017// Class to store the patch writer options about format, type and
18// brotli_quality.
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080019class BsdiffArguments {
20 public:
21 BsdiffArguments()
22 : format_(BsdiffFormat::kLegacy),
23 compressor_type_(CompressorType::kBZ2),
Tianjie Xu2e70b552018-03-02 16:22:10 -080024 brotli_quality_(-1) {}
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080025
Tianjie Xu2e70b552018-03-02 16:22:10 -080026 BsdiffArguments(BsdiffFormat format, CompressorType type, int brotli_quality)
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080027 : format_(format),
28 compressor_type_(type),
Tianjie Xu2e70b552018-03-02 16:22:10 -080029 brotli_quality_(brotli_quality) {}
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080030
31 // Check if the compressor type is compatible with the bsdiff format.
32 bool IsValid() const;
33
34 // Getter functions.
35 BsdiffFormat format() const { return format_; }
36
Alex Deymo383f6772018-02-08 15:50:11 +010037 int min_length() const { return min_length_; }
38
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080039 CompressorType compressor_type() const { return compressor_type_; }
40
Tianjie Xu2e70b552018-03-02 16:22:10 -080041 int brotli_quality() const { return brotli_quality_; }
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080042
43 // Parse the command line arguments of the main function and set all the
44 // fields accordingly.
45 bool ParseCommandLine(int argc, char** argv);
46
47 // Parse the compression type from string.
Alex Deymo89439c52018-02-13 11:27:30 +010048 static bool ParseCompressorType(const std::string& str, CompressorType* type);
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080049
Alex Deymo383f6772018-02-08 15:50:11 +010050 // Parse the minimum length parameter from string.
Alex Deymo89439c52018-02-13 11:27:30 +010051 static bool ParseMinLength(const std::string& str, size_t* len);
Alex Deymo383f6772018-02-08 15:50:11 +010052
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080053 // Parse the bsdiff format from string.
Alex Deymo89439c52018-02-13 11:27:30 +010054 static bool ParseBsdiffFormat(const std::string& str, BsdiffFormat* format);
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080055
Tianjie Xu2e70b552018-03-02 16:22:10 -080056 // Parse the compression quality (for brotli) from string; also check if the
57 // value is within the valid range.
58 static bool ParseQuality(const std::string& str,
59 int* quality,
60 int min,
61 int max);
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080062
63 private:
64 // Current format supported are the legacy "BSDIFF40" or "BSDF2".
65 BsdiffFormat format_;
66
67 // The algorithm to compress the patch, i.e. BZ2 or Brotli.
68 CompressorType compressor_type_;
69
Tianjie Xu2e70b552018-03-02 16:22:10 -080070 // The quality of brotli compressor.
71 int brotli_quality_;
Alex Deymo383f6772018-02-08 15:50:11 +010072
73 size_t min_length_{0};
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080074};
75
76} // namespace bsdiff
77
Alex Deymo383f6772018-02-08 15:50:11 +010078#endif // _BSDIFF_BSDIFF_ARGUMENTS_H_