|
|
|
|
@ -5,6 +5,8 @@ |
|
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
|
|
#include <GzUtil.h> |
|
|
|
|
#include <zip.h> |
|
|
|
|
#include <iostream> |
|
|
|
|
|
|
|
|
|
const char* TAG_PREVIEW_NAME = "preview"; |
|
|
|
|
const int TAG_PREVIEW_NAME_LEN = strlen(TAG_PREVIEW_NAME); |
|
|
|
|
@ -108,23 +110,69 @@ PreviewExtractResult XojPreviewExtractor::readFile(Path file) |
|
|
|
|
{ |
|
|
|
|
return PREVIEW_RESULT_BAD_FILE_EXTENSION; |
|
|
|
|
} |
|
|
|
|
//read the new file format
|
|
|
|
|
int zipError = 0; |
|
|
|
|
zip_t* zipFp = zip_open(file.c_str(), ZIP_RDONLY, &zipError); |
|
|
|
|
|
|
|
|
|
gzFile fp = GzUtil::openPath(file, "r"); |
|
|
|
|
if (!fp) |
|
|
|
|
if (!zipFp && zipError == ZIP_ER_NOZIP) |
|
|
|
|
{ |
|
|
|
|
return PREVIEW_RESULT_COULD_NOT_OPEN_FILE; |
|
|
|
|
gzFile fp = GzUtil::openPath(file, "r"); |
|
|
|
|
if (!fp) |
|
|
|
|
{ |
|
|
|
|
return PREVIEW_RESULT_COULD_NOT_OPEN_FILE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// The <preview> Tag is within the first 179 Bytes
|
|
|
|
|
// The Preview should end within the first 8k
|
|
|
|
|
|
|
|
|
|
char buffer[BUF_SIZE]; |
|
|
|
|
int readLen = gzread(fp, buffer, BUF_SIZE); |
|
|
|
|
|
|
|
|
|
PreviewExtractResult result = readPreview(buffer, readLen); |
|
|
|
|
|
|
|
|
|
gzclose(fp); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// The <preview> Tag is within the first 179 Bytes
|
|
|
|
|
// The Preview should end within the first 8k
|
|
|
|
|
zip_stat_t thumbStat; |
|
|
|
|
int statStatus = zip_stat(zipFp, "thumbnails/thumbnail.png", 0, &thumbStat); |
|
|
|
|
if (statStatus != 0) |
|
|
|
|
{ |
|
|
|
|
return PREVIEW_RESULT_NO_PREVIEW; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
char buffer[BUF_SIZE]; |
|
|
|
|
int readLen = gzread(fp, buffer, BUF_SIZE); |
|
|
|
|
if (thumbStat.valid & ZIP_STAT_SIZE) |
|
|
|
|
{ |
|
|
|
|
dataLen = thumbStat.size; |
|
|
|
|
} else |
|
|
|
|
{ |
|
|
|
|
return PREVIEW_RESULT_ERROR_READING_PREVIEW; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
zip_file_t* thumb = zip_fopen(zipFp, "thumbnails/thumbnail.png", 0); |
|
|
|
|
|
|
|
|
|
if (!thumb) |
|
|
|
|
{ |
|
|
|
|
return PREVIEW_RESULT_ERROR_READING_PREVIEW; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
PreviewExtractResult result = readPreview(buffer, readLen); |
|
|
|
|
data = (unsigned char *)g_malloc(thumbStat.size); |
|
|
|
|
zip_uint64_t readBytes = 0; |
|
|
|
|
while (readBytes < dataLen) |
|
|
|
|
{ |
|
|
|
|
zip_int64_t read = zip_fread(thumb, data, thumbStat.size); |
|
|
|
|
if (read == -1) |
|
|
|
|
{ |
|
|
|
|
g_free(data); |
|
|
|
|
return PREVIEW_RESULT_ERROR_READING_PREVIEW; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
readBytes += read; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
gzclose(fp); |
|
|
|
|
return result; |
|
|
|
|
zip_fclose(thumb); |
|
|
|
|
zip_close(zipFp); |
|
|
|
|
return PREVIEW_RESULT_IMAGE_READ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|