summaryrefslogtreecommitdiff
path: root/source/xps/xps-zip.c
blob: 8b96773f9e4992109d17068d9fff2a4662b2c750 (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "mupdf/xps.h"

static void xps_init_document(xps_document *doc);

xps_part *
xps_new_part(xps_document *doc, char *name, unsigned char *data, int size)
{
	xps_part *part;

	part = fz_malloc_struct(doc->ctx, xps_part);
	fz_try(doc->ctx)
	{
		part->name = fz_strdup(doc->ctx, name);
		part->data = data;
		part->size = size;
	}
	fz_catch(doc->ctx)
	{
		fz_free(doc->ctx, part->name);
		fz_free(doc->ctx, part->data);
		fz_free(doc->ctx, part);
		fz_rethrow(doc->ctx);
	}

	return part;
}

void
xps_free_part(xps_document *doc, xps_part *part)
{
	fz_free(doc->ctx, part->name);
	fz_free(doc->ctx, part->data);
	fz_free(doc->ctx, part);
}

/*
 * Read and interleave split parts from a ZIP file.
 */
xps_part *
xps_read_part(xps_document *doc, char *partname)
{
	fz_context *ctx = doc->ctx;
	fz_archive *zip = doc->zip;
	fz_buffer *buf, *tmp;
	char path[2048];
	unsigned char *data;
	int size;
	int count;
	char *name;
	int seen_last;

	name = partname;
	if (name[0] == '/')
		name ++;

	/* All in one piece */
	if (fz_has_archive_entry(ctx, zip, name))
	{
		buf = fz_read_archive_entry(ctx, zip, name);
	}

	/* Assemble all the pieces */
	else
	{
		buf = fz_new_buffer(ctx, 512);
		seen_last = 0;
		for (count = 0; !seen_last; ++count)
		{
			sprintf(path, "%s/[%d].piece", name, count);
			if (fz_has_archive_entry(ctx, zip, path))
			{
				tmp = fz_read_archive_entry(ctx, zip, path);
				fz_buffer_cat(ctx, buf, tmp);
				fz_drop_buffer(ctx, tmp);
			}
			else
			{
				sprintf(path, "%s/[%d].last.piece", name, count);
				if (fz_has_archive_entry(ctx, zip, path))
				{
					tmp = fz_read_archive_entry(ctx, zip, path);
					fz_buffer_cat(ctx, buf, tmp);
					fz_drop_buffer(ctx, tmp);
					seen_last = 1;
				}
				else
				{
					fz_drop_buffer(ctx, buf);
					fz_throw(ctx, FZ_ERROR_GENERIC, "cannot find all pieces for part '%s'", partname);
				}
			}
		}
	}

	fz_write_buffer_byte(ctx, buf, 0); /* zero-terminate */

	/* take over the data */
	data = buf->data;
	/* size doesn't include the added zero-terminator */
	size = buf->len - 1;
	fz_free(ctx, buf);

	return xps_new_part(doc, partname, data, size);
}

int
xps_has_part(xps_document *doc, char *name)
{
	char buf[2048];
	if (name[0] == '/')
		name++;
	if (fz_has_archive_entry(doc->ctx, doc->zip, name))
		return 1;
	sprintf(buf, "%s/[0].piece", name);
	if (fz_has_archive_entry(doc->ctx, doc->zip, buf))
		return 1;
	sprintf(buf, "%s/[0].last.piece", name);
	if (fz_has_archive_entry(doc->ctx, doc->zip, buf))
		return 1;
	return 0;
}

static xps_document *
xps_open_document_with_directory(fz_context *ctx, const char *directory)
{
	xps_document *doc;

	doc = fz_malloc_struct(ctx, xps_document);
	xps_init_document(doc);
	doc->ctx = ctx;
	doc->zip = fz_open_directory(ctx, directory);

	fz_try(ctx)
	{
		xps_read_page_list(doc);
	}
	fz_catch(ctx)
	{
		xps_close_document(doc);
		fz_rethrow(ctx);
	}

	return doc;
}

xps_document *
xps_open_document_with_stream(fz_context *ctx, fz_stream *file)
{
	xps_document *doc;

	doc = fz_malloc_struct(ctx, xps_document);
	xps_init_document(doc);
	doc->ctx = ctx;

	fz_try(ctx)
	{
		doc->zip = fz_open_archive_with_stream(ctx, file);
		xps_read_page_list(doc);
	}
	fz_catch(ctx)
	{
		xps_close_document(doc);
		fz_rethrow(ctx);
	}

	return doc;
}

xps_document *
xps_open_document(fz_context *ctx, const char *filename)
{
	char buf[2048];
	fz_stream *file;
	char *p;
	xps_document *doc;

	if (strstr(filename, "/_rels/.rels") || strstr(filename, "\\_rels\\.rels"))
	{
		fz_strlcpy(buf, filename, sizeof buf);
		p = strstr(buf, "/_rels/.rels");
		if (!p)
			p = strstr(buf, "\\_rels\\.rels");
		*p = 0;
		return xps_open_document_with_directory(ctx, buf);
	}

	file = fz_open_file(ctx, filename);
	if (!file)
		fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));

	fz_try(ctx)
	{
		doc = xps_open_document_with_stream(ctx, file);
	}
	fz_always(ctx)
	{
		fz_close(file);
	}
	fz_catch(ctx)
	{
		fz_rethrow_message(ctx, "cannot load document '%s'", filename);
	}
	return doc;
}

void
xps_close_document(xps_document *doc)
{
	xps_font_cache *font, *next;

	if (!doc)
		return;

	if (doc->zip)
		fz_close_archive(doc->ctx, doc->zip);

	font = doc->font_table;
	while (font)
	{
		next = font->next;
		fz_drop_font(doc->ctx, font->font);
		fz_free(doc->ctx, font->name);
		fz_free(doc->ctx, font);
		font = next;
	}

	xps_free_page_list(doc);

	fz_free(doc->ctx, doc->start_part);
	fz_free(doc->ctx, doc);
}

static int
xps_meta(xps_document *doc, int key, void *ptr, int size)
{
	switch (key)
	{
	case FZ_META_FORMAT_INFO:
		sprintf((char *)ptr, "XPS");
		return FZ_META_OK;
	default:
		return FZ_META_UNKNOWN_KEY;
	}
}

static void
xps_rebind(xps_document *doc, fz_context *ctx)
{
	doc->ctx = ctx;
	fz_rebind_archive(doc->zip, ctx);
	fz_rebind_device(doc->dev, ctx);
}

static void
xps_init_document(xps_document *doc)
{
	doc->super.close = (fz_document_close_fn *)xps_close_document;
	doc->super.load_outline = (fz_document_load_outline_fn *)xps_load_outline;
	doc->super.count_pages = (fz_document_count_pages_fn *)xps_count_pages;
	doc->super.load_page = (fz_document_load_page_fn *)xps_load_page;
	doc->super.load_links = (fz_document_load_links_fn *)xps_load_links;
	doc->super.bound_page = (fz_document_bound_page_fn *)xps_bound_page;
	doc->super.run_page_contents = (fz_document_run_page_contents_fn *)xps_run_page;
	doc->super.free_page = (fz_document_free_page_fn *)xps_free_page;
	doc->super.meta = (fz_document_meta_fn *)xps_meta;
	doc->super.rebind = (fz_document_rebind_fn *)xps_rebind;
}