_LZNT Memory compression UDF Autolt


SUBMITTED BY: Guest

DATE: May 28, 2013, 5:28 p.m.

FORMAT: Text only

SIZE: 5.0 kB

HITS: 949

  1. ; #FUNCTION# ;===============================================================================
  2. ;
  3. ; Name...........: _LZNTDecompress
  4. ; Description ...: Decompresses input data.
  5. ; Syntax.........: _LZNTDecompress ($bBinary)
  6. ; Parameters ....: $vInput - Binary data to decompress.
  7. ; Return values .: Success - Returns decompressed binary data.
  8. ; - Sets @error to 0
  9. ; Failure - Returns empty string and sets @error:
  10. ; |1 - Error decompressing.
  11. ; Author ........: trancexx
  12. ; Related .......: _LZNTCompress
  13. ; Link ..........; <a href='http://msdn.microsoft.com/en-us/library/bb981784.aspx' class='bbc_url' title='External link' rel='nofollow external'>http://msdn.microsoft.com/en-us/library/bb981784.aspx</a>
  14. ;
  15. ;==========================================================================================
  16. Func _LZNTDecompress($bBinary)
  17. $bBinary = Binary($bBinary)
  18. Local $tInput = DllStructCreate("byte[" & BinaryLen($bBinary) & "]")
  19. DllStructSetData($tInput, 1, $bBinary)
  20. Local $tBuffer = DllStructCreate("byte[" & 16 * DllStructGetSize($tInput) & "]") ; initially oversizing buffer
  21. Local $a_Call = DllCall("ntdll.dll", "int", "RtlDecompressBuffer", _
  22. "ushort", 2, _
  23. "ptr", DllStructGetPtr($tBuffer), _
  24. "dword", DllStructGetSize($tBuffer), _
  25. "ptr", DllStructGetPtr($tInput), _
  26. "dword", DllStructGetSize($tInput), _
  27. "dword*", 0)
  28. If @error Or $a_Call[0] Then
  29. Return SetError(1, 0, "") ; error decompressing
  30. EndIf
  31. Local $tOutput = DllStructCreate("byte[" & $a_Call[6] & "]", DllStructGetPtr($tBuffer))
  32. Return SetError(0, 0, DllStructGetData($tOutput, 1))
  33. EndFunc ;==>_LZNTDecompress
  34. ; #FUNCTION# ;===============================================================================
  35. ;
  36. ; Name...........: _LZNTCompress
  37. ; Description ...: Compresses input data.
  38. ; Syntax.........: _LZNTCompress ($vInput [, $iCompressionFormatAndEngine])
  39. ; Parameters ....: $vInput - Data to compress.
  40. ; $iCompressionFormatAndEngine - Compression format and engine type. Default is 2 (standard compression). Can be:
  41. ; |2 - COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_STANDARD
  42. ; |258 - COMPRESSION_FORMAT_LZNT1 | COMPRESSION_ENGINE_MAXIMUM
  43. ; Return values .: Success - Returns compressed binary data.
  44. ; - Sets @error to 0
  45. ; Failure - Returns empty string and sets @error:
  46. ; |1 - Error determining workspace buffer size.
  47. ; |2 - Error compressing.
  48. ; Author ........: trancexx
  49. ; Related .......: _LZNTDecompress
  50. ; Link ..........; <a href='http://msdn.microsoft.com/en-us/library/bb981783.aspx' class='bbc_url' title='External link' rel='nofollow external'>http://msdn.microsoft.com/en-us/library/bb981783.aspx</a>
  51. ;
  52. ;==========================================================================================
  53. Func _LZNTCompress($vInput, $iCompressionFormatAndEngine = 2)
  54. If Not ($iCompressionFormatAndEngine = 258) Then
  55. $iCompressionFormatAndEngine = 2
  56. EndIf
  57. Local $bBinary = Binary($vInput)
  58. Local $tInput = DllStructCreate("byte[" & BinaryLen($bBinary) & "]")
  59. DllStructSetData($tInput, 1, $bBinary)
  60. Local $a_Call = DllCall("ntdll.dll", "int", "RtlGetCompressionWorkSpaceSize", _
  61. "ushort", $iCompressionFormatAndEngine, _
  62. "dword*", 0, _
  63. "dword*", 0)
  64. If @error Or $a_Call[0] Then
  65. Return SetError(1, 0, "") ; error determining workspace buffer size
  66. EndIf
  67. Local $tWorkSpace = DllStructCreate("byte[" & $a_Call[2] & "]") ; workspace is needed for compression
  68. Local $tBuffer = DllStructCreate("byte[" & 16 * DllStructGetSize($tInput) & "]") ; initially oversizing buffer
  69. Local $a_Call = DllCall("ntdll.dll", "int", "RtlCompressBuffer", _
  70. "ushort", $iCompressionFormatAndEngine, _
  71. "ptr", DllStructGetPtr($tInput), _
  72. "dword", DllStructGetSize($tInput), _
  73. "ptr", DllStructGetPtr($tBuffer), _
  74. "dword", DllStructGetSize($tBuffer), _
  75. "dword", 4096, _
  76. "dword*", 0, _
  77. "ptr", DllStructGetPtr($tWorkSpace))
  78. If @error Or $a_Call[0] Then
  79. Return SetError(2, 0, "") ; error compressing
  80. EndIf
  81. Local $tOutput = DllStructCreate("byte[" & $a_Call[7] & "]", DllStructGetPtr($tBuffer))
  82. Return SetError(0, 0, DllStructGetData($tOutput, 1))
  83. EndFunc ;==>_LZNTCompress

comments powered by Disqus