// Bulddll.cpp : Defines the exported functions for the DLL application.
//
//#include <iostream>
//#include <fstream>
//#include <bitset>

//#include <algorithm>

#include "stdafx.h"
#include "amp.h"
#include <stack>
#include <list>

using namespace std;

//typedef basic_ifstream<char, char_traits<char> > ifstream;
//ifstream::pos_type size;
char * memblock;
unsigned long p;

struct indefinded_inden
{
	unsigned int identifier;
	unsigned long start;
	//unsigned long end; 
};


stack <indefinded_inden> cards; //stack

extern "C" __declspec ( dllexport ) byte* _stdcall readf(byte memblock[], int size)
{
	byte mask;
	//mask=block[0] & 0xf0;
	byte* value = new byte[size]; //put all values in this variable
	
	byte* pvalue= value;//pointer to value variable
	byte* pmemvalue;
	
	unsigned long start=0;
	
	while (start < size)
	{
			int o;
		char iden=0; //identifier
		unsigned int lenV=0;//lenght value
		int lenght=0; // lenght of lenght value
		bool par;// paranert indecator
		byte *value;//value of record
		unsigned char mask;
		p=start;
		iden= memblock[start];
				// std::bitset<8> bk(memblock[start]);			
				// std::cout << bk << endl ;
		mask = iden & 0x40;
		if (mask == 0x40)
		{
			mask = iden & 0x1f;
			if (mask == 0x1f)
			{
				char indlen;
				unsigned int indlenV;
				start++;
				indlen=memblock[start];
				mask=indlen&0x80;
				if (mask == 0x80)
				{
					// long lenght
					// cout<<"long identifer"<<endl;
					indlen&= 0x7F;
					start++;
					indlenV =(unsigned char)memblock[start] ;
					//for (int b=1; b<indlen;b++)
					//{
						//cout<<"long identifer loop" <<endl;
						//indlenV=indlenV << 8;
						//start++;
						//indlenV |=block[start];
						
					//}
				}
				else
				{	
					// short lenght
					// cout<<"short identifer"<<endl;
					indlen&= 0x7F;
					indlenV=indlen;
				}
			}
			start++;
			lenght=memblock[start];
				// std::bitset<8> bk(memblock[start]);			
				// std::cout << bk << endl ;
			mask=lenght & 0X80;
				 
				// bk=mask;			
				// std::cout << bk << endl ;
			if (mask == 0X80)
			{
				//long lenght
				// cout<<"long lenght"<<endl;
				lenght&= 0x7F;
				start++;
					// std::bitset<8> bk(memblock[start]);			
					// std::cout << bk << endl ;
				

				lenV =(unsigned char)memblock[start] ;
				for (int c=1; c<lenght;c++)
				{
					lenV=lenV << 8;
					start++;
						// std::bitset<8> bk(memblock[start]);			
						// std::cout << bk << endl ;
					lenV |=memblock[start];
					
				}
			}
			else
			{
				// cout<<"short lenght"<<endl;
				//short lenght
				lenght&= 0x7F;
				if (lenght > 0)
					lenV = lenght;
				else// if lenght =0 
					lenV = -2;
			}
			mask = iden & 0x20;
			if ( mask == 0x20 )
			{
				//parent
				// cout  << "parant" << endl;
				int t;
				start++;
				// cout  << start << endl;
					
					// cout<< lenV << endl;		
				//cin >> o;
					
			
			}
			else
			{
				//child
				// cout  << "child" << endl;
				//value=&block[start];
				
				int t;
				start++;
				
				pmemvalue=&memblock[start];
				memcpy (pvalue, pmemvalue, lenV * sizeof(char));
				pvalue+=lenV+1;
				// cout  << start << endl;
				// cout<< lenV << endl;
				start+=lenV;
					//			cin >> o;
			
				
			}
		}
	}
	return value;
	
}



extern "C" __declspec ( dllexport ) int _stdcall index_of_indentifier(byte memblock[], int size,byte value[],int valuesize,int start )
{
	//byte FATind[]={0x7f,0x6b}; // file avaliable timestamp identifier
	int FATindex;
    if ( size < valuesize)
        return -1;
    for (int i = start; i < size - valuesize; i++)
    {
        bool found = true;
        for (int j = 0; j < valuesize; j++)
        {
            if (memblock[i + j] != value[j])
            {
                found = false;
                break;
            }
        }
        if (found)
        {
            return i;
        }
    }
    return -1;

}


extern "C" __declspec ( dllexport ) byte* _stdcall readindntifier(byte memblock[], int size , int &index)
{
	byte mask;
	indefinded_inden z;
	//byte value[2];//put all values in this variable
	//byte* pvalue = value;//pointer to value variable
	//byte* pmemvalue;

	unsigned long start = index;

	while (start < size)
	{
		int o;
		char iden = 0; //identifier
		unsigned long iden_start = 0; //identifier
		unsigned int lenV = 0;//lenght value
		int lenght = 0; // lenght of lenght value
		bool par;// paranert indecator
		byte *value;//value of record
		byte indlen;//length of identifier
		unsigned int indlenV;//value of identifier

		//unsigned char mask;
		p = start;
		iden = memblock[start];

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

			//if (z.identifier == 0x63)
			//{
			//	std::bitset<16> r(z.start);
			//	std::cout << "stack out " << z.start << endl;
			//}

			cards.pop();

		}

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

				start++;
				indlen = memblock[start];
				mask = indlen & 0x80;
				if (mask == 0x80)
				{
					// long lenght
					indlen &= 0x7F;
					indlenV = (unsigned char)indlen;
					indlenV = indlenV << 7;
					start++;
					indlenV |= (unsigned char)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 = (unsigned char)iden;
			}
			start++;
			lenght = memblock[start];
			mask = lenght & 0x80;


			if (mask == 0x80)
			{
				mask = lenght & 0x7F;
				if (mask >= 0x01)
				{
					//long lenght
					lenght &= 0x7F;
					start++;
					lenV = (unsigned char)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 = iden & 0x20;
			if (mask == 0x20)
			{
				//parent
				int t;
				start++;
				if (lenV == -1)
				{
					z.identifier = indlenV;
					z.start = iden_start;
					cards.push(z);
				}

			}
			else
			{
				//child

				if (lenV >0)
				{
					int t;

					start++;
					byte* value = new byte[lenV]; //put all values in this variable
					byte* pvalue = value;//pointer to value variable
					byte* pmemvalue;
					pmemvalue = &memblock[start];
					memcpy(pvalue, pmemvalue, lenV * sizeof(char));
					pvalue += lenV + 1;
					start += lenV;
					index = start;
					return value;
				}
				else if (lenV!=-2)
				{
					std::list<char> valuelist;
					
					while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
					{
						valuelist.push_back(memblock[start]);
						start++;
					}
					byte* value = new byte[valuelist.size()]; //put all values in this variable
					byte* pvalue = value;//pointer to value variable
					byte* pmemvalue;
					copy(valuelist.begin(), valuelist.end(), value);
					start++;
					start++;
					index = start;
					return value;
				}
				else if (lenV == -2)
					start++;
			}
		}
	}
}
extern "C" __declspec (dllexport) int _stdcall valuesize_readindntifier(byte memblock[], int size, int index)
{
	byte mask;
	indefinded_inden z;
	//byte value[2];//put all values in this variable
	//byte* pvalue = value;//pointer to value variable
	//byte* pmemvalue;
	unsigned int end_of_indlenV = 0;// end of indentifer value
	unsigned int Value_Index = 0;

	unsigned long start = index;

	while (start < size)
	{
		int o;
		char iden = 0; //identifier
		unsigned long iden_start = 0; //identifier
		unsigned int lenV = 0;//lenght value
		int lenght = 0; // lenght of lenght value
		bool par;// paranert indecator
		byte *value;//value of record
		byte indlen;//length of identifier
		unsigned int indlenV;//value of identifier
		

		//unsigned char mask;
		p = start;
		iden = memblock[start];

		if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
		{
			start += 2;
			z = cards.top();
			
			//if (z.identifier == 3)
				//return 0;
			if (end_of_indlenV == z.identifier)
				return start -Value_Index - 2; // 0x00 of the ending identifier is not inclouded
			//if (z.identifier == 0x63)
			//{
			//	std::bitset<16> r(z.start);
			//	std::cout << "stack out " << z.start << endl;
			//}

			cards.pop();

		}

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

				start++;
				indlen = memblock[start];
				mask = indlen & 0x80;
				if (mask == 0x80)
				{
					// long lenght
					indlen &= 0x7F;
					indlenV = (unsigned char)indlen;
					indlenV = indlenV << 7;
					start++;
					indlenV |= (unsigned char)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 = (unsigned char)iden;
				indlenV &= 0x1f;
			}

			if (end_of_indlenV == 0)
				end_of_indlenV = indlenV;
			
			start++;
			lenght = memblock[start];
			mask = lenght & 0x80;


			if (mask == 0x80)
			{
				mask = lenght & 0x7F;
				if (mask >= 0x01)
				{
					//long lenght
					lenght &= 0x7F;
					start++;
					lenV = (unsigned char)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 = iden & 0x20;
			if (mask == 0x20)
			{
				//parent
				int t;
				start++;
				if (lenV == -1)
				{
					z.identifier = indlenV;
					z.start = iden_start;
					cards.push(z);
					if (Value_Index == 0)
						Value_Index = start;
				}
				else if (end_of_indlenV == indlenV )
					return  lenV;

			}
			else
			{
				//child

				if (lenV >0)
				{
					int t;

					start++;
					start += lenV;
					if (end_of_indlenV == indlenV)
						return lenV;
				}
				else if (lenV !=-2)
				{
					
					while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
					{
						lenV++;
						start++;
					}
					
					start++;
					start++;
					if (end_of_indlenV == indlenV)
						return lenV;
				}
				else if (lenV == -2)
					start++;
			}
		}
	}
	
}
extern "C" __declspec (dllexport) int _stdcall Identifier_length(byte memblock[], int size, int index)
{
	byte mask;
	indefinded_inden z;
	//byte value[2];//put all values in this variable
	//byte* pvalue = value;//pointer to value variable
	//byte* pmemvalue;
	unsigned int end_of_indlenV = 0;// end of indentifer value
	unsigned int Value_Index = 0;

	unsigned long start = index;

	while (start < size)
	{
		int o;
		char iden = 0; //identifier
		unsigned long iden_start = 0; //identifier
		unsigned int lenV = 0;//lenght value
		int lenght = 0; // lenght of lenght value
		bool par;// paranert indecator
		byte *value;//value of record
		byte indlen;//length of identifier
		unsigned int indlenV;//value of identifier


		//unsigned char mask;
		p = start;
		iden = memblock[start];

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

			//if (z.identifier == 3)
			//return 0;
			if (end_of_indlenV == z.identifier)
				return start - index ; // 0x00 of the ending identifier is not inclouded
			//if (z.identifier == 0x63)
			//{
			//	std::bitset<16> r(z.start);
			//	std::cout << "stack out " << z.start << endl;
			//}

			cards.pop();

		}

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

				start++;
				indlen = memblock[start];
				mask = indlen & 0x80;
				if (mask == 0x80)
				{
					// long lenght
					indlen &= 0x7F;
					indlenV = (unsigned char)indlen;
					indlenV = indlenV << 7;
					start++;
					indlenV |= (unsigned char)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 = (unsigned char)iden;
				indlenV &= 0x1f;
			}

			if (end_of_indlenV == 0)
				end_of_indlenV = indlenV;

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


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

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

				if (lenght > 0)
					lenV = lenght;
				else// if lenght =0 
					lenV = -2;
			}
			mask = iden & 0x20;
			if (mask == 0x20)
			{
				//parent
				int t;
				start++;
				if (lenV == -1)
				{
					z.identifier = indlenV;
					z.start = iden_start;
					cards.push(z);

				}
				else if (end_of_indlenV == indlenV)
					return  lenV;

			}
			else
			{
				//child

				if (lenV >0)
				{
					int t;

					start++;
					start += lenV;
					if (end_of_indlenV == indlenV)
						return lenV;
				}
				else if (lenV!=-2)
				{

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

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

}

extern "C" __declspec (dllexport) int _stdcall valueINDEX_readindntifier(byte memblock[], int size, int index)
{
	byte mask;
	indefinded_inden z;
	//byte value[2];//put all values in this variable
	//byte* pvalue = value;//pointer to value variable
	//byte* pmemvalue;
	unsigned int end_of_indlenV = 0;// end of indentifer value
	
	unsigned long start = index;

	while (start < size)
	{
		int o;
		char iden = 0; //identifier
		unsigned long iden_start = 0; //identifier
		unsigned int lenV = 0;//lenght value
		int lenght = 0; // lenght of lenght value
		bool par;// paranert indecator
		byte *value;//value of record
		byte indlen;//length of identifier
		unsigned int indlenV;//value of identifier


		//unsigned char mask;
		p = start;
		iden = memblock[start];

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

			//if (z.identifier == 3)
			//return 0;
			//if (end_of_indlenV == z.identifier)
			//	return  value_Index ; // 0x00 of the ending identifier is not inclouded
			//if (z.identifier == 0x63)
			//{
			//	std::bitset<16> r(z.start);
			//	std::cout << "stack out " << z.start << endl;
			//}

			cards.pop();

		}

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

				start++;
				indlen = memblock[start];
				mask = indlen & 0x80;
				if (mask == 0x80)
				{
					// long lenght
					indlen &= 0x7F;
					indlenV = (unsigned char)indlen;
					indlenV = indlenV << 7;
					start++;
					indlenV |= (unsigned char)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 = (unsigned char)iden;
				indlenV &= 0x1f;
			}

			if (end_of_indlenV == 0)
				end_of_indlenV = indlenV;

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


			if (mask == 0x80)
			{
				mask = lenght & 0x7F;
				if (mask >= 0x01)
				{
					//long lenght
					lenght &= 0x7F;
					start++;
					lenV = (unsigned char)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 = iden & 0x20;
			if (mask == 0x20)
			{
				//parent
				int t;
				start++;
				if (lenV == -1)
				{
					z.identifier = indlenV;
					z.start = iden_start;
					cards.push(z);
					
					return start;
				}
				else
				{
					return start;
				}

			}
			else
			{
				//child

				if (lenV >0)
				{
					int t;

					start++;
					return start;
					start += lenV;
					
				}
				else if (lenV!=-2)
				{
					return start;
					while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
					{
						lenV++;
						start++;
					}

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

}

extern "C" __declspec (dllexport) int _stdcall List_of_indntifier(byte memblock[], int size, int index, unsigned int search_iden)
{
	byte mask;
	indefinded_inden z;
	byte value[2];//put all values in this variable
	byte* pvalue = value;//pointer to value variable
	byte* pmemvalue;

	unsigned long start = index;

	while (start < size)
	{
		int o;
		char iden = 0; //identifier
		unsigned long iden_start = 0; //identifier
		unsigned int lenV = 0;//lenght value
		int lenght = 0; // lenght of lenght value
		bool par;// paranert indecator
		byte *value;//value of record
		byte indlen;//length of identifier
		unsigned int indlenV;//value of identifier
		
		//unsigned char mask;
	checkagain:
		p = start;
		iden = memblock[start];
		
		if (memblock[start] == 0x00 & memblock[start + 1] == 0x00)
		{
			start += 2;
			z = cards.top();

			//if (z.identifier == 0x63)
			//{
			//	std::bitset<16> r(z.start);
			//	std::cout << "stack out " << z.start << endl;
			//}

			cards.pop();
			if (start == size)
				break;
			goto checkagain;
		}

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

				start++;
				indlen = memblock[start];
				mask = indlen & 0x80;
				if (mask == 0x80)
				{
					// long lenght
					indlen &= 0x7F;
					indlenV = (unsigned char)indlen;
					indlenV = indlenV << 7;
					start++;
					indlenV |= (unsigned char)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 = (unsigned char)iden;
				indlenV &= 0x1f;
			}
			start++;
			lenght = memblock[start];
			mask = lenght & 0x80;

			if (indlenV == search_iden)
			{
				return p;
			}

			if (mask == 0x80)
			{
				mask = lenght & 0x7F;
				if (mask >= 0x01)
				{
					//long lenght
					lenght &= 0x7F;
					start++;
					lenV = (unsigned char)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 = iden & 0x20;
			if (mask == 0x20)
			{
				//parent
				int t;
				start++;
				if (lenV == -1)
				{
					z.identifier = indlenV;
					z.start = iden_start;
					cards.push(z);
				}

			}
			else
			{
				//child

				if (lenV >0)
				{
					int t;
					start++;
					pmemvalue = &memblock[start];
					//memcpy (pvalue, pmemvalue, lenV * sizeof(char));
					pvalue += lenV + 1;
					start += lenV;
				}
				else if (lenV !=-2)
				{
					while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
					{
						start++;
					}
					start++;
					start++;
				}
				else if (lenV == -2)
					start++;
			}
		}
		else
			return -1;
	}
	return -1;
}
struct indensize
{
	int start;
	int end;
};


extern "C" __declspec ( dllexport ) int _stdcall write_value_indntifier(byte memblock[], int size , int index,byte newvalue[])
{
	byte mask;
	//mask=block[0] & 0xf0;
	unsigned long start=index;
	
	while (start < size)
	{
			int o;
		char iden=0; //identifier
		unsigned int lenV=0;//lenght value
		int lenght=0; // lenght of lenght value
		bool par;// paranert indecator
		byte *value;//value of record
		unsigned char mask;
		p=start;
		iden= memblock[start];
				// std::bitset<8> bk(memblock[start]);			
				// std::cout << bk << endl ;
		mask = iden & 0x40;
		if (mask == 0x40)
		{
			mask = iden & 0x1f;
			if (mask == 0x1f)
			{
				char indlen;
				unsigned int indlenV;
				start++;
				indlen=memblock[start];
				mask=indlen&0x80;
				if (mask == 0x80)
				{
					// long lenght
					// cout<<"long identifer"<<endl;
					indlen&= 0x7F;
					start++;
					indlenV =(unsigned char)memblock[start] ;
					//for (int b=1; b<indlen;b++)
					//{
						//cout<<"long identifer loop" <<endl;
						//indlenV=indlenV << 8;
						//start++;
						//indlenV |=block[start];
						
					//}
				}
				else
				{	
					// short lenght
					// cout<<"short identifer"<<endl;
					indlen &= 0x7F;
					indlenV = indlen;
				}
			}
			start++;
			lenght=memblock[start];
				// std::bitset<8> bk(memblock[start]);			
				// std::cout << bk << endl ;
			mask=lenght & 0X80;
				 
				// bk=mask;			
				// std::cout << bk << endl ;
			if (mask == 0X80)
			{
				//long lenght
				// cout<<"long lenght"<<endl;
				lenght&= 0x7F;
				start++;
					// std::bitset<8> bk(memblock[start]);			
					// std::cout << bk << endl ;
				

				lenV =(unsigned char)memblock[start] ;
				for (int c=1; c<lenght;c++)
				{
					lenV=lenV << 8;
					start++;
						// std::bitset<8> bk(memblock[start]);			
						// std::cout << bk << endl ;
					lenV |=memblock[start];
					
				}
			}
			else
			{
				// cout<<"short lenght"<<endl;
				//short lenght
				lenght&= 0x7F;
				if (lenght > 0)
					lenV = lenght;
				else// if lenght =0 
					lenV = -2;
			}
			mask = iden & 0x20;
			if ( mask == 0x20 )
			{
				//parent
				// cout  << "parant" << endl;
				int t;
				start++;
				// cout  << start << endl;
					
					// cout<< lenV << endl;		
				//cin >> o;
					
			
			}
			else
			{
				//child
				// cout  << "child" << endl;
				//value=&block[start];
				
				int t;
				start++;
				byte* value = new byte[lenV]; //put all values in this variable
				byte* pvalue= value;//pointer to value variable
				byte* pmemvalue;
				
				pmemvalue=&memblock[start];
				memcpy (pmemvalue,&newvalue[0] , lenV * sizeof(char));
				//pvalue+=lenV+1;
				// cout  << start << endl;
				// cout<< lenV << endl;
				start+=lenV;
				return start;
					//			cin >> o;
			
				
			}
		}
	}
	return -1;
	
}


extern "C" __declspec (dllexport) long* _stdcall Calc_Audit(byte memblock[], int size, int index,  byte Lastest_call[], byte earliest_call[])
{
	long audit[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0 };
	
	byte mask;
	indefinded_inden z;
	byte value[2];//put all values in this variable
	byte* pvalue = value;//pointer to value variable
	byte* pmemvalue;

	unsigned long start = index;

	while (start < size)
	{
		int o;
		char iden = 0; //identifier
		unsigned long iden_start = 0; //identifier
		unsigned int lenV = 0;//lenght value
		int lenght = 0; // lenght of lenght value
		bool par;// paranert indecator
		byte *value;//value of record
		byte indlen;//length of identifier
		unsigned int indlenV;//value of identifier

		//unsigned char mask;
		p = start;
		iden = memblock[start];

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

			//if (z.identifier == 0x63)
			//{
			//	std::bitset<16> r(z.start);
			//	std::cout << "stack out " << z.start << endl;
			//}

			cards.pop();

		}

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

				start++;
				indlen = memblock[start];
				mask = indlen & 0x80;
				if (mask == 0x80)
				{
					// long lenght
					indlen &= 0x7F;
					indlenV = (unsigned char)indlen;
					indlenV = indlenV << 7;
					start++;
					indlenV |= (unsigned char)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 = (unsigned char)iden;
				indlenV &= 0x1f;
			}
			start++;
			lenght = memblock[start];
			mask = lenght & 0x80;

			

			if (mask == 0x80)
			{
				mask = lenght & 0x7F;
				if (mask >= 0x01)
				{
					//long lenght
					lenght &= 0x7F;
					start++;
					lenV = (unsigned char)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)
			{
				audit[0] += 1;
			}
			mask = iden & 0x20;
			if (mask == 0x20)
			{
				//parent
			
				int t;
				start++;
				if (lenV == -1)
				{
					z.identifier = indlenV;
					z.start = iden_start;
					cards.push(z);
				}

			}
			else
			{
				//child

				if (lenV >0)
				{
					int t;

					start++;
					byte* value = new byte[lenV]; //put all values in this variable
					byte* pvalue = value;//pointer to value variable
					byte* pmemvalue;
					pmemvalue = &memblock[start];
					memcpy(pvalue, pmemvalue, lenV * sizeof(char));
					pvalue += lenV + 1;
					start += lenV;
					if (indlenV == 62)
					{
						int i = (unsigned int)value[0];
						for (int c = 1; c<lenV; c++)
						{
							i = i << 8;
							i |= value[c];
						}
						audit[1] +=  i;
					}
					if (indlenV == 397)
					{
						int i = (unsigned int)value[0];
						for (int c = 1; c<lenV; c++)
						{
							i = i << 8;
							i |= value[c];
						}
						audit[2] += i;
					}
					if (indlenV == 412)
					{
						int i = (unsigned int)value[0];
						for (int c = 1; c<lenV; c++)
						{
							i = i << 8;
							i |= value[c];
						}
						audit[3] += i;
					}
					if (indlenV == 415)//TotalCharge 
					{
						int i = (unsigned int)value[0];
						for (int c = 1; c<lenV; c++)
						{
							i = i << 8;
							i |= value[c];
						}
						audit[4] = i;//TotalCharge 
						audit[5] = iden_start;
						audit[6] = start;

					}
					if (indlenV == 226)//TotalTaxValue
					{
						int i = (unsigned int)value[0];
						for (int c = 1; c<lenV; c++)
						{
							i = i << 8;
							i |= value[c];
						}
						audit[7] += i;//TotalTaxValue
						audit[8] = iden_start;
						audit[9] = start;

					}
					if (indlenV == 225)//TotalDiscountValue 
					{
						int i = (unsigned int)value[0];
						for (int c = 1; c<lenV; c++)
						{
							i = i << 8;
							i |= value[c];
						}
						audit[10] += i;//TotalDiscountValue 
						audit[11] = iden_start;
						audit[12] = start;

					}
					if (indlenV == 43)//CallEventDetailsCount 
					{
						int i = (unsigned int)value[0];
						for (int c = 1; c<lenV; c++)
						{
							i = i << 8;
							i |= value[c];
						}
						audit[7] += i;
						audit[13] = iden_start;
						audit[14] = start;

					}

					z = cards.top();

					if (indlenV == 16 && z.identifier == 9 || indlenV == 16 && z.identifier == 10 || indlenV == 16 && z.identifier == 11 || indlenV == 16 && z.identifier == 12 || indlenV == 16 && z.identifier == 14 || indlenV == 16 && z.identifier == 17 || indlenV == 16 && z.identifier == 297 || indlenV == 16 && z.identifier == 433 || indlenV == 16 && z.identifier == 434)
					{
						int n;
				
						//str.assign(buf, buf + l);
						audit[8] += 1;
						audit[8] = memcmp(value, Lastest_call, 14 * sizeof(byte));
						if (audit[8] > 0 )//latest call
							memcpy(Lastest_call, value, 14 * sizeof(char));


						n = memcmp(value, earliest_call, 14 * sizeof(byte));
						if (n < 0)//earliest call
							memcpy(earliest_call, value, 14 * sizeof(byte));
						//audit[8] = (long)ts[3];
						
					}         
				}
				else if (lenV!=-2)
				{
					std::list<char> valuelist;

					while (memblock[start] != 0x00 && memblock[start + 1] != 0x00)
					{
						valuelist.push_back(memblock[start]);
						start++;
					}
					byte* value = new byte[valuelist.size()]; //put all values in this variable
					byte* pvalue = value;//pointer to value variable
					byte* pmemvalue;
					copy(valuelist.begin(), valuelist.end(), value);
					if (indlenV == 415)//TotalCharge 
					{
						int i = (unsigned int)value[0];
						for (int c = 1; c<lenV; c++)
						{
							i = i << 8;
							i |= value[c];
						}
						audit[4] = i;//TotalCharge 
					}
					start++;
					start++;
					index = start;
					
				}
				else if (lenV == -2)
					start++;
			}
		}
	}
	//memcpy(audit, result, 14 * sizeof(long));
	return audit;
}

extern "C" __declspec (dllexport) int _stdcall Memory_compare(byte array1[], byte array2[])
{
	int i = memcmp(array1, array2, 14 * sizeof(byte));
	return i;
}

extern "C" __declspec (dllexport) void _stdcall Memory_Copy(byte Source[], byte Destention[])
{
	
	memcpy(Destention, Source, 14 * sizeof(char));
}