1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #import "OOoSpotlightImporter.h"
23 #import "OOoMetaDataParser.h"
24 #import "OOoContentDataParser.h"
26 /* a dictionary to hold the UTIs */
27 static NSDictionary *uti2kind;
30 unsigned short min_version;
31 unsigned short general_flag;
32 unsigned short compression;
33 unsigned short lastmod_time;
34 unsigned short lastmod_date;
36 unsigned compressed_size;
37 unsigned uncompressed_size;
38 unsigned short filename_size;
39 unsigned short extra_field_size;
41 NSString *extra_field;
45 unsigned short creator_version;
46 unsigned short min_version;
47 unsigned short general_flag;
48 unsigned short compression;
49 unsigned short lastmod_time;
50 unsigned short lastmod_date;
52 unsigned compressed_size;
53 unsigned uncompressed_size;
54 unsigned short filename_size;
55 unsigned short extra_field_size;
56 unsigned short file_comment_size;
57 unsigned short disk_num;
58 unsigned short internal_attr;
59 unsigned external_attr;
62 NSString *extra_field;
63 NSString *file_comment;
64 } CentralDirectoryEntry;
67 unsigned short disk_num;
68 unsigned short cdir_disk;
69 unsigned short disk_entries;
70 unsigned short cdir_entries;
73 unsigned short comment_size;
75 } CentralDirectoryEnd;
77 #define CDIR_ENTRY_SIG (0x02014b50)
78 #define LOC_FILE_HEADER_SIG (0x04034b50)
79 #define CDIR_END_SIG (0x06054b50)
81 static unsigned char readByte(NSFileHandle *file)
85 NSData* tmpBuf = [file readDataOfLength: 1];
88 unsigned char *d = (unsigned char*)[tmpBuf bytes];
92 static unsigned short readShort(NSFileHandle *file)
94 unsigned short p0 = (unsigned short)readByte(file);
95 unsigned short p1 = (unsigned short)readByte(file);
96 return (unsigned short)(p0|(p1<<8));
99 static unsigned readInt(NSFileHandle *file)
101 unsigned p0 = (unsigned)readByte(file);
102 unsigned p1 = (unsigned)readByte(file);
103 unsigned p2 = (unsigned)readByte(file);
104 unsigned p3 = (unsigned)readByte(file);
105 return (unsigned)(p0|(p1<<8)|(p2<<16)|(p3<<24));
108 static bool readCentralDirectoryEnd(NSFileHandle *file, CentralDirectoryEnd *end)
110 unsigned signature = readInt(file);
111 if (signature != CDIR_END_SIG)
114 end->disk_num = readShort(file);
115 end->cdir_disk = readShort(file);
116 end->disk_entries = readShort(file);
117 end->cdir_entries = readShort(file);
118 end->cdir_size = readInt(file);
119 end->cdir_offset = readInt(file);
120 end->comment_size = readShort(file);
121 NSData *data = [file readDataOfLength: end->comment_size];
122 end->comment = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
126 static bool readCentralDirectoryEntry(NSFileHandle *file, CentralDirectoryEntry *entry)
128 unsigned signature = readInt(file);
129 if (signature != CDIR_ENTRY_SIG)
132 entry->creator_version = readShort(file);
133 entry->min_version = readShort(file);
134 entry->general_flag = readShort(file);
135 entry->compression = readShort(file);
136 entry->lastmod_time = readShort(file);
137 entry->lastmod_date = readShort(file);
138 entry->crc32 = readInt(file);
139 entry->compressed_size = readInt(file);
140 entry->uncompressed_size = readInt(file);
141 entry->filename_size = readShort(file);
142 entry->extra_field_size = readShort(file);
143 entry->file_comment_size = readShort(file);
144 entry->disk_num = readShort(file);
145 entry->internal_attr = readShort(file);
146 entry->external_attr = readInt(file);
147 entry->offset = readInt(file);
148 NSData *data = [file readDataOfLength: entry->filename_size];
149 entry->filename = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
150 data = [file readDataOfLength: entry->extra_field_size];
151 entry->extra_field = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
152 data = [file readDataOfLength: entry->file_comment_size];
153 entry->file_comment = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
157 static bool readLocalFileHeader(NSFileHandle *file, LocalFileHeader *header)
159 unsigned signature = readInt(file);
160 if (signature != LOC_FILE_HEADER_SIG)
163 header->min_version = readShort(file);
164 header->general_flag = readShort(file);
165 header->compression = readShort(file);
166 header->lastmod_time = readShort(file);
167 header->lastmod_date = readShort(file);
168 header->crc32 = readInt(file);
169 header->compressed_size = readInt(file);
170 header->uncompressed_size = readInt(file);
171 header->filename_size = readShort(file);
172 header->extra_field_size = readShort(file);
173 NSData *data = [file readDataOfLength: header->filename_size];
174 header->filename = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
175 data = [file readDataOfLength: header->extra_field_size];
176 header->extra_field = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
180 static bool areHeadersConsistent(const LocalFileHeader *header, const CentralDirectoryEntry *entry)
182 if (header->min_version != entry->min_version)
184 if (header->general_flag != entry->general_flag)
186 if (header->compression != entry->compression)
188 if (!(header->general_flag & 0x08))
190 if (header->crc32 != entry->crc32)
192 if (header->compressed_size != entry->compressed_size)
194 if (header->uncompressed_size != entry->uncompressed_size)
200 static bool findCentralDirectoryEnd(NSFileHandle *file)
202 // Assume the cdir end is in the last 1024 bytes
203 // Scan backward from end of file for the end signature
205 [file seekToEndOfFile];
206 unsigned long long fileLength = [file offsetInFile];
211 [file seekToFileOffset: (fileLength - 4)];
213 unsigned long long limit;
214 if (fileLength > 1024)
215 limit = fileLength - 1024;
219 unsigned long long offset;
220 while ((offset = [file offsetInFile]) > limit)
222 unsigned signature = readInt(file);
223 if (signature == CDIR_END_SIG)
225 // Seek back over the CDIR_END_SIG
226 [file seekToFileOffset: offset];
231 // Seek one byte back
232 [file seekToFileOffset: (offset - 1)];
238 static bool isZipFile(NSFileHandle *file)
240 if (!findCentralDirectoryEnd(file))
242 CentralDirectoryEnd end;
243 if (!readCentralDirectoryEnd(file, &end))
245 [file seekToFileOffset: end.cdir_offset];
246 CentralDirectoryEntry entry;
247 if (!readCentralDirectoryEntry(file, &entry))
249 [file seekToFileOffset: entry.offset];
250 LocalFileHeader header;
251 if (!readLocalFileHeader(file, &header))
253 if (!areHeadersConsistent(&header, &entry))
258 static bool findDataStream(NSFileHandle *file, CentralDirectoryEntry *entry, NSString *name)
260 [file seekToEndOfFile];
261 unsigned long long fileLength = [file offsetInFile];
262 if (!findCentralDirectoryEnd(file))
264 CentralDirectoryEnd end;
265 if (!readCentralDirectoryEnd(file, &end))
267 [file seekToFileOffset: end.cdir_offset];
270 if (!readCentralDirectoryEntry(file, entry))
272 if ([entry->filename compare: name] == NSOrderedSame)
275 while ( [file offsetInFile] < fileLength && [file offsetInFile] < end.cdir_offset + end.cdir_size);
276 if ([entry->filename compare: name] != NSOrderedSame)
278 [file seekToFileOffset: entry->offset];
279 LocalFileHeader header;
280 if (!readLocalFileHeader(file, &header))
282 if (!areHeadersConsistent(&header, entry))
287 static NSData *getUncompressedData(NSFileHandle *file, NSString *name)
289 CentralDirectoryEntry entry;
290 if (!findDataStream(file, &entry, name))
292 if (!entry.compression)
293 return [file readDataOfLength: entry.compressed_size];
299 /* allocate inflate state */
300 strm.zalloc = Z_NULL;
302 strm.opaque = Z_NULL;
304 strm.next_in = Z_NULL;
305 ret = inflateInit2(&strm,-MAX_WBITS);
309 NSData *compressedData = [file readDataOfLength: entry.compressed_size];
311 strm.avail_in = [compressedData length];
312 strm.next_in = (Bytef *)[compressedData bytes];
314 Bytef *uncompressedData = (Bytef *)malloc(entry.uncompressed_size);
315 if (!uncompressedData)
317 (void)inflateEnd(&strm);
320 strm.avail_out = entry.uncompressed_size;
321 strm.next_out = uncompressedData;
322 ret = inflate(&strm, Z_FINISH);
328 (void)inflateEnd(&strm);
329 free(uncompressedData);
332 (void)inflateEnd(&strm);
333 NSData *returnBuffer = [NSData dataWithBytes:(const void *)uncompressedData length:entry.uncompressed_size];
334 free(uncompressedData);
339 @implementation OOoSpotlightImporter
341 /* initialize is only called once the first time this class is loaded */
344 static BOOL isInitialized = NO;
345 if (isInitialized == NO) {
346 NSMutableDictionary *temp = [NSMutableDictionary new];
347 [temp setObject:@"OpenOffice.org 1.0 Text" forKey:@"org.openoffice.text"];
348 [temp setObject:@"OpenDocument Text" forKey:@"org.oasis.opendocument.text"];
349 [temp setObject:@"OpenOffice.org 1.0 Spreadsheet" forKey:@"org.openoffice.spreadsheet"];
350 [temp setObject:@"OpenDocument Spreadsheet" forKey:@"org.oasis.opendocument.spreadsheet"];
351 [temp setObject:@"OpenOffice.org 1.0 Presentation" forKey:@"org.openoffice.presentation"];
352 [temp setObject:@"OpenDocument Presentation" forKey:@"org.oasis.opendocument.presentation"];
353 [temp setObject:@"OpenOffice.org 1.0 Drawing" forKey:@"org.openoffice.graphics"];
354 [temp setObject:@"OpenDocument Drawing" forKey:@"org.oasis.opendocument.graphics"];
355 [temp setObject:@"OpenOffice.org 1.0 Master" forKey:@"org.openoffice.text-master"];
356 [temp setObject:@"OpenDocument Master" forKey:@"org.oasis.opendocument.text-master"];
357 [temp setObject:@"OpenOffice.org 1.0 Formula" forKey:@"org.openoffice.formula"];
358 [temp setObject:@"OpenDocument Formula" forKey:@"org.oasis.opendocument.formula"];
359 [temp setObject:@"OpenOffice.org 1.0 Text Template" forKey:@"org.openoffice.text-template"];
360 [temp setObject:@"OpenDocument Text Template" forKey:@"org.oasis.opendocument.text-template"];
361 [temp setObject:@"OpenOffice.org 1.0 Spreadsheet Template" forKey:@"org.openoffice.spreadsheet-template"];
362 [temp setObject:@"OpenDocument Spreadsheet Template" forKey:@"org.oasis.opendocument.spreadsheet-template"];
363 [temp setObject:@"OpenOffice.org 1.0 Presentation Template" forKey:@"org.openoffice.presentation-template"];
364 [temp setObject:@"OpenDocument Presentation Template" forKey:@"org.oasis.opendocument.presentation-template"];
365 [temp setObject:@"OpenOffice.org 1.0 Drawing Template" forKey:@"org.openoffice.graphics-template"];
366 [temp setObject:@"OpenDocument Drawing Template" forKey:@"org.oasis.opendocument.graphics-template"];
367 [temp setObject:@"OpenOffice.org 1.0 Database" forKey:@"org.openoffice.database"];
368 [temp setObject:@"OpenDocument Chart" forKey:@"org.oasis.opendocument.chart"];
370 uti2kind = [[NSDictionary dictionaryWithDictionary:temp] retain];
377 /* importDocument is the real starting point for our plugin */
378 - (BOOL)importDocument:(NSString*)pathToFile contentType:(NSString*)contentTypeUTI attributes:(NSMutableDictionary*)attributes
380 //NSLog(contentTypeUTI);
383 NSString *itemKind = [uti2kind objectForKey:contentTypeUTI];
384 if (itemKind != nil) {
385 [attributes setObject:itemKind forKey:(NSString*)kMDItemKind];
388 //first check to see if this is a valid zipped file that contains a file "meta.xml"
389 NSFileHandle *unzipFile = [self openZipFileAtPath:pathToFile];
392 if (unzipFile == nil) {
393 //NSLog(@"zip file not open");
397 //first get the metadata
398 NSData *metaData = [self metaDataFileFromZip:unzipFile];
399 if (metaData == nil) {
400 [unzipFile closeFile];
406 OOoMetaDataParser *parser = [OOoMetaDataParser new];
408 //parse and extract the data
409 [parser parseXML:metaData intoDictionary:attributes];
415 //and now get the content
416 NSData *contentData = [self contentDataFileFromZip:unzipFile];
417 if (contentData == nil) {
418 [unzipFile closeFile];
422 [contentData retain];
424 OOoContentDataParser *parser2 = [OOoContentDataParser new];
425 if (parser2 != nil) {
426 //parse and extract the data
427 [parser2 parseXML:contentData intoDictionary:attributes];
430 [contentData release];
433 [unzipFile closeFile];
438 /* openZipFileAtPath returns the file as a valid data structure or nil otherwise*/
439 - (NSFileHandle*)openZipFileAtPath:(NSString*)pathToFile
441 NSFileHandle* unzipFile = nil;
443 if ([pathToFile length] != 0)
445 unzipFile = [NSFileHandle fileHandleForReadingAtPath: pathToFile];
448 if (unzipFile == nil)
450 //NSLog(@"Cannot open %s",zipfilename);
454 if (!isZipFile(unzipFile))
456 [unzipFile closeFile];
459 //NSLog(@"%s opened",zipfilename);
464 /* metaDataFileFromZip extracts the file meta.xml from the zip file and returns it as an NSData* structure
465 or nil if the metadata is not present */
466 - (NSData*) metaDataFileFromZip:(NSFileHandle*)unzipFile
468 if (unzipFile == nil)
470 return getUncompressedData(unzipFile, @"meta.xml");
473 /* contentDataFileFromZip extracts the file content.xml from the zip file and returns it as an NSData* structure
474 or nil if the metadata is not present */
475 - (NSData*) contentDataFileFromZip:(NSFileHandle*)unzipFile
477 if (unzipFile == nil)
479 return getUncompressedData(unzipFile, @"content.xml");
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */