Mercurial > hg > xemacs-beta
comparison lib-src/insert-data-in-exec.c @ 2015:2364237fbc0f
[xemacs-hg @ 2004-04-15 20:56:17 by olivierg]
Moving the dump file inside the executable
author | olivierg |
---|---|
date | Thu, 15 Apr 2004 20:56:23 +0000 |
parents | |
children | d31a2df82ddb |
comparison
equal
deleted
inserted
replaced
2014:92f7301e4a23 | 2015:2364237fbc0f |
---|---|
1 /* Copies the dump file inside the xemacs executable */ | |
2 | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 | |
7 static const unsigned char key[] = { | |
8 255, | |
9 6, | |
10 1, | |
11 2, | |
12 3, | |
13 4, | |
14 255, | |
15 3, | |
16 9, | |
17 62, | |
18 255, | |
19 10, | |
20 4, | |
21 61, | |
22 255 | |
23 }; | |
24 | |
25 int main(int argc, char **argv) | |
26 { | |
27 FILE *te, *xe, *dump; | |
28 unsigned char *xed, *p; | |
29 long size, size_dump, size1, i; | |
30 long max_size, offset; | |
31 | |
32 char msg[65536]; | |
33 | |
34 if(argc != 6 && (argc != 3 || strcmp(argv[1], "-s"))) { | |
35 fprintf(stderr, "Usage:\n%s temacs xemacs.dmp xemacs size offset\n%s -s xemacs.dmp\n", argv[0], argv[0]); | |
36 exit(1); | |
37 } | |
38 | |
39 if(argc == 3) { | |
40 sprintf(msg, "Opening %s failed", argv[2]); | |
41 dump = fopen(argv[2], "rb+"); | |
42 if(!dump) { | |
43 perror(msg); | |
44 exit(1); | |
45 } | |
46 | |
47 if(fseek(dump, 0, SEEK_END)) { | |
48 perror("fseek end dump"); | |
49 exit(1); | |
50 } | |
51 | |
52 size = ftell(dump); | |
53 if(size == -1) { | |
54 perror("ftell dump"); | |
55 exit(1); | |
56 } | |
57 | |
58 printf("%ld\n", size); | |
59 exit(0); | |
60 } | |
61 | |
62 | |
63 max_size = strtol(argv[4], 0, 10); | |
64 offset = strtol(argv[5], 0, 10); | |
65 | |
66 sprintf(msg, "Opening %s failed", argv[1]); | |
67 te = fopen(argv[1], "rb"); | |
68 if(!te) { | |
69 perror(msg); | |
70 exit(1); | |
71 } | |
72 | |
73 if(fseek(te, 0, SEEK_END)) { | |
74 perror("fseek end"); | |
75 exit(1); | |
76 } | |
77 | |
78 size = ftell(te); | |
79 if(size == -1) { | |
80 perror("ftell"); | |
81 exit(1); | |
82 } | |
83 | |
84 if(fseek(te, 0, SEEK_SET)) { | |
85 perror("fseek beginning"); | |
86 exit(1); | |
87 } | |
88 | |
89 xed = malloc(size); | |
90 if(!xed) { | |
91 perror("malloc"); | |
92 exit(1); | |
93 } | |
94 | |
95 size1 = fread(xed, 1, size, te); | |
96 if(size1 != size) { | |
97 if(ferror(te)) { | |
98 perror("fread temacs"); | |
99 exit(1); | |
100 } | |
101 fprintf(stderr, "Fread returned %ld, expected %ld ?\n", size1, size); | |
102 exit(1); | |
103 } | |
104 | |
105 if(fclose(te)) { | |
106 perror("fclose temacs"); | |
107 exit(1); | |
108 } | |
109 | |
110 p = xed; | |
111 for(i=0; i<size-(long)sizeof(key); i++) { | |
112 if(!memcmp(p, key, sizeof(key))) | |
113 goto found; | |
114 p++; | |
115 } | |
116 | |
117 fprintf(stderr, "dumped_data key not found in executable.\n"); | |
118 exit(1); | |
119 | |
120 found: | |
121 fprintf(stderr, "dumped_data found at offset 0x%lx, patching.\n", i); | |
122 | |
123 sprintf(msg, "Opening %s failed", argv[2]); | |
124 dump = fopen(argv[2], "r"); | |
125 if(!dump) { | |
126 perror(msg); | |
127 exit(1); | |
128 } | |
129 | |
130 if(fseek(dump, 0, SEEK_END)) { | |
131 perror("fseek end dump"); | |
132 exit(1); | |
133 } | |
134 | |
135 size_dump = ftell(dump); | |
136 if(size_dump == -1) { | |
137 perror("ftell dump"); | |
138 exit(1); | |
139 } | |
140 | |
141 if(size_dump > max_size) { | |
142 fprintf(stderr, "Dump file too big for available space (max=%ld, dump=%ld)\n", max_size, size); | |
143 exit(2); | |
144 } | |
145 | |
146 if(fseek(dump, 0, SEEK_SET)) { | |
147 perror("fseek beginning dump"); | |
148 exit(1); | |
149 } | |
150 | |
151 size1 = fread(xed+i+offset, 1, size_dump, dump); | |
152 if(size1 != size_dump) { | |
153 if(ferror(dump)) { | |
154 perror("fread dump"); | |
155 exit(1); | |
156 } | |
157 fprintf(stderr, "Fread dump returned %ld, expected %ld ?\n", size1, size_dump); | |
158 exit(1); | |
159 } | |
160 | |
161 if(fclose(dump)) { | |
162 perror("fclose dump"); | |
163 exit(1); | |
164 } | |
165 | |
166 memset(xed+i, 0, offset); | |
167 | |
168 xed[i ] = size_dump; | |
169 xed[i+1] = size_dump >> 8; | |
170 xed[i+2] = size_dump >> 16; | |
171 xed[i+3] = size_dump >> 24; | |
172 | |
173 fprintf(stderr, "dumped_data found at offset 0x%lx, patching.\n", i); | |
174 | |
175 sprintf(msg, "Opening %s failed", argv[3]); | |
176 xe = fopen(argv[3], "wb"); | |
177 if(!xe) { | |
178 perror(msg); | |
179 exit(1); | |
180 } | |
181 | |
182 size1 = fwrite(xed, 1, size, xe); | |
183 if(size1 != size) { | |
184 if(ferror(xe)) { | |
185 perror("fwrite xemacs"); | |
186 exit(1); | |
187 } | |
188 fprintf(stderr, "Fwrite xemacs returned %ld, expected %ld ?\n", size1, size); | |
189 exit(1); | |
190 } | |
191 | |
192 if(fclose(xe)) { | |
193 perror("fclose xemacs"); | |
194 exit(1); | |
195 } | |
196 | |
197 exit(0); | |
198 } | |
199 |