11using System ;
22using System . Collections . Generic ;
33using System . Diagnostics ;
4+ using System . Globalization ;
45using System . IO ;
56using System . Text ;
67using System . Text . RegularExpressions ;
@@ -42,7 +43,7 @@ public sealed class ApplicationConfig
4243
4344 static readonly object ndkInitLock = new object ( ) ;
4445 static readonly char [ ] readElfFieldSeparator = new [ ] { ' ' , '\t ' } ;
45- static readonly Regex stringLabelRegex = new Regex ( "^\\ .L\\ .env \\ .str \\ .[0-9]+:" , RegexOptions . Compiled ) ;
46+ static readonly Regex stringLabelRegex = new Regex ( "^\\ .L\\ .autostr \\ .[0-9]+:" , RegexOptions . Compiled ) ;
4647
4748 static readonly HashSet < string > expectedPointerTypes = new HashSet < string > ( StringComparer . Ordinal ) {
4849 ".long" ,
@@ -103,7 +104,6 @@ static ApplicationConfig ReadApplicationConfig (string envFile)
103104
104105 line = lines [ ++ i ] ;
105106 field = GetField ( envFile , line , i ) ;
106-
107107 AssertFieldType ( envFile , ".asciz" , field [ 0 ] , i ) ;
108108 strings [ label ] = AssertIsAssemblerString ( envFile , field [ 1 ] , i ) ;
109109 continue ;
@@ -222,7 +222,7 @@ static ApplicationConfig ReadApplicationConfig (string envFile)
222222 if ( String . Compare ( ".size" , field [ 0 ] , StringComparison . Ordinal ) == 0 ) {
223223 fieldCount -- ;
224224 Assert . IsTrue ( field [ 1 ] . StartsWith ( "application_config" , StringComparison . Ordinal ) , $ "Mismatched .size directive in '{ envFile } :{ i } '") ;
225- break ; // We've reached the end of the application_config structure
225+ gatherFields = false ; // We've reached the end of the application_config structure
226226 }
227227 }
228228 Assert . AreEqual ( ApplicationConfigFieldCount , fieldCount , $ "Invalid 'application_config' field count in environment file '{ envFile } '") ;
@@ -317,13 +317,20 @@ static Dictionary<string, string> ReadEnvironmentVariables (string envFile)
317317 static bool IsCommentLine ( string line )
318318 {
319319 string l = line ? . Trim ( ) ;
320- return ! String . IsNullOrEmpty ( l ) && l . StartsWith ( "/*" , StringComparison . Ordinal ) ;
320+ if ( String . IsNullOrEmpty ( l ) ) {
321+ return false ;
322+ }
323+
324+ return l . StartsWith ( "/*" , StringComparison . Ordinal ) ||
325+ l . StartsWith ( "//" , StringComparison . Ordinal ) ||
326+ l . StartsWith ( "#" , StringComparison . Ordinal ) ||
327+ l . StartsWith ( "@" , StringComparison . Ordinal ) ;
321328 }
322329
323330 static string [ ] GetField ( string file , string line , int lineNumber )
324331 {
325332 string [ ] ret = line ? . Trim ( ) ? . Split ( '\t ' ) ;
326- Assert . AreEqual ( 2 , ret . Length , $ "Invalid assembler field format in file '{ file } :{ lineNumber } ': '{ line } '") ;
333+ Assert . IsTrue ( ret . Length >= 2 , $ "Invalid assembler field format in file '{ file } :{ lineNumber } ': '{ line } '") ;
327334
328335 return ret ;
329336 }
@@ -503,10 +510,11 @@ static void DumpLines (string streamName, List <string> lines)
503510
504511 static bool ConvertFieldToBool ( string fieldName , string envFile , int fileLine , string value )
505512 {
506- Assert . AreEqual ( 1 , value . Length , $ "Field '{ fieldName } ' in { envFile } :{ fileLine } is not a valid boolean value (too long)") ;
513+ // Allow both decimal and hexadecimal values
514+ Assert . IsTrue ( value . Length > 0 && value . Length <= 3 , $ "Field '{ fieldName } ' in { envFile } :{ fileLine } is not a valid boolean value (length not between 1 and 3)") ;
507515
508516 uint fv ;
509- Assert . IsTrue ( UInt32 . TryParse ( value , out fv ) , $ "Field '{ fieldName } ' in { envFile } :{ fileLine } is not a valid boolean value (not a valid integer)") ;
517+ Assert . IsTrue ( TryParseInteger ( value , out fv ) , $ "Field '{ fieldName } ' in { envFile } :{ fileLine } is not a valid boolean value (not a valid integer)") ;
510518 Assert . IsTrue ( fv == 0 || fv == 1 , $ "Field '{ fieldName } ' in { envFile } :{ fileLine } is not a valid boolean value (not a valid boolean value 0 or 1)") ;
511519
512520 return fv == 1 ;
@@ -517,7 +525,7 @@ static uint ConvertFieldToUInt32 (string fieldName, string envFile, int fileLine
517525 Assert . IsTrue ( value . Length > 0 , $ "Field '{ fieldName } ' in { envFile } :{ fileLine } is not a valid uint32_t value (not long enough)") ;
518526
519527 uint fv ;
520- Assert . IsTrue ( UInt32 . TryParse ( value , out fv ) , $ "Field '{ fieldName } ' in { envFile } :{ fileLine } is not a valid uint32_t value (not a valid integer)") ;
528+ Assert . IsTrue ( TryParseInteger ( value , out fv ) , $ "Field '{ fieldName } ' in { envFile } :{ fileLine } is not a valid uint32_t value (not a valid integer)") ;
521529
522530 return fv ;
523531 }
@@ -527,9 +535,27 @@ static byte ConvertFieldToByte (string fieldName, string envFile, int fileLine,
527535 Assert . IsTrue ( value . Length > 0 , $ "Field '{ fieldName } ' in { envFile } :{ fileLine } is not a valid uint8_t value (not long enough)") ;
528536
529537 byte fv ;
530- Assert . IsTrue ( Byte . TryParse ( value , out fv ) , $ "Field '{ fieldName } ' in { envFile } :{ fileLine } is not a valid uint8_t value (not a valid integer)") ;
538+ Assert . IsTrue ( TryParseInteger ( value , out fv ) , $ "Field '{ fieldName } ' in { envFile } :{ fileLine } is not a valid uint8_t value (not a valid integer)") ;
531539
532540 return fv ;
533541 }
542+
543+ static bool TryParseInteger ( string value , out uint fv )
544+ {
545+ if ( value . StartsWith ( "0x" , StringComparison . Ordinal ) ) {
546+ return UInt32 . TryParse ( value . Substring ( 2 ) , NumberStyles . AllowHexSpecifier , null , out fv ) ;
547+ }
548+
549+ return UInt32 . TryParse ( value , out fv ) ;
550+ }
551+
552+ static bool TryParseInteger ( string value , out byte fv )
553+ {
554+ if ( value . StartsWith ( "0x" , StringComparison . Ordinal ) ) {
555+ return Byte . TryParse ( value . Substring ( 2 ) , NumberStyles . AllowHexSpecifier , null , out fv ) ;
556+ }
557+
558+ return Byte . TryParse ( value , out fv ) ;
559+ }
534560 }
535561}
0 commit comments