00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "analy_names.h"
00022 #include "htdebug.h"
00023 #include "htstring.h"
00024 #include "language.h"
00025 #include "snprintf.h"
00026 #include "stdio.h"
00027 #include "string.h"
00028 #include "tools.h"
00029
00030 char *import_func_name(const char *dllname, const char *funcname, int ordinal)
00031 {
00032 char res[1024];
00033 if (funcname) {
00034 ht_snprintf(res, sizeof res, "%s:%s", dllname, funcname);
00035
00036 } else {
00037 ht_snprintf(res, sizeof res, "%s:%04x", dllname, ordinal);
00038 }
00039 return ht_strdup(res);
00040 }
00041
00042 char *export_func_name(const char *funcname, int ordinal)
00043 {
00044 char res[1024];
00045 if (funcname) {
00046 ht_snprintf(res, sizeof res, "exp_%s_%x", funcname, ordinal);
00047 } else {
00048 ht_snprintf(res, sizeof res, "exp_%x", ordinal);
00049 }
00050 return ht_strdup(res);
00051 }
00052
00053 char *label_types[] = {"unknown", "function", "location ", "data"};
00054 char *label_types_short[] = {"? ", "func", "loc ", "data"};
00055
00056 char *label_type(int lt)
00057 {
00058 assert(lt < 4);
00059 return label_types[lt];
00060 }
00061
00062 char *label_type_short(int lt)
00063 {
00064 assert(lt < 4);
00065 return label_types_short[lt];
00066 }
00067
00068 char *xref_types[] = {"read", "write", "offset", "jump", "call", "ijump", "icall"};
00069 char xref_types_short[] = "rwojcJC";
00070
00071 char *xref_type(int xt)
00072 {
00073 assert(xt < 7);
00074 return xref_types[xt];
00075 }
00076
00077 char xref_type_short(int xt)
00078 {
00079 assert(xt < 7);
00080 return xref_types_short[xt];
00081 }
00082
00083 char *label_prefixes[] = {
00084 "unknown",
00085 "loc",
00086 "sub",
00087 "stub",
00088 "wrapper",
00089 "offset",
00090 "data",
00091 "?data",
00092 "str"
00093 };
00094
00095 char *label_prefix(const char *p)
00096 {
00097 if (p <= LPRFX_MAX) {
00098 return label_prefixes[(int)p];
00099 } else {
00100 return (char*)p;
00101 }
00102 }
00103
00104 bool valid_name(const char *str)
00105 {
00106 if ((!str) || (!*str)) return false;
00107 char mc = mapchar[(unsigned char)*str];
00108 if ((mc == '_') || (mc == '?') || (mc == 'A') || (mc == '@')) {
00109 str++;
00110 while (*str) {
00111 mc = mapchar[(unsigned char)*str];
00112 if ((mc == '_') || (mc == '?') || (mc == 'A') || (mc == '0') || (mc == ':') || (mc == '.') || (mc == '@')) {
00113 str++;
00114 } else return false;
00115 }
00116 return true;
00117 } else {
00118 return false;
00119 }
00120 }
00121
00122 void make_valid_name(char *result, const char *str)
00123 {
00124 if (!str || !*str) {
00125 *result++ = '_';
00126 *result = '\0';
00127 return;
00128 }
00129 char mc = mapchar[(unsigned char)*str];
00130 if (!((mc == '_') || (mc == '?') || (mc == 'A') || (mc == '@'))) {
00131 *result++ = '_';
00132 str++;
00133 }
00134 while (*str) {
00135 mc = mapchar[(unsigned char)*str];
00136 if ((mc == '_') || (mc == '?') || (mc == 'A') || (mc == '0') || (mc == ':') || (mc == '.') || (mc == '@')) {
00137 *result++ = *str;
00138 } else {
00139 *result++ = '_';
00140 }
00141 str++;
00142 }
00143 *result = 0;
00144 }
00145
00146
00147
00148
00149 char the_label_prefix_string[2]="l";
00150
00151 char *addr_label()
00152 {
00153 return the_label_prefix_string;
00154 }
00155
00156 char *real_name(char *s)
00157 {
00158 if (!s) return NULL;
00159 switch (s[0]) {
00160 case M_PREFIX_LABEL: {
00161 return "label";
00162 break;
00163 }
00164 case M_PREFIX_DUP:
00165 case M_PREFIX_REF: {
00166 return &s[1];
00167 break;
00168 }
00169 default: {}
00170 }
00171 return s;
00172 }
00173
00174 char *quote_string(char *s)
00175 {
00176 char *s2 = (char *) smalloc(strlen(s)+2);
00177 s2[0] = M_PREFIX_DUP;
00178 strcpy(&s2[1], s);
00179 return s2;
00180 }
00181
00182 ht_sorted_string_list *reference_strings;
00183
00184 char *reference_string(char *s)
00185 {
00186 char *r = reference_strings->get_string(s);
00187 if (!r) {
00188 reference_strings->insert_string(s);
00189 r = reference_strings->get_string(s);
00190 }
00191 return r;
00192 }
00193
00194 char *comment_lookup(int special)
00195 {
00196 return "testa";
00197 }
00198
00199 void init_analy_names()
00200 {
00201 reference_strings = new ht_sorted_string_list();
00202 reference_strings->init(compare_keys_string);
00203 }
00204
00205 void done_analy_names()
00206 {
00207 reference_strings->done();
00208 delete reference_strings;
00209 }
00210