blob: 883753843233aff9118884f7f8c5c5e952e665f3 [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>
21#include <utils/ObbFile.h>
22
23#include "jni.h"
Elliott Hughes8451b252011-04-07 19:17:57 -070024#include "JNIHelp.h"
Kenny Root02c87302010-07-01 08:10:18 -070025#include "utils/misc.h"
26#include "android_runtime/AndroidRuntime.h"
27
28namespace android {
29
30static struct {
31 jclass clazz;
32
33 jfieldID packageName;
34 jfieldID version;
Kenny Root02ca31f2010-08-12 07:36:02 -070035 jfieldID flags;
Kenny Root3b1abba2010-10-13 15:00:07 -070036 jfieldID salt;
Kenny Root02c87302010-07-01 08:10:18 -070037} gObbInfoClassInfo;
38
Kenny Root05105f72010-09-22 17:29:43 -070039static void android_content_res_ObbScanner_getObbInfo(JNIEnv* env, jobject clazz, jstring file,
Kenny Root02c87302010-07-01 08:10:18 -070040 jobject obbInfo)
41{
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070042 const char* filePath = env->GetStringUTFChars(file, NULL);
Kenny Root02c87302010-07-01 08:10:18 -070043
44 sp<ObbFile> obb = new ObbFile();
45 if (!obb->readFrom(filePath)) {
46 env->ReleaseStringUTFChars(file, filePath);
Elliott Hughes8451b252011-04-07 19:17:57 -070047 jniThrowException(env, "java/io/IOException", "Could not read OBB file");
Kenny Root05105f72010-09-22 17:29:43 -070048 return;
Kenny Root02c87302010-07-01 08:10:18 -070049 }
50
51 env->ReleaseStringUTFChars(file, filePath);
52
53 const char* packageNameStr = obb->getPackageName().string();
54
55 jstring packageName = env->NewStringUTF(packageNameStr);
56 if (packageName == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -070057 jniThrowException(env, "java/io/IOException", "Could not read OBB file");
Kenny Root05105f72010-09-22 17:29:43 -070058 return;
Kenny Root02c87302010-07-01 08:10:18 -070059 }
60
61 env->SetObjectField(obbInfo, gObbInfoClassInfo.packageName, packageName);
62 env->SetIntField(obbInfo, gObbInfoClassInfo.version, obb->getVersion());
Kenny Root05105f72010-09-22 17:29:43 -070063 env->SetIntField(obbInfo, gObbInfoClassInfo.flags, obb->getFlags());
Kenny Root3b1abba2010-10-13 15:00:07 -070064
65 size_t saltLen;
66 const unsigned char* salt = obb->getSalt(&saltLen);
67 if (saltLen > 0) {
68 jbyteArray saltArray = env->NewByteArray(saltLen);
69 env->SetByteArrayRegion(saltArray, 0, saltLen, (jbyte*)salt);
70 env->SetObjectField(obbInfo, gObbInfoClassInfo.salt, saltArray);
71 }
Kenny Root02c87302010-07-01 08:10:18 -070072}
73
74/*
75 * JNI registration.
76 */
77static JNINativeMethod gMethods[] = {
78 /* name, signature, funcPtr */
Kenny Root05105f72010-09-22 17:29:43 -070079 { "getObbInfo_native", "(Ljava/lang/String;Landroid/content/res/ObbInfo;)V",
Kenny Root02c87302010-07-01 08:10:18 -070080 (void*) android_content_res_ObbScanner_getObbInfo },
81};
82
83#define FIND_CLASS(var, className) \
84 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -080085 LOG_FATAL_IF(! var, "Unable to find class " className);
Kenny Root02c87302010-07-01 08:10:18 -070086
87#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
88 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
89 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
90
91int register_android_content_res_ObbScanner(JNIEnv* env)
92{
Carl Shapiro17cc33a2011-03-05 20:53:16 -080093 jclass clazz;
94 FIND_CLASS(clazz, "android/content/res/ObbInfo");
Kenny Root02c87302010-07-01 08:10:18 -070095
Carl Shapiro17cc33a2011-03-05 20:53:16 -080096 GET_FIELD_ID(gObbInfoClassInfo.packageName, clazz,
Kenny Root02c87302010-07-01 08:10:18 -070097 "packageName", "Ljava/lang/String;");
Carl Shapiro17cc33a2011-03-05 20:53:16 -080098 GET_FIELD_ID(gObbInfoClassInfo.version, clazz,
Kenny Root02c87302010-07-01 08:10:18 -070099 "version", "I");
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800100 GET_FIELD_ID(gObbInfoClassInfo.flags, clazz,
Kenny Root02ca31f2010-08-12 07:36:02 -0700101 "flags", "I");
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800102 GET_FIELD_ID(gObbInfoClassInfo.salt, clazz,
Kenny Root3b1abba2010-10-13 15:00:07 -0700103 "salt", "[B");
Kenny Root02c87302010-07-01 08:10:18 -0700104
105 return AndroidRuntime::registerNativeMethods(env, "android/content/res/ObbScanner", gMethods,
106 NELEM(gMethods));
107}
108
109}; // namespace android