[http://groups.google.com/group/Idea-Torrent/t/eef589e80ba5baca?hl=zh-CN 抽取EXE,DLL文件的版权信息]的小程序,来珠海后的第一个工作程序 ::-- ZoomQuiet [DateTime(2007-07-17T09:32:46Z)] TableOfContents

Include(CPUGnav)

1. 抽取EXE,DLL文件的版权

{{{Albert Lee <[email protected]> hide details 4:19 pm (36 minutes ago)

}}}

   1 #coding=utf-8
   2 from ctypes import *
   3 import md5
   4 import sha
   5 
   6 _MAX_PATH = 260
   7 
   8 WORD = c_ushort
   9 UINT = c_uint
  10 BYTE = c_ubyte
  11 
  12 blocks = [u"Comments",
  13            u"InternalName",
  14            u"ProductName",
  15            u"CompanyName",
  16            u"LegalCopyright",
  17            u"ProductVersion",
  18            u"FileDescription",
  19            u"LegalTrademarks",
  20            u"PrivateBuild",
  21            u"FileVersion",
  22            u"OriginalFilename",
  23            u"SpecialBuild"]
  24 
  25 class LANGANDCODEPAGE(Structure):
  26    _fields_ = [("wLanguage", WORD),
  27                ("wCodePage", WORD)]
  28 
  29 def get_infos(path):
  30    info_d = {}
  31    dll = windll.version
  32    buf_size = dll.GetFileVersionInfoSizeA(path, 0)
  33    if buf_size < 0:
  34        print "error:", buf_size
  35        return info_d
  36 
  37    buf = (c_ubyte * buf_size)()
  38    ret = dll.GetFileVersionInfoA(path, 0, buf_size, buf)
  39    if not ret:
  40        print "GetFileVersionInfo Error"
  41        return info_d
  42 
  43    # Get the translate CodePage infomation
  44    translate = LANGANDCODEPAGE(0, 0)
  45    cbTranslate = c_uint(0)
  46 
  47    ptrans_buf = c_void_p()
  48    ret = dll.VerQueryValueW(buf,
  49                       u"\\VarFileInfo\\Translation",
  50                       byref(ptrans_buf),
  51                       byref(cbTranslate))
  52 
  53    tmp = (c_ushort * 2)()
  54    memmove(tmp, ptrans_buf, 4)
  55    translate.wLanguage = tmp[0]
  56    translate.wCodePage = tmp[1]
  57    del tmp
  58 
  59    for i in range(cbTranslate.value / sizeof(translate)):
  60        for block in blocks[:]:
  61            print "\n", block,":",
  62 
  63            p = u"\\StringFileInfo\\%04x%04x\\%s" %
  64 (translate.wLanguage, translate.wCodePage, block)
  65            line = (c_wchar * _MAX_PATH)()
  66            lpData = c_void_p()
  67            uDataSize = c_uint(0)
  68            ps = pointer(uDataSize)
  69            ret = dll.VerQueryValueW(buf, p, byref(lpData), ps)
  70            if ret:
  71                memmove(line, lpData, uDataSize.value * sizeof(c_wchar))
  72 
  73                s = u""
  74                for pos in range(uDataSize.value, 0, -1):
  75                    if line[pos] != '\0':
  76                        break
  77 
  78                for c in line[0:pos+1]:
  79                    s += c
  80 
  81                info_d[block] = s
  82                print s
  83    return info_d
  84 
  85 def get_hash(path, method):
  86    mods = {"md5": md5, "sha1" : sha}
  87    try:
  88        mod = mods[method]
  89        f = file(path, 'rb')
  90        result = mod.new(f.read()).hexdigest()
  91        f.close()
  92        return result
  93    except:
  94        return -1
  95 
  96 get_md5 = lambda path:get_hash(path, "md5")
  97 get_sha1 = lambda path:get_hash(path, "sha1")
  98 
  99 def _test():
 100    test_path = "somefile.exe"
 101    info = get_infos(test_path)
 102    print info
 103 
 104    md = get_md5(test_path)
 105    print "md5:", md
 106 
 107    s = get_sha1(test_path)
 108    print "sha1:", s
 109 
 110 if __name__=='__main__':
 111    _test()

1.1. 反馈

PageComment2