Skip to content

Commit

Permalink
[SR-14496] - When Base64 encoding, only add line separators if there …
Browse files Browse the repository at this point in the history
…are more lines available
  • Loading branch information
Sergo Beruashvili committed May 1, 2021
1 parent eec4b26 commit 7f8462a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Sources/Foundation/NSData.swift
Expand Up @@ -900,12 +900,14 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
// anyway.
buffer.baseAddress!.advanced(by: outputIndex).copyMemory(from: &outputBytes, byteCount: 4)
outputIndex += 4
bytesLeft = dataBuffer.count - inputIndex

if lineLength != 0 {
// Add required EOL markers.
currentLineCount += 4
assert(currentLineCount <= lineLength)

if currentLineCount == lineLength {
if currentLineCount == lineLength && bytesLeft > 0 {
buffer[outputIndex] = separatorByte1
outputIndex += 1

Expand All @@ -916,7 +918,6 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
currentLineCount = 0
}
}
bytesLeft = dataBuffer.count - inputIndex
}

// Return the number of ASCII bytes written to the buffer
Expand Down
32 changes: 32 additions & 0 deletions Tests/Foundation/Tests/TestNSData.swift
Expand Up @@ -244,6 +244,8 @@ class TestNSData: LoopbackServerTest {
("test_base64EncodedDataWithOptionToInsertCarriageReturnContainsCarriageReturn", test_base64EncodedDataWithOptionToInsertCarriageReturnContainsCarriageReturn),
("test_base64EncodedDataWithOptionToInsertLineFeedsContainsLineFeed", test_base64EncodedDataWithOptionToInsertLineFeedsContainsLineFeed),
("test_base64EncodedDataWithOptionToInsertCarriageReturnAndLineFeedContainsBoth", test_base64EncodedDataWithOptionToInsertCarriageReturnAndLineFeedContainsBoth),
("test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine", test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine),
("test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine", test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine),
("test_base64EncodedStringGetsEncodedText", test_base64EncodedStringGetsEncodedText),
("test_initializeWithBase64EncodedStringGetsDecodedData", test_initializeWithBase64EncodedStringGetsDecodedData),
("test_base64DecodeWithPadding1", test_base64DecodeWithPadding1),
Expand Down Expand Up @@ -814,6 +816,36 @@ class TestNSData: LoopbackServerTest {
XCTAssertEqual(encodedTextResult, encodedText)
}

func test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine() {

XCTAssertEqual(
Data(repeating: 0, count: 48).base64EncodedString(options: .lineLength64Characters),
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"each 3 byte is converted into 4 characterss. 48 / 3 * 4 <= 64, therefore result should not have line separator."
)

XCTAssertEqual(
Data(repeating: 0, count: 57).base64EncodedString(options: .lineLength76Characters),
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"each 3 byte is converted into 4 characterss. 57 / 3 * 4 <= 76, therefore result should not have line separator."
)
}

func test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine() {

XCTAssertEqual(
Data(repeating: 0, count: 49).base64EncodedString(options: .lineLength64Characters),
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAA==",
"each 3 byte is converted into 4 characterss. 49 / 3 * 4 > 64, therefore result should have lines with separator."
)

XCTAssertEqual(
Data(repeating: 0, count: 58).base64EncodedString(options: .lineLength76Characters),
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAA==",
"each 3 byte is converted into 4 characterss. 58 / 3 * 4 > 76, therefore result should have lines with separator."
)
}

func test_base64EncodedStringGetsEncodedText() {
let plainText = "Revocate animos, maestumque timorem mittite: forsan et haec olim meminisse iuvabit."
let encodedText = "UmV2b2NhdGUgYW5pbW9zLCBtYWVzdHVtcXVlIHRpbW9yZW0gbWl0dGl0ZTogZm9yc2FuIGV0IGhhZWMgb2xpbSBtZW1pbmlzc2UgaXV2YWJpdC4="
Expand Down

0 comments on commit 7f8462a

Please sign in to comment.