android_content_res_ObbScanner.cpp revision 17cc33a35729733aaa0a7706f38b1c45f0b1590a
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 <utils/ObbFile.h>
22
23#include "jni.h"
24#include "utils/misc.h"
25#include "android_runtime/AndroidRuntime.h"
26
27namespace android {
28
29static struct {
30    jclass clazz;
31
32    jfieldID packageName;
33    jfieldID version;
34    jfieldID flags;
35    jfieldID salt;
36} gObbInfoClassInfo;
37
38static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
39{
40    jclass npeClazz;
41
42    npeClazz = env->FindClass(exc);
43    LOG_FATAL_IF(npeClazz == NULL, "Unable to find class %s", exc);
44
45    env->ThrowNew(npeClazz, msg);
46}
47
48static void android_content_res_ObbScanner_getObbInfo(JNIEnv* env, jobject clazz, jstring file,
49        jobject obbInfo)
50{
51    const char* filePath = env->GetStringUTFChars(file, JNI_FALSE);
52
53    sp<ObbFile> obb = new ObbFile();
54    if (!obb->readFrom(filePath)) {
55        env->ReleaseStringUTFChars(file, filePath);
56        doThrow(env, "java/io/IOException", "Could not read OBB file");
57        return;
58    }
59
60    env->ReleaseStringUTFChars(file, filePath);
61
62    const char* packageNameStr = obb->getPackageName().string();
63
64    jstring packageName = env->NewStringUTF(packageNameStr);
65    if (packageName == NULL) {
66        doThrow(env, "java/io/IOException", "Could not read OBB file");
67        return;
68    }
69
70    env->SetObjectField(obbInfo, gObbInfoClassInfo.packageName, packageName);
71    env->SetIntField(obbInfo, gObbInfoClassInfo.version, obb->getVersion());
72    env->SetIntField(obbInfo, gObbInfoClassInfo.flags, obb->getFlags());
73
74    size_t saltLen;
75    const unsigned char* salt = obb->getSalt(&saltLen);
76    if (saltLen > 0) {
77        jbyteArray saltArray = env->NewByteArray(saltLen);
78        env->SetByteArrayRegion(saltArray, 0, saltLen, (jbyte*)salt);
79        env->SetObjectField(obbInfo, gObbInfoClassInfo.salt, saltArray);
80    }
81}
82
83/*
84 * JNI registration.
85 */
86static JNINativeMethod gMethods[] = {
87    /* name, signature, funcPtr */
88    { "getObbInfo_native", "(Ljava/lang/String;Landroid/content/res/ObbInfo;)V",
89            (void*) android_content_res_ObbScanner_getObbInfo },
90};
91
92#define FIND_CLASS(var, className) \
93        var = env->FindClass(className); \
94        LOG_FATAL_IF(! var, "Unable to find class " className);
95
96#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
97        var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
98        LOG_FATAL_IF(! var, "Unable to find field " fieldName);
99
100int register_android_content_res_ObbScanner(JNIEnv* env)
101{
102    jclass clazz;
103    FIND_CLASS(clazz, "android/content/res/ObbInfo");
104
105    GET_FIELD_ID(gObbInfoClassInfo.packageName, clazz,
106            "packageName", "Ljava/lang/String;");
107    GET_FIELD_ID(gObbInfoClassInfo.version, clazz,
108            "version", "I");
109    GET_FIELD_ID(gObbInfoClassInfo.flags, clazz,
110            "flags", "I");
111    GET_FIELD_ID(gObbInfoClassInfo.salt, clazz,
112            "salt", "[B");
113
114    return AndroidRuntime::registerNativeMethods(env, "android/content/res/ObbScanner", gMethods,
115            NELEM(gMethods));
116}
117
118}; // namespace android
119