aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/src/iff.c
blob: 763d0c50c1c56218e4b3dcd4f35fde834387a38d (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* $Id$ */

/*
    libg3d - 3D object loading library

    Copyright (C) 2005-2009  Markus Dahms <mad@automagically.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <g3d/config.h>

#include <stdio.h>
#include <string.h>
#include <g3d/stream.h>
#include <g3d/read.h>
#include <g3d/iff.h>
#include <g3d/context.h>
#include <g3d/debug.h>

EAPI
gboolean g3d_iff_check(G3DStream *stream, guint32 *id, gsize *len)
{
	guint32 magic, form_bytes;

	magic = g3d_stream_read_int32_be(stream);
	if((magic != G3D_IFF_MKID('F','O','R','M')) &&
		(magic != G3D_IFF_MKID('F','O','R','4'))) {
		g_warning("IFF: %s is not a valid IFF stream", stream->uri);
		return FALSE;
	}
	form_bytes = g3d_stream_read_int32_be(stream);
	*id = g3d_stream_read_int32_be(stream);
	*len = form_bytes - 4;
	return TRUE;
}

#ifndef G3D_DISABLE_DEPRECATED
FILE *g3d_iff_open(const gchar *filename, guint32 *id, guint32 *len)
{
	FILE *f;
	guint32 form_bytes, magic;

	f = fopen(filename, "r");
	if(f == NULL)
	{
		g_critical("can't open file '%s'", filename);
		return NULL;
	}

	magic = g3d_read_int32_be(f);
	if((magic != G3D_IFF_MKID('F','O','R','M')) &&
		(magic != G3D_IFF_MKID('F','O','R','4')))
	{
		g_critical("file %s is not an IFF file", filename);
		fclose(f);
		return NULL;
	}

	form_bytes = g3d_read_int32_be(f);
	*id = g3d_read_int32_be(f);
	form_bytes -= 4;
	*len = form_bytes;

	return f;
}
#endif /* !G3D_DISABLE_DEPRECATED */

EAPI
gsize g3d_iff_read_chunk(G3DStream *stream, guint32 *id, gsize *len,
	guint32 flags)
{
	*id = g3d_stream_read_int32_be(stream);
	if(flags & G3D_IFF_LEN16) {
		*len = (flags & G3D_IFF_LE) ?
			g3d_stream_read_int16_le(stream) :
			g3d_stream_read_int16_be(stream);
		return 6 + *len + (*len % 2);
	} else {
		*len = (flags & G3D_IFF_LE) ?
			g3d_stream_read_int32_le(stream) :
			g3d_stream_read_int32_be(stream);
		return 8 + *len + (*len % 2);
	}
}

#ifndef G3D_DISABLE_DEPRECATED
int g3d_iff_readchunk(FILE *f, guint32 *id, guint32 *len, guint32 flags)
{
	*id = g3d_read_int32_be(f);
	if(flags & G3D_IFF_LEN16)
	{
		*len = (flags & G3D_IFF_LE) ?
			g3d_read_int16_le(f) : g3d_read_int16_be(f);
		return 6 + *len + (*len % 2);
	}
	else
	{
		*len = (flags & G3D_IFF_LE) ?
			g3d_read_int32_le(f) : g3d_read_int32_be(f);
		return 8 + *len + (*len % 2);
	}
}
#endif /* G3D_DISABLE_DEPRECATED */

EAPI
gchar *g3d_iff_id_to_text(guint32 id)
{
	gchar *tid;

	tid = g_new0(gchar, 5);

	tid[0] = (id >> 24) & 0xFF;
	tid[1] = (id >> 16) & 0xFF;
	tid[2] = (id >> 8) & 0xFF;
	tid[3] = id & 0xFF;

	return tid;
}

EAPI
gboolean g3d_iff_chunk_matches(guint32 id, gchar *tid)
{
	if(((id >> 24) & 0xFF) != tid[0]) return FALSE;
	if(((id >> 16) & 0xFF) != tid[1]) return FALSE;
	if(((id >> 8) & 0xFF) != tid[2]) return FALSE;
	return (id & 0xFF) == tid[3];
}

EAPI
G3DIffChunkInfo *g3d_iff_get_chunk_info(G3DIffChunkInfo *chunks,
	guint32 chunk_id)
{
	guint32 i = 0;

	while(chunks[i].id && !g3d_iff_chunk_matches(chunk_id, chunks[i].id))
		i ++;

	if(chunks[i].id)
		return &(chunks[i]);

	return NULL;
}

EAPI
void g3d_iff_debug_chunk(G3DIffChunkInfo *info, guint32 chunk_id,
	guint32 chunk_len, gchar chunk_type, guint32 pos, guint32 level)
{
	gchar *tid;
	gchar *padding = "                                   ";

	tid = g3d_iff_id_to_text(chunk_id);
	g_debug("\\%s(%d)[%s][%c%c%c] %s (%d) @ 0x%08x - %d bytes left",
		padding + (strlen(padding) - level), level,
		tid,
		chunk_type,
		info->container ? 'c' : ' ',
		info->callback ? 'f' : ' ',
		info->description,
		chunk_len,
		pos,
		chunk_len);
	g_free(tid);
}

static goffset g3d_iff_pos(G3DIffGlobal *global)
{
	if(global->stream)
		return g3d_stream_tell(global->stream);
	else
#ifndef G_DISABLE_DEPRECATED
		return ftell(global->f);
#else
		return -1;
#endif
}

EAPI
gpointer g3d_iff_handle_chunk(G3DIffGlobal *global, G3DIffLocal *plocal,
	G3DIffChunkInfo *chunks, guint32 flags)
{
	gpointer object = NULL;
	guint32 chunk_id;
	gsize chunk_len;
	G3DIffLocal *sublocal;
	G3DIffChunkInfo *info;

	/* read info for one chunk */
	if(global->stream)
		g3d_iff_read_chunk(global->stream, &chunk_id, &chunk_len, 0);
	else
#ifndef G3D_DISABLE_DEPRECATED
	{
		guint32 chunk_len32;
		g3d_iff_readchunk(global->f, &chunk_id, &chunk_len32, 0);
		chunk_len = chunk_len32;
	}
#else
		return NULL;
#endif

	plocal->nb -= 8;

	sublocal = g_new0(G3DIffLocal, 1);
	sublocal->parent_id = plocal->id;
	sublocal->id = chunk_id;
	sublocal->object = plocal->object;
	sublocal->level = plocal->level + 1;
	sublocal->nb = ((guint32)chunk_len);
	plocal->nb -= sublocal->nb;

	/* call function */
	info = g3d_iff_get_chunk_info(chunks, chunk_id);
	if(info)
	{
		g3d_iff_debug_chunk(info, chunk_id, chunk_len, 'X',
			(guint32)g3d_iff_pos(global) - 8, plocal->level);

		if(info->callback)
			info->callback(global, sublocal);

		if(sublocal->nb > 0) {
			if(global->stream) {
				g3d_stream_skip(global->stream, sublocal->nb);
			} else {
#ifndef G_DISABLE_DEPRECATED
				fseek(global->f, sublocal->nb, SEEK_CUR);
#else
				return NULL;
#endif
			}
		}
	}

	object = sublocal->level_object;
	g_free(sublocal);

	return object;
}

EAPI
gboolean g3d_iff_read_ctnr(G3DIffGlobal *global, G3DIffLocal *local,
	G3DIffChunkInfo *chunks, guint32 flags)
{
	G3DIffLocal *sublocal;
	G3DIffChunkInfo *info;
	guint32 chunk_id, chunk_mod, chunk_type;
	gsize chunk_len;
	gchar *tid;
	gpointer level_object;
#ifndef G3D_DISABLE_DEPRECATED
	long int fpos;
#endif

	level_object = NULL;

#ifndef G3D_DISABLE_DEPRECATED
	if(global->max_fpos == 0)
		global->max_fpos = local->nb + 12;
#endif
	while(local->nb >= ((flags & G3D_IFF_LEN16) ? 6 : 8))
	{
		chunk_id = 0;

		if(global->stream)
			g3d_iff_read_chunk(global->stream, &chunk_id, &chunk_len, flags);
		else
#ifndef G3D_DISABLE_DEPRECATED
		{
			guint32 chunk_len32 = ((guint32)chunk_len);
			g3d_iff_readchunk(global->f, &chunk_id, &chunk_len32, flags);
			chunk_len = chunk_len32;
		}
#else
			return FALSE;
#endif
		local->nb -= ((flags & G3D_IFF_LEN16) ? 6 : 8);

		chunk_mod = flags & 0x0F;
		if(chunk_mod == 0) {
			g_warning("[IFF] mod = 0 (flags: 0x%02X\n)", flags);
			chunk_mod = 2;
		}
		chunk_type = ' ';

		/* handle special chunks */
		switch(chunk_id)
		{
			case 0:
			case 0xFFFFFFFF:
				g_warning(
					"[IFF] got invalid ID, skipping %d bytes @ 0x%08x",
					local->nb, (guint32)g3d_iff_pos(global));

				/* skip rest of parent chunk */
				if(local->nb > 0)
				{
					fseek(global->f, local->nb, SEEK_CUR);
					local->nb = 0;
				}
				return FALSE;
				break;

			case G3D_IFF_MKID('F','O','R','4'):
				if(global->stream)
					chunk_id = g3d_stream_read_int32_be(global->stream);
				else
#ifndef G3D_DISABLE_DEPRECATED
					chunk_id = g3d_read_int32_be(global->f);
#else
					return FALSE;
#endif
				chunk_len -= 4;
				chunk_mod = 4;
				chunk_type = 'F';
				local->nb -= 4;
				break;

			case G3D_IFF_MKID('L','I','S','4'):
				if(global->stream)
					chunk_id = g3d_stream_read_int32_be(global->stream);
				else
#ifndef G3D_DISABLE_DEPRECATED
					chunk_id = g3d_read_int32_be(global->f);
#else
					return FALSE;
#endif
				chunk_len -= 4;
				chunk_mod = 4;
				chunk_type = 'L';
				local->nb -= 4;
				break;

			default:
				break;
		}

		info = g3d_iff_get_chunk_info(chunks, chunk_id);

		if(info) {
			g3d_iff_debug_chunk(info, chunk_id, chunk_len, chunk_type,
				(guint32)g3d_iff_pos(global) - ((chunk_type == ' ') ? 8 : 12),
				local->level);

			sublocal = g_new0(G3DIffLocal, 1);
			sublocal->parent_id = local->id;
			sublocal->id = chunk_id;
			sublocal->object = local->object;
			sublocal->level = local->level + 1;
			sublocal->level_object = level_object;
			sublocal->nb = ((guint32)chunk_len);

			if(info->callback)
				info->callback(global, sublocal);

			if(info->container) {
				/* LWO has 16 bit length in subchunks */
				if(flags & G3D_IFF_SUBCHUNK_LEN16)
					g3d_iff_read_ctnr(global, sublocal, chunks,
						flags | G3D_IFF_LEN16);
				else
					g3d_iff_read_ctnr(global, sublocal, chunks, flags);
			}

			if(info->container && info->callback) {
				sublocal->finalize = TRUE;
				info->callback(global, sublocal);
			}

			if(sublocal->nb > 0) {
				if(global->stream) {
					g3d_stream_skip(global->stream, sublocal->nb);
				} else {
#ifndef G3D_DISABLE_DEPRECATED
					fseek(global->f, sublocal->nb, SEEK_CUR);
#else
					return FALSE;
#endif
				}
			}
			level_object = sublocal->level_object;

			g_free(sublocal);
		} else {
			tid = g3d_iff_id_to_text(chunk_id);
			g_warning("[IFF] unknown chunk type \"%s\" (%d) @ 0x%08x",
				tid, (guint32)chunk_len, (guint32)g3d_iff_pos(global) - 8);
			g_free(tid);
			if(global->stream)
				g3d_stream_skip(global->stream, chunk_len);
			else
#ifndef G3D_DISABLE_DEPRECATED
				fseek(global->f, chunk_len, SEEK_CUR);
#else
				return FALSE;
#endif
		}

		local->nb -= chunk_len;

		if(chunk_len % chunk_mod) {
			if(global->stream)
				g3d_stream_skip(global->stream,
					chunk_mod - (chunk_len % chunk_mod));
			else
#ifndef G3D_DISABLE_DEPRECATED
				fseek(global->f,
					chunk_mod - (chunk_len % chunk_mod), SEEK_CUR);
#else
				return FALSE;
#endif
			local->nb -= (chunk_mod - (chunk_len % chunk_mod));
		}

		if(global->stream) {
			g3d_context_update_progress_bar(global->context,
				(G3DFloat)g3d_stream_tell(global->stream) /
				(G3DFloat)g3d_stream_size(global->stream), TRUE);
		}
#ifndef G3D_DISABLE_DEPRECATED
		else {
			fpos = ftell(global->f);
			g3d_context_update_progress_bar(global->context,
				((G3DFloat)fpos / (G3DFloat)global->max_fpos), TRUE);
		}
#endif
	} /* nb >= 8/6 */

	if(local->nb > 0)
	{
		g_warning("[IFF] skipping %d bytes at the end of chunk",
			local->nb);
		if(global->stream) {
			g3d_stream_skip(global->stream, local->nb);
		} else {
#ifndef G3D_DISABLE_DEPRECATED
			fseek(global->f, local->nb, SEEK_CUR);
#else
			return FALSE;
#endif
		}
		local->nb = 0;
	}

	return TRUE;
}