aboutsummaryrefslogtreecommitdiffstats
path: root/src/pdf_parser.c
blob: 781bafa9f03438597775e6ea67ca314e30a9d1e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (c) 2020-2023, yzrh <yzrh@noema.org>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifdef __linux__

#define _GNU_SOURCE

#endif /* __linux__ */

#include <stdlib.h>
#include <string.h>

#include "pdf.h"

static void *
_memmem_whitespace(const void *p0, size_t s0, const void *p1, size_t s1)
{
	const char whitespace[6] = {
		0x00,
		0x09,
		0x0a,
		0x0c,
		0x0d,
		0x20
	};

	char *ret = NULL;

	char str[s1 + 1];
	memcpy(str, p1, s1);

	size_t tmp_size = 0;
	char *tmp;

	for (int i = 0; i < 6; i++) {
		str[s1] = whitespace[i];

		if ((tmp = memmem(p0, s0, str, s1 + 1)) == NULL)
			continue;

		if (tmp_size == 0 || (size_t) (tmp - (char *) p0) < tmp_size) {
			tmp_size = tmp - (char *) p0;
			ret = tmp;
		}
	}

	return ret;
}

static int
_locate(pdf_object_t **pdf, FILE **fp, int size_buf)
{
	pdf_object_t *ptr = *pdf;
	while (ptr->next != NULL)
		ptr = ptr->next;

	char buf[size_buf];

	long cur = ftell(*fp);
	long end;

	fseek(*fp, 0, SEEK_END);
	end = ftell(*fp);
	fseek(*fp, cur, SEEK_SET);

	long head = 0;
	long tail = 0;
	char *pos;
	char *tmp;

	for (;;) {
		if (cur + size_buf < end) {
			fread(buf, size_buf, 1, *fp);
		} else {
			fread(buf, end - cur, 1, *fp);
			memset(buf + end - cur, 0, size_buf - end + cur);
		}

		if (head == 0 && (pos = _memmem_whitespace(buf, size_buf, " 0 obj", 6)) != NULL)
			head = cur + (pos - buf) + 7;

		if (tail == 0 && (pos = _memmem_whitespace(buf, size_buf, "endobj", 6)) != NULL) {
			/* We need to check if it is the object stored in stream */
			while (memcmp(pos + 7,
				"\r\nendstream", 11) == 0 &&
				(tmp = _memmem_whitespace(pos + 7,
				size_buf - (pos - buf) - 7,
				"endobj", 6)) != NULL)
					pos = tmp;

			if (pos - buf < size_buf - 7)
				tail = cur + (pos - buf);
		}

		if (tail > head) {
			if (ptr->next == NULL) {
				ptr->next = malloc(sizeof(pdf_object_t));

				if (ptr->next == NULL)
					return 1;

				ptr->next->id = 0;
				ptr->next->object_size = 0;
				ptr->next->object = NULL;
				ptr->next->dictionary_size = 0;
				ptr->next->dictionary = NULL;
				ptr->next->stream_size = 0;
				ptr->next->stream = NULL;
				ptr->next->next = NULL;
				ptr = ptr->next;
			}

			ptr->address = head;
			ptr->size = tail - head;

			fseek(*fp, tail + 7, SEEK_SET);
			head = tail = 0;
		} else if (head > 0 && tail > 0) {
			if (cur + size_buf < end)
				fseek(*fp, head, SEEK_SET);
			tail = 0;
		} else {
			fseek(*fp, -7, SEEK_CUR);
		}

		if ((cur = ftell(*fp)) + 7 >= end)
			break;
	}

	return 0;
}

int
pdf_load(pdf_object_t **pdf, FILE **fp, int size_buf)
{
	if (*pdf == NULL || *fp == NULL || size_buf < 7)
		return 1;

	if (_locate(pdf, fp, size_buf) != 0)
		return 1;

	pdf_object_t *ptr = (*pdf)->next;

	char str[8];
	char *buf;
	char *head;
	char *tail;
	char *tmp;

	while (ptr != NULL) {
		buf = malloc(ptr->size);

		if (buf == NULL)
			return 1;

		fseek(*fp, ptr->address - 15, SEEK_SET);
		fread(str, 8, 1, *fp);

		for (int i = 7; i >= 0; i--) {
			if (str[i] < '0' || str[i] > '9') {
				if (i < 7)
					ptr->id = atoi(str + i + 1);
				else
					ptr->id = 0;

				break;
			}
		}

		fseek(*fp, ptr->address, SEEK_SET);
		fread(buf, ptr->size, 1, *fp);

		if ((head = memmem(buf, ptr->size, "<<", 2)) != NULL &&
			(tail = _memmem_whitespace(buf, ptr->size, ">>", 2)) != NULL) {
			/*
			 * A dictionary object may have nested dictionary,
			 * but it should not be in a stream
			 */
			while ((tmp = _memmem_whitespace(tail + 3,
				ptr->size - (tail - buf) - 3,
				">>", 2)) != NULL &&
				memmem(tail + 3,
				ptr->size - (tail - buf) - 3,
				"stream\r\n", 8) == NULL)
				tail = tmp;

			ptr->dictionary_size = tail - head + 2;
			ptr->dictionary = malloc(ptr->dictionary_size + 1);

			if (ptr->dictionary == NULL)
				return 1;

			memcpy(ptr->dictionary, head, ptr->dictionary_size);
			memset(ptr->dictionary + ptr->dictionary_size, 0, 1);

			if ((head = memmem(tail,
				ptr->size - (tail - buf),
				"stream\r\n", 8)) != NULL &&
				(tail = _memmem_whitespace(head,
				ptr->size - (head - buf),
				"endstream", 9)) != NULL) {
				/*
				 * An object may contain a stream that
				 * contains another object that
				 * contains another stream
				 */
				while (_memmem_whitespace(tail + 10,
					ptr->size - (tail - buf) - 10,
					"endobj", 6) != NULL &&
					(tmp = _memmem_whitespace(tail + 10,
					ptr->size - (tail - buf) - 10,
					"endstream", 9)) != NULL)
						tail = tmp;

				ptr->stream_size = (tail - head) - 8;
				ptr->stream = malloc(ptr->stream_size);

				if (ptr->stream == NULL)
					return 1;

				memcpy(ptr->stream, head + 8, ptr->stream_size);
			}

			free(buf);
		} else {
			ptr->object_size = ptr->size;
			ptr->object = buf;
		}

		ptr = ptr->next;
	}

	return 0;
}