@@ -349,65 +349,44 @@ public static bool TestPermission(string directoryPath)
349349 /// <returns>内核编译签名信息</returns>
350350 public static string ReadKernelVersion ( string filePath )
351351 {
352- if ( ! File . Exists ( filePath ) ) return "" ;
353- try
352+ byte [ ] Signature = [ 0x69 , 0x6e , 0x69 , 0x74 , 0x63 , 0x61 , 0x6c , 0x6c , 0x5f , 0x64 , 0x65 , 0x62 , 0x75 , 0x67 , 0x00 ] ;
353+ using FileStream fs = new FileStream ( filePath , FileMode . Open , FileAccess . Read ) ;
354+ using BinaryReader br = new BinaryReader ( fs ) ;
355+ long signaturePosition = FindSignaturePosition ( br , Signature ) ;
356+ if ( signaturePosition == - 1 )
354357 {
355-
356- byte [ ] initcallSignature = [ 0x69 , 0x6e , 0x69 , 0x74 , 0x63 , 0x61 , 0x6c , 0x6c , 0x5f , 0x64 , 0x65 , 0x62 , 0x75 , 0x67 , 0x00 ] ;
357- byte [ ] linuxVersionSignature = Encoding . ASCII . GetBytes ( "Linux version " ) ;
358-
359- using FileStream fs = new FileStream ( filePath , FileMode . Open , FileAccess . Read ) ;
360- byte [ ] data = new byte [ fs . Length ] ;
361- fs . ReadExactly ( data ) ;
362-
363- // initcall_debug
364- int index = IndexOf ( data , initcallSignature ) ;
365- if ( index != - 1 )
366- {
367- return ReadNullTerminatedString ( data , index + initcallSignature . Length ) ;
368- }
369-
370- // Linux version
371- index = IndexOf ( data , linuxVersionSignature ) ;
372- if ( index != - 1 )
373- {
374- return ReadNullTerminatedString ( data , index ) ;
375- }
358+ return "" ;
376359 }
377- catch { }
378- return "" ;
360+ _ = fs . Seek ( signaturePosition + Signature . Length , SeekOrigin . Begin ) ;
361+ return ReadUntilTerminator ( br ) ;
379362 }
380-
381- private static int IndexOf ( byte [ ] data , byte [ ] pattern )
363+ private static long FindSignaturePosition ( BinaryReader reader , byte [ ] signature )
382364 {
383- if ( pattern . Length > data . Length ) return - 1 ;
384- for ( int i = 0 ; i <= data . Length - pattern . Length ; i ++ )
365+ byte [ ] buffer = new byte [ signature . Length ] ;
366+ long position = 0 ;
367+ while ( position + signature . Length <= reader . BaseStream . Length )
385368 {
386- bool found = true ;
387- for ( int j = 0 ; j < pattern . Length ; j ++ )
369+ _ = reader . BaseStream . Seek ( position , SeekOrigin . Begin ) ;
370+ _ = reader . Read ( buffer , 0 , signature . Length ) ;
371+ if ( buffer . SequenceEqual ( signature ) )
388372 {
389- if ( data [ i + j ] != pattern [ j ] )
390- {
391- found = false ;
392- break ;
393- }
373+ return position ;
394374 }
395- if ( found ) return i ;
375+ position ++ ;
396376 }
397377 return - 1 ;
398378 }
399-
400- private static string ReadNullTerminatedString ( byte [ ] data , int startIndex )
379+ private static string ReadUntilTerminator ( BinaryReader reader )
401380 {
402381 StringBuilder sb = new StringBuilder ( ) ;
403- for ( int i = startIndex ; i < data . Length ; i ++ )
382+ int b ;
383+ while ( ( b = reader . ReadByte ( ) ) != 0x00 )
404384 {
405- if ( data [ i ] == 0 )
406- {
407- if ( sb . Length == 0 ) continue ;
408- break ;
409- }
410- sb . Append ( ( char ) data [ i ] ) ;
385+ _ = sb . Append ( ( char ) b ) ;
386+ }
387+ while ( ( b = reader . ReadByte ( ) ) != 0x00 )
388+ {
389+ _ = sb . Append ( ( char ) b ) ;
411390 }
412391 return sb . ToString ( ) ;
413392 }
0 commit comments