Old 30th June 2013, 13:47   #1
zeeh3
Senior Member
 
Join Date: Aug 2005
Posts: 121
Hex Strings and lpBuffer

When using System::Call 'Kernel32::ReadFile(i r0, i r2, i r1, *i .r3, i 0)', $2 is a lpBuffer, a pointer to the buffer that receives the data read from a file. Is it possible to pass a hexadecimal string to a buffer like $2, instead of creating a file, writing the hexadecimal string to the file, reading the file to fill the $2 buffer and erasing the file to do what I want?
zeeh3 is offline   Reply With Quote
Old 30th June 2013, 16:37   #2
aerDNA
Senior Member
 
aerDNA's Avatar
 
Join Date: Feb 2007
Location: Rijeka, Croatia
Posts: 225
There may be a more elegant way than how I do it, but here's a working example:
code:
Outfile BufferStuffer.exe
Var Buffer
Var DataSize

!macro FillBuffer Data # Hex string expected for Data
StrLen $DataSize ${Data}
IntOp $DataSize $DataSize / 2
System::Alloc $DataSize
Pop $Buffer
StrCpy $9 0
IntOp $0 $9 * 2
StrCpy $0 ${Data} 2 $0
System::Call "*$Buffer(&v$9, &i1 '0x$0')"
IntOp $9 $9 + 1
IntCmp $9 $DataSize 0 -4
!macroend
!define FillBuffer "!insertmacro FillBuffer"


Function .onInit
${FillBuffer} "497420776F726B7321"
FileOpen $R0 "$EXEDIR\doesitwork.txt" w
System::Call "kernel32::WriteFile(i R0, i $Buffer, i $DataSize, *i.r1, i '') i.r0"
FileClose $R0
System::Free $Buffer
IntCmp $0 0 +2
IntCmp $1 $Datasize +2
MessageBox MB_OK|MB_ICONSTOP "Error writing file."
Abort
FunctionEnd

Section Blank
SectionEnd


PostEnd:
aerDNA is offline   Reply With Quote
Old 30th June 2013, 19:03   #3
zeeh3
Senior Member
 
Join Date: Aug 2005
Posts: 121
Thank you, I will try it
zeeh3 is offline   Reply With Quote
Old 2nd July 2013, 15:44   #4
zeeh3
Senior Member
 
Join Date: Aug 2005
Posts: 121
It works fine, but is pretty slow with big strings.
zeeh3 is offline   Reply With Quote
Old 2nd July 2013, 16:59   #5
aerDNA
Senior Member
 
aerDNA's Avatar
 
Join Date: Feb 2007
Location: Rijeka, Croatia
Posts: 225
Well, one System call for every byte of data, you can't expect turbo performance if you need to process more than a few KB. It's not appropriate for that. Maybe someone knows a way to fill the buffer in a single pass, I don't, apart from the one you know as well - ReadFile. I don't know what your goal is, is it patching of some sort? A FileWriteByte loop should be faster than my example, no buffer required. Vpatch would be a perfect solution, if applicable.

[Edit] Did some testing: a FileWriteByte variant turned out approx. 3x faster for the same input/output.

PostEnd:

Last edited by aerDNA; 2nd July 2013 at 19:09.
aerDNA is offline   Reply With Quote
Old 2nd July 2013, 17:39   #6
aerDNA
Senior Member
 
aerDNA's Avatar
 
Join Date: Feb 2007
Location: Rijeka, Croatia
Posts: 225
You're processing a load of 511B chunks because of maxstrlen, that's what the question about iterating through gflags in that other thread was about, right? Packing it all up in a binary file you'd extract at runtime and load with ReadFile really would be way faster and 'cleaner'.

PostEnd:
aerDNA is offline   Reply With Quote
Old 2nd July 2013, 20:27   #7
zeeh3
Senior Member
 
Join Date: Aug 2005
Posts: 121
Yes, that's correct. I am now getting the data as a resource.
zeeh3 is offline   Reply With Quote
Old 2nd July 2013, 22:20   #8
aerDNA
Senior Member
 
aerDNA's Avatar
 
Join Date: Feb 2007
Location: Rijeka, Croatia
Posts: 225
RCData? Nice solution.

PostEnd:
aerDNA is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump