private static List <chunktype> ReadChunks(Tools.Field f)
        {
            List <chunktype>   res  = new List <chunktype>();
            List <Tools.Entry> list = (List <Tools.Entry>)f.data;

            foreach (Tools.Entry e in list)
            {
                chunktype c = new chunktype();
                c.link = e;
                foreach (Tools.Field f2 in e.fields)
                {
                    switch (f2.fieldname)
                    {
                    case "id":
                        c.id = (byte[])f2.data;
                        break;

                    case "sha1":
                        c.SHA1 = (byte[])f2.data;
                        break;

                    case "size":
                        c.size = (byte[])f2.data;
                        break;
                    }
                }
                res.Add(c);
            }
            return(res);
        }
        private static List <dbxtype> ReadDbx(Tools.Field f)
        {
            List <dbxtype>     res  = new List <dbxtype>();
            List <Tools.Entry> list = (List <Tools.Entry>)f.data;

            foreach (Tools.Entry e in list)
            {
                dbxtype dbx = new dbxtype();
                dbx.link = e;
                foreach (Tools.Field f2 in e.fields)
                {
                    switch (f2.fieldname)
                    {
                    case "name":
                        dbx.name = (string)f2.data;
                        break;

                    case "sha1":
                        dbx.SHA1 = (byte[])f2.data;
                        break;

                    case "size":
                        dbx.size = (byte[])f2.data;
                        break;

                    case "originalSize":
                        dbx.osize = (byte[])f2.data;
                        break;
                    }
                }
                res.Add(dbx);
            }
            return(res);
        }
        public static void WriteField(Stream s, Tools.Field f)
        {
            if (f.type == 0)
            {
                s.WriteByte(0);
                return;
            }
            s.WriteByte(f.type);
            Tools.WriteNullString(s, f.fieldname);
            Tools.BinaryWriter7Bit w = new Tools.BinaryWriter7Bit(s);
            switch (f.type)
            {
            case 0x01:
                List <Tools.Entry> list = (List <Tools.Entry>)f.data;
                MemoryStream       m    = new MemoryStream();
                foreach (Tools.Entry e in list)
                {
                    WriteEntry(m, e);
                }
                m.WriteByte(0);
                w.Write7BitEncodedInt((int)m.Length);
                s.Write(m.ToArray(), 0, (int)m.Length);
                break;

            case 0x07:
                w.Write7BitEncodedInt((int)((string)f.data).Length + 1);
                Tools.WriteNullString(s, (string)f.data);
                break;

            case 0x06:
                s.WriteByte(((bool)f.data) ? (byte)1 : (byte)0);
                break;

            case 0x08:
            case 0x09:
            case 0xf:
            case 0x10:
                s.Write((byte[])f.data, 0, (int)((byte[])f.data).Length);
                break;

            case 0x02:
            case 0x13:
                w.Write7BitEncodedInt((int)((byte[])f.data).Length);
                s.Write((byte[])f.data, 0, (int)((byte[])f.data).Length);
                break;
            }
            if (f.fields != null)
            {
                foreach (Field subfield in f.fields)
                {
                    WriteField(s, subfield);
                }
            }
        }
        private static List <restype> ReadRes(Tools.Field f)
        {
            List <restype>     res  = new List <restype>();
            List <Tools.Entry> list = (List <Tools.Entry>)f.data;

            foreach (Tools.Entry e in list)
            {
                restype r = new restype();
                r.link = e;
                foreach (Tools.Field f2 in e.fields)
                {
                    switch (f2.fieldname)
                    {
                    case "name":
                        r.name = (string)f2.data;
                        break;

                    case "sha1":
                        r.SHA1 = (byte[])f2.data;
                        break;

                    case "size":
                        r.size = (byte[])f2.data;
                        break;

                    case "originalSize":
                        r.osize = (byte[])f2.data;
                        break;

                    case "resType":
                        r.rtype = (byte[])f2.data;
                        break;
                    }
                }
                res.Add(r);
            }
            return(res);
        }
        public static TreeNode MakeField(TreeNode t, Tools.Field f)
        {
            byte[] buff;
            if (f.fields != null)
            {
                foreach (Tools.Field subfield in f.fields)
                {
                    t.Nodes.Add(MakeField(new TreeNode(subfield.fieldname), subfield));
                }
            }
            else
            {
                switch (f.type)
                {
                case 1:
                    List <Tools.Entry> list = (List <Tools.Entry>)f.data;
                    foreach (Tools.Entry e in list)
                    {
                        t.Nodes.Add(MakeEntry(new TreeNode(e.type.ToString("X"))
                        {
                            Tag = f
                        }, e));
                    }
                    break;

                case 6:
                    bool b = (bool)f.data;
                    t.Nodes.Add(new TreeNode(b.ToString())
                    {
                        Tag = f
                    });
                    break;

                case 7:
                    string s = (string)f.data;
                    t.Nodes.Add(new TreeNode(s)
                    {
                        Tag = f
                    });
                    break;

                case 8:
                    buff = (byte[])f.data;
                    buff.Reverse();
                    if (t.Text == "resType")
                    {
                        string nodeTitle = string.Format("0x{0} {1}", BitConverter.ToUInt32(buff, 0).ToString("X"), GetResType(BitConverter.ToUInt32(buff, 0)));
                        t.Nodes.Add(new TreeNode(nodeTitle)
                        {
                            Tag = f
                        });
                    }
                    else
                    {
                        t.Nodes.Add(new TreeNode("0x" + BitConverter.ToUInt32(buff, 0).ToString("X"))
                        {
                            Tag = f
                        });
                    }
                    break;

                case 9:
                    buff = (byte[])f.data;
                    buff.Reverse();
                    t.Nodes.Add(new TreeNode("0x" + BitConverter.ToUInt64(buff, 0).ToString("X"))
                    {
                        Tag = f
                    });
                    break;

                case 0xf:
                    buff = (byte[])f.data;
                    buff.Reverse();
                    t.Nodes.Add(new TreeNode("0x" + BitConverter.ToUInt64(buff, 8).ToString("X"))
                    {
                        Tag = f
                    });
                    t.Nodes.Add(new TreeNode("0x" + BitConverter.ToUInt64(buff, 0).ToString("X"))
                    {
                        Tag = f
                    });
                    break;

                case 2:
                    buff = (byte[])f.data;
                    t.Nodes.Add(new TreeNode(System.Text.Encoding.UTF8.GetString(buff))
                    {
                        Tag = f
                    });
                    break;

                case 0x10:
                case 0x13:
                    // 'payload' fields are only found in the InitFS_Win32 file. Since they can be quite large, do not show them within a treeview.
                    if (f.fieldname != "payload")
                    {
                        buff = (byte[])f.data;
                        StringBuilder res = new StringBuilder();
                        foreach (byte bb in buff)
                        {
                            res.Append(bb.ToString("X2"));
                        }
                        t.Nodes.Add(new TreeNode(res.ToString())
                        {
                            Tag = f
                        });
                    }
                    else
                    {
                        t.Tag = f;
                    }
                    break;

                default:
                    break;
                }
            }
            return(t);
        }