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>
|
|
|
|
|
|
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>
|
|
|
|
|
|
public Func<byte[], int, int, string> CustomDecoder { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public ArchiveEncoding()
|
2015-12-30 11:19:42 +00:00
|
|
|
|
{
|
|
|
|
|
|
Default = Encoding.UTF8;
|
|
|
|
|
|
Password = Encoding.UTF8;
|
|
|
|
|
|
}
|
2017-07-17 10:53:20 -04:00
|
|
|
|
|
|
|
|
|
|
public string Decode(byte[] bytes)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Decode(bytes, 0, bytes.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string Decode(byte[] bytes, int start, int length)
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetDecoder().Invoke(bytes, start, 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) => (Default ?? Encoding.UTF8).GetString(bytes, index, count));
|
|
|
|
|
|
}
|
2015-12-30 11:19:42 +00:00
|
|
|
|
}
|
2013-04-28 12:32:55 +01:00
|
|
|
|
}
|