Skip to content

Commit cabb047

Browse files
committed
minor improvements
1 parent 9ed6059 commit cabb047

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

MimeKit/Encodings/Base64Validator.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,32 @@ unsafe void Validate (ref byte table, byte* input, int length)
103103
byte rank = Unsafe.Add (ref table, c);
104104

105105
if (rank == 0xFF) {
106+
// The current byte is outside of the base64 alphabet, but could be whitespace (which we will treat as valid).
106107
if (c == (byte) '\n') {
107108
if (octets % 4 != 0)
108109
reader.OnMimeComplianceViolation (MimeComplianceViolation.IncompleteBase64Quantum, streamOffset, lineNumber);
109110

110111
lineNumber++;
111112
octets = 0;
112113
} else if (!c.IsWhitespace ()) {
114+
// This is an invalid base64 character.
113115
reader.OnMimeComplianceViolation (MimeComplianceViolation.InvalidBase64Character, streamOffset, lineNumber);
114116
}
115117
} else if (c == (byte) '=') {
118+
// An '=' char is a valid base64 character, but is special and indicates the end of the content (other than additional padding).
119+
if (octets % 4 < 2) {
120+
// Padding is only valid in the last 2 positions of the final quantum.
121+
reader.OnMimeComplianceViolation (MimeComplianceViolation.InvalidBase64Padding, streamOffset, lineNumber);
122+
invalid = true;
123+
return;
124+
}
125+
116126
streamOffset++;
117127
padding = 1;
118128
octets++;
119129
break;
120130
} else {
131+
// Increment the number of octets in this base64 quantum (a quantum is a series of 4 octets).
121132
octets++;
122133
}
123134

MimeKit/Encodings/QuotedPrintableValidator.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,14 @@ unsafe void Validate (byte* input, int length)
135135
inptr++;
136136
break;
137137
case QpValidatorState.SoftBreak:
138-
c = *inptr;
139-
140-
if (c == '\n') {
138+
if (*inptr == '\n') {
141139
lineNumber++;
140+
inptr++;
142141
} else {
143142
reader.OnMimeComplianceViolation (MimeComplianceViolation.InvalidQuotedPrintableSoftBreak, streamOffset + (inptr - input), lineNumber);
144143
}
145144

146145
state = QpValidatorState.PassThrough;
147-
inptr++;
148146
break;
149147
case QpValidatorState.DecodeByte:
150148
c = *inptr;

0 commit comments

Comments
 (0)