2022-07-03 23:14:03 +01:00
/*
* nca_storage . h
*
2024-04-12 10:47:36 +01:00
* Copyright ( c ) 2020 - 2024 , DarkMatterCore < pabloacurielz @ gmail . com > .
2022-07-03 23:14:03 +01:00
*
* This file is part of nxdumptool ( https : //github.com/DarkMatterCore/nxdumptool).
*
* nxdumptool 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 .
*
* nxdumptool 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 this program . If not , see < https : //www.gnu.org/licenses/>.
*/
# pragma once
# ifndef __NCA_STORAGE_H__
# define __NCA_STORAGE_H__
# include "bktr.h"
# ifdef __cplusplus
extern " C " {
# endif
typedef enum {
2023-07-17 00:03:05 +01:00
NcaStorageBaseStorageType_Invalid = 0 , ///< Placeholder.
2022-07-03 23:14:03 +01:00
NcaStorageBaseStorageType_Regular = 1 ,
NcaStorageBaseStorageType_Sparse = 2 ,
NcaStorageBaseStorageType_Indirect = 3 ,
2023-07-17 00:03:05 +01:00
NcaStorageBaseStorageType_Compressed = 4 ,
NcaStorageBaseStorageType_Count = 5 ///< Total values supported by this enum.
2022-07-03 23:14:03 +01:00
} NcaStorageBaseStorageType ;
2022-07-04 13:30:48 +01:00
/// Used to perform multi-layered reads within a single NCA FS section.
2022-07-03 23:14:03 +01:00
typedef struct {
u8 base_storage_type ; ///< NcaStorageBaseStorageType.
NcaFsSectionContext * nca_fs_ctx ; ///< NCA FS section context used to initialize this context.
BucketTreeContext * sparse_storage ; ///< Sparse storage context.
BucketTreeContext * aes_ctr_ex_storage ; ///< AesCtrEx storage context.
BucketTreeContext * indirect_storage ; ///< Indirect storage context.
BucketTreeContext * compressed_storage ; ///< Compressed storage context.
} NcaStorageContext ;
2023-09-24 17:41:39 +01:00
/// Initializes a NCA storage context using a NCA FS section context, optionally providing a pointer to a base NcaStorageContext.
2023-10-12 10:24:03 +01:00
/// 'base_ctx' shall be provided if dealing with a patch NCA with available base NCA data. This is needed to perform combined reads between a base NCA and a patch NCA.
/// 'base_ctx' shall be NULL if dealing with a base NCA *or* a patch NCA with missing base NCA data.
2023-09-24 17:41:39 +01:00
bool ncaStorageInitializeContext ( NcaStorageContext * out , NcaFsSectionContext * nca_fs_ctx , NcaStorageContext * base_ctx ) ;
2022-07-03 23:14:03 +01:00
2022-07-04 13:30:48 +01:00
/// Retrieves the underlying NCA FS section's hierarchical hash target layer extents. Virtual extents may be returned, depending on the base storage type.
/// Output offset is relative to the start of the NCA FS section.
/// Either 'out_offset' or 'out_size' can be NULL, but at least one of them must be a valid pointer.
bool ncaStorageGetHashTargetExtents ( NcaStorageContext * ctx , u64 * out_offset , u64 * out_size ) ;
2022-07-03 23:14:03 +01:00
/// Reads data from the NCA storage using a previously initialized NcaStorageContext.
bool ncaStorageRead ( NcaStorageContext * ctx , void * out , u64 read_size , u64 offset ) ;
2022-07-07 01:30:45 +01:00
/// Checks if the provided block extents are within the provided Patch NcaStorageContext's Indirect Storage.
bool ncaStorageIsBlockWithinPatchStorageRange ( NcaStorageContext * ctx , u64 offset , u64 size , bool * out ) ;
2022-07-03 23:14:03 +01:00
/// Frees a previously initialized NCA storage context.
void ncaStorageFreeContext ( NcaStorageContext * ctx ) ;
2022-07-04 00:36:01 +01:00
/// Helper inline functions.
NX_INLINE bool ncaStorageIsValidContext ( NcaStorageContext * ctx )
{
return ( ctx & & ctx - > base_storage_type > = NcaStorageBaseStorageType_Regular & & ctx - > base_storage_type < = NcaStorageBaseStorageType_Compressed & & ctx - > nca_fs_ctx & & \
ctx - > nca_fs_ctx - > enabled ) ;
}
2022-07-03 23:14:03 +01:00
# ifdef __cplusplus
}
# endif
# endif /* __NCA_STORAGE_H__ */