@@ -88,153 +88,145 @@ private int ProcessRead(ReadOnlySpan<byte> buffer, bool readLine)
8888 throw new IOException ( SR . Format ( SR . net_io_readfailure , SR . net_io_connectionclosed ) ) ;
8989 }
9090
91- unsafe
91+ int i = 0 ;
92+ int length = buffer . Length ;
93+
94+ switch ( _readState )
9295 {
93- fixed ( byte * pBuffer = buffer )
94- {
95- byte * start = pBuffer ;
96- byte * ptr = start ;
97- byte * end = ptr + buffer . Length ;
96+ case ReadState . Status0 :
97+ {
98+ if ( i < length )
99+ {
100+ byte b = buffer [ i ++ ] ;
101+ if ( ! char . IsAsciiDigit ( ( char ) b ) )
102+ {
103+ throw new FormatException ( SR . SmtpInvalidResponse ) ;
104+ }
105+
106+ _statusCode = ( SmtpStatusCode ) ( 100 * ( b - '0' ) ) ;
98107
99- switch ( _readState )
108+ goto case ReadState . Status1 ;
109+ }
110+ _readState = ReadState . Status0 ;
111+ break ;
112+ }
113+ case ReadState . Status1 :
100114 {
101- case ReadState . Status0 :
115+ if ( i < length )
116+ {
117+ byte b = buffer [ i ++ ] ;
118+ if ( ! char . IsAsciiDigit ( ( char ) b ) )
102119 {
103- if ( ptr < end )
104- {
105- byte b = * ptr ++ ;
106- if ( b < '0' && b > '9' )
107- {
108- throw new FormatException ( SR . SmtpInvalidResponse ) ;
109- }
110-
111- _statusCode = ( SmtpStatusCode ) ( 100 * ( b - '0' ) ) ;
112-
113- goto case ReadState . Status1 ;
114- }
115- _readState = ReadState . Status0 ;
116- break ;
120+ throw new FormatException ( SR . SmtpInvalidResponse ) ;
117121 }
118- case ReadState . Status1 :
122+
123+ _statusCode += 10 * ( b - '0' ) ;
124+
125+ goto case ReadState . Status2 ;
126+ }
127+ _readState = ReadState . Status1 ;
128+ break ;
129+ }
130+ case ReadState . Status2 :
131+ {
132+ if ( i < length )
133+ {
134+ byte b = buffer [ i ++ ] ;
135+ if ( ! char . IsAsciiDigit ( ( char ) b ) )
136+ {
137+ throw new FormatException ( SR . SmtpInvalidResponse ) ;
138+ }
139+
140+ _statusCode += b - '0' ;
141+
142+ goto case ReadState . ContinueFlag ;
143+ }
144+ _readState = ReadState . Status2 ;
145+ break ;
146+ }
147+ case ReadState . ContinueFlag :
148+ {
149+ if ( i < length )
150+ {
151+ byte b = buffer [ i ++ ] ;
152+ if ( b == ' ' ) // last line
119153 {
120- if ( ptr < end )
121- {
122- byte b = * ptr ++ ;
123- if ( b < '0' && b > '9' )
124- {
125- throw new FormatException ( SR . SmtpInvalidResponse ) ;
126- }
127-
128- _statusCode += 10 * ( b - '0' ) ;
129-
130- goto case ReadState . Status2 ;
131- }
132- _readState = ReadState . Status1 ;
133- break ;
154+ goto case ReadState . LastCR ;
134155 }
135- case ReadState . Status2 :
156+ else if ( b == '-' ) // more lines coming
136157 {
137- if ( ptr < end )
138- {
139- byte b = * ptr ++ ;
140- if ( b < '0' && b > '9' )
141- {
142- throw new FormatException ( SR . SmtpInvalidResponse ) ;
143- }
144-
145- _statusCode += b - '0' ;
146-
147- goto case ReadState . ContinueFlag ;
148- }
149- _readState = ReadState . Status2 ;
150- break ;
158+ goto case ReadState . ContinueCR ;
151159 }
152- case ReadState . ContinueFlag :
160+ else // error
153161 {
154- if ( ptr < end )
155- {
156- byte b = * ptr ++ ;
157- if ( b == ' ' ) // last line
158- {
159- goto case ReadState . LastCR ;
160- }
161- else if ( b == '-' ) // more lines coming
162- {
163- goto case ReadState . ContinueCR ;
164- }
165- else // error
166- {
167- throw new FormatException ( SR . SmtpInvalidResponse ) ;
168- }
169- }
170- _readState = ReadState . ContinueFlag ;
171- break ;
162+ throw new FormatException ( SR . SmtpInvalidResponse ) ;
172163 }
173- case ReadState . ContinueCR :
164+ }
165+ _readState = ReadState . ContinueFlag ;
166+ break ;
167+ }
168+ case ReadState . ContinueCR :
169+ {
170+ while ( i < length )
171+ {
172+ if ( buffer [ i ++ ] == '\r ' )
174173 {
175- while ( ptr < end )
176- {
177- if ( * ptr ++ == '\r ' )
178- {
179- goto case ReadState . ContinueLF ;
180- }
181- }
182- _readState = ReadState . ContinueCR ;
183- break ;
174+ goto case ReadState . ContinueLF ;
184175 }
185- case ReadState . ContinueLF :
176+ }
177+ _readState = ReadState . ContinueCR ;
178+ break ;
179+ }
180+ case ReadState . ContinueLF :
181+ {
182+ if ( i < length )
183+ {
184+ if ( buffer [ i ++ ] != '\n ' )
186185 {
187- if ( ptr < end )
188- {
189- if ( * ptr ++ != '\n ' )
190- {
191- throw new FormatException ( SR . SmtpInvalidResponse ) ;
192- }
193- if ( readLine )
194- {
195- _readState = ReadState . Status0 ;
196- return ( int ) ( ptr - start ) ;
197- }
198- goto case ReadState . Status0 ;
199- }
200- _readState = ReadState . ContinueLF ;
201- break ;
186+ throw new FormatException ( SR . SmtpInvalidResponse ) ;
202187 }
203- case ReadState . LastCR :
188+ if ( readLine )
204189 {
205- while ( ptr < end )
206- {
207- if ( * ptr ++ == '\r ' )
208- {
209- goto case ReadState . LastLF ;
210- }
211- }
212- _readState = ReadState . LastCR ;
213- break ;
190+ _readState = ReadState . Status0 ;
191+ return i ;
214192 }
215- case ReadState . LastLF :
193+ goto case ReadState . Status0 ;
194+ }
195+ _readState = ReadState . ContinueLF ;
196+ break ;
197+ }
198+ case ReadState . LastCR :
199+ {
200+ while ( i < length )
201+ {
202+ if ( buffer [ i ++ ] == '\r ' )
216203 {
217- if ( ptr < end )
218- {
219- if ( * ptr ++ != '\n ' )
220- {
221- throw new FormatException ( SR . SmtpInvalidResponse ) ;
222- }
223- goto case ReadState . Done ;
224- }
225- _readState = ReadState . LastLF ;
226- break ;
204+ goto case ReadState . LastLF ;
227205 }
228- case ReadState . Done :
206+ }
207+ _readState = ReadState . LastCR ;
208+ break ;
209+ }
210+ case ReadState . LastLF :
211+ {
212+ if ( i < length )
213+ {
214+ if ( buffer [ i ++ ] != '\n ' )
229215 {
230- int actual = ( int ) ( ptr - start ) ;
231- _readState = ReadState . Done ;
232- return actual ;
216+ throw new FormatException ( SR . SmtpInvalidResponse ) ;
233217 }
218+ goto case ReadState . Done ;
219+ }
220+ _readState = ReadState . LastLF ;
221+ break ;
222+ }
223+ case ReadState . Done :
224+ {
225+ _readState = ReadState . Done ;
226+ return i ;
234227 }
235- return ( int ) ( ptr - start ) ;
236- }
237228 }
229+ return i ;
238230 }
239231
240232 internal int Read ( SmtpReplyReader caller , Span < byte > buffer )
0 commit comments