daily pastebin goal
51%
SHARE
TWEET

Untitled

a guest Jul 8th, 2014 341 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. #define BUFFER_SIZE 1024
  5. char LastCmd[BUFFER_SIZE];
  6. char DataCharacters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
  7.  
  8. bool IncreaseChar(char* ptr, DWORD index)
  9. {
  10.     char curchar[2];
  11.  
  12.     curchar[0] = ptr[index];
  13.     curchar[1] = 0;
  14.     if (curchar[0] == DataCharacters[sizeof(DataCharacters) - 2])
  15.     {
  16.         if (index == 0)
  17.         {
  18.             ptr[index] = DataCharacters[0];
  19.             return false;
  20.         }
  21.         else
  22.         {
  23.             ptr[index] = DataCharacters[0];
  24.             return IncreaseChar(ptr, index - 1);
  25.         }
  26.     }
  27.     else
  28.     {
  29.         char* temp = strstr(DataCharacters, curchar);
  30.         ptr[index] = *(temp + 1);
  31.     }
  32.  
  33.     return true;
  34. }
  35.  
  36. bool GenerateNextCommand(char* ptr, DWORD* size)
  37. {
  38.     if (*size == 0)
  39.     {
  40.         ptr[*size] = DataCharacters[0];
  41.         *size = 1;
  42.         ptr[*size] = 0;
  43.     }
  44.    
  45.     if (!IncreaseChar(ptr, *size - 1))
  46.     {
  47.         if (*size < BUFFER_SIZE - 1)
  48.         {
  49.             ptr[*size] = DataCharacters[0];
  50.             *size += 1;
  51.             ptr[*size] = 0;
  52.             return IncreaseChar(ptr, *size - 1);
  53.         }
  54.         else
  55.         {
  56.             return false;
  57.         }
  58.     }
  59.  
  60.     return true;
  61. }
  62.  
  63. bool SysconError(char* ptr, DWORD size)
  64. {
  65.     return !memcmp(ptr, "E:4E:NG F0000003", size - 1);
  66. }
  67.  
  68. char GenerateChecksum(char* ptr,DWORD size)
  69. {
  70.     char checksum = 0;
  71.     for(DWORD i = 0; i < size; i++)
  72.     {
  73.         checksum += ptr[i];
  74.     }
  75.  
  76.     return checksum;
  77. }
  78.  
  79. void SendCommand(HANDLE Comport, char* ptr, DWORD size)
  80. {
  81.     char Buffer[1024];
  82.     DWORD BytesWritten;
  83.     _OVERLAPPED OverlapDesc;
  84.  
  85.     memset(&OverlapDesc, 0, sizeof(_OVERLAPPED));
  86.    
  87.     sprintf_s(Buffer, 1024, "C:%02X:%s\r\n", GenerateChecksum(ptr, size) & 0xFF, ptr);
  88.     size = strlen(Buffer);
  89.     strcpy_s(LastCmd, BUFFER_SIZE, Buffer);
  90.     WriteFile(Comport, Buffer, size, &BytesWritten, &OverlapDesc);
  91. }
  92.  
  93. void main(int argc, char *argv[])
  94. {
  95.  
  96.     char Buffer[BUFFER_SIZE];
  97.     DWORD BytesRead;
  98.     char Command[BUFFER_SIZE];
  99.     DWORD CommandSize;
  100.     HANDLE Comport;
  101.     _DCB Config;
  102.     COMMTIMEOUTS Timeouts;
  103.     bool UserCommands;
  104.     OVERLAPPED OverlapDesc;
  105.  
  106.     memset(&OverlapDesc, 0, sizeof(OverlapDesc));
  107.     UserCommands = false;
  108.     if (argc > 2) {
  109.         if (!strcmp(argv[2],"UserCmd")) {
  110.             UserCommands = true;
  111.             printf("User commands active.\n");
  112.         } else {
  113.             sprintf_s(Command, BUFFER_SIZE, argv[2]);
  114.         }
  115.     } else {
  116.         sprintf_s(Command, BUFFER_SIZE, "A");
  117.     }
  118.  
  119.     if(!UserCommands)
  120.     {
  121.         printf("Starting at:%s\n\n",Command);
  122.         CommandSize = strlen(Command);
  123.     }
  124.  
  125.     Comport = CreateFileA(
  126.         argv[1],
  127.         GENERIC_READ | GENERIC_WRITE,
  128.         0,
  129.         NULL,
  130.         OPEN_EXISTING,
  131.         0,
  132.         //FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING ,
  133.         NULL
  134.         );
  135.  
  136.     if (Comport == INVALID_HANDLE_VALUE)
  137.     {
  138.         printf("Could not open comport\n");
  139.         return;
  140.     }
  141.  
  142.     OverlapDesc.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  143.  
  144.     Timeouts.ReadIntervalTimeout = 1;
  145.     Timeouts.ReadTotalTimeoutMultiplier = 1;
  146.     Timeouts.ReadTotalTimeoutConstant = 1;
  147.     Timeouts.WriteTotalTimeoutMultiplier = 1;
  148.     Timeouts.WriteTotalTimeoutConstant = 1;
  149.     if (!SetCommTimeouts(Comport, &timeouts)) {
  150.         printf("Could not set timeouts.\n");
  151.         return;
  152.     }
  153.  
  154.     if (GetCommState(Comport, &Config) == 0)
  155.     {
  156.        printf("Get configuration port failed.\n");
  157.        return;
  158.     }
  159.  
  160.     Config.DCBlength = 0x1c;
  161.     Config.BaudRate = 57600;
  162.     Config.StopBits = 0;
  163.     Config.Parity = 0;
  164.     Config.ByteSize = 8;
  165.     Config.fDtrControl = 0;
  166.     Config.fRtsControl = 0;
  167.    
  168.     if (SetCommState(Comport, &Config) == 0)
  169.     {
  170.        printf("Set configuration port failed.\n");
  171.        return;
  172.     }
  173.  
  174.     printf("listening\n");
  175.     memset(Buffer,0,BUFFER_SIZE);
  176.  
  177.     //while (ReadFile(Comport, Buffer, BUFFER_SIZE, &BytesRead, &OverlapDesc))
  178.     while (ReadFile(Comport, Buffer, BUFFER_SIZE, &BytesRead, NULL))
  179.     {
  180.         if (UserCommands)
  181.         {
  182.             if (BytesRead != 0)
  183.             {
  184.                 printf("%s\n",Buffer);
  185.             }
  186.  
  187.             printf("inp:");
  188.             gets_s(Command, BUFFER_SIZE);
  189.             SendCommand(Comport, Command, strlen(Command));
  190.             printf(LastCmd);
  191.             Sleep(30);
  192.             memset(Buffer,0,BytesRead);
  193.             continue;
  194.         }
  195.  
  196.         if (BytesRead == 0)
  197.         {
  198.             SendCommand(Comport, Command, CommandSize);
  199.             Sleep(30);
  200.             continue;
  201.         }
  202.  
  203.        
  204.         if (!SysconError(Buffer, BytesRead))
  205.         {
  206.             printf("Command:%s", LastCmd);
  207.             printf("Response:%s\n\n", Buffer);
  208.            
  209.         }
  210.  
  211.         if (GenerateNextCommand(Command, &CommandSize))
  212.         {
  213.             SendCommand(Comport, Command, CommandSize);
  214.             Sleep(30);
  215.         }
  216.         else
  217.         {
  218.             return;
  219.         }
  220.  
  221.         memset(Buffer,0,BytesRead);
  222.     }
  223.  
  224.     printf("%08X\n",GetLastError());
  225.     getchar();
  226. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top