Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Need a simple FLL
Message
From
19/09/2006 16:13:10
 
General information
Forum:
Visual FoxPro
Category:
Troubleshooting
Miscellaneous
Thread ID:
01154766
Message ID:
01155253
Views:
28
>I need a fast way to convert relatively large ascii strings to their "escaped" hex equivalent for use within dynamically generated javascript. Here I mean where "ABC" becomes "%41%42%43". This will be used as a support routine on a web server for generating portions of web pages that are intentionally obfuscated. It needs to be fast and callable from a VFP COM DLL that is working in conjunction with an ASP app to generate the web pages.
>
>The STRCONV function with opcode of 15 is very close in that it produces an ascii to hex conversion, but it does not include the javascript escape character "%".
>
>What I'm doing now is using STRCONV as above, then passing the result to the browser, and having a browser-side javascript function fix up the string so it can be passed to the unescape function as shown below. Whatever is passed back from the function is either document.written into the page or eval()'d if it's global-scope javascript code rather than HTML.
>function HtoE(cStr){
>var cNew="";
>for (var i=0; i<cStr.length; i=i+2){
>  cNew+= "%" + cStr.substr(i,2);
>  }
>return unescape(cNew);
>}
>
>Thanks very much for any suggestions.

This takes less than 0.3 seconds in my lousy Athlon 1.8 Mhz PC - 1GB RAM, using the redist.txt file in the VFP9 folder that is 77621 bytes as input, pure VFP9 code and a couple of API functions:
#Define HEAP_ZERO_MEMORY		0x8

m.lcString = Filetostr(Getfile())
m.lnLen = Len(m.lcString)

Declare Integer GetProcessHeap In win32api

Declare Integer HeapAlloc In win32api ;
	Integer hHeap, ;
	Integer dwFlags, ;
	Integer dwBytes

Declare Integer HeapFree In win32api ;
	Integer hHeap, ;
	Integer dwFlags, ;
	Integer lpMem

m.lnHeap  = GetProcessHeap()

m.lnSeconds = Seconds()
m.lnMemBlock1 = HeapAlloc(m.lnHeap, 0x8, m.lnLen)
m.lnMemBlock2 = HeapAlloc(m.lnHeap, 0x8, m.lnLen * 3)

Sys(2600, m.lnMemBlock1, m.lnLen, m.lcString)

For m.lnPos = 1 To m.lnLen
	m.lcChar = Sys(2600, m.lnMemBlock1 + m.lnPos - 1, 1)
	m.lnOffset = (m.lnPos - 1) * 3
	Sys(2600, m.lnMemBlock2 + m.lnOffset, 3, [%] + Strconv(m.lcChar, 15))
Endfor

m.lcEscHex = Sys(2600, m.lnMemBlock2,  m.lnLen * 3)

HeapFree(m.lnHeap, 0, m.lnMemBlock1)
HeapFree(m.lnHeap, 0, m.lnMemBlock2)

?[Time: ], Seconds() - m.lnSeconds, [Bytes: ], m.lnLen
?[Escaped Hex:        ], Left(m.lcEscHex, 30)
?[StrConv Equivalent: ], Left(Strconv(m.lcString, 15), 20)

Strtofile(m.lcEscHex, Putfile())
Doing it with pure VFP9 functions like SUBSTR takes about 2.6 seconds

Carlos Alloatti
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform