blob: 75975ad6dcff3684584e3a922a5586d333738ee4 [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
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
21// notice.
22
23#include <stdio.h>
24#include <sys/stat.h>
Sen Jiang0cce9cd2016-01-22 20:49:07 +080025#include <sys/types.h>
Doug Zongker512536a2010-02-17 16:11:44 -080026#include <errno.h>
Elliott Hughes26dbad22015-01-28 12:09:05 -080027#include <malloc.h>
Doug Zongker512536a2010-02-17 16:11:44 -080028#include <unistd.h>
29#include <string.h>
30
31#include <bzlib.h>
32
33#include "mincrypt/sha.h"
34#include "applypatch.h"
35
36void ShowBSDiffLicense() {
Doug Zongkerc4351c72010-02-22 14:46:32 -080037 puts("The bsdiff library used herein is:\n"
38 "\n"
39 "Copyright 2003-2005 Colin Percival\n"
40 "All rights reserved\n"
41 "\n"
42 "Redistribution and use in source and binary forms, with or without\n"
43 "modification, are permitted providing that the following conditions\n"
44 "are met:\n"
45 "1. Redistributions of source code must retain the above copyright\n"
46 " notice, this list of conditions and the following disclaimer.\n"
47 "2. Redistributions in binary form must reproduce the above copyright\n"
48 " notice, this list of conditions and the following disclaimer in the\n"
49 " documentation and/or other materials provided with the distribution.\n"
50 "\n"
51 "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
52 "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
53 "WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n"
54 "ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\n"
55 "DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n"
56 "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n"
57 "OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n"
58 "HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n"
59 "STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n"
60 "IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n"
61 "POSSIBILITY OF SUCH DAMAGE.\n"
62 "\n------------------\n\n"
63 "This program uses Julian R Seward's \"libbzip2\" library, available\n"
64 "from http://www.bzip.org/.\n"
65 );
Doug Zongker512536a2010-02-17 16:11:44 -080066}
67
68static off_t offtin(u_char *buf)
69{
Doug Zongkerc4351c72010-02-22 14:46:32 -080070 off_t y;
Doug Zongker512536a2010-02-17 16:11:44 -080071
Doug Zongkerc4351c72010-02-22 14:46:32 -080072 y=buf[7]&0x7F;
73 y=y*256;y+=buf[6];
74 y=y*256;y+=buf[5];
75 y=y*256;y+=buf[4];
76 y=y*256;y+=buf[3];
77 y=y*256;y+=buf[2];
78 y=y*256;y+=buf[1];
79 y=y*256;y+=buf[0];
Doug Zongker512536a2010-02-17 16:11:44 -080080
Doug Zongkerc4351c72010-02-22 14:46:32 -080081 if(buf[7]&0x80) y=-y;
Doug Zongker512536a2010-02-17 16:11:44 -080082
Doug Zongkerc4351c72010-02-22 14:46:32 -080083 return y;
Doug Zongker512536a2010-02-17 16:11:44 -080084}
85
Doug Zongkerc4351c72010-02-22 14:46:32 -080086int FillBuffer(unsigned char* buffer, int size, bz_stream* stream) {
87 stream->next_out = (char*)buffer;
88 stream->avail_out = size;
89 while (stream->avail_out > 0) {
90 int bzerr = BZ2_bzDecompress(stream);
91 if (bzerr != BZ_OK && bzerr != BZ_STREAM_END) {
92 printf("bz error %d decompressing\n", bzerr);
93 return -1;
94 }
95 if (stream->avail_out > 0) {
96 printf("need %d more bytes\n", stream->avail_out);
97 }
98 }
99 return 0;
100}
Doug Zongker512536a2010-02-17 16:11:44 -0800101
102int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size,
Doug Zongkerc4351c72010-02-22 14:46:32 -0800103 const Value* patch, ssize_t patch_offset,
Doug Zongker512536a2010-02-17 16:11:44 -0800104 SinkFn sink, void* token, SHA_CTX* ctx) {
105
Doug Zongkerc4351c72010-02-22 14:46:32 -0800106 unsigned char* new_data;
107 ssize_t new_size;
108 if (ApplyBSDiffPatchMem(old_data, old_size, patch, patch_offset,
109 &new_data, &new_size) != 0) {
110 return -1;
111 }
Doug Zongker512536a2010-02-17 16:11:44 -0800112
Doug Zongkerc4351c72010-02-22 14:46:32 -0800113 if (sink(new_data, new_size, token) < new_size) {
114 printf("short write of output: %d (%s)\n", errno, strerror(errno));
115 return 1;
116 }
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700117 if (ctx) SHA_update(ctx, new_data, new_size);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800118 free(new_data);
Doug Zongker512536a2010-02-17 16:11:44 -0800119
Doug Zongkerc4351c72010-02-22 14:46:32 -0800120 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800121}
122
123int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
Doug Zongkerc4351c72010-02-22 14:46:32 -0800124 const Value* patch, ssize_t patch_offset,
Doug Zongker512536a2010-02-17 16:11:44 -0800125 unsigned char** new_data, ssize_t* new_size) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800126 // Patch data format:
127 // 0 8 "BSDIFF40"
128 // 8 8 X
129 // 16 8 Y
130 // 24 8 sizeof(newfile)
131 // 32 X bzip2(control block)
132 // 32+X Y bzip2(diff block)
133 // 32+X+Y ??? bzip2(extra block)
134 // with control block a set of triples (x,y,z) meaning "add x bytes
135 // from oldfile to x bytes from the diff block; copy y bytes from the
136 // extra block; seek forwards in oldfile by z bytes".
Doug Zongker512536a2010-02-17 16:11:44 -0800137
Doug Zongkerc4351c72010-02-22 14:46:32 -0800138 unsigned char* header = (unsigned char*) patch->data + patch_offset;
139 if (memcmp(header, "BSDIFF40", 8) != 0) {
140 printf("corrupt bsdiff patch file header (magic number)\n");
Doug Zongker512536a2010-02-17 16:11:44 -0800141 return 1;
Doug Zongker512536a2010-02-17 16:11:44 -0800142 }
143
Doug Zongkerc4351c72010-02-22 14:46:32 -0800144 ssize_t ctrl_len, data_len;
145 ctrl_len = offtin(header+8);
146 data_len = offtin(header+16);
147 *new_size = offtin(header+24);
148
149 if (ctrl_len < 0 || data_len < 0 || *new_size < 0) {
150 printf("corrupt patch file header (data lengths)\n");
151 return 1;
Doug Zongker512536a2010-02-17 16:11:44 -0800152 }
153
Doug Zongkerc4351c72010-02-22 14:46:32 -0800154 int bzerr;
155
156 bz_stream cstream;
157 cstream.next_in = patch->data + patch_offset + 32;
158 cstream.avail_in = ctrl_len;
159 cstream.bzalloc = NULL;
160 cstream.bzfree = NULL;
161 cstream.opaque = NULL;
162 if ((bzerr = BZ2_bzDecompressInit(&cstream, 0, 0)) != BZ_OK) {
163 printf("failed to bzinit control stream (%d)\n", bzerr);
Doug Zongker512536a2010-02-17 16:11:44 -0800164 }
165
Doug Zongkerc4351c72010-02-22 14:46:32 -0800166 bz_stream dstream;
167 dstream.next_in = patch->data + patch_offset + 32 + ctrl_len;
168 dstream.avail_in = data_len;
169 dstream.bzalloc = NULL;
170 dstream.bzfree = NULL;
171 dstream.opaque = NULL;
172 if ((bzerr = BZ2_bzDecompressInit(&dstream, 0, 0)) != BZ_OK) {
173 printf("failed to bzinit diff stream (%d)\n", bzerr);
Doug Zongker512536a2010-02-17 16:11:44 -0800174 }
175
Doug Zongkerc4351c72010-02-22 14:46:32 -0800176 bz_stream estream;
177 estream.next_in = patch->data + patch_offset + 32 + ctrl_len + data_len;
178 estream.avail_in = patch->size - (patch_offset + 32 + ctrl_len + data_len);
179 estream.bzalloc = NULL;
180 estream.bzfree = NULL;
181 estream.opaque = NULL;
182 if ((bzerr = BZ2_bzDecompressInit(&estream, 0, 0)) != BZ_OK) {
183 printf("failed to bzinit extra stream (%d)\n", bzerr);
Doug Zongker512536a2010-02-17 16:11:44 -0800184 }
185
Tao Baoba9a42a2015-06-23 23:23:33 -0700186 *new_data = reinterpret_cast<unsigned char*>(malloc(*new_size));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800187 if (*new_data == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700188 printf("failed to allocate %zd bytes of memory for output file\n", *new_size);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800189 return 1;
Doug Zongker512536a2010-02-17 16:11:44 -0800190 }
191
Doug Zongkerc4351c72010-02-22 14:46:32 -0800192 off_t oldpos = 0, newpos = 0;
193 off_t ctrl[3];
194 off_t len_read;
195 int i;
196 unsigned char buf[24];
197 while (newpos < *new_size) {
198 // Read control data
199 if (FillBuffer(buf, 24, &cstream) != 0) {
200 printf("error while reading control stream\n");
201 return 1;
202 }
203 ctrl[0] = offtin(buf);
204 ctrl[1] = offtin(buf+8);
205 ctrl[2] = offtin(buf+16);
Doug Zongker512536a2010-02-17 16:11:44 -0800206
Doug Zongker4aa12dd2014-05-13 08:40:49 -0700207 if (ctrl[0] < 0 || ctrl[1] < 0) {
208 printf("corrupt patch (negative byte counts)\n");
209 return 1;
210 }
211
Doug Zongkerc4351c72010-02-22 14:46:32 -0800212 // Sanity check
213 if (newpos + ctrl[0] > *new_size) {
214 printf("corrupt patch (new file overrun)\n");
215 return 1;
216 }
Doug Zongker512536a2010-02-17 16:11:44 -0800217
Doug Zongkerc4351c72010-02-22 14:46:32 -0800218 // Read diff string
219 if (FillBuffer(*new_data + newpos, ctrl[0], &dstream) != 0) {
220 printf("error while reading diff stream\n");
221 return 1;
222 }
223
224 // Add old data to diff string
225 for (i = 0; i < ctrl[0]; ++i) {
226 if ((oldpos+i >= 0) && (oldpos+i < old_size)) {
227 (*new_data)[newpos+i] += old_data[oldpos+i];
228 }
229 }
230
231 // Adjust pointers
232 newpos += ctrl[0];
233 oldpos += ctrl[0];
234
235 // Sanity check
236 if (newpos + ctrl[1] > *new_size) {
237 printf("corrupt patch (new file overrun)\n");
238 return 1;
239 }
240
241 // Read extra string
242 if (FillBuffer(*new_data + newpos, ctrl[1], &estream) != 0) {
243 printf("error while reading extra stream\n");
244 return 1;
245 }
246
247 // Adjust pointers
248 newpos += ctrl[1];
249 oldpos += ctrl[2];
250 }
251
252 BZ2_bzDecompressEnd(&cstream);
253 BZ2_bzDecompressEnd(&dstream);
254 BZ2_bzDecompressEnd(&estream);
255 return 0;
Doug Zongker512536a2010-02-17 16:11:44 -0800256}