| 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,
 | 
        
           | 51 | cycrow | 10 | 		CATERR_CREATED, CATERR_MALLOC};
 | 
        
           | 1 | cycrow | 11 | enum {CATFILE_NONE, CATFILE_READ, CATFILE_DECYPTED};
 | 
        
           |  |  | 12 | enum {CATREAD_JUSTCONTENTS, CATREAD_CAT, CATREAD_CATDECRYPT, CATREAD_DAT};
 | 
        
           |  |  | 13 |   | 
        
           |  |  | 14 | typedef struct SInCatFile {
 | 
        
           | 65 | cycrow | 15 | 	SInCatFile () { lSize = 0; sData = 0; lOffset = 0; bDelete = false; bDecrypted = false; }
 | 
        
           | 197 | cycrow | 16 | 	Utils::WString sFile;
 | 
        
           | 1 | cycrow | 17 | 	size_t lSize;
 | 
        
           |  |  | 18 | 	unsigned char  *sData;
 | 
        
           |  |  | 19 | 	size_t lOffset;
 | 
        
           |  |  | 20 | 	bool	bDelete;
 | 
        
           | 41 | cycrow | 21 | 	bool	bDecrypted;
 | 
        
           | 1 | cycrow | 22 | } SInCatFile;
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 | unsigned char SPKEXPORT *PCKData ( unsigned char *data, size_t oldsize, size_t *newsize, bool bXor = true );
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | class SPKEXPORT CCatFile
 | 
        
           |  |  | 27 | {
 | 
        
           |  |  | 28 | public:
 | 
        
           | 41 | cycrow | 29 | 	enum CatFiletype {
 | 
        
           |  |  | 30 | 		FILETYPE_PLAIN,
 | 
        
           |  |  | 31 | 		FILETYPE_DEFLATE,
 | 
        
           |  |  | 32 | 		FILETYPE_PCK,
 | 
        
           |  |  | 33 | 	};
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 | public:
 | 
        
           | 197 | cycrow | 36 | 	static Utils::WString PckChangeExtension(const Utils::WString &f);
 | 
        
           |  |  | 37 | 	static Utils::WString RenameFileExtension(SInCatFile *f);
 | 
        
           |  |  | 38 | 	static bool IsAddonDir(const Utils::WString &dir);
 | 
        
           |  |  | 39 | 	static bool CheckPackedExtension(const Utils::WString &sFilename);
 | 
        
           | 1 | cycrow | 40 |   | 
        
           | 85 | cycrow | 41 | public:
 | 
        
           | 1 | cycrow | 42 | 	CCatFile ();
 | 
        
           |  |  | 43 | 	~CCatFile ();
 | 
        
           |  |  | 44 |   | 
        
           | 197 | cycrow | 45 | 	int  open(const Utils::WString &catfile, const Utils::WString &addon, int readtype = CATREAD_CATDECRYPT, bool create = true);
 | 
        
           | 1 | cycrow | 46 | 	bool DecryptData ();
 | 
        
           |  |  | 47 | 	bool DecryptData ( unsigned char *data, size_t size );
 | 
        
           |  |  | 48 | 	void DecryptDAT(unsigned char *buffer, size_t size);
 | 
        
           | 41 | cycrow | 49 | 	void DecryptDAT(SInCatFile *pFile);
 | 
        
           | 85 | cycrow | 50 | 	bool readFiles ();
 | 
        
           | 1 | cycrow | 51 | 	void LoadDatFile ();
 | 
        
           |  |  | 52 |   | 
        
           | 197 | cycrow | 53 | 	bool checkExtensionPck(const Utils::WString &filename) const;
 | 
        
           | 1 | cycrow | 54 |   | 
        
           | 197 | cycrow | 55 | 	unsigned char *readData(const Utils::WString &filename, size_t *size);
 | 
        
           | 181 | cycrow | 56 | 	unsigned char *readData(SInCatFile *c, size_t *size);
 | 
        
           | 197 | cycrow | 57 | 	SInCatFile *findData(const Utils::WString &filename) const;
 | 
        
           |  |  | 58 | 	bool readFileToData(const Utils::WString &filename);
 | 
        
           | 181 | cycrow | 59 | 	bool readFileToData(SInCatFile *c);
 | 
        
           | 1 | cycrow | 60 |   | 
        
           | 181 | cycrow | 61 | 	bool markRemoveFile(SInCatFile *f);
 | 
        
           | 197 | cycrow | 62 | 	bool markRemoveFile(const Utils::WString &filename);
 | 
        
           | 1 | cycrow | 63 |   | 
        
           | 322 | cycrow | 64 | 	size_t endOffset() const;
 | 
        
           | 124 | cycrow | 65 | 	size_t GetNumFiles() const;
 | 
        
           | 322 | cycrow | 66 | 	SInCatFile *GetFile(size_t num) const;
 | 
        
           | 1 | cycrow | 67 |   | 
        
           | 41 | cycrow | 68 | 	unsigned char *UnpackFile ( SInCatFile *c, size_t *size);
 | 
        
           | 1 | cycrow | 69 |   | 
        
           |  |  | 70 | 	static bool Opened(int error, bool allowCreate = true);
 | 
        
           | 124 | cycrow | 71 | 	bool removeFile(SInCatFile *f);
 | 
        
           | 197 | cycrow | 72 | 	bool removeFile(const Utils::WString &filename);
 | 
        
           | 1 | cycrow | 73 | 	bool WriteCatFile ();
 | 
        
           | 197 | cycrow | 74 | 	bool appendFile(const Utils::WString &sFilename, const Utils::WString &to, bool pck = true, bool bXor = true, Utils::WString *sChangeTo = NULL);
 | 
        
           |  |  | 75 | 	bool appendData (unsigned char *data, size_t size, const Utils::WString &to, bool pck = true, bool bXor = true );
 | 
        
           |  |  | 76 | 	bool addData(const Utils::WString &catfile, unsigned char *data, size_t size, const Utils::WString &to, bool pck = true, bool create = true );
 | 
        
           | 1 | cycrow | 77 |   | 
        
           | 124 | cycrow | 78 | 	std::vector<SInCatFile *> *GetFiles() const;
 | 
        
           | 1 | cycrow | 79 |   | 
        
           | 197 | cycrow | 80 | 	bool extractFile(const Utils::WString &filename, const Utils::WString &to = Utils::WString::Null(), bool preserve = false);
 | 
        
           |  |  | 81 | 	bool extractFile(SInCatFile* f, const Utils::WString &to = Utils::WString::Null(), bool preserve = false);
 | 
        
           |  |  | 82 | 	bool extractAll(const Utils::WString &dir);
 | 
        
           | 1 | cycrow | 83 |   | 
        
           | 124 | cycrow | 84 | 	void clearError () { m_iError = CATERR_NONE; _sError.clear(); }
 | 
        
           |  |  | 85 | 	int error () const { return m_iError; }
 | 
        
           | 197 | cycrow | 86 | 	const Utils::WString &errorString() const { return _sError; }
 | 
        
           |  |  | 87 | 	Utils::WString getErrorString () const;
 | 
        
           | 85 | cycrow | 88 |   | 
        
           | 197 | cycrow | 89 | 	const Utils::WString &internalDatFilename() const;
 | 
        
           | 196 | cycrow | 90 | 	const Utils::WString &catFilename() const { return m_fCatFile.fullFilename(); }
 | 
        
           |  |  | 91 | 	const Utils::WString &datFilename() const { return m_fDatFile.fullFilename(); }
 | 
        
           | 85 | cycrow | 92 |   | 
        
           | 197 | cycrow | 93 | 	bool writeFromCat(const Utils::WString &catfile, const Utils::WString &file);
 | 
        
           |  |  | 94 | 	bool writeFromCat(CCatFile* fcat, const Utils::WString &file);
 | 
        
           | 1 | cycrow | 95 |   | 
        
           | 197 | cycrow | 96 | 	void findFiles(Utils::WStringList &list, const Utils::WString &filemask) const;
 | 
        
           | 1 | cycrow | 97 |   | 
        
           |  |  | 98 | private:
 | 
        
           | 85 | cycrow | 99 | 	void _clearFiles();
 | 
        
           | 41 | cycrow | 100 | 	int _checkFiletype(const unsigned char *pBuffer, int iSize);
 | 
        
           | 1 | cycrow | 101 | 	void RemoveData ();
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 | 	CFileIO m_fCatFile;
 | 
        
           |  |  | 104 | 	CFileIO m_fDatFile;
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 | 	unsigned char *m_sData;
 | 
        
           |  |  | 107 | 	size_t m_lSize;
 | 
        
           |  |  | 108 | 	char m_iDataType;
 | 
        
           |  |  | 109 |   | 
        
           | 124 | cycrow | 110 | 	std::vector<SInCatFile *> *_lFiles;
 | 
        
           | 197 | cycrow | 111 | 	Utils::WString _sError;
 | 
        
           |  |  | 112 | 	Utils::WString _sAddonDir;
 | 
        
           | 1 | cycrow | 113 |   | 
        
           | 197 | cycrow | 114 | 	Utils::WString _sReadFilename;
 | 
        
           | 1 | cycrow | 115 | 	int m_iError;
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 | 	bool m_bCreate;
 | 
        
           | 53 | cycrow | 118 | 	bool m_bCatChanged;
 | 
        
           | 1 | cycrow | 119 | };
 | 
        
           |  |  | 120 |   | 
        
           |  |  | 121 | #endif //__CATFILE_H__
 |