1*acea8879SAndroid Build Coastguard Worker /*
2*acea8879SAndroid Build Coastguard Worker * Copyright (C) 2008 The Android Open Source Project
3*acea8879SAndroid Build Coastguard Worker *
4*acea8879SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*acea8879SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*acea8879SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*acea8879SAndroid Build Coastguard Worker *
8*acea8879SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*acea8879SAndroid Build Coastguard Worker *
10*acea8879SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*acea8879SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*acea8879SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*acea8879SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*acea8879SAndroid Build Coastguard Worker * limitations under the License.
15*acea8879SAndroid Build Coastguard Worker */
16*acea8879SAndroid Build Coastguard Worker
17*acea8879SAndroid Build Coastguard Worker // This file is a nearly line-for-line copy of bspatch.c from the
18*acea8879SAndroid Build Coastguard Worker // bsdiff-4.3 distribution; the primary differences being how the
19*acea8879SAndroid Build Coastguard Worker // input and output data are read and the error handling. Running
20*acea8879SAndroid Build Coastguard Worker // applypatch with the -l option will display the bsdiff license
21*acea8879SAndroid Build Coastguard Worker // notice.
22*acea8879SAndroid Build Coastguard Worker
23*acea8879SAndroid Build Coastguard Worker #include <stdio.h>
24*acea8879SAndroid Build Coastguard Worker #include <sys/types.h>
25*acea8879SAndroid Build Coastguard Worker
26*acea8879SAndroid Build Coastguard Worker #include <string>
27*acea8879SAndroid Build Coastguard Worker
28*acea8879SAndroid Build Coastguard Worker #include <android-base/logging.h>
29*acea8879SAndroid Build Coastguard Worker #include <bsdiff/bspatch.h>
30*acea8879SAndroid Build Coastguard Worker #include <openssl/sha.h>
31*acea8879SAndroid Build Coastguard Worker
32*acea8879SAndroid Build Coastguard Worker #include "applypatch/applypatch.h"
33*acea8879SAndroid Build Coastguard Worker #include "edify/expr.h"
34*acea8879SAndroid Build Coastguard Worker #include "otautil/print_sha1.h"
35*acea8879SAndroid Build Coastguard Worker
ShowBSDiffLicense()36*acea8879SAndroid Build Coastguard Worker void ShowBSDiffLicense() {
37*acea8879SAndroid Build Coastguard Worker puts("The bsdiff library used herein is:\n"
38*acea8879SAndroid Build Coastguard Worker "\n"
39*acea8879SAndroid Build Coastguard Worker "Copyright 2003-2005 Colin Percival\n"
40*acea8879SAndroid Build Coastguard Worker "All rights reserved\n"
41*acea8879SAndroid Build Coastguard Worker "\n"
42*acea8879SAndroid Build Coastguard Worker "Redistribution and use in source and binary forms, with or without\n"
43*acea8879SAndroid Build Coastguard Worker "modification, are permitted providing that the following conditions\n"
44*acea8879SAndroid Build Coastguard Worker "are met:\n"
45*acea8879SAndroid Build Coastguard Worker "1. Redistributions of source code must retain the above copyright\n"
46*acea8879SAndroid Build Coastguard Worker " notice, this list of conditions and the following disclaimer.\n"
47*acea8879SAndroid Build Coastguard Worker "2. Redistributions in binary form must reproduce the above copyright\n"
48*acea8879SAndroid Build Coastguard Worker " notice, this list of conditions and the following disclaimer in the\n"
49*acea8879SAndroid Build Coastguard Worker " documentation and/or other materials provided with the distribution.\n"
50*acea8879SAndroid Build Coastguard Worker "\n"
51*acea8879SAndroid Build Coastguard Worker "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
52*acea8879SAndroid Build Coastguard Worker "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
53*acea8879SAndroid Build Coastguard Worker "WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n"
54*acea8879SAndroid Build Coastguard Worker "ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\n"
55*acea8879SAndroid Build Coastguard Worker "DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n"
56*acea8879SAndroid Build Coastguard Worker "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n"
57*acea8879SAndroid Build Coastguard Worker "OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n"
58*acea8879SAndroid Build Coastguard Worker "HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n"
59*acea8879SAndroid Build Coastguard Worker "STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n"
60*acea8879SAndroid Build Coastguard Worker "IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n"
61*acea8879SAndroid Build Coastguard Worker "POSSIBILITY OF SUCH DAMAGE.\n"
62*acea8879SAndroid Build Coastguard Worker "\n------------------\n\n"
63*acea8879SAndroid Build Coastguard Worker "This program uses Julian R Seward's \"libbzip2\" library, available\n"
64*acea8879SAndroid Build Coastguard Worker "from http://www.bzip.org/.\n"
65*acea8879SAndroid Build Coastguard Worker );
66*acea8879SAndroid Build Coastguard Worker }
67*acea8879SAndroid Build Coastguard Worker
ApplyBSDiffPatch(const unsigned char * old_data,size_t old_size,const Value & patch,size_t patch_offset,SinkFn sink)68*acea8879SAndroid Build Coastguard Worker int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch,
69*acea8879SAndroid Build Coastguard Worker size_t patch_offset, SinkFn sink) {
70*acea8879SAndroid Build Coastguard Worker CHECK_LE(patch_offset, patch.data.size());
71*acea8879SAndroid Build Coastguard Worker
72*acea8879SAndroid Build Coastguard Worker int result = bsdiff::bspatch(old_data, old_size,
73*acea8879SAndroid Build Coastguard Worker reinterpret_cast<const uint8_t*>(&patch.data[patch_offset]),
74*acea8879SAndroid Build Coastguard Worker patch.data.size() - patch_offset, sink);
75*acea8879SAndroid Build Coastguard Worker if (result != 0) {
76*acea8879SAndroid Build Coastguard Worker LOG(ERROR) << "bspatch failed, result: " << result;
77*acea8879SAndroid Build Coastguard Worker // print SHA1 of the patch in the case of a data error.
78*acea8879SAndroid Build Coastguard Worker if (result == 2) {
79*acea8879SAndroid Build Coastguard Worker uint8_t digest[SHA_DIGEST_LENGTH];
80*acea8879SAndroid Build Coastguard Worker SHA1(reinterpret_cast<const uint8_t*>(patch.data.data() + patch_offset),
81*acea8879SAndroid Build Coastguard Worker patch.data.size() - patch_offset, digest);
82*acea8879SAndroid Build Coastguard Worker std::string patch_sha1 = print_sha1(digest);
83*acea8879SAndroid Build Coastguard Worker LOG(ERROR) << "Patch may be corrupted, offset: " << patch_offset << ", SHA1: " << patch_sha1;
84*acea8879SAndroid Build Coastguard Worker }
85*acea8879SAndroid Build Coastguard Worker }
86*acea8879SAndroid Build Coastguard Worker return result;
87*acea8879SAndroid Build Coastguard Worker }
88