#include "psf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <pspchnnlsv.h>
int find_psf_section(const char *name,
unsigned char *data,
int dataLen,
unsigned char **location,
int *size)
{
unsigned short int nameLoc;
int i, magicHead, strLoc, headLen, numSects;
int sectCurLen, sectBufLen, sectBufLoc, curPos;
if (dataLen < 0x14)
return -1;
magicHead = *(unsigned int *)&data[0x00];
strLoc = *(unsigned int *)&data[0x08];
headLen = *(unsigned int *)&data[0x0C];
numSects = *(unsigned int *)&data[0x10];
if (magicHead != 0x46535000)
return -2;
if ((strLoc > headLen) || (strLoc >= dataLen))
return -3;
if (headLen >= dataLen)
return -4;
if (numSects != ((strLoc - 0x14) / 0x10))
return -5;
for (i = 0; i < numSects; i++)
{
curPos = 0x14 + (i * 0x10);
if (curPos >= strLoc)
return -6;
nameLoc = *(unsigned short *)&data[curPos];
sectCurLen = *(unsigned short *)&data[curPos + 0x04];
sectBufLen = *(unsigned short *)&data[curPos + 0x08];
sectBufLoc = *(unsigned short *)&data[curPos + 0x0C];
if ((nameLoc < dataLen) && (sectCurLen < dataLen)
&& (sectBufLen < dataLen) && (sectBufLoc < dataLen))
{
if (!stricmp((char *)&data[strLoc + nameLoc], name))
{
*location = &data[headLen + sectBufLoc];
*size = sectBufLen;
return 0;
}
}
}
return -7;
}
int find_psf_datafile(const char *name,
unsigned char *filelist,
int size,
unsigned char **location)
{
int i;
for (i = 0; (i + 0x0d) <= size; i += 0x20)
{
if (!strncasecmp((char *)&filelist[i], name, 0x0d)) {
*location = &filelist[i];
return 0;
}
}
return -1;
}