fixed chicken messages
[snoogans.git] / populate.c
blobd714cdaa65396c11e6c54e456f5ea6ddc4153260
1 /*
2 * Copyright (C) 2010 gonzoj
4 * Please check the CREDITS file for further information.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <dlfcn.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include "d2pointers.h"
27 #include "kernel32.h"
28 #include "user32.h"
30 #include "debug.h"
32 const char *modules[] =
33 { "D2Client.dll", "D2Common.dll", "D2Gfx.dll", "D2Lang.dll", "D2Win.dll",
34 "D2Net.dll", "D2Game.dll", "D2Launch.dll", "Fog.dll", "BNClient.dll",
35 "Storm.dll", "D2Cmp.dll", "D2Multi.dll" };
37 static int
38 read_line(FILE *fd, char *line)
40 int n_bytes = 0;
41 while (!feof(fd))
43 char chr = fgetc(fd);
44 if (chr == EOF)
46 break;
48 if (chr == '\n')
50 n_bytes++;
51 line[n_bytes - 1] = '\0';
52 break;
54 n_bytes++;
55 line[n_bytes - 1] = chr;
57 return n_bytes == 0 ? 0 : 1;
60 static int
61 get_module_path(const char *module, char *path)
63 pid_t pid = getpid();
64 char maps[512];
65 sprintf(maps, "/proc/%i/maps", pid);
66 FILE *fd = fopen(maps, "r");
67 char line[512];
68 while (read_line(fd, line))
70 if (strstr(line, module) != NULL)
72 strcpy(path, strchr(line, '/'));
73 fclose(fd);
74 return 1;
76 memset(line, 0, 512);
78 fclose(fd);
79 return 0;
82 int
83 populate_kernel32_funcs()
85 char module_kernel32[512];
86 if (!get_module_path("kernel32.dll", module_kernel32))
88 printf("err: could not find location of kernel32.dll\n");
89 return 0;
91 void *h = dlopen(module_kernel32, RTLD_LAZY);
92 if (h == NULL)
94 printf("err: could not open %s\n", module_kernel32);
95 return 0;
97 void **func;
98 char **str;
99 for (func = (void **) &_KERNEL32_FUNC_START, str
100 = (char **) &_KERNEL32_STR_START; func <= (void **) &_KERNEL32_FUNC_END; func++, str++)
102 *func = dlsym(h, *str);
103 if (*func == NULL)
105 printf("err: could not resolve symbol %s\n", *str);
106 return 0;
108 DEBUG_DO(printf("%s: 0x%08X\n", *str, (vaddr) *func);)
110 return 1;
114 populate_user32_funcs()
116 char module_user32[512];
117 if (!get_module_path("user32.dll", module_user32))
119 printf("err: could not find location of user32.dll\n");
120 return 0;
122 void *h = dlopen(module_user32, RTLD_LAZY);
123 if (h == NULL)
125 printf("err: could not open %s\n", module_user32);
126 return 0;
128 void **func;
129 char **str;
130 for (func = (void **) &_USER32_FUNC_START, str = (char **) &_USER32_STR_START; func
131 <= (void **) &_USER32_FUNC_END; func++, str++)
133 *func = dlsym(h, *str);
134 if (*func == NULL)
136 printf("err: could not resolve symbol %s\n", *str);
137 return 0;
139 DEBUG_DO(printf("%s: 0x%08X\n", *str, (vaddr) *func);)
141 return 1;
145 populate_d2funcs()
147 if (GetModuleHandleA == NULL || GetProcAddress == NULL || LoadLibraryA
148 == NULL)
150 printf("err: necessary kernel32 functions missing\n");
151 return 0;
153 void **func;
154 for (func = (void **) &_D2FUNCS_START; func <= (void **) &_D2FUNCS_END; func++)
156 int index = (vaddr) *func & 0xff;
157 int offset = (int) *func >> 8;
158 void *module;
159 if (((int) *func >> 8) > 0)
161 module = LoadLibraryA(modules[index]);
162 if (module == NULL)
164 printf("err: could not get a handle for %s\n", modules[index]);
165 return 0;
167 *func = module + offset;
168 DEBUG_DO(printf("populated function pointer (0x%08X) to %s (0x%08X) + 0x%08X\n", (vaddr) *func, modules[index], (vaddr) module, offset);)
170 else
172 module = GetModuleHandleA(modules[index]);
173 if (module == NULL)
175 printf("err: could not get a handle for %s\n", modules[index]);
176 return 0;
178 *func = GetProcAddress(module, -offset);
179 if (*func == NULL)
181 printf(
182 "err: could not resolve exported function with ordinal %i\n",
183 -offset);
184 return 0;
186 DEBUG_DO(printf("populated function pointer (0x%08X) to %s (0x%08X) -> %i\n", (vaddr) *func, modules[index], (vaddr) module, -offset);)
190 return 1;
194 populate_d2vars()
196 if (GetModuleHandleA == NULL || GetProcAddress == NULL || LoadLibraryA
197 == NULL)
199 printf("err: necessary kernel32 functions missing\n");
200 return 0;
202 void **var;
203 for (var = (void **) &_D2VARS_START; var <= (void **) &_D2VARS_END; var++)
205 int index = (vaddr) *var & 0xff;
206 int offset = (vaddr) *var >> 8;
207 void *module = LoadLibraryA(modules[index]);
208 if (module == NULL)
210 printf("err: could not get a handle for %s\n", modules[index]);
211 return 0;
213 *var = module + offset;
214 DEBUG_DO(printf("populated variable pointer (0x%08X) to %s (0x%08X) + 0x%08X\n", (vaddr) *var, modules[index], (vaddr) module, offset);)
216 return 1;