/* 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;
namespace LibMSPackSharp.CHM
{
internal class _HeaderSectionTable
{
#region Regular Table
///
/// Header section 0 offset
///
/// 0x0000
public long OffsetHS0 { get; private set; }
///
/// Header section 0 length
///
/// 0x0008
public long LengthHS0 { get; private set; }
///
/// Header section 1 offset
/// The file offset of the first PMGL/PMGI directory chunk.
///
///
/// 0x0010
///
/// This is internally settable because it has to be corrected in some cases
///
public long OffsetHS1 { get; internal set; }
///
/// Header section 1 length
///
/// 0x0018
public long LengthHS1 { get; private set; }
///
/// Total size of the version 1 and 2 header section table in bytes
///
public const int Size = 0x0020;
#endregion
#region Version 3 Table
///
/// Header section 0 offset correct
///
/// 0x0020
public long OffsetCS0 { get; private set; }
///
/// Total size of the version 3 header section table in bytes
///
public const int V3Size = 0x0028;
#endregion
///
/// Private constructor
///
private _HeaderSectionTable() { }
///
/// Create a _HeaderSectionTable from a byte array, if possible
///
public static Error Create(byte[] buffer, out _HeaderSectionTable headerSectionTable)
{
headerSectionTable = null;
if (buffer == null || buffer.Length < Size)
return Error.MSPACK_ERR_READ;
headerSectionTable = new _HeaderSectionTable();
headerSectionTable.OffsetHS0 = BitConverter.ToInt64(buffer, 0x0000);
headerSectionTable.LengthHS0 = BitConverter.ToInt64(buffer, 0x0008);
headerSectionTable.OffsetHS1 = BitConverter.ToInt64(buffer, 0x0010);
headerSectionTable.LengthHS1 = BitConverter.ToInt64(buffer, 0x0018);
if (buffer.Length >= V3Size)
{
headerSectionTable.OffsetCS0 = BitConverter.ToInt64(buffer, 0x0020);
}
// OffsetCS0 does not exist in version 1 or 2 CHM files.
// The offset will be corrected later, once HS1 is read.
// TODO: Are these supposed to be == 0?
if (headerSectionTable.OffsetHS0 != 0 || headerSectionTable.OffsetHS1 != 0 || headerSectionTable.OffsetCS0 != 0)
return Error.MSPACK_ERR_DATAFORMAT;
return Error.MSPACK_ERR_OK;
}
}
}