[go: up one dir, main page]

File: unpost.c

package info (click to toggle)
t1utils 1.2-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 148 kB
  • ctags: 173
  • sloc: ansic: 1,558; makefile: 78; sh: 10
file content (368 lines) | stat: -rw-r--r-- 8,701 bytes parent folder | download
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
/* unpost
 *
 * This program converts Macintosh type-1 fonts stored in MacBinary (I or II)
 * format or raw resource fork to PFA and PFB formats.
 *
 * Copyright (c) 1992 by I. Lee Hetherington, all rights reserved.
 *
 * Permission is hereby granted to use, modify, and distribute this program
 * for any purpose provided this copyright notice and the one below remain
 * intact.
 *
 * I. Lee Hetherington (ilh@lcs.mit.edu)
 *
 * $Log:	unpost.c,v $
 * 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. */

#ifndef lint
static char rcsid[] =
  "@(#) $Id: unpost.c,v 1.2 92/06/23 10:57:33 ilh Exp $";
static char copyright[] =
  "@(#) Copyright (c) 1992 by I. Lee Hetherington, all rights reserved.";
#ifdef _MSDOS
static char portnotice[] =
  "@(#) Ported to MS-DOS by Kai-Uwe Herbing (herbing@netmbx.netmbx.de).";
#endif
#endif

#ifdef _MSDOS
  #include <fcntl.h>
  #include <getopt.h>
  #include <io.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

/* int32 must be at least 32-bit */
#if INT_MAX >= 0x7FFFFFFFUL
typedef int int32;
#else
typedef long int32;
#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 fgetc(fi);
}

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

  val = read_one(fi);
  val = (val << 8) + read_one(fi);

  return val;
}

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

  val = read_one(fi);
  val = (val << 8) + read_one(fi);
  val = (val << 8) + read_one(fi);

  return val;
}

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

  val = read_one(fi);
  val = (val << 8) + read_one(fi);
  val = (val << 8) + read_one(fi);
  val = (val << 8) + read_one(fi);

  return val;
}

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

static void write_pfb_length(FILE *fo, int32 len)
{
  fputc((int)(len & 0xff), fo);
  len >>= 8;
  fputc((int)(len & 0xff), fo);
  len >>= 8;
  fputc((int)(len & 0xff), fo);
  len >>= 8;
  fputc((int)(len & 0xff), fo);
}

static void reposition(FILE *fi, int32 absolute)
{
  if (fseek(fi, absolute, 0) == -1) {
    fprintf(stderr, "error: fseek failed to position %d.\n", absolute);
    fprintf(stderr, "       Is this file seekable?\n");
    exit(1);
  }
}

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

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

  if (hex_column > 62) {                          /* 64 column output */
    fputc('\n', fo);
    hex_column = 0;
  }
  fputc(hex[b >> 4], fo);
  fputc(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. */

static void extract_data(FILE *fi, FILE *fo, int32 offset, int binary)
{
  enum PS_type { PS_ascii = 1, PS_binary = 2, PS_end = 5 };
  static enum PS_type last_type = PS_ascii;
  int32 len, save_offset = ftell(fi);
  int 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 (binary) {
      fputc(128, fo);
      fputc(1, fo);
      write_pfb_length(fo, len);
      while (len--) {
	if ((c = read_one(fi)) == '\r')           /* change \r to \n */
	  c = '\n';
	fputc(c, fo);
      }
    } else {
      if (last_type == PS_binary)
	fputc('\n', fo);
      while (len--) {
	if ((c = read_one(fi)) == '\r')           /* change \r to \n */
	  c = '\n';
	fputc(c, fo);
      }
    }
    last_type = 1;
    break;
  case PS_binary:
    (void) read_one(fi);
    if (binary) {
      fputc(128, fo);
      fputc(2, fo);
      write_pfb_length(fo, len);
      while (len--)
	fputc(read_one(fi), fo);
    } else {
      if (last_type != 2)
	hex_column = 0;
      while (len--)
	output_hex_byte(fo, read_one(fi));
      last_type = 2;
    }
    break;
  case PS_end:
    (void) read_one(fi);
    if (binary) {
      fputc(128, fo);
      fputc(3, fo);
    }
    break;
  }
  reposition(fi, save_offset);
}

static void usage(void)
{
  fprintf(stderr, "usage: unpost [-b] [-r] in-file [out-file]\n");
  exit(1);
}

static void print_banner()
{
  static char rcs_revision[] = "$Revision: 1.2 $";
  static char revision[20];

  if (sscanf(rcs_revision, "$Revision: %19s", revision) != 1)
    revision[0] = '\0';
  fprintf(stderr, "This is unpost %s.\n", revision);
}

int main(int argc, char **argv)
{
  FILE *fi;
  FILE *fo = stdout;
  int32 data_fork_size;
  int32 res_offset, res_data_offset, res_map_offset, type_list_offset;
  int32 post_type;
  int num_types, num_of_type, num_extracted = 0, binary = 0, raw = 0;
  int c;
  extern int optind;
  extern int getopt(int, char **, char*);

  print_banner();

  /* parse command line */
  while ((c = getopt(argc, argv, "br?")) != -1)
    switch(c) {
    case 'b':
      ++binary;
      break;
    case 'r':
      ++raw;
      break;
    default:
      usage();
    }
  if (argc - optind < 1 || argc - optind > 2)
    usage();

  /* open input file */
  #ifdef _MSDOS
    /* As we are processing an input file in Macintosh */
    /* format, we should avoid any kind of translation */
    /* and consequently open it in binary file mode.   */
    fi = fopen(argv[optind], "rb");
  #else
    fi = fopen(argv[optind], "r");
  #endif
  if (!fi) {
    fprintf(stderr, "error: cannot open %s for reading\n", argv[1]);
    exit(1);
  }
  /* possibly open output file */
  if (argc - optind == 2) {
    fo = fopen(argv[optind + 1], "w");
    if (!fo) {
      fprintf(stderr, "error: cannot open %s for writing\n", argv[2]);
      exit(1);
    }
  }

  #ifdef _MSDOS
    /* If we are processing a PFB (binary) output */
    /* file, we must set its file mode to binary. */
    if (binary)
      _setmode(_fileno(fo), _O_BINARY);
  #endif

  if (raw) {

    /* raw resource file */

    res_offset = 0;

  } else {

    /* MacBinary (I or II) file */

    /* SHOULD CHECK INTEGRITY OF MACBINARY HEADER HERE TO VERIFY THAT WE
       REALLY HAVE A MACBINARY FILE.  MACBINARY-II-STANDARD.TXT DESCRIBES
       AN APPROPRIATE VERIFICATION PROCEDURE. */

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

    /* 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;

  }

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

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

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

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

  while (num_types--) {
    if (read_four(fi) == post_type) {
      num_of_type = 1 + read_two(fi);
      reposition(fi, type_list_offset + read_two(fi));
      while (num_of_type--) {
	(void) read_two(fi);                      /* ID */
	(void) read_two(fi);
	(void) read_one(fi);
	extract_data(fi, fo, res_data_offset + read_three(fi), binary);
	++num_extracted;
	(void) read_four(fi);
      }
      break;
    } else {
      (void) read_two(fi);
      (void) read_two(fi);
    }
  }

  fclose(fi);
  if (fo != stdout) {
    fclose(fo);
    fprintf(stderr, "Extracted %d POST resource%s.\n", num_extracted,
	    (num_extracted == 1) ? "" : "s");
  }

  return 0;
}

/*
 *  for Emacs...
 *  Local Variables:
 *  mode: C
 *  comment-column: 50
 *  fill-column: 79
 *  c-indent-level: 2
 *  c-continued-statement-offset: 2
 *  c-brace-offset: -2
 *  c-argdecl-indent: 2
 *  c-label-offset: -2
 *  End:
 */