Home | History | Annotate | Download | only in courgette
      1 // 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 COURGETTE_COURGETTE_H_
      6 #define COURGETTE_COURGETTE_H_
      7 
      8 #include <stddef.h>   // Required to define size_t on GCC
      9 
     10 #include "base/files/file_path.h"
     11 
     12 namespace courgette {
     13 
     14 // Status codes for Courgette APIs.
     15 //
     16 // Client code should only rely on the distintion between C_OK and the other
     17 // status codes.
     18 //
     19 enum Status {
     20   C_OK = 1,                       // Successful operation.
     21 
     22   C_GENERAL_ERROR = 2,            // Error other than listed below.
     23 
     24   C_READ_OPEN_ERROR = 3,          // Could not open input file for reading.
     25   C_READ_ERROR = 4,               // Could not read from opened input file.
     26 
     27   C_WRITE_OPEN_ERROR = 3,         // Could not open output file for writing.
     28   C_WRITE_ERROR = 4,              // Could not write to opened output file.
     29 
     30   C_BAD_ENSEMBLE_MAGIC = 5,       // Ensemble patch has bad magic.
     31   C_BAD_ENSEMBLE_VERSION = 6,     // Ensemble patch has wrong version.
     32   C_BAD_ENSEMBLE_HEADER = 7,      // Ensemble patch has corrupt header.
     33   C_BAD_ENSEMBLE_CRC = 8,         // Ensemble patch has corrupt data.
     34 
     35   C_BAD_TRANSFORM = 12,           // Transform mis-specified.
     36   C_BAD_BASE = 13,                // Base for transform malformed.
     37 
     38   C_BINARY_DIFF_CRC_ERROR = 14,   // Internal diff input doesn't have expected
     39                                   // CRC.
     40 
     41   // Internal errors.
     42   C_STREAM_ERROR = 20,            // Unexpected error from streams.h.
     43   C_STREAM_NOT_CONSUMED = 21,     // Stream has extra data, is expected to be
     44                                   // used up.
     45   C_SERIALIZATION_FAILED = 22,    //
     46   C_DESERIALIZATION_FAILED = 23,  //
     47   C_INPUT_NOT_RECOGNIZED = 24,    // Unrecognized input (not an executable).
     48   C_DISASSEMBLY_FAILED = 25,      //
     49   C_ASSEMBLY_FAILED = 26,         //
     50   C_ADJUSTMENT_FAILED = 27,       //
     51   C_TRIM_FAILED = 28,             // TrimLabels failed
     52 };
     53 
     54 // What type of executable is something
     55 // This is part of the patch format. Never reuse an id number.
     56 enum ExecutableType {
     57   EXE_UNKNOWN = 0,
     58   EXE_WIN_32_X86 = 1,
     59   EXE_ELF_32_X86 = 2,
     60   EXE_ELF_32_ARM = 3,
     61 };
     62 
     63 class SinkStream;
     64 class SinkStreamSet;
     65 class SourceStream;
     66 class SourceStreamSet;
     67 
     68 class AssemblyProgram;
     69 class 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.
     74 Status ApplyEnsemblePatch(SourceStream* old, SourceStream* patch,
     75                           SinkStream* output);
     76 
     77 // Applies the patch in |patch_file_name| to the bytes in |old_file_name| and
     78 // writes the transformed ensemble to |new_file_name|.
     79 // Returns C_OK unless something went wrong.
     80 // This function first validates that the patch file has a proper header, so the
     81 // function can be used to 'try' a patch.
     82 Status ApplyEnsemblePatch(const base::FilePath::CharType* old_file_name,
     83                           const base::FilePath::CharType* patch_file_name,
     84                           const base::FilePath::CharType* new_file_name);
     85 
     86 // Generates a patch that will transform the bytes in |old| into the bytes in
     87 // |target|.
     88 // Returns C_OK unless something when wrong (unexpected).
     89 Status GenerateEnsemblePatch(SourceStream* old, SourceStream* target,
     90                              SinkStream* patch);
     91 
     92 // Detects the type of an executable file, and it's length. The length
     93 // may be slightly smaller than some executables (like ELF), but will include
     94 // all bytes the courgette algorithm has special benefit for.
     95 // On sucess:
     96 //   Fill in type and detected_length, and return C_OK.
     97 // On failure:
     98 //   Fill in type with UNKNOWN, detected_length with 0, and
     99 //   return C_INPUT_NOT_RECOGNIZED
    100 Status DetectExecutableType(const void* buffer, size_t length,
    101                             ExecutableType* type,
    102                             size_t* detected_length);
    103 
    104 // Attempts to detect the type of executable, and parse it with the
    105 // appropriate tools, storing the pointer to the AssemblyProgram in |*output|.
    106 // Returns C_OK if successful, otherwise returns an error status and sets
    107 // |*output| to NULL.
    108 Status ParseDetectedExecutable(const void* buffer, size_t length,
    109                                AssemblyProgram** output);
    110 
    111 // Trims labels used fewer than a given number of times from an
    112 // assembly program in-place.
    113 Status TrimLabels(AssemblyProgram* program);
    114 
    115 // Converts |program| into encoded form, returning it as |*output|.
    116 // Returns C_OK if succeeded, otherwise returns an error status and
    117 // sets |*output| to NULL
    118 Status Encode(AssemblyProgram* program, EncodedProgram** output);
    119 
    120 // Serializes |encoded| into the stream set.
    121 // Returns C_OK if succeeded, otherwise returns an error status.
    122 Status WriteEncodedProgram(EncodedProgram* encoded, SinkStreamSet* sink);
    123 
    124 // Assembles |encoded|, emitting the bytes into |buffer|.
    125 // Returns C_OK if succeeded, otherwise returns an error status and leaves
    126 // |buffer| in an undefined state.
    127 Status Assemble(EncodedProgram* encoded, SinkStream* buffer);
    128 
    129 // Deserializes program from the stream set.
    130 // Returns C_OK if succeeded, otherwise returns an error status and
    131 // sets |*output| to NULL
    132 Status ReadEncodedProgram(SourceStreamSet* source, EncodedProgram** output);
    133 
    134 // Used to free an AssemblyProgram returned by other APIs.
    135 void DeleteAssemblyProgram(AssemblyProgram* program);
    136 
    137 // Used to free an EncodedProgram returned by other APIs.
    138 void DeleteEncodedProgram(EncodedProgram* encoded);
    139 
    140 // Adjusts |program| to look more like |model|.
    141 //
    142 Status Adjust(const AssemblyProgram& model, AssemblyProgram *program);
    143 
    144 }  // namespace courgette
    145 #endif  // COURGETTE_COURGETTE_H_
    146