18 #include <atalk/adouble.h>
19 #include <netatalk/endian.h>
23 /* This allows megatron to generate .bin files that won't choke other
24 well-known converter apps. It also makes sure that checksums
25 always match. (RLB) */
26 #define MACBINARY_PLAY_NICE_WITH_OTHERS
28 /* String used to indicate standard input instead of a disk
29 file. Should be a string not normally used for a file
40 /* Size of a macbinary file header
42 #define HEADBUFSIZ 128
44 /* Both input and output routines use this struct and the
45 following globals; therefore this module can only be used
46 for one of the two functions at a time.
48 struct bin_file_data {
49 u_int32_t forklen[ NUMFORKS ];
50 char path[ MAXPATHLEN + 1];
53 time_t gmtoff; /* to convert from/to localtime */
56 extern char *forkname[];
57 u_char head_buf[HEADBUFSIZ];
60 * bin_open must be called first. pass it a filename that is supposed
61 * to contain a macbinary file. an bin struct will be allocated and
62 * somewhat initialized; bin_filed is set.
65 int bin_open( binfile, flags, fh, options )
76 fprintf( stderr, "entering bin_open\n" );
79 /* call localtime so that we get the timezone offset */
81 #ifndef NO_STRUCT_TM_GMTOFF
85 bin.gmtoff = tp->tm_gmtoff;
88 if ( flags == O_RDONLY ) { /* input */
89 if ( strcmp( binfile, STDIN ) == 0 ) {
90 bin.filed = fileno( stdin );
91 } else if (( bin.filed = open( binfile, flags )) < 0 ) {
96 fprintf( stderr, "opened %s for read\n", binfile );
98 if ((( rc = test_header() ) > 0 ) &&
99 ( bin_header_read( fh, rc ) == 0 )) {
102 fprintf( stderr, "%s is not a macbinary file.\n", binfile );
104 } else { /* output */
105 if (options & OPTION_STDOUT)
106 bin.filed = fileno(stdout);
108 maxlen = sizeof( bin.path ) - 1;
110 fprintf( stderr, "sizeof bin.path\t\t\t%d\n", sizeof( bin.path ));
111 fprintf( stderr, "maxlen \t\t\t\t%d\n", maxlen );
113 strncpy( bin.path, fh->name, maxlen );
114 strncpy( bin.path, mtoupath( bin.path ), maxlen );
115 strncat( bin.path, ".bin", maxlen - strlen( bin.path ));
116 if (( bin.filed = open( bin.path, flags, 0666 )) < 0 ) {
121 fprintf( stderr, "opened %s for write\n",
122 (options & OPTION_STDOUT) ? "(stdout)" : bin.path );
126 if ( bin_header_write( fh ) != 0 ) {
128 fprintf( stderr, "%s\n", bin.path );
136 * bin_close must be called before a second file can be opened using
137 * bin_open. Upon successful completion, a value of 0 is returned.
138 * Otherwise, a value of -1 is returned.
141 int bin_close( keepflag )
145 fprintf( stderr, "entering bin_close\n" );
147 if ( keepflag == KEEP ) {
148 return( close( bin.filed ));
149 } else if ( keepflag == TRASH ) {
150 if (( strcmp( bin.path, STDIN ) != 0 ) &&
151 ( unlink( bin.path ) < 0 )) {
159 * bin_read is called until it returns zero for each fork. when it is
160 * and finds that there is zero left to give, it seeks to the position
161 * of the next fork (if there is one ).
162 * bin_read must be called enough times to
163 * return zero and no more than that.
166 int bin_read( fork, buffer, length )
177 fprintf( stderr, "bin_read: fork is %s\n", forkname[ fork ] );
178 fprintf( stderr, "bin_read: remaining length is %d\n", bin.forklen[fork] );
181 if ( bin.forklen[ fork ] < 0 ) {
182 fprintf( stderr, "This should never happen, dude!\n" );
183 return( bin.forklen[ fork ] );
186 if ( bin.forklen[ fork ] == 0 ) {
187 if ( fork == DATA ) {
188 pos = lseek( bin.filed, 0, SEEK_CUR );
190 fprintf( stderr, "current position is %ld\n", pos );
194 pos = lseek( bin.filed, HEADBUFSIZ - pos, SEEK_CUR );
197 fprintf( stderr, "current position is %ld\n", pos );
203 if ( bin.forklen[ fork ] < length ) {
204 readlen = bin.forklen[ fork ];
209 fprintf( stderr, "bin_read: readlen is %d\n", readlen );
210 fprintf( stderr, "bin_read: cc is %d\n", cc );
214 while (( readlen > 0 ) && ( cc > 0 )) {
215 if (( cc = read( bin.filed, buf_ptr, readlen )) > 0 ) {
217 fprintf( stderr, "bin_read: cc is %d\n", cc );
224 cc = buf_ptr - buffer;
225 bin.forklen[ fork ] -= cc;
229 fprintf( stderr, "bin_read: chars read is %d\n", cc );
238 int bin_write( fork, buffer, length )
247 u_char padchar = 0x7f;
248 /* Not sure why, but it seems this must be 0x7f to match
249 other converters, not 0. (RLB) */
252 fprintf( stderr, "bin_write: fork is %s\n", forkname[ fork ] );
253 fprintf( stderr, "bin_write: remaining length is %d\n", bin.forklen[fork] );
256 if (( fork == RESOURCE ) && ( bin.forklen[ DATA ] != 0 )) {
257 fprintf( stderr, "Forklength error.\n" );
261 buf_ptr = (char *)buffer;
262 if ( bin.forklen[ fork ] >= length ) {
265 fprintf( stderr, "Forklength error.\n" );
270 fprintf( stderr, "bin_write: write length is %d\n", writelen );
273 while (( writelen > 0 ) && ( cc >= 0 )) {
274 cc = write( bin.filed, buf_ptr, writelen );
279 perror( "Couldn't write to macbinary file:" );
282 bin.forklen[ fork ] -= length;
284 if ( bin.forklen[ fork ] < 0 ) {
285 fprintf( stderr, "This should never happen, dude!\n" );
286 return( bin.forklen[ fork ] );
290 * add the padding at end of data and resource forks
293 if ( bin.forklen[ fork ] == 0 ) {
294 pos = lseek( bin.filed, 0, SEEK_CUR );
296 fprintf( stderr, "current position is %ld\n", pos );
299 if (pos != 0) { /* pad only if we need to */
300 pos = lseek( bin.filed, HEADBUFSIZ - pos - 1, SEEK_CUR );
301 if ( write( bin.filed, &padchar, 1 ) != 1 ) {
302 perror( "Couldn't write to macbinary file:" );
307 fprintf( stderr, "current position is %ld\n", pos );
312 fprintf( stderr, "\n" );
319 * bin_header_read is called by bin_open, and before any information can
320 * read from the fh substruct. it must be called before any
321 * of the bytes of the other two forks can be read, as well.
324 int bin_header_read( fh, revision )
331 * Set the appropriate finder flags mask for the type of macbinary
332 * file it is, and copy the extra macbinary II stuff from the header.
333 * If it is not a macbinary file revision of I or II, then return
337 switch ( revision ) {
340 mask = htons( 0xfcee );
341 memcpy(&fh->finder_info.fdFlags + 1, head_buf + 101,1 );
344 mask = htons( 0xfc00 );
352 * Go through and copy all the stuff you can get from the
353 * MacBinary header into the fh struct. What fun!
356 memcpy(fh->name, head_buf + 2, head_buf[ 1 ] );
357 memcpy(&fh->create_date, head_buf + 91, 4 );
358 fh->create_date = MAC_DATE_TO_UNIX(fh->create_date) - bin.gmtoff;
359 fh->create_date = AD_DATE_FROM_UNIX(fh->create_date);
360 memcpy( &fh->mod_date, head_buf + 95, 4 );
361 fh->mod_date = MAC_DATE_TO_UNIX(fh->mod_date) - bin.gmtoff;
362 fh->mod_date = AD_DATE_FROM_UNIX(fh->mod_date);
363 fh->backup_date = AD_DATE_START;
364 memcpy( &fh->finder_info, head_buf + 65, 8 );
366 #ifndef MACBINARY_PLAY_NICE_WITH_OTHERS /* (RLB) */
367 memcpy( &fh->finder_info.fdFlags, head_buf + 73, 1 );
368 fh->finder_info.fdFlags &= mask;
370 memcpy( &fh->finder_info.fdFlags, head_buf + 73, 2 );
371 #endif /* ! MACBINARY_PLAY_NICE_WITH_OTHERS */
373 memcpy(&fh->finder_info.fdLocation, head_buf + 75, 4 );
374 memcpy(&fh->finder_info.fdFldr, head_buf + 79, 2 );
375 memcpy(&fh->forklen[ DATA ], head_buf + 83, 4 );
376 bin.forklen[ DATA ] = ntohl( fh->forklen[ DATA ] );
377 memcpy(&fh->forklen[ RESOURCE ], head_buf + 87, 4 );
378 bin.forklen[ RESOURCE ] = ntohl( fh->forklen[ RESOURCE ] );
379 fh->comment[0] = '\0';
382 fh->finder_xinfo.fdScript = *(head_buf + 106);
383 fh->finder_xinfo.fdXFlags = *(head_buf + 107);
391 fprintf( stderr, "Values read by bin_header_read\n" );
392 fprintf( stderr, "name length\t\t%d\n", head_buf[ 1 ] );
393 fprintf( stderr, "file name\t\t%s\n", fh->name );
394 fprintf( stderr, "get info comment\t%s\n", fh->comment );
395 fprintf( stderr, "type\t\t\t%.*s\n", sizeof( fh->finder_info.fdType ),
396 &fh->finder_info.fdType );
397 fprintf( stderr, "creator\t\t\t%.*s\n",
398 sizeof( fh->finder_info.fdCreator ),
399 &fh->finder_info.fdCreator );
400 memcpy( &flags, &fh->finder_info.fdFlags, sizeof( flags ));
401 flags = ntohs( flags );
402 fprintf( stderr, "flags\t\t\t%x\n", flags );
404 /* Show fdLocation too (RLB) */
405 memcpy( &flags_long, &fh->finder_info.fdLocation,
406 sizeof( flags_long ));
407 flags_long = ntohl( flags_long );
408 fprintf( stderr, "location flags\t\t%lx\n", flags_long );
410 fprintf( stderr, "data fork length\t%ld\n", bin.forklen[DATA] );
411 fprintf( stderr, "resource fork length\t%ld\n", bin.forklen[RESOURCE] );
412 fprintf( stderr, "\n" );
420 * bin_header_write is called by bin_open, and relies on information
421 * from the fh substruct. it must be called before any
422 * of the bytes of the other two forks can be written, as well.
423 * bin_header_write and bin_header_read are opposites.
426 int bin_header_write( fh )
434 memset(head_buf, 0, sizeof( head_buf ));
435 head_buf[ 1 ] = (u_char)strlen( fh->name );
436 memcpy( head_buf + 2, fh->name, head_buf[ 1 ] );
437 memcpy( head_buf + 65, &fh->finder_info, 8 );
439 #ifndef MACBINARY_PLAY_NICE_WITH_OTHERS /* (RLB) */
440 memcpy( head_buf + 73, &fh->finder_info.fdFlags, 1 );
442 memcpy( head_buf + 73, &fh->finder_info.fdFlags, 2 );
443 #endif /* ! MACBINARY_PLAY_NICE_WITH_OTHERS */
445 memcpy( head_buf + 75, &fh->finder_info.fdLocation, 4 );
446 memcpy( head_buf + 79, &fh->finder_info.fdFldr, 2 );
447 memcpy( head_buf + 83, &fh->forklen[ DATA ], 4 );
448 memcpy( head_buf + 87, &fh->forklen[ RESOURCE ], 4 );
449 t = AD_DATE_TO_UNIX(fh->create_date) + bin.gmtoff;
450 t = MAC_DATE_FROM_UNIX(t);
451 memcpy( head_buf + 91, &t, sizeof(t) );
452 t = AD_DATE_TO_UNIX(fh->mod_date) + bin.gmtoff;
453 t = MAC_DATE_FROM_UNIX(t);
454 memcpy( head_buf + 95, &t, sizeof(t) );
455 memcpy( head_buf + 101, &fh->finder_info.fdFlags + 1, 1);
458 memcpy( head_buf + 102, "mBIN", 4);
459 *(head_buf + 106) = fh->finder_xinfo.fdScript;
460 *(head_buf + 107) = fh->finder_xinfo.fdXFlags;
461 head_buf[ 122 ] = 130;
463 head_buf[ 123 ] = 129;
465 bin.headercrc = htons( updcrc( (u_short) 0, head_buf, 124 ));
466 memcpy(head_buf + 124, &bin.headercrc, sizeof( bin.headercrc ));
468 bin.forklen[ DATA ] = ntohl( fh->forklen[ DATA ] );
469 bin.forklen[ RESOURCE ] = ntohl( fh->forklen[ RESOURCE ] );
476 fprintf( stderr, "Values written by bin_header_write\n" );
477 fprintf( stderr, "name length\t\t%d\n", head_buf[ 1 ] );
478 fprintf( stderr, "file name\t\t%s\n", (char *)&head_buf[ 2 ] );
479 fprintf( stderr, "type\t\t\t%.4s\n", (char *)&head_buf[ 65 ] );
480 fprintf( stderr, "creator\t\t\t%.4s\n", (char *)&head_buf[ 69 ] );
482 memcpy( &flags, &fh->finder_info.fdFlags, sizeof( flags ));
483 flags = ntohs( flags );
484 fprintf( stderr, "flags\t\t\t%x\n", flags );
486 /* Show fdLocation too (RLB) */
487 memcpy( &flags_long, &fh->finder_info.fdLocation,
488 sizeof( flags_long ));
489 flags_long = ntohl( flags_long );
490 fprintf( stderr, "location flags\t\t%ldx\n", flags_long );
492 fprintf( stderr, "data fork length\t%ld\n", bin.forklen[DATA] );
493 fprintf( stderr, "resource fork length\t%ld\n", bin.forklen[RESOURCE] );
494 fprintf( stderr, "\n" );
498 write_ptr = (char *)head_buf;
499 wc = sizeof( head_buf );
501 while (( wc > 0 ) && ( wr >= 0 )) {
502 wr = write( bin.filed, write_ptr, wc );
507 perror( "Couldn't write macbinary header:" );
515 * test_header is called from bin_open. it checks certain values of
516 * the first 128 bytes, determines if the file is a MacBinary,
517 * MacBinary II, MacBinary III, or non-MacBinary file, and returns a
518 * one, two, three or negative one to indicate the file type.
520 * If the signature at 102 is equal to "mBIN," then it's a MacBinary
521 * III file. Bytes 0 and 74 must be zero for the file to be any type
522 * of MacBinary. If the crc of bytes 0 through 123 equals the value
523 * at offset 124 then it is a MacBinary II. If not, then if byte 82
524 * is zero, byte 2 is a valid value for a mac filename length (between
525 * one and sixty-three), and bytes 101 through 125 are all zero, then
526 * the file is a MacBinary.
528 * NOTE: apple's MacBinary II files have a non-zero value at byte 74.
529 * so, the check for byte 74 isn't very useful.
532 int test_header(void)
534 const char zeros[25] = "";
540 fprintf( stderr, "entering test_header\n" );
543 cc = read( bin.filed, (char *)head_buf, sizeof( head_buf ));
544 if ( cc < sizeof( head_buf )) {
545 perror( "Premature end of file :" );
550 fprintf( stderr, "was able to read HEADBUFSIZ bytes\n" );
553 /* check for macbinary III header */
554 if (memcmp(head_buf + 102, "mBIN", 4) == 0)
557 /* check for macbinary II even if only one of the bytes is zero */
558 if (( head_buf[ 0 ] == 0 ) || ( head_buf[ 74 ] == 0 )) {
560 fprintf( stderr, "byte 0 and 74 are both zero\n" );
562 bin.headercrc = updcrc( (u_short) 0, head_buf, 124 );
563 memcpy(&header_crc, head_buf + 124, sizeof( header_crc ));
564 header_crc = ntohs( header_crc );
565 if ( header_crc == bin.headercrc ) {
570 fprintf( stderr, "header crc didn't pan out\n" );
574 /* now see if we have a macbinary file. */
575 if ( head_buf[ 82 ] != 0 ) {
578 memcpy( &namelen, head_buf + 1, sizeof( namelen ));
580 fprintf( stderr, "name length is %d\n", namelen );
582 if (( namelen < 1 ) || ( namelen > 63 )) {
586 /* bytes 101 - 125 should be zero */
587 if (memcmp(head_buf + 101, zeros, sizeof(zeros)) != 0)
590 /* macbinary forks aren't larger than 0x7FFFFF */
591 memcpy(&cc, head_buf + 83, sizeof(cc));
595 memcpy(&cc, head_buf + 87, sizeof(cc));
602 fprintf( stderr, "byte 82 is zero and name length is cool\n" );