File: t1unmac.c

package info (click to toggle)
t1utils 1.41-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 768 kB
  • sloc: ansic: 5,253; sh: 4,173; makefile: 29
file content (923 lines) | stat: -rw-r--r-- 22,874 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
/* t1unmac/unpost
 *
 * This program converts Macintosh Type 1 fonts stored in MacBinary (I or II),
 * AppleSingle, AppleDouble, BinHex, or raw resource fork format to PFA and
 * PFB formats.
 *
 * Copyright (c) 1992 by I. Lee Hetherington, all rights reserved.
 * Copyright (c) 1998-2017 Eddie Kohler
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, subject to the
 * conditions listed in the Click LICENSE file, which is available in full at
 * http://github.com/kohler/click/blob/master/LICENSE. The conditions
 * include: you must preserve this copyright notice, and you cannot mention
 * the copyright holders in advertising related to the Software without
 * their permission. The Software is provided WITHOUT ANY WARRANTY, EXPRESS
 * OR IMPLIED. This notice is a summary of the Click LICENSE file; the
 * license in that file is binding.
 *
 * New change log in `NEWS'. Old change log:
 *
 * Revision 1.2  92/06/23  10:57:33  ilh
 * MSDOS porting by Kai-Uwe Herbing (herbing@netmbx.netmbx.de)
 * incoporated.
 *
 * Revision 1.1  92/05/22  12:07:49  ilh
 * initial version
 *
 * Ported to Microsoft C/C++ Compiler and MS-DOS operating system by
 * Kai-Uwe Herbing (herbing@netmbx.netmbx.de) on June 12, 1992. Code
 * specific to the MS-DOS version is encapsulated with #ifdef _MSDOS
 * ... #endif, where _MSDOS is an identifier, which is automatically
 * defined, if you compile with the Microsoft C/C++ Compiler.
 * */

/* Note: this is ANSI C. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#if defined(_MSDOS) || defined(_WIN32)
# include <fcntl.h>
# include <getopt.h>
# include <io.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <lcdf/clp.h>
#include "t1lib.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Some functions to read one, two, three, and four byte integers in 68000
   byte order (most significant byte first). */

static int
read_one(FILE *fi)
{
  return getc(fi);
}

static int
read_two(FILE *fi)
{
  int val;

  val = getc(fi);
  val = (val << 8) | getc(fi);

  return val;
}

static int32_t
read_three(FILE *fi)
{
  int32_t val;

  val = getc(fi);
  val = (val << 8) | getc(fi);
  val = (val << 8) | getc(fi);

  return val;
}

static int32_t
read_four(FILE *fi)
{
  int32_t val;

  val = getc(fi);
  val = (val << 8) | getc(fi);
  val = (val << 8) | getc(fi);
  val = (val << 8) | getc(fi);

  return val;
}

/* reposition a file with error messages */

static void
reposition(FILE *fi, int32_t absolute)
{
  if (fseek(fi, absolute, 0) == -1)
    fatal_error("can't seek to position %d\n\
   (The Mac file may be corrupted, or you may need the `-r' option.)",
		absolute);
}

static int blocklen = -1;

static int hex_column = 0;	/* current column of hex ASCII output */

static void
output_hex_byte(FILE *fo, int b)
{
  static const char *hex = "0123456789abcdef";

  if (hex_column >= blocklen) {
    putc('\n', fo);
    hex_column = 0;
  }
  putc(hex[b >> 4], fo);
  putc(hex[b & 0xf], fo);
  hex_column += 2;
}

/* Function to extract a particular POST resource.  Offset points to the four
   byte length which is followed by the data.  The first byte of the POST data
   specifies resource type: 1 for ASCII, 2 for binary, and 5 for end.  The
   second byte is always zero. */


/* Function to write four byte length to PFB file: least significant byte
   first. */

static int
extract_data(FILE *fi, FILE *fo, struct pfb_writer *w, int32_t offset, int pfb)
{
  enum PS_type { PS_ascii = 1, PS_binary = 2, PS_end = 5 };
  static int last_type = -1;
  static int skip_newline = 0;
  int32_t len;
  int more = 1;
  int i, c;

  reposition(fi, offset);
  len = read_four(fi) - 2;	/* subtract type field */

  switch ((enum PS_type)read_one(fi)) {

   case PS_ascii: {
     (void) read_one(fi);
     if (last_type != PFB_ASCII && pfb) {
       pfb_writer_output_block(w);
       w->blocktyp = PFB_ASCII;
     }
     for (i = 0; i < len; i++) {
       c = read_one(fi);
       if (c == '\n' && skip_newline) {
	 skip_newline = 0;
	 continue;
       }
       if (c == '\r') {
	 c = '\n';
	 skip_newline = 1;
       } else
	 skip_newline = 0;
       if (pfb)
	 PFB_OUTPUT_BYTE(w, c);
       else
	 putc(c, fo);
     }
     last_type = PFB_ASCII;
     break;
   }

   case PS_binary: {
     (void) read_one(fi);
     if (last_type != PFB_BINARY && pfb) {
       pfb_writer_output_block(w);
       w->blocktyp = PFB_BINARY;
     } else if (last_type != PFB_BINARY)
       hex_column = 0;
     if (pfb) {
       while (len--)
	 PFB_OUTPUT_BYTE(w, read_one(fi));
     } else {
       while (len--)
	 output_hex_byte(fo, read_one(fi));
     }
     last_type = PFB_BINARY;
     break;
   }

   case PS_end:
    more = 0;
    break;

  }

  return more;
}


/*****
 * Command line
 **/

#define OUTPUT_OPT	301
#define VERSION_OPT	302
#define HELP_OPT	303
#define PFB_OPT		304
#define PFA_OPT		305
#define MACBINARY_OPT	306
#define RAW_OPT		307
#define LINE_LEN_OPT	308
#define APPLEDOUBLE_OPT	309
#define BINHEX_OPT	310

static Clp_Option options[] = {
  { "applesingle", 0, APPLEDOUBLE_OPT, 0, 0 },
  { "appledouble", 0, APPLEDOUBLE_OPT, 0, 0 },
  { "binhex", 0, BINHEX_OPT, 0, 0 },
  { "block-length", 0, LINE_LEN_OPT, Clp_ValUnsigned, 0 },
  { "help", 0, HELP_OPT, 0, 0 },
  { "line-length", 'l', LINE_LEN_OPT, Clp_ValUnsigned, 0 },
  { "macbinary", 0, MACBINARY_OPT, 0, 0 },
  { "output", 'o', OUTPUT_OPT, Clp_ValString, 0 },
  { "pfa", 'a', PFA_OPT, 0, 0 },
  { "pfb", 'b', PFB_OPT, 0, 0 },
  { "raw", 'r', RAW_OPT, 0, 0 },
  { "version", 0, VERSION_OPT, 0, 0 },
};
static const char *program_name;

void
fatal_error(const char *message, ...)
{
  va_list val;
  va_start(val, message);
  fprintf(stderr, "%s: ", program_name);
  vfprintf(stderr, message, val);
  putc('\n', stderr);
  va_end(val);
  exit(1);
}

void
error(const char *message, ...)
{
  va_list val;
  va_start(val, message);
  fprintf(stderr, "%s: ", program_name);
  vfprintf(stderr, message, val);
  putc('\n', stderr);
  va_end(val);
}

static void
short_usage(void)
{
  fprintf(stderr, "Usage: %s [OPTION]... INPUT [OUTPUT]\n\
Try `%s --help' for more information.\n",
	  program_name, program_name);
}

static void
usage(void)
{
  printf("\
`T1unmac' extracts a PostScript Type 1 font from a Macintosh font file. It can\n\
read MacBinary, AppleSingle, AppleDouble, or BinHex files, or raw Macintosh\n\
resource forks. The result is written to the standard output unless an OUTPUT\n\
file is given.\n\
\n\
Usage: %s [OPTION]... INPUT [OUTPUT]\n\
\n\
Options:\n\
  -r, --raw                   Input is a raw Macintosh resource fork.\n\
      --macbinary             Input is in MacBinary format.\n\
      --applesingle           Input is in AppleSingle format.\n\
      --appledouble           Input is in AppleDouble format.\n\
      --binhex                Input is in BinHex format.\n\
  -a, --pfa                   Output font in ASCII (PFA) format.\n\
  -b, --pfb                   Output font in binary (PFB) format. This is\n\
                              the default.\n\
  -l, --block-length NUM      Set max block length for PFB output.\n\
  -l, --line-length NUM       Set max encrypted line length for PFA output.\n\
  -o, --output FILE           Write output to FILE.\n\
  -h, --help                  Print this message and exit.\n\
      --version               Print version number and warranty and exit.\n\
\n\
Report bugs to <ekohler@gmail.com>.\n", program_name);
}


static const char *
check_macbinary(FILE *ifp)
{
  int i, j;
  char buf[124];

  /* check "version" bytes at offsets 0 and 74 */
  reposition(ifp, 0);
  if (read_one(ifp) != 0)
    return "bad version byte";
  reposition(ifp, 74);
  if (read_one(ifp) != 0)
    return "bad version byte";

#if 0
  /* write out bullshit */
  {int t;
    reposition(ifp, 65);
    t = read_four(ifp);
    fprintf(stderr, "Type %c%c%c%c\n", (t>>24)&255, (t>>16)&255, (t>>8)&255, t&255);
    t = read_four(ifp);
    fprintf(stderr, "Creator %c%c%c%c\n", (t>>24)&255, (t>>16)&255, (t>>8)&255, t&255);
    t = read_one(ifp);
    fprintf(stderr, "Finder flags 1 %02x\n", t);
    read_one(ifp);
    t = read_two(ifp);
    fprintf(stderr, "horizpos %04x\n", t);
    t = read_two(ifp);
    fprintf(stderr, "vertpos %04x\n", t);
    t = read_two(ifp);
    fprintf(stderr, "folder id %d\n", t);
    t = read_one(ifp);
    fprintf(stderr, "protected? %x\n", t);
    t = read_one(ifp);
    t = read_four(ifp);
    fprintf(stderr, "data len %d\n", t);
    t = read_four(ifp);
    fprintf(stderr, "rsrc len %d\n", t);
    t = read_four(ifp);
    {
      struct tm *crap;
      fprintf(stderr, "creation date %x\n", t);
      t -= 2082844800;
      fprintf(stderr, "   %s\n", ctime(&t));
      t = read_four(ifp);
      fprintf(stderr, "modification date %x\n", t);
      t -= 2082844800;
      fprintf(stderr, "   %s\n", ctime(&t));
      t = read_two(ifp);
    }
    fprintf(stderr, "getinfo len %d\n", t);
    t = read_one(ifp);
    fprintf(stderr, "finderflags 2 %02x\n", t);
    reposition(ifp, 116);
    t = read_four(ifp);
    fprintf(stderr, "total len %d\n", t);
    t = read_two(ifp);
    fprintf(stderr, "secondary header len %d\n", t);
    t = read_one(ifp);
    fprintf(stderr, "version %d\n", t);
    t = read_one(ifp);
    fprintf(stderr, "version %d\n", t);
  }
#endif

  /* check file length */
  reposition(ifp, 1);
  i = read_one(ifp);
  if (i > 63)
    return "bad length";
  reposition(ifp, 83);
  i = read_four(ifp);
  j = read_four(ifp);
  if (i < 0 || j < 0 || i >= 0x800000 || j >= 0x800000)
    return "bad length";

  /* check CRC */
  reposition(ifp, 0);
  fread(buf, 1, 124, ifp);
  if (crcbuf(0, 124, buf) != read_two(ifp)) {
    reposition(ifp, 82);
    if (read_one(ifp) != 0)
      return "bad checksum";
  }

  return 0;
}

#define APPLESINGLE_MAGIC 0x00051600
#define APPLEDOUBLE_MAGIC 0x00051607

static const char *
check_appledouble(FILE *ifp)
{
  int i;
  reposition(ifp, 0);
  i = read_four(ifp);
  if (i != APPLEDOUBLE_MAGIC && i != APPLESINGLE_MAGIC)
    return "bad magic number";

  return 0;
}

static const char *
translate_binhex(FILE *f, FILE *tmpf)
{
  int i, c = 0, last_char, after_x90, bits, bitpos;
  unsigned char value_table[256];

  /* prepare value table */
  {
    const char *table = "!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
    for (i = 0; i < 256; i++)
      value_table[i] = 255;
    for (i = 0; *table; i++, table++)
      value_table[(unsigned char)*table] = i;
  }

  /* skip to comment */
  {
    const char *comment = "(This file must be converted with BinHex";
    while (!feof(f)) {
      const char *s;
      for (s = comment; *s; s++) {
	c = getc(f);
	if (c != *s)
	  break;
      }
      /* skip to end of line */
      while (c >= 0 && c != '\n' && c != '\r')
	c = getc(f);
      /* stop if read comment */
      if (!*s)
	goto found_comment;
    }
    /* failed */
    return "no comment";
  }

 found_comment:
  /* skip spaces, look for ':' */
  for (c = ' '; isspace(c); c = getc(f)) ;
  if (c != ':')
    return "no file-start character";

  /* found ':', process until you find another ':' */
  last_char = -1;
  after_x90 = bits = 0;
  bitpos = 10;
  for (c = getc(f); c >= 0; c = getc(f))
    if (!isspace(c)) {
      /* add 6 bits to bits */
      if (value_table[c] == 255)
	break;
      bits |= (value_table[c] << bitpos);
      bitpos -= 6;

      /* output character(s) */
      if (bitpos <= 2) {
	int d = bits >> 8;
	bits = (bits << 8) & 0xFF00;
	bitpos += 8;
	if (after_x90) {
	  /* handle compression */
	  if (d == 0) {
	    last_char = 0x90;
	    putc(0x90, tmpf);
	  } else
	    for (i = 1; i < d; i++)
	      putc(last_char, tmpf);
	  after_x90 = 0;
	} else if (d == 0x90)
	  after_x90 = 1;
	else {
	  last_char = d;
	  putc(d, tmpf);
	}
      }
    }

  if (c < 0)
    return "unexpected EOF";
  else if (c != ':')
    return "bad character";

  fflush(tmpf);
  return 0;
}

static int
check_binhex_crc(FILE *f, int offset, int length)
{
  int crc = 0;
  char buf[2048];
  reposition(f, offset);
  while (length > 0) {
    int n = (length < 2048 ? length : 2048);
    fread(buf, 1, n, f);
    crc = crcbuf(crc, n, buf);
    length -= n;
  }
  return crc == 0;
}

static const char *
check_binhex(FILE *f)
{
  int fname_len, data_len, rsrc_len, off;

  /* check lengths */
  reposition(f, 0);
  fname_len = read_one(f);
  if (fname_len < 1 || fname_len > 63)
    return "bad length";
  reposition(f, 1 + fname_len + 11);
  data_len = read_four(f);
  rsrc_len = read_four(f);
  if (data_len < 0 || rsrc_len < 0 || data_len >= 0x800000 || rsrc_len >= 0x800000)
    return "bad length";

  /* check version */
  reposition(f, 1 + fname_len);
  if (read_one(f) != 0)
    return "bad version";

  /* check CRC */
  off = 1 + fname_len + 21;
  if (!check_binhex_crc(f, 0, off))
    return "bad header CRC";
  if (!check_binhex_crc(f, off, data_len + 2))
    return "bad data CRC";
  if (!check_binhex_crc(f, off + data_len + 2, rsrc_len + 2))
    return "bad resource fork CRC";

  return 0;
}

#ifdef __cplusplus
}
#endif


int
main(int argc, char *argv[])
{
  FILE *ifp = 0;
  FILE *ofp = 0;
  struct pfb_writer w;
  const char *ifp_name = "<stdin>";
  int32_t res_offset, res_data_offset, res_map_offset, type_list_offset;
  int32_t post_type;
  int num_types, num_extracted = 0, pfb = 1;
  int raw = 0, appledouble = 0, binhex = 0, macbinary = 0;

  Clp_Parser *clp =
    Clp_NewParser(argc, (const char * const *)argv, sizeof(options) / sizeof(options[0]), options);
  program_name = Clp_ProgramName(clp);

  /* interpret command line arguments using CLP */
  while (1) {
    int opt = Clp_Next(clp);
    switch (opt) {

     case RAW_OPT:
      raw = 1;
      appledouble = binhex = macbinary = 0;
      break;

     case MACBINARY_OPT:
      macbinary = 1;
      raw = appledouble = binhex = 0;
      break;

     case APPLEDOUBLE_OPT:
      appledouble = 1;
      raw = binhex = macbinary = 0;
      break;

     case BINHEX_OPT:
      binhex = 1;
      raw = appledouble = macbinary = 0;
      break;

     output_file:
     case OUTPUT_OPT:
      if (ofp)
	fatal_error("output file already specified");
      if (strcmp(clp->vstr, "-") == 0)
	ofp = stdout;
      else {
	ofp = fopen(clp->vstr, "w");
	if (!ofp) fatal_error("%s: %s", clp->vstr, strerror(errno));
      }
      break;

     case PFB_OPT:
      pfb = 1;
      break;

     case PFA_OPT:
      pfb = 0;
      break;

     case LINE_LEN_OPT:
      blocklen = clp->val.i;
      break;

     case HELP_OPT:
      usage();
      exit(0);
      break;

     case VERSION_OPT:
      printf("t1unmac (LCDF t1utils) %s\n", VERSION);
      printf("Copyright (C) 1992-2017 I. Lee Hetherington, Eddie Kohler et al.\n\
This is free software; see the source for copying conditions.\n\
There is NO warranty, not even for merchantability or fitness for a\n\
particular purpose.\n");
      exit(0);
      break;

     case Clp_NotOption:
      if (ifp && ofp)
	fatal_error("too many arguments");
      else if (ifp)
	goto output_file;
      if (strcmp(clp->vstr, "-") == 0)
	ifp = stdin;
      else {
	ifp_name = clp->vstr;
	ifp = fopen(clp->vstr, "rb");
	if (!ifp) fatal_error("%s: %s", clp->vstr, strerror(errno));
      }
      break;

     case Clp_Done:
      goto done;

     case Clp_BadOption:
      short_usage();
      exit(1);
      break;

    }
  }

 done:
  if (!ifp) ifp = stdin;
  if (!ofp) ofp = stdout;

#if defined(_MSDOS) || defined(_WIN32)
  _setmode(_fileno(ifp), _O_BINARY);
  /* If we are processing a PFB (binary) output */
  /* file, we must set its file mode to binary. */
  if (pfb)
    _setmode(_fileno(ofp), _O_BINARY);
#endif

  if (pfb)
    init_pfb_writer(&w, blocklen, ofp);
  else {
    if (blocklen == -1)
      blocklen = 64;
    else if (blocklen < 8) {
      blocklen = 8;
      error("warning: line length raised to %d", blocklen);
    } else if (blocklen > 1024) {
      blocklen = 1024;
      error("warning: line length lowered to %d", blocklen);
    }
  }

  /* check for non-seekable input */
  if (fseek(ifp, 0, 0)) {
    char buf[2048];
    FILE *tmp = tmpfile();
    if (!tmp)
      fatal_error("cannot open temporary file: %s", strerror(errno));
    while (!feof(ifp)) {
      int i = fread(buf, 1, 2048, ifp);
      if (i > 0)
	fwrite(buf, 1, i, tmp);
    }
    if (ferror(ifp))
      fatal_error("%s: %s", ifp_name, strerror(errno));
    reposition(tmp, 0);
    fflush(tmp);
    if (ifp != stdin)
      fclose(ifp);
    ifp = tmp;
  }

  /* check for empty file */
  fseek(ifp, 0, 2);
  if (ftell(ifp) == 0)
    fatal_error("%s: empty file\n\
  (Try re-transferring the files using MacBinary format.)",
		ifp_name);

  reposition(ifp, 0);
  if (!raw && !appledouble && !binhex && !macbinary) {
    /* check magic number, try to figure out what it is */
    int i, magic;
    magic = read_four(ifp);
    reposition(ifp, 0);

    if (magic == APPLESINGLE_MAGIC || magic == APPLEDOUBLE_MAGIC)
      appledouble = 1;
    else if ((magic & 0xFF000000) == 0)
      macbinary = 1;
    else {
      binhex = 1;
      for (i = 0; i < 4; i++, magic >>= 8)
	if (!isprint(magic & 0xFF) && !isspace(magic & 0xFF))
	  /* not an ASCII character, assume not BinHex */
	  binhex = 0;
    }

    if (!appledouble && !macbinary && !binhex)
      fatal_error("%s: unknown file type", ifp_name);
  }

  if (raw) {
    /* raw resource file */
    res_offset = 0;

  } else if (macbinary) {	/* MacBinary (I or II) file */
    const char *check;
    int32_t data_fork_size;

    /* check integrity of file */
    check = check_macbinary(ifp);
    if (check)
      fatal_error("%s: not a MacBinary file (%s)", ifp_name, check);

    /* read data and resource fork sizes in MacBinary header */
    reposition(ifp, 83);
    data_fork_size = read_four(ifp);
    (void) read_four(ifp);

    /* round data_fork_size up to multiple of 128 */
    if (data_fork_size % 128)
      data_fork_size += 128 - data_fork_size % 128;

    res_offset = 128 + data_fork_size;

  } else if (appledouble) {	/* AppleDouble file */
    const char *check;
    const char *applewhat;
    int i, n;

    /* check integrity */
    check = check_appledouble(ifp);
    if (check)
      fatal_error("%s: not an AppleDouble file (%s)", ifp_name, check);
    reposition(ifp, 0);
    if (read_four(ifp) == APPLESINGLE_MAGIC)
      applewhat = "AppleSingle";
    else
      applewhat = "AppleDouble";

    /* find offset to resource and/or data fork */
    reposition(ifp, 24);
    n = read_two(ifp);
    res_offset = -1;
    for (i = 0; i < n; i++) {
      int type = read_four(ifp);
      if (type == 0)
	fatal_error("%s: bad %s file (bad entry descriptor)", ifp_name, applewhat);
      if (type == 2)		/* resource fork entry */
	res_offset = read_four(ifp);
      else
	(void) read_four(ifp);
      (void) read_four(ifp);
    }
    if (res_offset < 0)
      fatal_error("%s: bad %s file (no resource fork)", ifp_name, applewhat);

  } else if (binhex) {		/* BinHex file */
    const char *check;
    FILE *tmpf = tmpfile();
    if (!tmpf)
      fatal_error("cannot open temporary file: %s", strerror(errno));

    /* check integrity */
    check = translate_binhex(ifp, tmpf);
    if (check)
      fatal_error("%s: not a BinHex file (%s)", ifp_name, check);
    check = check_binhex(tmpf);
    if (check)
      fatal_error("%s: bad BinHex file (%s)", ifp_name, check);

    /* find resource offset */
    reposition(tmpf, 0);
    res_offset = read_one(tmpf);
    reposition(tmpf, 1 + res_offset + 11);
    res_offset += 22 + read_four(tmpf) + 2;
    if (ifp != stdin)
      fclose(ifp);
    ifp = tmpf;

  } else {
    fatal_error("%s: strange format", ifp_name);
    exit(1);
  }

  /* read offsets from resource fork header */
  reposition(ifp, res_offset);
  res_data_offset = res_offset + read_four(ifp);
  res_map_offset = res_offset + read_four(ifp);

  /* read type list offset from resource map header */
  reposition(ifp, res_map_offset + 24);
  type_list_offset = res_map_offset + read_two(ifp);

  /* read type list */
  reposition(ifp, type_list_offset);
  num_types = read_two(ifp) + 1;

  /* find POST type */
  post_type =  (int32_t)('P' & 0xff) << 24;
  post_type |= (int32_t)('O' & 0xff) << 16;
  post_type |= (int32_t)('S' & 0xff) << 8;
  post_type |= (int32_t)('T' & 0xff);

  while (num_types--) {
    if (read_four(ifp) == post_type) {
      int nrsrc = 1 + read_two(ifp);
      int list_offset = type_list_offset + read_two(ifp);
      int rsrc_pos = 0;
      int want_id = 501;
      int second_time = 1;
      reposition(ifp, list_offset);
      /* read resources sequentially, starting with ID 501, until we encounter
	 an "end" resource or we can't find the next resource */
      while (rsrc_pos < nrsrc) {
	int offset = ftell(ifp);
	int id = read_two(ifp);
	if (id == want_id) {
	  (void) read_two(ifp);
	  (void) read_one(ifp);
	  num_extracted++;
	  if (!extract_data(ifp, ofp, &w, res_data_offset + read_three(ifp), pfb))
	    break;
	  second_time = 0;
	  want_id++;
	}
	reposition(ifp, offset + 12);
	rsrc_pos++;
	if (rsrc_pos >= nrsrc && !second_time) {
	  reposition(ifp, list_offset);
	  rsrc_pos = 0;
	}
      }
      break;
    } else {
      (void) read_two(ifp);
      (void) read_two(ifp);
    }
  }

#if 0
  system("/bin/rm -f /tmp/x.*");
  {
    FILE *f;
    int i;
    reposition(ifp, res_offset + 16);
    if ((f = fopen("/tmp/x.systemarea", "wb"))) {
      for (i = res_offset + 16; i < res_data_offset; i++) {
	putc(getc(ifp), f);
      }
      fclose(f);
    }
  }
  reposition(ifp, type_list_offset);
  num_types = read_two(ifp) + 1;
  while (num_types--) {
    int t = read_four(ifp);
    int num_of_type = 1 + read_two(ifp);
    int32_t save_offset = ftell(ifp) + 2;
    reposition(ifp, type_list_offset + read_two(ifp));
    while (num_of_type--) {
      FILE *f;
      char buf[2048];
      int x, i, attrs;
      x = ftell(ifp);
      i = read_two(ifp);	/* ID */
      read_two(ifp);
      attrs = read_one(ifp);
      sprintf(buf, "/tmp/x.%c%c%c%c.%d", (t>>24)&255, (t>>16)&255, (t>>8)&255, t&255, i);
      fprintf(stderr, "%c%c%c%c.%d %d", (t>>24)&255, (t>>16)&255, (t>>8)&255, t&255, i, attrs);
      if ((f = fopen(buf, "wb"))) {
	int l;
	reposition(ifp, res_data_offset + read_three(ifp));
	l = read_four(ifp);
	fprintf(stderr, " %d\n", l);
	while (l > 0) {
	  int n = (l < 2048 ? l : 2048);
	  fread(buf, 1, n, ifp);
	  fwrite(buf, 1, n, f);
	  l -= n;
	}
	fclose(f);
      }
      reposition(ifp, x + 12);
    }
    reposition(ifp, save_offset);
  }
#endif

  if (pfb)
    pfb_writer_end(&w);
  if (num_extracted == 0)
    error("%s: not a Type 1 font (no POST resources)", ifp_name);

  fclose(ifp);
  fclose(ofp);
  return 0;
}