blob: de429a07d0f68297a7ffd25f1dceb3fe2f2d3ad1 [file] [log] [blame]
Kenny Root02c87302010-07-01 08:10:18 -07001/*
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>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080021#include <androidfw/ObbFile.h>
Kenny Root02c87302010-07-01 08:10:18 -070022
23#include "jni.h"
Steven Moreland2279b252017-07-19 09:50:45 -070024#include <nativehelper/JNIHelp.h>
Kenny Root02c87302010-07-01 08:10:18 -070025#include "utils/misc.h"
26#include "android_runtime/AndroidRuntime.h"
27
Andreas Gampe987f79f2014-11-18 17:29:46 -080028#include "core_jni_helpers.h"
29
Kenny Root02c87302010-07-01 08:10:18 -070030namespace android {
31
32static struct {
33 jclass clazz;
34
35 jfieldID packageName;
36 jfieldID version;
Kenny Root02ca31f2010-08-12 07:36:02 -070037 jfieldID flags;
Kenny Root3b1abba2010-10-13 15:00:07 -070038 jfieldID salt;
Kenny Root02c87302010-07-01 08:10:18 -070039} gObbInfoClassInfo;
40
Kenny Root05105f72010-09-22 17:29:43 -070041static void android_content_res_ObbScanner_getObbInfo(JNIEnv* env, jobject clazz, jstring file,
Kenny Root02c87302010-07-01 08:10:18 -070042 jobject obbInfo)
43{
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070044 const char* filePath = env->GetStringUTFChars(file, NULL);
Kenny Root02c87302010-07-01 08:10:18 -070045
46 sp<ObbFile> obb = new ObbFile();
47 if (!obb->readFrom(filePath)) {
48 env->ReleaseStringUTFChars(file, filePath);
Elliott Hughes8451b252011-04-07 19:17:57 -070049 jniThrowException(env, "java/io/IOException", "Could not read OBB file");
Kenny Root05105f72010-09-22 17:29:43 -070050 return;
Kenny Root02c87302010-07-01 08:10:18 -070051 }
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) {
Elliott Hughes8451b252011-04-07 19:17:57 -070059 jniThrowException(env, "java/io/IOException", "Could not read OBB file");
Kenny Root05105f72010-09-22 17:29:43 -070060 return;
Kenny Root02c87302010-07-01 08:10:18 -070061 }
62
63 env->SetObjectField(obbInfo, gObbInfoClassInfo.packageName, packageName);
64 env->SetIntField(obbInfo, gObbInfoClassInfo.version, obb->getVersion());
Kenny Root05105f72010-09-22 17:29:43 -070065 env->SetIntField(obbInfo, gObbInfoClassInfo.flags, obb->getFlags());
Kenny Root3b1abba2010-10-13 15:00:07 -070066
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 }
Kenny Root02c87302010-07-01 08:10:18 -070074}
75
76/*
77 * JNI registration.
78 */
Daniel Micay76f6a862015-09-19 17:31:01 -040079static const JNINativeMethod gMethods[] = {
Kenny Root02c87302010-07-01 08:10:18 -070080 /* name, signature, funcPtr */
Kenny Root05105f72010-09-22 17:29:43 -070081 { "getObbInfo_native", "(Ljava/lang/String;Landroid/content/res/ObbInfo;)V",
Kenny Root02c87302010-07-01 08:10:18 -070082 (void*) android_content_res_ObbScanner_getObbInfo },
83};
84
Kenny Root02c87302010-07-01 08:10:18 -070085int register_android_content_res_ObbScanner(JNIEnv* env)
86{
Andreas Gampe987f79f2014-11-18 17:29:46 -080087 jclass clazz = FindClassOrDie(env, "android/content/res/ObbInfo");
Kenny Root02c87302010-07-01 08:10:18 -070088
Andreas Gampe987f79f2014-11-18 17:29:46 -080089 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");
Kenny Root02c87302010-07-01 08:10:18 -070094
Andreas Gampe987f79f2014-11-18 17:29:46 -080095 return RegisterMethodsOrDie(env, "android/content/res/ObbScanner", gMethods, NELEM(gMethods));
Kenny Root02c87302010-07-01 08:10:18 -070096}
97
98}; // namespace android