courgette.h 4.3 KB

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