moved everything to old stuff

This commit is contained in:
georgemoralis 2023-03-28 09:40:22 +03:00
parent e420476829
commit 5f19daf3ba
53 changed files with 1 additions and 6 deletions

View file

@ -0,0 +1,90 @@
#include "FsFile.h"
FsFile::FsFile()
{
m_file = nullptr;
}
FsFile::FsFile(const std::string& path, fsOpenMode mode)
{
Open(path, mode);
}
bool FsFile::Open(const std::string& path, fsOpenMode mode)
{
Close();
fopen_s(&m_file, path.c_str(), getOpenMode(mode));
return IsOpen();
}
bool FsFile::Close()
{
if (!IsOpen() || std::fclose(m_file) != 0) {
m_file = nullptr;
return false;
}
m_file = nullptr;
return true;
}
FsFile::~FsFile()
{
Close();
}
bool FsFile::Write(const void* src, U64 size)
{
if (!IsOpen() || std::fwrite(src, 1, size, m_file) != size) {
return false;
}
return true;
}
bool FsFile::Read(void* dst, U64 size)
{
if (!IsOpen() || std::fread(dst, 1, size, m_file) != size) {
return false;
}
return true;
}
U32 FsFile::ReadBytes(void* dst, U64 size)
{
return std::fread(dst, 1, size, m_file);
}
bool FsFile::Seek(S64 offset, fsSeekMode mode)
{
if (!IsOpen() || _fseeki64(m_file, offset, getSeekMode(mode)) != 0) {
return false;
}
return true;
}
U64 FsFile::Tell() const
{
if (IsOpen()) {
return _ftelli64(m_file);
}
else {
return -1;
}
}
U64 FsFile::getFileSize()
{
U64 pos = _ftelli64(m_file);
if (_fseeki64(m_file, 0, SEEK_END) != 0) {
return 0;
}
U64 size = _ftelli64(m_file);
if (_fseeki64(m_file, pos, SEEK_SET) != 0) {
return 0;
}
return size;
}
bool FsFile::IsOpen() const
{
return m_file != nullptr;
}

View file

@ -0,0 +1,72 @@
#pragma once
#include <cstdio>
#include <string>
#include "Types.h"
enum fsOpenMode
{
fsRead = 0x1,
fsWrite = 0x2,
fsReadWrite = fsRead | fsWrite
};
enum fsSeekMode
{
fsSeekSet,
fsSeekCur,
fsSeekEnd,
};
class FsFile
{
std::FILE* m_file;
public:
FsFile();
FsFile(const std::string& path, fsOpenMode mode = fsRead);
bool Open(const std::string& path, fsOpenMode mode = fsRead);
bool IsOpen() const;
bool Close();
bool Read(void* dst, U64 size);
U32 ReadBytes(void* dst, U64 size);
bool Write(const void* src, U64 size);
bool Seek(S64 offset, fsSeekMode mode);
U64 getFileSize();
U64 Tell() const;
~FsFile();
template< typename T > bool ReadBE(T& dst)
{
if (!Read(&dst, sizeof(T))) {
return false;
}
::ReadBE(dst);
return true;
}
const char* getOpenMode(fsOpenMode mode)
{
switch (mode) {
case fsRead: return "rb";
case fsWrite: return "wb";
case fsReadWrite: return "r+b";
}
return "r";
}
const int getSeekMode(fsSeekMode mode)
{
switch (mode) {
case fsSeekSet: return SEEK_SET;
case fsSeekCur: return SEEK_CUR;
case fsSeekEnd: return SEEK_END;
}
return SEEK_SET;
}
std::FILE* fileDescr()
{
return m_file;
}
};

View file

@ -0,0 +1,18 @@
#include "PFS.h"
PFS::PFS()
{
}
PFS::~PFS()
{
}
bool PFS::pfsOuterReadHeader(U08* psfOuterStart)
{
psfOuterheader = (PFS_HDR&)psfOuterStart[0];
return true;
}

View file

@ -0,0 +1,75 @@
#pragma once
#include <string>
#include "Types.h"
#include <io.h>
#include <windows.h>
#include <stdio.h>
#include <inttypes.h>
enum PFSMode : U08 {
PFS_MODE_NONE = 0,
PFS_MODE_SIGNED = 0x1,
PFS_MODE_64BIT = 0x2,
PFS_MODE_ENCRYPTED = 0x4,
PFS_MODE_UNKNOWN = 0x8,
};
struct PFS_HDR {
U64 version;
U64 magic;
U64 id;
U08 fmode;
U08 clean;
U08 ronly;
U08 rsv;
U16 mode;
U16 unk1;
U32 blocksz;
U32 nbackup;
U64 nblock;
U64 ndinode;
U64 ndblock;
U64 ndinodeblock;
U64 superroot_ino;
};
class PFS
{
private:
PFS_HDR psfOuterheader;
public:
PFS();
~PFS();
bool pfsOuterReadHeader(U08* psfOuterStart);
void printPsfOuterHeader()
{
printf("PS4 PSF Outer Header:\n");
printf("- version: 0x%" PRIx64 "\n", psfOuterheader.version);
printf("- magic: 0x%" PRIx64 "\n", psfOuterheader.magic);
printf("- id: 0x%" PRIx64 "\n", psfOuterheader.id);
printf("- fmode: 0x%X\n", psfOuterheader.fmode);
printf("- clean: 0x%X\n", psfOuterheader.clean);
printf("- ronly: 0x%X\n", psfOuterheader.ronly);
printf("- rsv: 0x%X\n", psfOuterheader.rsv);
printf("- mode: 0x%X", psfOuterheader.mode);
if (psfOuterheader.mode & PFS_MODE_SIGNED)
printf(" signed");
if (psfOuterheader.mode & PFS_MODE_64BIT)
printf(" 64-bit");
if (psfOuterheader.mode & PFS_MODE_ENCRYPTED)
printf(" encrypted");
if (psfOuterheader.mode & PFS_MODE_UNKNOWN)
printf(" unknown");
printf("\n");
printf("- unk1: 0x%X\n", psfOuterheader.unk1);
printf("- blocksz: 0x%X\n", psfOuterheader.blocksz);
printf("- nbackup: 0x%X\n", psfOuterheader.nbackup);
printf("- nblock: 0x%" PRIx64 "\n", psfOuterheader.nblock);
printf("- ndinode: 0x%" PRIx64 "\n", psfOuterheader.ndinode);
printf("- ndblock: 0x%" PRIx64 "\n", psfOuterheader.ndblock);
printf("- ndinodeblock: 0x%" PRIx64 "\n", psfOuterheader.ndinodeblock);
printf("- superroot_ino: 0x%" PRIx64 "\n", psfOuterheader.superroot_ino);
printf("\n\n");
}
};

View file

@ -0,0 +1,117 @@
#include "PKG.h"
#include "FsFile.h"
#include "PFS.h"
#include <direct.h>
PKG::PKG()
{
}
PKG::~PKG()
{
}
bool PKG::open(const std::string& filepath) {
FsFile file;
if (!file.Open(filepath, fsRead))
{
return false;
}
pkgSize = file.getFileSize();
file.ReadBE(pkgheader);
//we have already checked magic should be ok
//find title id it is part of pkg_content_id starting at offset 0x40
file.Seek(0x47, fsSeekSet);//skip first 7 characters of content_id
file.Read(&pkgTitleID, sizeof(pkgTitleID));
file.Close();
return true;
}
bool PKG::extract(const std::string& filepath, const std::string& extractPath, std::string& failreason)
{
this->extractPath = extractPath;
FsFile file;
if (!file.Open(filepath, fsRead))
{
return false;
}
pkgSize = file.getFileSize();
file.ReadBE(pkgheader);
if (pkgheader.pkg_size > pkgSize)
{
failreason = "PKG file size is different";
return false;
}
if ((pkgheader.pkg_content_size + pkgheader.pkg_content_offset) > pkgheader.pkg_size)
{
failreason = "Content size is bigger than pkg size";
return false;
}
file.Seek(0, fsSeekSet);
pkg = (U08*)mmap(pkgSize, file.fileDescr());
if (pkg == nullptr)
{
failreason = "Can't allocate size for image";
return false;
}
file.Read(pkg, pkgSize);
U32 offset = pkgheader.pkg_table_entry_offset;
U32 n_files = pkgheader.pkg_table_entry_count;
for (U32 i = 0; i < n_files; i++) {
PKGEntry entry = (PKGEntry&)pkg[offset + i * 0x20];
ReadBE(entry);
//try to figure out the name
std::string name = getEntryNameByType(entry.id);
printPkgFileEntry(entry,name);
if (!name.empty())
{
std::size_t pos = name.find("/");//check if we have a directory (assuming we only have 1 level of subdirectories)
if (pos != std::string::npos)
{
//directory found
std::string dir = name.substr(0, pos+1);//include '/' as well
std::string path = extractPath + dir;
_mkdir(path.c_str());//make dir
std::string file = name.substr(pos + 1);//read filename
FsFile out;
out.Open(path + file, fsWrite);
out.Write(pkg + entry.offset, entry.size);
out.Close();
}
//found an name use it
FsFile out;
out.Open(extractPath + name, fsWrite);
out.Write(pkg + entry.offset, entry.size);
out.Close();
}
else
{
//just print with id
FsFile out;
out.Open(extractPath + std::to_string(entry.id), fsWrite);
out.Write(pkg + entry.offset, entry.size);
out.Close();
}
}
//PFS read
PFS pfs;
pfs.pfsOuterReadHeader(pkg + pkgheader.pfs_image_offset);
pfs.printPsfOuterHeader();
//extract pfs_image.dat
FsFile out;
out.Open(extractPath + "pfs_image.dat", fsWrite);
out.Write(pkg + pkgheader.pfs_image_offset, pkgheader.pfs_image_size);
out.Close();
munmap(pkg);
return true;
}

View file

@ -0,0 +1,412 @@
#pragma once
#include <string>
#include "Types.h"
#include <io.h>
#include <windows.h>
#include <stdio.h>
#include <inttypes.h>
enum PKGFlags : U32 {
PKG_FLAGS_UNKNOWN = 0x01,
PKG_FLAGS_VER_1 = 0x01000000,
PKG_FLAGS_VER_2 = 0x02000000,
PKG_FLAGS_INTERNAL = 0x40000000,
PKG_FLAGS_FINALIZED = 0x80000000,
};
enum PKGDrmType : U32
{
PKG_DRM_TYPE_NONE = 0x0,
PKG_DRM_TYPE_PS4 = 0xF,
};
enum PKGContentType : U32
{
/* pkg_ps4_app, pkg_ps4_patch, pkg_ps4_remaster */
PKG_CONTENT_TYPE_GD = 0x1A,
/* pkg_ps4_ac_data, pkg_ps4_sf_theme, pkg_ps4_theme */
PKG_CONTENT_TYPE_AC = 0x1B,
/* pkg_ps4_ac_nodata */
PKG_CONTENT_TYPE_AL = 0x1C,
/* pkg_ps4_delta_patch */
PKG_CONTENT_TYPE_DP = 0x1E,
};
enum PKGContentFlags : U32
{
PKG_CONTENT_FLAGS_FIRST_PATCH = 0x00100000,
PKG_CONTENT_FLAGS_PATCHGO = 0x00200000,
PKG_CONTENT_FLAGS_REMASTER = 0x00400000,
PKG_CONTENT_FLAGS_PS_CLOUD = 0x00800000,
PKG_CONTENT_FLAGS_GD_AC = 0x02000000,
PKG_CONTENT_FLAGS_NON_GAME = 0x04000000,
PKG_CONTENT_FLAGS_Unk_x8000000 = 0x08000000,
PKG_CONTENT_FLAGS_SUBSEQUENT_PATCH = 0x40000000,
PKG_CONTENT_FLAGS_DELTA_PATCH = 0x41000000,
PKG_CONTENT_FLAGS_CUMULATIVE_PATCH = 0x60000000,
};
enum IROTag : U32
{
PKG_IRO_TAG_NONE = 0,
/* SHAREfactory theme */
PKG_IRO_TAG_SF_THEME = 0x1,
/* System Software theme */
PKG_IRO_TAG_SS_THEME = 0x2,
};
struct PKGHeader {
/*BE*/U32 magic;// Magic
/*BE*/U32 pkg_flags;
/*BE*/U32 pkg_0x8; //unknown field
/*BE*/U32 pkg_file_count;
/*BE*/U32 pkg_table_entry_count;
/*BE*/U16 pkg_sc_entry_count;
/*BE*/U16 pkg_table_entry_count_2;// same as pkg_entry_count
/*BE*/U32 pkg_table_entry_offset;//file table offset
/*BE*/U32 pkg_sc_entry_data_size;
/*BE*/U64 pkg_body_offset;//offset of PKG entries
/*BE*/U64 pkg_body_size;//length of all PKG entries
/*BE*/U64 pkg_content_offset;
/*BE*/U64 pkg_content_size;
U08 pkg_content_id[0x24];//packages' content ID as a 36-byte string
U08 pkg_padding[0xC];//padding
/*BE*/U32 pkg_drm_type;//DRM type
/*BE*/U32 pkg_content_type;//Content type
/*BE*/U32 pkg_content_flags;//Content flags
/*BE*/U32 pkg_promote_size;
/*BE*/U32 pkg_version_date;
/*BE*/U32 pkg_version_hash;
/*BE*/U32 pkg_0x088;
/*BE*/U32 pkg_0x08C;
/*BE*/U32 pkg_0x090;
/*BE*/U32 pkg_0x094;
/*BE*/U32 pkg_iro_tag;
/*BE*/U32 pkg_drm_type_version;
U08 pkg_zeroes_1[0x60];
/* Digest table */
U08 digest_entries1[0x20]; // sha256 digest for main entry 1
U08 digest_entries2[0x20]; // sha256 digest for main entry 2
U08 digest_table_digest[0x20]; // sha256 digest for digest table
U08 digest_body_digest[0x20]; // sha256 digest for main table
U08 pkg_zeroes_2[0x280];
U32 pkg_0x400;
U32 pfs_image_count; // count of PFS images
U64 pfs_image_flags; // PFS flags
U64 pfs_image_offset; // offset to start of external PFS image
U64 pfs_image_size; // size of external PFS image
U64 mount_image_offset;
U64 mount_image_size;
U64 pkg_size;
U32 pfs_signed_size;
U32 pfs_cache_size;
U08 pfs_image_digest[0x20];
U08 pfs_signed_digest[0x20];
U64 pfs_split_size_nth_0;
U64 pfs_split_size_nth_1;
U08 pkg_zeroes_3[0xB50];
U08 pkg_digest[0x20];
};
inline void ReadBE(PKGHeader& s)
{
ReadBE(s.magic);
ReadBE(s.pkg_flags);
ReadBE(s.pkg_0x8);
ReadBE(s.pkg_file_count);
ReadBE(s.pkg_table_entry_count);
ReadBE(s.pkg_sc_entry_count);
ReadBE(s.pkg_table_entry_count_2);
ReadBE(s.pkg_table_entry_offset);
ReadBE(s.pkg_sc_entry_data_size);
ReadBE(s.pkg_body_offset);
ReadBE(s.pkg_body_size);
ReadBE(s.pkg_content_offset);
ReadBE(s.pkg_content_size);
ReadBE(s.pkg_drm_type);
ReadBE(s.pkg_content_type);
ReadBE(s.pkg_content_flags);
ReadBE(s.pkg_promote_size);
ReadBE(s.pkg_version_date);
ReadBE(s.pkg_version_hash);
ReadBE(s.pkg_0x088);
ReadBE(s.pkg_0x08C);
ReadBE(s.pkg_0x090);
ReadBE(s.pkg_0x094);
ReadBE(s.pkg_iro_tag);
ReadBE(s.pkg_drm_type_version);
ReadBE(s.pkg_0x400);
ReadBE(s.pfs_image_count);
ReadBE(s.pfs_image_flags);
ReadBE(s.pfs_image_offset);
ReadBE(s.pfs_image_size);
ReadBE(s.mount_image_offset);
ReadBE(s.mount_image_size);
ReadBE(s.pfs_signed_size);
ReadBE(s.pfs_cache_size);
ReadBE(s.pkg_size);
ReadBE(s.pfs_split_size_nth_0);
ReadBE(s.pfs_split_size_nth_1);
}
struct PKGEntry {
U32 id; // File ID, useful for files without a filename entry
U32 filename_offset; // Offset into the filenames table (ID 0x200) where this file's name is located
U32 flags1; // Flags including encrypted flag, etc
U32 flags2; // Flags including encryption key index, etc
U32 offset; // Offset into PKG to find the file
U32 size; // Size of the file
U64 padding; // blank padding
};
inline void ReadBE(PKGEntry& s)
{
ReadBE(s.id);
ReadBE(s.filename_offset);
ReadBE(s.flags1);
ReadBE(s.flags2);
ReadBE(s.offset);
ReadBE(s.size);
ReadBE(s.padding);
}
class PKG
{
private:
U08* pkg;
U64 pkgSize = 0;
S08 pkgTitleID[9];
std::string extractPath;
PKGHeader pkgheader;
public:
PKG();
~PKG();
bool open(const std::string& filepath);
U64 getPkgSize()
{
return pkgSize;
}
std::string getTitleID()
{
return std::string(pkgTitleID,9);
}
bool extract(const std::string& filepath, const std::string& extractPath, std::string& failreason);
void* mmap(size_t sLength, std::FILE* nFd) {
HANDLE hHandle;
void* pStart=nullptr;
hHandle = CreateFileMapping(
(HANDLE)_get_osfhandle(_fileno((nFd))),
NULL, // default security
PAGE_WRITECOPY, // read/write access
0, // maximum object size (high-order DWORD)
0, // maximum object size (low-order DWORD)
NULL); // name of mapping object
if (hHandle != NULL) {
pStart = MapViewOfFile(hHandle, FILE_MAP_COPY, 0, 0, sLength);
}
if(pStart == NULL)
{
return nullptr;
}
return pStart;
}
int munmap(void* pStart) {
if (UnmapViewOfFile(pStart) != 0)
return FALSE;
return TRUE;
}
typedef struct {
U32 type;
std::string name;
} pkg_entry_value;
std::string getEntryNameByType(U32 type)
{
pkg_entry_value entries[] = {
{ 0x0001, "digests" },
{ 0x0010, "entry_keys" },
{ 0x0020, "image_key" },
{ 0x0080, "general_digests" },
{ 0x0100, "metas" },
{ 0x0200, "entry_names" },
{ 0x0400, "license.dat" },
{ 0x0401, "license.info" },
{ 0x0402, "nptitle.dat" },
{ 0x0403, "npbind.dat" },
{ 0x0409, "psreserved.dat" },
{ 0x1000, "param.sfo" },
{ 0x1001, "playgo-chunk.dat" },
{ 0x1002, "playgo-chunk.sha" },
{ 0x1003, "playgo-manifest.xml" },
{ 0x1004, "pronunciation.xml" },
{ 0x1005, "pronunciation.sig" },
{ 0x1006, "pic1.png" },
{ 0x1007, "pubtoolinfo.dat" },
{ 0x100B, "shareparam.json" },
{ 0x100C, "shareoverlayimage.png" },
{ 0x100E, "shareprivacyguardimage.png"},
{ 0x1200, "icon0.png" },
{ 0x1220, "pic0.png" },
{ 0x1240, "snd0.at9" },
{ 0x1280, "icon0.dds" },
{ 0x12A0, "pic0.dds" },
{ 0x12C0, "pic1.dds" },
{ 0x1400, "trophy/trophy00.trp" }
};
std::string entry_name="";
for (size_t i = 0; i < sizeof entries / sizeof entries[0]; i++) {
if (type == entries[i].type) {
entry_name = entries[i].name;
break;
}
}
return entry_name;
}
void printPkgHeader()
{
printf("PS4 PKG header:\n");
printf("- PKG magic: 0x%X\n", pkgheader.magic);
printf("- PKG flags: 0x%X", pkgheader.pkg_flags);
if (pkgheader.pkg_flags & PKG_FLAGS_UNKNOWN)
printf(" unknown");
if (pkgheader.pkg_flags & PKG_FLAGS_VER_1)
printf(" ver_1");
if (pkgheader.pkg_flags & PKG_FLAGS_VER_2)
printf(" ver_2");
if (pkgheader.pkg_flags & PKG_FLAGS_INTERNAL)
printf(" internal");
if(pkgheader.pkg_flags & PKG_FLAGS_FINALIZED)
printf(" finalized");
printf("\n");
printf("- PKG 0x8: 0x%X\n", pkgheader.pkg_0x8);
printf("- PKG file count: %u\n", pkgheader.pkg_file_count);
printf("- PKG table entries count: %u\n", pkgheader.pkg_table_entry_count);
printf("- PKG system entries count: %u\n", pkgheader.pkg_sc_entry_count);
printf("- PKG table entries2 count: %u\n", pkgheader.pkg_table_entry_count_2);
printf("- PKG table offset: 0x%X\n", pkgheader.pkg_table_entry_offset);
printf("- PKG table entry data size: 0x%X\n", pkgheader.pkg_sc_entry_data_size);
printf("- PKG body offset: 0x%" PRIx64 "\n", pkgheader.pkg_body_offset);
printf("- PKG body size: 0x%" PRIx64 "\n", pkgheader.pkg_body_size);
printf("- PKG content offset: 0x%" PRIx64 "\n", pkgheader.pkg_content_offset);
printf("- PKG content size: 0x%" PRIx64 "\n", pkgheader.pkg_content_offset);
printf("- PKG pkg_content_id: %s\n", pkgheader.pkg_content_id);
printf("- PKG drm type: 0x%X", pkgheader.pkg_drm_type);
if (pkgheader.pkg_drm_type == PKG_DRM_TYPE_NONE)
printf(" None");
if (pkgheader.pkg_drm_type == PKG_DRM_TYPE_PS4)
printf(" PS4");
printf("\n");
printf("- PKG content type: 0x%X", pkgheader.pkg_content_type);
if (pkgheader.pkg_content_type == PKG_CONTENT_TYPE_GD)
printf(" GD");
if (pkgheader.pkg_content_type == PKG_CONTENT_TYPE_AC)
printf(" AC");
if (pkgheader.pkg_content_type == PKG_CONTENT_TYPE_AL)
printf(" AL");
if (pkgheader.pkg_content_type == PKG_CONTENT_TYPE_DP)
printf(" DP");
printf("\n");
printf("- PKG content flags: 0x%X", pkgheader.pkg_content_flags);
if (pkgheader.pkg_content_flags & PKG_CONTENT_FLAGS_FIRST_PATCH)
printf(" First_patch");
if (pkgheader.pkg_content_flags & PKG_CONTENT_FLAGS_PATCHGO)
printf(" Patch_go");
if (pkgheader.pkg_content_flags & PKG_CONTENT_FLAGS_REMASTER)
printf(" Remastered");
if (pkgheader.pkg_content_flags & PKG_CONTENT_FLAGS_PS_CLOUD)
printf(" Cloud");
if (pkgheader.pkg_content_flags & PKG_CONTENT_FLAGS_GD_AC)
printf(" AC");
if (pkgheader.pkg_content_flags & PKG_CONTENT_FLAGS_NON_GAME)
printf(" Non_game");
if (pkgheader.pkg_content_flags & PKG_CONTENT_FLAGS_Unk_x8000000)
printf(" Unk_x8000000");
if (pkgheader.pkg_content_flags & PKG_CONTENT_FLAGS_SUBSEQUENT_PATCH)
printf(" Subsequent_patch");
if (pkgheader.pkg_content_flags & PKG_CONTENT_FLAGS_DELTA_PATCH)
printf(" Delta_patch");
if (pkgheader.pkg_content_flags & PKG_CONTENT_FLAGS_CUMULATIVE_PATCH)
printf(" Cumulative_patch");
printf("\n");
printf("- PKG promote size: 0x%X\n", pkgheader.pkg_promote_size);
printf("- PKG version date: 0x%X\n", pkgheader.pkg_version_date);
printf("- PKG version hash: 0x%X\n", pkgheader.pkg_version_hash);
printf("- PKG 0x088: 0x%X\n", pkgheader.pkg_0x088);
printf("- PKG 0x08C: 0x%X\n", pkgheader.pkg_0x08C);
printf("- PKG 0x090: 0x%X\n", pkgheader.pkg_0x090);
printf("- PKG 0x094: 0x%X\n", pkgheader.pkg_0x094);
printf("- PKG iro tag: 0x%X", pkgheader.pkg_iro_tag);
if (pkgheader.pkg_iro_tag == PKG_IRO_TAG_NONE)
printf(" None");
if (pkgheader.pkg_iro_tag == PKG_IRO_TAG_SF_THEME)
printf(" SF Theme");
if (pkgheader.pkg_iro_tag == PKG_IRO_TAG_SS_THEME)
printf(" SS Theme");
printf("\n");
printf("- PKG drm type version: 0x%X\n", pkgheader.pkg_drm_type_version);
printf("- PKG digest_entries1: ");
for (U08 s = 0; s < 0x20; s++) printf("%X", pkgheader.digest_entries1[s]);
printf("\n");
printf("- PKG digest_entries2: ");
for (U08 s = 0; s < 0x20; s++)printf("%X", pkgheader.digest_entries2[s]);
printf("\n");
printf("- PKG digest_table_digest: ");
for (U08 s = 0; s < 0x20; s++)printf("%X", pkgheader.digest_table_digest[s]);
printf("\n");
printf("- PKG digest_body_digest: ");
for (U08 s = 0; s < 0x20; s++)printf("%X", pkgheader.digest_body_digest[s]);
printf("\n");
printf("- PKG 0x400: 0x%X\n", pkgheader.pkg_0x400);
printf("- PKG pfs_image_count: 0x%X\n", pkgheader.pfs_image_count);
printf("- PKG pfs_image_flags: 0x%" PRIx64 "\n", pkgheader.pfs_image_flags);
printf("- PKG pfs_image_offset: 0x%" PRIx64 "\n", pkgheader.pfs_image_offset);
printf("- PKG mount_image_offset: 0x%" PRIx64 "\n", pkgheader.mount_image_offset);
printf("- PKG mount_image_size: 0x%" PRIx64 "\n", pkgheader.mount_image_size);
printf("- PKG size: 0x%" PRIx64 "\n", pkgheader.pkg_size);
printf("- PKG pfs_signed_size: 0x%X\n", pkgheader.pfs_signed_size);
printf("- PKG pfs_cache_size: 0x%X\n", pkgheader.pfs_cache_size);
printf("- PKG pfs_image_digest: ");
for (U08 s = 0; s < 0x20; s++)printf("%X", pkgheader.pfs_image_digest[s]);
printf("\n");
printf("- PKG pfs_signed_digest: ");
for (U08 s = 0; s < 0x20; s++)printf("%X", pkgheader.pfs_signed_digest[s]);
printf("\n");
printf("- PKG pfs_split_size_nth_0: 0x%" PRIx64 "\n", pkgheader.pfs_split_size_nth_0);
printf("- PKG pfs_split_size_nth_1: 0x%" PRIx64 "\n", pkgheader.pfs_split_size_nth_1);
printf("- PKG pkg_digest: ");
for (U08 s = 0; s < 0x20; s++)printf("%X", pkgheader.pkg_digest[s]);
printf("\n");
printf("\n\n");
}
void printPkgFileEntry(PKGEntry entry, std::string name)
{
printf("-PS4 File Entry:\n");
printf("--found name: %s\n", name.c_str());
printf("--id: 0x%X\n", entry.id);
printf("--filename_offset: 0x%X\n", entry.filename_offset);
printf("--flags1: 0x%X\n", entry.flags1);
printf("--flags2: 0x%X\n", entry.flags2);
printf("--offset: 0x%X\n", entry.offset);
printf("--size: 0x%X\n", entry.size);
}
};

View file

@ -0,0 +1,46 @@
#pragma once
#include <immintrin.h>
using S08 = char;
using S16 = short;
using S32 = int;
using S64 = long long;
using U08 = unsigned char;
using U16 = unsigned short;
using U32 = unsigned int;
using U64 = unsigned long long;
using F32 = float;
using F64 = double;
template< typename T > T inline LoadBE(T* src) { return *src; };
template< typename T > inline void StoreBE(T* dst, T src) { *dst = src; };
inline S16 LoadBE(S16* src) { return _loadbe_i16(src); };
inline S32 LoadBE(S32* src) { return _loadbe_i32(src); };
inline S64 LoadBE(S64* src) { return _loadbe_i64(src); };
inline U16 LoadBE(U16* src) { return _load_be_u16(src); };
inline U32 LoadBE(U32* src) { return _load_be_u32(src); };
inline U64 LoadBE(U64* src) { return _load_be_u64(src); };
inline void StoreBE(S16* dst, S16 const src) { _storebe_i16(dst, src); };
inline void StoreBE(S32* dst, S32 const src) { _storebe_i32(dst, src); };
inline void StoreBE(S64* dst, S64 const src) { _storebe_i64(dst, src); };
inline void StoreBE(U16* dst, U16 const src) { _store_be_u16(dst, src); };
inline void StoreBE(U32* dst, U32 const src) { _store_be_u32(dst, src); };
inline void StoreBE(U64* dst, U64 const src) { _store_be_u64(dst, src); };
template< typename T > inline void ReadBE(T& val)
{
val = LoadBE(&val); // swap inplace
}
template< typename T > inline void WriteBE(T& val)
{
StoreBE(&val, val); // swap inplace
}

View file

@ -0,0 +1,27 @@
#include <iostream>
#include <sys/stat.h>
#include <direct.h>
#include "PKG.h"
int main()
{
PKG pkg;
if (!pkg.open("test.pkg"))
{
std::cout << "Error reading test.pkg\n";
return 0;
}
pkg.printPkgHeader();
std::string gamedir = "game/" + pkg.getTitleID();
struct stat sb;
if (stat(gamedir.c_str(), &sb) != 0)
{
_mkdir(gamedir.c_str());
}
std::string extractpath = "game/" + pkg.getTitleID() + "/";
std::string failreason;
if (!pkg.extract("test.pkg", extractpath, failreason))
{
std::cout << "Error extraction " << failreason;
}
}

View file

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33213.308
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pkgextract", "pkgextract.vcxproj", "{446E2603-4743-49E6-B4CB-56835155399E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{446E2603-4743-49E6-B4CB-56835155399E}.Debug|x64.ActiveCfg = Debug|x64
{446E2603-4743-49E6-B4CB-56835155399E}.Debug|x64.Build.0 = Debug|x64
{446E2603-4743-49E6-B4CB-56835155399E}.Debug|x86.ActiveCfg = Debug|Win32
{446E2603-4743-49E6-B4CB-56835155399E}.Debug|x86.Build.0 = Debug|Win32
{446E2603-4743-49E6-B4CB-56835155399E}.Release|x64.ActiveCfg = Release|x64
{446E2603-4743-49E6-B4CB-56835155399E}.Release|x64.Build.0 = Release|x64
{446E2603-4743-49E6-B4CB-56835155399E}.Release|x86.ActiveCfg = Release|Win32
{446E2603-4743-49E6-B4CB-56835155399E}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3DFF47CF-BF9E-4143-A8C0-8ED0C43581CB}
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,144 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{446e2603-4743-49e6-b4cb-56835155399e}</ProjectGuid>
<RootNamespace>pkgextract</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="FsFile.cpp" />
<ClCompile Include="PFS.cpp" />
<ClCompile Include="PKG.cpp" />
<ClCompile Include="pkgextract.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="FsFile.h" />
<ClInclude Include="PFS.h" />
<ClInclude Include="PKG.h" />
<ClInclude Include="Types.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View file

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pkgextract.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="PKG.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="FsFile.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="PFS.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="PKG.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="FsFile.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="Types.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="PFS.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
</Project>