Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 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" |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 24 | #include "JNIHelp.h" |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 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; |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 35 | jfieldID flags; |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 36 | jfieldID salt; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 37 | } gObbInfoClassInfo; |
| 38 | |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 39 | static void android_content_res_ObbScanner_getObbInfo(JNIEnv* env, jobject clazz, jstring file, |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 40 | jobject obbInfo) |
| 41 | { |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 42 | const char* filePath = env->GetStringUTFChars(file, NULL); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 43 | |
| 44 | sp<ObbFile> obb = new ObbFile(); |
| 45 | if (!obb->readFrom(filePath)) { |
| 46 | env->ReleaseStringUTFChars(file, filePath); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 47 | jniThrowException(env, "java/io/IOException", "Could not read OBB file"); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 48 | return; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 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) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 57 | jniThrowException(env, "java/io/IOException", "Could not read OBB file"); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 58 | return; |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | env->SetObjectField(obbInfo, gObbInfoClassInfo.packageName, packageName); |
| 62 | env->SetIntField(obbInfo, gObbInfoClassInfo.version, obb->getVersion()); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 63 | env->SetIntField(obbInfo, gObbInfoClassInfo.flags, obb->getFlags()); |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 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 | } |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* |
| 75 | * JNI registration. |
| 76 | */ |
| 77 | static JNINativeMethod gMethods[] = { |
| 78 | /* name, signature, funcPtr */ |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 79 | { "getObbInfo_native", "(Ljava/lang/String;Landroid/content/res/ObbInfo;)V", |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 80 | (void*) android_content_res_ObbScanner_getObbInfo }, |
| 81 | }; |
| 82 | |
| 83 | #define FIND_CLASS(var, className) \ |
| 84 | var = env->FindClass(className); \ |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 85 | LOG_FATAL_IF(! var, "Unable to find class " className); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 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 | { |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 93 | jclass clazz; |
| 94 | FIND_CLASS(clazz, "android/content/res/ObbInfo"); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 95 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 96 | GET_FIELD_ID(gObbInfoClassInfo.packageName, clazz, |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 97 | "packageName", "Ljava/lang/String;"); |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 98 | GET_FIELD_ID(gObbInfoClassInfo.version, clazz, |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 99 | "version", "I"); |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 100 | GET_FIELD_ID(gObbInfoClassInfo.flags, clazz, |
Kenny Root | 02ca31f | 2010-08-12 07:36:02 -0700 | [diff] [blame] | 101 | "flags", "I"); |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 102 | GET_FIELD_ID(gObbInfoClassInfo.salt, clazz, |
Kenny Root | 3b1abba | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 103 | "salt", "[B"); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 104 | |
| 105 | return AndroidRuntime::registerNativeMethods(env, "android/content/res/ObbScanner", gMethods, |
| 106 | NELEM(gMethods)); |
| 107 | } |
| 108 | |
| 109 | }; // namespace android |