-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
thirdparty: Add MuPDF tar patch (#1042)
- Loading branch information
Showing
2 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
diff --git a/source/fitz/untar.c b/source/fitz/untar.c | ||
index 1588a8a..ee1a4ec 100644 | ||
--- a/source/fitz/untar.c | ||
+++ b/source/fitz/untar.c | ||
@@ -48,11 +48,24 @@ static void drop_tar_archive(fz_context *ctx, fz_archive *arch) | ||
fz_free(ctx, tar->entries); | ||
} | ||
|
||
+static int is_zeroed(fz_context *ctx, unsigned char *buf, size_t size) | ||
+{ | ||
+ size_t off; | ||
+ | ||
+ for (off = 0; off < size; off++) | ||
+ if (buf[off] != 0) | ||
+ return 0; | ||
+ | ||
+ return 1; | ||
+} | ||
+ | ||
static void ensure_tar_entries(fz_context *ctx, fz_tar_archive *tar) | ||
{ | ||
fz_stream *file = tar->super.file; | ||
- char name[100]; | ||
- char octsize[12]; | ||
+ unsigned char record[512]; | ||
+ char *longname = NULL; | ||
+ char name[101]; | ||
+ char octsize[13]; | ||
char typeflag; | ||
int offset, blocks, size; | ||
size_t n; | ||
@@ -64,35 +77,51 @@ static void ensure_tar_entries(fz_context *ctx, fz_tar_archive *tar) | ||
while (1) | ||
{ | ||
offset = fz_tell(ctx, file); | ||
- n = fz_read(ctx, file, (unsigned char *) name, nelem(name)); | ||
- if (n < nelem(name)) | ||
- fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of data in tar entry name"); | ||
- name[nelem(name) - 1] = '\0'; | ||
- | ||
- if (strlen(name) == 0) | ||
+ n = fz_read(ctx, file, record, nelem(record)); | ||
+ if (n == 0) | ||
break; | ||
+ if (n < nelem(record)) | ||
+ fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of data in tar record"); | ||
|
||
- fz_seek(ctx, file, 24, 1); | ||
- n = fz_read(ctx, file, (unsigned char *) octsize, nelem(octsize)); | ||
- if (n < nelem(octsize)) | ||
- fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of data in tar entry size"); | ||
+ if (is_zeroed(ctx, record, nelem(record))) | ||
+ continue; | ||
+ | ||
+ memcpy(name, record + 0, nelem(name) - 1); | ||
+ name[nelem(name) - 1] = '\0'; | ||
+ | ||
+ memcpy(octsize, record + 124, nelem(octsize) - 1); | ||
+ octsize[nelem(octsize) - 1] = '\0'; | ||
size = otoi(octsize); | ||
|
||
- fz_seek(ctx, file, 20, 1); | ||
- typeflag = fz_read_byte(ctx, file); | ||
+ typeflag = (char) record[156]; | ||
|
||
- fz_seek(ctx, file, 355, 1); | ||
- blocks = (size + 511) / 512; | ||
- fz_seek(ctx, file, blocks * 512, 1); | ||
+ if (typeflag == 'L') | ||
+ { | ||
+ longname = fz_malloc(ctx, size); | ||
+ n = fz_read(ctx, file, (unsigned char *) longname, size); | ||
+ if (n < (size_t) size) | ||
+ fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of data in tar long name entry name"); | ||
|
||
- if (typeflag != '0') | ||
+ fz_seek(ctx, file, 512 - (size % 512), 1); | ||
+ } | ||
+ | ||
+ if (typeflag != '0' && typeflag != '7' && typeflag != '\0') | ||
continue; | ||
|
||
+ blocks = (size + 511) / 512; | ||
+ fz_seek(ctx, file, blocks * 512, 1); | ||
+ | ||
tar->entries = fz_resize_array(ctx, tar->entries, tar->count + 1, sizeof *tar->entries); | ||
|
||
- tar->entries[tar->count].name = fz_strdup(ctx, name); | ||
tar->entries[tar->count].offset = offset; | ||
tar->entries[tar->count].size = size; | ||
+ if (longname != NULL) | ||
+ { | ||
+ tar->entries[tar->count].name = longname; | ||
+ longname = NULL; | ||
+ } | ||
+ else | ||
+ tar->entries[tar->count].name = fz_strdup(ctx, name); | ||
|
||
tar->count++; | ||
} | ||
@@ -174,18 +203,25 @@ static int count_tar_entries(fz_context *ctx, fz_archive *arch) | ||
int | ||
fz_is_tar_archive(fz_context *ctx, fz_stream *file) | ||
{ | ||
- const unsigned char signature[6] = { 'u', 's', 't', 'a', 'r', ' ' }; | ||
+ const unsigned char gnusignature[6] = { 'u', 's', 't', 'a', 'r', ' ' }; | ||
+ const unsigned char paxsignature[6] = { 'u', 's', 't', 'a', 'r', '\0' }; | ||
+ const unsigned char v7signature[6] = { '\0', '\0', '\0', '\0', '\0', '\0' }; | ||
unsigned char data[6]; | ||
size_t n; | ||
|
||
fz_seek(ctx, file, 257, 0); | ||
n = fz_read(ctx, file, data, nelem(data)); | ||
- if (n != nelem(signature)) | ||
- return 0; | ||
- if (memcmp(data, signature, nelem(signature))) | ||
+ if (n != nelem(data)) | ||
return 0; | ||
|
||
- return 1; | ||
+ if (!memcmp(data, gnusignature, nelem(gnusignature))) | ||
+ return 1; | ||
+ if (!memcmp(data, paxsignature, nelem(paxsignature))) | ||
+ return 1; | ||
+ if (!memcmp(data, v7signature, nelem(v7signature))) | ||
+ return 1; | ||
+ | ||
+ return 0; | ||
} | ||
|
||
fz_archive * |