2 * $Id: macbin.c,v 1.8 2001-06-29 14:14:46 rufustfirefly Exp $
7 #endif /* HAVE_CONFIG_H */
12 #include <sys/param.h>
15 #endif /* HAVE_FCNTL_H */
18 #endif /* HAVE_UNISTD_H */
26 #include <atalk/adouble.h>
27 #include <netatalk/endian.h>
31 /* This allows megatron to generate .bin files that won't choke other
32 well-known converter apps. It also makes sure that checksums
33 always match. (RLB) */
34 #define MACBINARY_PLAY_NICE_WITH_OTHERS
36 /* String used to indicate standard input instead of a disk
37 file. Should be a string not normally used for a file
48 /* Size of a macbinary file header
50 #define HEADBUFSIZ 128
52 /* Both input and output routines use this struct and the
53 following globals; therefore this module can only be used
54 for one of the two functions at a time.
56 struct bin_file_data {
57 u_int32_t forklen[ NUMFORKS ];
58 char path[ MAXPATHLEN + 1];
61 time_t gmtoff; /* to convert from/to localtime */
64 extern char *forkname[];
65 u_char head_buf[HEADBUFSIZ];
68 * bin_open must be called first. pass it a filename that is supposed
69 * to contain a macbinary file. an bin struct will be allocated and
70 * somewhat initialized; bin_filed is set.
73 int bin_open( binfile, flags, fh, options )
84 fprintf( stderr, "entering bin_open\n" );
87 /* call localtime so that we get the timezone offset */
89 #ifndef NO_STRUCT_TM_GMTOFF
93 bin.gmtoff = tp->tm_gmtoff;
94 #endif /* ! NO_STRUCT_TM_GMTOFF */
96 if ( flags == O_RDONLY ) { /* input */
97 if ( strcmp( binfile, STDIN ) == 0 ) {
98 bin.filed = fileno( stdin );
99 } else if (( bin.filed = open( binfile, flags )) < 0 ) {
104 fprintf( stderr, "opened %s for read\n", binfile );
106 if ((( rc = test_header() ) > 0 ) &&
107 ( bin_header_read( fh, rc ) == 0 )) {
110 fprintf( stderr, "%s is not a macbinary file.\n", binfile );
112 } else { /* output */
113 if (options & OPTION_STDOUT)
114 bin.filed = fileno(stdout);
116 maxlen = sizeof( bin.path ) - 1;
118 fprintf( stderr, "sizeof bin.path\t\t\t%d\n", sizeof( bin.path ));
119 fprintf( stderr, "maxlen \t\t\t\t%d\n", maxlen );
121 strncpy( bin.path, fh->name, maxlen );
122 strncpy( bin.path, mtoupath( bin.path ), maxlen );
123 strncat( bin.path, ".bin", maxlen - strlen( bin.path ));
124 if (( bin.filed = open( bin.path, flags, 0666 )) < 0 ) {
129 fprintf( stderr, "opened %s for write\n",
130 (options & OPTION_STDOUT) ? "(stdout)" : bin.path );
134 if ( bin_header_write( fh ) != 0 ) {
136 fprintf( stderr, "%s\n", bin.path );
144 * bin_close must be called before a second file can be opened using
145 * bin_open. Upon successful completion, a value of 0 is returned.
146 * Otherwise, a value of -1 is returned.
149 int bin_close( keepflag )
153 fprintf( stderr, "entering bin_close\n" );
155 if ( keepflag == KEEP ) {
156 return( close( bin.filed ));
157 } else if ( keepflag == TRASH ) {
158 if (( strcmp( bin.path, STDIN ) != 0 ) &&
159 ( unlink( bin.path ) < 0 )) {
167 * bin_read is called until it returns zero for each fork. when it is
168 * and finds that there is zero left to give, it seeks to the position
169 * of the next fork (if there is one ).
170 * bin_read must be called enough times to
171 * return zero and no more than that.
174 int bin_read( fork, buffer, length )
185 fprintf( stderr, "bin_read: fork is %s\n", forkname[ fork ] );
186 fprintf( stderr, "bin_read: remaining length is %d\n", bin.forklen[fork] );
187 #endif /* DEBUG >= 3 */
189 if ( bin.forklen[ fork ] < 0 ) {
190 fprintf( stderr, "This should never happen, dude!\n" );
191 return( bin.forklen[ fork ] );
194 if ( bin.forklen[ fork ] == 0 ) {
195 if ( fork == DATA ) {
196 pos = lseek( bin.filed, 0, SEEK_CUR );
198 fprintf( stderr, "current position is %ld\n", pos );
202 pos = lseek( bin.filed, HEADBUFSIZ - pos, SEEK_CUR );
205 fprintf( stderr, "current position is %ld\n", pos );
211 if ( bin.forklen[ fork ] < length ) {
212 readlen = bin.forklen[ fork ];
217 fprintf( stderr, "bin_read: readlen is %d\n", readlen );
218 fprintf( stderr, "bin_read: cc is %d\n", cc );
219 #endif /* DEBUG >= 3 */
222 while (( readlen > 0 ) && ( cc > 0 )) {
223 if (( cc = read( bin.filed, buf_ptr, readlen )) > 0 ) {
225 fprintf( stderr, "bin_read: cc is %d\n", cc );
226 #endif /* DEBUG >= 3 */
232 cc = buf_ptr - buffer;
233 bin.forklen[ fork ] -= cc;
237 fprintf( stderr, "bin_read: chars read is %d\n", cc );
238 #endif /* DEBUG >= 3 */
246 int bin_write( fork, buffer, length )
255 u_char padchar = 0x7f;
256 /* Not sure why, but it seems this must be 0x7f to match
257 other converters, not 0. (RLB) */
260 fprintf( stderr, "bin_write: fork is %s\n", forkname[ fork ] );
261 fprintf( stderr, "bin_write: remaining length is %d\n", bin.forklen[fork] );
262 #endif /* DEBUG >= 3 */
264 if (( fork == RESOURCE ) && ( bin.forklen[ DATA ] != 0 )) {
265 fprintf( stderr, "Forklength error.\n" );
269 buf_ptr = (char *)buffer;
270 if ( bin.forklen[ fork ] >= length ) {
273 fprintf( stderr, "Forklength error.\n" );
278 fprintf( stderr, "bin_write: write length is %d\n", writelen );
279 #endif /* DEBUG >= 3 */
281 while (( writelen > 0 ) && ( cc >= 0 )) {
282 cc = write( bin.filed, buf_ptr, writelen );
287 perror( "Couldn't write to macbinary file:" );
290 bin.forklen[ fork ] -= length;
292 if ( bin.forklen[ fork ] < 0 ) {
293 fprintf( stderr, "This should never happen, dude!\n" );
294 return( bin.forklen[ fork ] );
298 * add the padding at end of data and resource forks
301 if ( bin.forklen[ fork ] == 0 ) {
302 pos = lseek( bin.filed, 0, SEEK_CUR );
304 fprintf( stderr, "current position is %ld\n", pos );
307 if (pos != 0) { /* pad only if we need to */
308 pos = lseek( bin.filed, HEADBUFSIZ - pos - 1, SEEK_CUR );
309 if ( write( bin.filed, &padchar, 1 ) != 1 ) {
310 perror( "Couldn't write to macbinary file:" );
315 fprintf( stderr, "current position is %ld\n", pos );
320 fprintf( stderr, "\n" );
327 * bin_header_read is called by bin_open, and before any information can
328 * read from the fh substruct. it must be called before any
329 * of the bytes of the other two forks can be read, as well.
332 int bin_header_read( fh, revision )
339 * Set the appropriate finder flags mask for the type of macbinary
340 * file it is, and copy the extra macbinary II stuff from the header.
341 * If it is not a macbinary file revision of I or II, then return
345 switch ( revision ) {
348 mask = htons( 0xfcee );
349 memcpy(&fh->finder_info.fdFlags + 1, head_buf + 101,1 );
352 mask = htons( 0xfc00 );
360 * Go through and copy all the stuff you can get from the
361 * MacBinary header into the fh struct. What fun!
364 memcpy(fh->name, head_buf + 2, head_buf[ 1 ] );
365 memcpy(&fh->create_date, head_buf + 91, 4 );
366 fh->create_date = MAC_DATE_TO_UNIX(fh->create_date) - bin.gmtoff;
367 fh->create_date = AD_DATE_FROM_UNIX(fh->create_date);
368 memcpy( &fh->mod_date, head_buf + 95, 4 );
369 fh->mod_date = MAC_DATE_TO_UNIX(fh->mod_date) - bin.gmtoff;
370 fh->mod_date = AD_DATE_FROM_UNIX(fh->mod_date);
371 fh->backup_date = AD_DATE_START;
372 memcpy( &fh->finder_info, head_buf + 65, 8 );
374 #ifndef MACBINARY_PLAY_NICE_WITH_OTHERS /* (RLB) */
375 memcpy( &fh->finder_info.fdFlags, head_buf + 73, 1 );
376 fh->finder_info.fdFlags &= mask;
377 #else /* ! MACBINARY_PLAY_NICE_WITH_OTHERS */
378 memcpy( &fh->finder_info.fdFlags, head_buf + 73, 2 );
379 #endif /* ! MACBINARY_PLAY_NICE_WITH_OTHERS */
381 memcpy(&fh->finder_info.fdLocation, head_buf + 75, 4 );
382 memcpy(&fh->finder_info.fdFldr, head_buf + 79, 2 );
383 memcpy(&fh->forklen[ DATA ], head_buf + 83, 4 );
384 bin.forklen[ DATA ] = ntohl( fh->forklen[ DATA ] );
385 memcpy(&fh->forklen[ RESOURCE ], head_buf + 87, 4 );
386 bin.forklen[ RESOURCE ] = ntohl( fh->forklen[ RESOURCE ] );
387 fh->comment[0] = '\0';
390 fh->finder_xinfo.fdScript = *(head_buf + 106);
391 fh->finder_xinfo.fdXFlags = *(head_buf + 107);
399 fprintf( stderr, "Values read by bin_header_read\n" );
400 fprintf( stderr, "name length\t\t%d\n", head_buf[ 1 ] );
401 fprintf( stderr, "file name\t\t%s\n", fh->name );
402 fprintf( stderr, "get info comment\t%s\n", fh->comment );
403 fprintf( stderr, "type\t\t\t%.*s\n", sizeof( fh->finder_info.fdType ),
404 &fh->finder_info.fdType );
405 fprintf( stderr, "creator\t\t\t%.*s\n",
406 sizeof( fh->finder_info.fdCreator ),
407 &fh->finder_info.fdCreator );
408 memcpy( &flags, &fh->finder_info.fdFlags, sizeof( flags ));
409 flags = ntohs( flags );
410 fprintf( stderr, "flags\t\t\t%x\n", flags );
412 /* Show fdLocation too (RLB) */
413 memcpy( &flags_long, &fh->finder_info.fdLocation,
414 sizeof( flags_long ));
415 flags_long = ntohl( flags_long );
416 fprintf( stderr, "location flags\t\t%lx\n", flags_long );
418 fprintf( stderr, "data fork length\t%ld\n", bin.forklen[DATA] );
419 fprintf( stderr, "resource fork length\t%ld\n", bin.forklen[RESOURCE] );
420 fprintf( stderr, "\n" );
422 #endif /* DEBUG >= 5 */
428 * bin_header_write is called by bin_open, and relies on information
429 * from the fh substruct. it must be called before any
430 * of the bytes of the other two forks can be written, as well.
431 * bin_header_write and bin_header_read are opposites.
434 int bin_header_write( fh )
442 memset(head_buf, 0, sizeof( head_buf ));
443 head_buf[ 1 ] = (u_char)strlen( fh->name );
444 memcpy( head_buf + 2, fh->name, head_buf[ 1 ] );
445 memcpy( head_buf + 65, &fh->finder_info, 8 );
447 #ifndef MACBINARY_PLAY_NICE_WITH_OTHERS /* (RLB) */
448 memcpy( head_buf + 73, &fh->finder_info.fdFlags, 1 );
449 #else /* ! MACBINARY_PLAY_NICE_WITH_OTHERS */
450 memcpy( head_buf + 73, &fh->finder_info.fdFlags, 2 );
451 #endif /* ! MACBINARY_PLAY_NICE_WITH_OTHERS */
453 memcpy( head_buf + 75, &fh->finder_info.fdLocation, 4 );
454 memcpy( head_buf + 79, &fh->finder_info.fdFldr, 2 );
455 memcpy( head_buf + 83, &fh->forklen[ DATA ], 4 );
456 memcpy( head_buf + 87, &fh->forklen[ RESOURCE ], 4 );
457 t = AD_DATE_TO_UNIX(fh->create_date) + bin.gmtoff;
458 t = MAC_DATE_FROM_UNIX(t);
459 memcpy( head_buf + 91, &t, sizeof(t) );
460 t = AD_DATE_TO_UNIX(fh->mod_date) + bin.gmtoff;
461 t = MAC_DATE_FROM_UNIX(t);
462 memcpy( head_buf + 95, &t, sizeof(t) );
463 memcpy( head_buf + 101, &fh->finder_info.fdFlags + 1, 1);
466 memcpy( head_buf + 102, "mBIN", 4);
467 *(head_buf + 106) = fh->finder_xinfo.fdScript;
468 *(head_buf + 107) = fh->finder_xinfo.fdXFlags;
469 head_buf[ 122 ] = 130;
471 head_buf[ 123 ] = 129;
473 bin.headercrc = htons( updcrc( (u_short) 0, head_buf, 124 ));
474 memcpy(head_buf + 124, &bin.headercrc, sizeof( bin.headercrc ));
476 bin.forklen[ DATA ] = ntohl( fh->forklen[ DATA ] );
477 bin.forklen[ RESOURCE ] = ntohl( fh->forklen[ RESOURCE ] );
484 fprintf( stderr, "Values written by bin_header_write\n" );
485 fprintf( stderr, "name length\t\t%d\n", head_buf[ 1 ] );
486 fprintf( stderr, "file name\t\t%s\n", (char *)&head_buf[ 2 ] );
487 fprintf( stderr, "type\t\t\t%.4s\n", (char *)&head_buf[ 65 ] );
488 fprintf( stderr, "creator\t\t\t%.4s\n", (char *)&head_buf[ 69 ] );
490 memcpy( &flags, &fh->finder_info.fdFlags, sizeof( flags ));
491 flags = ntohs( flags );
492 fprintf( stderr, "flags\t\t\t%x\n", flags );
494 /* Show fdLocation too (RLB) */
495 memcpy( &flags_long, &fh->finder_info.fdLocation,
496 sizeof( flags_long ));
497 flags_long = ntohl( flags_long );
498 fprintf( stderr, "location flags\t\t%ldx\n", flags_long );
500 fprintf( stderr, "data fork length\t%ld\n", bin.forklen[DATA] );
501 fprintf( stderr, "resource fork length\t%ld\n", bin.forklen[RESOURCE] );
502 fprintf( stderr, "\n" );
504 #endif /* DEBUG >= 5 */
506 write_ptr = (char *)head_buf;
507 wc = sizeof( head_buf );
509 while (( wc > 0 ) && ( wr >= 0 )) {
510 wr = write( bin.filed, write_ptr, wc );
515 perror( "Couldn't write macbinary header:" );
523 * test_header is called from bin_open. it checks certain values of
524 * the first 128 bytes, determines if the file is a MacBinary,
525 * MacBinary II, MacBinary III, or non-MacBinary file, and returns a
526 * one, two, three or negative one to indicate the file type.
528 * If the signature at 102 is equal to "mBIN," then it's a MacBinary
529 * III file. Bytes 0 and 74 must be zero for the file to be any type
530 * of MacBinary. If the crc of bytes 0 through 123 equals the value
531 * at offset 124 then it is a MacBinary II. If not, then if byte 82
532 * is zero, byte 2 is a valid value for a mac filename length (between
533 * one and sixty-three), and bytes 101 through 125 are all zero, then
534 * the file is a MacBinary.
536 * NOTE: apple's MacBinary II files have a non-zero value at byte 74.
537 * so, the check for byte 74 isn't very useful.
540 int test_header(void)
542 const char zeros[25] = "";
548 fprintf( stderr, "entering test_header\n" );
551 cc = read( bin.filed, (char *)head_buf, sizeof( head_buf ));
552 if ( cc < sizeof( head_buf )) {
553 perror( "Premature end of file :" );
558 fprintf( stderr, "was able to read HEADBUFSIZ bytes\n" );
561 /* check for macbinary III header */
562 if (memcmp(head_buf + 102, "mBIN", 4) == 0)
565 /* check for macbinary II even if only one of the bytes is zero */
566 if (( head_buf[ 0 ] == 0 ) || ( head_buf[ 74 ] == 0 )) {
568 fprintf( stderr, "byte 0 and 74 are both zero\n" );
570 bin.headercrc = updcrc( (u_short) 0, head_buf, 124 );
571 memcpy(&header_crc, head_buf + 124, sizeof( header_crc ));
572 header_crc = ntohs( header_crc );
573 if ( header_crc == bin.headercrc ) {
578 fprintf( stderr, "header crc didn't pan out\n" );
582 /* now see if we have a macbinary file. */
583 if ( head_buf[ 82 ] != 0 ) {
586 memcpy( &namelen, head_buf + 1, sizeof( namelen ));
588 fprintf( stderr, "name length is %d\n", namelen );
590 if (( namelen < 1 ) || ( namelen > 63 )) {
594 /* bytes 101 - 125 should be zero */
595 if (memcmp(head_buf + 101, zeros, sizeof(zeros)) != 0)
598 /* macbinary forks aren't larger than 0x7FFFFF */
599 memcpy(&cc, head_buf + 83, sizeof(cc));
603 memcpy(&cc, head_buf + 87, sizeof(cc));
610 fprintf( stderr, "byte 82 is zero and name length is cool\n" );