frameworks/base refactoring.
step 2: move libutils headers to their new home: androidfw Change-Id: I14624ba23db92a81f2cb929f104386e1fab293ef
This commit is contained in:
parent
f21c255fde
commit
c2b7fb1f91
|
@ -1,322 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2006 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//
|
||||
// Class providing access to a read-only asset. Asset objects are NOT
|
||||
// thread-safe, and should not be shared across threads.
|
||||
//
|
||||
#ifndef __LIBS_ASSET_H
|
||||
#define __LIBS_ASSET_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <utils/Compat.h>
|
||||
#include <utils/Errors.h>
|
||||
#include <utils/FileMap.h>
|
||||
#include <utils/String8.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
/*
|
||||
* Instances of this class provide read-only operations on a byte stream.
|
||||
*
|
||||
* Access may be optimized for streaming, random, or whole buffer modes. All
|
||||
* operations are supported regardless of how the file was opened, but some
|
||||
* things will be less efficient. [pass that in??]
|
||||
*
|
||||
* "Asset" is the base class for all types of assets. The classes below
|
||||
* provide most of the implementation. The AssetManager uses one of the
|
||||
* static "create" functions defined here to create a new instance.
|
||||
*/
|
||||
class Asset {
|
||||
public:
|
||||
virtual ~Asset(void);
|
||||
|
||||
static int32_t getGlobalCount();
|
||||
static String8 getAssetAllocations();
|
||||
|
||||
/* used when opening an asset */
|
||||
typedef enum AccessMode {
|
||||
ACCESS_UNKNOWN = 0,
|
||||
|
||||
/* read chunks, and seek forward and backward */
|
||||
ACCESS_RANDOM,
|
||||
|
||||
/* read sequentially, with an occasional forward seek */
|
||||
ACCESS_STREAMING,
|
||||
|
||||
/* caller plans to ask for a read-only buffer with all data */
|
||||
ACCESS_BUFFER,
|
||||
} AccessMode;
|
||||
|
||||
/*
|
||||
* Read data from the current offset. Returns the actual number of
|
||||
* bytes read, 0 on EOF, or -1 on error.
|
||||
*/
|
||||
virtual ssize_t read(void* buf, size_t count) = 0;
|
||||
|
||||
/*
|
||||
* Seek to the specified offset. "whence" uses the same values as
|
||||
* lseek/fseek. Returns the new position on success, or (off64_t) -1
|
||||
* on failure.
|
||||
*/
|
||||
virtual off64_t seek(off64_t offset, int whence) = 0;
|
||||
|
||||
/*
|
||||
* Close the asset, freeing all associated resources.
|
||||
*/
|
||||
virtual void close(void) = 0;
|
||||
|
||||
/*
|
||||
* Get a pointer to a buffer with the entire contents of the file.
|
||||
*/
|
||||
virtual const void* getBuffer(bool wordAligned) = 0;
|
||||
|
||||
/*
|
||||
* Get the total amount of data that can be read.
|
||||
*/
|
||||
virtual off64_t getLength(void) const = 0;
|
||||
|
||||
/*
|
||||
* Get the total amount of data that can be read from the current position.
|
||||
*/
|
||||
virtual off64_t getRemainingLength(void) const = 0;
|
||||
|
||||
/*
|
||||
* Open a new file descriptor that can be used to read this asset.
|
||||
* Returns -1 if you can not use the file descriptor (for example if the
|
||||
* asset is compressed).
|
||||
*/
|
||||
virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const = 0;
|
||||
|
||||
/*
|
||||
* Return whether this asset's buffer is allocated in RAM (not mmapped).
|
||||
* Note: not virtual so it is safe to call even when being destroyed.
|
||||
*/
|
||||
virtual bool isAllocated(void) const { return false; }
|
||||
|
||||
/*
|
||||
* Get a string identifying the asset's source. This might be a full
|
||||
* path, it might be a colon-separated list of identifiers.
|
||||
*
|
||||
* This is NOT intended to be used for anything except debug output.
|
||||
* DO NOT try to parse this or use it to open a file.
|
||||
*/
|
||||
const char* getAssetSource(void) const { return mAssetSource.string(); }
|
||||
|
||||
protected:
|
||||
Asset(void); // constructor; only invoked indirectly
|
||||
|
||||
/* handle common seek() housekeeping */
|
||||
off64_t handleSeek(off64_t offset, int whence, off64_t curPosn, off64_t maxPosn);
|
||||
|
||||
/* set the asset source string */
|
||||
void setAssetSource(const String8& path) { mAssetSource = path; }
|
||||
|
||||
AccessMode getAccessMode(void) const { return mAccessMode; }
|
||||
|
||||
private:
|
||||
/* these operations are not implemented */
|
||||
Asset(const Asset& src);
|
||||
Asset& operator=(const Asset& src);
|
||||
|
||||
/* AssetManager needs access to our "create" functions */
|
||||
friend class AssetManager;
|
||||
|
||||
/*
|
||||
* Create the asset from a named file on disk.
|
||||
*/
|
||||
static Asset* createFromFile(const char* fileName, AccessMode mode);
|
||||
|
||||
/*
|
||||
* Create the asset from a named, compressed file on disk (e.g. ".gz").
|
||||
*/
|
||||
static Asset* createFromCompressedFile(const char* fileName,
|
||||
AccessMode mode);
|
||||
|
||||
#if 0
|
||||
/*
|
||||
* Create the asset from a segment of an open file. This will fail
|
||||
* if "offset" and "length" don't fit within the bounds of the file.
|
||||
*
|
||||
* The asset takes ownership of the file descriptor.
|
||||
*/
|
||||
static Asset* createFromFileSegment(int fd, off64_t offset, size_t length,
|
||||
AccessMode mode);
|
||||
|
||||
/*
|
||||
* Create from compressed data. "fd" should be seeked to the start of
|
||||
* the compressed data. This could be inside a gzip file or part of a
|
||||
* Zip archive.
|
||||
*
|
||||
* The asset takes ownership of the file descriptor.
|
||||
*
|
||||
* This may not verify the validity of the compressed data until first
|
||||
* use.
|
||||
*/
|
||||
static Asset* createFromCompressedData(int fd, off64_t offset,
|
||||
int compressionMethod, size_t compressedLength,
|
||||
size_t uncompressedLength, AccessMode mode);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Create the asset from a memory-mapped file segment.
|
||||
*
|
||||
* The asset takes ownership of the FileMap.
|
||||
*/
|
||||
static Asset* createFromUncompressedMap(FileMap* dataMap, AccessMode mode);
|
||||
|
||||
/*
|
||||
* Create the asset from a memory-mapped file segment with compressed
|
||||
* data. "method" is a Zip archive compression method constant.
|
||||
*
|
||||
* The asset takes ownership of the FileMap.
|
||||
*/
|
||||
static Asset* createFromCompressedMap(FileMap* dataMap, int method,
|
||||
size_t uncompressedLen, AccessMode mode);
|
||||
|
||||
|
||||
/*
|
||||
* Create from a reference-counted chunk of shared memory.
|
||||
*/
|
||||
// TODO
|
||||
|
||||
AccessMode mAccessMode; // how the asset was opened
|
||||
String8 mAssetSource; // debug string
|
||||
|
||||
Asset* mNext; // linked list.
|
||||
Asset* mPrev;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* ===========================================================================
|
||||
*
|
||||
* Innards follow. Do not use these classes directly.
|
||||
*/
|
||||
|
||||
/*
|
||||
* An asset based on an uncompressed file on disk. It may encompass the
|
||||
* entire file or just a piece of it. Access is through fread/fseek.
|
||||
*/
|
||||
class _FileAsset : public Asset {
|
||||
public:
|
||||
_FileAsset(void);
|
||||
virtual ~_FileAsset(void);
|
||||
|
||||
/*
|
||||
* Use a piece of an already-open file.
|
||||
*
|
||||
* On success, the object takes ownership of "fd".
|
||||
*/
|
||||
status_t openChunk(const char* fileName, int fd, off64_t offset, size_t length);
|
||||
|
||||
/*
|
||||
* Use a memory-mapped region.
|
||||
*
|
||||
* On success, the object takes ownership of "dataMap".
|
||||
*/
|
||||
status_t openChunk(FileMap* dataMap);
|
||||
|
||||
/*
|
||||
* Standard Asset interfaces.
|
||||
*/
|
||||
virtual ssize_t read(void* buf, size_t count);
|
||||
virtual off64_t seek(off64_t offset, int whence);
|
||||
virtual void close(void);
|
||||
virtual const void* getBuffer(bool wordAligned);
|
||||
virtual off64_t getLength(void) const { return mLength; }
|
||||
virtual off64_t getRemainingLength(void) const { return mLength-mOffset; }
|
||||
virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const;
|
||||
virtual bool isAllocated(void) const { return mBuf != NULL; }
|
||||
|
||||
private:
|
||||
off64_t mStart; // absolute file offset of start of chunk
|
||||
off64_t mLength; // length of the chunk
|
||||
off64_t mOffset; // current local offset, 0 == mStart
|
||||
FILE* mFp; // for read/seek
|
||||
char* mFileName; // for opening
|
||||
|
||||
/*
|
||||
* To support getBuffer() we either need to read the entire thing into
|
||||
* a buffer or memory-map it. For small files it's probably best to
|
||||
* just read them in.
|
||||
*/
|
||||
enum { kReadVsMapThreshold = 4096 };
|
||||
|
||||
FileMap* mMap; // for memory map
|
||||
unsigned char* mBuf; // for read
|
||||
|
||||
const void* ensureAlignment(FileMap* map);
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* An asset based on compressed data in a file.
|
||||
*/
|
||||
class _CompressedAsset : public Asset {
|
||||
public:
|
||||
_CompressedAsset(void);
|
||||
virtual ~_CompressedAsset(void);
|
||||
|
||||
/*
|
||||
* Use a piece of an already-open file.
|
||||
*
|
||||
* On success, the object takes ownership of "fd".
|
||||
*/
|
||||
status_t openChunk(int fd, off64_t offset, int compressionMethod,
|
||||
size_t uncompressedLen, size_t compressedLen);
|
||||
|
||||
/*
|
||||
* Use a memory-mapped region.
|
||||
*
|
||||
* On success, the object takes ownership of "fd".
|
||||
*/
|
||||
status_t openChunk(FileMap* dataMap, int compressionMethod,
|
||||
size_t uncompressedLen);
|
||||
|
||||
/*
|
||||
* Standard Asset interfaces.
|
||||
*/
|
||||
virtual ssize_t read(void* buf, size_t count);
|
||||
virtual off64_t seek(off64_t offset, int whence);
|
||||
virtual void close(void);
|
||||
virtual const void* getBuffer(bool wordAligned);
|
||||
virtual off64_t getLength(void) const { return mUncompressedLen; }
|
||||
virtual off64_t getRemainingLength(void) const { return mUncompressedLen-mOffset; }
|
||||
virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const { return -1; }
|
||||
virtual bool isAllocated(void) const { return mBuf != NULL; }
|
||||
|
||||
private:
|
||||
off64_t mStart; // offset to start of compressed data
|
||||
off64_t mCompressedLen; // length of the compressed data
|
||||
off64_t mUncompressedLen; // length of the uncompressed data
|
||||
off64_t mOffset; // current offset, 0 == start of uncomp data
|
||||
|
||||
FileMap* mMap; // for memory-mapped input
|
||||
int mFd; // for file input
|
||||
|
||||
class StreamingZipInflater* mZipInflater; // for streaming large compressed assets
|
||||
|
||||
unsigned char* mBuf; // for getBuffer()
|
||||
};
|
||||
|
||||
// need: shared mmap version?
|
||||
|
||||
}; // namespace android
|
||||
|
||||
#endif // __LIBS_ASSET_H
|
|
@ -1,145 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2006 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//
|
||||
// Access a chunk of the asset hierarchy as if it were a single directory.
|
||||
//
|
||||
#ifndef __LIBS_ASSETDIR_H
|
||||
#define __LIBS_ASSETDIR_H
|
||||
|
||||
#include <utils/String8.h>
|
||||
#include <utils/Vector.h>
|
||||
#include <utils/SortedVector.h>
|
||||
#include <utils/misc.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
/*
|
||||
* This provides vector-style access to a directory. We do this rather
|
||||
* than modeling opendir/readdir access because it's simpler and the
|
||||
* nature of the operation requires us to have all data on hand anyway.
|
||||
*
|
||||
* The list of files will be sorted in ascending order by ASCII value.
|
||||
*
|
||||
* The contents are populated by our friend, the AssetManager.
|
||||
*/
|
||||
class AssetDir {
|
||||
public:
|
||||
AssetDir(void)
|
||||
: mFileInfo(NULL)
|
||||
{}
|
||||
virtual ~AssetDir(void) {
|
||||
delete mFileInfo;
|
||||
}
|
||||
|
||||
/*
|
||||
* Vector-style access.
|
||||
*/
|
||||
size_t getFileCount(void) { return mFileInfo->size(); }
|
||||
const String8& getFileName(int idx) {
|
||||
return mFileInfo->itemAt(idx).getFileName();
|
||||
}
|
||||
const String8& getSourceName(int idx) {
|
||||
return mFileInfo->itemAt(idx).getSourceName();
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the type of a file (usually regular or directory).
|
||||
*/
|
||||
FileType getFileType(int idx) {
|
||||
return mFileInfo->itemAt(idx).getFileType();
|
||||
}
|
||||
|
||||
private:
|
||||
/* these operations are not implemented */
|
||||
AssetDir(const AssetDir& src);
|
||||
const AssetDir& operator=(const AssetDir& src);
|
||||
|
||||
friend class AssetManager;
|
||||
|
||||
/*
|
||||
* This holds information about files in the asset hierarchy.
|
||||
*/
|
||||
class FileInfo {
|
||||
public:
|
||||
FileInfo(void) {}
|
||||
FileInfo(const String8& path) // useful for e.g. svect.indexOf
|
||||
: mFileName(path), mFileType(kFileTypeUnknown)
|
||||
{}
|
||||
~FileInfo(void) {}
|
||||
FileInfo(const FileInfo& src) {
|
||||
copyMembers(src);
|
||||
}
|
||||
const FileInfo& operator= (const FileInfo& src) {
|
||||
if (this != &src)
|
||||
copyMembers(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void copyMembers(const FileInfo& src) {
|
||||
mFileName = src.mFileName;
|
||||
mFileType = src.mFileType;
|
||||
mSourceName = src.mSourceName;
|
||||
}
|
||||
|
||||
/* need this for SortedVector; must compare only on file name */
|
||||
bool operator< (const FileInfo& rhs) const {
|
||||
return mFileName < rhs.mFileName;
|
||||
}
|
||||
|
||||
/* used by AssetManager */
|
||||
bool operator== (const FileInfo& rhs) const {
|
||||
return mFileName == rhs.mFileName;
|
||||
}
|
||||
|
||||
void set(const String8& path, FileType type) {
|
||||
mFileName = path;
|
||||
mFileType = type;
|
||||
}
|
||||
|
||||
const String8& getFileName(void) const { return mFileName; }
|
||||
void setFileName(const String8& path) { mFileName = path; }
|
||||
|
||||
FileType getFileType(void) const { return mFileType; }
|
||||
void setFileType(FileType type) { mFileType = type; }
|
||||
|
||||
const String8& getSourceName(void) const { return mSourceName; }
|
||||
void setSourceName(const String8& path) { mSourceName = path; }
|
||||
|
||||
/*
|
||||
* Handy utility for finding an entry in a sorted vector of FileInfo.
|
||||
* Returns the index of the matching entry, or -1 if none found.
|
||||
*/
|
||||
static int findEntry(const SortedVector<FileInfo>* pVector,
|
||||
const String8& fileName);
|
||||
|
||||
private:
|
||||
String8 mFileName; // filename only
|
||||
FileType mFileType; // regular, directory, etc
|
||||
|
||||
String8 mSourceName; // currently debug-only
|
||||
};
|
||||
|
||||
/* AssetManager uses this to initialize us */
|
||||
void setFileList(SortedVector<FileInfo>* list) { mFileInfo = list; }
|
||||
|
||||
SortedVector<FileInfo>* mFileInfo;
|
||||
};
|
||||
|
||||
}; // namespace android
|
||||
|
||||
#endif // __LIBS_ASSETDIR_H
|
|
@ -1,373 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2006 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//
|
||||
// Asset management class. AssetManager objects are thread-safe.
|
||||
//
|
||||
#ifndef __LIBS_ASSETMANAGER_H
|
||||
#define __LIBS_ASSETMANAGER_H
|
||||
|
||||
#include <utils/Asset.h>
|
||||
#include <utils/AssetDir.h>
|
||||
#include <utils/KeyedVector.h>
|
||||
#include <utils/String8.h>
|
||||
#include <utils/Vector.h>
|
||||
#include <utils/String16.h>
|
||||
#include <utils/ZipFileRO.h>
|
||||
#include <utils/threads.h>
|
||||
|
||||
/*
|
||||
* Native-app access is via the opaque typedef struct AAssetManager in the C namespace.
|
||||
*/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct AAssetManager { };
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Now the proper C++ android-namespace definitions
|
||||
*/
|
||||
|
||||
namespace android {
|
||||
|
||||
class Asset; // fwd decl for things that include Asset.h first
|
||||
class ResTable;
|
||||
struct ResTable_config;
|
||||
|
||||
/*
|
||||
* Every application that uses assets needs one instance of this. A
|
||||
* single instance may be shared across multiple threads, and a single
|
||||
* thread may have more than one instance (the latter is discouraged).
|
||||
*
|
||||
* The purpose of the AssetManager is to create Asset objects. To do
|
||||
* this efficiently it may cache information about the locations of
|
||||
* files it has seen. This can be controlled with the "cacheMode"
|
||||
* argument.
|
||||
*
|
||||
* The asset hierarchy may be examined like a filesystem, using
|
||||
* AssetDir objects to peruse a single directory.
|
||||
*/
|
||||
class AssetManager : public AAssetManager {
|
||||
public:
|
||||
typedef enum CacheMode {
|
||||
CACHE_UNKNOWN = 0,
|
||||
CACHE_OFF, // don't try to cache file locations
|
||||
CACHE_DEFER, // construct cache as pieces are needed
|
||||
//CACHE_SCAN, // scan full(!) asset hierarchy at init() time
|
||||
} CacheMode;
|
||||
|
||||
AssetManager(CacheMode cacheMode = CACHE_OFF);
|
||||
virtual ~AssetManager(void);
|
||||
|
||||
static int32_t getGlobalCount();
|
||||
|
||||
/*
|
||||
* Add a new source for assets. This can be called multiple times to
|
||||
* look in multiple places for assets. It can be either a directory (for
|
||||
* finding assets as raw files on the disk) or a ZIP file. This newly
|
||||
* added asset path will be examined first when searching for assets,
|
||||
* before any that were previously added.
|
||||
*
|
||||
* Returns "true" on success, "false" on failure. If 'cookie' is non-NULL,
|
||||
* then on success, *cookie is set to the value corresponding to the
|
||||
* newly-added asset source.
|
||||
*/
|
||||
bool addAssetPath(const String8& path, void** cookie);
|
||||
|
||||
/*
|
||||
* Convenience for adding the standard system assets. Uses the
|
||||
* ANDROID_ROOT environment variable to find them.
|
||||
*/
|
||||
bool addDefaultAssets();
|
||||
|
||||
/*
|
||||
* Iterate over the asset paths in this manager. (Previously
|
||||
* added via addAssetPath() and addDefaultAssets().) On first call,
|
||||
* 'cookie' must be NULL, resulting in the first cookie being returned.
|
||||
* Each next cookie will be returned there-after, until NULL indicating
|
||||
* the end has been reached.
|
||||
*/
|
||||
void* nextAssetPath(void* cookie) const;
|
||||
|
||||
/*
|
||||
* Return an asset path in the manager. 'which' must be between 0 and
|
||||
* countAssetPaths().
|
||||
*/
|
||||
String8 getAssetPath(void* cookie) const;
|
||||
|
||||
/*
|
||||
* Set the current locale and vendor. The locale can change during
|
||||
* the lifetime of an AssetManager if the user updates the device's
|
||||
* language setting. The vendor is less likely to change.
|
||||
*
|
||||
* Pass in NULL to indicate no preference.
|
||||
*/
|
||||
void setLocale(const char* locale);
|
||||
void setVendor(const char* vendor);
|
||||
|
||||
/*
|
||||
* Choose screen orientation for resources values returned.
|
||||
*/
|
||||
void setConfiguration(const ResTable_config& config, const char* locale = NULL);
|
||||
|
||||
void getConfiguration(ResTable_config* outConfig) const;
|
||||
|
||||
typedef Asset::AccessMode AccessMode; // typing shortcut
|
||||
|
||||
/*
|
||||
* Open an asset.
|
||||
*
|
||||
* This will search through locale-specific and vendor-specific
|
||||
* directories and packages to find the file.
|
||||
*
|
||||
* The object returned does not depend on the AssetManager. It should
|
||||
* be freed by calling Asset::close().
|
||||
*/
|
||||
Asset* open(const char* fileName, AccessMode mode);
|
||||
|
||||
/*
|
||||
* Open a non-asset file as an asset.
|
||||
*
|
||||
* This is for opening files that are included in an asset package
|
||||
* but aren't assets. These sit outside the usual "locale/vendor"
|
||||
* path hierarchy, and will not be seen by "AssetDir" or included
|
||||
* in our filename cache.
|
||||
*/
|
||||
Asset* openNonAsset(const char* fileName, AccessMode mode);
|
||||
|
||||
/*
|
||||
* Explicit non-asset file. The file explicitly named by the cookie (the
|
||||
* resource set to look in) and fileName will be opened and returned.
|
||||
*/
|
||||
Asset* openNonAsset(void* cookie, const char* fileName, AccessMode mode);
|
||||
|
||||
/*
|
||||
* Open a directory within the asset hierarchy.
|
||||
*
|
||||
* The contents of the directory are an amalgam of vendor-specific,
|
||||
* locale-specific, and generic assets stored loosely or in asset
|
||||
* packages. Depending on the cache setting and previous accesses,
|
||||
* this call may incur significant disk overhead.
|
||||
*
|
||||
* To open the top-level directory, pass in "".
|
||||
*/
|
||||
AssetDir* openDir(const char* dirName);
|
||||
|
||||
/*
|
||||
* Open a directory within a particular path of the asset manager.
|
||||
*
|
||||
* The contents of the directory are an amalgam of vendor-specific,
|
||||
* locale-specific, and generic assets stored loosely or in asset
|
||||
* packages. Depending on the cache setting and previous accesses,
|
||||
* this call may incur significant disk overhead.
|
||||
*
|
||||
* To open the top-level directory, pass in "".
|
||||
*/
|
||||
AssetDir* openNonAssetDir(void* cookie, const char* dirName);
|
||||
|
||||
/*
|
||||
* Get the type of a file in the asset hierarchy. They will either
|
||||
* be "regular" or "directory". [Currently only works for "regular".]
|
||||
*
|
||||
* Can also be used as a quick test for existence of a file.
|
||||
*/
|
||||
FileType getFileType(const char* fileName);
|
||||
|
||||
/*
|
||||
* Return the complete resource table to find things in the package.
|
||||
*/
|
||||
const ResTable& getResources(bool required = true) const;
|
||||
|
||||
/*
|
||||
* Discard cached filename information. This only needs to be called
|
||||
* if somebody has updated the set of "loose" files, and we want to
|
||||
* discard our cached notion of what's where.
|
||||
*/
|
||||
void purge(void) { purgeFileNameCacheLocked(); }
|
||||
|
||||
/*
|
||||
* Return true if the files this AssetManager references are all
|
||||
* up-to-date (have not been changed since it was created). If false
|
||||
* is returned, you will need to create a new AssetManager to get
|
||||
* the current data.
|
||||
*/
|
||||
bool isUpToDate();
|
||||
|
||||
/**
|
||||
* Get the known locales for this asset manager object.
|
||||
*/
|
||||
void getLocales(Vector<String8>* locales) const;
|
||||
|
||||
private:
|
||||
struct asset_path
|
||||
{
|
||||
String8 path;
|
||||
FileType type;
|
||||
String8 idmap;
|
||||
};
|
||||
|
||||
Asset* openInPathLocked(const char* fileName, AccessMode mode,
|
||||
const asset_path& path);
|
||||
Asset* openNonAssetInPathLocked(const char* fileName, AccessMode mode,
|
||||
const asset_path& path);
|
||||
Asset* openInLocaleVendorLocked(const char* fileName, AccessMode mode,
|
||||
const asset_path& path, const char* locale, const char* vendor);
|
||||
String8 createPathNameLocked(const asset_path& path, const char* locale,
|
||||
const char* vendor);
|
||||
String8 createPathNameLocked(const asset_path& path, const char* rootDir);
|
||||
String8 createZipSourceNameLocked(const String8& zipFileName,
|
||||
const String8& dirName, const String8& fileName);
|
||||
|
||||
ZipFileRO* getZipFileLocked(const asset_path& path);
|
||||
Asset* openAssetFromFileLocked(const String8& fileName, AccessMode mode);
|
||||
Asset* openAssetFromZipLocked(const ZipFileRO* pZipFile,
|
||||
const ZipEntryRO entry, AccessMode mode, const String8& entryName);
|
||||
|
||||
bool scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
|
||||
const asset_path& path, const char* rootDir, const char* dirName);
|
||||
SortedVector<AssetDir::FileInfo>* scanDirLocked(const String8& path);
|
||||
bool scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
|
||||
const asset_path& path, const char* rootDir, const char* dirName);
|
||||
void mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
|
||||
const SortedVector<AssetDir::FileInfo>* pContents);
|
||||
|
||||
void loadFileNameCacheLocked(void);
|
||||
void fncScanLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
|
||||
const char* dirName);
|
||||
bool fncScanAndMergeDirLocked(
|
||||
SortedVector<AssetDir::FileInfo>* pMergedInfo,
|
||||
const asset_path& path, const char* locale, const char* vendor,
|
||||
const char* dirName);
|
||||
void purgeFileNameCacheLocked(void);
|
||||
|
||||
const ResTable* getResTable(bool required = true) const;
|
||||
void setLocaleLocked(const char* locale);
|
||||
void updateResourceParamsLocked() const;
|
||||
|
||||
bool createIdmapFileLocked(const String8& originalPath, const String8& overlayPath,
|
||||
const String8& idmapPath);
|
||||
|
||||
bool isIdmapStaleLocked(const String8& originalPath, const String8& overlayPath,
|
||||
const String8& idmapPath);
|
||||
|
||||
Asset* openIdmapLocked(const struct asset_path& ap) const;
|
||||
|
||||
bool getZipEntryCrcLocked(const String8& zipPath, const char* entryFilename, uint32_t* pCrc);
|
||||
|
||||
class SharedZip : public RefBase {
|
||||
public:
|
||||
static sp<SharedZip> get(const String8& path);
|
||||
|
||||
ZipFileRO* getZip();
|
||||
|
||||
Asset* getResourceTableAsset();
|
||||
Asset* setResourceTableAsset(Asset* asset);
|
||||
|
||||
ResTable* getResourceTable();
|
||||
ResTable* setResourceTable(ResTable* res);
|
||||
|
||||
bool isUpToDate();
|
||||
|
||||
protected:
|
||||
~SharedZip();
|
||||
|
||||
private:
|
||||
SharedZip(const String8& path, time_t modWhen);
|
||||
SharedZip(); // <-- not implemented
|
||||
|
||||
String8 mPath;
|
||||
ZipFileRO* mZipFile;
|
||||
time_t mModWhen;
|
||||
|
||||
Asset* mResourceTableAsset;
|
||||
ResTable* mResourceTable;
|
||||
|
||||
static Mutex gLock;
|
||||
static DefaultKeyedVector<String8, wp<SharedZip> > gOpen;
|
||||
};
|
||||
|
||||
/*
|
||||
* Manage a set of Zip files. For each file we need a pointer to the
|
||||
* ZipFile and a time_t with the file's modification date.
|
||||
*
|
||||
* We currently only have two zip files (current app, "common" app).
|
||||
* (This was originally written for 8, based on app/locale/vendor.)
|
||||
*/
|
||||
class ZipSet {
|
||||
public:
|
||||
ZipSet(void);
|
||||
~ZipSet(void);
|
||||
|
||||
/*
|
||||
* Return a ZipFileRO structure for a ZipFileRO with the specified
|
||||
* parameters.
|
||||
*/
|
||||
ZipFileRO* getZip(const String8& path);
|
||||
|
||||
Asset* getZipResourceTableAsset(const String8& path);
|
||||
Asset* setZipResourceTableAsset(const String8& path, Asset* asset);
|
||||
|
||||
ResTable* getZipResourceTable(const String8& path);
|
||||
ResTable* setZipResourceTable(const String8& path, ResTable* res);
|
||||
|
||||
// generate path, e.g. "common/en-US-noogle.zip"
|
||||
static String8 getPathName(const char* path);
|
||||
|
||||
bool isUpToDate();
|
||||
|
||||
private:
|
||||
void closeZip(int idx);
|
||||
|
||||
int getIndex(const String8& zip) const;
|
||||
mutable Vector<String8> mZipPath;
|
||||
mutable Vector<sp<SharedZip> > mZipFile;
|
||||
};
|
||||
|
||||
// Protect all internal state.
|
||||
mutable Mutex mLock;
|
||||
|
||||
ZipSet mZipSet;
|
||||
|
||||
Vector<asset_path> mAssetPaths;
|
||||
char* mLocale;
|
||||
char* mVendor;
|
||||
|
||||
mutable ResTable* mResources;
|
||||
ResTable_config* mConfig;
|
||||
|
||||
/*
|
||||
* Cached data for "loose" files. This lets us avoid poking at the
|
||||
* filesystem when searching for loose assets. Each entry is the
|
||||
* "extended partial" path, e.g. "default/default/foo/bar.txt". The
|
||||
* full set of files is present, including ".EXCLUDE" entries.
|
||||
*
|
||||
* We do not cache directory names. We don't retain the ".gz",
|
||||
* because to our clients "foo" and "foo.gz" both look like "foo".
|
||||
*/
|
||||
CacheMode mCacheMode; // is the cache enabled?
|
||||
bool mCacheValid; // clear when locale or vendor changes
|
||||
SortedVector<AssetDir::FileInfo> mCache;
|
||||
};
|
||||
|
||||
}; // namespace android
|
||||
|
||||
#endif // __LIBS_ASSETMANAGER_H
|
|
@ -1,169 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2009 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _UTILS_BACKUP_HELPERS_H
|
||||
#define _UTILS_BACKUP_HELPERS_H
|
||||
|
||||
#include <utils/Errors.h>
|
||||
#include <utils/String8.h>
|
||||
#include <utils/KeyedVector.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
enum {
|
||||
BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian)
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
int type; // BACKUP_HEADER_ENTITY_V1
|
||||
int keyLen; // length of the key name, not including the null terminator
|
||||
int dataSize; // size of the data, not including the padding, -1 means delete
|
||||
} entity_header_v1;
|
||||
|
||||
struct SnapshotHeader {
|
||||
int magic0;
|
||||
int fileCount;
|
||||
int magic1;
|
||||
int totalSize;
|
||||
};
|
||||
|
||||
struct FileState {
|
||||
int modTime_sec;
|
||||
int modTime_nsec;
|
||||
int mode;
|
||||
int size;
|
||||
int crc32;
|
||||
int nameLen;
|
||||
};
|
||||
|
||||
struct FileRec {
|
||||
String8 file;
|
||||
bool deleted;
|
||||
FileState s;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Writes the data.
|
||||
*
|
||||
* If an error occurs, it poisons this object and all write calls will fail
|
||||
* with the error that occurred.
|
||||
*/
|
||||
class BackupDataWriter
|
||||
{
|
||||
public:
|
||||
BackupDataWriter(int fd);
|
||||
// does not close fd
|
||||
~BackupDataWriter();
|
||||
|
||||
status_t WriteEntityHeader(const String8& key, size_t dataSize);
|
||||
|
||||
/* Note: WriteEntityData will write arbitrary data into the file without
|
||||
* validation or a previously-supplied header. The full backup implementation
|
||||
* uses it this way to generate a controlled binary stream that is not
|
||||
* entity-structured. If the implementation here is changed, either this
|
||||
* use case must remain valid, or the full backup implementation should be
|
||||
* adjusted to use some other appropriate mechanism.
|
||||
*/
|
||||
status_t WriteEntityData(const void* data, size_t size);
|
||||
|
||||
void SetKeyPrefix(const String8& keyPrefix);
|
||||
|
||||
private:
|
||||
explicit BackupDataWriter();
|
||||
status_t write_padding_for(int n);
|
||||
|
||||
int m_fd;
|
||||
status_t m_status;
|
||||
ssize_t m_pos;
|
||||
int m_entityCount;
|
||||
String8 m_keyPrefix;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads the data.
|
||||
*
|
||||
* If an error occurs, it poisons this object and all write calls will fail
|
||||
* with the error that occurred.
|
||||
*/
|
||||
class BackupDataReader
|
||||
{
|
||||
public:
|
||||
BackupDataReader(int fd);
|
||||
// does not close fd
|
||||
~BackupDataReader();
|
||||
|
||||
status_t Status();
|
||||
status_t ReadNextHeader(bool* done, int* type);
|
||||
|
||||
bool HasEntities();
|
||||
status_t ReadEntityHeader(String8* key, size_t* dataSize);
|
||||
status_t SkipEntityData(); // must be called with the pointer at the beginning of the data.
|
||||
ssize_t ReadEntityData(void* data, size_t size);
|
||||
|
||||
private:
|
||||
explicit BackupDataReader();
|
||||
status_t skip_padding();
|
||||
|
||||
int m_fd;
|
||||
bool m_done;
|
||||
status_t m_status;
|
||||
ssize_t m_pos;
|
||||
ssize_t m_dataEndPos;
|
||||
int m_entityCount;
|
||||
union {
|
||||
int type;
|
||||
entity_header_v1 entity;
|
||||
} m_header;
|
||||
String8 m_key;
|
||||
};
|
||||
|
||||
int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
|
||||
char const* const* files, char const* const *keys, int fileCount);
|
||||
|
||||
int write_tarfile(const String8& packageName, const String8& domain,
|
||||
const String8& rootPath, const String8& filePath, BackupDataWriter* outputStream);
|
||||
|
||||
class RestoreHelperBase
|
||||
{
|
||||
public:
|
||||
RestoreHelperBase();
|
||||
~RestoreHelperBase();
|
||||
|
||||
status_t WriteFile(const String8& filename, BackupDataReader* in);
|
||||
status_t WriteSnapshot(int fd);
|
||||
|
||||
private:
|
||||
void* m_buf;
|
||||
bool m_loggedUnknownMetadata;
|
||||
KeyedVector<String8,FileRec> m_files;
|
||||
};
|
||||
|
||||
#define TEST_BACKUP_HELPERS 1
|
||||
|
||||
#if TEST_BACKUP_HELPERS
|
||||
int backup_helper_test_empty();
|
||||
int backup_helper_test_four();
|
||||
int backup_helper_test_files();
|
||||
int backup_helper_test_null_base();
|
||||
int backup_helper_test_missing_file();
|
||||
int backup_helper_test_data_writer();
|
||||
int backup_helper_test_data_reader();
|
||||
#endif
|
||||
|
||||
} // namespace android
|
||||
|
||||
#endif // _UTILS_BACKUP_HELPERS_H
|
|
@ -1,145 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OBBFILE_H_
|
||||
#define OBBFILE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include <utils/RefBase.h>
|
||||
#include <utils/String8.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
// OBB flags (bit 0)
|
||||
#define OBB_OVERLAY (1 << 0)
|
||||
#define OBB_SALTED (1 << 1)
|
||||
|
||||
class ObbFile : public RefBase {
|
||||
protected:
|
||||
virtual ~ObbFile();
|
||||
|
||||
public:
|
||||
ObbFile();
|
||||
|
||||
bool readFrom(const char* filename);
|
||||
bool readFrom(int fd);
|
||||
bool writeTo(const char* filename);
|
||||
bool writeTo(int fd);
|
||||
bool removeFrom(const char* filename);
|
||||
bool removeFrom(int fd);
|
||||
|
||||
const char* getFileName() const {
|
||||
return mFileName;
|
||||
}
|
||||
|
||||
const String8 getPackageName() const {
|
||||
return mPackageName;
|
||||
}
|
||||
|
||||
void setPackageName(String8 packageName) {
|
||||
mPackageName = packageName;
|
||||
}
|
||||
|
||||
int32_t getVersion() const {
|
||||
return mVersion;
|
||||
}
|
||||
|
||||
void setVersion(int32_t version) {
|
||||
mVersion = version;
|
||||
}
|
||||
|
||||
int32_t getFlags() const {
|
||||
return mFlags;
|
||||
}
|
||||
|
||||
void setFlags(int32_t flags) {
|
||||
mFlags = flags;
|
||||
}
|
||||
|
||||
const unsigned char* getSalt(size_t* length) const {
|
||||
if ((mFlags & OBB_SALTED) == 0) {
|
||||
*length = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*length = sizeof(mSalt);
|
||||
return mSalt;
|
||||
}
|
||||
|
||||
bool setSalt(const unsigned char* salt, size_t length) {
|
||||
if (length != sizeof(mSalt)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(mSalt, salt, sizeof(mSalt));
|
||||
mFlags |= OBB_SALTED;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isOverlay() {
|
||||
return (mFlags & OBB_OVERLAY) == OBB_OVERLAY;
|
||||
}
|
||||
|
||||
void setOverlay(bool overlay) {
|
||||
if (overlay) {
|
||||
mFlags |= OBB_OVERLAY;
|
||||
} else {
|
||||
mFlags &= ~OBB_OVERLAY;
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t get4LE(const unsigned char* buf) {
|
||||
return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
|
||||
}
|
||||
|
||||
static inline void put4LE(unsigned char* buf, uint32_t val) {
|
||||
buf[0] = val & 0xFF;
|
||||
buf[1] = (val >> 8) & 0xFF;
|
||||
buf[2] = (val >> 16) & 0xFF;
|
||||
buf[3] = (val >> 24) & 0xFF;
|
||||
}
|
||||
|
||||
private:
|
||||
/* Package name this ObbFile is associated with */
|
||||
String8 mPackageName;
|
||||
|
||||
/* Package version this ObbFile is associated with */
|
||||
int32_t mVersion;
|
||||
|
||||
/* Flags for this OBB type. */
|
||||
int32_t mFlags;
|
||||
|
||||
/* Whether the file is salted. */
|
||||
bool mSalted;
|
||||
|
||||
/* The encryption salt. */
|
||||
unsigned char mSalt[8];
|
||||
|
||||
const char* mFileName;
|
||||
|
||||
size_t mFileSize;
|
||||
|
||||
size_t mFooterStart;
|
||||
|
||||
unsigned char* mReadBuf;
|
||||
|
||||
bool parseObbFile(int fd);
|
||||
};
|
||||
|
||||
}
|
||||
#endif /* OBBFILE_H_ */
|
File diff suppressed because it is too large
Load Diff
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __LIBS_STREAMINGZIPINFLATER_H
|
||||
#define __LIBS_STREAMINGZIPINFLATER_H
|
||||
|
||||
#include <unistd.h>
|
||||
#include <inttypes.h>
|
||||
#include <zlib.h>
|
||||
|
||||
#include <utils/Compat.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
class StreamingZipInflater {
|
||||
public:
|
||||
static const size_t INPUT_CHUNK_SIZE = 64 * 1024;
|
||||
static const size_t OUTPUT_CHUNK_SIZE = 64 * 1024;
|
||||
|
||||
// Flavor that pages in the compressed data from a fd
|
||||
StreamingZipInflater(int fd, off64_t compDataStart, size_t uncompSize, size_t compSize);
|
||||
|
||||
// Flavor that gets the compressed data from an in-memory buffer
|
||||
StreamingZipInflater(class FileMap* dataMap, size_t uncompSize);
|
||||
|
||||
~StreamingZipInflater();
|
||||
|
||||
// read 'count' bytes of uncompressed data from the current position. outBuf may
|
||||
// be NULL, in which case the data is consumed and discarded.
|
||||
ssize_t read(void* outBuf, size_t count);
|
||||
|
||||
// seeking backwards requires uncompressing fom the beginning, so is very
|
||||
// expensive. seeking forwards only requires uncompressing from the current
|
||||
// position to the destination.
|
||||
off64_t seekAbsolute(off64_t absoluteInputPosition);
|
||||
|
||||
private:
|
||||
void initInflateState();
|
||||
int readNextChunk();
|
||||
|
||||
// where to find the uncompressed data
|
||||
int mFd;
|
||||
off64_t mInFileStart; // where the compressed data lives in the file
|
||||
class FileMap* mDataMap;
|
||||
|
||||
z_stream mInflateState;
|
||||
bool mStreamNeedsInit;
|
||||
|
||||
// output invariants for this asset
|
||||
uint8_t* mOutBuf; // output buf for decompressed bytes
|
||||
size_t mOutBufSize; // allocated size of mOutBuf
|
||||
size_t mOutTotalSize; // total uncompressed size of the blob
|
||||
|
||||
// current output state bookkeeping
|
||||
off64_t mOutCurPosition; // current position in total offset
|
||||
size_t mOutLastDecoded; // last decoded byte + 1 in mOutbuf
|
||||
size_t mOutDeliverable; // next undelivered byte of decoded output in mOutBuf
|
||||
|
||||
// input invariants
|
||||
uint8_t* mInBuf;
|
||||
size_t mInBufSize; // allocated size of mInBuf;
|
||||
size_t mInTotalSize; // total size of compressed data for this blob
|
||||
|
||||
// input state bookkeeping
|
||||
size_t mInNextChunkOffset; // offset from start of blob at which the next input chunk lies
|
||||
// the z_stream contains state about input block consumption
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,61 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//
|
||||
// C API for ead-only access to Zip archives, with minimal heap allocation.
|
||||
//
|
||||
#ifndef __LIBS_ZIPFILECRO_H
|
||||
#define __LIBS_ZIPFILECRO_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <utils/Compat.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Trivial typedef to ensure that ZipFileCRO is not treated as a simple integer.
|
||||
*/
|
||||
typedef void* ZipFileCRO;
|
||||
|
||||
/*
|
||||
* Trivial typedef to ensure that ZipEntryCRO is not treated as a simple
|
||||
* integer. We use NULL to indicate an invalid value.
|
||||
*/
|
||||
typedef void* ZipEntryCRO;
|
||||
|
||||
extern ZipFileCRO ZipFileXRO_open(const char* path);
|
||||
|
||||
extern void ZipFileCRO_destroy(ZipFileCRO zip);
|
||||
|
||||
extern ZipEntryCRO ZipFileCRO_findEntryByName(ZipFileCRO zip,
|
||||
const char* fileName);
|
||||
|
||||
extern bool ZipFileCRO_getEntryInfo(ZipFileCRO zip, ZipEntryCRO entry,
|
||||
int* pMethod, size_t* pUncompLen,
|
||||
size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32);
|
||||
|
||||
extern bool ZipFileCRO_uncompressEntry(ZipFileCRO zip, ZipEntryCRO entry, int fd);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*__LIBS_ZIPFILECRO_H*/
|
|
@ -1,262 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2007 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Read-only access to Zip archives, with minimal heap allocation.
|
||||
*
|
||||
* This is similar to the more-complete ZipFile class, but no attempt
|
||||
* has been made to make them interchangeable. This class operates under
|
||||
* a very different set of assumptions and constraints.
|
||||
*
|
||||
* One such assumption is that if you're getting file descriptors for
|
||||
* use with this class as a child of a fork() operation, you must be on
|
||||
* a pread() to guarantee correct operation. This is because pread() can
|
||||
* atomically read at a file offset without worrying about a lock around an
|
||||
* lseek() + read() pair.
|
||||
*/
|
||||
#ifndef __LIBS_ZIPFILERO_H
|
||||
#define __LIBS_ZIPFILERO_H
|
||||
|
||||
#include <utils/Compat.h>
|
||||
#include <utils/Errors.h>
|
||||
#include <utils/FileMap.h>
|
||||
#include <utils/threads.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
/*
|
||||
* Trivial typedef to ensure that ZipEntryRO is not treated as a simple
|
||||
* integer. We use NULL to indicate an invalid value.
|
||||
*/
|
||||
typedef void* ZipEntryRO;
|
||||
|
||||
/*
|
||||
* Open a Zip archive for reading.
|
||||
*
|
||||
* We want "open" and "find entry by name" to be fast operations, and we
|
||||
* want to use as little memory as possible. We memory-map the file,
|
||||
* and load a hash table with pointers to the filenames (which aren't
|
||||
* null-terminated). The other fields are at a fixed offset from the
|
||||
* filename, so we don't need to extract those (but we do need to byte-read
|
||||
* and endian-swap them every time we want them).
|
||||
*
|
||||
* To speed comparisons when doing a lookup by name, we could make the mapping
|
||||
* "private" (copy-on-write) and null-terminate the filenames after verifying
|
||||
* the record structure. However, this requires a private mapping of
|
||||
* every page that the Central Directory touches. Easier to tuck a copy
|
||||
* of the string length into the hash table entry.
|
||||
*
|
||||
* NOTE: If this is used on file descriptors inherited from a fork() operation,
|
||||
* you must be on a platform that implements pread() to guarantee correctness
|
||||
* on the shared file descriptors.
|
||||
*/
|
||||
class ZipFileRO {
|
||||
public:
|
||||
ZipFileRO()
|
||||
: mFd(-1), mFileName(NULL), mFileLength(-1),
|
||||
mDirectoryMap(NULL),
|
||||
mNumEntries(-1), mDirectoryOffset(-1),
|
||||
mHashTableSize(-1), mHashTable(NULL)
|
||||
{}
|
||||
|
||||
~ZipFileRO();
|
||||
|
||||
/*
|
||||
* Open an archive.
|
||||
*/
|
||||
status_t open(const char* zipFileName);
|
||||
|
||||
/*
|
||||
* Find an entry, by name. Returns the entry identifier, or NULL if
|
||||
* not found.
|
||||
*
|
||||
* If two entries have the same name, one will be chosen at semi-random.
|
||||
*/
|
||||
ZipEntryRO findEntryByName(const char* fileName) const;
|
||||
|
||||
/*
|
||||
* Return the #of entries in the Zip archive.
|
||||
*/
|
||||
int getNumEntries(void) const {
|
||||
return mNumEntries;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the Nth entry. Zip file entries are not stored in sorted
|
||||
* order, and updated entries may appear at the end, so anyone walking
|
||||
* the archive needs to avoid making ordering assumptions. We take
|
||||
* that further by returning the Nth non-empty entry in the hash table
|
||||
* rather than the Nth entry in the archive.
|
||||
*
|
||||
* Valid values are [0..numEntries).
|
||||
*
|
||||
* [This is currently O(n). If it needs to be fast we can allocate an
|
||||
* additional data structure or provide an iterator interface.]
|
||||
*/
|
||||
ZipEntryRO findEntryByIndex(int idx) const;
|
||||
|
||||
/*
|
||||
* Copy the filename into the supplied buffer. Returns 0 on success,
|
||||
* -1 if "entry" is invalid, or the filename length if it didn't fit. The
|
||||
* length, and the returned string, include the null-termination.
|
||||
*/
|
||||
int getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen) const;
|
||||
|
||||
/*
|
||||
* Get the vital stats for an entry. Pass in NULL pointers for anything
|
||||
* you don't need.
|
||||
*
|
||||
* "*pOffset" holds the Zip file offset of the entry's data.
|
||||
*
|
||||
* Returns "false" if "entry" is bogus or if the data in the Zip file
|
||||
* appears to be bad.
|
||||
*/
|
||||
bool getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
|
||||
size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32) const;
|
||||
|
||||
/*
|
||||
* Create a new FileMap object that maps a subset of the archive. For
|
||||
* an uncompressed entry this effectively provides a pointer to the
|
||||
* actual data, for a compressed entry this provides the input buffer
|
||||
* for inflate().
|
||||
*/
|
||||
FileMap* createEntryFileMap(ZipEntryRO entry) const;
|
||||
|
||||
/*
|
||||
* Uncompress the data into a buffer. Depending on the compression
|
||||
* format, this is either an "inflate" operation or a memcpy.
|
||||
*
|
||||
* Use "uncompLen" from getEntryInfo() to determine the required
|
||||
* buffer size.
|
||||
*
|
||||
* Returns "true" on success.
|
||||
*/
|
||||
bool uncompressEntry(ZipEntryRO entry, void* buffer) const;
|
||||
|
||||
/*
|
||||
* Uncompress the data to an open file descriptor.
|
||||
*/
|
||||
bool uncompressEntry(ZipEntryRO entry, int fd) const;
|
||||
|
||||
/* Zip compression methods we support */
|
||||
enum {
|
||||
kCompressStored = 0, // no compression
|
||||
kCompressDeflated = 8, // standard deflate
|
||||
};
|
||||
|
||||
/*
|
||||
* Utility function: uncompress deflated data, buffer to buffer.
|
||||
*/
|
||||
static bool inflateBuffer(void* outBuf, const void* inBuf,
|
||||
size_t uncompLen, size_t compLen);
|
||||
|
||||
/*
|
||||
* Utility function: uncompress deflated data, buffer to fd.
|
||||
*/
|
||||
static bool inflateBuffer(int fd, const void* inBuf,
|
||||
size_t uncompLen, size_t compLen);
|
||||
|
||||
/*
|
||||
* Utility function to convert ZIP's time format to a timespec struct.
|
||||
*/
|
||||
static inline void zipTimeToTimespec(long when, struct tm* timespec) {
|
||||
const long date = when >> 16;
|
||||
timespec->tm_year = ((date >> 9) & 0x7F) + 80; // Zip is years since 1980
|
||||
timespec->tm_mon = (date >> 5) & 0x0F;
|
||||
timespec->tm_mday = date & 0x1F;
|
||||
|
||||
timespec->tm_hour = (when >> 11) & 0x1F;
|
||||
timespec->tm_min = (when >> 5) & 0x3F;
|
||||
timespec->tm_sec = (when & 0x1F) << 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Some basic functions for raw data manipulation. "LE" means
|
||||
* Little Endian.
|
||||
*/
|
||||
static inline unsigned short get2LE(const unsigned char* buf) {
|
||||
return buf[0] | (buf[1] << 8);
|
||||
}
|
||||
static inline unsigned long get4LE(const unsigned char* buf) {
|
||||
return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
|
||||
}
|
||||
|
||||
private:
|
||||
/* these are private and not defined */
|
||||
ZipFileRO(const ZipFileRO& src);
|
||||
ZipFileRO& operator=(const ZipFileRO& src);
|
||||
|
||||
/* locate and parse the central directory */
|
||||
bool mapCentralDirectory(void);
|
||||
|
||||
/* parse the archive, prepping internal structures */
|
||||
bool parseZipArchive(void);
|
||||
|
||||
/* add a new entry to the hash table */
|
||||
void addToHash(const char* str, int strLen, unsigned int hash);
|
||||
|
||||
/* compute string hash code */
|
||||
static unsigned int computeHash(const char* str, int len);
|
||||
|
||||
/* convert a ZipEntryRO back to a hash table index */
|
||||
int entryToIndex(const ZipEntryRO entry) const;
|
||||
|
||||
/*
|
||||
* One entry in the hash table.
|
||||
*/
|
||||
typedef struct HashEntry {
|
||||
const char* name;
|
||||
unsigned short nameLen;
|
||||
//unsigned int hash;
|
||||
} HashEntry;
|
||||
|
||||
/* open Zip archive */
|
||||
int mFd;
|
||||
|
||||
/* Lock for handling the file descriptor (seeks, etc) */
|
||||
mutable Mutex mFdLock;
|
||||
|
||||
/* zip file name */
|
||||
char* mFileName;
|
||||
|
||||
/* length of file */
|
||||
size_t mFileLength;
|
||||
|
||||
/* mapped file */
|
||||
FileMap* mDirectoryMap;
|
||||
|
||||
/* number of entries in the Zip archive */
|
||||
int mNumEntries;
|
||||
|
||||
/* CD directory offset in the Zip archive */
|
||||
off64_t mDirectoryOffset;
|
||||
|
||||
/*
|
||||
* We know how many entries are in the Zip archive, so we have a
|
||||
* fixed-size hash table. We probe for an empty slot.
|
||||
*/
|
||||
int mHashTableSize;
|
||||
HashEntry* mHashTable;
|
||||
};
|
||||
|
||||
}; // namespace android
|
||||
|
||||
#endif /*__LIBS_ZIPFILERO_H*/
|
|
@ -1,67 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2007 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
//
|
||||
// Miscellaneous zip/gzip utility functions.
|
||||
//
|
||||
#ifndef __LIBS_ZIPUTILS_H
|
||||
#define __LIBS_ZIPUTILS_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
/*
|
||||
* Container class for utility functions, primarily for namespace reasons.
|
||||
*/
|
||||
class ZipUtils {
|
||||
public:
|
||||
/*
|
||||
* General utility function for uncompressing "deflate" data from a file
|
||||
* to a buffer.
|
||||
*/
|
||||
static bool inflateToBuffer(int fd, void* buf, long uncompressedLen,
|
||||
long compressedLen);
|
||||
static bool inflateToBuffer(FILE* fp, void* buf, long uncompressedLen,
|
||||
long compressedLen);
|
||||
|
||||
/*
|
||||
* Someday we might want to make this generic and handle bzip2 ".bz2"
|
||||
* files too.
|
||||
*
|
||||
* We could declare gzip to be a sub-class of zip that has exactly
|
||||
* one always-compressed entry, but we currently want to treat Zip
|
||||
* and gzip as distinct, so there's no value.
|
||||
*
|
||||
* The zlib library has some gzip utilities, but it has no interface
|
||||
* for extracting the uncompressed length of the file (you do *not*
|
||||
* want to gzseek to the end).
|
||||
*
|
||||
* Pass in a seeked file pointer for the gzip file. If this is a gzip
|
||||
* file, we set our return values appropriately and return "true" with
|
||||
* the file seeked to the start of the compressed data.
|
||||
*/
|
||||
static bool examineGzip(FILE* fp, int* pCompressionMethod,
|
||||
long* pUncompressedLen, long* pCompressedLen, unsigned long* pCRC32);
|
||||
|
||||
private:
|
||||
ZipUtils() {}
|
||||
~ZipUtils() {}
|
||||
};
|
||||
|
||||
}; // namespace android
|
||||
|
||||
#endif /*__LIBS_ZIPUTILS_H*/
|
|
@ -21,23 +21,23 @@
|
|||
#define LOG_TAG "asset"
|
||||
//#define NDEBUG 0
|
||||
|
||||
#include <utils/Asset.h>
|
||||
#include <androidfw/Asset.h>
|
||||
#include <androidfw/StreamingZipInflater.h>
|
||||
#include <androidfw/ZipFileRO.h>
|
||||
#include <androidfw/ZipUtils.h>
|
||||
#include <utils/Atomic.h>
|
||||
#include <utils/FileMap.h>
|
||||
#include <utils/StreamingZipInflater.h>
|
||||
#include <utils/ZipUtils.h>
|
||||
#include <utils/ZipFileRO.h>
|
||||
#include <utils/Log.h>
|
||||
#include <utils/threads.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <memory.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace android;
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
// implementation is in the header file or in friend functions in
|
||||
// AssetManager.
|
||||
//
|
||||
#include <utils/AssetDir.h>
|
||||
#include <androidfw/AssetDir.h>
|
||||
|
||||
using namespace android;
|
||||
|
||||
|
|
|
@ -21,23 +21,23 @@
|
|||
#define LOG_TAG "asset"
|
||||
//#define LOG_NDEBUG 0
|
||||
|
||||
#include <utils/AssetManager.h>
|
||||
#include <utils/AssetDir.h>
|
||||
#include <utils/Asset.h>
|
||||
#include <androidfw/Asset.h>
|
||||
#include <androidfw/AssetDir.h>
|
||||
#include <androidfw/AssetManager.h>
|
||||
#include <androidfw/ResourceTypes.h>
|
||||
#include <androidfw/ZipFileRO.h>
|
||||
#include <utils/Atomic.h>
|
||||
#include <utils/String8.h>
|
||||
#include <utils/ResourceTypes.h>
|
||||
#include <utils/String8.h>
|
||||
#include <utils/ZipFileRO.h>
|
||||
#include <utils/Log.h>
|
||||
#include <utils/Timers.h>
|
||||
#include <utils/String8.h>
|
||||
#include <utils/String8.h>
|
||||
#include <utils/threads.h>
|
||||
#include <utils/Timers.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <strings.h>
|
||||
#include <fcntl.h>
|
||||
#include <strings.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
#define LOG_TAG "backup_data"
|
||||
|
||||
#include <utils/BackupHelpers.h>
|
||||
#include <androidfw/BackupHelpers.h>
|
||||
#include <utils/ByteOrder.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
#define LOG_TAG "file_backup_helper"
|
||||
|
||||
#include <utils/BackupHelpers.h>
|
||||
#include <androidfw/BackupHelpers.h>
|
||||
|
||||
#include <utils/KeyedVector.h>
|
||||
#include <utils/ByteOrder.h>
|
||||
|
|
|
@ -23,9 +23,9 @@
|
|||
|
||||
#define LOG_TAG "ObbFile"
|
||||
|
||||
#include <androidfw/ObbFile.h>
|
||||
#include <utils/Compat.h>
|
||||
#include <utils/Log.h>
|
||||
#include <utils/ObbFile.h>
|
||||
|
||||
//#define DEBUG 1
|
||||
|
||||
|
|
|
@ -17,14 +17,14 @@
|
|||
#define LOG_TAG "ResourceType"
|
||||
//#define LOG_NDEBUG 0
|
||||
|
||||
#include <androidfw/ResourceTypes.h>
|
||||
#include <utils/Atomic.h>
|
||||
#include <utils/ByteOrder.h>
|
||||
#include <utils/Debug.h>
|
||||
#include <utils/ResourceTypes.h>
|
||||
#include <utils/Log.h>
|
||||
#include <utils/String16.h>
|
||||
#include <utils/String8.h>
|
||||
#include <utils/TextOutput.h>
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
|
|
@ -18,8 +18,8 @@
|
|||
#define LOG_TAG "szipinf"
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include <androidfw/StreamingZipInflater.h>
|
||||
#include <utils/FileMap.h>
|
||||
#include <utils/StreamingZipInflater.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <assert.h>
|
||||
|
|
|
@ -14,8 +14,8 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <utils/ZipFileCRO.h>
|
||||
#include <utils/ZipFileRO.h>
|
||||
#include <androidfw/ZipFileCRO.h>
|
||||
#include <androidfw/ZipFileRO.h>
|
||||
|
||||
using namespace android;
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
//
|
||||
#define LOG_TAG "zipro"
|
||||
//#define LOG_NDEBUG 0
|
||||
#include <utils/ZipFileRO.h>
|
||||
#include <androidfw/ZipFileRO.h>
|
||||
#include <utils/Log.h>
|
||||
#include <utils/misc.h>
|
||||
#include <utils/threads.h>
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
|
||||
#define LOG_TAG "ziputil"
|
||||
|
||||
#include <utils/ZipUtils.h>
|
||||
#include <utils/ZipFileRO.h>
|
||||
#include <androidfw/ZipUtils.h>
|
||||
#include <androidfw/ZipFileRO.h>
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
*/
|
||||
|
||||
#define LOG_TAG "ObbFile_test"
|
||||
#include <androidfw/ObbFile.h>
|
||||
#include <utils/Log.h>
|
||||
#include <utils/ObbFile.h>
|
||||
#include <utils/RefBase.h>
|
||||
#include <utils/String8.h>
|
||||
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
*/
|
||||
|
||||
#define LOG_TAG "ZipFileRO_test"
|
||||
#include <androidfw/ZipFileRO.h>
|
||||
#include <utils/Log.h>
|
||||
#include <utils/ZipFileRO.h>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
|
Loading…
Reference in New Issue