Skip to content

Commit 778e00d

Browse files
authored
Fixes decoding (#23)
1 parent bf6706e commit 778e00d

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Diff for: Sources/ExtrasBase64/Chromium.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ extension Base64 {
341341
}
342342

343343
// inIndex is the first index in the last chunk
344-
let inIndex = fullchunks > 1 ? fullchunks * 4 : 0
344+
let inIndex = fullchunks * 4
345345
let a0 = inBuffer[inIndex]
346346
let a1 = inBuffer[inIndex + 1]
347347
var a2: UInt8?

Diff for: Tests/ExtrasBase64Tests/ChromiumTests.swift

+24
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ class ChromiumTests: XCTestCase {
8686
}
8787
}
8888

89+
func testBase64DecodingOneTwoThreeFour() {
90+
let base64 = "AQIDBA=="
91+
let bytes: [UInt8] = [1, 2, 3, 4]
92+
93+
XCTAssertEqual(Base64.encodeString(bytes: bytes), base64)
94+
XCTAssertEqual(try Base64.decode(string: base64), bytes)
95+
}
96+
97+
func testBase64DecodingOneTwoThreeFourFive() {
98+
let base64 = "AQIDBAU="
99+
let bytes: [UInt8] = [1, 2, 3, 4, 5]
100+
101+
XCTAssertEqual(Base64.encodeString(bytes: bytes), base64)
102+
XCTAssertEqual(try Base64.decode(string: base64), bytes)
103+
}
104+
105+
func testBase64DecodingOneTwoThreeFourFiveSix() {
106+
let base64 = "AQIDBAUG"
107+
let bytes: [UInt8] = [1, 2, 3, 4, 5, 6]
108+
109+
XCTAssertEqual(Base64.encodeString(bytes: bytes), base64)
110+
XCTAssertEqual(try Base64.decode(string: base64), bytes)
111+
}
112+
89113
func testBase64DecodingWithInvalidLength() {
90114
XCTAssertThrowsError(_ = try Base64.decode(bytes: "AAAAA".utf8)) { error in
91115
XCTAssertEqual(error as? DecodingError, .invalidLength)

0 commit comments

Comments
 (0)