Coverage Report

Created: 2026-12-01 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/file/src/cdf.c
Line
Count
Source
1
/*-
2
 * Copyright (c) 2008 Christos Zoulas
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
 * POSSIBILITY OF SUCH DAMAGE.
25
 */
26
/*
27
 * Parse Composite Document Files, the format used in Microsoft Office
28
 * document files before they switched to zipped XML.
29
 * Info from: http://sc.openoffice.org/compdocfileformat.pdf
30
 *
31
 * N.B. This is the "Composite Document File" format, and not the
32
 * "Compound Document Format", nor the "Channel Definition Format".
33
 */
34
35
#include "file.h"
36
37
#ifndef lint
38
FILE_RCSID("@(#)$File: cdf.c,v 1.137 2026/11/26 14:39:31 christos Exp $")
39
#endif
40
41
#include <assert.h>
42
#ifdef CDF_DEBUG
43
#include <err.h>
44
#endif
45
#include <stdlib.h>
46
#include <unistd.h>
47
#include <string.h>
48
#include <time.h>
49
#include <ctype.h>
50
#include <limits.h>
51
#ifdef HAVE_BYTESWAP_H
52
#include <byteswap.h>
53
#endif
54
#ifdef HAVE_SYS_BSWAP_H
55
#include <sys/bswap.h>
56
#endif
57
58
#ifndef EFTYPE
59
4.15k
#define EFTYPE EINVAL
60
#endif
61
62
#ifndef SIZE_T_MAX
63
174k
#define SIZE_T_MAX CAST(size_t, ~0ULL)
64
#endif
65
66
#include "cdf.h"
67
68
#ifdef CDF_DEBUG
69
#define DPRINTF(a) printf a, fflush(stdout)
70
#else
71
#define DPRINTF(a)
72
#endif
73
74
file_private union {
75
  char s[4];
76
  uint32_t u;
77
} cdf_bo;
78
79
1.19k
#define NEED_SWAP (cdf_bo.u == CAST(uint32_t, 0x01020304))
80
81
#define CDF_TOLE8(x)  \
82
13.2k
    (CAST(uint64_t, NEED_SWAP ? _cdf_tole8(x) : CAST(uint64_t, x)))
83
#define CDF_TOLE4(x)  \
84
1.14M
    (CAST(uint32_t, NEED_SWAP ? _cdf_tole4(x) : CAST(uint32_t, x)))
85
#define CDF_TOLE2(x)  \
86
78.6k
    (CAST(uint16_t, NEED_SWAP ? _cdf_tole2(x) : CAST(uint16_t, x)))
87
#define CDF_TOLE(x) (/*CONSTCOND*/sizeof(x) == 2 ? \
88
          CDF_TOLE2(CAST(uint16_t, x)) : \
89
      (/*CONSTCOND*/sizeof(x) == 4 ? \
90
          CDF_TOLE4(CAST(uint32_t, x)) : \
91
          CDF_TOLE8(CAST(uint64_t, x))))
92
267k
#define CDF_GETUINT32(x, y) cdf_getuint32(x, y)
93
94
#define CDF_MALLOC(n) cdf_malloc(__FILE__, __LINE__, (n))
95
#define CDF_REALLOC(p, n) cdf_realloc(__FILE__, __LINE__, (p), (n))
96
1.15k
#define CDF_CALLOC(n, u) cdf_calloc(__FILE__, __LINE__, (n), (u))
97
98
99
/*ARGSUSED*/
100
file_private void *
101
cdf_malloc(const char *file __attribute__((__unused__)),
102
    size_t line __attribute__((__unused__)), size_t n)
103
1.20k
{
104
1.20k
  DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u\n",
105
1.20k
      file, line, __func__, n));
106
1.20k
  if (n == 0)
107
0
      n++;
108
1.20k
  return malloc(n);
109
1.20k
}
110
111
/*ARGSUSED*/
112
file_private void *
113
cdf_realloc(const char *file __attribute__((__unused__)),
114
    size_t line __attribute__((__unused__)), void *p, size_t n)
115
778
{
116
778
  DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u\n",
117
778
      file, line, __func__, n));
118
778
  return realloc(p, n);
119
778
}
120
121
/*ARGSUSED*/
122
file_private void *
123
cdf_calloc(const char *file __attribute__((__unused__)),
124
    size_t line __attribute__((__unused__)), size_t n, size_t u)
125
7.05k
{
126
7.05k
  DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u %"
127
7.05k
      SIZE_T_FORMAT "u\n", file, line, __func__, n, u));
128
7.05k
  if (n == 0)
129
59
      n++;
130
7.05k
  return calloc(n, u);
131
7.05k
}
132
133
#if defined(HAVE_BYTESWAP_H)
134
# define _cdf_tole2(x)  bswap_16(x)
135
# define _cdf_tole4(x)  bswap_32(x)
136
# define _cdf_tole8(x)  bswap_64(x)
137
#elif defined(HAVE_SYS_BSWAP_H)
138
# define _cdf_tole2(x)  bswap16(x)
139
# define _cdf_tole4(x)  bswap32(x)
140
# define _cdf_tole8(x)  bswap64(x)
141
#else
142
/*
143
 * swap a short
144
 */
145
file_private uint16_t
146
_cdf_tole2(uint16_t sv)
147
{
148
  uint16_t rv;
149
  uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
150
  uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
151
  d[0] = s[1];
152
  d[1] = s[0];
153
  return rv;
154
}
155
156
/*
157
 * swap an int
158
 */
159
file_private uint32_t
160
_cdf_tole4(uint32_t sv)
161
{
162
  uint32_t rv;
163
  uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
164
  uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
165
  d[0] = s[3];
166
  d[1] = s[2];
167
  d[2] = s[1];
168
  d[3] = s[0];
169
  return rv;
170
}
171
172
/*
173
 * swap a quad
174
 */
175
file_private uint64_t
176
_cdf_tole8(uint64_t sv)
177
{
178
  uint64_t rv;
179
  uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
180
  uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
181
  d[0] = s[7];
182
  d[1] = s[6];
183
  d[2] = s[5];
184
  d[3] = s[4];
185
  d[4] = s[3];
186
  d[5] = s[2];
187
  d[6] = s[1];
188
  d[7] = s[0];
189
  return rv;
190
}
191
#endif
192
193
/*
194
 * grab a uint32_t from a possibly unaligned address, and return it in
195
 * the native host order.
196
 */
197
file_private uint32_t
198
cdf_getuint32(const uint8_t *p, size_t offs)
199
267k
{
200
267k
  uint32_t rv;
201
267k
  (void)memcpy(&rv, p + offs * sizeof(uint32_t), sizeof(rv));
202
267k
  return CDF_TOLE4(rv);
203
267k
}
204
205
#define CDF_UNPACK(a) \
206
640k
    (void)memcpy(&(a), &buf[len], sizeof(a)), len += sizeof(a)
207
#define CDF_UNPACKA(a)  \
208
37.1k
    (void)memcpy((a), &buf[len], sizeof(a)), len += sizeof(a)
209
210
file_protected uint16_t
211
cdf_tole2(uint16_t sv)
212
0
{
213
0
  return CDF_TOLE2(sv);
214
0
}
215
216
file_protected uint32_t
217
cdf_tole4(uint32_t sv)
218
0
{
219
0
  return CDF_TOLE4(sv);
220
0
}
221
222
file_protected uint64_t
223
cdf_tole8(uint64_t sv)
224
0
{
225
0
  return CDF_TOLE8(sv);
226
0
}
227
228
file_protected void
229
cdf_swap_header(cdf_header_t *h)
230
4.01k
{
231
4.01k
  size_t i;
232
233
4.01k
  h->h_magic = CDF_TOLE8(h->h_magic);
234
4.01k
  h->h_uuid[0] = CDF_TOLE8(h->h_uuid[0]);
235
4.01k
  h->h_uuid[1] = CDF_TOLE8(h->h_uuid[1]);
236
4.01k
  h->h_revision = CDF_TOLE2(h->h_revision);
237
4.01k
  h->h_version = CDF_TOLE2(h->h_version);
238
4.01k
  h->h_byte_order = CDF_TOLE2(h->h_byte_order);
239
4.01k
  h->h_sec_size_p2 = CDF_TOLE2(h->h_sec_size_p2);
240
4.01k
  h->h_short_sec_size_p2 = CDF_TOLE2(h->h_short_sec_size_p2);
241
4.01k
  h->h_num_sectors_in_sat = CDF_TOLE4(h->h_num_sectors_in_sat);
242
4.01k
  h->h_secid_first_directory = CDF_TOLE4(h->h_secid_first_directory);
243
4.01k
  h->h_min_size_standard_stream =
244
4.01k
      CDF_TOLE4(h->h_min_size_standard_stream);
245
4.01k
  h->h_secid_first_sector_in_short_sat =
246
4.01k
      CDF_TOLE4(CAST(uint32_t, h->h_secid_first_sector_in_short_sat));
247
4.01k
  h->h_num_sectors_in_short_sat =
248
4.01k
      CDF_TOLE4(h->h_num_sectors_in_short_sat);
249
4.01k
  h->h_secid_first_sector_in_master_sat =
250
4.01k
      CDF_TOLE4(CAST(uint32_t, h->h_secid_first_sector_in_master_sat));
251
4.01k
  h->h_num_sectors_in_master_sat =
252
4.01k
      CDF_TOLE4(h->h_num_sectors_in_master_sat);
253
441k
  for (i = 0; i < __arraycount(h->h_master_sat); i++) {
254
437k
    h->h_master_sat[i] =
255
437k
        CDF_TOLE4(CAST(uint32_t, h->h_master_sat[i]));
256
437k
  }
257
4.01k
}
258
259
file_protected void
260
cdf_unpack_header(cdf_header_t *h, char *buf)
261
4.01k
{
262
4.01k
  size_t i;
263
4.01k
  size_t len = 0;
264
265
4.01k
  CDF_UNPACK(h->h_magic);
266
4.01k
  CDF_UNPACKA(h->h_uuid);
267
4.01k
  CDF_UNPACK(h->h_revision);
268
4.01k
  CDF_UNPACK(h->h_version);
269
4.01k
  CDF_UNPACK(h->h_byte_order);
270
4.01k
  CDF_UNPACK(h->h_sec_size_p2);
271
4.01k
  CDF_UNPACK(h->h_short_sec_size_p2);
272
4.01k
  CDF_UNPACKA(h->h_unused0);
273
4.01k
  CDF_UNPACK(h->h_num_sectors_in_sat);
274
4.01k
  CDF_UNPACK(h->h_secid_first_directory);
275
4.01k
  CDF_UNPACKA(h->h_unused1);
276
4.01k
  CDF_UNPACK(h->h_min_size_standard_stream);
277
4.01k
  CDF_UNPACK(h->h_secid_first_sector_in_short_sat);
278
4.01k
  CDF_UNPACK(h->h_num_sectors_in_short_sat);
279
4.01k
  CDF_UNPACK(h->h_secid_first_sector_in_master_sat);
280
4.01k
  CDF_UNPACK(h->h_num_sectors_in_master_sat);
281
441k
  for (i = 0; i < __arraycount(h->h_master_sat); i++)
282
437k
    CDF_UNPACK(h->h_master_sat[i]);
283
4.01k
}
284
285
file_protected void
286
cdf_swap_dir(cdf_directory_t *d)
287
0
{
288
0
  d->d_namelen = CDF_TOLE2(d->d_namelen);
289
0
  d->d_left_child = CDF_TOLE4(CAST(uint32_t, d->d_left_child));
290
0
  d->d_right_child = CDF_TOLE4(CAST(uint32_t, d->d_right_child));
291
0
  d->d_storage = CDF_TOLE4(CAST(uint32_t, d->d_storage));
292
0
  d->d_storage_uuid[0] = CDF_TOLE8(d->d_storage_uuid[0]);
293
0
  d->d_storage_uuid[1] = CDF_TOLE8(d->d_storage_uuid[1]);
294
0
  d->d_flags = CDF_TOLE4(d->d_flags);
295
0
  d->d_created = CDF_TOLE8(CAST(uint64_t, d->d_created));
296
0
  d->d_modified = CDF_TOLE8(CAST(uint64_t, d->d_modified));
297
0
  d->d_stream_first_sector = CDF_TOLE4(
298
0
      CAST(uint32_t, d->d_stream_first_sector));
299
0
  d->d_size = CDF_TOLE4(d->d_size);
300
0
}
301
302
file_protected void
303
cdf_swap_class(cdf_classid_t *d)
304
841
{
305
841
  d->cl_dword = CDF_TOLE4(d->cl_dword);
306
841
  d->cl_word[0] = CDF_TOLE2(d->cl_word[0]);
307
841
  d->cl_word[1] = CDF_TOLE2(d->cl_word[1]);
308
841
}
309
310
file_protected void
311
cdf_unpack_dir(cdf_directory_t *d, char *buf)
312
12.5k
{
313
12.5k
  size_t len = 0;
314
315
12.5k
  CDF_UNPACKA(d->d_name);
316
12.5k
  CDF_UNPACK(d->d_namelen);
317
12.5k
  CDF_UNPACK(d->d_type);
318
12.5k
  CDF_UNPACK(d->d_color);
319
12.5k
  CDF_UNPACK(d->d_left_child);
320
12.5k
  CDF_UNPACK(d->d_right_child);
321
12.5k
  CDF_UNPACK(d->d_storage);
322
12.5k
  CDF_UNPACKA(d->d_storage_uuid);
323
12.5k
  CDF_UNPACK(d->d_flags);
324
12.5k
  CDF_UNPACK(d->d_created);
325
12.5k
  CDF_UNPACK(d->d_modified);
326
12.5k
  CDF_UNPACK(d->d_stream_first_sector);
327
12.5k
  CDF_UNPACK(d->d_size);
328
12.5k
  CDF_UNPACK(d->d_unused0);
329
12.5k
}
330
331
file_protected int
332
cdf_zero_stream(cdf_stream_t *scn)
333
4.11k
{
334
4.11k
  scn->sst_len = 0;
335
4.11k
  scn->sst_dirlen = 0;
336
4.11k
  scn->sst_ss = 0;
337
4.11k
  free(scn->sst_tab);
338
4.11k
  scn->sst_tab = NULL;
339
4.11k
  return -1;
340
4.11k
}
341
342
file_private size_t
343
cdf_check_stream(const cdf_stream_t *sst, const cdf_header_t *h)
344
11.7k
{
345
11.7k
  size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
346
11.7k
      CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
347
11.7k
  assert(ss == sst->sst_ss);
348
11.7k
  return sst->sst_ss;
349
11.7k
}
350
351
file_private int
352
cdf_check_stream_offset(const cdf_stream_t *sst, const cdf_header_t *h,
353
    const void *p, size_t tail, int line)
354
11.7k
{
355
11.7k
  const char *b = RCAST(const char *, sst->sst_tab);
356
11.7k
  const char *e = RCAST(const char *, p) + tail;
357
11.7k
  size_t ss = cdf_check_stream(sst, h);
358
11.7k
  /*LINTED*/(void)&line;
359
11.7k
  if (e >= b && CAST(size_t, e - b) <= ss * sst->sst_len)
360
11.5k
    return 0;
361
176
  DPRINTF(("%d: offset begin %p < end %p || %" SIZE_T_FORMAT "u"
362
176
      " > %" SIZE_T_FORMAT "u [%" SIZE_T_FORMAT "u %"
363
176
      SIZE_T_FORMAT "u]\n", line, b, e, (size_t)(e - b),
364
176
      ss * sst->sst_len, ss, sst->sst_len));
365
176
  errno = EFTYPE;
366
176
  return -1;
367
11.7k
}
368
369
file_private ssize_t
370
cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len)
371
190k
{
372
190k
  size_t siz = CAST(size_t, off + len);
373
374
190k
  if (CAST(off_t, off + len) != CAST(off_t, siz))
375
0
    goto out;
376
377
190k
  if (info->i_buf != NULL && info->i_len >= siz) {
378
178k
    (void)memcpy(buf, &info->i_buf[off], len);
379
178k
    return CAST(ssize_t, len);
380
178k
  }
381
382
12.6k
  if (info->i_fd == -1)
383
4.13k
    goto out;
384
385
8.47k
  if (pread(info->i_fd, buf, len, off) != CAST(ssize_t, len))
386
8.47k
    return -1;
387
388
0
  return CAST(ssize_t, len);
389
4.13k
out:
390
4.13k
  errno = EINVAL;
391
4.13k
  return -1;
392
8.47k
}
393
394
file_protected int
395
cdf_read_header(const cdf_info_t *info, cdf_header_t *h)
396
16.4k
{
397
16.4k
  char buf[512];
398
399
16.4k
  (void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
400
16.4k
  if (cdf_read(info, CAST(off_t, 0), buf, sizeof(buf)) == -1)
401
12.3k
    return -1;
402
4.01k
  cdf_unpack_header(h, buf);
403
4.01k
  cdf_swap_header(h);
404
4.01k
  if (h->h_magic != CDF_MAGIC) {
405
2.24k
    DPRINTF(("Bad magic %#" INT64_T_FORMAT "x != %#"
406
2.24k
        INT64_T_FORMAT "x\n",
407
2.24k
        (unsigned long long)h->h_magic,
408
2.24k
        (unsigned long long)CDF_MAGIC));
409
2.24k
    goto out;
410
2.24k
  }
411
1.76k
  if (h->h_sec_size_p2 > 20) {
412
1
    DPRINTF(("Bad sector size %hu\n", h->h_sec_size_p2));
413
1
    goto out;
414
1
  }
415
1.76k
  if (h->h_short_sec_size_p2 > 20) {
416
4
    DPRINTF(("Bad short sector size %hu\n",
417
4
        h->h_short_sec_size_p2));
418
4
    goto out;
419
4
  }
420
1.76k
  return 0;
421
2.25k
out:
422
2.25k
  errno = EFTYPE;
423
2.25k
  return -1;
424
1.76k
}
425
426
427
ssize_t
428
cdf_read_sector(const cdf_info_t *info, void *buf, size_t offs, size_t len,
429
    const cdf_header_t *h, cdf_secid_t id)
430
174k
{
431
174k
  size_t ss = CDF_SEC_SIZE(h);
432
174k
  size_t pos;
433
434
174k
  if (SIZE_T_MAX / ss < CAST(size_t, id))
435
0
    return -1;
436
437
174k
  pos = CDF_SEC_POS(h, id);
438
174k
  assert(ss == len);
439
174k
  return cdf_read(info, CAST(off_t, pos), RCAST(char *, buf) + offs, len);
440
174k
}
441
442
ssize_t
443
cdf_read_short_sector(const cdf_stream_t *sst, void *buf, size_t offs,
444
    size_t len, const cdf_header_t *h, cdf_secid_t id)
445
534
{
446
534
  size_t ss = CDF_SHORT_SEC_SIZE(h);
447
534
  size_t pos;
448
449
534
  if (SIZE_T_MAX / ss < CAST(size_t, id))
450
0
    return -1;
451
452
534
  pos = CDF_SHORT_SEC_POS(h, id);
453
534
  assert(ss == len);
454
534
  if (pos + len > CDF_SEC_SIZE(h) * sst->sst_len) {
455
8
    DPRINTF(("Out of bounds read %" SIZE_T_FORMAT "u > %"
456
8
        SIZE_T_FORMAT "u\n",
457
8
        pos + len, CDF_SEC_SIZE(h) * sst->sst_len));
458
8
    goto out;
459
8
  }
460
526
  (void)memcpy(RCAST(char *, buf) + offs,
461
526
      RCAST(const char *, sst->sst_tab) + pos, len);
462
526
  return len;
463
8
out:
464
8
  errno = EFTYPE;
465
8
  return -1;
466
534
}
467
468
/*
469
 * Read the sector allocation table.
470
 */
471
file_protected int
472
cdf_read_sat(const cdf_info_t *info, cdf_header_t *h, cdf_sat_t *sat)
473
1.76k
{
474
1.76k
  size_t i, j, k;
475
1.76k
  size_t ss = CDF_SEC_SIZE(h);
476
1.76k
  cdf_secid_t *msa, mid, sec;
477
1.76k
  size_t nsatpersec = (ss / sizeof(mid)) - 1;
478
479
108k
  for (i = 0; i < __arraycount(h->h_master_sat); i++)
480
107k
    if (h->h_master_sat[i] == CDF_SECID_FREE)
481
909
      break;
482
483
3.36k
#define CDF_SEC_LIMIT (UINT32_MAX / (64 * ss))
484
1.76k
  if ((nsatpersec > 0 &&
485
1.61k
      h->h_num_sectors_in_master_sat > CDF_SEC_LIMIT / nsatpersec) ||
486
1.74k
      i > CDF_SEC_LIMIT) {
487
20
    DPRINTF(("Number of sectors in master SAT too big %u %"
488
20
        SIZE_T_FORMAT "u\n", h->h_num_sectors_in_master_sat, i));
489
20
    errno = EFTYPE;
490
20
    return -1;
491
20
  }
492
493
1.74k
  sat->sat_len = h->h_num_sectors_in_master_sat * nsatpersec + i;
494
1.74k
#define CDF_SAT_LIMIT (16 * 1024 * 1024)
495
1.74k
  if (ss != 0 && sat->sat_len > CDF_SAT_LIMIT / ss) {
496
28
    errno = EFTYPE;
497
28
    return -1;
498
28
  }
499
1.71k
  DPRINTF(("sat_len = %" SIZE_T_FORMAT "u ss = %" SIZE_T_FORMAT "u\n",
500
1.71k
      sat->sat_len, ss));
501
1.71k
  if ((sat->sat_tab = CAST(cdf_secid_t *, CDF_CALLOC(sat->sat_len, ss)))
502
1.71k
      == NULL)
503
0
    return -1;
504
505
46.7k
  for (i = 0; i < __arraycount(h->h_master_sat); i++) {
506
46.3k
    if (h->h_master_sat[i] < 0)
507
1.29k
      break;
508
45.0k
    if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
509
45.0k
        h->h_master_sat[i]) != CAST(ssize_t, ss)) {
510
37
      DPRINTF(("Reading sector %d", h->h_master_sat[i]));
511
37
      goto out1;
512
37
    }
513
45.0k
  }
514
515
1.67k
  if ((msa = CAST(cdf_secid_t *, CDF_CALLOC(1, ss))) == NULL)
516
0
    goto out1;
517
518
1.67k
  mid = h->h_secid_first_sector_in_master_sat;
519
24.9k
  for (j = 0; j < h->h_num_sectors_in_master_sat; j++) {
520
24.3k
    if (mid < 0)
521
147
      goto out;
522
24.1k
    if (j >= CDF_LOOP_LIMIT) {
523
2
      DPRINTF(("Reading master sector loop limit"));
524
2
      goto out3;
525
2
    }
526
24.1k
    if (cdf_read_sector(info, msa, 0, ss, h, mid) !=
527
24.1k
        CAST(ssize_t, ss)) {
528
73
      DPRINTF(("Reading master sector %d", mid));
529
73
      goto out2;
530
73
    }
531
120k
    for (k = 0; k < nsatpersec; k++, i++) {
532
96.7k
      sec = CDF_TOLE4(CAST(uint32_t, msa[k]));
533
96.7k
      if (sec < 0)
534
755
        goto out;
535
95.9k
      if (i >= sat->sat_len) {
536
0
          DPRINTF(("Out of bounds reading MSA %"
537
0
        SIZE_T_FORMAT "u >= %" SIZE_T_FORMAT "u",
538
0
        i, sat->sat_len));
539
0
          goto out3;
540
0
      }
541
95.9k
      if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
542
95.9k
          sec) != CAST(ssize_t, ss)) {
543
45
        DPRINTF(("Reading sector %d",
544
45
            CDF_TOLE4(msa[k])));
545
45
        goto out2;
546
45
      }
547
95.9k
    }
548
23.2k
    mid = CDF_TOLE4(CAST(uint32_t, msa[nsatpersec]));
549
23.2k
  }
550
1.55k
out:
551
1.55k
  sat->sat_len = i;
552
1.55k
  free(msa);
553
1.55k
  return 0;
554
2
out3:
555
2
  errno = EFTYPE;
556
120
out2:
557
120
  free(msa);
558
157
out1:
559
157
  free(sat->sat_tab);
560
157
  return -1;
561
120
}
562
563
size_t
564
cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size)
565
4.25k
{
566
4.25k
  size_t i, j;
567
4.25k
  cdf_secid_t maxsector;
568
569
4.25k
  if (size == 0 || sat->sat_len > SIZE_MAX / size)
570
0
    goto out;
571
4.25k
  maxsector = CAST(cdf_secid_t, (sat->sat_len * size) /
572
4.25k
      sizeof(maxsector));
573
574
4.25k
  DPRINTF(("Chain:"));
575
4.25k
  if (sid == CDF_SECID_END_OF_CHAIN) {
576
    /* 0-length chain. */
577
22
    DPRINTF((" empty\n"));
578
22
    return 0;
579
22
  }
580
581
280k
  for (j = i = 0; sid >= 0; i++, j++) {
582
277k
    DPRINTF((" %d", sid));
583
277k
    if (j >= CDF_LOOP_LIMIT) {
584
25
      DPRINTF(("Counting chain loop limit"));
585
25
      goto out;
586
25
    }
587
277k
    if (i >= CDF_MEMORY_LIMIT / size) {
588
5
      DPRINTF(("Counting chain size limit"));
589
5
      goto out;
590
5
    }
591
276k
    if (sid >= maxsector) {
592
344
      DPRINTF(("Sector %d >= %d\n", sid, maxsector));
593
344
      goto out;
594
344
    }
595
276k
    sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
596
276k
  }
597
3.85k
  if (i == 0) {
598
195
    DPRINTF((" none, sid: %d\n", sid));
599
195
    goto out;
600
601
195
  }
602
3.66k
  DPRINTF(("\n"));
603
3.66k
  return i;
604
569
out:
605
569
  errno = EFTYPE;
606
569
  return CAST(size_t, -1);
607
3.85k
}
608
609
file_protected int
610
cdf_read_long_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
611
    const cdf_sat_t *sat, cdf_secid_t sid, size_t len, cdf_stream_t *scn)
612
1.17k
{
613
1.17k
  size_t ss = CDF_SEC_SIZE(h), i, j;
614
1.17k
  ssize_t nr;
615
1.17k
  scn->sst_tab = NULL;
616
1.17k
  scn->sst_len = cdf_count_chain(sat, sid, ss);
617
1.17k
  scn->sst_dirlen = MAX(h->h_min_size_standard_stream, len);
618
1.17k
  scn->sst_ss = ss;
619
620
1.17k
  if (sid == CDF_SECID_END_OF_CHAIN || len == 0)
621
79
    return cdf_zero_stream(scn);
622
623
1.09k
  if (scn->sst_len == CAST(size_t, -1))
624
79
    goto out;
625
626
1.01k
  scn->sst_tab = CDF_CALLOC(scn->sst_len, ss);
627
1.01k
  if (scn->sst_tab == NULL)
628
0
    return cdf_zero_stream(scn);
629
630
4.02k
  for (j = i = 0; sid >= 0; i++, j++) {
631
3.03k
    if (j >= CDF_LOOP_LIMIT) {
632
0
      DPRINTF(("Read long sector chain loop limit"));
633
0
      goto out;
634
0
    }
635
3.03k
    if (i >= scn->sst_len) {
636
0
      DPRINTF(("Out of bounds reading long sector chain "
637
0
          "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n", i,
638
0
          scn->sst_len));
639
0
      goto out;
640
0
    }
641
3.03k
    if ((nr = cdf_read_sector(info, scn->sst_tab, i * ss, ss, h,
642
3.03k
        sid)) != CAST(ssize_t, ss)) {
643
22
      if (i == scn->sst_len - 1 && nr > 0) {
644
        /* Last sector might be truncated */
645
0
        return 0;
646
0
      }
647
22
      DPRINTF(("Reading long sector chain %d", sid));
648
22
      goto out;
649
22
    }
650
3.00k
    sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
651
3.00k
  }
652
990
  return 0;
653
101
out:
654
101
  errno = EFTYPE;
655
101
  return cdf_zero_stream(scn);
656
1.01k
}
657
658
file_protected int
659
cdf_read_short_sector_chain(const cdf_header_t *h,
660
    const cdf_sat_t *ssat, const cdf_stream_t *sst,
661
    cdf_secid_t sid, size_t len, cdf_stream_t *scn)
662
251
{
663
251
  size_t ss = CDF_SHORT_SEC_SIZE(h), i, j;
664
251
  scn->sst_tab = NULL;
665
251
  scn->sst_len = cdf_count_chain(ssat, sid, CDF_SEC_SIZE(h));
666
251
  scn->sst_dirlen = len;
667
251
  scn->sst_ss = ss;
668
669
251
  if (scn->sst_len == CAST(size_t, -1))
670
104
    goto out;
671
672
147
  scn->sst_tab = CDF_CALLOC(scn->sst_len, ss);
673
147
  if (scn->sst_tab == NULL)
674
0
    return cdf_zero_stream(scn);
675
676
673
  for (j = i = 0; sid >= 0; i++, j++) {
677
534
    if (j >= CDF_LOOP_LIMIT) {
678
0
      DPRINTF(("Read short sector chain loop limit"));
679
0
      goto out;
680
0
    }
681
534
    if (i >= scn->sst_len) {
682
0
      DPRINTF(("Out of bounds reading short sector chain "
683
0
          "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n",
684
0
          i, scn->sst_len));
685
0
      goto out;
686
0
    }
687
534
    if (cdf_read_short_sector(sst, scn->sst_tab, i * ss, ss, h,
688
534
        sid) != CAST(ssize_t, ss)) {
689
8
      DPRINTF(("Reading short sector chain %d", sid));
690
8
      goto out;
691
8
    }
692
526
    sid = CDF_TOLE4(CAST(uint32_t, ssat->sat_tab[sid]));
693
526
  }
694
139
  return 0;
695
112
out:
696
112
  errno = EFTYPE;
697
112
  return cdf_zero_stream(scn);
698
147
}
699
700
file_protected int
701
cdf_read_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
702
    const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
703
    cdf_secid_t sid, size_t len, cdf_stream_t *scn)
704
1.06k
{
705
706
1.06k
  if (len < h->h_min_size_standard_stream && sst->sst_tab != NULL)
707
251
    return cdf_read_short_sector_chain(h, ssat, sst, sid, len,
708
251
        scn);
709
815
  else
710
815
    return cdf_read_long_sector_chain(info, h, sat, sid, len, scn);
711
1.06k
}
712
713
file_protected int
714
cdf_read_dir(const cdf_info_t *info, const cdf_header_t *h,
715
    const cdf_sat_t *sat, cdf_dir_t *dir)
716
1.27k
{
717
1.27k
  size_t i, j;
718
1.27k
  size_t ss = CDF_SEC_SIZE(h), ns, nd;
719
1.27k
  char *buf;
720
1.27k
  cdf_secid_t sid = h->h_secid_first_directory;
721
722
1.27k
  ns = cdf_count_chain(sat, sid, ss);
723
1.27k
  if (ns == CAST(size_t, -1))
724
72
    return -1;
725
726
1.20k
  nd = ss / CDF_DIRECTORY_SIZE;
727
1.20k
  if (nd != 0 && ns > CDF_MEMORY_LIMIT / nd / sizeof(dir->dir_tab[0])) {
728
0
    DPRINTF(("Directory size limit"));
729
0
    errno = EFTYPE;
730
0
    return -1;
731
0
  }
732
733
1.20k
  dir->dir_len = ns * nd;
734
1.20k
  dir->dir_tab = CAST(cdf_directory_t *,
735
1.20k
      CDF_CALLOC(dir->dir_len, sizeof(dir->dir_tab[0])));
736
1.20k
  if (dir->dir_tab == NULL)
737
0
    return -1;
738
739
1.20k
  if ((buf = CAST(char *, CDF_MALLOC(ss))) == NULL) {
740
0
    free(dir->dir_tab);
741
0
    return -1;
742
0
  }
743
744
4.71k
  for (j = i = 0; i < ns; i++, j++) {
745
3.51k
    if (j >= CDF_LOOP_LIMIT) {
746
0
      DPRINTF(("Read dir loop limit"));
747
0
      goto out;
748
0
    }
749
3.51k
    if (cdf_read_sector(info, buf, 0, ss, h, sid) !=
750
3.51k
        CAST(ssize_t, ss)) {
751
8
      DPRINTF(("Reading directory sector %d", sid));
752
8
      goto out;
753
8
    }
754
16.0k
    for (j = 0; j < nd; j++) {
755
12.5k
      cdf_unpack_dir(&dir->dir_tab[i * nd + j],
756
12.5k
          &buf[j * CDF_DIRECTORY_SIZE]);
757
12.5k
    }
758
3.50k
    sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
759
3.50k
  }
760
1.19k
  if (NEED_SWAP)
761
0
    for (i = 0; i < dir->dir_len; i++)
762
0
      cdf_swap_dir(&dir->dir_tab[i]);
763
1.19k
  free(buf);
764
1.19k
  return 0;
765
8
out:
766
8
  free(dir->dir_tab);
767
8
  free(buf);
768
8
  errno = EFTYPE;
769
8
  return -1;
770
1.20k
}
771
772
773
file_protected int
774
cdf_read_ssat(const cdf_info_t *info, const cdf_header_t *h,
775
    const cdf_sat_t *sat, cdf_sat_t *ssat)
776
1.55k
{
777
1.55k
  size_t i, j;
778
1.55k
  size_t ss = CDF_SEC_SIZE(h);
779
1.55k
  cdf_secid_t sid = h->h_secid_first_sector_in_short_sat;
780
781
1.55k
  ssat->sat_tab = NULL;
782
1.55k
  ssat->sat_len = cdf_count_chain(sat, sid, ss);
783
1.55k
  if (ssat->sat_len == CAST(size_t, -1))
784
259
    goto out;
785
786
1.29k
  ssat->sat_tab = CAST(cdf_secid_t *, CDF_CALLOC(ssat->sat_len, ss));
787
1.29k
  if (ssat->sat_tab == NULL)
788
0
    goto out1;
789
790
3.79k
  for (j = i = 0; sid >= 0; i++, j++) {
791
2.52k
    if (j >= CDF_LOOP_LIMIT) {
792
0
      DPRINTF(("Read short sat sector loop limit"));
793
0
      goto out;
794
0
    }
795
2.52k
    if (i >= ssat->sat_len) {
796
0
      DPRINTF(("Out of bounds reading short sector chain "
797
0
          "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n", i,
798
0
          ssat->sat_len));
799
0
      goto out;
800
0
    }
801
2.52k
    if (cdf_read_sector(info, ssat->sat_tab, i * ss, ss, h, sid) !=
802
2.52k
        CAST(ssize_t, ss)) {
803
23
      DPRINTF(("Reading short sat sector %d", sid));
804
23
      goto out1;
805
23
    }
806
2.50k
    sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
807
2.50k
  }
808
1.27k
  return 0;
809
259
out:
810
259
  errno = EFTYPE;
811
282
out1:
812
282
  free(ssat->sat_tab);
813
282
  return -1;
814
259
}
815
816
file_protected int
817
cdf_read_short_stream(const cdf_info_t *info, const cdf_header_t *h,
818
    const cdf_sat_t *sat, const cdf_dir_t *dir, cdf_stream_t *scn,
819
    const cdf_directory_t **root)
820
1.19k
{
821
1.19k
  size_t i;
822
1.19k
  const cdf_directory_t *d;
823
824
1.19k
  *root = NULL;
825
9.57k
  for (i = 0; i < dir->dir_len; i++)
826
8.89k
    if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_ROOT_STORAGE)
827
513
      break;
828
829
  /* If the it is not there, just fake it; some docs don't have it */
830
1.19k
  if (i == dir->dir_len) {
831
683
    DPRINTF(("Cannot find root storage dir\n"));
832
683
    goto out;
833
683
  }
834
513
  d = &dir->dir_tab[i];
835
513
  *root = d;
836
837
  /* If the it is not there, just fake it; some docs don't have it */
838
513
  if (d->d_stream_first_sector < 0) {
839
158
    DPRINTF(("No first secror in dir\n"));
840
158
    goto out;
841
158
  }
842
843
355
  return cdf_read_long_sector_chain(info, h, sat,
844
355
      d->d_stream_first_sector, d->d_size, scn);
845
841
out:
846
841
  scn->sst_tab = NULL;
847
841
  (void)cdf_zero_stream(scn);
848
841
  return 0;
849
513
}
850
851
file_protected int
852
cdf_namecmp(const char *d, const uint16_t *s, size_t l)
853
16.3k
{
854
39.7k
  for (; l--; d++, s++)
855
38.6k
    if (*d != CDF_TOLE2(*s))
856
15.2k
      return CAST(unsigned char, *d) - CDF_TOLE2(*s);
857
1.07k
  return 0;
858
16.3k
}
859
860
file_protected int
861
cdf_read_doc_summary_info(const cdf_info_t *info, const cdf_header_t *h,
862
    const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
863
    const cdf_dir_t *dir, cdf_stream_t *scn)
864
971
{
865
971
  return cdf_read_user_stream(info, h, sat, ssat, sst, dir,
866
971
      "\05DocumentSummaryInformation", scn);
867
971
}
868
869
file_protected int
870
cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h,
871
    const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
872
    const cdf_dir_t *dir, cdf_stream_t *scn)
873
1.10k
{
874
1.10k
  return cdf_read_user_stream(info, h, sat, ssat, sst, dir,
875
1.10k
      "\05SummaryInformation", scn);
876
1.10k
}
877
878
file_protected int
879
cdf_read_user_stream(const cdf_info_t *info, const cdf_header_t *h,
880
    const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
881
    const cdf_dir_t *dir, const char *name, cdf_stream_t *scn)
882
3.17k
{
883
3.17k
  const cdf_directory_t *d;
884
3.17k
  int i = cdf_find_stream(dir, name, CDF_DIR_TYPE_USER_STREAM);
885
886
3.17k
  if (i <= 0) {
887
2.10k
    memset(scn, 0, sizeof(*scn));
888
2.10k
    return -1;
889
2.10k
  }
890
891
1.06k
  d = &dir->dir_tab[i - 1];
892
1.06k
  return cdf_read_sector_chain(info, h, sat, ssat, sst,
893
1.06k
      d->d_stream_first_sector, d->d_size, scn);
894
3.17k
}
895
896
file_protected int
897
cdf_find_stream(const cdf_dir_t *dir, const char *name, int type)
898
3.17k
{
899
3.17k
  size_t i, name_len = strlen(name) + 1;
900
901
24.0k
  for (i = 1; i <= dir->dir_len; i++)
902
21.8k
    if (dir->dir_tab[i-1].d_type == type &&
903
4.41k
        cdf_namecmp(name, dir->dir_tab[i-1].d_name, name_len)
904
4.41k
        == 0)
905
1.06k
      break;
906
3.17k
  if (i <= dir->dir_len)
907
1.06k
    return CAST(int, i);
908
909
2.10k
  DPRINTF(("Cannot find type %d `%s'\n", type, name));
910
2.10k
  errno = ESRCH;
911
2.10k
  return 0;
912
3.17k
}
913
914
747
#define CDF_SHLEN_LIMIT (UINT32_MAX / 64)
915
1.44k
#define CDF_PROP_LIMIT (UINT32_MAX / (64 * sizeof(cdf_property_info_t)))
916
917
file_private const void *
918
cdf_offset(const void *p, size_t l)
919
9.73k
{
920
9.73k
  return CAST(const void *, CAST(const uint8_t *, p) + l);
921
9.73k
}
922
923
file_private const uint8_t *
924
cdf_get_property_info_pos(const cdf_stream_t *sst, const cdf_header_t *h,
925
    const uint8_t *p, const uint8_t *e, size_t i)
926
7.87k
{
927
7.87k
  size_t tail = (i << 1) + 1;
928
7.87k
  size_t ofs;
929
930
7.87k
  if (p >= e) {
931
0
    DPRINTF(("Past end %p < %p\n", e, p));
932
0
    return NULL;
933
0
  }
934
935
7.87k
  if (cdf_check_stream_offset(sst, h, p, (tail + 1) * sizeof(uint32_t),
936
7.87k
      __LINE__) == -1)
937
14
    return NULL;
938
939
7.85k
  ofs = CDF_GETUINT32(p, tail);
940
7.85k
  if (ofs < 2 * sizeof(uint32_t)) {
941
40
    DPRINTF(("Offset too small %zu\n", ofs));
942
40
    return NULL;
943
40
  }
944
945
7.81k
  ofs -= 2 * sizeof(uint32_t);
946
7.81k
  if (ofs > CAST(size_t, e - p)) {
947
207
    DPRINTF(("Offset too big %zu %td\n", ofs, e - p));
948
207
    return NULL;
949
207
  }
950
951
7.61k
  return CAST(const uint8_t *, cdf_offset(CAST(const void *, p), ofs));
952
7.81k
}
953
954
file_private cdf_property_info_t *
955
cdf_grow_info(cdf_property_info_t **info, size_t *maxcount, size_t incr)
956
779
{
957
779
  cdf_property_info_t *inp;
958
779
  size_t newcount = *maxcount + incr;
959
960
779
  if (newcount > CDF_PROP_LIMIT) {
961
1
    DPRINTF(("exceeded property limit %" SIZE_T_FORMAT "u > %"
962
1
        SIZE_T_FORMAT "u\n", newcount, CDF_PROP_LIMIT));
963
1
    goto out;
964
1
  }
965
778
  inp = CAST(cdf_property_info_t *,
966
778
      CDF_REALLOC(*info, newcount * sizeof(*inp)));
967
778
  if (inp == NULL)
968
0
    goto out;
969
970
778
  *info = inp;
971
778
  *maxcount = newcount;
972
778
  return inp;
973
1
out:
974
1
  free(*info);
975
1
  *maxcount = 0;
976
1
  *info = NULL;
977
1
  return NULL;
978
778
}
979
980
file_private int
981
cdf_copy_info(cdf_property_info_t *inp, const void *p, const void *e,
982
    size_t len)
983
3.55k
{
984
3.55k
  if (inp->pi_type & CDF_VECTOR)
985
183
    return 0;
986
987
3.36k
  if (CAST(size_t, CAST(const char *, e) - CAST(const char *, p)) < len)
988
887
    return 0;
989
990
2.48k
  (void)memcpy(&inp->pi_val, p, len);
991
992
2.48k
  switch (len) {
993
367
  case 2:
994
367
    inp->pi_u16 = CDF_TOLE2(inp->pi_u16);
995
367
    break;
996
953
  case 4:
997
953
    inp->pi_u32 = CDF_TOLE4(inp->pi_u32);
998
953
    break;
999
1.16k
  case 8:
1000
1.16k
    inp->pi_u64 = CDF_TOLE8(inp->pi_u64);
1001
1.16k
    break;
1002
0
  default:
1003
0
    abort();
1004
2.48k
  }
1005
2.48k
  return 1;
1006
2.48k
}
1007
1008
file_protected int
1009
cdf_read_property_info(const cdf_stream_t *sst, const cdf_header_t *h,
1010
    uint32_t offs, cdf_property_info_t **info, size_t *count, size_t *maxcount)
1011
841
{
1012
841
  const uint8_t *shp;
1013
841
  cdf_section_header_t sh;
1014
841
  const uint8_t *p, *q, *e;
1015
841
  size_t i, o4, nelements, j, slen, left;
1016
841
  cdf_property_info_t *inp;
1017
1018
841
  if (offs > UINT32_MAX / 4) {
1019
14
    errno = EFTYPE;
1020
14
    goto out;
1021
14
  }
1022
827
  shp = CAST(const uint8_t *, cdf_offset(sst->sst_tab, offs));
1023
827
  if (cdf_check_stream_offset(sst, h, shp, sizeof(sh), __LINE__) == -1)
1024
80
    goto out;
1025
747
  memcpy(&sh, shp, sizeof(sh));
1026
747
  sh.sh_len = CDF_TOLE4(sh.sh_len);
1027
747
  if (sh.sh_len > CDF_SHLEN_LIMIT) {
1028
18
    errno = EFTYPE;
1029
18
    goto out;
1030
18
  }
1031
1032
729
  if (cdf_check_stream_offset(sst, h, shp, sh.sh_len, __LINE__) == -1)
1033
59
    goto out;
1034
1035
670
  sh.sh_properties = CDF_TOLE4(sh.sh_properties);
1036
670
  DPRINTF(("section len: %u properties %u\n", sh.sh_len,
1037
670
      sh.sh_properties));
1038
670
  if (sh.sh_properties > CDF_PROP_LIMIT)
1039
22
    goto out;
1040
648
  inp = cdf_grow_info(info, maxcount, sh.sh_properties);
1041
648
  if (inp == NULL)
1042
0
    goto out;
1043
648
  inp += *count;
1044
648
  *count += sh.sh_properties;
1045
648
  p = CAST(const uint8_t *, cdf_offset(sst->sst_tab, offs + sizeof(sh)));
1046
648
  e = CAST(const uint8_t *, cdf_offset(shp, sh.sh_len));
1047
648
  if (p >= e || cdf_check_stream_offset(sst, h, e, 0, __LINE__) == -1)
1048
26
    goto out;
1049
1050
8.12k
  for (i = 0; i < sh.sh_properties; i++) {
1051
7.87k
    if ((q = cdf_get_property_info_pos(sst, h, p, e, i)) == NULL)
1052
261
      goto out;
1053
7.61k
    inp[i].pi_id = CDF_GETUINT32(p, i << 1);
1054
7.61k
    left = CAST(size_t, e - q);
1055
7.61k
    if (left < sizeof(uint32_t)) {
1056
16
      DPRINTF(("short info (no type)_\n"));
1057
16
      goto out;
1058
16
    }
1059
7.59k
    inp[i].pi_type = CDF_GETUINT32(q, 0);
1060
7.59k
    DPRINTF(("%" SIZE_T_FORMAT "u) id=%#x type=%#x offs=%#tx,%#x\n",
1061
7.59k
        i, inp[i].pi_id, inp[i].pi_type, q - p, offs));
1062
7.59k
    if (inp[i].pi_type & CDF_VECTOR) {
1063
624
      if (left < sizeof(uint32_t) * 2) {
1064
3
        DPRINTF(("missing CDF_VECTOR length\n"));
1065
3
        goto out;
1066
3
      }
1067
621
      nelements = CDF_GETUINT32(q, 1);
1068
621
      if (nelements > CDF_ELEMENT_LIMIT || nelements == 0) {
1069
35
        DPRINTF(("CDF_VECTOR with nelements == %"
1070
35
            SIZE_T_FORMAT "u\n", nelements));
1071
35
        goto out;
1072
35
      }
1073
586
      slen = 2;
1074
6.97k
    } else {
1075
6.97k
      nelements = 1;
1076
6.97k
      slen = 1;
1077
6.97k
    }
1078
7.55k
    o4 = slen * sizeof(uint32_t);
1079
7.55k
    if (inp[i].pi_type & (CDF_ARRAY|CDF_BYREF|CDF_RESERVED))
1080
484
      goto unknown;
1081
7.07k
    switch (inp[i].pi_type & CDF_TYPEMASK) {
1082
370
    case CDF_NULL:
1083
958
    case CDF_EMPTY:
1084
958
      break;
1085
692
    case CDF_SIGNED16:
1086
692
      if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int16_t)))
1087
325
        goto unknown;
1088
367
      break;
1089
367
    case CDF_SIGNED32:
1090
536
    case CDF_BOOL:
1091
831
    case CDF_UNSIGNED32:
1092
1.32k
    case CDF_FLOAT:
1093
1.32k
      if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int32_t)))
1094
370
        goto unknown;
1095
953
      break;
1096
953
    case CDF_SIGNED64:
1097
489
    case CDF_UNSIGNED64:
1098
714
    case CDF_DOUBLE:
1099
1.53k
    case CDF_FILETIME:
1100
1.53k
      if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int64_t)))
1101
375
        goto unknown;
1102
1.16k
      break;
1103
1.16k
    case CDF_LENGTH32_STRING:
1104
738
    case CDF_LENGTH32_WSTRING:
1105
738
      if (nelements > 1) {
1106
131
        size_t nelem = inp - *info;
1107
131
        inp = cdf_grow_info(info, maxcount, nelements);
1108
131
        if (inp == NULL)
1109
1
          goto out;
1110
130
        inp += nelem;
1111
130
      }
1112
244k
      for (j = 0; j < nelements && i < sh.sh_properties;
1113
243k
          j++, i++)
1114
243k
      {
1115
243k
        uint32_t l;
1116
1117
243k
        if (o4 + sizeof(uint32_t) > left)
1118
5
          goto out;
1119
1120
243k
        l = CDF_GETUINT32(q, slen);
1121
243k
        o4 += sizeof(uint32_t);
1122
243k
        if (o4 + l > left)
1123
44
          goto out;
1124
1125
243k
        inp[i].pi_str.s_len = l;
1126
243k
        inp[i].pi_str.s_buf = CAST(const char *,
1127
243k
            CAST(const void *, &q[o4]));
1128
1129
243k
        DPRINTF(("o=%" SIZE_T_FORMAT "u l=%d(%"
1130
243k
            SIZE_T_FORMAT "u), t=%" SIZE_T_FORMAT
1131
243k
            "u s=%.*s\n", o4, l,
1132
243k
            CDF_ROUND(l, sizeof(l)),
1133
243k
            left, (int)l, inp[i].pi_str.s_buf));
1134
1135
243k
        if (l & 1)
1136
346
          l++;
1137
1138
243k
        slen += l >> 1;
1139
243k
        o4 = slen * sizeof(uint32_t);
1140
243k
      }
1141
688
      i--;
1142
688
      break;
1143
261
    case CDF_CLIPBOARD:
1144
261
      if (inp[i].pi_type & CDF_VECTOR)
1145
20
        goto unknown;
1146
241
      break;
1147
1.56k
    default:
1148
3.13k
    unknown:
1149
3.13k
      memset(&inp[i].pi_val, 0, sizeof(inp[i].pi_val));
1150
3.13k
      DPRINTF(("Don't know how to deal with %#x\n",
1151
3.13k
          inp[i].pi_type));
1152
3.13k
      break;
1153
7.07k
    }
1154
7.07k
  }
1155
257
  return 0;
1156
584
out:
1157
584
  free(*info);
1158
584
  *info = NULL;
1159
584
  *count = 0;
1160
584
  *maxcount = 0;
1161
584
  errno = EFTYPE;
1162
584
  return -1;
1163
622
}
1164
1165
file_protected int
1166
cdf_unpack_summary_info(const cdf_stream_t *sst, const cdf_header_t *h,
1167
    cdf_summary_info_header_t *ssi, cdf_property_info_t **info, size_t *count)
1168
864
{
1169
864
  size_t maxcount;
1170
864
  const cdf_summary_info_header_t *si =
1171
864
      CAST(const cdf_summary_info_header_t *, sst->sst_tab);
1172
864
  const cdf_section_declaration_t *sd =
1173
864
      CAST(const cdf_section_declaration_t *, RCAST(const void *,
1174
864
      RCAST(const char *, sst->sst_tab)
1175
864
      + CDF_SECTION_DECLARATION_OFFSET));
1176
1177
864
  if (cdf_check_stream_offset(sst, h, si, sizeof(*si), __LINE__) == -1 ||
1178
847
      cdf_check_stream_offset(sst, h, sd, sizeof(*sd), __LINE__) == -1)
1179
23
    return -1;
1180
841
  ssi->si_byte_order = CDF_TOLE2(si->si_byte_order);
1181
841
  ssi->si_os_version = CDF_TOLE2(si->si_os_version);
1182
841
  ssi->si_os = CDF_TOLE2(si->si_os);
1183
841
  ssi->si_class = si->si_class;
1184
841
  cdf_swap_class(&ssi->si_class);
1185
841
  ssi->si_count = CDF_TOLE4(si->si_count);
1186
841
  *count = 0;
1187
841
  maxcount = 0;
1188
841
  *info = NULL;
1189
841
  if (cdf_read_property_info(sst, h, CDF_TOLE4(sd->sd_offset), info,
1190
841
      count, &maxcount) == -1)
1191
584
    return -1;
1192
257
  return 0;
1193
841
}
1194
1195
1196
#define extract_catalog_field(t, f, l) \
1197
0
    if (b + l + sizeof(cep->f) > eb) { \
1198
0
      cep->ce_namlen = 0; \
1199
0
      break; \
1200
0
    } \
1201
0
    memcpy(&cep->f, b + (l), sizeof(cep->f)); \
1202
0
    ce[i].f = CAST(t, CDF_TOLE(cep->f))
1203
1204
file_protected int
1205
cdf_unpack_catalog(const cdf_header_t *h, const cdf_stream_t *sst,
1206
    cdf_catalog_t **cat)
1207
0
{
1208
0
  size_t ss = cdf_check_stream(sst, h);
1209
0
  const char *b = CAST(const char *, sst->sst_tab);
1210
0
  const char *nb, *eb = b + ss * sst->sst_len;
1211
0
  size_t nr, i, j;
1212
0
  cdf_catalog_entry_t *ce;
1213
0
  uint16_t reclen;
1214
0
  const char *np;
1215
1216
0
  for (nr = 0;; nr++) {
1217
0
    if (b + sizeof(reclen) > eb)
1218
0
      break;
1219
0
    memcpy(&reclen, b, sizeof(reclen));
1220
0
    reclen = CDF_TOLE2(reclen);
1221
0
    if (reclen == 0)
1222
0
      break;
1223
0
    b += reclen;
1224
0
    if (b > eb)
1225
0
        break;
1226
0
  }
1227
0
  if (nr == 0)
1228
0
    return -1;
1229
0
  nr--;
1230
0
  *cat = CAST(cdf_catalog_t *,
1231
0
      CDF_MALLOC(sizeof(cdf_catalog_t) + nr * sizeof(*ce)));
1232
0
  if (*cat == NULL)
1233
0
    return -1;
1234
0
  ce = (*cat)->cat_e;
1235
0
  memset(ce, 0, nr * sizeof(*ce));
1236
0
  b = CAST(const char *, sst->sst_tab);
1237
0
  for (j = i = 0; i < nr; b += reclen) {
1238
0
    cdf_catalog_entry_t *cep = &ce[j];
1239
0
    uint16_t rlen;
1240
1241
0
    extract_catalog_field(uint16_t, ce_namlen, 0);
1242
0
    extract_catalog_field(uint16_t, ce_num, 4);
1243
0
    extract_catalog_field(uint64_t, ce_timestamp, 8);
1244
0
    reclen = cep->ce_namlen;
1245
1246
0
    if (reclen == 0)
1247
0
      break;
1248
1249
0
    if (reclen < 14) {
1250
0
      cep->ce_namlen = 0;
1251
0
      continue;
1252
0
    }
1253
1254
0
    cep->ce_namlen = __arraycount(cep->ce_name) - 1;
1255
0
    rlen = reclen - 14;
1256
0
    if (cep->ce_namlen > rlen)
1257
0
      cep->ce_namlen = rlen;
1258
1259
0
    np = b + 16;
1260
0
    nb = np + cep->ce_namlen * sizeof(cep->ce_name[0]);
1261
0
    if (nb > eb) {
1262
0
      cep->ce_namlen = 0;
1263
0
      break;
1264
0
    }
1265
1266
    /* XXX: CDF_TOLE2? */
1267
0
    (void)memcpy(cep->ce_name, np,
1268
0
        cep->ce_namlen * sizeof(cep->ce_name[0]));
1269
0
    cep->ce_name[cep->ce_namlen] = 0;
1270
0
    j = i;
1271
0
    i++;
1272
0
  }
1273
0
  (*cat)->cat_num = j;
1274
0
  return 0;
1275
0
}
1276
1277
file_protected int
1278
cdf_print_classid(char *buf, size_t buflen, const cdf_classid_t *id)
1279
0
{
1280
0
  return snprintf(buf, buflen, "%.8x-%.4x-%.4x-%.2x%.2x-"
1281
0
      "%.2x%.2x%.2x%.2x%.2x%.2x", id->cl_dword, id->cl_word[0],
1282
0
      id->cl_word[1], id->cl_two[0], id->cl_two[1], id->cl_six[0],
1283
0
      id->cl_six[1], id->cl_six[2], id->cl_six[3], id->cl_six[4],
1284
0
      id->cl_six[5]);
1285
0
}
1286
1287
file_private const struct {
1288
  uint32_t v;
1289
  const char *n;
1290
} vn[] = {
1291
  { CDF_PROPERTY_CODE_PAGE, "Code page" },
1292
  { CDF_PROPERTY_TITLE, "Title" },
1293
  { CDF_PROPERTY_SUBJECT, "Subject" },
1294
  { CDF_PROPERTY_AUTHOR, "Author" },
1295
  { CDF_PROPERTY_KEYWORDS, "Keywords" },
1296
  { CDF_PROPERTY_COMMENTS, "Comments" },
1297
  { CDF_PROPERTY_TEMPLATE, "Template" },
1298
  { CDF_PROPERTY_LAST_SAVED_BY, "Last Saved By" },
1299
  { CDF_PROPERTY_REVISION_NUMBER, "Revision Number" },
1300
  { CDF_PROPERTY_TOTAL_EDITING_TIME, "Total Editing Time" },
1301
  { CDF_PROPERTY_LAST_PRINTED, "Last Printed" },
1302
  { CDF_PROPERTY_CREATE_TIME, "Create Time/Date" },
1303
  { CDF_PROPERTY_LAST_SAVED_TIME, "Last Saved Time/Date" },
1304
  { CDF_PROPERTY_NUMBER_OF_PAGES, "Number of Pages" },
1305
  { CDF_PROPERTY_NUMBER_OF_WORDS, "Number of Words" },
1306
  { CDF_PROPERTY_NUMBER_OF_CHARACTERS, "Number of Characters" },
1307
  { CDF_PROPERTY_THUMBNAIL, "Thumbnail" },
1308
  { CDF_PROPERTY_NAME_OF_APPLICATION, "Name of Creating Application" },
1309
  { CDF_PROPERTY_SECURITY, "Security" },
1310
  { CDF_PROPERTY_LOCALE_ID, "Locale ID" },
1311
};
1312
1313
file_protected int
1314
cdf_print_property_name(char *buf, size_t bufsiz, uint32_t p)
1315
1.12k
{
1316
1.12k
  size_t i;
1317
1318
19.1k
  for (i = 0; i < __arraycount(vn); i++)
1319
18.3k
    if (vn[i].v == p)
1320
310
      return snprintf(buf, bufsiz, "%s", vn[i].n);
1321
812
  return snprintf(buf, bufsiz, "%#x", p);
1322
1.12k
}
1323
1324
file_protected int
1325
cdf_print_elapsed_time(char *buf, size_t bufsiz, cdf_timestamp_t ts)
1326
172
{
1327
172
  int len = 0;
1328
172
  int days, hours, mins, secs;
1329
1330
172
  ts /= CDF_TIME_PREC;
1331
172
  secs = CAST(int, ts % 60);
1332
172
  ts /= 60;
1333
172
  mins = CAST(int, ts % 60);
1334
172
  ts /= 60;
1335
172
  hours = CAST(int, ts % 24);
1336
172
  ts /= 24;
1337
172
  days = CAST(int, ts);
1338
1339
172
  if (days) {
1340
94
    len += snprintf(buf + len, bufsiz - len, "%dd+", days);
1341
94
    if (CAST(size_t, len) >= bufsiz)
1342
0
      return len;
1343
94
  }
1344
1345
172
  if (days || hours) {
1346
132
    len += snprintf(buf + len, bufsiz - len, "%.2d:", hours);
1347
132
    if (CAST(size_t, len) >= bufsiz)
1348
0
      return len;
1349
132
  }
1350
1351
172
  len += snprintf(buf + len, bufsiz - len, "%.2d:", mins);
1352
172
  if (CAST(size_t, len) >= bufsiz)
1353
0
    return len;
1354
1355
172
  len += snprintf(buf + len, bufsiz - len, "%.2d", secs);
1356
172
  return len;
1357
172
}
1358
1359
file_protected char *
1360
cdf_u16tos8(char *buf, size_t len, const uint16_t *p)
1361
0
{
1362
0
  size_t i;
1363
0
  for (i = 0; i < len && p[i]; i++)
1364
0
    buf[i] = CAST(char, p[i]);
1365
0
  buf[i] = '\0';
1366
0
  return buf;
1367
0
}
1368
1369
#ifdef CDF_DEBUG
1370
file_protected void
1371
cdf_dump_header(const cdf_header_t *h)
1372
{
1373
  size_t i;
1374
1375
#define DUMP(a, b) (void)fprintf(stderr, "%40.40s = " a "\n", # b, h->h_ ## b)
1376
#define DUMP2(a, b) (void)fprintf(stderr, "%40.40s = " a " (" a ")\n", # b, \
1377
    h->h_ ## b, 1 << h->h_ ## b)
1378
  DUMP("%d", revision);
1379
  DUMP("%d", version);
1380
  DUMP("%#x", byte_order);
1381
  DUMP2("%d", sec_size_p2);
1382
  DUMP2("%d", short_sec_size_p2);
1383
  DUMP("%d", num_sectors_in_sat);
1384
  DUMP("%d", secid_first_directory);
1385
  DUMP("%d", min_size_standard_stream);
1386
  DUMP("%d", secid_first_sector_in_short_sat);
1387
  DUMP("%d", num_sectors_in_short_sat);
1388
  DUMP("%d", secid_first_sector_in_master_sat);
1389
  DUMP("%d", num_sectors_in_master_sat);
1390
  for (i = 0; i < __arraycount(h->h_master_sat); i++) {
1391
    if (h->h_master_sat[i] == CDF_SECID_FREE)
1392
      break;
1393
    (void)fprintf(stderr, "%35.35s[%.3" SIZE_T_FORMAT "u] = %d\n",
1394
        "master_sat", i, h->h_master_sat[i]);
1395
  }
1396
}
1397
1398
file_protected void
1399
cdf_dump_sat(const char *prefix, const cdf_sat_t *sat, size_t size)
1400
{
1401
  size_t i, j, s = size / sizeof(cdf_secid_t);
1402
1403
  for (i = 0; i < sat->sat_len; i++) {
1404
    (void)fprintf(stderr, "%s[%" SIZE_T_FORMAT "u]:\n%.6"
1405
        SIZE_T_FORMAT "u: ", prefix, i, i * s);
1406
    for (j = 0; j < s; j++) {
1407
      (void)fprintf(stderr, "%5d, ",
1408
          CDF_TOLE4(sat->sat_tab[s * i + j]));
1409
      if ((j + 1) % 10 == 0)
1410
        (void)fprintf(stderr, "\n%.6" SIZE_T_FORMAT
1411
            "u: ", i * s + j + 1);
1412
    }
1413
    (void)fprintf(stderr, "\n");
1414
  }
1415
}
1416
1417
file_protected void
1418
cdf_dump(const void *v, size_t len)
1419
{
1420
  size_t i, j;
1421
  const unsigned char *p = v;
1422
  char abuf[16];
1423
1424
  (void)fprintf(stderr, "%.4x: ", 0);
1425
  for (i = 0, j = 0; i < len; i++, p++) {
1426
    (void)fprintf(stderr, "%.2x ", *p);
1427
    abuf[j++] = isprint(*p) ? *p : '.';
1428
    if (j == 16) {
1429
      j = 0;
1430
      abuf[15] = '\0';
1431
      (void)fprintf(stderr, "%s\n%.4" SIZE_T_FORMAT "x: ",
1432
          abuf, i + 1);
1433
    }
1434
  }
1435
  (void)fprintf(stderr, "\n");
1436
}
1437
1438
file_protected void
1439
cdf_dump_stream(const cdf_stream_t *sst)
1440
{
1441
  size_t ss = sst->sst_ss;
1442
  cdf_dump(sst->sst_tab, ss * sst->sst_len);
1443
}
1444
1445
file_protected void
1446
cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h,
1447
    const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
1448
    const cdf_dir_t *dir)
1449
{
1450
  size_t i, j;
1451
  cdf_directory_t *d;
1452
  char name[__arraycount(d->d_name)];
1453
  cdf_stream_t scn;
1454
  struct timespec ts;
1455
1456
  static const char *types[] = { "empty", "user storage",
1457
      "user stream", "lockbytes", "property", "root storage" };
1458
1459
  for (i = 0; i < dir->dir_len; i++) {
1460
    char buf[26];
1461
    d = &dir->dir_tab[i];
1462
    for (j = 0; j < sizeof(name); j++)
1463
      name[j] = (char)CDF_TOLE2(d->d_name[j]);
1464
    name[sizeof(name} - 1] = '\0';
1465
    (void)fprintf(stderr, "Directory %" SIZE_T_FORMAT "u: %s\n",
1466
        i, name);
1467
    if (d->d_type < __arraycount(types))
1468
      (void)fprintf(stderr, "Type: %s\n", types[d->d_type]);
1469
    else
1470
      (void)fprintf(stderr, "Type: %d\n", d->d_type);
1471
    (void)fprintf(stderr, "Color: %s\n",
1472
        d->d_color ? "black" : "red");
1473
    (void)fprintf(stderr, "Left child: %d\n", d->d_left_child);
1474
    (void)fprintf(stderr, "Right child: %d\n", d->d_right_child);
1475
    (void)fprintf(stderr, "Flags: %#x\n", d->d_flags);
1476
    cdf_timestamp_to_timespec(&ts, d->d_created);
1477
    (void)fprintf(stderr, "Created %s", cdf_ctime(&ts.tv_sec, buf));
1478
    cdf_timestamp_to_timespec(&ts, d->d_modified);
1479
    (void)fprintf(stderr, "Modified %s",
1480
        cdf_ctime(&ts.tv_sec, buf));
1481
    (void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector);
1482
    (void)fprintf(stderr, "Size %d\n", d->d_size);
1483
    switch (d->d_type) {
1484
    case CDF_DIR_TYPE_USER_STORAGE:
1485
      (void)fprintf(stderr, "Storage: %d\n", d->d_storage);
1486
      break;
1487
    case CDF_DIR_TYPE_USER_STREAM:
1488
      if (sst == NULL)
1489
        break;
1490
      if (cdf_read_sector_chain(info, h, sat, ssat, sst,
1491
          d->d_stream_first_sector, d->d_size, &scn) == -1) {
1492
        warn("Can't read stream for %s at %d len %d",
1493
            name, d->d_stream_first_sector, d->d_size);
1494
        break;
1495
      }
1496
      cdf_dump_stream(&scn);
1497
      free(scn.sst_tab);
1498
      break;
1499
    default:
1500
      break;
1501
    }
1502
1503
  }
1504
}
1505
1506
file_protected void
1507
cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
1508
{
1509
  cdf_timestamp_t tp;
1510
  struct timespec ts;
1511
  char buf[64];
1512
  size_t i, j;
1513
1514
  for (i = 0; i < count; i++) {
1515
    cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
1516
    (void)fprintf(stderr, "%" SIZE_T_FORMAT "u) %s: ", i, buf);
1517
    switch (info[i].pi_type) {
1518
    case CDF_NULL:
1519
      break;
1520
    case CDF_SIGNED16:
1521
      (void)fprintf(stderr, "signed 16 [%hd]\n",
1522
          info[i].pi_s16);
1523
      break;
1524
    case CDF_SIGNED32:
1525
      (void)fprintf(stderr, "signed 32 [%d]\n",
1526
          info[i].pi_s32);
1527
      break;
1528
    case CDF_UNSIGNED32:
1529
      (void)fprintf(stderr, "unsigned 32 [%u]\n",
1530
          info[i].pi_u32);
1531
      break;
1532
    case CDF_FLOAT:
1533
      (void)fprintf(stderr, "float [%g]\n",
1534
          info[i].pi_f);
1535
      break;
1536
    case CDF_DOUBLE:
1537
      (void)fprintf(stderr, "double [%g]\n",
1538
          info[i].pi_d);
1539
      break;
1540
    case CDF_LENGTH32_STRING:
1541
      (void)fprintf(stderr, "string %u [%.*s]\n",
1542
          info[i].pi_str.s_len,
1543
          info[i].pi_str.s_len, info[i].pi_str.s_buf);
1544
      break;
1545
    case CDF_LENGTH32_WSTRING:
1546
      (void)fprintf(stderr, "string %u [",
1547
          info[i].pi_str.s_len);
1548
      for (j = 0; j < info[i].pi_str.s_len - 1; j++)
1549
          (void)fputc(info[i].pi_str.s_buf[j << 1], stderr);
1550
      (void)fprintf(stderr, "]\n");
1551
      break;
1552
    case CDF_FILETIME:
1553
      tp = info[i].pi_tp;
1554
      if (tp < 1000000000000000LL) {
1555
        cdf_print_elapsed_time(buf, sizeof(buf), tp);
1556
        (void)fprintf(stderr, "timestamp %s\n", buf);
1557
      } else {
1558
        char tbuf[26];
1559
        cdf_timestamp_to_timespec(&ts, tp);
1560
        (void)fprintf(stderr, "timestamp %s",
1561
            cdf_ctime(&ts.tv_sec, tbuf));
1562
      }
1563
      break;
1564
    case CDF_CLIPBOARD:
1565
      (void)fprintf(stderr, "CLIPBOARD %u\n", info[i].pi_u32);
1566
      break;
1567
    default:
1568
      DPRINTF(("Don't know how to deal with %#x\n",
1569
          info[i].pi_type));
1570
      break;
1571
    }
1572
  }
1573
}
1574
1575
1576
file_protected void
1577
cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
1578
{
1579
  char buf[128];
1580
  cdf_summary_info_header_t ssi;
1581
  cdf_property_info_t *info;
1582
  size_t count;
1583
1584
  (void)&h;
1585
  if (cdf_unpack_summary_info(sst, h, &ssi, &info, &count) == -1)
1586
    return;
1587
  (void)fprintf(stderr, "Endian: %#x\n", ssi.si_byte_order);
1588
  (void)fprintf(stderr, "Os Version %d.%d\n", ssi.si_os_version & 0xff,
1589
      ssi.si_os_version >> 8);
1590
  (void)fprintf(stderr, "Os %d\n", ssi.si_os);
1591
  cdf_print_classid(buf, sizeof(buf), &ssi.si_class);
1592
  (void)fprintf(stderr, "Class %s\n", buf);
1593
  (void)fprintf(stderr, "Count %d\n", ssi.si_count);
1594
  cdf_dump_property_info(info, count);
1595
  free(info);
1596
}
1597
1598
1599
file_protected void
1600
cdf_dump_catalog(const cdf_header_t *h, const cdf_stream_t *sst)
1601
{
1602
  cdf_catalog_t *cat;
1603
  cdf_unpack_catalog(h, sst, &cat);
1604
  const cdf_catalog_entry_t *ce = cat->cat_e;
1605
  struct timespec ts;
1606
  char tbuf[64], sbuf[256];
1607
  size_t i;
1608
1609
  printf("Catalog:\n");
1610
  for (i = 0; i < cat->cat_num; i++) {
1611
    cdf_timestamp_to_timespec(&ts, ce[i].ce_timestamp);
1612
    printf("\t%d %s %s", ce[i].ce_num,
1613
        cdf_u16tos8(sbuf, ce[i].ce_namlen, ce[i].ce_name),
1614
        cdf_ctime(&ts.tv_sec, tbuf));
1615
  }
1616
  free(cat);
1617
}
1618
1619
#endif
1620
1621
#ifdef TEST
1622
int
1623
main(int argc, char *argv[])
1624
{
1625
  int i;
1626
  cdf_header_t h;
1627
  cdf_sat_t sat, ssat;
1628
  cdf_stream_t sst, scn;
1629
  cdf_dir_t dir;
1630
  cdf_info_t info;
1631
  const cdf_directory_t *root;
1632
#ifdef __linux__
1633
#define getprogname() __progname
1634
  extern char *__progname;
1635
#endif
1636
  if (argc < 2) {
1637
    (void)fprintf(stderr, "Usage: %s <filename>\n", getprogname());
1638
    return -1;
1639
  }
1640
1641
  info.i_buf = NULL;
1642
  info.i_len = 0;
1643
  for (i = 1; i < argc; i++) {
1644
    if ((info.i_fd = open(argv[1], O_RDONLY)) == -1)
1645
      err(EXIT_FAILURE, "Cannot open `%s'", argv[1]);
1646
1647
    if (cdf_read_header(&info, &h) == -1)
1648
      err(EXIT_FAILURE, "Cannot read header");
1649
#ifdef CDF_DEBUG
1650
    cdf_dump_header(&h);
1651
#endif
1652
1653
    if (cdf_read_sat(&info, &h, &sat) == -1)
1654
      err(EXIT_FAILURE, "Cannot read sat");
1655
#ifdef CDF_DEBUG
1656
    cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
1657
#endif
1658
1659
    if (cdf_read_ssat(&info, &h, &sat, &ssat) == -1)
1660
      err(EXIT_FAILURE, "Cannot read ssat");
1661
#ifdef CDF_DEBUG
1662
    cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
1663
#endif
1664
1665
    if (cdf_read_dir(&info, &h, &sat, &dir) == -1)
1666
      err(EXIT_FAILURE, "Cannot read dir");
1667
1668
    if (cdf_read_short_stream(&info, &h, &sat, &dir, &sst, &root)
1669
        == -1)
1670
      err(EXIT_FAILURE, "Cannot read short stream");
1671
#ifdef CDF_DEBUG
1672
    cdf_dump_stream(&sst);
1673
#endif
1674
1675
#ifdef CDF_DEBUG
1676
    cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
1677
#endif
1678
1679
1680
    if (cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
1681
        &scn) == -1)
1682
      warn("Cannot read summary info");
1683
#ifdef CDF_DEBUG
1684
    else
1685
      cdf_dump_summary_info(&h, &scn);
1686
#endif
1687
    if (cdf_read_user_stream(&info, &h, &sat, &ssat, &sst,
1688
        &dir, "Catalog", &scn) == -1)
1689
      warn("Cannot read catalog");
1690
#ifdef CDF_DEBUG
1691
    else
1692
      cdf_dump_catalog(&h, &scn);
1693
#endif
1694
1695
    (void)close(info.i_fd);
1696
  }
1697
1698
  return 0;
1699
}
1700
#endif