corinthia-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject incubator-corinthia git commit: global dir works, file offset missing
Date Fri, 31 Jul 2015 20:34:39 GMT
Repository: incubator-corinthia
Updated Branches:
  refs/heads/newZipExperiment d0ceb0e00 -> ba684e00e


global dir works, file offset missing


Project: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/commit/ba684e00
Tree: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/tree/ba684e00
Diff: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/diff/ba684e00

Branch: refs/heads/newZipExperiment
Commit: ba684e00e6935b0411899c297458ee3366c5902c
Parents: d0ceb0e
Author: jani <jani@apache.org>
Authored: Fri Jul 31 22:34:16 2015 +0200
Committer: jani <jani@apache.org>
Committed: Fri Jul 31 22:34:16 2015 +0200

----------------------------------------------------------------------
 DocFormats/headers/DFPlatform.h       |  13 ++-
 DocFormats/platform/src/Wrapper_zip.c | 124 +++++++++++++++++++++++++----
 2 files changed, 117 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ba684e00/DocFormats/headers/DFPlatform.h
----------------------------------------------------------------------
diff --git a/DocFormats/headers/DFPlatform.h b/DocFormats/headers/DFPlatform.h
index 09e3b71..5868073 100644
--- a/DocFormats/headers/DFPlatform.h
+++ b/DocFormats/headers/DFPlatform.h
@@ -70,10 +70,15 @@ void DFInitOnce(DFOnce *once, DFOnceFunction fun);
 
 // Zip functions
 typedef struct {
-	FILE *zipFile;
-	int   zipFileCount;
-	int   zipDirSize;
-	int   zipDirOffset;
+	int   compressedSize;   // File size on disk
+	int   uncompressedSize; // Real file size
+	int   fileNameLength;   // Length of filename
+	char *fileName;         // filename
+} DFextZipDirEntry;
+typedef struct {
+	FILE             *zipFile;        // file handle to zip file
+	int               zipFileCount;   // number of entries in array
+	DFextZipDirEntry *zipFileEntries; // array with filenames in zip
 } DFextZipHandle;
 typedef DFextZipHandle * DFextZipHandleP;
 

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ba684e00/DocFormats/platform/src/Wrapper_zip.c
----------------------------------------------------------------------
diff --git a/DocFormats/platform/src/Wrapper_zip.c b/DocFormats/platform/src/Wrapper_zip.c
index 8da5664..eaaa73c 100644
--- a/DocFormats/platform/src/Wrapper_zip.c
+++ b/DocFormats/platform/src/Wrapper_zip.c
@@ -36,15 +36,45 @@ typedef struct {
 #pragma pack()
 static const uint32_t ZipEndRecord_signature = 0x06054B50;
 
+#pragma pack(1)
+typedef struct {
+	uint32_t signature;                   // 0x02014B50
+	uint16_t versionMadeBy;               // NOT USED
+	uint16_t versionNeededToExtract;      // NOT USED
+	uint16_t generalPurposeBitFlag;       // NOT USED
+	uint16_t compressionMethod;           // Zip algorithm
+	uint16_t lastModFileTime;             // NOT USED
+	uint16_t lastModFileDate;             // NOT USED
+	uint32_t crc32;                       // NOT USED
+	uint32_t compressedSize;              // File size on disk
+	uint32_t uncompressedSize;            // Real file size
+	uint16_t fileNameLength;              // Length of filename 
+	uint16_t extraFieldLength;            // Only used to skip to next record
+	uint16_t fileCommentLength;           // Only used to skip to next record
+	uint16_t diskNumberStart;             // NOT USED
+	uint16_t internalFileAttributes;      // NOT USED
+	uint32_t externalFileAttributes;      // NOT USED
+	uint32_t relativeOffsetOflocalHeader; // NOT USED
+} ZipDirectoryRecord;
+#pragma pack()
+static const uint32_t ZipDirectoryRecord_signature = 0x02014B50;
 
 
-static int readEndRecord(FILE *zipFile, DFextZipHandleP zipHandle)
+
+static int readDirectory(FILE *zipFile, DFextZipHandleP zipHandle)
 {
-	unsigned long fileSize, readBytes, i;
-	unsigned char workBuf[1024];
-	ZipEndRecord *rec;
+	unsigned long       fileSize, readBytes;
+	unsigned char       workBuf[4096];
+	ZipEndRecord       *recEnd;
+	ZipDirectoryRecord *recDir;
+	DFextZipDirEntry   *zipDirEntry;
+	int                 i, zipDirSize, zipDirOffset;
+
 
 
+    //***** Read EndRecord *****
+	// the EndRecord contains information, where the directory is located
+
 	// find end of file, and calculate size
 	if (fseek(zipFile, 0, SEEK_END))
 		return -1;
@@ -61,35 +91,97 @@ static int readEndRecord(FILE *zipFile, DFextZipHandleP zipHandle)
 
 	// search for EndRecord signature
 	for (i = readBytes - sizeof(ZipEndRecord); i >= 0; i--) {
-		rec = (ZipEndRecord *)(workBuf + i);
-		if (rec->signature == ZipEndRecord_signature)
+		recEnd = (ZipEndRecord *)(workBuf + i);
+
+		// check if we have a signature
+		if (recEnd->signature == ZipEndRecord_signature)
 			break;
 	}
 	if (i < 0)
 		return -1;
 
 	// update zipHandle
-	zipHandle->zipFileCount = rec->numEntries;
-	zipHandle->zipDirSize   = rec->centralDirectorySize;
-	zipHandle->zipDirOffset = rec->centralDirectoryOffset;
+	zipDirSize                = recEnd->centralDirectorySize;
+	zipDirOffset              = recEnd->centralDirectoryOffset;
+	zipHandle->zipFileCount   = recEnd->numEntries;
+	zipHandle->zipFileEntries = xmalloc(zipHandle->zipFileCount * sizeof(DFextZipDirEntry));
+
+
+	//***** Read Directory *****
+	// Each file has a global and a local entry, read both in a loop
+
+	// Find directory
+	if (fseek(zipFile, zipDirOffset, SEEK_SET))
+		return -1;
+
+	// loop through all entries
+	for (i = 0; i < zipHandle->zipFileCount; i++) {
+		// Read global directory entry
+		if (fread(workBuf, 1, sizeof(ZipDirectoryRecord), zipFile) < sizeof(ZipDirectoryRecord))
+			return -1;
+		recDir = (ZipDirectoryRecord *)workBuf;
+
+		// verify signature
+		if (recDir->signature != ZipDirectoryRecord_signature)
+			return -1;
+
+		// Save global information
+		zipDirEntry                   = zipHandle->zipFileEntries + (i * sizeof(zipDirEntry));
+		zipDirEntry->compressedSize   = recDir->compressedSize;
+		zipDirEntry->fileNameLength   = recDir->fileNameLength;
+		zipDirEntry->uncompressedSize = recDir->uncompressedSize;
+
+		// Add filename
+		zipDirEntry->fileName = xmalloc(zipDirEntry->fileNameLength + 1);
+		if (fread(zipDirEntry->fileName, 1, zipDirEntry->fileNameLength, zipFile) < (unsigned
long)zipDirEntry->fileNameLength)
+			return -1;
+		zipDirEntry->fileName[zipDirEntry->fileNameLength] = '\0';
+
+		// Skip extra info
+		if (fseek(zipFile, recDir->extraFieldLength, SEEK_CUR)
+			|| fseek(zipFile, recDir->fileCommentLength, SEEK_CUR))
+			return -1;
+	}
+
 	return 0;
 }
 
 
 
-DFextZipHandleP DFextZipOpen(const char *zipFilename) {
-    DFextZipHandleP zipHandle = xmalloc(sizeof(DFextZipHandle));
+static int buildDirectory(FILE *zipFile, DFextZipHandleP zipHandle, int zipDirSize, int zipDirOffset)
+{
 
-	// open zip file for reading
-	zipHandle->zipFile = fopen(zipFilename, "rb");
-	if (zipHandle->zipFile &&
-		!readEndRecord(zipHandle->zipFile, zipHandle)) {
-		return zipHandle;
+#if 0
+	JZGlobalFileHeader fileHeader;
+	JZFileHeader header;
+	int i;
+
+
+	for (i = 0; i<endRecord->numEntries; i++) {
+
+		// Construct JZFileHeader from global file header
+		memcpy(&header, &fileHeader.compressionMethod, sizeof(header));
+		header.offset = fileHeader.relativeOffsetOflocalHeader;
+
+		if (!callback(zip, i, &header, (char *)jzBuffer))
+			break; // end if callback returns zero
 	}
 
+	return Z_OK;
+#endif
+	return 0;
+}
 
 
 
+DFextZipHandleP DFextZipOpen(const char *zipFilename) {
+    DFextZipHandleP zipHandle = xmalloc(sizeof(DFextZipHandle));
+
+	// open zip file for reading
+	zipHandle->zipFile = fopen(zipFilename, "rb");
+	if (zipHandle->zipFile
+		&& !readDirectory(zipHandle->zipFile, zipHandle))
+		return zipHandle;
 
 	// release memory
     free(zipHandle);


Mime
View raw message