/*
* Process Hacker
*
* Copyright (C) 2008 wj32
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace ProcessHacker.Structs
{
public class StructDef
{
private List _fields = new List();
private Dictionary _fieldsByName = new Dictionary();
public IStructIOProvider IOProvider { get; set; }
public int Size
{
get
{
int size = 0;
foreach (StructField field in _fields)
size += field.Size;
return size;
}
}
public int Offset { get; set; }
public Dictionary Structs { get; set; }
public StructField AddField(StructField field)
{
_fieldsByName.Add(field.Name, field);
_fields.Add(field);
return field;
}
public bool ContainsField(string name)
{
return _fieldsByName.ContainsKey(name);
}
public StructField GetField(int index)
{
return _fields[index];
}
public StructField GetField(string name)
{
return _fieldsByName[name];
}
public void RemoveField(int index)
{
_fieldsByName.Remove(_fields[index].Name);
_fields.RemoveAt(index);
}
public void RemoveField(string name)
{
_fields.Remove(_fieldsByName[name]);
_fieldsByName.Remove(name);
}
public void RemoveField(StructField field)
{
_fieldsByName.Remove(field.Name);
_fields.Remove(field);
}
public FieldValue[] Read()
{
FieldValue[] values;
this.Read(out values);
return values;
}
private int Read(StructField field, int offset, out FieldValue valueOut)
{
if (!field.IsArray)
return this.ReadOnce(field, offset, out valueOut);
// read array
FieldValue value = new FieldValue() { FieldType = field.RawType, Name = field.Name };
int readSize = 0;
List