Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CFURL and CFAttributedString patches for TextEdit #6

Merged
merged 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CFAttributedString.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static __CFRunArrayItem * _CFRunArrayItemInit(CFRange range, CFDictionaryRef dic
{
__CFRunArrayItem *obj = (__CFRunArrayItem *)malloc(sizeof(__CFRunArrayItem));
obj->_range = range;
obj->_dictionary = CFRetain(dict);
obj->_dictionary = CFDictionaryCreateCopy(kCFAllocatorDefault, dict);
return obj;
}

Expand Down
48 changes: 48 additions & 0 deletions NSURL.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import <objc/runtime.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <dlfcn.h>

#define STACK_BUFFER_SIZE 100 // pretty safe bet this will be quite unlikely to use more than this since there are only 94 properties

Expand Down Expand Up @@ -156,6 +157,17 @@ static void posixError(CFErrorRef *error) {
CFRelease(err);
}

static void dlError(CFErrorRef *error) {
int saved_errno = errno;
const CFStringRef key = kCFErrorUnderlyingErrorKey;
CFStringRef underlyingError = CFStringCreateWithCString(kCFAllocatorDefault, dlerror(), kCFStringEncodingUTF8);
CFTypeRef value = underlyingError;

if(error != NULL)
*error = CFErrorCreateWithUserInfoKeysAndValues(kCFAllocatorDefault, kCFErrorDomainPOSIX, saved_errno, (const void *const *) &key, (const void *const *) &value, 1);
CFRelease(underlyingError);
};
CKegel marked this conversation as resolved.
Show resolved Hide resolved

static Boolean CFURLStat(CFURLRef url, struct stat *info) {
UInt8 path[PATH_MAX] = { 0 };

Expand Down Expand Up @@ -351,7 +363,43 @@ static CFTypeRef CFURLCreatePropertyForKey(CFURLRef url, CFStringRef key, CFErro
else if (CFEqual(key, kCFURLTypeIdentifierKey))
{
// Key for the resource’s uniform type identifier (UTI), returned as a CFString object.
static dispatch_once_t pred;
static void* LaunchServicesHandle;
static CFErrorRef LaunchServicesHandleError;

dispatch_once(&pred, ^{
LaunchServicesHandle = dlopen("/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/LaunchServices", RTLD_LAZY | RTLD_LOCAL);
if(LaunchServicesHandle == NULL)
dlError(&LaunchServicesHandleError);
});

if(LaunchServicesHandle == NULL){
if (error != NULL)
*error = CFRetain(LaunchServicesHandleError);
return NULL;
}

static CFStringRef (*UTTypeCreatePreferredIdentifierForTag)(CFStringRef, CFStringRef, _Nullable CFStringRef);
static CFStringRef *tagType;
static dispatch_once_t dlsym_pred;

dispatch_once(&dlsym_pred, ^{
UTTypeCreatePreferredIdentifierForTag = dlsym(LaunchServicesHandle, "UTTypeCreatePreferredIdentifierForTag");
tagType = dlsym(LaunchServicesHandle, "kUTTagClassFilenameExtension");
if(UTTypeCreatePreferredIdentifierForTag == NULL || tagType == NULL)
dlError(&LaunchServicesHandleError);
});

if(UTTypeCreatePreferredIdentifierForTag == NULL || tagType == NULL){
if (error != NULL)
*error = CFRetain(LaunchServicesHandleError);
return NULL;
}

CFStringRef extension = CFURLCopyPathExtension(url);
value = UTTypeCreatePreferredIdentifierForTag(*tagType, extension, NULL);

CFRelease(extension);
}
else if (CFEqual(key, kCFURLLocalizedTypeDescriptionKey))
{
Expand Down