+ const char *path(file.c_str());
+ const char *base = strrchr(path, '/');
+ char *temp(NULL), *dir;
+
+ if (base != NULL)
+ dir = strndup_(path, base++ - path + 1);
+ else {
+ dir = strdup("");
+ base = path;
+ }
+
+ const char *name(flag_I ?: base);
+
+ if (flag_r) {
+ uint32_t clip(0); {
+ FatHeader fat_header(Map(path));
+ _foreach (mach_header, fat_header.GetMachHeaders()) {
+ if (flag_A) {
+ if (mach_header.GetCPUType() != flag_CPUType)
+ continue;
+ if (mach_header.GetCPUSubtype() != flag_CPUSubtype)
+ continue;
+ }
+
+ mach_header->flags = mach_header.Swap(mach_header.Swap(mach_header->flags) | MH_DYLDLINK);
+
+ uint32_t size(_not(uint32_t)); {
+ _foreach (load_command, mach_header.GetLoadCommands()) {
+ switch (mach_header.Swap(load_command->cmd)) {
+ case LC_CODE_SIGNATURE: {
+ struct linkedit_data_command *signature = reinterpret_cast<struct linkedit_data_command *>(load_command);
+ memset(reinterpret_cast<uint8_t *>(mach_header.GetBase()) + mach_header.Swap(signature->dataoff), 0, mach_header.Swap(signature->datasize));
+ memset(signature, 0, sizeof(struct linkedit_data_command));
+
+ mach_header->ncmds = mach_header.Swap(mach_header.Swap(mach_header->ncmds) - 1);
+ mach_header->sizeofcmds = mach_header.Swap(uint32_t(mach_header.Swap(mach_header->sizeofcmds) - sizeof(struct linkedit_data_command)));
+ } break;
+
+ case LC_SYMTAB: {
+ struct symtab_command *symtab = reinterpret_cast<struct symtab_command *>(load_command);
+ size = mach_header.Swap(symtab->stroff) + mach_header.Swap(symtab->strsize);
+ } break;
+ }
+ }
+ }
+
+ _assert(size != _not(uint32_t));
+
+ _foreach (segment, mach_header.GetSegments("__LINKEDIT")) {
+ segment->filesize -= mach_header.GetSize() - size;
+
+ if (fat_arch *fat_arch = mach_header.GetFatArch()) {
+ fat_arch->size = fat_header.Swap(size);
+ clip = std::max(clip, fat_header.Swap(fat_arch->offset) + size);
+ } else
+ clip = std::max(clip, size);
+ }
+
+ _foreach (segment, mach_header.GetSegments64("__LINKEDIT")) {
+ segment->filesize -= mach_header.GetSize() - size;
+
+ if (fat_arch *fat_arch = mach_header.GetFatArch()) {
+ fat_arch->size = fat_header.Swap(size);
+ clip = std::max(clip, fat_header.Swap(fat_arch->offset) + size);
+ } else
+ clip = std::max(clip, size);
+ }
+ }
+ }
+
+ if (clip != 0)
+ _syscall(truncate(path, clip));
+ }
+
+ if (flag_S) {
+ FatHeader source(Map(path));
+
+ size_t offset(0);
+
+ if (source.IsFat())
+ offset += sizeof(fat_header) + sizeof(fat_arch) * source.Swap(source->nfat_arch);
+
+ std::vector<CodesignAllocation> allocations; {
+ _foreach (mach_header, source.GetMachHeaders()) {
+ if (flag_A) {
+ if (mach_header.GetCPUType() != flag_CPUType)
+ continue;
+ if (mach_header.GetCPUSubtype() != flag_CPUSubtype)
+ continue;
+ }
+
+ mach_header->flags = mach_header.Swap(mach_header.Swap(mach_header->flags) | MH_DYLDLINK);
+
+ size_t size(_not(size_t)); {
+ _foreach (load_command, mach_header.GetLoadCommands()) {
+ uint32_t cmd(mach_header.Swap(load_command->cmd));
+ if (cmd == LC_CODE_SIGNATURE) {
+ struct linkedit_data_command *signature = reinterpret_cast<struct linkedit_data_command *>(load_command);
+ size = mach_header.Swap(signature->dataoff);
+ _assert(size < mach_header.GetSize());
+ break;
+ }
+ }
+
+ if (size == _not(size_t))
+ size = mach_header.GetSize();
+ }
+
+ size_t alloc(0);
+ alloc += sizeof(struct SuperBlob);
+ uint32_t special(0);
+
+ special = std::max(special, CSSLOT_CODEDIRECTORY);
+ alloc += sizeof(struct BlobIndex);
+ alloc += sizeof(struct CodeDirectory);
+ alloc += strlen(name) + 1;
+
+ special = std::max(special, CSSLOT_REQUIREMENTS);
+ alloc += sizeof(struct BlobIndex);
+ alloc += 0xc;
+
+ if (xmld != NULL) {
+ special = std::max(special, CSSLOT_ENTITLEMENTS);
+ alloc += sizeof(struct BlobIndex);
+ alloc += sizeof(struct Blob);
+ alloc += xmls;
+ }
+
+ size_t normal((size + 0x1000 - 1) / 0x1000);
+ alloc = Align(alloc + (special + normal) * 0x14, 16);
+
+ auto *fat_arch(mach_header.GetFatArch());
+ uint32_t align(fat_arch == NULL ? 0 : source.Swap(fat_arch->align));
+ offset = Align(offset, 1 << align);
+
+ allocations.push_back(CodesignAllocation(mach_header, offset, size, alloc, align));
+ offset += size + alloc;
+ offset = Align(offset, 16);
+ }
+ }
+
+ asprintf(&temp, "%s.%s.cs", dir, base);
+ fclose(fopen(temp, "w+"));
+ _syscall(truncate(temp, offset));
+
+ void *file(map(temp, 0, offset, NULL, false));
+ memset(file, 0, offset);
+
+ fat_arch *fat_arch;
+ if (!source.IsFat())
+ fat_arch = NULL;
+ else {
+ auto *fat_header(reinterpret_cast<struct fat_header *>(file));
+ fat_header->magic = Swap(FAT_MAGIC);
+ fat_header->nfat_arch = Swap(source.Swap(source->nfat_arch));
+ fat_arch = reinterpret_cast<struct fat_arch *>(fat_header + 1);
+ }
+
+ _foreach (allocation, allocations) {
+ auto &source(allocation.mach_header_);
+
+ uint32_t align(allocation.size_);
+ align = Align(align, 0x10);
+
+ if (fat_arch != NULL) {
+ fat_arch->cputype = Swap(source->cputype);
+ fat_arch->cpusubtype = Swap(source->cpusubtype);
+ fat_arch->offset = Swap(allocation.offset_);
+ fat_arch->size = Swap(align + allocation.alloc_);
+ fat_arch->align = Swap(allocation.align_);
+ ++fat_arch;
+ }
+
+ void *target(reinterpret_cast<uint8_t *>(file) + allocation.offset_);
+ memcpy(target, source, allocation.size_);
+ MachHeader mach_header(target, align + allocation.alloc_);
+
+ struct linkedit_data_command *signature(NULL);
+ _foreach (load_command, mach_header.GetLoadCommands()) {
+ uint32_t cmd(mach_header.Swap(load_command->cmd));
+ if (cmd != LC_CODE_SIGNATURE)
+ continue;
+ signature = reinterpret_cast<struct linkedit_data_command *>(load_command);
+ break;
+ }
+
+ if (signature == NULL) {
+ mach_header->ncmds = mach_header.Swap(mach_header.Swap(mach_header->ncmds) + 1);
+ signature = reinterpret_cast<struct linkedit_data_command *>(reinterpret_cast<uint8_t *>(mach_header.GetLoadCommand()) + mach_header.Swap(mach_header->sizeofcmds));
+ mach_header->sizeofcmds = mach_header.Swap(mach_header.Swap(mach_header->sizeofcmds) + uint32_t(sizeof(*signature)));
+ signature->cmd = mach_header.Swap(LC_CODE_SIGNATURE);
+ signature->cmdsize = mach_header.Swap(uint32_t(sizeof(*signature)));
+ }
+
+ signature->dataoff = mach_header.Swap(align);
+ signature->datasize = mach_header.Swap(allocation.alloc_);
+
+ _foreach (segment, mach_header.GetSegments("__LINKEDIT")) {
+ size_t size(mach_header.Swap(align + allocation.alloc_ - mach_header.Swap(segment->fileoff)));
+ segment->filesize = size;
+ segment->vmsize = Align(size, 0x1000);
+ }
+
+ _foreach (segment, mach_header.GetSegments64("__LINKEDIT")) {
+ size_t size(mach_header.Swap(align + allocation.alloc_ - mach_header.Swap(segment->fileoff)));
+ segment->filesize = size;
+ segment->vmsize = Align(size, 0x1000);
+ }
+ }
+ }