core :: CString :: check_base64
self
a well-formed Base64 entity ?Will return an Error otherwise with info on which part is erroneous.
# Is `self` a well-formed Base64 entity ?
#
# Will return an Error otherwise with info on which part is erroneous.
fun check_base64(length: Int): nullable Error do
var rlen = 0
var opos = length
for i in [0 .. length[ do
if self[i] == u'=' then
opos = i
break
end
if self[i].is_whitespace then continue
if not self[i].is_base64_char then return new Error("Invalid Base64 character at position {i}: {self[i].code_point}")
rlen += 1
if rlen > 4 then rlen -= 4
end
var pad = 0
for i in [opos .. length[ do
if self[i].is_whitespace then continue
if self[i] != u'=' then return new Error("Invalid padding character {self[i].code_point} at position {i}")
pad += 1
end
if rlen + pad != 4 then return new Error("Invalid padding length")
return null
end
lib/base64/base64.nit:173,2--197,4