Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Jiang | 0cce9cd | 2016-01-22 20:49:07 +0800 | [diff] [blame] | 24 | #include <sys/types.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 25 | |
Tianjie Xu | ce5fa5e | 2017-05-15 12:32:33 -0700 | [diff] [blame] | 26 | #include <string> |
| 27 | |
| 28 | #include <android-base/logging.h> |
Sen Jiang | 25c5697 | 2016-05-10 15:23:25 -0700 | [diff] [blame] | 29 | #include <bspatch.h> |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 30 | #include <openssl/sha.h> |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 31 | |
Tao Bao | d80a998 | 2016-03-03 11:43:47 -0800 | [diff] [blame] | 32 | #include "applypatch/applypatch.h" |
Tianjie Xu | ce5fa5e | 2017-05-15 12:32:33 -0700 | [diff] [blame] | 33 | #include "print_sha1.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 34 | |
| 35 | void ShowBSDiffLicense() { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 36 | puts("The bsdiff library used herein is:\n" |
| 37 | "\n" |
| 38 | "Copyright 2003-2005 Colin Percival\n" |
| 39 | "All rights reserved\n" |
| 40 | "\n" |
| 41 | "Redistribution and use in source and binary forms, with or without\n" |
| 42 | "modification, are permitted providing that the following conditions\n" |
| 43 | "are met:\n" |
| 44 | "1. Redistributions of source code must retain the above copyright\n" |
| 45 | " notice, this list of conditions and the following disclaimer.\n" |
| 46 | "2. Redistributions in binary form must reproduce the above copyright\n" |
| 47 | " notice, this list of conditions and the following disclaimer in the\n" |
| 48 | " documentation and/or other materials provided with the distribution.\n" |
| 49 | "\n" |
| 50 | "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n" |
| 51 | "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n" |
| 52 | "WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n" |
| 53 | "ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\n" |
| 54 | "DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n" |
| 55 | "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n" |
| 56 | "OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n" |
| 57 | "HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n" |
| 58 | "STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n" |
| 59 | "IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n" |
| 60 | "POSSIBILITY OF SUCH DAMAGE.\n" |
| 61 | "\n------------------\n\n" |
| 62 | "This program uses Julian R Seward's \"libbzip2\" library, available\n" |
| 63 | "from http://www.bzip.org/.\n" |
| 64 | ); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Tao Bao | f7eb760 | 2017-03-27 15:12:48 -0700 | [diff] [blame] | 67 | int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value* patch, |
Tao Bao | c0e1c46 | 2017-02-01 10:20:10 -0800 | [diff] [blame] | 68 | size_t patch_offset, SinkFn sink, SHA_CTX* ctx) { |
| 69 | auto sha_sink = [&sink, &ctx](const uint8_t* data, size_t len) { |
| 70 | len = sink(data, len); |
Sen Jiang | 25c5697 | 2016-05-10 15:23:25 -0700 | [diff] [blame] | 71 | if (ctx) SHA1_Update(ctx, data, len); |
| 72 | return len; |
| 73 | }; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 74 | |
Tianjie Xu | ce5fa5e | 2017-05-15 12:32:33 -0700 | [diff] [blame] | 75 | CHECK(patch != nullptr); |
| 76 | CHECK_LE(patch_offset, patch->data.size()); |
| 77 | |
| 78 | int result = bsdiff::bspatch(old_data, old_size, |
| 79 | reinterpret_cast<const uint8_t*>(&patch->data[patch_offset]), |
| 80 | patch->data.size() - patch_offset, sha_sink); |
| 81 | if (result != 0) { |
| 82 | LOG(ERROR) << "bspatch failed, result: " << result; |
| 83 | // print SHA1 of the patch in the case of a data error. |
| 84 | if (result == 2) { |
| 85 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 86 | SHA1(reinterpret_cast<const uint8_t*>(patch->data.data() + patch_offset), |
| 87 | patch->data.size() - patch_offset, digest); |
| 88 | std::string patch_sha1 = print_sha1(digest); |
| 89 | LOG(ERROR) << "Patch may be corrupted, offset: " << patch_offset << ", SHA1: " |
| 90 | << patch_sha1; |
| 91 | } |
| 92 | } |
| 93 | return result; |
| 94 | } |