LibRay/libray/sfo.py

124 lines
3.6 KiB
Python
Raw Normal View History

# -*- coding: utf8 -*-
# libray - Libre Blu-Ray PS3 ISO Tool
2026-05-18 16:31:58 +02:00
# Copyright © 2018 - 2024 Nichlas Severinsen
#
# This file is part of libray.
#
# libray is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# libray is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libray. If not, see <https://www.gnu.org/licenses/>.
try:
2026-05-18 16:31:58 +02:00
from libray import core
except ImportError:
2026-05-18 16:31:58 +02:00
import core
class SFO:
2026-05-18 16:31:58 +02:00
"""Class for handling .sfo files
2026-05-18 16:31:58 +02:00
Attributes:
magic: Magic header
version: .SFO version
key_table_start: Absolute offset for key_table in .SFO
data_table_start: Absolute offset for index_table in .SFO
tables_entries: Number of entries in index_table and key_table
key_data: Parsed keys and data tables from .SFO transformed into dict
"""
2026-05-18 16:31:58 +02:00
def __init__(self, fp):
2026-05-18 16:31:58 +02:00
self.file_start = fp.tell()
2026-05-18 16:31:58 +02:00
# Header
2026-05-18 16:31:58 +02:00
self.magic = fp.read(4)
self.version = fp.read(4)
self.key_table_start = core.to_int(fp.read(4), 'little')
self.data_table_start = core.to_int(fp.read(4), 'little')
self.tables_entries = core.to_int(fp.read(4), 'little')
2026-05-18 16:31:58 +02:00
# Index table
2026-05-18 16:31:58 +02:00
index_table = []
2026-05-18 16:31:58 +02:00
for _ in range(0, self.tables_entries):
index_table.append({
'key_offset': core.to_int(fp.read(2), 'little'),
'data_fmt': fp.read(2),
'data_len': core.to_int(fp.read(4), 'little'),
'data_max_len': core.to_int(fp.read(4), 'little'),
'data_offset': core.to_int(fp.read(4), 'little'),
})
2026-05-18 16:31:58 +02:00
# Key table
2026-05-18 16:31:58 +02:00
key_table = []
2026-05-18 16:31:58 +02:00
for i in range(0, self.tables_entries):
2026-05-18 16:31:58 +02:00
# Seek to absolute offset + relative offset of key
2026-05-18 16:31:58 +02:00
fp.seek(self.file_start + self.key_table_start +
index_table[i]['key_offset'])
2026-05-18 16:31:58 +02:00
# Read key string until nullbyte
2026-05-18 16:31:58 +02:00
key = ''
2026-05-18 16:31:58 +02:00
while True:
2026-05-18 16:31:58 +02:00
data = fp.read(1)
2026-05-18 16:31:58 +02:00
if data == b'\x00':
break
2026-05-18 16:31:58 +02:00
key += data.decode('utf8')
2026-05-18 16:31:58 +02:00
key_table.append(key)
2026-05-18 16:31:58 +02:00
# Data table
2026-05-18 16:31:58 +02:00
self.key_data = {}
2026-05-18 16:31:58 +02:00
for i in range(0, self.tables_entries):
2026-05-18 16:31:58 +02:00
# Seek to absolute offset + relative offset of data
2026-05-18 16:31:58 +02:00
fp.seek(self.file_start + self.data_table_start + index_table[i]['data_offset'])
2026-05-18 16:31:58 +02:00
if index_table[i]['data_fmt'] == b'\x04\x02': # UTF8
data = fp.read(index_table[i]['data_len'] - 1).decode('utf8')
elif index_table[i]['data_fmt'] == b'\x04\x04': # int32
data = core.to_int(
fp.read(index_table[i]['data_len']), 'little')
else: # Meh
data = fp.read(index_table[i]['data_len'])
2026-05-18 16:31:58 +02:00
self.key_data[key_table[i]] = data
2026-05-18 16:31:58 +02:00
def __getitem__(self, key):
"""Overload [] so we can directly select data using key from .SFO"""
return self.key_data[key]
2026-05-18 16:31:58 +02:00
def print_info(self):
2026-05-18 16:31:58 +02:00
print('Magic:', self.magic)
print('Version: ', self.version)
print('key_table_start:', self.key_table_start)
print('data_table_start:', self.data_table_start)
print('tables_entries:', self.tables_entries)
2026-05-18 16:31:58 +02:00
print(self.key_data)