TeamViterbi has asked for the wisdom of the Perl Monks concerning the following question:

Hello everybody,

Ive run into another problem with my checksums. whenever i run "perl test.pl file.name" i always end up with errors such as operator missing in line 5 and quantifier follows nothing in regex. Can anyone help me out, i dont know why its not working.
#!/usr/bin/perl uint32_t CalChecksum( uint32_t *buffer, /* --% DESCRIPTION: Pointer to data buffer. --% UNITS: N/A --% RANGE: N/A */ uint32_t word_count, /* --% DESCRIPTION: Number of 32-bit words. --% UNITS: N/A --% RANGE: N/A */ uint32_t bias /* --% DESCRIPTION: Bias added to checksum. --% UNITS: N/A --% RANGE: N/A */ ) { uint32_t checksum = bias; while(word_count) { checksum += *buffer; word_count--; buffer++; } return checksum; }
Thanks again.

Replies are listed 'Best First'.
Re: Checksum Help Again
by graff (Chancellor) on Jul 08, 2009 at 02:54 UTC
    Let me know whether I understand your situation...

    • For some reason, you've been scanning the web for source code regarding the computation of CRC checksums.

    • As you find stuff, you aren't able to tell on your own what language a given chunk of code is written in.

    • You already have one piece of perl code that works (provided for you in your other thread about checksums, but for some reason, that's not enough.

    • This latest piece of code you've posted (which probably is written in C) has somehow struck you as useful, but you don't really know what it does, and so cannot describe the algorithm it uses, which is what you'd have to do in order to implement it in some other language (such as perl).

    Does that sum it up? Could you explain a little more about what you are really trying to accomplish, and what you lacking in order to accomplish that? What led you to take this particular chunk of C and try to pretend that Perl would know what to do with it?

    Whatever your goal may be, putting "#!/usr/bin/perl" at the top of any randomly selected chunk of source code found on the web is not an effective strategy.

      Thank you all for your assistance. Graff, in a sense, much of what you said is correct. I apologize for lacking so many of the fundamentals of programming in general, I've just recently picked it up and am working very hard to understand it all.
Re: Checksum Help Again
by toolic (Bishop) on Jul 07, 2009 at 23:36 UTC
    If the code you show is the contents of the 'test.pl' file, then you get errors because it is not correct Perl syntax.
      hmm. i didnt realize that. Is this in C? how can i convert this into correct syntax?

        Your "program" defines one subroutine. For it to be of any use, you will have to have or to write a program to call the subroutine. If that is in C then you should leave your subroutine definition in C. If it is Perl, then you might write a new subroutine in Perl.

        There will be no trivial way to convert the subroutine definition. First you might determine how the arguments will be passed. This will be different because basic Perl data types are different from basic C data types - Perl doesn't have pointers like C does and strings are somewhat different also. Then you can do the same for your return value. Finally, you can write the body of your subroutine. Your sample code is quite simple, so writing an equivalent in Perl should be easy. If it is not obvious, then you probably need to read an introduction to C or to Perl or both.

Re: Checksum Help Again
by spx2 (Deacon) on Jul 08, 2009 at 08:57 UTC

    First of all , if you're doing this in C , the checksum variable will overflow due to it being a uint32 and you are adding up to it uint32s formed of bytes from the text and depending on the length of the text ... you will most likely have an overflow in checksum, which you should judge as a good/bad algorithm.

    If you would've taken a look at the documentation of the unpack function you would have seen an example of exactly what you're trying to achieve , except for the fact that they are taking the checksum modulo 2^16 - 1

    $checksum = do { local $/; # slurp! unpack("%32W*",<>) % 65535; };
    P.S. : The pack documentation is more useful since it explains the "%32W*"