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

clean up and export crc32c function #22274

Merged
merged 14 commits into from
Jun 13, 2017
Prev Previous commit
Next Next commit
avoid resizing buffer in readbytes
  • Loading branch information
stevengj committed Jun 7, 2017
commit bd6465154943b42ad139f8faeff1f6735b2a1fd7
12 changes: 7 additions & 5 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,10 @@ note that the result is endian-dependent.
"""
function crc32c end

unsafe_crc32c(a, n, crc) = ccall(:jl_crc32c, UInt32, (UInt32, Ptr{UInt8}, Csize_t), crc, a, n)

crc32c(a::Union{Array{UInt8},FastContiguousSubArray{UInt8,N,<:Array{UInt8}} where N}, crc::UInt32=0x00000000) =
ccall(:jl_crc32c, UInt32, (UInt32, Ptr{UInt8}, Csize_t), crc, a, length(a))
unsafe_crc32c(a, length(a), crc)

crc32c(buf::IOBuffer, crc::UInt32=0x00000000) = crc32c(buf.data, crc)

Expand All @@ -796,12 +798,12 @@ mixed with a starting `crc` integer.
"""
function crc32c(f::IO, nb::Integer, crc::UInt32=0x00000000)
buf = Array{UInt8}(min(nb, 16384))
while !eof(f) && nb > 0
n = readbytes!(f, buf, nb)
crc = ccall(:jl_crc32c, UInt32, (UInt32, Ptr{UInt8}, Csize_t), crc, buf, n)
while !eof(f) && nb > 16384
n = readbytes!(f, buf)
crc = unsafe_crc32c(buf, n, crc)
nb -= n
end
return crc
return unsafe_crc32c(buf, readbytes!(f, buf, nb), crc)
end

crc32c(filename::AbstractString, crc::UInt32=0x00000000) =
Expand Down