77#define CDIR_ENTRY_SIG (0x02014b50)
78#define LOC_FILE_HEADER_SIG (0x04034b50)
79#define CDIR_END_SIG (0x06054b50)
81static unsigned char readByte(NSFileHandle *file)
85 NSData* tmpBuf = [file readDataOfLength: 1];
88 unsigned char *
d = (
unsigned char*)[tmpBuf
bytes];
96 unsigned short p0 = (
unsigned short)
readByte(file);
97 unsigned short p1 = (
unsigned short)
readByte(file);
98 return (
unsigned short)(p0|(p1<<8));
103 unsigned p0 = (unsigned)
readByte(file);
104 unsigned p1 = (unsigned)
readByte(file);
105 unsigned p2 = (unsigned)
readByte(file);
106 unsigned p3 = (unsigned)
readByte(file);
107 return (
unsigned)(p0|(p1<<8)|(p2<<16)|(p3<<24));
112 unsigned signature =
readInt(file);
123 NSData *data = [file readDataOfLength: end->comment_size];
124 end->comment = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
130 unsigned signature =
readInt(file);
150 NSData *data = [file readDataOfLength: entry->filename_size];
151 entry->
filename = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
152 data = [file readDataOfLength: entry->extra_field_size];
153 entry->
extra_field = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
154 data = [file readDataOfLength: entry->file_comment_size];
155 entry->
file_comment = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
161 unsigned signature =
readInt(file);
175 NSData *data = [file readDataOfLength: header->filename_size];
176 header->filename = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
177 data = [file readDataOfLength: header->extra_field_size];
178 header->extra_field = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
190 if (!(
header->general_flag & 0x08))
207 [file seekToEndOfFile];
208 unsigned long long fileLength = [file offsetInFile];
213 [file seekToFileOffset: (fileLength - 4)];
215 unsigned long long limit;
216 if (fileLength > 1024)
217 limit = fileLength - 1024;
221 unsigned long long offset;
222 while ((offset = [file offsetInFile]) > limit)
224 unsigned signature =
readInt(file);
228 [file seekToFileOffset: offset];
234 [file seekToFileOffset: (offset - 1)];
247 [file seekToFileOffset: end.cdir_offset];
251 [file seekToFileOffset: entry.offset];
262 [file seekToEndOfFile];
263 unsigned long long fileLength = [file offsetInFile];
269 [file seekToFileOffset: end.cdir_offset];
277 while ( [file offsetInFile] < fileLength && [file offsetInFile] <
end.cdir_offset +
end.cdir_size);
280 [file seekToFileOffset: entry->offset];
295 return [file readDataOfLength: entry.compressed_size];
302 strm.zalloc = Z_NULL;
304 strm.opaque = Z_NULL;
306 strm.next_in = Z_NULL;
307 ret = inflateInit2(&strm,-MAX_WBITS);
311 NSData *compressedData = [file readDataOfLength: entry.compressed_size];
313 strm.avail_in = [compressedData length];
314 strm.next_in = (Bytef *)[compressedData
bytes];
317 if (!uncompressedData)
319 (void)inflateEnd(&strm);
323 strm.next_out = uncompressedData;
324 ret = inflate(&strm, Z_FINISH);
330 (void)inflateEnd(&strm);
331 free(uncompressedData);
334 (void)inflateEnd(&strm);
335 NSData *returnBuffer = [NSData dataWithBytes:(const void *)uncompressedData length:entry.uncompressed_size];
336 free(uncompressedData);
346 static BOOL isInitialized =
NO;
347 if (isInitialized == NO) {
348 NSMutableDictionary *temp = [NSMutableDictionary new];
349 [temp setObject:@"OpenOffice.org 1.0 Text" forKey:@"org.openoffice.text"];
350 [temp setObject:@"OpenDocument Text" forKey:@"org.oasis.opendocument.text"];
351 [temp setObject:@"OpenOffice.org 1.0 Spreadsheet" forKey:@"org.openoffice.spreadsheet"];
352 [temp setObject:@"OpenDocument Spreadsheet" forKey:@"org.oasis.opendocument.spreadsheet"];
353 [temp setObject:@"OpenOffice.org 1.0 Presentation" forKey:@"org.openoffice.presentation"];
354 [temp setObject:@"OpenDocument Presentation" forKey:@"org.oasis.opendocument.presentation"];
355 [temp setObject:@"OpenOffice.org 1.0 Drawing" forKey:@"org.openoffice.graphics"];
356 [temp setObject:@"OpenDocument Drawing" forKey:@"org.oasis.opendocument.graphics"];
357 [temp setObject:@"OpenOffice.org 1.0 Master" forKey:@"org.openoffice.text-master"];
358 [temp setObject:@"OpenDocument Master" forKey:@"org.oasis.opendocument.text-master"];
359 [temp setObject:@"OpenOffice.org 1.0 Formula" forKey:@"org.openoffice.formula"];
360 [temp setObject:@"OpenDocument Formula" forKey:@"org.oasis.opendocument.formula"];
361 [temp setObject:@"OpenOffice.org 1.0 Text Template" forKey:@"org.openoffice.text-template"];
362 [temp setObject:@"OpenDocument Text Template" forKey:@"org.oasis.opendocument.text-template"];
363 [temp setObject:@"OpenOffice.org 1.0 Spreadsheet Template" forKey:@"org.openoffice.spreadsheet-template"];
364 [temp setObject:@"OpenDocument Spreadsheet Template" forKey:@"org.oasis.opendocument.spreadsheet-template"];
365 [temp setObject:@"OpenOffice.org 1.0 Presentation Template" forKey:@"org.openoffice.presentation-template"];
366 [temp setObject:@"OpenDocument Presentation Template" forKey:@"org.oasis.opendocument.presentation-template"];
367 [temp setObject:@"OpenOffice.org 1.0 Drawing Template" forKey:@"org.openoffice.graphics-template"];
368 [temp setObject:@"OpenDocument Drawing Template" forKey:@"org.oasis.opendocument.graphics-template"];
369 [temp setObject:@"OpenOffice.org 1.0 Database" forKey:@"org.openoffice.database"];
370 [temp setObject:@"OpenDocument Chart" forKey:@"org.oasis.opendocument.chart"];
372 uti2kind = [[NSDictionary dictionaryWithDictionary:temp] retain];
380- (
BOOL)importDocument:(NSString*)pathToFile contentType:(NSString*)contentTypeUTI attributes:(NSMutableDictionary*)attributes
385 NSString *itemKind = [uti2kind objectForKey:contentTypeUTI];
386 if (itemKind != nil) {
387 [attributes setObject:itemKind forKey:(NSString*)kMDItemKind];
391 NSFileHandle *unzipFile = [
self openZipFileAtPath:pathToFile];
394 if (unzipFile == nil) {
400 NSData *metaData = [
self metaDataFileFromZip:unzipFile];
401 if (metaData == nil) {
402 [unzipFile closeFile];
418 NSData *contentData = [
self contentDataFileFromZip:unzipFile];
419 if (contentData == nil) {
420 [unzipFile closeFile];
424 [contentData retain];
427 if (parser2 != nil) {
432 [contentData release];
435 [unzipFile closeFile];
441- (NSFileHandle*)openZipFileAtPath:(NSString*)pathToFile
443 NSFileHandle* unzipFile = nil;
445 if ([pathToFile length] != 0)
447 unzipFile = [NSFileHandle fileHandleForReadingAtPath: pathToFile];
450 if (unzipFile == nil)
458 [unzipFile closeFile];
468- (NSData*) metaDataFileFromZip:(NSFileHandle*)unzipFile
470 if (unzipFile == nil)
477- (NSData*) contentDataFileFromZip:(NSFileHandle*)unzipFile
479 if (unzipFile == nil)
constexpr sal_Int8 header[]
static unsigned char readByte(NSFileHandle *file)
static NSData * getUncompressedData(NSFileHandle *file, NSString *name)
static bool isZipFile(NSFileHandle *file)
static unsigned short readShort(NSFileHandle *file)
static unsigned readInt(NSFileHandle *file)
static bool readCentralDirectoryEnd(NSFileHandle *file, CentralDirectoryEnd *end)
static bool areHeadersConsistent(const LocalFileHeader *header, const CentralDirectoryEntry *entry)
static bool readLocalFileHeader(NSFileHandle *file, LocalFileHeader *header)
static bool findCentralDirectoryEnd(NSFileHandle *file)
static bool readCentralDirectoryEntry(NSFileHandle *file, CentralDirectoryEntry *entry)
static bool findDataStream(NSFileHandle *file, CentralDirectoryEntry *entry, NSString *name)
static NSDictionary * uti2kind
#define LOC_FILE_HEADER_SIG
void parseXML:intoDictionary:(NSData *data,[intoDictionary] NSMutableDictionary *dict)
std::vector< sal_uInt8 > bytes
const wchar_t *typedef BOOL
SbxDecimal::CmpResult compare(SAL_UNUSED_PARAMETER const SbxDecimal &, SAL_UNUSED_PARAMETER const SbxDecimal &)
unsigned short disk_entries
unsigned short cdir_entries
unsigned short comment_size
unsigned short creator_version
unsigned short min_version
unsigned short lastmod_date
unsigned short internal_attr
unsigned short extra_field_size
unsigned short filename_size
unsigned short lastmod_time
unsigned short compression
unsigned short file_comment_size
unsigned uncompressed_size
unsigned short general_flag