Mercurial > hg > xemacs-beta
annotate lib-src/insert-data-in-exec.c @ 5919:2800105fcc9f cygwin
root of all my changes
author | Henry Thompson <ht@markup.co.uk> |
---|---|
date | Thu, 16 Dec 2021 11:20:17 +0000 |
parents | 86d33ddc7fd6 |
children |
rev | line source |
---|---|
4722
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
1 /* Copies the dump file inside the xemacs executable. |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
2 Copyright (C) 2003-2004 Olivier Galibert. |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
3 Copyright (C) 2003 Larry McVoy. |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
4 |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
5 Redistribution and use in source and binary forms, with or without |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
6 modification, are permitted provided that the following conditions are met: |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
7 |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
8 1. Redistributions of source code must retain the above copyright notice, |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
9 this list of conditions and the following disclaimer. |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
10 2. Redistributions in binary form must reproduce the above copyright notice, |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
11 this list of conditions and the following disclaimer in the documentation |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
12 and/or other materials provided with the distribution. |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
13 |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
14 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
15 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LARRY |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
17 MCVOY, THE XEMACS PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
18 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
19 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
20 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
21 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
22 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
23 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
24 |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
25 The views and conclusions contained in the software and documentation are those |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
26 of the authors and should not be interpreted as representing official policies, |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
27 either expressed or implied, of the XEmacs Project. |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
28 |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
29 The "key" array is the work of Larry McVoy. See |
dfc9fe46c294
Add BSD header to lib-src/insert-data-in-exec.c, with permission of Olivier
Jerry James <james@xemacs.org>
parents:
2502
diff
changeset
|
30 http://lkml.org/lkml/2003/7/11/141 for more information. */ |
2015 | 31 |
32 #include <stdio.h> | |
33 #include <stdlib.h> | |
34 #include <string.h> | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
35 #include <config.h> |
2015 | 36 |
37 static const unsigned char key[] = { | |
38 255, | |
39 6, | |
40 1, | |
41 2, | |
42 3, | |
43 4, | |
44 255, | |
45 3, | |
46 9, | |
47 62, | |
48 255, | |
49 10, | |
50 4, | |
51 61, | |
52 255 | |
53 }; | |
54 | |
55 int main(int argc, char **argv) | |
56 { | |
57 FILE *te, *xe, *dump; | |
58 unsigned char *xed, *p; | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
59 size_t size, size_dump, size1, i; |
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
60 size_t max_size, offset; |
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
61 OFF_T off; |
2015 | 62 |
63 char msg[65536]; | |
64 | |
65 if(argc != 6 && (argc != 3 || strcmp(argv[1], "-s"))) { | |
66 fprintf(stderr, "Usage:\n%s temacs xemacs.dmp xemacs size offset\n%s -s xemacs.dmp\n", argv[0], argv[0]); | |
67 exit(1); | |
68 } | |
69 | |
70 if(argc == 3) { | |
71 sprintf(msg, "Opening %s failed", argv[2]); | |
72 dump = fopen(argv[2], "rb+"); | |
73 if(!dump) { | |
74 perror(msg); | |
75 exit(1); | |
76 } | |
77 | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
78 if(FSEEK(dump, 0, SEEK_END)) { |
2015 | 79 perror("fseek end dump"); |
80 exit(1); | |
81 } | |
82 | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
83 off = FTELL(dump); |
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
84 if(off == -1) { |
2015 | 85 perror("ftell dump"); |
86 exit(1); | |
87 } | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
88 size = (size_t)off; |
2015 | 89 |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
90 printf("%zu\n", size); |
2015 | 91 exit(0); |
92 } | |
93 | |
94 | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
95 max_size = strtoul(argv[4], 0, 10); |
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
96 offset = strtoul(argv[5], 0, 10); |
2015 | 97 |
98 sprintf(msg, "Opening %s failed", argv[1]); | |
99 te = fopen(argv[1], "rb"); | |
100 if(!te) { | |
101 perror(msg); | |
102 exit(1); | |
103 } | |
104 | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
105 if(FSEEK(te, 0, SEEK_END)) { |
2015 | 106 perror("fseek end"); |
107 exit(1); | |
108 } | |
109 | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
110 off = FTELL(te); |
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
111 if(off == -1) { |
2015 | 112 perror("ftell"); |
113 exit(1); | |
114 } | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
115 size = (size_t)off; |
2015 | 116 |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
117 if(FSEEK(te, 0, SEEK_SET)) { |
2015 | 118 perror("fseek beginning"); |
119 exit(1); | |
120 } | |
121 | |
122 xed = malloc(size); | |
123 if(!xed) { | |
124 perror("malloc"); | |
125 exit(1); | |
126 } | |
127 | |
128 size1 = fread(xed, 1, size, te); | |
129 if(size1 != size) { | |
130 if(ferror(te)) { | |
131 perror("fread temacs"); | |
132 exit(1); | |
133 } | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
134 fprintf(stderr, "Fread returned %zu, expected %zu ?\n", size1, size); |
2015 | 135 exit(1); |
136 } | |
137 | |
138 if(fclose(te)) { | |
139 perror("fclose temacs"); | |
140 exit(1); | |
141 } | |
142 | |
143 p = xed; | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
144 for(i=0; i<size-sizeof(key); i++) { |
2015 | 145 if(!memcmp(p, key, sizeof(key))) |
146 goto found; | |
147 p++; | |
148 } | |
149 | |
150 fprintf(stderr, "dumped_data key not found in executable.\n"); | |
151 exit(1); | |
152 | |
153 found: | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
154 fprintf(stderr, "dumped_data found at offset 0x%zx, patching.\n", i); |
2015 | 155 |
156 sprintf(msg, "Opening %s failed", argv[2]); | |
2502 | 157 dump = fopen(argv[2], "rb"); |
2015 | 158 if(!dump) { |
159 perror(msg); | |
160 exit(1); | |
161 } | |
162 | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
163 if(FSEEK(dump, 0, SEEK_END)) { |
2015 | 164 perror("fseek end dump"); |
165 exit(1); | |
166 } | |
167 | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
168 off = FTELL(dump); |
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
169 if(off == -1) { |
2015 | 170 perror("ftell dump"); |
171 exit(1); | |
172 } | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
173 size_dump = (size_t)off; |
2015 | 174 |
175 if(size_dump > max_size) { | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
176 fprintf(stderr, "Dump file too big for available space (max=%zu, dump=%zu)\n", max_size, size_dump); |
2015 | 177 exit(2); |
178 } | |
179 | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
180 if(FSEEK(dump, 0, SEEK_SET)) { |
2015 | 181 perror("fseek beginning dump"); |
182 exit(1); | |
183 } | |
184 | |
185 size1 = fread(xed+i+offset, 1, size_dump, dump); | |
186 if(size1 != size_dump) { | |
187 if(ferror(dump)) { | |
188 perror("fread dump"); | |
189 exit(1); | |
190 } | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
191 fprintf(stderr, "Fread dump returned %zu, expected %zu ?\n", size1, size_dump); |
2015 | 192 exit(1); |
193 } | |
194 | |
195 if(fclose(dump)) { | |
196 perror("fclose dump"); | |
197 exit(1); | |
198 } | |
199 | |
200 memset(xed+i, 0, offset); | |
201 | |
202 xed[i ] = size_dump; | |
203 xed[i+1] = size_dump >> 8; | |
204 xed[i+2] = size_dump >> 16; | |
205 xed[i+3] = size_dump >> 24; | |
206 | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
207 fprintf(stderr, "dumped_data found at offset 0x%zx, patching.\n", i); |
2015 | 208 |
209 sprintf(msg, "Opening %s failed", argv[3]); | |
210 xe = fopen(argv[3], "wb"); | |
211 if(!xe) { | |
212 perror(msg); | |
213 exit(1); | |
214 } | |
215 | |
216 size1 = fwrite(xed, 1, size, xe); | |
217 if(size1 != size) { | |
218 if(ferror(xe)) { | |
219 perror("fwrite xemacs"); | |
220 exit(1); | |
221 } | |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
4722
diff
changeset
|
222 fprintf(stderr, "Fwrite xemacs returned %zu, expected %zu ?\n", size1, size); |
2015 | 223 exit(1); |
224 } | |
225 | |
226 if(fclose(xe)) { | |
227 perror("fclose xemacs"); | |
228 exit(1); | |
229 } | |
230 | |
231 exit(0); | |
232 } | |
233 |