Skip to content

Commit

Permalink
Fix PDF417 message decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Arclite committed Dec 15, 2024
1 parent 80946b5 commit 7b29f7d
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Sources/App/PDF417MessageDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ struct PDF417MessageDecoder {
private let intConverter = IntToCodewordConverter()
private let decoder = HumanReadableDecoder()
func decodedMessage(from string: String) throws -> String {
let codewords = try string.base64Decoded().map(Int.init).map(intConverter.codeword(for:))
dump(codewords)
let bytes = try string.base64Decoded()
let codewords = try stride(from: bytes.startIndex, to: bytes.endIndex, by: 2)
.compactMap { index -> UInt16? in
guard index + 1 < bytes.endIndex else { return nil }
let networkOrder = UInt16(bytes[index]) << 8 | UInt16(bytes[index + 1])
return UInt16(bigEndian: networkOrder)
}
.map(Int.init)
.map(intConverter.codeword(for:))
return try decoder.string(for: codewords)
}
}

0 comments on commit 7b29f7d

Please sign in to comment.