Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 4 commits into from

Conversation

kojix2
Copy link
Contributor

@kojix2 kojix2 commented Jul 7, 2024

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

This solves part of #14789

@kojix2 kojix2 changed the title Gzipcheckhcrc Fix Compress::Gzip::Header to check for CRC16 Jul 7, 2024
@kojix2
Copy link
Contributor Author

kojix2 commented Jul 7, 2024

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

@straight-shoota
Copy link
Member

straight-shoota commented Jul 7, 2024

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.

Copy link
Contributor

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: what about computing the CRC value as we parse the data instead of allocating many objects into the HEAP?

For example you can check flg.hcrc? first to create a digest object, then check if it exits to update the checksum.

if flg.hcrc?
  crc = Digest::CRC32.new
  crc.update header.to_slice
end

if flg.extra?
  # ...

  if crc
    crc.update(xlen.unsafe_as(...).to_slice
    crc.update(@extra)
  end
end

# ...

if crc
  checksum = 0_u32
  crc.final(Slice.new(pointerof(checksum).as(UInt8*), 4)

  crc16 = io.read_bytes(UInt16, ...)
  raise "mismatch" unless checksum == crc16
end

@kojix2 kojix2 closed this Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants