| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file contains the code to apply a Courgette patch. | 5 // This file contains the code to apply a Courgette patch. |
| 6 | 6 |
| 7 #include "courgette/ensemble.h" | 7 #include "courgette/ensemble.h" |
| 8 | 8 |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 | 11 |
| 12 #include <memory> |
| 13 #include <utility> |
| 14 |
| 12 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 13 #include "base/files/memory_mapped_file.h" | 16 #include "base/files/memory_mapped_file.h" |
| 14 #include "base/logging.h" | 17 #include "base/logging.h" |
| 15 #include "base/macros.h" | 18 #include "base/macros.h" |
| 16 #include "courgette/crc.h" | 19 #include "courgette/crc.h" |
| 17 #include "courgette/patcher_x86_32.h" | 20 #include "courgette/patcher_x86_32.h" |
| 18 #include "courgette/region.h" | 21 #include "courgette/region.h" |
| 19 #include "courgette/simple_delta.h" | 22 #include "courgette/simple_delta.h" |
| 20 #include "courgette/streams.h" | 23 #include "courgette/streams.h" |
| 21 | 24 |
| 22 namespace courgette { | 25 namespace courgette { |
| 23 | 26 |
| 24 // EnsemblePatchApplication is all the logic and data required to apply the | 27 // EnsemblePatchApplication is all the logic and data required to apply the |
| 25 // multi-stage patch. | 28 // multi-stage patch. |
| 26 class EnsemblePatchApplication { | 29 class EnsemblePatchApplication { |
| 27 public: | 30 public: |
| 28 EnsemblePatchApplication(); | 31 EnsemblePatchApplication(); |
| 29 ~EnsemblePatchApplication(); | 32 ~EnsemblePatchApplication() = default; |
| 30 | 33 |
| 31 Status ReadHeader(SourceStream* header_stream); | 34 Status ReadHeader(SourceStream* header_stream); |
| 32 | 35 |
| 33 Status InitBase(const Region& region); | 36 Status InitBase(const Region& region); |
| 34 | 37 |
| 35 Status ValidateBase(); | 38 Status ValidateBase(); |
| 36 | 39 |
| 37 Status ReadInitialParameters(SourceStream* initial_parameters); | 40 Status ReadInitialParameters(SourceStream* initial_parameters); |
| 38 | 41 |
| 39 Status PredictTransformParameters(SinkStreamSet* predicted_parameters); | 42 Status PredictTransformParameters(SinkStreamSet* predicted_parameters); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 61 SourceStream* correction, | 64 SourceStream* correction, |
| 62 SourceStreamSet* corrected_items, | 65 SourceStreamSet* corrected_items, |
| 63 SinkStream* corrected_items_storage); | 66 SinkStream* corrected_items_storage); |
| 64 | 67 |
| 65 Region base_region_; // Location of in-memory copy of 'old' version. | 68 Region base_region_; // Location of in-memory copy of 'old' version. |
| 66 | 69 |
| 67 uint32_t source_checksum_; | 70 uint32_t source_checksum_; |
| 68 uint32_t target_checksum_; | 71 uint32_t target_checksum_; |
| 69 uint32_t final_patch_input_size_prediction_; | 72 uint32_t final_patch_input_size_prediction_; |
| 70 | 73 |
| 71 std::vector<TransformationPatcher*> patchers_; | 74 std::vector<std::unique_ptr<TransformationPatcher>> patchers_; |
| 72 | 75 |
| 73 SinkStream corrected_parameters_storage_; | 76 SinkStream corrected_parameters_storage_; |
| 74 SinkStream corrected_elements_storage_; | 77 SinkStream corrected_elements_storage_; |
| 75 | 78 |
| 76 DISALLOW_COPY_AND_ASSIGN(EnsemblePatchApplication); | 79 DISALLOW_COPY_AND_ASSIGN(EnsemblePatchApplication); |
| 77 }; | 80 }; |
| 78 | 81 |
| 79 EnsemblePatchApplication::EnsemblePatchApplication() | 82 EnsemblePatchApplication::EnsemblePatchApplication() |
| 80 : source_checksum_(0), target_checksum_(0), | 83 : source_checksum_(0), target_checksum_(0), |
| 81 final_patch_input_size_prediction_(0) { | 84 final_patch_input_size_prediction_(0) { |
| 82 } | 85 } |
| 83 | 86 |
| 84 EnsemblePatchApplication::~EnsemblePatchApplication() { | |
| 85 for (size_t i = 0; i < patchers_.size(); ++i) { | |
| 86 delete patchers_[i]; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 Status EnsemblePatchApplication::ReadHeader(SourceStream* header_stream) { | 87 Status EnsemblePatchApplication::ReadHeader(SourceStream* header_stream) { |
| 91 uint32_t magic; | 88 uint32_t magic; |
| 92 if (!header_stream->ReadVarint32(&magic)) | 89 if (!header_stream->ReadVarint32(&magic)) |
| 93 return C_BAD_ENSEMBLE_MAGIC; | 90 return C_BAD_ENSEMBLE_MAGIC; |
| 94 | 91 |
| 95 if (magic != CourgettePatchFile::kMagic) | 92 if (magic != CourgettePatchFile::kMagic) |
| 96 return C_BAD_ENSEMBLE_MAGIC; | 93 return C_BAD_ENSEMBLE_MAGIC; |
| 97 | 94 |
| 98 uint32_t version; | 95 uint32_t version; |
| 99 if (!header_stream->ReadVarint32(&version)) | 96 if (!header_stream->ReadVarint32(&version)) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 SourceStream* transformation_parameters) { | 128 SourceStream* transformation_parameters) { |
| 132 uint32_t number_of_transformations = 0; | 129 uint32_t number_of_transformations = 0; |
| 133 if (!transformation_parameters->ReadVarint32(&number_of_transformations)) | 130 if (!transformation_parameters->ReadVarint32(&number_of_transformations)) |
| 134 return C_BAD_ENSEMBLE_HEADER; | 131 return C_BAD_ENSEMBLE_HEADER; |
| 135 | 132 |
| 136 for (size_t i = 0; i < number_of_transformations; ++i) { | 133 for (size_t i = 0; i < number_of_transformations; ++i) { |
| 137 uint32_t kind; | 134 uint32_t kind; |
| 138 if (!transformation_parameters->ReadVarint32(&kind)) | 135 if (!transformation_parameters->ReadVarint32(&kind)) |
| 139 return C_BAD_ENSEMBLE_HEADER; | 136 return C_BAD_ENSEMBLE_HEADER; |
| 140 | 137 |
| 141 TransformationPatcher* patcher = NULL; | 138 std::unique_ptr<TransformationPatcher> patcher; |
| 142 | 139 |
| 143 switch (kind) | 140 switch (kind) { |
| 144 { | 141 case EXE_WIN_32_X86: // Fall through. |
| 145 case EXE_WIN_32_X86: | 142 case EXE_ELF_32_X86: |
| 146 patcher = new PatcherX86_32(base_region_); | 143 case EXE_ELF_32_ARM: |
| 144 case EXE_WIN_32_X64: |
| 145 patcher.reset(new PatcherX86_32(base_region_)); |
| 147 break; | 146 break; |
| 148 case EXE_ELF_32_X86: | 147 default: |
| 149 patcher = new PatcherX86_32(base_region_); | 148 return C_BAD_ENSEMBLE_HEADER; |
| 150 break; | |
| 151 case EXE_ELF_32_ARM: | |
| 152 patcher = new PatcherX86_32(base_region_); | |
| 153 break; | |
| 154 case EXE_WIN_32_X64: | |
| 155 patcher = new PatcherX86_32(base_region_); | |
| 156 break; | |
| 157 } | 149 } |
| 158 | 150 |
| 159 if (patcher) | 151 DCHECK(patcher); |
| 160 patchers_.push_back(patcher); | 152 patchers_.push_back(std::move(patcher)); |
| 161 else | |
| 162 return C_BAD_ENSEMBLE_HEADER; | |
| 163 } | 153 } |
| 164 | 154 |
| 165 for (size_t i = 0; i < patchers_.size(); ++i) { | 155 for (size_t i = 0; i < patchers_.size(); ++i) { |
| 166 Status status = patchers_[i]->Init(transformation_parameters); | 156 Status status = patchers_[i]->Init(transformation_parameters); |
| 167 if (status != C_OK) | 157 if (status != C_OK) |
| 168 return status; | 158 return status; |
| 169 } | 159 } |
| 170 | 160 |
| 171 // All transformation_parameters should have been consumed by the above loop. | 161 // All transformation_parameters should have been consumed by the above loop. |
| 172 if (!transformation_parameters->Empty()) | 162 if (!transformation_parameters->Empty()) |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 static_cast<int>(new_sink_stream.Length())); | 413 static_cast<int>(new_sink_stream.Length())); |
| 424 if (written == -1) | 414 if (written == -1) |
| 425 return C_WRITE_OPEN_ERROR; | 415 return C_WRITE_OPEN_ERROR; |
| 426 if (static_cast<size_t>(written) != new_sink_stream.Length()) | 416 if (static_cast<size_t>(written) != new_sink_stream.Length()) |
| 427 return C_WRITE_ERROR; | 417 return C_WRITE_ERROR; |
| 428 | 418 |
| 429 return C_OK; | 419 return C_OK; |
| 430 } | 420 } |
| 431 | 421 |
| 432 } // namespace | 422 } // namespace |
| OLD | NEW |