Coverage Report

Created: 2026-05-16 06:34

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.127 2026/05/09 22:06:12 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
6.75k
#define EFTYPE EINVAL
60
#endif
61
62
#ifndef SIZE_T_MAX
63
5.69M
#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
2.22k
#define NEED_SWAP (cdf_bo.u == CAST(uint32_t, 0x01020304))
80
81
#define CDF_TOLE8(x)  \
82
21.5k
    (CAST(uint64_t, NEED_SWAP ? _cdf_tole8(x) : CAST(uint64_t, x)))
83
#define CDF_TOLE4(x)  \
84
7.91M
    (CAST(uint32_t, NEED_SWAP ? _cdf_tole4(x) : CAST(uint32_t, x)))
85
#define CDF_TOLE2(x)  \
86
146k
    (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
919k
#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
2.33k
#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
2.25k
{
104
2.25k
  DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u\n",
105
2.25k
      file, line, __func__, n));
106
2.25k
  if (n == 0)
107
0
      n++;
108
2.25k
  return malloc(n);
109
2.25k
}
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
1.48k
{
116
1.48k
  DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u\n",
117
1.48k
      file, line, __func__, n));
118
1.48k
  return realloc(p, n);
119
1.48k
}
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
13.3k
{
126
13.3k
  DPRINTF(("%s,%" SIZE_T_FORMAT "u: %s %" SIZE_T_FORMAT "u %"
127
13.3k
      SIZE_T_FORMAT "u\n", file, line, __func__, n, u));
128
13.3k
  if (n == 0)
129
163
      n++;
130
13.3k
  return calloc(n, u);
131
13.3k
}
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
919k
{
200
919k
  uint32_t rv;
201
919k
  (void)memcpy(&rv, p + offs * sizeof(uint32_t), sizeof(rv));
202
919k
  return CDF_TOLE4(rv);
203
919k
}
204
205
#define CDF_UNPACK(a) \
206
1.65M
    (void)memcpy(&(a), &buf[len], sizeof(a)), len += sizeof(a)
207
#define CDF_UNPACKA(a)  \
208
160k
    (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
6.64k
{
231
6.64k
  size_t i;
232
233
6.64k
  h->h_magic = CDF_TOLE8(h->h_magic);
234
6.64k
  h->h_uuid[0] = CDF_TOLE8(h->h_uuid[0]);
235
6.64k
  h->h_uuid[1] = CDF_TOLE8(h->h_uuid[1]);
236
6.64k
  h->h_revision = CDF_TOLE2(h->h_revision);
237
6.64k
  h->h_version = CDF_TOLE2(h->h_version);
238
6.64k
  h->h_byte_order = CDF_TOLE2(h->h_byte_order);
239
6.64k
  h->h_sec_size_p2 = CDF_TOLE2(h->h_sec_size_p2);
240
6.64k
  h->h_short_sec_size_p2 = CDF_TOLE2(h->h_short_sec_size_p2);
241
6.64k
  h->h_num_sectors_in_sat = CDF_TOLE4(h->h_num_sectors_in_sat);
242
6.64k
  h->h_secid_first_directory = CDF_TOLE4(h->h_secid_first_directory);
243
6.64k
  h->h_min_size_standard_stream =
244
6.64k
      CDF_TOLE4(h->h_min_size_standard_stream);
245
6.64k
  h->h_secid_first_sector_in_short_sat =
246
6.64k
      CDF_TOLE4(CAST(uint32_t, h->h_secid_first_sector_in_short_sat));
247
6.64k
  h->h_num_sectors_in_short_sat =
248
6.64k
      CDF_TOLE4(h->h_num_sectors_in_short_sat);
249
6.64k
  h->h_secid_first_sector_in_master_sat =
250
6.64k
      CDF_TOLE4(CAST(uint32_t, h->h_secid_first_sector_in_master_sat));
251
6.64k
  h->h_num_sectors_in_master_sat =
252
6.64k
      CDF_TOLE4(h->h_num_sectors_in_master_sat);
253
730k
  for (i = 0; i < __arraycount(h->h_master_sat); i++) {
254
723k
    h->h_master_sat[i] =
255
723k
        CDF_TOLE4(CAST(uint32_t, h->h_master_sat[i]));
256
723k
  }
257
6.64k
}
258
259
file_protected void
260
cdf_unpack_header(cdf_header_t *h, char *buf)
261
6.64k
{
262
6.64k
  size_t i;
263
6.64k
  size_t len = 0;
264
265
6.64k
  CDF_UNPACK(h->h_magic);
266
6.64k
  CDF_UNPACKA(h->h_uuid);
267
6.64k
  CDF_UNPACK(h->h_revision);
268
6.64k
  CDF_UNPACK(h->h_version);
269
6.64k
  CDF_UNPACK(h->h_byte_order);
270
6.64k
  CDF_UNPACK(h->h_sec_size_p2);
271
6.64k
  CDF_UNPACK(h->h_short_sec_size_p2);
272
6.64k
  CDF_UNPACKA(h->h_unused0);
273
6.64k
  CDF_UNPACK(h->h_num_sectors_in_sat);
274
6.64k
  CDF_UNPACK(h->h_secid_first_directory);
275
6.64k
  CDF_UNPACKA(h->h_unused1);
276
6.64k
  CDF_UNPACK(h->h_min_size_standard_stream);
277
6.64k
  CDF_UNPACK(h->h_secid_first_sector_in_short_sat);
278
6.64k
  CDF_UNPACK(h->h_num_sectors_in_short_sat);
279
6.64k
  CDF_UNPACK(h->h_secid_first_sector_in_master_sat);
280
6.64k
  CDF_UNPACK(h->h_num_sectors_in_master_sat);
281
730k
  for (i = 0; i < __arraycount(h->h_master_sat); i++)
282
723k
    CDF_UNPACK(h->h_master_sat[i]);
283
6.64k
}
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
1.57k
{
305
1.57k
  d->cl_dword = CDF_TOLE4(d->cl_dword);
306
1.57k
  d->cl_word[0] = CDF_TOLE2(d->cl_word[0]);
307
1.57k
  d->cl_word[1] = CDF_TOLE2(d->cl_word[1]);
308
1.57k
}
309
310
file_protected void
311
cdf_unpack_dir(cdf_directory_t *d, char *buf)
312
70.0k
{
313
70.0k
  size_t len = 0;
314
315
70.0k
  CDF_UNPACKA(d->d_name);
316
70.0k
  CDF_UNPACK(d->d_namelen);
317
70.0k
  CDF_UNPACK(d->d_type);
318
70.0k
  CDF_UNPACK(d->d_color);
319
70.0k
  CDF_UNPACK(d->d_left_child);
320
70.0k
  CDF_UNPACK(d->d_right_child);
321
70.0k
  CDF_UNPACK(d->d_storage);
322
70.0k
  CDF_UNPACKA(d->d_storage_uuid);
323
70.0k
  CDF_UNPACK(d->d_flags);
324
70.0k
  CDF_UNPACK(d->d_created);
325
70.0k
  CDF_UNPACK(d->d_modified);
326
70.0k
  CDF_UNPACK(d->d_stream_first_sector);
327
70.0k
  CDF_UNPACK(d->d_size);
328
70.0k
  CDF_UNPACK(d->d_unused0);
329
70.0k
}
330
331
file_protected int
332
cdf_zero_stream(cdf_stream_t *scn)
333
7.45k
{
334
7.45k
  scn->sst_len = 0;
335
7.45k
  scn->sst_dirlen = 0;
336
7.45k
  scn->sst_ss = 0;
337
7.45k
  free(scn->sst_tab);
338
7.45k
  scn->sst_tab = NULL;
339
7.45k
  return -1;
340
7.45k
}
341
342
file_private size_t
343
cdf_check_stream(const cdf_stream_t *sst, const cdf_header_t *h)
344
22.3k
{
345
22.3k
  size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
346
22.3k
      CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
347
22.3k
  assert(ss == sst->sst_ss);
348
22.3k
  return sst->sst_ss;
349
22.3k
}
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
22.3k
{
355
22.3k
  const char *b = RCAST(const char *, sst->sst_tab);
356
22.3k
  const char *e = RCAST(const char *, p) + tail;
357
22.3k
  size_t ss = cdf_check_stream(sst, h);
358
22.3k
  /*LINTED*/(void)&line;
359
22.3k
  if (e >= b && CAST(size_t, e - b) <= ss * sst->sst_len)
360
22.0k
    return 0;
361
252
  DPRINTF(("%d: offset begin %p < end %p || %" SIZE_T_FORMAT "u"
362
252
      " > %" SIZE_T_FORMAT "u [%" SIZE_T_FORMAT "u %"
363
252
      SIZE_T_FORMAT "u]\n", line, b, e, (size_t)(e - b),
364
252
      ss * sst->sst_len, ss, sst->sst_len));
365
252
  errno = EFTYPE;
366
252
  return -1;
367
22.3k
}
368
369
file_private ssize_t
370
cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len)
371
5.72M
{
372
5.72M
  size_t siz = CAST(size_t, off + len);
373
374
5.72M
  if (CAST(off_t, off + len) != CAST(off_t, siz))
375
0
    goto out;
376
377
5.72M
  if (info->i_buf != NULL && info->i_len >= siz) {
378
5.70M
    (void)memcpy(buf, &info->i_buf[off], len);
379
5.70M
    return CAST(ssize_t, len);
380
5.70M
  }
381
382
17.9k
  if (info->i_fd == -1)
383
10.8k
    goto out;
384
385
7.15k
  if (pread(info->i_fd, buf, len, off) != CAST(ssize_t, len))
386
7.15k
    return -1;
387
388
0
  return CAST(ssize_t, len);
389
10.8k
out:
390
10.8k
  errno = EINVAL;
391
10.8k
  return -1;
392
7.15k
}
393
394
file_protected int
395
cdf_read_header(const cdf_info_t *info, cdf_header_t *h)
396
24.1k
{
397
24.1k
  char buf[512];
398
399
24.1k
  (void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
400
24.1k
  if (cdf_read(info, CAST(off_t, 0), buf, sizeof(buf)) == -1)
401
17.5k
    return -1;
402
6.64k
  cdf_unpack_header(h, buf);
403
6.64k
  cdf_swap_header(h);
404
6.64k
  if (h->h_magic != CDF_MAGIC) {
405
3.36k
    DPRINTF(("Bad magic %#" INT64_T_FORMAT "x != %#"
406
3.36k
        INT64_T_FORMAT "x\n",
407
3.36k
        (unsigned long long)h->h_magic,
408
3.36k
        (unsigned long long)CDF_MAGIC));
409
3.36k
    goto out;
410
3.36k
  }
411
3.27k
  if (h->h_sec_size_p2 > 20) {
412
16
    DPRINTF(("Bad sector size %hu\n", h->h_sec_size_p2));
413
16
    goto out;
414
16
  }
415
3.25k
  if (h->h_short_sec_size_p2 > 20) {
416
17
    DPRINTF(("Bad short sector size %hu\n",
417
17
        h->h_short_sec_size_p2));
418
17
    goto out;
419
17
  }
420
3.24k
  return 0;
421
3.39k
out:
422
3.39k
  errno = EFTYPE;
423
3.39k
  return -1;
424
3.25k
}
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
5.69M
{
431
5.69M
  size_t ss = CDF_SEC_SIZE(h);
432
5.69M
  size_t pos;
433
434
5.69M
  if (SIZE_T_MAX / ss < CAST(size_t, id))
435
0
    return -1;
436
437
5.69M
  pos = CDF_SEC_POS(h, id);
438
5.69M
  assert(ss == len);
439
5.69M
  return cdf_read(info, CAST(off_t, pos), RCAST(char *, buf) + offs, len);
440
5.69M
}
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
1.19k
{
446
1.19k
  size_t ss = CDF_SHORT_SEC_SIZE(h);
447
1.19k
  size_t pos;
448
449
1.19k
  if (SIZE_T_MAX / ss < CAST(size_t, id))
450
0
    return -1;
451
452
1.19k
  pos = CDF_SHORT_SEC_POS(h, id);
453
1.19k
  assert(ss == len);
454
1.19k
  if (pos + len > CDF_SEC_SIZE(h) * sst->sst_len) {
455
43
    DPRINTF(("Out of bounds read %" SIZE_T_FORMAT "u > %"
456
43
        SIZE_T_FORMAT "u\n",
457
43
        pos + len, CDF_SEC_SIZE(h) * sst->sst_len));
458
43
    goto out;
459
43
  }
460
1.15k
  (void)memcpy(RCAST(char *, buf) + offs,
461
1.15k
      RCAST(const char *, sst->sst_tab) + pos, len);
462
1.15k
  return len;
463
43
out:
464
43
  errno = EFTYPE;
465
43
  return -1;
466
1.19k
}
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
3.24k
{
474
3.24k
  size_t i, j, k;
475
3.24k
  size_t ss = CDF_SEC_SIZE(h);
476
3.24k
  cdf_secid_t *msa, mid, sec;
477
3.24k
  size_t nsatpersec = (ss / sizeof(mid)) - 1;
478
479
224k
  for (i = 0; i < __arraycount(h->h_master_sat); i++)
480
222k
    if (h->h_master_sat[i] == CDF_SECID_FREE)
481
1.51k
      break;
482
483
6.21k
#define CDF_SEC_LIMIT (UINT32_MAX / (64 * ss))
484
3.24k
  if ((nsatpersec > 0 &&
485
3.00k
      h->h_num_sectors_in_master_sat > CDF_SEC_LIMIT / nsatpersec) ||
486
3.20k
      i > CDF_SEC_LIMIT) {
487
44
    DPRINTF(("Number of sectors in master SAT too big %u %"
488
44
        SIZE_T_FORMAT "u\n", h->h_num_sectors_in_master_sat, i));
489
44
    errno = EFTYPE;
490
44
    return -1;
491
44
  }
492
493
3.19k
  sat->sat_len = h->h_num_sectors_in_master_sat * nsatpersec + i;
494
3.19k
  DPRINTF(("sat_len = %" SIZE_T_FORMAT "u ss = %" SIZE_T_FORMAT "u\n",
495
3.19k
      sat->sat_len, ss));
496
3.19k
  if ((sat->sat_tab = CAST(cdf_secid_t *, CDF_CALLOC(sat->sat_len, ss)))
497
3.19k
      == NULL)
498
0
    return -1;
499
500
64.3k
  for (i = 0; i < __arraycount(h->h_master_sat); i++) {
501
63.9k
    if (h->h_master_sat[i] < 0)
502
2.63k
      break;
503
61.2k
    if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
504
61.2k
        h->h_master_sat[i]) != CAST(ssize_t, ss)) {
505
90
      DPRINTF(("Reading sector %d", h->h_master_sat[i]));
506
90
      goto out1;
507
90
    }
508
61.2k
  }
509
510
3.10k
  if ((msa = CAST(cdf_secid_t *, CDF_CALLOC(1, ss))) == NULL)
511
0
    goto out1;
512
513
3.10k
  mid = h->h_secid_first_sector_in_master_sat;
514
292k
  for (j = 0; j < h->h_num_sectors_in_master_sat; j++) {
515
291k
    if (mid < 0)
516
754
      goto out;
517
290k
    if (j >= CDF_LOOP_LIMIT) {
518
15
      DPRINTF(("Reading master sector loop limit"));
519
15
      goto out3;
520
15
    }
521
290k
    if (cdf_read_sector(info, msa, 0, ss, h, mid) !=
522
290k
        CAST(ssize_t, ss)) {
523
115
      DPRINTF(("Reading master sector %d", mid));
524
115
      goto out2;
525
115
    }
526
5.61M
    for (k = 0; k < nsatpersec; k++, i++) {
527
5.32M
      sec = CDF_TOLE4(CAST(uint32_t, msa[k]));
528
5.32M
      if (sec < 0)
529
718
        goto out;
530
5.32M
      if (i >= sat->sat_len) {
531
0
          DPRINTF(("Out of bounds reading MSA %"
532
0
        SIZE_T_FORMAT "u >= %" SIZE_T_FORMAT "u",
533
0
        i, sat->sat_len));
534
0
          goto out3;
535
0
      }
536
5.32M
      if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
537
5.32M
          sec) != CAST(ssize_t, ss)) {
538
88
        DPRINTF(("Reading sector %d",
539
88
            CDF_TOLE4(msa[k])));
540
88
        goto out2;
541
88
      }
542
5.32M
    }
543
289k
    mid = CDF_TOLE4(CAST(uint32_t, msa[nsatpersec]));
544
289k
  }
545
2.88k
out:
546
2.88k
  sat->sat_len = i;
547
2.88k
  free(msa);
548
2.88k
  return 0;
549
15
out3:
550
15
  errno = EFTYPE;
551
218
out2:
552
218
  free(msa);
553
308
out1:
554
308
  free(sat->sat_tab);
555
308
  return -1;
556
218
}
557
558
size_t
559
cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size)
560
8.16k
{
561
8.16k
  size_t i, j;
562
8.16k
  cdf_secid_t maxsector = CAST(cdf_secid_t, (sat->sat_len * size)
563
8.16k
      / sizeof(maxsector));
564
565
8.16k
  DPRINTF(("Chain:"));
566
8.16k
  if (sid == CDF_SECID_END_OF_CHAIN) {
567
    /* 0-length chain. */
568
99
    DPRINTF((" empty\n"));
569
99
    return 0;
570
99
  }
571
572
589k
  for (j = i = 0; sid >= 0; i++, j++) {
573
581k
    DPRINTF((" %d", sid));
574
581k
    if (j >= CDF_LOOP_LIMIT) {
575
56
      DPRINTF(("Counting chain loop limit"));
576
56
      goto out;
577
56
    }
578
581k
    if (sid >= maxsector) {
579
601
      DPRINTF(("Sector %d >= %d\n", sid, maxsector));
580
601
      goto out;
581
601
    }
582
581k
    sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
583
581k
  }
584
7.40k
  if (i == 0) {
585
389
    DPRINTF((" none, sid: %d\n", sid));
586
389
    goto out;
587
588
389
  }
589
7.02k
  DPRINTF(("\n"));
590
7.02k
  return i;
591
1.04k
out:
592
1.04k
  errno = EFTYPE;
593
1.04k
  return CAST(size_t, -1);
594
7.40k
}
595
596
file_protected int
597
cdf_read_long_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
598
    const cdf_sat_t *sat, cdf_secid_t sid, size_t len, cdf_stream_t *scn)
599
2.26k
{
600
2.26k
  size_t ss = CDF_SEC_SIZE(h), i, j;
601
2.26k
  ssize_t nr;
602
2.26k
  scn->sst_tab = NULL;
603
2.26k
  scn->sst_len = cdf_count_chain(sat, sid, ss);
604
2.26k
  scn->sst_dirlen = MAX(h->h_min_size_standard_stream, len);
605
2.26k
  scn->sst_ss = ss;
606
607
2.26k
  if (sid == CDF_SECID_END_OF_CHAIN || len == 0)
608
132
    return cdf_zero_stream(scn);
609
610
2.13k
  if (scn->sst_len == CAST(size_t, -1))
611
182
    goto out;
612
613
1.94k
  scn->sst_tab = CDF_CALLOC(scn->sst_len, ss);
614
1.94k
  if (scn->sst_tab == NULL)
615
0
    return cdf_zero_stream(scn);
616
617
8.41k
  for (j = i = 0; sid >= 0; i++, j++) {
618
6.53k
    if (j >= CDF_LOOP_LIMIT) {
619
0
      DPRINTF(("Read long sector chain loop limit"));
620
0
      goto out;
621
0
    }
622
6.53k
    if (i >= scn->sst_len) {
623
0
      DPRINTF(("Out of bounds reading long sector chain "
624
0
          "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n", i,
625
0
          scn->sst_len));
626
0
      goto out;
627
0
    }
628
6.53k
    if ((nr = cdf_read_sector(info, scn->sst_tab, i * ss, ss, h,
629
6.53k
        sid)) != CAST(ssize_t, ss)) {
630
63
      if (i == scn->sst_len - 1 && nr > 0) {
631
        /* Last sector might be truncated */
632
0
        return 0;
633
0
      }
634
63
      DPRINTF(("Reading long sector chain %d", sid));
635
63
      goto out;
636
63
    }
637
6.47k
    sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
638
6.47k
  }
639
1.88k
  return 0;
640
245
out:
641
245
  errno = EFTYPE;
642
245
  return cdf_zero_stream(scn);
643
1.94k
}
644
645
file_protected int
646
cdf_read_short_sector_chain(const cdf_header_t *h,
647
    const cdf_sat_t *ssat, const cdf_stream_t *sst,
648
    cdf_secid_t sid, size_t len, cdf_stream_t *scn)
649
579
{
650
579
  size_t ss = CDF_SHORT_SEC_SIZE(h), i, j;
651
579
  scn->sst_tab = NULL;
652
579
  scn->sst_len = cdf_count_chain(ssat, sid, CDF_SEC_SIZE(h));
653
579
  scn->sst_dirlen = len;
654
579
  scn->sst_ss = ss;
655
656
579
  if (scn->sst_len == CAST(size_t, -1))
657
189
    goto out;
658
659
390
  scn->sst_tab = CDF_CALLOC(scn->sst_len, ss);
660
390
  if (scn->sst_tab == NULL)
661
0
    return cdf_zero_stream(scn);
662
663
1.54k
  for (j = i = 0; sid >= 0; i++, j++) {
664
1.19k
    if (j >= CDF_LOOP_LIMIT) {
665
0
      DPRINTF(("Read short sector chain loop limit"));
666
0
      goto out;
667
0
    }
668
1.19k
    if (i >= scn->sst_len) {
669
0
      DPRINTF(("Out of bounds reading short sector chain "
670
0
          "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n",
671
0
          i, scn->sst_len));
672
0
      goto out;
673
0
    }
674
1.19k
    if (cdf_read_short_sector(sst, scn->sst_tab, i * ss, ss, h,
675
1.19k
        sid) != CAST(ssize_t, ss)) {
676
43
      DPRINTF(("Reading short sector chain %d", sid));
677
43
      goto out;
678
43
    }
679
1.15k
    sid = CDF_TOLE4(CAST(uint32_t, ssat->sat_tab[sid]));
680
1.15k
  }
681
347
  return 0;
682
232
out:
683
232
  errno = EFTYPE;
684
232
  return cdf_zero_stream(scn);
685
390
}
686
687
file_protected int
688
cdf_read_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
689
    const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
690
    cdf_secid_t sid, size_t len, cdf_stream_t *scn)
691
2.03k
{
692
693
2.03k
  if (len < h->h_min_size_standard_stream && sst->sst_tab != NULL)
694
579
    return cdf_read_short_sector_chain(h, ssat, sst, sid, len,
695
579
        scn);
696
1.46k
  else
697
1.46k
    return cdf_read_long_sector_chain(info, h, sat, sid, len, scn);
698
2.03k
}
699
700
file_protected int
701
cdf_read_dir(const cdf_info_t *info, const cdf_header_t *h,
702
    const cdf_sat_t *sat, cdf_dir_t *dir)
703
2.43k
{
704
2.43k
  size_t i, j;
705
2.43k
  size_t ss = CDF_SEC_SIZE(h), ns, nd;
706
2.43k
  char *buf;
707
2.43k
  cdf_secid_t sid = h->h_secid_first_directory;
708
709
2.43k
  ns = cdf_count_chain(sat, sid, ss);
710
2.43k
  if (ns == CAST(size_t, -1))
711
183
    return -1;
712
713
2.25k
  nd = ss / CDF_DIRECTORY_SIZE;
714
715
2.25k
  dir->dir_len = ns * nd;
716
2.25k
  dir->dir_tab = CAST(cdf_directory_t *,
717
2.25k
      CDF_CALLOC(dir->dir_len, sizeof(dir->dir_tab[0])));
718
2.25k
  if (dir->dir_tab == NULL)
719
0
    return -1;
720
721
2.25k
  if ((buf = CAST(char *, CDF_MALLOC(ss))) == NULL) {
722
0
    free(dir->dir_tab);
723
0
    return -1;
724
0
  }
725
726
9.90k
  for (j = i = 0; i < ns; i++, j++) {
727
7.68k
    if (j >= CDF_LOOP_LIMIT) {
728
0
      DPRINTF(("Read dir loop limit"));
729
0
      goto out;
730
0
    }
731
7.68k
    if (cdf_read_sector(info, buf, 0, ss, h, sid) !=
732
7.68k
        CAST(ssize_t, ss)) {
733
30
      DPRINTF(("Reading directory sector %d", sid));
734
30
      goto out;
735
30
    }
736
77.7k
    for (j = 0; j < nd; j++) {
737
70.0k
      cdf_unpack_dir(&dir->dir_tab[i * nd + j],
738
70.0k
          &buf[j * CDF_DIRECTORY_SIZE]);
739
70.0k
    }
740
7.65k
    sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
741
7.65k
  }
742
2.22k
  if (NEED_SWAP)
743
0
    for (i = 0; i < dir->dir_len; i++)
744
0
      cdf_swap_dir(&dir->dir_tab[i]);
745
2.22k
  free(buf);
746
2.22k
  return 0;
747
30
out:
748
30
  free(dir->dir_tab);
749
30
  free(buf);
750
30
  errno = EFTYPE;
751
30
  return -1;
752
2.25k
}
753
754
755
file_protected int
756
cdf_read_ssat(const cdf_info_t *info, const cdf_header_t *h,
757
    const cdf_sat_t *sat, cdf_sat_t *ssat)
758
2.88k
{
759
2.88k
  size_t i, j;
760
2.88k
  size_t ss = CDF_SEC_SIZE(h);
761
2.88k
  cdf_secid_t sid = h->h_secid_first_sector_in_short_sat;
762
763
2.88k
  ssat->sat_tab = NULL;
764
2.88k
  ssat->sat_len = cdf_count_chain(sat, sid, ss);
765
2.88k
  if (ssat->sat_len == CAST(size_t, -1))
766
403
    goto out;
767
768
2.48k
  ssat->sat_tab = CAST(cdf_secid_t *, CDF_CALLOC(ssat->sat_len, ss));
769
2.48k
  if (ssat->sat_tab == NULL)
770
0
    goto out1;
771
772
7.79k
  for (j = i = 0; sid >= 0; i++, j++) {
773
5.35k
    if (j >= CDF_LOOP_LIMIT) {
774
0
      DPRINTF(("Read short sat sector loop limit"));
775
0
      goto out;
776
0
    }
777
5.35k
    if (i >= ssat->sat_len) {
778
0
      DPRINTF(("Out of bounds reading short sector chain "
779
0
          "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n", i,
780
0
          ssat->sat_len));
781
0
      goto out;
782
0
    }
783
5.35k
    if (cdf_read_sector(info, ssat->sat_tab, i * ss, ss, h, sid) !=
784
5.35k
        CAST(ssize_t, ss)) {
785
51
      DPRINTF(("Reading short sat sector %d", sid));
786
51
      goto out1;
787
51
    }
788
5.30k
    sid = CDF_TOLE4(CAST(uint32_t, sat->sat_tab[sid]));
789
5.30k
  }
790
2.43k
  return 0;
791
403
out:
792
403
  errno = EFTYPE;
793
454
out1:
794
454
  free(ssat->sat_tab);
795
454
  return -1;
796
403
}
797
798
file_protected int
799
cdf_read_short_stream(const cdf_info_t *info, const cdf_header_t *h,
800
    const cdf_sat_t *sat, const cdf_dir_t *dir, cdf_stream_t *scn,
801
    const cdf_directory_t **root)
802
2.22k
{
803
2.22k
  size_t i;
804
2.22k
  const cdf_directory_t *d;
805
806
2.22k
  *root = NULL;
807
20.9k
  for (i = 0; i < dir->dir_len; i++)
808
19.7k
    if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_ROOT_STORAGE)
809
1.02k
      break;
810
811
  /* If the it is not there, just fake it; some docs don't have it */
812
2.22k
  if (i == dir->dir_len) {
813
1.20k
    DPRINTF(("Cannot find root storage dir\n"));
814
1.20k
    goto out;
815
1.20k
  }
816
1.02k
  d = &dir->dir_tab[i];
817
1.02k
  *root = d;
818
819
  /* If the it is not there, just fake it; some docs don't have it */
820
1.02k
  if (d->d_stream_first_sector < 0) {
821
220
    DPRINTF(("No first secror in dir\n"));
822
220
    goto out;
823
220
  }
824
825
802
  return cdf_read_long_sector_chain(info, h, sat,
826
802
      d->d_stream_first_sector, d->d_size, scn);
827
1.42k
out:
828
1.42k
  scn->sst_tab = NULL;
829
1.42k
  (void)cdf_zero_stream(scn);
830
1.42k
  return 0;
831
1.02k
}
832
833
file_protected int
834
cdf_namecmp(const char *d, const uint16_t *s, size_t l)
835
31.1k
{
836
77.2k
  for (; l--; d++, s++)
837
75.1k
    if (*d != CDF_TOLE2(*s))
838
29.1k
      return CAST(unsigned char, *d) - CDF_TOLE2(*s);
839
2.08k
  return 0;
840
31.1k
}
841
842
file_protected int
843
cdf_read_doc_summary_info(const cdf_info_t *info, const cdf_header_t *h,
844
    const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
845
    const cdf_dir_t *dir, cdf_stream_t *scn)
846
1.74k
{
847
1.74k
  return cdf_read_user_stream(info, h, sat, ssat, sst, dir,
848
1.74k
      "\05DocumentSummaryInformation", scn);
849
1.74k
}
850
851
file_protected int
852
cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h,
853
    const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
854
    const cdf_dir_t *dir, cdf_stream_t *scn)
855
2.01k
{
856
2.01k
  return cdf_read_user_stream(info, h, sat, ssat, sst, dir,
857
2.01k
      "\05SummaryInformation", scn);
858
2.01k
}
859
860
file_protected int
861
cdf_read_user_stream(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, const char *name, cdf_stream_t *scn)
864
5.77k
{
865
5.77k
  const cdf_directory_t *d;
866
5.77k
  int i = cdf_find_stream(dir, name, CDF_DIR_TYPE_USER_STREAM);
867
868
5.77k
  if (i <= 0) {
869
3.73k
    memset(scn, 0, sizeof(*scn));
870
3.73k
    return -1;
871
3.73k
  }
872
873
2.03k
  d = &dir->dir_tab[i - 1];
874
2.03k
  return cdf_read_sector_chain(info, h, sat, ssat, sst,
875
2.03k
      d->d_stream_first_sector, d->d_size, scn);
876
5.77k
}
877
878
file_protected int
879
cdf_find_stream(const cdf_dir_t *dir, const char *name, int type)
880
5.77k
{
881
5.77k
  size_t i, name_len = strlen(name) + 1;
882
883
130k
  for (i = 1; i <= dir->dir_len; i++)
884
126k
    if (dir->dir_tab[i-1].d_type == type &&
885
8.31k
        cdf_namecmp(name, dir->dir_tab[i-1].d_name, name_len)
886
8.31k
        == 0)
887
2.03k
      break;
888
5.77k
  if (i <= dir->dir_len)
889
2.03k
    return CAST(int, i);
890
891
3.73k
  DPRINTF(("Cannot find type %d `%s'\n", type, name));
892
3.73k
  errno = ESRCH;
893
3.73k
  return 0;
894
5.77k
}
895
896
1.43k
#define CDF_SHLEN_LIMIT (UINT32_MAX / 64)
897
2.78k
#define CDF_PROP_LIMIT (UINT32_MAX / (64 * sizeof(cdf_property_info_t)))
898
899
file_private const void *
900
cdf_offset(const void *p, size_t l)
901
18.6k
{
902
18.6k
  return CAST(const void *, CAST(const uint8_t *, p) + l);
903
18.6k
}
904
905
file_private const uint8_t *
906
cdf_get_property_info_pos(const cdf_stream_t *sst, const cdf_header_t *h,
907
    const uint8_t *p, const uint8_t *e, size_t i)
908
14.9k
{
909
14.9k
  size_t tail = (i << 1) + 1;
910
14.9k
  size_t ofs;
911
912
14.9k
  if (p >= e) {
913
0
    DPRINTF(("Past end %p < %p\n", e, p));
914
0
    return NULL;
915
0
  }
916
917
14.9k
  if (cdf_check_stream_offset(sst, h, p, (tail + 1) * sizeof(uint32_t),
918
14.9k
      __LINE__) == -1)
919
22
    return NULL;
920
921
14.9k
  ofs = CDF_GETUINT32(p, tail);
922
14.9k
  if (ofs < 2 * sizeof(uint32_t)) {
923
49
    DPRINTF(("Offset too small %zu\n", ofs));
924
49
    return NULL;
925
49
  }
926
927
14.9k
  ofs -= 2 * sizeof(uint32_t);
928
14.9k
  if (ofs > CAST(size_t, e - p)) {
929
390
    DPRINTF(("Offset too big %zu %td\n", ofs, e - p));
930
390
    return NULL;
931
390
  }
932
933
14.5k
  return CAST(const uint8_t *, cdf_offset(CAST(const void *, p), ofs));
934
14.9k
}
935
936
file_private cdf_property_info_t *
937
cdf_grow_info(cdf_property_info_t **info, size_t *maxcount, size_t incr)
938
1.48k
{
939
1.48k
  cdf_property_info_t *inp;
940
1.48k
  size_t newcount = *maxcount + incr;
941
942
1.48k
  if (newcount > CDF_PROP_LIMIT) {
943
5
    DPRINTF(("exceeded property limit %" SIZE_T_FORMAT "u > %"
944
5
        SIZE_T_FORMAT "u\n", newcount, CDF_PROP_LIMIT));
945
5
    goto out;
946
5
  }
947
1.48k
  inp = CAST(cdf_property_info_t *,
948
1.48k
      CDF_REALLOC(*info, newcount * sizeof(*inp)));
949
1.48k
  if (inp == NULL)
950
0
    goto out;
951
952
1.48k
  *info = inp;
953
1.48k
  *maxcount = newcount;
954
1.48k
  return inp;
955
5
out:
956
5
  free(*info);
957
5
  *maxcount = 0;
958
5
  *info = NULL;
959
5
  return NULL;
960
1.48k
}
961
962
file_private int
963
cdf_copy_info(cdf_property_info_t *inp, const void *p, const void *e,
964
    size_t len)
965
6.17k
{
966
6.17k
  if (inp->pi_type & CDF_VECTOR)
967
974
    return 0;
968
969
5.20k
  if (CAST(size_t, CAST(const char *, e) - CAST(const char *, p)) < len)
970
1.35k
    return 0;
971
972
3.84k
  (void)memcpy(&inp->pi_val, p, len);
973
974
3.84k
  switch (len) {
975
701
  case 2:
976
701
    inp->pi_u16 = CDF_TOLE2(inp->pi_u16);
977
701
    break;
978
1.48k
  case 4:
979
1.48k
    inp->pi_u32 = CDF_TOLE4(inp->pi_u32);
980
1.48k
    break;
981
1.65k
  case 8:
982
1.65k
    inp->pi_u64 = CDF_TOLE8(inp->pi_u64);
983
1.65k
    break;
984
0
  default:
985
0
    abort();
986
3.84k
  }
987
3.84k
  return 1;
988
3.84k
}
989
990
file_protected int
991
cdf_read_property_info(const cdf_stream_t *sst, const cdf_header_t *h,
992
    uint32_t offs, cdf_property_info_t **info, size_t *count, size_t *maxcount)
993
1.57k
{
994
1.57k
  const cdf_section_header_t *shp;
995
1.57k
  cdf_section_header_t sh;
996
1.57k
  const uint8_t *p, *q, *e;
997
1.57k
  size_t i, o4, nelements, j, slen, left;
998
1.57k
  cdf_property_info_t *inp;
999
1000
1.57k
  if (offs > UINT32_MAX / 4) {
1001
41
    errno = EFTYPE;
1002
41
    goto out;
1003
41
  }
1004
1.52k
  shp = CAST(const cdf_section_header_t *,
1005
1.52k
      cdf_offset(sst->sst_tab, offs));
1006
1.52k
  if (cdf_check_stream_offset(sst, h, shp, sizeof(*shp), __LINE__) == -1)
1007
96
    goto out;
1008
1.43k
  sh.sh_len = CDF_TOLE4(shp->sh_len);
1009
1.43k
  if (sh.sh_len > CDF_SHLEN_LIMIT) {
1010
30
    errno = EFTYPE;
1011
30
    goto out;
1012
30
  }
1013
1014
1.40k
  if (cdf_check_stream_offset(sst, h, shp, sh.sh_len, __LINE__) == -1)
1015
103
    goto out;
1016
1017
1.30k
  sh.sh_properties = CDF_TOLE4(shp->sh_properties);
1018
1.30k
  DPRINTF(("section len: %u properties %u\n", sh.sh_len,
1019
1.30k
      sh.sh_properties));
1020
1.30k
  if (sh.sh_properties > CDF_PROP_LIMIT)
1021
27
    goto out;
1022
1.27k
  inp = cdf_grow_info(info, maxcount, sh.sh_properties);
1023
1.27k
  if (inp == NULL)
1024
0
    goto out;
1025
1.27k
  inp += *count;
1026
1.27k
  *count += sh.sh_properties;
1027
1.27k
  p = CAST(const uint8_t *, cdf_offset(sst->sst_tab, offs + sizeof(sh)));
1028
1.27k
  e = CAST(const uint8_t *, cdf_offset(shp, sh.sh_len));
1029
1.27k
  if (p >= e || cdf_check_stream_offset(sst, h, e, 0, __LINE__) == -1)
1030
28
    goto out;
1031
1032
15.5k
  for (i = 0; i < sh.sh_properties; i++) {
1033
14.9k
    if ((q = cdf_get_property_info_pos(sst, h, p, e, i)) == NULL)
1034
461
      goto out;
1035
14.5k
    inp[i].pi_id = CDF_GETUINT32(p, i << 1);
1036
14.5k
    left = CAST(size_t, e - q);
1037
14.5k
    if (left < sizeof(uint32_t)) {
1038
8
      DPRINTF(("short info (no type)_\n"));
1039
8
      goto out;
1040
8
    }
1041
14.5k
    inp[i].pi_type = CDF_GETUINT32(q, 0);
1042
14.5k
    DPRINTF(("%" SIZE_T_FORMAT "u) id=%#x type=%#x offs=%#tx,%#x\n",
1043
14.5k
        i, inp[i].pi_id, inp[i].pi_type, q - p, offs));
1044
14.5k
    if (inp[i].pi_type & CDF_VECTOR) {
1045
2.42k
      if (left < sizeof(uint32_t) * 2) {
1046
6
        DPRINTF(("missing CDF_VECTOR length\n"));
1047
6
        goto out;
1048
6
      }
1049
2.42k
      nelements = CDF_GETUINT32(q, 1);
1050
2.42k
      if (nelements > CDF_ELEMENT_LIMIT || nelements == 0) {
1051
69
        DPRINTF(("CDF_VECTOR with nelements == %"
1052
69
            SIZE_T_FORMAT "u\n", nelements));
1053
69
        goto out;
1054
69
      }
1055
2.35k
      slen = 2;
1056
12.0k
    } else {
1057
12.0k
      nelements = 1;
1058
12.0k
      slen = 1;
1059
12.0k
    }
1060
14.4k
    o4 = slen * sizeof(uint32_t);
1061
14.4k
    if (inp[i].pi_type & (CDF_ARRAY|CDF_BYREF|CDF_RESERVED))
1062
2.08k
      goto unknown;
1063
12.3k
    switch (inp[i].pi_type & CDF_TYPEMASK) {
1064
849
    case CDF_NULL:
1065
2.32k
    case CDF_EMPTY:
1066
2.32k
      break;
1067
1.29k
    case CDF_SIGNED16:
1068
1.29k
      if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int16_t)))
1069
589
        goto unknown;
1070
701
      break;
1071
701
    case CDF_SIGNED32:
1072
894
    case CDF_BOOL:
1073
1.42k
    case CDF_UNSIGNED32:
1074
2.29k
    case CDF_FLOAT:
1075
2.29k
      if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int32_t)))
1076
813
        goto unknown;
1077
1.48k
      break;
1078
1.48k
    case CDF_SIGNED64:
1079
640
    case CDF_UNSIGNED64:
1080
1.20k
    case CDF_DOUBLE:
1081
2.58k
    case CDF_FILETIME:
1082
2.58k
      if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int64_t)))
1083
930
        goto unknown;
1084
1.65k
      break;
1085
1.65k
    case CDF_LENGTH32_STRING:
1086
1.35k
    case CDF_LENGTH32_WSTRING:
1087
1.35k
      if (nelements > 1) {
1088
213
        size_t nelem = inp - *info;
1089
213
        inp = cdf_grow_info(info, maxcount, nelements);
1090
213
        if (inp == NULL)
1091
5
          goto out;
1092
208
        inp += nelem;
1093
208
      }
1094
874k
      for (j = 0; j < nelements && i < sh.sh_properties;
1095
873k
          j++, i++)
1096
873k
      {
1097
873k
        uint32_t l;
1098
1099
873k
        if (o4 + sizeof(uint32_t) > left)
1100
16
          goto out;
1101
1102
873k
        l = CDF_GETUINT32(q, slen);
1103
873k
        o4 += sizeof(uint32_t);
1104
873k
        if (o4 + l > left)
1105
88
          goto out;
1106
1107
873k
        inp[i].pi_str.s_len = l;
1108
873k
        inp[i].pi_str.s_buf = CAST(const char *,
1109
873k
            CAST(const void *, &q[o4]));
1110
1111
873k
        DPRINTF(("o=%" SIZE_T_FORMAT "u l=%d(%"
1112
873k
            SIZE_T_FORMAT "u), t=%" SIZE_T_FORMAT
1113
873k
            "u s=%.*s\n", o4, l,
1114
873k
            CDF_ROUND(l, sizeof(l)),
1115
873k
            left, (int)l, inp[i].pi_str.s_buf));
1116
1117
873k
        if (l & 1)
1118
745
          l++;
1119
1120
873k
        slen += l >> 1;
1121
873k
        o4 = slen * sizeof(uint32_t);
1122
873k
      }
1123
1.24k
      i--;
1124
1.24k
      break;
1125
434
    case CDF_CLIPBOARD:
1126
434
      if (inp[i].pi_type & CDF_VECTOR)
1127
56
        goto unknown;
1128
378
      break;
1129
2.07k
    default:
1130
6.55k
    unknown:
1131
6.55k
      memset(&inp[i].pi_val, 0, sizeof(inp[i].pi_val));
1132
6.55k
      DPRINTF(("Don't know how to deal with %#x\n",
1133
6.55k
          inp[i].pi_type));
1134
6.55k
      break;
1135
12.3k
    }
1136
12.3k
  }
1137
592
  return 0;
1138
978
out:
1139
978
  free(*info);
1140
978
  *info = NULL;
1141
978
  *count = 0;
1142
978
  *maxcount = 0;
1143
978
  errno = EFTYPE;
1144
978
  return -1;
1145
1.24k
}
1146
1147
file_protected int
1148
cdf_unpack_summary_info(const cdf_stream_t *sst, const cdf_header_t *h,
1149
    cdf_summary_info_header_t *ssi, cdf_property_info_t **info, size_t *count)
1150
1.60k
{
1151
1.60k
  size_t maxcount;
1152
1.60k
  const cdf_summary_info_header_t *si =
1153
1.60k
      CAST(const cdf_summary_info_header_t *, sst->sst_tab);
1154
1.60k
  const cdf_section_declaration_t *sd =
1155
1.60k
      CAST(const cdf_section_declaration_t *, RCAST(const void *,
1156
1.60k
      RCAST(const char *, sst->sst_tab)
1157
1.60k
      + CDF_SECTION_DECLARATION_OFFSET));
1158
1159
1.60k
  if (cdf_check_stream_offset(sst, h, si, sizeof(*si), __LINE__) == -1 ||
1160
1.57k
      cdf_check_stream_offset(sst, h, sd, sizeof(*sd), __LINE__) == -1)
1161
31
    return -1;
1162
1.57k
  ssi->si_byte_order = CDF_TOLE2(si->si_byte_order);
1163
1.57k
  ssi->si_os_version = CDF_TOLE2(si->si_os_version);
1164
1.57k
  ssi->si_os = CDF_TOLE2(si->si_os);
1165
1.57k
  ssi->si_class = si->si_class;
1166
1.57k
  cdf_swap_class(&ssi->si_class);
1167
1.57k
  ssi->si_count = CDF_TOLE4(si->si_count);
1168
1.57k
  *count = 0;
1169
1.57k
  maxcount = 0;
1170
1.57k
  *info = NULL;
1171
1.57k
  if (cdf_read_property_info(sst, h, CDF_TOLE4(sd->sd_offset), info,
1172
1.57k
      count, &maxcount) == -1)
1173
978
    return -1;
1174
592
  return 0;
1175
1.57k
}
1176
1177
1178
#define extract_catalog_field(t, f, l) \
1179
0
    if (b + l + sizeof(cep->f) > eb) { \
1180
0
      cep->ce_namlen = 0; \
1181
0
      break; \
1182
0
    } \
1183
0
    memcpy(&cep->f, b + (l), sizeof(cep->f)); \
1184
0
    ce[i].f = CAST(t, CDF_TOLE(cep->f))
1185
1186
file_protected int
1187
cdf_unpack_catalog(const cdf_header_t *h, const cdf_stream_t *sst,
1188
    cdf_catalog_t **cat)
1189
0
{
1190
0
  size_t ss = cdf_check_stream(sst, h);
1191
0
  const char *b = CAST(const char *, sst->sst_tab);
1192
0
  const char *nb, *eb = b + ss * sst->sst_len;
1193
0
  size_t nr, i, j, k;
1194
0
  cdf_catalog_entry_t *ce;
1195
0
  uint16_t reclen;
1196
0
  const uint16_t *np;
1197
1198
0
  for (nr = 0;; nr++) {
1199
0
    if (b + sizeof(reclen) > eb)
1200
0
      break;
1201
0
    memcpy(&reclen, b, sizeof(reclen));
1202
0
    reclen = CDF_TOLE2(reclen);
1203
0
    if (reclen == 0)
1204
0
      break;
1205
0
    b += reclen;
1206
0
    if (b > eb)
1207
0
        break;
1208
0
  }
1209
0
  if (nr == 0)
1210
0
    return -1;
1211
0
  nr--;
1212
0
  *cat = CAST(cdf_catalog_t *,
1213
0
      CDF_MALLOC(sizeof(cdf_catalog_t) + nr * sizeof(*ce)));
1214
0
  if (*cat == NULL)
1215
0
    return -1;
1216
0
  ce = (*cat)->cat_e;
1217
0
  memset(ce, 0, nr * sizeof(*ce));
1218
0
  b = CAST(const char *, sst->sst_tab);
1219
0
  for (j = i = 0; i < nr; b += reclen) {
1220
0
    cdf_catalog_entry_t *cep = &ce[j];
1221
0
    uint16_t rlen;
1222
1223
0
    extract_catalog_field(uint16_t, ce_namlen, 0);
1224
0
    extract_catalog_field(uint16_t, ce_num, 4);
1225
0
    extract_catalog_field(uint64_t, ce_timestamp, 8);
1226
0
    reclen = cep->ce_namlen;
1227
1228
0
    if (reclen < 14) {
1229
0
      cep->ce_namlen = 0;
1230
0
      continue;
1231
0
    }
1232
1233
0
    cep->ce_namlen = __arraycount(cep->ce_name) - 1;
1234
0
    rlen = reclen - 14;
1235
0
    if (cep->ce_namlen > rlen)
1236
0
      cep->ce_namlen = rlen;
1237
1238
0
    np = CAST(const uint16_t *, CAST(const void *, (b + 16)));
1239
0
    nb = CAST(const char *, CAST(const void *,
1240
0
        (np + cep->ce_namlen)));
1241
0
    if (nb > eb) {
1242
0
      cep->ce_namlen = 0;
1243
0
      break;
1244
0
    }
1245
1246
0
    for (k = 0; k < cep->ce_namlen; k++)
1247
0
      cep->ce_name[k] = np[k]; /* XXX: CDF_TOLE2? */
1248
0
    cep->ce_name[cep->ce_namlen] = 0;
1249
0
    j = i;
1250
0
    i++;
1251
0
  }
1252
0
  (*cat)->cat_num = j;
1253
0
  return 0;
1254
0
}
1255
1256
file_protected int
1257
cdf_print_classid(char *buf, size_t buflen, const cdf_classid_t *id)
1258
0
{
1259
0
  return snprintf(buf, buflen, "%.8x-%.4x-%.4x-%.2x%.2x-"
1260
0
      "%.2x%.2x%.2x%.2x%.2x%.2x", id->cl_dword, id->cl_word[0],
1261
0
      id->cl_word[1], id->cl_two[0], id->cl_two[1], id->cl_six[0],
1262
0
      id->cl_six[1], id->cl_six[2], id->cl_six[3], id->cl_six[4],
1263
0
      id->cl_six[5]);
1264
0
}
1265
1266
file_private const struct {
1267
  uint32_t v;
1268
  const char *n;
1269
} vn[] = {
1270
  { CDF_PROPERTY_CODE_PAGE, "Code page" },
1271
  { CDF_PROPERTY_TITLE, "Title" },
1272
  { CDF_PROPERTY_SUBJECT, "Subject" },
1273
  { CDF_PROPERTY_AUTHOR, "Author" },
1274
  { CDF_PROPERTY_KEYWORDS, "Keywords" },
1275
  { CDF_PROPERTY_COMMENTS, "Comments" },
1276
  { CDF_PROPERTY_TEMPLATE, "Template" },
1277
  { CDF_PROPERTY_LAST_SAVED_BY, "Last Saved By" },
1278
  { CDF_PROPERTY_REVISION_NUMBER, "Revision Number" },
1279
  { CDF_PROPERTY_TOTAL_EDITING_TIME, "Total Editing Time" },
1280
  { CDF_PROPERTY_LAST_PRINTED, "Last Printed" },
1281
  { CDF_PROPERTY_CREATE_TIME, "Create Time/Date" },
1282
  { CDF_PROPERTY_LAST_SAVED_TIME, "Last Saved Time/Date" },
1283
  { CDF_PROPERTY_NUMBER_OF_PAGES, "Number of Pages" },
1284
  { CDF_PROPERTY_NUMBER_OF_WORDS, "Number of Words" },
1285
  { CDF_PROPERTY_NUMBER_OF_CHARACTERS, "Number of Characters" },
1286
  { CDF_PROPERTY_THUMBNAIL, "Thumbnail" },
1287
  { CDF_PROPERTY_NAME_OF_APPLICATION, "Name of Creating Application" },
1288
  { CDF_PROPERTY_SECURITY, "Security" },
1289
  { CDF_PROPERTY_LOCALE_ID, "Locale ID" },
1290
};
1291
1292
file_protected int
1293
cdf_print_property_name(char *buf, size_t bufsiz, uint32_t p)
1294
2.80k
{
1295
2.80k
  size_t i;
1296
1297
47.1k
  for (i = 0; i < __arraycount(vn); i++)
1298
45.2k
    if (vn[i].v == p)
1299
920
      return snprintf(buf, bufsiz, "%s", vn[i].n);
1300
1.88k
  return snprintf(buf, bufsiz, "%#x", p);
1301
2.80k
}
1302
1303
file_protected int
1304
cdf_print_elapsed_time(char *buf, size_t bufsiz, cdf_timestamp_t ts)
1305
393
{
1306
393
  int len = 0;
1307
393
  int days, hours, mins, secs;
1308
1309
393
  ts /= CDF_TIME_PREC;
1310
393
  secs = CAST(int, ts % 60);
1311
393
  ts /= 60;
1312
393
  mins = CAST(int, ts % 60);
1313
393
  ts /= 60;
1314
393
  hours = CAST(int, ts % 24);
1315
393
  ts /= 24;
1316
393
  days = CAST(int, ts);
1317
1318
393
  if (days) {
1319
244
    len += snprintf(buf + len, bufsiz - len, "%dd+", days);
1320
244
    if (CAST(size_t, len) >= bufsiz)
1321
0
      return len;
1322
244
  }
1323
1324
393
  if (days || hours) {
1325
314
    len += snprintf(buf + len, bufsiz - len, "%.2d:", hours);
1326
314
    if (CAST(size_t, len) >= bufsiz)
1327
0
      return len;
1328
314
  }
1329
1330
393
  len += snprintf(buf + len, bufsiz - len, "%.2d:", mins);
1331
393
  if (CAST(size_t, len) >= bufsiz)
1332
0
    return len;
1333
1334
393
  len += snprintf(buf + len, bufsiz - len, "%.2d", secs);
1335
393
  return len;
1336
393
}
1337
1338
file_protected char *
1339
cdf_u16tos8(char *buf, size_t len, const uint16_t *p)
1340
0
{
1341
0
  size_t i;
1342
0
  for (i = 0; i < len && p[i]; i++)
1343
0
    buf[i] = CAST(char, p[i]);
1344
0
  buf[i] = '\0';
1345
0
  return buf;
1346
0
}
1347
1348
#ifdef CDF_DEBUG
1349
file_protected void
1350
cdf_dump_header(const cdf_header_t *h)
1351
{
1352
  size_t i;
1353
1354
#define DUMP(a, b) (void)fprintf(stderr, "%40.40s = " a "\n", # b, h->h_ ## b)
1355
#define DUMP2(a, b) (void)fprintf(stderr, "%40.40s = " a " (" a ")\n", # b, \
1356
    h->h_ ## b, 1 << h->h_ ## b)
1357
  DUMP("%d", revision);
1358
  DUMP("%d", version);
1359
  DUMP("%#x", byte_order);
1360
  DUMP2("%d", sec_size_p2);
1361
  DUMP2("%d", short_sec_size_p2);
1362
  DUMP("%d", num_sectors_in_sat);
1363
  DUMP("%d", secid_first_directory);
1364
  DUMP("%d", min_size_standard_stream);
1365
  DUMP("%d", secid_first_sector_in_short_sat);
1366
  DUMP("%d", num_sectors_in_short_sat);
1367
  DUMP("%d", secid_first_sector_in_master_sat);
1368
  DUMP("%d", num_sectors_in_master_sat);
1369
  for (i = 0; i < __arraycount(h->h_master_sat); i++) {
1370
    if (h->h_master_sat[i] == CDF_SECID_FREE)
1371
      break;
1372
    (void)fprintf(stderr, "%35.35s[%.3" SIZE_T_FORMAT "u] = %d\n",
1373
        "master_sat", i, h->h_master_sat[i]);
1374
  }
1375
}
1376
1377
file_protected void
1378
cdf_dump_sat(const char *prefix, const cdf_sat_t *sat, size_t size)
1379
{
1380
  size_t i, j, s = size / sizeof(cdf_secid_t);
1381
1382
  for (i = 0; i < sat->sat_len; i++) {
1383
    (void)fprintf(stderr, "%s[%" SIZE_T_FORMAT "u]:\n%.6"
1384
        SIZE_T_FORMAT "u: ", prefix, i, i * s);
1385
    for (j = 0; j < s; j++) {
1386
      (void)fprintf(stderr, "%5d, ",
1387
          CDF_TOLE4(sat->sat_tab[s * i + j]));
1388
      if ((j + 1) % 10 == 0)
1389
        (void)fprintf(stderr, "\n%.6" SIZE_T_FORMAT
1390
            "u: ", i * s + j + 1);
1391
    }
1392
    (void)fprintf(stderr, "\n");
1393
  }
1394
}
1395
1396
file_protected void
1397
cdf_dump(const void *v, size_t len)
1398
{
1399
  size_t i, j;
1400
  const unsigned char *p = v;
1401
  char abuf[16];
1402
1403
  (void)fprintf(stderr, "%.4x: ", 0);
1404
  for (i = 0, j = 0; i < len; i++, p++) {
1405
    (void)fprintf(stderr, "%.2x ", *p);
1406
    abuf[j++] = isprint(*p) ? *p : '.';
1407
    if (j == 16) {
1408
      j = 0;
1409
      abuf[15] = '\0';
1410
      (void)fprintf(stderr, "%s\n%.4" SIZE_T_FORMAT "x: ",
1411
          abuf, i + 1);
1412
    }
1413
  }
1414
  (void)fprintf(stderr, "\n");
1415
}
1416
1417
file_protected void
1418
cdf_dump_stream(const cdf_stream_t *sst)
1419
{
1420
  size_t ss = sst->sst_ss;
1421
  cdf_dump(sst->sst_tab, ss * sst->sst_len);
1422
}
1423
1424
file_protected void
1425
cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h,
1426
    const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
1427
    const cdf_dir_t *dir)
1428
{
1429
  size_t i, j;
1430
  cdf_directory_t *d;
1431
  char name[__arraycount(d->d_name)];
1432
  cdf_stream_t scn;
1433
  struct timespec ts;
1434
1435
  static const char *types[] = { "empty", "user storage",
1436
      "user stream", "lockbytes", "property", "root storage" };
1437
1438
  for (i = 0; i < dir->dir_len; i++) {
1439
    char buf[26];
1440
    d = &dir->dir_tab[i];
1441
    for (j = 0; j < sizeof(name); j++)
1442
      name[j] = (char)CDF_TOLE2(d->d_name[j]);
1443
    (void)fprintf(stderr, "Directory %" SIZE_T_FORMAT "u: %s\n",
1444
        i, name);
1445
    if (d->d_type < __arraycount(types))
1446
      (void)fprintf(stderr, "Type: %s\n", types[d->d_type]);
1447
    else
1448
      (void)fprintf(stderr, "Type: %d\n", d->d_type);
1449
    (void)fprintf(stderr, "Color: %s\n",
1450
        d->d_color ? "black" : "red");
1451
    (void)fprintf(stderr, "Left child: %d\n", d->d_left_child);
1452
    (void)fprintf(stderr, "Right child: %d\n", d->d_right_child);
1453
    (void)fprintf(stderr, "Flags: %#x\n", d->d_flags);
1454
    cdf_timestamp_to_timespec(&ts, d->d_created);
1455
    (void)fprintf(stderr, "Created %s", cdf_ctime(&ts.tv_sec, buf));
1456
    cdf_timestamp_to_timespec(&ts, d->d_modified);
1457
    (void)fprintf(stderr, "Modified %s",
1458
        cdf_ctime(&ts.tv_sec, buf));
1459
    (void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector);
1460
    (void)fprintf(stderr, "Size %d\n", d->d_size);
1461
    switch (d->d_type) {
1462
    case CDF_DIR_TYPE_USER_STORAGE:
1463
      (void)fprintf(stderr, "Storage: %d\n", d->d_storage);
1464
      break;
1465
    case CDF_DIR_TYPE_USER_STREAM:
1466
      if (sst == NULL)
1467
        break;
1468
      if (cdf_read_sector_chain(info, h, sat, ssat, sst,
1469
          d->d_stream_first_sector, d->d_size, &scn) == -1) {
1470
        warn("Can't read stream for %s at %d len %d",
1471
            name, d->d_stream_first_sector, d->d_size);
1472
        break;
1473
      }
1474
      cdf_dump_stream(&scn);
1475
      free(scn.sst_tab);
1476
      break;
1477
    default:
1478
      break;
1479
    }
1480
1481
  }
1482
}
1483
1484
file_protected void
1485
cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
1486
{
1487
  cdf_timestamp_t tp;
1488
  struct timespec ts;
1489
  char buf[64];
1490
  size_t i, j;
1491
1492
  for (i = 0; i < count; i++) {
1493
    cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
1494
    (void)fprintf(stderr, "%" SIZE_T_FORMAT "u) %s: ", i, buf);
1495
    switch (info[i].pi_type) {
1496
    case CDF_NULL:
1497
      break;
1498
    case CDF_SIGNED16:
1499
      (void)fprintf(stderr, "signed 16 [%hd]\n",
1500
          info[i].pi_s16);
1501
      break;
1502
    case CDF_SIGNED32:
1503
      (void)fprintf(stderr, "signed 32 [%d]\n",
1504
          info[i].pi_s32);
1505
      break;
1506
    case CDF_UNSIGNED32:
1507
      (void)fprintf(stderr, "unsigned 32 [%u]\n",
1508
          info[i].pi_u32);
1509
      break;
1510
    case CDF_FLOAT:
1511
      (void)fprintf(stderr, "float [%g]\n",
1512
          info[i].pi_f);
1513
      break;
1514
    case CDF_DOUBLE:
1515
      (void)fprintf(stderr, "double [%g]\n",
1516
          info[i].pi_d);
1517
      break;
1518
    case CDF_LENGTH32_STRING:
1519
      (void)fprintf(stderr, "string %u [%.*s]\n",
1520
          info[i].pi_str.s_len,
1521
          info[i].pi_str.s_len, info[i].pi_str.s_buf);
1522
      break;
1523
    case CDF_LENGTH32_WSTRING:
1524
      (void)fprintf(stderr, "string %u [",
1525
          info[i].pi_str.s_len);
1526
      for (j = 0; j < info[i].pi_str.s_len - 1; j++)
1527
          (void)fputc(info[i].pi_str.s_buf[j << 1], stderr);
1528
      (void)fprintf(stderr, "]\n");
1529
      break;
1530
    case CDF_FILETIME:
1531
      tp = info[i].pi_tp;
1532
      if (tp < 1000000000000000LL) {
1533
        cdf_print_elapsed_time(buf, sizeof(buf), tp);
1534
        (void)fprintf(stderr, "timestamp %s\n", buf);
1535
      } else {
1536
        char tbuf[26];
1537
        cdf_timestamp_to_timespec(&ts, tp);
1538
        (void)fprintf(stderr, "timestamp %s",
1539
            cdf_ctime(&ts.tv_sec, tbuf));
1540
      }
1541
      break;
1542
    case CDF_CLIPBOARD:
1543
      (void)fprintf(stderr, "CLIPBOARD %u\n", info[i].pi_u32);
1544
      break;
1545
    default:
1546
      DPRINTF(("Don't know how to deal with %#x\n",
1547
          info[i].pi_type));
1548
      break;
1549
    }
1550
  }
1551
}
1552
1553
1554
file_protected void
1555
cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
1556
{
1557
  char buf[128];
1558
  cdf_summary_info_header_t ssi;
1559
  cdf_property_info_t *info;
1560
  size_t count;
1561
1562
  (void)&h;
1563
  if (cdf_unpack_summary_info(sst, h, &ssi, &info, &count) == -1)
1564
    return;
1565
  (void)fprintf(stderr, "Endian: %#x\n", ssi.si_byte_order);
1566
  (void)fprintf(stderr, "Os Version %d.%d\n", ssi.si_os_version & 0xff,
1567
      ssi.si_os_version >> 8);
1568
  (void)fprintf(stderr, "Os %d\n", ssi.si_os);
1569
  cdf_print_classid(buf, sizeof(buf), &ssi.si_class);
1570
  (void)fprintf(stderr, "Class %s\n", buf);
1571
  (void)fprintf(stderr, "Count %d\n", ssi.si_count);
1572
  cdf_dump_property_info(info, count);
1573
  free(info);
1574
}
1575
1576
1577
file_protected void
1578
cdf_dump_catalog(const cdf_header_t *h, const cdf_stream_t *sst)
1579
{
1580
  cdf_catalog_t *cat;
1581
  cdf_unpack_catalog(h, sst, &cat);
1582
  const cdf_catalog_entry_t *ce = cat->cat_e;
1583
  struct timespec ts;
1584
  char tbuf[64], sbuf[256];
1585
  size_t i;
1586
1587
  printf("Catalog:\n");
1588
  for (i = 0; i < cat->cat_num; i++) {
1589
    cdf_timestamp_to_timespec(&ts, ce[i].ce_timestamp);
1590
    printf("\t%d %s %s", ce[i].ce_num,
1591
        cdf_u16tos8(sbuf, ce[i].ce_namlen, ce[i].ce_name),
1592
        cdf_ctime(&ts.tv_sec, tbuf));
1593
  }
1594
  free(cat);
1595
}
1596
1597
#endif
1598
1599
#ifdef TEST
1600
int
1601
main(int argc, char *argv[])
1602
{
1603
  int i;
1604
  cdf_header_t h;
1605
  cdf_sat_t sat, ssat;
1606
  cdf_stream_t sst, scn;
1607
  cdf_dir_t dir;
1608
  cdf_info_t info;
1609
  const cdf_directory_t *root;
1610
#ifdef __linux__
1611
#define getprogname() __progname
1612
  extern char *__progname;
1613
#endif
1614
  if (argc < 2) {
1615
    (void)fprintf(stderr, "Usage: %s <filename>\n", getprogname());
1616
    return -1;
1617
  }
1618
1619
  info.i_buf = NULL;
1620
  info.i_len = 0;
1621
  for (i = 1; i < argc; i++) {
1622
    if ((info.i_fd = open(argv[1], O_RDONLY)) == -1)
1623
      err(EXIT_FAILURE, "Cannot open `%s'", argv[1]);
1624
1625
    if (cdf_read_header(&info, &h) == -1)
1626
      err(EXIT_FAILURE, "Cannot read header");
1627
#ifdef CDF_DEBUG
1628
    cdf_dump_header(&h);
1629
#endif
1630
1631
    if (cdf_read_sat(&info, &h, &sat) == -1)
1632
      err(EXIT_FAILURE, "Cannot read sat");
1633
#ifdef CDF_DEBUG
1634
    cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
1635
#endif
1636
1637
    if (cdf_read_ssat(&info, &h, &sat, &ssat) == -1)
1638
      err(EXIT_FAILURE, "Cannot read ssat");
1639
#ifdef CDF_DEBUG
1640
    cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
1641
#endif
1642
1643
    if (cdf_read_dir(&info, &h, &sat, &dir) == -1)
1644
      err(EXIT_FAILURE, "Cannot read dir");
1645
1646
    if (cdf_read_short_stream(&info, &h, &sat, &dir, &sst, &root)
1647
        == -1)
1648
      err(EXIT_FAILURE, "Cannot read short stream");
1649
#ifdef CDF_DEBUG
1650
    cdf_dump_stream(&sst);
1651
#endif
1652
1653
#ifdef CDF_DEBUG
1654
    cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
1655
#endif
1656
1657
1658
    if (cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
1659
        &scn) == -1)
1660
      warn("Cannot read summary info");
1661
#ifdef CDF_DEBUG
1662
    else
1663
      cdf_dump_summary_info(&h, &scn);
1664
#endif
1665
    if (cdf_read_user_stream(&info, &h, &sat, &ssat, &sst,
1666
        &dir, "Catalog", &scn) == -1)
1667
      warn("Cannot read catalog");
1668
#ifdef CDF_DEBUG
1669
    else
1670
      cdf_dump_catalog(&h, &scn);
1671
#endif
1672
1673
    (void)close(info.i_fd);
1674
  }
1675
1676
  return 0;
1677
}
1678
#endif