OLD | NEW |
| (Empty) |
1 /*- | |
2 * Copyright 2003,2004 Colin Percival | |
3 * All rights reserved | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted providing that the following conditions | |
7 * are met: | |
8 * 1. Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright | |
11 * notice, this list of conditions and the following disclaimer in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * | |
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
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 | |
24 * POSSIBILITY OF SUCH DAMAGE. | |
25 * | |
26 * Changelog: | |
27 * 2009-03-31 - Change to use Streams. Move CRC code to crc.{h,cc} | |
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> | |
31 */ | |
32 | |
33 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
34 // Use of this source code is governed by a BSD-style license that can be | |
35 // found in the LICENSE file. | |
36 | |
37 #include "courgette/third_party/bsdiff.h" | |
38 | |
39 #include <stddef.h> | |
40 #include <stdint.h> | |
41 | |
42 #include "base/files/memory_mapped_file.h" | |
43 #include "courgette/crc.h" | |
44 #include "courgette/streams.h" | |
45 | |
46 namespace courgette { | |
47 | |
48 BSDiffStatus MBS_ReadHeader(SourceStream* stream, MBSPatchHeader* header) { | |
49 if (!stream->Read(header->tag, sizeof(header->tag))) return READ_ERROR; | |
50 if (!stream->ReadVarint32(&header->slen)) return READ_ERROR; | |
51 if (!stream->ReadVarint32(&header->scrc32)) return READ_ERROR; | |
52 if (!stream->ReadVarint32(&header->dlen)) return READ_ERROR; | |
53 | |
54 // The string will have a NUL terminator that we don't use, hence '-1'. | |
55 static_assert(sizeof(MBS_PATCH_HEADER_TAG) - 1 == sizeof(header->tag), | |
56 "MBS_PATCH_HEADER_TAG must match header field size"); | |
57 if (memcmp(header->tag, MBS_PATCH_HEADER_TAG, 8) != 0) | |
58 return UNEXPECTED_ERROR; | |
59 | |
60 return OK; | |
61 } | |
62 | |
63 BSDiffStatus MBS_ApplyPatch(const MBSPatchHeader* header, | |
64 SourceStream* patch_stream, | |
65 const uint8_t* old_start, | |
66 size_t old_size, | |
67 SinkStream* new_stream) { | |
68 const uint8_t* old_end = old_start + old_size; | |
69 | |
70 SourceStreamSet patch_streams; | |
71 if (!patch_streams.Init(patch_stream)) | |
72 return READ_ERROR; | |
73 | |
74 SourceStream* control_stream_copy_counts = patch_streams.stream(0); | |
75 SourceStream* control_stream_extra_counts = patch_streams.stream(1); | |
76 SourceStream* control_stream_seeks = patch_streams.stream(2); | |
77 SourceStream* diff_skips = patch_streams.stream(3); | |
78 SourceStream* diff_bytes = patch_streams.stream(4); | |
79 SourceStream* extra_bytes = patch_streams.stream(5); | |
80 | |
81 const uint8_t* extra_start = extra_bytes->Buffer(); | |
82 const uint8_t* extra_end = extra_start + extra_bytes->Remaining(); | |
83 const uint8_t* extra_position = extra_start; | |
84 | |
85 const uint8_t* old_position = old_start; | |
86 | |
87 if (header->dlen && !new_stream->Reserve(header->dlen)) | |
88 return MEM_ERROR; | |
89 | |
90 uint32_t pending_diff_zeros = 0; | |
91 if (!diff_skips->ReadVarint32(&pending_diff_zeros)) | |
92 return UNEXPECTED_ERROR; | |
93 | |
94 while (!control_stream_copy_counts->Empty()) { | |
95 uint32_t copy_count, extra_count; | |
96 int32_t seek_adjustment; | |
97 if (!control_stream_copy_counts->ReadVarint32(©_count)) | |
98 return UNEXPECTED_ERROR; | |
99 if (!control_stream_extra_counts->ReadVarint32(&extra_count)) | |
100 return UNEXPECTED_ERROR; | |
101 if (!control_stream_seeks->ReadVarint32Signed(&seek_adjustment)) | |
102 return UNEXPECTED_ERROR; | |
103 | |
104 #ifdef DEBUG_bsmedberg | |
105 printf("Applying block: copy: %-8u extra: %-8u seek: %+i\n", | |
106 copy_count, extra_count, seek_adjustment); | |
107 #endif | |
108 // Byte-wise arithmetically add bytes from old file to bytes from the diff | |
109 // block. | |
110 if (copy_count > static_cast<size_t>(old_end - old_position)) | |
111 return UNEXPECTED_ERROR; | |
112 | |
113 // Add together bytes from the 'old' file and the 'diff' stream. | |
114 for (size_t i = 0; i < copy_count; ++i) { | |
115 uint8_t diff_byte = 0; | |
116 if (pending_diff_zeros) { | |
117 --pending_diff_zeros; | |
118 } else { | |
119 if (!diff_skips->ReadVarint32(&pending_diff_zeros)) | |
120 return UNEXPECTED_ERROR; | |
121 if (!diff_bytes->Read(&diff_byte, 1)) | |
122 return UNEXPECTED_ERROR; | |
123 } | |
124 uint8_t byte = old_position[i] + diff_byte; | |
125 if (!new_stream->Write(&byte, 1)) | |
126 return MEM_ERROR; | |
127 } | |
128 old_position += copy_count; | |
129 | |
130 // Copy bytes from the extra block. | |
131 if (extra_count > static_cast<size_t>(extra_end - extra_position)) | |
132 return UNEXPECTED_ERROR; | |
133 | |
134 if (!new_stream->Write(extra_position, extra_count)) | |
135 return MEM_ERROR; | |
136 | |
137 extra_position += extra_count; | |
138 | |
139 // "seek" forwards (or backwards) in oldfile. | |
140 if (old_position + seek_adjustment < old_start || | |
141 old_position + seek_adjustment > old_end) | |
142 return UNEXPECTED_ERROR; | |
143 | |
144 old_position += seek_adjustment; | |
145 } | |
146 | |
147 if (!control_stream_copy_counts->Empty() || | |
148 !control_stream_extra_counts->Empty() || | |
149 !control_stream_seeks->Empty() || | |
150 !diff_skips->Empty() || | |
151 !diff_bytes->Empty() || | |
152 !extra_bytes->Empty()) | |
153 return UNEXPECTED_ERROR; | |
154 | |
155 return OK; | |
156 } | |
157 | |
158 BSDiffStatus ApplyBinaryPatch(SourceStream* old_stream, | |
159 SourceStream* patch_stream, | |
160 SinkStream* new_stream) { | |
161 MBSPatchHeader header; | |
162 BSDiffStatus ret = MBS_ReadHeader(patch_stream, &header); | |
163 if (ret != OK) return ret; | |
164 | |
165 const uint8_t* old_start = old_stream->Buffer(); | |
166 size_t old_size = old_stream->Remaining(); | |
167 | |
168 if (old_size != header.slen) return UNEXPECTED_ERROR; | |
169 | |
170 if (CalculateCrc(old_start, old_size) != header.scrc32) | |
171 return CRC_ERROR; | |
172 | |
173 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream); | |
174 | |
175 return OK; | |
176 } | |
177 | |
178 BSDiffStatus ApplyBinaryPatch(const base::FilePath& old_file_path, | |
179 const base::FilePath& patch_file_path, | |
180 const base::FilePath& new_file_path) { | |
181 // Set up the old stream. | |
182 base::MemoryMappedFile old_file; | |
183 if (!old_file.Initialize(old_file_path)) { | |
184 return READ_ERROR; | |
185 } | |
186 SourceStream old_file_stream; | |
187 old_file_stream.Init(old_file.data(), old_file.length()); | |
188 | |
189 // Set up the patch stream. | |
190 base::MemoryMappedFile patch_file; | |
191 if (!patch_file.Initialize(patch_file_path)) { | |
192 return READ_ERROR; | |
193 } | |
194 SourceStream patch_file_stream; | |
195 patch_file_stream.Init(patch_file.data(), patch_file.length()); | |
196 | |
197 // Set up the new stream and apply the patch. | |
198 SinkStream new_sink_stream; | |
199 BSDiffStatus status = ApplyBinaryPatch(&old_file_stream, | |
200 &patch_file_stream, | |
201 &new_sink_stream); | |
202 if (status != OK) { | |
203 return status; | |
204 } | |
205 | |
206 // Write the stream to disk. | |
207 int written = base::WriteFile( | |
208 new_file_path, | |
209 reinterpret_cast<const char*>(new_sink_stream.Buffer()), | |
210 static_cast<int>(new_sink_stream.Length())); | |
211 if (written != static_cast<int>(new_sink_stream.Length())) | |
212 return WRITE_ERROR; | |
213 return OK; | |
214 } | |
215 | |
216 } // namespace | |
OLD | NEW |