Repository navigation
Expand file tree
/
Copy pathpatchlib.c
More file actions
143 lines (121 loc) · 4.51 KB
/
Copy pathpatchlib.c
File metadata and controls
143 lines (121 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* $Id: patchlib.c,v 1.7 2007/11/07 10:26:31 pekberg Exp $
******************************************************************************
Alter the path to the config file in binary libggi
Copyright (C) 1998 Marcus Sundberg [marcus@ggi-project.org]
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <sys/stat.h>
static void print_usage(const char *fname)
{
fprintf(stderr, "Usage: %s libggi.so-filename [new_path_to_patch_in]\n",
fname);
fprintf(stderr,
" Output will be located in a file named as the original\n"
" with \".patched\" appended, e.g. libggi.so.patched\n");
}
static char tag[] = GGIPATHTAG;
#define PATH_SPACE (256)
int main(int argc, const char *argv[])
{
int size;
int fd;
struct stat st;
char *solib;
char *path;
char *newname;
int offset=0;
int i;
if (argc<2) {
print_usage(argv[0]);
exit(0);
}
fd=open(argv[1], O_RDWR);
if ((fd=open(argv[1], O_RDWR)) < 0 ||
fstat(fd, &st) != 0 ||
(solib=malloc(size=st.st_size)) == NULL ||
read(fd, solib, size) < size) {
perror(argv[1]);
exit(1);
}
close(fd);
path=solib;
while ((path=memchr(path, tag[0], size-offset)) != NULL) {
if (memcmp(path, tag, strlen(tag)) == 0) {
path += GGITAGLEN;
offset = path-solib;
break;
}
path++;
offset = path-solib;
}
if (path == NULL) {
fprintf(stderr,
"Could not locate the path tag, bailing out...\n");
exit(1);
}
printf("Currently compiled in path (at %d) is :\n%s\n", offset, path);
if (offset > size - PATH_SPACE) {
fprintf(stderr,
"Not enough space after tag, bailing out...\n");
exit(1);
}
for (i = 0; i < PATH_SPACE; ++i) {
if (!path[i])
/* first nul found */
break;
}
for (; i < PATH_SPACE; ++i) {
if (path[i]) {
/* ouch, found non-nul character */
fprintf(stderr,
"Non-empty tail after the existing path, "
"bailing out...\n");
exit(1);
}
}
if (argc == 2) {
fprintf(stderr, "No new path given. No new file generated.\n");
exit(1);
}
strncpy(path, argv[2], PATH_SPACE);
newname=malloc(strlen(argv[1]) + strlen(".patched") + 1);
if (newname == NULL) {
fprintf(stderr, "Memory allocation error\n");
exit(1);
}
strcpy(newname, argv[1]);
strcat(newname, ".patched");
if ((fd=creat(newname, st.st_mode)) < 0 ||
write(fd, solib, size) < size) {
perror(newname);
exit(1);
}
printf("New path (at %d) in %s is :\n%s\n", offset, newname, argv[2]);
return 0;
}