Page 3 of 10

Re: C development and patching for P01/P59

Posted: Fri Jul 30, 2021 5:11 am
by Gampy
Have a look see here: Difference between C and C++

Re: C development and patching for P01/P59

Posted: Sun Aug 01, 2021 7:27 am
by NSFW
I agree with sticking to plain C. It's pretty easy to see how each line of C maps to and from the corresponding asm instructions, and that will make it easier to jump in and out of the existing code.

Re: C development and patching for P01/P59

Posted: Sun Aug 01, 2021 8:57 am
by antus
yep the ++ adds the concept of objects which makes the code very modular. thats fine for some applications but not so suitable for creating patches that are injected and run inline modifying variables out side of the object. see the allpro firmware for and example of c++ done right in a small embedded system, and maybe you can read between the lines and see how its not a great fit for inline os patches. https://github.com/antuspcm/allpro

Re: C development and patching for P01/P59

Posted: Sun Aug 01, 2021 2:21 pm
by bubba2533
Ok, guess I'll be buying another book lol

Any recommendations?

Re: C development and patching for P01/P59

Posted: Mon Aug 02, 2021 4:25 am
by bubba2533
Ok, so trying to do my first basic file read of a bin to check the OS and I'm not getting the correct value.

The output I'm getting is 1393737728 when I expect 12587603. If anyone could spot the obvious mistake that I'm making I would appreciate it.

Code: Select all

#include <stdio.h>

int main()
{
	FILE* fptr;

	int operatingsystem;

	/* Open bin file */
	fopen_s(&fptr, "C:\\Users\\bubba2533\\Projects\\LS1 Boost OS V3\\12587603.bin", "rb");
	if (fptr == NULL) {
		return 3;
	}

	/* Seek to OS and read value*/
	fseek(fptr, 0x504, SEEK_SET);
	size_t elements_read = fread_s(&operatingsystem, sizeof(operatingsystem), sizeof(operatingsystem), 1, fptr);
	if (elements_read == 0) {
		return 4;
	}

	printf("Operating system of file: %u\n", operatingsystem);

	fclose(fptr);

	return 0;
}
I'm mainly just doing this exercise to try and learn more as I go. I'm definitely open to ideas on how to go about how to structure the code as I am not experienced with programming.

Re: C development and patching for P01/P59

Posted: Mon Aug 02, 2021 6:02 am
by kur4o
The result you get is byte swapped for some reason. Convert the result in hex and you will see it for yourself.

Re: C development and patching for P01/P59

Posted: Mon Aug 02, 2021 6:49 am
by bubba2533
Interesting. Didn’t expect that. Any idea why that is happening?

Re: C development and patching for P01/P59

Posted: Mon Aug 02, 2021 12:36 pm
by Gampy
The PCM is Big Endian, your PC is Little Endian.

The OS id is 4 bytes, use an unsigned long, int is to small.

Use a char buffer with fread().

Bit shift results into unsigned long.

Code: Select all

#include <stdio.h>

int main()
{
    FILE* fptr;

    unsigned long operatingsystem; // OsID is 4 bytes.
    char buffer[4];                // char buffer, makes handling BE simple.

    // Open bin file
    fopen_s(&fptr, "12587603.bin", "rb");
    if (fptr == NULL) {
        return 3;
    }

    // Seek to OS and read value
    fseek(fptr, 0x504, SEEK_SET);
    size_t elements_read = fread_s(&buffer, sizeof(buffer), sizeof(buffer), 1, fptr); //Changed to use buffer
    if (elements_read == 0) {
        return 4;
    }

    // Bit shift everything into unsigned long.
    operatingsystem = ((buffer[0] & 0xFF) << 24 | (buffer[1] & 0xFF) << 16 | (buffer[2] & 0xFF) << 8 | buffer[3] & 0xFF);

    // print raw values
    printf("Operating system of file: %x %x %x %x\n", buffer[0] & 0xFF, buffer[1] & 0xFF, buffer[2] & 0xFF, buffer[3] & 0xFF);
    // print value
    printf("Operating system of file: %u\n", operatingsystem);

    fclose(fptr);

    return 0;
}
TIP: If you use C++ style comments (//), then if you need to comment out a large block you can use C style comments (/**/) without interference of nested C style comments.

-Enjoy

Re: C development and patching for P01/P59

Posted: Mon Aug 02, 2021 1:29 pm
by bubba2533
Awesome and thanks for the tip!

Re: C development and patching for P01/P59

Posted: Mon Aug 02, 2021 8:37 pm
by Gampy
You working in an IDE or CLI ??