File: rsrc.c

package info (click to toggle)
hfsutils 3.2.6-16
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,680 kB
  • sloc: ansic: 12,858; tcl: 1,937; makefile: 566; sh: 156; perl: 29
file content (494 lines) | stat: -rw-r--r-- 10,107 bytes parent folder | download | duplicates (7)
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * librsrc - library for reading and writing Macintosh resources
 * Copyright (C) 1996-1998 Robert Leslie
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: rsrc.c,v 1.7 1998/11/02 22:09:15 rob Exp $
 */

# ifdef HAVE_CONFIG_H
#  include "config.h"
# endif

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

# include "librsrc.h"
# include "data.h"

const char *rsrc_error = "no error";	/* static error string */

/*
 * NAME:	rsrc->init()
 * DESCRIPTION:	initialize access to a resource file
 */
rsrcfile *rsrc_init(void *priv, const struct rsrcprocs *procs)
{
  rsrcfile *rfile;
  byte head[16];
  const byte *ptr = head;
  unsigned long nbytes;
  unsigned short tlistoff, nlistoff;

  rfile = ALLOC(rsrcfile, 1);
  if (rfile == 0)
    ERROR(ENOMEM, 0);

  rfile->priv     = priv;
  rfile->procs    = *procs;

  rfile->map.data = 0;

  if (rfile->procs.seek(rfile->priv, 0, RSRC_SEEK_SET) == (unsigned long) -1)
    ERROR(errno, "error seeking resource header");

  nbytes = rfile->procs.read(rfile->priv, head, sizeof(head));
  if (nbytes != sizeof(head))
    {
      if (nbytes == (unsigned long) -1)
	ERROR(errno, "error reading resource header");
      else
	ERROR(EINVAL, "truncated resource header");
    }

  d_fetchul(&ptr, &rfile->hdr.dstart);
  d_fetchul(&ptr, &rfile->hdr.mstart);

  d_fetchul(&ptr, &rfile->hdr.dlen);
  d_fetchul(&ptr, &rfile->hdr.mlen);

  rfile->map.data = ALLOC(byte, rfile->hdr.mlen);
  if (rfile->map.data == 0)
    ERROR(ENOMEM, 0);

  if (rfile->procs.seek(rfile->priv, rfile->hdr.mstart, RSRC_SEEK_SET) ==
      (unsigned long) -1)
    ERROR(errno, "error seeking resource map");

  nbytes = rfile->procs.read(rfile->priv, rfile->map.data, rfile->hdr.mlen);
  if (nbytes != rfile->hdr.mlen)
    {
      if (nbytes == (unsigned long) -1)
	ERROR(errno, "error reading resource map");
      else
	ERROR(EIO, "truncated resource map");
    }

  memcpy(rfile->map.data, head, sizeof(head));

  ptr = rfile->map.data + 22;

  d_fetchuw(&ptr, &rfile->map.attrs);

  d_fetchuw(&ptr, &tlistoff);
  d_fetchuw(&ptr, &nlistoff);

  rfile->map.tlist = rfile->map.data + tlistoff;
  rfile->map.nlist = rfile->map.data + nlistoff;

  return rfile;

fail:
  if (rfile)
    FREE(rfile->map.data);

  FREE(rfile);

  return 0;
}

/*
 * NAME:	rsrc->finish()
 * DESCRIPTION:	terminate access to a resource file
 */
int rsrc_finish(rsrcfile *rfile)
{
  FREE(rfile->map.data);
  FREE(rfile);

  return 0;
}

/*
 * NAME:	findtype()
 * DESCRIPTION:	locate an item in a resource type list
 */
static
const byte *findtype(rsrcmap *map, const char *type)
{
  const byte *ptr;
  long typeint;
  short ntypes;

  typeint = d_getsl((const unsigned char *) type);
  ntypes  = d_getsw(map->tlist);

  for (ptr = map->tlist + 2; ntypes >= 0; ptr += 8, --ntypes)
    {
      if (d_getsl(ptr) == typeint)
	break;
    }

  if (ntypes < 0)
    ERROR(EINVAL, "resource type not found");

  return ptr;

fail:
  return 0;
}

/*
 * NAME:	rsrc->counttypes()
 * DESCRIPTION:	return the number of unique resource types
 */
int rsrc_counttypes(rsrcfile *rfile)
{
  return d_getsw(rfile->map.tlist) + 1;
}

/*
 * NAME:	rsrc->count()
 * DESCRIPTION:	return the number of resources of a given type
 */
int rsrc_count(rsrcfile *rfile, const char *type)
{
  const byte *ptr;

  ptr = findtype(&rfile->map, type);
  if (ptr == 0)
    return 0;

  return d_getsw(ptr + 4) + 1;
}

/*
 * NAME:	rsrc->gettype()
 * DESCRIPTION:	return an indexed type from the resource file
 */
void rsrc_gettype(rsrcfile *rfile, int index, char *type)
{
  if (index < 1 || index > (d_getsw(rfile->map.tlist) + 1))
    memset(type, 0, 5);
  else
    {
      memcpy(type, rfile->map.tlist + 2 + 8 * (index - 1), 4);
      type[4] = 0;
    }
}

/*
 * NAME:	find()
 * DESCRIPTION:	generic routine to locate and return a resource
 */
static
const byte *find(rsrcmap *map, const char *type,
		 int (*compare)(rsrcmap *, const byte *, const void *),
		 const void *key)
{
  const byte *ptr;
  short nitems;
  unsigned short rlistoff;

  ptr = findtype(map, type);
  if (ptr == 0)
    goto fail;

  ptr += 4;
  d_fetchsw(&ptr, &nitems);
  d_fetchuw(&ptr, &rlistoff);

  for (ptr = map->tlist + rlistoff; nitems >= 0; ptr += 12, --nitems)
    {
      if (compare(map, ptr, key))
	break;
    }

  if (nitems < 0)
    ERROR(EINVAL, "resource not found");

  return ptr;

fail:
  return 0;
}

/*
 * NAME:	compare_id()
 * DESCRIPTION:	compare reference list item with numeric id
 */
static
int compare_id(rsrcmap *map, const byte *ritem, const void *key)
{
  return d_getsw(ritem) == *(const int *) key;
}

/*
 * NAME:	compare_name()
 * DESCRIPTION:	compare reference list item with character name
 */
static
int compare_name(rsrcmap *map, const byte *ritem, const void *key)
{
  char name[256];
  const byte *nptr = map->nlist + d_getuw(ritem + 2);

  memcpy(name, nptr + 1, *nptr);
  name[*nptr] = 0;

  return d_relstring(name, key) == 0;
}

/*
 * NAME:	load()
 * DESCRIPTION:	retrieve a resource from the resource file
 */
static
rsrchandle *load(rsrcfile *rfile, const byte *ritem)
{
  unsigned long offs, nbytes, len, count;
  byte data[260];
  rsrchandle *rsrc = 0;

  offs = d_getul(ritem + 4) & 0x00ffffff;

  if (rfile->procs.seek(rfile->priv,
			rfile->hdr.dstart + offs, RSRC_SEEK_SET) ==
      (unsigned long) -1)
    ERROR(errno, "error seeking resource data");

  nbytes = rfile->procs.read(rfile->priv, data, sizeof(data));
  if (nbytes < 4)
    {
      if (nbytes == (unsigned long) -1)
	ERROR(errno, "error reading resource data");
      else
	ERROR(EIO, "truncated resource data");
    }

  len = d_getul(data);

  rsrc = (rsrchandle *) ALLOC(byte, sizeof(rsrchandle) + len);
  if (rsrc == 0)
    ERROR(ENOMEM, 0);

  count = nbytes - 4;
  if (count > len)
    count = len;

  memcpy(rsrc->data, data + 4, count);

  if (count < len)
    {
      nbytes = rfile->procs.read(rfile->priv, rsrc->data + count, len - count);
      if (nbytes != len - count)
	{
	  if (nbytes == (unsigned long) -1)
	    ERROR(errno, "error reading resource data");
	  else
	    ERROR(EIO, "truncated resource data");
	}
    }

  rsrc->rfile = rfile;
  rsrc->ritem = ritem;
  rsrc->len   = len;

  return rsrc;

fail:
  FREE(rsrc);
  return 0;
}

/*
 * NAME:	getrdata()
 * DESCRIPTION:	generate application resource data pointer from resource handle
 */
static
void *getrdata(rsrchandle *rsrc)
{
  return rsrc->data;
}

/*
 * NAME:	gethandle()
 * DESCRIPTION:	recover resource handle from application resource data pointer
 */
static
rsrchandle *gethandle(void *rdata)
{
  rsrchandle dummy;

  return (rsrchandle *) ((char *) rdata -
			 ((char *) dummy.data - (char *) &dummy));
}

/*
 * NAME:	rsrc->maxsize()
 * DESCRIPTION:	return maximum possible size of the given resource (type/id)
 */
unsigned long rsrc_maxsize(rsrcfile *rfile, const char *type, int id)
{
  const byte *ptr;
  unsigned long offs1, offs2;

  ptr = find(&rfile->map, type, compare_id, &id);
  if (ptr == 0)
    return 0;

  offs1 = d_getul(ptr + 4) & 0x00ffffff;
  /* ... */

  offs2 = 0;

  return 0;
}

/*
 * NAME:	rsrc->get()
 * DESCRIPTION:	return a resource by type/id
 */
void *rsrc_get(rsrcfile *rfile, const char *type, int id)
{
  const byte *ptr;
  rsrchandle *rsrc;

  ptr = find(&rfile->map, type, compare_id, &id);
  if (ptr == 0)
    return 0;

  rsrc = load(rfile, ptr);
  if (rsrc == 0)
    return 0;

  return getrdata(rsrc);
}

/*
 * NAME:	rsrc->getnamed()
 * DESCRIPTION:	return a resource by type/name
 */
void *rsrc_getnamed(rsrcfile *rfile, const char *type, const char *name)
{
  const byte *ptr;
  rsrchandle *rsrc;

  ptr = find(&rfile->map, type, compare_name, name);
  if (ptr == 0)
    return 0;

  rsrc = load(rfile, ptr);
  if (rsrc == 0)
    return 0;

  return getrdata(rsrc);
}

/*
 * NAME:	rsrc->getind()
 * DESCRIPTION:	return a resource by type/index
 */
void *rsrc_getind(rsrcfile *rfile, const char *type, int index)
{
  const byte *ptr;
  short nitems;
  rsrchandle *rsrc;

  ptr = findtype(&rfile->map, type);
  if (ptr == 0)
    goto fail;

  nitems = d_getsw(ptr + 4) + 1;
  if (index < 1 || index > nitems)
    ERROR(EINVAL, "index out of range");

  rsrc = load(rfile, rfile->map.tlist + d_getsw(ptr + 6) + 12 * (index - 1));
  if (rsrc == 0)
    goto fail;

  return getrdata(rsrc);

fail:
  return 0;
}

/*
 * NAME:	rsrc->size()
 * DESCRIPTION:	return the length of a resource in bytes
 */
unsigned long rsrc_size(void *rdata)
{
  rsrchandle *rsrc = gethandle(rdata);

  return rsrc->len;
}

/*
 * NAME:	rsrc->resize()
 * DESCRIPTION:	change the length of a resource
 */
void *rsrc_resize(void *rdata, unsigned long newsize)
{
  rsrchandle *newrsrc, *rsrc = gethandle(rdata);

  if (rsrc->len == newsize)
    goto done;

  newrsrc = (rsrchandle *) REALLOC(rsrc, byte, sizeof(rsrchandle) + newsize);
  if (newrsrc == 0)
    ERROR(ENOMEM, 0);

  newrsrc->attrs |= RSRC_RES_CHANGED;
  newrsrc->len    = newsize;

  rdata = getrdata(newrsrc);

done:
  return rdata;

fail:
  return 0;
}

/*
 * NAME:	rsrc->changed()
 * DESCRIPTION:	indicate a changed resource which must be rewritten to disk
 */
void rsrc_changed(void *rdata)
{
  rsrchandle *rsrc = gethandle(rdata);

  rsrc->attrs |= RSRC_RES_CHANGED;
}

/*
 * NAME:	rsrc->release()
 * DESCRIPTION:	dispose memory used by a resource
 */
void rsrc_release(void *rdata)
{
  rsrchandle *rsrc = gethandle(rdata);

  if (rsrc->attrs & RSRC_RES_CHANGED)
    {
      /* ... */
    }

  FREE(rsrc);
}