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 32 bit systems by explicitly using Int64 #52

Merged
merged 1 commit into from
Oct 29, 2024
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
44 changes: 22 additions & 22 deletions src/BSDiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,14 @@ generate_index(data::AbstractVector{<:UInt8}) = suffixsort(data, 0)
# transform used to serialize integers to avoid lots of
# high bytes being emitted for small negative values
int_io(x::Signed) = ifelse(x == abs(x), x, typemin(x) - x)
write_int(io::IO, x::Signed) = write(io, int_io(Int64(x)))
read_int(io::IO) = Int(int_io(read(io, Int64)))
write_int(io::IO, x::Signed) = write(io, htol(int_io(Int64(x))))
read_int(io::IO) = int_io(ltoh(read(io, Int64)))

"""
Return lexicographic order and length of common prefix.
"""
function strcmplen(p::Ptr{UInt8}, m::Int, q::Ptr{UInt8}, n::Int)
i = 0
function strcmplen(p::Ptr{UInt8}, m::Int64, q::Ptr{UInt8}, n::Int64)
i = Int64(0)
while i < min(m, n)
a = unsafe_load(p + i)
b = unsafe_load(q + i)
Expand All @@ -232,18 +232,18 @@ function prefix_search(
index::IndexType, # suffix array
old::AbstractVector{UInt8}, # old data to search in
new::AbstractVector{UInt8}, # new data to search for
t::Int, # search for longest match of new[t:end]
t::Int64, # search for longest match of new[t:end]
)
old_n = length(old)
new_n = length(new) - t + 1
old_n = Int64(length(old))
new_n = Int64(length(new)) - t + 1
old_p = pointer(old)
new_p = pointer(new, t)
# invariant: longest match is in index[lo:hi]
lo, hi = 1, old_n
c = lo_c = hi_c = 0
lo, hi = Int64(1), old_n
c = lo_c = hi_c = Int64(0)
while hi - lo ≥ 2
m = (lo + hi) >>> 1
s = index[m]
s = Int64(index[m])
x, l = strcmplen(new_p+c, new_n+c, old_p+s+c, old_n-s-c)
if 0 < x
lo, lo_c = m, c+l
Expand All @@ -252,7 +252,7 @@ function prefix_search(
end
c = min(lo_c, hi_c)
end
lo_c > hi_c ? (index[lo]+1, lo_c) : (index[hi]+1, hi_c)
lo_c > hi_c ? (Int64(index[lo])+1, lo_c) : (Int64(index[hi])+1, hi_c)
end

"""
Expand All @@ -265,11 +265,11 @@ function generate_patch(
new::AbstractVector{UInt8},
index::IndexType = generate_index(old),
)
oldsize, newsize = length(old), length(new)
scan = len = pos = lastscan = lastpos = lastoffset = 0
oldsize, newsize = Int64(length(old)), Int64(length(new))
scan = len = pos = lastscan = lastpos = lastoffset = Int64(0)

while scan < newsize
oldscore = 0
oldscore = Int64(0)
scsc = scan += len
while scan < newsize
pos, len = prefix_search(index, old, new, scan+1)
Expand All @@ -287,7 +287,7 @@ function generate_patch(
scan += 1
end
if len ≠ oldscore || scan == newsize
i = s = Sf = lenf = 0
i = s = Sf = lenf = Int64(0)
while lastscan + i < scan && lastpos + i < oldsize
s += old[lastpos + i + 1] == new[lastscan + i + 1]
i += 1
Expand All @@ -296,10 +296,10 @@ function generate_patch(
lenf = i
end
end
lenb = 0
lenb = Int64(0)
if scan < newsize
s = Sb = 0
i = 1
s = Sb = Int64(0)
i = Int64(1)
while scan ≥ lastscan + i && pos ≥ i
s += old[pos - i + 1] == new[scan - i + 1]
if 2s - i > 2Sb - lenb
Expand All @@ -311,7 +311,7 @@ function generate_patch(
end
if lastscan + lenf > scan - lenb
overlap = (lastscan + lenf) - (scan - lenb)
i = s = Ss = lens = 0
i = s = Ss = lens = Int64(0)
while i < overlap
s += new[lastscan + lenf - overlap + i + 1] ==
old[lastpos + lenf - overlap + i + 1]
Expand Down Expand Up @@ -352,10 +352,10 @@ function apply_patch(
patch::Patch,
old::AbstractVector{UInt8},
new::IO,
new_size::Int = hasfield(typeof(patch), :new_size) ? patch.new_size : typemax(Int),
new_size::Int64 = hasfield(typeof(patch), :new_size) ? patch.new_size : typemax(Int64),
)
old_pos = new_pos = 0
old_size = length(old)
old_pos = new_pos = Int64(0)
old_size = Int64(length(old))
while true
ctrl = decode_control(patch)
ctrl == nothing && break
Expand Down
28 changes: 14 additions & 14 deletions src/classic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ function write_start(
old_data::AbstractVector{UInt8},
new_data::AbstractVector{UInt8},
)
ClassicPatch(patch_io, length(new_data))
ClassicPatch(patch_io, Int64(length(new_data)))
end

function read_start(::Type{ClassicPatch}, patch_io::IO)
ctrl_size = read_int(patch_io)
diff_size = read_int(patch_io)
ctrl_size = Int(read_int(patch_io))
diff_size = Int(read_int(patch_io))
new_size = read_int(patch_io)
ctrl_io = IOBuffer(read(patch_io, ctrl_size))
diff_io = IOBuffer(read(patch_io, diff_size))
Expand Down Expand Up @@ -63,9 +63,9 @@ end

function encode_control(
patch::ClassicPatch,
diff_size::Int,
copy_size::Int,
skip_size::Int,
diff_size::Int64,
copy_size::Int64,
skip_size::Int64,
)
write_int(patch.ctrl, diff_size)
write_int(patch.ctrl, copy_size)
Expand All @@ -82,9 +82,9 @@ end

function encode_diff(
patch::ClassicPatch,
diff_size::Int,
new::AbstractVector{UInt8}, new_pos::Int,
old::AbstractVector{UInt8}, old_pos::Int,
diff_size::Int64,
new::AbstractVector{UInt8}, new_pos::Int64,
old::AbstractVector{UInt8}, old_pos::Int64,
)
for i = 1:diff_size
write(patch.diff, new[new_pos + i] - old[old_pos + i])
Expand All @@ -93,10 +93,10 @@ end

function decode_diff(
patch::ClassicPatch,
diff_size::Int,
diff_size::Int64,
new::IO,
old::AbstractVector{UInt8},
old_pos::Int,
old_pos::Int64,
)
for i = 1:diff_size
write(new, old[old_pos + i] + read(patch.diff, UInt8))
Expand All @@ -105,8 +105,8 @@ end

function encode_data(
patch::ClassicPatch,
copy_size::Int,
new::AbstractVector{UInt8}, pos::Int,
copy_size::Int64,
new::AbstractVector{UInt8}, pos::Int64,
)
for i = 1:copy_size
write(patch.data, new[pos + i])
Expand All @@ -115,7 +115,7 @@ end

function decode_data(
patch::ClassicPatch,
copy_size::Int,
copy_size::Int64,
new::IO,
)
for i = 1:copy_size
Expand Down
24 changes: 12 additions & 12 deletions src/endsley.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function write_start(
old_data::AbstractVector{UInt8},
new_data::AbstractVector{UInt8},
)
new_size = length(new_data)
new_size = Int64(length(new_data))
write_int(patch_io, new_size)
stream = TranscodingStream(compressor(), patch_io)
patch = EndsleyPatch(stream, new_size)
Expand Down Expand Up @@ -42,9 +42,9 @@ end

function encode_control(
patch::EndsleyPatch,
diff_size::Int,
copy_size::Int,
skip_size::Int,
diff_size::Int64,
copy_size::Int64,
skip_size::Int64,
)
write_int(patch.io, diff_size)
write_int(patch.io, copy_size)
Expand All @@ -61,9 +61,9 @@ end

function encode_diff(
patch::EndsleyPatch,
diff_size::Int,
new::AbstractVector{UInt8}, new_pos::Int,
old::AbstractVector{UInt8}, old_pos::Int,
diff_size::Int64,
new::AbstractVector{UInt8}, new_pos::Int64,
old::AbstractVector{UInt8}, old_pos::Int64,
)
for i = 1:diff_size
write(patch.io, new[new_pos + i] - old[old_pos + i])
Expand All @@ -72,10 +72,10 @@ end

function decode_diff(
patch::EndsleyPatch,
diff_size::Int,
diff_size::Int64,
new::IO,
old::AbstractVector{UInt8},
old_pos::Int,
old_pos::Int64,
)
for i = 1:diff_size
write(new, old[old_pos + i] + read(patch.io, UInt8))
Expand All @@ -84,8 +84,8 @@ end

function encode_data(
patch::EndsleyPatch,
copy_size::Int,
new::AbstractVector{UInt8}, pos::Int,
copy_size::Int64,
new::AbstractVector{UInt8}, pos::Int64,
)
for i = 1:copy_size
write(patch.io, new[pos + i])
Expand All @@ -94,7 +94,7 @@ end

function decode_data(
patch::EndsleyPatch,
copy_size::Int,
copy_size::Int64,
new::IO,
)
for i = 1:copy_size
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ const FORMATS = sort!(collect(keys(BSDiff.FORMATS)))
# test that diff is identical to reference diff
index = BSDiff.generate_index(old_data)
diff = sprint() do io
patch = BSDiff.EndsleyPatch(io, length(new_data))
patch = BSDiff.EndsleyPatch(io, Int64(length(new_data)))
BSDiff.generate_patch(patch, old_data, new_data, index)
end |> codeunits
@test read(ref) == diff
# test that applying reference patch to old produces new
new_data′ = open(ref) do io
patch = BSDiff.EndsleyPatch(io, length(new_data))
patch = BSDiff.EndsleyPatch(io, Int64(length(new_data)))
sprint() do new_io
BSDiff.apply_patch(patch, old_data, new_io)
end |> codeunits
Expand Down
Loading