forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Trying to simply load a file in a buffer

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sat Sep 06, 2008 9:26 pm    Post subject: Trying to simply load a file in a buffer Reply with quote

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

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?
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Sat Sep 06, 2008 9:32 pm    Post subject: Reply with quote

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

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sat Sep 06, 2008 9:49 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
Noko



Joined: 06 Sep 2008
Posts: 23

PostPosted: Sat Sep 06, 2008 10:20 pm    Post subject: Reply with quote

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

change your second line to
Code:
static u8* g_dataOut2=g_dataOut;


By the way, why are you aligning g_dataOut?
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sat Sep 06, 2008 10:32 pm    Post subject: Reply with quote

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

Code:
SceUID blockid = sceKernelAllocPartitionMemory(2, "block", 0, (sizeof(unsigned char) * off), NULL);
if (blockid < 0)
{
   printf("Memory allocation error.");
}
g_dataOut2 = (unsigned char*) sceKernelGetBlockHeadAddr(blockid);


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:
int read = sceIoRead(fd, &g_dataOut, off);
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sat Sep 06, 2008 11:42 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sat Sep 06, 2008 11:44 pm    Post subject: Reply with quote

Aligning read buffer to 64bytes can be beneficial, especially when reading/writing to usbhost (cause I know how I implemented that).
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Sep 07, 2008 5:13 am    Post subject: Reply with quote

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

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

static u8* g_dataOut2;


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

        if (cbDecrypted > 0)
        {
                if ((g_dataOut[0] == 0x1F && g_dataOut[1] == 0x8B) || memcmp(g_dataOut, "2RLZ", 4) == 0 || memcmp(g_dataOut, "KL4E", 4) == 0)
                {
                    printf("Expanding...OK!");

                    int cbExp = pspDecompress(g_dataOut, g_dataOut2, sizeof(g_dataOut));

                    if (cbExp > 0)
                    {
                            printf("Expanding again...OK!");
                    }
                    else
                    {
                            printf("Decompress error\n"
                            "File will be written compressed.\n");
                    }
                }
        }
        else
        {
                printf("Error in decryption.\n");
                sleep(2000000);
        }
    }
   
    printf("\nWriting file!\n");
   
    SceUID newfile = sceIoOpen(dstname, PSP_O_ALLWR, 0777);
   
    int bytes = sceIoWrite(newfile, g_dataOut, sizeof((u8)*g_dataOut));
   
    sceIoClose(newfile);
   
    printf("%d bytes writted", bytes);
    sleep(2000000);
   
    return 0;
}
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Sun Sep 07, 2008 10:29 am    Post subject: Reply with quote

Code:

static u8* g_dataOut2;
...
g_dataOut2 = (u8*)malloc_64((sizeof(u8))*off);
...
int read = sceIoRead(fd, &g_dataOut2, off);


should be
Code:

int read = sceIoRead(fd, g_dataOut2, off);

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Sep 07, 2008 8:35 pm    Post subject: Reply with quote

???
What about the post of Torch?
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Sun Sep 07, 2008 9:21 pm    Post subject: Reply with quote

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
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Sep 07, 2008 10:03 pm    Post subject: Reply with quote

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:

static u8 g_dataOut[3000000];

static u8* g_dataOut2;


int Decrypt_file(const char* name, const char* dstname)
{
    Init();
   
    printf("Init");
   
    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);
   
    // g_dataOut2 = (u8*)malloc_64((sizeof(u8))*off);
   
    int read = sceIoRead(fd, g_dataOut2, off);
   
    sceIoClose(fd);
   
    if(read != off)
    {
        printf("Error reading file");
        printf("%d of %d bytes readed\n", read, (int)off);
        sleep(1000000);
    }
   
    int ret = pspDecryptCode_Start();
    if(sceKernelDevkitVersion() >= 0x03080010)
    {
            if(ret == 2)
                    printf("You HAVE NATIVE support for KL3E and 2LRZ now.");
            else if(ret == 1)
                    printf("You HAVE NATIVE support for KL3E, but not 2LRZ. \nYou CAN'T decompress <=3.73 firmwares");
            else if(!ret)
                    printf("You DON'T have NATIVE support for KL3E and 2LRZ.\nYou CAN'T decompress <=3.73 firmwares and \nreboot.bin in >=3.80");
            else
                    printf("Unknow support...");
    }
    else
    {
        printf("\nThis utility need >= 3.80 firmware to run, please update your CF! ;)");
        sleep(3000000);
        return 1;
    }
   
    if ((memcmp(g_dataOut2, "~PSP", 4) == 0))
    {
        printf("\n\nDecrypting");
       
        int cbDecrypted = pspDecryptPRX(g_dataOut2, g_dataOut, off);
       
        printf("\n\nDecrypted");

        if (cbDecrypted > 0)
        {
                if ((g_dataOut[0] == 0x1F && g_dataOut[1] == 0x8B) || memcmp(g_dataOut, "2RLZ", 4) == 0 || memcmp(g_dataOut, "KL4E", 4) == 0)
                {
                    printf("Expanding...OK!");

                    int cbExp = pspDecompress(g_dataOut, g_dataOut2, sizeof(g_dataOut));

                    if (cbExp > 0)
                    {
                            printf("Expanding again...OK!");
                    }
                    else
                    {
                            printf("Decompress error\n"
                            "File will be written compressed.\n");
                    }
                }
        }
        else
        {
                printf("Error in decryption.\n");
                sleep(2000000);
        }
    }
   
    printf("\nWriting file!\n");
   
    SceUID newfile = sceIoOpen(dstname, PSP_O_ALLWR, 0777);
   
    int bytes = sceIoWrite(newfile, g_dataOut, sizeof((u8)*g_dataOut));
   
    sceIoClose(newfile);
   
    printf("%d bytes writted", bytes);
    sleep(2000000);
   
    return 0;
}
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sun Sep 07, 2008 10:39 pm    Post subject: Reply with quote

Your g_dataOut2 pointer doesn't seem to be set above. You have the memory allocation commented out.
Back to top
View user's profile Send private message AIM Address
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Sep 07, 2008 11:20 pm    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group