From f52373609eac871c2883e1052020ff5327b19707 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 3 Nov 2016 18:34:16 -0400 Subject: move classes to portable project --- .../Cryptography/CryptographyProvider.cs | 8 ++++ .../Reflection/AssemblyInfo.cs | 18 ++++++++ .../TextEncoding/TextEncoding.cs | 48 ++++++++++++++++++++++ .../Xml/XmlReaderSettingsFactory.cs | 22 ++++++++++ 4 files changed, 96 insertions(+) create mode 100644 Emby.Common.Implementations/Reflection/AssemblyInfo.cs create mode 100644 Emby.Common.Implementations/TextEncoding/TextEncoding.cs create mode 100644 Emby.Common.Implementations/Xml/XmlReaderSettingsFactory.cs (limited to 'Emby.Common.Implementations') diff --git a/Emby.Common.Implementations/Cryptography/CryptographyProvider.cs b/Emby.Common.Implementations/Cryptography/CryptographyProvider.cs index e5e4b1c7c1..7b8d95b96c 100644 --- a/Emby.Common.Implementations/Cryptography/CryptographyProvider.cs +++ b/Emby.Common.Implementations/Cryptography/CryptographyProvider.cs @@ -35,5 +35,13 @@ namespace Emby.Common.Implementations.Cryptography return provider.ComputeHash(str); } } + + public byte[] GetMD5Bytes(byte[] bytes) + { + using (var provider = MD5.Create()) + { + return provider.ComputeHash(bytes); + } + } } } diff --git a/Emby.Common.Implementations/Reflection/AssemblyInfo.cs b/Emby.Common.Implementations/Reflection/AssemblyInfo.cs new file mode 100644 index 0000000000..820856da5f --- /dev/null +++ b/Emby.Common.Implementations/Reflection/AssemblyInfo.cs @@ -0,0 +1,18 @@ +using System; +using System.IO; +using MediaBrowser.Model.Reflection; +using System.Reflection; + +namespace Emby.Common.Implementations.Reflection +{ + public class AssemblyInfo : IAssemblyInfo + { + public Stream GetManifestResourceStream(Type type, string resource) + { +#if NET46 + return type.Assembly.GetManifestResourceStream(resource); +#endif + return type.GetTypeInfo().Assembly.GetManifestResourceStream(resource); + } + } +} diff --git a/Emby.Common.Implementations/TextEncoding/TextEncoding.cs b/Emby.Common.Implementations/TextEncoding/TextEncoding.cs new file mode 100644 index 0000000000..35b869e43b --- /dev/null +++ b/Emby.Common.Implementations/TextEncoding/TextEncoding.cs @@ -0,0 +1,48 @@ +using System.Text; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.TextEncoding; + +namespace Emby.Common.Implementations.TextEncoding +{ + public class TextEncoding : IEncoding + { + private readonly IFileSystem _fileSystem; + + public TextEncoding(IFileSystem fileSystem) + { + _fileSystem = fileSystem; + } + + public byte[] GetASCIIBytes(string text) + { + return Encoding.ASCII.GetBytes(text); + } + + public string GetASCIIString(byte[] bytes, int startIndex, int length) + { + return Encoding.ASCII.GetString(bytes, 0, bytes.Length); + } + + public Encoding GetFileEncoding(string srcFile) + { + // *** Detect byte order mark if any - otherwise assume default + var buffer = new byte[5]; + + using (var file = _fileSystem.OpenRead(srcFile)) + { + file.Read(buffer, 0, 5); + } + + if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf) + return Encoding.UTF8; + if (buffer[0] == 0xfe && buffer[1] == 0xff) + return Encoding.Unicode; + if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff) + return Encoding.UTF32; + if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76) + return Encoding.UTF7; + + return null; + } + } +} diff --git a/Emby.Common.Implementations/Xml/XmlReaderSettingsFactory.cs b/Emby.Common.Implementations/Xml/XmlReaderSettingsFactory.cs new file mode 100644 index 0000000000..806290cf44 --- /dev/null +++ b/Emby.Common.Implementations/Xml/XmlReaderSettingsFactory.cs @@ -0,0 +1,22 @@ +using System.Xml; +using MediaBrowser.Model.Xml; + +namespace Emby.Common.Implementations.Xml +{ + public class XmlReaderSettingsFactory : IXmlReaderSettingsFactory + { + public XmlReaderSettings Create(bool enableValidation) + { + var settings = new XmlReaderSettings(); + + if (!enableValidation) + { +#if NET46 + settings.ValidationType = ValidationType.None; +#endif + } + + return settings; + } + } +} -- cgit v1.2.3