Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | /* * Copyright (c) 2022 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <string.h> #include <assert.h> #if __has_feature(ptrauth_calls) #include <ptrauth.h> #endif #include "Utilities.h" namespace dyld4 { // based on ANSI-C strstr() const char* Utils::strrstr(const char* str, const char* sub) { const size_t sublen = strlen(sub); for (const char* p = &str[strlen(str)]; p != str; --p) { if ( ::strncmp(p, sub, sublen) == 0 ) return p; } return nullptr; } size_t Utils::concatenatePaths(char *path, const char *suffix, size_t pathsize) { if ( (path[strlen(path) - 1] == '/') && (suffix[0] == '/') ) return strlcat(path, &suffix[1], pathsize); // avoid double slash when combining path else return strlcat(path, suffix, pathsize); } }; /* namespace dyld4 */ void escapeCStringLiteral(const char* s, char* b, size_t bufferLength, char** end) { char* e = b + bufferLength - 1; // reserve one character for null terminator while (b < e) { char c = *s++; if ( c == '\n' ) { *b++ = '\\'; *b++ = 'n'; } else if ( c == '\r' ) { *b++ = '\\'; *b++ = 'r'; } else if ( c == '\t' ) { *b++ = '\\'; *b++ = 't'; } else if ( c == '\"' ) { *b++ = '\\'; *b++ = '\"'; } else if ( c == '\0' ) { break; } else { *b++ = c; } } *b = '\0'; if ( end ) *end = b; } #if __has_feature(ptrauth_calls) uint64_t signPointer(uint64_t unsignedAddr, void* loc, bool addrDiv, uint16_t diversity, ptrauth_key key) { // don't sign NULL if ( unsignedAddr == 0 ) return 0; uint64_t extendedDiscriminator = diversity; if ( addrDiv ) extendedDiscriminator = __builtin_ptrauth_blend_discriminator(loc, extendedDiscriminator); switch ( key ) { case ptrauth_key_asia: return (uint64_t)__builtin_ptrauth_sign_unauthenticated((void*)unsignedAddr, 0, extendedDiscriminator); case ptrauth_key_asib: return (uint64_t)__builtin_ptrauth_sign_unauthenticated((void*)unsignedAddr, 1, extendedDiscriminator); case ptrauth_key_asda: return (uint64_t)__builtin_ptrauth_sign_unauthenticated((void*)unsignedAddr, 2, extendedDiscriminator); case ptrauth_key_asdb: return (uint64_t)__builtin_ptrauth_sign_unauthenticated((void*)unsignedAddr, 3, extendedDiscriminator); default: assert(0 && "invalid signing key"); } } #endif |