1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Camino code.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2002
19 * the Initial Developer. All Rights Reserved.
22 * Nate Weaver (Wevah) - wevah@derailer.org
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #import "NSURL+Utils.h"
41 @implementation NSURL (CaminoExtensions)
43 + (NSURL*)decodeLocalFileURL:(NSURL*)url
45 NSString* urlPathString = [url path];
46 NSString* ext = [[urlPathString pathExtension] lowercaseString];
47 OSType fileType = NSHFSTypeCodeFromFileType(NSHFSTypeOfFile(urlPathString));
49 if ([ext isEqualToString:@"url"] || fileType == 'LINK') {
50 url = [NSURL URLFromIEURLFile:urlPathString];
52 else if ([ext isEqualToString:@"webloc"] || [ext isEqualToString:@"ftploc"] ||
53 fileType == 'ilht' || fileType == 'ilft')
55 url = [NSURL URLFromInetloc:urlPathString];
62 // Reads the URL from a .webloc/.ftploc file.
63 // Returns the URL, or nil on failure.
65 +(NSURL*)URLFromInetloc:(NSString*)inFile
70 if (inFile && FSPathMakeRef((UInt8 *)[inFile fileSystemRepresentation], &ref, NULL) == noErr) {
73 resRef = FSOpenResFile(&ref, fsRdPerm);
75 if (resRef != -1) { // Has resouce fork.
78 if ((urlResHandle = Get1Resource('url ', 256))) { // Has 'url ' resource with ID 256.
81 size = GetMaxResourceSize(urlResHandle);
82 // Begin Google Modified
83 // ret = [NSURL URLWithString:[NSString stringWithCString:(char *)*urlResHandle length:size]];
84 NSString *urlString = [[[NSString alloc] initWithBytes:(void *)*urlResHandle
86 encoding:NSMacOSRomanStringEncoding] // best guess here
88 ret = [NSURL URLWithString:urlString];
89 // End Google Modified
95 if (!ret) { // Look for valid plist data.
97 if ((plist = [[NSDictionary alloc] initWithContentsOfFile:inFile])) {
98 ret = [NSURL URLWithString:[plist objectForKey:@"URL"]];
108 // Reads the URL from a .url file.
109 // Returns the URL or nil on failure.
111 +(NSURL*)URLFromIEURLFile:(NSString*)inFile
115 // Is this really an IE .url file?
117 NSCharacterSet *newlines = [NSCharacterSet characterSetWithCharactersInString:@"\r\n"];
118 // Begin Google Modified
119 // NSScanner *scanner = [NSScanner scannerWithString:[NSString stringWithContentsOfFile:inFile]];
120 NSString *fileString = [NSString stringWithContentsOfFile:inFile
121 encoding:NSWindowsCP1252StringEncoding // best guess here
123 NSScanner *scanner = [NSScanner scannerWithString:fileString];
124 // End Google Modified
125 [scanner scanUpToString:@"[InternetShortcut]" intoString:nil];
127 if ([scanner scanString:@"[InternetShortcut]" intoString:nil]) {
128 // Scan each non-empty line in this section. We don't need to explicitly scan the newlines or
129 // whitespace because NSScanner ignores these by default.
132 while ([scanner scanUpToCharactersFromSet:newlines intoString:&line]) {
133 if ([line hasPrefix:@"URL="]) {
134 ret = [NSURL URLWithString:[line substringFromIndex:4]];
137 else if ([line hasPrefix:@"["]) {
138 // This is the start of a new section, so if we haven't found an URL yet, we should bail.