I don't even know if I'm allocating the memory correctly.
Run the program and look at the output files, you'll understand what I mean.
If the entire loop runs, only the last file is extracted correctly.
I tried printing the addresses that each image[].data points to, and the difference between subsequent ones (image[0].data and image[1].data) is on average around 4000 bytes.
When allocating image[0].data it should have allocated 44817 bytes, not 4000!!! Because image[0].hdr.size is 44817, and this value is passed to sceKernelAllocPartitionMemory in order to allocate image[0].data.
The structure of the lockdown.thm file is as follows.
4 byte int
4 byte int
4 byte int
4 byte int
4 byte unsigned int (this is hdr.size, the length of the following data until the next header)
....data....
....data....
Its repeated NUMFILES times.
Over here I'm writing the extracted files to the memory stick just to test if the extraction is working. I'm going to render the images directly from the images[].data
I'm clueless about dynamic memory allocation. Someone please help me T_T
Code: Select all
#include <pspkernel.h>
#include <pspdebug.h>
#include <stdio.h>
#define printf pspDebugScreenPrintf
PSP_MODULE_INFO("Bytetest", 0, 1, 0);
#define NUMFILES 14
typedef struct
{
int x,y,h,w;
unsigned int size;
} imagehdr;
typedef struct
{
imagehdr hdr;
SceUID blockid;
unsigned char* data;
} imagefile;
imagefile images[NUMFILES];
int exit_callback(int arg1, int arg2, void *common) {
sceKernelExitGame();
return 0;
}
int CallbackThread(SceSize args, void *argp) {
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
int SetupCallbacks(void) {
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0) {
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
int main(int argc, char **argv)
{
pspDebugScreenInit();
SetupCallbacks();
printf("Opening flash0:/lockdown.thm.\n");
int i, bytesRead;
SceUID fp, fo;
fp = sceIoOpen("flash0:/lockdown.thm", PSP_O_RDONLY, 0777);
if (fp < 0)
{
printf("Error loading flash0:/lockdown.thm.\n");
return 0;
}
else
{
for (i = 0; i < NUMFILES; i++)
{
bytesRead = sceIoRead(fp, &images[i].hdr, sizeof(imagehdr));
if (bytesRead != sizeof(imagehdr))
{
printf("Unexpected end of header %d. Read %d bytes. Header size %d bytes.\n", i, bytesRead, sizeof(imagehdr));
return 0;
}
images[i].blockid = sceKernelAllocPartitionMemory(2, "block", 0, (sizeof(unsigned char) * images[i].hdr.size), NULL);
if (images[i].blockid < 0)
{
printf("Memory allocation error 0x%x.\n", images[i].blockid);
return 0;
}
images[i].data = (unsigned char*) sceKernelGetBlockHeadAddr(images[i].blockid);
bytesRead = sceIoRead(fp, &images[i].data, images[i].hdr.size);
if (bytesRead != images[i].hdr.size)
{
printf("Unexpected end of data in image %d. Read %d bytes. Data size %u bytes.\n", i, bytesRead, images[i].hdr.size);
return 0;
}
}
}
sceIoClose(fp);
for (i = 0; i < NUMFILES; i++)
{
char temp[30];
sprintf(temp, "ms0:/%d.png", i);
printf("Writing %s\n", temp);
fo = sceIoOpen(temp, PSP_O_WRONLY|PSP_O_TRUNC|PSP_O_CREAT, 0777);
sceIoWrite(fo, &images[i].data, images[i].hdr.size);
sceIoClose(fo);
sceKernelFreePartitionMemory(images[i].blockid);
}
sceKernelSleepThread();
return 0;
}
Code: Select all
TARGET = bytetest
OBJS = main.o
INCDIR =
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
#BUILD_PRX = 1
#PRX_EXPORTS = exports.exp
#USE_KERNEL_LIBC=1
#USE_KERNEL_LIBS=1
PSP_FW_VERSION = 390
LIBDIR =
LIBS =
LDFLAGS = -mno-crt0
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Byte Test
#PSP_EBOOT_ICON="icon0.png"
#PSP_EBOOT_PIC1="pic1.png"
#PSP_EBOOT_SND0="snd0.at3"
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak