/*---------------------------------------------------------------------------* Copyright (C) Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. *---------------------------------------------------------------------------*/ #ifndef __SZFILE_H #define __SZFILE_H /* --------------------------------------------------------------------------------- This library is provided to assist in parsing and decoding data in .7z files. You can create a .7z file on Windows by installing the 7Zip toolkit from www.7zip.com, and then using the tool directory or if you install the shell extension by right-clicking on selection of files or a folder and selecting "7Zip -> Add To Archive..." or one of the other options on the menu. The library works on both Windows and Cafe, and respects differences in CPU endianness. 7z files are in little-endian byte order but the target system may be big-endian (Cafe). The main library function is SZFILE_CreateArc It takes as input the address of a control structure to use, and a blob of memory that is the original .7z compressed ZIP file, which has been placed into memory in its entirety by some mechanism (built in, loaded, etc.). This library does not do the loading. The function parses the file and creates a manifest that looks like a folder tree. The manifest has pointers in it that refer to data in the original input blob. A pointer to the root folder is returned by the function into the control structure. The library function uses allocate/free functions you provide to allocate and free memory as needed during operations with the archive. Different functions may be specified for different archives. The manifest of directories in the archive contains the SZFILE_DIR and SZFILE_ENTRY structures that refer to each other. The structures are in target system byte order and can be used directly. When you are finished with the archive you should purge all memory used for it using the SZFILE_PurgeArc function. This function does not remove or free the original blob of memory used to create the archive, which may not be freeable in some circumstances. --------------------------------------------------------------------------------- */ #ifdef _MSC_VER #include #else #ifdef EPPC #include #else #error !!! Unknown compilation target #endif #endif #ifdef __cplusplus extern "C" { #endif #ifdef EPPC typedef u8 UINT8; typedef u16 UINT16; typedef u32 UINT32; typedef u64 UINT64; #endif /* --------------------------------------------------------------------------------- */ /* these functions are used by SZFILE_ functions that work on an archive to allocate and free memory. You associate specific functions with an archive when it is created using the SZFILE_CreateArc function. */ typedef void * (*SZFILE_pf_Alloc)(size_t aBytes); typedef void (*SZFILE_pf_Free)(void *addr); /* This manifest structure describes a single file in the .7z archive. */ struct _SZFILE_ENTRY { struct _SZFILE_DIR * mpParentDir; UINT32 mArcIndex; char const * mpNameOnly; UINT32 mNameOnlyLen; UINT32 mStreamIx; UINT32 mStreamUnpackedOffset; UINT32 mUncompBytes; }; typedef struct _SZFILE_ENTRY SZFILE_ENTRY; /* This manifest structure describes a single directory/subdirectory in the .7z archive. */ struct _SZFILE_DIR { struct _SZFILE_ARC * mpArc; struct _SZFILE_DIR * mpParentDir; char const * mpNameOnly; UINT32 mNameOnlyLen; UINT32 mNumFiles; SZFILE_ENTRY * mpFilesArray; struct _SZFILE_DIR * mpSubDirList; struct _SZFILE_DIR * mpNextSib; }; typedef struct _SZFILE_DIR SZFILE_DIR; /* this is the archive control structure. it has an opaque pointer to implementation bits, a reference to the original data used to build the manifest, and pointers to both the uncompressed streams (if they have been uncompressed), and the root directory for the directories and files in the archive */ struct _SZFILE_ARC { void * mpImp; UINT8 const * mpSrcFile; UINT32 mSrcFileBytes; SZFILE_pf_Alloc mfAlloc; SZFILE_pf_Free mfFree; UINT32 mNumStreams; UINT8 ** mppUnpackedStreams; SZFILE_DIR * mpRootDir; }; typedef struct _SZFILE_ARC SZFILE_ARC; /* this function is used to populate an SZFILE_ARC structure by parsing the .7z file specified as an argument. The afAlloc and afFree functions match the prototypes of malloc and free (respectively), and are associated with the archive for its lifespan. */ int SZFILE_CreateArc(SZFILE_ARC *apArc, UINT8 const *apFileData, UINT32 aFileBytes, SZFILE_pf_Alloc afAlloc, SZFILE_pf_Free afFree); /* this function extracts a specific stream in the archive (there may be more than one stream). a stream is never extracted more than once */ int SZFILE_UnpackStream(SZFILE_ARC *apArc, UINT32 aStreamIx); /* this function allows you to find a file by its full path in an archive. it returns via an output parameter a pointer to the entry for the file if it is found */ int SZFILE_FindInArc(SZFILE_ARC *apArc, char const *apFileName, SZFILE_ENTRY **apRetEntryPtr); /* this function extracts any stream associated with a file entry and returns a pointer to its data as well as the size of the extracted file. a stream is never extracted more than once but this function will extract a stream automatically (via SZFILE_UnpackStream) if it needs to */ int SZFILE_GetUnpackedFile(SZFILE_ENTRY *apEntry, UINT8 **apRetDataPtr, UINT32 *apRetDataBytes); /* this is an easy-use helper function that you can use to get to the data of a file and have any extracting that needs to be done execute automatically. It is implemented using the SZFILE_FindInArc and SZFILE_GetUnpackedFile functions */ UINT8 * SZFILE_Get(SZFILE_ARC *apArc, char const *apFileName, UINT32 *apRetFileBytes); /* this function is used to free all memory used to create the manifest for an archive from the original .7z file contents. this function does not free the original file data buffer specified in the SZFILE_CreateArc function call */ void SZFILE_PurgeArc(SZFILE_ARC *apArc); /* these are errorcodes that can occur as results of use of the functions above. a zero result from a function means "success" */ #define SZFILE_ERRCODE(x) ((int)(0xBADE0000 | (x))) #define ERRCODE_SZFILE_OUTOFMEMORY SZFILE_ERRCODE(1) #define ERRCODE_SZFILE_INTERNAL SZFILE_ERRCODE(2) #define ERRCODE_SZFILE_BADARG SZFILE_ERRCODE(3) #define ERRCODE_SZFILE_CORRUPT SZFILE_ERRCODE(4) #define ERRCODE_SZFILE_EMPTY SZFILE_ERRCODE(5) #define ERRCODE_SZFILE_FILENOTFOUND SZFILE_ERRCODE(6) #define ERRCODE_SZFILE_PATHNOTFOUND SZFILE_ERRCODE(7) /* --------------------------------------------------------------------------------- */ #ifdef __cplusplus } #endif /* --------------------------------------------------------------------------------- */ #endif // __SZFILE_H