/* This file is part of libmspack.
* (C) 2003-2004 Stuart Caie.
*
* libmspack is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License (LGPL) version 2.1
*
* For further details, see the file COPYING.LIB distributed with libmspack
*/
using System;
using LibMSPackSharp.Compression;
namespace LibMSPackSharp.CHM
{
internal class _LZXControlData
{
#region Fields
///
/// Length of the control data
///
/// 0x0000
public uint Length { get; private set; }
///
/// "LZXC"
///
/// 0x0004
public uint Signature { get; private set; }
///
/// Control data version
///
/// 0x0008
public uint Version { get; private set; }
///
/// Reset interval
///
/// 0x000C
public uint ResetInterval { get; private set; }
///
/// Window size
///
/// 0x0010
public uint WindowSize { get; private set; }
///
/// Cache size
///
/// 0x0014
public uint CacheSize { get; private set; }
///
/// Cache size
///
/// 0x0018
public uint Unknown1 { get; private set; }
///
/// Total size of the LZX control data in bytes
///
public const int Size = 0x001C;
#endregion
///
/// Private constructor
///
private _LZXControlData() { }
///
/// Create a _LZXControlData from a byte array, if possible
///
public static Error Create(byte[] buffer, out _LZXControlData controlData)
{
controlData = null;
if (buffer == null || buffer.Length < Size)
return Error.MSPACK_ERR_READ;
controlData = new _LZXControlData();
controlData.Length = BitConverter.ToUInt32(buffer, 0x0000);
controlData.Signature = BitConverter.ToUInt32(buffer, 0x0004);
if (controlData.Signature != 0x43585A4C)
return Error.MSPACK_ERR_SIGNATURE;
controlData.Version = BitConverter.ToUInt32(buffer, 0x0008);
switch (controlData.Version)
{
case 1:
controlData.ResetInterval = BitConverter.ToUInt32(buffer, 0x000C);
controlData.WindowSize = BitConverter.ToUInt32(buffer, 0x0010);
break;
case 2:
controlData.ResetInterval = BitConverter.ToUInt32(buffer, 0x000C) * LZX.LZX_FRAME_SIZE;
controlData.WindowSize = BitConverter.ToUInt32(buffer, 0x0010) * LZX.LZX_FRAME_SIZE;
break;
default:
return Error.MSPACK_ERR_DATAFORMAT;
}
controlData.CacheSize = BitConverter.ToUInt32(buffer, 0x0014);
controlData.Unknown1 = BitConverter.ToUInt32(buffer, 0x0018);
return Error.MSPACK_ERR_OK;
}
///
/// Get the number of bits in the window based on the window size
///
/// Window
/// An error code or MSPACK_ERR_OK if all is good
public Error GetWindowBits(out int windowBits)
{
switch (WindowSize)
{
case 0x008000:
windowBits = 15;
break;
case 0x010000:
windowBits = 16;
break;
case 0x020000:
windowBits = 17;
break;
case 0x040000:
windowBits = 18;
break;
case 0x080000:
windowBits = 19;
break;
case 0x100000:
windowBits = 20;
break;
case 0x200000:
windowBits = 21;
break;
default:
windowBits = -1;
return Error.MSPACK_ERR_DATAFORMAT;
}
return Error.MSPACK_ERR_OK;
}
}
}