Skip to content

Commit 244d4bf

Browse files
stevengjmbauman
authored andcommitted
fix base64decode to return array of decoded bytes, not a string (see JuliaLang#9157)
1 parent d9aed9f commit 244d4bf

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

Diff for: base/base64.jl

+9-16
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,6 @@ base64encode(x...) = base64encode(write, x...)
163163

164164
#############################################################################
165165

166-
# read(b::Base64Pipe, ::Type{UInt8}) = # TODO: decode base64
167-
168-
#############################################################################
169-
170166
type Base64DecodePipe <: IO
171167
io::IO
172168
# reading works in blocks of 4 characters that are decoded into 3 bytes and 2 of them cached
@@ -182,7 +178,7 @@ end
182178

183179
function read(b::Base64DecodePipe, t::Type{UInt8})
184180
if length(b.cache) > 0
185-
val = shift!(b.cache)
181+
return shift!(b.cache)
186182
else
187183
empty!(b.encvec)
188184
while !eof(b.io) && length(b.encvec) < 4
@@ -191,24 +187,21 @@ function read(b::Base64DecodePipe, t::Type{UInt8})
191187
push!(b.encvec, c)
192188
end
193189
end
194-
val = b64decode!(b.encvec,b.cache)
190+
return b64decode!(b.encvec,b.cache)
195191
end
196-
val
197-
end
198-
199-
function eof(b::Base64DecodePipe)
200-
return length(b.cache) == 0 && eof(b.io)
201192
end
202193

203-
function close(b::Base64DecodePipe)
204-
end
194+
eof(b::Base64DecodePipe) = length(b.cache) == 0 && eof(b.io)
195+
close(b::Base64DecodePipe) = nothing
205196

206197
# Decodes a base64-encoded string
207198
function base64decode(s)
208199
b = IOBuffer(s)
209-
decoded = readall(Base64DecodePipe(b))
210-
close(b)
211-
decoded
200+
try
201+
return readbytes(Base64DecodePipe(b))
202+
finally
203+
close(b)
204+
end
212205
end
213206

214207
end # module

Diff for: doc/stdlib/io-network.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ Text I/O
436436

437437
.. function:: base64decode(string)
438438

439-
Decodes the base64-encoded ``string`` and returns the obtained bytes.
440-
439+
Decodes the base64-encoded ``string`` and returns a ``Vector{UInt8}``
440+
of the decoded bytes.
441441

442442
Multimedia I/O
443443
--------------

Diff for: test/base64.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ end
2424
rm(fname)
2525

2626
# Encode to string and decode
27-
@test base64decode(base64encode(inputText)) == inputText
27+
@test utf8(base64decode(base64encode(inputText))) == inputText
2828

2929
# Decode with max line chars = 76 and padding
3030
ipipe = Base64DecodePipe(IOBuffer(encodedMaxLine76))

0 commit comments

Comments
 (0)