Home | History | Annotate | Download | only in bsdiff
      1 // 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 
     15 namespace bsdiff {
     16 
     17 // Class to store the patch writer options about format, type and quality.
     18 class 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 
     36   int min_length() const { return min_length_; }
     37 
     38   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.
     47   static bool ParseCompressorType(const std::string& str, CompressorType* type);
     48 
     49   // Parse the minimum length parameter from string.
     50   static bool ParseMinLength(const std::string& str, size_t* len);
     51 
     52   // Parse the bsdiff format from string.
     53   static bool ParseBsdiffFormat(const std::string& str, BsdiffFormat* format);
     54 
     55   // Parse the compression quality (for brotli) from string.
     56   static bool ParseQuality(const std::string& str, int* quality);
     57 
     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_;
     68 
     69   size_t min_length_{0};
     70 };
     71 
     72 }  // namespace bsdiff
     73 
     74 #endif  // _BSDIFF_BSDIFF_ARGUMENTS_H_
     75