blob: ba33c3a9c6c708ddd0592c2864e4728e23e7d3be [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>
Sen Jiang0cce9cd2016-01-22 20:49:07 +080024#include <sys/types.h>
Doug Zongker512536a2010-02-17 16:11:44 -080025
Tianjie Xuce5fa5e2017-05-15 12:32:33 -070026#include <string>
27
28#include <android-base/logging.h>
Alex Deymo3afe5f52017-11-08 12:26:44 +010029#include <bsdiff/bspatch.h>
Tao Baoc0e1c462017-02-01 10:20:10 -080030#include <openssl/sha.h>
Doug Zongker512536a2010-02-17 16:11:44 -080031
Tao Baod80a9982016-03-03 11:43:47 -080032#include "applypatch/applypatch.h"
Tao Bao38d78d12017-10-09 11:03:38 -070033#include "edify/expr.h"
Tao Bao09e468f2017-09-29 14:39:33 -070034#include "otautil/print_sha1.h"
Doug Zongker512536a2010-02-17 16:11:44 -080035
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
Tao Bao1e0941f2017-11-10 11:49:53 -080068int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch,
Tao Bao8b0b0f12018-04-19 21:02:13 -070069 size_t patch_offset, SinkFn sink) {
Tao Bao1e0941f2017-11-10 11:49:53 -080070 CHECK_LE(patch_offset, patch.data.size());
Tianjie Xuce5fa5e2017-05-15 12:32:33 -070071
72 int result = bsdiff::bspatch(old_data, old_size,
Tao Bao1e0941f2017-11-10 11:49:53 -080073 reinterpret_cast<const uint8_t*>(&patch.data[patch_offset]),
Tao Bao8b0b0f12018-04-19 21:02:13 -070074 patch.data.size() - patch_offset, sink);
Tianjie Xuce5fa5e2017-05-15 12:32:33 -070075 if (result != 0) {
76 LOG(ERROR) << "bspatch failed, result: " << result;
77 // print SHA1 of the patch in the case of a data error.
78 if (result == 2) {
79 uint8_t digest[SHA_DIGEST_LENGTH];
Tao Bao1e0941f2017-11-10 11:49:53 -080080 SHA1(reinterpret_cast<const uint8_t*>(patch.data.data() + patch_offset),
81 patch.data.size() - patch_offset, digest);
Tianjie Xuce5fa5e2017-05-15 12:32:33 -070082 std::string patch_sha1 = print_sha1(digest);
Tao Bao1e0941f2017-11-10 11:49:53 -080083 LOG(ERROR) << "Patch may be corrupted, offset: " << patch_offset << ", SHA1: " << patch_sha1;
Tianjie Xuce5fa5e2017-05-15 12:32:33 -070084 }
85 }
86 return result;
Tao Bao38d78d12017-10-09 11:03:38 -070087}