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

I want to know if this problem can be done easily in perl, and does anybody have any links or help to do it. I am having trouble trying to figure out how to read the file in. It can be of any type .txt, .gif .jpg, etc. Then how to work with that data. you cant open a .jpg or .gif to mess with the data, so I am stumped, I have no idea what to do. here is a more complete description of my problem.

I need to write a program that computes a CRC on an input file. The program should accept command-line arguments that give the input file name and an optional output file name.

If there are two filename arguments, then it should assume that the input file is a "normal" file without any CRC code attached to it. It should compute a CRC and append the computed remainder (the CRC code) to the end of the output file in the last two bytes. If there is one filename argument, then it should assume that the input file contains a computed CRC. Thus, it should perform a checking action by recomputing the CRC and determining whether the resulting remainder is zero. If there are two filename arguments, then the output file is essentially the same as the input file, except that it has the computed CRC appended to the end. If there is one filename argument, then the output is to the screen: the program reports whether an error was detected. All help is welcome=)

Edit by tye

Replies are listed 'Best First'.
Re: crc-16 help
by lhoward (Vicar) on Jun 12, 2001 at 20:07 UTC
    The String::CRC module will compute CRC's of various lengths.
Re: crc-16 help
by bikeNomad (Priest) on Jun 12, 2001 at 20:03 UTC
    Paul, I hope this isn't homework.

    The quickest way I've found to do a CRC-32 (not a CRC-16, sorry, but it'll work fine for checking files) is to use Compress::Zlib's crc32 function:

    use Compress::Zlib; my $crc32 = 0; while (<>) { $crc32 = Compress::Zlib::crc32($_, $crc32); } print "CRC is $crc32\n";

    Perhaps this will help you. There is also Digest::MD5, and a simple checksum available using Perl's builtin unpack.