Files
sharpcompress/src/SharpCompress/Common/ArchiveEncoding.cs

73 lines
2.0 KiB
C#
Raw Normal View History

using System;
using System.Text;
2015-12-30 11:19:42 +00:00
namespace SharpCompress.Common
{
public class ArchiveEncoding
2015-12-30 11:19:42 +00:00
{
/// <summary>
/// Default encoding to use when archive format doesn't specify one.
/// </summary>
public Encoding Default { get; set; }
2015-12-30 11:19:42 +00:00
/// <summary>
/// ArchiveEncoding used by encryption schemes which don't comply with RFC 2898.
2015-12-30 11:19:42 +00:00
/// </summary>
public Encoding Password { get; set; }
2015-12-30 11:19:42 +00:00
/// <summary>
/// Set this encoding when you want to force it for all encoding operations.
/// </summary>
2020-05-23 16:27:55 -07:00
public Encoding? Forced { get; set; }
/// <summary>
/// Set this when you want to use a custom method for all decoding operations.
/// </summary>
/// <returns>string Func(bytes, index, length)</returns>
2020-05-23 16:27:55 -07:00
public Func<byte[], int, int, string>? CustomDecoder { get; set; }
public ArchiveEncoding()
2015-12-30 11:19:42 +00:00
{
Default = Encoding.GetEncoding(437);
Password = Encoding.GetEncoding(437);
2015-12-30 11:19:42 +00:00
}
2020-05-14 13:16:00 +01:00
#if NETSTANDARD1_3 || NETSTANDARD2_0 || NETSTANDARD2_1
2018-04-29 16:27:26 +01:00
static ArchiveEncoding()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
2020-05-14 13:16:00 +01:00
#endif
2018-04-29 16:27:26 +01:00
public string Decode(byte[] bytes)
{
return Decode(bytes, 0, bytes.Length);
}
public string Decode(byte[] bytes, int start, int length)
2018-04-29 16:27:26 +01:00
{
return GetDecoder().Invoke(bytes, start, length);
2018-04-29 16:27:26 +01:00
}
public string DecodeUTF8(byte[] bytes)
{
return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}
public byte[] Encode(string str)
{
return GetEncoding().GetBytes(str);
}
public Encoding GetEncoding()
{
return Forced ?? Default ?? Encoding.UTF8;
}
public Func<byte[], int, int, string> GetDecoder()
{
return CustomDecoder ?? ((bytes, index, count) => GetEncoding().GetString(bytes, index, count));
}
2015-12-30 11:19:42 +00:00
}
}