blob: 6d7ffd78c9f592328561118e11febe90f1c5083f [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#ifndef _APPLYPATCH_H
18#define _APPLYPATCH_H
19
Sen Jiang696692a2016-02-02 16:01:23 +080020#include <stdint.h>
Doug Zongker512536a2010-02-17 16:11:44 -080021#include <sys/stat.h>
Yabin Cuid483c202016-02-03 17:08:52 -080022
Tao Baoc0e1c462017-02-01 10:20:10 -080023#include <functional>
Tao Baofada91c2016-10-27 18:16:06 -070024#include <memory>
Tianjie Xuaced5d92016-10-12 10:55:04 -070025#include <string>
Yabin Cuid483c202016-02-03 17:08:52 -080026#include <vector>
27
Tao Baofada91c2016-10-27 18:16:06 -070028#include <openssl/sha.h>
29
Tao Bao38d78d12017-10-09 11:03:38 -070030// Forward declaration to avoid including "edify/expr.h" in the header.
31struct Value;
Doug Zongker512536a2010-02-17 16:11:44 -080032
Yabin Cuid6c93af2016-02-10 16:41:10 -080033struct FileContents {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080034 uint8_t sha1[SHA_DIGEST_LENGTH];
Yabin Cuid6c93af2016-02-10 16:41:10 -080035 std::vector<unsigned char> data;
Doug Zongker512536a2010-02-17 16:11:44 -080036 struct stat st;
Yabin Cuid6c93af2016-02-10 16:41:10 -080037};
Doug Zongker512536a2010-02-17 16:11:44 -080038
Tianjie Xua88cc542017-10-25 13:16:54 -070039// When there isn't enough room on the target filesystem to hold the patched version of the file,
40// we copy the original here and delete it to free up space. If the expected source file doesn't
41// exist, or is corrupted, we look to see if the cached file contains the bits we want and use it as
42// the source instead. The default location for the cached source is "/cache/saved.file".
43extern std::string cache_temp_source;
Doug Zongker512536a2010-02-17 16:11:44 -080044
Tao Baoc0e1c462017-02-01 10:20:10 -080045using SinkFn = std::function<size_t(const unsigned char*, size_t)>;
Doug Zongker512536a2010-02-17 16:11:44 -080046
Tao Baofada91c2016-10-27 18:16:06 -070047// applypatch.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -080048
Doug Zongkerc4351c72010-02-22 14:46:32 -080049int ShowLicenses();
Doug Zongker512536a2010-02-17 16:11:44 -080050size_t FreeSpaceForFile(const char* filename);
Doug Zongkerc4351c72010-02-22 14:46:32 -080051int CacheSizeCheck(size_t bytes);
52int ParseSha1(const char* str, uint8_t* digest);
53
54int applypatch(const char* source_filename,
55 const char* target_filename,
56 const char* target_sha1_str,
57 size_t target_size,
Tianjie Xuaced5d92016-10-12 10:55:04 -070058 const std::vector<std::string>& patch_sha1_str,
Tao Baofada91c2016-10-27 18:16:06 -070059 const std::vector<std::unique_ptr<Value>>& patch_data,
60 const Value* bonus_data);
Doug Zongkerc4351c72010-02-22 14:46:32 -080061int applypatch_check(const char* filename,
Tianjie Xuaced5d92016-10-12 10:55:04 -070062 const std::vector<std::string>& patch_sha1_str);
Tao Baofada91c2016-10-27 18:16:06 -070063int applypatch_flash(const char* source_filename, const char* target_filename,
64 const char* target_sha1_str, size_t target_size);
Doug Zongker512536a2010-02-17 16:11:44 -080065
Doug Zongkera1bc1482014-02-13 15:18:19 -080066int LoadFileContents(const char* filename, FileContents* file);
Doug Zongker1c43c972012-02-28 11:07:09 -080067int SaveFileContents(const char* filename, const FileContents* file);
Doug Zongker512536a2010-02-17 16:11:44 -080068
Tao Baoc8e79342016-12-28 10:11:22 -080069// bspatch.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -080070
Doug Zongker512536a2010-02-17 16:11:44 -080071void ShowBSDiffLicense();
Tao Bao1e0941f2017-11-10 11:49:53 -080072
73// Applies the bsdiff-patch given in 'patch' (from offset 'patch_offset' to the end) to the source
74// data given by (old_data, old_size). Writes the patched output through the given 'sink', and
75// updates the SHA-1 context with the output data. Returns 0 on success.
76int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch,
Tao Baoc0e1c462017-02-01 10:20:10 -080077 size_t patch_offset, SinkFn sink, SHA_CTX* ctx);
Doug Zongker512536a2010-02-17 16:11:44 -080078
Yabin Cuid483c202016-02-03 17:08:52 -080079// imgpatch.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -080080
81// Applies the imgdiff-patch given in 'patch' to the source data given by (old_data, old_size), with
82// the optional bonus data. Writes the patched output through the given 'sink', and updates the
83// SHA-1 context with the output data. Returns 0 on success.
84int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& patch, SinkFn sink,
Tao Baoc0e1c462017-02-01 10:20:10 -080085 SHA_CTX* ctx, const Value* bonus_data);
Doug Zongker512536a2010-02-17 16:11:44 -080086
Yabin Cuid483c202016-02-03 17:08:52 -080087// freecache.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -080088
Doug Zongker512536a2010-02-17 16:11:44 -080089int MakeFreeSpaceOnCache(size_t bytes_needed);
90
91#endif