@@ -252,6 +252,13 @@ protected function dictionaryComplianceError(string $key, string $lastKey): void
252252 $ this ->complianceError ($ msg . " dictionary entry ' " . $ key . "' " , $ offset );
253253 }
254254
255+ protected function digitException (): DecodingException
256+ {
257+ return (str_contains ('0123456789 ' , $ this ->bencoded [$ this ->offset ]))
258+ ? new ComplianceError ('Illegal character ' , $ this ->offset )
259+ : new DecodingException ('Illegal character ' , $ this ->offset );
260+ }
261+
255262 /**
256263 * Return the rightmost boundary to the last safe character that can start a value
257264 *
@@ -276,23 +283,26 @@ protected function getSafeBoundary(): int
276283
277284 protected function readDigits (string $ terminator ): string
278285 {
279- // Digits sorted by decreasing frequency as observed on a random sample of torrent files
280- $ spn = strspn ($ this ->bencoded , '1463720859 ' , $ this ->offset );
281- if ($ spn === 0 )
286+ if ($ this ->bencoded [$ this ->offset ] === '0 ' )
282287 {
283- throw new DecodingException ('Illegal character ' , $ this ->offset );
288+ ++$ this ->offset ;
289+ $ string = '0 ' ;
284290 }
285-
286- $ string = substr ($ this ->bencoded , $ this ->offset , $ spn );
287- if ($ string [0 ] === '0 ' && $ spn !== 1 )
291+ else
288292 {
289- $ this ->complianceError ('Illegal character ' , 1 + $ this ->offset );
293+ // Digits sorted by decreasing frequency as observed on a random sample of torrent files
294+ $ spn = strspn ($ this ->bencoded , '1463720859 ' , $ this ->offset );
295+ if ($ spn === 0 )
296+ {
297+ throw new DecodingException ('Illegal character ' , $ this ->offset );
298+ }
299+ $ string = substr ($ this ->bencoded , $ this ->offset , $ spn );
300+ $ this ->offset += $ spn ;
290301 }
291302
292- $ this ->offset += $ spn ;
293303 if ($ this ->bencoded [$ this ->offset ] !== $ terminator )
294304 {
295- throw new DecodingException ( ' Illegal character ' , $ this ->offset );
305+ throw $ this ->digitException ( );
296306 }
297307 ++$ this ->offset ;
298308
0 commit comments