xref: /aosp_15_r20/external/zucchini/zucchini.h (revision a03ca8b91e029cd15055c20c78c2e087c84792e4)
1 // Copyright 2017 The Chromium 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 COMPONENTS_ZUCCHINI_ZUCCHINI_H_
6 #define COMPONENTS_ZUCCHINI_ZUCCHINI_H_
7 
8 #include <string>
9 
10 #include "components/zucchini/buffer_view.h"
11 #include "components/zucchini/patch_reader.h"
12 #include "components/zucchini/patch_writer.h"
13 
14 // Core Zucchini library, consisting of:
15 // - Global constants.
16 // - Patch gen and apply functions, where "old" and "new" data are represented
17 //   as buffers, and patch data represented as EnsemblePatchWriter or
18 //   EnsemblePatchReader.
19 
20 namespace zucchini {
21 
22 namespace status {
23 
24 // Zucchini status code, which can also be used as process exit code. Therefore
25 // success is explicitly 0.
26 enum Code {
27   kStatusSuccess = 0,
28   kStatusInvalidParam = 1,
29   kStatusFileReadError = 2,
30   kStatusFileWriteError = 3,
31   kStatusPatchReadError = 4,
32   kStatusPatchWriteError = 5,
33   kStatusInvalidOldImage = 6,
34   kStatusInvalidNewImage = 7,
35   kStatusFatal = 8,
36 };
37 
38 }  // namespace status
39 
40 // Generates ensemble patch from |old_image| to |new_image| using the default
41 // element detection and matching heuristics, writes the results to
42 // |patch_writer|, and returns a status::Code.
43 status::Code GenerateBuffer(ConstBufferView old_image,
44                             ConstBufferView new_image,
45                             EnsemblePatchWriter* patch_writer);
46 
47 // Same as GenerateEnsemble(), but if |imposed_matches| is non-empty, then
48 // overrides default element detection and matching heuristics with custom
49 // element matching encoded in |imposed_matches|, which should be formatted as:
50 //   "#+#=#+#,#+#=#+#,..."  (e.g., "1+2=3+4", "1+2=3+4,5+6=7+8"),
51 // where "#+#=#+#" encodes a match as 4 unsigned integers:
52 //   [offset in "old", size in "old", offset in "new", size in "new"].
53 status::Code GenerateBufferImposed(ConstBufferView old_image,
54                                    ConstBufferView new_image,
55                                    std::string imposed_matches,
56                                    EnsemblePatchWriter* patch_writer);
57 
58 // Generates raw patch from |old_image| to |new_image|, and writes it to
59 // |patch_writer|.
60 status::Code GenerateBufferRaw(ConstBufferView old_image,
61                                ConstBufferView new_image,
62                                EnsemblePatchWriter* patch_writer);
63 
64 // Applies |patch_reader| to |old_image| to build |new_image|, which refers to
65 // preallocated memory of sufficient size.
66 status::Code ApplyBuffer(ConstBufferView old_image,
67                          const EnsemblePatchReader& patch_reader,
68                          MutableBufferView new_image);
69 
70 }  // namespace zucchini
71 
72 #endif  // COMPONENTS_ZUCCHINI_ZUCCHINI_H_
73