static inline __u32 ipv6_addr_hash(struct __sk_buff *ctx, __u64 off) { __u64 w0 = load_word(ctx, off); __u64 w1 = load_word(ctx, off + 4); __u64 w2 = load_word(ctx, off + 8); __u64 w3 = load_word(ctx, off + 12); return (__u32)(w0 ^ w1 ^ w2 ^ w3); }
void check_storeload(){ create_segment(1, 0x1); create_segment(5, 0x2); create_segment(10, 0x3); store_word(0x1, 0, 0x1); store_word(0x2, 4, 0x10); store_word(0x3, 4, 0x100); uint64_t a = load_word(1, 0); uint64_t b = load_word(2, 4); uint64_t c = load_word(3, 4); assert(a == 0x1 && b == 0x10 && c == 0x100); }
static void *check_data(u8 *buf, size_t bsize, u32 *swap) { u32 word, p = 0; /* possition */ /* Because buf doesn't need to be aligned let's read it by chars */ for (p = 0; p < bsize; p++) { word = load_word(&buf[p], SWAP_NO); debug("%s: word %x %x/%x\n", __func__, word, p, (u32)&buf[p]); /* Find the first bitstream dummy word */ if (word == DUMMY_WORD) { debug("%s: Found dummy word at position %x/%x\n", __func__, p, (u32)&buf[p]); *swap = check_header(&buf[p]); if (*swap) { /* FIXME add full bitstream checking here */ return &buf[p]; } } /* Loop can be huge - support CTRL + C */ if (ctrlc()) return 0; } return 0; }
int ajout_pile( mem memory, registre *reg, uint32_t ajout ) { load_word( memory, reg[29].val, ajout ); DEBUG_MSG( "0x%08x a été ajouté à la pile à l'adresse 0x%08x \n", ajout, reg[29].val ); reg[29].val += 4; return cmd_ok; }
static u32 check_header(const void *buf) { u32 i, pattern; int swap = SWAP_NO; u32 *test = (u32 *)buf; debug("%s: Let's check bitstream header\n", __func__); /* Checking that passing bin is not a bitstream */ for (i = 0; i < ARRAY_SIZE(bin_format); i++) { pattern = load_word(&test[i], swap); /* * Bitstreams in binary format are swapped * compare to regular bistream. * Do not swap dummy word but if swap is done assume * that parsing buffer is binary format */ if ((__swab32(pattern) != DUMMY_WORD) && (__swab32(pattern) == bin_format[i])) { swap = SWAP_DONE; debug("%s: data swapped - let's swap\n", __func__); } debug("%s: %d/%px: pattern %x/%x bin_format\n", __func__, i, &test[i], pattern, bin_format[i]); } debug("%s: Found bitstream header at %px %s swapinng\n", __func__, buf, swap == SWAP_NO ? "without" : "with"); return swap; }
int enleve_pile( mem memory, registre *reg, uint32_t* res ) { reg[29].val += -4; find_word( memory, reg[29].val, &res ); load_word( memory, reg[29].val, 0 ); DEBUG_MSG("0x%08x a été enlevé de la pile à l'adresse 0x%08x \n", *res, reg[29].val ); return cmd_ok; }
static inline void set_tcp_ip_src(struct __sk_buff *skb, __u32 new_ip) { __u32 old_ip = _htonl(load_word(skb, IP_SRC_OFF)); bpf_l4_csum_replace(skb, TCP_CSUM_OFF, old_ip, new_ip, IS_PSEUDO | sizeof(new_ip)); bpf_l3_csum_replace(skb, IP_CSUM_OFF, old_ip, new_ip, sizeof(new_ip)); bpf_skb_store_bytes(skb, IP_SRC_OFF, &new_ip, sizeof(new_ip), 0); }
void opcode_pull(void) { uint16_t value = 0; uint16_t spare_slots; uint8_t *user_stack; TRACE_LOG("Opcode: PULL.\n"); if (ver == 6) (void)read_z_result_variable(); if ( (ver != 6) || (number_of_operands < 1) ) { if ( (stack_words_from_active_routine == 0) && (bool_equal(skip_active_routines_stack_check_warning, false)) ) { i18n_translate_and_exit( libfizmo_module_name, i18n_libfizmo_NOT_ENOUGH_STACK_WORDS_FROM_LOCAL_ROUTINE_ON_STACK, -1); } else { TRACE_LOG("Pulling to variable %x.\n", op[0]); value = z_stack_pull_word(); stack_words_from_active_routine--; } } else { user_stack = z_mem + (uint16_t)op[0]; spare_slots = load_word(user_stack); spare_slots++; value = load_word(user_stack + spare_slots); store_word(user_stack, spare_slots); } if (ver == 6) set_variable(z_res_var, value, true); else set_variable(op[0], value, true); }
uint16_t get_variable(uint8_t variable_number, bool keep_stack_index) { uint16_t result; if (variable_number == 0) { if (stack_words_from_active_routine == 0) { if (bool_equal(skip_active_routines_stack_check_warning, false)) i18n_translate_and_exit( libfizmo_module_name, i18n_libfizmo_NOT_ENOUGH_STACK_WORDS_FROM_LOCAL_ROUTINE_ON_STACK, -1); else return 0; } if (bool_equal(keep_stack_index, true)) result = z_stack_peek_word(); else { result = z_stack_pull_word(); stack_words_from_active_routine--; } return result; } else if (variable_number < 0x10) { if (variable_number > number_of_locals_active) i18n_translate_and_exit( libfizmo_module_name, i18n_libfizmo_TRYING_TO_STORE_VARIABLE_L_P0D_BUT_ONLY_P1D_VARIABLES_ACTIVE, -1, (long int)variable_number-1, (long int)number_of_locals_active); variable_number--; result = local_variable_storage_index[variable_number]; TRACE_LOG("Reading %x from L0%x.\n", result, variable_number); return result; } else { variable_number -= 0x10; result = load_word(active_z_story->global_variables+(variable_number*2)); TRACE_LOG("Reading %x from global variable G%02x.\n", result, variable_number); return result; } }
static ulong zynqmp_align_dma_buffer(u32 *buf, u32 len, u32 swap) { u32 *new_buf; u32 i; if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) { new_buf = (u32 *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN); /* * This might be dangerous but permits to flash if * ARCH_DMA_MINALIGN is greater than header size */ if (new_buf > (u32 *)buf) { debug("%s: Aligned buffer is after buffer start\n", __func__); new_buf -= ARCH_DMA_MINALIGN; } printf("%s: Align buffer at %px to %px(swap %d)\n", __func__, buf, new_buf, swap); for (i = 0; i < (len/4); i++) new_buf[i] = load_word(&buf[i], swap); buf = new_buf; } else if (swap != SWAP_DONE) { /* For bitstream which are aligned */ u32 *new_buf = (u32 *)buf; printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__, swap); for (i = 0; i < (len/4); i++) new_buf[i] = load_word(&buf[i], swap); } return (ulong)buf; }
int main(int argc, char *argv[]) { fw_dir_t dir; WikiScanFile *wsf; struct file_st file[10]; ffi_arg_t *ff = &m_ffi_arg; ffi_init_option(argc, argv); strcpy(dir.path, ff->dir); wsf = new WikiScanFile(); int total = wsf->wsf_scan_file(file, sizeof(file) / sizeof(struct file_st), &dir, 1); if (total > 1) { printf("found many language data in this folder.\n"); exit(0); } if (total == 0) { printf("Not found any fastwiki idx file and dat files in this folder\n"); printf("run 'fastwiki-full-index -h' to see help\n"); exit(0); } m_word_hash = new SHash(); m_word_hash->sh_set_hash_magic(get_best_hash(800*10000)); m_word_hash->sh_init(10*10000, sizeof(struct wfi_tmp_key), 0); if (ff->word_file[0]) { if (load_word(ff->word_file) == -1) { printf("load file %s error\n", ff->word_file); return 1; } } start_create_index(file[0].index_file, file[0].data_file, file[0].data_total, ff->tmp, ff->mem_size, "gzip"); return 0; }
int main(int argc, char const *argv[]) { char* source = "わたしわ阿飞, and my English name is Rex Lee. 网名是独孤影! ^_^。下面是一段多音分词歧义测试,这个人无伤无臭味磕头如捣蒜。"; // char* source = "hello world, here is an English string to test buffer transfer. thank you."; char* pinyin = 0; PinTable dict; pinyin_init(&dict); pinyin_set_locale("zh_CN.UTF-8"); // load_char("/root/pinyin-php/data/chars.csv", &dict); load_word("../data/words.csv", &dict); load_char("../data/chars.csv", &dict); printf("[source] \n%s\n\n", source); printf("[transl] \n"); pinyin = pinyin_translate(source, &dict); printf("%s\n", pinyin); free_buffer(pinyin); // HashNode* r = ht_lookup(&dict, "下"); // printf("[L]%d\n", dict.ht_size); // printf("t[%s]\n", r->nValue); // ht_print(&dict); // char buf[256]={"わたしわ阿飞, and"}; // setlocale(LC_ALL,"zh_CN.UTF-8"); // wchar_t ar[256]={'\0'}; // int read = mbstowcs(ar,buf,strlen(buf)); // printf("%lu\n",strlen(buf)); //输出为:9 [字节] UF-8编码下一个汉字占三个字节3*3=9 // printf("%d\n",read); //输出为:3 [个数] “你好啊”三个子字个数 return 0; }
void opcode_push_user_stack(void) { uint16_t spare_slots; uint8_t *user_stack; TRACE_LOG("Opcode: PUSH_USER_STACK.\n"); (void)read_z_result_variable(); user_stack = z_mem + (uint16_t)op[1]; if ((spare_slots = load_word(user_stack)) > 0) { store_word(user_stack + spare_slots, (uint16_t)op[0]); spare_slots--; store_word(user_stack, spare_slots); evaluate_branch((uint8_t)1); } else { evaluate_branch((uint8_t)0); } }
void opcode_loadw(void) { uint16_t value; uint8_t *address = z_mem + (uint16_t)(op[0] + ((int16_t)op[1])*2); TRACE_LOG("Opcode: LOADW.\n"); read_z_result_variable(); if (address > active_z_story->static_memory_end) { TRACE_LOG("ERROR: Trying to loadw from %x which is above static memory.\n", address); set_variable(z_res_var, 0, false); } else { value = load_word(address); TRACE_LOG("Loading %x from %x var %x.\n", value, address, z_res_var); set_variable(z_res_var, value, false); } }
/** * Copy @size bytes from the buffer @src_tracer to the address * @dest_tracee within the memory space of the @tracee process. It * returns -errno if an error occured, otherwise 0. */ int write_data(const Tracee *tracee, word_t dest_tracee, const void *src_tracer, word_t size) { word_t *src = (word_t *)src_tracer; word_t *dest = (word_t *)dest_tracee; long status; word_t word, i, j; word_t nb_trailing_bytes; word_t nb_full_words; uint8_t *last_dest_word; uint8_t *last_src_word; if (belongs_to_heap_prealloc(tracee, dest_tracee)) return -EFAULT; #if defined(HAVE_PROCESS_VM) struct iovec local; struct iovec remote; local.iov_base = src; local.iov_len = size; remote.iov_base = dest; remote.iov_len = size; status = process_vm_writev(tracee->pid, &local, 1, &remote, 1, 0); if ((size_t) status == size) return 0; /* Fallback to ptrace if something went wrong. */ #endif /* HAVE_PROCESS_VM */ nb_trailing_bytes = size % sizeof(word_t); nb_full_words = (size - nb_trailing_bytes) / sizeof(word_t); /* Copy one word by one word, except for the last one. */ for (i = 0; i < nb_full_words; i++) { status = ptrace(PTRACE_POKEDATA, tracee->pid, dest + i, load_word(&src[i])); if (status < 0) { note(tracee, WARNING, SYSTEM, "ptrace(POKEDATA)"); return -EFAULT; } } if (nb_trailing_bytes == 0) return 0; /* Copy the bytes in the last word carefully since we have to * overwrite only the relevant ones. */ word = ptrace(PTRACE_PEEKDATA, tracee->pid, dest + i, NULL); if (errno != 0) { note(tracee, WARNING, SYSTEM, "ptrace(PEEKDATA)"); return -EFAULT; } last_dest_word = (uint8_t *)&word; last_src_word = (uint8_t *)&src[i]; for (j = 0; j < nb_trailing_bytes; j++) last_dest_word[j] = last_src_word[j]; status = ptrace(PTRACE_POKEDATA, tracee->pid, dest + i, word); if (status < 0) { note(tracee, WARNING, SYSTEM, "ptrace(POKEDATA)"); return -EFAULT; } return 0; }
int zynq_load(Xilinx_desc *desc, const void *buf, size_t bsize) { unsigned long ts; /* Timestamp */ u32 partialbit = 0; u32 i, control, isr_status, status, swap, diff; u32 *buf_start; /* Detect if we are going working with partial or full bitstream */ if (bsize != desc->size) { printf("%s: Working with partial bitstream\n", __func__); partialbit = 1; } buf_start = check_data((u8 *)buf, bsize, &swap); if (!buf_start) return FPGA_FAIL; /* Check if data is postpone from start */ diff = (u32)buf_start - (u32)buf; if (diff) { printf("%s: Bitstream is not validated yet (diff %x)\n", __func__, diff); return FPGA_FAIL; } if ((u32)buf_start & 0x3) { u32 *new_buf = (u32 *)((u32)buf & ~0x3); printf("%s: Align buffer at %x to %x(swap %d)\n", __func__, (u32)buf_start, (u32)new_buf, swap); for (i = 0; i < (bsize/4); i++) new_buf[i] = load_word(&buf_start[i], swap); swap = SWAP_DONE; buf = new_buf; } else if (swap != SWAP_DONE) { /* For bitstream which are aligned */ u32 *new_buf = (u32 *)buf; printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__, swap); for (i = 0; i < (bsize/4); i++) new_buf[i] = load_word(&buf_start[i], swap); swap = SWAP_DONE; } /* Clear loopback bit */ clrbits_le32(&devcfg_base->mctrl, DEVCFG_MCTRL_PCAP_LPBK); if (!partialbit) { zynq_slcr_devcfg_disable(); /* Setting PCFG_PROG_B signal to high */ control = readl(&devcfg_base->ctrl); writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl); /* Setting PCFG_PROG_B signal to low */ writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl); /* Polling the PCAP_INIT status for Reset */ ts = get_timer(0); while (readl(&devcfg_base->status) & DEVCFG_STATUS_PCFG_INIT) { if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) { printf("%s: Timeout wait for INIT to clear\n", __func__); return FPGA_FAIL; } } /* Setting PCFG_PROG_B signal to high */ writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl); /* Polling the PCAP_INIT status for Set */ ts = get_timer(0); while (!(readl(&devcfg_base->status) & DEVCFG_STATUS_PCFG_INIT)) { if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) { printf("%s: Timeout wait for INIT to set\n", __func__); return FPGA_FAIL; } } } isr_status = readl(&devcfg_base->int_sts); /* Clear it all, so if Boot ROM comes back, it can proceed */ writel(0xFFFFFFFF, &devcfg_base->int_sts); if (isr_status & DEVCFG_ISR_FATAL_ERROR_MASK) { debug("%s: Fatal errors in PCAP 0x%X\n", __func__, isr_status); /* If RX FIFO overflow, need to flush RX FIFO first */ if (isr_status & DEVCFG_ISR_RX_FIFO_OV) { writel(DEVCFG_MCTRL_RFIFO_FLUSH, &devcfg_base->mctrl); writel(0xFFFFFFFF, &devcfg_base->int_sts); } return FPGA_FAIL; } status = readl(&devcfg_base->status); debug("%s: Status = 0x%08X\n", __func__, status); if (status & DEVCFG_STATUS_DMA_CMD_Q_F) { debug("%s: Error: device busy\n", __func__); return FPGA_FAIL; } debug("%s: Device ready\n", __func__); if (!(status & DEVCFG_STATUS_DMA_CMD_Q_E)) { if (!(readl(&devcfg_base->int_sts) & DEVCFG_ISR_DMA_DONE)) { /* Error state, transfer cannot occur */ debug("%s: ISR indicates error\n", __func__); return FPGA_FAIL; } else { /* Clear out the status */ writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts); } } if (status & DEVCFG_STATUS_DMA_DONE_CNT_MASK) { /* Clear the count of completed DMA transfers */ writel(DEVCFG_STATUS_DMA_DONE_CNT_MASK, &devcfg_base->status); } debug("%s: Source = 0x%08X\n", __func__, (u32)buf); debug("%s: Size = %zu\n", __func__, bsize); /* Set up the transfer */ writel((u32)buf | 1, &devcfg_base->dma_src_addr); writel(0xFFFFFFFF, &devcfg_base->dma_dst_addr); writel(bsize >> 2, &devcfg_base->dma_src_len); writel(0, &devcfg_base->dma_dst_len); isr_status = readl(&devcfg_base->int_sts); /* Polling the PCAP_INIT status for Set */ ts = get_timer(0); while (!(isr_status & DEVCFG_ISR_DMA_DONE)) { if (isr_status & DEVCFG_ISR_ERROR_FLAGS_MASK) { debug("%s: Error: isr = 0x%08X\n", __func__, isr_status); debug("%s: Write count = 0x%08X\n", __func__, readl(&devcfg_base->write_count)); debug("%s: Read count = 0x%08X\n", __func__, readl(&devcfg_base->read_count)); return FPGA_FAIL; } if (get_timer(ts) > CONFIG_SYS_FPGA_PROG_TIME) { printf("%s: Timeout wait for DMA to complete\n", __func__); return FPGA_FAIL; } isr_status = readl(&devcfg_base->int_sts); } debug("%s: DMA transfer is done\n", __func__); /* Check FPGA configuration completion */ ts = get_timer(0); while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) { if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) { printf("%s: Timeout wait for FPGA to config\n", __func__); return FPGA_FAIL; } isr_status = readl(&devcfg_base->int_sts); } debug("%s: FPGA config done\n", __func__); /* Clear out the DMA status */ writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts); if (!partialbit) zynq_slcr_devcfg_enable(); return FPGA_SUCCESS; }
int main() { struct EditStep edit_step[DICT_SIZE]; int len_list[WORD_SIZE+2]; int edit_step_count; int i; int idx; int ans; for (i = 0; i < ARRAY_SIZE(len_list); ++i) { len_list[i] = -1; } edit_step_count = 0; load_word(&edit_step[edit_step_count]); insert_list(&edit_step[edit_step_count], edit_step_count, len_list); ans = 1; ++edit_step_count; while (load_word(&edit_step[edit_step_count]) == 0) { /* * step[i] = maximum the longest edit step end in i * * step[i] = max | step[j] + 1 if j -> i is one edit step. * | 1 * * To find a suitable j, we can use the balanced binary search tree. The * balanced B.S.T uses { length, step } as key. The suitable j will be: * * max step in | { strlen(i)-1, step } * | { strlen(i), step } * | { strlen(i)+1, step } * * We can use an array of list to store all previous words that have * particular length. * */ #if DEBUG printf("[edit step] start processing %s\n", edit_step[edit_step_count].word); #endif for (i = edit_step[edit_step_count].word_len-1; i <= edit_step[edit_step_count].word_len+1; ++i) { idx = len_list[i]; while (idx != -1) { if (is_one_step(&edit_step[idx], &edit_step[edit_step_count])) { edit_step[edit_step_count].step = max( edit_step[edit_step_count].step, edit_step[idx].step+1); #if DEBUG printf("[edit step] Found %s in step[%d] = %d\n", edit_step[idx].word, idx, edit_step[idx].step); printf("[edit step] Set step[%d] = %d\n", edit_step_count, edit_step[edit_step_count].step); #endif ans = max(ans, edit_step[edit_step_count].step); } idx = edit_step[idx].next; } } insert_list(&edit_step[edit_step_count], edit_step_count, len_list); ++edit_step_count; } printf("%d\n", ans); return 0; }
//Executes 'cmd' and increases program counter 'pc'. void execute_instruction(COMMAND cmd, COMMAND* commands_arr, int* registers_arr, byte* mem, int* pc){ /*Instruction Type: R-type*/ if (strcmp(cmd.cmd_type,"R") == 0){ if(stricmp(cmd.cmd_name,"add") == 0){ registers_arr[cmd.Rd] = registers_arr[cmd.Rs]+ registers_arr[cmd.Rt]; (*pc)++; }else if (stricmp(cmd.cmd_name,"sub") == 0){ registers_arr[cmd.Rd] = registers_arr[cmd.Rs] - registers_arr[cmd.Rt]; (*pc)++; }else if (stricmp(cmd.cmd_name,"mul") == 0){ registers_arr[cmd.Rd] = registers_arr[cmd.Rs]*registers_arr[cmd.Rt]; (*pc)++; }else if (stricmp(cmd.cmd_name,"div") == 0){ registers_arr[cmd.Rd] = registers_arr[cmd.Rs]/registers_arr[cmd.Rt]; (*pc)++; }else if (stricmp(cmd.cmd_name,"slt") == 0){ if (registers_arr[cmd.Rs] < registers_arr[cmd.Rt]){ registers_arr[cmd.Rd] = 1; }else{ registers_arr[cmd.Rd] = 0; } (*pc)++; } }else if (strcmp(cmd.cmd_type,"I") == 0){/*Instruction Type: I-type*/ if (stricmp(cmd.cmd_name,"addi") == 0){ registers_arr[cmd.Rt] = registers_arr[cmd.Rs] + atoi(cmd.immediate); (*pc)++; }else if (stricmp(cmd.cmd_name,"subi") == 0){ registers_arr[cmd.Rt]= registers_arr[cmd.Rs] - atoi(cmd.immediate); (*pc)++; }else if (stricmp(cmd.cmd_name,"lw") == 0){ int address = registers_arr[cmd.Rs] + atoi(cmd.immediate); registers_arr[cmd.Rt] = load_word(address, mem); (*pc)++; }else if (stricmp(cmd.cmd_name,"sw") == 0){ int address = registers_arr[cmd.Rs] + atoi(cmd.immediate); store_word (registers_arr[cmd.Rt], address, mem); (*pc)++; }else if (stricmp(cmd.cmd_name,"slti") == 0){ if (registers_arr[cmd.Rs] < atoi(cmd.immediate)){ registers_arr[cmd.Rt] = 1; }else{ registers_arr[cmd.Rt] = 0; } (*pc)++; }else if (stricmp(cmd.cmd_name,"beq") == 0){ if (cmd.Rs == cmd.Rt){ for(int i = 0; strcmp(commands_arr[i].cmd_type, "H") != 0; i++){ if (commands_arr[i].b_is_labled){ if (strcmp(cmd.immediate , commands_arr[i].label) == 0){ *pc = i; } }else{ continue; } } }else{ (*pc)++; } }else if (stricmp(cmd.cmd_name,"bne") == 0){ if (registers_arr[cmd.Rs] != registers_arr[cmd.Rt]){ for(int i = 0; strcmp(commands_arr[i].cmd_type, "H") != 0; i++){ if (commands_arr[i].b_is_labled){ if (strcmp(cmd.immediate , commands_arr[i].label) == 0){ *pc = i; break; } }else{ continue; } } }else{ (*pc)++; } } }else{/*Instruction Type: J-type*/ if (stricmp(cmd.cmd_name,"j") == 0){ for(int i = 0; strcmp(commands_arr[i].cmd_type, "H") != 0; i++){ if (commands_arr[i].b_is_labled){ if (strcmp(cmd.address_label , commands_arr[i].label) == 0){ (*pc = i); break; } }else{ continue; } } }else if (stricmp(cmd.cmd_name,"jr") == 0){ //NO NEED TO IMPLEMENT FOR NOW } } if (registers_arr[0] !=0){// make sure that $R0=0 registers_arr[0] = 0; } }