1*a03ca8b9SKrzysztof Kosiński // Copyright 2017 The Chromium Authors. All rights reserved. 2*a03ca8b9SKrzysztof Kosiński // Use of this source code is governed by a BSD-style license that can be 3*a03ca8b9SKrzysztof Kosiński // found in the LICENSE file. 4*a03ca8b9SKrzysztof Kosiński 5*a03ca8b9SKrzysztof Kosiński #ifndef COMPONENTS_ZUCCHINI_ZUCCHINI_INTEGRATION_H_ 6*a03ca8b9SKrzysztof Kosiński #define COMPONENTS_ZUCCHINI_ZUCCHINI_INTEGRATION_H_ 7*a03ca8b9SKrzysztof Kosiński 8*a03ca8b9SKrzysztof Kosiński #include <string> 9*a03ca8b9SKrzysztof Kosiński 10*a03ca8b9SKrzysztof Kosiński #include "base/files/file.h" 11*a03ca8b9SKrzysztof Kosiński #include "base/files/file_path.h" 12*a03ca8b9SKrzysztof Kosiński #include "components/zucchini/zucchini.h" 13*a03ca8b9SKrzysztof Kosiński 14*a03ca8b9SKrzysztof Kosiński // Zucchini integration interface to wrap core Zucchini library with file I/O. 15*a03ca8b9SKrzysztof Kosiński 16*a03ca8b9SKrzysztof Kosiński namespace zucchini { 17*a03ca8b9SKrzysztof Kosiński 18*a03ca8b9SKrzysztof Kosiński // Generates a patch to transform |old_file| to |new_file|, and writes the 19*a03ca8b9SKrzysztof Kosiński // result to |patch_file|. Since this uses memory mapped files, crashes are 20*a03ca8b9SKrzysztof Kosiński // expected in case of I/O errors. On Windows, |patch_file| is kept iff returned 21*a03ca8b9SKrzysztof Kosiński // code is kStatusSuccess or if |force_keep == true|, and is deleted otherwise. 22*a03ca8b9SKrzysztof Kosiński // For UNIX systems the caller needs to do cleanup since it has ownership of the 23*a03ca8b9SKrzysztof Kosiński // base::File params, and Zucchini has no knowledge of which base::FilePath to 24*a03ca8b9SKrzysztof Kosiński // delete. If |is_raw == true| then uses Raw Zucchini. If |imposed_matches| is 25*a03ca8b9SKrzysztof Kosiński // non-empty, then overrides default element detection and matching heuristics 26*a03ca8b9SKrzysztof Kosiński // with custom element matching encoded in |imposed_matches|, which should be 27*a03ca8b9SKrzysztof Kosiński // formatted as: 28*a03ca8b9SKrzysztof Kosiński // "#+#=#+#,#+#=#+#,..." (e.g., "1+2=3+4", "1+2=3+4,5+6=7+8"), 29*a03ca8b9SKrzysztof Kosiński // where "#+#=#+#" encodes a match as 4 unsigned integers: 30*a03ca8b9SKrzysztof Kosiński // [offset in "old", size in "old", offset in "new", size in "new"]. 31*a03ca8b9SKrzysztof Kosiński status::Code Generate(base::File old_file, 32*a03ca8b9SKrzysztof Kosiński base::File new_file, 33*a03ca8b9SKrzysztof Kosiński base::File patch_file, 34*a03ca8b9SKrzysztof Kosiński bool force_keep = false, 35*a03ca8b9SKrzysztof Kosiński bool is_raw = false, 36*a03ca8b9SKrzysztof Kosiński std::string imposed_matches = ""); 37*a03ca8b9SKrzysztof Kosiński 38*a03ca8b9SKrzysztof Kosiński // Alternative Generate() interface that takes base::FilePath as arguments. 39*a03ca8b9SKrzysztof Kosiński // Performs proper cleanup in Windows and UNIX if failure occurs. 40*a03ca8b9SKrzysztof Kosiński status::Code Generate(const base::FilePath& old_path, 41*a03ca8b9SKrzysztof Kosiński const base::FilePath& new_path, 42*a03ca8b9SKrzysztof Kosiński const base::FilePath& patch_path, 43*a03ca8b9SKrzysztof Kosiński bool force_keep = false, 44*a03ca8b9SKrzysztof Kosiński bool is_raw = false, 45*a03ca8b9SKrzysztof Kosiński std::string imposed_matches = ""); 46*a03ca8b9SKrzysztof Kosiński 47*a03ca8b9SKrzysztof Kosiński // Applies the patch in |patch_file| to |old_file|, and writes the result to 48*a03ca8b9SKrzysztof Kosiński // |new_file|. Since this uses memory mapped files, crashes are expected in case 49*a03ca8b9SKrzysztof Kosiński // of I/O errors. On Windows, |new_file| is kept iff returned code is 50*a03ca8b9SKrzysztof Kosiński // kStatusSuccess or if |force_keep == true|, and is deleted otherwise. For UNIX 51*a03ca8b9SKrzysztof Kosiński // systems the caller needs to do cleanup since it has ownership of the 52*a03ca8b9SKrzysztof Kosiński // base::File params, and Zucchini has no knowledge of which base::FilePath to 53*a03ca8b9SKrzysztof Kosiński // delete. 54*a03ca8b9SKrzysztof Kosiński status::Code Apply(base::File old_file, 55*a03ca8b9SKrzysztof Kosiński base::File patch_file, 56*a03ca8b9SKrzysztof Kosiński base::File new_file, 57*a03ca8b9SKrzysztof Kosiński bool force_keep = false); 58*a03ca8b9SKrzysztof Kosiński 59*a03ca8b9SKrzysztof Kosiński // Alternative Apply() interface that takes base::FilePath as arguments. 60*a03ca8b9SKrzysztof Kosiński // Performs proper cleanup in Windows and UNIX if failure occurs. 61*a03ca8b9SKrzysztof Kosiński status::Code Apply(const base::FilePath& old_path, 62*a03ca8b9SKrzysztof Kosiński const base::FilePath& patch_path, 63*a03ca8b9SKrzysztof Kosiński const base::FilePath& new_path, 64*a03ca8b9SKrzysztof Kosiński bool force_keep = false); 65*a03ca8b9SKrzysztof Kosiński 66*a03ca8b9SKrzysztof Kosiński // Verifies the patch format in |patch_file| and returns 67*a03ca8b9SKrzysztof Kosiński // Code::kStatusPatchReadError if the patch is malformed or version is 68*a03ca8b9SKrzysztof Kosiński // unsupported. Since this uses memory mapped files, crashes are expected in 69*a03ca8b9SKrzysztof Kosiński // case of I/O errors. 70*a03ca8b9SKrzysztof Kosiński status::Code VerifyPatch(base::File patch_file); 71*a03ca8b9SKrzysztof Kosiński 72*a03ca8b9SKrzysztof Kosiński // Alternative VerifyPatch() interface that takes base::FilePath as arguments. 73*a03ca8b9SKrzysztof Kosiński // Performs proper cleanup in Windows and UNIX if failure occurs. 74*a03ca8b9SKrzysztof Kosiński status::Code VerifyPatch(const base::FilePath& patch_path); 75*a03ca8b9SKrzysztof Kosiński 76*a03ca8b9SKrzysztof Kosiński } // namespace zucchini 77*a03ca8b9SKrzysztof Kosiński 78*a03ca8b9SKrzysztof Kosiński #endif // COMPONENTS_ZUCCHINI_ZUCCHINI_INTEGRATION_H_ 79