From 5951d041351afe9c2e04ad6db4558bd621200171 Mon Sep 17 00:00:00 2001 From: Cezary Rojewski Date: Tue, 2 May 2023 16:30:15 +0200 Subject: [PATCH 1/7] ucm: Remove unused constructors Default constructors for abstract classes are not referenced anywhere. Signed-off-by: Cezary Rojewski --- NUcmSerializer/src/Components.cs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/NUcmSerializer/src/Components.cs b/NUcmSerializer/src/Components.cs index 4d6e85c..e775cb5 100644 --- a/NUcmSerializer/src/Components.cs +++ b/NUcmSerializer/src/Components.cs @@ -23,11 +23,6 @@ public Section(string identifier) { Identifier = identifier; } - - public Section() - : this(null) - { - } } public static class TPLG_CTL @@ -267,11 +262,6 @@ protected VendorTuples(string identifier) { } - protected VendorTuples() - : this(null) - { - } - public abstract int Size(); } @@ -418,11 +408,6 @@ public SectionControl(string identifier) : base(identifier) { } - - public SectionControl() - : this(null) - { - } } public class SectionControlMixer : SectionControl From 4fec59a60c83c29715bfb0295fcf1759b5f2526e Mon Sep 17 00:00:00 2001 From: Cezary Rojewski Date: Tue, 2 May 2023 16:44:06 +0200 Subject: [PATCH 2/7] ucm: Rewrite setters for FormatsString and RatesString First, handle case where value provided is null to avoid ArgumentNullException. Second, be more thorough when splitting string-representation of constants into separate entries. That is, trim and remove empty entries. Last, while '_' is part of PCM_RATE constants names, do not allow for ambiguity and treat rate-strings such as "_192000" as invalid. Signed-off-by: Cezary Rojewski --- NUcmSerializer/src/Components.cs | 37 ++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/NUcmSerializer/src/Components.cs b/NUcmSerializer/src/Components.cs index e775cb5..758b020 100644 --- a/NUcmSerializer/src/Components.cs +++ b/NUcmSerializer/src/Components.cs @@ -689,11 +689,17 @@ public string FormatsString set { - string[] substrs = value.Split(new[] { ',' }); - Formats.Clear(); - foreach (var s in substrs) - Formats.Add((PCM_FORMAT)Enum.Parse(typeof(PCM_FORMAT), s)); + if (value == null) + return; + + string[] substrs = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) + .Select(s => s.Trim()) + .Distinct().ToArray(); + + foreach (string s in substrs) + if (Enum.GetNames().Contains(s)) + Formats.Add(Enum.Parse(s)); } } @@ -708,14 +714,27 @@ public string RatesString set { - string[] substrs = value.Split(new[] { ',' }); - Rates.Clear(); - foreach (var s in substrs) + if (value == null) + return; + + string[] substrs = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) + .Select(s => s.Trim()) + .Distinct().ToArray(); + + foreach (string s in substrs) { + string tmp; + + if (s[0] == '_') + continue; + if (char.IsDigit(s[0])) - s.Insert(0, "_"); - Rates.Add((PCM_RATE)Enum.Parse(typeof(PCM_RATE), s)); + tmp = s.Insert(0, "_"); + else + tmp = s; + if (Enum.GetNames().Contains(tmp)) + Rates.Add(Enum.Parse(tmp)); } } } From 4dc289997d1759f0dbebc96fee358f962b3ce636 Mon Sep 17 00:00:00 2001 From: Cezary Rojewski Date: Tue, 2 May 2023 17:23:14 +0200 Subject: [PATCH 3/7] ucm: Simplify conditional statements in GetTypeTokenType() No need for else-if-statements if the preceding if-statement ends with return. Signed-off-by: Cezary Rojewski --- NUcmSerializer/src/TypeHelper.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/NUcmSerializer/src/TypeHelper.cs b/NUcmSerializer/src/TypeHelper.cs index 7f80164..cfc9b14 100644 --- a/NUcmSerializer/src/TypeHelper.cs +++ b/NUcmSerializer/src/TypeHelper.cs @@ -50,13 +50,13 @@ internal static TokenType GetTypeTokenType(this Type type) { if (type.IsSimpleType()) return TokenType.Element; - else if (type.IsSimpleArrayType()) + if (type.IsSimpleArrayType()) return TokenType.Array; - else if (type.IsSimpleTupleType()) + if (type.IsSimpleTupleType()) return TokenType.Tuple; - else if (type.IsVendorArrayType()) + if (type.IsVendorArrayType()) return TokenType.VendorArray; - else if (type.IsSubclassOf(typeof(Section))) + if (type.IsSubclassOf(typeof(Section))) return TokenType.Section; return TokenType.None; From d81e24fc04d92aab378e06002f5b6561266cca06 Mon Sep 17 00:00:00 2001 From: Cezary Rojewski Date: Tue, 2 May 2023 17:25:46 +0200 Subject: [PATCH 4/7] ucm: Treat Section-type as TokenType.Section While abstract classes cannot be instantiated, for consistency streamline TokenType value returned for the base Section-type. Signed-off-by: Cezary Rojewski --- NUcmSerializer/src/TypeHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NUcmSerializer/src/TypeHelper.cs b/NUcmSerializer/src/TypeHelper.cs index cfc9b14..3c6c47d 100644 --- a/NUcmSerializer/src/TypeHelper.cs +++ b/NUcmSerializer/src/TypeHelper.cs @@ -56,7 +56,7 @@ internal static TokenType GetTypeTokenType(this Type type) return TokenType.Tuple; if (type.IsVendorArrayType()) return TokenType.VendorArray; - if (type.IsSubclassOf(typeof(Section))) + if (type.IsSubclassOf(typeof(Section)) || type == typeof(Section)) return TokenType.Section; return TokenType.None; From 981d62eea05b27f8d88eb6c3ba62880677d22c49 Mon Sep 17 00:00:00 2001 From: Cezary Rojewski Date: Tue, 2 May 2023 17:30:34 +0200 Subject: [PATCH 5/7] ucm: Simplify ConvertFromString() extension method TypeDescriptor::GetConverter() either returns non-null value or throws an exception so remove the existing null-check. Signed-off-by: Cezary Rojewski --- NUcmSerializer/src/TypeHelper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/NUcmSerializer/src/TypeHelper.cs b/NUcmSerializer/src/TypeHelper.cs index 3c6c47d..addc5f2 100644 --- a/NUcmSerializer/src/TypeHelper.cs +++ b/NUcmSerializer/src/TypeHelper.cs @@ -98,8 +98,7 @@ internal static object ConvertFromString(this Type type, string value) } TypeConverter conv = TypeDescriptor.GetConverter(type); - if (conv != null) - return conv.ConvertFromString(value); + return conv.ConvertFromString(value); } catch (NotSupportedException) { From 127affdca314d5873f9fbf9f94ae50544ec7cee6 Mon Sep 17 00:00:00 2001 From: Cezary Rojewski Date: Tue, 2 May 2023 17:33:32 +0200 Subject: [PATCH 6/7] ucm: Fix indentation Improves readability. Signed-off-by: Cezary Rojewski --- NUcmSerializer/src/TypeHelper.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/NUcmSerializer/src/TypeHelper.cs b/NUcmSerializer/src/TypeHelper.cs index addc5f2..d1f4a7a 100644 --- a/NUcmSerializer/src/TypeHelper.cs +++ b/NUcmSerializer/src/TypeHelper.cs @@ -120,7 +120,7 @@ internal static byte[] ToBytes(this string value) { if (substr.StartsWith("0x", StringComparison.CurrentCulture) && byte.TryParse(substr.Substring(2), NumberStyles.HexNumber, - CultureInfo.CurrentCulture, out byte val)) + CultureInfo.CurrentCulture, out byte val)) result.Add(val); else if (byte.TryParse(substr, out val)) result.Add(val); @@ -139,7 +139,7 @@ internal static ushort[] ToUInts16(this string value) { if (substr.StartsWith("0x", StringComparison.CurrentCulture) && ushort.TryParse(substr.Substring(2), NumberStyles.HexNumber, - CultureInfo.CurrentCulture, out ushort val)) + CultureInfo.CurrentCulture, out ushort val)) result.Add(val); else if (ushort.TryParse(substr, out val)) result.Add(val); @@ -158,7 +158,7 @@ internal static uint[] ToUInts32(this string value) { if (substr.StartsWith("0x", StringComparison.CurrentCulture) && uint.TryParse(substr.Substring(2), NumberStyles.HexNumber, - CultureInfo.CurrentCulture, out uint val)) + CultureInfo.CurrentCulture, out uint val)) result.Add(val); else if (uint.TryParse(substr, out val)) result.Add(val); From 6a1938d7eceb236d6548d0caa99eec5680d56517 Mon Sep 17 00:00:00 2001 From: Cezary Rojewski Date: Tue, 2 May 2023 17:36:59 +0200 Subject: [PATCH 7/7] ucm: Check stream-arguments before operating on them Throw null-exception when invalid stream is specified. Signed-off-by: Cezary Rojewski --- NUcmSerializer/src/UcmReader.cs | 3 +++ NUcmSerializer/src/UcmSerializer.cs | 5 +++++ NUcmSerializer/src/UcmWriter.cs | 3 +++ 3 files changed, 11 insertions(+) diff --git a/NUcmSerializer/src/UcmReader.cs b/NUcmSerializer/src/UcmReader.cs index 7325c50..bc0cb6c 100644 --- a/NUcmSerializer/src/UcmReader.cs +++ b/NUcmSerializer/src/UcmReader.cs @@ -36,6 +36,9 @@ public class UcmReader : IDisposable public UcmReader(Stream stream, Encoding encoding) { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + this.encoding = encoding; if (encoding == null) reader = new StreamReader(stream); diff --git a/NUcmSerializer/src/UcmSerializer.cs b/NUcmSerializer/src/UcmSerializer.cs index 5a88117..5c29675 100644 --- a/NUcmSerializer/src/UcmSerializer.cs +++ b/NUcmSerializer/src/UcmSerializer.cs @@ -22,6 +22,8 @@ public UcmSerializer() public void Serialize(Stream stream, IEnumerable
sections, Encoding encoding) { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); if (sections == null) throw new ArgumentNullException("sections"); @@ -45,6 +47,9 @@ public void Serialize(Stream stream, IEnumerable
topology) public IEnumerable
Deserialize(Stream stream, Encoding encoding) { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + List
result = new List
(); Section section; diff --git a/NUcmSerializer/src/UcmWriter.cs b/NUcmSerializer/src/UcmWriter.cs index 9a20a08..541c819 100644 --- a/NUcmSerializer/src/UcmWriter.cs +++ b/NUcmSerializer/src/UcmWriter.cs @@ -89,6 +89,9 @@ public void Init(object token, PropertyInfo tokenInfo) public UcmWriter(Stream stream, Encoding encoding) { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + this.encoding = encoding; if (encoding == null) writer = new StreamWriter(stream);