From 9e53befd4622b3e5befc69ccf959f1d4c9b290ca Mon Sep 17 00:00:00 2001 From: Johann Studanski Date: Tue, 21 Jan 2025 19:58:45 +0100 Subject: [PATCH] Add call to preprocessors in the plist parsers --- plist-cil/ASCIIPropertyListParser.cs | 24 ++++++++++++------------ plist-cil/BinaryPropertyListParser.cs | 6 +++--- plist-cil/NSDate.cs | 2 +- plist-cil/NSNumber.cs | 6 +++--- plist-cil/NSString.cs | 4 ++-- plist-cil/PropertyListParser.cs | 8 +++++++- plist-cil/XmlPropertyListParser.cs | 15 ++++++++------- 7 files changed, 36 insertions(+), 29 deletions(-) diff --git a/plist-cil/ASCIIPropertyListParser.cs b/plist-cil/ASCIIPropertyListParser.cs index 42ecb43..7849c5a 100644 --- a/plist-cil/ASCIIPropertyListParser.cs +++ b/plist-cil/ASCIIPropertyListParser.cs @@ -409,15 +409,15 @@ namespace Claunia.PropertyList quotedString[4] == DATE_DATE_FIELD_DELIMITER) try { - return new NSDate(quotedString); + return new NSDate(ValuePreprocessor.Preprocess(quotedString, ValuePreprocessor.Types.DATE)); } catch(Exception) { //not a date? --> return string - return new NSString(quotedString); + return new NSString(ValuePreprocessor.Preprocess(quotedString, ValuePreprocessor.Types.STRING)); } - return new NSString(quotedString); + return new NSString(ValuePreprocessor.Preprocess(quotedString, ValuePreprocessor.Types.STRING)); } default: { @@ -429,7 +429,7 @@ namespace Claunia.PropertyList //non-numerical -> string or boolean string parsedString = ParseString(); - return new NSString(parsedString); + return new NSString(ValuePreprocessor.Preprocess(parsedString, ValuePreprocessor.Types.STRING)); } } } @@ -529,9 +529,9 @@ namespace Claunia.PropertyList Expect(DATA_GSBOOL_TRUE_TOKEN, DATA_GSBOOL_FALSE_TOKEN); if(Accept(DATA_GSBOOL_TRUE_TOKEN)) - obj = new NSNumber(true); + obj = new NSNumber(ValuePreprocessor.Preprocess(true, ValuePreprocessor.Types.BOOL)); else - obj = new NSNumber(false); + obj = new NSNumber(ValuePreprocessor.Preprocess(false, ValuePreprocessor.Types.BOOL)); //Skip the parsed boolean token Skip(); @@ -541,14 +541,14 @@ namespace Claunia.PropertyList //Date Skip(); string dateString = ReadInputUntil(DATA_END_TOKEN); - obj = new NSDate(dateString); + obj = new NSDate(ValuePreprocessor.Preprocess(dateString, ValuePreprocessor.Types.DATE)); } else if(Accept(DATA_GSINT_BEGIN_TOKEN, DATA_GSREAL_BEGIN_TOKEN)) { //Number Skip(); string numberString = ReadInputUntil(DATA_END_TOKEN); - obj = new NSNumber(numberString); + obj = new NSNumber(ValuePreprocessor.Preprocess(numberString, ValuePreprocessor.Types.UNDEFINED_NUMBER)); } //parse data end token @@ -569,7 +569,7 @@ namespace Claunia.PropertyList bytes[i] = (byte)byteValue; } - obj = new NSData(bytes); + obj = new NSData(ValuePreprocessor.Preprocess(bytes, ValuePreprocessor.Types.DATA)); //skip end token Skip(); @@ -586,18 +586,18 @@ namespace Claunia.PropertyList if(numericalString.Length <= 4 || numericalString[4] != DATE_DATE_FIELD_DELIMITER) - return new NSString(numericalString); + return new NSString(ValuePreprocessor.Preprocess(numericalString, ValuePreprocessor.Types.STRING)); try { - return new NSDate(numericalString); + return new NSDate(ValuePreprocessor.Preprocess(numericalString, ValuePreprocessor.Types.DATE)); } catch(Exception) { //An exception occurs if the string is not a date but just a string } - return new NSString(numericalString); + return new NSString(ValuePreprocessor.Preprocess(numericalString, ValuePreprocessor.Types.STRING)); } /// diff --git a/plist-cil/BinaryPropertyListParser.cs b/plist-cil/BinaryPropertyListParser.cs index 95a0e41..c94fbe9 100644 --- a/plist-cil/BinaryPropertyListParser.cs +++ b/plist-cil/BinaryPropertyListParser.cs @@ -204,12 +204,12 @@ namespace Claunia.PropertyList case 0x8: { //false - return new NSNumber(false); + return new NSNumber(ValuePreprocessor.Preprocess(false, ValuePreprocessor.Types.BOOL)); } case 0x9: { //true - return new NSNumber(true); + return new NSNumber(ValuePreprocessor.Preprocess(true, ValuePreprocessor.Types.BOOL)); } case 0xC: { @@ -267,7 +267,7 @@ namespace Claunia.PropertyList //Data ReadLengthAndOffset(bytes, objInfo, offset, out int length, out int dataoffset); - return new NSData(CopyOfRange(bytes, offset + dataoffset, offset + dataoffset + length)); + return new NSData(ValuePreprocessor.Preprocess(CopyOfRange(bytes, offset + dataoffset, offset + dataoffset + length), ValuePreprocessor.Types.DATA)); } case 0x5: { diff --git a/plist-cil/NSDate.cs b/plist-cil/NSDate.cs index d424925..68d706f 100644 --- a/plist-cil/NSDate.cs +++ b/plist-cil/NSDate.cs @@ -52,7 +52,7 @@ namespace Claunia.PropertyList public NSDate(ReadOnlySpan bytes) => //dates are 8 byte big-endian double, seconds since the epoch - Date = EPOCH.AddSeconds(BinaryPropertyListParser.ParseDouble(bytes)); + Date = EPOCH.AddSeconds(ValuePreprocessor.Preprocess(BinaryPropertyListParser.ParseDouble(bytes), ValuePreprocessor.Types.DATE)); /// /// Parses a date from its textual representation. That representation has the following pattern: diff --git a/plist-cil/NSNumber.cs b/plist-cil/NSNumber.cs index 380530a..7729fbb 100644 --- a/plist-cil/NSNumber.cs +++ b/plist-cil/NSNumber.cs @@ -69,13 +69,13 @@ namespace Claunia.PropertyList switch(type) { case INTEGER: - doubleValue = longValue = BinaryPropertyListParser.ParseLong(bytes); + doubleValue = longValue = ValuePreprocessor.Preprocess(BinaryPropertyListParser.ParseLong(bytes), ValuePreprocessor.Types.INTEGER); break; case REAL: - doubleValue = BinaryPropertyListParser.ParseDouble(bytes); - longValue = (long)Math.Round(doubleValue); + doubleValue = ValuePreprocessor.Preprocess(BinaryPropertyListParser.ParseDouble(bytes), ValuePreprocessor.Types.FLOATING_POINT); + longValue = ValuePreprocessor.Preprocess((long)Math.Round(doubleValue), ValuePreprocessor.Types.INTEGER); break; diff --git a/plist-cil/NSString.cs b/plist-cil/NSString.cs index cfad91a..6196bd1 100644 --- a/plist-cil/NSString.cs +++ b/plist-cil/NSString.cs @@ -48,9 +48,9 @@ namespace Claunia.PropertyList public NSString(ReadOnlySpan bytes, Encoding encoding) { #if NATIVE_SPAN - Content = encoding.GetString(bytes); + Content = ValuePreprocessor.Preprocess(encoding.GetString(bytes), ValuePreprocessor.Types.STRING); #else - Content = encoding.GetString(bytes.ToArray()); + Content = ValuePreprocessor.Preprocess(encoding.GetString(bytes.ToArray()), ValuePreprocessor.Types.STRING); #endif } diff --git a/plist-cil/PropertyListParser.cs b/plist-cil/PropertyListParser.cs index d5105fc..5c102e1 100644 --- a/plist-cil/PropertyListParser.cs +++ b/plist-cil/PropertyListParser.cs @@ -1,4 +1,4 @@ -// plist-cil - An open source library to parse and generate property lists for .NET +// plist-cil - An open source library to parse and generate property lists for .NET // Copyright (C) 2015 Natalia Portillo // // This code is based on: @@ -147,6 +147,12 @@ namespace Claunia.PropertyList return type; } + /// Register preprocessing functions for for plist values. + /// A function that preprocesses the passed string and returns the adjusted value. + /// The type of value preprocessor to use. + public static void RegisterValuePreprocessor(Func preprocessor, ValuePreprocessor.Types type) => + ValuePreprocessor.Register(preprocessor, type); + /// Reads all bytes from an Stream and stores them in an array, up to a maximum count. /// The Stream pointing to the data that should be stored in the array. internal static byte[] ReadAll(Stream fs) diff --git a/plist-cil/XmlPropertyListParser.cs b/plist-cil/XmlPropertyListParser.cs index e1b2f32..203d0b3 100644 --- a/plist-cil/XmlPropertyListParser.cs +++ b/plist-cil/XmlPropertyListParser.cs @@ -23,6 +23,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -173,13 +174,13 @@ namespace Claunia.PropertyList return array; } - case "true": return new NSNumber(true); - case "false": return new NSNumber(false); - case "integer": return new NSNumber(GetNodeTextContents(n), NSNumber.INTEGER); - case "real": return new NSNumber(GetNodeTextContents(n), NSNumber.REAL); - case "string": return new NSString(GetNodeTextContents(n)); - case "data": return new NSData(GetNodeTextContents(n)); - default: return n.Name.Equals("date") ? new NSDate(GetNodeTextContents(n)) : null; + case "true": return new NSNumber(ValuePreprocessor.Preprocess(true, ValuePreprocessor.Types.BOOL)); + case "false": return new NSNumber(ValuePreprocessor.Preprocess(false, ValuePreprocessor.Types.BOOL)); + case "integer": return new NSNumber(ValuePreprocessor.Preprocess(GetNodeTextContents(n), ValuePreprocessor.Types.INTEGER)); + case "real": return new NSNumber(ValuePreprocessor.Preprocess(GetNodeTextContents(n), ValuePreprocessor.Types.FLOATING_POINT)); + case "string": return new NSString(ValuePreprocessor.Preprocess(GetNodeTextContents(n), ValuePreprocessor.Types.STRING)); + case "data": return new NSData(ValuePreprocessor.Preprocess(GetNodeTextContents(n), ValuePreprocessor.Types.DATA)); + default: return n.Name.Equals("date") ? new NSDate(ValuePreprocessor.Preprocess(GetNodeTextContents(n), ValuePreprocessor.Types.DATE)) : null; } }