Fixes for datatype size on amd64.
[crack-attack.git] / src / prefix.c
blob976c98055112a6e9504a0fb6ecb9934bbf6c91f1
1 /*
2 * BinReloc - a library for creating relocatable executables
3 * Written by: Mike Hearn <mike@theoretic.com>
4 * Hongli Lai <h.lai@chello.nl>
5 * http://autopackage.org/
6 *
7 * This source code is public domain. You can relicense this code
8 * under whatever license you want.
10 * NOTE: if you're using C++ and are getting "undefined reference
11 * to br_*", try renaming prefix.c to prefix.cpp
14 /* WARNING, BEFORE YOU MODIFY PREFIX.C:
16 * If you make changes to any of the functions in prefix.c, you MUST
17 * change the BR_NAMESPACE macro (in prefix.h).
18 * This way you can avoid symbol table conflicts with other libraries
19 * that also happen to use BinReloc.
21 * Example:
22 * #define BR_NAMESPACE(funcName) foobar_ ## funcName
23 * --> expands br_locate to foobar_br_locate
26 #ifndef _PREFIX_C_
27 #define _PREFIX_C_
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #ifndef BR_PTHREADS
34 /* Change 1 to 0 if you don't want pthread support */
35 #define BR_PTHREADS 1
36 #endif /* BR_PTHREADS */
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <limits.h>
41 #include <string.h>
42 #include "prefix.h"
44 #ifdef __cplusplus
45 extern "C" {
46 #endif /* __cplusplus */
49 #undef NULL
50 #define NULL ((void *) 0)
52 #ifdef __GNUC__
53 #define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
54 #else
55 #define br_return_val_if_fail(expr,val) if (!(expr)) return val
56 #endif /* __GNUC__ */
59 static br_locate_fallback_func fallback_func = NULL;
60 static void *fallback_data = NULL;
63 #ifdef ENABLE_BINRELOC
64 #include <sys/types.h>
65 #include <sys/stat.h>
66 #include <sys/param.h>
67 #include <unistd.h>
70 /**
71 * br_locate:
72 * symbol: A symbol that belongs to the app/library you want to locate.
73 * Returns: A newly allocated string containing the full path of the
74 * app/library that func belongs to, or NULL on error. This
75 * string should be freed when not when no longer needed.
77 * Finds out to which application or library symbol belongs, then locate
78 * the full path of that application or library.
79 * Note that symbol cannot be a pointer to a function. That will not work.
81 * Example:
82 * --> main.c
83 * #include "prefix.h"
84 * #include "libfoo.h"
86 * int main (int argc, char *argv[]) {
87 * printf ("Full path of this app: %s\n", br_locate (&argc));
88 * libfoo_start ();
89 * return 0;
90 * }
92 * --> libfoo.c starts here
93 * #include "prefix.h"
95 * void libfoo_start () {
96 * --> "" is a symbol that belongs to libfoo (because it's called
97 * --> from libfoo_start()); that's why this works.
98 * printf ("libfoo is located in: %s\n", br_locate (""));
99 * }
101 char *
102 br_locate (void *symbol)
104 char line[5000];
105 FILE *f;
106 char *path;
108 br_return_val_if_fail (symbol != NULL, NULL);
110 f = fopen ("/proc/self/maps", "r");
111 if (!f) {
112 if (fallback_func)
113 return fallback_func(symbol, fallback_data);
114 else
115 return NULL;
118 while (!feof (f))
120 unsigned long start, end;
122 if (!fgets (line, sizeof (line), f))
123 continue;
124 if (!strstr (line, " r-xp ") || !strchr (line, '/'))
125 continue;
127 sscanf (line, "%lx-%lx ", &start, &end);
128 if (symbol >= (void *) start && symbol < (void *) end)
130 char *tmp;
131 size_t len;
133 /* Extract the filename; it is always an absolute path */
134 path = strchr (line, '/');
136 /* Get rid of the newline */
137 tmp = strrchr (path, '\n');
138 if (tmp) *tmp = 0;
140 /* Get rid of "(deleted)" */
141 len = strlen (path);
142 if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
144 tmp = path + len - 10;
145 *tmp = 0;
148 fclose(f);
149 return strdup (path);
153 fclose (f);
154 return NULL;
159 * br_locate_prefix:
160 * symbol: A symbol that belongs to the app/library you want to locate.
161 * Returns: A prefix. This string should be freed when no longer needed.
163 * Locates the full path of the app/library that symbol belongs to, and return
164 * the prefix of that path, or NULL on error.
165 * Note that symbol cannot be a pointer to a function. That will not work.
167 * Example:
168 * --> This application is located in /usr/bin/foo
169 * br_locate_prefix (&argc); --> returns: "/usr"
171 char *
172 br_locate_prefix (void *symbol)
174 char *path, *prefix;
176 br_return_val_if_fail (symbol != NULL, NULL);
178 path = br_locate (symbol);
179 if (!path) return NULL;
181 prefix = br_extract_prefix (path);
182 free (path);
183 return prefix;
188 * br_prepend_prefix:
189 * symbol: A symbol that belongs to the app/library you want to locate.
190 * path: The path that you want to prepend the prefix to.
191 * Returns: The new path, or NULL on error. This string should be freed when no
192 * longer needed.
194 * Gets the prefix of the app/library that symbol belongs to. Prepend that prefix to path.
195 * Note that symbol cannot be a pointer to a function. That will not work.
197 * Example:
198 * --> The application is /usr/bin/foo
199 * br_prepend_prefix (&argc, "/share/foo/data.png"); --> Returns "/usr/share/foo/data.png"
201 char *
202 br_prepend_prefix (void *symbol, char *path)
204 char *tmp, *newpath;
206 br_return_val_if_fail (symbol != NULL, NULL);
207 br_return_val_if_fail (path != NULL, NULL);
209 tmp = br_locate_prefix (symbol);
210 if (!tmp) return NULL;
212 if (strcmp (tmp, "/") == 0)
213 newpath = strdup (path);
214 else
215 newpath = br_strcat (tmp, path);
217 /* Get rid of compiler warning ("br_prepend_prefix never used") */
218 if (0) br_prepend_prefix (NULL, NULL);
220 free (tmp);
221 return newpath;
224 #endif /* ENABLE_BINRELOC */
227 /* Pthread stuff for thread safetiness */
228 #if BR_PTHREADS && defined(ENABLE_BINRELOC)
230 #include <pthread.h>
232 static pthread_key_t br_thread_key;
233 static pthread_once_t br_thread_key_once = PTHREAD_ONCE_INIT;
236 static void
237 br_thread_local_store_fini ()
239 char *specific;
241 specific = (char *) pthread_getspecific (br_thread_key);
242 if (specific)
244 free (specific);
245 pthread_setspecific (br_thread_key, NULL);
247 pthread_key_delete (br_thread_key);
248 br_thread_key = 0;
252 static void
253 br_str_free (void *str)
255 if (str)
256 free (str);
260 static void
261 br_thread_local_store_init ()
263 if (pthread_key_create (&br_thread_key, br_str_free) == 0)
264 atexit (br_thread_local_store_fini);
267 #else /* BR_PTHREADS */
268 #ifdef ENABLE_BINRELOC
270 static char *br_last_value = (char *) NULL;
272 static void
273 br_free_last_value ()
275 if (br_last_value)
276 free (br_last_value);
279 #endif /* ENABLE_BINRELOC */
280 #endif /* BR_PTHREADS */
283 #ifdef ENABLE_BINRELOC
286 * br_thread_local_store:
287 * str: A dynamically allocated string.
288 * Returns: str. This return value must not be freed.
290 * Store str in a thread-local variable and return str. The next
291 * you run this function, that variable is freed too.
292 * This function is created so you don't have to worry about freeing
293 * strings. Just be careful about doing this sort of thing:
295 * some_function( BR_DATADIR("/one.png"), BR_DATADIR("/two.png") )
297 * Examples:
298 * char *foo;
299 * foo = br_thread_local_store (strdup ("hello")); --> foo == "hello"
300 * foo = br_thread_local_store (strdup ("world")); --> foo == "world"; "hello" is now freed.
302 const char *
303 br_thread_local_store (char *str)
305 #if BR_PTHREADS
306 char *specific;
308 pthread_once (&br_thread_key_once, br_thread_local_store_init);
310 specific = (char *) pthread_getspecific (br_thread_key);
311 br_str_free (specific);
312 pthread_setspecific (br_thread_key, str);
314 #else /* BR_PTHREADS */
315 static int initialized = 0;
317 if (!initialized)
319 atexit (br_free_last_value);
320 initialized = 1;
323 if (br_last_value)
324 free (br_last_value);
325 br_last_value = str;
326 #endif /* BR_PTHREADS */
328 return (const char *) str;
331 #endif /* ENABLE_BINRELOC */
335 * br_strcat:
336 * str1: A string.
337 * str2: Another string.
338 * Returns: A newly-allocated string. This string should be freed when no longer needed.
340 * Concatenate str1 and str2 to a newly allocated string.
342 char *
343 br_strcat (const char *str1, const char *str2)
345 char *result;
346 size_t len1, len2;
348 if (!str1) str1 = "";
349 if (!str2) str2 = "";
351 len1 = strlen (str1);
352 len2 = strlen (str2);
354 result = (char *) malloc (len1 + len2 + 1);
355 memcpy (result, str1, len1);
356 memcpy (result + len1, str2, len2);
357 result[len1 + len2] = '\0';
359 return result;
363 /* Emulates glibc's strndup() */
364 static char *
365 br_strndup (char *str, size_t size)
367 char *result = (char *) NULL;
368 size_t len;
370 br_return_val_if_fail (str != (char *) NULL, (char *) NULL);
372 len = strlen (str);
373 if (!len) return strdup ("");
374 if (size > len) size = len;
376 result = (char *) calloc (sizeof (char), len + 1);
377 memcpy (result, str, size);
378 return result;
383 * br_extract_dir:
384 * path: A path.
385 * Returns: A directory name. This string should be freed when no longer needed.
387 * Extracts the directory component of path. Similar to g_dirname() or the dirname
388 * commandline application.
390 * Example:
391 * br_extract_dir ("/usr/local/foobar"); --> Returns: "/usr/local"
393 char *
394 br_extract_dir (const char *path)
396 char *end, *result;
398 br_return_val_if_fail (path != (char *) NULL, (char *) NULL);
400 end = strrchr (path, '/');
401 if (!end) return strdup (".");
403 while (end > path && *end == '/')
404 end--;
405 result = br_strndup ((char *) path, end - path + 1);
406 if (!*result)
408 free (result);
409 return strdup ("/");
410 } else
411 return result;
416 * br_extract_prefix:
417 * path: The full path of an executable or library.
418 * Returns: The prefix, or NULL on error. This string should be freed when no longer needed.
420 * Extracts the prefix from path. This function assumes that your executable
421 * or library is installed in an LSB-compatible directory structure.
423 * Example:
424 * br_extract_prefix ("/usr/bin/gnome-panel"); --> Returns "/usr"
425 * br_extract_prefix ("/usr/local/lib/libfoo.so"); --> Returns "/usr/local"
426 * br_extract_prefix ("/usr/local/libfoo.so"); --> Returns "/usr"
428 char *
429 br_extract_prefix (const char *path)
431 char *end, *tmp, *result;
433 br_return_val_if_fail (path != (char *) NULL, (char *) NULL);
435 if (!*path) return strdup ("/");
436 end = strrchr (path, '/');
437 if (!end) return strdup (path);
439 tmp = br_strndup ((char *) path, end - path);
440 if (!*tmp)
442 free (tmp);
443 return strdup ("/");
445 end = strrchr (tmp, '/');
446 if (!end) return tmp;
448 result = br_strndup (tmp, end - tmp);
449 free (tmp);
451 if (!*result)
453 free (result);
454 result = strdup ("/");
457 return result;
462 * br_set_fallback_function:
463 * func: A function to call to find the binary.
464 * data: User data to pass to func.
466 * Sets a function to call to find the path to the binary, in
467 * case "/proc/self/maps" can't be opened. The function set should
468 * return a string that is safe to free with free().
470 void
471 br_set_locate_fallback_func (br_locate_fallback_func func, void *data)
473 fallback_func = func;
474 fallback_data = data;
478 #ifdef __cplusplus
480 #endif /* __cplusplus */
482 #endif /* _PREFIX_C */