fixes bug in base64 decoder when decoding byte sequence

the decoder accepts string or byte sequence but assume
the inner type is a char, now fixed
This commit is contained in:
jangko 2021-06-17 08:16:56 +07:00
parent 70680e2af2
commit e10da4a90e
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 10 additions and 2 deletions

View File

@ -170,7 +170,7 @@ proc decode*[T: byte|char](btype: typedesc[Base64Types], instr: openarray[T],
var inlen = len(instr)
when (btype is Base64PadTypes):
for i in countdown(inlen - 1, 0):
if instr[i] != '=':
if instr[i] != T('='):
break
dec(inlen)

View File

@ -1,5 +1,5 @@
import unittest
import ../stew/base64
import ../stew/[base64, byteutils]
when defined(nimHasUsed): {.used.}
@ -162,3 +162,11 @@ suite "BASE64 encoding test suite":
decsize == 0
Base64Url.decode("/+", decres, decsize) == Base64Status.Incorrect
decsize == 0
test "Decode byte sequence":
let data = "Hello World".toBytes()
let base64 = Base64.encode(data).toBytes()
var decres = newSeq[byte](20)
var decsize = 0
check:
Base64.decode(base64, decres, decsize) == Base64Status.Success