讀取 dex檔案中的所有字串

2021-06-29 14:49:20 字數 1895 閱讀 6092

import struct

import os

#這裡定義乙個讀取字串長度的函式

def decunsignedleb128(file):

result = struct.unpack("i", file.read(4))[0]#讀取4位元組中的第乙個位元組

result = result&0x000000ff

file.seek(-3, 1) #倒退回前面的第三個位元組 # 不能直接從1位元組強轉為4位元組,所以先取4位元組,再清空3位元組

if (result > 0x7f):

next = struct.unpack("i", file.read(4))[0]

next = next&0x000000ff #第一位是個位

file.seek(-3, 1)

result = (result&0x7f) | (next&0x7f)<<7

if(next > 0x7f):

next = struct.unpack("i", file.read(4))[0]

next = next&0x000000ff #加入十位

file.seek(-3, 1)

result = result | (next&0x7f)<<14

if(next > 0x7f):

next = struct.unpack("i", file.read(4))[0]

next = next&0x000000ff

file.seek(-3, 1)

result = result | (next&0x7f)<<21

if(next > 0x7f):

next = struct.unpack("i", file.read(4))[0]

next = next&0x000000ff

file.seek(-3, 1)

result = result | next<<28

#print "result:", result

return result

dex = open("imisstest.dex", 'rb') #rb的意思是 read and write in binary file

dex.seek(0x38, 0)#string table的偏移

tmp = dex.read(8)

string_count, string_table_off = struct.unpack("ii", tmp) #"ii"是分別讀取的意思

print ("size:", string_count, " off:", string_table_off)

dex.seek(string_table_off, 0)

dexstrofflist =

count = 0

while(countcount+=1

dexstrlist =

nonullcount = 0

for stroff in dexstrofflist:

dex.seek(stroff, 0)

strlen = decunsignedleb128(dex)

if(strlen == 0):

continue

input = dex.read(strlen)

nonullcount+=1

outputfile = open("string.txt", "w")

count = 0

print ("string:",string_count)

for i in dexstrlist:

outputfile.write('%s\n'%i) #將元組中的元素寫入檔案

outputfile.close()

dex.close()

從檔案中讀取字串

問題 如何從磁碟檔案中讀取字串兒,然後按行將其倒序輸出?下面一段材料取自陳壽 三國志 卷三十五諸葛亮傳,我把它分成了10段,現在要求從磁碟檔案中讀取這段文字,然後按照一定的規則倒序輸出。比如說,如果我要求輸出3行,系統會依次輸出第10 9 8三行 如果我要求輸出20行,則系統會依次輸出10 9 8 ...

C語言讀取檔案中字串

int get key value char path,char key str,int val long file len fseek fp,0,seek end 將檔案指標移動到檔案結尾,成功返回0,不成功返回 1 file len ftell fp 求出當前檔案指標距離檔案開始的位元組數 fs...

字串讀取

有兩種方法可以讀c 字串 使用提取操作符 和getline函式。1 字串提取操作符 首先,它跳過前導空白字元,然後提取所有鄰近的非空白字元。當發現空白字元時,它就停下來。終止空白字元被留在輸入流中.提取操作符可以用來從鍵盤 cin流 或者使用開放式檔案流從檔案讀資料。例如,要把乙個值讀入字串 物件s...