1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "BackupHelperDispatcher_native"
#include <utils/Log.h>
#include "JNIHelp.h"
#include <android_runtime/AndroidRuntime.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#define VERSION_1_HEADER 0x01706c48 // 'Hlp'1 little endian
namespace android
{
struct chunk_header_v1 {
int headerSize;
int version;
int dataSize; // corresponds to Header.chunkSize
int nameLength; // not including the NULL terminator, which is not written to the file
};
// java.io.FileDescriptor
static jfieldID s_descriptorField = 0;
static jfieldID s_chunkSizeField = 0;
static jfieldID s_keyPrefixField = 0;
static int
readHeader_native(JNIEnv* env, jobject clazz, jobject headerObj, jobject fdObj)
{
chunk_header_v1 flattenedHeader;
int fd;
ssize_t amt;
String8 keyPrefix;
char* buf;
fd = env->GetIntField(fdObj, s_descriptorField);
amt = read(fd, &flattenedHeader.headerSize, sizeof(flattenedHeader.headerSize));
if (amt != sizeof(flattenedHeader.headerSize)) {
return -1;
}
int remainingHeader = flattenedHeader.headerSize - sizeof(flattenedHeader.headerSize);
if (flattenedHeader.headerSize < (int)sizeof(chunk_header_v1)) {
LOGW("Skipping unknown header: %d bytes", flattenedHeader.headerSize);
if (remainingHeader > 0) {
lseek(fd, remainingHeader, SEEK_CUR);
// >0 means skip this chunk
return 1;
}
}
amt = read(fd, &flattenedHeader.version,
sizeof(chunk_header_v1)-sizeof(flattenedHeader.headerSize));
if (amt <= 0) {
LOGW("Failed reading chunk header");
return -1;
}
remainingHeader -= sizeof(chunk_header_v1)-sizeof(flattenedHeader.headerSize);
if (flattenedHeader.version != VERSION_1_HEADER) {
LOGW("Skipping unknown header version: 0x%08x, %d bytes", flattenedHeader.version,
flattenedHeader.headerSize);
if (remainingHeader > 0) {
lseek(fd, remainingHeader, SEEK_CUR);
// >0 means skip this chunk
return 1;
}
}
#if 0
LOGD("chunk header:");
LOGD(" headerSize=%d", flattenedHeader.headerSize);
LOGD(" version=0x%08x", flattenedHeader.version);
LOGD(" dataSize=%d", flattenedHeader.dataSize);
LOGD(" nameLength=%d", flattenedHeader.nameLength);
#endif
if (flattenedHeader.dataSize < 0 || flattenedHeader.nameLength < 0 ||
remainingHeader < flattenedHeader.nameLength) {
LOGW("Malformed V1 header remainingHeader=%d dataSize=%d nameLength=%d", remainingHeader,
flattenedHeader.dataSize, flattenedHeader.nameLength);
return -1;
}
buf = keyPrefix.lockBuffer(flattenedHeader.nameLength);
if (buf == NULL) {
LOGW("unable to allocate %d bytes", flattenedHeader.nameLength);
return -1;
}
amt = read(fd, buf, flattenedHeader.nameLength);
buf[flattenedHeader.nameLength] = 0;
keyPrefix.unlockBuffer(flattenedHeader.nameLength);
remainingHeader -= flattenedHeader.nameLength;
if (remainingHeader > 0) {
lseek(fd, remainingHeader, SEEK_CUR);
}
env->SetIntField(headerObj, s_chunkSizeField, flattenedHeader.dataSize);
env->SetObjectField(headerObj, s_keyPrefixField, env->NewStringUTF(keyPrefix.string()));
return 0;
}
static int
skipChunk_native(JNIEnv* env, jobject clazz, jobject fdObj, jint bytesToSkip)
{
int fd;
fd = env->GetIntField(fdObj, s_descriptorField);
lseek(fd, bytesToSkip, SEEK_CUR);
return 0;
}
static int
padding_len(int len)
{
len = len % 4;
return len == 0 ? len : 4 - len;
}
static int
allocateHeader_native(JNIEnv* env, jobject clazz, jobject headerObj, jobject fdObj)
{
int pos;
jstring nameObj;
int nameLength;
int namePadding;
int headerSize;
int fd;
fd = env->GetIntField(fdObj, s_descriptorField);
nameObj = (jstring)env->GetObjectField(headerObj, s_keyPrefixField);
nameLength = env->GetStringUTFLength(nameObj);
namePadding = padding_len(nameLength);
headerSize = sizeof(chunk_header_v1) + nameLength + namePadding;
pos = lseek(fd, 0, SEEK_CUR);
lseek(fd, headerSize, SEEK_CUR);
return pos;
}
static int
writeHeader_native(JNIEnv* env, jobject clazz, jobject headerObj, jobject fdObj, jint pos)
{
int err;
chunk_header_v1 header;
int fd;
int namePadding;
int prevPos;
jstring nameObj;
const char* buf;
fd = env->GetIntField(fdObj, s_descriptorField);
prevPos = lseek(fd, 0, SEEK_CUR);
nameObj = (jstring)env->GetObjectField(headerObj, s_keyPrefixField);
header.nameLength = env->GetStringUTFLength(nameObj);
namePadding = padding_len(header.nameLength);
header.headerSize = sizeof(chunk_header_v1) + header.nameLength + namePadding;
header.version = VERSION_1_HEADER;
header.dataSize = prevPos - (pos + header.headerSize);
lseek(fd, pos, SEEK_SET);
err = write(fd, &header, sizeof(chunk_header_v1));
if (err != sizeof(chunk_header_v1)) {
return errno;
}
buf = env->GetStringUTFChars(nameObj, NULL);
err = write(fd, buf, header.nameLength);
env->ReleaseStringUTFChars(nameObj, buf);
if (err != header.nameLength) {
return errno;
}
if (namePadding != 0) {
int zero = 0;
err = write(fd, &zero, namePadding);
if (err != namePadding) {
return errno;
}
}
lseek(fd, prevPos, SEEK_SET);
return 0;
}
static const JNINativeMethod g_methods[] = {
{ "readHeader_native",
"(Landroid/backup/BackupHelperDispatcher$Header;Ljava/io/FileDescriptor;)I",
(void*)readHeader_native },
{ "skipChunk_native",
"(Ljava/io/FileDescriptor;I)I",
(void*)skipChunk_native },
{ "allocateHeader_native",
"(Landroid/backup/BackupHelperDispatcher$Header;Ljava/io/FileDescriptor;)I",
(void*)allocateHeader_native },
{ "writeHeader_native",
"(Landroid/backup/BackupHelperDispatcher$Header;Ljava/io/FileDescriptor;I)I",
(void*)writeHeader_native },
};
int register_android_backup_BackupHelperDispatcher(JNIEnv* env)
{
jclass clazz;
clazz = env->FindClass("java/io/FileDescriptor");
LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor");
s_descriptorField = env->GetFieldID(clazz, "descriptor", "I");
LOG_FATAL_IF(s_descriptorField == NULL,
"Unable to find descriptor field in java.io.FileDescriptor");
clazz = env->FindClass("android/backup/BackupHelperDispatcher$Header");
LOG_FATAL_IF(clazz == NULL,
"Unable to find class android.backup.BackupHelperDispatcher.Header");
s_chunkSizeField = env->GetFieldID(clazz, "chunkSize", "I");
LOG_FATAL_IF(s_chunkSizeField == NULL,
"Unable to find chunkSize field in android.backup.BackupHelperDispatcher.Header");
s_keyPrefixField = env->GetFieldID(clazz, "keyPrefix", "Ljava/lang/String;");
LOG_FATAL_IF(s_keyPrefixField == NULL,
"Unable to find keyPrefix field in android.backup.BackupHelperDispatcher.Header");
return AndroidRuntime::registerNativeMethods(env, "android/backup/BackupHelperDispatcher",
g_methods, NELEM(g_methods));
}
}
|