Home | History | Annotate | Download | only in jni
      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 "JNIHelp.h"
     25 #include "utils/misc.h"
     26 #include "android_runtime/AndroidRuntime.h"
     27 
     28 namespace android {
     29 
     30 static struct {
     31     jclass clazz;
     32 
     33     jfieldID packageName;
     34     jfieldID version;
     35     jfieldID flags;
     36     jfieldID salt;
     37 } gObbInfoClassInfo;
     38 
     39 static void android_content_res_ObbScanner_getObbInfo(JNIEnv* env, jobject clazz, jstring file,
     40         jobject obbInfo)
     41 {
     42     const char* filePath = env->GetStringUTFChars(file, NULL);
     43 
     44     sp<ObbFile> obb = new ObbFile();
     45     if (!obb->readFrom(filePath)) {
     46         env->ReleaseStringUTFChars(file, filePath);
     47         jniThrowException(env, "java/io/IOException", "Could not read OBB file");
     48         return;
     49     }
     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) {
     57         jniThrowException(env, "java/io/IOException", "Could not read OBB file");
     58         return;
     59     }
     60 
     61     env->SetObjectField(obbInfo, gObbInfoClassInfo.packageName, packageName);
     62     env->SetIntField(obbInfo, gObbInfoClassInfo.version, obb->getVersion());
     63     env->SetIntField(obbInfo, gObbInfoClassInfo.flags, obb->getFlags());
     64 
     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     }
     72 }
     73 
     74 /*
     75  * JNI registration.
     76  */
     77 static JNINativeMethod gMethods[] = {
     78     /* name, signature, funcPtr */
     79     { "getObbInfo_native", "(Ljava/lang/String;Landroid/content/res/ObbInfo;)V",
     80             (void*) android_content_res_ObbScanner_getObbInfo },
     81 };
     82 
     83 #define FIND_CLASS(var, className) \
     84         var = env->FindClass(className); \
     85         LOG_FATAL_IF(! var, "Unable to find class " className);
     86 
     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 
     91 int register_android_content_res_ObbScanner(JNIEnv* env)
     92 {
     93     jclass clazz;
     94     FIND_CLASS(clazz, "android/content/res/ObbInfo");
     95 
     96     GET_FIELD_ID(gObbInfoClassInfo.packageName, clazz,
     97             "packageName", "Ljava/lang/String;");
     98     GET_FIELD_ID(gObbInfoClassInfo.version, clazz,
     99             "version", "I");
    100     GET_FIELD_ID(gObbInfoClassInfo.flags, clazz,
    101             "flags", "I");
    102     GET_FIELD_ID(gObbInfoClassInfo.salt, clazz,
    103             "salt", "[B");
    104 
    105     return AndroidRuntime::registerNativeMethods(env, "android/content/res/ObbScanner", gMethods,
    106             NELEM(gMethods));
    107 }
    108 
    109 }; // namespace android
    110