Rev 509 | Rev 511 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 509 | Rev 510 | ||
---|---|---|---|
Line 272... | Line 272... | ||
272 | FSClose(fptr); |
272 | FSClose(fptr); |
273 | 273 | ||
274 | return res == noErr; // res=0 means everything was noErr, res=1 means something was !=noErr |
274 | return res == noErr; // res=0 means everything was noErr, res=1 means something was !=noErr |
275 | } |
275 | } |
276 | 276 | ||
277 | int binary_replace_file(FSSpec* dst, uint64_t search, uint64_t replace, Boolean align, int maxamount) { |
- | |
278 | uint64_t srecord = 0; |
- | |
279 | int found = 0; |
- | |
280 | FILEREF fptr; |
- | |
281 | FILECOUNT cnt; |
- | |
282 | - | ||
283 | if (FSpOpenDF(dst, fsRdWrPerm, &fptr) != noErr) return -1; |
- | |
284 | - | ||
285 | cnt = sizeof(srecord); |
- | |
286 | while (FSRead(fptr, &cnt, &srecord) == noErr) |
- | |
287 | { |
- | |
288 | if (cnt != sizeof(srecord)) break; // EOF reached |
- | |
289 | if (srecord == search) { |
- | |
290 | srecord = replace; |
- | |
291 | SetFPos(fptr, fsFromMark, -1 * (long)sizeof(srecord)); |
- | |
292 | cnt = (int)sizeof(srecord); |
- | |
293 | FSWrite(fptr, &cnt, &srecord); |
- | |
294 | SetFPos(fptr, fsFromStart, 0); // important for fseek |
- | |
295 | found++; |
- | |
296 | if (found == maxamount) break; |
- | |
297 | } |
- | |
298 | else { |
- | |
299 | if (!align) { |
- | |
300 | SetFPos(fptr, fsFromMark, -1 * (long)(sizeof(srecord) - 1)); |
- | |
301 | } |
- | |
302 | } |
- | |
303 | } |
- | |
304 | FSClose(fptr); |
- | |
305 | - | ||
306 | return found; |
- | |
307 | } |
- | |
308 | - | ||
309 | uint32_t calculate_checksum(FSSpec* dst) { |
277 | uint32_t calculate_checksum(FSSpec* dst) { |
310 | //Calculate checksum of image |
278 | //Calculate checksum of image |
311 | // Taken from "PE Bliss" Cross-Platform Portable Executable C++ Library |
279 | // Taken from "PE Bliss" Cross-Platform Portable Executable C++ Library |
312 | // https://github.com/mrexodia/portable-executable-library/blob/master/pe_lib/pe_checksum.cpp |
280 | // https://github.com/mrexodia/portable-executable-library/blob/master/pe_lib/pe_checksum.cpp |
313 | // Converted from C++ to C by Daniel Marschall |
281 | // Converted from C++ to C by Daniel Marschall |
Line 545... | Line 513... | ||
545 | 513 | ||
546 | // First try with alignment "4" (this should be the usual case), |
514 | // First try with alignment "4" (this should be the usual case), |
547 | // and if that failed, try without alignment ("1"). |
515 | // and if that failed, try without alignment ("1"). |
548 | // We only need to set maxamount to "1", because "const volatile" makes sure that |
516 | // We only need to set maxamount to "1", because "const volatile" makes sure that |
549 | // the compiler won't place (inline) it at several locations in the code. |
517 | // the compiler won't place (inline) it at several locations in the code. |
550 | // TODO: This is very slow |
- | |
551 | if ((binary_replace_file(dst, GetObfuscSeed(), obfuscseed, /*0 means "align to 1"*/0, /*maxamount=*/1) == 0) || |
518 | if (!obfusc_seed_replace(dst, GetObfuscSeed(), GetObfuscSeed2(), obfuscseed, obfuscseed2, 1, 1)) |
552 | (binary_replace_file(dst, GetObfuscSeed2(), obfuscseed2, /*0 means "align to 1"*/0, /*maxamount=*/1) == 0)) |
- | |
553 | { |
519 | { |
554 | simplewarning((TCHAR*)TEXT("binary_replace_file failed")); // TODO (Not so important): TRANSLATE |
520 | simplewarning((TCHAR*)TEXT("obfusc_seed_replace failed")); // TODO (Not so important): TRANSLATE |
555 | discard = true; |
521 | discard = true; |
556 | } |
522 | } |
557 | } |
523 | } |
558 | 524 | ||
559 | if (!update_pe_timestamp(dst, (__time32_t)time(0))) { |
525 | if (!update_pe_timestamp(dst, (__time32_t)time(0))) { |