Skip to content

Commit

Permalink
Make base64 decoder tolerant
Browse files Browse the repository at this point in the history
Add padding if necessary, Foundation is picky
about those.
Also fix the recursion in base64url.
  • Loading branch information
helje5 committed Dec 12, 2024
1 parent 6a4971e commit b75c788
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions Sources/MacroCore/Buffer/BufferStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,23 @@ public extension Buffer {
return buffer

case "base64":
guard let data = Data(base64Encoded: String(string)) else {
throw DataDecodingError.failedToDecodeBase64
let s = String(string)
if let data = Data(base64Encoded: s) { return Buffer(data) }

// https://stackoverflow.com/questions/4080988/why-does-base64-encoding
switch s.count % 4 {
case 0: // should have decoded above
throw DataDecodingError.failedToDecodeBase64
case 1: // invalid base64
throw DataDecodingError.failedToDecodeBase64
case 2:
if let data = Data(base64Encoded: s + "==") { return Buffer(data) }
case 3:
if let data = Data(base64Encoded: s + "=") { return Buffer(data) }
default:
assertionFailure("Invalid % operation.")
}
return Buffer(data)
throw DataDecodingError.failedToDecodeBase64

case "base64url":
return try Self.from(String(string.map {
Expand All @@ -163,7 +176,7 @@ public extension Buffer {
case "_" : return "/"
default : return $0
}
}), encoding)
}), "base64")

default:
return try from(string, .encodingWithName(encoding))
Expand Down

0 comments on commit b75c788

Please sign in to comment.