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 2716c4d154dca43c662571129af31b27433b919a32Adam Lesinski#include <utils/Compat.h> 2816c4d154dca43c662571129af31b27433b919a32Adam Lesinski#include <utils/Errors.h> 2916c4d154dca43c662571129af31b27433b919a32Adam Lesinski#include <utils/FileMap.h> 3016c4d154dca43c662571129af31b27433b919a32Adam Lesinski#include <utils/String8.h> 3116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 3216c4d154dca43c662571129af31b27433b919a32Adam Lesinskinamespace android { 3316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 3416c4d154dca43c662571129af31b27433b919a32Adam Lesinski/* 3516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Instances of this class provide read-only operations on a byte stream. 3616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 3716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Access may be optimized for streaming, random, or whole buffer modes. All 3816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * operations are supported regardless of how the file was opened, but some 3916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * things will be less efficient. [pass that in??] 4016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 4116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * "Asset" is the base class for all types of assets. The classes below 4216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * provide most of the implementation. The AssetManager uses one of the 4316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * static "create" functions defined here to create a new instance. 4416c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 4516c4d154dca43c662571129af31b27433b919a32Adam Lesinskiclass Asset { 4616c4d154dca43c662571129af31b27433b919a32Adam Lesinskipublic: 4716c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual ~Asset(void); 4816c4d154dca43c662571129af31b27433b919a32Adam Lesinski 4916c4d154dca43c662571129af31b27433b919a32Adam Lesinski static int32_t getGlobalCount(); 5016c4d154dca43c662571129af31b27433b919a32Adam Lesinski static String8 getAssetAllocations(); 5116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 5216c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* used when opening an asset */ 5316c4d154dca43c662571129af31b27433b919a32Adam Lesinski typedef enum AccessMode { 5416c4d154dca43c662571129af31b27433b919a32Adam Lesinski ACCESS_UNKNOWN = 0, 5516c4d154dca43c662571129af31b27433b919a32Adam Lesinski 5616c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* read chunks, and seek forward and backward */ 5716c4d154dca43c662571129af31b27433b919a32Adam Lesinski ACCESS_RANDOM, 5816c4d154dca43c662571129af31b27433b919a32Adam Lesinski 5916c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* read sequentially, with an occasional forward seek */ 6016c4d154dca43c662571129af31b27433b919a32Adam Lesinski ACCESS_STREAMING, 6116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 6216c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* caller plans to ask for a read-only buffer with all data */ 6316c4d154dca43c662571129af31b27433b919a32Adam Lesinski ACCESS_BUFFER, 6416c4d154dca43c662571129af31b27433b919a32Adam Lesinski } AccessMode; 6516c4d154dca43c662571129af31b27433b919a32Adam Lesinski 6616c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 6716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Read data from the current offset. Returns the actual number of 6816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * bytes read, 0 on EOF, or -1 on error. 6916c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 7016c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual ssize_t read(void* buf, size_t count) = 0; 7116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 7216c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 7316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Seek to the specified offset. "whence" uses the same values as 7416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * lseek/fseek. Returns the new position on success, or (off64_t) -1 7516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * on failure. 7616c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 7716c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t seek(off64_t offset, int whence) = 0; 7816c4d154dca43c662571129af31b27433b919a32Adam Lesinski 7916c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 8016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Close the asset, freeing all associated resources. 8116c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 8216c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual void close(void) = 0; 8316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 8416c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 8516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Get a pointer to a buffer with the entire contents of the file. 8616c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 8716c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual const void* getBuffer(bool wordAligned) = 0; 8816c4d154dca43c662571129af31b27433b919a32Adam Lesinski 8916c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 9016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Get the total amount of data that can be read. 9116c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 9216c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getLength(void) const = 0; 9316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 9416c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 9516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Get the total amount of data that can be read from the current position. 9616c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 9716c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getRemainingLength(void) const = 0; 9816c4d154dca43c662571129af31b27433b919a32Adam Lesinski 9916c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 10016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Open a new file descriptor that can be used to read this asset. 10116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Returns -1 if you can not use the file descriptor (for example if the 10216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * asset is compressed). 10316c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 10416c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const = 0; 10516c4d154dca43c662571129af31b27433b919a32Adam Lesinski 10616c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 10716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Return whether this asset's buffer is allocated in RAM (not mmapped). 10816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Note: not virtual so it is safe to call even when being destroyed. 10916c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 11016c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual bool isAllocated(void) const { return false; } 11116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 11216c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 11316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Get a string identifying the asset's source. This might be a full 11416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * path, it might be a colon-separated list of identifiers. 11516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 11616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * This is NOT intended to be used for anything except debug output. 11716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * DO NOT try to parse this or use it to open a file. 11816c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 11916c4d154dca43c662571129af31b27433b919a32Adam Lesinski const char* getAssetSource(void) const { return mAssetSource.string(); } 12016c4d154dca43c662571129af31b27433b919a32Adam Lesinski 12116c4d154dca43c662571129af31b27433b919a32Adam Lesinskiprotected: 12216c4d154dca43c662571129af31b27433b919a32Adam Lesinski Asset(void); // constructor; only invoked indirectly 12316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 12416c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* handle common seek() housekeeping */ 12516c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t handleSeek(off64_t offset, int whence, off64_t curPosn, off64_t maxPosn); 12616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 12716c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* set the asset source string */ 12816c4d154dca43c662571129af31b27433b919a32Adam Lesinski void setAssetSource(const String8& path) { mAssetSource = path; } 12916c4d154dca43c662571129af31b27433b919a32Adam Lesinski 13016c4d154dca43c662571129af31b27433b919a32Adam Lesinski AccessMode getAccessMode(void) const { return mAccessMode; } 13116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 13216c4d154dca43c662571129af31b27433b919a32Adam Lesinskiprivate: 13316c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* these operations are not implemented */ 13416c4d154dca43c662571129af31b27433b919a32Adam Lesinski Asset(const Asset& src); 13516c4d154dca43c662571129af31b27433b919a32Adam Lesinski Asset& operator=(const Asset& src); 13616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 13716c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* AssetManager needs access to our "create" functions */ 13816c4d154dca43c662571129af31b27433b919a32Adam Lesinski friend class AssetManager; 13916c4d154dca43c662571129af31b27433b919a32Adam Lesinski 14016c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 14116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create the asset from a named file on disk. 14216c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 14316c4d154dca43c662571129af31b27433b919a32Adam Lesinski static Asset* createFromFile(const char* fileName, AccessMode mode); 14416c4d154dca43c662571129af31b27433b919a32Adam Lesinski 14516c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 14616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create the asset from a named, compressed file on disk (e.g. ".gz"). 14716c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 14816c4d154dca43c662571129af31b27433b919a32Adam Lesinski static Asset* createFromCompressedFile(const char* fileName, 14916c4d154dca43c662571129af31b27433b919a32Adam Lesinski AccessMode mode); 15016c4d154dca43c662571129af31b27433b919a32Adam Lesinski 15116c4d154dca43c662571129af31b27433b919a32Adam Lesinski#if 0 15216c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 15316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create the asset from a segment of an open file. This will fail 15416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * if "offset" and "length" don't fit within the bounds of the file. 15516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 15616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * The asset takes ownership of the file descriptor. 15716c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 15816c4d154dca43c662571129af31b27433b919a32Adam Lesinski static Asset* createFromFileSegment(int fd, off64_t offset, size_t length, 15916c4d154dca43c662571129af31b27433b919a32Adam Lesinski AccessMode mode); 16016c4d154dca43c662571129af31b27433b919a32Adam Lesinski 16116c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 16216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create from compressed data. "fd" should be seeked to the start of 16316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * the compressed data. This could be inside a gzip file or part of a 16416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Zip archive. 16516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 16616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * The asset takes ownership of the file descriptor. 16716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 16816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * This may not verify the validity of the compressed data until first 16916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * use. 17016c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 17116c4d154dca43c662571129af31b27433b919a32Adam Lesinski static Asset* createFromCompressedData(int fd, off64_t offset, 17216c4d154dca43c662571129af31b27433b919a32Adam Lesinski int compressionMethod, size_t compressedLength, 17316c4d154dca43c662571129af31b27433b919a32Adam Lesinski size_t uncompressedLength, AccessMode mode); 17416c4d154dca43c662571129af31b27433b919a32Adam Lesinski#endif 17516c4d154dca43c662571129af31b27433b919a32Adam Lesinski 17616c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 17716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create the asset from a memory-mapped file segment. 17816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 17916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * The asset takes ownership of the FileMap. 18016c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 18116c4d154dca43c662571129af31b27433b919a32Adam Lesinski static Asset* createFromUncompressedMap(FileMap* dataMap, AccessMode mode); 18216c4d154dca43c662571129af31b27433b919a32Adam Lesinski 18316c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 18416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create the asset from a memory-mapped file segment with compressed 18516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * data. "method" is a Zip archive compression method constant. 18616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 18716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * The asset takes ownership of the FileMap. 18816c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 18916c4d154dca43c662571129af31b27433b919a32Adam Lesinski static Asset* createFromCompressedMap(FileMap* dataMap, int method, 19016c4d154dca43c662571129af31b27433b919a32Adam Lesinski size_t uncompressedLen, AccessMode mode); 19116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 19216c4d154dca43c662571129af31b27433b919a32Adam Lesinski 19316c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 19416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Create from a reference-counted chunk of shared memory. 19516c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 19616c4d154dca43c662571129af31b27433b919a32Adam Lesinski // TODO 19716c4d154dca43c662571129af31b27433b919a32Adam Lesinski 19816c4d154dca43c662571129af31b27433b919a32Adam Lesinski AccessMode mAccessMode; // how the asset was opened 19916c4d154dca43c662571129af31b27433b919a32Adam Lesinski String8 mAssetSource; // debug string 20016c4d154dca43c662571129af31b27433b919a32Adam Lesinski 20116c4d154dca43c662571129af31b27433b919a32Adam Lesinski Asset* mNext; // linked list. 20216c4d154dca43c662571129af31b27433b919a32Adam Lesinski Asset* mPrev; 20316c4d154dca43c662571129af31b27433b919a32Adam Lesinski}; 20416c4d154dca43c662571129af31b27433b919a32Adam Lesinski 20516c4d154dca43c662571129af31b27433b919a32Adam Lesinski 20616c4d154dca43c662571129af31b27433b919a32Adam Lesinski/* 20716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * =========================================================================== 20816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 20916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Innards follow. Do not use these classes directly. 21016c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 21116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 21216c4d154dca43c662571129af31b27433b919a32Adam Lesinski/* 21316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * An asset based on an uncompressed file on disk. It may encompass the 21416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * entire file or just a piece of it. Access is through fread/fseek. 21516c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 21616c4d154dca43c662571129af31b27433b919a32Adam Lesinskiclass _FileAsset : public Asset { 21716c4d154dca43c662571129af31b27433b919a32Adam Lesinskipublic: 21816c4d154dca43c662571129af31b27433b919a32Adam Lesinski _FileAsset(void); 21916c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual ~_FileAsset(void); 22016c4d154dca43c662571129af31b27433b919a32Adam Lesinski 22116c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 22216c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Use a piece of an already-open file. 22316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 22416c4d154dca43c662571129af31b27433b919a32Adam Lesinski * On success, the object takes ownership of "fd". 22516c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 22616c4d154dca43c662571129af31b27433b919a32Adam Lesinski status_t openChunk(const char* fileName, int fd, off64_t offset, size_t length); 22716c4d154dca43c662571129af31b27433b919a32Adam Lesinski 22816c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 22916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Use a memory-mapped region. 23016c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 23116c4d154dca43c662571129af31b27433b919a32Adam Lesinski * On success, the object takes ownership of "dataMap". 23216c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 23316c4d154dca43c662571129af31b27433b919a32Adam Lesinski status_t openChunk(FileMap* dataMap); 23416c4d154dca43c662571129af31b27433b919a32Adam Lesinski 23516c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 23616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Standard Asset interfaces. 23716c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 23816c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual ssize_t read(void* buf, size_t count); 23916c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t seek(off64_t offset, int whence); 24016c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual void close(void); 24116c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual const void* getBuffer(bool wordAligned); 24216c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getLength(void) const { return mLength; } 24316c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getRemainingLength(void) const { return mLength-mOffset; } 24416c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const; 24516c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual bool isAllocated(void) const { return mBuf != NULL; } 24616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 24716c4d154dca43c662571129af31b27433b919a32Adam Lesinskiprivate: 24816c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mStart; // absolute file offset of start of chunk 24916c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mLength; // length of the chunk 25016c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mOffset; // current local offset, 0 == mStart 25116c4d154dca43c662571129af31b27433b919a32Adam Lesinski FILE* mFp; // for read/seek 25216c4d154dca43c662571129af31b27433b919a32Adam Lesinski char* mFileName; // for opening 25316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 25416c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 25516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * To support getBuffer() we either need to read the entire thing into 25616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * a buffer or memory-map it. For small files it's probably best to 25716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * just read them in. 25816c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 25916c4d154dca43c662571129af31b27433b919a32Adam Lesinski enum { kReadVsMapThreshold = 4096 }; 26016c4d154dca43c662571129af31b27433b919a32Adam Lesinski 26116c4d154dca43c662571129af31b27433b919a32Adam Lesinski FileMap* mMap; // for memory map 26216c4d154dca43c662571129af31b27433b919a32Adam Lesinski unsigned char* mBuf; // for read 26316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 26416c4d154dca43c662571129af31b27433b919a32Adam Lesinski const void* ensureAlignment(FileMap* map); 26516c4d154dca43c662571129af31b27433b919a32Adam Lesinski}; 26616c4d154dca43c662571129af31b27433b919a32Adam Lesinski 26716c4d154dca43c662571129af31b27433b919a32Adam Lesinski 26816c4d154dca43c662571129af31b27433b919a32Adam Lesinski/* 26916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * An asset based on compressed data in a file. 27016c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 27116c4d154dca43c662571129af31b27433b919a32Adam Lesinskiclass _CompressedAsset : public Asset { 27216c4d154dca43c662571129af31b27433b919a32Adam Lesinskipublic: 27316c4d154dca43c662571129af31b27433b919a32Adam Lesinski _CompressedAsset(void); 27416c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual ~_CompressedAsset(void); 27516c4d154dca43c662571129af31b27433b919a32Adam Lesinski 27616c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 27716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Use a piece of an already-open file. 27816c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 27916c4d154dca43c662571129af31b27433b919a32Adam Lesinski * On success, the object takes ownership of "fd". 28016c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 28116c4d154dca43c662571129af31b27433b919a32Adam Lesinski status_t openChunk(int fd, off64_t offset, int compressionMethod, 28216c4d154dca43c662571129af31b27433b919a32Adam Lesinski size_t uncompressedLen, size_t compressedLen); 28316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 28416c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 28516c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Use a memory-mapped region. 28616c4d154dca43c662571129af31b27433b919a32Adam Lesinski * 28716c4d154dca43c662571129af31b27433b919a32Adam Lesinski * On success, the object takes ownership of "fd". 28816c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 28916c4d154dca43c662571129af31b27433b919a32Adam Lesinski status_t openChunk(FileMap* dataMap, int compressionMethod, 29016c4d154dca43c662571129af31b27433b919a32Adam Lesinski size_t uncompressedLen); 29116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 29216c4d154dca43c662571129af31b27433b919a32Adam Lesinski /* 29316c4d154dca43c662571129af31b27433b919a32Adam Lesinski * Standard Asset interfaces. 29416c4d154dca43c662571129af31b27433b919a32Adam Lesinski */ 29516c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual ssize_t read(void* buf, size_t count); 29616c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t seek(off64_t offset, int whence); 29716c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual void close(void); 29816c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual const void* getBuffer(bool wordAligned); 29916c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getLength(void) const { return mUncompressedLen; } 30016c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual off64_t getRemainingLength(void) const { return mUncompressedLen-mOffset; } 30116c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const { return -1; } 30216c4d154dca43c662571129af31b27433b919a32Adam Lesinski virtual bool isAllocated(void) const { return mBuf != NULL; } 30316c4d154dca43c662571129af31b27433b919a32Adam Lesinski 30416c4d154dca43c662571129af31b27433b919a32Adam Lesinskiprivate: 30516c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mStart; // offset to start of compressed data 30616c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mCompressedLen; // length of the compressed data 30716c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mUncompressedLen; // length of the uncompressed data 30816c4d154dca43c662571129af31b27433b919a32Adam Lesinski off64_t mOffset; // current offset, 0 == start of uncomp data 30916c4d154dca43c662571129af31b27433b919a32Adam Lesinski 31016c4d154dca43c662571129af31b27433b919a32Adam Lesinski FileMap* mMap; // for memory-mapped input 31116c4d154dca43c662571129af31b27433b919a32Adam Lesinski int mFd; // for file input 31216c4d154dca43c662571129af31b27433b919a32Adam Lesinski 31316c4d154dca43c662571129af31b27433b919a32Adam Lesinski class StreamingZipInflater* mZipInflater; // for streaming large compressed assets 31416c4d154dca43c662571129af31b27433b919a32Adam Lesinski 31516c4d154dca43c662571129af31b27433b919a32Adam Lesinski unsigned char* mBuf; // for getBuffer() 31616c4d154dca43c662571129af31b27433b919a32Adam Lesinski}; 31716c4d154dca43c662571129af31b27433b919a32Adam Lesinski 31816c4d154dca43c662571129af31b27433b919a32Adam Lesinski// need: shared mmap version? 31916c4d154dca43c662571129af31b27433b919a32Adam Lesinski 32016c4d154dca43c662571129af31b27433b919a32Adam Lesinski}; // namespace android 32116c4d154dca43c662571129af31b27433b919a32Adam Lesinski 32216c4d154dca43c662571129af31b27433b919a32Adam Lesinski#endif // __LIBS_ASSET_H 323