18 #include <atalk/adouble.h>
19 #include <netatalk/endian.h>
22 /* This allows megatron to generate .bin files that won't choke other
23 well-known converter apps. It also makes sure that checksums
24 always match. (RLB) */
25 #define MACBINARY_PLAY_NICE_WITH_OTHERS
27 /* String used to indicate standard input instead of a disk
28 file. Should be a string not normally used for a file
39 /* Size of a macbinary file header
41 #define HEADBUFSIZ 128
43 /* Forward Declarations
45 int bin_open(char *binfile, int flags, struct FHeader *fh, int options);
46 int bin_close(int keepflag);
47 int bin_read(int fork, char *buffer, int length);
48 int bin_write(int fork, char *buffer, int length);
49 int bin_header_read(struct FHeader *fh, int revision);
50 int bin_header_write(struct FHeader *fh);
51 int test_header(void);
54 /* Both input and output routines use this struct and the
55 following globals; therefore this module can only be used
56 for one of the two functions at a time.
58 struct bin_file_data {
59 u_int32_t forklen[ NUMFORKS ];
60 char path[ MAXPATHLEN + 1];
63 time_t gmtoff; /* to convert from/to localtime */
66 extern char *forkname[];
67 u_char head_buf[HEADBUFSIZ];
70 * bin_open must be called first. pass it a filename that is supposed
71 * to contain a macbinary file. an bin struct will be allocated and
72 * somewhat initialized; bin_filed is set.
75 int bin_open( binfile, flags, fh, options )
86 fprintf( stderr, "entering bin_open\n" );
89 /* call localtime so that we get the timezone offset */
91 #ifndef NO_STRUCT_TM_GMTOFF
95 bin.gmtoff = tp->tm_gmtoff;
98 if ( flags == O_RDONLY ) { /* input */
99 if ( strcmp( binfile, STDIN ) == 0 ) {
100 bin.filed = fileno( stdin );
101 } else if (( bin.filed = open( binfile, flags )) < 0 ) {
106 fprintf( stderr, "opened %s for read\n", binfile );
108 if ((( rc = test_header() ) > 0 ) &&
109 ( bin_header_read( fh, rc ) == 0 )) {
112 fprintf( stderr, "%s is not a macbinary file.\n", binfile );
114 } else { /* output */
115 if (options & OPTION_STDOUT)
116 bin.filed = fileno(stdout);
118 maxlen = sizeof( bin.path ) - 1;
120 fprintf( stderr, "sizeof bin.path\t\t\t%d\n", sizeof( bin.path ));
121 fprintf( stderr, "maxlen \t\t\t\t%d\n", maxlen );
123 strncpy( bin.path, fh->name, maxlen );
124 strncpy( bin.path, mtoupath( bin.path ), maxlen );
125 strncat( bin.path, ".bin", maxlen - strlen( bin.path ));
126 if (( bin.filed = open( bin.path, flags, 0666 )) < 0 ) {
131 fprintf( stderr, "opened %s for write\n",
132 (options & OPTION_STDOUT) ? "(stdout)" : bin.path );
136 if ( bin_header_write( fh ) != 0 ) {
138 fprintf( stderr, "%s\n", bin.path );
146 * bin_close must be called before a second file can be opened using
147 * bin_open. Upon successful completion, a value of 0 is returned.
148 * Otherwise, a value of -1 is returned.
151 int bin_close( keepflag )
155 fprintf( stderr, "entering bin_close\n" );
157 if ( keepflag == KEEP ) {
158 return( close( bin.filed ));
159 } else if ( keepflag == TRASH ) {
160 if (( strcmp( bin.path, STDIN ) != 0 ) &&
161 ( unlink( bin.path ) < 0 )) {
169 * bin_read is called until it returns zero for each fork. when it is
170 * and finds that there is zero left to give, it seeks to the position
171 * of the next fork (if there is one ).
172 * bin_read must be called enough times to
173 * return zero and no more than that.
176 int bin_read( fork, buffer, length )
187 fprintf( stderr, "bin_read: fork is %s\n", forkname[ fork ] );
188 fprintf( stderr, "bin_read: remaining length is %d\n", bin.forklen[fork] );
191 if ( bin.forklen[ fork ] < 0 ) {
192 fprintf( stderr, "This should never happen, dude!\n" );
193 return( bin.forklen[ fork ] );
196 if ( bin.forklen[ fork ] == 0 ) {
197 if ( fork == DATA ) {
198 pos = lseek( bin.filed, 0, SEEK_CUR );
200 fprintf( stderr, "current position is %ld\n", pos );
204 pos = lseek( bin.filed, HEADBUFSIZ - pos, SEEK_CUR );
207 fprintf( stderr, "current position is %ld\n", pos );
213 if ( bin.forklen[ fork ] < length ) {
214 readlen = bin.forklen[ fork ];
219 fprintf( stderr, "bin_read: readlen is %d\n", readlen );
220 fprintf( stderr, "bin_read: cc is %d\n", cc );
224 while (( readlen > 0 ) && ( cc > 0 )) {
225 if (( cc = read( bin.filed, buf_ptr, readlen )) > 0 ) {
227 fprintf( stderr, "bin_read: cc is %d\n", cc );
234 cc = buf_ptr - buffer;
235 bin.forklen[ fork ] -= cc;
239 fprintf( stderr, "bin_read: chars read is %d\n", cc );
248 int bin_write( fork, buffer, length )
257 u_char padchar = 0x7f;
258 /* Not sure why, but it seems this must be 0x7f to match
259 other converters, not 0. (RLB) */
262 fprintf( stderr, "bin_write: fork is %s\n", forkname[ fork ] );
263 fprintf( stderr, "bin_write: remaining length is %d\n", bin.forklen[fork] );
266 if (( fork == RESOURCE ) && ( bin.forklen[ DATA ] != 0 )) {
267 fprintf( stderr, "Forklength error.\n" );
271 buf_ptr = (char *)buffer;
272 if ( bin.forklen[ fork ] >= length ) {
275 fprintf( stderr, "Forklength error.\n" );
280 fprintf( stderr, "bin_write: write length is %d\n", writelen );
283 while (( writelen > 0 ) && ( cc >= 0 )) {
284 cc = write( bin.filed, buf_ptr, writelen );
289 perror( "Couldn't write to macbinary file:" );
292 bin.forklen[ fork ] -= length;
294 if ( bin.forklen[ fork ] < 0 ) {
295 fprintf( stderr, "This should never happen, dude!\n" );
296 return( bin.forklen[ fork ] );
300 * add the padding at end of data and resource forks
303 if ( bin.forklen[ fork ] == 0 ) {
304 pos = lseek( bin.filed, 0, SEEK_CUR );
306 fprintf( stderr, "current position is %ld\n", pos );
309 if (pos != 0) { /* pad only if we need to */
310 pos = lseek( bin.filed, HEADBUFSIZ - pos - 1, SEEK_CUR );
311 if ( write( bin.filed, &padchar, 1 ) != 1 ) {
312 perror( "Couldn't write to macbinary file:" );
317 fprintf( stderr, "current position is %ld\n", pos );
322 fprintf( stderr, "\n" );
329 * bin_header_read is called by bin_open, and before any information can
330 * read from the fh substruct. it must be called before any
331 * of the bytes of the other two forks can be read, as well.
334 int bin_header_read( fh, revision )
341 * Set the appropriate finder flags mask for the type of macbinary
342 * file it is, and copy the extra macbinary II stuff from the header.
343 * If it is not a macbinary file revision of I or II, then return
347 switch ( revision ) {
350 mask = htons( 0xfcee );
351 memcpy(&fh->finder_info.fdFlags + 1, head_buf + 101,1 );
354 mask = htons( 0xfc00 );
362 * Go through and copy all the stuff you can get from the
363 * MacBinary header into the fh struct. What fun!
366 memcpy(fh->name, head_buf + 2, head_buf[ 1 ] );
367 memcpy(&fh->create_date, head_buf + 91, 4 );
368 fh->create_date = MAC_DATE_TO_UNIX(fh->create_date) - bin.gmtoff;
369 fh->create_date = AD_DATE_FROM_UNIX(fh->create_date);
370 memcpy( &fh->mod_date, head_buf + 95, 4 );
371 fh->mod_date = MAC_DATE_TO_UNIX(fh->mod_date) - bin.gmtoff;
372 fh->mod_date = AD_DATE_FROM_UNIX(fh->mod_date);
373 fh->backup_date = AD_DATE_START;
374 memcpy( &fh->finder_info, head_buf + 65, 8 );
376 #ifndef MACBINARY_PLAY_NICE_WITH_OTHERS /* (RLB) */
377 memcpy( &fh->finder_info.fdFlags, head_buf + 73, 1 );
378 fh->finder_info.fdFlags &= mask;
380 memcpy( &fh->finder_info.fdFlags, head_buf + 73, 2 );
381 #endif /* ! MACBINARY_PLAY_NICE_WITH_OTHERS */
383 memcpy(&fh->finder_info.fdLocation, head_buf + 75, 4 );
384 memcpy(&fh->finder_info.fdFldr, head_buf + 79, 2 );
385 memcpy(&fh->forklen[ DATA ], head_buf + 83, 4 );
386 bin.forklen[ DATA ] = ntohl( fh->forklen[ DATA ] );
387 memcpy(&fh->forklen[ RESOURCE ], head_buf + 87, 4 );
388 bin.forklen[ RESOURCE ] = ntohl( fh->forklen[ RESOURCE ] );
389 fh->comment[0] = '\0';
392 fh->finder_xinfo.fdScript = *(head_buf + 106);
393 fh->finder_xinfo.fdXFlags = *(head_buf + 107);
401 fprintf( stderr, "Values read by bin_header_read\n" );
402 fprintf( stderr, "name length\t\t%d\n", head_buf[ 1 ] );
403 fprintf( stderr, "file name\t\t%s\n", fh->name );
404 fprintf( stderr, "get info comment\t%s\n", fh->comment );
405 fprintf( stderr, "type\t\t\t%.*s\n", sizeof( fh->finder_info.fdType ),
406 &fh->finder_info.fdType );
407 fprintf( stderr, "creator\t\t\t%.*s\n",
408 sizeof( fh->finder_info.fdCreator ),
409 &fh->finder_info.fdCreator );
410 memcpy( &flags, &fh->finder_info.fdFlags, sizeof( flags ));
411 flags = ntohs( flags );
412 fprintf( stderr, "flags\t\t\t%x\n", flags );
414 /* Show fdLocation too (RLB) */
415 memcpy( &flags_long, &fh->finder_info.fdLocation,
416 sizeof( flags_long ));
417 flags_long = ntohl( flags_long );
418 fprintf( stderr, "location flags\t\t%lx\n", flags_long );
420 fprintf( stderr, "data fork length\t%ld\n", bin.forklen[DATA] );
421 fprintf( stderr, "resource fork length\t%ld\n", bin.forklen[RESOURCE] );
422 fprintf( stderr, "\n" );
430 * bin_header_write is called by bin_open, and relies on information
431 * from the fh substruct. it must be called before any
432 * of the bytes of the other two forks can be written, as well.
433 * bin_header_write and bin_header_read are opposites.
436 int bin_header_write( fh )
444 memset(head_buf, 0, sizeof( head_buf ));
445 head_buf[ 1 ] = (u_char)strlen( fh->name );
446 memcpy( head_buf + 2, fh->name, head_buf[ 1 ] );
447 memcpy( head_buf + 65, &fh->finder_info, 8 );
449 #ifndef MACBINARY_PLAY_NICE_WITH_OTHERS /* (RLB) */
450 memcpy( head_buf + 73, &fh->finder_info.fdFlags, 1 );
452 memcpy( head_buf + 73, &fh->finder_info.fdFlags, 2 );
453 #endif /* ! MACBINARY_PLAY_NICE_WITH_OTHERS */
455 memcpy( head_buf + 75, &fh->finder_info.fdLocation, 4 );
456 memcpy( head_buf + 79, &fh->finder_info.fdFldr, 2 );
457 memcpy( head_buf + 83, &fh->forklen[ DATA ], 4 );
458 memcpy( head_buf + 87, &fh->forklen[ RESOURCE ], 4 );
459 t = AD_DATE_TO_UNIX(fh->create_date) + bin.gmtoff;
460 t = MAC_DATE_FROM_UNIX(t);
461 memcpy( head_buf + 91, &t, sizeof(t) );
462 t = AD_DATE_TO_UNIX(fh->mod_date) + bin.gmtoff;
463 t = MAC_DATE_FROM_UNIX(t);
464 memcpy( head_buf + 95, &t, sizeof(t) );
465 memcpy( head_buf + 101, &fh->finder_info.fdFlags + 1, 1);
468 memcpy( head_buf + 102, "mBIN", 4);
469 *(head_buf + 106) = fh->finder_xinfo.fdScript;
470 *(head_buf + 107) = fh->finder_xinfo.fdXFlags;
471 head_buf[ 122 ] = 130;
473 head_buf[ 123 ] = 129;
475 bin.headercrc = htons( updcrc( (u_short) 0, head_buf, 124 ));
476 memcpy(head_buf + 124, &bin.headercrc, sizeof( bin.headercrc ));
478 bin.forklen[ DATA ] = ntohl( fh->forklen[ DATA ] );
479 bin.forklen[ RESOURCE ] = ntohl( fh->forklen[ RESOURCE ] );
486 fprintf( stderr, "Values written by bin_header_write\n" );
487 fprintf( stderr, "name length\t\t%d\n", head_buf[ 1 ] );
488 fprintf( stderr, "file name\t\t%s\n", (char *)&head_buf[ 2 ] );
489 fprintf( stderr, "type\t\t\t%.4s\n", (char *)&head_buf[ 65 ] );
490 fprintf( stderr, "creator\t\t\t%.4s\n", (char *)&head_buf[ 69 ] );
492 memcpy( &flags, &fh->finder_info.fdFlags, sizeof( flags ));
493 flags = ntohs( flags );
494 fprintf( stderr, "flags\t\t\t%x\n", flags );
496 /* Show fdLocation too (RLB) */
497 memcpy( &flags_long, &fh->finder_info.fdLocation,
498 sizeof( flags_long ));
499 flags_long = ntohl( flags_long );
500 fprintf( stderr, "location flags\t\t%ldx\n", flags_long );
502 fprintf( stderr, "data fork length\t%ld\n", bin.forklen[DATA] );
503 fprintf( stderr, "resource fork length\t%ld\n", bin.forklen[RESOURCE] );
504 fprintf( stderr, "\n" );
508 write_ptr = (char *)head_buf;
509 wc = sizeof( head_buf );
511 while (( wc > 0 ) && ( wr >= 0 )) {
512 wr = write( bin.filed, write_ptr, wc );
517 perror( "Couldn't write macbinary header:" );
525 * test_header is called from bin_open. it checks certain values of
526 * the first 128 bytes, determines if the file is a MacBinary,
527 * MacBinary II, MacBinary III, or non-MacBinary file, and returns a
528 * one, two, three or negative one to indicate the file type.
530 * If the signature at 102 is equal to "mBIN," then it's a MacBinary
531 * III file. Bytes 0 and 74 must be zero for the file to be any type
532 * of MacBinary. If the crc of bytes 0 through 123 equals the value
533 * at offset 124 then it is a MacBinary II. If not, then if byte 82
534 * is zero, byte 2 is a valid value for a mac filename length (between
535 * one and sixty-three), and bytes 101 through 125 are all zero, then
536 * the file is a MacBinary.
538 * NOTE: apple's MacBinary II files have a non-zero value at byte 74.
539 * so, the check for byte 74 isn't very useful.
542 int test_header(void)
544 const char zeros[25] = "";
550 fprintf( stderr, "entering test_header\n" );
553 cc = read( bin.filed, (char *)head_buf, sizeof( head_buf ));
554 if ( cc < sizeof( head_buf )) {
555 perror( "Premature end of file :" );
560 fprintf( stderr, "was able to read HEADBUFSIZ bytes\n" );
563 /* check for macbinary III header */
564 if (memcmp(head_buf + 102, "mBIN", 4) == 0)
567 /* check for macbinary II even if only one of the bytes is zero */
568 if (( head_buf[ 0 ] == 0 ) || ( head_buf[ 74 ] == 0 )) {
570 fprintf( stderr, "byte 0 and 74 are both zero\n" );
572 bin.headercrc = updcrc( (u_short) 0, head_buf, 124 );
573 memcpy(&header_crc, head_buf + 124, sizeof( header_crc ));
574 header_crc = ntohs( header_crc );
575 if ( header_crc == bin.headercrc ) {
580 fprintf( stderr, "header crc didn't pan out\n" );
584 /* now see if we have a macbinary file. */
585 if ( head_buf[ 82 ] != 0 ) {
588 memcpy( &namelen, head_buf + 1, sizeof( namelen ));
590 fprintf( stderr, "name length is %d\n", namelen );
592 if (( namelen < 1 ) || ( namelen > 63 )) {
596 /* bytes 101 - 125 should be zero */
597 if (memcmp(head_buf + 101, zeros, sizeof(zeros)) != 0)
600 /* macbinary forks aren't larger than 0x7FFFFF */
601 memcpy(&cc, head_buf + 83, sizeof(cc));
605 memcpy(&cc, head_buf + 87, sizeof(cc));
612 fprintf( stderr, "byte 82 is zero and name length is cool\n" );