static BOOL AddSection(const TCHAR *SectionName) { SECTION_INFO *TmpSection; if(SectionName == NULL || *SectionName == TEXT('\0')){ return FALSE; } if(SectionAllocCnt < SectionCnt + 1){ //再確保 SectionAllocCnt += ALLOC_CNT; TmpSection = (SECTION_INFO *)LocalAlloc(LPTR, sizeof(SECTION_INFO) * SectionAllocCnt); if(TmpSection == NULL){ return FALSE; } if(SectionInfo != NULL){ CopyMemory(TmpSection, SectionInfo, sizeof(SECTION_INFO) * SectionCnt); LocalFree(SectionInfo); } SectionInfo = TmpSection; } //セクション追加 lstrcpyn((SectionInfo + SectionCnt)->SectionName, SectionName, BUF_SIZE); Trim((SectionInfo + SectionCnt)->SectionName); (SectionInfo + SectionCnt)->hash = str2hash((SectionInfo + SectionCnt)->SectionName); SectionCnt++; return TRUE; }
struct Elem *find_elem(char *key, struct Elem **table, tableindex_t tablesize, tableindex_t seed) { tableindex_t hash; struct Elem *ptr, *pPrev; pPrev = 0; hash = str2hash(key, tablesize, seed); for (ptr = table[hash]; ptr; ptr = ptr->pNext) { if (!strcmp(key, ptr->pKey)) { break; } pPrev = ptr; } /* After success finding, Move-To-Front */ if (ptr && pPrev) { pPrev->pNext = ptr->pNext; ptr->pNext = table[hash]; table[hash] = ptr; } return ptr; }
int main(int argc, char * argv[]) { int i, time, hash; char *temp, *prompt; time = 1; /* equivalent to SplayTime = ( N ) */ for (i = 1; i < argc; i++) { hash = str2hash(argv[i]); printf("host=%s hash=%d time=%d\n", argv[i], hash, (int)(time*60*hash/CF_HASHTABLESIZE)); } }
/* read the data of a chunk from file (identified by hash) */ char* read_chunk_data_from_file(bt_config_t* config, char* hash){ char buf[BT_FILENAME_LEN], buf2[BT_FILENAME_LEN]; FILE* master_chunk_file = fopen(config->chunk_file, "r"); /* read master data file name */ if (fgets(buf, sizeof(buf), master_chunk_file) == NULL){ perror("read master chunk file error"); exit(-1); } buf[strlen(buf) - 1] = '\0'; /* read chunk id */ fgets(buf2, sizeof(buf2), master_chunk_file); int chunk_id; fscanf(master_chunk_file, "%d %s\n", &chunk_id, buf2); str2hash(buf2); while (memcmp(hash, buf2, CHUNK_HASH_SIZE) != 0){ fscanf(master_chunk_file, "%d %s\n", &chunk_id, buf2); str2hash(buf2); } fclose(master_chunk_file); /* read data */ FILE* master_data_file = fopen(buf+6, "r"); fseek(master_data_file, chunk_id * 512 * 1024, SEEK_SET); char* data = malloc(512 * 1024); int ret = fread(data, 1, 512*1024, master_data_file); if (ret < 512*1024){ perror("read master data file error"); exit(-1); } fclose(master_data_file); return data; }
/* * data_create_data - データの作成 */ DATA_INFO *data_create_data(const UINT format, TCHAR *format_name, const HANDLE data, const DWORD size, const BOOL init, TCHAR *err_str) { DATA_INFO *new_item; // アイテムの確保 if ((new_item = (DATA_INFO *)mem_calloc(sizeof(DATA_INFO))) == NULL) { message_get_error(GetLastError(), err_str); return NULL; } new_item->struct_size = sizeof(DATA_INFO); new_item->type = TYPE_DATA; new_item->format = (format != 0) ? format : clipboard_get_format(0, format_name); new_item->format_name = alloc_copy(format_name); new_item->format_name_hash = str2hash(new_item->format_name); new_item->data = data; new_item->size = size; format_initialize_item(new_item, (data == NULL) ? init : FALSE); return new_item; }
static int FindSection(const TCHAR *SectionName) { int hash; int i; if(SectionInfo == NULL || SectionName == NULL || *SectionName == TEXT('\0')){ return -1; } hash = str2hash(SectionName); for(i = 0; i < SectionCnt; i++){ if((SectionInfo + i)->hash != hash){ continue; } if(lstrcmpi((SectionInfo + i)->SectionName, SectionName) == 0){ return i; } } return -1; }
static int FindKey(const SECTION_INFO *si, const TCHAR *KeyName) { int hash; int i; if(si->KeyInfo == NULL || KeyName == NULL || *KeyName == TEXT('\0')){ return -1; } hash = str2hash(KeyName); for(i = 0; i < si->KeyCnt; i++){ if((si->KeyInfo + i)->CommentFlag == TRUE || (si->KeyInfo + i)->hash != hash){ continue; } if(lstrcmpi((si->KeyInfo + i)->KeyName, KeyName) == 0){ return i; } } return -1; }
static BOOL AddKey(SECTION_INFO *si, const TCHAR *KeyName, const TCHAR *String, const BOOL cFlag) { KEY_INFO *TmpKey; int index = -1; if(KeyName == NULL || *KeyName == TEXT('\0') || String == NULL){ return FALSE; } if(si->KeyAllocCnt < si->KeyCnt + 1){ //再確保 si->KeyAllocCnt += ALLOC_CNT; TmpKey = (KEY_INFO *)LocalAlloc(LPTR, sizeof(KEY_INFO) * si->KeyAllocCnt); if(TmpKey == NULL){ return FALSE; } if(si->KeyInfo != NULL){ CopyMemory(TmpKey, si->KeyInfo, sizeof(KEY_INFO) * si->KeyCnt); LocalFree(si->KeyInfo); } si->KeyInfo = TmpKey; } //キー追加 (si->KeyInfo + si->KeyCnt)->String = (TCHAR *)LocalAlloc(LMEM_FIXED, sizeof(TCHAR) * (lstrlen(String) + 1)); if((si->KeyInfo + si->KeyCnt)->String == NULL){ return FALSE; } lstrcpyn((si->KeyInfo + si->KeyCnt)->KeyName, KeyName, BUF_SIZE); Trim((si->KeyInfo + si->KeyCnt)->KeyName); if(cFlag == FALSE){ (si->KeyInfo + si->KeyCnt)->hash = str2hash((si->KeyInfo + si->KeyCnt)->KeyName); } lstrcpy((si->KeyInfo + si->KeyCnt)->String, String); (si->KeyInfo + si->KeyCnt)->CommentFlag = cFlag; si->KeyCnt++; return TRUE; }
static int masm_main_loop(char * obj_file,char * src_file) { FILE * obj_fp, *src_fp; char buf[BUFSIZ] = {0}; int length; char * p, * q; src_fp = obj_fp = NULL; uint32_t counter = 0; uint32_t lines = 0; hash_table * label_hash = hash_create(512); char op_name[128]; char label[128]; char rd[6]; char rs[6]; char rt[6]; char imm[20]; int32_t rd_num,rs_num,rt_num,imm_num; if((src_fp = fopen(src_file,"r")) == NULL) { printf("Can not open %s:%s\n",src_file,strerror(errno)); } if((obj_fp = fopen(obj_file,"w+")) == NULL) { printf("Can not open %s:%s\n",obj_file,strerror(errno)); } int total_lines = get_file_lines(src_fp); uint32_t * instruction = calloc(1,total_lines * sizeof(uint32_t)); var_t * var = calloc(1,total_lines * sizeof(var_t)); int var_count = 0; fseek(src_fp,0L,SEEK_SET); while(1) { fgets(buf,BUFSIZ,src_fp); if(feof(src_fp)) { break; } lines++; length = strlen(buf); p = buf; //skip whitespace while(length > 0 &&isspace(p[0])) { length--; p++; } //printf("length=%d\t%s",length,buf+i); if(p[0] == ';' || p[0] == '\0') { continue; } q = get_first_token(p); strncpy(op_name, p , q-p); op_name[q-p] = '\0'; if(line_has_label(p)) { /* it is label */ label_t l; l.name = op_name; l.real_line = lines; l.line = counter; hash_add_item(&label_hash,str2hash(op_name),&l); p = skip_label_wthie(q); /* 获得字符串 */ q = get_opcode_token(p); strncpy(op_name, p , q-p); op_name[q-p] = '\0'; //printf("%s",op_name); } /* p now a opcode start q-p is opecode */ int op_index = verify_opcode(op_name,lines); q = skip_wthie(q); p = q; /* now at rd */ switch(op_index) { #if 1 case ADD: case SUB: case MUL: case DIV: case MOD: case AND: case OR: case NOT: case XOR: case LWORD: case SWORD: /* 获得字符串 */ q = get_reg_token(p); strncpy(rd, p , q-p); rd[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(rs, p , q-p); rs[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(rt, p , q-p); rt[q-p] = '\0'; rd_num = get_reg_index(rd,lines); rs_num = get_reg_index(rs,lines); rt_num = get_reg_index(rt,lines); instruction[counter] = (op_index << 26) | (rd_num << 21) | (rs_num << 16)| (rt_num << 11); break; ///C语言中的左移就是逻辑左移,而右移, ///对于无符号数来说就是逻辑右移, ///对有符号来说就是算术右移 ///想要实现符号左移,比较麻烦一点,首先保存最高位,然后左移之后补上最高位。 case SLL: case SLR: case SAL: case SAR: case ADDI: case ANDI: ///这里的立即数是0扩展的。 case ORI: case XORI: case LUI: ///哦,载入高16位数啊,靠,那么低位怎么载入呢?用ori q = get_reg_token(p); strncpy(rd, p , q-p); rd[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(rs, p , q-p); rs[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(imm, p , q-p); imm[q-p] = '\0'; rd_num = get_reg_index(rd,lines); rs_num = get_reg_index(rs,lines); imm_num = atoi(imm); if(imm_num > 32767 || imm_num < -32768) { printf("________\n"); printf("[ERROR 6] line: %d imm num is too lager or too smaller\n",lines); } instruction[counter] = (op_index << 26) | (rd_num << 21) | (rs_num << 16)| (imm_num & 0x0000ffff); break; case LESS: case GREAT: case LESSE: case GREATE: case LESSU: case GREATU: case LESSEU: case GREATEU: case EQUAL: case UEQUAL: q = get_reg_token(p); strncpy(rd, p , q-p); rd[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(rs, p , q-p); rs[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(label, p , q-p); label[q-p] = '\0'; rd_num = get_reg_index(rd,lines); rs_num = get_reg_index(rs,lines); var[var_count].name = malloc(strlen(label) + 1); strcpy(var[var_count].name, label); var[var_count].line = counter; var_count++; instruction[counter] = (op_index << 26) | (rd_num << 21) | (rs_num << 16) | 0x0; break; case JMP: q = get_reg_token(p); strncpy(label, p , q-p); label[q-p] = '\0'; var[var_count].name = malloc(strlen(label) + 1); strcpy(var[var_count].name, label); var[var_count].line = counter; var_count++; instruction[counter] = (op_index << 26); break; /* 存储指令 */ case MOV: q = get_reg_token(p); strncpy(rd, p , q-p); rd[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(rs, p , q-p); rs[q-p] = '\0'; rd_num = get_reg_index(rd,lines); rs_num = get_reg_index(rs,lines); instruction[counter] = (op_index << 26) | (rd_num << 21) | (rs_num << 16) | 0x0; break; default: break; #endif } counter++; } /* 第二趟汇编 */ struct blist * head; for(int i = 0; i < var_count; i++) { if((head = hash_lookup_item(label_hash,str2hash(var[i].name),&var[i])) != NULL) { label_t * node = head->item; int imm_2 = node->line - var[i].line; if((instruction[var[i].line] >> 26) == JMP) { if(imm_2 > 33554431 || imm_2 < -33554432) { printf("[ERROR 7] line: %d imm num is too lager or too smaller\n",lines); } instruction[var[i].line] |= imm_2 & 0x03ffffff; } else { if(imm_2 > 32767 || imm_2 < -32768) { printf("[ERROR 6] line: %d imm num is too lager or too smaller\n",lines); } instruction[var[i].line] |= imm_2 & 0x0000ffff; } }
struct Elem *add_elem(char *pKey, struct Elem **ppTable, tableindex_t tablesize, tableindex_t seed, struct Parameters *pParam) { tableindex_t hash; struct Elem *ptr, *pPrev; hash = str2hash(pKey, tablesize, seed); if (ppTable[hash]) { pPrev = 0; ptr = ppTable[hash]; while (ptr) { if (!strcmp(pKey, ptr->pKey)) { break; } pPrev = ptr; ptr = ptr->pNext; } if (ptr) { ptr->count++; if (pPrev) { pPrev->pNext = ptr->pNext; ptr->pNext = ppTable[hash]; ppTable[hash] = ptr; } } else { ptr = (struct Elem *) malloc(sizeof(struct Elem)); if (!ptr) { log_msg(MALLOC_ERR_6007, LOG_ERR, pParam); exit(1); } ptr->pKey = (char *) malloc(strlen(pKey) + 1); if (!ptr->pKey) { log_msg(MALLOC_ERR_6007, LOG_ERR, pParam); exit(1); } strcpy(ptr->pKey, pKey); ptr->count = 1; ptr->pNext = ppTable[hash]; ppTable[hash] = ptr; } } else { ptr = (struct Elem *) malloc(sizeof(struct Elem)); if (!ptr) { log_msg(MALLOC_ERR_6007, LOG_ERR, pParam); exit(1); } ptr->pKey = (char *) malloc(strlen(pKey) + 1); if (!ptr->pKey) { log_msg(MALLOC_ERR_6007, LOG_ERR, pParam); exit(1); } strcpy(ptr->pKey, pKey); ptr->count = 1; ptr->pNext = 0; ppTable[hash] = ptr; } return ptr; }
int GetCacheMesTextureID( char *msg, int font_size, int font_style ) { // キャッシュ済みのテクスチャIDを返す(OpenGLテクスチャIDを返す) // (作成されていないメッセージテクスチャは自動的に作成する) // (作成の必要がない場合は-1を返す) // GLuint id; int texid; int tsx,tsy; unsigned char *pImg; TEXINF *t; int mylen; short mycache; mycache = str2hash( msg, &mylen ); // キャッシュを取得 if ( mylen <= 0 ) return -1; texid = getCache( msg, mycache, font_size, font_style ); if ( texid >= 0 ) { return texid; // キャッシュがあった } // キャッシュが存在しないので作成 pImg = (unsigned char *)j_callFontBitmap( msg, font_size, font_style, &tsx, &tsy ); texid = MakeEmptyTex( tsx, tsy ); if ( texid < 0 ) return -1; t = GetTex( texid ); t->hash = mycache; t->font_size = font_size; t->font_style = font_style; if ( curmestex >= GetSysReq(SYSREQ_MESCACHE_MAX) ) { // エントリ数がオーバーしているものは次のフレームで破棄 t->life = 0; t->buf[0] = 0; } else { // キャッシュの登録 if ( mylen >= ( TEXMES_NAME_BUFFER - 1 ) ) { t->text = (char *)malloc( mylen+1 ); // テキストハッシュネーム用バッファを作成する strcpy( t->text, msg ); } else { strcpy( t->buf, msg ); // 標準バッファにコピーする } } id = (GLuint)t->texid; glBindTexture( GL_TEXTURE_2D, id ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glPixelStorei( GL_UNPACK_ALIGNMENT, 1); glTexSubImage2D( GL_TEXTURE_2D, 0, (GLint)0, (GLint)0, (GLsizei)tsx, (GLsizei)tsy, GL_ALPHA, GL_UNSIGNED_BYTE, pImg ); glBindTexture(GL_TEXTURE_2D, 0); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); free(pImg); return texid; }
int GetCacheMesTextureID( char *msg, int font_size, int font_style ) { // キャッシュ済みのテクスチャIDを返す(OpenGLテクスチャIDを返す) // (作成されていないメッセージテクスチャは自動的に作成する) // (作成の必要がない場合は-1を返す) // #ifdef HSPNDK GLuint id; int texid; int tsx,tsy; unsigned char *pImg; TEXINF *t; int mylen; short mycache; mycache = str2hash( msg, &mylen ); // キャッシュを取得 if ( mylen <= 0 ) return -1; texid = getCache( msg, mycache, font_size, font_style ); if ( texid >= 0 ) { return texid; // キャッシュがあった } // キャッシュが存在しないので作成 pImg = (unsigned char *)j_callFontBitmap( msg, font_size, font_style, &tsx, &tsy ); texid = MakeEmptyTex( tsx, tsy ); if ( texid < 0 ) return -1; t = GetTex( texid ); t->hash = mycache; t->font_size = font_size; t->font_style = font_style; if ( curmestex >= GetSysReq(SYSREQ_MESCACHE_MAX) ) { // エントリ数がオーバーしているものは次のフレームで破棄 t->life = 0; t->buf[0] = 0; } else { // キャッシュの登録 if ( mylen >= ( TEXMES_NAME_BUFFER - 1 ) ) { t->text = (char *)malloc( mylen+1 ); // テキストハッシュネーム用バッファを作成する strcpy( t->text, msg ); } else { strcpy( t->buf, msg ); // 標準バッファにコピーする } } id = (GLuint)t->texid; glBindTexture( GL_TEXTURE_2D, id ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glPixelStorei( GL_UNPACK_ALIGNMENT, 1); glTexSubImage2D( GL_TEXTURE_2D, 0, (GLint)0, (GLint)0, (GLsizei)tsx, (GLsizei)tsy, GL_ALPHA, GL_UNSIGNED_BYTE, pImg ); glBindTexture(GL_TEXTURE_2D, 0); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); free(pImg); return texid; #endif #ifdef HSPEMSCRIPTEN GLuint id; int texid; int tsx,tsy; unsigned char *pImg; TEXINF *t; int mylen; short mycache; mycache = str2hash( msg, &mylen ); // キャッシュを取得 if ( mylen <= 0 ) return -1; texid = getCache( msg, mycache, font_size, font_style ); if ( texid >= 0 ) { return texid; // キャッシュがあった } EM_ASM_({ var d = document.getElementById('hsp3dishFontDiv'); if (!d) { d = document.createElement("div"); d.id = 'hsp3dishFontDiv'; d.style.setProperty("width", "auto"); d.style.setProperty("height", "auto"); d.style.setProperty("position", "absolute"); d.style.setProperty("visibility", "hidden"); } d.style.setProperty("font", $1 + "px 'sans-serif'"); document.body.appendChild(d); var t = document.createTextNode(Pointer_stringify($0)); if (d.hasChildNodes()) d.removeChild(d.firstChild); d.appendChild(t); }, msg, font_size);