1 /* 2 * Copyright (C) 2006 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 // 18 // Class providing access to a read-only asset. Asset objects are NOT 19 // thread-safe, and should not be shared across threads. 20 // 21 #ifndef __LIBS_ASSET_H 22 #define __LIBS_ASSET_H 23 24 #include <stdio.h> 25 #include <sys/types.h> 26 27 #include <memory> 28 29 #include <utils/Compat.h> 30 #include <utils/Errors.h> 31 #include <utils/String8.h> 32 33 namespace android { 34 35 class FileMap; 36 37 /* 38 * Instances of this class provide read-only operations on a byte stream. 39 * 40 * Access may be optimized for streaming, random, or whole buffer modes. All 41 * operations are supported regardless of how the file was opened, but some 42 * things will be less efficient. [pass that in??] 43 * 44 * "Asset" is the base class for all types of assets. The classes below 45 * provide most of the implementation. The AssetManager uses one of the 46 * static "create" functions defined here to create a new instance. 47 */ 48 class Asset { 49 public: 50 virtual ~Asset(void) = default; 51 52 static int32_t getGlobalCount(); 53 static String8 getAssetAllocations(); 54 55 /* used when opening an asset */ 56 typedef enum AccessMode { 57 ACCESS_UNKNOWN = 0, 58 59 /* read chunks, and seek forward and backward */ 60 ACCESS_RANDOM, 61 62 /* read sequentially, with an occasional forward seek */ 63 ACCESS_STREAMING, 64 65 /* caller plans to ask for a read-only buffer with all data */ 66 ACCESS_BUFFER, 67 } AccessMode; 68 69 /* 70 * Read data from the current offset. Returns the actual number of 71 * bytes read, 0 on EOF, or -1 on error. 72 */ 73 virtual ssize_t read(void* buf, size_t count) = 0; 74 75 /* 76 * Seek to the specified offset. "whence" uses the same values as 77 * lseek/fseek. Returns the new position on success, or (off64_t) -1 78 * on failure. 79 */ 80 virtual off64_t seek(off64_t offset, int whence) = 0; 81 82 /* 83 * Close the asset, freeing all associated resources. 84 */ 85 virtual void close(void) = 0; 86 87 /* 88 * Get a pointer to a buffer with the entire contents of the file. 89 */ 90 virtual const void* getBuffer(bool wordAligned) = 0; 91 92 /* 93 * Get the total amount of data that can be read. 94 */ 95 virtual off64_t getLength(void) const = 0; 96 97 /* 98 * Get the total amount of data that can be read from the current position. 99 */ 100 virtual off64_t getRemainingLength(void) const = 0; 101 102 /* 103 * Open a new file descriptor that can be used to read this asset. 104 * Returns -1 if you can not use the file descriptor (for example if the 105 * asset is compressed). 106 */ 107 virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const = 0; 108 109 /* 110 * Return whether this asset's buffer is allocated in RAM (not mmapped). 111 * Note: not virtual so it is safe to call even when being destroyed. 112 */ isAllocated(void)113 virtual bool isAllocated(void) const { return false; } 114 115 /* 116 * Get a string identifying the asset's source. This might be a full 117 * path, it might be a colon-separated list of identifiers. 118 * 119 * This is NOT intended to be used for anything except debug output. 120 * DO NOT try to parse this or use it to open a file. 121 */ getAssetSource(void)122 const char* getAssetSource(void) const { return mAssetSource.string(); } 123 124 protected: 125 /* 126 * Adds this Asset to the global Asset list for debugging and 127 * accounting. 128 * Concrete subclasses must call this in their constructor. 129 */ 130 static void registerAsset(Asset* asset); 131 132 /* 133 * Removes this Asset from the global Asset list. 134 * Concrete subclasses must call this in their destructor. 135 */ 136 static void unregisterAsset(Asset* asset); 137 138 Asset(void); // constructor; only invoked indirectly 139 140 /* handle common seek() housekeeping */ 141 off64_t handleSeek(off64_t offset, int whence, off64_t curPosn, off64_t maxPosn); 142 143 /* set the asset source string */ setAssetSource(const String8 & path)144 void setAssetSource(const String8& path) { mAssetSource = path; } 145 getAccessMode(void)146 AccessMode getAccessMode(void) const { return mAccessMode; } 147 148 private: 149 /* these operations are not implemented */ 150 Asset(const Asset& src); 151 Asset& operator=(const Asset& src); 152 153 /* AssetManager needs access to our "create" functions */ 154 friend class AssetManager; 155 friend class ApkAssets; 156 157 /* 158 * Create the asset from a named file on disk. 159 */ 160 static Asset* createFromFile(const char* fileName, AccessMode mode); 161 162 /* 163 * Create the asset from a named, compressed file on disk (e.g. ".gz"). 164 */ 165 static Asset* createFromCompressedFile(const char* fileName, 166 AccessMode mode); 167 168 #if 0 169 /* 170 * Create the asset from a segment of an open file. This will fail 171 * if "offset" and "length" don't fit within the bounds of the file. 172 * 173 * The asset takes ownership of the file descriptor. 174 */ 175 static Asset* createFromFileSegment(int fd, off64_t offset, size_t length, 176 AccessMode mode); 177 178 /* 179 * Create from compressed data. "fd" should be seeked to the start of 180 * the compressed data. This could be inside a gzip file or part of a 181 * Zip archive. 182 * 183 * The asset takes ownership of the file descriptor. 184 * 185 * This may not verify the validity of the compressed data until first 186 * use. 187 */ 188 static Asset* createFromCompressedData(int fd, off64_t offset, 189 int compressionMethod, size_t compressedLength, 190 size_t uncompressedLength, AccessMode mode); 191 #endif 192 193 /* 194 * Create the asset from a memory-mapped file segment. 195 * 196 * The asset takes ownership of the FileMap. 197 */ 198 static Asset* createFromUncompressedMap(FileMap* dataMap, AccessMode mode); 199 200 static std::unique_ptr<Asset> createFromUncompressedMap(std::unique_ptr<FileMap> dataMap, 201 AccessMode mode); 202 203 /* 204 * Create the asset from a memory-mapped file segment with compressed 205 * data. 206 * 207 * The asset takes ownership of the FileMap. 208 */ 209 static Asset* createFromCompressedMap(FileMap* dataMap, 210 size_t uncompressedLen, AccessMode mode); 211 212 static std::unique_ptr<Asset> createFromCompressedMap(std::unique_ptr<FileMap> dataMap, 213 size_t uncompressedLen, AccessMode mode); 214 215 216 /* 217 * Create from a reference-counted chunk of shared memory. 218 */ 219 // TODO 220 221 AccessMode mAccessMode; // how the asset was opened 222 String8 mAssetSource; // debug string 223 224 Asset* mNext; // linked list. 225 Asset* mPrev; 226 }; 227 228 229 /* 230 * =========================================================================== 231 * 232 * Innards follow. Do not use these classes directly. 233 */ 234 235 /* 236 * An asset based on an uncompressed file on disk. It may encompass the 237 * entire file or just a piece of it. Access is through fread/fseek. 238 */ 239 class _FileAsset : public Asset { 240 public: 241 _FileAsset(void); 242 virtual ~_FileAsset(void); 243 244 /* 245 * Use a piece of an already-open file. 246 * 247 * On success, the object takes ownership of "fd". 248 */ 249 status_t openChunk(const char* fileName, int fd, off64_t offset, size_t length); 250 251 /* 252 * Use a memory-mapped region. 253 * 254 * On success, the object takes ownership of "dataMap". 255 */ 256 status_t openChunk(FileMap* dataMap); 257 258 /* 259 * Standard Asset interfaces. 260 */ 261 virtual ssize_t read(void* buf, size_t count); 262 virtual off64_t seek(off64_t offset, int whence); 263 virtual void close(void); 264 virtual const void* getBuffer(bool wordAligned); getLength(void)265 virtual off64_t getLength(void) const { return mLength; } getRemainingLength(void)266 virtual off64_t getRemainingLength(void) const { return mLength-mOffset; } 267 virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const; isAllocated(void)268 virtual bool isAllocated(void) const { return mBuf != NULL; } 269 270 private: 271 off64_t mStart; // absolute file offset of start of chunk 272 off64_t mLength; // length of the chunk 273 off64_t mOffset; // current local offset, 0 == mStart 274 FILE* mFp; // for read/seek 275 char* mFileName; // for opening 276 277 /* 278 * To support getBuffer() we either need to read the entire thing into 279 * a buffer or memory-map it. For small files it's probably best to 280 * just read them in. 281 */ 282 enum { kReadVsMapThreshold = 4096 }; 283 284 FileMap* mMap; // for memory map 285 unsigned char* mBuf; // for read 286 287 const void* ensureAlignment(FileMap* map); 288 }; 289 290 291 /* 292 * An asset based on compressed data in a file. 293 */ 294 class _CompressedAsset : public Asset { 295 public: 296 _CompressedAsset(void); 297 virtual ~_CompressedAsset(void); 298 299 /* 300 * Use a piece of an already-open file. 301 * 302 * On success, the object takes ownership of "fd". 303 */ 304 status_t openChunk(int fd, off64_t offset, int compressionMethod, 305 size_t uncompressedLen, size_t compressedLen); 306 307 /* 308 * Use a memory-mapped region. 309 * 310 * On success, the object takes ownership of "fd". 311 */ 312 status_t openChunk(FileMap* dataMap, size_t uncompressedLen); 313 314 /* 315 * Standard Asset interfaces. 316 */ 317 virtual ssize_t read(void* buf, size_t count); 318 virtual off64_t seek(off64_t offset, int whence); 319 virtual void close(void); 320 virtual const void* getBuffer(bool wordAligned); getLength(void)321 virtual off64_t getLength(void) const { return mUncompressedLen; } getRemainingLength(void)322 virtual off64_t getRemainingLength(void) const { return mUncompressedLen-mOffset; } openFileDescriptor(off64_t *,off64_t *)323 virtual int openFileDescriptor(off64_t* /* outStart */, off64_t* /* outLength */) const { return -1; } isAllocated(void)324 virtual bool isAllocated(void) const { return mBuf != NULL; } 325 326 private: 327 off64_t mStart; // offset to start of compressed data 328 off64_t mCompressedLen; // length of the compressed data 329 off64_t mUncompressedLen; // length of the uncompressed data 330 off64_t mOffset; // current offset, 0 == start of uncomp data 331 332 FileMap* mMap; // for memory-mapped input 333 int mFd; // for file input 334 335 class StreamingZipInflater* mZipInflater; // for streaming large compressed assets 336 337 unsigned char* mBuf; // for getBuffer() 338 }; 339 340 // need: shared mmap version? 341 342 }; // namespace android 343 344 #endif // __LIBS_ASSET_H 345