blob: 9920c2be1c4ab2dad1aed297a785550b2a84db45 [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
Sen Jiang25c56972016-05-10 15:23:25 -070026#include <bspatch.h>
Doug Zongker512536a2010-02-17 16:11:44 -080027
Tao Baod80a9982016-03-03 11:43:47 -080028#include "applypatch/applypatch.h"
Sen Jiang25c56972016-05-10 15:23:25 -070029#include "openssl/sha.h"
Doug Zongker512536a2010-02-17 16:11:44 -080030
31void ShowBSDiffLicense() {
Doug Zongkerc4351c72010-02-22 14:46:32 -080032 puts("The bsdiff library used herein is:\n"
33 "\n"
34 "Copyright 2003-2005 Colin Percival\n"
35 "All rights reserved\n"
36 "\n"
37 "Redistribution and use in source and binary forms, with or without\n"
38 "modification, are permitted providing that the following conditions\n"
39 "are met:\n"
40 "1. Redistributions of source code must retain the above copyright\n"
41 " notice, this list of conditions and the following disclaimer.\n"
42 "2. Redistributions in binary form must reproduce the above copyright\n"
43 " notice, this list of conditions and the following disclaimer in the\n"
44 " documentation and/or other materials provided with the distribution.\n"
45 "\n"
46 "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
47 "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
48 "WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n"
49 "ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\n"
50 "DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n"
51 "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n"
52 "OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n"
53 "HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n"
54 "STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n"
55 "IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n"
56 "POSSIBILITY OF SUCH DAMAGE.\n"
57 "\n------------------\n\n"
58 "This program uses Julian R Seward's \"libbzip2\" library, available\n"
59 "from http://www.bzip.org/.\n"
60 );
Doug Zongker512536a2010-02-17 16:11:44 -080061}
62
Sen Jiang25c56972016-05-10 15:23:25 -070063int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size, const Value* patch,
64 ssize_t patch_offset, SinkFn sink, void* token, SHA_CTX* ctx) {
65 auto sha_sink = [&](const uint8_t* data, size_t len) {
66 len = sink(data, len, token);
67 if (ctx) SHA1_Update(ctx, data, len);
68 return len;
69 };
70 return bsdiff::bspatch(old_data, old_size,
71 reinterpret_cast<const uint8_t*>(&patch->data[patch_offset]),
72 patch->data.size(), sha_sink);
Doug Zongker512536a2010-02-17 16:11:44 -080073}
74
Sen Jiang25c56972016-05-10 15:23:25 -070075int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size, const Value* patch,
76 ssize_t patch_offset, std::vector<unsigned char>* new_data) {
77 auto vector_sink = [new_data](const uint8_t* data, size_t len) {
78 new_data->insert(new_data->end(), data, data + len);
79 return len;
80 };
81 return bsdiff::bspatch(old_data, old_size,
82 reinterpret_cast<const uint8_t*>(&patch->data[patch_offset]),
83 patch->data.size(), vector_sink);
Doug Zongker512536a2010-02-17 16:11:44 -080084}