1 /*
2 * Copyright (C) 2019 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 #include "patch_utils.h"
18
19 #include <stdio.h>
20
21 #include "adb_io.h"
22 #include "adb_utils.h"
23 #include "android-base/endian.h"
24 #include "sysdeps.h"
25
26 #include "apk_archive.h"
27
28 using namespace com::android;
29 using namespace com::android::fastdeploy;
30 using namespace android::base;
31
32 static constexpr char kSignature[] = "FASTDEPLOY";
33
GetDeviceAPKMetaData(const APKDump & apk_dump)34 APKMetaData PatchUtils::GetDeviceAPKMetaData(const APKDump& apk_dump) {
35 APKMetaData apkMetaData;
36 apkMetaData.set_absolute_path(apk_dump.absolute_path());
37
38 std::string md5Hash;
39 int64_t localFileHeaderOffset;
40 int64_t dataSize;
41
42 const auto& cd = apk_dump.cd();
43 auto cur = cd.data();
44 int64_t size = cd.size();
45 while (auto consumed = ApkArchive::ParseCentralDirectoryRecord(
46 cur, size, &md5Hash, &localFileHeaderOffset, &dataSize)) {
47 cur += consumed;
48 size -= consumed;
49
50 auto apkEntry = apkMetaData.add_entries();
51 apkEntry->set_md5(md5Hash);
52 apkEntry->set_dataoffset(localFileHeaderOffset);
53 apkEntry->set_datasize(dataSize);
54 }
55 return apkMetaData;
56 }
57
GetHostAPKMetaData(const char * apkPath)58 APKMetaData PatchUtils::GetHostAPKMetaData(const char* apkPath) {
59 ApkArchive archive(apkPath);
60 auto dump = archive.ExtractMetadata();
61 if (dump.cd().empty()) {
62 fprintf(stderr, "adb: Could not extract Central Directory from %s\n", apkPath);
63 exit(1);
64 }
65
66 auto apkMetaData = GetDeviceAPKMetaData(dump);
67
68 // Now let's set data sizes.
69 for (auto& apkEntry : *apkMetaData.mutable_entries()) {
70 auto dataSize =
71 archive.CalculateLocalFileEntrySize(apkEntry.dataoffset(), apkEntry.datasize());
72 if (dataSize == 0) {
73 fprintf(stderr, "adb: empty local file entry in %s\n", apkPath);
74 exit(1);
75 }
76 apkEntry.set_datasize(dataSize);
77 }
78
79 return apkMetaData;
80 }
81
WriteSignature(borrowed_fd output)82 void PatchUtils::WriteSignature(borrowed_fd output) {
83 WriteFdExactly(output, kSignature, sizeof(kSignature) - 1);
84 }
85
WriteLong(int64_t value,borrowed_fd output)86 void PatchUtils::WriteLong(int64_t value, borrowed_fd output) {
87 int64_t littleEndian = htole64(value);
88 WriteFdExactly(output, &littleEndian, sizeof(littleEndian));
89 }
90
WriteString(const std::string & value,android::base::borrowed_fd output)91 void PatchUtils::WriteString(const std::string& value, android::base::borrowed_fd output) {
92 WriteLong(value.size(), output);
93 WriteFdExactly(output, value);
94 }
95
Pipe(borrowed_fd input,borrowed_fd output,size_t amount)96 void PatchUtils::Pipe(borrowed_fd input, borrowed_fd output, size_t amount) {
97 constexpr static size_t BUFFER_SIZE = 128 * 1024;
98 char buffer[BUFFER_SIZE];
99 size_t transferAmount = 0;
100 while (transferAmount != amount) {
101 auto chunkAmount = std::min(amount - transferAmount, BUFFER_SIZE);
102 auto readAmount = adb_read(input, buffer, chunkAmount);
103 if (readAmount < 0) {
104 fprintf(stderr, "adb: failed to read from input: %s\n", strerror(errno));
105 exit(1);
106 }
107 WriteFdExactly(output, buffer, readAmount);
108 transferAmount += readAmount;
109 }
110 }
111