#include <cafe/szfile.h>

typedef void * (*SZFILE_pf_Alloc)(size_t aBytes);
typedef void   (*SZFILE_pf_Free)(void *addr);

    struct _SZFILE_DIR *    mpParentDir;
    u32                     mArcIndex;
    char const *            mpNameOnly;
    u32                     mNameOnlyLen;
    u32                     mStreamIx;
    u32                     mStreamUnpackedOffset;
    u32                     mUncompBytes;

struct _SZFILE_DIR
    struct _SZFILE_ARC *    mpArc;
    struct _SZFILE_DIR *    mpParentDir;
    char const *            mpNameOnly;
    u32                     mNameOnlyLen;
    u32                     mNumFiles;
    SZFILE_ENTRY *          mpFilesArray;
    struct _SZFILE_DIR *    mpSubDirList;
    struct _SZFILE_DIR *    mpNextSib;
typedef struct _SZFILE_DIR SZFILE_DIR;

struct _SZFILE_ARC
    void *          mpImp;

    u8 const *      mpSrcFile;
    u32             mSrcFileBytes;

    SZFILE_pf_Alloc mfAlloc;
    SZFILE_pf_Free  mfFree;

    u32             mNumStreams;
    u8 * *          mppUnpackedStreams;

    SZFILE_DIR *    mpRootDir;
typedef struct _SZFILE_ARC SZFILE_ARC;

int SZFILE_CreateArc(SZFILE_ARC *apArc, u8 const *apFileData, u32 aFileBytes, 
                     SZFILE_pf_Alloc afAlloc, SZFILE_pf_Free  afFree);


apArc Pointer to memory to hold archive control structure. This memory must be maintained for the life of the archive and the 7-Zip file data that it maps.
apFileData Pointer to byte array of the entire 7-Zip file to be parsed.
aFileBytes Number of bytes in the 7-Zip file pointed to by apFileData argument.
afAlloc Pointer to function to use to allocate memory for use with the archive. This is maintained in the SZFILE_ARC structure.
afFree Pointer to function to use to deallocated memory for use with the archive that has been allocated with the function pointed to by the afAlloc argument. This is maintained in the SZFILE_ARC structure.

Return Values

Returns zero for success or an error code to indicate failure. Error codes are:

ERRCODE_SZFILE_OUTOFMEMORYThe afAlloc memory allocation function returned NULL
ERRCODE_SZFILE_INTERNALAn internal error has occurred and should be reported.
ERRCODE_SZFILE_BADARGA bad argument was passed to a function
ERRCODE_SZFILE_CORRUPTThe 7-Zip file does not make sense or has been corrupted.
ERRCODE_SZFILE_EMPTYThe 7-Zip file specified is empty and contains no files.


This library provides facilities for parsing a 7-Zip file so that files in the 7-Zip can be found and extracted. 7-Zip files are in little-endian format and this library parses a manifest into target byte order (big-endian), so that files can be found and extracted.

7-Zip files are usually arranged in one compressed contiguous stream for all files in the archive. To extract a single file from the archive you generally must extract all files 'ahead' of the target file in the compressed stream.

As such, 7-Zip is a good compression candidate when you need to use all files in an archive, and potentially all at the same time. It is not a good compression file format to use for random access to compressed files. If you wish to use random access to compressed files you should look at the ZIPFILE library and use ZIP files.

Demonstration of Use in Cafe

There is a demonstration of usage of the helper library in the demo folder:


Do Not Call From

Multiple threads This function is not thread-safe.

See Also

SZFILE helper library and demo

Revision History

2013/05/06 Initial version.