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 #ifndef _APPLYPATCH_H 18 #define _APPLYPATCH_H 19 20 #include <stdint.h> 21 #include <sys/stat.h> 22 23 #include <functional> 24 #include <memory> 25 #include <string> 26 #include <vector> 27 28 #include <openssl/sha.h> 29 30 #include "edify/expr.h" 31 32 struct FileContents { 33 uint8_t sha1[SHA_DIGEST_LENGTH]; 34 std::vector<unsigned char> data; 35 struct stat st; 36 }; 37 38 // When there isn't enough room on the target filesystem to hold the 39 // patched version of the file, we copy the original here and delete 40 // it to free up space. If the expected source file doesn't exist, or 41 // is corrupted, we look to see if this file contains the bits we want 42 // and use it as the source instead. 43 #define CACHE_TEMP_SOURCE "/cache/saved.file" 44 45 using SinkFn = std::function<size_t(const unsigned char*, size_t)>; 46 47 // applypatch.cpp 48 int ShowLicenses(); 49 size_t FreeSpaceForFile(const char* filename); 50 int CacheSizeCheck(size_t bytes); 51 int ParseSha1(const char* str, uint8_t* digest); 52 53 int applypatch(const char* source_filename, 54 const char* target_filename, 55 const char* target_sha1_str, 56 size_t target_size, 57 const std::vector<std::string>& patch_sha1_str, 58 const std::vector<std::unique_ptr<Value>>& patch_data, 59 const Value* bonus_data); 60 int applypatch_check(const char* filename, 61 const std::vector<std::string>& patch_sha1_str); 62 int applypatch_flash(const char* source_filename, const char* target_filename, 63 const char* target_sha1_str, size_t target_size); 64 65 int LoadFileContents(const char* filename, FileContents* file); 66 int SaveFileContents(const char* filename, const FileContents* file); 67 68 // bspatch.cpp 69 void ShowBSDiffLicense(); 70 int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value* patch, 71 size_t patch_offset, SinkFn sink, SHA_CTX* ctx); 72 73 // imgpatch.cpp 74 int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value* patch, SinkFn sink, 75 SHA_CTX* ctx, const Value* bonus_data); 76 77 // freecache.cpp 78 int MakeFreeSpaceOnCache(size_t bytes_needed); 79 80 #endif 81