crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.39k stars 1.62k forks source link

Fix `Compress::Gzip::Header` to check for CRC16 #14791

Closed kojix2 closed 6 days ago

kojix2 commented 3 months ago

I wrote code to check the value of CRC16 if the Gzip header flag contains an HCRC.

This solves part of #14789

kojix2 commented 3 months ago

Feel free to correct me if you have a better way to write this.

straight-shoota commented 3 months ago

As always, this change is void if there's no spec to ensure its functionality. We'll need at least one example with an invalid CRC and a test to ensure it raises.

Capturing the header bytes in an array seems very inefficient. It also shouldn't be necessary to do in one go. Instead of passing the entire data to CRC32.checksum, we can update the checksum with individual bytes or slices as we read them. The result should be identical. We can either instantiate the CRC32 class and append data bits, or use the .initial and .update class methods. The latter is used in the Gzip::Reader and Gzip::Writer implementations, for example.