David Cook
Well-known member
For testing and cataloging purposes, I wrote a routine that compares two files to determine any differences. Everything works well, except when comparing the resource forks. It turns out, in classic Mac OS, a portion of the resource fork header differs when a file is copied.
Steps to Reproduce
1. Create a test file with a resource fork. For example: Open SimpleText (not TeachText), choose New, type some stuff, choose Save, name it “untitled”. This creates a really boring text file that importantly includes a resource fork.
2. In the Finder, duplicate your “untitled” file however you wish. For example, choose Duplicate from the File menu, or option drag “untitled” to another folder, or even Stuff and Unstuff it to another copy. At this point, you should have two copies of the “untitled” file, perhaps with the second one named “untitled copy” or “untitled (2)”.
3. Using HexEdit (or any other editor that let’s you view the raw contents of the resource fork), open the raw resource fork of the original “untitled” file and the copy you made.

Observe that a chunk of the file differs in these "identical" files. (Aside: there is a lot of unrelated garbage in the first 256 bytes because whatever OS routine created this did not clear the memory buffer first. Boo! Boo!)

In the example above, notice that the filename and file types unexpectedly appear within the resource fork and differ between the two “duplicate” files. The second copy was created from Unstuffing and has a clue in the type/owner of “Part” and “SIT!”. It appears the information about the file itself is captured as the resource fork is being established (i.e. “Part”) – before StuffIt gives the file the final type/creator of “TEXT” “ttxt”.
The leaked Macintosh source code confirms that a “directory copy” is placed in the resource header.

Technical Note 74 claims the directory information is to “aid in scavenging”. And, in fact, Apple acknowledges in Technical Note 74 that duplicated files won’t match: “The duplicate may not be exactly like the original”.

Who Cares?
Let’s say you have a massive collection of backed-up disks and you want to create a listing of unique files. Or maybe you want to recognize newer/patched software when the developer didn’t actually increment the version number. Or, you have written a transfer program that verifies that the transferred file matches the source file. Well, you can’t just checksum the file and use that for comparison. You need to exclude some of the bytes in the resource header during the calculation.
Can You Fix a Duplicate?
I wrote code to intentionally target the resource header bytes of an existing file. It appears bytes from offset $30 to $7D are being protected. FileManager? The write call returns noErr but doesn’t do anything to those bytes. It does successfully write before and after the protected range.

According to Inside Macintosh Vol I, the first 16 bytes are for the resource manager, the next 112 are for the system, and the last 128 are for the application. (Technical Note #62 takes that back – the application should not use those bytes in the header. But, I was able to write to that space.)

I have not yet discovered how a well-behaving application (including the Finder) can perfectly duplicate a file.
Next Steps
I have searched through the ROM source without any luck of finding the operating system code that block writes to the header. I guess I will need to step through using Macsbug. I suspect there is a flag that indicates when the system is making a write call, to allow it to modify the header. Or, maybe a range of bytes is being set to protected on the OpenRF call?
I have not tried using PBHCopyFile. However, that is a newer call not available on many OS versions, and is primarily aimed at optimizing remote (AppleShare) server copying. Such a call would not help with stuffing/unstuffing or modem transfers.
Steps to Reproduce
1. Create a test file with a resource fork. For example: Open SimpleText (not TeachText), choose New, type some stuff, choose Save, name it “untitled”. This creates a really boring text file that importantly includes a resource fork.
2. In the Finder, duplicate your “untitled” file however you wish. For example, choose Duplicate from the File menu, or option drag “untitled” to another folder, or even Stuff and Unstuff it to another copy. At this point, you should have two copies of the “untitled” file, perhaps with the second one named “untitled copy” or “untitled (2)”.
3. Using HexEdit (or any other editor that let’s you view the raw contents of the resource fork), open the raw resource fork of the original “untitled” file and the copy you made.

Observe that a chunk of the file differs in these "identical" files. (Aside: there is a lot of unrelated garbage in the first 256 bytes because whatever OS routine created this did not clear the memory buffer first. Boo! Boo!)

In the example above, notice that the filename and file types unexpectedly appear within the resource fork and differ between the two “duplicate” files. The second copy was created from Unstuffing and has a clue in the type/owner of “Part” and “SIT!”. It appears the information about the file itself is captured as the resource fork is being established (i.e. “Part”) – before StuffIt gives the file the final type/creator of “TEXT” “ttxt”.
The leaked Macintosh source code confirms that a “directory copy” is placed in the resource header.

Technical Note 74 claims the directory information is to “aid in scavenging”. And, in fact, Apple acknowledges in Technical Note 74 that duplicated files won’t match: “The duplicate may not be exactly like the original”.

Who Cares?
Let’s say you have a massive collection of backed-up disks and you want to create a listing of unique files. Or maybe you want to recognize newer/patched software when the developer didn’t actually increment the version number. Or, you have written a transfer program that verifies that the transferred file matches the source file. Well, you can’t just checksum the file and use that for comparison. You need to exclude some of the bytes in the resource header during the calculation.
Can You Fix a Duplicate?
I wrote code to intentionally target the resource header bytes of an existing file. It appears bytes from offset $30 to $7D are being protected. FileManager? The write call returns noErr but doesn’t do anything to those bytes. It does successfully write before and after the protected range.

According to Inside Macintosh Vol I, the first 16 bytes are for the resource manager, the next 112 are for the system, and the last 128 are for the application. (Technical Note #62 takes that back – the application should not use those bytes in the header. But, I was able to write to that space.)

I have not yet discovered how a well-behaving application (including the Finder) can perfectly duplicate a file.
Next Steps
I have searched through the ROM source without any luck of finding the operating system code that block writes to the header. I guess I will need to step through using Macsbug. I suspect there is a flag that indicates when the system is making a write call, to allow it to modify the header. Or, maybe a range of bytes is being set to protected on the OpenRF call?
I have not tried using PBHCopyFile. However, that is a newer call not available on many OS versions, and is primarily aimed at optimizing remote (AppleShare) server copying. Such a call would not help with stuffing/unstuffing or modem transfers.