using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Data;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;

namespace BulkTap3
{
    public class functions
    {

 
        [DllImport("Bulddll", CallingConvention = CallingConvention.StdCall)]
        //extern unsafe static int sum(int x, int y);
        //extern unsafe static byte[] readf(byte []block,  int size);
        public static extern IntPtr readindntifier(byte[] block, int size, ref int index);
        [DllImport("Bulddll", CallingConvention = CallingConvention.StdCall)]
        public static extern int index_of_indentifier(byte[] memblock, int size, byte[] value, int valuesize, int start);
        [DllImport("Bulddll", CallingConvention = CallingConvention.StdCall)]
        public static extern int write_value_indntifier(byte[] block, int size, int index, char[] newvalue);
        [DllImport("Bulddll", CallingConvention = CallingConvention.StdCall)]
        public static extern int write_value_indntifier(byte[] block, int size, int index, byte[] newvalue);
        [DllImport("Bulddll", CallingConvention = CallingConvention.StdCall)]
        public static extern int List_of_indntifier(byte[] block, int size, int index, uint search_iden);
        [DllImport("Bulddll", CallingConvention = CallingConvention.StdCall)]
        public static extern int valuesize_readindntifier(byte[] block, int size, int index);
        [DllImport("Bulddll", CallingConvention = CallingConvention.StdCall)]
        public static extern int valueINDEX_readindntifier(byte[] block, int size, int index);
        [DllImport("Bulddll", CallingConvention = CallingConvention.StdCall)]
        public static extern int Identifier_length(byte[] block, int size, int index);

        [DllImport("Bulddll", CallingConvention = CallingConvention.StdCall)]
        public static extern int Memory_compare(byte[] array1, byte[] array2);
        [DllImport("Bulddll", CallingConvention = CallingConvention.StdCall)]
        public static extern void Memory_Copy(byte[] Source, byte[] Destention);
        
        public static Boolean FAT(string Filename, string LATS, string result)
        {
            bool ok;

            byte[] array = File.ReadAllBytes(Filename);
            byte[] b = { 0x7f, 0x6b };
            int index = index_of_indentifier(array, array.Length, b, 2, 0);
            b = new byte[2] { 0x50, 0x0E };
            index = index_of_indentifier(array, array.Length, b, 2, index);

            char[] newvalue = LATS.ToCharArray();
            index = write_value_indntifier(array, array.Length, index, newvalue);


            ok = ByteArrayToFile(result, array);
            return ok;
        }
        public static Boolean Change_seq_number(string Filename, string New_Seq, string result)
        {

            bool ok;
            byte[] array = File.ReadAllBytes(Filename);
            byte[] b = { 0x64};
            int index = index_of_indentifier(array, array.Length, b, 1, 0);
            b = new byte[3] {0x5f, 0x6d,0x05};
            index = index_of_indentifier(array, array.Length, b, 3, index);

            char[] newvalue = New_Seq.ToCharArray();
            index = write_value_indntifier(array, array.Length, index, newvalue);
            ok = ByteArrayToFile(result, array);
            return ok;

        }
        public static Boolean ByteArrayToFile(string _FileName, byte[] _ByteArray)
        {
            try
            {
                // Open file for reading
                System.IO.FileStream _FileStream =
                   new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
                                            System.IO.FileAccess.Write);
                // Writes a block of bytes to this stream using data from
                // a byte array.
                _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

                // close file stream
                _FileStream.Close();

                return true;
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}",
                                  _Exception.ToString());
            }

            // error occured, return false
            return false;
        }

        public struct write_inden
        {

            public long start;
            public int lenght;
            public long end;
        };

        private static byte[] creatIDEN(int iden, string value, byte type)
        {
            byte[] v;
            if (type == 1 || type == 4)//ascii
            {

                v = Encoding.ASCII.GetBytes(value);

            }
            else if (type == 2)//HEX
            {
                if (value.Length % 2 == 1)
                    value += "f";
                v = ToByteArray(value);

            }
            else //if (type == 3)//INT
            {
                Int32 b = Int32.Parse(value);
                bool added = false; //zero added to the began on value
                string binary = Convert.ToString(b, 2);
                if (binary[0] == '1')
                {
                    binary = "00000000" + binary;
                    added = true;
                }
                byte len = (byte)Math.Ceiling((double)binary.Length / 8);
                v = new byte[len];
                byte[] intBytes = BitConverter.GetBytes(b);
                if (added)
                {
                    v[0] = 0;
                    Array.Copy(intBytes, v, len - 1);
                }
                else
                    Array.Copy(intBytes, v, len);
                Array.Reverse(v);

            }

            List<byte> idenlist = new List<byte>();
            byte temp;
            int mask;
            if (iden < 32)
            {
                temp = 0x40;
                temp |= (byte)iden;
                idenlist.Add(temp);
            }
            else if (iden >= 32)
            {
                idenlist.Add(0x5f);
                if (iden <= 127)
                {
                    idenlist.Add((byte)iden);
                }
                else
                {
                    mask = 0xff80;
                    mask &= iden;
                    mask = mask >> 7;
                    mask |= 0x80;
                    idenlist.Add((byte)mask);
                    mask = 0x007f;
                    mask &= iden;
                    idenlist.Add((byte)mask);
                }
            }
            if (v.Length <= 127)
            {
                idenlist.Add((byte)v.Length);
            }
            else
            {

                string binary = Convert.ToString(v.Length, 2);
                byte len = (byte)Math.Ceiling((double)binary.Length / 8);
                byte[] result = new byte[len];
                byte[] intBytes = BitConverter.GetBytes(v.Length);

                Array.Copy(intBytes, result, len);
                Array.Reverse(result);

                len |= 0x80;
                idenlist.Add(len);


                idenlist.AddRange(result);
            }
            idenlist.AddRange(v);
            return idenlist.ToArray();
        }

        private static byte[] create_Parent_IDEN(int iden)
        {


            List<byte> idenlist = new List<byte>();
            byte temp;
            int mask;
            if (iden < 32)
            {
                temp = 0x60;
                temp |= (byte)iden;
                idenlist.Add(temp);
            }
            else if (iden >= 32)
            {
                idenlist.Add(0x7f);
                if (iden <= 127)
                {
                    idenlist.Add((byte)iden);
                }
                else
                {
                    mask = 0xff80;
                    mask &= iden;
                    mask = mask >> 7;
                    mask |= 0x80;
                    idenlist.Add((byte)mask);
                    mask = 0x007f;
                    mask &= iden;
                    idenlist.Add((byte)mask);
                }
            }
            idenlist.Add(0x80);
            
            return idenlist.ToArray();
        }
        public static byte[] ToByteArray(String HexString)
        {
            int NumberChars = HexString.Length;
            byte[] bytes = new byte[(int)Math.Ceiling((double)NumberChars / 2)];
            for (int i = 0; i < NumberChars; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
            }
            return bytes;
        }

        public static string Convert_iden(byte[] value, byte type)
        {
            if (type == 1 || type == 4)//ascii
            {
                string s = Encoding.UTF8.GetString(value, 0, value.Length);
                return s;

            }
            else if (type == 2)//HEX
            {
                //Array.Resize(ref value, value.Length / 2);
                string hex = BitConverter.ToString(value).Replace("-", string.Empty);
                hex = hex.Replace("F", string.Empty);
                return hex;

            }
            else //if (type == 3)//INT
            {

                if (BitConverter.IsLittleEndian)
                    Array.Reverse(value);

                Array.Resize(ref value, 4);
                int i = BitConverter.ToInt32(value, 0);


                return i.ToString();
            }
        }
        public struct indefinded_name
        {
            public int cidentifier { get; private set; }
            public byte ctype { get; private set; }
            public byte cmin { get; private set; }
            public byte cmax { get; private set; }
            public indefinded_name(int identifier, byte type, byte min, byte max)
                : this()
            {
                cidentifier = identifier;
                ctype = type;
                cmin = min;
                cmax = max;
            }
        };
        public struct indefinded_inden
        {
            public int identifier;
            public long start;
            public string key;
            public long end;
        };


        public static string Find_and_replace (int indentifier_index,string find,string replace , string Filename,string result)
        {
            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack
           
            indefinded_name[] indentefiers = new indefinded_name[] { new indefinded_name(261, 1, 1, 63), new indefinded_name(262, 1, 1, 37), new indefinded_name(302, 1, 0, 0), new indefinded_name(33, 2, 0, 0), new indefinded_name(349, 3, 0, 0), new indefinded_name(348, 1, 0, 0), new indefinded_name(396, 3, 0, 0), new indefinded_name(42, 1, 0, 0), new indefinded_name(46, 1, 0, 0), new indefinded_name(43, 3, 0, 0), new indefinded_name(44, 1, 0, 0), new indefinded_name(45, 1, 1, 8), new indefinded_name(259, 3, 0, 0), new indefinded_name(255, 3, 0, 0), new indefinded_name(256, 3, 0, 0), new indefinded_name(55, 3, 0, 0), new indefinded_name(56, 3, 0, 0), new indefinded_name(58, 3, 0, 0), new indefinded_name(59, 3, 0, 0), new indefinded_name(65, 3, 0, 0), new indefinded_name(410, 1, 0, 0), new indefinded_name(66, 1, 1, 0), new indefinded_name(287, 1, 0, 0), new indefinded_name(305, 3, 0, 0), new indefinded_name(67, 3, 0, 0), new indefinded_name(68, 3, 0, 0), new indefinded_name(344, 3, 0, 0), new indefinded_name(71, 1, 2, 3), new indefinded_name(72, 3, 0, 0), new indefinded_name(73, 1, 1, 0), new indefinded_name(74, 1, 0, 0), new indefinded_name(75, 3, 0, 0), new indefinded_name(350, 3, 0, 0), new indefinded_name(76, 1, 0, 0), new indefinded_name(345, 3, 0, 0), new indefinded_name(292, 1, 0, 0), new indefinded_name(291, 3, 0, 0), new indefinded_name(334, 1, 0, 0), new indefinded_name(336, 3, 0, 0), new indefinded_name(337, 3, 0, 0), new indefinded_name(79, 1, 1, 40), new indefinded_name(364, 1, 0, 0), new indefinded_name(363, 3, 0, 0), new indefinded_name(81, 3, 0, 0), new indefinded_name(250, 3, 0, 0), new indefinded_name(251, 3, 0, 0), new indefinded_name(87, 3, 0, 0), new indefinded_name(88, 1, 0, 0), new indefinded_name(90, 1, 0, 0), new indefinded_name(279, 1, 0, 0), new indefinded_name(91, 3, 0, 0), new indefinded_name(92, 3, 0, 0), new indefinded_name(98, 1, 1, 0), new indefinded_name(101, 1, 0, 0), new indefinded_name(437, 1, 0, 0), new indefinded_name(438, 3, 0, 0), new indefinded_name(290, 1, 0, 0), new indefinded_name(322, 3, 0, 0), new indefinded_name(103, 1, 0, 0), new indefinded_name(435, 1, 0, 0), new indefinded_name(104, 3, 0, 0), new indefinded_name(105, 3, 0, 0), new indefinded_name(107, 1, 0, 0), new indefinded_name(108, 1, 0, 0), new indefinded_name(109, 1, 5, 0), new indefinded_name(110, 1, 1, 0), new indefinded_name(111, 3, 0, 0), new indefinded_name(420, 1, 1, 0), new indefinded_name(122, 1, 0, 0), new indefinded_name(288, 1, 0, 0), new indefinded_name(311, 3, 0, 0), new indefinded_name(392, 3, 0, 0), new indefinded_name(385, 3, 0, 0), new indefinded_name(424, 1, 1, 0), new indefinded_name(128, 2, 7, 8), new indefinded_name(129, 2, 3, 8), new indefinded_name(418, 3, 0, 0), new indefinded_name(294, 1, 0, 0), new indefinded_name(293, 3, 0, 0), new indefinded_name(331, 3, 0, 0), new indefinded_name(295, 1, 0, 0), new indefinded_name(133, 1, 0, 0), new indefinded_name(384, 1, 0, 0), new indefinded_name(391, 3, 0, 0), new indefinded_name(135, 1, 0, 0), new indefinded_name(16, 1, 14, 0), new indefinded_name(136, 3, 0, 0), new indefinded_name(137, 1, 0, 0), new indefinded_name(289, 1, 0, 0), new indefinded_name(315, 3, 0, 0), new indefinded_name(421, 1, 1, 0), new indefinded_name(253, 1, 0, 0), new indefinded_name(142, 1, 0, 0), new indefinded_name(141, 3, 0, 0), new indefinded_name(144, 3, 0, 0), new indefinded_name(145, 3, 0, 0), new indefinded_name(439, 3, 0, 0), new indefinded_name(146, 1, 2, 15), new indefinded_name(440, 3, 0, 0), new indefinded_name(152, 2, 1, 9), new indefinded_name(417, 1, 0, 0), new indefinded_name(155, 1, 1, 6), new indefinded_name(245, 3, 0, 0), new indefinded_name(402, 1, 0, 0), new indefinded_name(445, 1, 0, 0), new indefinded_name(159, 3, 0, 0), new indefinded_name(281, 3, 0, 0), new indefinded_name(163, 1, 0, 0), new indefinded_name(300, 1, 0, 0), new indefinded_name(164, 1, 0, 0), new indefinded_name(165, 1, 0, 0), new indefinded_name(346, 3, 0, 0), new indefinded_name(166, 1, 1, 0), new indefinded_name(347, 3, 0, 0), new indefinded_name(167, 1, 0, 0), new indefinded_name(260, 1, 0, 0), new indefinded_name(169, 1, 5, 0), new indefinded_name(395, 3, 0, 0), new indefinded_name(170, 3, 0, 0), new indefinded_name(446, 1, 0, 0), new indefinded_name(181, 1, 0, 0), new indefinded_name(184, 3, 0, 0), new indefinded_name(400, 1, 0, 0), new indefinded_name(186, 3, 0, 0), new indefinded_name(182, 1, 0, 0), new indefinded_name(189, 3, 0, 0), new indefinded_name(301, 1, 0, 0), new indefinded_name(452, 1, 0, 0), new indefinded_name(394, 3, 0, 0), new indefinded_name(387, 3, 0, 0), new indefinded_name(196, 1, 0, 0), new indefinded_name(447, 1, 0, 0), new indefinded_name(198, 1, 0, 0), new indefinded_name(195, 1, 0, 0), new indefinded_name(200, 1, 1, 0), new indefinded_name(419, 1, 0, 0), new indefinded_name(425, 1, 0, 0), new indefinded_name(201, 3, 0, 0), new indefinded_name(204, 1, 1, 40), new indefinded_name(208, 3, 0, 0), new indefinded_name(210, 1, 0, 0), new indefinded_name(244, 3, 0, 0), new indefinded_name(212, 3, 0, 0), new indefinded_name(432, 1, 1, 0), new indefinded_name(217, 1, 2, 0), new indefinded_name(223, 3, 0, 0), new indefinded_name(343, 3, 0, 0), new indefinded_name(389, 3, 0, 0), new indefinded_name(388, 3, 0, 0), new indefinded_name(342, 1, 0, 0), new indefinded_name(338, 3, 0, 0), new indefinded_name(339, 1, 0, 0), new indefinded_name(341, 1, 0, 0), new indefinded_name(340, 1, 0, 0), new indefinded_name(303, 3, 0, 0), new indefinded_name(227, 1, 0, 0), new indefinded_name(228, 3, 0, 0), new indefinded_name(280, 3, 0, 0), new indefinded_name(231, 1, 5, 0), new indefinded_name(232, 3, 0, 0), new indefinded_name(393, 3, 0, 0), new indefinded_name(386, 3, 0, 0), new indefinded_name(241, 3, 0, 0), new indefinded_name(242, 1, 5, 0), new indefinded_name(243, 3, 0, 0), new indefinded_name(218, 1, 2, 0) };
            int search_iden = indentefiers[indentifier_index].cidentifier;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            List<write_inden> L_found_inden = new List<write_inden>();
            write_inden found_inden;
            found_inden.start = 0;
            found_inden.end = 0;
            found_inden.lenght = 0;
            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden z;
            indefinded_inden pear;

            long start = 0;

            byte[] memblock = File.ReadAllBytes(Filename);

            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator
                string f = "*"; // string indentifer value
                byte indlen;//length of identifier
                int indlenV;//value of identifier


                //unsigned char mask;
                //p = start;
                check_again1:
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {
                            //    Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                            cards.Pop();
                            goto check_again;
                        }
                    }
                }
                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {
                        // Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                        cards.Pop();
                        if( start!=memblock.Length)
                            goto check_again1;
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }
                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                            //for (int b=1; b<indlen;b++)
                            //{
                            //cout<<"long identifer loop" <<endl;
                            //indlenV=indlenV << 8;
                            //start++;
                            //indlenV |=block[start];
                            // cin >> p;				
                            //}
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }
                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {
                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }
                        }
                        else
                        {
                            lenV = -1;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        if (lenght > 0)
                            lenV = lenght;
                        else// if lenght =0 
                            lenV = -2;
                    }
                    mask = 0x20;
                    mask &= iden;
                    if (mask == 0x20)
                    {
                        //parent



                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;
                        if (lenV > 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0)
                        {

                            pear = cards.Peek();
                            z.key = indlenV.ToString();
                            if (lenV != 0 && lenV != -2)
                            cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                               cards.Push(z);
                        }
                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;
                            byte[] valuelist = new byte[lenV];
                            Array.Copy(memblock, start, valuelist, 0, lenV);
                            start += lenV;
                            if (indlenV == search_iden)
                            {
                                f = Convert_iden(valuelist, indentefiers[indentifier_index].ctype);
                            }
                        }
                        else if (lenV != -2)
                        {
                            List<byte> valuelist = new List<byte>(); ;

                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                valuelist.Add(memblock[start]);
                                start++;
                            }
                            if (indlenV == search_iden)
                            {
                                f = Convert_iden(valuelist.ToArray(), indentefiers[indentifier_index].ctype);
                            }
                            start++;
                            start++;
                        }
                        if (lenV != -2)
                            start++; // to skip the 0 lenght

                        if (indlenV == search_iden && f == find)
                        {
                            found_inden.start = iden_start;
                            found_inden.end = start;
                            L_found_inden.Add(found_inden);
                        }
                    }
                }
            }
            if (L_found_inden.Count > 0)
            {
                System.IO.FileStream _FileStream =
                    new System.IO.FileStream(result, System.IO.FileMode.Create,
                                               System.IO.FileAccess.Write);


                byte[] t = creatIDEN(indentefiers[indentifier_index].cidentifier, replace, indentefiers[indentifier_index].ctype);
                _FileStream.Write(memblock, 0, (int)L_found_inden[0].start);
                _FileStream.Write(t, 0, t.Length);
                long l;
                for (int k = 1; k <= L_found_inden.Count - 1; k++)
                {
                    l = (int)L_found_inden[k].start - L_found_inden[k - 1].end;
                    _FileStream.Write(memblock, (int)L_found_inden[k - 1].end, (int)l);
                    _FileStream.Write(t, 0, t.Length);

                }
                l = memblock.Length - (int)L_found_inden[L_found_inden.Count - 1].end;
                _FileStream.Write(memblock, (int)L_found_inden[L_found_inden.Count - 1].end, (int)l);
                _FileStream.Close();
            }
            stopwatch.Stop();
            return stopwatch.Elapsed.ToString();

        }
        public static string To_variable_lenth(string Filename, string result)
        {
            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack
            List<write_inden> L_found_inden = new List<write_inden>();
            write_inden start_inden, end_inden;
            start_inden.start = 0;
            start_inden.end = 0;
            start_inden.lenght = 0;
            end_inden.start = 0;
            end_inden.end = 1;
            end_inden.lenght = 0;
            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden z;
            indefinded_inden pear;

            long start = 0;

            byte[] memblock = File.ReadAllBytes(Filename);

            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator

                byte indlen;//length of identifier
                int indlenV;//value of identifier


                //unsigned char mask;
                //p = start;
            check_again2:
                if (start >= memblock.Length)
                    break;
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {

                       
                            cards.Pop();
                            goto check_again;
                        }
                    }
                }

                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {
                        
                        cards.Pop();

                        goto check_again2;
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }

                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                           
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }
                    
                    
                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {

                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            start_inden.start = start;
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }

                            start_inden.lenght = lenght+1;
                            end_inden.start = start + lenV + 1;
                        }
                        else
                        {
                            lenV = -1;
                        }
                    }
                    else
                    {

                        //short lenght
                        start_inden.start = start;
                        start_inden.lenght = 1;
                        lenght &= 0x7F;
                        if (lenght > 0)
                        {
                            lenV = lenght;
                            end_inden.start = start + lenV + 1;
                        }
                        else// if lenght =0 
                        {
                            lenV = -2;
                            //end_inden.start = start + 1;
                        }
                        
                    }
                    mask = 0x20;
                    mask &= iden;
                    if (mask == 0x20)
                    {
                        //parent
                        if (lenV>0)
                        {
                            L_found_inden.Add(start_inden);
                            L_found_inden.Add(end_inden);
                        }
                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;
                        if (lenV >= 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0)
                        {

                            pear = cards.Peek();
                            z.key = indlenV.ToString();
                            if (lenV!=0 && lenV!=-2)
                                cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                            cards.Push(z);
                        }


                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;

                            start += lenV;

                        }
                        else if (lenV != -2)
                        {
                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                start++;
                            }
                            start++;
                            start++;
                        }
                        else if (lenV == -2)
                            start++; // to skip the 0 lenght
                    }

                }
                else
                    return "corrupted";
            }
           
            if (L_found_inden.Count == 0)
            {
                return "no change";
            }

            L_found_inden = L_found_inden.OrderBy(x => x.start).ToList();

            System.IO.FileStream _FileStream =
                new System.IO.FileStream(result, System.IO.FileMode.Create,
                                           System.IO.FileAccess.Write);
            byte[] t = { 0x80, 0, 0 };
            _FileStream.Write(memblock, 0, (int)L_found_inden[0].start);
            _FileStream.Write(t, 0, 1);
            long l, y;

            for (int k = 1; k <= L_found_inden.Count - 1; k++)
            {

                l = (int)L_found_inden[k - 1].start + L_found_inden[k - 1].lenght;
                y = (int)L_found_inden[k].start - L_found_inden[k - 1].start - L_found_inden[k - 1].lenght;
                _FileStream.Write(memblock, (int)l, (int)y);
                if (L_found_inden[k].end == 0)
                    _FileStream.Write(t, 0, 1);
                else
                    _FileStream.Write(t, 1, 2);

            }

            l = memblock.Length - (int)L_found_inden[L_found_inden.Count - 1].start;
            _FileStream.Write(memblock, (int)L_found_inden[L_found_inden.Count - 1].start, (int)l);
            _FileStream.Close();

            


            //MessageBox.Show(L_found_inden.Count.ToString());
            return "done";
        }
        public struct Update_identifer
        {
            public long start;
            public long end;
            public byte[]  identifier;
        };
        public struct Audit
        {
            public long Calc_charge; // calculated charge from file
            public long Calc_Discount ;// calculated discount from file
            public long Calc_Tax;// calculated Tac from file
            public int Calc_Call_event ; // count call from file
            public string Calc_Earliest_call;//calculated earliest call
            public string Calc_Lastest_call;//calculated latest call

            public long file_charge ; // file charge from file
            public long start_file_charge ; // file start index
            public long end_file_charge ; // file end index

            public long file_Discount ;// file discount from file
            public long start_file_Discount ;// file start index
            public long end_file_Discount ;// file start index

            public long file_Tax ;// file Tac from file
            public long start_file_Tax ;// file start index
            public long end_file_Tax ;// file start index

            public int file_Call_event ; // file call from file
            public long start_Call_event ;// file start index
            public long end_Call_event ;// file start index

            public string file_Earliest_call ;//file earliest call
            public long start_file_Earliest_call ;// file start index
            public long end_file_Earliest_call ;// file start index

            public string file_Lastest_call;//file latest call
            public long start_file_Lastest_call;// file start index
            public long end_file_Lastest_call;// file start index

        };
        public static Audit Calc_Audit( string Filename, string result)
        {
            Audit audit1 = new Audit();// sturct it has everything about the file audit calculated and writen in the file 
            byte[] earliest_call = { 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57 };
            byte[] Lastest_call = { 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 };

            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack

            indefinded_name[] indentefiers = new indefinded_name[] { new indefinded_name(261, 1, 1, 63), new indefinded_name(262, 1, 1, 37), new indefinded_name(302, 1, 0, 0), new indefinded_name(33, 2, 0, 0), new indefinded_name(349, 3, 0, 0), new indefinded_name(348, 1, 0, 0), new indefinded_name(396, 3, 0, 0), new indefinded_name(42, 1, 0, 0), new indefinded_name(46, 1, 0, 0), new indefinded_name(43, 3, 0, 0), new indefinded_name(44, 1, 0, 0), new indefinded_name(45, 1, 1, 8), new indefinded_name(259, 3, 0, 0), new indefinded_name(255, 3, 0, 0), new indefinded_name(256, 3, 0, 0), new indefinded_name(55, 3, 0, 0), new indefinded_name(56, 3, 0, 0), new indefinded_name(58, 3, 0, 0), new indefinded_name(59, 3, 0, 0), new indefinded_name(65, 3, 0, 0), new indefinded_name(410, 1, 0, 0), new indefinded_name(66, 1, 1, 0), new indefinded_name(287, 1, 0, 0), new indefinded_name(305, 3, 0, 0), new indefinded_name(67, 3, 0, 0), new indefinded_name(68, 3, 0, 0), new indefinded_name(344, 3, 0, 0), new indefinded_name(71, 1, 2, 3), new indefinded_name(72, 3, 0, 0), new indefinded_name(73, 1, 1, 0), new indefinded_name(74, 1, 0, 0), new indefinded_name(75, 3, 0, 0), new indefinded_name(350, 3, 0, 0), new indefinded_name(76, 1, 0, 0), new indefinded_name(345, 3, 0, 0), new indefinded_name(292, 1, 0, 0), new indefinded_name(291, 3, 0, 0), new indefinded_name(334, 1, 0, 0), new indefinded_name(336, 3, 0, 0), new indefinded_name(337, 3, 0, 0), new indefinded_name(79, 1, 1, 40), new indefinded_name(364, 1, 0, 0), new indefinded_name(363, 3, 0, 0), new indefinded_name(81, 3, 0, 0), new indefinded_name(250, 3, 0, 0), new indefinded_name(251, 3, 0, 0), new indefinded_name(87, 3, 0, 0), new indefinded_name(88, 1, 0, 0), new indefinded_name(90, 1, 0, 0), new indefinded_name(279, 1, 0, 0), new indefinded_name(91, 3, 0, 0), new indefinded_name(92, 3, 0, 0), new indefinded_name(98, 1, 1, 0), new indefinded_name(101, 1, 0, 0), new indefinded_name(437, 1, 0, 0), new indefinded_name(438, 3, 0, 0), new indefinded_name(290, 1, 0, 0), new indefinded_name(322, 3, 0, 0), new indefinded_name(103, 1, 0, 0), new indefinded_name(435, 1, 0, 0), new indefinded_name(104, 3, 0, 0), new indefinded_name(105, 3, 0, 0), new indefinded_name(107, 1, 0, 0), new indefinded_name(108, 1, 0, 0), new indefinded_name(109, 1, 5, 0), new indefinded_name(110, 1, 1, 0), new indefinded_name(111, 3, 0, 0), new indefinded_name(420, 1, 1, 0), new indefinded_name(122, 1, 0, 0), new indefinded_name(288, 1, 0, 0), new indefinded_name(311, 3, 0, 0), new indefinded_name(392, 3, 0, 0), new indefinded_name(385, 3, 0, 0), new indefinded_name(424, 1, 1, 0), new indefinded_name(128, 2, 7, 8), new indefinded_name(129, 2, 3, 8), new indefinded_name(418, 3, 0, 0), new indefinded_name(294, 1, 0, 0), new indefinded_name(293, 3, 0, 0), new indefinded_name(331, 3, 0, 0), new indefinded_name(295, 1, 0, 0), new indefinded_name(133, 1, 0, 0), new indefinded_name(384, 1, 0, 0), new indefinded_name(391, 3, 0, 0), new indefinded_name(135, 1, 0, 0), new indefinded_name(16, 1, 14, 0), new indefinded_name(136, 3, 0, 0), new indefinded_name(137, 1, 0, 0), new indefinded_name(289, 1, 0, 0), new indefinded_name(315, 3, 0, 0), new indefinded_name(421, 1, 1, 0), new indefinded_name(253, 1, 0, 0), new indefinded_name(142, 1, 0, 0), new indefinded_name(141, 3, 0, 0), new indefinded_name(144, 3, 0, 0), new indefinded_name(145, 3, 0, 0), new indefinded_name(439, 3, 0, 0), new indefinded_name(146, 1, 2, 15), new indefinded_name(440, 3, 0, 0), new indefinded_name(152, 2, 1, 9), new indefinded_name(417, 1, 0, 0), new indefinded_name(155, 1, 1, 6), new indefinded_name(245, 3, 0, 0), new indefinded_name(402, 1, 0, 0), new indefinded_name(445, 1, 0, 0), new indefinded_name(159, 3, 0, 0), new indefinded_name(281, 3, 0, 0), new indefinded_name(163, 1, 0, 0), new indefinded_name(300, 1, 0, 0), new indefinded_name(164, 1, 0, 0), new indefinded_name(165, 1, 0, 0), new indefinded_name(346, 3, 0, 0), new indefinded_name(166, 1, 1, 0), new indefinded_name(347, 3, 0, 0), new indefinded_name(167, 1, 0, 0), new indefinded_name(260, 1, 0, 0), new indefinded_name(169, 1, 5, 0), new indefinded_name(395, 3, 0, 0), new indefinded_name(170, 3, 0, 0), new indefinded_name(446, 1, 0, 0), new indefinded_name(181, 1, 0, 0), new indefinded_name(184, 3, 0, 0), new indefinded_name(400, 1, 0, 0), new indefinded_name(186, 3, 0, 0), new indefinded_name(182, 1, 0, 0), new indefinded_name(189, 3, 0, 0), new indefinded_name(301, 1, 0, 0), new indefinded_name(452, 1, 0, 0), new indefinded_name(394, 3, 0, 0), new indefinded_name(387, 3, 0, 0), new indefinded_name(196, 1, 0, 0), new indefinded_name(447, 1, 0, 0), new indefinded_name(198, 1, 0, 0), new indefinded_name(195, 1, 0, 0), new indefinded_name(200, 1, 1, 0), new indefinded_name(419, 1, 0, 0), new indefinded_name(425, 1, 0, 0), new indefinded_name(201, 3, 0, 0), new indefinded_name(204, 1, 1, 40), new indefinded_name(208, 3, 0, 0), new indefinded_name(210, 1, 0, 0), new indefinded_name(244, 3, 0, 0), new indefinded_name(212, 3, 0, 0), new indefinded_name(432, 1, 1, 0), new indefinded_name(217, 1, 2, 0), new indefinded_name(223, 3, 0, 0), new indefinded_name(343, 3, 0, 0), new indefinded_name(389, 3, 0, 0), new indefinded_name(388, 3, 0, 0), new indefinded_name(342, 1, 0, 0), new indefinded_name(338, 3, 0, 0), new indefinded_name(339, 1, 0, 0), new indefinded_name(341, 1, 0, 0), new indefinded_name(340, 1, 0, 0), new indefinded_name(303, 3, 0, 0), new indefinded_name(227, 1, 0, 0), new indefinded_name(228, 3, 0, 0), new indefinded_name(280, 3, 0, 0), new indefinded_name(231, 1, 5, 0), new indefinded_name(232, 3, 0, 0), new indefinded_name(393, 3, 0, 0), new indefinded_name(386, 3, 0, 0), new indefinded_name(241, 3, 0, 0), new indefinded_name(242, 1, 5, 0), new indefinded_name(243, 3, 0, 0), new indefinded_name(218, 1, 2, 0) };
            //int search_iden = indentefiers[indentifier_index].cidentifier;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            List<write_inden> L_found_inden = new List<write_inden>();
            write_inden found_inden;

            List<Update_identifer> L_of_update_identifer = new List<Update_identifer>();
            Update_identifer update_iden;

            found_inden.start = 0;
            found_inden.end = 0;
            found_inden.lenght = 0;
            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden z;
            indefinded_inden pear;

            long start = 0;

            byte[] memblock = File.ReadAllBytes(Filename);

            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator
                string f = "*"; // string indentifer value
                byte indlen;//length of identifier
                int indlenV;//value of identifier


                //unsigned char mask;
                //p = start;
            checkagain1:
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {
                            //    Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                            cards.Pop();
                            goto check_again;
                        }
                    }
                }
                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {
                        // Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                        cards.Pop();
                        if (start == memblock.Length)
                            break;
                        goto checkagain1;
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }
                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                            //for (int b=1; b<indlen;b++)
                            //{
                            //cout<<"long identifer loop" <<endl;
                            //indlenV=indlenV << 8;
                            //start++;
                            //indlenV |=block[start];
                            // cin >> p;				
                            //}
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }
                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {
                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }
                        }
                        else
                        {
                            lenV =-1;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        if (lenght > 0)
                            lenV = lenght;
                        else// if lenght =0 
                            lenV = -2;
                        
                    }

                    if (indlenV == 9 || indlenV == 10 || indlenV == 11 || indlenV == 12 || indlenV == 14 || indlenV == 17 || indlenV == 297 || indlenV == 433 || indlenV == 434)
                    {
                        audit1.Calc_Call_event += 1;
                    }
                    
                    mask = 0x20;
                    mask &= iden;
                    if (mask == 0x20)
                    {
                        //parent



                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;

                        if (lenV > 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0 )
                        {

                            pear = cards.Peek();
                            z.key = indlenV.ToString();
                            if (lenV != 0 && lenV != -2)
                               cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                            cards.Push(z);
                        }
                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;
                            byte[] valuelist = new byte[lenV];
                            Array.Copy(memblock, start, valuelist, 0, lenV);
                            start += lenV;
                            if (indlenV == 62)
                            {
                                long i = (int)valuelist[0];
                                for (int c = 1; c < lenV; c++)
                                {
                                    i = i << 8;
                                    i |= valuelist[c];
                                }
                                audit1.Calc_charge += i;
                            }
                            if (indlenV == 397)
                            {
                                long i = (int)valuelist[0];
                                for (int c = 1; c < lenV; c++)
                                {
                                    i = i << 8;
                                    i |= valuelist[c];
                                }
                                audit1.Calc_Tax += i;
                            }
                            if (indlenV == 412)
                            {
                                long i = (int)valuelist[0];
                                for (int c = 1; c < lenV; c++)
                                {
                                    i = i << 8;
                                    i |= valuelist[c];
                                }
                                audit1.Calc_Discount += i;
                            }
                            if (indlenV == 415)//TotalCharge 
                            {
                                long i = (int)valuelist[0];
                                for (int c = 1; c < lenV; c++)
                                {
                                    i = i << 8;
                                    i |= valuelist[c];
                                }
                                audit1.file_charge = i;//TotalCharge 
                                audit1.start_file_charge = iden_start;
                                audit1.end_file_charge = start;

                            }
                            if (indlenV == 226)//TotalTaxValue
                            {
                                long i = (int)valuelist[0];
                                for (int c = 1; c < lenV; c++)
                                {
                                    i = i << 8;
                                    i |= valuelist[c];
                                }
                                audit1.file_Tax = i;//TotalTaxValue
                                audit1.start_file_Tax = iden_start;
                                audit1.end_file_Tax = start;

                            }
                            if (indlenV == 225)//TotalDiscountValue 
                            {
                                long i = (int)valuelist[0];
                                for (int c = 1; c < lenV; c++)
                                {
                                    i = i << 8;
                                    i |= valuelist[c];
                                }
                                audit1.file_Discount = i;//TotalDiscountValue 
                                audit1.start_file_Discount = iden_start;
                                audit1.end_file_Discount = start;

                            }
                            if (indlenV == 43)//CallEventDetailsCount 
                            {
                                int i = (int)valuelist[0];
                                for (int c = 1; c < lenV; c++)
                                {
                                    i = i << 8;
                                    i |= valuelist[c];
                                }
                                audit1.file_Call_event = i;//CallEventDetailsCount 
                                audit1.start_Call_event = iden_start;
                                audit1.end_Call_event = start;

                            }

                            z = cards.Peek();
                            if (indlenV == 16 && z.identifier == 44)
                            {
                                int n;

                                //str.assign(buf, buf + l);

                                n = Memory_compare(valuelist, Lastest_call);
                                if (n > 0)//latest call
                                    Memory_Copy(valuelist, Lastest_call);


                                n = Memory_compare(valuelist, earliest_call);
                                if (n < 0)//latest call
                                    Memory_Copy(valuelist, earliest_call);


                            }
                            if (indlenV == 16 && z.identifier == 101)//EarliestCallTimeStamp  
                            {
                                audit1.file_Earliest_call = System.Text.Encoding.Default.GetString(valuelist); ;//EarliestCallTimeStamp  
                                audit1.start_file_Earliest_call = iden_start;
                                audit1.end_file_Earliest_call = start;

                            }
                            if (indlenV == 16 && z.identifier == 133)//LatestCallTimeStamp   
                            {
                                audit1.file_Lastest_call = System.Text.Encoding.Default.GetString(valuelist); ;//LatestCallTimeStamp   
                                audit1.start_file_Lastest_call = iden_start;
                                audit1.end_file_Lastest_call = start;

                            }
                            // if (indlenV == search_iden)
                            //   {
                            //     f = Convert_iden(valuelist, indentefiers[indentifier_index].ctype);
                            //  }
                        }
                        else if (lenV != -2)
                        {
                            List<byte> valuelist = new List<byte>(); ;

                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                valuelist.Add(memblock[start]);
                                start++;
                            }
                            //  if (indlenV == search_iden)
                            //    {
                            //       f = Convert_iden(valuelist.ToArray(), indentefiers[indentifier_index].ctype);
                            //   }
                            start++;
                            start++;
                        }
                        else if (lenV == -2)
                            start++;

                      //  if (indlenV == search_iden && f == find)
                     //   {
                    //        found_inden.start = iden_start;
                    //        found_inden.end = start;
                     //       L_found_inden.Add(found_inden);
                     //   }
                    }
                }
            }

            audit1.Calc_Earliest_call = System.Text.Encoding.Default.GetString(earliest_call);
            audit1.Calc_Lastest_call = System.Text.Encoding.Default.GetString(Lastest_call);

            if (audit1.file_Call_event != audit1.Calc_Call_event)
            {
                update_iden.start = audit1.start_Call_event;
                update_iden.end = audit1.end_Call_event;
                update_iden.identifier = creatIDEN(43, audit1.Calc_Call_event.ToString(), 3);
                L_of_update_identifer.Add(update_iden);
            }

            if (audit1.file_charge != audit1.Calc_charge)
            {
                update_iden.start = audit1.start_file_charge;
                update_iden.end = audit1.end_file_charge ;
                update_iden.identifier = creatIDEN(415, audit1.Calc_charge.ToString(), 3);
                L_of_update_identifer.Add(update_iden);
            }
            if (audit1.file_Tax != audit1.Calc_Tax)
            {
                update_iden.start = audit1.start_file_Tax;
                update_iden.end = audit1.end_file_Tax;
                update_iden.identifier = creatIDEN(226, audit1.Calc_Tax.ToString(), 3);
                L_of_update_identifer.Add(update_iden);
            }
            if (audit1.file_Discount != audit1.Calc_Discount)
            {
                update_iden.start = audit1.start_file_Discount;
                update_iden.end = audit1.end_file_Discount;
                update_iden.identifier = creatIDEN(225, audit1.Calc_Discount.ToString(), 3);
                L_of_update_identifer.Add(update_iden);
            }
            if (audit1.file_Earliest_call != audit1.Calc_Earliest_call)
            {
                update_iden.start = audit1.start_file_Earliest_call;
                update_iden.end = audit1.end_file_Earliest_call;
                update_iden.identifier = creatIDEN(16, audit1.Calc_Earliest_call, 1);
                L_of_update_identifer.Add(update_iden);
            }
            if (audit1.file_Lastest_call != audit1.Calc_Lastest_call)
            {
                update_iden.start = audit1.start_file_Lastest_call;
                update_iden.end = audit1.end_file_Lastest_call;
                update_iden.identifier = creatIDEN(16, audit1.Calc_Lastest_call, 1);
                L_of_update_identifer.Add(update_iden);
            }
     


             //byte[] t = creatIDEN(indentefiers[indentifier_index].cidentifier, replace, indentefiers[indentifier_index].ctype);
             if (L_of_update_identifer.Count>0)
             {
                 L_of_update_identifer = L_of_update_identifer.OrderBy(x => x.start).ToList();
                 System.IO.FileStream _FileStream =
                    new System.IO.FileStream(result, System.IO.FileMode.Create,
                                    System.IO.FileAccess.Write);
                 _FileStream.Write(memblock, 0, (int)L_of_update_identifer[0].start);
                 _FileStream.Write(L_of_update_identifer[0].identifier, 0, L_of_update_identifer[0].identifier.Length);
                 long l;
                 for (int k = 1; k <= L_of_update_identifer.Count - 1; k++)
                 {
                     l = (int)L_of_update_identifer[k].start - L_of_update_identifer[k - 1].end;
                     _FileStream.Write(memblock, (int)L_of_update_identifer[k - 1].end, (int)l);
                     _FileStream.Write(L_of_update_identifer[k].identifier, 0, L_of_update_identifer[k].identifier.Length);

                 }
                 l = memblock.Length - (int)L_of_update_identifer[L_of_update_identifer.Count - 1].end;
                 _FileStream.Write(memblock, (int)L_of_update_identifer[L_of_update_identifer.Count - 1].end, (int)l);
                 _FileStream.Close();
             }
            stopwatch.Stop();
            return audit1;

        }

        public static void Create_Notifications(string result,string Sender,string Recipient,string Sqe,string timestamp,string ver,string relesed)
        {


            List<byte> file = new List<byte>();
            file.Add(0x62);// addind the header of notification file
            file.Add(0x80);
            file.AddRange(creatIDEN(196, Sender, 1));// adding the sender
            file.AddRange(creatIDEN(182, Recipient, 1));// adding the Recipient
            file.AddRange(creatIDEN(109, Sqe, 1));// adding the FileSequenceNumber
            file.Add(0x7f);  // FileCreationTimpStamp
            file.Add(0x6c); //FileCreationTimpStamp
            file.Add(0x80);//FileCreationTimpStamp
            file.AddRange(creatIDEN(16, timestamp, 1));//adding LocalTimeStamp 
            file.AddRange(creatIDEN(231, "+0300", 1));//adding UtcTimeOffset 
            file.Add(0);//closeing FileCreationTimpStamp
            file.Add(0);//closeing FileCreationTimpStamp
            file.Add(0x7f);// FileAvailableTimeStamp
            file.Add(0x6b);// FileAvailableTimeStamp
            file.Add(0x80);// FileAvailableTimeStamp
            file.AddRange(creatIDEN(16, timestamp, 1));//adding LocalTimeStamp 
            file.AddRange(creatIDEN(231, "+0300", 1));//adding UtcTimeOffset 
            file.Add(0);//closeing FileAvailableTimeStamp
            file.Add(0);//closeing FileAvailableTimeStamp
            file.Add(0x7f);// TransferCutoffTimeStamp
            file.Add(0x81);// TransferCutoffTimeStamp
            file.Add(0x63);// TransferCutoffTimeStamp
            file.Add(0x80);// TransferCutoffTimeStamp
            file.AddRange(creatIDEN(16, timestamp, 1));//adding LocalTimeStamp 
            file.AddRange(creatIDEN(231, "+0300", 1));//adding UtcTimeOffset 
            file.Add(0);//closeing TransferCutoffTimeStamp
            file.Add(0);//closeing TransferCutoffTimeStamp
            file.AddRange(creatIDEN(201, ver, 3));//adding SpecificationVersionNumber
            file.AddRange(creatIDEN(189, relesed, 3));//adding ReleaseVersionNumber
            file.Add(0);//closeing notification file
            file.Add(0);//closeing notification file

          
               
                System.IO.FileStream _FileStream =
                   new System.IO.FileStream(result, System.IO.FileMode.Create,
                                   System.IO.FileAccess.Write);
                _FileStream.Write(file.ToArray(), 0,file.Count );
               
                _FileStream.Close();
            
            

        }

        public static byte[] ReturnbyteArray(byte[] file, int filelenght, ref int index)
        {
            int length;
            length = valuesize_readindntifier(file, filelenght, index);
            byte[] ReturnArray = new byte[length];
            Marshal.Copy(readindntifier(file, filelenght, ref index), ReturnArray, 0, length);
            return ReturnArray;
        }

        public static void Create_Notifications_from_file(string filename,string result)
        {
            int index = 0;
            byte[] array = File.ReadAllBytes(filename);
            
            index = List_of_indntifier(array, array.Length, index, 196);
            byte[] Sender = ReturnbyteArray(array, array.Length, ref index);

            index = List_of_indntifier(array, array.Length, index, 182);
            byte[] recipient = ReturnbyteArray(array, array.Length, ref index);

            index = List_of_indntifier(array, array.Length, index, 109);
            byte[] seq = ReturnbyteArray(array, array.Length, ref index);

            DateTime LTS;// to get past tow days
            LTS = DateTime.Now.AddDays(-2);

            index = List_of_indntifier(array, array.Length, index, 201);
            byte[] ver = ReturnbyteArray(array, array.Length, ref index);

            index = List_of_indntifier(array, array.Length, index, 189);
            byte[] res = ReturnbyteArray(array, array.Length, ref index);


            Create_Notifications(result, Convert_iden(Sender, 1), Convert_iden(recipient, 1), Convert_iden(seq, 1), LTS.ToString("yyyyMMddHHmmss"), Convert_iden(ver, 3), Convert_iden(res, 3));
        }

        public static void Merge_file(string main_filename, string Merge_file,string result)
        {
            int main_index = 0;
            int marge_index = 0;
            byte[] main_file_arr = File.ReadAllBytes(main_filename);
            byte[] marge_file_arr = File.ReadAllBytes(Merge_file);

            main_index = List_of_indntifier(main_file_arr, main_file_arr.Length, main_index, 201);//get version of main file
            byte[] main_file_ver = ReturnbyteArray(main_file_arr, main_file_arr.Length, ref main_index);//get version of main file

            main_index = List_of_indntifier(main_file_arr, main_file_arr.Length, main_index, 189);//get relesed of main file
            byte[] main_file_res = ReturnbyteArray(main_file_arr, main_file_arr.Length, ref main_index);//get relesed of main file

            marge_index = List_of_indntifier(marge_file_arr, marge_file_arr.Length, marge_index, 201);//get version of marge file
            byte[] marge_file_ver = ReturnbyteArray(marge_file_arr, marge_file_arr.Length, ref marge_index);//get version of marge file

            marge_index = List_of_indntifier(marge_file_arr, marge_file_arr.Length, marge_index, 189);//get relesed of marge file
            byte[] marge_file_res = ReturnbyteArray(marge_file_arr, marge_file_arr.Length, ref marge_index);//get relesed of marge file


            //int ver = Memory_compare(main_file_ver, marge_file_ver);// compareing version
            //int res = Memory_compare(main_file_res, marge_file_res);// compareing relese
            if (main_file_ver[0] == marge_file_ver[0] &&   marge_file_res[0] == main_file_res[0])// if the vesions are equle
            {
                byte[] endingbyte = { 0x00, 0x00 };
                
                main_index = List_of_indntifier(main_file_arr, main_file_arr.Length, main_index, 3); //getting the index of callEvents
                int main_call_length = valuesize_readindntifier(main_file_arr, main_file_arr.Length, main_index);// getting the lenght of callEvents 0x00 ending of identifiers is NOT included
                int main_call_value_index = valueINDEX_readindntifier(main_file_arr, main_file_arr.Length, main_index);// getting the value INDEX
                
                marge_index = List_of_indntifier(marge_file_arr, marge_file_arr.Length, marge_index, 3); //getting the index of callEvents
                int merge_call_length = valuesize_readindntifier(marge_file_arr, marge_file_arr.Length, marge_index);// getting the lenght of callEvents 0x00 ending of identifiers is NOT included
                int merge_call_value_index = valueINDEX_readindntifier(marge_file_arr, marge_file_arr.Length, marge_index);// getting the value INDEX

                System.IO.FileStream _FileStream =
                    new System.IO.FileStream(result, System.IO.FileMode.Create,
                                    System.IO.FileAccess.Write);
                _FileStream.Write(main_file_arr, 0, main_call_value_index + main_call_length);
                _FileStream.Write(marge_file_arr, merge_call_value_index, merge_call_length);
                //_FileStream.Write(endingbyte, 0, 2);
                _FileStream.Write(main_file_arr, main_call_value_index + main_call_length, main_file_arr.Length - (main_call_value_index + main_call_length));

                _FileStream.Close();
                functions.Audit audit1 = new functions.Audit();
                audit1 = functions.Calc_Audit(result, result);
            }

        }

         public static void Split_file(string Filename,string Result, string Seq,int part)
        {
            int call_event_count=0;// get count of calls
            long call_index = 0;//index of first call
            long audit_index = 0;//index of audit identifier

            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack

            
            //int search_iden = indentefiers[indentifier_index].cidentifier;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            List<write_inden> L_found_call = new List<write_inden>();
            write_inden found_call;

            


            found_call.start = 0;
            found_call.end = 0;
            found_call.lenght = 0;
            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden z;
            indefinded_inden pear;

            long start = 0;

            byte[] memblock = File.ReadAllBytes(Filename);

            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator
                string f = "*"; // string indentifer value
                byte indlen;//length of identifier
                int indlenV;//value of identifier


                //unsigned char mask;
                //p = start;
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {
                            //    Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                            cards.Pop();
                            goto check_again;
                        }
                    }
                }
                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {
                        // Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                        cards.Pop();
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }
                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                            //for (int b=1; b<indlen;b++)
                            //{
                            //cout<<"long identifer loop" <<endl;
                            //indlenV=indlenV << 8;
                            //start++;
                            //indlenV |=block[start];
                            // cin >> p;				
                            //}
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }
                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {
                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }
                        }
                        else
                        {
                            lenV = -1;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        if (lenght > 0)
                            lenV = lenght;
                        else// if lenght =0 
                            lenV = -2;
                    }

                    if (indlenV == 9 || indlenV == 10 || indlenV == 11 || indlenV == 12 || indlenV == 14 || indlenV == 17 || indlenV == 297 || indlenV == 433 || indlenV == 434)
                    {
                        call_event_count += 1;
                        found_call.start = iden_start;
                        found_call.end = start;
                        L_found_call.Add(found_call);
                    }
                    if (indlenV ==3)
                        call_index = iden_start;
                    if (indlenV==15)
                        audit_index = iden_start;

                    mask = 0x20;
                    mask &= iden;
                    if (mask == 0x20)
                    {
                        //parent



                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;

                        if (lenV > 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0)
                        {

                            pear = cards.Peek();
                            z.key = indlenV.ToString();
                            if (lenV != 0 && lenV != -2)
                            cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                            cards.Push(z);
                        }
                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;
                            byte[] valuelist = new byte[lenV];
                            Array.Copy(memblock, start, valuelist, 0, lenV);
                            start += lenV;

                        }
                        else if (lenV != -2)
                        {
                            List<byte> valuelist = new List<byte>(); ;

                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                valuelist.Add(memblock[start]);
                                start++;
                            }

                            start++;
                            start++;
                        }
                        else if (lenV == -2)
                            start++;

                    }
                }
            }

            int count = L_found_call.Count();
            count =(int)Math.Ceiling((double)count / part);



            int call_value_index = valueINDEX_readindntifier(memblock, memblock.Length, (int)call_index);// getting the value INDEX
            int audit_length = valuesize_readindntifier(memblock, memblock.Length, (int)audit_index);// getting the lenght of audit 0x00 ending of identifiers is NOT included
           
            string[] seqNo =  Seq.Split(',');
            byte[] endingbyte = { 0x00, 0x00 };

            seqNo[0] = String.Format("{0:D5}", int.Parse(seqNo[0]));
            string Split_filename = Path.GetFileName(Filename);
            Split_filename = Split_filename.Substring(0, 12) + seqNo[0];

            int end_call_lenght = Identifier_length(memblock, memblock.Length, (int)L_found_call[count-1].start);
            //end_call_lenght += (int)L_found_call[0].start;

            // count -1 array start from 0 and the count start from 1
            System.IO.FileStream _FileStream =
                   new System.IO.FileStream(Result+"\\"+Split_filename, System.IO.FileMode.Create,
                                   System.IO.FileAccess.Write);
            _FileStream.Write(memblock, 0, call_value_index);
            _FileStream.Write(memblock, (int)L_found_call[0].start, (int)L_found_call[count-1].start + end_call_lenght - (int)L_found_call[0].start);
            _FileStream.Write(endingbyte, 0, 2);

            _FileStream.Write(memblock, (int)audit_index, memblock.Length - (int)audit_index);
            
             _FileStream.Close();
             Change_seq_number(Result + "\\" + Split_filename, seqNo[0], Result + "\\" + Split_filename);
             functions.Audit audit1 = new functions.Audit();
             audit1 = functions.Calc_Audit(Result + "\\" + Split_filename, Result + "\\" + Split_filename );

            for (int partcount=2;partcount<part;partcount++)
             {
                 seqNo[partcount - 1] = String.Format("{0:D5}", int.Parse(seqNo[partcount - 1]));
                 Split_filename = Split_filename.Substring(0, 12) + seqNo[partcount - 1];

                 end_call_lenght = Identifier_length(memblock, memblock.Length, (int)L_found_call[count * partcount-1].start);

                
                 _FileStream = new System.IO.FileStream(Result+"\\" + Split_filename, System.IO.FileMode.Create,
                                       System.IO.FileAccess.Write);
                _FileStream.Write(memblock, 0, call_value_index);
                _FileStream.Write(memblock, (int)L_found_call[count * (partcount - 1)].start, (int)L_found_call[count * partcount-1].start + end_call_lenght - (int)L_found_call[count * (partcount - 1)].start);
                _FileStream.Write(endingbyte, 0, 2);
                _FileStream.Write(memblock, (int)audit_index, memblock.Length - (int)audit_index);
                
               _FileStream.Close();

               Change_seq_number(Result + "\\" + Split_filename, seqNo[partcount - 1], Result + "\\" + Split_filename);
               audit1 = functions.Calc_Audit(Result + "\\" + Split_filename, Result + "\\" + Split_filename );
             }

            seqNo[part-1] = String.Format("{0:D5}", int.Parse(seqNo[part-1]));
            Split_filename = Split_filename.Substring(0, 12) + seqNo[part - 1];
            end_call_lenght = Identifier_length(memblock, memblock.Length, (int)L_found_call[L_found_call.Count-1].start);


            _FileStream = new System.IO.FileStream(Result +"\\"+ Split_filename, System.IO.FileMode.Create,
                                   System.IO.FileAccess.Write);
            _FileStream.Write(memblock, 0, call_value_index);
            _FileStream.Write(memblock, (int)L_found_call[count * (part - 1)].start, (int)L_found_call[L_found_call.Count-1].start + end_call_lenght - (int)L_found_call[count * (part - 1)].start);
            _FileStream.Write(endingbyte, 0, 2);
            _FileStream.Write(memblock, (int)audit_index, memblock.Length - (int)audit_index);
            _FileStream.Close();

            Change_seq_number(Result + "\\" + Split_filename, seqNo[part - 1], Result + "\\" + Split_filename);
            audit1 = functions.Calc_Audit(Result + "\\" + Split_filename, Result + "\\" + Split_filename );

            stopwatch.Stop();
           
        }
        
        public static void Split_file_Date(string Filename, string Result, string Seq, byte[] Compare_date)
         {
           
             long call_index = 0;//index of first call
             long audit_index = 0;//index of audit identifier

             Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack


             //int search_iden = indentefiers[indentifier_index].cidentifier;
             Stopwatch stopwatch = new Stopwatch();
             stopwatch.Start();

             List<write_inden> less_date_call = new List<write_inden>();
             List<write_inden> greater_date_call = new List<write_inden>();
             write_inden found_call;


            

             found_call.start = 0;
             found_call.end = 0;
             found_call.lenght = 0;
             //TreeView Tap3 = new TreeView();
             //TreeNode selected = new TreeNode();
             byte mask;

             indefinded_inden z;
             indefinded_inden pear;

             long start = 0;

             byte[] memblock = File.ReadAllBytes(Filename);

             while (start < memblock.Length)
             {
                 int o;
                 byte iden = 0; //identifier
                 long iden_start = 0; //identifier
                 int lenV = 0;//lenght value
                 int lenght = 0; // lenght of lenght value
                 bool par;// paranert indecator
                 string f = "*"; // string indentifer value
                 byte indlen;//length of identifier
                 int indlenV;//value of identifier


                 //unsigned char mask;
                 //p = start;
                 iden = memblock[start];
                 if (cards.Count > 0)
                 {
                 check_again:
                     if (cards.Count > 0)
                     {
                         z = cards.Peek();
                         if (z.end == start)
                         {
                             //    Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                             cards.Pop();
                             goto check_again;
                         }
                     }
                 }
                 if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                 {
                     start += 2;
                     z = cards.Peek();

                     if (z.end == 0)
                     {
                         // Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                         cards.Pop();
                     }
                     //{
                     //	std::bitset<16> r(z.start);
                     //	std::cout << "stack out " << z.start << endl;
                     //}


                 }
                 mask = 0x40;
                 mask &= iden;
                 if (mask == 0x40)
                 {
                     iden_start = start;
                     mask = 0x1f;
                     mask &= iden;
                     if (mask == 0x1f)
                     {

                         start++;
                         indlen = memblock[start];
                         mask = 0x80;
                         mask &= indlen;
                         if (mask == 0x80)
                         {
                             // long lenght
                             indlen &= 0x7F;
                             indlenV = (byte)indlen;
                             indlenV = indlenV << 7;
                             start++;
                             indlenV |= (byte)memblock[start];
                             //for (int b=1; b<indlen;b++)
                             //{
                             //cout<<"long identifer loop" <<endl;
                             //indlenV=indlenV << 8;
                             //start++;
                             //indlenV |=block[start];
                             // cin >> p;				
                             //}
                         }
                         else
                         {
                             // short lenght
                             indlen &= 0x7F;
                             indlenV = indlen;
                         }
                     }
                     else
                     {

                         indlenV = (byte)iden & 0x1F;
                     }
                     start++;
                     lenght = memblock[start];
                     mask = 0x80;
                     mask &= (byte)lenght;

                     if (mask == 0x80)
                     {
                         mask = 0x7f;
                         mask &= (byte)lenght;
                         if (mask >= 0x01)
                         {
                             //long lenght
                             lenght &= 0x7F;
                             start++;
                             lenV = (byte)memblock[start];
                             for (int c = 1; c < lenght; c++)
                             {
                                 lenV = lenV << 8;
                                 start++;
                                 lenV |= memblock[start];

                             }
                         }
                         else
                         {
                             lenV = -1;
                         }
                     }
                     else
                     {
                         //short lenght
                         lenght &= 0x7F;
                         if (lenght > 0)
                             lenV = lenght;
                         else// if lenght =0 
                             lenV = -2;
                     }

                     if (indlenV == 9 || indlenV == 10 || indlenV == 11 || indlenV == 12 || indlenV == 14 || indlenV == 17 || indlenV == 297 || indlenV == 433 || indlenV == 434)
                     {
                         
                         found_call.start = iden_start;
                         
                     }
                     if (indlenV == 3)
                         call_index = iden_start;
                     if (indlenV == 15)
                         audit_index = iden_start;

                     mask = 0x20;
                     mask &= iden;
                     if (mask == 0x20)
                     {
                         //parent



                         start++;
                         z.identifier = indlenV;
                         z.start = iden_start;

                         if (lenV > 0)
                             z.end = start + lenV;
                         else
                             z.end = 0;

                         if (cards.Count > 0)
                         {

                             pear = cards.Peek();
                             z.key = indlenV.ToString();
                             if (lenV != 0 && lenV != -2)
                             cards.Push(z);
                         }
                         else
                         {
                             z.key = "root";
                             cards.Push(z);
                         }
                     }
                     else
                     {

                         if (lenV > 0)
                         {
                             start++;
                             byte[] valuelist = new byte[lenV];
                             Array.Copy(memblock, start, valuelist, 0, lenV);
                             start += lenV;

                             z = cards.Peek();
                             if (indlenV == 16 && z.identifier == 44)
                             {
                                 int n;

                                 //str.assign(buf, buf + l);

                                 n = Memory_compare(valuelist, Compare_date);
                                 if (n > 0)//latest call
                                     less_date_call.Add(found_call);
                                 else
                                     greater_date_call.Add(found_call);

                               


                             }
                         }
                         else if (lenV != -2)
                         {
                             List<byte> valuelist = new List<byte>(); ;

                             while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                             {
                                 valuelist.Add(memblock[start]);
                                 start++;
                             }

                             start++;
                             start++;
                         }
                         else if (lenV == -2)
                             start++; // to skip the 0 lenght
                     }
                 }
             }

        


             int call_value_index = valueINDEX_readindntifier(memblock, memblock.Length, (int)call_index);// getting the value INDEX
             int audit_length = valuesize_readindntifier(memblock, memblock.Length, (int)audit_index);// getting the lenght of audit 0x00 ending of identifiers is NOT included

             string[] seqNo = Seq.Split(',');
             byte[] endingbyte = { 0x00, 0x00 };

             seqNo[0] = String.Format("{0:D5}", int.Parse(seqNo[0]));
             string Split_filename = Path.GetFileName(Filename);
             Split_filename = Split_filename.Substring(0, 12) + seqNo[0];

             
            /// generate file that have less then the compare date
             System.IO.FileStream _FileStream =
                    new System.IO.FileStream(Result + "\\" + Split_filename, System.IO.FileMode.Create,
                                    System.IO.FileAccess.Write);
             _FileStream.Write(memblock, 0, call_value_index);
             int end_call_lenght = 0;
             for (int Count = 0; Count < less_date_call.Count;Count++ )
             {
                  end_call_lenght = Identifier_length(memblock, memblock.Length, (int)less_date_call[Count].start);
                  _FileStream.Write(memblock, (int)less_date_call[Count].start , end_call_lenght);

             }


                 _FileStream.Write(endingbyte, 0, 2);

             _FileStream.Write(memblock, (int)audit_index, memblock.Length - (int)audit_index);

             _FileStream.Close();
             Change_seq_number(Result + "\\" + Split_filename, seqNo[0], Result + "\\" + Split_filename);
             functions.Audit audit1 = new functions.Audit();
             audit1 = functions.Calc_Audit(Result + "\\" + Split_filename, Result + "\\" + Split_filename);

             /// generate file that have greater then the compare date
             /// 
             seqNo[1] = String.Format("{0:D5}", int.Parse(seqNo[1]));
             
             Split_filename = Split_filename.Substring(0, 12) + seqNo[1];

            _FileStream = new System.IO.FileStream(Result + "\\" + Split_filename, System.IO.FileMode.Create,
                                     System.IO.FileAccess.Write);
             _FileStream.Write(memblock, 0, call_value_index);
             end_call_lenght = 0;
             for (int Count = 0; Count < greater_date_call.Count; Count++)
             {
                 end_call_lenght = Identifier_length(memblock, memblock.Length, (int)greater_date_call[Count].start);
                 _FileStream.Write(memblock, (int)greater_date_call[Count].start, end_call_lenght);

             }


             _FileStream.Write(endingbyte, 0, 2);

             _FileStream.Write(memblock, (int)audit_index, memblock.Length - (int)audit_index);

             _FileStream.Close();
             Change_seq_number(Result + "\\" + Split_filename, seqNo[0], Result + "\\" + Split_filename);
              audit1 = new functions.Audit();
             audit1 = functions.Calc_Audit(Result + "\\" + Split_filename, Result + "\\" + Split_filename);


             stopwatch.Stop();

         }

        public static void Filter_Records(string Filename, string Result, bool[] Events_filtered)
        {
            
            long call_index = 0;//index of first call
            long audit_index = 0;//index of audit identifier

            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack


            //int search_iden = indentefiers[indentifier_index].cidentifier;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            List<write_inden> L_Filtered_call = new List<write_inden>();
            write_inden Filtered_call;




            Filtered_call.start = 0;
            Filtered_call.end = 0;
            Filtered_call.lenght = 0;
            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden z;
            indefinded_inden pear;

            long start = 0;

            byte[] memblock = File.ReadAllBytes(Filename);

            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator
                string f = "*"; // string indentifer value
                byte indlen;//length of identifier
                int indlenV;//value of identifier


                //unsigned char mask;
                //p = start;
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {
                            //    Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                            cards.Pop();
                            goto check_again;
                        }
                    }
                }
                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {
                        // Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                        cards.Pop();
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }
                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                            //for (int b=1; b<indlen;b++)
                            //{
                            //cout<<"long identifer loop" <<endl;
                            //indlenV=indlenV << 8;
                            //start++;
                            //indlenV |=block[start];
                            // cin >> p;				
                            //}
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }
                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {
                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }
                        }
                        else
                        {
                            lenV = -1;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        if (lenght > 0)
                            lenV = lenght;
                        else// if lenght =0 
                            lenV = -2;
                    }

                    if (indlenV == 9 && Events_filtered[0] == true || 
                        indlenV == 10 && Events_filtered[1] == true ||
                        indlenV == 11 && Events_filtered[2] == true || 
                        indlenV == 12  && Events_filtered[3]==true||
                        indlenV == 14 && Events_filtered[4] == true ||
                        indlenV == 17 && Events_filtered[5] == true ||
                        indlenV == 297 && Events_filtered[6] == true ||
                        indlenV == 433 && Events_filtered[7] == true ||
                        indlenV == 434 && Events_filtered[8] == true)//MobileOriginatedCall
                                                                    //MobileTerminatedCall
                                                                    //SupplServiceEvent
                                                                    //ServiceCentreUsage
                                                                    //GprsCall
                                                                    //ContentTransaction
                                                                    //LocationService
                                                                    //MessagingEvent
                                                                    //MobileSession

                    {

                        Filtered_call.start = iden_start;
                        Filtered_call.end = start;
                        L_Filtered_call.Add(Filtered_call);
                    }
                    if (indlenV == 3)
                        call_index = iden_start;
                    if (indlenV == 15)
                        audit_index = iden_start;

                    mask = 0x20;
                    mask &= iden;
                    if (mask == 0x20)
                    {
                        //parent



                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;

                        if (lenV > 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0)
                        {

                            pear = cards.Peek();
                            z.key = indlenV.ToString();
                            if (lenV != 0 && lenV != -2)
                            cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                            cards.Push(z);
                        }
                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;
                            byte[] valuelist = new byte[lenV];
                            Array.Copy(memblock, start, valuelist, 0, lenV);
                            start += lenV;

                        }
                        else if (lenV != -2)
                        {
                            List<byte> valuelist = new List<byte>(); ;

                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                valuelist.Add(memblock[start]);
                                start++;
                            }

                            start++;
                            start++;
                        }
                        else if (lenV == -2)
                            start++; // to skip the 0 lenght
                    }
                }
            }

       



            int call_value_index = valueINDEX_readindntifier(memblock, memblock.Length, (int)call_index);// getting the value INDEX
            int audit_length = valuesize_readindntifier(memblock, memblock.Length, (int)audit_index);// getting the lenght of audit 0x00 ending of identifiers is NOT included


            byte[] endingbyte = { 0x00, 0x00 };
            


            System.IO.FileStream _FileStream =
                   new System.IO.FileStream(Result , System.IO.FileMode.Create,
                                   System.IO.FileAccess.Write);
            _FileStream.Write(memblock, 0, call_value_index);
            int end_call_lenght = 0;
            for (int Count = 0; Count < L_Filtered_call.Count; Count++)
            {
                end_call_lenght = Identifier_length(memblock, memblock.Length, (int)L_Filtered_call[Count].start);
                _FileStream.Write(memblock, (int)L_Filtered_call[Count].start, end_call_lenght);

            }


            _FileStream.Write(endingbyte, 0, 2);

            _FileStream.Write(memblock, (int)audit_index, memblock.Length - (int)audit_index);

            _FileStream.Close();
           
            functions.Audit audit1 = new functions.Audit();
            audit1 = functions.Calc_Audit(Result, Result);

           
            stopwatch.Stop();

        }

   
        public static TreeNode OpenFile_tree(string Filename, TreeView Tree,BackgroundWorker BW)
        {
            List<indefinded_inden> Calls = new List<indefinded_inden> () ;

            int TransferBatch_index;
            int BatchControlInfo_index;
            int AccountingInfo_index;
            int NetworkInfo_index;
            int CallEventDetailList_index;
            int AuditControlInfo_index;
            
           

            byte[] memblock = File.ReadAllBytes(Filename);

           
           TreeNode treeNode = new TreeNode();
          
            treeNode.ImageIndex=0;
            
            TransferBatch_index = List_of_indntifier(memblock, memblock.Length, 0, 1);
            if (TransferBatch_index >= 0)
            {
                //TreeNode treeNodedoc = new TreeNode("TransferBatch");
                treeNode.Text = "TransferBatch";
                //Tree.Nodes.Add("TransferBatch");
                BatchControlInfo_index = List_of_indntifier(memblock, memblock.Length, TransferBatch_index, 4);

                if (BatchControlInfo_index > 0)
                {
                    treeNode.Nodes.Add(BatchControlInfo_index.ToString(), "BatchControlInfo", 2, 2);
                    treeNode.Nodes[0].Tag = BatchControlInfo_index;
                    AccountingInfo_index = List_of_indntifier(memblock, memblock.Length, BatchControlInfo_index, 5);
                }
                else
                {
                    treeNode.Nodes.Add("BatchControlInfo <Missing>", "BatchControlInfo <Missing>", 2, 2);
                    AccountingInfo_index = List_of_indntifier(memblock, memblock.Length, 0, 5);
                }


                if (AccountingInfo_index > 0)
                {
                    treeNode.Nodes.Add(AccountingInfo_index.ToString(), "AccountingInfo", 2, 2);
                    treeNode.Nodes[1].Tag = AccountingInfo_index;
                    NetworkInfo_index = List_of_indntifier(memblock, memblock.Length, AccountingInfo_index, 6);
                }
                else
                {
                    treeNode.Nodes.Add("AccountingInfo <Missing>", "AccountingInfo <Missing>", 2, 2);
                    NetworkInfo_index = List_of_indntifier(memblock, memblock.Length, 0, 6);
                }

                if (NetworkInfo_index > 0)
                {
                    treeNode.Nodes.Add(NetworkInfo_index.ToString(), "NetworkInfo", 2, 2);
                    treeNode.Nodes[2].Tag = NetworkInfo_index;
                    CallEventDetailList_index = List_of_indntifier(memblock, memblock.Length, NetworkInfo_index, 3);
                }
                else
                {
                    treeNode.Nodes.Add("NetworkInfo  <Missing>", "NetworkInfo  <Missing>", 2, 2);
                    CallEventDetailList_index = List_of_indntifier(memblock, memblock.Length, 0, 3);
                }

                if (CallEventDetailList_index > 0)
                {
                    // main f = new main();
                    // f.backgroundWorker1.ReportProgress(30);
                    BW.ReportProgress(2);
                    treeNode.Nodes.Add("CallEventDetailList", "CallEventDetailList", 0, 0);
                    Calls = Get_calls(memblock, CallEventDetailList_index);
                    BW.ReportProgress(75);

                    if (Calls.Count < 5000)
                        for (int count = 0; count < Calls.Count; count++)
                        {
                            int imageindex = 0;
                            if (Calls[count].key == "MobileOriginatedCall")
                                imageindex = 3;
                            else if (Calls[count].key == "MobileTerminatedCall")
                                imageindex = 4;
                            else if (Calls[count].key == "GprsCall")
                                imageindex = 5;
                            else
                                imageindex = 6;
                            treeNode.Nodes[3].Nodes.Add(Calls[count].identifier.ToString(), Calls[count].key + "(" + (count + 1).ToString() + ")", imageindex, imageindex);
                            treeNode.Nodes[3].Nodes[count].Tag = Calls[count].start;
                        }
                    else
                    {

                        int part = (int)Math.Ceiling((double)Calls.Count / 5000);
                        int callcounts = (int)Math.Ceiling((double)Calls.Count / part);
                        for (int partcount = 1; partcount <= part - 1; partcount++)
                        {
                            treeNode.Nodes[3].Nodes.Add(partcount.ToString(), "CallEventDetails [" + Convert.ToString(5000 * (partcount - 1)) + "-" + Convert.ToString(5000 * partcount) + "]", 0, 0);
                            int inside_folder_count = 0;
                            for (int count = 5000 * (partcount - 1); count < 5000 * partcount; count++)
                            {
                                int imageindex1 = 0;
                                if (Calls[count].key == "MobileOriginatedCall")
                                    imageindex1 = 3;
                                else if (Calls[count].key == "MobileTerminatedCall")
                                    imageindex1 = 4;
                                else if (Calls[count].key == "GprsCall")
                                    imageindex1 = 5;
                                else
                                    imageindex1 = 6;
                                treeNode.Nodes[3].Nodes[partcount - 1].Nodes.Add(Calls[count].identifier.ToString(), Calls[count].key + "(" + (count + 1).ToString() + ")", imageindex1, imageindex1);
                                treeNode.Nodes[3].Nodes[partcount - 1].Nodes[inside_folder_count].Tag = Calls[count].start;
                                inside_folder_count++;
                            }
                            inside_folder_count = 0;
                        }
                        treeNode.Nodes[3].Nodes.Add(part.ToString(), "CallEventDetails [" + Convert.ToString(5000 * (part - 1)) + "-" + Convert.ToString(Calls.Count) + "]", 0);
                        int inside_End_folder_count = 0;
                        for (int count = 5000 * (part - 1); count < Calls.Count; count++)
                        {
                            int imageindex1 = 0;
                            if (Calls[count].key == "MobileOriginatedCall")
                                imageindex1 = 3;
                            else if (Calls[count].key == "MobileTerminatedCall")
                                imageindex1 = 4;
                            else if (Calls[count].key == "GprsCall")
                                imageindex1 = 5;
                            else
                                imageindex1 = 6;
                            treeNode.Nodes[3].Nodes[part - 1].Nodes.Add(Calls[count].identifier.ToString(), Calls[count].key + "(" + (count + 1).ToString() + ")", imageindex1, imageindex1);
                            treeNode.Nodes[3].Nodes[part - 1].Nodes[inside_End_folder_count].Tag = Calls[count].start;
                            inside_End_folder_count++;
                        }
                    }
                    if (Calls.Count > 0)
                        AuditControlInfo_index = List_of_indntifier(memblock, memblock.Length, (int)Calls[Calls.Count - 1].start, 15);
                    else
                        AuditControlInfo_index = List_of_indntifier(memblock, memblock.Length, CallEventDetailList_index, 15);
                }
                else
                {
                    treeNode.Nodes.Add("CallEventDetailList  <Missing>");
                    AuditControlInfo_index = List_of_indntifier(memblock, memblock.Length, 0, 15);

                }

                if (AuditControlInfo_index > 0)
                {
                    treeNode.Nodes.Add("AuditControlInfo", "AuditControlInfo", 2, 2);
                    treeNode.Nodes[4].Tag = AuditControlInfo_index;
                }
                else
                    treeNode.Nodes.Add("AuditControlInfo  <Missing>", "AuditControlInfo  <Missing>", 2, 2);
                BW.ReportProgress(100);
                //Tree.Nodes.Add(treeNode);
                //return "done";
                return treeNode;
            }
            else
            {
                int Notification_index=List_of_indntifier(memblock, memblock.Length, 0, 2);
                if (Notification_index >= 0)
                {
                    treeNode.Text = "Notification";
                    treeNode.Tag = Notification_index;
                    return treeNode;
                }
                else
                    return treeNode;
            }
            // return "is file is Not TAP3 file or Notification file";
            
        }

        public static List<indefinded_inden> Get_calls(Byte[] memblock, int CallEventList_index)
        {
         

            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack


            //int search_iden = indentefiers[indentifier_index].cidentifier;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            List<indefinded_inden> CallEvents = new List<indefinded_inden>();
            indefinded_inden Call;



            
            Call.start = 0;
            Call.end = 0;
            Call.key = "";
            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden z;
            indefinded_inden pear;

            long start = 0;

            

            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator
                string f = "*"; // string indentifer value
                byte indlen;//length of identifier
                int indlenV;//value of identifier


                //unsigned char mask;
                //p = start;
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {
                            //    Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                            cards.Pop();
                            if (z.identifier == 3)
                                return CallEvents;
                            goto check_again;
                        }
                    }
                }
                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {
                        // Tap3.SelectedNode = Tap3.SelectedNode.Parent;
                        cards.Pop();
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }
                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                            //for (int b=1; b<indlen;b++)
                            //{
                            //cout<<"long identifer loop" <<endl;
                            //indlenV=indlenV << 8;
                            //start++;
                            //indlenV |=block[start];
                            // cin >> p;				
                            //}
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }


                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {
                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }
                        }
                        else
                        {
                            lenV = -1;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        if (lenght > 0)
                            lenV = lenght;
                        else// if lenght =0 
                            lenV = -2;
                           

                    }

                    if (indlenV == 9  ||
                        indlenV == 10 ||
                        indlenV == 11 ||
                        indlenV == 12 ||
                        indlenV == 14 ||
                        indlenV == 17 ||
                        indlenV == 297||
                        indlenV == 433||
                        indlenV == 434)//MobileOriginatedCall
                    //MobileTerminatedCall
                    //SupplServiceEvent
                    //ServiceCentreUsage
                    //GprsCall
                    //ContentTransaction
                    //LocationService
                    //MessagingEvent
                    //MobileSession
                    {

                        Call.start = iden_start;
                       
                        Call.identifier = indlenV;
                        if (indlenV==9)
                            Call.key = "MobileOriginatedCall";
                        if (indlenV == 10)
                            Call.key = "MobileTerminatedCall";
                        if (indlenV == 11)
                            Call.key = "SupplServiceEvent";
                        if (indlenV == 12)
                            Call.key = "ServiceCentreUsage";
                        if (indlenV == 14)
                            Call.key = "GprsCall";
                        if (indlenV == 17)
                            Call.key = "ContentTransaction";
                        if (indlenV == 297)
                            Call.key = "LocationService";
                        if (indlenV == 433)
                            Call.key = "MessagingEvent";
                        if (indlenV == 434)
                            Call.key = "MobileSession";

                        CallEvents.Add(Call);
                    }
                  

                    mask = 0x20;
                    mask &= iden;
                    if (mask == 0x20)
                    {
                        //parent



                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;

                        if (lenV > 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0)
                        {

                            pear = cards.Peek();
                            z.key = indlenV.ToString();
                            if (lenV!=0 && lenV!=-2)
                                 cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                            cards.Push(z);
                        }
                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;
                            byte[] valuelist = new byte[lenV];
                            Array.Copy(memblock, start, valuelist, 0, lenV);
                            start += lenV;

                        }
                        else if (lenV != -2)
                        {
                            List<byte> valuelist = new List<byte>(); ;

                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                valuelist.Add(memblock[start]);
                                start++;
                            }

                            start++;
                            start++;
                        }
                        else if (lenV == -2)
                            start++; // to skip the 0 lenght
                    }
                }
            }
            return CallEvents;
        }

        public struct indefinded_TREE_name
        {
            public string cidentifier { get; private set; }
            public int ckey { get; private set; }
            public byte ctype { get; private set; }
            public byte cmin { get; private set; }
            public byte cmax { get; private set; }
            public indefinded_TREE_name(string identifier, int key, byte type, byte min, byte max)
                : this()
            {
                cidentifier = identifier;
                ckey = key;
                ctype = type;
                cmin = min;
                cmax = max;
            }
        };
        public struct indefinded_TREE
        {
            public string identifier;
            public int key;
            public byte type;
            public byte min;
            public byte max;
        };

        public static indefinded_TREE_name[] indentefiers_Tree = new indefinded_TREE_name[] { new indefinded_TREE_name("TransferBatch", 1, 0, 0, 0), new indefinded_TREE_name("Notification", 2, 0, 0, 0), new indefinded_TREE_name("CallEventDetailList", 3, 0, 0, 0), new indefinded_TREE_name("BatchControlInfo", 4, 0, 0, 0), new indefinded_TREE_name("AccountingInfo", 5, 0, 0, 0), new indefinded_TREE_name("NetworkInfo", 6, 0, 0, 0), new indefinded_TREE_name("MessageDescriptionInfoList", 8, 0, 0, 0), new indefinded_TREE_name("MobileOriginatedCall", 9, 0, 0, 0), new indefinded_TREE_name("MobileTerminatedCall", 10, 0, 0, 0), new indefinded_TREE_name("SupplServiceEvent", 11, 0, 0, 0), new indefinded_TREE_name("ServiceCentreUsage", 12, 0, 0, 0), new indefinded_TREE_name("GprsCall", 14, 0, 0, 0), new indefinded_TREE_name("AuditControlInfo", 15, 0, 0, 0), new indefinded_TREE_name("LocalTimeStamp", 16, 1, 14, 0), new indefinded_TREE_name("ContentTransaction", 17, 0, 0, 0), new indefinded_TREE_name("BasicService", 36, 0, 0, 0), new indefinded_TREE_name("BasicServiceCodeList", 37, 0, 0, 0), new indefinded_TREE_name("BasicServiceUsedList", 38, 0, 0, 0), new indefinded_TREE_name("BasicServiceUsed", 39, 0, 0, 0), new indefinded_TREE_name("BearerServiceCode", 40, 1, 2, 0), new indefinded_TREE_name("CallOriginator", 41, 0, 0, 0), new indefinded_TREE_name("CalledPlace", 42, 1, 0, 0), new indefinded_TREE_name("CallEventDetailsCount", 43, 3, 0, 0), new indefinded_TREE_name("CallEventStartTimeStamp", 44, 0, 0, 0), new indefinded_TREE_name("CallReference", 45, 2, 1, 8), new indefinded_TREE_name("CalledRegion", 46, 1, 0, 0), new indefinded_TREE_name("CamelServiceKey", 55, 3, 0, 0), new indefinded_TREE_name("CamelServiceLevel", 56, 3, 0, 0), new indefinded_TREE_name("CamelServiceUsed", 57, 0, 0, 0), new indefinded_TREE_name("CauseForTerm", 58, 3, 0, 0), new indefinded_TREE_name("CellId", 59, 3, 0, 0), new indefinded_TREE_name("Charge", 62, 3, 0, 0), new indefinded_TREE_name("ChargeDetail", 63, 0, 0, 0), new indefinded_TREE_name("ChargeDetailList", 64, 0, 0, 0), new indefinded_TREE_name("ChargeableUnits", 65, 3, 0, 0), new indefinded_TREE_name("ChargedItem", 66, 1, 1, 0), new indefinded_TREE_name("ChargedPartyStatus", 67, 3, 0, 0), new indefinded_TREE_name("ChargedUnits", 68, 3, 0, 0), new indefinded_TREE_name("ChargeInformation", 69, 0, 0, 0), new indefinded_TREE_name("ChargeInformationList", 70, 0, 0, 0), new indefinded_TREE_name("ChargeType", 71, 1, 2, 3), new indefinded_TREE_name("ChargingId", 72, 3, 0, 0), new indefinded_TREE_name("ChargingPoint", 73, 1, 1, 0), new indefinded_TREE_name("ChargingTimeStamp", 74, 0, 0, 0), new indefinded_TREE_name("ClirIndicator", 75, 3, 0, 0), new indefinded_TREE_name("CompletionTimeStamp", 76, 0, 0, 0), new indefinded_TREE_name("CseInformation", 79, 1, 1, 40), new indefinded_TREE_name("CurrencyConversionList", 80, 0, 0, 0), new indefinded_TREE_name("DefaultCallHandlingIndicator", 87, 3, 0, 0), new indefinded_TREE_name("DepositTimeStamp", 88, 0, 0, 0), new indefinded_TREE_name("Destination", 89, 0, 0, 0), new indefinded_TREE_name("DestinationNetwork", 90, 1, 1, 6), new indefinded_TREE_name("DiscountCode", 91, 3, 0, 0), new indefinded_TREE_name("DiscountRate", 92, 3, 0, 0), new indefinded_TREE_name("DiscountingList", 95, 0, 0, 0), new indefinded_TREE_name("DiscountInformation", 96, 0, 0, 0), new indefinded_TREE_name("DistanceChargeBandCode", 98, 1, 1, 0), new indefinded_TREE_name("EarliestCallTimeStamp", 101, 0, 0, 0), new indefinded_TREE_name("Esn", 103, 1, 0, 0), new indefinded_TREE_name("ExchangeRate", 104, 3, 0, 0), new indefinded_TREE_name("ExchangeRateCode", 105, 3, 0, 0), new indefinded_TREE_name("FileAvailableTimeStamp", 107, 0, 0, 0), new indefinded_TREE_name("FileCreationTimeStamp", 108, 0, 0, 0), new indefinded_TREE_name("FileSequenceNumber", 109, 1, 5, 0), new indefinded_TREE_name("FileTypeIndicator", 110, 1, 1, 0), new indefinded_TREE_name("Fnur", 111, 3, 0, 0), new indefinded_TREE_name("GeographicalLocation", 113, 0, 0, 0), new indefinded_TREE_name("GprsBasicCallInformation", 114, 0, 0, 0), new indefinded_TREE_name("GprsChargeableSubscriber", 115, 0, 0, 0), new indefinded_TREE_name("GprsDestination", 116, 0, 0, 0), new indefinded_TREE_name("GprsLocationInformation", 117, 0, 0, 0), new indefinded_TREE_name("GprsNetworkLocation", 118, 0, 0, 0), new indefinded_TREE_name("GprsServiceUsed", 121, 0, 0, 0), new indefinded_TREE_name("HomeBid", 122, 1, 5, 0), new indefinded_TREE_name("HomeLocationInformation", 123, 0, 0, 0), new indefinded_TREE_name("Imei", 128, 2, 7, 8), new indefinded_TREE_name("Imsi", 129, 2, 3, 8), new indefinded_TREE_name("LatestCallTimeStamp", 133, 0, 0, 0), new indefinded_TREE_name("LocalCurrency", 135, 1, 0, 0), new indefinded_TREE_name("LocationArea", 136, 3, 0, 0), new indefinded_TREE_name("LocationInformation", 138, 0, 0, 0), new indefinded_TREE_name("MessageDescriptionCode", 141, 3, 0, 0), new indefinded_TREE_name("MessageDescription", 142, 1, 0, 0), new indefinded_TREE_name("MessageStatus", 144, 3, 0, 0), new indefinded_TREE_name("MessageType", 145, 3, 0, 0), new indefinded_TREE_name("Min", 146, 1, 2, 15), new indefinded_TREE_name("basicCallInformation", 147, 0, 0, 0), new indefinded_TREE_name("Msisdn", 152, 2, 1, 9), new indefinded_TREE_name("basicCallInformation", 153, 0, 0, 0), new indefinded_TREE_name("NetworkLocation", 156, 0, 0, 0), new indefinded_TREE_name("NumberOfDecimalPlaces", 159, 3, 0, 0), new indefinded_TREE_name("OperatorSpecInfoList", 162, 0, 0, 0), new indefinded_TREE_name("OperatorSpecInformation", 163, 1, 0, 0), new indefinded_TREE_name("OriginatingNetwork", 164, 1, 1, 6), new indefinded_TREE_name("PacketDataProtocolAddress", 165, 1, 0, 0), new indefinded_TREE_name("PartialTypeIndicator", 166, 1, 1, 0), new indefinded_TREE_name("PdpAddress", 167, 1, 0, 0), new indefinded_TREE_name("PriorityCode", 170, 3, 0, 0), new indefinded_TREE_name("RapFileSequenceNumber", 181, 1, 5, 0), new indefinded_TREE_name("Recipient", 182, 1, 5, 0), new indefinded_TREE_name("RecEntityCode", 184, 3, 0, 0), new indefinded_TREE_name("recEntity", 185, 0, 0, 0), new indefinded_TREE_name("RecEntityType", 186, 3, 0, 0), new indefinded_TREE_name("RecEntityInfoList", 188, 0, 0, 0), new indefinded_TREE_name("ReleaseVersionNumber", 189, 3, 0, 0), new indefinded_TREE_name("basicInformation", 191, 0, 0, 0), new indefinded_TREE_name("ScuChargeType", 192, 0, 0, 0), new indefinded_TREE_name("ScuTimeStamps", 193, 0, 0, 0), new indefinded_TREE_name("ServingNetwork", 195, 1, 0, 0), new indefinded_TREE_name("Sender", 196, 1, 5, 0), new indefinded_TREE_name("ServingBid", 198, 1, 0, 0), new indefinded_TREE_name("SimChargeableSubscriber", 199, 0, 0, 0), new indefinded_TREE_name("SimToolkitIndicator", 200, 1, 1, 0), new indefinded_TREE_name("SpecificationVersionNumber", 201, 3, 0, 0), new indefinded_TREE_name("SsParameters", 204, 1, 1, 40), new indefinded_TREE_name("SupplServiceUsed", 206, 0, 0, 0), new indefinded_TREE_name("SupplServiceActionCode", 208, 3, 0, 0), new indefinded_TREE_name("SupplServiceCode", 209, 1, 2, 0), new indefinded_TREE_name("TapCurrency", 210, 1, 0, 0), new indefinded_TREE_name("TaxationList", 211, 0, 0, 0), new indefinded_TREE_name("TaxCode", 212, 3, 0, 0), new indefinded_TREE_name("TaxInformation", 213, 0, 0, 0), new indefinded_TREE_name("TaxInformationList", 214, 0, 0, 0), new indefinded_TREE_name("TaxRate", 215, 1, 7, 0), new indefinded_TREE_name("TaxType", 217, 1, 2, 0), new indefinded_TREE_name("TeleServiceCode", 218, 1, 2, 0), new indefinded_TREE_name("ThirdPartyInformation", 219, 0, 0, 0), new indefinded_TREE_name("TotalCallEventDuration", 223, 3, 0, 0), new indefinded_TREE_name("TotalDiscountValue", 225, 3, 0, 0), new indefinded_TREE_name("TotalTaxValue", 226, 3, 0, 0), new indefinded_TREE_name("TransferCutOffTimeStamp", 227, 0, 0, 0), new indefinded_TREE_name("TransparencyIndicator", 228, 3, 0, 0), new indefinded_TREE_name("UtcTimeOffset", 231, 1, 5, 0), new indefinded_TREE_name("UtcTimeOffsetCode", 232, 3, 0, 0), new indefinded_TREE_name("UtcTimeOffsetInfoList", 234, 0, 0, 0), new indefinded_TREE_name("TapDecimalPlaces", 244, 3, 0, 0), new indefinded_TREE_name("NetworkInitPDPContext", 245, 3, 0, 0), new indefinded_TREE_name("DataVolumeIncoming", 250, 3, 0, 0), new indefinded_TREE_name("DataVolumeOutgoing", 251, 3, 0, 0), new indefinded_TREE_name("Mdn", 253, 1, 0, 0), new indefinded_TREE_name("MinChargeableSubscriber", 254, 0, 0, 0), new indefinded_TREE_name("CallTypeLevel2", 255, 3, 0, 0), new indefinded_TREE_name("CallTypeLevel3", 256, 3, 0, 0), new indefinded_TREE_name("CallTypeGroup", 258, 0, 0, 0), new indefinded_TREE_name("CallTypeLevel1", 259, 3, 0, 0), new indefinded_TREE_name("PDPContextStartTimestamp", 260, 0, 0, 0), new indefinded_TREE_name("AccessPointNameNI", 261, 1, 1, 63), new indefinded_TREE_name("AccessPointNameOI", 262, 1, 2, 37), new indefinded_TREE_name("DialledDigits", 279, 1, 0, 0), new indefinded_TREE_name("UserProtocolIndicator", 280, 3, 0, 0), new indefinded_TREE_name("ObjectType", 281, 3, 0, 0), new indefinded_TREE_name("contentServiceUsed", 285, 0, 0, 0), new indefinded_TREE_name("GsmChargeableSubscriber", 286, 0, 0, 0), new indefinded_TREE_name("ChargedPartyIdentifier", 287, 1, 0, 0), new indefinded_TREE_name("HomeIdentifier", 288, 1, 0, 0), new indefinded_TREE_name("LocationIdentifier", 289, 1, 0, 0), new indefinded_TREE_name("EquipmentId", 290, 1, 0, 0), new indefinded_TREE_name("ContentProviderIdType", 291, 3, 0, 0), new indefinded_TREE_name("ContentProviderIdentifier", 292, 1, 0, 0), new indefinded_TREE_name("IspIdType", 293, 3, 0, 0), new indefinded_TREE_name("IspIdentifier", 294, 1, 0, 0), new indefinded_TREE_name("NetworkIdentifier", 295, 1, 0, 0), new indefinded_TREE_name("LocationService", 297, 0, 0, 0), new indefinded_TREE_name("TrackingCustomerInformation", 298, 0, 0, 0), new indefinded_TREE_name("TrackingCustomerIdList", 299, 0, 0, 0), new indefinded_TREE_name("OrderPlacedTimeStamp", 300, 0, 0, 0), new indefinded_TREE_name("RequestedDeliveryTimeStamp", 301, 0, 0, 0), new indefinded_TREE_name("ActualDeliveryTimeStamp", 302, 0, 0, 0), new indefinded_TREE_name("TransactionStatus", 303, 3, 0, 0), new indefinded_TREE_name("ContentTransactionBasicInfo", 304, 0, 0, 0), new indefinded_TREE_name("ChargedPartyIdType", 305, 3, 0, 0), new indefinded_TREE_name("ChargedPartyIdentification", 309, 0, 0, 0), new indefinded_TREE_name("ChargedPartyIdList", 310, 0, 0, 0), new indefinded_TREE_name("HomeIdType", 311, 3, 0, 0), new indefinded_TREE_name("ChargedPartyHomeIdentification", 313, 0, 0, 0), new indefinded_TREE_name("ChargedPartyHomeIdList", 314, 0, 0, 0), new indefinded_TREE_name("LocationIdType", 315, 3, 0, 0), new indefinded_TREE_name("ChargedPartyLocation", 320, 0, 0, 0), new indefinded_TREE_name("ChargedPartyLocationList", 321, 0, 0, 0), new indefinded_TREE_name("EquipmentIdType", 322, 3, 0, 0), new indefinded_TREE_name("ChargedPartyEquipment", 323, 0, 0, 0), new indefinded_TREE_name("ChargedPartyInformation", 324, 0, 0, 0), new indefinded_TREE_name("ContentProvider", 327, 0, 0, 0), new indefinded_TREE_name("ContentProviderIdList", 328, 0, 0, 0), new indefinded_TREE_name("InternetServiceProvider", 329, 0, 0, 0), new indefinded_TREE_name("InternetServiceProviderIdList", 330, 0, 0, 0), new indefinded_TREE_name("NetworkIdType", 331, 3, 0, 0), new indefinded_TREE_name("Network", 332, 0, 0, 0), new indefinded_TREE_name("NetworkList", 333, 0, 0, 0), new indefinded_TREE_name("ContentProviderName", 334, 1, 0, 0), new indefinded_TREE_name("ServingPartiesInformation", 335, 0, 0, 0), new indefinded_TREE_name("ContentTransactionCode", 336, 3, 0, 0), new indefinded_TREE_name("ContentTransactionType", 337, 3, 0, 0), new indefinded_TREE_name("TransactionDescriptionSupp", 338, 3, 0, 0), new indefinded_TREE_name("TransactionDetailDescription", 339, 1, 0, 0), new indefinded_TREE_name("TransactionShortDescription", 340, 1, 0, 0), new indefinded_TREE_name("TransactionIdentifier", 341, 1, 0, 0), new indefinded_TREE_name("TransactionAuthCode", 342, 1, 0, 0), new indefinded_TREE_name("TotalDataVolume", 343, 3, 0, 0), new indefinded_TREE_name("ChargeRefundIndicator", 344, 3, 0, 0), new indefinded_TREE_name("ContentChargingPoint", 345, 3, 0, 0), new indefinded_TREE_name("PaidIndicator", 346, 3, 0, 0), new indefinded_TREE_name("PaymentMethod", 347, 3, 0, 0), new indefinded_TREE_name("AdvisedChargeCurrency", 348, 1, 0, 0), new indefinded_TREE_name("Commission", 350, 3, 0, 0), new indefinded_TREE_name("AdvisedChargeInformation", 351, 0, 0, 0), new indefinded_TREE_name("ContentServiceUsed", 352, 0, 0, 0), new indefinded_TREE_name("TotalTaxRefund", 353, 3, 0, 0), new indefinded_TREE_name("TotalDiscountRefund", 354, 3, 0, 0), new indefinded_TREE_name("TotalChargeRefund", 355, 3, 0, 0), new indefinded_TREE_name("AdvisedCharge", 356, 3, 0, 0), new indefinded_TREE_name("TotalAdvisedChargeRefund", 357, 3, 0, 0), new indefinded_TREE_name("TotalCommission", 358, 3, 0, 0), new indefinded_TREE_name("TotalCommissionRefund", 359, 3, 0, 0), new indefinded_TREE_name("TotalAdvisedChargeValue", 360, 0, 0, 0), new indefinded_TREE_name("TotalAdvisedChargeValueList", 361, 0, 0, 0), new indefinded_TREE_name("TrackingCustomerIdentification", 362, 0, 0, 0), new indefinded_TREE_name("CustomerIdType", 363, 3, 0, 0), new indefinded_TREE_name("CustomerIdentifier", 364, 1, 0, 0), new indefinded_TREE_name("TrackingCustomerHomeIdList", 365, 0, 0, 0), new indefinded_TREE_name("TrackingCustomerHomeId", 366, 0, 0, 0), new indefinded_TREE_name("TrackedCustomerInformation", 367, 0, 0, 0), new indefinded_TREE_name("TrackingCustomerLocList", 368, 0, 0, 0), new indefinded_TREE_name("TrackingCustomerLocation", 369, 0, 0, 0), new indefinded_TREE_name("TrackedCustomerIdList", 370, 0, 0, 0), new indefinded_TREE_name("TrackingCustomerEquipment", 371, 0, 0, 0), new indefinded_TREE_name("TrackedCustomerIdentification", 372, 0, 0, 0), new indefinded_TREE_name("LCSSPInformation", 373, 0, 0, 0), new indefinded_TREE_name("LCSSPIdentificationList", 374, 0, 0, 0), new indefinded_TREE_name("LCSSPIdentification", 375, 0, 0, 0), new indefinded_TREE_name("TrackedCustomerHomeIdList", 376, 0, 0, 0), new indefinded_TREE_name("TrackedCustomerHomeId", 377, 0, 0, 0), new indefinded_TREE_name("ISPList", 378, 0, 0, 0), new indefinded_TREE_name("TrackedCustomerLocList", 379, 0, 0, 0), new indefinded_TREE_name("TrackedCustomerLocation", 380, 0, 0, 0), new indefinded_TREE_name("TrackedCustomerEquipment", 381, 0, 0, 0), new indefinded_TREE_name("LocationServiceUsage", 382, 0, 0, 0), new indefinded_TREE_name("LCSQosRequested", 383, 0, 0, 0), new indefinded_TREE_name("LCSRequestTimestamp", 384, 0, 0, 0), new indefinded_TREE_name("HorizontalAccuracyRequested", 385, 3, 0, 0), new indefinded_TREE_name("VerticalAccuracyRequested", 386, 3, 0, 0), new indefinded_TREE_name("ResponseTimeCategory", 387, 3, 0, 0), new indefinded_TREE_name("TrackingPeriod", 388, 3, 0, 0), new indefinded_TREE_name("TrackingFrequency", 389, 3, 0, 0), new indefinded_TREE_name("TaxValue", 397, 3, 0, 0), new indefinded_TREE_name("TaxableAmount", 398, 3, 0, 0), new indefinded_TREE_name("RecEntityId", 400, 1, 0, 0), new indefinded_TREE_name("NonChargedNumber", 402, 1, 0, 0), new indefinded_TREE_name("ThirdPartyNumber", 403, 2, 0, 0), new indefinded_TREE_name("CamelDestinationNumber", 404, 2, 0, 0), new indefinded_TREE_name("CallingNumber", 405, 2, 0, 0), new indefinded_TREE_name("CalledNumber", 407, 2, 0, 0), new indefinded_TREE_name("ChargeDetailTimeStamp", 410, 0, 0, 0), new indefinded_TREE_name("FixedDiscountValue", 411, 3, 0, 0), new indefinded_TREE_name("Discount", 412, 3, 0, 0), new indefinded_TREE_name("HomeLocationDescription", 413, 1, 0, 0), new indefinded_TREE_name("ServingLocationDescription", 414, 1, 0, 0), new indefinded_TREE_name("TotalCharge", 415, 3, 0, 0), new indefinded_TREE_name("NetworkAccessIdentifier", 417, 1, 0, 0), new indefinded_TREE_name("IMSSignallingContext", 418, 3, 0, 0), new indefinded_TREE_name("SMSDestinationNumber", 419, 1, 0, 0), new indefinded_TREE_name("GuaranteedBitRate", 420, 1, 0, 0), new indefinded_TREE_name("MaximumBitRate", 421, 1, 0, 0), new indefinded_TREE_name("CamelInvocationFee", 422, 3, 0, 0), new indefinded_TREE_name("DiscountableAmount", 423, 3, 0, 0), new indefinded_TREE_name("HSCSDIndicator", 424, 1, 1, 0), new indefinded_TREE_name("SMSOriginator", 425, 1, 0, 0), new indefinded_TREE_name("BasicServiceCode", 426, 0, 0, 0), new indefinded_TREE_name("ChargeableSubscriber", 427, 0, 0, 0), new indefinded_TREE_name("DiscountApplied", 428, 0, 0, 0), new indefinded_TREE_name("equipmentIdentifier", 429, 0, 0, 0), new indefinded_TREE_name("chargeableSubscriber", 430, 0, 0, 0), new indefinded_TREE_name("ThreeGcamelDestination", 431, 0, 0, 0), new indefinded_TREE_name("TaxIndicator", 432, 1, 1, 0), new indefinded_TREE_name("MessagingEvent", 433, 0, 0, 0), new indefinded_TREE_name("MobileSession", 434, 0, 0, 0), new indefinded_TREE_name("EventReference", 435, 1, 0, 0), new indefinded_TREE_name("ElementType", 438, 3, 0, 0), new indefinded_TREE_name("MessagingEventService", 439, 3, 0, 0), new indefinded_TREE_name("MobileSessionService", 440, 3, 0, 0), new indefinded_TREE_name("NetworkElement", 441, 0, 0, 0), new indefinded_TREE_name("NetworkElementList", 442, 0, 0, 0), new indefinded_TREE_name("ChargedParty", 443, 0, 0, 0), new indefinded_TREE_name("NonChargedPartyNumber", 444, 2, 0, 0), new indefinded_TREE_name("NonChargedPublicUserId", 445, 1, 0, 0), new indefinded_TREE_name("ServiceStartTimestamp", 447, 0, 0, 0), new indefinded_TREE_name("SessionChargeInfoList", 448, 0, 0, 0), new indefinded_TREE_name("SessionChargeInformation", 449, 0, 0, 0), new indefinded_TREE_name("TotalTransactionDuration", 416 / 223, 3, 0, 0), new indefinded_TREE_name("FileSequenceNumber", 109, 0, 0, 0), new indefinded_TREE_name("Taxation", 216, 0, 0, 0), new indefinded_TREE_name("Discounting", 94, 0, 0, 0), new indefinded_TREE_name("CurrencyConversion", 106, 0, 0, 0), new indefinded_TREE_name("UtcTimeOffsetInfo", 233, 0, 0, 0), new indefinded_TREE_name("RecEntityInformation", 183, 0, 0, 0), new indefinded_TREE_name("MessageDescriptionInformation", 143, 0, 0, 0), new indefinded_TREE_name("BasicServiceUsed", 39, 0, 0, 0), new indefinded_TREE_name("PacketDataProtocolAddress", 165, 0, 0, 0) };
        public static int[] indefinder_SEQUENCE = new int[] { 8, 37, 38, 64, 314, 310, 321, 70, 328, 285, 80, 95, 330, 378, 333, 374, 442, 162, 185, 188, 448, 211, 214, 361, 376, 370, 379, 365, 299, 368, 234 };
       
        public static int Open_identifier_tree(string Filename,int index ,TreeView Tree)
        {
             
            int Identifier = 0;
            int identifier_text_index=0;

            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack
            indefinded_inden z;
            
            int Lindex = 0;
            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden pear;

            long start = index;

            byte[] memblock = File.ReadAllBytes(Filename);

            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator

                byte indlen;//length of identifier
                int indlenV=0;//value of identifier

               

                //unsigned char mask;
                //p = start;
            check_again2:
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {
                            
                            Tree.SelectedNode.Tag +=  ","+ start.ToString();
                            Tree.SelectedNode = Tree.SelectedNode.Parent;
                            
                            if (z.identifier == Identifier)
                            {
                                return (int)start;
                            }
                            cards.Pop();
                            goto check_again;
                        }
                    }
                }
            
                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {
                        Tree.SelectedNode.Tag += "," + start.ToString();
                        Tree.SelectedNode = Tree.SelectedNode.Parent;
                        if (z.identifier == Identifier)
                        {
                            return (int)start;
                        }
                        cards.Pop();
                       
                        goto check_again2;
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }
                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                            //for (int b=1; b<indlen;b++)
                            //{
                            //cout<<"long identifer loop" <<endl;
                            //indlenV=indlenV << 8;
                            //start++;
                            //indlenV |=block[start];
                            // cin >> p;				
                            //}
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }



                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {
                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }
                        }
                        else
                        {
                            lenV = -1;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        if (lenght > 0)
                            lenV = lenght;
                        else// if lenght =0 
                            lenV = -2;
                    }
                    mask = 0x20;
                    mask &= iden;

                    if (Identifier == 0)
                        Identifier = indlenV;

                    if (mask == 0x20)
                    {
                        //parent



                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;
                        if (lenV >= 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0)
                        {
                            string identifier_text = indlenV.ToString();
                            pear = cards.Peek();
                           
                            for (int Count = 0; Count < indentefiers_Tree.Length; Count++)
                            {
                                if (indlenV == indentefiers_Tree[Count].ckey)
                                {
                                    identifier_text = indentefiers_Tree[Count].cidentifier;
                                    break;
                                }
                            }

                            z.key = indlenV.ToString() +Tree.SelectedNode.GetNodeCount(false);
                          
                            Tree.SelectedNode.Nodes.Add(z.key, identifier_text,1,1);

                            if (lenV == 0)
                                Tree.SelectedNode.Nodes[Tree.SelectedNode.Nodes.Count - 1].Tag = indlenV.ToString() + "," + iden_start.ToString() + "," + z.end;
                            //Tree.SelectedNode = Tree.SelectedNode.Nodes[z.key];

                            if (lenV != 0 && lenV != -2)
                            {
                                Tree.SelectedNode = Tree.SelectedNode.Nodes[z.key];

                                if (z.end > 0)
                                    Tree.SelectedNode.Tag = indlenV.ToString() + "," + iden_start.ToString() + "," + z.end;
                                else
                                    Tree.SelectedNode.Tag = indlenV.ToString() + "," + iden_start.ToString();
                                
                                cards.Push(z);
                            }
                        }
                        else
                        {
                            string identifier_text = indlenV.ToString();
                          

                            for (int Count = 0; Count < indentefiers_Tree.Length; Count++)
                            {
                                if (indlenV == indentefiers_Tree[Count].ckey)
                                {
                                    identifier_text = indentefiers_Tree[Count].cidentifier;
                                    break;
                                }
                            }
                            z.key = "root";
                            Tree.Nodes.Add("root", identifier_text,1,1);
                            Tree.SelectedNode = Tree.Nodes["root"];
                            if (z.end > 0)
                                Tree.SelectedNode.Tag = indlenV.ToString() + "," + iden_start.ToString() + "," + z.end;
                            else
                                Tree.SelectedNode.Tag = indlenV.ToString() + "," + iden_start.ToString();

                            cards.Push(z);
                        }
                    }

                    else
                    {
                        //child
                        //pear = cards.Peek();
                        Lindex++;
                        string identifier_text = indlenV.ToString();
                        

                        for (int Count = 0; Count < indentefiers_Tree.Length; Count++)
                        {
                            if (indlenV == indentefiers_Tree[Count].ckey)
                            {
                                identifier_text = indentefiers_Tree[Count].cidentifier;
                                identifier_text_index = Count;
                                break;
                            }
                        }

                        
                        if (lenV > 0)
                        {
                            start++;
                            byte[] valuelist = new byte[lenV];
                            Array.Copy(memblock, start, valuelist, 0, lenV);
                            start += lenV;
                            Tree.SelectedNode.Nodes.Add(indlenV.ToString() + Tree.SelectedNode.GetNodeCount(false).ToString(), identifier_text + ":        " + Convert_iden(valuelist, indentefiers_Tree[identifier_text_index].ctype).ToString(),6,6);
                            Tree.SelectedNode.Nodes[Tree.SelectedNode.Nodes.Count - 1].Tag = indlenV.ToString() + "," + iden_start.ToString() + "," + start.ToString();

                        }
                        else if (lenV != -2)
                        {
                            List<byte> valuelist = new List<byte>(); ;
                            

                            for (int Count = 0; Count < indentefiers_Tree.Length; Count++)
                            {
                                if (indlenV == indentefiers_Tree[Count].ckey)
                                {
                                    identifier_text = indentefiers_Tree[Count].cidentifier;
                                    identifier_text_index = Count;
                                    break;
                                }
                            }

                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                valuelist.Add(memblock[start]);
                                start++;
                            }
                            Tree.SelectedNode.Nodes.Add(indlenV.ToString() + Tree.SelectedNode.GetNodeCount(false).ToString(), identifier_text + ":        " + Convert_iden(valuelist.ToArray(), indentefiers_Tree[identifier_text_index].ctype).ToString(),6,6);
                            Tree.SelectedNode.Nodes[Tree.SelectedNode.Nodes.Count-1].Tag = indlenV.ToString() + "," + iden_start.ToString()+"," + start.ToString();
                            start++;
                            start++;
                        }
                        else if (lenV == -2)
                        {
                            Tree.SelectedNode.Nodes.Add(indlenV.ToString() + Tree.SelectedNode.GetNodeCount(false).ToString(), identifier_text + ":        " , 6, 6);
                            Tree.SelectedNode.Nodes[Tree.SelectedNode.Nodes.Count - 1].Tag = indlenV.ToString() + "," + iden_start.ToString() + "," + start.ToString();
                            start++;
                        }
                    }
                }
                else
                {
                    Tree.Nodes.Clear();
                    Tree.Nodes.Add("Corrupted!");
                    return 0;
                }
            }
            Tree.Nodes.Clear();
            Tree.Nodes.Add("Corrupted!");
            return 0;
        }
        public struct Edited_iden
        {
           public int identifer;
           public int identifer_start;
           public int identifer_end;
           public string New_Value;
        }
        public struct Deleted_iden
        {
            public int identifer_start;
            public int identifer_end;
        }
        public struct inserted_iden
        {
            public int identifer;
            public string identifer_name;//string name of identifier
            public int identifer_start;
            public int identifer_end;
            public string New_Value;
            public bool Parent;
            public int Node_index;// index of the node before the missing identifier
        }
        public struct Changes
        {
            public int identifer;
            public int identifer_start;
            public int identifer_end;
            public string New_Value;
            public byte type;
            public bool Parent;
            public int Parent_Name;
            public int Node_index;

        }
        public static void Save_identifier_tree(string Filename, List<Changes> ChangeList, string result)
        {
            //List<Changes> Changes_list=new List<Changes>();
            //Changes Change;
            if (ChangeList.Count > 0)
            {
                /*if (Deleted_list.Count>0)
                {
                    for(int count=0;count<Edit_list.Count;count++)
                    {
                        for (int count1 = 0; count1 < Deleted_list.Count; count1++)
                        {
                            if (Edit_list[count].identifer_start >= Deleted_list[count1].identifer_start && Edit_list[count].identifer_end <= Deleted_list[count1].identifer_end)
                            {
                                Edit_list.RemoveAt(count);
                            }
                        }
                    }
                }
                
                Deleted_list = Deleted_list.OrderBy(x => x.identifer_start).ToList();
                if (Deleted_list.Count > 0)
                {
                    for (int count = 0; count < Deleted_list.Count; count++)
                    {
                        Change.identifer_start = Deleted_list[count].identifer_start;
                        Change.identifer_end = Deleted_list[count].identifer_end;
                        Change.type = 2;
                        Change.Index = count;
                        Changes_list.Add(Change);

                    }
                }
                if (Edit_list.Count > 0)
                {
                    for (int count = 0; count < Edit_list.Count; count++)
                    {
                        Change.identifer_start = Edit_list[count].identifer_start;
                        Change.identifer_end = Edit_list[count].identifer_end;
                        Change.type = 1;
                        Change.Index = count;
                        Changes_list.Add(Change);

                    }
                }*/
                int dublicate = 0;
                for (int count = 0; count < ChangeList.Count; count++) // keep the last edit value
                {
                    dublicate = ChangeList.FindLastIndex(x => x.identifer_start == ChangeList[count].identifer_start && x.identifer == ChangeList[count].identifer && x.Node_index == ChangeList[count].Node_index && x.type == ChangeList[count].type);
                    //dublicate = ChangeList.FindIndex(x => x.identifer == 16);
                    if (dublicate > 0)
                    {
                        if (dublicate > count)
                            ChangeList.RemoveAt(count);
                        else if (dublicate < count)
                            ChangeList.RemoveAt(dublicate);
                    }
                }


                for (int count = 0; count < ChangeList.Count; count++)
                {

                    for (int search = 0; search < ChangeList.Count; search++)
                    {
                        if (search > count && ChangeList[search].type == 2 && ChangeList[count].type == 1 && ChangeList[search].identifer_start == ChangeList[count].identifer_start)// change the inseted end_identifer if there is a deleted indentifer before
                        {
                            Changes tmp;
                            tmp = ChangeList[search];
                            tmp.identifer_end = ChangeList[count].identifer_end;
                            ChangeList.RemoveAt(search);
                            ChangeList.Insert(search, tmp);
                            ChangeList.RemoveAt(count);
                            search -= 2;
                        }

                    }

                }

                for (int count = 0; count < ChangeList.Count; count++)
                {

                    for (int search = 0; search < ChangeList.Count; search++)
                    {

                        if (search < count
                            && ChangeList[count].type == 1
                            && ChangeList[search].identifer_start == ChangeList[count].identifer_start
                            && ChangeList[search].identifer_end == ChangeList[count].identifer_end
                            && ChangeList[search].type == 0)// delete edited value
                        {

                            ChangeList.RemoveAt(search);
                            search--;
                            count--;
                        }
                    }

                }


                int Deleted = -1;// if there is deleted value for inserted value
                for (int count = 0; count < ChangeList.Count; count++)
                {
                    Deleted = -1;
                    for (int search = 0; search < ChangeList.Count; search++)
                    {
                        if (search < count
                            && ChangeList[search].identifer_start == ChangeList[count].identifer_start
                            && ChangeList[search].identifer_end == ChangeList[count].identifer_end
                            && ChangeList[count].type == 1
                            && ChangeList[search].type == 2)// delete edited value
                        {
                            ChangeList.RemoveAt(search);
                            search--;
                            count--;
                            Deleted = search;
                        }
                    }
                    if (Deleted > 0)
                        ChangeList.RemoveAt(Deleted);
                }

                byte type = 0;
                int start = 1;
                byte[] Edited_inden;
                byte[] memblock = File.ReadAllBytes(Filename);


                ChangeList = ChangeList.OrderBy(x => x.identifer_start).ToList();

                System.IO.FileStream _FileStream =
                   new System.IO.FileStream(result, System.IO.FileMode.Create,
                                   System.IO.FileAccess.Write);
                if (ChangeList[0].type == 0)//edit
                {
                    _FileStream.Write(memblock, 0, (int)ChangeList[0].identifer_start);
                    for (int count = 0; count < indentefiers_Tree.Length; count++) // get type of identifer
                    {
                        if (indentefiers_Tree[count].ckey == ChangeList[0].identifer)
                        {
                            type = indentefiers_Tree[count].ctype;
                            break;
                        }
                    }

                    Edited_inden = creatIDEN(ChangeList[0].identifer, ChangeList[0].New_Value, type);

                    _FileStream.Write(Edited_inden, 0, Edited_inden.Length);
                }
                if (ChangeList[0].type == 1)//delete
                    _FileStream.Write(memblock, 0, (int)ChangeList[0].identifer_start);

                if (ChangeList[0].type == 2)//insert
                {
                    //List<byte> insert_byte_list = new List<byte>();
                    _FileStream.Write(memblock, 0, (int)ChangeList[0].identifer_start);
                    start = 0;
                    byte[] insert = insert_Identifier(ChangeList, ref start);
                    _FileStream.Write(insert, 0, insert.Length);
                }
                long l;

                for (int k = start; k <= ChangeList.Count - 1; k++)
                {
                    if (ChangeList[k].type == 0)//edit
                    {
                        l = (int)ChangeList[k].identifer_start - ChangeList[k - 1].identifer_end;
                        _FileStream.Write(memblock, (int)ChangeList[k - 1].identifer_end, (int)l);

                        for (int count = 0; count < indentefiers_Tree.Length; count++)
                        {
                            if (indentefiers_Tree[count].ckey == ChangeList[k].identifer)
                            {
                                type = indentefiers_Tree[count].ctype;
                                break;
                            }
                        }

                        Edited_inden = creatIDEN(ChangeList[k].identifer, ChangeList[k].New_Value, type);

                        _FileStream.Write(Edited_inden, 0, Edited_inden.Length);
                    }


                    if (ChangeList[k].type == 1)//delete
                    {
                        l = (int)ChangeList[k].identifer_start - ChangeList[k - 1].identifer_end;
                        _FileStream.Write(memblock, (int)ChangeList[k - 1].identifer_end, (int)l);
                    }
                    if (ChangeList[k].type == 2)//insert
                    {
                        // List<byte> insert_byte_list = new List<byte>();
                        l = (int)ChangeList[k].identifer_start - ChangeList[k - 1].identifer_end;
                        _FileStream.Write(memblock, (int)ChangeList[k - 1].identifer_end, (int)l);
                        byte[] insert = insert_Identifier(ChangeList, ref k);
                        _FileStream.Write(insert, 0, insert.Length);
                    }

                }

                l = memblock.Length - (int)ChangeList[ChangeList.Count - 1].identifer_end;
                _FileStream.Write(memblock, (int)ChangeList[ChangeList.Count - 1].identifer_end, (int)l);
                _FileStream.Close();
            }
        }

        public static byte[] insert_Identifier(List<Changes> ChangeList,ref int Index)//,List<byte> inserted_Iden_byte)
        {
            int Iden_parent_start=0;
            List<byte> inserted_Iden_byte=new List<byte>();
            if (ChangeList[Index].Parent)
            {
                byte[] tmp = create_Parent_IDEN(ChangeList[Index].identifer);
                inserted_Iden_byte.AddRange(tmp);
                Iden_parent_start=Index;
                Index++;
                inserted_Iden_byte.AddRange( insert_Identifier(ChangeList, ref Index));
            chack_again:
                if (Index<ChangeList.Count)
                {
                    if (ChangeList[Index].type == 2 && ChangeList[Index].identifer_start == ChangeList[Iden_parent_start].identifer_start && ChangeList[Index].Parent_Name == ChangeList[Iden_parent_start].identifer)
                    {
                        
                        inserted_Iden_byte.AddRange(insert_Identifier(ChangeList, ref Index));
                        goto chack_again;
                    }
                }
                inserted_Iden_byte.Add(0x00);
                inserted_Iden_byte.Add(0x00);
                return inserted_Iden_byte.ToArray();
            }
            else
            {
                byte type=0;
                byte[] Edited_inden;
                for (int k = Index; k <= ChangeList.Count - 1; k++)
                {
                if (ChangeList[k].type==0 && ChangeList[k].identifer==ChangeList[Index].identifer && ChangeList[k].identifer_start==ChangeList[Index].identifer_start && ChangeList[k].Node_index==ChangeList[Index].Node_index)
                {
                    
                    for (int count = 0; count < indentefiers_Tree.Length; count++) // get type of identifer
                    {
                        if (indentefiers_Tree[count].ckey == ChangeList[k].identifer)
                        {
                            type = indentefiers_Tree[count].ctype;
                            break;
                        }
                    }

                    Edited_inden = creatIDEN(ChangeList[k].identifer, ChangeList[k].New_Value, type);
                    //inserted_Iden_byte.AddRange(Edited_inden);
                    //Index+=2;
                    ChangeList.RemoveAt(k);
                    Index++;
                    //return inserted_Iden_byte.ToArray();
                    return Edited_inden;
                }
                }
               
                   
                    for (int count = 0; count < indentefiers_Tree.Length; count++) // get type of identifer
                    {
                        if (indentefiers_Tree[count].ckey == ChangeList[Index + 1].identifer)
                        {
                            type = indentefiers_Tree[count].ctype;
                            break;
                        }
                    }

                    Edited_inden = creatIDEN(ChangeList[Index].identifer, "", type);
                    inserted_Iden_byte.AddRange(Edited_inden);
                    Index++;
                    return Edited_inden;
                
            }

        }

        public static string tree_to_text(TreeNode TreeNode,int parentnode)
        {
            string str="";
            //str = TreeNode.Text + "\r\n";
            foreach (TreeNode childNode in TreeNode.Nodes)
            {

                if (childNode.Nodes.Count > 0)
                {
                    for (int count = 1; count <= childNode.Level - parentnode; count++)
                        str += "\t";
                    str += childNode.Text + "\r\n";
                    string childStr;
                    childStr = tree_to_text(childNode, parentnode);
                   
                    str +=  childStr;
                }
                else
                {
                    for (int count = 1; count <= childNode.Level - parentnode; count++)
                        str += "\t";
                    str += childNode.Text + "\r\n";
                }
            }
            return str;
        }

        public static TreeNode Paste(TreeNode TreeNode, TreeNode ParentTreeNode, string paststring, ref int lineNumber, List<functions.Changes> ChangesList)
        {
            if (paststring.Substring(paststring.Length - 2, 2) == "\r\n")
                paststring = paststring.Substring(0, paststring.Length - 2);
            string[] clipBL = paststring.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
            if (ParentTreeNode.GetNodeCount(true) == clipBL.Length - 1)
            {
                if (ParentTreeNode.GetNodeCount(true)==0)
                { 
                        string[] clipstr = clipBL[0].Split(':');
                        string[] strNode = ParentTreeNode.Text.Split(':');
                        if (clipstr.Length == 2)
                        {
                            clipstr[0] = clipstr[0].Replace("\t", "");
                            if (strNode[0] == clipstr[0])
                            {
                                functions.Changes New_Edit;
                                ParentTreeNode.Text = clipstr[0] + ":        " + clipstr[1].Trim();
                                string Node_tag = ParentTreeNode.Tag.ToString();
                                string[] str = Node_tag.Split(',');
                                New_Edit.identifer = int.Parse(str[0]);
                                New_Edit.identifer_start = int.Parse(str[1]);
                                New_Edit.identifer_end = int.Parse(str[2]);
                                New_Edit.type = 0;
                                New_Edit.Parent = false;
                                New_Edit.Node_index = ParentTreeNode.Index;
                                New_Edit.Parent_Name = int.Parse(ParentTreeNode.Parent.Tag.ToString().Split(',')[0]);
                                New_Edit.New_Value = clipstr[1].Trim();
                                if (clipstr[1].Trim() != strNode[1].TrimStart())
                                    ChangesList.Add(New_Edit);
                                lineNumber++;
                            }
                        }
                        
                }
                foreach (TreeNode n in TreeNode.Nodes)
                {

                    if (n.Nodes.Count == 0)
                    {
                        string[] clipstr = clipBL[lineNumber].Split(':');
                        string[] strNode = n.Text.Split(':');
                        if (clipstr.Length == 2)
                        {
                            clipstr[0] = clipstr[0].Replace("\t", "");
                            if (strNode[0] == clipstr[0])
                            {
                                functions.Changes New_Edit;
                                n.Text = clipstr[0] + ":        " + clipstr[1].Trim();
                                string Node_tag = n.Tag.ToString();
                                string[] str = Node_tag.Split(',');
                                New_Edit.identifer = int.Parse(str[0]);
                                New_Edit.identifer_start = int.Parse(str[1]);
                                New_Edit.identifer_end = int.Parse(str[2]);
                                New_Edit.type = 0;
                                New_Edit.Parent = false;
                                New_Edit.Node_index = ParentTreeNode.Index;
                                if (ParentTreeNode.Parent != null)
                                    New_Edit.Parent_Name = int.Parse(ParentTreeNode.Parent.Tag.ToString().Split(',')[0]);
                                else
                                    New_Edit.Parent_Name = 0;
                                New_Edit.New_Value = clipstr[1].Trim();
                                if (clipstr[1].Trim() != strNode[1].TrimStart())
                                    ChangesList.Add(New_Edit);
                                lineNumber++;
                            }
                        }

                    }
                    else
                    {
                        clipBL[lineNumber] = clipBL[lineNumber].Replace("\t", "");
                        if (n.Text == clipBL[lineNumber])
                        {
                            lineNumber++;
                            Paste(n, ParentTreeNode, paststring, ref lineNumber, ChangesList);
                        }
                    }

                }
            }
            return TreeNode;
        }
        public static void Update_Insert_on_tree(TreeNode branch_node, List<Changes> ChangesList)
        {
            if (ChangesList.Count > 0)
            {
                for (int count = 0; count < ChangesList.Count; count++)//check insert
                {
                    string[] str1 = branch_node.Tag.ToString().Split(',');
                    if (ChangesList[count].type == 2 && ChangesList[count].identifer_start > int.Parse(str1[1]) && ChangesList[count].identifer_end < int.Parse(str1[2]) && ChangesList[count].Parent_Name == int.Parse(str1[0]))
                    {
                        string dont;
                        int tree_imageindex;
                        // change the icon
                        if (ChangesList[count].Parent)
                        {
                            dont = "";
                            tree_imageindex = 0;
                        }
                        else
                        {
                            dont = ":        ";
                            tree_imageindex = 6;
                        }

                        // get identifer text name
                        string identifier_text = "";
                        for (int Count = 0; Count < indentefiers_Tree.Length; Count++)
                        {
                            if (ChangesList[count].identifer == indentefiers_Tree[Count].ckey)
                            {
                                identifier_text = indentefiers_Tree[Count].cidentifier;
                                //identifier_text_index = Count;
                                break;
                            }
                        }
                        branch_node.Nodes.Insert(ChangesList[count].Node_index, identifier_text, identifier_text + dont, tree_imageindex, tree_imageindex);
                        branch_node.Nodes[ChangesList[count].Node_index].Tag = ChangesList[count].identifer.ToString() + "," + ChangesList[count].identifer_start + "," + ChangesList[count].identifer_end;
                        if (ChangesList[count].Parent)
                        {
                            for (int count_child = 0; count_child < ChangesList.Count; count_child++)
                            {
                                if (ChangesList[count_child].type == 2 && ChangesList[count_child].identifer_start == ChangesList[count].identifer_start && ChangesList[count_child].Parent_Name == ChangesList[count].identifer)
                                {
                                    for (int Count = 0; Count < indentefiers_Tree.Length; Count++)
                                    {
                                        if (ChangesList[count_child].identifer == indentefiers_Tree[Count].ckey)
                                        {
                                            identifier_text = indentefiers_Tree[Count].cidentifier;
                                            //identifier_text_index = Count;
                                            break;
                                        }
                                    }
                                    // change the icon
                                    if (ChangesList[count_child].Parent)
                                    {
                                        dont = "";
                                        tree_imageindex = 0;
                                    }
                                    else
                                    {
                                        dont = ":        ";
                                        tree_imageindex = 6;
                                    }
                                    branch_node.Nodes[ChangesList[count].Node_index].Nodes.Insert(ChangesList[count_child].Node_index, identifier_text, identifier_text + dont, tree_imageindex, tree_imageindex);
                                    branch_node.Nodes[ChangesList[count].Node_index].Nodes[ChangesList[count_child].Node_index].Tag = ChangesList[count_child].identifer.ToString() + "," + ChangesList[count_child].identifer_start + "," + ChangesList[count_child].identifer_end;

                                }
                            }
                        }
                    }
                }
            }
        }
        public static void Update_Changes_on_tree(TreeNode branch_node,List<Changes> ChangesList)
        {
            List<Changes> List = new List<Changes> (ChangesList);
            if (List.Count > 0)
            {
                foreach (TreeNode node in branch_node.Nodes)
                {
                    if (node != null)
                    {
                        if (node.Nodes.Count > 0)
                        {
                            Update_Changes_on_tree(node, List);
                        }

                        for (int count = 0; count < List.Count; count++)//check insert
                        {

                            string[] str = node.Tag.ToString().Split(',');
                            if (List[count].identifer.ToString() == str[0] && List[count].identifer_start.ToString() == str[1] && List[count].identifer_end.ToString() == str[2])
                            {
                                if (List[count].type == 1)
                                {
                                    node.Remove();
                                    List.RemoveAt(count);
                                    break;
                                }
                                if (List[count].type == 0)
                                {
                                    string[] nodename = node.Text.Split(':');
                                    node.Text = nodename[0] + ":        " + List[count].New_Value;
                                    List.RemoveAt(count);
                                    break;
                                }

                            }
                        }
                    }
                }
            }
        }

        public static bool Find(byte[] memblock, int indentifier, int index, string find, List<Changes> ChangesList)
        {
            
            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack
            byte indentifierType = 0;
            for (int Count = 0; Count < indentefiers_Tree.Length; Count++)
            {
                if (indentifier == indentefiers_Tree[Count].ckey)
                {
                    indentifierType = indentefiers_Tree[Count].ctype;
                    
                    break;
                }
            }

            int search_iden = indentifier;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();


            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden z;
            indefinded_inden pear;

            long start = index;
            
            int Identifier=0; //started identifier
                
            

            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator
                string f = "*"; // string indentifer value
                byte indlen;//length of identifier
                int indlenV;//value of identifier


                //unsigned char mask;
            //p = start;
            check_again2:
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {
                            

                            
                            if (z.identifier == Identifier)
                            {
                                return false;
                            }
                            cards.Pop();
                            goto check_again;
                        }
                    }
                }
            
                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {

                        if (z.identifier == Identifier)
                        {
                            return false;
                        }
                        cards.Pop();
                       
                        goto check_again2;
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }
                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                            //for (int b=1; b<indlen;b++)
                            //{
                            //cout<<"long identifer loop" <<endl;
                            //indlenV=indlenV << 8;
                            //start++;
                            //indlenV |=block[start];
                            // cin >> p;				
                            //}
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }
                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {
                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }
                        }
                        else
                        {
                            lenV = -1;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        if (lenght > 0)
                            lenV = lenght;
                        else// if lenght =0 
                            lenV = -2;
                    }
                    mask = 0x20;
                    mask &= iden;
					
					if (Identifier == 0)
                        Identifier = indlenV;
					
                    if (mask == 0x20)
                    {
                        //parent



                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;
                        if (lenV > 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0)
                        {

                            pear = cards.Peek();
                            z.key = indlenV.ToString();
                            if (lenV != 0 && lenV != -2)
                            cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                            
                                cards.Push(z);
                        }
                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;
                            byte[] valuelist = new byte[lenV];
                            Array.Copy(memblock, start, valuelist, 0, lenV);
                            start += lenV;
                            if (indlenV == search_iden)
                            {
                                f = Convert_iden(valuelist, indentifierType);
                            }
                        }
                        else if (lenV != -2)
                        {
                            List<byte> valuelist = new List<byte>(); ;

                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                valuelist.Add(memblock[start]);
                                start++;
                            }
                            if (indlenV == search_iden)
                            {
                                f = Convert_iden(valuelist.ToArray(), indentifierType);
                            }
                            start++;
                            start++;
                        }
                        else if (lenV == -2)
                            start++; // to skip the 0 lenght


                        if (indlenV == search_iden)
                        {

                            if (f == find)
                            {
                                for (int count = 0; count < ChangesList.Count; count++)
                                {
                                    if (indlenV == ChangesList[count].identifer && iden_start == ChangesList[count].identifer_start && start == ChangesList[count].identifer_end)
                                        return false;
                                }
                                return true;
                            }
                            else
                            {
                                for (int count = 0; count < ChangesList.Count; count++)
                                {
                                    if (indlenV == ChangesList[count].identifer && iden_start == ChangesList[count].identifer_start && start == ChangesList[count].identifer_end && find == ChangesList[count].New_Value)
                                        return true;
                                }
                                return false;
                            }
                        }
                    }
                }
            }
            stopwatch.Stop();
            return false;

        }
        public static TreeNode[] Filter_by_identifier(TreeNode tree, int identifer, string value, string filename, List<Changes> ChangesList)
        {
            List<TreeNode> FoundedNode = new List<TreeNode>();
            
            byte[] memblock = File.ReadAllBytes(filename);
            foreach (TreeNode node in tree.Nodes)
            {
                if (node.Nodes.Count > 0)
                {
                   TreeNode[] tmp =Filter_by_identifier(node, identifer, value, filename,ChangesList);
                   node.Nodes.Clear();
                   node.Nodes.AddRange(tmp);
                }
                else
                {
                    if (node.Tag.ToString().IndexOf(',') > 0)
                    {
                        string[] str = node.Tag.ToString().Split(',');
                        if (Find(memblock, identifer, int.Parse(str[0]), value,ChangesList))
                        {
                            FoundedNode.Add(node);
                        }
                    }
                    else
                    {
                       
                        if (Find(memblock, identifer, int.Parse(node.Tag.ToString()), value,ChangesList))
                        {
                            FoundedNode.Add(node);
                        }
                    }
                  
                }
            }

            return FoundedNode.ToArray();
        }

        public static bool Find_greater_Less_than(byte[] memblock, int indentifier, int index, string find)
        {
            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack
            byte indentifierType = 3;
         
                   

      

            int search_iden = indentifier;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();


            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden z;
            indefinded_inden pear;

            long start = index;

            int Identifier = 0; //started identifier



            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator
                string f = "*"; // string indentifer value
                byte indlen;//length of identifier
                int indlenV;//value of identifier


                //unsigned char mask;
            //p = start;
            check_again2:
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {



                            if (z.identifier == Identifier)
                            {
                                return false;
                            }
                            cards.Pop();
                            goto check_again;
                        }
                    }
                }

                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {

                        if (z.identifier == Identifier)
                        {
                            return false;
                        }
                        cards.Pop();

                        goto check_again2;
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }
                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                            //for (int b=1; b<indlen;b++)
                            //{
                            //cout<<"long identifer loop" <<endl;
                            //indlenV=indlenV << 8;
                            //start++;
                            //indlenV |=block[start];
                            // cin >> p;				
                            //}
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }
                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {
                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }
                        }
                        else
                        {
                            lenV = -1;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        if (lenght > 0)
                            lenV = lenght;
                        else// if lenght =0 
                            lenV = -2;
                    }
                    mask = 0x20;
                    mask &= iden;

                    if (Identifier == 0)
                        Identifier = indlenV;

                    if (mask == 0x20)
                    {
                        //parent



                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;
                        if (lenV > 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0)
                        {

                            pear = cards.Peek();
                            z.key = indlenV.ToString();
                            if (lenV != 0 && lenV != -2)
                            cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                            
                                cards.Push(z);
                        }
                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;
                            byte[] valuelist = new byte[lenV];
                            Array.Copy(memblock, start, valuelist, 0, lenV);
                            start += lenV;
                            if (indlenV == search_iden)
                            {
                                f = Convert_iden(valuelist, indentifierType);
                            }
                        }
                        else if (lenV != -2)
                        {
                            List<byte> valuelist = new List<byte>(); ;

                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                valuelist.Add(memblock[start]);
                                start++;
                            }
                            if (indlenV == search_iden)
                            {
                                f = Convert_iden(valuelist.ToArray(), indentifierType);
                            }
                            start++;
                            start++;
                        }
                        else if (lenV == -2)
                            start++; // to skip the 0 lenght

                        if (indlenV == search_iden)
                        {
                            if (find.IndexOf('>')>-1)
                            {
                                int intfind = int.Parse(find.Substring(1, find.Length-1));
                                if (intfind > int.Parse(f))
                                    return true;
                                else
                                    return false;
                            }
                            if (find.IndexOf('<') > -1)
                            {
                                int intfind = int.Parse(find.Substring(1, find.Length-1));
                                if (intfind < int.Parse(f))
                                    return true;
                                else
                                    return false;
                            }
                        }
                    }
                }
            }
            stopwatch.Stop();
            return false;

        }
        public static TreeNode[] Filter_Greater_Less_duration(TreeNode tree, int identifer, string value, string filename, List<Changes> ChangesList)
        {
            List<TreeNode> FoundedNode = new List<TreeNode>();

            byte[] memblock = File.ReadAllBytes(filename);
            foreach (TreeNode node in tree.Nodes)
            {
                if (node.Nodes.Count > 0)
                {
                    TreeNode[] tmp = Filter_Greater_Less_duration(node, identifer, value, filename, ChangesList);
                    node.Nodes.Clear();
                    node.Nodes.AddRange(tmp);
                }
                else
                {
                    if (node.Tag.ToString().IndexOf(',') > 0)
                    {
                        string[] str = node.Tag.ToString().Split(',');
                        if (Find_greater_Less_than(memblock, identifer, int.Parse(str[0]), value))
                        {
                            FoundedNode.Add(node);
                        }
                    }
                    else
                    {

                        if (Find_greater_Less_than(memblock, identifer, int.Parse(node.Tag.ToString()), value))
                        {
                            FoundedNode.Add(node);
                        }
                    }

                }
            }

            return FoundedNode.ToArray();
        }

        public static bool Find_greater_Less_than(byte[] memblock, int indentifier, int index, byte[] find,byte Condetion)
        {
            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack
            byte indentifierType = 0;
            for (int Count = 0; Count < indentefiers_Tree.Length; Count++)
            {
                if (indentifier == indentefiers_Tree[Count].ckey)
                {
                    indentifierType = indentefiers_Tree[Count].ctype;

                    break;
                }
            }

            int search_iden = indentifier;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();


            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden z;
            indefinded_inden pear;

            long start = index;

            int Identifier = 0; //started identifier

            byte[] f= new byte[1];

            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator
                
                byte indlen;//length of identifier
                int indlenV;//value of identifier


            //unsigned char mask;
            //p = start;
            check_again2:
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {



                            if (z.identifier == Identifier)
                            {
                                return false;
                            }
                            cards.Pop();
                            goto check_again;
                        }
                    }
                }

                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {

                        if (z.identifier == Identifier)
                        {
                            return false;
                        }
                        cards.Pop();

                        goto check_again2;
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }
                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                            //for (int b=1; b<indlen;b++)
                            //{
                            //cout<<"long identifer loop" <<endl;
                            //indlenV=indlenV << 8;
                            //start++;
                            //indlenV |=block[start];
                            // cin >> p;				
                            //}
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }
                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {
                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }
                        }
                        else
                        {
                            lenV = -1;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        if (lenght > 0)
                            lenV = lenght;
                        else// if lenght =0 
                            lenV = -2;
                    }
                    mask = 0x20;
                    mask &= iden;

                    if (Identifier == 0)
                        Identifier = indlenV;

                    if (mask == 0x20)
                    {
                        //parent



                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;
                        if (lenV > 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0)
                        {

                            pear = cards.Peek();
                            z.key = indlenV.ToString();
                            if (lenV != 0 && lenV != -2)
                            cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                                cards.Push(z);
                        }
                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;
                            byte[] valuelist = new byte[lenV];
                            Array.Copy(memblock, start, valuelist, 0, lenV);
                            start += lenV;
                            if (indlenV == search_iden)
                            {
                                Array.Resize(ref f, lenV);
                               f =valuelist;
                            }
                        }
                        else if (lenV != -2)
                        {
                            List<byte> valuelist = new List<byte>(); ;

                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                valuelist.Add(memblock[start]);
                                start++;
                            }
                            if (indlenV == search_iden)
                            {
                                Array.Resize(ref f, valuelist.Count);
                                f = valuelist.ToArray();
                            }
                            start++;
                            start++;
                        }
                        else if (lenV == -2)
                            start++; // to skip the 0 lenght

                        if (indlenV == search_iden)
                        {
                            if (Condetion == 1)
                            {
                                int n = Memory_compare(f, find);
                                if (n > 0)
                                    return true;
                                else
                                    return false;
                            }
                            if (Condetion == 2)
                            {
                                int n = Memory_compare(f, find);
                                if (n < 0)
                                    return true;
                                else
                                    return false;
                            }
                            if (Condetion == 3)
                            {
                                bool great = false;
                                bool less = false;
                                int n = Memory_compare(f, find);
                                
                                if (n > 0)
                                    great = true;
                                else
                                    great = false;
                                
                                
                                if (n > 0)
                                    less = true;
                                else
                                    less = false;
                                
                                
                                if (great && less)
                                    return true;
                                else
                                    return false;
                            }
                        }
                    }
                }
            }
            stopwatch.Stop();
            return false;

        }
        public static TreeNode[] Filter_Greater_Less_timeStamp(TreeNode tree, int identifer, string value, string filename, byte type)
        {
            List<TreeNode> FoundedNode = new List<TreeNode>();

            byte[] memblock = File.ReadAllBytes(filename);
            foreach (TreeNode node in tree.Nodes)
            {
                if (node.Nodes.Count > 0)
                {
                    TreeNode[] tmp = Filter_Greater_Less_timeStamp(node, identifer, value, filename, type);
                    node.Nodes.Clear();
                    node.Nodes.AddRange(tmp);
                }
                else
                {
                    byte[] array = Encoding.ASCII.GetBytes(value);
                    if (node.Tag.ToString().IndexOf(',') > 0)
                    {
                        string[] str = node.Tag.ToString().Split(',');
                        
                        if (Find_greater_Less_than(memblock, identifer, int.Parse(str[0]), array, type))
                        {
                            FoundedNode.Add(node);
                        }
                    }
                    else
                    {

                        if (Find_greater_Less_than(memblock, identifer, int.Parse(node.Tag.ToString()), array, type))
                        {
                            FoundedNode.Add(node);
                        }
                    }

                }
            }

            return FoundedNode.ToArray();
        }

        public static bool Find_Between(byte[] memblock, int indentifier, int index, byte[] Value1, byte[] Value2)
        {
            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack
            byte indentifierType = 0;
            for (int Count = 0; Count < indentefiers_Tree.Length; Count++)
            {
                if (indentifier == indentefiers_Tree[Count].ckey)
                {
                    indentifierType = indentefiers_Tree[Count].ctype;

                    break;
                }
            }

            int search_iden = indentifier;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();


            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden z;
            indefinded_inden pear;

            long start = index;

            int Identifier = 0; //started identifier

            byte[] f = new byte[1];

            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator

                byte indlen;//length of identifier
                int indlenV;//value of identifier


            //unsigned char mask;
            //p = start;
            check_again2:
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {



                            if (z.identifier == Identifier)
                            {
                                return false;
                            }
                            cards.Pop();
                            goto check_again;
                        }
                    }
                }

                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {

                        if (z.identifier == Identifier)
                        {
                            return false;
                        }
                        cards.Pop();

                        goto check_again2;
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }
                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                            //for (int b=1; b<indlen;b++)
                            //{
                            //cout<<"long identifer loop" <<endl;
                            //indlenV=indlenV << 8;
                            //start++;
                            //indlenV |=block[start];
                            // cin >> p;				
                            //}
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }
                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {
                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }
                        }
                        else
                        {
                            lenV = -1;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        if (lenght > 0)
                            lenV = lenght;
                        else// if lenght =0 
                            lenV = -2;
                    }
                    mask = 0x20;
                    mask &= iden;

                    if (Identifier == 0)
                        Identifier = indlenV;

                    if (mask == 0x20)
                    {
                        //parent



                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;
                        if (lenV > 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0)
                        {

                            pear = cards.Peek();
                            z.key = indlenV.ToString();
                            if (lenV != 0 && lenV != -2)
                            cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                            
                                cards.Push(z);
                        }
                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;
                            byte[] valuelist = new byte[lenV];
                            Array.Copy(memblock, start, valuelist, 0, lenV);
                            start += lenV;
                            if (indlenV == search_iden)
                            {
                                Array.Resize(ref f, lenV);
                                f = valuelist;
                            }
                        }
                        else if (lenV != -2)
                        {
                            List<byte> valuelist = new List<byte>(); ;

                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                valuelist.Add(memblock[start]);
                                start++;
                            }
                            if (indlenV == search_iden)
                            {
                                Array.Resize(ref f, valuelist.Count);
                                f = valuelist.ToArray();
                            }
                            start++;
                            start++;
                        }
                        else if (lenV == -2)
                            start++; // to skip the 0 lenght

                        if (indlenV == search_iden)
                        {

                            bool great = false;
                            bool less = false;
                            int n = Memory_compare(f, Value1);

                            if (n > 0)
                                great = true;
                            else
                                great = false;

                            n = Memory_compare(f, Value2);
                            if (n < 0)
                                less = true;
                            else
                                less = false;


                            if (great && less)
                                return true;
                            else
                                return false;

                        }
                    }
                }
            }
            stopwatch.Stop();
            return false;

        }
        public static TreeNode[] Filter_Between_timeStamp(TreeNode tree, int identifer, string value1, string value2, string filename)
        {
            List<TreeNode> FoundedNode = new List<TreeNode>();

            byte[] memblock = File.ReadAllBytes(filename);
            foreach (TreeNode node in tree.Nodes)
            {
                if (node.Nodes.Count > 0)
                {
                    TreeNode[] tmp = Filter_Between_timeStamp(node, identifer, value1, value2, filename);
                    node.Nodes.Clear();
                    node.Nodes.AddRange(tmp);
                }
                else
                {
                    byte[] array1 = Encoding.ASCII.GetBytes(value1);
                    byte[] array2 = Encoding.ASCII.GetBytes(value2);
                    if (node.Tag.ToString().IndexOf(',') > 0)
                    {
                        string[] str = node.Tag.ToString().Split(',');

                        if (Find_Between(memblock, identifer, int.Parse(str[0]), array1, array2))
                        {
                            FoundedNode.Add(node);
                        }
                    }
                    else
                    {

                        if (Find_Between(memblock, identifer, int.Parse(node.Tag.ToString()), array1, array2))
                        {
                            FoundedNode.Add(node);
                        }
                    }

                }
            }

            return FoundedNode.ToArray();
        }


        public static TreeNode[] Find_by_identifier(TreeNode tree, int identifer, string value, string filename,List<Changes> ChangesList)
        {
            List<TreeNode> FoundedNode = new List<TreeNode>();

            byte[] memblock = File.ReadAllBytes(filename);
            foreach (TreeNode node in tree.Nodes)
            {
                if (node.Nodes.Count > 0)
                {
                    TreeNode[] tmp = Find_by_identifier(node, identifer, value, filename,ChangesList);
                    FoundedNode.AddRange(tmp);
                }
                else
                {
                    if (node.Tag.ToString().IndexOf(',') > 0)
                    {
                        string[] str = node.Tag.ToString().Split(',');
                        if (Find_start(memblock, identifer, int.Parse(str[0]), value, ChangesList))
                        {
                            FoundedNode.Add(node);
                        }
                    }
                    else
                    {

                        if (Find(memblock, identifer, int.Parse(node.Tag.ToString()), value,ChangesList))
                        {
                            FoundedNode.Add(node);
                        }
                    }

                }
            }

            return FoundedNode.ToArray();
        }

        public static bool Find_start(byte[] memblock, int indentifier, int index, string find, List<Changes> ChangesList)
        {
            // chack if the value can be found in change list


            Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack
            byte indentifierType = 0;
            for (int Count = 0; Count < indentefiers_Tree.Length; Count++)
            {
                if (indentifier == indentefiers_Tree[Count].ckey)
                {
                    indentifierType = indentefiers_Tree[Count].ctype;
                    
                    break;
                }
            }

            int search_iden = indentifier;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();


            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
            byte mask;

            indefinded_inden z;
            indefinded_inden pear;

            long start = index;
            
            int Identifier=0; //started identifier
                
            

            while (start < memblock.Length)
            {
                int o;
                byte iden = 0; //identifier
                long iden_start = 0; //identifier
                int lenV = 0;//lenght value
                int lenght = 0; // lenght of lenght value
                bool par;// paranert indecator
                string f = "*"; // string indentifer value
                byte indlen;//length of identifier
                int indlenV;//value of identifier


                //unsigned char mask;
            //p = start;
            check_again2:
                iden = memblock[start];
                if (cards.Count > 0)
                {
                check_again:
                    if (cards.Count > 0)
                    {
                        z = cards.Peek();
                        if (z.end == start)
                        {
                            

                            
                            if (z.identifier == Identifier)
                            {

                                return false;
                            }
                            cards.Pop();
                            goto check_again;
                        }
                    }
                }
            
                if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
                {
                    start += 2;
                    z = cards.Peek();

                    if (z.end == 0)
                    {

                        if (z.identifier == Identifier)
                        {
                            return false;
                        }
                        cards.Pop();
                       
                        goto check_again2;
                    }
                    //{
                    //	std::bitset<16> r(z.start);
                    //	std::cout << "stack out " << z.start << endl;
                    //}


                }
                mask = 0x40;
                mask &= iden;
                if (mask == 0x40)
                {
                    iden_start = start;
                    mask = 0x1f;
                    mask &= iden;
                    if (mask == 0x1f)
                    {

                        start++;
                        indlen = memblock[start];
                        mask = 0x80;
                        mask &= indlen;
                        if (mask == 0x80)
                        {
                            // long lenght
                            indlen &= 0x7F;
                            indlenV = (byte)indlen;
                            indlenV = indlenV << 7;
                            start++;
                            indlenV |= (byte)memblock[start];
                            //for (int b=1; b<indlen;b++)
                            //{
                            //cout<<"long identifer loop" <<endl;
                            //indlenV=indlenV << 8;
                            //start++;
                            //indlenV |=block[start];
                            // cin >> p;				
                            //}
                        }
                        else
                        {
                            // short lenght
                            indlen &= 0x7F;
                            indlenV = indlen;
                        }
                    }
                    else
                    {

                        indlenV = (byte)iden & 0x1F;
                    }
                    start++;
                    lenght = memblock[start];
                    mask = 0x80;
                    mask &= (byte)lenght;

                    if (mask == 0x80)
                    {
                        mask = 0x7f;
                        mask &= (byte)lenght;
                        if (mask >= 0x01)
                        {
                            //long lenght
                            lenght &= 0x7F;
                            start++;
                            lenV = (byte)memblock[start];
                            for (int c = 1; c < lenght; c++)
                            {
                                lenV = lenV << 8;
                                start++;
                                lenV |= memblock[start];

                            }
                        }
                        else
                        {
                            lenV = -1;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        if (lenght > 0)
                            lenV = lenght;
                        else// if lenght =0 
                            lenV = -2;
                    }
                    mask = 0x20;
                    mask &= iden;
					
					if (Identifier == 0)
                        Identifier = indlenV;
					
                    if (mask == 0x20)
                    {
                        //parent



                        start++;
                        z.identifier = indlenV;
                        z.start = iden_start;
                        if (lenV > 0)
                            z.end = start + lenV;
                        else
                            z.end = 0;

                        if (cards.Count > 0)
                        {

                            pear = cards.Peek();
                            z.key = indlenV.ToString();
                            if (lenV != 0 && lenV != -2)
                            cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                            
                                cards.Push(z);
                        }
                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;
                            byte[] valuelist = new byte[lenV];
                            Array.Copy(memblock, start, valuelist, 0, lenV);
                            start += lenV;
                            if (indlenV == search_iden)
                            {
                                f = Convert_iden(valuelist, indentifierType);
                            }
                        }
                        else if (lenV != -2)
                        {
                            List<byte> valuelist = new List<byte>(); ;

                            while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
                            {
                                valuelist.Add(memblock[start]);
                                start++;
                            }
                            if (indlenV == search_iden)
                            {
                                for (int count = 0; count < ChangesList.Count; count++)
                                {
                                    if (indlenV == ChangesList[count].identifer && iden_start == ChangesList[count].identifer_start && start == ChangesList[count].identifer_end)
                                        return false;
                                }
                                f = Convert_iden(valuelist.ToArray(), indentifierType);
                            }
                            start++;
                            start++;
                        }
                        else if (lenV == -2)
                            start++; // to skip the 0 lenght

                        if (indlenV == search_iden)
                        {

                            if (f == find)
                            {
                                for (int count = 0; count < ChangesList.Count; count++)
                                {
                                    if (indlenV == ChangesList[count].identifer && iden_start == ChangesList[count].identifer_start && start == ChangesList[count].identifer_end)
                                        return false;
                                }
                                return true;
                            }
                            else
                            {
                                for (int count = 0; count < ChangesList.Count; count++)
                                {
                                    if (indlenV == ChangesList[count].identifer && iden_start == ChangesList[count].identifer_start && start == ChangesList[count].identifer_end && find ==ChangesList[count].New_Value)
                                        return true;
                                }
                                return false;
                            }
                        }
                    }
                }
            }
            stopwatch.Stop();
            return false;

        }
        public static TreeNode FindNameInTreeView(TreeNode node, String name)
        {
            // check if we have a node
            if (node == null)
                return null;

            // check if the name of this node matches the one we're looking for
            if (node.Text == name)
                return node;

            // iterate through the childs of this node
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                // call this same function to each child node
                TreeNode foundNode = FindNameInTreeView(node.Nodes[i], name);
                // if we found the node, return it
                if (foundNode != null)
                    return foundNode;
            }

            // no node found
            return null;
        }
    }

    
}

