summaryrefslogtreecommitdiff
path: root/samus/sa_pack.c
blob: e1f910ba9d3806ce9c662b37c00e5a6c55d5137b (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
/*
 * Metro physical packages and parts, mapped to a zip archive.
 */

#include "fitz.h"
#include "samus.h"

struct sa_package_s
{
	sa_zip *zip;
	fz_obj *defaults;
	fz_obj *overrides;
};

static fz_error *
readcontenttypes(sa_package *pack)
{
	fz_error *error;
	sa_xmlparser *parser;
	sa_xmlitem *item;
	fz_obj *val;

	error = fz_newdict(&pack->defaults, 8);
	if (error)
		return error;

	error = fz_newdict(&pack->overrides, 8);
	if (error)
		return error;

	error = sa_openzipentry(pack->zip, "[Content_Types].xml");
	if (error)
		return error;

	error = sa_openxml(&parser, pack->zip->file, 0);
	if (error)
		goto cleanupzip;

	item = sa_xmlnext(parser);
	if (item && !strcmp(sa_xmlname(item), "Types"))
	{
		sa_xmldown(parser);
		item = sa_xmlnext(parser);
		while (item)
		{
			if (!strcmp(sa_xmlname(item), "Default"))
			{
				char *ext = sa_xmlatt(item, "Extension");
				char *type = sa_xmlatt(item, "ContentType");
				if (ext && type)
				{
					if (strstr(type, ';'))
						strstr(type, ';')[0] = 0;
					error = fz_newname(&val, type);
					if (error)
						goto cleanupxml;
					error = fz_dictputs(pack->defaults, ext, val);
					if (error)
						goto cleanupval;
					val = nil;
				}
			}

			if (!strcmp(sa_xmlname(item), "Override"))
			{
				char *name = sa_xmlatt(item, "PartName");
				char *type = sa_xmlatt(item, "ContentType");
				if (name && type)
				{
					if (strstr(type, ';'))
						strstr(type, ';')[0] = 0;
					error = fz_newname(&val, type);
					if (error)
						goto cleanupxml;
					error = fz_dictputs(pack->overrides, name, val);
					if (error)
						goto cleanupval;
					val = nil;
				}
			}

			item = sa_xmlnext(parser);
		}
		sa_xmlup(parser);
	}

	sa_closexml(parser);
	sa_closezipentry(pack->zip);
	return nil;

cleanupval:
	fz_dropobj(val);
cleanupxml:
	sa_closexml(parser);
cleanupzip:
	sa_closezipentry(pack->zip);
	return error;
}

fz_error *
sa_openpackage(sa_package **packp, char *filename)
{
	fz_error *error;
	sa_package *pack;

	pack = fz_malloc(sizeof(sa_package));
	if (!pack)
		return fz_outofmem;

	pack->zip = nil;
	pack->defaults = nil;
	pack->overrides = nil;

	error = sa_openzip(&pack->zip, filename);
	if (error)
	{
		sa_closepackage(pack);
		return error;
	}

	error = readcontenttypes(pack->zip);
	if (error)
	{
		sa_closepackage(pack);
		return error;
	}

	*packp = pack;
	return nil;
}

void
sa_closepackage(sa_package *pack)
{
	if (pack->zip) sa_closezip(pack->zip);
	if (pack->defaults) fz_dropobj(pack->defaults);
	if (pack->overrides) fz_dropobj(pack->overrides);
	fz_free(pack);
}

/*
 * Check access of a part, return either nil or its mime-type.
 */
char *
sa_accesspart(sa_package *pack, char *partname)
{
	fz_obj *type;
	char *ext;

	if (sa_accesszipentry(pack->zip, partname))
	{
		type = fz_dictgets(pack->overrides, partname);
		if (type)
			return fz_toname(type);

		ext = strrstr(partname, ".");
		if (ext)
		{
			type = fz_dictgets(pack->defaults, ext + 1);
			if (type)
				return fz_toname(type);
		}
	}

	return nil;
}

/*
 * Open a part for reading. It is NOT safe to open more than one
 * part at a time.
 */
fz_error *
sa_openpart(fz_file **filep, sa_package *pack, char *partname)
{
	*filep = pack->zip->file;
	return sa_openzipentry(pack->zip, partname);
}

/*
 * Call this instead of fz_closefile()
 * FIXME i gotto do something about this icky file API
 */
void sa_closepart(sa_package *pack, fz_file *file)
{
	sa_closezipentry(pack->zip);
}