summaryrefslogtreecommitdiff
path: root/bsdiff_arguments.h
blob: becd8b883d352d95472b55eb3bd979bf7ba9268e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef _BSDIFF_BSDIFF_ARGUMENTS_H_
#define _BSDIFF_BSDIFF_ARGUMENTS_H_

#include <stdint.h>

#include <set>
#include <string>
#include <vector>

#include "bsdiff/constants.h"
#include "bsdiff/patch_writer_interface.h"

namespace bsdiff {

// Class to store the patch writer options about format, type and
// brotli_quality.
class BsdiffArguments {
 public:
  BsdiffArguments() : format_(BsdiffFormat::kLegacy), brotli_quality_(-1) {
    compressor_types_.emplace(CompressorType::kBZ2);
  }

  BsdiffArguments(BsdiffFormat format,
                  std::set<CompressorType> types,
                  int brotli_quality)
      : format_(format),
        compressor_types_(std::move(types)),
        brotli_quality_(brotli_quality) {}

  // Check if the compressor type is compatible with the bsdiff format.
  bool IsValid() const;

  // Getter functions.
  BsdiffFormat format() const { return format_; }

  int min_length() const { return min_length_; }

  std::vector<CompressorType> compressor_types() const;

  int brotli_quality() const { return brotli_quality_; }

  // Parse the command line arguments of the main function and set all the
  // fields accordingly.
  bool ParseCommandLine(int argc, char** argv);

  // Parse the compression type from string.
  static bool ParseCompressorTypes(const std::string& str,
                                   std::set<CompressorType>* types);

  // Parse the minimum length parameter from string.
  static bool ParseMinLength(const std::string& str, size_t* len);

  // Parse the bsdiff format from string.
  static bool ParseBsdiffFormat(const std::string& str, BsdiffFormat* format);

  // Parse the compression quality (for brotli) from string; also check if the
  // value is within the valid range.
  static bool ParseQuality(const std::string& str,
                           int* quality,
                           int min,
                           int max);

 private:
  bool IsCompressorSupported(CompressorType type) const;

  // Current format supported are the legacy "BSDIFF40" or "BSDF2".
  BsdiffFormat format_;

  // The algorithms to compress the patch, e.g. bz2, brotli.
  std::set<CompressorType> compressor_types_;

  // The quality of brotli compressor.
  int brotli_quality_;

  size_t min_length_{0};
};

}  // namespace bsdiff

#endif  // _BSDIFF_BSDIFF_ARGUMENTS_H_