Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: third_party/courgette/bsdiff_apply.cc

Issue 115062: Move Courgette... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/courgette/bsdiff.h ('k') | third_party/courgette/bsdiff_create.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 */
30
31 // 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
33 // found in the LICENSE file.
34
35 #include "third_party/courgette/bsdiff.h"
36
37 #include "third_party/courgette/crc.h"
38 #include "third_party/courgette/streams.h"
39
40 namespace courgette {
41
42 BSDiffStatus MBS_ReadHeader(SourceStream* stream, MBSPatchHeader* header) {
43 if (!stream->Read(header->tag, sizeof(header->tag))) return READ_ERROR;
44 if (!stream->ReadVarint32(&header->slen)) return READ_ERROR;
45 if (!stream->ReadVarint32(&header->scrc32)) return READ_ERROR;
46 if (!stream->ReadVarint32(&header->dlen)) return READ_ERROR;
47 if (!stream->ReadVarint32(&header->cblen)) return READ_ERROR;
48 if (!stream->ReadVarint32(&header->difflen)) return READ_ERROR;
49 if (!stream->ReadVarint32(&header->extralen)) return READ_ERROR;
50
51 // The string will have a NUL terminator that we don't use, hence '-1'.
52 COMPILE_ASSERT(sizeof(MBS_PATCH_HEADER_TAG) - 1 == sizeof(header->tag),
53 MBS_PATCH_HEADER_TAG_must_match_header_field_size);
54 if (memcmp(header->tag, MBS_PATCH_HEADER_TAG, 8) != 0)
55 return UNEXPECTED_ERROR;
56
57 size_t bytes_remaining = stream->Remaining();
58 if (header->cblen +
59 header->difflen +
60 header->extralen != bytes_remaining)
61 return UNEXPECTED_ERROR;
62
63 return OK;
64 }
65
66 BSDiffStatus MBS_ApplyPatch(const MBSPatchHeader *header,
67 SourceStream* patch_stream,
68 const uint8* old_start, size_t old_size,
69 SinkStream* new_stream) {
70 const uint8* old_end = old_start + old_size;
71
72 SourceStream control_stream;
73
74 const uint8* control_start = patch_stream->Buffer();
75 if (!patch_stream->ReadSubstream(header->cblen, &control_stream))
76 return READ_ERROR;
77 if (!patch_stream->Skip(header->difflen + header->extralen))
78 return READ_ERROR;
79 if (!patch_stream->Empty())
80 return READ_ERROR;
81
82 const uint8* diff_start = control_start + header->cblen;
83 const uint8* diff_end = diff_start + header->difflen;
84 const uint8* extra_start = diff_end;
85 const uint8* extra_end = extra_start + header->extralen;
86
87 const uint8* old_position = old_start;
88 const uint8* diff_position = diff_start;
89 const uint8* extra_position = extra_start;
90
91 new_stream->Reserve(header->dlen);
92
93 while (!control_stream.Empty()) {
94 uint32 copy_count, extra_count;
95 int32 seek_adjustment;
96 if (!control_stream.ReadVarint32(&copy_count))
97 return UNEXPECTED_ERROR;
98 if (!control_stream.ReadVarint32(&extra_count))
99 return UNEXPECTED_ERROR;
100 if (!control_stream.ReadVarint32Signed(&seek_adjustment))
101 return UNEXPECTED_ERROR;
102
103 #ifdef DEBUG_bsmedberg
104 printf("Applying block: copy: %-8u extra: %-8u seek: %+i\n",
105 copy_count, extra_count, seek_adjustment);
106 #endif
107 // Byte-wise arithmetically add bytes from old file to bytes from the diff
108 // block.
109 if (copy_count > static_cast<size_t>(old_end - old_position))
110 return UNEXPECTED_ERROR;
111 if (copy_count > static_cast<size_t>(diff_end - diff_position))
112 return UNEXPECTED_ERROR;
113
114 // Add together bytes from the 'old' file and the 'diff' stream.
115 for (size_t i = 0; i < copy_count; ++i) {
116 uint8 byte = old_position[i] + diff_position[i];
117 new_stream->Write(&byte, 1);
118 }
119 old_position += copy_count;
120 diff_position += copy_count;
121
122 // Copy bytes from the extra block.
123 if (extra_count > static_cast<size_t>(extra_end - extra_position))
124 return UNEXPECTED_ERROR;
125
126 new_stream->Write(extra_position, extra_count);
127 extra_position += extra_count;
128
129 // "seek" forwards (or backwards) in oldfile.
130 if (old_position + seek_adjustment < old_start ||
131 old_position + seek_adjustment > old_end)
132 return UNEXPECTED_ERROR;
133
134 old_position += seek_adjustment;
135 }
136
137 if (diff_position != diff_end)
138 return UNEXPECTED_ERROR;
139 if (extra_position != extra_end)
140 return UNEXPECTED_ERROR;
141
142 return OK;
143 }
144
145 BSDiffStatus ApplyBinaryPatch(SourceStream* old_stream,
146 SourceStream* patch_stream,
147 SinkStream* new_stream) {
148 MBSPatchHeader header;
149 BSDiffStatus ret = MBS_ReadHeader(patch_stream, &header);
150 if (ret != OK) return ret;
151
152 const uint8* old_start = old_stream->Buffer();
153 size_t old_size = old_stream->Remaining();
154
155 if (old_size != header.slen) return UNEXPECTED_ERROR;
156
157 if (CalculateCrc(old_start, old_size) != header.scrc32)
158 return CRC_ERROR;
159
160 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream);
161
162 return OK;
163 }
164
165 } // namespace
OLDNEW
« no previous file with comments | « third_party/courgette/bsdiff.h ('k') | third_party/courgette/bsdiff_create.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698