blob: b80c3ca50eb2b45e4eaf4a35fbbcf2a3ef7552be [file] [log] [blame]
Yavor Goulishev9c08e842020-04-29 14:03:33 -07001// Copyright (c) 2011 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_COURGETTE_COURGETTE_H_
6#define COMPONENTS_COURGETTE_COURGETTE_H_
7
8#include <stddef.h> // Required to define size_t on GCC
9
10#include "base/files/file.h"
11#include "base/files/file_path.h"
12
13namespace courgette {
14
15// Status codes for Courgette APIs.
16//
17// Client code should only rely on the distintion between C_OK and the other
18// status codes.
19//
20enum Status {
21 C_OK = 1, // Successful operation.
22
23 C_GENERAL_ERROR = 2, // Error other than listed below.
24
25 C_READ_OPEN_ERROR = 3, // Could not open input file for reading.
26 C_READ_ERROR = 4, // Could not read from opened input file.
27
28 C_WRITE_OPEN_ERROR = 3, // Could not open output file for writing.
29 C_WRITE_ERROR = 4, // Could not write to opened output file.
30
31 C_BAD_ENSEMBLE_MAGIC = 5, // Ensemble patch has bad magic.
32 C_BAD_ENSEMBLE_VERSION = 6, // Ensemble patch has wrong version.
33 C_BAD_ENSEMBLE_HEADER = 7, // Ensemble patch has corrupt header.
34 C_BAD_ENSEMBLE_CRC = 8, // Ensemble patch has corrupt data.
35
36 C_BAD_TRANSFORM = 12, // Transform mis-specified.
37 C_BAD_BASE = 13, // Base for transform malformed.
38
39 C_BINARY_DIFF_CRC_ERROR = 14, // Internal diff input doesn't have expected
40 // CRC.
41
42 // Internal errors.
43 C_STREAM_ERROR = 20, // Unexpected error from streams.h.
44 C_STREAM_NOT_CONSUMED = 21, // Stream has extra data, is expected to be
45 // used up.
46 C_SERIALIZATION_FAILED = 22, //
47 C_DESERIALIZATION_FAILED = 23, //
48 C_INPUT_NOT_RECOGNIZED = 24, // Unrecognized input (not an executable).
49 C_DISASSEMBLY_FAILED = 25, //
50 C_ASSEMBLY_FAILED = 26, //
51 C_ADJUSTMENT_FAILED = 27, //
52};
53
54// What type of executable is something
55// This is part of the patch format. Never reuse an id number.
56enum ExecutableType {
57 EXE_UNKNOWN = 0,
58 EXE_WIN_32_X86 = 1,
59 EXE_ELF_32_X86 = 2,
60 EXE_ELF_32_ARM = 3,
61 EXE_WIN_32_X64 = 4,
62};
63
64class SinkStream;
65class SinkStreamSet;
66class SourceStream;
67
68class AssemblyProgram;
69class EncodedProgram;
70
71// Applies the patch to the bytes in |old| and writes the transformed ensemble
72// to |output|.
73// Returns C_OK unless something went wrong.
74Status ApplyEnsemblePatch(SourceStream* old,
75 SourceStream* patch,
76 SinkStream* output);
77
78// Applies the patch in |patch_file| to the bytes in |old_file| and writes the
79// transformed ensemble to |new_file|.
80// Returns C_OK unless something went wrong.
81// This function first validates that the patch file has a proper header, so the
82// function can be used to 'try' a patch.
83Status ApplyEnsemblePatch(base::File old_file,
84 base::File patch_file,
85 base::File new_file);
86
87// Applies the patch in |patch_file_name| to the bytes in |old_file_name| and
88// writes the transformed ensemble to |new_file_name|.
89// Returns C_OK unless something went wrong.
90// This function first validates that the patch file has a proper header, so the
91// function can be used to 'try' a patch.
92Status ApplyEnsemblePatch(const base::FilePath::CharType* old_file_name,
93 const base::FilePath::CharType* patch_file_name,
94 const base::FilePath::CharType* new_file_name);
95
96// Generates a patch that will transform the bytes in |old| into the bytes in
97// |target|.
98// Returns C_OK unless something when wrong (unexpected).
99Status GenerateEnsemblePatch(SourceStream* old,
100 SourceStream* target,
101 SinkStream* patch);
102
103// Serializes |encoded| into the stream set.
104// Returns C_OK if succeeded, otherwise returns an error status.
105Status WriteEncodedProgram(EncodedProgram* encoded, SinkStreamSet* sink);
106
107// Assembles |encoded|, emitting the bytes into |buffer|.
108// Returns C_OK if succeeded, otherwise returns an error status and leaves
109// |buffer| in an undefined state.
110Status Assemble(EncodedProgram* encoded, SinkStream* buffer);
111
112// Adjusts |program| to look more like |model|.
113//
114Status Adjust(const AssemblyProgram& model, AssemblyProgram* program);
115
116} // namespace courgette
117
118#endif // COMPONENTS_COURGETTE_COURGETTE_H_