void as_set_current_file(asgard_handle * handle, fs_file * file) { #if USE_ENTRY_STACK as_push(handle, file); //temporaly use as_push #else if(handle->cur_file != NULL) free(handle->cur_file); if(file->entry.type & FS_TYPE_DIR) handle->cur_dir = file; //check if currently selected file is directory then set handle->cur_dir handle->cur_file = file; #endif }
int main(int argc, char **argv) { // Open file FILE * fp; if (argc == 2) { fp = fopen(argv[1], "r"); if (!fp) { fprintf(stderr, "Could not open file: %s\n", argv[1]); return 1; } } else { fp = stdin; } // Initialize the stack ArrayStack stack; as_init(&stack); // Read the input line by line char * line = NULL; size_t len = 0; ssize_t read; int return_value = 0; size_t line_number = 1; while ((read = getline(&line, &len, fp)) != -1) { for (int i = 0; i < read; i++) { switch (line[i]) { case '(': case '[': case '{': { // Push opening parenthesis to the stack char *c = malloc(sizeof(char)); *c = line[i]; as_push(&stack, c); break; } case ')': case ']': case '}': { // Pop closing parenthesis from the stack and see if it // matches if (as_is_empty(&stack)) { fprintf(stderr, "Unbalanced closing %c at line %ld.\n", line[i], line_number); return_value = 1; goto cleanup; } char *c = as_pop(&stack); if ((*c == '(' && line[i]!= ')') || (*c == '[' && line[i]!= ']') || (*c == '{' && line[i]!= '}')) { fprintf(stderr, "Unexpected %d at line %ld.\n", line[i], line_number); return_value = 1; goto cleanup; } free(c); break; } } } line_number++; } // Check if there are any unclosed parenthesis if (!as_is_empty(&stack)) { fprintf(stderr, "Unclosed parentheses.\n"); return_value = 1; goto cleanup; } // Clean up cleanup: while(!as_is_empty(&stack)) { char *c = as_pop(&stack); free(c); } as_destroy(&stack); fclose(fp); if (line) { free(line); } return return_value; }
void test_stack_instructions() { asm_p as = &(asm_t){ 0 }; char* disassembly = NULL; as_new(as); as_clear(as); as_push(as, imm(0x11223344)); for(size_t i = 0; i < 16; i++) as_push(as, reg(i)); for(size_t i = 0; i < 16; i++) as_push(as, memrd(reg(i), 0xbeba)); disassembly = disassemble(as); st_check_str(disassembly, "push 0x11223344\n" "push rax\n" "push rcx\n" "push rdx\n" "push rbx\n" "push rsp\n" "push rbp\n" "push rsi\n" "push rdi\n" "push r8\n" "push r9\n" "push r10\n" "push r11\n" "push r12\n" "push r13\n" "push r14\n" "push r15\n" "push QWORD PTR [rax+0xbeba]\n" "push QWORD PTR [rcx+0xbeba]\n" "push QWORD PTR [rdx+0xbeba]\n" "push QWORD PTR [rbx+0xbeba]\n" "push QWORD PTR [rsp+0xbeba]\n" "push QWORD PTR [rbp+0xbeba]\n" "push QWORD PTR [rsi+0xbeba]\n" "push QWORD PTR [rdi+0xbeba]\n" "push QWORD PTR [r8+0xbeba]\n" "push QWORD PTR [r9+0xbeba]\n" "push QWORD PTR [r10+0xbeba]\n" "push QWORD PTR [r11+0xbeba]\n" "push QWORD PTR [r12+0xbeba]\n" "push QWORD PTR [r13+0xbeba]\n" "push QWORD PTR [r14+0xbeba]\n" "push QWORD PTR [r15+0xbeba]\n" ); as_clear(as); for(size_t i = 0; i < 16; i++) as_pop(as, reg(i)); for(size_t i = 0; i < 16; i++) as_pop(as, memrd(reg(i), 0xbeba)); disassembly = disassemble(as); st_check_str(disassembly, "pop rax\n" "pop rcx\n" "pop rdx\n" "pop rbx\n" "pop rsp\n" "pop rbp\n" "pop rsi\n" "pop rdi\n" "pop r8\n" "pop r9\n" "pop r10\n" "pop r11\n" "pop r12\n" "pop r13\n" "pop r14\n" "pop r15\n" "pop QWORD PTR [rax+0xbeba]\n" "pop QWORD PTR [rcx+0xbeba]\n" "pop QWORD PTR [rdx+0xbeba]\n" "pop QWORD PTR [rbx+0xbeba]\n" "pop QWORD PTR [rsp+0xbeba]\n" "pop QWORD PTR [rbp+0xbeba]\n" "pop QWORD PTR [rsi+0xbeba]\n" "pop QWORD PTR [rdi+0xbeba]\n" "pop QWORD PTR [r8+0xbeba]\n" "pop QWORD PTR [r9+0xbeba]\n" "pop QWORD PTR [r10+0xbeba]\n" "pop QWORD PTR [r11+0xbeba]\n" "pop QWORD PTR [r12+0xbeba]\n" "pop QWORD PTR [r13+0xbeba]\n" "pop QWORD PTR [r14+0xbeba]\n" "pop QWORD PTR [r15+0xbeba]\n" ); free(disassembly); }
uint16 as_select(asgard_handle * handle, uint16 fid) { fs_file * cur_file = NULL; fs_file * root = NULL; #if USE_ENTRY_STACK if(handle->stack_index > 2 && handle->stack_index <=8) { cur_file = handle->stack[handle->stack_index -2]; //check for parent if(cur_file != NULL) { if(cur_file->entry.fid == fid) { as_pop(handle); //freed current file goto as_select_finished; } } } cur_file = fs_fopen(as_peek(handle), fid, handle->mode); //select child as_select_finished: if(cur_file != NULL) { as_push(handle, cur_file); return APDU_SUCCESS_RESPONSE; } cur_file = as_peek(handle); if(cur_file != NULL && (file->entry.type & FS_TYPE_DIR)) { //entry exist and type directory as_push(handle, NULL); //push no selected file } #else //printf("select\n"); if(fid == FID_MF) { root = fs_root(handle->fs); if(root == NULL) return APDU_FILE_NOT_FOUND; //printf("root : %x\n", root); cur_file = fs_fopen(root, FID_MF, handle->mode); if(handle->cur_dir != NULL) { free(handle->cur_dir); handle->cur_dir = NULL; } if(handle->cur_file != NULL && handle->cur_dir != handle->cur_file) { free(handle->cur_file); handle->cur_file = NULL; } handle->cur_dir = cur_file; handle->cur_file = cur_file; free(root); //prevent memory leakage if(cur_file != NULL) return APDU_SUCCESS_RESPONSE; //selection successful } if(handle->cur_dir != NULL && fid == handle->cur_dir->entry.fid) return APDU_SUCCESS_RESPONSE; if(handle->cur_file != NULL && fid == handle->cur_file->entry.fid) return APDU_SUCCESS_RESPONSE; //m_mem_dump(); //printf("select : %x\n", handle->cur_dir->entry.fid); //printf("handle->cur_dir : %x\n", handle->cur_dir); cur_file = fs_fopen(handle->cur_dir, fid, handle->mode); //if(cur_file == NULL) printf("file not found\n"); //getch(); if(cur_file == NULL) return APDU_FILE_NOT_FOUND; if(cur_file->entry.type & FS_TYPE_DIR) { //type is directory //printf("handle is directory\n"); if(handle->cur_dir != NULL) { free(handle->cur_dir); handle->cur_dir = NULL; } if(handle->cur_file != NULL && handle->cur_dir != handle->cur_file) { free(handle->cur_file); handle->cur_file = NULL; } handle->cur_dir = cur_file; handle->cur_file = cur_file; //getch(); if(cur_file != NULL) return APDU_SUCCESS_RESPONSE; //selection successful } else { //type is file //if(handle->cur_dir != NULL) { free(handle->cur_dir); handle->cur_dir = NULL; } if(handle->cur_file != NULL && handle->cur_dir != handle->cur_file) { free(handle->cur_file); handle->cur_file = NULL; } handle->cur_file = cur_file; //getch(); if(cur_file != NULL) return APDU_SUCCESS_RESPONSE; //selection successful } #endif return APDU_FILE_NOT_FOUND; }
void test_stack_instructions() { asm_p as = &(asm_t){ 0 }; char* disassembly = NULL; as_free(as); as_push(as, as_imm(1, 0x7a)); as_push(as, as_imm(2, 0x1122)); as_push(as, as_imm(4, 0x1122334455667788)); disassembly = disassemble(as); st_check_str(disassembly, "push 0x7a\n" "pushw 0x1122\n" "push 0x55667788\n" ); as_free(as); for(size_t i = 0; i < 16; i++) as_push(as, as_reg(8, i)); for(size_t i = 0; i < 16; i++) as_push(as, as_mem_rd(8, as_reg(8, i), 0xbeba)); disassembly = disassemble(as); st_check_str(disassembly, "push rax\n" "push rcx\n" "push rdx\n" "push rbx\n" "push rsp\n" "push rbp\n" "push rsi\n" "push rdi\n" "push r8\n" "push r9\n" "push r10\n" "push r11\n" "push r12\n" "push r13\n" "push r14\n" "push r15\n" "push QWORD PTR [rax+0xbeba]\n" "push QWORD PTR [rcx+0xbeba]\n" "push QWORD PTR [rdx+0xbeba]\n" "push QWORD PTR [rbx+0xbeba]\n" "push QWORD PTR [rsp+0xbeba]\n" "push QWORD PTR [rbp+0xbeba]\n" "push QWORD PTR [rsi+0xbeba]\n" "push QWORD PTR [rdi+0xbeba]\n" "push QWORD PTR [r8+0xbeba]\n" "push QWORD PTR [r9+0xbeba]\n" "push QWORD PTR [r10+0xbeba]\n" "push QWORD PTR [r11+0xbeba]\n" "push QWORD PTR [r12+0xbeba]\n" "push QWORD PTR [r13+0xbeba]\n" "push QWORD PTR [r14+0xbeba]\n" "push QWORD PTR [r15+0xbeba]\n" ); as_free(as); for(size_t i = 0; i < 16; i++) as_pop(as, as_reg(8, i)); for(size_t i = 0; i < 16; i++) as_pop(as, as_mem_rd(8, as_reg(8, i), 0xbeba)); disassembly = disassemble(as); st_check_str(disassembly, "pop rax\n" "pop rcx\n" "pop rdx\n" "pop rbx\n" "pop rsp\n" "pop rbp\n" "pop rsi\n" "pop rdi\n" "pop r8\n" "pop r9\n" "pop r10\n" "pop r11\n" "pop r12\n" "pop r13\n" "pop r14\n" "pop r15\n" "pop QWORD PTR [rax+0xbeba]\n" "pop QWORD PTR [rcx+0xbeba]\n" "pop QWORD PTR [rdx+0xbeba]\n" "pop QWORD PTR [rbx+0xbeba]\n" "pop QWORD PTR [rsp+0xbeba]\n" "pop QWORD PTR [rbp+0xbeba]\n" "pop QWORD PTR [rsi+0xbeba]\n" "pop QWORD PTR [rdi+0xbeba]\n" "pop QWORD PTR [r8+0xbeba]\n" "pop QWORD PTR [r9+0xbeba]\n" "pop QWORD PTR [r10+0xbeba]\n" "pop QWORD PTR [r11+0xbeba]\n" "pop QWORD PTR [r12+0xbeba]\n" "pop QWORD PTR [r13+0xbeba]\n" "pop QWORD PTR [r14+0xbeba]\n" "pop QWORD PTR [r15+0xbeba]\n" ); as_free(as); free(disassembly); }