| 1 | cycrow | 1 | #ifndef __CATFILE_H__
 | 
        
           |  |  | 2 | #define __CATFILE_H__
 | 
        
           |  |  | 3 |   | 
        
           |  |  | 4 | #include "File.h"
 | 
        
           |  |  | 5 | #include "File_IO.h"
 | 
        
           |  |  | 6 | #include "lists.h"
 | 
        
           |  |  | 7 | #include "spkdll.h"
 | 
        
           |  |  | 8 |   | 
        
           |  |  | 9 | enum {CATERR_NONE, CATERR_NODATFILE, CATERR_NOCATFILE, CATERR_FILEEMPTY, CATERR_READCAT, CATERR_DECRYPT, CATERR_MISMATCH, CATERR_NOFILE, CATERR_CANTREAD, CATERR_CANTCREATEDIR, CATERR_INVALIDDEST,
 | 
        
           |  |  | 10 | 		CATERR_CREATED};
 | 
        
           |  |  | 11 | enum {CATFILE_NONE, CATFILE_READ, CATFILE_DECYPTED};
 | 
        
           |  |  | 12 | enum {CATREAD_JUSTCONTENTS, CATREAD_CAT, CATREAD_CATDECRYPT, CATREAD_DAT};
 | 
        
           |  |  | 13 |   | 
        
           |  |  | 14 | typedef struct SInCatFile {
 | 
        
           |  |  | 15 | 	struct SInCatFile () { lSize = 0; sData = 0; lOffset = 0; bDelete = false; }
 | 
        
           |  |  | 16 | 	CyString sFile;
 | 
        
           |  |  | 17 | 	size_t lSize;
 | 
        
           |  |  | 18 | 	unsigned char  *sData;
 | 
        
           |  |  | 19 | 	size_t lOffset;
 | 
        
           |  |  | 20 | 	bool	bDelete;
 | 
        
           |  |  | 21 | } SInCatFile;
 | 
        
           |  |  | 22 |   | 
        
           |  |  | 23 | unsigned char SPKEXPORT *PCKData ( unsigned char *data, size_t oldsize, size_t *newsize, bool bXor = true );
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | class SPKEXPORT CCatFile
 | 
        
           |  |  | 26 | {
 | 
        
           |  |  | 27 | public:
 | 
        
           |  |  | 28 | 	static CyString PckChangeExtension ( CyString f );
 | 
        
           |  |  | 29 | 	static CyString RenameFileExtension(SInCatFile *f);
 | 
        
           |  |  | 30 | 	static bool IsAddonDir(CyString dir);
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | 	CCatFile ();
 | 
        
           |  |  | 33 | 	~CCatFile ();
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 | 	int  Open ( CyString catfile, CyString addon, int readtype = CATREAD_CATDECRYPT, bool = true );
 | 
        
           |  |  | 36 | 	bool DecryptData ();
 | 
        
           |  |  | 37 | 	bool DecryptData ( unsigned char *data, size_t size );
 | 
        
           |  |  | 38 | 	void DecryptDAT(unsigned char *buffer, size_t size);
 | 
        
           |  |  | 39 | 	bool ReadFiles ();
 | 
        
           |  |  | 40 | 	void LoadDatFile ();
 | 
        
           |  |  | 41 | 	CyString GetCatFilename() { return m_fCatFile.GetFullFilename(); }
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 | 	bool CheckExtensionPck ( CyString filename );
 | 
        
           |  |  | 44 | 	bool CheckPackedExtension ( CyString filename );
 | 
        
           |  |  | 45 | 	CyStringList *GetTShipsEntries();
 | 
        
           |  |  | 46 | 	CyString GetTShipsEntry(CyString id);
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 | 	unsigned char *ReadData ( CyString filename, size_t *size );
 | 
        
           |  |  | 49 | 	unsigned char *ReadData ( SInCatFile *c, size_t *size );
 | 
        
           |  |  | 50 | 	SInCatFile *FindData ( CyString filename );
 | 
        
           |  |  | 51 | 	bool ReadFileToData ( CyString filename );
 | 
        
           |  |  | 52 | 	bool ReadFileToData ( SInCatFile *c );
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 | 	bool MarkRemoveFile ( SInCatFile *f );
 | 
        
           |  |  | 55 | 	bool MarkRemoveFile ( CyString filename );
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 | 	int GetEndOffset();
 | 
        
           |  |  | 58 | 	int GetNumFiles () { return m_lFiles.size(); }
 | 
        
           |  |  | 59 | 	SInCatFile *GetFile ( int num ) { return m_lFiles.Get(num); }
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 | 	unsigned char *UnpackFile ( SInCatFile *c, size_t *size, bool forcepck );
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 | 	static bool Opened(int error, bool allowCreate = true);
 | 
        
           |  |  | 64 | 	bool RemoveFile ( SInCatFile *f );
 | 
        
           |  |  | 65 | 	bool RemoveFile ( CyString filename );
 | 
        
           |  |  | 66 | 	bool WriteCatFile ();
 | 
        
           |  |  | 67 | 	bool AppendFile ( CyString filename, CyString to, bool pck = true, bool bXor = true );
 | 
        
           |  |  | 68 | 	bool AppendData ( unsigned char *data, size_t size, CyString to, bool pck = true, bool bXor = true );
 | 
        
           |  |  | 69 | 	bool AddData ( CyString catfile, unsigned char *data, size_t size, CyString to, bool pck = true, bool create = true );
 | 
        
           |  |  | 70 | 	void WriteFiles ();
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 | 	CyString ChangeExtension ( CyString f );
 | 
        
           |  |  | 73 | 	CLinkList<SInCatFile> *GetFiles() { return &m_lFiles; }
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 | 	bool ExtractFile ( CyString filename, CyString to = NullString, bool preserve = false );
 | 
        
           |  |  | 76 | 	bool ExtractFile ( SInCatFile *f, CyString to = NullString, bool preserve = false );
 | 
        
           |  |  | 77 | 	bool ExtractAll (CyString dir);
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 | 	void ClearError () { m_iError = CATERR_NONE; m_sError = ""; }
 | 
        
           |  |  | 80 | 	int Error () { return m_iError; }
 | 
        
           |  |  | 81 | 	CyString ErrorString() { return m_sError; }
 | 
        
           |  |  | 82 | 	CyString GetErrorString ();
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 | 	bool WriteFromCat ( CyString catfile, CyString file );
 | 
        
           |  |  | 85 | 	bool WriteFromCat ( CCatFile *fcat, CyString file );
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 | 	void FindFiles(CyStringList *list, CyString filemask);
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 | private:
 | 
        
           |  |  | 90 | 	void RemoveData ();
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 | 	CFileIO m_fCatFile;
 | 
        
           |  |  | 93 | 	CFileIO m_fDatFile;
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 | 	unsigned char *m_sData;
 | 
        
           |  |  | 96 | 	size_t m_lSize;
 | 
        
           |  |  | 97 | 	char m_iDataType;
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 | 	CLinkList<SInCatFile> m_lFiles;
 | 
        
           |  |  | 100 | 	CyString m_sError;
 | 
        
           |  |  | 101 | 	CyString m_sAddonDir;
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 | 	int m_iError;
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 | 	bool m_bCreate;
 | 
        
           |  |  | 106 | };
 | 
        
           |  |  | 107 |   | 
        
           |  |  | 108 | #endif //__CATFILE_H__
 |