Handle binary data in dictionary.

This commit is contained in:
yzrh 2020-12-31 18:58:52 +00:00
parent 3bd7ea7520
commit b20c6ad3ed
3 changed files with 31 additions and 19 deletions

View file

@ -110,7 +110,7 @@ cnki_pdf(cnki_t **param)
strcat(dictionary, " ");
}
snprintf(buf, 64,
"]\n/Count %d\n>>\n",
"]\n/Count %d\n>>",
pdf_get_kid_count(&pdf, parent[i]));
strcat(dictionary, buf);
@ -198,7 +198,7 @@ cnki_pdf(cnki_t **param)
snprintf(buf, 64, "/Count %d\n", (*param)->file_stat->page);
strcat(dictionary, buf);
strcat(dictionary, ">>\n");
strcat(dictionary, ">>");
pdf_obj_prepend(&pdf, root, NULL, dictionary, NULL);
@ -238,10 +238,10 @@ cnki_pdf(cnki_t **param)
if (catalog != 0) {
if ((*param)->stat > 0)
printf("catalog object is %d.\n", catalog);
printf("Catalog object is %d.\n", catalog);
} else {
if ((*param)->stat > 0)
printf("catalog object is missing\n");
printf("Catalog object is missing\n");
if ((*param)->stat > 1)
printf("Generating catalog object\n");
@ -258,7 +258,7 @@ cnki_pdf(cnki_t **param)
strcat(dictionary, buf);
}
strcat(dictionary, ">>\n");
strcat(dictionary, ">>");
pdf_obj_append(&pdf, 0, NULL, dictionary, NULL);
@ -273,7 +273,7 @@ cnki_pdf(cnki_t **param)
if (xref != 0) {
if ((*param)->stat > 0)
printf("xref object is %d.\n", xref);
printf("Xref object is %d.\n", xref);
if ((*param)->stat > 1)
printf("Deleting xref object\n");
@ -284,7 +284,7 @@ cnki_pdf(cnki_t **param)
printf("Deleted xref object\n");
} else {
if ((*param)->stat > 0)
printf("xref object is missing\n");
printf("Xref object is missing\n");
}
free(dictionary);

View file

@ -84,24 +84,24 @@ pdf_obj_add(pdf_object_t **pdf, int id,
(*pdf)->id = id;
if (dictionary != NULL) {
(*pdf)->dictionary_size = strlen(dictionary) + 1;
(*pdf)->dictionary_size = strlen(dictionary);
(*pdf)->dictionary = malloc((*pdf)->dictionary_size);
if ((*pdf)->dictionary == NULL)
return 1;
strncpy((*pdf)->dictionary, dictionary, (*pdf)->dictionary_size);
memcpy((*pdf)->dictionary, dictionary, (*pdf)->dictionary_size);
(*pdf)->object_size = 0;
(*pdf)->object = NULL;
} else if (object != NULL) {
(*pdf)->object_size = strlen(object) + 1;
(*pdf)->object_size = strlen(object);
(*pdf)->object = malloc((*pdf)->object_size);
if ((*pdf)->object == NULL)
return 1;
strncpy((*pdf)->object, object, (*pdf)->object_size);
memcpy((*pdf)->object, object, (*pdf)->object_size);
(*pdf)->dictionary_size = 0;
(*pdf)->dictionary = NULL;

View file

@ -4,6 +4,12 @@
* SPDX-License-Identifier: Apache-2.0
*/
#ifdef __linux__
#define _GNU_SOURCE
#endif /* __linux__ */
#include <stdlib.h>
#include <string.h>
@ -152,7 +158,8 @@ pdf_get_catalog_id(pdf_object_t **pdf)
while (ptr != NULL) {
if (ptr->dictionary != NULL &&
strstr(ptr->dictionary, "/Catalog") != NULL)
memmem(ptr->dictionary, ptr->dictionary_size,
"/Catalog", 8) != NULL)
catalog_id = ptr->id;
ptr = ptr->next;
@ -173,7 +180,8 @@ pdf_get_xref_id(pdf_object_t **pdf)
while (ptr != NULL) {
if (ptr->dictionary != NULL &&
strstr(ptr->dictionary, "/XRef") != NULL)
memmem(ptr->dictionary, ptr->dictionary_size,
"/XRef", 5) != NULL)
xref_id = ptr->id;
ptr = ptr->next;
@ -208,10 +216,11 @@ pdf_get_parent_id(pdf_object_t **pdf, int **id)
while (ptr != NULL) {
if (ptr->dictionary != NULL &&
(head = strstr(ptr->dictionary, "/Parent ")) != NULL &&
(head = memmem(ptr->dictionary, ptr->dictionary_size,
"/Parent ", 8)) != NULL &&
(tail = strchr(head + 8, ' ')) != NULL) {
memset(str, 0, 8);
strncpy(str, head + 8, (tail - head) - 8);
memcpy(str, head + 8, (tail - head) - 8);
str_val = atoi(str);
if (!_id_in(str_val, *id)) {
@ -258,7 +267,8 @@ pdf_get_kid_id(pdf_object_t **pdf, int id, int **kid)
}
if (ptr->dictionary != NULL &&
strstr(ptr->dictionary, str) != NULL) {
memmem(ptr->dictionary, ptr->dictionary_size,
str, strlen(str)) != NULL) {
ret = realloc(*kid, ++kid_size * sizeof(int));
if (ret == NULL)
@ -297,13 +307,15 @@ pdf_get_kid_count(pdf_object_t **pdf, int id)
while (ptr != NULL) {
if (ptr->dictionary != NULL &&
strstr(ptr->dictionary, id_str) != NULL &&
(pos = strstr(ptr->dictionary, "/Count ")) != NULL) {
memmem(ptr->dictionary, ptr->dictionary_size,
id_str, strlen(id_str)) != NULL &&
(pos = memmem(ptr->dictionary, ptr->dictionary_size,
"/Count ", 7)) != NULL) {
for (int i = 8; i >= 0; i--) {
if (i + 7 <= ptr->dictionary_size - (pos - ptr->dictionary) &&
pos[i + 7] >= '0' && pos[i + 7] <= '9') {
memset(str, 0, 8);
strncpy(str, pos + 7, i + 1);
memcpy(str, pos + 7, i + 1);
str_val = atoi(str);
count += str_val;
break;