using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Runtime.InteropServices;

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;

namespace BulkTap3
{
     
    public partial class Form1 : Form
    {
        [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 Identifier_length(byte[] block, int size, int index);
     //   [DllImport("Bulddll", CallingConvention = CallingConvention.StdCall)]
     //   public unsafe static extern long* Calc_Audit(byte[] block, int size, int index, byte[] Lastest_call, byte[] earliest_call);

        
        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int t;
          //  t = sum(10, 20);
          //  label1.Text = t.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bool ok;

            byte[] array = File.ReadAllBytes("d:\\tt");
            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);
             //byte[] a = ReturnIntArray(array, array.Length, ref index);
           // bool ok;
            // string test = System.Text.Encoding.Default.GetString(a);
            //textBox1.Text = test;

            char[] newvalue = {'2','0','1','4','0','3','2','6','1','0','1','8','5','3' };
            index = write_value_indntifier(array, array.Length, index, newvalue);
            //a = ReturnIntArray(array, array.Length, ref index);
           // textBox1.Text += " " + System.Text.Encoding.Default.GetString(a);

            ok = ByteArrayToFile("d:\\FAT1", array);
            /*b = new byte[3] { 0x5f, 0x81, 0x67 };
            
            index = index_of_indentifier(array, array.Length, b, b.Length, index);
            a = new byte[5];
            a = ReturnIntArray(array, array.Length, ref index);
            test = System.Text.Encoding.Default.GetString(a);
            textBox1.Text += " " + test;
            ok = ByteArrayToFile("d:\\FAT", array);
           // MessageBox.Show(index.ToString());
           // MessageBox.Show(t.ToString());*/
        }


        public static byte[] ReturnIntArray(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 bool 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;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            byte[] array = File.ReadAllBytes("d:\\aa");
            int index;
            index = List_of_indntifier(array, array.Length, 0, 0x63);
            MessageBox.Show(index.ToString());
        }


        public struct indefinded_inden
        {
         public int identifier;
	     public long start;
         public string key;
	     public long end; 
        };

        Stack<indefinded_inden> cards = new Stack<indefinded_inden>(); //stack
        indefinded_inden z;
        
        public class kData
        {   // instead of class you can use struct, but with some difficulties
            public int index;
            public int level;
            public string NodeText;

            //public stData()  { } // here you can initialize variables
        };
        List<kData> L; // instead of list you can use array, but I prefer Lists

        private void button4_Click(object sender, EventArgs e)
        {
            L = new List<kData>();
            int Lindex=0;
            //TreeView Tap3 = new TreeView();
            //TreeNode selected = new TreeNode();
	        byte mask;
	        indefinded_inden z;
            indefinded_inden pear;
            
            long start = 0;

            byte[] memblock = File.ReadAllBytes("d:\\70\\CDJORFLAREDU03220.bak");

	        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;
		        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 == -1)
                    {
                       // 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;
				        lenV = lenght;
			        }
                    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 = -1;

                        if (cards.Count > 0)
                        {
                            
                            pear = cards.Peek();
                            z.key = indlenV.ToString();// +Tap3.SelectedNode.GetNodeCount(false);
                            //Tap3.SelectedNode.Nodes.Add(z.key, indlenV.ToString());
                            //Tap3.SelectedNode = Tap3.SelectedNode.Nodes[z.key];
                            Lindex++;
                            L.Add(new kData());
                            L[Lindex].index = Lindex;
                            L[Lindex].level = cards.Count;
                            L[Lindex].NodeText = z.key;
                            if (lenV!=0)
                            cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                            //Tap3.Nodes.Add("root", indlenV.ToString());
                            //Tap3.SelectedNode = Tap3.Nodes["root"];
                            L.Add(new kData());
                            L[Lindex].index = Lindex;
                            L[Lindex].level = 0;
                            L[Lindex].NodeText = z.key;

                            cards.Push(z);
                        }
			        }
			        else
			        {
				        //child
                        //pear = cards.Peek();
                        Lindex++;
                        L.Add(new kData());
                        L[Lindex].index = Lindex;
                        L[Lindex].level = cards.Count;
                        L[Lindex].NodeText = indlenV.ToString();

                        //Tap3.SelectedNode.Nodes.Add(indlenV.ToString() + Tap3.SelectedNode.GetNodeCount(false).ToString(), indlenV.ToString());
				        if (lenV >0)
				        {
					        start++;
					        //pmemvalue = &memblock[start];
					        //memcpy (pvalue, pmemvalue, lenV * sizeof(char));
					        //pvalue += lenV + 1;
					        start += lenV;
                            
				        }
				        else
				        {
					        while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
					        {
						        start++;
					        }
					        start++;
					        start++;
				        }
			        }
		        }
	        }
	        //return -1;
            //treeView1.Nodes.Add(Tap3.Nodes["root"]);
            //Tap3.Visible = true;
            //populateBaseNodes(Treeview1);
        }



        public void populateBaseNodes(TreeView tvData)
        {
            int i;
            tvData.Nodes.Clear();
            tvData.BeginUpdate();

            for (i = 0; i < L.Count(); i++)
            {
                if (L[i].level == 0)
                {
                    tvData.Nodes.Add(L[i].NodeText, L[i].NodeText);
                    tvData.Nodes[tvData.Nodes.Count - 1].Tag = L[i];
                }
            }

            for (i = 0; i < tvData.Nodes.Count; i++)
               populateChilds(tvData.Nodes[i]);

            tvData.EndUpdate();
            tvData.Refresh();
        }
        int count = 0;
        public void populateChilds(TreeNode parentNode)
        {
            kData parentRed = (kData)parentNode.Tag;

            /*if (parentRed.NodeText == "99")
            {
                int y=0;
                int i = parentRed.index + 1;
                while(L[i].level >= (parentRed.level + 1))
                {
                    if (L[i].level ==2 ) y++;
                    if (listBox1.Items.Count < 5000) listBox1.Items.Add(L[i].NodeText);
                    i++;
                }
                MessageBox.Show(y.ToString());
                
            }
            */
            
            for (int i = parentRed.index + 1; i < L.Count; i++)
            {
                if (parentRed.NodeText == "3")
                    count++;
                if (parentRed.NodeText == "3" && count <= 10)
                {
                    
                    if (L[i].level == (parentRed.level + 1))
                    {
                        parentNode.Nodes.Add(L[i].NodeText, L[i].NodeText);
                        parentNode.Nodes[parentNode.Nodes.Count - 1].Tag = L[i];

                        populateChilds(parentNode.Nodes[parentNode.Nodes.Count - 1]);
                    }
                }
                if (parentRed.NodeText != "3" )
                {
                    if (L[i].level == (parentRed.level + 1))
                    {
                        parentNode.Nodes.Add(L[i].NodeText, L[i].NodeText);
                        parentNode.Nodes[parentNode.Nodes.Count - 1].Tag = L[i];

                        populateChilds(parentNode.Nodes[parentNode.Nodes.Count - 1]);
                    }
                } 
                if (L[i].level <= parentRed.level) break;
            }
           
        }


        private void button5_Click(object sender, EventArgs e)
        {
            
            populateBaseNodes(Treeview1);
            MessageBox.Show(count.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;
               }
        };
        private void button6_Click(object sender, EventArgs e)
        {
            uint[] indentefier_num = new uint[] { 261, 262, 302, 33, 349, 348, 396, 34, 40, 257, 247, 42, 46, 43, 44, 45, 259, 255, 256, 50, 52, 53, 399, 55, 56, 58, 59, 60, 62, 65, 66, 287, 305, 67, 68, 344, 71, 263, 72, 73, 74, 75, 350, 76, 345, 292, 291, 334, 336, 337, 77, 79, 364, 363, 81, 250, 251, 82, 85, 86, 87, 88, 90, 279, 91, 92, 93, 98, 99, 100, 101, 290, 322, 103, 104, 105, 107, 108, 109, 110, 111, 112, 296, 122, 288, 311, 392, 385, 126, 128, 129, 130, 131, 132, 294, 293, 331, 295, 133, 134, 384, 391, 135, 16, 136, 137, 289, 315, 139, 253, 142, 141, 144, 145, 146, 148, 150, 151, 152, 154, 155, 245, 157, 161, 252, 159, 281, 163, 300, 164, 165, 346, 166, 347, 167, 260, 168, 169, 395, 170, 275, 171, 283, 284, 274, 264, 173, 174, 175, 176, 268, 179, 180, 181, 184, 186, 182, 189, 301, 394, 387, 196, 198, 195, 200, 201, 202, 203, 204, 208, 209, 210, 244, 398, 212, 215, 217, 397, 218, 220, 221, 356, 357, 223, 355, 358, 359, 343, 354, 225, 353, 226, 389, 388, 342, 338, 339, 341, 340, 303, 227, 228, 229, 270, 271, 280, 231, 232, 237, 239, 240, 393, 386, 241, 242, 243 };
            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), };

            //MessageBox.Show(indentefier_num[comboBox1.SelectedIndex].ToString());
            //byte[] array = File.ReadAllBytes("d:\\aa1");
            List<byte> L = new List<byte>();
            byte[] array = File.ReadAllBytes("d:\\CDSSDZSNORTM00386");
           // long pos = 0;
            //while (pos < array.Length)
            //{
            //    L.Add(array[pos]);
            //    pos++;
            //}
            //MessageBox.Show(indentefier_num[comboBox1.SelectedIndex].ToString());
            int index=0;
            bool ok=false;
            int count=0;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            //File.Create("D:\\Yourfile.csv");
            FileStream fs1 = new FileStream("D:\\Yourfile.csv", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter writer = new StreamWriter(fs1);
            while (index != -1)
            {
            index = List_of_indntifier(array, array.Length, index, indentefier_num[comboBox1.SelectedIndex]);
            //byte[] newvalue = { 4,2,0,0,4,0,1,5,0,7,0,7,9,3,8 };
                if (index > -1)
                {
                    byte[] a = ReturnIntArray(array, array.Length, ref index);
                    string hex = BitConverter.ToString(a).Replace("-", string.Empty);
                    hex=hex.Replace("F", string.Empty);
                    

                    writer.WriteLine(hex);
                    
                    
                }
            //index = write_value_indntifier(array, array.Length, index, newvalue);
            count++;
            //MessageBox.Show(index.ToString());
            }
            writer.Close();
            stopwatch.Stop();
            //ok = ByteArrayToFile("d:\\cc", array);
            MessageBox.Show(stopwatch.Elapsed.ToString());
            MessageBox.Show(count.ToString());
            
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            List<byte> L = new List<byte> ();
            byte[] array = File.ReadAllBytes("d:\\aa");
                // 2.
                // Position and length variables.
                long pos = 0;
                // 2A.
                // Use BaseStream.
                //long length = b.BaseStream.Length;
                while (pos < array.Length)
                {
                    // 3.
                    // Read integer.

                    L.Add(array[pos]);

                    // 4.
                    // Advance our position variable.
                    pos++;
                }
            
           /* stopwatch.Stop();
            MessageBox.Show(stopwatch.Elapsed.ToString());
            MessageBox.Show(L.Count().ToString());
            MessageBox.Show(L[L.Count()-1].ToString());
            stopwatch.Reset();
            stopwatch.Start();*/
                for (int k = 0; k < 50000; k++)
                {
                    L.Insert(10, 20);
                }
            stopwatch.Stop();
            MessageBox.Show(stopwatch.Elapsed.ToString());
            MessageBox.Show(L.Count().ToString());
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            
            byte[] array = File.ReadAllBytes("d:\\aa");
            
           /* stopwatch.Stop();
            MessageBox.Show(stopwatch.Elapsed.ToString());
            MessageBox.Show(array.Count().ToString());
            MessageBox.Show(array[array.Count() - 1].ToString());
            stopwatch.Reset();
            stopwatch.Start();*/
            //Array.Resize(ref array, array.Length + 2);
            //for (int i = 200; i < array.Length - 1; i++)
            //{
            //    array[i + 1] = array[i];
            //}
            //array[200] = 3;
            //for (int k = 0; k < 100;k++ )
            //{ 
              //  Array.Resize(ref array, array.Length + 2);
                //for (int i = array.Length - 1; i >200 ; i--)
                //{
                  // array[i] = array[i-2];
                //}
               // button8.Text = k.ToString();
            //}
            byte[] newarray = new byte[array.Length + 2];
            
            Array.Copy(array, 0, newarray, 0, 200);
            Array.Copy(array, 200, newarray, 202, array.Length-200);
            newarray[200] = 3;
            newarray[201] = 3;
            Array.Resize(ref array, array.Length + 2);
            Array.Copy(array, newarray, newarray.Length);

            stopwatch.Stop();
            MessageBox.Show(stopwatch.Elapsed.ToString());
            MessageBox.Show(array.Length.ToString());
        }

        public struct write_inden
        {
            
            public long start;
            public int lenght;
            public long end;
        };
        
        private byte[] creatIDEN(int iden, string value,byte type)
        {
            byte[] v;
            if (type == 1 || type == 4)//ascii
            {

                v = Encoding.ASCII.GetBytes(textBox2.Text);
               
            }
            else if (type == 2)//HEX
            {
                if (textBox2.Text.Length % 2 == 1)
                    textBox2.Text += "f";
                 v = ToByteArray(textBox2.Text);
                
            }
            else //if (type == 3)//INT
            {
                int b = int.Parse(textBox2.Text);

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

                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();
        }

        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.ToInt16(value, 0);
                    
                
                return i.ToString();
            }
        }
        private void button9_Click(object sender, EventArgs e)
        {
            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[comboBox1.SelectedIndex].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("d:\\70\\CDJORFLAREDU03223.bak");

            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 = 0;
                        }
                    }
                    else
                    {
                        //short lenght
                        lenght &= 0x7F;
                        lenV = lenght;
                    }
                    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();
                            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[comboBox1.SelectedIndex].ctype);
                            }
                        }
                        else
                        {
                            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[comboBox1.SelectedIndex].ctype);
                            }
                            start++;
                            start++;
                        }

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

            System.IO.FileStream _FileStream =
                new System.IO.FileStream("d:\\tt", System.IO.FileMode.Create,
                                           System.IO.FileAccess.Write);
            

            byte[] t = creatIDEN(indentefiers[comboBox1.SelectedIndex].cidentifier, textBox2.Text, indentefiers[comboBox1.SelectedIndex].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();
            MessageBox.Show(stopwatch.Elapsed.ToString());
 
        }

        private void button10_Click(object sender, EventArgs e)
        {
            byte[] k = {33,36};
            //byte[] u = creatIDEN(65, "asdsa",1); 
            
        }

        private void button11_Click(object sender, EventArgs e)
        {
            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("d:\\CDSSDZSNORTM00386");

            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;
                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)
                        {
                            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;
                            end_inden.start = start+ lenV+1;
                        }
                        else
                        {
                            lenV = 0;
                        }
                    }
                    else
                    {
                        //short lenght
                        start_inden.start = start;
                        start_inden.lenght = 1;
                        lenght &= 0x7F;
                        lenV = lenght;
                        end_inden.start = start + lenV+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();
                            cards.Push(z);
                        }
                        else
                        {
                            z.key = "root";
                            cards.Push(z);
                        }
                        
                        
                    }
                    else
                    {

                        if (lenV > 0)
                        {
                            start++;

                            start += lenV;

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

                    }
                }
            }
            if (L_found_inden.Count ==0)
            {
                MessageBox.Show("finish");
                return;
            }

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

            System.IO.FileStream _FileStream =
                new System.IO.FileStream("d:\\jj", 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();
            
            /*L_found_inden=L_found_inden.OrderBy(x => x.end).ToList();
            
             memblock = File.ReadAllBytes("d:\\jj");
             _FileStream = new System.IO.FileStream("d:\\uu", System.IO.FileMode.Create,
                                            System.IO.FileAccess.Write);
             
             _FileStream.Write(memblock, 0, (int)L_found_inden[0].end);
             _FileStream.Write(t, 1, 2);
             

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

                 
                 l = (int)L_found_inden[k].end - L_found_inden[k - 1].end;
                 _FileStream.Write(memblock, (int)L_found_inden[k - 1].end, (int)l);
                 _FileStream.Write(t, 1, 2);

             }

             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();*/


            MessageBox.Show(L_found_inden.Count.ToString());
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
            byte[] array = File.ReadAllBytes("d:\\CDSSDZSNORTM00386");
            string k = "00000000000000";
            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};
            //int i = Calc_Audit(array, array.Length, 0, Lastest_call, earliest_call);
            long[] ReturnArray=new long[14];
            //Marshal.Copy(Calc_Audit(array, array.Length, 0,  Lastest_call, earliest_call), ReturnArray, 0, 14);
          // int i= Calc_Audit(array, array.Length, 0, ReturnArray, Lastest_call, earliest_call);
           //MessageBox.Show(ReturnArray[0].ToString());
           //ReturnArray = Calc_Audit(array, array.Length, 0, Lastest_call, earliest_call);
            functions.Audit audit1 = new functions.Audit();
            audit1 = functions.Calc_Audit("d:\\CDJORFLSAUZN02577.bak", "d:\\CDJORFLSAUZN02578.bak");
            MessageBox.Show(audit1.Calc_Call_event.ToString());
            MessageBox.Show(audit1.Calc_charge.ToString());
            MessageBox.Show(audit1.Calc_Discount.ToString());
            MessageBox.Show(audit1.Calc_Tax.ToString());

            MessageBox.Show(audit1.file_Call_event.ToString());
            MessageBox.Show(audit1.start_Call_event.ToString());
            MessageBox.Show(audit1.end_Call_event.ToString());

            MessageBox.Show(audit1.Calc_Earliest_call);
            MessageBox.Show(audit1.Calc_Lastest_call);

            MessageBox.Show(audit1.file_Earliest_call);
            MessageBox.Show(audit1.start_file_Earliest_call.ToString());
            MessageBox.Show(audit1.end_file_Earliest_call.ToString());

            MessageBox.Show(audit1.file_Lastest_call);
            MessageBox.Show(audit1.start_file_Lastest_call.ToString());
            MessageBox.Show(audit1.end_file_Lastest_call.ToString());
            string s = System.Text.Encoding.Default.GetString(earliest_call);
            //MessageBox.Show(s);
             s = System.Text.Encoding.Default.GetString(Lastest_call);
           // MessageBox.Show(s);
        }

        private void button13_Click(object sender, EventArgs e)
        {
           // functions.Create_Notifications("asd", "d://CDSYR01OMNGT00073");
        }

        private void treeView2_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.All;
            else
                e.Effect = DragDropEffects.None;

            
        }

        private void treeView2_DragDrop(object sender, DragEventArgs e)
        {
            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            int i;
           // dropnode=treeView2.GetNodeAt(e.X, e.Y);
            Point pt = new Point(e.X, e.Y);

            // Convert to a point in the TreeView's coordinate system.
            pt = treeView2.PointToClient(pt);
            treeView2.SelectedNode = treeView2.GetNodeAt(pt);
            if (treeView2.SelectedNode!= null)
            { 
                for (i = 0; i < s.Length; i++)
                    treeView2.SelectedNode.Nodes.Add(s[i]).Checked=true;
                treeView2.SelectedNode.Checked = true;
            }
                //treeView2.Nodes.Add( dropnode  );
        }

        private void treeView2_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }
        TreeNode dropnode;
        private void treeView2_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
        {
            dropnode = e.Node;
        }

        private void treeView2_DragOver(object sender, DragEventArgs e)
        {
           // Point pt = new Point(e.X, e.Y);

            // Convert to a point in the TreeView's coordinate system.
         //   pt = treeView2.PointToClient(pt);
           // dropnode = treeView2.GetNodeAt(pt);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            int length;
            byte[] memblock = File.ReadAllBytes("d:\\70\\CDJORFLDEUE105533.bak");
            int index=0;
            index = List_of_indntifier(memblock, memblock.Length, index, 3);
            length = valuesize_readindntifier(memblock, memblock.Length,  index);
            index = List_of_indntifier(memblock, memblock.Length, index, 15);
            length = valuesize_readindntifier(memblock, memblock.Length, index);

            functions.Merge_file("d:\\70\\CDJORFLAREDU03220.bak", "d:\\70\\CDJORFLDEUE105535.bak", "d:\\70\\Result\\CDJORFLAREDU03220.bak");
            functions.Calc_Audit("d:\\70\\Result\\CDJORFLDEUE105536.bak", "d:\\70\\Result\\CDJORFLDEUE105536.bak");
        }

        private void button15_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow)
                {
                    if (row.Cells[0].GetType() == typeof(DataGridViewCheckBoxCell))
                    {
                        if (Convert.ToString(row.Cells[0].Value).Contains("True"))
                        {
                            MessageBox.Show(row.Cells[1].Value.ToString());
                        }
                    }
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button16_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Add(new object[] { true,"D:\\wwewewewe\\","0","00000"});

        }

        private void button17_Click(object sender, EventArgs e)
        {
            functions.Split_file("d:\\CDJORFLSAUZN02577.bak", "d:\\70\\result", "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30", 30);
        }

        private void button18_Click(object sender, EventArgs e)
        {
            var cells = dataGridView1.Rows.Cast<DataGridViewRow>()
               .SelectMany(row => dataGridView1.Columns.Cast<DataGridViewColumn>()
                                .Select(col => row.Cells[col.Name]))
               .Where(cell => Convert.ToString(cell.Value).Contains("True"));

            foreach (var cell in cells)
            {
                cell.Style.BackColor = Color.Red;
                var Selected_cells = dataGridView1.Rows.Cast<DataGridViewRow>()
              .SelectMany(row => dataGridView1.Columns.Cast<DataGridViewColumn>()
                               .Select(col => row.Cells[col.Name]))
              .Where(row => Convert.ToString(row.RowIndex).Contains(cell.RowIndex.ToString()));
                foreach (var cell1 in Selected_cells)
                {
                    cell1.Style.BackColor = Color.Blue;
                    if(cell1.OwningColumn.Index==1)
                    {
                        MessageBox.Show(cell1.Value.ToString());
                    }
                }
               // if (cell.Value=="True")
                 //   cells
                //MessageBox.Show(cell.);
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            byte[] date = Encoding.ASCII.GetBytes("20140320120349");
            functions.Split_file_Date("d:\\70\\CDJORFLAREDU03220.bak", "d:\\70\\result", "1,2", date);

        }

        private void button20_Click(object sender, EventArgs e)
        {
            bool[] call_event_type= new bool[9];
            call_event_type[0] = true;//MobileOriginatedCall
            call_event_type[1] = true;//MobileTerminatedCall
            call_event_type[2] = false;//SupplServiceEvent
            call_event_type[3] = false;//ServiceCentreUsage
            call_event_type[4] = true;//GprsCall
            call_event_type[5] = false;//ContentTransaction
            call_event_type[6] = false;//LocationService
            call_event_type[7] = false;//MessagingEvent
            call_event_type[8] = false;//MobileSession
            functions.Filter_Records("d:\\CDJORFLSAUZN02577.bak", "d:\\70\\result\\CDJORFLSAUZN02577.bak", call_event_type);
        }

        private void button21_Click(object sender, EventArgs e)
        {
            //functions aa = new functions();

            //aa.OpenFile_tree("d:\\CDJORFLSAUZN02577.bak",Treeview1);
        }
        TreeNode mySelectedNode;
        private void Treeview1_MouseDown(object sender, MouseEventArgs e)
        {
            mySelectedNode = Treeview1.GetNodeAt(e.X, e.Y);
        }

        private void Treeview1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (mySelectedNode != null && mySelectedNode.Parent != null)
            {
                Treeview1.SelectedNode = mySelectedNode;
                //Treeview1.LabelEdit = true;
                if (!mySelectedNode.IsEditing)
                {
                   // mySelectedNode.BeginEdit();
                    Point pt = new Point(e.X, e.Y);

                    // Convert to a point in the TreeView's coordinate system.
                    pt = Treeview1.PointToClient(pt);
                    //textBox5.Top = Treeview1.Top + pt.Y;
                    //textBox5.Left = Treeview1.Left + pt.X;
                    Treeview1.SelectedNode = Treeview1.GetNodeAt(pt);
                    string[] nodename = Treeview1.SelectedNode.Text.Split(':');

                    var font = new Font(Treeview1.Font.Name, Treeview1.Font.Size, Treeview1.Font.Style);
                    // x=TextRenderer.MeasureText(mySelectedNode.Text, font);
                    textBox5.SetBounds(Treeview1.Left + Treeview1.SelectedNode.Bounds.X + TextRenderer.MeasureText(nodename[0], font).Width,
        Treeview1.Top +Treeview1.SelectedNode.Bounds.Y, textBox5.Width, textBox5.Height);

                    textBox5.Text = nodename[1].TrimStart();
                }
            }
            else
            {
                MessageBox.Show("No tree node selected or selected node is a root node.\n" +
                   "Editing of root nodes is not allowed.", "Invalid selection");
            }
        }

        private void Treeview1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.Label != null)
            {
                if (e.Label.Length > 0)
                {
                    if (e.Label.IndexOfAny(new char[] { '@', '.', ',', '!' }) == -1)
                    {
                        // Stop editing without canceling the label change.
                        e.Node.EndEdit(false);
                    }
                    else
                    {
                        /* Cancel the label edit action, inform the user, and 
                           place the node in edit mode again. */
                        e.CancelEdit = true;
                        MessageBox.Show("Invalid tree node label.\n" +
                           "The invalid characters are: '@','.', ',', '!'",
                           "Node Label Edit");
                        e.Node.BeginEdit();
                    }
                }
                else
                {
                    /* Cancel the label edit action, inform the user, and 
                       place the node in edit mode again. */
                    e.CancelEdit = true;
                    MessageBox.Show("Invalid tree node label.\nThe label cannot be blank",
                       "Node Label Edit");
                    e.Node.BeginEdit();
                }
            }
        }

        private void Treeview1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }
    }
}
