116c4d154dca43c662571129af31b27433b919a32Adam Lesinski/* 216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Copyright (C) 2006 The Android Open Source Project 316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); 516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * you may not use this file except in compliance with the License. 616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * You may obtain a copy of the License at 716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 1016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Unless required by applicable law or agreed to in writing, software 1116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, 1216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * See the License for the specific language governing permissions and 1416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * limitations under the License. 1516c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 1616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 1716c4d154dca43c662571129af31b27433b919a32Adam Lesinski// 1816c4d154dca43c662571129af31b27433b919a32Adam Lesinski// Class providing access to a read-only asset. Asset objects are NOT 1916c4d154dca43c662571129af31b27433b919a32Adam Lesinski// thread-safe, and should not be shared across threads. 2016c4d154dca43c662571129af31b27433b919a32Adam Lesinski// 2116c4d154dca43c662571129af31b27433b919a32Adam Lesinski#ifndef __LIBS_ASSET_H 2216c4d154dca43c662571129af31b27433b919a32Adam Lesinski#define __LIBS_ASSET_H 2316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 2416c4d154dca43c662571129af31b27433b919a32Adam Lesinski#include <stdio.h> 2516c4d154dca43c662571129af31b27433b919a32Adam Lesinski#include <sys/types.h> 2616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 27d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski#include <memory> 28d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski 2916c4d154dca43c662571129af31b27433b919a32Adam Lesinski#include <utils/Compat.h> 3016c4d154dca43c662571129af31b27433b919a32Adam Lesinski#include <utils/Errors.h> 3116c4d154dca43c662571129af31b27433b919a32Adam Lesinski#include <utils/String8.h> 3216c4d154dca43c662571129af31b27433b919a32Adam Lesinski 3316c4d154dca43c662571129af31b27433b919a32Adam Lesinskinamespace android { 3416c4d154dca43c662571129af31b27433b919a32Adam Lesinski 35ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinskiclass FileMap; 36ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski 3716c4d154dca43c662571129af31b27433b919a32Adam Lesinski/* 3816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Instances of this class provide read-only operations on a byte stream. 3916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 4016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Access may be optimized for streaming, random, or whole buffer modes. All 4116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * operations are supported regardless of how the file was opened, but some 4216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * things will be less efficient. [pass that in??] 4316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 4416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * "Asset" is the base class for all types of assets. The classes below 4516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * provide most of the implementation. The AssetManager uses one of the 4616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * static "create" functions defined here to create a new instance. 4716c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 4816c4d154dca43c662571129af31b27433b919a32Adam Lesinskiclass Asset { 4916c4d154dca43c662571129af31b27433b919a32Adam Lesinskipublic: 500358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski virtual ~Asset(void) = default; 5116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 5216c4d154dca43c662571129af31b27433b919a32Adam Lesinski static int32_t getGlobalCount(); 5316c4d154dca43c662571129af31b27433b919a32Adam Lesinski static String8 getAssetAllocations(); 54f6113af2d6f6eebee68d3ac510fe96d38a7a39e9John Reck 5516c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* used when opening an asset */ 5616c4d154dca43c662571129af31b27433b919a32Adam Lesinski typedef enum AccessMode { 5716c4d154dca43c662571129af31b27433b919a32Adam Lesinski ACCESS_UNKNOWN = 0, 5816c4d154dca43c662571129af31b27433b919a32Adam Lesinski 5916c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* read chunks, and seek forward and backward */ 6016c4d154dca43c662571129af31b27433b919a32Adam Lesinski ACCESS_RANDOM, 6116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 6216c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* read sequentially, with an occasional forward seek */ 6316c4d154dca43c662571129af31b27433b919a32Adam Lesinski ACCESS_STREAMING, 6416c4d154dca43c662571129af31b27433b919a32Adam Lesinski 6516c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* caller plans to ask for a read-only buffer with all data */ 6616c4d154dca43c662571129af31b27433b919a32Adam Lesinski ACCESS_BUFFER, 6716c4d154dca43c662571129af31b27433b919a32Adam Lesinski } AccessMode; 6816c4d154dca43c662571129af31b27433b919a32Adam Lesinski 6916c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 7016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Read data from the current offset. Returns the actual number of 7116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * bytes read, 0 on EOF, or -1 on error. 7216c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 7316c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual ssize_t read(void* buf, size_t count) = 0; 7416c4d154dca43c662571129af31b27433b919a32Adam Lesinski 7516c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 7616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Seek to the specified offset. "whence" uses the same values as 7716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * lseek/fseek. Returns the new position on success, or (off64_t) -1 7816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * on failure. 7916c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 8016c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t seek(off64_t offset, int whence) = 0; 8116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 8216c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 8316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Close the asset, freeing all associated resources. 8416c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 8516c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual void close(void) = 0; 8616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 8716c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 8816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Get a pointer to a buffer with the entire contents of the file. 8916c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 9016c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual const void* getBuffer(bool wordAligned) = 0; 9116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 9216c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 9316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Get the total amount of data that can be read. 9416c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 9516c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getLength(void) const = 0; 9616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 9716c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 9816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Get the total amount of data that can be read from the current position. 9916c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 10016c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getRemainingLength(void) const = 0; 10116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 10216c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 10316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Open a new file descriptor that can be used to read this asset. 10416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Returns -1 if you can not use the file descriptor (for example if the 10516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * asset is compressed). 10616c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 10716c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const = 0; 10816c4d154dca43c662571129af31b27433b919a32Adam Lesinski 10916c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 11016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Return whether this asset's buffer is allocated in RAM (not mmapped). 11116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Note: not virtual so it is safe to call even when being destroyed. 11216c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 11316c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual bool isAllocated(void) const { return false; } 11416c4d154dca43c662571129af31b27433b919a32Adam Lesinski 11516c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 11616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Get a string identifying the asset's source. This might be a full 11716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * path, it might be a colon-separated list of identifiers. 11816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 11916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * This is NOT intended to be used for anything except debug output. 12016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * DO NOT try to parse this or use it to open a file. 12116c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 12216c4d154dca43c662571129af31b27433b919a32Adam Lesinski const char* getAssetSource(void) const { return mAssetSource.string(); } 12316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 12416c4d154dca43c662571129af31b27433b919a32Adam Lesinskiprotected: 1250358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski /* 1260358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski * Adds this Asset to the global Asset list for debugging and 1270358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski * accounting. 1280358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski * Concrete subclasses must call this in their constructor. 1290358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski */ 1300358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski static void registerAsset(Asset* asset); 1310358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski 1320358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski /* 1330358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski * Removes this Asset from the global Asset list. 1340358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski * Concrete subclasses must call this in their destructor. 1350358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski */ 1360358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski static void unregisterAsset(Asset* asset); 1370358efe4f76f42d9eea91600202a5ab0831d9cefAdam Lesinski 13816c4d154dca43c662571129af31b27433b919a32Adam Lesinski Asset(void); // constructor; only invoked indirectly 13916c4d154dca43c662571129af31b27433b919a32Adam Lesinski 14016c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* handle common seek() housekeeping */ 14116c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t handleSeek(off64_t offset, int whence, off64_t curPosn, off64_t maxPosn); 14216c4d154dca43c662571129af31b27433b919a32Adam Lesinski 14316c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* set the asset source string */ 14416c4d154dca43c662571129af31b27433b919a32Adam Lesinski void setAssetSource(const String8& path) { mAssetSource = path; } 14516c4d154dca43c662571129af31b27433b919a32Adam Lesinski 14616c4d154dca43c662571129af31b27433b919a32Adam Lesinski AccessMode getAccessMode(void) const { return mAccessMode; } 14716c4d154dca43c662571129af31b27433b919a32Adam Lesinski 14816c4d154dca43c662571129af31b27433b919a32Adam Lesinskiprivate: 14916c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* these operations are not implemented */ 15016c4d154dca43c662571129af31b27433b919a32Adam Lesinski Asset(const Asset& src); 15116c4d154dca43c662571129af31b27433b919a32Adam Lesinski Asset& operator=(const Asset& src); 15216c4d154dca43c662571129af31b27433b919a32Adam Lesinski 15316c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* AssetManager needs access to our "create" functions */ 15416c4d154dca43c662571129af31b27433b919a32Adam Lesinski friend class AssetManager; 155d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski friend class ApkAssets; 15616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 15716c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 15816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create the asset from a named file on disk. 15916c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 16016c4d154dca43c662571129af31b27433b919a32Adam Lesinski static Asset* createFromFile(const char* fileName, AccessMode mode); 16116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 16216c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 16316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create the asset from a named, compressed file on disk (e.g. ".gz"). 16416c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 16516c4d154dca43c662571129af31b27433b919a32Adam Lesinski static Asset* createFromCompressedFile(const char* fileName, 16616c4d154dca43c662571129af31b27433b919a32Adam Lesinski AccessMode mode); 16716c4d154dca43c662571129af31b27433b919a32Adam Lesinski 16816c4d154dca43c662571129af31b27433b919a32Adam Lesinski#if 0 16916c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 17016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create the asset from a segment of an open file. This will fail 17116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * if "offset" and "length" don't fit within the bounds of the file. 17216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 17316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * The asset takes ownership of the file descriptor. 17416c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 17516c4d154dca43c662571129af31b27433b919a32Adam Lesinski static Asset* createFromFileSegment(int fd, off64_t offset, size_t length, 17616c4d154dca43c662571129af31b27433b919a32Adam Lesinski AccessMode mode); 17716c4d154dca43c662571129af31b27433b919a32Adam Lesinski 17816c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 17916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create from compressed data. "fd" should be seeked to the start of 18016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * the compressed data. This could be inside a gzip file or part of a 18116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Zip archive. 18216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 18316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * The asset takes ownership of the file descriptor. 18416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 18516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * This may not verify the validity of the compressed data until first 18616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * use. 18716c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 18816c4d154dca43c662571129af31b27433b919a32Adam Lesinski static Asset* createFromCompressedData(int fd, off64_t offset, 18916c4d154dca43c662571129af31b27433b919a32Adam Lesinski int compressionMethod, size_t compressedLength, 19016c4d154dca43c662571129af31b27433b919a32Adam Lesinski size_t uncompressedLength, AccessMode mode); 19116c4d154dca43c662571129af31b27433b919a32Adam Lesinski#endif 19216c4d154dca43c662571129af31b27433b919a32Adam Lesinski 19316c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 19416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create the asset from a memory-mapped file segment. 19516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 19616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * The asset takes ownership of the FileMap. 19716c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 19816c4d154dca43c662571129af31b27433b919a32Adam Lesinski static Asset* createFromUncompressedMap(FileMap* dataMap, AccessMode mode); 19916c4d154dca43c662571129af31b27433b919a32Adam Lesinski 200d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski static std::unique_ptr<Asset> createFromUncompressedMap(std::unique_ptr<FileMap> dataMap, 201d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski AccessMode mode); 202d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski 20316c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 20416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create the asset from a memory-mapped file segment with compressed 2054600dd053dbdbd4b95f3b11057a1cc55b99f9c77Narayan Kamath * data. 20616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 20716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * The asset takes ownership of the FileMap. 20816c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 2094600dd053dbdbd4b95f3b11057a1cc55b99f9c77Narayan Kamath static Asset* createFromCompressedMap(FileMap* dataMap, 21016c4d154dca43c662571129af31b27433b919a32Adam Lesinski size_t uncompressedLen, AccessMode mode); 21116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 212d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski static std::unique_ptr<Asset> createFromCompressedMap(std::unique_ptr<FileMap> dataMap, 213d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski size_t uncompressedLen, AccessMode mode); 214d1ecd7af687bcad0f87c37fe33515ff6c5ea0f1dAdam Lesinski 21516c4d154dca43c662571129af31b27433b919a32Adam Lesinski 21616c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 21716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create from a reference-counted chunk of shared memory. 21816c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 21916c4d154dca43c662571129af31b27433b919a32Adam Lesinski // TODO 22016c4d154dca43c662571129af31b27433b919a32Adam Lesinski 22116c4d154dca43c662571129af31b27433b919a32Adam Lesinski AccessMode mAccessMode; // how the asset was opened 22216c4d154dca43c662571129af31b27433b919a32Adam Lesinski String8 mAssetSource; // debug string 223f6113af2d6f6eebee68d3ac510fe96d38a7a39e9John Reck 22416c4d154dca43c662571129af31b27433b919a32Adam Lesinski Asset* mNext; // linked list. 22516c4d154dca43c662571129af31b27433b919a32Adam Lesinski Asset* mPrev; 22616c4d154dca43c662571129af31b27433b919a32Adam Lesinski}; 22716c4d154dca43c662571129af31b27433b919a32Adam Lesinski 22816c4d154dca43c662571129af31b27433b919a32Adam Lesinski 22916c4d154dca43c662571129af31b27433b919a32Adam Lesinski/* 23016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * =========================================================================== 23116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 23216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Innards follow. Do not use these classes directly. 23316c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 23416c4d154dca43c662571129af31b27433b919a32Adam Lesinski 23516c4d154dca43c662571129af31b27433b919a32Adam Lesinski/* 23616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * An asset based on an uncompressed file on disk. It may encompass the 23716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * entire file or just a piece of it. Access is through fread/fseek. 23816c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 23916c4d154dca43c662571129af31b27433b919a32Adam Lesinskiclass _FileAsset : public Asset { 24016c4d154dca43c662571129af31b27433b919a32Adam Lesinskipublic: 24116c4d154dca43c662571129af31b27433b919a32Adam Lesinski _FileAsset(void); 24216c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual ~_FileAsset(void); 24316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 24416c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 24516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Use a piece of an already-open file. 24616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 24716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * On success, the object takes ownership of "fd". 24816c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 24916c4d154dca43c662571129af31b27433b919a32Adam Lesinski status_t openChunk(const char* fileName, int fd, off64_t offset, size_t length); 25016c4d154dca43c662571129af31b27433b919a32Adam Lesinski 25116c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 25216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Use a memory-mapped region. 25316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 25416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * On success, the object takes ownership of "dataMap". 25516c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 25616c4d154dca43c662571129af31b27433b919a32Adam Lesinski status_t openChunk(FileMap* dataMap); 25716c4d154dca43c662571129af31b27433b919a32Adam Lesinski 25816c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 25916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Standard Asset interfaces. 26016c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 26116c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual ssize_t read(void* buf, size_t count); 26216c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t seek(off64_t offset, int whence); 26316c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual void close(void); 26416c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual const void* getBuffer(bool wordAligned); 26516c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getLength(void) const { return mLength; } 26616c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getRemainingLength(void) const { return mLength-mOffset; } 26716c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const; 26816c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual bool isAllocated(void) const { return mBuf != NULL; } 26916c4d154dca43c662571129af31b27433b919a32Adam Lesinski 27016c4d154dca43c662571129af31b27433b919a32Adam Lesinskiprivate: 27116c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mStart; // absolute file offset of start of chunk 27216c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mLength; // length of the chunk 27316c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mOffset; // current local offset, 0 == mStart 27416c4d154dca43c662571129af31b27433b919a32Adam Lesinski FILE* mFp; // for read/seek 27516c4d154dca43c662571129af31b27433b919a32Adam Lesinski char* mFileName; // for opening 27616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 27716c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 27816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * To support getBuffer() we either need to read the entire thing into 27916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * a buffer or memory-map it. For small files it's probably best to 28016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * just read them in. 28116c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 28216c4d154dca43c662571129af31b27433b919a32Adam Lesinski enum { kReadVsMapThreshold = 4096 }; 28316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 28416c4d154dca43c662571129af31b27433b919a32Adam Lesinski FileMap* mMap; // for memory map 28516c4d154dca43c662571129af31b27433b919a32Adam Lesinski unsigned char* mBuf; // for read 286f6113af2d6f6eebee68d3ac510fe96d38a7a39e9John Reck 28716c4d154dca43c662571129af31b27433b919a32Adam Lesinski const void* ensureAlignment(FileMap* map); 28816c4d154dca43c662571129af31b27433b919a32Adam Lesinski}; 28916c4d154dca43c662571129af31b27433b919a32Adam Lesinski 29016c4d154dca43c662571129af31b27433b919a32Adam Lesinski 29116c4d154dca43c662571129af31b27433b919a32Adam Lesinski/* 29216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * An asset based on compressed data in a file. 29316c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 29416c4d154dca43c662571129af31b27433b919a32Adam Lesinskiclass _CompressedAsset : public Asset { 29516c4d154dca43c662571129af31b27433b919a32Adam Lesinskipublic: 29616c4d154dca43c662571129af31b27433b919a32Adam Lesinski _CompressedAsset(void); 29716c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual ~_CompressedAsset(void); 29816c4d154dca43c662571129af31b27433b919a32Adam Lesinski 29916c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 30016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Use a piece of an already-open file. 30116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 30216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * On success, the object takes ownership of "fd". 30316c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 30416c4d154dca43c662571129af31b27433b919a32Adam Lesinski status_t openChunk(int fd, off64_t offset, int compressionMethod, 30516c4d154dca43c662571129af31b27433b919a32Adam Lesinski size_t uncompressedLen, size_t compressedLen); 30616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 30716c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 30816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Use a memory-mapped region. 30916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 31016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * On success, the object takes ownership of "fd". 31116c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 3124600dd053dbdbd4b95f3b11057a1cc55b99f9c77Narayan Kamath status_t openChunk(FileMap* dataMap, size_t uncompressedLen); 31316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 31416c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 31516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Standard Asset interfaces. 31616c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 31716c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual ssize_t read(void* buf, size_t count); 31816c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t seek(off64_t offset, int whence); 31916c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual void close(void); 32016c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual const void* getBuffer(bool wordAligned); 32116c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getLength(void) const { return mUncompressedLen; } 32216c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getRemainingLength(void) const { return mUncompressedLen-mOffset; } 323f6113af2d6f6eebee68d3ac510fe96d38a7a39e9John Reck virtual int openFileDescriptor(off64_t* /* outStart */, off64_t* /* outLength */) const { return -1; } 32416c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual bool isAllocated(void) const { return mBuf != NULL; } 32516c4d154dca43c662571129af31b27433b919a32Adam Lesinski 32616c4d154dca43c662571129af31b27433b919a32Adam Lesinskiprivate: 32716c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mStart; // offset to start of compressed data 32816c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mCompressedLen; // length of the compressed data 32916c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mUncompressedLen; // length of the uncompressed data 33016c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mOffset; // current offset, 0 == start of uncomp data 33116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 33216c4d154dca43c662571129af31b27433b919a32Adam Lesinski FileMap* mMap; // for memory-mapped input 33316c4d154dca43c662571129af31b27433b919a32Adam Lesinski int mFd; // for file input 33416c4d154dca43c662571129af31b27433b919a32Adam Lesinski 33516c4d154dca43c662571129af31b27433b919a32Adam Lesinski class StreamingZipInflater* mZipInflater; // for streaming large compressed assets 33616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 33716c4d154dca43c662571129af31b27433b919a32Adam Lesinski unsigned char* mBuf; // for getBuffer() 33816c4d154dca43c662571129af31b27433b919a32Adam Lesinski}; 33916c4d154dca43c662571129af31b27433b919a32Adam Lesinski 34016c4d154dca43c662571129af31b27433b919a32Adam Lesinski// need: shared mmap version? 34116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 34216c4d154dca43c662571129af31b27433b919a32Adam Lesinski}; // namespace android 34316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 34416c4d154dca43c662571129af31b27433b919a32Adam Lesinski#endif // __LIBS_ASSET_H 345