1 /*
2  * Copyright 2010, 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 #define LOG_TAG "ObbScanner"
18 
19 #include <utils/Log.h>
20 #include <utils/String8.h>
21 #include <androidfw/ObbFile.h>
22 
23 #include "jni.h"
24 #include "JNIHelp.h"
25 #include "utils/misc.h"
26 #include "android_runtime/AndroidRuntime.h"
27 
28 #include "core_jni_helpers.h"
29 
30 namespace android {
31 
32 static struct {
33     jclass clazz;
34 
35     jfieldID packageName;
36     jfieldID version;
37     jfieldID flags;
38     jfieldID salt;
39 } gObbInfoClassInfo;
40 
android_content_res_ObbScanner_getObbInfo(JNIEnv * env,jobject clazz,jstring file,jobject obbInfo)41 static void android_content_res_ObbScanner_getObbInfo(JNIEnv* env, jobject clazz, jstring file,
42         jobject obbInfo)
43 {
44     const char* filePath = env->GetStringUTFChars(file, NULL);
45 
46     sp<ObbFile> obb = new ObbFile();
47     if (!obb->readFrom(filePath)) {
48         env->ReleaseStringUTFChars(file, filePath);
49         jniThrowException(env, "java/io/IOException", "Could not read OBB file");
50         return;
51     }
52 
53     env->ReleaseStringUTFChars(file, filePath);
54 
55     const char* packageNameStr = obb->getPackageName().string();
56 
57     jstring packageName = env->NewStringUTF(packageNameStr);
58     if (packageName == NULL) {
59         jniThrowException(env, "java/io/IOException", "Could not read OBB file");
60         return;
61     }
62 
63     env->SetObjectField(obbInfo, gObbInfoClassInfo.packageName, packageName);
64     env->SetIntField(obbInfo, gObbInfoClassInfo.version, obb->getVersion());
65     env->SetIntField(obbInfo, gObbInfoClassInfo.flags, obb->getFlags());
66 
67     size_t saltLen;
68     const unsigned char* salt = obb->getSalt(&saltLen);
69     if (saltLen > 0) {
70         jbyteArray saltArray = env->NewByteArray(saltLen);
71         env->SetByteArrayRegion(saltArray, 0, saltLen, (jbyte*)salt);
72         env->SetObjectField(obbInfo, gObbInfoClassInfo.salt, saltArray);
73     }
74 }
75 
76 /*
77  * JNI registration.
78  */
79 static JNINativeMethod gMethods[] = {
80     /* name, signature, funcPtr */
81     { "getObbInfo_native", "(Ljava/lang/String;Landroid/content/res/ObbInfo;)V",
82             (void*) android_content_res_ObbScanner_getObbInfo },
83 };
84 
register_android_content_res_ObbScanner(JNIEnv * env)85 int register_android_content_res_ObbScanner(JNIEnv* env)
86 {
87     jclass clazz = FindClassOrDie(env, "android/content/res/ObbInfo");
88 
89     gObbInfoClassInfo.packageName = GetFieldIDOrDie(env, clazz, "packageName",
90                                                     "Ljava/lang/String;");
91     gObbInfoClassInfo.version = GetFieldIDOrDie(env, clazz, "version", "I");
92     gObbInfoClassInfo.flags = GetFieldIDOrDie(env, clazz, "flags", "I");
93     gObbInfoClassInfo.salt = GetFieldIDOrDie(env, clazz, "salt", "[B");
94 
95     return RegisterMethodsOrDie(env, "android/content/res/ObbScanner", gMethods, NELEM(gMethods));
96 }
97 
98 }; // namespace android
99