2017-07-17 10:53:20 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Text;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
|
|
|
|
|
namespace SharpCompress.Common
|
|
|
|
|
|
{
|
2017-07-17 10:53:20 -04:00
|
|
|
|
public class ArchiveEncoding
|
2015-12-30 11:19:42 +00:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Default encoding to use when archive format doesn't specify one.
|
|
|
|
|
|
/// </summary>
|
2017-07-17 10:53:20 -04:00
|
|
|
|
public Encoding Default { get; set; }
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-07-17 10:53:20 -04:00
|
|
|
|
/// ArchiveEncoding used by encryption schemes which don't comply with RFC 2898.
|
2015-12-30 11:19:42 +00:00
|
|
|
|
/// </summary>
|
2017-07-17 10:53:20 -04:00
|
|
|
|
public Encoding Password { get; set; }
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
2017-07-17 10:53:20 -04: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; }
|
2017-07-17 10:53:20 -04:00
|
|
|
|
|
|
|
|
|
|
/// <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; }
|
2017-07-17 10:53:20 -04:00
|
|
|
|
|
|
|
|
|
|
public ArchiveEncoding()
|
2015-12-30 11:19:42 +00:00
|
|
|
|
{
|
2018-05-30 21:47:31 +05:00
|
|
|
|
Default = Encoding.GetEncoding(437);
|
|
|
|
|
|
Password = Encoding.GetEncoding(437);
|
2015-12-30 11:19:42 +00:00
|
|
|
|
}
|
2017-07-17 10:53:20 -04: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
|
|
|
|
|
2017-07-17 10:53:20 -04:00
|
|
|
|
public string Decode(byte[] bytes)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Decode(bytes, 0, bytes.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-30 21:47:31 +05:00
|
|
|
|
public string Decode(byte[] bytes, int start, int length)
|
2018-04-29 16:27:26 +01:00
|
|
|
|
{
|
2018-05-30 21:47:31 +05:00
|
|
|
|
return GetDecoder().Invoke(bytes, start, length);
|
2018-04-29 16:27:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-30 21:47:31 +05:00
|
|
|
|
public string DecodeUTF8(byte[] bytes)
|
2017-07-17 10:53:20 -04:00
|
|
|
|
{
|
2018-05-30 21:47:31 +05:00
|
|
|
|
return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
|
2017-07-17 10:53:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] Encode(string str)
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetEncoding().GetBytes(str);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Encoding GetEncoding()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Forced ?? Default ?? Encoding.UTF8;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Func<byte[], int, int, string> GetDecoder()
|
|
|
|
|
|
{
|
2018-05-14 16:20:57 +01:00
|
|
|
|
return CustomDecoder ?? ((bytes, index, count) => GetEncoding().GetString(bytes, index, count));
|
2017-07-17 10:53:20 -04:00
|
|
|
|
}
|
2015-12-30 11:19:42 +00:00
|
|
|
|
}
|
2019-12-30 18:58:25 +01:00
|
|
|
|
}
|