Skip to content

Commit

Permalink
Replace usages of CRC32 in std-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardiff committed Mar 6, 2020
1 parent b7fe4f5 commit f6763f8
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions spec/std/zip/zip_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe Zip do
io = IO::Memory.new

text = "contents of foo"
crc32 = CRC32.checksum(text)
crc32 = Digest::CRC32.checksum(text)

Zip::Writer.open(io) do |zip|
entry = Zip::Writer::Entry.new("foo.txt")
Expand Down Expand Up @@ -98,7 +98,7 @@ describe Zip do
io = IO::Memory.new

text = "contents of foo"
crc32 = CRC32.checksum(text)
crc32 = Digest::CRC32.checksum(text)

Zip::Writer.open(io) do |zip|
entry = Zip::Writer::Entry.new("foo.txt")
Expand Down
2 changes: 1 addition & 1 deletion src/gzip/gzip.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "flate"
require "crc32"
require "digest/crc32"

# The Gzip module contains readers and writers of gzip format compressed
# data, as specified in [RFC 1952](https://www.ietf.org/rfc/rfc1952.txt).
Expand Down
6 changes: 3 additions & 3 deletions src/gzip/reader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Gzip::Reader < IO
# Creates a new reader from the given *io*.
def initialize(@io : IO, @sync_close = false)
# CRC32 of written data
@crc32 = CRC32.initial
@crc32 = Digest::CRC32.initial

# Total size of the original (uncompressed) input data modulo 2^32.
@isize = 0_u32
Expand Down Expand Up @@ -101,7 +101,7 @@ class Gzip::Reader < IO
end

# Reset checksum and total size for next entry
@crc32 = CRC32.initial
@crc32 = Digest::CRC32.initial
@isize = 0_u32

# Check if another header with data comes
Expand All @@ -115,7 +115,7 @@ class Gzip::Reader < IO
end
else
# Update CRC32 and total data size
@crc32 = CRC32.update(slice[0, read_bytes], @crc32)
@crc32 = Digest::CRC32.update(slice[0, read_bytes], @crc32)

# Using wrapping addition here because isize is only 32 bits wide but
# uncompressed data size can be bigger.
Expand Down
4 changes: 2 additions & 2 deletions src/gzip/writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Gzip::Writer < IO
# Creates a new writer to the given *io*.
def initialize(@io : IO, @level = Gzip::DEFAULT_COMPRESSION, @sync_close = false)
# CRC32 of written data
@crc32 = CRC32.initial
@crc32 = Digest::CRC32.initial

# Total size of the original (uncompressed) input data modulo 2^32.
@isize = 0
Expand Down Expand Up @@ -77,7 +77,7 @@ class Gzip::Writer < IO
flate_io.write(slice)

# Update CRC32 and total data size
@crc32 = CRC32.update(slice, @crc32)
@crc32 = Digest::CRC32.update(slice, @crc32)

# Using wrapping addition here because isize is only 32 bits wide but
# uncompressed data size can be bigger.
Expand Down
4 changes: 2 additions & 2 deletions src/zip/checksum_reader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Zip
# optionally verifying the computed value against an
# expected one.
private class ChecksumReader < IO
getter crc32 = CRC32.initial
getter crc32 = Digest::CRC32.initial

def initialize(@io : IO, @filename : String, verify @expected_crc32 : UInt32? = nil)
end
Expand All @@ -15,7 +15,7 @@ module Zip
raise Zip::Error.new("Checksum failed for entry #{@filename} (expected #{expected_crc32}, got #{crc32}")
end
else
@crc32 = CRC32.update(slice[0, read_bytes], @crc32)
@crc32 = Digest::CRC32.update(slice[0, read_bytes], @crc32)
end
read_bytes
end
Expand Down
6 changes: 3 additions & 3 deletions src/zip/checksum_writer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Zip
# checksum while writing to an underlying IO.
private class ChecksumWriter < IO
getter count = 0_u32
getter crc32 = CRC32.initial
getter crc32 = Digest::CRC32.initial
getter! io : IO

def initialize(@compute_crc32 = false)
Expand All @@ -17,13 +17,13 @@ module Zip
return if slice.empty?

@count += slice.size
@crc32 = CRC32.update(slice, @crc32) if @compute_crc32
@crc32 = Digest::CRC32.update(slice, @crc32) if @compute_crc32
io.write(slice)
end

def io=(@io)
@count = 0_u32
@crc32 = CRC32.initial
@crc32 = Digest::CRC32.initial
end
end
end
2 changes: 1 addition & 1 deletion src/zip/zip.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "flate"
require "crc32"
require "digest/crc32"
require "./*"

# The Zip module contains readers and writers of the zip
Expand Down

0 comments on commit f6763f8

Please sign in to comment.