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
|
/*
* Copyright (c) 2020-2023, yzrh <yzrh@noema.org>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "version.h"
#include "md5.h"
#include "pdf.h"
static int
_info_obj(pdf_object_t **pdf)
{
char dictionary[128] = "<<\n"
"/Producer (Melon " VERSION "." RELEASE "." PATCH EXTRA ")\n"
"/CreationDate (D:";
char buf[64];
time_t timestamp = time(NULL);
strftime(buf, 64, "%Y%m%d%H%M%S", gmtime(×tamp));
strcat(dictionary, buf);
strcat(dictionary, "+00'00')\n>>");
return pdf_obj_append(pdf, 0, NULL, dictionary, NULL, 0);
}
int
pdf_dump_obj(pdf_object_t **pdf, FILE **fp)
{
if (*pdf == NULL || *fp == NULL || _info_obj(pdf) != 0)
return 1;
long cur;
pdf_object_t *ptr = (*pdf)->next;
while (ptr != NULL) {
ptr->address = cur = ftell(*fp);
fprintf(*fp, "%d 0 obj\n", ptr->id);
if (ptr->dictionary != NULL) {
fwrite(ptr->dictionary, ptr->dictionary_size, 1, *fp);
fputs("\n", *fp);
} else if (ptr->object != NULL) {
fwrite(ptr->object, ptr->object_size, 1, *fp);
fputs("\n", *fp);
} else if (ptr->stream == NULL) {
fputs("null\n", *fp);
}
if (ptr->stream != NULL) {
fputs("stream\r\n", *fp);
fwrite(ptr->stream, ptr->stream_size, 1, *fp);
fputs("endstream\n", *fp);
}
fputs("endobj\n", *fp);
ptr->size = ftell(*fp) - cur;
ptr = ptr->next;
}
return 0;
}
int
pdf_dump_header(pdf_object_t **pdf, FILE **fp)
{
if (*pdf == NULL || *fp == NULL)
return 1;
fputs("%PDF-1.7\n", *fp);
const unsigned char bin[4] = {
0xf6,
0xe4,
0xfc,
0xdf,
};
fputs("%", *fp);
fwrite(bin, 4, 1, *fp);
fputs("\n", *fp);
return 0;
}
int
pdf_dump_xref(pdf_object_t **pdf, FILE **fp)
{
if (*pdf == NULL || *fp == NULL)
return 1;
fputs("xref\n", *fp);
pdf_object_t *ptr = *pdf;
pdf_object_t *start = ptr;
int count = 1;
while (ptr != NULL) {
if (ptr->next == NULL ||
(ptr->next != NULL && ptr->next->id != ptr->id + 1)) {
fprintf(*fp, "%d %d\n", start->id, count);
for (; count > 0; count--) {
fprintf(*fp, "%010d %05d %s\r\n",
start->address,
start->address > 0 ? 0 : 65535,
start->size > 0 ? "n" : "f");
start = start->next;
}
if (ptr->next != NULL)
start = ptr->next;
}
ptr = ptr->next;
count++;
}
return 0;
}
int
pdf_dump_trailer(pdf_object_t **pdf, FILE **fp, int xref)
{
if (*pdf == NULL || *fp == NULL)
return 1;
fputs("trailer\n", *fp);
fputs("<<\n", *fp);
/*
* File identifiers should be generated using
* (a) Current time
* (b) File path
* (c) Size of file
* (d) Values of all entries in the
* file's document information dictionary
*
* It is recommended to be computed according to RFC 1321
*/
time_t timestamp = time(NULL);
int size = pdf_get_size(pdf);
int buf_size;
char buf[64];
#ifdef __ILP32__
buf_size = snprintf(buf, 64, "%x%x", timestamp, size);
#else
buf_size = snprintf(buf, 64, "%lx%x", timestamp, size);
#endif
int fid_size;
unsigned char *fid;
if (strmd5(&fid, &fid_size, (unsigned char *) buf, buf_size) != 0)
return 1;
pdf_object_t *ptr = *pdf;
while (ptr->next != NULL)
ptr = ptr->next;
fprintf(*fp,
"/Size %d\n/Root %d 0 R\n/Info %d 0 R\n",
ptr->id + 1,
pdf_get_catalog_id(pdf),
ptr->id);
fputs("/ID [", *fp);
for (int i = 0; i < 2; i++) {
fputs("<", *fp);
for (int j = 0; j < fid_size; j++)
fprintf(*fp, "%02x", fid[j]);
fputs(">", *fp);
if (i < 1)
fputs(" ", *fp);
}
fputs("]\n", *fp);
fputs(">>\n", *fp);
fputs("startxref\n", *fp);
fprintf(*fp, "%d\n", xref);
fputs("%%EOF\n", *fp);
free(fid);
return 0;
}
|