Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "main.h"
- char *mainfunc;
- DWORD *dstack;
- DWORD stackp;
- int main(int argc, char *argv[])
- {
- char *filedata = NULL;
- HANDLE file = CreateFileA(argv[1], GENERIC_READ, FILE_SHARE_READ,
- NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (file == NULL) return 1;
- DWORD filesize = GetFileSize(file, NULL);
- filedata = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, filesize);
- ReadFile(file, filedata, filesize, &filesize, NULL);
- stackp = 0;
- dstack = (DWORD*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32);
- char *currpos = interpret(filedata, filedata, TRUE);
- while (currpos != NULL)
- {
- if (GetAsyncKeyState(VK_SPACE))
- {
- currpos = interpret(filedata, currpos, TRUE);
- Sleep(100);
- }
- }
- HeapFree(GetProcessHeap(), 0, dstack);
- HeapFree(GetProcessHeap(), 0, filedata);
- printf("\n*END OF PROGRAM*\n");
- while (!GetAsyncKeyState(VK_SPACE));
- return 0;
- }
- char *findblock(char *haystack, char *needle, size_t nsize)
- {
- char *haystackptr = haystack;
- char *cmpstr = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 8 + nsize);
- strncpy(cmpstr, ": ", 2); strncat(cmpstr, needle, nsize);
- while (*haystackptr++ != NULL)
- {
- if (strncmp(haystackptr, cmpstr, nsize) == 0)
- {
- //printf("FOUND NEEDLE @ %d\n", (haystackptr - haystack) + nsize + 2);
- HeapFree(GetProcessHeap(), 0, cmpstr);
- return haystackptr + nsize + 3; //skip \n
- }
- }
- HeapFree(GetProcessHeap(), 0, cmpstr);
- return NULL;
- }
- char *interpret(char *full, char *at, BOOL run)
- {
- if (*at == ' ' || *at == '\n') return (at + 1);
- char *tptr = NULL;
- char *part1 = at;
- DWORD len1 = 0;
- tptr = part1; while (*tptr++ != ' ') { if (*tptr==NULL) return NULL; len1++; }
- //*(part1 + len1) = NULL; printf("PART1 '%s'\t", part1); *(part1 + len1) = ' ';
- char *part2 = at + len1 + 1;
- DWORD len2 = 0;
- tptr = part2; while (*tptr++ != '\n') { if (*tptr==NULL) return NULL; len2++; }
- //*(part2 + len2) = NULL; printf("PART2 '%s'\n", part2); *(part2 + len2) = '\n';
- if (run == TRUE)
- {
- if (strncmp(part1, "__MAIN", 6) == 0) {
- mainfunc = findblock(full, part2, len2);
- return mainfunc;
- } else if (strncmp(part1, ":", 1) == 0) {
- if (findblock(full, part2, len2) != mainfunc) return NULL;
- } else if (strncmp(part1, "GOTO", 4) == 0) {
- char *part2ptr = part2;
- while (*part2ptr != '\n' && *part2ptr != '+') *part2ptr++;
- if (*part2ptr != '\n' && *part2ptr != NULL) {
- *(part2 + len2) = NULL;
- int maxj = atoi(part2ptr) - 1;
- *(part2 + len2) = '\n';
- char *px = findblock(full, part2, len2);
- int j = 0; for (; j < maxj; j++)
- px = interpret(full, px, FALSE);
- return px;
- } else return findblock(full, part2, len2);
- } else if (strncmp(part1, "CALL", 4) == 0) {
- DWORD offset = (part2-full) + len2 + 1;
- stack_push(dstack, offset);
- return findblock(full, part2, len2);
- } else if (strncmp(part1, "RETURN", 6) == 0) {
- DWORD offset = stack_pop(dstack);
- return (full + offset);
- } else if (strncmp(part1, "PRINT", 5) == 0) {
- *(part2 + len2 - 1) = NULL;
- if (strncmp(part2+len2-3, "\\n", 2) == 0) {
- *(part2 + len2 - 3) = NULL;
- printf("%s\n", part2+1);
- *(part2 + len2 - 3) = '\\';
- } else printf("%s", part2 + 1);
- *(part2 + len2 - 1) = '"';
- } else if (strncmp(part1, "SLEEP", 5) == 0) {
- *(part2 + len2) = NULL;
- Sleep(atoi(part2));
- *(part2 + len2) = '\n';
- }
- }
- return (part2 + len2 + 1);
- }
- void stack_push(DWORD *stack, DWORD val)
- {
- if (stackp == 32) return; //stack full
- stack[stackp++] = val;
- }
- DWORD stack_pop(DWORD *stack)
- {
- if (stackp == 0) return 0; //stack empty
- return stack[--stackp];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement