Discuss the development of new homebrew software, tools and libraries.
Moderators: cheriff , TyRaNiD
ne0h
Posts: 386 Joined: Thu Feb 21, 2008 2:15 am
Post
by ne0h » Sat Sep 06, 2008 9:26 pm
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?
Jim
Posts: 476 Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:
Post
by Jim » Sat Sep 06, 2008 9:32 pm
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 » Sat Sep 06, 2008 9:49 pm
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 » Sat Sep 06, 2008 10:20 pm
g_dataOut2 is not a buffer, it's a pointer.
change your second line to
By the way, why are you aligning g_dataOut?
Torch
Posts: 825 Joined: Wed May 28, 2008 2:50 am
Post
by Torch » Sat Sep 06, 2008 10:32 pm
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 (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: Select all
int read = sceIoRead(fd, &g_dataOut, off);
ne0h
Posts: 386 Joined: Thu Feb 21, 2008 2:15 am
Post
by ne0h » Sat Sep 06, 2008 11:42 pm
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 » Sat Sep 06, 2008 11:44 pm
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 » Sun Sep 07, 2008 5:13 am
mmm, the PSP crash everytimes!
Here is the complete code:
Code: Select all
// 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;
}
Jim
Posts: 476 Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:
Post
by Jim » Sun Sep 07, 2008 10:29 am
Code: Select all
static u8* g_dataOut2;
...
g_dataOut2 = (u8*)malloc_64((sizeof(u8))*off);
...
int read = sceIoRead(fd, &g_dataOut2, off);
should be
Code: Select all
int read = sceIoRead(fd, g_dataOut2, off);
Jim
ne0h
Posts: 386 Joined: Thu Feb 21, 2008 2:15 am
Post
by ne0h » Sun Sep 07, 2008 8:35 pm
???
What about the post of Torch?
Jim
Posts: 476 Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:
Post
by Jim » Sun Sep 07, 2008 9:21 pm
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 » Sun Sep 07, 2008 10:03 pm
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[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;
}
J.F.
Posts: 2906 Joined: Sun Feb 22, 2004 11:41 am
Post
by J.F. » Sun Sep 07, 2008 10:39 pm
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 » Sun Sep 07, 2008 11:20 pm
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!