`
yuanjinxiu
  • 浏览: 658131 次
文章分类
社区版块
存档分类
最新评论

Android培训班(65)dex文件打开

 
阅读更多

我们知道,要读取一个类代码,或读取类里的方法代码,都需要打开Dex文件,然后按前面介绍的格式去分析,并且读取出相应的内容,才可以给虚拟机进行解释执行。现在,我们就来学习和分析Dex文件的读取相关的代码。如下:

/*

*Open the specified file read-only. We memory-map the entire thingand

*parse the contents.

*

*This will be called on non-Zip files, especially during VM startup,so

*we don't want to be too noisy about certain types of failure. (Do

*we want a "quiet" flag?)

*

*On success, we fill out the contents of "pArchive" andreturn 0.

*/

intdexZipOpenArchive(const char* fileName, ZipArchive* pArchive)

{

上面这段代码输入文件名称,输出文件内容对象。


int fd, err;


LOGV("Opening archive '%s' %p\n",fileName, pArchive);


memset(pArchive, 0, sizeof(ZipArchive));

上面这行代码清空Dex文件对象。



fd = open(fileName, O_RDONLY, 0);

if (fd < 0) {

err = errno ? errno : -1;

LOGV("Unable to open '%s': %s\n",fileName, strerror(err));

return err;

}

这段代码调用函数open来打开文件,如果不成功就输出出错提示。



return dexZipPrepArchive(fd, fileName,pArchive);

这行代码调用函数dexZipPrepArchive来生成dex文件对象。


}


在上面这个函数里,主要输入两个参数:fileNamepArchive。其中fileName是输入要打开的dex文件名称,当然它是包括文件路径的;pArchive是打开这个文件后用来表达dex文件内容的对象。代码具体过程是先调用函数memset来清空pArchive文件对象,然后调用函数open打开文件,把文件句柄传送给函数dexZipPrepArchive

行文件分析,并把相应内容设置给文件对象pArchive


下面来看一下pArchive对象的结构,如下:

typedefstruct ZipArchive {

/* open Zip archive */

int mFd;

这个zip文件的文件句柄,也就是上面调用open函数打开成功后句柄。


/* mapped file */

MemMapping mMap;

这个是把zip文件映射到内存,加快文件读取,提高文件读取效率。



/* number of entries in the Zip archive */

int mNumEntries;

这个是保存zip文件的入口。


/*

* We know how many entries are in the Ziparchive, so we can have a

* fixed-size hash table. We probe oncollisions.

*/

int mHashTableSize;

ZipHashEntry* mHashTable;

这里是通过hash的方法来提高读取文件速度。


}ZipArchive;


ZipArchive结构保存zip文件的句柄、文件内容映射内存地址、zip入口个数和入口地址(使用hash表达)。


这一段代码实现打开Dex文件,由于Dex文件采用zip压缩,所以需要先从zip文件里解压出来,才可以恢复到Dex原始数据。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics