blob: becd8b883d352d95472b55eb3bd979bf7ba9268e [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
Tianjie Xu77833b62018-03-07 18:13:47 -080010#include <set>
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080011#include <string>
Tianjie Xu77833b62018-03-07 18:13:47 -080012#include <vector>
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080013
14#include "bsdiff/constants.h"
15#include "bsdiff/patch_writer_interface.h"
16
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080017namespace bsdiff {
18
Tianjie Xu2e70b552018-03-02 16:22:10 -080019// Class to store the patch writer options about format, type and
20// brotli_quality.
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080021class BsdiffArguments {
22 public:
Tianjie Xu77833b62018-03-07 18:13:47 -080023 BsdiffArguments() : format_(BsdiffFormat::kLegacy), brotli_quality_(-1) {
24 compressor_types_.emplace(CompressorType::kBZ2);
25 }
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080026
Tianjie Xu77833b62018-03-07 18:13:47 -080027 BsdiffArguments(BsdiffFormat format,
28 std::set<CompressorType> types,
29 int brotli_quality)
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080030 : format_(format),
Tianjie Xu77833b62018-03-07 18:13:47 -080031 compressor_types_(std::move(types)),
Tianjie Xu2e70b552018-03-02 16:22:10 -080032 brotli_quality_(brotli_quality) {}
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080033
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
Alex Deymo383f6772018-02-08 15:50:11 +010040 int min_length() const { return min_length_; }
41
Tianjie Xu77833b62018-03-07 18:13:47 -080042 std::vector<CompressorType> compressor_types() const;
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080043
Tianjie Xu2e70b552018-03-02 16:22:10 -080044 int brotli_quality() const { return brotli_quality_; }
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080045
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.
Tianjie Xu77833b62018-03-07 18:13:47 -080051 static bool ParseCompressorTypes(const std::string& str,
52 std::set<CompressorType>* types);
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080053
Alex Deymo383f6772018-02-08 15:50:11 +010054 // Parse the minimum length parameter from string.
Alex Deymo89439c52018-02-13 11:27:30 +010055 static bool ParseMinLength(const std::string& str, size_t* len);
Alex Deymo383f6772018-02-08 15:50:11 +010056
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080057 // Parse the bsdiff format from string.
Alex Deymo89439c52018-02-13 11:27:30 +010058 static bool ParseBsdiffFormat(const std::string& str, BsdiffFormat* format);
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080059
Tianjie Xu2e70b552018-03-02 16:22:10 -080060 // 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);
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080066
67 private:
Tianjie Xu77833b62018-03-07 18:13:47 -080068 bool IsCompressorSupported(CompressorType type) const;
69
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080070 // Current format supported are the legacy "BSDIFF40" or "BSDF2".
71 BsdiffFormat format_;
72
Tianjie Xu77833b62018-03-07 18:13:47 -080073 // The algorithms to compress the patch, e.g. bz2, brotli.
74 std::set<CompressorType> compressor_types_;
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080075
Tianjie Xu2e70b552018-03-02 16:22:10 -080076 // The quality of brotli compressor.
77 int brotli_quality_;
Alex Deymo383f6772018-02-08 15:50:11 +010078
79 size_t min_length_{0};
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080080};
81
82} // namespace bsdiff
83
Alex Deymo383f6772018-02-08 15:50:11 +010084#endif // _BSDIFF_BSDIFF_ARGUMENTS_H_