This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CHMContainer.m
253 lines (207 loc) · 7.05 KB
/
CHMContainer.m
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
// Chmox a CHM file viewer for Mac OS X
// Copyright (c) 2004 Stéphane Boisson.
// Copyright (c) 2017 Alessandro Gatti.
//
// Chmox is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// Chmox is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Foobar; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include <CommonCrypto/CommonCrypto.h>
#import "CHMContainer.h"
#import "chm_lib.h"
static inline NSString *_Nonnull readString(NSData *_Nonnull data,
unsigned long offset);
static inline NSString *_Nonnull readTrimmedString(NSData *_Nonnull data,
unsigned long offset);
typedef NS_ENUM(uint16_t, CHMObject) {
CHMObjectTableOfContentsPath = 0,
CHMObjectIndexPath,
CHMObjectHomePagePath,
CHMObjectTitle,
CHMObjectSystem,
CHMObjectDefaultWindow,
CHMObjectCompiledFile,
CHMObjectCompiler = 9,
CHMObjectTimestamp,
CHMObjectInformationTypesCount = 12,
CHMObjectDefaultFont = 16
};
@interface CHMContainer ()
- (instancetype)initWithContentsOfURL:(nonnull NSURL *)url
NS_DESIGNATED_INITIALIZER;
- (BOOL)loadMetadata;
@end
@implementation CHMContainer
+ (nullable instancetype)containerWithContentsOfURL:(nonnull NSURL *)url {
return [[CHMContainer alloc] initWithContentsOfURL:url];
}
- (nullable instancetype)initWithContentsOfURL:(nonnull NSURL *)url {
if (self = [super init]) {
_handle = chm_open(url.fileSystemRepresentation);
if (!_handle) {
return nil;
}
if (![self loadMetadata]) {
chm_close(_handle);
return nil;
}
}
return self;
}
- (void)dealloc {
if (self.handle) {
chm_close(self.handle);
}
}
NSString *_Nonnull readString(NSData *_Nonnull data, unsigned long offset) {
return @((const char *)data.bytes + offset);
}
NSString *_Nonnull readTrimmedString(NSData *_Nonnull data,
unsigned long offset) {
const char *stringData = (const char *)data.bytes + offset;
return [[NSMutableString stringWithUTF8String:stringData]
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];
}
- (BOOL)hasObjectWithPath:(nonnull NSString *)path {
struct chmUnitInfo info;
return chm_resolve_object(self.handle, path.UTF8String, &info) ==
CHM_RESOLVE_SUCCESS;
}
- (nullable NSData *)dataWithContentsOfObject:(nullable NSString *)path {
if (!path) {
return nil;
}
if ([path hasPrefix:@"/"]) {
if ([path hasPrefix:@"///"]) {
path = [path substringFromIndex:2];
}
} else {
path = [NSString stringWithFormat:@"/%@", path];
}
struct chmUnitInfo info;
if (chm_resolve_object(_handle, path.UTF8String, &info) !=
CHM_RESOLVE_SUCCESS) {
return nil;
}
void *buffer = malloc(info.length);
if (!buffer) {
return nil;
}
if (!chm_retrieve_object(_handle, &info, buffer, 0, info.length)) {
free(buffer);
return nil;
}
return [NSData dataWithBytesNoCopy:buffer length:info.length];
}
- (nullable NSData *)dataWithTableOfContents {
return [self dataWithContentsOfObject:self.tocPath];
}
- (BOOL)loadMetadata {
NSData *windowsData = [self dataWithContentsOfObject:@"/#WINDOWS"];
NSData *stringsData = [self dataWithContentsOfObject:@"/#STRINGS"];
if (windowsData && stringsData) {
const uint32_t entryCount = OSReadLittleInt32(windowsData.bytes, 0);
const uint32_t entrySize = OSReadLittleInt32(windowsData.bytes, 4);
for (int entryIndex = 0; entryIndex < entryCount; ++entryIndex) {
unsigned long entryOffset = 8 + (entryIndex * entrySize);
if (self.title.length == 0) {
self.title = readTrimmedString(
stringsData,
OSReadLittleInt32(windowsData.bytes, entryOffset + 0x14));
}
if (self.tocPath.length == 0) {
self.tocPath =
readString(stringsData, OSReadLittleInt32(windowsData.bytes,
entryOffset + 0x60));
}
if (self.indexPath.length == 0) {
self.indexPath =
readString(stringsData, OSReadLittleInt32(windowsData.bytes,
entryOffset + 0x64));
}
if (self.homePath.length == 0) {
self.homePath =
readString(stringsData, OSReadLittleInt32(windowsData.bytes,
entryOffset + 0x68));
}
}
}
NSData *systemData = [self dataWithContentsOfObject:@"/#SYSTEM"];
if (systemData == nil) {
return NO;
}
NSUInteger maxOffset = systemData.length;
for (unsigned int offset = 0; offset < maxOffset;
offset += OSReadLittleInt16(systemData.bytes, offset + 2) + 4) {
switch (OSReadLittleInt16(systemData.bytes, offset)) {
case CHMObjectTableOfContentsPath:
if (self.tocPath.length == 0) {
self.tocPath = readString(systemData, offset + 4);
}
break;
case CHMObjectIndexPath:
if (self.indexPath.length == 0) {
self.indexPath = readString(systemData, offset + 4);
}
break;
case CHMObjectHomePagePath:
if (self.homePath.length == 0) {
self.homePath = readString(systemData, offset + 4);
}
break;
case CHMObjectTitle:
if (self.title.length == 0) {
self.title = readTrimmedString(systemData, offset + 4);
}
break;
case CHMObjectSystem:
case CHMObjectDefaultWindow:
case CHMObjectCompiledFile:
case CHMObjectCompiler:
case CHMObjectTimestamp:
case CHMObjectInformationTypesCount:
case CHMObjectDefaultFont:
default:
break;
}
}
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(systemData.bytes, (CC_LONG)systemData.length, digest);
unsigned int *ptr = (unsigned int *)digest;
self.uniqueId = [[NSString alloc]
initWithFormat:@"%x%x%x%x%x", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4]];
if (self.title.length == 0) {
self.title = nil;
}
if (!self.homePath) {
self.homePath = [self findHomeForPath:@"/"];
}
return YES;
}
- (nonnull NSString *)findHomeForPath:(nonnull NSString *)basePath {
NSString *base = [basePath hasSuffix:@"/"]
? basePath
: [basePath stringByAppendingString:@"/"];
NSString *indexPath = [NSString stringWithFormat:@"%@index.htm", base];
if ([self hasObjectWithPath:indexPath]) {
return indexPath;
}
NSString *defaultPath = [NSString stringWithFormat:@"%@default.html", base];
if ([self hasObjectWithPath:defaultPath]) {
return defaultPath;
}
return indexPath;
}
@end