Rev 454 | Rev 482 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 454 | Rev 456 | ||
---|---|---|---|
Line 28... | Line 28... | ||
28 | #include "versioninfo_modify_win.h" |
28 | #include "versioninfo_modify_win.h" |
29 | #include "version.h" |
29 | #include "version.h" |
30 | 30 | ||
31 | extern HINSTANCE hDllInstance; |
31 | extern HINSTANCE hDllInstance; |
32 | 32 | ||
- | 33 | typedef struct _PE32 { |
|
- | 34 | uint32_t magic; // 0x50450000 |
|
- | 35 | IMAGE_FILE_HEADER fileHeader; // COFF Header without Signature |
|
- | 36 | IMAGE_OPTIONAL_HEADER32 optHeader; // Standard COFF fields, Windows Specific Fields, Data Directories |
|
- | 37 | } PE32; |
|
- | 38 | ||
33 | Boolean doresources(FSSpec* dst, int bits); |
39 | Boolean doresources(FSSpec* dst, int bits); |
34 | 40 | ||
35 | void dbglasterror(TCHAR *func){ |
41 | void dbglasterror(TCHAR *func){ |
36 | TCHAR s[0x300] = {0}; |
42 | TCHAR s[0x300] = {0}; |
37 | 43 | ||
Line 208... | Line 214... | ||
208 | free(changeRequestStrW); |
214 | free(changeRequestStrW); |
209 | 215 | ||
210 | return dwError; |
216 | return dwError; |
211 | } |
217 | } |
212 | 218 | ||
- | 219 | typedef long __time32_t; |
|
- | 220 | ||
213 | Boolean update_pe_timestamp(const FSSpec* dst, time_t timestamp) { |
221 | Boolean update_pe_timestamp(const FSSpec* dst, __time32_t timestamp) { |
214 | size_t peoffset; |
222 | size_t peoffset; |
215 | FILEREF fptr; |
223 | FILEREF fptr; |
216 | Boolean res; |
224 | Boolean res; |
217 | FILECOUNT cnt; |
225 | FILECOUNT cnt; |
218 | 226 | ||
Line 220... | Line 228... | ||
220 | 228 | ||
221 | res = |
229 | res = |
222 | SetFPos(fptr, fsFromStart, 0x3C) || |
230 | SetFPos(fptr, fsFromStart, 0x3C) || |
223 | (cnt = sizeof(peoffset), noErr) || |
231 | (cnt = sizeof(peoffset), noErr) || |
224 | FSRead(fptr, &cnt, &peoffset) || |
232 | FSRead(fptr, &cnt, &peoffset) || |
225 | SetFPos(fptr, fsFromStart, (long)peoffset + 8) || |
233 | SetFPos(fptr, fsFromStart, (long)peoffset + /*0x0008*/offsetof(PE32, fileHeader.TimeDateStamp)) || |
226 | (cnt = sizeof(time_t), noErr) || |
234 | (cnt = sizeof(__time32_t), noErr) || |
227 | FSWrite(fptr, &cnt, ×tamp); |
235 | FSWrite(fptr, &cnt, ×tamp); |
228 | 236 | ||
229 | FSClose(fptr); |
237 | FSClose(fptr); |
230 | 238 | ||
231 | return res == noErr; // res=0 means everything was noErr, res=1 means something was !=noErr |
239 | return res == noErr; // res=0 means everything was noErr, res=1 means something was !=noErr |
Line 260... | Line 268... | ||
260 | FSClose(fptr); |
268 | FSClose(fptr); |
261 | 269 | ||
262 | return found; |
270 | return found; |
263 | } |
271 | } |
264 | 272 | ||
265 | //DOS .EXE header |
- | |
266 | struct image_dos_header |
- | |
267 | { |
- | |
268 | uint16_t e_magic; // Magic number |
- | |
269 | uint16_t e_cblp; // Bytes on last page of file |
- | |
270 | uint16_t e_cp; // Pages in file |
- | |
271 | uint16_t e_crlc; // Relocations |
- | |
272 | uint16_t e_cparhdr; // Size of header in paragraphs |
- | |
273 | uint16_t e_minalloc; // Minimum extra paragraphs needed |
- | |
274 | uint16_t e_maxalloc; // Maximum extra paragraphs needed |
- | |
275 | uint16_t e_ss; // Initial (relative) SS value |
- | |
276 | uint16_t e_sp; // Initial SP value |
- | |
277 | uint16_t e_csum; // Checksum |
- | |
278 | uint16_t e_ip; // Initial IP value |
- | |
279 | uint16_t e_cs; // Initial (relative) CS value |
- | |
280 | uint16_t e_lfarlc; // File address of relocation table |
- | |
281 | uint16_t e_ovno; // Overlay number |
- | |
282 | uint16_t e_res[4]; // Reserved words |
- | |
283 | uint16_t e_oemid; // OEM identifier (for e_oeminfo) |
- | |
284 | uint16_t e_oeminfo; // OEM information; e_oemid specific |
- | |
285 | uint16_t e_res2[10]; // Reserved words |
- | |
286 | int32_t e_lfanew; // File address of new exe header |
- | |
287 | }; |
- | |
288 | - | ||
289 | struct image_file_header |
- | |
290 | { |
- | |
291 | uint16_t Machine; |
- | |
292 | uint16_t NumberOfSections; |
- | |
293 | uint32_t TimeDateStamp; |
- | |
294 | uint32_t PointerToSymbolTable; |
- | |
295 | uint32_t NumberOfSymbols; |
- | |
296 | uint16_t SizeOfOptionalHeader; |
- | |
297 | uint16_t Characteristics; |
- | |
298 | }; |
- | |
299 | - | ||
300 | uint32_t calculate_checksum(FSSpec* dst) { |
273 | uint32_t calculate_checksum(FSSpec* dst) { |
301 | //Calculate checksum of image |
274 | //Calculate checksum of image |
302 | // Taken from "PE Bliss" Cross-Platform Portable Executable C++ Library |
275 | // Taken from "PE Bliss" Cross-Platform Portable Executable C++ Library |
303 | // https://github.com/mrexodia/portable-executable-library/blob/master/pe_lib/pe_checksum.cpp |
276 | // https://github.com/mrexodia/portable-executable-library/blob/master/pe_lib/pe_checksum.cpp |
304 | // Converted from C++ to C by Daniel Marschall |
277 | // Converted from C++ to C by Daniel Marschall |
305 | 278 | ||
306 | FILEREF fptr; |
279 | FILEREF fptr; |
307 | unsigned long long checksum = 0; |
280 | unsigned long long checksum = 0; |
308 | struct image_dos_header header; |
281 | IMAGE_DOS_HEADER header; |
309 | FILEPOS filesize, i; |
282 | FILEPOS filesize, i; |
310 | unsigned long long top; |
283 | unsigned long long top; |
311 | unsigned long pe_checksum_pos; |
284 | unsigned long pe_checksum_pos; |
312 | static const unsigned long checksum_pos_in_optional_headers = 64; |
285 | static const unsigned long checksum_pos_in_optional_headers = 64; |
313 | FILECOUNT cnt; |
286 | FILECOUNT cnt; |
314 | 287 | ||
315 | if (FSpOpenDF(dst, fsRdWrPerm, &fptr) != noErr) return 0x00000000; |
288 | if (FSpOpenDF(dst, fsRdWrPerm, &fptr) != noErr) return 0x00000000; |
316 | 289 | ||
317 | //Read DOS header |
290 | //Read DOS header |
318 | SetFPos(fptr, fsFromStart, 0); |
291 | SetFPos(fptr, fsFromStart, 0); |
319 | cnt = sizeof(struct image_dos_header); |
292 | cnt = sizeof(IMAGE_DOS_HEADER); |
320 | FSRead(fptr, &cnt, &header); |
293 | FSRead(fptr, &cnt, &header); |
321 | 294 | ||
322 | //Calculate PE checksum |
295 | //Calculate PE checksum |
323 | SetFPos(fptr, fsFromStart, 0); |
296 | SetFPos(fptr, fsFromStart, 0); |
324 | top = 0xFFFFFFFF; |
297 | top = 0xFFFFFFFF; |
325 | top++; |
298 | top++; |
326 | 299 | ||
327 | //"CheckSum" field position in optional PE headers - it's always 64 for PE and PE+ |
300 | //"CheckSum" field position in optional PE headers - it's always 64 for PE and PE+ |
328 | //Calculate real PE headers "CheckSum" field position |
301 | //Calculate real PE headers "CheckSum" field position |
329 | //Sum is safe here |
302 | //Sum is safe here |
330 | pe_checksum_pos = header.e_lfanew + sizeof(struct image_file_header) + sizeof(uint32_t) + checksum_pos_in_optional_headers; |
303 | pe_checksum_pos = header.e_lfanew + sizeof(IMAGE_FILE_HEADER) + sizeof(uint32_t) + checksum_pos_in_optional_headers; |
331 | 304 | ||
332 | //Calculate checksum for each byte of file |
305 | //Calculate checksum for each byte of file |
333 | filesize = 0; |
306 | filesize = 0; |
334 | GetEOF(fptr, &filesize); |
307 | GetEOF(fptr, &filesize); |
335 | SetFPos(fptr, fsFromStart, 0); |
308 | SetFPos(fptr, fsFromStart, 0); |
Line 376... | Line 349... | ||
376 | 349 | ||
377 | res = |
350 | res = |
378 | SetFPos(fptr, fsFromStart, 0x3C) || |
351 | SetFPos(fptr, fsFromStart, 0x3C) || |
379 | (cnt = sizeof(peoffset), noErr) || |
352 | (cnt = sizeof(peoffset), noErr) || |
380 | FSRead(fptr, &cnt, &peoffset) || |
353 | FSRead(fptr, &cnt, &peoffset) || |
381 | SetFPos(fptr, fsFromStart, (long)peoffset + 88) || |
354 | SetFPos(fptr, fsFromStart, (long)peoffset + /*0x0058*/offsetof(PE32, optHeader.CheckSum)) || |
382 | (cnt = sizeof(uint32_t), noErr) || |
355 | (cnt = sizeof(uint32_t), noErr) || |
383 | FSWrite(fptr, &cnt, &checksum); |
356 | FSWrite(fptr, &cnt, &checksum); |
384 | 357 | ||
385 | FSClose(fptr); |
358 | FSClose(fptr); |
386 | 359 | ||
Line 545... | Line 518... | ||
545 | dbg((TCHAR*)TEXT("binary_replace_file failed")); |
518 | dbg((TCHAR*)TEXT("binary_replace_file failed")); |
546 | discard = true; |
519 | discard = true; |
547 | } |
520 | } |
548 | } |
521 | } |
549 | 522 | ||
550 | if (!update_pe_timestamp(dst, time(0))) { |
523 | if (!update_pe_timestamp(dst, (__time32_t)time(0))) { |
551 | simplewarning((TCHAR*)TEXT("update_pe_timestamp failed")); |
524 | simplewarning((TCHAR*)TEXT("update_pe_timestamp failed")); |
552 | } |
525 | } |
553 | 526 | ||
554 | if (!repair_pe_checksum(dst)) { |
527 | if (!repair_pe_checksum(dst)) { |
555 | simplewarning((TCHAR*)TEXT("repair_pe_checksum failed")); |
528 | simplewarning((TCHAR*)TEXT("repair_pe_checksum failed")); |