You can get access to the GetVolumeInformation() API via Win32API::File.
The rest of it is almost a straight forward line for line conversion, though you might need to use bytes for the bit-twiddling. There is one bit that, as far as I am aware, there is no way to emulate in Perl.
volatile DWORD r32=0;
...
There isn't, and probably couldn't be, any equivalent to the volatile keyword in perl. And, unless the DWORD is truely volatile. ie. subject to change from outside of the scope of this piece of code, or the piece of code that the C-compiler can see when compiling this piece of code, then the declaration of r32, and the entire for loop that follows it is a comeplete waste of time, as r32 is never re-used in the snippet you have shown us. However, if it is volatile, then it would probably need to declared as extern as well.
Similarly, the whole: Read the volume serial number from the first disk we can find, or use some arbitrary number (0xe16e) instead. Then use the serial number/arbitrary number, exclucive or'd with some other arbitrary number (0x35af) as a seed for the random number generator is, at best flaky, and at worst, totally pointless. That said, the perl's PRNG self seeds in a pretty well thought out way and use of srand is discouraged -- unless you actually want to create repeatable sequences.
So, everything preceeding the volatile keyword in your snippet is redundant in a perl program and should be ignored.
The for loop following the volatile keyword is pointless unless r32 is truely volatile, and if it is, you can't emulate that from perl. (Maybe with inline C or XS, but non-trivial!).
That leaves the sprintf. All that does it create a string containing a 32-bit random number in hex, which it does by combining a 16-bit random number with a 32-bit one, which is a bit suspect anyway.
So, assuming that gcheck is used somewhere, that can be translated to
my $check = sprintf '%x', int rand( 0xffffffff ); ## Bad!! See [roger
+]'s post below.
my $check = sprintf '%x', int( rand(0xffff) )<<16 | int( rand( 0xffff
+) ); ## Probably OK.
That's it. As far as I can tell the whole lot can be reduced to that one line.
Except the volatile r32, which is probably a piece of C-runtime global memory that is used to store the current/last random number returned by the C version of rand(), but the perl rand doesn't use that anyway, so there is no point in performing that piece of shenanigins either.
That's it. One line!
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!
|