]> git.saurik.com Git - ldid.git/blob - ldid.cpp
8700e7e16dd0ff27a70e5acee7552855d3482ca0
[ldid.git] / ldid.cpp
1 /* JocStrap - Java/Objective-C Bootstrap
2 * Copyright (C) 2007 Jay Freeman (saurik)
3 */
4
5 /*
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the
11 * above copyright notice, this list of conditions
12 * and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the
17 * distribution.
18 * 3. The name of the author may not be used to endorse
19 * or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include "minimal/stdlib.h"
39 #include "minimal/string.h"
40 #include "minimal/mapping.h"
41
42 #include "sha1.h"
43
44 #include <cstring>
45 #include <string>
46 #include <vector>
47
48 #include <sys/wait.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51
52 struct fat_header {
53 uint32_t magic;
54 uint32_t nfat_arch;
55 } _packed;
56
57 #define FAT_MAGIC 0xcafebabe
58 #define FAT_CIGAM 0xbebafeca
59
60 struct fat_arch {
61 uint32_t cputype;
62 uint32_t cpusubtype;
63 uint32_t offset;
64 uint32_t size;
65 uint32_t align;
66 } _packed;
67
68 struct mach_header {
69 uint32_t magic;
70 uint32_t cputype;
71 uint32_t cpusubtype;
72 uint32_t filetype;
73 uint32_t ncmds;
74 uint32_t sizeofcmds;
75 uint32_t flags;
76 } _packed;
77
78 #define MH_MAGIC 0xfeedface
79 #define MH_CIGAM 0xcefaedfe
80
81 #define MH_MAGIC_64 0xfeedfacf
82 #define MH_CIGAM_64 0xcffaedfe
83
84 #define MH_DYLDLINK 0x4
85
86 #define MH_EXECUTE 0x2
87 #define MH_DYLIB 0x6
88 #define MH_BUNDLE 0x8
89 #define MH_DYLIB_STUB 0x9
90
91 struct load_command {
92 uint32_t cmd;
93 uint32_t cmdsize;
94 } _packed;
95
96 #define LC_REQ_DYLD uint32_t(0x80000000)
97
98 #define LC_SEGMENT uint32_t(0x01)
99 #define LC_LOAD_DYLIB uint32_t(0x0c)
100 #define LC_ID_DYLIB uint32_t(0x0d)
101 #define LC_UUID uint32_t(0x1b)
102 #define LC_CODE_SIGNATURE uint32_t(0x1d)
103 #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD)
104
105 struct dylib {
106 uint32_t name;
107 uint32_t timestamp;
108 uint32_t current_version;
109 uint32_t compatibility_version;
110 } _packed;
111
112 struct dylib_command {
113 uint32_t cmd;
114 uint32_t cmdsize;
115 struct dylib dylib;
116 } _packed;
117
118 struct uuid_command {
119 uint32_t cmd;
120 uint32_t cmdsize;
121 uint8_t uuid[16];
122 } _packed;
123
124 struct segment_command {
125 uint32_t cmd;
126 uint32_t cmdsize;
127 char segname[16];
128 uint32_t vmaddr;
129 uint32_t vmsize;
130 uint32_t fileoff;
131 uint32_t filesize;
132 uint32_t maxprot;
133 uint32_t initprot;
134 uint32_t nsects;
135 uint32_t flags;
136 };
137
138 struct section {
139 char sectname[16];
140 char segname[16];
141 uint32_t addr;
142 uint32_t size;
143 uint32_t offset;
144 uint32_t align;
145 uint32_t reloff;
146 uint32_t nreloc;
147 uint32_t flags;
148 uint32_t reserved1;
149 uint32_t reserved2;
150 };
151
152 struct linkedit_data_command {
153 uint32_t cmd;
154 uint32_t cmdsize;
155 uint32_t dataoff;
156 uint32_t datasize;
157 } _packed;
158
159 uint16_t Swap_(uint16_t value) {
160 return
161 ((value >> 8) & 0x00ff) |
162 ((value << 8) & 0xff00);
163 }
164
165 uint32_t Swap_(uint32_t value) {
166 value = ((value >> 8) & 0x00ff00ff) |
167 ((value << 8) & 0xff00ff00);
168 value = ((value >> 16) & 0x0000ffff) |
169 ((value << 16) & 0xffff0000);
170 return value;
171 }
172
173 int16_t Swap_(int16_t value) {
174 return Swap_(static_cast<uint16_t>(value));
175 }
176
177 int32_t Swap_(int32_t value) {
178 return Swap_(static_cast<uint32_t>(value));
179 }
180
181 uint16_t Swap(uint16_t value) {
182 return true ? Swap_(value) : value;
183 }
184
185 uint32_t Swap(uint32_t value) {
186 return true ? Swap_(value) : value;
187 }
188
189 int16_t Swap(int16_t value) {
190 return Swap(static_cast<uint16_t>(value));
191 }
192
193 int32_t Swap(int32_t value) {
194 return Swap(static_cast<uint32_t>(value));
195 }
196
197 template <typename Target_>
198 class Pointer;
199
200 class Framework {
201 private:
202 void *base_;
203 size_t size_;
204
205 struct mach_header *mach_header_;
206 struct load_command *load_command_;
207
208 bool swapped_;
209 bool bits64_;
210
211 public:
212 uint16_t Swap(uint16_t value) const {
213 return swapped_ ? Swap_(value) : value;
214 }
215
216 uint32_t Swap(uint32_t value) const {
217 return swapped_ ? Swap_(value) : value;
218 }
219
220 int16_t Swap(int16_t value) const {
221 return Swap(static_cast<uint16_t>(value));
222 }
223
224 int32_t Swap(int32_t value) const {
225 return Swap(static_cast<uint32_t>(value));
226 }
227
228 Framework(const char *framework_path) :
229 swapped_(false)
230 {
231 base_ = map(framework_path, 0, _not(size_t), &size_, false);
232 fat_header *fat_header = reinterpret_cast<struct fat_header *>(base_);
233
234 if (Swap(fat_header->magic) == FAT_CIGAM) {
235 swapped_ = !swapped_;
236 goto fat;
237 } else if (Swap(fat_header->magic) != FAT_MAGIC)
238 mach_header_ = (mach_header *) base_;
239 else fat: {
240 size_t fat_narch = Swap(fat_header->nfat_arch);
241 fat_arch *fat_arch = reinterpret_cast<struct fat_arch *>(fat_header + 1);
242 size_t arch;
243 for (arch = 0; arch != fat_narch; ++arch) {
244 uint32_t arch_offset = Swap(fat_arch->offset);
245 mach_header_ = (mach_header *) ((uint8_t *) base_ + arch_offset);
246 goto found;
247 ++fat_arch;
248 }
249
250 _assert(false);
251 }
252
253 found:
254 switch (Swap(mach_header_->magic)) {
255 case MH_CIGAM:
256 swapped_ = !swapped_;
257 case MH_MAGIC:
258 bits64_ = false;
259 break;
260
261 case MH_CIGAM_64:
262 swapped_ = !swapped_;
263 case MH_MAGIC_64:
264 bits64_ = true;
265 break;
266
267 default:
268 _assert(false);
269 }
270
271 void *post = mach_header_ + 1;
272 if (bits64_)
273 post = (uint32_t *) post + 1;
274 load_command_ = (struct load_command *) post;
275
276 _assert(
277 Swap(mach_header_->filetype) == MH_EXECUTE ||
278 Swap(mach_header_->filetype) == MH_DYLIB ||
279 Swap(mach_header_->filetype) == MH_BUNDLE
280 );
281 }
282
283 struct mach_header *operator ->() const {
284 return mach_header_;
285 }
286
287 void *GetBase() {
288 return base_;
289 }
290
291 size_t GetSize() const {
292 return size_;
293 }
294
295 uint32_t GetCPUType() const {
296 return Swap(mach_header_->cputype);
297 }
298
299 uint16_t GetCPUSubtype() const {
300 return Swap(mach_header_->cpusubtype) & 0xff;
301 }
302
303 std::vector<struct load_command *> GetLoadCommands() {
304 std::vector<struct load_command *> load_commands;
305
306 struct load_command *load_command = load_command_;
307 for (uint32_t cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) {
308 load_commands.push_back(load_command);
309 load_command = (struct load_command *) ((uint8_t *) load_command + Swap(load_command->cmdsize));
310 }
311
312 return load_commands;
313 }
314
315 std::vector<segment_command *> GetSegments(const char *segment_name) {
316 std::vector<struct segment_command *> segment_commands;
317
318 _foreach (load_command, GetLoadCommands())
319 if (Swap((*load_command)->cmd) == LC_SEGMENT) {
320 segment_command *segment_command = reinterpret_cast<struct segment_command *>(*load_command);
321 if (strncmp(segment_command->segname, segment_name, 16) == 0)
322 segment_commands.push_back(segment_command);
323 }
324
325 return segment_commands;
326 }
327
328 std::vector<section *> GetSections(const char *segment_name, const char *section_name) {
329 std::vector<section *> sections;
330
331 _foreach (segment, GetSegments(segment_name)) {
332 section *section = (struct section *) (*segment + 1);
333
334 uint32_t sect;
335 for (sect = 0; sect != Swap((*segment)->nsects); ++sect) {
336 if (strncmp(section->sectname, section_name, 16) == 0)
337 sections.push_back(section);
338 ++section;
339 }
340 }
341
342 return sections;
343 }
344
345 template <typename Target_>
346 Pointer<Target_> GetPointer(uint32_t address, const char *segment_name = NULL) {
347 load_command *load_command = (struct load_command *) (mach_header_ + 1);
348 uint32_t cmd;
349
350 for (cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) {
351 if (Swap(load_command->cmd) == LC_SEGMENT) {
352 segment_command *segment_command = (struct segment_command *) load_command;
353 if (segment_name != NULL && strncmp(segment_command->segname, segment_name, 16) != 0)
354 goto next_command;
355
356 section *sections = (struct section *) (segment_command + 1);
357
358 uint32_t sect;
359 for (sect = 0; sect != Swap(segment_command->nsects); ++sect) {
360 section *section = &sections[sect];
361 //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
362 if (address >= Swap(section->addr) && address < Swap(section->addr) + Swap(section->size)) {
363 //printf("0x%.8x %s\n", address, segment_command->segname);
364 return Pointer<Target_>(this, reinterpret_cast<Target_ *>(address - Swap(section->addr) + Swap(section->offset) + (char *) mach_header_));
365 }
366 }
367 }
368
369 next_command:
370 load_command = (struct load_command *) ((char *) load_command + Swap(load_command->cmdsize));
371 }
372
373 return Pointer<Target_>(this);
374 }
375
376 template <typename Target_>
377 Pointer<Target_> GetOffset(uint32_t offset) {
378 return Pointer<Target_>(this, reinterpret_cast<Target_ *>(offset + (uint8_t *) mach_header_));
379 }
380 };
381
382 template <typename Target_>
383 class Pointer {
384 private:
385 const Framework *framework_;
386 const Target_ *pointer_;
387
388 public:
389 Pointer(const Framework *framework = NULL, const Target_ *pointer = NULL) :
390 framework_(framework),
391 pointer_(pointer)
392 {
393 }
394
395 operator const Target_ *() const {
396 return pointer_;
397 }
398
399 const Target_ *operator ->() const {
400 return pointer_;
401 }
402
403 Pointer<Target_> &operator ++() {
404 ++pointer_;
405 return *this;
406 }
407
408 template <typename Value_>
409 Value_ Swap(Value_ value) {
410 return framework_->Swap(value);
411 }
412 };
413
414 #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
415 #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
416 #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171)
417
418 #define CSSLOT_CODEDIRECTORY uint32_t(0)
419 #define CSSLOT_REQUIREMENTS uint32_t(2)
420 #define CSSLOT_ENTITLEMENTS uint32_t(5)
421
422 struct BlobIndex {
423 uint32_t type;
424 uint32_t offset;
425 } _packed;
426
427 struct Blob {
428 uint32_t magic;
429 uint32_t length;
430 } _packed;
431
432 struct SuperBlob {
433 struct Blob blob;
434 uint32_t count;
435 struct BlobIndex index[];
436 } _packed;
437
438 struct CodeDirectory {
439 struct Blob blob;
440 uint32_t version;
441 uint32_t flags;
442 uint32_t hashOffset;
443 uint32_t identOffset;
444 uint32_t nSpecialSlots;
445 uint32_t nCodeSlots;
446 uint32_t codeLimit;
447 uint8_t hashSize;
448 uint8_t hashType;
449 uint8_t spare1;
450 uint8_t pageSize;
451 uint32_t spare2;
452 } _packed;
453
454 extern "C" uint32_t hash(uint8_t *k, uint32_t length, uint32_t initval);
455
456 #define CODESIGN_ALLOCATE "arm-apple-darwin9-codesign_allocate"
457
458 void sha1(uint8_t *hash, uint8_t *data, size_t size) {
459 SHA1Context context;
460 SHA1Reset(&context);
461 SHA1Input(&context, data, size);
462 SHA1Result(&context, hash);
463 }
464
465 int main(int argc, const char *argv[]) {
466 bool flag_R(false);
467 bool flag_t(false);
468 bool flag_p(false);
469 bool flag_u(false);
470 bool flag_e(false);
471
472 bool flag_T(false);
473
474 bool flag_S(false);
475 bool flag_s(false);
476
477 bool timeh(false);
478 uint32_t timev(0);
479
480 const void *xmld(NULL);
481 size_t xmls(0);
482
483 uintptr_t noffset(_not(uintptr_t));
484 uintptr_t woffset(_not(uintptr_t));
485
486 std::vector<std::string> files;
487
488 if (argc == 1) {
489 fprintf(stderr, "usage: %s -S[entitlements.xml] <binary>\n", argv[0]);
490 fprintf(stderr, " %s -e MobileSafari\n", argv[0]);
491 fprintf(stderr, " %s -S cat\n", argv[0]);
492 fprintf(stderr, " %s -Stfp.xml gdb\n", argv[0]);
493 exit(0);
494 }
495
496 for (int argi(1); argi != argc; ++argi)
497 if (argv[argi][0] != '-')
498 files.push_back(argv[argi]);
499 else switch (argv[argi][1]) {
500 case 'R': flag_R = true; break;
501 case 't': flag_t = true; break;
502 case 'u': flag_u = true; break;
503 case 'p': flag_p = true; break;
504 case 'e': flag_e = true; break;
505
506 case 's':
507 _assert(!flag_S);
508 flag_s = true;
509 break;
510
511 case 'S':
512 _assert(!flag_s);
513 flag_S = true;
514 if (argv[argi][2] != '\0') {
515 const char *xml = argv[argi] + 2;
516 xmld = map(xml, 0, _not(size_t), &xmls, true);
517 }
518 break;
519
520 case 'T': {
521 flag_T = true;
522 if (argv[argi][2] == '-')
523 timeh = true;
524 else {
525 char *arge;
526 timev = strtoul(argv[argi] + 2, &arge, 0);
527 _assert(arge == argv[argi] + strlen(argv[argi]));
528 }
529 } break;
530
531 case 'n': {
532 char *arge;
533 noffset = strtoul(argv[argi] + 2, &arge, 0);
534 _assert(arge == argv[argi] + strlen(argv[argi]));
535 } break;
536
537 case 'w': {
538 char *arge;
539 woffset = strtoul(argv[argi] + 2, &arge, 0);
540 _assert(arge == argv[argi] + strlen(argv[argi]));
541 } break;
542
543 default:
544 goto usage;
545 break;
546 }
547
548 if (files.empty()) usage: {
549 exit(0);
550 }
551
552 size_t filei(0), filee(0);
553 _foreach (file, files) try {
554 const char *path(file->c_str());
555 const char *base = strrchr(path, '/');
556 char *temp(NULL), *dir;
557
558 if (base != NULL)
559 dir = strndup_(path, base++ - path + 1);
560 else {
561 dir = strdup("");
562 base = path;
563 }
564
565 if (flag_S) {
566 asprintf(&temp, "%s.%s.cs", dir, base);
567 const char *allocate = getenv("CODESIGN_ALLOCATE");
568 if (allocate == NULL)
569 allocate = "codesign_allocate";
570
571 size_t size = _not(size_t);
572 const char *arch; {
573 Framework framework(path);
574 framework->flags |= MH_DYLDLINK;
575
576 _foreach (load_command, framework.GetLoadCommands()) {
577 uint32_t cmd(framework.Swap((*load_command)->cmd));
578 if (cmd == LC_CODE_SIGNATURE) {
579 struct linkedit_data_command *signature = reinterpret_cast<struct linkedit_data_command *>(*load_command);
580 size = framework.Swap(signature->dataoff);
581 _assert(size < framework.GetSize());
582 break;
583 }
584 }
585
586 if (size == _not(size_t))
587 size = framework.GetSize();
588
589 switch (framework.GetCPUType()) {
590 case 7: switch (framework.GetCPUSubtype()) {
591 case 3: arch = "i386"; break;
592 default: arch = NULL; break;
593 } break;
594
595 case 12: switch (framework.GetCPUSubtype()) {
596 case 0: arch = "arm"; break;
597 case 6: arch = "armv6"; break;
598 case 9: arch = "armv7"; break;
599 default: arch = NULL; break;
600 } break;
601
602 case 18: switch (framework.GetCPUSubtype()) {
603 case 10: arch = "ppc7400"; break;
604 default: arch = NULL; break;
605 } break;
606
607 case 16777223: switch (framework.GetCPUSubtype()) {
608 case 3: arch = "x86_64"; break;
609 default: arch = NULL; break;
610 } break;
611
612 case 16777234: switch (framework.GetCPUSubtype()) {
613 case 0: arch = "ppc64"; break;
614 default: arch = NULL; break;
615 } break;
616
617 default: arch = NULL; break;
618 }
619 }
620
621 _assert(arch != NULL);
622
623 pid_t pid = fork();
624 _syscall(pid);
625 if (pid == 0) {
626 char *ssize;
627 asprintf(&ssize, "%u", (sizeof(struct SuperBlob) + 2 * sizeof(struct BlobIndex) + sizeof(struct CodeDirectory) + strlen(base) + 1 + ((xmld == NULL ? CSSLOT_REQUIREMENTS : CSSLOT_ENTITLEMENTS) + (size + 0x1000 - 1) / 0x1000) * 0x14 + 0xc + (xmld == NULL ? 0 : 0x10 + xmls) + 15) / 16 * 16);
628 //printf("%s -i %s -a %s %s -o %s\n", allocate, path, arch, ssize, temp);
629 execlp(allocate, allocate, "-i", path, "-a", arch, ssize, "-o", temp, NULL);
630 _assert(false);
631 }
632
633 int status;
634 _syscall(waitpid(pid, &status, 0));
635 _assert(WIFEXITED(status));
636 _assert(WEXITSTATUS(status) == 0);
637 }
638
639 Framework framework(temp == NULL ? path : temp);
640 struct linkedit_data_command *signature(NULL);
641
642 if (flag_p)
643 printf("path%zu='%s'\n", filei, file->c_str());
644
645 if (woffset != _not(uintptr_t)) {
646 Pointer<uint32_t> wvalue(framework.GetPointer<uint32_t>(woffset));
647 if (wvalue == NULL)
648 printf("(null) %p\n", reinterpret_cast<void *>(woffset));
649 else
650 printf("0x%.08x\n", *wvalue);
651 }
652
653 if (noffset != _not(uintptr_t))
654 printf("%s\n", &*framework.GetPointer<char>(noffset));
655
656 _foreach (load_command, framework.GetLoadCommands()) {
657 uint32_t cmd(framework.Swap((*load_command)->cmd));
658
659 if (flag_R && cmd == LC_REEXPORT_DYLIB)
660 (*load_command)->cmd = framework.Swap(LC_LOAD_DYLIB);
661 else if (cmd == LC_CODE_SIGNATURE)
662 signature = reinterpret_cast<struct linkedit_data_command *>(*load_command);
663 else if (cmd == LC_UUID) {
664 volatile struct uuid_command *uuid_command(reinterpret_cast<struct uuid_command *>(*load_command));
665
666 if (flag_u) {
667 printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei,
668 uuid_command->uuid[ 0], uuid_command->uuid[ 1], uuid_command->uuid[ 2], uuid_command->uuid[ 3],
669 uuid_command->uuid[ 4], uuid_command->uuid[ 5], uuid_command->uuid[ 6], uuid_command->uuid[ 7],
670 uuid_command->uuid[ 8], uuid_command->uuid[ 9], uuid_command->uuid[10], uuid_command->uuid[11],
671 uuid_command->uuid[12], uuid_command->uuid[13], uuid_command->uuid[14], uuid_command->uuid[15]
672 );
673 }
674 } else if (cmd == LC_ID_DYLIB) {
675 volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(*load_command));
676
677 if (flag_t)
678 printf("time%zu=0x%.8x\n", filei, framework.Swap(dylib_command->dylib.timestamp));
679
680 if (flag_T) {
681 uint32_t timed;
682
683 if (!timeh)
684 timed = timev;
685 else {
686 dylib_command->dylib.timestamp = 0;
687 timed = hash(reinterpret_cast<uint8_t *>(framework.GetBase()), framework.GetSize(), timev);
688 }
689
690 dylib_command->dylib.timestamp = framework.Swap(timed);
691 }
692 }
693 }
694
695 if (flag_e) {
696 _assert(signature != NULL);
697
698 uint32_t data = framework.Swap(signature->dataoff);
699 uint32_t size = framework.Swap(signature->datasize);
700
701 uint8_t *top = reinterpret_cast<uint8_t *>(framework.GetBase());
702 uint8_t *blob = top + data;
703 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
704
705 for (size_t index(0); index != Swap(super->count); ++index)
706 if (Swap(super->index[index].type) == CSSLOT_ENTITLEMENTS) {
707 uint32_t begin = Swap(super->index[index].offset);
708 struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin);
709 fwrite(entitlements + 1, 1, Swap(entitlements->length) - sizeof(struct Blob), stdout);
710 }
711 }
712
713 if (flag_s) {
714 _assert(signature != NULL);
715
716 uint32_t data = framework.Swap(signature->dataoff);
717 uint32_t size = framework.Swap(signature->datasize);
718
719 uint8_t *top = reinterpret_cast<uint8_t *>(framework.GetBase());
720 uint8_t *blob = top + data;
721 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
722
723 for (size_t index(0); index != Swap(super->count); ++index)
724 if (Swap(super->index[index].type) == CSSLOT_CODEDIRECTORY) {
725 uint32_t begin = Swap(super->index[index].offset);
726 struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin);
727
728 uint8_t (*hashes)[20] = reinterpret_cast<uint8_t (*)[20]>(blob + begin + Swap(directory->hashOffset));
729 uint32_t pages = Swap(directory->nCodeSlots);
730
731 if (pages != 1)
732 for (size_t i = 0; i != pages - 1; ++i)
733 sha1(hashes[i], top + 0x1000 * i, 0x1000);
734 if (pages != 0)
735 sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), ((data - 1) % 0x1000) + 1);
736 }
737 }
738
739 if (flag_S) {
740 _assert(signature != NULL);
741
742 uint32_t data = framework.Swap(signature->dataoff);
743 uint32_t size = framework.Swap(signature->datasize);
744
745 uint8_t *top = reinterpret_cast<uint8_t *>(framework.GetBase());
746 uint8_t *blob = top + data;
747 struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob);
748 super->blob.magic = Swap(CSMAGIC_EMBEDDED_SIGNATURE);
749
750 uint32_t count = xmld == NULL ? 2 : 3;
751 uint32_t offset = sizeof(struct SuperBlob) + count * sizeof(struct BlobIndex);
752
753 super->index[0].type = Swap(CSSLOT_CODEDIRECTORY);
754 super->index[0].offset = Swap(offset);
755
756 uint32_t begin = offset;
757 struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin);
758 offset += sizeof(struct CodeDirectory);
759
760 directory->blob.magic = Swap(CSMAGIC_CODEDIRECTORY);
761 directory->version = Swap(uint32_t(0x00020001));
762 directory->flags = Swap(uint32_t(0));
763 directory->codeLimit = Swap(data);
764 directory->hashSize = 0x14;
765 directory->hashType = 0x01;
766 directory->spare1 = 0x00;
767 directory->pageSize = 0x0c;
768 directory->spare2 = Swap(uint32_t(0));
769
770 directory->identOffset = Swap(offset - begin);
771 strcpy(reinterpret_cast<char *>(blob + offset), base);
772 offset += strlen(base) + 1;
773
774 uint32_t special = xmld == NULL ? CSSLOT_REQUIREMENTS : CSSLOT_ENTITLEMENTS;
775 directory->nSpecialSlots = Swap(special);
776
777 uint8_t (*hashes)[20] = reinterpret_cast<uint8_t (*)[20]>(blob + offset);
778 memset(hashes, 0, sizeof(*hashes) * special);
779
780 offset += sizeof(*hashes) * special;
781 hashes += special;
782
783 uint32_t pages = (data + 0x1000 - 1) / 0x1000;
784 directory->nCodeSlots = Swap(pages);
785
786 if (pages != 1)
787 for (size_t i = 0; i != pages - 1; ++i)
788 sha1(hashes[i], top + 0x1000 * i, 0x1000);
789 if (pages != 0)
790 sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), ((data - 1) % 0x1000) + 1);
791
792 directory->hashOffset = Swap(offset - begin);
793 offset += sizeof(*hashes) * pages;
794 directory->blob.length = Swap(offset - begin);
795
796 super->index[1].type = Swap(CSSLOT_REQUIREMENTS);
797 super->index[1].offset = Swap(offset);
798
799 memcpy(blob + offset, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc);
800 offset += 0xc;
801
802 if (xmld != NULL) {
803 super->index[2].type = Swap(CSSLOT_ENTITLEMENTS);
804 super->index[2].offset = Swap(offset);
805
806 uint32_t begin = offset;
807 struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin);
808 offset += sizeof(struct Blob);
809
810 memcpy(blob + offset, xmld, xmls);
811 offset += xmls;
812
813 entitlements->magic = Swap(CSMAGIC_ENTITLEMENTS);
814 entitlements->length = Swap(offset - begin);
815 }
816
817 for (size_t index(0); index != count; ++index) {
818 uint32_t type = Swap(super->index[index].type);
819 if (type != 0 && type <= special) {
820 uint32_t offset = Swap(super->index[index].offset);
821 struct Blob *local = (struct Blob *) (blob + offset);
822 sha1((uint8_t *) (hashes - type), (uint8_t *) local, Swap(local->length));
823 }
824 }
825
826 super->count = Swap(count);
827 super->blob.length = Swap(offset);
828
829 if (offset > size) {
830 fprintf(stderr, "offset (%u) > size (%u)\n", offset, size);
831 _assert(false);
832 } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size);
833
834 memset(blob + offset, 0, size - offset);
835 }
836
837 if (flag_S) {
838 uint8_t *top = reinterpret_cast<uint8_t *>(framework.GetBase());
839 size_t size = framework.GetSize();
840
841 char *copy;
842 asprintf(&copy, "%s.%s.cp", dir, base);
843 FILE *file = fopen(copy, "w+");
844 size_t writ = fwrite(top, 1, size, file);
845 _assert(writ == size);
846 fclose(file);
847
848 _syscall(unlink(temp));
849 free(temp);
850 temp = copy;
851 }
852
853 if (temp) {
854 struct stat info;
855 _syscall(stat(path, &info));
856 _syscall(chown(temp, info.st_uid, info.st_gid));
857 _syscall(chmod(temp, info.st_mode));
858 _syscall(unlink(path));
859 _syscall(rename(temp, path));
860 free(temp);
861 }
862
863 free(dir);
864 ++filei;
865 } catch (const char *) {
866 ++filee;
867 ++filei;
868 }
869
870 return filee;
871 }