If I have a very big buffer in one place, but I want to copy only a tiny buffer, I still know the offset that would correspond if I want to access it (for example, if I want to read only 1 or 2 sectors of a FAT12 table at a time).
A slow way would be like this:
Code: Select all
var originalValue=24531;
//The following will result in 467,
//but will be uselessly slow:
///
while(originalValue>=512)
{
originalValue-=512;
}
And it seems that a way would be to use an AND with a mask set to all 1's for the size that corresponds to the size of the partial buffer. For example, for a 512-byte partial buffer, it would just take:
Code: Select all
var originalValue=24531;
//The following will result in 467, with base address of 0:
///
originalValue&=parseInt("111111111", 2); //AND with 511 to get a value between 0 to 511
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
(FAT) Reading 2 File Allocation Table Sectors At a Time Instead of 1
It seems to be a trick similar to that from the Graphics Programming Black Book, which talks about reading and searching through partially-loaded files with resume.
It looks like some Cluster Numbers when stored on disk traverse and go beyond a single sector, specifically those that happen to start at byte 511 of a 512-byte sector. Each cluster for FAT12 uses 2 bytes, so it would end up with 1 byte in each sector, the first at the very end of the first sector (offset 511) and the second at the very start of the second/next sector (offset 0).
So, as long as we have already adjusted and know the actual offset of the cluster into the FAT, we will always avoid that division of a cluster value between 2 sectors if we simply read 2 FAT sectors instead of 1. Then the partial value will now always be available with this trick.
The code to handle it will also be smaller and we will just need a 1024-byte buffer instead of a 512-byte one.
It seems to be very good trick to be able to read partial values, be able to resume them, and avoid reading the whole data set (the whole FAT table).
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
Getting Lowest and Highest Number from Bigger Value?
Now the question is, how could I also get an offset only between N and M, for example, only between 1-511 or only between 127-511 instead of the typical one 0-511 where only the highest value matters but not the lowest?