Hacking [C#] TCPGecko.cs, how to peek just 1 byte from an address?

Dekirai

Well-Known Member
OP
Member
Joined
Jun 21, 2016
Messages
1,050
Trophies
0
XP
2,616
Country
Germany
Is there a good way to peek just 1 byte from an address?
Everytime I'll try it, it takes the full 4 byte length of the address, no matter what I try.

E.g. Wind Waker Map Address starts with "0x109764F0", the first byte is "73" the second "65", the third "61" and the fourth "00", so its 73656100
Now I wan't to read only the first or second byte instead of the full range.
So I would use "0x109764F1" to get the second byte, but it still uses the full range starting with 73, instead of 65.
Can somebody help me out here?

Also every "0" gets ignored by peeking, which corrupts the reading of a value
e.g. when the value is 000001FF the result will be something like 1FF.

The code for peeking is this
Code:
        public UInt32 peek(UInt32 address)
        {
            if (!ValidMemory.ValidAddress(address))
            {
                return 0;
            }

            //address will be alligned to 4
            UInt32 paddress = address & 0xFFFFFFFC;

            //Create a memory stream for the actual dump
            MemoryStream stream = new MemoryStream();

            //make sure to not send data to the output
            GeckoProgress oldUpdate = this.PChunkUpdate;
            this.PChunkUpdate = null;

            try
            {
                //dump data
                this.Dump(paddress, paddress + 4, stream);

                //go to beginning
                stream.Seek(0, SeekOrigin.Begin);
                Byte[] buffer = new Byte[4];
                stream.Read(buffer, 0, 4);

                //Read buffer
                UInt32 result = BitConverter.ToUInt32(buffer, 0);

                //Swap to machine endianness and return
                result = ByteSwap.Swap(result);

                return result;
            }
            finally
            {
                this.PChunkUpdate = oldUpdate;

                //make sure the Stream is properly closed
                stream.Close();
            }
        }

How would I modify it to read just 1 byte from an address?
TCPGecko.cs on GitHub > TCPGecko.cs
 
Last edited by Dekirai,

smf

Well-Known Member
Member
Joined
Feb 23, 2009
Messages
6,643
Trophies
2
XP
5,867
Country
United Kingdom
if you call ReadAddress(address,1) then it looks like it should do what you want.

In a round about way it does the equivalent of this (which should also work).

public UInt8 ReadAddress8(UInt32 addressToRead)
{
if (addressToRead < startAddress) return 0;
if (addressToRead > endAddress - 1) return 0;
return mem[index(addressToRead)];
}

0's aren't ignored when peeking, they will get dropped when you're printing them out if you aren't telling it to pad with 0's.

for example
string.Format("{0:X}", value); // only print as many characters as required to display the value
vs
string.Format("{0:X8}", value); // always print at least 8 hex characters, padding with zeros.
 
Last edited by smf,
  • Like
Reactions: Dekirai

smf

Well-Known Member
Member
Joined
Feb 23, 2009
Messages
6,643
Trophies
2
XP
5,867
Country
United Kingdom
Although that uses the Dump class, rather than the peek() method. It looks a really badly designed class. Not helped by having a class called the same thing as one of the important methods. If it were me. If you need bytes then you could do something like

(peek(address) >> ((address & 3) * 4)) & 0xff

but I can't even get my brain around what that will do for endian issues. So you might need to reverse the order by inverting address first. which I think this would do it

(peek(address) >> ((~address & 3) * 4)) & 0xff

but my c# is rusty.

I'd probably end up rewriting all that code, it's way over complicated and has much overhead.
 
Last edited by smf,
  • Like
Reactions: Dekirai

Dekirai

Well-Known Member
OP
Member
Joined
Jun 21, 2016
Messages
1,050
Trophies
0
XP
2,616
Country
Germany
Thank you for the quick reply.
Using "(peek(address) >> ((~address & 3) * 4)) & 0xff"
worked very well and yea I noticed that the Code is kinda garbage too but I don't care that much yet.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    Psionic Roshambo @ Psionic Roshambo: Poor and jaundiced is he!