Advertisement
captmicro

myscript

Sep 15th, 2010
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.62 KB | None | 0 0
  1. #include "main.h"
  2.  
  3. char *mainfunc;
  4. DWORD *dstack;
  5. DWORD stackp;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     char *filedata = NULL;
  10.     HANDLE file = CreateFileA(argv[1], GENERIC_READ, FILE_SHARE_READ,
  11.         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  12.     if (file == NULL) return 1;
  13.     DWORD filesize = GetFileSize(file, NULL);
  14.     filedata = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, filesize);
  15.     ReadFile(file, filedata, filesize, &filesize, NULL);
  16.  
  17.     stackp = 0;
  18.     dstack = (DWORD*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32);
  19.     char *currpos = interpret(filedata, filedata, TRUE);
  20.     while (currpos != NULL)
  21.     {
  22.         if (GetAsyncKeyState(VK_SPACE))
  23.         {
  24.             currpos = interpret(filedata, currpos, TRUE);
  25.             Sleep(100);
  26.         }
  27.     }
  28.     HeapFree(GetProcessHeap(), 0, dstack);
  29.  
  30.     HeapFree(GetProcessHeap(), 0, filedata);
  31.     printf("\n*END OF PROGRAM*\n");
  32.     while (!GetAsyncKeyState(VK_SPACE));
  33.     return 0;
  34. }
  35.  
  36. char *findblock(char *haystack, char *needle, size_t nsize)
  37. {
  38.     char *haystackptr = haystack;
  39.     char *cmpstr = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 8 + nsize);
  40.     strncpy(cmpstr, ": ", 2); strncat(cmpstr, needle, nsize);
  41.     while (*haystackptr++ != NULL)
  42.     {
  43.         if (strncmp(haystackptr, cmpstr, nsize) == 0)
  44.         {
  45.             //printf("FOUND NEEDLE @ %d\n", (haystackptr - haystack) + nsize + 2);
  46.             HeapFree(GetProcessHeap(), 0, cmpstr);
  47.             return haystackptr + nsize + 3; //skip \n
  48.         }
  49.     }
  50.     HeapFree(GetProcessHeap(), 0, cmpstr);
  51.     return NULL;
  52. }
  53.  
  54. char *interpret(char *full, char *at, BOOL run)
  55. {
  56.     if (*at == ' ' || *at == '\n') return (at + 1);
  57.  
  58.     char *tptr = NULL;
  59.  
  60.     char *part1 = at;
  61.     DWORD len1 = 0;
  62.     tptr = part1; while (*tptr++ != ' ') { if (*tptr==NULL) return NULL; len1++; }
  63.     //*(part1 + len1) = NULL; printf("PART1 '%s'\t", part1); *(part1 + len1) = ' ';
  64.  
  65.     char *part2 = at + len1 + 1;
  66.     DWORD len2 = 0;
  67.     tptr = part2; while (*tptr++ != '\n') { if (*tptr==NULL) return NULL; len2++; }
  68.     //*(part2 + len2) = NULL; printf("PART2 '%s'\n", part2); *(part2 + len2) = '\n';
  69.  
  70.     if (run == TRUE)
  71.     {
  72.         if (strncmp(part1, "__MAIN", 6) == 0) {
  73.             mainfunc = findblock(full, part2, len2);
  74.             return mainfunc;
  75.         } else if (strncmp(part1, ":", 1) == 0) {
  76.             if (findblock(full, part2, len2) != mainfunc) return NULL;
  77.         } else if (strncmp(part1, "GOTO", 4) == 0) {
  78.             char *part2ptr = part2;
  79.             while (*part2ptr != '\n' && *part2ptr != '+') *part2ptr++;
  80.             if (*part2ptr != '\n' && *part2ptr != NULL) {
  81.                 *(part2 + len2) = NULL;
  82.                 int maxj = atoi(part2ptr) - 1;
  83.                 *(part2 + len2) = '\n';
  84.                 char *px = findblock(full, part2, len2);
  85.                 int j = 0; for (; j < maxj; j++)
  86.                     px = interpret(full, px, FALSE);
  87.                 return px;
  88.             } else return findblock(full, part2, len2);
  89.         } else if (strncmp(part1, "CALL", 4) == 0) {
  90.             DWORD offset = (part2-full) + len2 + 1;
  91.             stack_push(dstack, offset);
  92.             return findblock(full, part2, len2);
  93.         } else if (strncmp(part1, "RETURN", 6) == 0) {
  94.             DWORD offset = stack_pop(dstack);
  95.             return (full + offset);
  96.         } else if (strncmp(part1, "PRINT", 5) == 0) {
  97.             *(part2 + len2 - 1) = NULL;
  98.             if (strncmp(part2+len2-3, "\\n", 2) == 0) {
  99.                 *(part2 + len2 - 3) = NULL;
  100.                 printf("%s\n", part2+1);
  101.                 *(part2 + len2 - 3) = '\\';
  102.             } else printf("%s", part2 + 1);
  103.             *(part2 + len2 - 1) = '"';
  104.         } else if (strncmp(part1, "SLEEP", 5) == 0) {
  105.             *(part2 + len2) = NULL;
  106.             Sleep(atoi(part2));
  107.             *(part2 + len2) = '\n';
  108.         }
  109.     }
  110.  
  111.     return (part2 + len2 + 1);
  112. }
  113.  
  114. void stack_push(DWORD *stack, DWORD val)
  115. {
  116.     if (stackp == 32) return; //stack full
  117.     stack[stackp++] = val;
  118.  
  119. }
  120.  
  121. DWORD stack_pop(DWORD *stack)
  122. {
  123.     if (stackp == 0) return 0; //stack empty
  124.     return stack[--stackp];
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement