Mac .pref 文件十六进制路径

发布于 2024-11-15 01:53:55 字数 504 浏览 4 评论 0原文

我试图在几个 .pref 文件中输入文件路径,但该字段需要某种十六进制序列而不是字符串。如何将我的路径转换为这种十六进制格式?

Mac OS 10.6.7

以下是文件位置的示例:

<00000000 009e0003 00010000 c94bbb14 0000482b 00000000 000d3ad2 000dfc12 0000c950 e4db0000 00000920 fffe0000 00000000 0000ffff ffff0001 0008000d 3ad2000c 9ce1000e 000c0005 0069006e 0062006f 0078000f 001a000c 004d0061 00630069 006e0074 006f0073 00680020 00480044 00120015 55736572 732f7263 68617265 7474652f 696e626f 78000013 00012f00 00150002 0010ffff 0000> 0010ffff 0000>

I'm trying to enter a file path into a few .pref files, but instead of a String, the field wants some sort of Hexadecimal sequence. How can I convert my path into this Hex format?

Mac OS 10.6.7

Here is an example of a file location:

<00000000 009e0003 00010000 c94bbb14 0000482b 00000000 000d3ad2 000dfc12 0000c950 e4db0000 00000920 fffe0000 00000000 0000ffff ffff0001 0008000d 3ad2000c 9ce1000e 000c0005 0069006e 0062006f 0078000f 001a000c 004d0061 00630069 006e0074 006f0073 00680020 00480044 00120015 55736572 732f7263 68617265 7474652f 696e626f 78000013 00012f00 00150002 0010ffff 0000>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2024-11-22 01:53:55

它是别名 'alis' 数据,在过去 20 年或更长时间里,它一直是 Mac OS 中以持久方式引用文件的标准方式。 ;-) AliasHandle 等。所有这些都可以在 Aliases.h 中找到,它位于 CoreServices 伞式框架的 CarbonCore.framework 中。

请注意,属性列表编辑器(或 Xcode)向您显示 NSData 的十六进制表示形式。数据告诉我们,您没有更改硬盘驱动器的名称(它似乎仍然是“Macintosh HD”),并且别名的路径是 /Users/rcharette/inbox

“newfangled”名称是书签数据,在 10.6 中引入。

我相信,有几个围绕别名的高级包装器,即 Nathan Day 的 NDalias

否则,以下是我的,NSString 上的一个类别,以 NSURL 中可用的新书签 API 为模型(需要 10.6)。 (此代码应适用于 10.3+):

MDBookmarks.h:

#import <Foundation/Foundation.h>

//  Constants
//  MDBookmarkResolutionWithoutUI
// Option for specifying that no UI feedback accompany resolution of the bookmark data.
enum {
    MDBookmarkResolutionDefaultOptions      = 1,
    MDBookmarkResolutionWithoutUI = ( 1UL << 8 )
};
typedef NSUInteger MDBookmarkResolutionOptions;


@interface NSString (MDBookmarks)

- (NSData *)bookmarkDataWithError:(NSError **)error;

+ (id)stringByResolvingBookmarkData:(NSData *)bookmarkData 
                            options:(MDBookmarkResolutionOptions)options
       bookmarkDataIsStale:(BOOL *)isStale error:(NSError **)error;

@end

MDBookmarks.m:

#import "MDBookmarks.h"
#import <CoreServices/CoreServices.h>
#import <sys/syslimits.h>

@implementation NSString (MDBookmarks)

- (NSData *)bookmarkDataWithError:(NSError **)outError {
    if (outError) *outError = nil;
    NSString *path = [[self stringByResolvingSymlinksInPath]
                               stringByStandardizingPath];
    AliasHandle alias = NULL;
    FSRef itemRef;
    OSStatus status = FSPathMakeRef((const UInt8 *)[path UTF8String], &itemRef, NULL);
    if (status != noErr) {
        if (outError) {
            if (status == fnfErr) *outError = [NSError errorWithDomain:NSCocoaErrorDomain 
              code:NSFileNoSuchFileError userInfo:nil];
            else *outError = [NSError errorWithDomain:NSOSStatusErrorDomain
              code:status userInfo:nil];
        }
        return nil;
    }
    status = FSNewAlias(NULL, &itemRef, &alias);
    if (status != noErr ) {
        if (outError)
        *outError = [NSError errorWithDomain:NSOSStatusErrorDomain
                code:status userInfo:nil];
        return nil;
    }
    HLock((Handle)alias);
    NSData *bookmarkData =
[[[NSData dataWithBytes:*alias length:GetHandleSize((Handle)alias)] retain] autorelease];
    HUnlock((Handle)alias);
    if (alias) DisposeHandle((Handle)alias);
    return bookmarkData;
}

+ (id)stringByResolvingBookmarkData:(NSData *)bookmarkData
        options:(MDBookmarkResolutionOptions)options
         bookmarkDataIsStale:(BOOL *)isStale error:(NSError **)outError {
    if (bookmarkData == nil) return nil;
    if (outError) *outError = nil;
    AliasHandle alias = NULL;
    FSRef resolvedRef;
    Boolean wasChanged = false;
    OSStatus status = PtrToHand([bookmarkData bytes], (Handle *)&alias,
                                              [bookmarkData length]);
    if (status) {
        if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain
                         code:status userInfo:nil];
        return nil;
    }
    status = FSResolveAliasWithMountFlags(NULL, alias, &resolvedRef, &wasChanged,
             (options & MDBookmarkResolutionWithoutUI ? kResolveAliasFileNoUI : 0));
    if (status) {
        if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain
                            code:status userInfo:nil];
        return nil;
    }
    UInt8 thePath[PATH_MAX + 1];
    status = FSRefMakePath(&resolvedRef, thePath, PATH_MAX);
    if (status != noErr) {
        if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain 
                        code:status userInfo:nil];
        return nil;
    }
    if (isStale) *isStale = wasChanged;
    return [NSString stringWithUTF8String:(const char *)thePath];
}
@end

否则,如果您需要 10.6,请在 NSURL 中查看较新的 API。

It's alias 'alis' data, which has been the standard way to refer to files in a persistent manner in Mac OS for the last 20 years or more. ;-) The AliasHandle et. al are found in Aliases.h, which is in the CarbonCore.framework of the CoreServices umbrella framework.

Note that it is Property List Editor (or Xcode) that's showing you a hexadecimal representation of NSData. The data tells us that you haven't changed the name of your hard drive (it appears to still be "Macintosh HD") and that the path of the alias was to /Users/rcharette/inbox

The "newfangled" name for this is bookmark data, which was introduced in 10.6.

There are a couple of high-level wrappers around aliases, Nathan Day's NDAlias, I believe.

Otherwise, the following is mine, a category on NSString modeled after the new bookmark APIs available in NSURL (which require 10.6). (This code should work on 10.3+):

MDBookmarks.h:

#import <Foundation/Foundation.h>

//  Constants
//  MDBookmarkResolutionWithoutUI
// Option for specifying that no UI feedback accompany resolution of the bookmark data.
enum {
    MDBookmarkResolutionDefaultOptions      = 1,
    MDBookmarkResolutionWithoutUI = ( 1UL << 8 )
};
typedef NSUInteger MDBookmarkResolutionOptions;


@interface NSString (MDBookmarks)

- (NSData *)bookmarkDataWithError:(NSError **)error;

+ (id)stringByResolvingBookmarkData:(NSData *)bookmarkData 
                            options:(MDBookmarkResolutionOptions)options
       bookmarkDataIsStale:(BOOL *)isStale error:(NSError **)error;

@end

MDBookmarks.m:

#import "MDBookmarks.h"
#import <CoreServices/CoreServices.h>
#import <sys/syslimits.h>

@implementation NSString (MDBookmarks)

- (NSData *)bookmarkDataWithError:(NSError **)outError {
    if (outError) *outError = nil;
    NSString *path = [[self stringByResolvingSymlinksInPath]
                               stringByStandardizingPath];
    AliasHandle alias = NULL;
    FSRef itemRef;
    OSStatus status = FSPathMakeRef((const UInt8 *)[path UTF8String], &itemRef, NULL);
    if (status != noErr) {
        if (outError) {
            if (status == fnfErr) *outError = [NSError errorWithDomain:NSCocoaErrorDomain 
              code:NSFileNoSuchFileError userInfo:nil];
            else *outError = [NSError errorWithDomain:NSOSStatusErrorDomain
              code:status userInfo:nil];
        }
        return nil;
    }
    status = FSNewAlias(NULL, &itemRef, &alias);
    if (status != noErr ) {
        if (outError)
        *outError = [NSError errorWithDomain:NSOSStatusErrorDomain
                code:status userInfo:nil];
        return nil;
    }
    HLock((Handle)alias);
    NSData *bookmarkData =
[[[NSData dataWithBytes:*alias length:GetHandleSize((Handle)alias)] retain] autorelease];
    HUnlock((Handle)alias);
    if (alias) DisposeHandle((Handle)alias);
    return bookmarkData;
}

+ (id)stringByResolvingBookmarkData:(NSData *)bookmarkData
        options:(MDBookmarkResolutionOptions)options
         bookmarkDataIsStale:(BOOL *)isStale error:(NSError **)outError {
    if (bookmarkData == nil) return nil;
    if (outError) *outError = nil;
    AliasHandle alias = NULL;
    FSRef resolvedRef;
    Boolean wasChanged = false;
    OSStatus status = PtrToHand([bookmarkData bytes], (Handle *)&alias,
                                              [bookmarkData length]);
    if (status) {
        if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain
                         code:status userInfo:nil];
        return nil;
    }
    status = FSResolveAliasWithMountFlags(NULL, alias, &resolvedRef, &wasChanged,
             (options & MDBookmarkResolutionWithoutUI ? kResolveAliasFileNoUI : 0));
    if (status) {
        if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain
                            code:status userInfo:nil];
        return nil;
    }
    UInt8 thePath[PATH_MAX + 1];
    status = FSRefMakePath(&resolvedRef, thePath, PATH_MAX);
    if (status != noErr) {
        if (outError) *outError = [NSError errorWithDomain:NSOSStatusErrorDomain 
                        code:status userInfo:nil];
        return nil;
    }
    if (isStale) *isStale = wasChanged;
    return [NSString stringWithUTF8String:(const char *)thePath];
}
@end

Otherwise, if you can require 10.6, then check out the newer APIs in NSURL.

酒解孤独 2024-11-22 01:53:55

首选项文件中的路径有时采用 Base64 编码。您可以使用 openssl 对值进行反/编码:

Decode: echo; | openssl enc -d -base64

编码: echo; | openssl enc -e -base64

Paths in preference files are sometimes base64 encoded. You can use openssl to de/encode the values:

Decode: echo <encoded-path> | openssl enc -d -base64

Encode: echo <path> | openssl enc -e -base64

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文