Trying to simply load a file in a buffer

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Trying to simply load a file in a buffer

Post by ne0h »

Hi,
I'm trying to load a file in a buffer, but I've some problems

Code: Select all

static u8 g_dataOut[3000000] __attribute__((aligned(0x40)));

static u8* g_dataOut2;

int Decrypt_file(const char* name, const char* dstname)
{
    Init(); // initialize the screen
    
    SceUID fd;
    
    if(!(fd = sceIoOpen(name, PSP_O_RDONLY, 0777)))
    {
        printf("Error opening file");
        sleep(1000000);
    }
    
    
    SceOff off = sceIoLseek(fd, 0, SEEK_END);
    
    sceIoLseek(fd, 0, SEEK_SET);
    
    int read = sceIoRead(fd, g_dataOut2, off);
    
    if(read != off)
    {
        printf("Error reading file");
        printf("%d of %d bytes readed\n", read, (int)off);
        sleep(1000000);
    }
the output of this is: Error reading file -2901839843 ( or something like it) of 30128 bytes readed
and the PSP crash when try to access to the buffer, but off variable contains the correct size, so the file is loaded succesfull!
Please help, the code is very simple but I got this error, why?
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

What does g_dataOut2 point at? Right now you're reading the file into memory you don't own.

Jim
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Doesn't sceIoRead read a buffer from a file and "copy" it in a buffer?
I think that sceIoRead read a buffer lenght ( variable off ) from the file fd and copy that in a buffer ( g_dataOut2 ), g_dataOut2 has to point somewhere?
Noko
Posts: 23
Joined: Sat Sep 06, 2008 8:35 pm

Post by Noko »

g_dataOut2 is not a buffer, it's a pointer.

change your second line to

Code: Select all

static u8* g_dataOut2=g_dataOut;
By the way, why are you aligning g_dataOut?
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

After you sceIoLseek to read the file size into off, you must allocate memory to g_dataOut2 like this:

Code: Select all

SceUID blockid = sceKernelAllocPartitionMemory(2, "block", 0, (sizeof(unsigned char) * off), NULL);
if &#40;blockid < 0&#41;
&#123;
	printf&#40;"Memory allocation error."&#41;;
&#125;
g_dataOut2 = &#40;unsigned char*&#41; sceKernelGetBlockHeadAddr&#40;blockid&#41;;
For consistency sake you should declare
static unsigned char* g_dataOut2;
But u8 is the same anyway.

The above is assuming that you want to store the data at g_dataOut2.

If you want to store the data in g_dataOut then all you have to do is change it like this:

Code: Select all

int read = sceIoRead&#40;fd, &g_dataOut, off&#41;; 
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Thanks Torch, I've don't think to allocate the memory ( I've thinked that the memory is automatically allocated by the function, anyway, can I allocate the memory with malloc?
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

Aligning read buffer to 64bytes can be beneficial, especially when reading/writing to usbhost (cause I know how I implemented that).
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

mmm, the PSP crash everytimes!
Here is the complete code:

Code: Select all

// static u8 g_dataOut&#91;3000000&#93; __attribute__&#40;&#40;aligned&#40;0x40&#41;&#41;&#41;;
static u8 g_dataOut&#91;3000000&#93;;

static u8* g_dataOut2;


int Decrypt_file&#40;const char* name, const char* dstname&#41;
&#123;
    Init&#40;&#41;;
    
    printf&#40;"Init"&#41;;
    
    SceUID fd;
    
    if&#40;!&#40;fd = sceIoOpen&#40;name, PSP_O_RDONLY, 0777&#41;&#41;&#41;
    &#123;
        printf&#40;"Error opening file"&#41;;
        sleep&#40;1000000&#41;;
    &#125;
    
    
    SceOff off = sceIoLseek&#40;fd, 0, SEEK_END&#41;;
    
    sceIoLseek&#40;fd, 0, SEEK_SET&#41;;
    
    g_dataOut2 = &#40;u8*&#41;malloc_64&#40;&#40;sizeof&#40;u8&#41;&#41;*off&#41;;
    
    int read = sceIoRead&#40;fd, &g_dataOut2, off&#41;;
    
    sceIoClose&#40;fd&#41;;
    
    if&#40;read != off&#41;
    &#123;
        printf&#40;"Error reading file"&#41;;
        printf&#40;"%d of %d bytes readed\n", read, &#40;int&#41;off&#41;;
        sleep&#40;1000000&#41;;
    &#125;
    
    int ret = pspDecryptCode_Start&#40;&#41;;
    if&#40;sceKernelDevkitVersion&#40;&#41; >= 0x03080010&#41;
    &#123;
            if&#40;ret == 2&#41;
                    printf&#40;"You HAVE NATIVE support for KL3E and 2LRZ now."&#41;;
            else if&#40;ret == 1&#41;
                    printf&#40;"You HAVE NATIVE support for KL3E, but not 2LRZ. \nYou CAN'T decompress <=3.73 firmwares"&#41;;
            else if&#40;!ret&#41;
                    printf&#40;"You DON'T have NATIVE support for KL3E and 2LRZ.\nYou CAN'T decompress <=3.73 firmwares and \nreboot.bin in >=3.80"&#41;;
            else
                    printf&#40;"Unknow support..."&#41;;
    &#125;
    else
    &#123;
        printf&#40;"\nThis utility need >= 3.80 firmware to run, please update your CF! ;&#41;"&#41;;
        sleep&#40;3000000&#41;;
        return 1;
    &#125;
    
    if &#40;&#40;memcmp&#40;&g_dataOut2, "~PSP", 4&#41; == 0&#41;&#41;
    &#123;
        printf&#40;"\n\nDecrypting"&#41;;
        
        int cbDecrypted = pspDecryptPRX&#40;g_dataOut2, g_dataOut, off&#41;; // Here the programm crash!
        
        printf&#40;"\n\nDecrypted"&#41;;

        if &#40;cbDecrypted > 0&#41;
        &#123;
                if &#40;&#40;g_dataOut&#91;0&#93; == 0x1F && g_dataOut&#91;1&#93; == 0x8B&#41; || memcmp&#40;g_dataOut, "2RLZ", 4&#41; == 0 || memcmp&#40;g_dataOut, "KL4E", 4&#41; == 0&#41;
                &#123;
                    printf&#40;"Expanding...OK!"&#41;;

                    int cbExp = pspDecompress&#40;g_dataOut, g_dataOut2, sizeof&#40;g_dataOut&#41;&#41;;

                    if &#40;cbExp > 0&#41;
                    &#123;
                            printf&#40;"Expanding again...OK!"&#41;;
                    &#125;
                    else
                    &#123;
                            printf&#40;"Decompress error\n"
                            "File will be written compressed.\n"&#41;;
                    &#125;
                &#125;
        &#125;
        else
        &#123;
                printf&#40;"Error in decryption.\n"&#41;;
                sleep&#40;2000000&#41;;
        &#125;
    &#125;
    
    printf&#40;"\nWriting file!\n"&#41;;
    
    SceUID newfile = sceIoOpen&#40;dstname, PSP_O_ALLWR, 0777&#41;;
    
    int bytes = sceIoWrite&#40;newfile, g_dataOut, sizeof&#40;&#40;u8&#41;*g_dataOut&#41;&#41;;
    
    sceIoClose&#40;newfile&#41;;
    
    printf&#40;"%d bytes writted", bytes&#41;;
    sleep&#40;2000000&#41;;
    
    return 0;
&#125;
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Code: Select all

static u8* g_dataOut2; 
...
g_dataOut2 = &#40;u8*&#41;malloc_64&#40;&#40;sizeof&#40;u8&#41;&#41;*off&#41;;
...
int read = sceIoRead&#40;fd, &g_dataOut2, off&#41;;
should be

Code: Select all

int read = sceIoRead&#40;fd, g_dataOut2, off&#41;;
Jim
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

???
What about the post of Torch?
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

What about it? Either you know how pointers work or you don't. Why are you passing the address of the pointer and not the pointer??? I'm fucking baffled.

Jim
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

I don't know
This is my code now and it crash in the memcmp function and I've error reading file -121243423235 of 30128 bytes readed, but why?
The file is opened correctly because Lseek return the correct size of the file!

Code: Select all

static u8 g_dataOut&#91;3000000&#93;;

static u8* g_dataOut2;


int Decrypt_file&#40;const char* name, const char* dstname&#41;
&#123;
    Init&#40;&#41;;
    
    printf&#40;"Init"&#41;;
    
    SceUID fd;
    
    if&#40;!&#40;fd = sceIoOpen&#40;name, PSP_O_RDONLY, 0777&#41;&#41;&#41;
    &#123;
        printf&#40;"Error opening file"&#41;;
        sleep&#40;1000000&#41;;
    &#125;
    
    
    SceOff off = sceIoLseek&#40;fd, 0, SEEK_END&#41;;
    
    sceIoLseek&#40;fd, 0, SEEK_SET&#41;;
    
    // g_dataOut2 = &#40;u8*&#41;malloc_64&#40;&#40;sizeof&#40;u8&#41;&#41;*off&#41;;
    
    int read = sceIoRead&#40;fd, g_dataOut2, off&#41;;
    
    sceIoClose&#40;fd&#41;;
    
    if&#40;read != off&#41;
    &#123;
        printf&#40;"Error reading file"&#41;;
        printf&#40;"%d of %d bytes readed\n", read, &#40;int&#41;off&#41;;
        sleep&#40;1000000&#41;;
    &#125;
    
    int ret = pspDecryptCode_Start&#40;&#41;;
    if&#40;sceKernelDevkitVersion&#40;&#41; >= 0x03080010&#41;
    &#123;
            if&#40;ret == 2&#41;
                    printf&#40;"You HAVE NATIVE support for KL3E and 2LRZ now."&#41;;
            else if&#40;ret == 1&#41;
                    printf&#40;"You HAVE NATIVE support for KL3E, but not 2LRZ. \nYou CAN'T decompress <=3.73 firmwares"&#41;;
            else if&#40;!ret&#41;
                    printf&#40;"You DON'T have NATIVE support for KL3E and 2LRZ.\nYou CAN'T decompress <=3.73 firmwares and \nreboot.bin in >=3.80"&#41;;
            else
                    printf&#40;"Unknow support..."&#41;;
    &#125;
    else
    &#123;
        printf&#40;"\nThis utility need >= 3.80 firmware to run, please update your CF! ;&#41;"&#41;;
        sleep&#40;3000000&#41;;
        return 1;
    &#125;
    
    if &#40;&#40;memcmp&#40;g_dataOut2, "~PSP", 4&#41; == 0&#41;&#41;
    &#123;
        printf&#40;"\n\nDecrypting"&#41;;
        
        int cbDecrypted = pspDecryptPRX&#40;g_dataOut2, g_dataOut, off&#41;;
        
        printf&#40;"\n\nDecrypted"&#41;;

        if &#40;cbDecrypted > 0&#41;
        &#123;
                if &#40;&#40;g_dataOut&#91;0&#93; == 0x1F && g_dataOut&#91;1&#93; == 0x8B&#41; || memcmp&#40;g_dataOut, "2RLZ", 4&#41; == 0 || memcmp&#40;g_dataOut, "KL4E", 4&#41; == 0&#41;
                &#123;
                    printf&#40;"Expanding...OK!"&#41;;

                    int cbExp = pspDecompress&#40;g_dataOut, g_dataOut2, sizeof&#40;g_dataOut&#41;&#41;;

                    if &#40;cbExp > 0&#41;
                    &#123;
                            printf&#40;"Expanding again...OK!"&#41;;
                    &#125;
                    else
                    &#123;
                            printf&#40;"Decompress error\n"
                            "File will be written compressed.\n"&#41;;
                    &#125;
                &#125;
        &#125;
        else
        &#123;
                printf&#40;"Error in decryption.\n"&#41;;
                sleep&#40;2000000&#41;;
        &#125;
    &#125;
    
    printf&#40;"\nWriting file!\n"&#41;;
    
    SceUID newfile = sceIoOpen&#40;dstname, PSP_O_ALLWR, 0777&#41;;
    
    int bytes = sceIoWrite&#40;newfile, g_dataOut, sizeof&#40;&#40;u8&#41;*g_dataOut&#41;&#41;;
    
    sceIoClose&#40;newfile&#41;;
    
    printf&#40;"%d bytes writted", bytes&#41;;
    sleep&#40;2000000&#41;;
    
    return 0;
&#125;
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Your g_dataOut2 pointer doesn't seem to be set above. You have the memory allocation commented out.
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

O.o
Sorry!
Ok, I've tryied to decrypt a 3.71 fw prx, I have Error in decryption but can be another problem...
Thanks to all!
Post Reply