CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Tools/SaveTool/psf.c
Views: 1401
/*1* PSP Software Development Kit - http://www.pspdev.org2* -----------------------------------------------------------------------3* Licensed under the BSD license, see LICENSE in PSPSDK root for details.4*5* psf.c - PSF parsing routines6*7* Copyright (c) 2005 Jim Paris <[email protected]>8* Coypright (c) 2005 psp1239*10* $Id: psf.c 1560 2005-12-10 01:16:32Z jim $11*/1213#include "psf.h"14#include <stdio.h>15#include <stdlib.h>16#include <string.h>17#include <malloc.h>18#include <pspchnnlsv.h>1920/* Find to the named section in the PSF file, and return an21absolute pointer to it and the section size. */22int find_psf_section(const char *name,23unsigned char *data,24int dataLen,25unsigned char **location,26int *size)27{28unsigned short int nameLoc;29int i, magicHead, strLoc, headLen, numSects;30int sectCurLen, sectBufLen, sectBufLoc, curPos;3132if (dataLen < 0x14)33return -1;3435/* Get the basics from the header */36magicHead = *(unsigned int *)&data[0x00];37strLoc = *(unsigned int *)&data[0x08];38headLen = *(unsigned int *)&data[0x0C];39numSects = *(unsigned int *)&data[0x10];4041/* Do some error checking */42if (magicHead != 0x46535000)43return -2;4445/* Verify strLoc is proper */46if ((strLoc > headLen) || (strLoc >= dataLen))47return -3;4849/* Verify headLen is proper */50if (headLen >= dataLen)51return -4;5253/* Verify numSects is proper */54if (numSects != ((strLoc - 0x14) / 0x10))55return -5;5657/* Process all sections */58for (i = 0; i < numSects; i++)59{60/* Get the curPos */61curPos = 0x14 + (i * 0x10);6263/* Verify curPos is proper */64if (curPos >= strLoc)65return -6;6667/* Get some basic info about this section */68nameLoc = *(unsigned short *)&data[curPos];69sectCurLen = *(unsigned short *)&data[curPos + 0x04];70sectBufLen = *(unsigned short *)&data[curPos + 0x08];71sectBufLoc = *(unsigned short *)&data[curPos + 0x0C];7273/* Do some error checking */74if ((nameLoc < dataLen) && (sectCurLen < dataLen)75&& (sectBufLen < dataLen) && (sectBufLoc < dataLen))76{77/* Check if this is the section we want */78if (!stricmp((char *)&data[strLoc + nameLoc], name))79{80/* Update the location and size */81*location = &data[headLen + sectBufLoc];82*size = sectBufLen;83return 0;84}85}86}8788/* Section was not found if it makes it here */89return -7;90}9192/* Find the named file inside the FILE_LIST, and return93an absolute pointer to it. */94int find_psf_datafile(const char *name,95unsigned char *filelist,96int size,97unsigned char **location)98{99int i;100101/* Process all files */102for (i = 0; (i + 0x0d) <= size; i += 0x20)103{104/* Check if this is the filename we want */105if (!strncasecmp((char *)&filelist[i], name, 0x0d)) {106*location = &filelist[i];107return 0;108}109}110111/* File was not found if it makes it here */112return -1;113}114115116