| 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 16 matching lines...) Expand all Loading... |
| 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. | 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/bsdiff.h" |
| 38 | 38 |
| 39 #include <stddef.h> | 39 #include <stddef.h> |
| 40 #include <stdint.h> | 40 #include <stdint.h> |
| 41 | 41 |
| 42 #include "base/files/memory_mapped_file.h" | 42 #include "base/files/memory_mapped_file.h" |
| 43 #include "courgette/crc.h" | 43 #include "courgette/crc.h" |
| 44 #include "courgette/streams.h" | 44 #include "courgette/streams.h" |
| 45 | 45 |
| 46 namespace courgette { | 46 namespace courgette { |
| 47 | 47 |
| 48 BSDiffStatus MBS_ReadHeader(SourceStream* stream, MBSPatchHeader* header) { | 48 BSDiffStatus MBS_ReadHeader(SourceStream* stream, MBSPatchHeader* header) { |
| 49 if (!stream->Read(header->tag, sizeof(header->tag))) return READ_ERROR; | 49 if (!stream->Read(header->tag, sizeof(header->tag))) |
| 50 if (!stream->ReadVarint32(&header->slen)) return READ_ERROR; | 50 return READ_ERROR; |
| 51 if (!stream->ReadVarint32(&header->scrc32)) return READ_ERROR; | 51 if (!stream->ReadVarint32(&header->slen)) |
| 52 if (!stream->ReadVarint32(&header->dlen)) return READ_ERROR; | 52 return READ_ERROR; |
| 53 if (!stream->ReadVarint32(&header->scrc32)) |
| 54 return READ_ERROR; |
| 55 if (!stream->ReadVarint32(&header->dlen)) |
| 56 return READ_ERROR; |
| 53 | 57 |
| 54 // The string will have a NUL terminator that we don't use, hence '-1'. | 58 // 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), | 59 static_assert(sizeof(MBS_PATCH_HEADER_TAG) - 1 == sizeof(header->tag), |
| 56 "MBS_PATCH_HEADER_TAG must match header field size"); | 60 "MBS_PATCH_HEADER_TAG must match header field size"); |
| 57 if (memcmp(header->tag, MBS_PATCH_HEADER_TAG, 8) != 0) | 61 if (memcmp(header->tag, MBS_PATCH_HEADER_TAG, 8) != 0) |
| 58 return UNEXPECTED_ERROR; | 62 return UNEXPECTED_ERROR; |
| 59 | 63 |
| 60 return OK; | 64 return OK; |
| 61 } | 65 } |
| 62 | 66 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 uint32_t copy_count, extra_count; | 99 uint32_t copy_count, extra_count; |
| 96 int32_t seek_adjustment; | 100 int32_t seek_adjustment; |
| 97 if (!control_stream_copy_counts->ReadVarint32(©_count)) | 101 if (!control_stream_copy_counts->ReadVarint32(©_count)) |
| 98 return UNEXPECTED_ERROR; | 102 return UNEXPECTED_ERROR; |
| 99 if (!control_stream_extra_counts->ReadVarint32(&extra_count)) | 103 if (!control_stream_extra_counts->ReadVarint32(&extra_count)) |
| 100 return UNEXPECTED_ERROR; | 104 return UNEXPECTED_ERROR; |
| 101 if (!control_stream_seeks->ReadVarint32Signed(&seek_adjustment)) | 105 if (!control_stream_seeks->ReadVarint32Signed(&seek_adjustment)) |
| 102 return UNEXPECTED_ERROR; | 106 return UNEXPECTED_ERROR; |
| 103 | 107 |
| 104 #ifdef DEBUG_bsmedberg | 108 #ifdef DEBUG_bsmedberg |
| 105 printf("Applying block: copy: %-8u extra: %-8u seek: %+i\n", | 109 printf("Applying block: copy: %-8u extra: %-8u seek: %+i\n", copy_count, |
| 106 copy_count, extra_count, seek_adjustment); | 110 extra_count, seek_adjustment); |
| 107 #endif | 111 #endif |
| 108 // Byte-wise arithmetically add bytes from old file to bytes from the diff | 112 // Byte-wise arithmetically add bytes from old file to bytes from the diff |
| 109 // block. | 113 // block. |
| 110 if (copy_count > static_cast<size_t>(old_end - old_position)) | 114 if (copy_count > static_cast<size_t>(old_end - old_position)) |
| 111 return UNEXPECTED_ERROR; | 115 return UNEXPECTED_ERROR; |
| 112 | 116 |
| 113 // Add together bytes from the 'old' file and the 'diff' stream. | 117 // Add together bytes from the 'old' file and the 'diff' stream. |
| 114 for (size_t i = 0; i < copy_count; ++i) { | 118 for (size_t i = 0; i < copy_count; ++i) { |
| 115 uint8_t diff_byte = 0; | 119 uint8_t diff_byte = 0; |
| 116 if (pending_diff_zeros) { | 120 if (pending_diff_zeros) { |
| 117 --pending_diff_zeros; | 121 --pending_diff_zeros; |
| 118 } else { | 122 } else { |
| 119 if (!diff_skips->ReadVarint32(&pending_diff_zeros)) | 123 if (!diff_skips->ReadVarint32(&pending_diff_zeros)) |
| 120 return UNEXPECTED_ERROR; | 124 return UNEXPECTED_ERROR; |
| 121 if (!diff_bytes->Read(&diff_byte, 1)) | 125 if (!diff_bytes->Read(&diff_byte, 1)) |
| 122 return UNEXPECTED_ERROR; | 126 return UNEXPECTED_ERROR; |
| 123 } | 127 } |
| 124 uint8_t byte = old_position[i] + diff_byte; | 128 uint8_t byte = old_position[i] + diff_byte; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 138 | 142 |
| 139 // "seek" forwards (or backwards) in oldfile. | 143 // "seek" forwards (or backwards) in oldfile. |
| 140 if (old_position + seek_adjustment < old_start || | 144 if (old_position + seek_adjustment < old_start || |
| 141 old_position + seek_adjustment > old_end) | 145 old_position + seek_adjustment > old_end) |
| 142 return UNEXPECTED_ERROR; | 146 return UNEXPECTED_ERROR; |
| 143 | 147 |
| 144 old_position += seek_adjustment; | 148 old_position += seek_adjustment; |
| 145 } | 149 } |
| 146 | 150 |
| 147 if (!control_stream_copy_counts->Empty() || | 151 if (!control_stream_copy_counts->Empty() || |
| 148 !control_stream_extra_counts->Empty() || | 152 !control_stream_extra_counts->Empty() || !control_stream_seeks->Empty() || |
| 149 !control_stream_seeks->Empty() || | 153 !diff_skips->Empty() || !diff_bytes->Empty() || !extra_bytes->Empty()) |
| 150 !diff_skips->Empty() || | |
| 151 !diff_bytes->Empty() || | |
| 152 !extra_bytes->Empty()) | |
| 153 return UNEXPECTED_ERROR; | 154 return UNEXPECTED_ERROR; |
| 154 | 155 |
| 155 return OK; | 156 return OK; |
| 156 } | 157 } |
| 157 | 158 |
| 158 BSDiffStatus ApplyBinaryPatch(SourceStream* old_stream, | 159 BSDiffStatus ApplyBinaryPatch(SourceStream* old_stream, |
| 159 SourceStream* patch_stream, | 160 SourceStream* patch_stream, |
| 160 SinkStream* new_stream) { | 161 SinkStream* new_stream) { |
| 161 MBSPatchHeader header; | 162 MBSPatchHeader header; |
| 162 BSDiffStatus ret = MBS_ReadHeader(patch_stream, &header); | 163 BSDiffStatus ret = MBS_ReadHeader(patch_stream, &header); |
| 163 if (ret != OK) return ret; | 164 if (ret != OK) |
| 165 return ret; |
| 164 | 166 |
| 165 const uint8_t* old_start = old_stream->Buffer(); | 167 const uint8_t* old_start = old_stream->Buffer(); |
| 166 size_t old_size = old_stream->Remaining(); | 168 size_t old_size = old_stream->Remaining(); |
| 167 | 169 |
| 168 if (old_size != header.slen) return UNEXPECTED_ERROR; | 170 if (old_size != header.slen) |
| 171 return UNEXPECTED_ERROR; |
| 169 | 172 |
| 170 if (CalculateCrc(old_start, old_size) != header.scrc32) | 173 if (CalculateCrc(old_start, old_size) != header.scrc32) |
| 171 return CRC_ERROR; | 174 return CRC_ERROR; |
| 172 | 175 |
| 173 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream); | 176 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream); |
| 174 | 177 |
| 175 return OK; | 178 return OK; |
| 176 } | 179 } |
| 177 | 180 |
| 178 BSDiffStatus ApplyBinaryPatch(const base::FilePath& old_file_path, | 181 BSDiffStatus ApplyBinaryPatch(const base::FilePath& old_file_path, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 189 // Set up the patch stream. | 192 // Set up the patch stream. |
| 190 base::MemoryMappedFile patch_file; | 193 base::MemoryMappedFile patch_file; |
| 191 if (!patch_file.Initialize(patch_file_path)) { | 194 if (!patch_file.Initialize(patch_file_path)) { |
| 192 return READ_ERROR; | 195 return READ_ERROR; |
| 193 } | 196 } |
| 194 SourceStream patch_file_stream; | 197 SourceStream patch_file_stream; |
| 195 patch_file_stream.Init(patch_file.data(), patch_file.length()); | 198 patch_file_stream.Init(patch_file.data(), patch_file.length()); |
| 196 | 199 |
| 197 // Set up the new stream and apply the patch. | 200 // Set up the new stream and apply the patch. |
| 198 SinkStream new_sink_stream; | 201 SinkStream new_sink_stream; |
| 199 BSDiffStatus status = ApplyBinaryPatch(&old_file_stream, | 202 BSDiffStatus status = |
| 200 &patch_file_stream, | 203 ApplyBinaryPatch(&old_file_stream, &patch_file_stream, &new_sink_stream); |
| 201 &new_sink_stream); | |
| 202 if (status != OK) { | 204 if (status != OK) { |
| 203 return status; | 205 return status; |
| 204 } | 206 } |
| 205 | 207 |
| 206 // Write the stream to disk. | 208 // Write the stream to disk. |
| 207 int written = base::WriteFile( | 209 int written = base::WriteFile( |
| 208 new_file_path, | 210 new_file_path, reinterpret_cast<const char*>(new_sink_stream.Buffer()), |
| 209 reinterpret_cast<const char*>(new_sink_stream.Buffer()), | |
| 210 static_cast<int>(new_sink_stream.Length())); | 211 static_cast<int>(new_sink_stream.Length())); |
| 211 if (written != static_cast<int>(new_sink_stream.Length())) | 212 if (written != static_cast<int>(new_sink_stream.Length())) |
| 212 return WRITE_ERROR; | 213 return WRITE_ERROR; |
| 213 return OK; | 214 return OK; |
| 214 } | 215 } |
| 215 | 216 |
| 216 } // namespace | 217 } // namespace |
| OLD | NEW |