13 #include <atalk/adouble.h>
14 #include <netatalk/endian.h>
19 /* String used to indicate standard input instead of a disk
20 file. Should be a string not normally used for a file
31 /* Size of a macbinary file header
33 #define HEADBUFSIZ 128
37 /* Both input and output routines use this struct and the
38 following globals; therefore this module can only be used
39 for one of the two functions at a time.
41 struct bin_file_data {
42 u_int32_t forklen[ NUMFORKS ];
43 char path[ MAXPATHLEN + 1];
46 time_t gmtoff; /* to convert from/to localtime */
49 extern char *forkname[];
50 u_char head_buf[HEADBUFSIZ];
53 * bin_open must be called first. pass it a filename that is supposed
54 * to contain a macbinary file. an bin struct will be allocated and
55 * somewhat initialized; bin_filed is set.
58 bin_open( binfile, flags, fh, options )
69 fprintf( stderr, "entering bin_open\n" );
72 /* call localtime so that we get the timezone offset */
74 #ifndef NO_STRUCT_TM_GMTOFF
76 if (tp = localtime(&t))
77 bin.gmtoff = tp->tm_gmtoff;
80 if ( flags == O_RDONLY ) { /* input */
81 if ( strcmp( binfile, STDIN ) == 0 ) {
82 bin.filed = fileno( stdin );
83 } else if (( bin.filed = open( binfile, flags )) < 0 ) {
88 fprintf( stderr, "opened %s for read\n", binfile );
90 if ((( rc = test_header() ) > 0 ) &&
91 ( bin_header_read( fh, rc ) == 0 )) {
94 fprintf( stderr, "%s is not a macbinary file.\n", binfile );
97 if (options & OPTION_STDOUT)
98 bin.filed = fileno(stdout);
100 maxlen = sizeof( bin.path ) - 1;
102 fprintf( stderr, "sizeof bin.path\t\t\t%d\n", sizeof( bin.path ));
103 fprintf( stderr, "maxlen \t\t\t\t%d\n", maxlen );
105 strncpy( bin.path, fh->name, maxlen );
106 strncpy( bin.path, mtoupath( bin.path ), maxlen );
107 strncat( bin.path, ".bin", maxlen - strlen( bin.path ));
108 if (( bin.filed = open( bin.path, flags, 0666 )) < 0 ) {
113 fprintf( stderr, "opened %s for write\n",
114 (options & OPTION_STDOUT) ? "(stdout)" : bin.path );
118 if ( bin_header_write( fh ) != 0 ) {
120 fprintf( stderr, "%s\n", bin.path );
128 * bin_close must be called before a second file can be opened using
129 * bin_open. Upon successful completion, a value of 0 is returned.
130 * Otherwise, a value of -1 is returned.
133 bin_close( keepflag )
137 fprintf( stderr, "entering bin_close\n" );
139 if ( keepflag == KEEP ) {
140 return( close( bin.filed ));
141 } else if ( keepflag == TRASH ) {
142 if (( strcmp( bin.path, STDIN ) != 0 ) &&
143 ( unlink( bin.path ) < 0 )) {
151 * bin_read is called until it returns zero for each fork. when it is
152 * and finds that there is zero left to give, it seeks to the position
153 * of the next fork (if there is one ).
154 * bin_read must be called enough times to
155 * return zero and no more than that.
158 bin_read( fork, buffer, length )
169 fprintf( stderr, "bin_read: fork is %s\n", forkname[ fork ] );
170 fprintf( stderr, "bin_read: remaining length is %d\n", bin.forklen[fork] );
173 if ( bin.forklen[ fork ] < 0 ) {
174 fprintf( stderr, "This should never happen, dude!\n" );
175 return( bin.forklen[ fork ] );
178 if ( bin.forklen[ fork ] == 0 ) {
179 if ( fork == DATA ) {
180 pos = lseek( bin.filed, 0, SEEK_CUR );
182 fprintf( stderr, "current position is %ld\n", pos );
184 if (pos = pos % HEADBUFSIZ) {
185 pos = lseek( bin.filed, HEADBUFSIZ - pos, SEEK_CUR );
188 fprintf( stderr, "current position is %ld\n", pos );
194 if ( bin.forklen[ fork ] < length ) {
195 readlen = bin.forklen[ fork ];
200 fprintf( stderr, "bin_read: readlen is %d\n", readlen );
201 fprintf( stderr, "bin_read: cc is %d\n", cc );
205 while (( readlen > 0 ) && ( cc > 0 )) {
206 if (( cc = read( bin.filed, buf_ptr, readlen )) > 0 ) {
208 fprintf( stderr, "bin_read: cc is %d\n", cc );
215 cc = buf_ptr - buffer;
216 bin.forklen[ fork ] -= cc;
220 fprintf( stderr, "bin_read: chars read is %d\n", cc );
229 bin_write( fork, buffer, length )
241 fprintf( stderr, "bin_write: fork is %s\n", forkname[ fork ] );
242 fprintf( stderr, "bin_write: remaining length is %d\n", bin.forklen[fork] );
245 if (( fork == RESOURCE ) && ( bin.forklen[ DATA ] != 0 )) {
246 fprintf( stderr, "Forklength error.\n" );
250 buf_ptr = (char *)buffer;
251 if ( bin.forklen[ fork ] >= length ) {
254 fprintf( stderr, "Forklength error.\n" );
259 fprintf( stderr, "bin_write: write length is %d\n", writelen );
262 while (( writelen > 0 ) && ( cc >= 0 )) {
263 cc = write( bin.filed, buf_ptr, writelen );
268 perror( "Couldn't write to macbinary file:" );
271 bin.forklen[ fork ] -= length;
273 if ( bin.forklen[ fork ] < 0 ) {
274 fprintf( stderr, "This should never happen, dude!\n" );
275 return( bin.forklen[ fork ] );
279 * add the padding at end of data and resource forks
282 if ( bin.forklen[ fork ] == 0 ) {
283 pos = lseek( bin.filed, 0, SEEK_CUR );
285 fprintf( stderr, "current position is %ld\n", pos );
287 if (pos = pos % HEADBUFSIZ) { /* pad only if we need to */
288 pos = lseek( bin.filed, HEADBUFSIZ - pos - 1, SEEK_CUR );
289 if ( write( bin.filed, &padchar, 1 ) != 1 ) {
290 perror( "Couldn't write to macbinary file:" );
295 fprintf( stderr, "current position is %ld\n", pos );
300 fprintf( stderr, "\n" );
307 * bin_header_read is called by bin_open, and before any information can
308 * read from the fh substruct. it must be called before any
309 * of the bytes of the other two forks can be read, as well.
312 bin_header_read( fh, revision )
319 * Set the appropriate finder flags mask for the type of macbinary
320 * file it is, and copy the extra macbinary II stuff from the header.
321 * If it is not a macbinary file revision of I or II, then return
325 switch ( revision ) {
328 mask = htons( 0xfcee );
329 memcpy(&fh->finder_info.fdFlags + 1, head_buf + 101,1 );
332 mask = htons( 0xfc00 );
340 * Go through and copy all the stuff you can get from the
341 * MacBinary header into the fh struct. What fun!
344 memcpy(fh->name, head_buf + 2, head_buf[ 1 ] );
345 memcpy(&fh->create_date, head_buf + 91, 4 );
346 fh->create_date = MAC_DATE_TO_UNIX(fh->create_date) - bin.gmtoff;
347 fh->create_date = AD_DATE_FROM_UNIX(fh->create_date);
348 memcpy( &fh->mod_date, head_buf + 95, 4 );
349 fh->mod_date = MAC_DATE_TO_UNIX(fh->mod_date) - bin.gmtoff;
350 fh->mod_date = AD_DATE_FROM_UNIX(fh->mod_date);
351 fh->backup_date = AD_DATE_START;
352 memcpy( &fh->finder_info, head_buf + 65, 8 );
353 memcpy( &fh->finder_info.fdFlags, head_buf + 73, 1 );
354 fh->finder_info.fdFlags &= mask;
355 memcpy(&fh->finder_info.fdLocation, head_buf + 75, 4 );
356 memcpy(&fh->finder_info.fdFldr, head_buf + 79, 2 );
357 memcpy(&fh->forklen[ DATA ], head_buf + 83, 4 );
358 bin.forklen[ DATA ] = ntohl( fh->forklen[ DATA ] );
359 memcpy(&fh->forklen[ RESOURCE ], head_buf + 87, 4 );
360 bin.forklen[ RESOURCE ] = ntohl( fh->forklen[ RESOURCE ] );
361 fh->comment[0] = '\0';
364 fh->finder_xinfo.fdScript = *(head_buf + 106);
365 fh->finder_xinfo.fdXFlags = *(head_buf + 107);
372 fprintf( stderr, "Values read by bin_header_read\n" );
373 fprintf( stderr, "name length\t\t%d\n", head_buf[ 1 ] );
374 fprintf( stderr, "file name\t\t%s\n", fh->name );
375 fprintf( stderr, "get info comment\t%s\n", fh->comment );
376 fprintf( stderr, "type\t\t\t%.*s\n", sizeof( fh->finder_info.fdType ),
377 &fh->finder_info.fdType );
378 fprintf( stderr, "creator\t\t\t%.*s\n",
379 sizeof( fh->finder_info.fdCreator ),
380 &fh->finder_info.fdCreator );
381 memcpy( &flags, &fh->finder_info.fdFlags, sizeof( flags ));
382 flags = ntohs( flags );
383 fprintf( stderr, "flags\t\t\t%x\n", flags );
384 fprintf( stderr, "data fork length\t%ld\n", bin.forklen[DATA] );
385 fprintf( stderr, "resource fork length\t%ld\n", bin.forklen[RESOURCE] );
386 fprintf( stderr, "\n" );
394 * bin_header_write is called by bin_open, and relies on information
395 * from the fh substruct. it must be called before any
396 * of the bytes of the other two forks can be written, as well.
397 * bin_header_write and bin_header_read are opposites.
400 bin_header_write( fh )
408 memset(head_buf, 0, sizeof( head_buf ));
409 head_buf[ 1 ] = (u_char)strlen( fh->name );
410 memcpy( head_buf + 2, fh->name, head_buf[ 1 ] );
411 memcpy( head_buf + 65, &fh->finder_info, 8 );
412 memcpy( head_buf + 73, &fh->finder_info.fdFlags, 1);
413 memcpy( head_buf + 75, &fh->finder_info.fdLocation, 4 );
414 memcpy( head_buf + 79, &fh->finder_info.fdFldr, 2 );
415 memcpy( head_buf + 83, &fh->forklen[ DATA ], 4 );
416 memcpy( head_buf + 87, &fh->forklen[ RESOURCE ], 4 );
417 t = AD_DATE_TO_UNIX(fh->create_date) + bin.gmtoff;
418 t = MAC_DATE_FROM_UNIX(t);
419 memcpy( head_buf + 91, &t, sizeof(t) );
420 t = AD_DATE_TO_UNIX(fh->mod_date) + bin.gmtoff;
421 t = MAC_DATE_FROM_UNIX(t);
422 memcpy( head_buf + 95, &t, sizeof(t) );
423 memcpy( head_buf + 101, &fh->finder_info.fdFlags + 1, 1);
426 memcpy( head_buf + 102, "mBIN", 4);
427 *(head_buf + 106) = fh->finder_xinfo.fdScript;
428 *(head_buf + 107) = fh->finder_xinfo.fdXFlags;
429 head_buf[ 122 ] = 130;
431 head_buf[ 123 ] = 129;
433 bin.headercrc = htons( updcrc( (u_short) 0, head_buf, 124 ));
434 memcpy(head_buf + 124, &bin.headercrc, sizeof( bin.headercrc ));
436 bin.forklen[ DATA ] = ntohl( fh->forklen[ DATA ] );
437 bin.forklen[ RESOURCE ] = ntohl( fh->forklen[ RESOURCE ] );
441 fprintf( stderr, "Values written by bin_header_write\n" );
442 fprintf( stderr, "name length\t\t%d\n", head_buf[ 1 ] );
443 fprintf( stderr, "file name\t\t%s\n", (char *)&head_buf[ 2 ] );
444 fprintf( stderr, "type\t\t\t%.4s\n", (char *)&head_buf[ 65 ] );
445 fprintf( stderr, "creator\t\t\t%.4s\n", (char *)&head_buf[ 69 ] );
446 fprintf( stderr, "data fork length\t%ld\n", bin.forklen[DATA] );
447 fprintf( stderr, "resource fork length\t%ld\n", bin.forklen[RESOURCE] );
448 fprintf( stderr, "\n" );
452 write_ptr = (char *)head_buf;
453 wc = sizeof( head_buf );
455 while (( wc > 0 ) && ( wr >= 0 )) {
456 wr = write( bin.filed, write_ptr, wc );
461 perror( "Couldn't write macbinary header:" );
469 * test_header is called from bin_open. it checks certain values of
470 * the first 128 bytes, determines if the file is a MacBinary,
471 * MacBinary II, MacBinary III, or non-MacBinary file, and returns a
472 * one, two, three or negative one to indicate the file type.
474 * If the signature at 102 is equal to "mBIN," then it's a MacBinary
475 * III file. Bytes 0 and 74 must be zero for the file to be any type
476 * of MacBinary. If the crc of bytes 0 through 123 equals the value
477 * at offset 124 then it is a MacBinary II. If not, then if byte 82
478 * is zero, byte 2 is a valid value for a mac filename length (between
479 * one and sixty-three), and bytes 101 through 125 are all zero, then
480 * the file is a MacBinary.
482 * NOTE: apple's MacBinary II files have a non-zero value at byte 74.
483 * so, the check for byte 74 isn't very useful.
488 const char zeros[25] = "";
494 fprintf( stderr, "entering test_header\n" );
497 cc = read( bin.filed, (char *)head_buf, sizeof( head_buf ));
498 if ( cc < sizeof( head_buf )) {
499 perror( "Premature end of file :" );
504 fprintf( stderr, "was able to read HEADBUFSIZ bytes\n" );
507 /* check for macbinary III header */
508 if (memcmp(head_buf + 102, "mBIN", 4) == 0)
511 /* check for macbinary II even if only one of the bytes is zero */
512 if (( head_buf[ 0 ] == 0 ) || ( head_buf[ 74 ] == 0 )) {
514 fprintf( stderr, "byte 0 and 74 are both zero\n" );
516 bin.headercrc = updcrc( (u_short) 0, head_buf, 124 );
517 memcpy(&header_crc, head_buf + 124, sizeof( header_crc ));
518 header_crc = ntohs( header_crc );
519 if ( header_crc == bin.headercrc ) {
524 fprintf( stderr, "header crc didn't pan out\n" );
528 /* now see if we have a macbinary file. */
529 if ( head_buf[ 82 ] != 0 ) {
532 memcpy( &namelen, head_buf + 1, sizeof( namelen ));
534 fprintf( stderr, "name length is %d\n", namelen );
536 if (( namelen < 1 ) || ( namelen > 63 )) {
540 /* bytes 101 - 125 should be zero */
541 if (memcmp(head_buf + 101, zeros, sizeof(zeros)) != 0)
544 /* macbinary forks aren't larger than 0x7FFFFF */
545 memcpy(&cc, head_buf + 83, sizeof(cc));
549 memcpy(&cc, head_buf + 87, sizeof(cc));
556 fprintf( stderr, "byte 82 is zero and name length is cool\n" );