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!


In reply to Re: A little C code converted to Perl *HELP* by BrowserUk
in thread A little C code converted to Perl *HELP* by regnab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.