1 /*
2  *	HT Editor
3  *	analy_names.cc
4  *
5  *	Copyright (C) 1999-2002 Sebastian Biallas (sb@biallas.net)
6  *
7  *	This program is free software; you can redistribute it and/or modify
8  *	it under the terms of the GNU General Public License version 2 as
9  *	published by the Free Software Foundation.
10  *
11  *	This program is distributed in the hope that it will be useful,
12  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *	GNU General Public License for more details.
15  *
16  *	You should have received a copy of the GNU General Public License
17  *	along with this program; if not, write to the Free Software
18  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include "analy_names.h"
22 #include "htdebug.h"
23 #include "strtools.h"
24 #include "language.h"
25 #include "snprintf.h"
26 #include "stdio.h"
27 #include "string.h"
28 #include "tools.h"
29 
import_func_name(const char * dllname,const char * funcname,int ordinal)30 char *import_func_name(const char *dllname, const char *funcname, int ordinal)
31 {
32 	char res[1024];
33 	if (funcname) {
34 		ht_snprintf(res, sizeof res, "%s:%s", dllname, funcname);
35 
36 	} else {
37 		ht_snprintf(res, sizeof res, "%s:%04x", dllname, ordinal);
38 	}
39 	return ht_strdup(res);
40 }
41 
export_func_name(const char * funcname,int ordinal)42 char *export_func_name(const char *funcname, int ordinal)
43 {
44 	char res[1024];
45 	if (funcname) {
46 		ht_snprintf(res, sizeof res, "exp_%s_%x", funcname, ordinal);
47 	} else {
48 		ht_snprintf(res, sizeof res, "exp_%x", ordinal);
49 	}
50 	return ht_strdup(res);
51 }
52 
53 const char *label_types[] = {"unknown", "function", "location ", "data"};
54 const char *label_types_short[] = {"?   ", "func", "loc ", "data"};
55 
label_type(int lt)56 const char *label_type(int lt)
57 {
58 	assert(lt < 4);
59 	return label_types[lt];
60 }
61 
label_type_short(int lt)62 const char *label_type_short(int lt)
63 {
64 	assert(lt < 4);
65 	return label_types_short[lt];
66 }
67 
68 const char *xref_types[] = {"read", "write", "offset", "jump", "call", "ijump", "icall"};
69 const char *xref_types_short = "rwojcJC";
70 
xref_type(int xt)71 const char *xref_type(int xt)
72 {
73 	assert(xt < 7);
74 	return xref_types[xt];
75 }
76 
xref_type_short(int xt)77 char xref_type_short(int xt)
78 {
79 	assert(xt < 7);
80 	return xref_types_short[xt];
81 }
82 
valid_name(const char * str)83 bool valid_name(const char *str)
84 {
85 	if (!str || !*str) return false;
86 	char mc = mapchar[(uint8)*str];
87 	if (mc == '_' || mc == '?' || mc == 'A' || mc == '@') {
88 		str++;
89 		while (*str) {
90 			mc = mapchar[(uint8)*str];
91 			if (mc == '_' || mc == '?' || mc == 'A' || mc == '0' || mc == ':' || mc == '.' || mc == '@') {
92 				str++;
93 			} else return false;
94 		}
95 		return true;
96 	} else {
97 		return false;
98 	}
99 }
100 
101 template<class S>
make_valid_name_T(S & result,const S & str)102 void make_valid_name_T(S &result, const S &str)
103 {
104 	char mc = mapchar[(uint8)str[0]];
105 	if (!(mc == '_' || mc == '?' || mc == 'A' || mc == '@')) {
106 		result[0] = '_';
107 	}
108 	int i = 1;
109 	while (str[i]) {
110 		mc = mapchar[(uint8)str[i]];
111 		if (mc == '_' || mc == '?' || mc == 'A' || mc == '0' || mc == ':' || mc == '.' || mc == '@') {
112 			result[i] = str[i];
113 		} else {
114 			result[i] = '_';
115 		}
116 		i++;
117 	}
118 	result[i] = 0;
119 }
120 
make_valid_name(String & s)121 void make_valid_name(String &s)
122 {
123 	if (s.length() < 1) {
124 		s = "_";
125 	} else {
126 		make_valid_name_T(s, s);
127 	}
128 }
129 
make_valid_name(char * result,const char * str)130 void make_valid_name(char *result, const char *str)
131 {
132 	if (!str || !*str) {
133 		*result++ = '_';
134 		*result = '\0';
135 		return;
136 	}
137 	char mc = mapchar[(uint8)*str];
138 	if (!(mc == '_' || mc == '?' || mc == 'A' || mc == '@')) {
139 		*result++ = '_';
140 		str++;
141 	}
142 	while (*str) {
143 		mc = mapchar[(uint8)*str];
144 		if (mc == '_' || mc == '?' || mc == 'A' || mc == '0' || mc == ':' || mc == '.' || mc == '@') {
145 			*result++ = *str;
146 		} else {
147 			*result++ = '_';
148 		}
149 		str++;
150 	}
151 	*result = 0;
152 }
153 
154