Chromium Code Reviews| 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 #include <memory> | |
|
huangs
2016/05/11 04:49:23
NIT: new line to separate C-style and C++-style.
etiennep
2016/05/11 18:19:14
Done.
| |
| 11 | 12 |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/files/memory_mapped_file.h" | 14 #include "base/files/memory_mapped_file.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "courgette/crc.h" | 17 #include "courgette/crc.h" |
| 17 #include "courgette/patcher_x86_32.h" | 18 #include "courgette/patcher_x86_32.h" |
| 18 #include "courgette/region.h" | 19 #include "courgette/region.h" |
| 19 #include "courgette/simple_delta.h" | 20 #include "courgette/simple_delta.h" |
| 20 #include "courgette/streams.h" | 21 #include "courgette/streams.h" |
| 21 | 22 |
| 22 namespace courgette { | 23 namespace courgette { |
| 23 | 24 |
| 24 // EnsemblePatchApplication is all the logic and data required to apply the | 25 // EnsemblePatchApplication is all the logic and data required to apply the |
| 25 // multi-stage patch. | 26 // multi-stage patch. |
| 26 class EnsemblePatchApplication { | 27 class EnsemblePatchApplication { |
| 27 public: | 28 public: |
| 28 EnsemblePatchApplication(); | 29 EnsemblePatchApplication(); |
| 29 ~EnsemblePatchApplication(); | 30 ~EnsemblePatchApplication() = default; |
| 30 | 31 |
| 31 Status ReadHeader(SourceStream* header_stream); | 32 Status ReadHeader(SourceStream* header_stream); |
| 32 | 33 |
| 33 Status InitBase(const Region& region); | 34 Status InitBase(const Region& region); |
| 34 | 35 |
| 35 Status ValidateBase(); | 36 Status ValidateBase(); |
| 36 | 37 |
| 37 Status ReadInitialParameters(SourceStream* initial_parameters); | 38 Status ReadInitialParameters(SourceStream* initial_parameters); |
| 38 | 39 |
| 39 Status PredictTransformParameters(SinkStreamSet* predicted_parameters); | 40 Status PredictTransformParameters(SinkStreamSet* predicted_parameters); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 61 SourceStream* correction, | 62 SourceStream* correction, |
| 62 SourceStreamSet* corrected_items, | 63 SourceStreamSet* corrected_items, |
| 63 SinkStream* corrected_items_storage); | 64 SinkStream* corrected_items_storage); |
| 64 | 65 |
| 65 Region base_region_; // Location of in-memory copy of 'old' version. | 66 Region base_region_; // Location of in-memory copy of 'old' version. |
| 66 | 67 |
| 67 uint32_t source_checksum_; | 68 uint32_t source_checksum_; |
| 68 uint32_t target_checksum_; | 69 uint32_t target_checksum_; |
| 69 uint32_t final_patch_input_size_prediction_; | 70 uint32_t final_patch_input_size_prediction_; |
| 70 | 71 |
| 71 std::vector<TransformationPatcher*> patchers_; | 72 std::vector<std::unique_ptr<TransformationPatcher>> patchers_; |
| 72 | 73 |
| 73 SinkStream corrected_parameters_storage_; | 74 SinkStream corrected_parameters_storage_; |
| 74 SinkStream corrected_elements_storage_; | 75 SinkStream corrected_elements_storage_; |
| 75 | 76 |
| 76 DISALLOW_COPY_AND_ASSIGN(EnsemblePatchApplication); | 77 DISALLOW_COPY_AND_ASSIGN(EnsemblePatchApplication); |
| 77 }; | 78 }; |
| 78 | 79 |
| 79 EnsemblePatchApplication::EnsemblePatchApplication() | 80 EnsemblePatchApplication::EnsemblePatchApplication() |
| 80 : source_checksum_(0), target_checksum_(0), | 81 : source_checksum_(0), target_checksum_(0), |
| 81 final_patch_input_size_prediction_(0) { | 82 final_patch_input_size_prediction_(0) { |
| 82 } | 83 } |
| 83 | 84 |
| 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) { | 85 Status EnsemblePatchApplication::ReadHeader(SourceStream* header_stream) { |
| 91 uint32_t magic; | 86 uint32_t magic; |
| 92 if (!header_stream->ReadVarint32(&magic)) | 87 if (!header_stream->ReadVarint32(&magic)) |
| 93 return C_BAD_ENSEMBLE_MAGIC; | 88 return C_BAD_ENSEMBLE_MAGIC; |
| 94 | 89 |
| 95 if (magic != CourgettePatchFile::kMagic) | 90 if (magic != CourgettePatchFile::kMagic) |
| 96 return C_BAD_ENSEMBLE_MAGIC; | 91 return C_BAD_ENSEMBLE_MAGIC; |
| 97 | 92 |
| 98 uint32_t version; | 93 uint32_t version; |
| 99 if (!header_stream->ReadVarint32(&version)) | 94 if (!header_stream->ReadVarint32(&version)) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 SourceStream* transformation_parameters) { | 126 SourceStream* transformation_parameters) { |
| 132 uint32_t number_of_transformations = 0; | 127 uint32_t number_of_transformations = 0; |
| 133 if (!transformation_parameters->ReadVarint32(&number_of_transformations)) | 128 if (!transformation_parameters->ReadVarint32(&number_of_transformations)) |
| 134 return C_BAD_ENSEMBLE_HEADER; | 129 return C_BAD_ENSEMBLE_HEADER; |
| 135 | 130 |
| 136 for (size_t i = 0; i < number_of_transformations; ++i) { | 131 for (size_t i = 0; i < number_of_transformations; ++i) { |
| 137 uint32_t kind; | 132 uint32_t kind; |
| 138 if (!transformation_parameters->ReadVarint32(&kind)) | 133 if (!transformation_parameters->ReadVarint32(&kind)) |
| 139 return C_BAD_ENSEMBLE_HEADER; | 134 return C_BAD_ENSEMBLE_HEADER; |
| 140 | 135 |
| 141 TransformationPatcher* patcher = NULL; | 136 TransformationPatcher* patcher = NULL; |
|
huangs
2016/05/11 04:49:24
std::unique_ptr<TransformationPatcher> patcher;
etiennep
2016/05/11 18:19:13
Done.
| |
| 142 | 137 |
| 143 switch (kind) | 138 switch (kind) |
|
huangs
2016/05/11 04:49:24
Unwrap.
etiennep
2016/05/11 18:19:14
Done.
| |
| 144 { | 139 { |
| 145 case EXE_WIN_32_X86: | 140 case EXE_WIN_32_X86: |
| 146 patcher = new PatcherX86_32(base_region_); | 141 patcher = new PatcherX86_32(base_region_); |
|
huangs
2016/05/11 04:49:23
patcher.reset(...);
...
etiennep
2016/05/11 18:19:13
Done.
| |
| 147 break; | 142 break; |
| 148 case EXE_ELF_32_X86: | 143 case EXE_ELF_32_X86: |
| 149 patcher = new PatcherX86_32(base_region_); | 144 patcher = new PatcherX86_32(base_region_); |
| 150 break; | 145 break; |
| 151 case EXE_ELF_32_ARM: | 146 case EXE_ELF_32_ARM: |
| 152 patcher = new PatcherX86_32(base_region_); | 147 patcher = new PatcherX86_32(base_region_); |
| 153 break; | 148 break; |
| 154 case EXE_WIN_32_X64: | 149 case EXE_WIN_32_X64: |
| 155 patcher = new PatcherX86_32(base_region_); | 150 patcher = new PatcherX86_32(base_region_); |
| 156 break; | 151 break; |
| 157 } | 152 } |
|
huangs
2016/05/11 04:49:23
default:
return C_BAD_ENSEMBLE_HEADER;
etiennep
2016/05/11 18:19:13
Done.
| |
| 158 | 153 |
| 159 if (patcher) | 154 if (patcher) |
|
huangs
2016/05/11 04:49:23
Can do:
DCHECK(patcher);
patchers_.push_back(
etiennep
2016/05/11 18:19:14
Done.
| |
| 160 patchers_.push_back(patcher); | 155 patchers_.emplace_back(patcher); |
| 161 else | 156 else |
| 162 return C_BAD_ENSEMBLE_HEADER; | 157 return C_BAD_ENSEMBLE_HEADER; |
| 163 } | 158 } |
| 164 | 159 |
| 165 for (size_t i = 0; i < patchers_.size(); ++i) { | 160 for (size_t i = 0; i < patchers_.size(); ++i) { |
| 166 Status status = patchers_[i]->Init(transformation_parameters); | 161 Status status = patchers_[i]->Init(transformation_parameters); |
| 167 if (status != C_OK) | 162 if (status != C_OK) |
| 168 return status; | 163 return status; |
| 169 } | 164 } |
| 170 | 165 |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 423 static_cast<int>(new_sink_stream.Length())); | 418 static_cast<int>(new_sink_stream.Length())); |
| 424 if (written == -1) | 419 if (written == -1) |
| 425 return C_WRITE_OPEN_ERROR; | 420 return C_WRITE_OPEN_ERROR; |
| 426 if (static_cast<size_t>(written) != new_sink_stream.Length()) | 421 if (static_cast<size_t>(written) != new_sink_stream.Length()) |
| 427 return C_WRITE_ERROR; | 422 return C_WRITE_ERROR; |
| 428 | 423 |
| 429 return C_OK; | 424 return C_OK; |
| 430 } | 425 } |
| 431 | 426 |
| 432 } // namespace | 427 } // namespace |
| OLD | NEW |