Jump to content

setting hex values for a file?


sw0o0sh

Recommended Posts

Im not sure the terminology for what I am exactly trying to do, but I need to know the functions if any for what I'm trying to do.

 

Say I had a GameSave for a typical game wheres its memory is available for edit and people know the addresses to edit what.

 

Say I wanted to open a file called "gamesave.sps", and set the hex address 0x03003056 to have the value FF, or so. Is this possible with PHP?

Link to comment
Share on other sites

Yeah, i tried doing lots of things like turning it into a string and then rewritting it with file commands after the edits have been made. However, it breaks and such since its not exactly... ISO (or a correct character type anyway) (ever try to open an image in a text document, yeah, looks a bit egyption >_>) ... Do you know how to exactly do what your speaking of? Because as far as ive seen, when this file is opened in a text document, its all one line.

Link to comment
Share on other sites

Well um.. it worked lol.. wrote the value in the right spot, however corrupted the rest of the file and left it all blank. Anyway I could loop this and keep everything else intact aswell as the REST of the line where the address 0x030030ED resides?

Link to comment
Share on other sites

Actually, Im starting to believe the reason ANYTHING happened from what you gave me is due to the parameter in your fopen, ive never heard of w+b.. and yeah, when i switched to A+ (since this is meant to UPDATE their save file), it didnt do anything at all >_>

 

Oh, and dont worry, im only using this on a test file atm.

 

but thanks for the input, im pretty sure what you are telling me works somehow, how is yet to be found out though.

Link to comment
Share on other sites

Wont allow me to edit the above post, so yeah. Heres where I'm standing at the moment...

 

Sorry, I didnt realize the b meant force binary reading (did some research).

 

So anyway, if possible. I'd like to open a file up named gamesave.sps

 

I also want it to edit the line 0x03003056 and set it first value to 0xFF.

 

which can apparently be done with fwrite($handle,0xFF,3); (FF translated = 255);

 

Problem at the moment, if it writes, the whole file is ate and just the value is set. If i use w+b, it sets the line however erased everything else.. if i use a+b it just adds it to the end of the file (as it appends >_>)

 

But anyway, i need to be able to fseek the line 0x3003056, which can be done with fseek($handle,0x3003056,0x3003056) { to prevent it from going further }

 

So... the question being, does anyone here know how to find the hexadecimal address, append the first value, and the break (meaning stop), and leaving the rest of the file intact?

 

Someone please help >_>

Link to comment
Share on other sites

Okay, I'll have to adjust some of my advice as I didn't read carefully.  I've done similar things in Perl but not PHP.  And I used the wrong hex in the fwrite....

 

As you've found out the 'w' mode truncates the file to zero length first and 'a' always appends to the end, so you'll need to use the 'r+b' mode (that's not rhythm & blues).

 

$fh = fopen('gamesave.sps','r+b');
fseek($fh,0x03003056);
fwrite($fh,0xFF,1);
fclose($fh);

 

That should be closer to the truth.

Link to comment
Share on other sites

If I get this down my sites, alot of people I know will be very happy for the ease involved in it. People have to do hard manual labor elsewise >_>;

 

Chr(), as from what I can see (read the manual), is used to return one value from a given str of data?

 

Anyway, 0xFF = 255, and I was going to debug our routine here once I actually got it somewhat to work. First of all, the PHP is writing 0xFF as 255 when it HAS to be FF, not 255. (Basically its translating it when I dont want it too), so I was just going to make it a string rather than integer.

 

In my example we have been using one of the many examples for addresses, however the funny thing is 0xFF isnt even really valid for this address.

 

"03003056"

 

Valid attributes to set here would be 00 00, 27 A0, 0C C2, 27 0F, and ofcourse FF FF. So in this specific instance, the max we would want to set is 4 characters to be modified, started right at that address, and not editting anymore.

 

A more simpler REAL address would be:

 

"03003098"

 

Which can be set from anything to 00 - 19.

 

I personally know its possible, its just a matter of finding this exact address, only modifying the four or two characters found with the users input, and leaving the rest of the file intact.

 

But thanks, it's very much appreciated.

 

 

~tim

Link to comment
Share on other sites

Well, try fwrite($fh,chr(0xFF),1) (or whatever) instead -- maybe that will write the single byte value of FF to the file.  Perhaps PHP is writing "255" instead because it's interpreting 0xFF as an integer or string or something.  I've never worked with PHP this way before, so I'm not certain myself.

Link to comment
Share on other sites

Oh, it writes fine in the appropriate position (at least from what I can tell). It's just a matter of not letting it strip out the content somewhat before it aswell as after.

 

$fh = fopen('gamesave.sps','r+b');
fseek($fh,0x03003056);
fwrite($fh,0xFF,1);
fclose($fh);

 

That is what I got down so far.

 

And yeah, I'm notorious for pushing the hell out of the limits within PHP..

Link to comment
Share on other sites

This snippet worked as follows:

 

<?php

file_put_contents('test.bin','abc123');
echo file_get_contents('test.bin'),"\n";
foreach (str_split(file_get_contents('test.bin')) as $v) printf('%X ',ord($v));
echo "\n";
// --------------------------- Begin changes
$fh = fopen('test.bin','r+b');
fseek($fh,0x1);
fwrite($fh,chr(0x42),1);
fclose($fh);
// --------------------------- End changes
foreach (str_split(file_get_contents('test.bin')) as $v) printf('%X ',ord($v));
echo "\n";
echo file_get_contents('test.bin'),"\n";

?>

Output:

abc123
61 62 63 31 32 33 
61 42 63 31 32 33 
aBc123

 

I used it to change 'b' to 'B' -- change the value of offset 0x01 to 0x42.

Link to comment
Share on other sites

Still co

This snippet worked as follows:

 

<?php

file_put_contents('test.bin','abc123');
echo file_get_contents('test.bin'),"\n";
foreach (str_split(file_get_contents('test.bin')) as $v) printf('%X ',ord($v));
echo "\n";
// --------------------------- Begin changes
$fh = fopen('test.bin','r+b');
fseek($fh,0x1);
fwrite($fh,chr(0x42),1);
fclose($fh);
// --------------------------- End changes
foreach (str_split(file_get_contents('test.bin')) as $v) printf('%X ',ord($v));
echo "\n";
echo file_get_contents('test.bin'),"\n";

?>

Output:

abc123
61 62 63 31 32 33 
61 42 63 31 32 33 
aBc123

 

I used it to change 'b' to 'B' -- change the value of offset 0x01 to 0x42.

 

Still corrupts data, is it because the file you are trying to edit here is small opposed to one that is 64KB?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.