19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* //device/libs/android_runtime/android_util_AssetManager.cpp
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** Copyright 2006, The Android Open Source Project
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** Licensed under the Apache License, Version 2.0 (the "License");
669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** you may not use this file except in compliance with the License.
769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** You may obtain a copy of the License at
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
969a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes**     http://www.apache.org/licenses/LICENSE-2.0
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project**
1169a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** Unless required by applicable law or agreed to in writing, software
1269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** distributed under the License is distributed on an "AS IS" BASIS,
1369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1469a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes** See the License for the specific language governing permissions and
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project** limitations under the License.
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project*/
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOG_TAG "asset"
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
20b8d81679553ee33f6ae5281310abf2effca4ffcdDianne Hackborn#define DEBUG_STYLES(x) //x
2120cb56e26e91df91bd64d4251222e0d421cdbe47Dianne Hackborn#define THROW_ON_BAD_ID 0
22b8d81679553ee33f6ae5281310abf2effca4ffcdDianne Hackborn
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <android_runtime/android_util_AssetManager.h>
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "jni.h"
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "JNIHelp.h"
2769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes#include "ScopedStringChars.h"
2869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes#include "ScopedUtfChars.h"
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_util_Binder.h"
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/misc.h>
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <android_runtime/AndroidRuntime.h>
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Log.h>
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
34b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/Asset.h>
35b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/AssetManager.h>
36a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski#include <androidfw/AttributeFinder.h>
37b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/ResourceTypes.h>
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3948d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad#include <private/android_filesystem_config.h> // for AID_SYSTEM
4048d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stdio.h>
4248d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad#include <sys/types.h>
4348d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad#include <sys/wait.h>
4448d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
4548d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad#include <linux/capability.h>
4648d22323ce39f9aab003dce74456889b6414af55Mårten Kongstadextern "C" int capget(cap_user_header_t hdrp, cap_user_data_t datap);
4748d22323ce39f9aab003dce74456889b6414af55Mårten Kongstadextern "C" int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
4848d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnamespace android {
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic struct typedvalue_offsets_t
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mType;
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mData;
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mString;
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mAssetCookie;
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mResourceId;
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mChangingConfigurations;
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mDensity;
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} gTypedValueOffsets;
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic struct assetfiledescriptor_offsets_t
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mFd;
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mStartOffset;
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mLength;
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} gAssetFileDescriptorOffsets;
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic struct assetmanager_offsets_t
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID mObject;
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} gAssetManagerOffsets;
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
77de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinskistatic struct sparsearray_offsets_t
78de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski{
79de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski    jclass classObject;
80de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski    jmethodID constructor;
81de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski    jmethodID put;
82de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski} gSparseArrayOffsets;
83de898ff42912bd7ca1bfb099cd439562496765a4Adam Lesinski
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjclass g_stringClass = NULL;
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectenum {
890d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn    STYLE_NUM_ENTRIES = 6,
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    STYLE_TYPE = 0,
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    STYLE_DATA = 1,
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    STYLE_ASSET_COOKIE = 2,
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    STYLE_RESOURCE_ID = 3,
940d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn    STYLE_CHANGING_CONFIGURATIONS = 4,
950d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn    STYLE_DENSITY = 5
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jint copyValue(JNIEnv* env, jobject outValue, const ResTable* table,
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                      const Res_value& value, uint32_t ref, ssize_t block,
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                      uint32_t typeSpecFlags, ResTable_config* config = NULL);
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectjint copyValue(JNIEnv* env, jobject outValue, const ResTable* table,
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project               const Res_value& value, uint32_t ref, ssize_t block,
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project               uint32_t typeSpecFlags, ResTable_config* config)
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(outValue, gTypedValueOffsets.mType, value.dataType);
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(outValue, gTypedValueOffsets.mAssetCookie,
108896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat                     static_cast<jint>(table->getTableCookie(block)));
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(outValue, gTypedValueOffsets.mData, value.data);
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetObjectField(outValue, gTypedValueOffsets.mString, NULL);
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(outValue, gTypedValueOffsets.mResourceId, ref);
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(outValue, gTypedValueOffsets.mChangingConfigurations,
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            typeSpecFlags);
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (config != NULL) {
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->SetIntField(outValue, gTypedValueOffsets.mDensity, config->density);
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return block;
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12048d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad// This is called by zygote (running as user root) as part of preloadResources.
12148d22323ce39f9aab003dce74456889b6414af55Mårten Kongstadstatic void verifySystemIdmaps()
12248d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad{
12348d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad    pid_t pid;
12448d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad    char system_id[10];
12548d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
12648d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad    snprintf(system_id, sizeof(system_id), "%d", AID_SYSTEM);
12748d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
12848d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad    switch (pid = fork()) {
12948d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad        case -1:
13048d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad            ALOGE("failed to fork for idmap: %s", strerror(errno));
13148d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad            break;
13248d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad        case 0: // child
13348d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad            {
13448d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                struct __user_cap_header_struct capheader;
13548d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                struct __user_cap_data_struct capdata;
13648d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
13748d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                memset(&capheader, 0, sizeof(capheader));
13848d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                memset(&capdata, 0, sizeof(capdata));
13948d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
14048d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                capheader.version = _LINUX_CAPABILITY_VERSION;
14148d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                capheader.pid = 0;
14248d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
14348d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                if (capget(&capheader, &capdata) != 0) {
14448d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                    ALOGE("capget: %s\n", strerror(errno));
14548d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                    exit(1);
14648d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                }
14748d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
14848d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                capdata.effective = capdata.permitted;
14948d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                if (capset(&capheader, &capdata) != 0) {
15048d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                    ALOGE("capset: %s\n", strerror(errno));
15148d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                    exit(1);
15248d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                }
15348d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
15448d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                if (setgid(AID_SYSTEM) != 0) {
15548d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                    ALOGE("setgid: %s\n", strerror(errno));
15648d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                    exit(1);
15748d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                }
15848d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
15948d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                if (setuid(AID_SYSTEM) != 0) {
16048d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                    ALOGE("setuid: %s\n", strerror(errno));
16148d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                    exit(1);
16248d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                }
16348d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
16448d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                execl(AssetManager::IDMAP_BIN, AssetManager::IDMAP_BIN, "--scan",
16548d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                        AssetManager::OVERLAY_DIR, AssetManager::TARGET_PACKAGE_NAME,
16648d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                        AssetManager::TARGET_APK_PATH, AssetManager::IDMAP_DIR, (char*)NULL);
16748d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                ALOGE("failed to execl for idmap: %s", strerror(errno));
16848d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad                exit(1); // should never get here
16948d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad            }
17048d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad            break;
17148d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad        default: // parent
17248d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad            waitpid(pid, NULL, 0);
17348d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad            break;
17448d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad    }
17548d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad}
17648d22323ce39f9aab003dce74456889b6414af55Mårten Kongstad
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// this guy is exported to other jni routines
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectAssetManager* assetManagerForJavaObject(JNIEnv* env, jobject obj)
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
182896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    jlong amHandle = env->GetLongField(obj, gAssetManagerOffsets.mObject);
183896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    AssetManager* am = reinterpret_cast<AssetManager*>(amHandle);
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (am != NULL) {
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return am;
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jniThrowException(env, "java/lang/IllegalStateException", "AssetManager has been finalized!");
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NULL;
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
191896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhatstatic jlong android_content_AssetManager_openAsset(JNIEnv* env, jobject clazz,
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                jstring fileName, jint mode)
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    AssetManager* am = assetManagerForJavaObject(env, clazz);
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (am == NULL) {
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19971f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("openAsset in %p (Java object %p)\n", am, clazz);
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
20169a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes    ScopedUtfChars fileName8(env, fileName);
20269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes    if (fileName8.c_str() == NULL) {
203896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat        jniThrowException(env, "java/lang/IllegalArgumentException", "Empty file name");
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (mode != Asset::ACCESS_UNKNOWN && mode != Asset::ACCESS_RANDOM
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        && mode != Asset::ACCESS_STREAMING && mode != Asset::ACCESS_BUFFER) {
2094d4040b7b3c46ea29a42878c14b325f09f0029adGilles Debunne        jniThrowException(env, "java/lang/IllegalArgumentException", "Bad access mode");
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
21369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes    Asset* a = am->open(fileName8.c_str(), (Asset::AccessMode)mode);
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (a == NULL) {
21669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str());
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //printf("Created Asset Stream: %p\n", a);
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
222896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhat    return reinterpret_cast<jlong>(a);
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject returnParcelFileDescriptor(JNIEnv* env, Asset* a, jlongArray outOffsets)
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
227ddb76c4644756b31be948d70aaa8ee541dd94999Kenny Root    off64_t startOffset, length;
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int fd = a->openFileDescriptor(&startOffset, &length);
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    delete a;
23069a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fd < 0) {
2324d4040b7b3c46ea29a42878c14b325f09f0029adGilles Debunne        jniThrowException(env, "java/io/FileNotFoundException",
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                "This file can not be opened as a file descriptor; it is probably compressed");
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
23669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jlong* offsets = (jlong*)env->GetPrimitiveArrayCritical(outOffsets, 0);
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (offsets == NULL) {
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        close(fd);
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
24269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    offsets[0] = startOffset;
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    offsets[1] = length;
24569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->ReleasePrimitiveArrayCritical(outOffsets, offsets, 0);
24769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
248a3804cf77f0edd93f6247a055cdafb856b117eecElliott Hughes    jobject fileDesc = jniCreateFileDescriptor(env, fd);
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (fileDesc == NULL) {
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        close(fd);
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
25369a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return newParcelFileDescriptor(env, fileDesc);
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject android_content_AssetManager_openAssetFd(JNIEnv* env, jobject clazz,
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                jstring fileName, jlongArray outOffsets)
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    AssetManager* am = assetManagerForJavaObject(env, clazz);
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (am == NULL) {
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
26571f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("openAssetFd in %p (Java object %p)\n", am, clazz);
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
26769a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes    ScopedUtfChars fileName8(env, fileName);
26869a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes    if (fileName8.c_str() == NULL) {
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
27269a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes    Asset* a = am->open(fileName8.c_str(), Asset::ACCESS_RANDOM);
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (a == NULL) {
27569a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes        jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str());
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //printf("Created Asset Stream: %p\n", a);
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return returnParcelFileDescriptor(env, a, outOffsets);
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
284896043d67d3ac75760bd99db8a1561e31ebee1e1Ashok Bhatstatic jlong android_content_AssetManager_openNonAssetNative(JNIEnv* env, jobject clazz,
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                         jint cookie,
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                         jstring fileName,
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                         jint mode)
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    AssetManager* am = assetManagerForJavaObject(env, clazz);
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (am == NULL) {
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
29471f2cf116aab893e224056c38ab146bd1538dd3eSteve Block    ALOGV("openNonAssetNative in %p (Java object %p)\n", am, clazz);
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
29669a017bc1d1649350f830dfada5c6ed5eac0b770Elliott Hughes    ScopedUtfChars