Skip to content

Commit cc5fc93

Browse files
authored
Merge pull request #2997 from ogres/main
[SR-14496] - When Base64 encoding, only add line separators if there …
2 parents 0a485f5 + 7f8462a commit cc5fc93

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

Sources/Foundation/NSData.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,12 +900,14 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
900900
// anyway.
901901
buffer.baseAddress!.advanced(by: outputIndex).copyMemory(from: &outputBytes, byteCount: 4)
902902
outputIndex += 4
903+
bytesLeft = dataBuffer.count - inputIndex
904+
903905
if lineLength != 0 {
904906
// Add required EOL markers.
905907
currentLineCount += 4
906908
assert(currentLineCount <= lineLength)
907909

908-
if currentLineCount == lineLength {
910+
if currentLineCount == lineLength && bytesLeft > 0 {
909911
buffer[outputIndex] = separatorByte1
910912
outputIndex += 1
911913

@@ -916,7 +918,6 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
916918
currentLineCount = 0
917919
}
918920
}
919-
bytesLeft = dataBuffer.count - inputIndex
920921
}
921922

922923
// Return the number of ASCII bytes written to the buffer

Tests/Foundation/Tests/TestNSData.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ class TestNSData: LoopbackServerTest {
244244
("test_base64EncodedDataWithOptionToInsertCarriageReturnContainsCarriageReturn", test_base64EncodedDataWithOptionToInsertCarriageReturnContainsCarriageReturn),
245245
("test_base64EncodedDataWithOptionToInsertLineFeedsContainsLineFeed", test_base64EncodedDataWithOptionToInsertLineFeedsContainsLineFeed),
246246
("test_base64EncodedDataWithOptionToInsertCarriageReturnAndLineFeedContainsBoth", test_base64EncodedDataWithOptionToInsertCarriageReturnAndLineFeedContainsBoth),
247+
("test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine", test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine),
248+
("test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine", test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine),
247249
("test_base64EncodedStringGetsEncodedText", test_base64EncodedStringGetsEncodedText),
248250
("test_initializeWithBase64EncodedStringGetsDecodedData", test_initializeWithBase64EncodedStringGetsDecodedData),
249251
("test_base64DecodeWithPadding1", test_base64DecodeWithPadding1),
@@ -814,6 +816,36 @@ class TestNSData: LoopbackServerTest {
814816
XCTAssertEqual(encodedTextResult, encodedText)
815817
}
816818

819+
func test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine() {
820+
821+
XCTAssertEqual(
822+
Data(repeating: 0, count: 48).base64EncodedString(options: .lineLength64Characters),
823+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
824+
"each 3 byte is converted into 4 characterss. 48 / 3 * 4 <= 64, therefore result should not have line separator."
825+
)
826+
827+
XCTAssertEqual(
828+
Data(repeating: 0, count: 57).base64EncodedString(options: .lineLength76Characters),
829+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
830+
"each 3 byte is converted into 4 characterss. 57 / 3 * 4 <= 76, therefore result should not have line separator."
831+
)
832+
}
833+
834+
func test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine() {
835+
836+
XCTAssertEqual(
837+
Data(repeating: 0, count: 49).base64EncodedString(options: .lineLength64Characters),
838+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAA==",
839+
"each 3 byte is converted into 4 characterss. 49 / 3 * 4 > 64, therefore result should have lines with separator."
840+
)
841+
842+
XCTAssertEqual(
843+
Data(repeating: 0, count: 58).base64EncodedString(options: .lineLength76Characters),
844+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAA==",
845+
"each 3 byte is converted into 4 characterss. 58 / 3 * 4 > 76, therefore result should have lines with separator."
846+
)
847+
}
848+
817849
func test_base64EncodedStringGetsEncodedText() {
818850
let plainText = "Revocate animos, maestumque timorem mittite: forsan et haec olim meminisse iuvabit."
819851
let encodedText = "UmV2b2NhdGUgYW5pbW9zLCBtYWVzdHVtcXVlIHRpbW9yZW0gbWl0dGl0ZTogZm9yc2FuIGV0IGhhZWMgb2xpbSBtZW1pbmlzc2UgaXV2YWJpdC4="

0 commit comments

Comments
 (0)