2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // This file is a nearly line-for-line copy of bspatch.c from the
18 // bsdiff-4.3 distribution; the primary differences being how the
19 // input and output data are read and the error handling. Running
20 // applypatch with the -l option will display the bsdiff license
31 #include "mincrypt/sha.h"
32 #include "applypatch.h"
34 void ShowBSDiffLicense() {
35 puts("The bsdiff library used herein is:\n"
37 "Copyright 2003-2005 Colin Percival\n"
38 "All rights reserved\n"
40 "Redistribution and use in source and binary forms, with or without\n"
41 "modification, are permitted providing that the following conditions\n"
43 "1. Redistributions of source code must retain the above copyright\n"
44 " notice, this list of conditions and the following disclaimer.\n"
45 "2. Redistributions in binary form must reproduce the above copyright\n"
46 " notice, this list of conditions and the following disclaimer in the\n"
47 " documentation and/or other materials provided with the distribution.\n"
49 "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
50 "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
51 "WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n"
52 "ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\n"
53 "DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n"
54 "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n"
55 "OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n"
56 "HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n"
57 "STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n"
58 "IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n"
59 "POSSIBILITY OF SUCH DAMAGE.\n"
60 "\n------------------\n\n"
61 "This program uses Julian R Seward's \"libbzip2\" library, available\n"
62 "from http://www.bzip.org/.\n"
66 static off_t offtin(u_char *buf)
85 int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size,
86 const char* patch_filename, ssize_t patch_offset,
87 SinkFn sink, void* token, SHA_CTX* ctx) {
89 unsigned char* new_data;
91 if (ApplyBSDiffPatchMem(old_data, old_size, patch_filename, patch_offset,
92 &new_data, &new_size) != 0) {
96 if (sink(new_data, new_size, token) < new_size) {
97 fprintf(stderr, "short write of output: %d (%s)\n", errno, strerror(errno));
101 SHA_update(ctx, new_data, new_size);
108 int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
109 const char* patch_filename, ssize_t patch_offset,
110 unsigned char** new_data, ssize_t* new_size) {
113 if ((f = fopen(patch_filename, "rb")) == NULL) {
114 fprintf(stderr, "failed to open patch file\n");
122 // 24 8 sizeof(newfile)
123 // 32 X bzip2(control block)
124 // 32+X Y bzip2(diff block)
125 // 32+X+Y ??? bzip2(extra block)
126 // with control block a set of triples (x,y,z) meaning "add x bytes
127 // from oldfile to x bytes from the diff block; copy y bytes from the
128 // extra block; seek forwards in oldfile by z bytes".
130 fseek(f, patch_offset, SEEK_SET);
132 unsigned char header[32];
133 if (fread(header, 1, 32, f) < 32) {
134 fprintf(stderr, "failed to read patch file header\n");
138 if (memcmp(header, "BSDIFF40", 8) != 0) {
139 fprintf(stderr, "corrupt bsdiff patch file header (magic number)\n");
143 ssize_t ctrl_len, data_len;
144 ctrl_len = offtin(header+8);
145 data_len = offtin(header+16);
146 *new_size = offtin(header+24);
148 if (ctrl_len < 0 || data_len < 0 || *new_size < 0) {
149 fprintf(stderr, "corrupt patch file header (data lengths)\n");
157 #define OPEN_AT(f, bzf, offset) \
160 if ((f = fopen(patch_filename, "rb")) == NULL) { \
161 fprintf(stderr, "failed to open patch file\n"); \
164 if (fseeko(f, offset+patch_offset, SEEK_SET)) { \
165 fprintf(stderr, "failed to seek in patch file\n"); \
168 if ((bzf = BZ2_bzReadOpen(&bzerr, f, 0, 0, NULL, 0)) == NULL) { \
169 fprintf(stderr, "failed to bzReadOpen in patch file (%d)\n", bzerr); \
173 OPEN_AT(cpf, cpfbz2, 32);
174 OPEN_AT(dpf, dpfbz2, 32+ctrl_len);
175 OPEN_AT(epf, epfbz2, 32+ctrl_len+data_len);
179 *new_data = malloc(*new_size);
180 if (*new_data == NULL) {
181 fprintf(stderr, "failed to allocate %d bytes of memory for output file\n",
186 off_t oldpos = 0, newpos = 0;
190 unsigned char buf[8];
191 while (newpos < *new_size) {
193 for (i = 0; i < 3; ++i) {
194 len_read = BZ2_bzRead(&bzerr, cpfbz2, buf, 8);
195 if (len_read < 8 || !(bzerr == BZ_OK || bzerr == BZ_STREAM_END)) {
196 fprintf(stderr, "corrupt patch (read control)\n");
199 ctrl[i] = offtin(buf);
203 if (newpos + ctrl[0] > *new_size) {
204 fprintf(stderr, "corrupt patch (new file overrun)\n");
209 len_read = BZ2_bzRead(&bzerr, dpfbz2, *new_data + newpos, ctrl[0]);
210 if (len_read < ctrl[0] || !(bzerr == BZ_OK || bzerr == BZ_STREAM_END)) {
211 fprintf(stderr, "corrupt patch (read diff)\n");
215 // Add old data to diff string
216 for (i = 0; i < ctrl[0]; ++i) {
217 if ((oldpos+i >= 0) && (oldpos+i < old_size)) {
218 (*new_data)[newpos+i] += old_data[oldpos+i];
227 if (newpos + ctrl[1] > *new_size) {
228 fprintf(stderr, "corrupt patch (new file overrun)\n");
233 len_read = BZ2_bzRead(&bzerr, epfbz2, *new_data + newpos, ctrl[1]);
234 if (len_read < ctrl[1] || !(bzerr == BZ_OK || bzerr == BZ_STREAM_END)) {
235 fprintf(stderr, "corrupt patch (read extra)\n");
244 BZ2_bzReadClose(&bzerr, cpfbz2);
245 BZ2_bzReadClose(&bzerr, dpfbz2);
246 BZ2_bzReadClose(&bzerr, epfbz2);