-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathPARAM_SFO.cs
566 lines (501 loc) · 19.8 KB
/
PARAM_SFO.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* Copyright (c) 2015 - 2018 TheDarkporgramer
*
* This was originally done by Jappi88 (Jappi88 at Gmail dot com) https://github.com/Jappi88
* All modifications have been TheDarkporgramer (save sfo ext ext ) https://github.com/xXxTheDarkprogramerxXx
*
* This(software Is provided) 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications*, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledge in the product documentation is required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* *Contact must be made to discuses permission and terms.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using PARAM.SFO;
using System.Runtime.InteropServices;
using System.Linq;
namespace Param_SFO
{
public class PARAM_SFO
{
#region << Enums >>
public enum DataTypes : uint
{
PSN_Game =18248,
GameData = 0x4744,
SaveData = 0x5344,
AppPhoto = 0x4150,
AppMusic = 0x414D,
AppVideo = 0x4156,
BroadCastVideo = 0x4256,
AppleTV = 4154,
WebTV = 5754,
CellBE = 0x4342,
Home = 0x484D,
StoreFronted = 0x5346,
HDDGame = 0x4847,
DiscGame = 0x4447,
AutoInstallRoot = 0x4152,
DiscPackage = 0x4450,
ExtraRoot = 0x5852,
VideoRoot = 0x5652,
ThemeRoot = 0x5452,
DiscMovie = 0x444D,
Game_Digital_Application = 0x4081AC0,//GD
PS4_Game_Application_Patch = 28775,
Additional_Content = 25441,//PSvita PS4
GameContent=25447,//PSVITA
Blu_Ray_Disc = 25698,//PS4
None
}
public enum FMT : ushort
{
UTF_8 = 0x0004,
ASCII = 0x0402,
Utf8Null = 0x0204,
UINT32 = 0x0404,
}
#endregion << Enums >>
#region << Vars>>
public List<Table> Tables { get; set; }
#endregion << Vars>>
#region << Example Of Calling Functions >>
//ypu can use this as SFO.Atribute
public string Attribute
{
get
{
if (Tables == null)
return "";
foreach(Table t in Tables)
{
if (t.Name == "ATTRIBUTE")
return t.Value;
}
return "";
}
}
public DataTypes DataType
{
get
{
if (Tables == null)
return DataTypes.None;
foreach (Table t in Tables)
if (t.Name == "CATEGORY")
return ((DataTypes)BitConverter.ToUInt16(Encoding.UTF8.GetBytes(t.Value), 0));
return DataTypes.None;
}
}
public string APP_VER
{
get
{
if (Tables == null)
return "";
foreach (Table t in Tables)
{
if (t.Name == "APP_VER")
return t.Value;
}
return "";
}
}
public string Detail
{
get
{
if (Tables == null)
return "";
foreach (Table t in Tables)
if (t.Name == "DETAIL")
return t.Value;
return "";
}
}
public string ContentID
{
get
{
if (Tables == null)
return "";
foreach (Table t in Tables)
if (t.Name == "CONTENT_ID")
return t.Value;
return "";
}
}
public string TITLEID
{
get
{
if (Tables == null)
return "";
foreach (Table t in Tables)
if (t.Name == "TITLE_ID")
return t.Value;
return "";
}
}
public string TitleID
{
get
{
string name = TITLEID;
if (name == "")
return "";
return name.Split('-')[0];
}
}
public string Title
{
get
{
if (Tables == null)
return "";
foreach (Table t in Tables)
if (t.Name == "TITLE")
return t.Value;
return "";
}
}
public string Category
{
get
{
if (Tables == null)
return "";
foreach (Table t in Tables)
if (t.Name == "CATEGORY")
return t.Value;
return "";
}
}
public enum Playstation
{
ps3 = 0,
psvita = 1,
ps4 = 2,
psp = 3,
unknown = 4,//there will be a time i no longer support the scene this will be for ps5+ most probabbly
}
public Playstation PlaystationVersion
{
get
{
if (Tables == null)
return Playstation.unknown;
foreach (Table t in Tables)
{
if (t.Name == "PS3_SYSTEM_VER")
return Playstation.ps3;//this is the unique offset for ps3
if (t.Name == "PSP2_SYSTEM_VER")
{
return Playstation.psvita;//this is the only flag that tells us its a psvita
}
if (t.Name == "PSP_SYSTEM_VER")
{
return Playstation.psp;//this is how we know its a psp
}
if (t.Name == "SYSTEM_VER")//i believe this to be only ps4
{
return Playstation.ps4;
}
}
return Playstation.unknown;
}
}
#endregion << Example Of Calling Functions >>
#region Param.SFO Struct
public struct Header
{
public static byte[] Magic = {0, 0x50, 0x53, 0x46};
public static byte[] version = {01, 01, 0, 0};
public static uint KeyTableStart = 0;
public static uint DataTableStart = 0;
public static uint IndexTableEntries = 0;
private static byte[] Buffer
{
get
{
var header = new byte[20];
Array.Copy(Magic, 0, header, 0, 4);
Array.Copy(version, 0, header, 4, 4);
Array.Copy(BitConverter.GetBytes(KeyTableStart), 0, header, 8, 4);
Array.Copy(BitConverter.GetBytes(DataTableStart), 0, header, 12, 4);
Array.Copy(BitConverter.GetBytes(IndexTableEntries), 0, header, 16, 4);
return header;
}
}
public static void Read(BinaryReader input)
{
input.BaseStream.Seek(0, SeekOrigin.Begin);
input.Read(Magic, 0, 4);
input.Read(version, 0, 4);
KeyTableStart = input.ReadUInt32();
DataTableStart = input.ReadUInt32();
IndexTableEntries = input.ReadUInt32();
}
}
[Serializable]
public struct Table : IComparable
{
public index_table Indextable;
public string Name;
public string Value;
public int index;
public byte[] NameBuffer
{
get
{
var buffer = new byte[Name.Length + 1];
Array.Copy(Encoding.UTF8.GetBytes(Name), 0, buffer, 0, Encoding.UTF8.GetBytes(Name).Length);
return buffer;
}
}
public byte[] ValueBuffer
{
get
{
byte[] buffer;
switch (Indextable.param_data_fmt)
{
case FMT.ASCII:
buffer = new byte[Indextable.param_data_max_len];
Array.Copy(Encoding.ASCII.GetBytes(Value), 0, buffer, 0, Encoding.UTF8.GetBytes(Value).Length);
return buffer;
case FMT.UINT32:
return BitConverter.GetBytes(uint.Parse(Value));
case FMT.UTF_8:
buffer = new byte[Indextable.param_data_max_len];
Array.Copy(Encoding.UTF8.GetBytes(Value), 0, buffer, 0, Encoding.UTF8.GetBytes(Value).Length);
return buffer;
case FMT.Utf8Null:
buffer = new byte[Indextable.param_data_max_len];
Array.Copy(Encoding.UTF8.GetBytes(Value), 0, buffer, 0, Encoding.UTF8.GetBytes(Value).Length);/*write the length of the array*/
return buffer;
default:
return null;
}
}
}
public int CompareTo(object obj)
{
throw new NotImplementedException();
}
}
[Serializable]
public struct index_table
{
public FMT param_data_fmt; /* param_data data type */
public uint param_data_len; /* param_data used bytes */
public uint param_data_max_len; /* param_data total reserved bytes */
public uint param_data_offset; /* param_data offset (relative to start offset of data_table) */
public ushort param_key_offset; /* param_key offset (relative to start offset of key_table) */
private byte[] Buffer
{
get
{
var data = new byte[16];
Array.Copy(BitConverter.GetBytes(param_key_offset), 0, data, 0, 2);
Array.Copy(BitConverter.GetBytes(((ushort) param_data_fmt)), 0, data, 2, 2);
Array.Copy(BitConverter.GetBytes(param_data_len), 0, data, 4, 4);
Array.Copy(BitConverter.GetBytes(param_data_max_len), 0, data, 8, 4);
Array.Copy(BitConverter.GetBytes(param_data_offset), 0, data, 12, 4);
return data;
}
}
public void Read(BinaryReader input)
{
param_key_offset = input.ReadUInt16();
param_data_fmt = (FMT) input.ReadUInt16();
param_data_len = input.ReadUInt32();
param_data_max_len = input.ReadUInt32();
param_data_offset = input.ReadUInt32();
}
}
[Serializable]
private enum DATA_TYPE : byte
{
BinaryData = 0,
Utf8Text = 2,
Si32Integer = 4
}
#endregion Param.SFO Struct
#region << Methods >>
public PARAM_SFO()
{
}
public PARAM_SFO(string filepath)
{
Init(new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read));
}
public PARAM_SFO(byte[] inputdata)
{
Init(new MemoryStream(inputdata));
}
public PARAM_SFO(Stream input)
{
Init(input);
}
/// <summary>
/// This is the SaveSFO Function for PS3/PS4/PSVita/And PSP no longer needed for Sony's CMD
/// </summary>
/// <param name="psfo">SFO That has been opened</param>
/// <param name="filename">Save Location</param>
public void SaveSFO(PARAM_SFO psfo, string filename)
{
//we start by opening the stream to the file
using (var stream = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.Read))
{
if (!stream.CanSeek)
throw new ArgumentException("Stream must be seekable");//throw this error we cant seek the stream
var utf8 = new UTF8Encoding(false);//encoding
using (var writer = new BinaryWriter(stream, utf8))//start binary reader
{
#region << Header Info (DevWiki) >>
/*
Header
* 0x00 0x04 magic PSF
0x04 0x04 version 01 01 00 00 1.01
0x08 0x04 key_table_start 24 00 00 00 Absolute start offset of key_table = 0x24
0x0C 0x04 data_table_start 30 00 00 00 Absolute start offset of data_table = 0x30
0x10 0x04 tables_entries 01 00 00 00 Number of entries in index_table, key_table, and data_table = 1
*/
#endregion <<Header Info >>
//so lets start writing the info
writer.Write(Header.Magic);//write magic "\0PSF"
writer.Write(Header.version);//write version info this is mayjor and minor (01 01 00 00 1.01)
Header.KeyTableStart = 0x14 + Header.IndexTableEntries * 0x10;/*we can write all this lovely info from the tables back*/
writer.Write(Header.KeyTableStart);
Header.DataTableStart = Convert.ToUInt32(Header.KeyTableStart + Tables.Sum(i => i.Name.Length + 1));//needs to be Uint
if (Header.DataTableStart % 4 != 0)
Header.DataTableStart = (Header.DataTableStart / 4 + 1) * 4;
writer.Write(Header.DataTableStart);
Header.IndexTableEntries = Convert.ToUInt32(Tables.Count);
writer.Write(Header.IndexTableEntries);
int lastKeyOffset = Convert.ToInt32(Header.KeyTableStart);
int lastValueOffset = Convert.ToInt32(Header.DataTableStart);
for (var i = 0; i < Tables.Count; i++)
{
var entry = Tables[i];
writer.BaseStream.Seek(0x14 + i * 0x10, SeekOrigin.Begin);
writer.Write((ushort)(lastKeyOffset - Header.KeyTableStart));
writer.Write((ushort)entry.Indextable.param_data_fmt);
writer.Write(entry.Indextable.param_data_len);
writer.Write(entry.Indextable.param_data_max_len);
writer.Write(lastValueOffset - Header.DataTableStart);
writer.BaseStream.Seek(lastKeyOffset, SeekOrigin.Begin);
writer.Write(utf8.GetBytes(entry.Name));
writer.Write((byte)0);
lastKeyOffset = (int)writer.BaseStream.Position;
writer.BaseStream.Seek(lastValueOffset, SeekOrigin.Begin);
writer.Write(entry.ValueBuffer);
lastValueOffset = (int)writer.BaseStream.Position;
}
//I'm doing this to just rewrite the first item (Some Cleanup will be needed)
//Or maybe not as when I checked this gives a 1 - 1 match with how the Sony tool works
//we need to rewrite that first item (PS4/PS3/PSV should be APP-VER)
lastKeyOffset = Convert.ToInt32(Header.KeyTableStart);
lastValueOffset = Convert.ToInt32(Header.DataTableStart);
var tableentry = Tables[0];
writer.BaseStream.Seek(lastKeyOffset, SeekOrigin.Begin);
writer.Write(utf8.GetBytes(tableentry.Name));
writer.Write((byte)0);
lastKeyOffset = (int)writer.BaseStream.Position;
}
}
}
private string ReadValue(BinaryReader br, index_table table)
{
br.BaseStream.Position = ((Header.DataTableStart) + table.param_data_offset);
switch (table.param_data_fmt)
{
case FMT.ASCII:
//return Encoding.GetEncoding(1252).GetString(br.ReadBytes((int) table.param_data_max_len)).Replace("\0", "");
return Encoding.UTF8.GetString(br.ReadBytes((int)table.param_data_max_len)).Replace("\0", "");
case FMT.UINT32:
return br.ReadUInt32().ToString();
case FMT.UTF_8:
return Encoding.UTF8.GetString(br.ReadBytes((int)table.param_data_max_len)).Replace("\0", "");
case FMT.Utf8Null:
return Encoding.UTF8.GetString(br.ReadBytes((int)table.param_data_max_len)).Replace("\0", "");
default:
return null;
}
}
private string ReadValueSpecialChars(BinaryReader br, index_table table)
{
br.BaseStream.Position = ((Header.DataTableStart) + table.param_data_offset);
switch (table.param_data_fmt)
{
case FMT.ASCII:
return Encoding.UTF8.GetString(br.ReadBytes((int)table.param_data_max_len)).Replace("\0", "");
case FMT.UINT32:
return br.ReadUInt32().ToString();
case FMT.UTF_8:
return Encoding.UTF8.GetString(br.ReadBytes((int)table.param_data_max_len)).Replace("\0", "");
default:
return null;
}
}
private string ReadName(BinaryReader br, index_table table)
{
br.BaseStream.Position = (Header.KeyTableStart + table.param_key_offset);
string name = "";
while (((byte)br.PeekChar()) != 0)
name += br.ReadChar();
br.BaseStream.Position++;
return name;
}
/// <summary>
/// Start Reading the Parameter file
/// </summary>
/// <param name="input">Input Stream</param>
private void Init(Stream input)
{
using (var br = new BinaryReader(input))
{
Header.Read(br);
if (!Functions.CompareBytes(Header.Magic, new byte[] { 0, 0x50, 0x53, 0x46 }))
throw new Exception("Invalid PARAM.SFO Header Magic");
var tables = new List<index_table>();
for (int i = 0; i < Header.IndexTableEntries; i++)
{
var t = new index_table();
t.Read(br);
tables.Add(t);
}
var xtables = new List<Table>();
int count = 0;
foreach (index_table t in tables)
{
var x = new Table();
x.index = count;
x.Indextable = t;
x.Name = ReadName(br, t);
x.Value = ReadValue(br, t);
count++;
xtables.Add(x);
}
Tables = xtables;
br.Close();
}
}
#endregion << Methods >>
}
}