msgbox

library.ahk

May 29th, 2011
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. pause::pause
  4.  
  5.  
  6. MoveWin:
  7. PostMessage, 0xA1, 2
  8. return
  9.  
  10.  
  11. decodehash(hash)
  12. {
  13.     hash:=substr(hash,-4) . substr(hash,5,-8)
  14.     return strrev(hash)
  15. }
  16.  
  17. strrev(Str) {
  18.     DllCall("msvcrt\_" ( A_IsUnicode ? "wcs" : "str" ) "rev", "UInt",&Str, "CDecl")
  19.     return Str
  20. }
  21.  
  22.  
  23. Text_AES(ByRef sResult, pData, nSize, sPassword, SID = 256, bEncrypt = True)
  24. {
  25.    VarSetCapacity(sResult, bEncrypt ? nSize+16 : nSize)
  26.    DllCall("RtlMoveMemory", "Uint", &sResult, "Uint", pData, "Uint", nSize)
  27.    nSize := Crypt_AES(&sResult, nSize, sPassword, SID, bEncrypt)
  28.    NumPut(0, sResult, nSize, "char"), VarSetCapacity(sResult,-1)
  29.    Return   nSize
  30. }
  31.  
  32. Hex4Bin(p, l)
  33. {
  34.    VarSetCapacity(h,l*2)
  35.    o:=A_FormatInteger
  36.    SetFormat, Integer, H
  37.    Loop,   %l%
  38.       h.=SubStr(*p++,-1)
  39.    SetFormat, Integer, %o%
  40.    StringReplace,   h, h, x, 0, All
  41.    Return   h
  42. }
  43.  
  44.  
  45. Ansi2UTF8(sString)
  46. {
  47.    Ansi2Unicode(sString, wString, 0)
  48.    Unicode2Ansi(wString, zString, 65001)
  49.    Return zString
  50. }
  51.  
  52. UTF82Ansi(zString)
  53. {
  54.    Ansi2Unicode(zString, wString, 65001)
  55.    Unicode2Ansi(wString, sString, 0)
  56.    Return sString
  57. }
  58.  
  59.  
  60. Ansi2Unicode(ByRef sString, ByRef wString, CP = 0)
  61. {
  62.      nSize := DllCall("MultiByteToWideChar"
  63.       , "Uint", CP
  64.       , "Uint", 0
  65.       , "Uint", &sString
  66.       , "int",  -1
  67.       , "Uint", 0
  68.       , "int",  0)
  69.  
  70.    VarSetCapacity(wString, nSize * 2)
  71.  
  72.    DllCall("MultiByteToWideChar"
  73.       , "Uint", CP
  74.       , "Uint", 0
  75.       , "Uint", &sString
  76.       , "int",  -1
  77.       , "Uint", &wString
  78.       , "int",  nSize)
  79. }
  80.  
  81. Unicode2Ansi(ByRef wString, ByRef sString, CP = 0)
  82. {
  83.      nSize := DllCall("WideCharToMultiByte"
  84.       , "Uint", CP
  85.       , "Uint", 0
  86.       , "Uint", &wString
  87.       , "int",  -1
  88.       , "Uint", 0
  89.       , "int",  0
  90.       , "Uint", 0
  91.       , "Uint", 0)
  92.  
  93.    VarSetCapacity(sString, nSize)
  94.  
  95.    DllCall("WideCharToMultiByte"
  96.       , "Uint", CP
  97.       , "Uint", 0
  98.       , "Uint", &wString
  99.       , "int",  -1
  100.       , "str",  sString
  101.       , "int",  nSize
  102.       , "Uint", 0
  103.       , "Uint", 0)
  104. }
  105.  
  106. Bin4Hex(ByRef b, h)
  107. {
  108.    VarSetCapacity(b,l:=StrLen(h)//2)
  109.    Loop,   %l%
  110.       NumPut("0x" . SubStr(h,2*A_Index-1,2),b,A_Index-1,"Uchar")
  111.    NumPut(0,b,l,"Uchar"), VarSetCapacity(b,-1)
  112.    Return   l
  113. }
  114.  
  115.  
  116. Crypt_AES(pData, nSize, sPassword, SID = 256, bEncrypt = True) {
  117.    CALG_AES_256 := 1 + CALG_AES_192 := 1 + CALG_AES_128 := 0x660E
  118.    CALG_SHA1 := 1 + CALG_MD5 := 0x8003
  119.    DllCall("advapi32\CryptAcquireContextA", "UintP", hProv, "Uint", 0, "str"
  120.    , "Microsoft Enhanced RSA and AES Cryptographic Provider" . (A_OSVersion="WIN_XP" ? " (Prototype)" : ""), "Uint", 24, "Uint", 0)
  121.    DllCall("advapi32\CryptCreateHash", "Uint", hProv, "Uint", CALG_SHA1, "Uint", 0, "Uint", 0, "UintP", hHash)
  122.    DllCall("advapi32\CryptHashData", "Uint", hHash, "Uint", &sPassword, "Uint", StrLen(sPassword), "Uint", 0)
  123.    DllCall("advapi32\CryptDeriveKey", "Uint", hProv, "Uint", CALG_AES_%SID%, "Uint", hHash, "Uint", SID<<16, "UintP", hKey)
  124.    DllCall("advapi32\CryptDestroyHash", "Uint", hHash)
  125.    If bEncrypt
  126.       DllCall("advapi32\CryptEncrypt", "Uint", hKey, "Uint", 0, "Uint", True, "Uint", 0, "Uint", pData, "UintP", nSize, "Uint", nSize+16)
  127.    Else  DllCall("advapi32\CryptDecrypt", "Uint", hKey, "Uint", 0, "Uint", True, "Uint", 0, "Uint", pData, "UintP", nSize)
  128.    DllCall("advapi32\CryptDestroyKey", "Uint", hKey)
  129.    DllCall("advapi32\CryptReleaseContext", "Uint", hProv, "Uint", 0)
  130.    Return   nSize
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. write_bin(byref bin,filename,size){
  141.    
  142.     filename:=SubStr(filename, 1, 200)
  143.    
  144.    
  145.    h := DllCall("CreateFile","str",filename,"Uint",0x40000000
  146.             ,"Uint",0,"UInt",0,"UInt",4,"Uint",0,"UInt",0)
  147.    IfEqual h,-1, SetEnv, ErrorLevel, -1
  148.   ; IfNotEqual ErrorLevel,0,ExitApp ; couldn't create the file
  149.    r := DllCall("SetFilePointerEx","Uint",h,"Int64",0,"UInt *",p,"Int",0)
  150.    IfEqual r,0, SetEnv, ErrorLevel, -3
  151.    IfNotEqual ErrorLevel,0, {
  152.       t = %ErrorLevel%              ; save ErrorLevel to be returned
  153.       DllCall("CloseHandle", "Uint", h)
  154.       ErrorLevel = %t%              ; return seek error
  155.    }
  156.    result := DllCall("WriteFile","UInt",h,"Str",bin,"UInt"
  157.                ,size,"UInt *",Written,"UInt",0)
  158.    h := DllCall("CloseHandle", "Uint", h)
  159.    return, 1
  160. }
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. BinaryToString(ByRef bin, sz=-1, fmt=4) {  ; return base64 or formatted-hex
  169.    n := sz>0 ? sz : VarSetCapacity(bin)
  170.    DllCall("Crypt32.dll\CryptBinaryToStringA", UInt,&bin, UInt,n, UInt,fmt, UInt,0, UIntP,cp, "CDECL UInt") ; get size
  171.    VarSetCapacity(str,cp)
  172.    DllCall("Crypt32.dll\CryptBinaryToStringA", UInt,&bin, UInt,n, UInt,fmt, UInt,&str, UIntP,cp, "CDECL UInt")
  173.    Return str
  174. }
  175.  
  176. Utf( str ) {
  177.   RawLen := StrLen(str)
  178.  
  179.   BufSize := (RawLen + 1) * 2
  180.   VarSetCapacity(Buf1, BufSize, 0)    ; For UTF-16.
  181.   VarSetCapacity(Buf2, BufSize, 0)    ; For UTF-8.
  182.  
  183.   DllCall("MultiByteToWideChar", "uint", 0, "int", 0, "str", str
  184.                                , "int", -1, "uint", &Buf1, "uint", RawLen + 1)
  185.   DllCall("WideCharToMultiByte", "uint", 65001, "int", 0, "uint", &Buf1
  186.                                , "int", -1, "str", Buf2, "uint", BufSize
  187.                                , "int", 0, "int", 0)
  188.   Return Buf2
  189. }
  190.  
  191. UtfDecode( str ) {
  192.   RawLen := StrLen(str)
  193.  
  194.   Charset := 0    ; Put 1252 or 0
  195.  
  196.   BufSize := (RawLen + 1) * 2
  197.   VarSetCapacity(Buf, BufSize, 0)
  198.  
  199.   DllCall("MultiByteToWideChar", "uint", 65001, "int", 0, "str", str
  200.                                , "int", -1, "uint", &Buf, "uint", RawLen + 1)
  201.   DllCall("WideCharToMultiByte", "uint", Charset, "int", 0, "uint", &Buf, "int", -1
  202.                                , "str", str, "uint", RawLen + 1
  203.                                , "int", 0, "int", 0)
  204.   Return str
  205. }
  206.  
  207. UtfEncode( str ) {
  208.   RawLen := StrLen(str)
  209.  
  210.   BufSize := (RawLen + 1) * 2
  211.   VarSetCapacity(Buf1, BufSize, 0)    ; For UTF-16.
  212.   VarSetCapacity(Buf2, BufSize, 0)    ; For UTF-8.
  213.  
  214.   DllCall("MultiByteToWideChar", "uint", 0, "int", 0, "str", str
  215.                                , "int", -1, "uint", &Buf1, "uint", RawLen + 1)
  216.   DllCall("WideCharToMultiByte", "uint", 65001, "int", 0, "uint", &Buf1
  217.                                , "int", -1, "str", Buf2, "uint", BufSize
  218.                                , "int", 0, "int", 0)
  219.   Return Buf2
  220. }
  221.  
  222. UrlEncode(str)
  223. { ; v 0.3 / (w) 24.06.2008 by derRaphael / zLib-Style release
  224.    b_Format := A_FormatInteger
  225.    data := ""
  226.    SetFormat,Integer,H
  227.    Loop,Parse,str
  228.       if ((Asc(A_LoopField)>0x7f) || (Asc(A_LoopField)<0x30) || (asc(A_LoopField)=0x3d))
  229.          data .= "%" . ((StrLen(c:=SubStr(ASC(A_LoopField),3))<2) ? "0" . c : c)
  230.       Else
  231.          data .= A_LoopField
  232.    SetFormat,Integer,%b_format%
  233.    return data
  234. }
  235.  
  236. UrlDecode(str)
  237. { ; v 0.1 / (w) 28.06.2008 by derRaphael / zLib-Style release
  238.    Loop,Parse,str,`%
  239.       txt := (A_Index=1) ? A_LoopField : txt chr("0x" substr(A_LoopField,1,2)) SubStr(A_LoopField,3)
  240.    return txt
  241. }
  242.  
  243. httpQuery(byref Result, lpszUrl, POSTDATA="", HEADERS="")
  244. {   global httpQueryOps, httpAgent, httpProxy, httpProxyByPass, httpQueryReferer, httpQueryAcceptType
  245.        , httpQueryDwFlags
  246.    defaultOps =
  247.    (LTrim Join|
  248.       httpAgent=script|httpProxy=0|httpProxyByPass=0|INTERNET_FLAG_SECURE=0x00800000
  249.       SECURITY_FLAG_IGNORE_UNKNOWN_CA=0x00000100|SECURITY_FLAG_IGNORE_CERT_CN_INVALID=0x00001000
  250.       SECURITY_FLAG_IGNORE_CERT_DATE_INVALID=0x00002000|SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE=0x00000200
  251.       INTERNET_OPEN_TYPE_PROXY=3|INTERNET_OPEN_TYPE_DIRECT=1|INTERNET_SERVICE_HTTP=3
  252.    )
  253.    Loop,Parse,defaultOps,|
  254.    {
  255.       RegExMatch(A_LoopField,"(?P<Option>[^=]+)=(?P<Default>.*)",http)
  256.       if StrLen(%httpOption%)=0
  257.          %httpOption% := httpDefault
  258.    }
  259.  
  260.    hModule := DllCall("LoadLibrary", "Str", "WinINet.Dll")
  261.  
  262.    offset_name_length:= "4-lpszScheme-255|16-lpszHostName-1024|28-lpszUserName-1024|"
  263.                   . "36-lpszPassword-1024|44-lpszUrlPath-1024|52-lpszExtrainfo-1024"
  264.    VarSetCapacity(URL_COMPONENTS,60,0)
  265.    NumPut(60,URL_COMPONENTS,0), NumPut(255,URL_COMPONENTS,12), NumPut(0xffff,URL_COMPONENTS,24)
  266.    
  267.    Loop,Parse,offset_name_length,|
  268.    {
  269.       RegExMatch(A_LoopField,"(?P<Offset>\d+)-(?P<Name>[a-zA-Z]+)-(?P<Size>\d+)",iCU_)
  270.       VarSetCapacity(%iCU_Name%,iCU_Size,0)
  271.       NumPut(&%iCU_Name%,URL_COMPONENTS,iCU_Offset)
  272.       NumPut(iCU_Size,URL_COMPONENTS,iCU_Offset+4)
  273.    }
  274.  
  275.    DllCall("WinINet\InternetCrackUrlA","Str",lpszUrl,"uInt",StrLen(lpszUrl),"uInt",0,"uInt",&URL_COMPONENTS)
  276.  
  277.    Loop,Parse,offset_name_length,|
  278.    {
  279.       RegExMatch(A_LoopField,"-(?P<Name>[a-zA-Z]+)-",iCU_)
  280.       VarSetCapacity(%iCU_Name%,-1)
  281.    }
  282.    nPort:=NumGet(URL_COMPONENTS,24,"uInt")
  283.    
  284.    dwFlags := httpQueryDwFlags
  285.    if (lpszScheme = "https")
  286.       dwFlags |= (INTERNET_FLAG_SECURE|SECURITY_FLAG_IGNORE_CERT_CN_INVALID
  287.                |SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)
  288.  
  289.    if (lpszScheme="unknown") {
  290.       Result := "ERR: No Valid URL supplied."
  291.       Return StrLen(Result)
  292.    }
  293.  
  294.    hInternet := DllCall("WinINet\InternetOpenA"
  295.                   ,"Str",httpAgent,"UInt"
  296.                   ,(httpProxy != 0 ?  INTERNET_OPEN_TYPE_PROXY : INTERNET_OPEN_TYPE_DIRECT )
  297.                   ,"Str",httpProxy,"Str",httpProxyBypass,"Uint",0)
  298.  
  299.    hConnect := DllCall("WinINet\InternetConnectA"
  300.                   ,"uInt",hInternet,"Str",lpszHostname, "Int",nPort
  301.                   ,"Str",lpszUserName, "Str",lpszPassword,"uInt",INTERNET_SERVICE_HTTP
  302.                   ,"uInt",0,"uInt*",0)
  303.  
  304.    if (Strlen(POSTDATA)>0) {
  305.       HTTPVerb:="POST"
  306.       if StrLen(Headers)=0
  307.          Headers:="Content-Type: application/x-www-form-urlencoded"
  308.    } else
  309.       HTTPVerb:="GET"    
  310.  
  311.    hRequest := DllCall("WinINet\HttpOpenRequestA"
  312.                   ,"uInt",hConnect,"Str",HTTPVerb,"Str",lpszUrlPath . lpszExtrainfo
  313.                   ,"Str",ProVer := "HTTP/1.1", "Str",httpQueryReferer,"Str",httpQueryAcceptTypes
  314.                   ,"uInt",dwFlags,"uInt",Context:=0 )
  315.  
  316.    sRequest := DllCall("WinINet\HttpSendRequestA"
  317.                   , "uInt",hRequest,"Str",Headers, "uInt",Strlen(Headers)
  318.                   , "Str",POSTData,"uInt",Strlen(POSTData))
  319.  
  320.    VarSetCapacity(header, 2048, 0)  ; max 2K header data for httpResponseHeader
  321.    VarSetCapacity(header_len, 4, 0)
  322.    
  323.    Loop, 5
  324.      if ((headerRequest:=DllCall("WinINet\HttpQueryInfoA","uint",hRequest
  325.       ,"uint",21,"uint",&header,"uint",&header_len,"uint",0))=1)
  326.       break
  327.  
  328.    If (headerRequest=1) {
  329.       VarSetCapacity(res,headerLength:=NumGet(header_len),32)
  330.       DllCall("RtlMoveMemory","uInt",&res,"uInt",&header,"uInt",headerLength)
  331.       Loop,% headerLength
  332.          if (*(&res-1+a_index)=0) ; Change binary zero to linefeed
  333.             NumPut(Asc("`n"),res,a_index-1,"uChar")
  334.       VarSetCapacity(res,-1)
  335.    } else
  336.       res := "timeout"
  337.  
  338.    Loop,Parse,res,`n,`r
  339.    {
  340.       RetValue := A_LoopField
  341.       break
  342.    }
  343.    
  344.    If (RetValue="timeout") {
  345.       html := "Error: timeout"
  346.       return -1
  347.    }
  348.    RetValue := RegExReplace(RetValue,"HTTP/1\.[01]\s+")
  349.    
  350.    HttpRetCodes := "100=Continue|101=Switching Protocols|102=Processing (WebDAV) (RFC 2518)|"
  351.               . "200=OK|201=Created|202=Accepted|203=Non-Authoritative Information|204=No"
  352.               . " Content|205=Reset Content|206=Partial Content|207=Multi-Status (WebDAV)"
  353.               . "|300=Multiple Choices|301=Moved Permanently|302=Found|303=See Other|304="
  354.               . "Not Modified|305=Use Proxy|306=Switch Proxy|307=Temporary Redirect|400=B"
  355.               . "ad Request|401=Unauthorized|402=Payment Required|403=Forbidden|404=Not F"
  356.               . "ound|405=Method Not Allowed|406=Not Acceptable|407=Proxy Authentication "
  357.               . "Required|408=Request Timeout|409=Conflict|410=Gone|411=Length Required|4"
  358.               . "12=Precondition Failed|413=Request Entity Too Large|414=Request-URI Too "
  359.               . "Long|415=Unsupported Media Type|416=Requested Range Not Satisfiable|417="
  360.               . "Expectation Failed|418=I'm a teapot (RFC 2324)|422=Unprocessable Entity "
  361.               . "(WebDAV) (RFC 4918)|423=Locked (WebDAV) (RFC 4918)|424=Failed Dependency"
  362.               . " (WebDAV) (RFC 4918)|425=Unordered Collection (RFC 3648)|426=Upgrade Req"
  363.               . "uired (RFC 2817)|449=Retry With|500=Internal Server Error|501=Not Implem"
  364.               . "ented|502=Bad Gateway|503=Service Unavailable|504=Gateway Timeout|505=HT"
  365.               . "TP Version Not Supported|506=Variant Also Negotiates (RFC 2295)|507=Insu"
  366.               . "fficient Storage (WebDAV) (RFC 4918)|509=Bandwidth Limit Exceeded|510=No"
  367.               . "t Extended (RFC 2774)"
  368.    
  369.    RetValue := SubStr(RetValue,1,3)
  370.    
  371.    Loop,Parse,HttpRetCodes,|
  372.    {
  373.       HttpReturnCode := SubStr(A_LoopField,1,3)
  374.       HttpReturnMsg  := SubStr(A_LoopField,5)
  375.       if (RetValue=HttpReturnCode) {
  376.          RetMsg := HttpReturnMsg
  377.          break
  378.       }
  379.    }
  380.  
  381.    if strlen(HTTPQueryOps)>0 {
  382.       if (instr(HTTPQueryOps,"showHeader"))
  383.          MsgBox % res
  384.       if (instr(HTTPQueryOps,"storeHeader"))
  385.          global HttpQueryHeader := res
  386.       if (instr(HTTPQueryOps,"updateSize")) {
  387.          Loop,Parse,res,`n
  388.             If RegExMatch(A_LoopField,"Content-Length:\s+?(?P<Size>\d+)",full) {
  389.                global HttpQueryFullSize := fullSize
  390.                break
  391.             }
  392.          if (fullSize+0=0)
  393.             HttpQueryFullSize := "size unavailable"
  394.       }
  395.    }
  396.  
  397.    if !(InStr("100 200 201 202 302",RetValue)) {
  398.       Result := RetValue " " RetMsg
  399.       return StrLen(Result)
  400.    }
  401.  
  402.    VarSetCapacity(BytesRead,4,0)
  403.    fsize := 0
  404.    Loop            
  405.    {  bc := A_Index
  406.       VarSetCapacity(buffer%bc%,1024,0) ; setup new chunk for this receive round
  407.       ReadFile := DllCall("wininet\InternetReadFile"
  408.                   ,"uInt",hRequest,"uInt",&buffer%bc%,"uInt",1024,"uInt",&BytesRead)
  409.       ReadBytes := NumGet(BytesRead)  
  410.       If ((ReadFile!=0)&&(!ReadBytes))  
  411.          break                      
  412.       Else {
  413.          fsize += ReadBytes  
  414.          sizeArray .= ReadBytes "|"
  415.       }
  416.       if (instr(HTTPQueryOps,"updateSize"))
  417.          Global HttpQueryCurrentSize := fsize
  418.    }
  419.    sizeArray := SubStr(sizeArray,1,-1)  
  420.    
  421.    VarSetCapacity(result,fSize+1,0)      
  422.    Dest := &result            
  423.    Loop,Parse,SizeArray,|
  424.       DllCall("RtlMoveMemory","uInt",Dest,"uInt",&buffer%A_Index%,"uInt",A_LoopField)
  425.       , Dest += A_LoopField
  426.      
  427.    DllCall("WinINet\InternetCloseHandle", "uInt", hRequest)  
  428.    DllCall("WinINet\InternetCloseHandle", "uInt", hInternet)
  429.    DllCall("WinINet\InternetCloseHandle", "uInt", hConnect)
  430.    DllCall("FreeLibrary", "UInt", hModule)              
  431.    return fSize
  432. }
  433.  
  434. MD5( ByRef V, L=0 ) { ; www.autohotkey.com/forum/viewtopic.php?p=275910#275910
  435.  VarSetCapacity( MD5_CTX,104,0 ), DllCall( "advapi32\MD5Init", Str,MD5_CTX )
  436.  DllCall( "advapi32\MD5Update", Str,MD5_CTX, Str,V, UInt,L ? L : VarSetCapacity(V) )
  437.  DllCall( "advapi32\MD5Final", Str,MD5_CTX )
  438.  Loop % StrLen( Hex:="123456789ABCDEF0" )
  439.   N := NumGet( MD5_CTX,87+A_Index,"Char"), MD5 .= SubStr(Hex,N>>4,1) . SubStr(Hex,N&15,1)
  440. Return MD5
  441. }
Advertisement
Add Comment
Please, Sign In to add comment