Oolite
Loading...
Searching...
No Matches
NSDataOOExtensions.m
Go to the documentation of this file.
1/*
2
3NSDataOOExtensions.m
4
5Extensions to NSData.
6
7
8Copyright (C) 2008-2013 Jens Ayton and contributors
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files (the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27
28*/
29
30#import "OOFoundation.h"
31#import "unzip.h"
32
33#define ZIP_BUFFER_SIZE 8192
34
35@implementation NSData (OOExtensions)
36
37+ (instancetype)oo_dataWithOXZFile:(NSString*)path
38{
39 NSUInteger i, cl;
40 NSArray* components = [path pathComponents];
41 cl = [components count];
42 for (i = 0; i < cl; i++) {
43 NSString* component = [components objectAtIndex:i];
44 if ([[[component pathExtension] lowercaseString] isEqualToString:@"oxz"]) {
45 break;
46 }
47 }
48 // if i == cl then the path is entirely uncompressed
49 if (i == cl) {
50/* -initWithContentsOfMappedFile fails quietly under OS X if there's no file,
51 but GNUstep complains. */
52#if OOLITE_MAC_OS_X
53 return [[[NSData alloc] initWithContentsOfMappedFile:path] autorelease];
54#else
55 NSFileManager* fmgr = [NSFileManager defaultManager];
56 BOOL dir;
57
58 if ([fmgr fileExistsAtPath:path isDirectory:&dir]) {
59 if (!dir) {
60 if ([[fmgr fileAttributesAtPath:path traverseLink:NO] fileSize] == 0) {
61 OOLog(kOOLogFileNotFound, @"Expected file but found empty file at %@", path);
62 } else {
63 return [[[NSData alloc] initWithContentsOfMappedFile:path] autorelease];
64 }
65 } else {
66 OOLog(kOOLogFileNotFound, @"Expected file but found directory at %@", path);
67 }
68 }
69 return nil;
70#endif
71 }
72 // otherwise components 0..i are the OXZ path, and i+1..n are the
73 // path inside the OXZ
74 NSRange range;
75 range.location = 0;
76 range.length = i + 1;
77 NSString* zipFile = [NSString pathWithComponents:[components subarrayWithRange:range]];
78 range.location = i + 1;
79 range.length = cl - (i + 1);
80 NSString* containedFile = [NSString pathWithComponents:[components subarrayWithRange:range]];
81
82 unzFile uf = NULL;
83 const char* zipname = [zipFile UTF8String];
84 if (zipname != NULL) {
85 uf = unzOpen64(zipname);
86 }
87 if (uf == NULL) {
88 // This is not necessarily an error - the OXZ manager tries to
89 // do this as a test for the presence of managed OXZs
90 // OOLog(kOOLogFileNotFound, @"Could not unzip OXZ at %@", zipFile);
91 return nil;
92 }
93 const char* filename = [containedFile UTF8String];
94 // unzLocateFile(*, *, 1) = case-sensitive extract
95 if (unzLocateFile(uf, filename, 1) != UNZ_OK) {
96 unzClose(uf);
97 /* Much of the time this function is called with the
98 * expectation that the file may not necessarily exist -
99 * e.g. on plist merges, config scans, etc. So don't add log
100 * entries for this failure mode */
101 // OOLog(kOOLogFileNotFound, @"Could not find %@ within OXZ at %@", containedFile, zipFile);
102 return nil;
103 }
104
105 int err = UNZ_OK;
106 unz_file_info64 file_info = { 0 };
107 err = unzGetCurrentFileInfo64(uf, &file_info, NULL, 0, NULL, 0, NULL, 0);
108 if (err != UNZ_OK) {
109 unzClose(uf);
110 OOLog(kOOLogFileNotFound, @"Could not get properties of %@ within OXZ at %@", containedFile, zipFile);
111 return nil;
112 }
113
114 err = unzOpenCurrentFile(uf);
115 if (err != UNZ_OK) {
116 unzClose(uf);
117 OOLog(kOOLogFileNotFound, @"Could not read %@ within OXZ at %@", containedFile, zipFile);
118 return nil;
119 }
120
121 NSMutableData* tmp = [NSMutableData dataWithCapacity:file_info.uncompressed_size];
122 void* buf = (void*)malloc(ZIP_BUFFER_SIZE);
123 do {
125 if (err < 0) {
126 OOLog(kOOLogFileNotFound, @"Could not read %@ within OXZ at %@ (err %d)", containedFile, zipFile, err);
127 break;
128 }
129 if (err == 0) {
130 break;
131 }
132 [tmp appendBytes:buf length:err];
133 } while (err > 0);
134 free(buf);
135
136 err = unzCloseCurrentFile(uf);
137 if (err != UNZ_OK) {
138 unzClose(uf);
139 OOLog(kOOLogFileNotFound, @"Could not close %@ within OXZ at %@", containedFile, zipFile);
140 return nil;
141 }
142
143 unzClose(uf);
144 return [[tmp retain] autorelease];
145}
146
147@end
#define ZIP_BUFFER_SIZE
#define OOLog(class, format,...)
Definition OOLogging.h:84
NSString *const kOOLogFileNotFound
Definition OOLogging.m:601
return nil
const char * filename
Definition ioapi.h:125
voidpf void * buf
Definition ioapi.h:126
int ZEXPORT unzLocateFile(unzFile file, const char *szFileName, int iCaseSensitivity)
Definition unzip.c:1161
int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize)
Definition unzip.c:1058
int ZEXPORT unzOpenCurrentFile(unzFile file)
Definition unzip.c:1558
int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, unsigned len)
Definition unzip.c:1600
unzFile ZEXPORT unzOpen64(const void *path)
Definition unzip.c:758
int ZEXPORT unzCloseCurrentFile(unzFile file)
Definition unzip.c:1893
int ZEXPORT unzClose(unzFile file)
Definition unzip.c:768
struct unz_file_info64_s unz_file_info64
voidp unzFile
Definition unzip.h:72
#define UNZ_OK
Definition unzip.h:75