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 |
(...skipping 18 matching lines...) Expand all Loading... |
29 * 2013-04-10 - Add wrapper method to apply a patch to files directly. | 29 * 2013-04-10 - Add wrapper method to apply a patch to files directly. |
30 * --Joshua Pawlicki <waffles@chromium.org> | 30 * --Joshua Pawlicki <waffles@chromium.org> |
31 */ | 31 */ |
32 | 32 |
33 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 34 // Use of this source code is governed by a BSD-style license that can be |
35 // found in the LICENSE file. | 35 // found in the LICENSE file. |
36 | 36 |
37 #include "courgette/third_party/bsdiff.h" | 37 #include "courgette/third_party/bsdiff.h" |
38 | 38 |
| 39 #include <stddef.h> |
| 40 #include <stdint.h> |
| 41 |
39 #include "base/files/memory_mapped_file.h" | 42 #include "base/files/memory_mapped_file.h" |
40 #include "courgette/crc.h" | 43 #include "courgette/crc.h" |
41 #include "courgette/streams.h" | 44 #include "courgette/streams.h" |
42 | 45 |
43 namespace courgette { | 46 namespace courgette { |
44 | 47 |
45 BSDiffStatus MBS_ReadHeader(SourceStream* stream, MBSPatchHeader* header) { | 48 BSDiffStatus MBS_ReadHeader(SourceStream* stream, MBSPatchHeader* header) { |
46 if (!stream->Read(header->tag, sizeof(header->tag))) return READ_ERROR; | 49 if (!stream->Read(header->tag, sizeof(header->tag))) return READ_ERROR; |
47 if (!stream->ReadVarint32(&header->slen)) return READ_ERROR; | 50 if (!stream->ReadVarint32(&header->slen)) return READ_ERROR; |
48 if (!stream->ReadVarint32(&header->scrc32)) return READ_ERROR; | 51 if (!stream->ReadVarint32(&header->scrc32)) return READ_ERROR; |
49 if (!stream->ReadVarint32(&header->dlen)) return READ_ERROR; | 52 if (!stream->ReadVarint32(&header->dlen)) return READ_ERROR; |
50 | 53 |
51 // The string will have a NUL terminator that we don't use, hence '-1'. | 54 // The string will have a NUL terminator that we don't use, hence '-1'. |
52 static_assert(sizeof(MBS_PATCH_HEADER_TAG) - 1 == sizeof(header->tag), | 55 static_assert(sizeof(MBS_PATCH_HEADER_TAG) - 1 == sizeof(header->tag), |
53 "MBS_PATCH_HEADER_TAG must match header field size"); | 56 "MBS_PATCH_HEADER_TAG must match header field size"); |
54 if (memcmp(header->tag, MBS_PATCH_HEADER_TAG, 8) != 0) | 57 if (memcmp(header->tag, MBS_PATCH_HEADER_TAG, 8) != 0) |
55 return UNEXPECTED_ERROR; | 58 return UNEXPECTED_ERROR; |
56 | 59 |
57 return OK; | 60 return OK; |
58 } | 61 } |
59 | 62 |
60 BSDiffStatus MBS_ApplyPatch(const MBSPatchHeader *header, | 63 BSDiffStatus MBS_ApplyPatch(const MBSPatchHeader* header, |
61 SourceStream* patch_stream, | 64 SourceStream* patch_stream, |
62 const uint8* old_start, size_t old_size, | 65 const uint8_t* old_start, |
| 66 size_t old_size, |
63 SinkStream* new_stream) { | 67 SinkStream* new_stream) { |
64 const uint8* old_end = old_start + old_size; | 68 const uint8_t* old_end = old_start + old_size; |
65 | 69 |
66 SourceStreamSet patch_streams; | 70 SourceStreamSet patch_streams; |
67 if (!patch_streams.Init(patch_stream)) | 71 if (!patch_streams.Init(patch_stream)) |
68 return READ_ERROR; | 72 return READ_ERROR; |
69 | 73 |
70 SourceStream* control_stream_copy_counts = patch_streams.stream(0); | 74 SourceStream* control_stream_copy_counts = patch_streams.stream(0); |
71 SourceStream* control_stream_extra_counts = patch_streams.stream(1); | 75 SourceStream* control_stream_extra_counts = patch_streams.stream(1); |
72 SourceStream* control_stream_seeks = patch_streams.stream(2); | 76 SourceStream* control_stream_seeks = patch_streams.stream(2); |
73 SourceStream* diff_skips = patch_streams.stream(3); | 77 SourceStream* diff_skips = patch_streams.stream(3); |
74 SourceStream* diff_bytes = patch_streams.stream(4); | 78 SourceStream* diff_bytes = patch_streams.stream(4); |
75 SourceStream* extra_bytes = patch_streams.stream(5); | 79 SourceStream* extra_bytes = patch_streams.stream(5); |
76 | 80 |
77 const uint8* extra_start = extra_bytes->Buffer(); | 81 const uint8_t* extra_start = extra_bytes->Buffer(); |
78 const uint8* extra_end = extra_start + extra_bytes->Remaining(); | 82 const uint8_t* extra_end = extra_start + extra_bytes->Remaining(); |
79 const uint8* extra_position = extra_start; | 83 const uint8_t* extra_position = extra_start; |
80 | 84 |
81 const uint8* old_position = old_start; | 85 const uint8_t* old_position = old_start; |
82 | 86 |
83 if (header->dlen && !new_stream->Reserve(header->dlen)) | 87 if (header->dlen && !new_stream->Reserve(header->dlen)) |
84 return MEM_ERROR; | 88 return MEM_ERROR; |
85 | 89 |
86 uint32 pending_diff_zeros = 0; | 90 uint32_t pending_diff_zeros = 0; |
87 if (!diff_skips->ReadVarint32(&pending_diff_zeros)) | 91 if (!diff_skips->ReadVarint32(&pending_diff_zeros)) |
88 return UNEXPECTED_ERROR; | 92 return UNEXPECTED_ERROR; |
89 | 93 |
90 while (!control_stream_copy_counts->Empty()) { | 94 while (!control_stream_copy_counts->Empty()) { |
91 uint32 copy_count, extra_count; | 95 uint32_t copy_count, extra_count; |
92 int32 seek_adjustment; | 96 int32_t seek_adjustment; |
93 if (!control_stream_copy_counts->ReadVarint32(©_count)) | 97 if (!control_stream_copy_counts->ReadVarint32(©_count)) |
94 return UNEXPECTED_ERROR; | 98 return UNEXPECTED_ERROR; |
95 if (!control_stream_extra_counts->ReadVarint32(&extra_count)) | 99 if (!control_stream_extra_counts->ReadVarint32(&extra_count)) |
96 return UNEXPECTED_ERROR; | 100 return UNEXPECTED_ERROR; |
97 if (!control_stream_seeks->ReadVarint32Signed(&seek_adjustment)) | 101 if (!control_stream_seeks->ReadVarint32Signed(&seek_adjustment)) |
98 return UNEXPECTED_ERROR; | 102 return UNEXPECTED_ERROR; |
99 | 103 |
100 #ifdef DEBUG_bsmedberg | 104 #ifdef DEBUG_bsmedberg |
101 printf("Applying block: copy: %-8u extra: %-8u seek: %+i\n", | 105 printf("Applying block: copy: %-8u extra: %-8u seek: %+i\n", |
102 copy_count, extra_count, seek_adjustment); | 106 copy_count, extra_count, seek_adjustment); |
103 #endif | 107 #endif |
104 // Byte-wise arithmetically add bytes from old file to bytes from the diff | 108 // Byte-wise arithmetically add bytes from old file to bytes from the diff |
105 // block. | 109 // block. |
106 if (copy_count > static_cast<size_t>(old_end - old_position)) | 110 if (copy_count > static_cast<size_t>(old_end - old_position)) |
107 return UNEXPECTED_ERROR; | 111 return UNEXPECTED_ERROR; |
108 | 112 |
109 // Add together bytes from the 'old' file and the 'diff' stream. | 113 // Add together bytes from the 'old' file and the 'diff' stream. |
110 for (size_t i = 0; i < copy_count; ++i) { | 114 for (size_t i = 0; i < copy_count; ++i) { |
111 uint8 diff_byte = 0; | 115 uint8_t diff_byte = 0; |
112 if (pending_diff_zeros) { | 116 if (pending_diff_zeros) { |
113 --pending_diff_zeros; | 117 --pending_diff_zeros; |
114 } else { | 118 } else { |
115 if (!diff_skips->ReadVarint32(&pending_diff_zeros)) | 119 if (!diff_skips->ReadVarint32(&pending_diff_zeros)) |
116 return UNEXPECTED_ERROR; | 120 return UNEXPECTED_ERROR; |
117 if (!diff_bytes->Read(&diff_byte, 1)) | 121 if (!diff_bytes->Read(&diff_byte, 1)) |
118 return UNEXPECTED_ERROR; | 122 return UNEXPECTED_ERROR; |
119 } | 123 } |
120 uint8 byte = old_position[i] + diff_byte; | 124 uint8_t byte = old_position[i] + diff_byte; |
121 if (!new_stream->Write(&byte, 1)) | 125 if (!new_stream->Write(&byte, 1)) |
122 return MEM_ERROR; | 126 return MEM_ERROR; |
123 } | 127 } |
124 old_position += copy_count; | 128 old_position += copy_count; |
125 | 129 |
126 // Copy bytes from the extra block. | 130 // Copy bytes from the extra block. |
127 if (extra_count > static_cast<size_t>(extra_end - extra_position)) | 131 if (extra_count > static_cast<size_t>(extra_end - extra_position)) |
128 return UNEXPECTED_ERROR; | 132 return UNEXPECTED_ERROR; |
129 | 133 |
130 if (!new_stream->Write(extra_position, extra_count)) | 134 if (!new_stream->Write(extra_position, extra_count)) |
(...skipping 20 matching lines...) Expand all Loading... |
151 return OK; | 155 return OK; |
152 } | 156 } |
153 | 157 |
154 BSDiffStatus ApplyBinaryPatch(SourceStream* old_stream, | 158 BSDiffStatus ApplyBinaryPatch(SourceStream* old_stream, |
155 SourceStream* patch_stream, | 159 SourceStream* patch_stream, |
156 SinkStream* new_stream) { | 160 SinkStream* new_stream) { |
157 MBSPatchHeader header; | 161 MBSPatchHeader header; |
158 BSDiffStatus ret = MBS_ReadHeader(patch_stream, &header); | 162 BSDiffStatus ret = MBS_ReadHeader(patch_stream, &header); |
159 if (ret != OK) return ret; | 163 if (ret != OK) return ret; |
160 | 164 |
161 const uint8* old_start = old_stream->Buffer(); | 165 const uint8_t* old_start = old_stream->Buffer(); |
162 size_t old_size = old_stream->Remaining(); | 166 size_t old_size = old_stream->Remaining(); |
163 | 167 |
164 if (old_size != header.slen) return UNEXPECTED_ERROR; | 168 if (old_size != header.slen) return UNEXPECTED_ERROR; |
165 | 169 |
166 if (CalculateCrc(old_start, old_size) != header.scrc32) | 170 if (CalculateCrc(old_start, old_size) != header.scrc32) |
167 return CRC_ERROR; | 171 return CRC_ERROR; |
168 | 172 |
169 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream); | 173 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream); |
170 | 174 |
171 return OK; | 175 return OK; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 int written = base::WriteFile( | 207 int written = base::WriteFile( |
204 new_file_path, | 208 new_file_path, |
205 reinterpret_cast<const char*>(new_sink_stream.Buffer()), | 209 reinterpret_cast<const char*>(new_sink_stream.Buffer()), |
206 static_cast<int>(new_sink_stream.Length())); | 210 static_cast<int>(new_sink_stream.Length())); |
207 if (written != static_cast<int>(new_sink_stream.Length())) | 211 if (written != static_cast<int>(new_sink_stream.Length())) |
208 return WRITE_ERROR; | 212 return WRITE_ERROR; |
209 return OK; | 213 return OK; |
210 } | 214 } |
211 | 215 |
212 } // namespace | 216 } // namespace |
OLD | NEW |