sw0o0sh Posted April 25, 2007 Share Posted April 25, 2007 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 https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/ Share on other sites More sharing options...
taith Posted April 25, 2007 Share Posted April 25, 2007 ... no... php is used for web/server based programs... Link to comment https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238406 Share on other sites More sharing options...
sw0o0sh Posted April 25, 2007 Author Share Posted April 25, 2007 Obviously, but PHP has a lot of functions for hex related issues, thus the question. Link to comment https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238440 Share on other sites More sharing options...
Wildbug Posted April 25, 2007 Share Posted April 25, 2007 Is your "address" the offset from the beginning of the file? Then why don't you draw the file into a string and use something like: <?php $string = substr_replace($string,chr(0xFF),0x03003056,1); ?> Link to comment https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238525 Share on other sites More sharing options...
sw0o0sh Posted April 25, 2007 Author Share Posted April 25, 2007 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 https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238531 Share on other sites More sharing options...
Wildbug Posted April 25, 2007 Share Posted April 25, 2007 Have you tried the filesystem functions? $fh = fopen('gamesave.sps','w+b'); fseek($fh,0x03003056); fwrite($fh,0xFF); fclose($fh); Link to comment https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238541 Share on other sites More sharing options...
sw0o0sh Posted April 25, 2007 Author Share Posted April 25, 2007 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 https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238591 Share on other sites More sharing options...
Wildbug Posted April 25, 2007 Share Posted April 25, 2007 Oh, you might need to restrict the write length: fwrite($fh,0x03003056,1); Disclaimer: I've never actually done this. You might corrupt the rest of your file and leave it all blank. This disclaimer applies retroactively. Link to comment https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238598 Share on other sites More sharing options...
sw0o0sh Posted April 25, 2007 Author Share Posted April 25, 2007 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 https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238606 Share on other sites More sharing options...
sw0o0sh Posted April 25, 2007 Author Share Posted April 25, 2007 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 https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238617 Share on other sites More sharing options...
Wildbug Posted April 25, 2007 Share Posted April 25, 2007 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 https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238627 Share on other sites More sharing options...
sw0o0sh Posted April 26, 2007 Author Share Posted April 26, 2007 Left a LOT of the original file in tact, but also whitespaced out a lot.. bah. Is this idea hopeless? Link to comment https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238666 Share on other sites More sharing options...
Wildbug Posted April 26, 2007 Share Posted April 26, 2007 That's weird. No, it's not hopeless, just weird. I'll try this when I get home tonight. Maybe writing the hex value in the fwrite is wrong; maybe it expects chr() or something. Link to comment https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238711 Share on other sites More sharing options...
sw0o0sh Posted April 26, 2007 Author Share Posted April 26, 2007 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 https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238719 Share on other sites More sharing options...
Wildbug Posted April 26, 2007 Share Posted April 26, 2007 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 https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238726 Share on other sites More sharing options...
sw0o0sh Posted April 26, 2007 Author Share Posted April 26, 2007 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 https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-238731 Share on other sites More sharing options...
Wildbug Posted April 26, 2007 Share Posted April 26, 2007 $fh = fopen('gamesave.sps','r+b'); fseek($fh,0x03003056); fwrite($fh,chr(0xFF),1) fclose($fh); Did you try that? It worked for me. Link to comment https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-239052 Share on other sites More sharing options...
sw0o0sh Posted April 26, 2007 Author Share Posted April 26, 2007 It's still corrupting a good amount of the file to whitespace Link to comment https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-239264 Share on other sites More sharing options...
Wildbug Posted April 26, 2007 Share Posted April 26, 2007 That's odd. Are you sure the offset is correct? 0x03003056 = 50,344,022 bytes. That's a big file. Link to comment https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-239282 Share on other sites More sharing options...
Wildbug Posted April 26, 2007 Share Posted April 26, 2007 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 https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-239355 Share on other sites More sharing options...
sw0o0sh Posted May 3, 2007 Author Share Posted May 3, 2007 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 https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-244337 Share on other sites More sharing options...
Wildbug Posted May 3, 2007 Share Posted May 3, 2007 Still corrupts data, is it because the file you are trying to edit here is small opposed to one that is 64KB? I doubt it. I suspect your offset is wrong. Otherwise, I have no idea. Link to comment https://forums.phpfreaks.com/topic/48671-setting-hex-values-for-a-file/#findComment-244378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.