📄 string.c
字号:
/* * a64's string function */int str_cmp(char *d, char *s) { if (!d || !s) return 0; while (*d && *s && *d == *s) { d++; s++; } return ((*d == *s) && (*(d-1) == *(s-1)));}int str_len(char *s) { int ret = 0; if (!s) return 0; while (*s++) ret++; return ret;}/*************************************************************************************************************/int trim_space(char *d, char *s){ if (!d || !s || !s[0]) return 0; int i = 0, j = 0; char *p = s; char *q = s; while (*s) { if (*s != ' ') *d++ = *s++; else if (s == p) { /* head */ while (*s && (*s == ' ')) s++; } else { q = s - 1; while (*s && (*s == ' ')) s++; if ((is_c(*q) || is_n(*q)) && (is_c(*s) || is_n(*s))) *d++ = ' '; } } *d = 0; return 1;}int get_c(char *s, char c) { int ret = 0; while (*s) { ret += (*s == c); s++; } return ret;}int is_c(char c) { return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));}int is_n(char c) { return (c >= '0' && c <= '9');}int is_hc(char c) { return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));}int is_dec(char *s) { if (!s) return 0; while (*s) { if (!(*s >= '0' && *s <= '9')) return 0; s++; } return 1;}int is_hex(char *s) { if (!s) return 0; if ((*s == '0') && (*(s+1) == 'x' || *(s+1) == 'X')) { s += 2; while (*s) { if (!(is_n(*s) || is_hc(*s))) return 0; s++; } } else if ((*s == '0') && is_hc(*(s+1))) { s += 2; while (*s) { if (!(is_n(*s) || is_hc(*s))) break; s++; } if (*s == 0) return 0; if ((*s == 'h' || *s == 'H') && (*(s+1) == 0)) return 1; } else return 0; return 1;}int is_binary(char *s) { if (!s) return 0; while (*s && (*s == '0' || *s == '1')) s++; if (*s == 0) return 0; else if ((*s == 'b' || *s == 'B') && (*(s+1) == 0)) return 1; else return 0; return 1;}int is_numeric(char *s) { int ret = 0; if (!s) return 0; if (is_c(*s)) return 0; if (!(ret = is_hex(s))) if (!(ret = is_dec(s))) ret = is_binary(s); return ret;}int get_sizeof(long long n) { int ret = 0; union { long long ll; long l[2]; } u; u.ll = n; if (n) { if (u.l[1]) ret = 8; else if (n <= 0xff) ret = 1; else if (n <= 0xffff) ret = 2; else if (n <= 0xffffffff) ret = 4; } return ret;}/* ----------------------------------------------- */unsigned char get_regid(char *s) { reg_t *p = regs; if (!s || !s[0]) return 0; while (p->reg && !str_cmp(s, p->reg)) p++; return p->reg_id;}unsigned char get_osegid(char *s) { oseg_t *p = osegs; if (!s || !s[0]) return 0; while (p->oseg && !str_cmp(s, p->oseg)) p++; return p->oseg_id;}unsigned char get_scaleid(char *s) { scale_t *p = scales; if (!s || !s[0]) return 0; while (p->scale && !str_cmp(s, p->scale)) p++; return p->scale_id;}unsigned char get_castid(char *s) { cast_t *p = casts; if (!s || !s[0]) return 0; while (p->cast && !str_cmp(s, p->cast)) p++; return p->cast_id;}unsigned char get_prefixid(char *s){ i_prefix_t *p = i_prefixs; if (!s || !s[0]) return 0; while (p->prefix && !str_cmp(s, p->prefix)) p++; return p->prefix_id;}unsigned char get_modeid(char *s) { mode_t *p = modes; if (!s || !s[0]) return 0; while (p->mode && !str_cmp(s, p->mode)) p++; return p->mode_id;}unsigned char get_bitsid(char *s){ bits_t *p = bits; if (!s || !s[0]) return 0; while (p->bits && !str_cmp(s, p->bits)) p++; return p->bits_id;}/*************************************************************/int check_cast(unsigned char cast){ if (cast && (current_mode == LONG_64 || current_bits == 64)) { } else if (!cast || cast == 64) return 0; return 1;}int check_oseg(unsigned char oseg){ if (oseg && (current_mode != LONG_64 && current_bits != 64)) { /* 32-bit */ } else if (!oseg || (oseg == 0x2e) || (oseg == 0x3e) || (oseg == 0x26) || (oseg == 0x36)) /* in 64-bit mode: the CS,DS,ES and SS is invalid */ return 0; return 1;}/************************************************** the funtion check base or index register***************************************************/int check_mreg(unsigned char regid){ if (!regid || ((regid & 0xF0) == 0x10)) /* 1-byte register */ return 0; else if ((regid & 0xf0) >= 0xa0) /* not GPR */ return 0; else if ((regid & 0xF0) == 0x20) { /* 2-byte register */ if (current_mode == LONG_64 || current_bits == 64) return 0; else if (regid == BX || regid == BP || regid == SI || regid == DI) { /* at 32-bit or 16-bit... OK */ } else return 0; } return 1;}/********************************************************* check the string is var ????**********************************************************/int check_var(char *s){ return 1;}/************************************************************/int is_keyword(char *s) { char **p = keyword; if (!s) return 0; while (*p && !str_cmp(*p, s)) p++; if (*p == 0) return 0; return 1;}long long strd_to_n(char *s) { long long retval = 0; while (*s) retval = retval * 10 + (*s++ - '0'); return retval;}long long strh_to_n(char *s) { long long retval = 0; int i = 0; s++; if (*s == 'x' || *s == 'X') { s++; while (*s) { if (is_n(*s)) i = *s - '0'; else if (is_hc(*s)) i = *s - 'a' + 10; else return 0; retval = retval * 16 + i; s++; } } else { while (*s) { if (*s == 'h' || *s == 'H') break; if (is_n(*s)) i = *s - '0'; else if (is_hc(*s)) i = *s - 'a' + 10; else return 0; retval = retval * 16 + i; s++; } } return retval;}long long strb_to_n(char *s) { long long retval = 0; while (*s) { if (*s == 'b' || *s == 'B') break; retval = retval * 2 + (*s - '0'); s++; } return retval;}long long str_to_n(char *s) { long long retval = 0; if (!s || !s[0]) return 0; if (is_hex(s)) retval = strh_to_n(s); else if (is_dec(s)) retval = strd_to_n(s); else if (is_binary(s)) retval = strb_to_n(s); else return 0; return retval;}/* -------------------------------------------------------- */#if 0o_link_t *get_o_link() { op_link_t *tp_op_link = 0; if (tp_op_link = malloc(sizeof(op_link_t))) { // success tp_op_link->ops = 0; tp_op_link->n = 0; tp_op_link->next = 0; } return tp_op_link;}int release_op_link(op_link_t *op_link) { op_link_t *tp_op_link = op_link; op_link_t *next = 0; if (!op_link) return 0; for (; tp_op_link; next = tp_op_link->next) { if (tp_op_link->ops) free(tp_op_link->ops); free(tp_op_link); tp_op_link = next; } return 1;}int get_ops(op_link_t *op_link, char *s) { int i = 0; int j = 0; char *p = s + str_len(s) - 1; char *q = 0; op_link->n = 1; while (p >= s) { q = p; while (*p && (*p != ',') && (*p != ' ')) p--; if (*p == ',') { i++; if (i > op_link->n) { op_link->next = malloc(sizeof(op_link_t)); op_link->n++; op_link = op_link->next; } p++; j = 0; while (p+j <= q) j++; op_link->ops = (char *)malloc(j + 1); j = 0; while (p+j <= q) { op_link->ops[j] = *(p+j); j++; } op_link->ops[j] = 0; op_link->next = 0; p--; } else if (*p == ' ') { i++; if (i > op_link->n) { op_link->next = malloc(sizeof(op_link_t)); op_link->n++; op_link = op_link->next; } p++; j = 0; while (p+j <= q) j++; op_link->ops = (char *)malloc(j + 1); j = 0; while (p+j <= q) { op_link->ops[j] = *(p+j); j++; } op_link->ops[j] = 0; op_link->next = 0; p--; } p--; } return 1;}#endif/********************************************************************************************************************//*e_key_t *get_ekey_from_elem(void *elem){ if (!elem) return 0; return (elem - ((e_key_t *)0}*//*********************************************************** * i_key_t *get_i_key(char *s) * return value: i_key if found * 0 if no found************************************************************/i_key_t *get_ins(char *s){ i_set_t *p = i_set; if (!s || !s[0]) return 0; while (p->mnemonic && !str_cmp(s, p->mnemonic)) p++; return p->i_key;}i_key_t *get_i_key(i_key_t *i_key, ops_attr_t *ops_attr){ if (!i_key || !ops_attr) return 0; while (i_key->opcode || i_key->i_attr) { if ((ops_attr->to_attr == i_key->to_attr) && (ops_attr->do_attr == i_key->do_attr) && (ops_attr->so_attr == i_key->so_attr)) return i_key; else if ((SIZE(i_key->so_attr) == 0xe) && (SIZE(i_key->do_attr) == 0xe) && (SIZE(ops_attr->so_attr) != SIZE(ops_attr->do_attr))) { i_key++; continue; } else if (((!ops_attr->to_attr && !i_key->to_attr) || (ops_attr->to_attr && i_key->to_attr)) && ((!ops_attr->do_attr && !i_key->do_attr) || (ops_attr->do_attr && i_key->do_attr)) && ((!ops_attr->so_attr && !i_key->so_attr) || (ops_attr->so_attr && i_key->so_attr))) if (((ops_attr->to_attr & i_key->to_attr) == ops_attr->to_attr) && ((ops_attr->do_attr & i_key->do_attr) == ops_attr->do_attr) && ((ops_attr->so_attr & i_key->so_attr) == ops_attr->so_attr)) return i_key; i_key++; } return 0;}/********************************************************************************************************************************/label_t *init_label_table(){ label_t *head_label_table = label_table; if (!head_label_table) { label_table = (label_t *)malloc(sizeof(label_t)); if (!label_table) { fprintf(stderr, "no engouth memory\n"); exit(1); } memset(label_table, 0, sizeof(label_t)); head_label_table = label_table; } else { while (head_label_table->next) head_label_table = head_label_table->next; head_label_table->next = (label_t *)malloc(sizeof(label_t)); if (!head_label_table->next) { fprintf(stderr, "no engouth memory\n"); exit(1); } memset(head_label_table->next, 0, sizeof(label_t)); head_label_table = head_label_table->next; } return head_label_table;}variable_t *init_var_table(){ variable_t *head_var_table = var_table; if (!head_var_table) { var_table = (variable_t *)malloc(sizeof(variable_t)); if (!var_table) { fprintf(stderr, "no enought memory\n"); exit(1); } memset(var_table, 0, sizeof(variable_t)); head_var_table = var_table; } else { while (head_var_table->next) head_var_table = head_var_table->next; head_var_table->next = (variable_t *)malloc(sizeof(variable_t)); if (!head_var_table->next) { fprintf(stderr, "no enought memory\n"); exit(1); } memset(head_var_table->next, 0, sizeof(variable_t)); head_var_table = head_var_table->next; } return head_var_table;}/****************************************************** function: int is_free_sym(char *s) return: true if the symbol is free and can by used******************************************************/int is_free_sym(char *s){ if (!s || !s[0]) return 0; char str[80]; label_t *head_label_table = label_table; variable_t *head_var_table = var_table; trim_space(str, s); if (is_c(*str) || (*str == '_')) { /* OK */ while (head_label_table && !str_cmp(head_label_table->token, str)) head_label_table = head_label_table->next; while (head_var_table && !str_cmp(head_var_table->token, str)) head_var_table = head_var_table->next; return !head_label_table && !head_var_table; } return 0;}int is_label(char *s){ label_t *head_label_table = label_table; char str[80]; if (!s || !s[0]) return 0; if (is_c(*str) || (*str == '_')) { while (head_label_table && !str_cmp(head_label_table->token, str)) head_label_table = head_label_table->next; return (int)head_label_table; } return 0;}int is_variable(char *s){ variable_t *head_var_table = var_table; char str[80]; if (!s || !s[0]) return 0; trim_space(str, s); if (is_c(*str) || (*str == '_')) { while (head_var_table && !str_cmp(head_var_table->token, str)) head_var_table = head_var_table->next; return (int)head_var_table; } return 0;}/*int is_valid_sym(char *s){ if (!s || !s[0]) return 0; symbol_t *head_sym_table = sym_table; char str[80]; trim_space(str, s); if (is_c(*str) || (*str == '_')) { while (head_sym_table && !str_cmp(head_sym_table->token, str)) head_sym_table = head_sym_table->next; return (int)head_sys_table; } return 0;}*//********************************************************************//*main() { printf("%x: %d\n", 0xe,get_sum_of_1bit(0xe));}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -