CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Tools/SaveTool/psf.c
Views: 1401
1
/*
2
* PSP Software Development Kit - http://www.pspdev.org
3
* -----------------------------------------------------------------------
4
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
5
*
6
* psf.c - PSF parsing routines
7
*
8
* Copyright (c) 2005 Jim Paris <[email protected]>
9
* Coypright (c) 2005 psp123
10
*
11
* $Id: psf.c 1560 2005-12-10 01:16:32Z jim $
12
*/
13
14
#include "psf.h"
15
#include <stdio.h>
16
#include <stdlib.h>
17
#include <string.h>
18
#include <malloc.h>
19
#include <pspchnnlsv.h>
20
21
/* Find to the named section in the PSF file, and return an
22
absolute pointer to it and the section size. */
23
int find_psf_section(const char *name,
24
unsigned char *data,
25
int dataLen,
26
unsigned char **location,
27
int *size)
28
{
29
unsigned short int nameLoc;
30
int i, magicHead, strLoc, headLen, numSects;
31
int sectCurLen, sectBufLen, sectBufLoc, curPos;
32
33
if (dataLen < 0x14)
34
return -1;
35
36
/* Get the basics from the header */
37
magicHead = *(unsigned int *)&data[0x00];
38
strLoc = *(unsigned int *)&data[0x08];
39
headLen = *(unsigned int *)&data[0x0C];
40
numSects = *(unsigned int *)&data[0x10];
41
42
/* Do some error checking */
43
if (magicHead != 0x46535000)
44
return -2;
45
46
/* Verify strLoc is proper */
47
if ((strLoc > headLen) || (strLoc >= dataLen))
48
return -3;
49
50
/* Verify headLen is proper */
51
if (headLen >= dataLen)
52
return -4;
53
54
/* Verify numSects is proper */
55
if (numSects != ((strLoc - 0x14) / 0x10))
56
return -5;
57
58
/* Process all sections */
59
for (i = 0; i < numSects; i++)
60
{
61
/* Get the curPos */
62
curPos = 0x14 + (i * 0x10);
63
64
/* Verify curPos is proper */
65
if (curPos >= strLoc)
66
return -6;
67
68
/* Get some basic info about this section */
69
nameLoc = *(unsigned short *)&data[curPos];
70
sectCurLen = *(unsigned short *)&data[curPos + 0x04];
71
sectBufLen = *(unsigned short *)&data[curPos + 0x08];
72
sectBufLoc = *(unsigned short *)&data[curPos + 0x0C];
73
74
/* Do some error checking */
75
if ((nameLoc < dataLen) && (sectCurLen < dataLen)
76
&& (sectBufLen < dataLen) && (sectBufLoc < dataLen))
77
{
78
/* Check if this is the section we want */
79
if (!stricmp((char *)&data[strLoc + nameLoc], name))
80
{
81
/* Update the location and size */
82
*location = &data[headLen + sectBufLoc];
83
*size = sectBufLen;
84
return 0;
85
}
86
}
87
}
88
89
/* Section was not found if it makes it here */
90
return -7;
91
}
92
93
/* Find the named file inside the FILE_LIST, and return
94
an absolute pointer to it. */
95
int find_psf_datafile(const char *name,
96
unsigned char *filelist,
97
int size,
98
unsigned char **location)
99
{
100
int i;
101
102
/* Process all files */
103
for (i = 0; (i + 0x0d) <= size; i += 0x20)
104
{
105
/* Check if this is the filename we want */
106
if (!strncasecmp((char *)&filelist[i], name, 0x0d)) {
107
*location = &filelist[i];
108
return 0;
109
}
110
}
111
112
/* File was not found if it makes it here */
113
return -1;
114
}
115
116