C development and patching for P01/P59

User avatar
Gampy
Posts: 2330
Joined: Sat Dec 15, 2018 7:38 am

Re: C development and patching for P01/P59

Post by Gampy »

Have a look see here: Difference between C and C++
Intelligence is in the details!

It is easier not to learn bad habits, then it is to break them!

If I was here to win a popularity contest, their would be no point, so I wouldn't be here!
User avatar
NSFW
Posts: 679
Joined: Fri Feb 02, 2018 3:13 pm

Re: C development and patching for P01/P59

Post 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.
Please don't PM me with technical questions - start a thread instead, and send me a link to it. That way I can answer in public, and help other people who have the same question. Thanks!
User avatar
antus
Site Admin
Posts: 8228
Joined: Sat Feb 28, 2009 8:34 pm
cars: TX Gemini 2L Twincam
TX Gemini SR20 18psi
Datsun 1200 Ute
Subaru Blitzen '06 EZ30 4th gen, 3.0R Spec B
Contact:

Re: C development and patching for P01/P59

Post 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
Have you read the FAQ? For lots of information and links to significant threads see here: http://pcmhacking.net/forums/viewtopic.php?f=7&t=1396
bubba2533
Posts: 498
Joined: Wed Apr 11, 2018 8:50 am
cars: 03 Chevy S10 Turbo V6

Re: C development and patching for P01/P59

Post by bubba2533 »

Ok, guess I'll be buying another book lol

Any recommendations?
LS1 Boost OS V3 Here. For feature suggestions post in here Development Thread. Support future development ->Patreon.
bubba2533
Posts: 498
Joined: Wed Apr 11, 2018 8:50 am
cars: 03 Chevy S10 Turbo V6

Re: C development and patching for P01/P59

Post 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.
LS1 Boost OS V3 Here. For feature suggestions post in here Development Thread. Support future development ->Patreon.
kur4o
Posts: 945
Joined: Sun Apr 10, 2016 9:20 pm

Re: C development and patching for P01/P59

Post by kur4o »

The result you get is byte swapped for some reason. Convert the result in hex and you will see it for yourself.
bubba2533
Posts: 498
Joined: Wed Apr 11, 2018 8:50 am
cars: 03 Chevy S10 Turbo V6

Re: C development and patching for P01/P59

Post by bubba2533 »

Interesting. Didn’t expect that. Any idea why that is happening?
LS1 Boost OS V3 Here. For feature suggestions post in here Development Thread. Support future development ->Patreon.
User avatar
Gampy
Posts: 2330
Joined: Sat Dec 15, 2018 7:38 am

Re: C development and patching for P01/P59

Post 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
Intelligence is in the details!

It is easier not to learn bad habits, then it is to break them!

If I was here to win a popularity contest, their would be no point, so I wouldn't be here!
bubba2533
Posts: 498
Joined: Wed Apr 11, 2018 8:50 am
cars: 03 Chevy S10 Turbo V6

Re: C development and patching for P01/P59

Post by bubba2533 »

Awesome and thanks for the tip!
LS1 Boost OS V3 Here. For feature suggestions post in here Development Thread. Support future development ->Patreon.
User avatar
Gampy
Posts: 2330
Joined: Sat Dec 15, 2018 7:38 am

Re: C development and patching for P01/P59

Post by Gampy »

You working in an IDE or CLI ??
Intelligence is in the details!

It is easier not to learn bad habits, then it is to break them!

If I was here to win a popularity contest, their would be no point, so I wouldn't be here!
Post Reply