OLD | NEW |
1 /*- | 1 /*- |
2 * Copyright 2003,2004 Colin Percival | 2 * Copyright 2003,2004 Colin Percival |
3 * All rights reserved | 3 * All rights reserved |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted providing that the following conditions | 6 * modification, are permitted providing that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
13 * | 13 * |
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | 22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
24 * POSSIBILITY OF SUCH DAMAGE. | 24 * POSSIBILITY OF SUCH DAMAGE. |
25 * | 25 * |
26 * Changelog: | 26 * Changelog: |
27 * 2009-03-31 - Change to use Streams. Move CRC code to crc.{h,cc} | 27 * 2009-03-31 - Change to use Streams. Move CRC code to crc.{h,cc} |
28 * --Stephen Adams <sra@chromium.org> | 28 * --Stephen Adams <sra@chromium.org> |
| 29 * 2013-04-10 - Add wrapper method to apply a patch to files directly. |
| 30 * --Joshua Pawlicki <waffles@chromium.org> |
29 */ | 31 */ |
30 | 32 |
31 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 33 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
32 // Use of this source code is governed by a BSD-style license that can be | 34 // Use of this source code is governed by a BSD-style license that can be |
33 // found in the LICENSE file. | 35 // found in the LICENSE file. |
34 | 36 |
35 #include "courgette/third_party/bsdiff.h" | 37 #include "courgette/third_party/bsdiff.h" |
36 | 38 |
| 39 #include "base/files/memory_mapped_file.h" |
37 #include "courgette/crc.h" | 40 #include "courgette/crc.h" |
38 #include "courgette/streams.h" | 41 #include "courgette/streams.h" |
39 | 42 |
40 namespace courgette { | 43 namespace courgette { |
41 | 44 |
42 BSDiffStatus MBS_ReadHeader(SourceStream* stream, MBSPatchHeader* header) { | 45 BSDiffStatus MBS_ReadHeader(SourceStream* stream, MBSPatchHeader* header) { |
43 if (!stream->Read(header->tag, sizeof(header->tag))) return READ_ERROR; | 46 if (!stream->Read(header->tag, sizeof(header->tag))) return READ_ERROR; |
44 if (!stream->ReadVarint32(&header->slen)) return READ_ERROR; | 47 if (!stream->ReadVarint32(&header->slen)) return READ_ERROR; |
45 if (!stream->ReadVarint32(&header->scrc32)) return READ_ERROR; | 48 if (!stream->ReadVarint32(&header->scrc32)) return READ_ERROR; |
46 if (!stream->ReadVarint32(&header->dlen)) return READ_ERROR; | 49 if (!stream->ReadVarint32(&header->dlen)) return READ_ERROR; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 if (old_size != header.slen) return UNEXPECTED_ERROR; | 164 if (old_size != header.slen) return UNEXPECTED_ERROR; |
162 | 165 |
163 if (CalculateCrc(old_start, old_size) != header.scrc32) | 166 if (CalculateCrc(old_start, old_size) != header.scrc32) |
164 return CRC_ERROR; | 167 return CRC_ERROR; |
165 | 168 |
166 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream); | 169 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream); |
167 | 170 |
168 return OK; | 171 return OK; |
169 } | 172 } |
170 | 173 |
| 174 BSDiffStatus ApplyBinaryPatch(const base::FilePath& old_file_path, |
| 175 const base::FilePath& patch_file_path, |
| 176 const base::FilePath& new_file_path) { |
| 177 // Set up the old stream. |
| 178 base::MemoryMappedFile old_file; |
| 179 if (!old_file.Initialize(old_file_path)) { |
| 180 return READ_ERROR; |
| 181 } |
| 182 SourceStream old_file_stream; |
| 183 old_file_stream.Init(old_file.data(), old_file.length()); |
| 184 |
| 185 // Set up the patch stream. |
| 186 base::MemoryMappedFile patch_file; |
| 187 if (!patch_file.Initialize(patch_file_path)) { |
| 188 return READ_ERROR; |
| 189 } |
| 190 SourceStream patch_file_stream; |
| 191 patch_file_stream.Init(patch_file.data(), patch_file.length()); |
| 192 |
| 193 // Set up the new stream and apply the patch. |
| 194 SinkStream new_sink_stream; |
| 195 BSDiffStatus status = ApplyBinaryPatch(&old_file_stream, |
| 196 &patch_file_stream, |
| 197 &new_sink_stream); |
| 198 if (status != OK) { |
| 199 return status; |
| 200 } |
| 201 |
| 202 // Write the stream to disk. |
| 203 int written = file_util::WriteFile( |
| 204 new_file_path, |
| 205 reinterpret_cast<const char*>(new_sink_stream.Buffer()), |
| 206 static_cast<int>(new_sink_stream.Length())); |
| 207 if (written != static_cast<int>(new_sink_stream.Length())) |
| 208 return WRITE_ERROR; |
| 209 return OK; |
| 210 } |
| 211 |
171 } // namespace | 212 } // namespace |
OLD | NEW |