views:

141

answers:

2

This might sound silly, but I'd like to know how to do the following.

I've created a .txt file. The content of the .txt file is the word "hi" without the quotes. So the .txt file contains 2 characters. When opening the .txt file in a hex-editor one sees 2 hexadecimal pairs, namely "68" and "69":

Offset
00000000 68 69 hi

What I want to do is the following. I want to take the "69" hex-pair and put it at a very specific address on the hard disk. So basically I want to take a byte and place it at a very specific address on the hard disk.

So "68" should be at address n and "69" should be at address n+1000000.

Now this is the silly part, after having done this I want my .txt file to still be 2 bytes large and I still want to be able to open my .txt file in Notepad and see the word "hi". How can I do this?

+1  A: 

Not happening.

No filesystem will let you assign byte locations for bytes on the same sector (usu. 512 bytes).

Almost no filesystem will allow you to assign byte locations that are not aligned to sectors.

Most filesystems use larger sizes of allocation units (typically 4096 bytes).

Joshua
Theoretically, you could read the entire sector and replace it with the exact same sector with two bytes changed. You would need the ability to read and write sectors at will. Unless you program drivers, this is unlikely to happen. Perhaps someone who writes disc defrag utilities might know more.
Andres
@Joshua's answer:But then how do operating systems fragment files and how do defragmentation applications create contiguous files from fragmented files on a disk? Perhaps the way those applications work is a way to do what I intend, albeit perhaps in a slightly different way. Software that manages fragmentation surely needs to be able to place parts of a file at specific address locations and move them around or make files contiguous again, right?Perhaps inspiration can be gained from that type of software.
TyTN
Software that handles disk defragmentaion moves things around at the block level. Its normally an offline operation but various online implementations have been built.
Joshua
FYI, a two byte file cannot be fragmented.
Joshua
@Andres: on Windows: \\.\PHYSICALDRIVE0 for the first hard disk
Joshua
Would it be possible to change the offset of the second byte to for example 17233? I know offsets are relative memory addresses, but perhaps messing with those would make it possible. So then one would have the first byte located in a sector at offset 00000000 and the second byte would have an offset of 00017233. All within one sector/block while maintaining the 2-byte file size. However I fear this would only be possible if offsets were somehow stored somewhere, rather than calculated relatively from the first offset 00000000.
TyTN
@TyTN: You mentioned "a way to do what I intend". What, exactly, are you trying to do? What would be the use of being able to put bytes in an arbitrary position on a hard disk?
David Thornley
@David Thornley: For clarification see my reply to TheUndeadFish's answer.
TyTN
+1  A: 

What you're asking to do pretty much goes against everything that modern operating systems and file systems try to take care of. The literal location and layout of data on the disk are purposefully abstracted away.

Somewhere somehow there is probably a way to get low level access to the file system. And in theory you might be able to place the first byte of your file in one sector and then the second byte of your file in a different section. But this makes the assumption that the file system supports that first sector being partially filled without the file being ended there. And this also assumes that no utility or such ever comes along and compacts that data together.

Generally speaking, there's no reason to ever want to do such a thing in the first place. The only reason I can imagine is that you're wanting to hide data in between those two bytes. Like writing 1000000 bytes (or how ever many) to the literal disk, but then make the OS believe that only the first and last bytes actually exists in the file. So then anyone who views the file through normal means will never see the 999998 bytes in the middle. Presumably, only your specially written program would know how to get at them.

If that is really your goal, then there might or might not be a way to do it. I don't have any other specific advice in that regard. However, you might want to research into something like TrueCrypt and how it is able to make a hidden encrypted partition on a disk.

TheUndeadFish
The goal is not to hide data, but rather to use the address of the second byte as a third byte. That way I could store 3 bytes in a 2-byte file. But in order to assign specific data to the "third byte" one would need to place that byte at a very specific address.Perhaps there is a more sensible way of storing 3 bytes in 2 bytes, but I don't know how to do that.
TyTN
@TyTN: There is no general way of storing 3 bytes in 2 bytes. If the second bytes is not right after the first one, you need to record where it is somehow, and that's extra storage.
David Thornley
@TyTN: Most (all?) modern file systems are going to be optimized for storing larger files. Hence the whole concept of sectors. So any file under the sector size will take up one whole sector regardless. As such trying to "compress" 3 bytes of data into a 2 byte file is would not only be useless but also subject to many practical problems. In addition to the file system not letting you put bytes where you want, how would this scheme ever deal with two files having the same third byte?
TheUndeadFish
*"In addition to the file system not letting you put bytes where you want, how would this scheme ever deal with two files having the same third byte?"*That's a good point.
TyTN