PpmdProperties.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using SharpCompress.Compressors.PPMd.I1;
  2. using SharpCompress.Converters;
  3. namespace SharpCompress.Compressors.PPMd
  4. {
  5. public class PpmdProperties
  6. {
  7. private int _allocatorSize;
  8. internal Allocator _allocator;
  9. public PpmdProperties()
  10. : this(16 << 20, 6)
  11. {
  12. }
  13. public PpmdProperties(int allocatorSize, int modelOrder)
  14. : this(allocatorSize, modelOrder, ModelRestorationMethod.Restart)
  15. {
  16. }
  17. internal PpmdProperties(int allocatorSize, int modelOrder, ModelRestorationMethod modelRestorationMethod)
  18. {
  19. AllocatorSize = allocatorSize;
  20. ModelOrder = modelOrder;
  21. RestorationMethod = modelRestorationMethod;
  22. }
  23. public int ModelOrder { get; }
  24. public PpmdVersion Version { get; } = PpmdVersion.I1;
  25. internal ModelRestorationMethod RestorationMethod { get; }
  26. public PpmdProperties(byte[] properties)
  27. {
  28. if (properties.Length == 2)
  29. {
  30. ushort props = DataConverter.LittleEndian.GetUInt16(properties, 0);
  31. AllocatorSize = (((props >> 4) & 0xff) + 1) << 20;
  32. ModelOrder = (props & 0x0f) + 1;
  33. RestorationMethod = (ModelRestorationMethod)(props >> 12);
  34. }
  35. else if (properties.Length == 5)
  36. {
  37. Version = PpmdVersion.H7Z;
  38. AllocatorSize = DataConverter.LittleEndian.GetInt32(properties, 1);
  39. ModelOrder = properties[0];
  40. }
  41. }
  42. public int AllocatorSize
  43. {
  44. get => _allocatorSize;
  45. set
  46. {
  47. _allocatorSize = value;
  48. if (Version == PpmdVersion.I1)
  49. {
  50. if (_allocator == null)
  51. {
  52. _allocator = new Allocator();
  53. }
  54. _allocator.Start(_allocatorSize);
  55. }
  56. }
  57. }
  58. public byte[] Properties => DataConverter.LittleEndian.GetBytes(
  59. (ushort)
  60. ((ModelOrder - 1) + (((AllocatorSize >> 20) - 1) << 4) + ((ushort)RestorationMethod << 12)));
  61. }
  62. }