/* This file is part of libmspack.
* (C) 2003-2018 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.CAB
{
internal class _FileHeader
{
#region Fields
///
/// The uncompressed length of the file, in bytes.
///
/// 0x00
public uint UncompressedSize { get; private set; }
///
/// The uncompressed offset of this file in its folder.
///
/// 0x04
public uint FolderOffset { get; private set; }
///
/// Internal index of the folder
///
/// 0x08
public FileFlags FolderIndex { get; internal set; }
///
/// File's last modified date, day field.
///
/// 0x0A
public byte LastModifiedDateDay { get; set; }
///
/// File's last modified date, month field.
///
/// 0x0A
public byte LastModifiedDateMonth { get; set; }
///
/// File's last modified date, year field.
///
/// 0x0A
public int LastModifiedDateYear { get; set; }
///
/// File's last modified time, hour field.
///
/// 0x0C
public byte LastModifiedTimeHour { get; set; }
///
/// File's last modified time, minute field.
///
/// 0x0C
public byte LastModifiedTimeMinute { get; set; }
///
/// File's last modified time, second field.
///
/// 0x0C
public byte LastModifiedTimeSecond { get; set; }
///
/// File attributes.
///
/// 0x0E
public FileAttributes Attributes { get; set; }
///
/// Size of the File header in bytes
///
public const int Size = 0x10;
#endregion
///
/// Private constructor
///
private _FileHeader() { }
///
/// Create a _FileHeader from a byte array, if possible
///
public static Error Create(byte[] buffer, out _FileHeader header)
{
header = null;
if (buffer == null || buffer.Length < Size)
return Error.MSPACK_ERR_READ;
header = new _FileHeader();
header.UncompressedSize = BitConverter.ToUInt32(buffer, 0x00);
header.FolderOffset = BitConverter.ToUInt32(buffer, 0x04);
header.FolderIndex = (FileFlags)BitConverter.ToUInt16(buffer, 0x08);
// Get date
ushort x = BitConverter.ToUInt16(buffer, 0x0A);
header.LastModifiedDateDay = (byte)(x & 0x1F);
header.LastModifiedDateMonth = (byte)((x >> 5) & 0xF);
header.LastModifiedDateYear = (x >> 9) + 1980;
// Get time
x = BitConverter.ToUInt16(buffer, 0x0C);
header.LastModifiedTimeHour = (byte)(x >> 11);
header.LastModifiedTimeMinute = (byte)((x >> 5) & 0x3F);
header.LastModifiedTimeSecond = (byte)((x << 1) & 0x3E);
header.Attributes = (FileAttributes)BitConverter.ToUInt16(buffer, 0x0E);
return Error.MSPACK_ERR_OK;
}
}
}