Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
schedule:
- cron: '0 2 * * *' # Daily at 2 AM UTC (8 PM CST)
push:
branches: [master]
branches: [main]
tags: ["*"]
pull_request:
concurrency:
Expand Down
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Zlib_jll = "83775a58-1f1d-513f-b197-d71354ab007a"
julia = "1.3"

[extras]
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
test = ["OffsetArrays", "Test"]
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ CRC-32 checksum.
crc32(data, crc::UInt32=0x00000000)
```

Computes the CRC-32 checksum (ISO 3309, ITU-T V.42, CRC-32-IEEE) of the given `data`, which can be
an `Array{UInt8}`, a contiguous subarray thereof, or a `String`. Optionally, you can pass
a starting `crc` integer to be mixed in with the checksum. The `crc` parameter
can be used to compute a checksum on data divided into chunks: performing
Compute the CRC-32 checksum (ISO 3309, ITU-T V.42, CRC-32-IEEE) of the given `data`, which can be
an `Array{UInt8}`, a contiguous subarray thereof, an `AbstractVector{UInt8}`, or a `String`.
Optionally, you can pass a starting `crc` integer to be mixed in with the checksum.
The `crc` parameter can be used to compute a checksum on data divided into chunks: performing
`crc32(data2, crc32(data1))` is equivalent to the checksum of `[data1; data2]`.

There is also a method `crc32(io, nb, crc)` to checksum `nb` bytes from
Expand Down
36 changes: 30 additions & 6 deletions src/CRC32.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@ module CRC32
export crc32

# contiguous byte arrays compatible with C `unsigned char *` API of zlib
const ByteArray = Union{Array{UInt8},
Base.FastContiguousSubArray{UInt8,N,<:Array{UInt8}} where N,
Base.CodeUnits{UInt8, String}, Base.CodeUnits{UInt8, SubString{String}}}
if VERSION ≥ v"1.11"
const ByteArray = Union{Array{UInt8},
Memory{UInt8},
Base.FastContiguousSubArray{UInt8,N,<:Array{UInt8}} where N,
Base.FastContiguousSubArray{UInt8,1,Memory{UInt8}},
Base.CodeUnits{UInt8, String}, Base.CodeUnits{UInt8, SubString{String}}}
else
const ByteArray = Union{Array{UInt8},
Base.FastContiguousSubArray{UInt8,N,<:Array{UInt8}} where N,
Base.CodeUnits{UInt8, String}, Base.CodeUnits{UInt8, SubString{String}}}
end

"""
crc32(data, crc::UInt32=0x00000000)

Compute the CRC-32 checksum (ISO 3309, ITU-T V.42, CRC-32-IEEE) of the given `data`, which can be
an `Array{UInt8}`, a contiguous subarray thereof, or a `String`. Optionally, you can pass
a starting `crc` integer to be mixed in with the checksum. The `crc` parameter
can be used to compute a checksum on data divided into chunks: performing
an `Array{UInt8}`, a contiguous subarray thereof, an `AbstractVector{UInt8}`, or a `String`.
Optionally, you can pass a starting `crc` integer to be mixed in with the checksum.
The `crc` parameter can be used to compute a checksum on data divided into chunks: performing
`crc32(data2, crc32(data1))` is equivalent to the checksum of `[data1; data2]`.

There is also a method `crc32(io, nb, crc)` to checksum `nb` bytes from
Expand All @@ -39,6 +47,7 @@ For a `String`, note that the result is specific to the UTF-8 encoding
function crc32 end

crc32(a::ByteArray, crc::UInt32=0x00000000) = _crc32(a, crc)
crc32(a::AbstractVector{UInt8}, crc::UInt32=0x00000000) = _crc32(a, crc)
crc32(s::Union{String, SubString{String}}, crc::UInt32=0x00000000) = _crc32(s, crc)

"""
Expand Down Expand Up @@ -66,6 +75,21 @@ function _crc32(s::Union{String, SubString{String}}, crc::UInt32=0x00000000)
unsafe_crc32(s, sizeof(s) % Csize_t, crc)
end

function _crc32(a::AbstractVector{UInt8}, crc::UInt32=0x00000000)
# use block size 24576=8192*3, since that is the threshold for
# 3-way parallel SIMD code in the underlying jl_crc32 C function.
last = lastindex(a)
nb = length(a)
buf = Vector{UInt8}(undef, min(nb, 24576))
while nb > 0
n = min(nb, 24576)
copyto!(buf, 1, a, last - nb + 1, n)
crc = unsafe_crc32(buf, n % Csize_t, crc)
nb -= n
end
return crc
end

function _crc32(io::IO, nb::Integer, crc::UInt32=0x00000000)
nb < 0 && throw(ArgumentError("number of bytes to checksum must be ≥ 0, got $nb"))
# use block size 24576=8192*3, since that is the threshold for
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CRC32
using Test
using OffsetArrays: Origin

# based on julia/stdlib/CRC32/test/runtests.jl

Expand All @@ -9,6 +10,12 @@ using Test
s = String(UInt8[1:n;])
ss = SubString(String(UInt8[0:(n+1);]), 2:(n+1))
@test crc32(UInt8[1:n;]) == crc == crc32(s) == crc32(ss) == crc32(codeunits(s)) == crc32(codeunits(ss))
@test crc == crc32(UInt8(1):UInt8(n))
if VERSION ≥ v"1.11"
m = Memory{UInt8}(undef, n)
m .= 1:n
@test crc == crc32(m)
end
end

# test that crc parameter is equivalent to checksum of concatenated data,
Expand Down Expand Up @@ -59,4 +66,9 @@ using Test
if Sys.WORD_SIZE == 64
@test crc32(zeros(UInt8, 2^32)) == 0xd202ef8d #crc32(zeros(UInt8, 2^31), crc32(zeros(UInt8, 2^31)))
end

# Test crc of AbstractVector{UInt8}
a = view(rand(UInt8, 300000), 1:2:300000)
@test crc32(a) == crc32(collect(a))
@test crc32(Origin(0)(b"hello")) == crc32(b"hello")
end