Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding corrupts data #94

Open
andrew804 opened this issue Mar 1, 2024 · 0 comments · May be fixed by #95
Open

Encoding corrupts data #94

andrew804 opened this issue Mar 1, 2024 · 0 comments · May be fixed by #95
Labels
bug Something isn't working

Comments

@andrew804
Copy link

Describe the bug

When using FormDataEncoder to encode data objects, specifically JPEG image data, the data gets corrupted.

To Reproduce

This can easily be reproduced by feeding JPEG image data into FormDataEncoder's encode function, then feeding it's output into FormDataDecoder's decode function and comparing the input data with the output data.

let folder = #filePath.split(separator: "/").dropLast().joined(separator: "/")
let path = "/" + folder + "/Utilities/image.jpeg"
let originalData = try Data(contentsOf: URL(fileURLWithPath: path))

struct ObjectToEncode: Codable {
    let data: Data
}

let object = ObjectToEncode(data: originalData)
let boundary = UUID().uuidString

let encoded = try FormDataEncoder().encode(object, boundary: boundary)
let decoded = try FormDataDecoder().decode(ObjectToEncode.self, from: encoded, boundary: boundary)

XCTAssertEqual(originalData, decoded.data)

The comparison fails indicating that the original data is not equal to the decoded data.

When inspecting the bytes it shows a ~90% increase in size. @Joannis suggested one can expect such increase with base64 encoding while @vzsg was more specific pointing out that base64's "expanded size" is 4:3 not 3:2.

@vzsg also expressed concern that the encode method returns a String instead of Data as binary files may not be valid UTF-8.

Expected behavior

The decoded data is equal to the original data.

Environment

multipart-kit version: 4.6.0
swift-tools-version: 5.7
macOS version: 14.2.1

@andrew804 andrew804 added the bug Something isn't working label Mar 1, 2024
@andrew804 andrew804 linked a pull request Mar 1, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant