#include <windows.h>
#include <stdio.h>

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
char szArrayOfNames[][100] =
{
	"GetModuleHandleA" , "GetModuleHandleW" ,
	"FlushInstructionCache" ,
	"GetCurrentProcess" ,
	"QueryPerformanceFrequency" ,
	"OpenEventA" , "OpenEventW" ,
	"CreateEventA" , "CreateEventW" ,
	"ResetEvent" ,
	"SetEvent" ,
	"CreateFileA" , "CreateFileW" ,
	"WriteFile" ,
	"ReadFile" ,
	"CloseHandle" ,
	"SetErrorMode",
	"GetFileInformationByHandle",
	"GetDiskFreeSpaceA",
	"SetFileAttributsA",
	"GetFileAttributsA",
	"GetDiskFreeSpaceW",
	"SetFileAttributsW",
	"GetFileAttributsW",
	"GetFileTime",
	"SetFileTime",
	"SetFilePointer" ,
	"FlushFileBuffer" ,
	"QueryPerformanceCounter" ,
	"GetDriveTypeA" , "GetDrivrTypeW" ,
	"GetVolumeInformationA" , "GetVolumeInformationW" ,
	"GetLogicalDriveStringsA" , "GetLogicalDriveStringsW" ,
	"GetFileSize" ,
	"CreateToolhelp32Snapshot" ,
	"Process32First" , "Process32Next" ,
	"Module32First" , "Module32Next" ,
	"EnumProcesses" , "EnumProcessModules" ,
	"LoadLibraryA" , "LoadLibraryW" ,
	"FreeLibrary" ,
	"Kernel32.dll" ,
	"User32.dll" ,
	"PSAPI.dll" ,
	"ADVAPI32.dll" ,
	"GDI32.dll" ,
	"VERSION.dll" ,
	"WINMM.dll" ,
	""
};


//int main(int argc,char *argv[])
//{
//    unsigned char cXor = 0x87;

//    char c;
//    char szText[1024],szCode[255];
//    FILE *pFile;
//    long l;

//    unsigned long ulXor;

//    ////////////////////////////////
//    pFile = fopen("x:\\cpa\\public\\PTC\\PTC_ABC.h" ,"wt");

//    fprintf (pFile, "/* ----------------------------------------------------------------------------------------------------------------- */\n");
//    fprintf (pFile, "#ifndef _PTC_ABC_H_    // {\n");
//    fprintf (pFile, "#define _PTC_ABC_H_\n");
//    fprintf (pFile, "/* ----------------------------------------------------------------------------------------------------------------- */\n");
//    fprintf (pFile , "\n");

//    /* character code */

//    ulXor = (unsigned long)cXor;

//    fprintf (pFile, "#define PTC_C_Code_Xor 0x%x\n\n" , ulXor);

//    for (c='a' ; c<='z' ; c++)
//    {
//        ulXor = (unsigned long)c^cXor;
//        fprintf (pFile, "#define PTC_C_Code_%c\t\t\"\\x%X\"\n" , c , c^cXor);
//    }

//    fprintf (pFile , "\n");

//    for (c='A' ; c<='Z' ; c++)
//    {
//        ulXor = (unsigned long)c^cXor;
//        fprintf (pFile, "#define PTC_C_Code_%c\t\t\"\\x%X\"\n" , c , c^cXor);
//    }

//    fprintf (pFile , "\n");

//    for (c='0' ; c<='9' ; c++)
//    {
//        ulXor = (unsigned long)c^cXor;
//        fprintf (pFile, "#define PTC_C_Code_%c\t\t\"\\x%X\"\n" , c , c^cXor);
//    }

//    fprintf (pFile , "\n");
//    fprintf (pFile, "#define PTC_C_Code_%s\t\t\"\\x%X\"\n" , "Point" , '.'^cXor);
//    fprintf (pFile , "\n");

//    fprintf (pFile , "\n");
//    fprintf (pFile, "/* ----------------------------------------------------------------------------------------------------------------- */\n");
//    fprintf (pFile, "/* Function Names code */\n");
//    fprintf (pFile , "\n");

//    ////////////////////////////////////////////
//    fprintf (pFile , "\n\n");

//    for (l=0 ; l<100 ; l++)
//    {
//        char *szName = szArrayOfNames[l];
//        if (*szName)
//        {
//            char *p_cChar = szName+1;
//            char *p_cPoint;

//            *szText = 0;
//            while (*p_cChar)
//            {
//                if (*p_cChar == '.')
//                {
//                    strcpy (szCode , " PTC_C_Code_Point");p_cChar++;
//                }
//                else
//                    sprintf (szCode , " PTC_C_Code_%c" , *p_cChar++);
//                strcat(szText , szCode);
//            }
//            p_cPoint = strchr(szName , '.');
//            if (p_cPoint)   *p_cPoint = '_';

//            fprintf (pFile , "#define PTC_C_%sName\t\tPTC_C_Code_%c%s\n" , szName , szName[0],szText);
//            //fprintf (pFile , "%s\n" , szText);
//        }
//        else
//        {
//            break;
//        }
//    }

//    fprintf (pFile , "\n");
//    fprintf (pFile, "/* ----------------------------------------------------------------------------------------------------------------- */\n");
//    fprintf (pFile , "\n");
//    fprintf (pFile , "#endif    // _PTC_ABC_H_ }\n");
//    fclose (pFile);

//  return(0);
//}

/* ---------------- */
/* random functions */
/* ---------------- */
long PTC_g_lCurrentRandom;


long PTC_fn_lGetRandom()
{
	long lTmp;

    PTC_g_lCurrentRandom ^= 123459876;
    lTmp = PTC_g_lCurrentRandom / 127773;
    PTC_g_lCurrentRandom = 16807 * (PTC_g_lCurrentRandom - lTmp * 127773) - 2836 * lTmp;
    if (PTC_g_lCurrentRandom < 0) PTC_g_lCurrentRandom += 2147483647;

	return PTC_g_lCurrentRandom;
}

long PTC_fn_lGetRandomRange(long _lMin , long _lMax)
{
	return (_lMin + (PTC_fn_lGetRandom() % (_lMax - _lMin)));
}

int main(int argc,char *argv[])
{
    LARGE_INTEGER xCounter;

    unsigned char cXor = 0x87;

	char c;
	char szText[1024],szCode[255];
	FILE *pFile;
	long l;

	unsigned long ulXor;

    if (QueryPerformanceCounter (&xCounter))
	{
		PTC_g_lCurrentRandom = xCounter.LowPart;
	}
	else
	{
		PTC_g_lCurrentRandom = 0x12345678;
	}

	////////////////////////////////
	pFile = fopen("x:\\cpa\\public\\PTC\\PTC_ABC.h" ,"wt");

	fprintf (pFile, "/* ----------------------------------------------------------------------------------------------------------------- */\n");
	fprintf (pFile, "#ifndef _PTC_ABC_H_	// {\n");
	fprintf (pFile, "#define _PTC_ABC_H_\n");
	fprintf (pFile, "/* ----------------------------------------------------------------------------------------------------------------- */\n");
	fprintf (pFile, "/* Function Names code */\n");
	fprintf (pFile , "\n");

	/* character code */

	for (l=0 ; l<100 ; l++)
	{
		char *szName = szArrayOfNames[l];
		if (*szName)
		{
            long            lCryptKey       = PTC_fn_lGetRandom();
            unsigned char  *p_ucCryptKey    = (unsigned char*)&lCryptKey;
            unsigned char  *p_ucChar         = (unsigned char*)szName;
			char		   *p_cPoint;

			// Crypt Key
            sprintf (szText , "\\x%02X\\x%02X\\x%02X\\x%02X" , p_ucCryptKey[0] , p_ucCryptKey[1] , p_ucCryptKey[2] , p_ucCryptKey[3]);

			// Size
            sprintf (szCode , "\\x%02X" , strlen (szName));
            strcat (szText , szCode);

			// Name
            while(*p_ucChar)
            {
                unsigned char cCode = (*p_ucChar++ ^ p_ucCryptKey[1]) + p_ucCryptKey[0];
                p_ucCryptKey[0] += p_ucCryptKey[2];
                p_ucCryptKey[1] += p_ucCryptKey[3];

                sprintf (szCode , "\\x%02X" , cCode);
                strcat (szText , szCode);
            }
            p_cPoint = strchr(szName , '.');
            if (p_cPoint)   *p_cPoint = '_';

            fprintf (pFile , "#define PTC_C_%sName\t\t\"%s\"\n" , szName , szText);
		}
		else
		{
			break;
		}
    }


	fprintf (pFile , "\n");
	fprintf (pFile, "/* ----------------------------------------------------------------------------------------------------------------- */\n");
	fprintf (pFile , "\n");
	fprintf (pFile , "#endif	// _PTC_ABC_H_ }\n");
	fclose (pFile);

  return(0);
}

