/* COPYRIGHT(C) 2004 hardkoder@gmail.com / http://www.kipple.pe.kr ÀúÀÛ±Ç Á¤º¸ : - ÀÌ ¼Ò½º´Â ÀÚÀ¯·ÎÀÌ »ç¿ë/¼öÁ¤/Àç¹èÆ÷ °¡´ÉÇÕ´Ï´Ù. - ´Ü, ´ç½ÅÀÌ ¸¸µé¾ú´Ù°í ÁÖÀåÇϰųª °ÅÁþ¸»ÇÏ¸é ¾ÈµÊ. - ¼Ò½ºÀÇ ¿À·ù¸¦ ã¾Ò°Å³ª, ¹®Á¦Á¡À» ¼öÁ¤ÇÏ¿´À» °æ¿ì ÀÌ¿¡ ´ëÇÑ ³»¿ëÀ» ¾Ë·ÁÁÖ¸é Á¤¸» °í¸¶¿ï²¬.. - ÀÚ½ÅÀÇ ÇÁ·Î±×·¥¿¡ ÀÌ ¼Ò½º¸¦ »ç¿ëÇÏ¿´À» °æ¿ì ¿ø ÀúÀÛÀÚ¿¡°Ô ¾Ë·ÁÁÖ¸é ¿ø ÀúÀÛÀÚ°¡ °í¸¶¿ö ÇÒ²¬.. ¼Ò½º ¼³¸í : - .ALZ ÀÇ ¾ÐÃàÀ» ÇØÁ¦Çϱâ À§ÇÑ ¸ðµâ. - ALZ´Â BZIP2º¯Çü(º¯ÇüÀ̶ó°í ¹¹ °³¼±ÇÑ°Ô ¾Æ´Ï°í, Çì´õ³ª CRC Á¤º¸µîÀ» »©¼­ Å©±â¸¦ ÁÙÀÎ °ÍÀÓ)°ú, DEFLATE ¾ÐÃà ¾Ë°í¸®Áò¿¡ ZIP°ú À¯»çÇÑ Çì´õ¸¦ ¸¸µé¾î¼­ ¾º¿î Æ÷¸ËÀÓ. ( bzip2 ´Â 4.9x ¿¡¼­ »ç¿ëÇÏ¿´°í, deflate ´Â 5.x ºÎÅÍ »ç¿ëÇÏ¿´À½. 5.x ºÎÅÍ´Â bzip2´Â ³Ê¹« ´À·Á¼­ »ç¿ë ¾ÈÇÔ ) - UnAlzBz2decompress.c ¿Í UnAlzbzlib.c ´Â ¿ø·¡ÀÇ bzip2 ¼Ò½º¿¡¼­ alz ¿ëÀ¸·Î ¼öÁ¤µÈ ÆÄÀÏÀÓ ( deflate ´Â º¯ÇüÀÌ ¾ÈµÇ¾ú±â ¶§¹®¿¡ ±×³É zlib ¸¦ ½áµµ µÇÁö¸¸.. bzip2 ´Â º¯ÇüÀÌ µÇ¾î¼­ ¿ø·¡ÀÇ bzip2 ¼Ò½º¸¦ ±×´ë·Î ¾²¸é ¾ÈµÈ´Ù. ) - ÀÌ ¼Ò½º´Â 4Ä­ ÅÇÀ» »ç¿ë ÇÏ¿´À½. °³¹ß ¼ø¼­ : 2004/02/06 - http://www.wotsit.org/ ¿¡¼­ ZIP File Format Specification version 4.5 [PKWARE Inc.] ¸¦ ´Ù¿î·Îµå ¹Þ¾Æ¼­ ºÐ¼®. 2004/02/07 - µµÅ¥¸ÕÆ®·Î unzip Ŭ·¡½º ´ëÃæ ±¸Çö 2004/02/08 - unzip Ŭ·¡½º¸¦ alzip Æ÷¸Ë¿¡ ¸ÂÃß¾î º¯Çü ¹× Å×½ºÆ® - deflate, rawdata ¾ÐÃà ÇØÁ¦ ±¸Çö. - ´ÙÀÌ¾Ë·Î±× ¹Ú½º ²®µ¥±â ¾º¿ò. 2004/02/08 - bzip2 ´ëÃæ Áö¿ø 2004/03/01 - bzip2 °ÅÀÇ ¿Ïº® Áö¿ø Ò¬ - callback ±¸Çö.. 2004/03/07 - À¯Æ¿ ÇÔ¼ö Ãß°¡ ( ExtractCurrentFileToBuf() ) 2004/10/03 - ºÐÇÒ ¾ÐÃà ÇØÁ¦ ±â´É Ãß°¡ ( FILE I/O ¿¡ ´ëÇÑ ·¡ÆÛ Å¬·¡½º ±¸Çö ) - 2GB ÀÌ»óÀÇ ÆÄÀÏ Ã³¸® Áö¿ø (WINDOWS ONLY) 2004/10/22 - ´ÙÁß Ç÷§Æû(BSD/LINUX)Áö¿øÀ» À§ÇÑ ¼öÁ¤ ( BSD/LINUX ÀÇ °æ¿ì 2GB ÀÌÇÏÀÇ ÆÄÀϸ¸ Áö¿ø ) 2004/10/23 - by xxfree86 : DARWIN ÄÄÆÄÀÏ Áö¿ø, °æ·Î¸í¿¡ "\\" Æ÷ÇԽà ¹®Á¦Á¡ ¼öÁ¤ 2004/10/24 - by aqua0125 : ÄÚµåÆäÀÌÁö º¯È¯Ã³¸®, 64bit ÆÄÀÏ Ã³¸® - ºò¿£µð¾È, ÄÚµåÆäÀÌÁö º¯È¯ °ü·Ã ¼Ò½º Á¤¸® 2004/10/25 - by yongari : __LP64__ , ºò¿£µð¾È(le64toh/le132oh/le16toh) °ü·Ã À̽´ ¼öÁ¤ 2004/10/26 - BSD/LINUX : byte-order, libiconv À̽´ Á¤¸® 2004/10/30 - Á¤¸® & Á¤¸®.. ÇÒÀÏ : ( * Ç¥´Â ÇÑ°Å ) - bzip2 ·Î ¾ÐÃàµÈ ÆÄÀÏÀÇ ¾ÐÃà ÇØÁ¦ * - UI °³¼± ( PROGRESS, ¿¡·¯ ¸Þ½ÃÁö Ãâ·Â, ¾ÐÃà ÇØÁ¦ °á°ú ) * - ºÐÇÒ ¾ÐÃà ÆÄÀÏ Áö¿ø * - ¾ÏÈ£ °É¸° ÆÄÀÏ Áö¿ø - ¼Õ»óµÈ ÆÄÀÏ¿¡¼­ÀÇ Á»´õ ¸¹Àº Å×½ºÆ® - ÆÄÀÏ CRC üũ ÄÄÆÄÀÏ ¿É¼Ç ( -DXXXX ) - _WIN32 : WIN32 - _UNALZ_ICONV : iconv ¸¦ »ç¿ëÇؼ­ code ÆäÀÌÁö º¯È¯ Áö¿ø - _UNALZ_UTF8 : _UNALZ_ICONV ¸¦ »ç¿ëÇÒ °æ¿ì ±âº» ÄÚµåÆäÀÌÁö¸¦ "UTF-8" ·Î ÁöÁ¤ */ #ifndef _UNALZ_H_ #define _UNALZ_H_ #include #include using namespace std; #ifndef INT64 #ifdef _WIN32 # define INT64 __int64 #else # define INT64 long long #endif #endif #ifndef UINT64 #ifdef _WIN32 # define UINT64 unsigned __int64 #else # define UINT64 unsigned long long #endif #endif #ifndef UINT32 typedef unsigned int UINT32; #endif #ifndef UINT16 typedef unsigned short UINT16; #endif #ifndef SHORT typedef short SHORT; #endif #ifndef BYTE typedef unsigned char BYTE; #endif #ifndef CHAR typedef char CHAR; #endif #ifndef BYTE typedef unsigned char BYTE; #endif #ifndef UINT typedef unsigned int UINT; #endif #ifndef LONG typedef long LONG; #endif #ifndef BOOL typedef int BOOL; #endif #ifndef FALSE # define FALSE 0 #endif #ifndef TRUE # define TRUE 1 #endif #ifndef LPCTSTR typedef const char* LPCSTR; #endif #ifndef HANDLE # ifdef _WIN32 typedef void *HANDLE; # else typedef FILE *HANDLE; # endif #endif #ifndef ASSERT # include //# define ASSERT(x) assert(x) # define ASSERT(x) {printf("assert file:%s line:%d\n", __FILE__, __LINE__);} #endif namespace UNALZ { #ifdef _WIN32 # pragma pack(push, UNALZ, 1) // structure packing #else # pragma pack(1) #endif static const char UNALZ_VERSION[] = "CUnAlz0.23"; static const char UNALZ_COPYRIGHT[] = "Copyright(C) 2004 hardkoder@gmail.com"; // ¸Ç ÆÄÀÏ ¾Õ.. struct SAlzHeader { UINT32 unknown; // ?? }; /* union _UGeneralPurposeBitFlag // zip ¿¡¼­¸¸ »ç¿ë.. { SHORT data; struct { BYTE bit0 : 1; BYTE bit1 : 1; BYTE bit2 : 1; BYTE bit3 : 1; BYTE bit4 : 1; BYTE bit5 : 1; }; }; */ enum COMPRESSION_METHOD ///< ¾ÐÃà ¹æ¹ý.. { COMP_NOCOMP = 0, COMP_BZIP2 = 1, COMP_DEFLATE = 2, }; struct _SLocalFileHeaderHead ///< °íÁ¤ Çì´õ. { SHORT fileNameLength; BYTE unknown[5]; ///< ??? BYTE fileSizeByte; ///< ÆÄÀÏ Å©±â ÇʵåÀÇ Å©±â : 0x10, 0x20, 0x40, 0x80 °¢°¢ 1byte, 2byte, 4byte, 8byte BYTE unknown2[1]; ///< ??? /* SHORT versionNeededToExtract; _UGeneralPurposeBitFlag generalPurposeBitFlag; SHORT compressionMethod; SHORT lastModFileTime; SHORT lastModFileDate; UINT32 crc32; UINT32 compressedSize; UINT32 uncompressedSize; SHORT fileNameLength; SHORT extraFieldLength; */ }; struct _SDataDescriptor { UINT32 crc32; UINT32 compressed; UINT32 uncompressed; }; struct SLocalFileHeader { SLocalFileHeader() { memset(this, 0, sizeof(*this)); } //~SLocalFileHeader() { if(fileName) free(fileName); if(extraField) free(extraField); } void Clear() { if(fileName) free(fileName); fileName=NULL; if(extraField) free(extraField);extraField=NULL; } _SLocalFileHeaderHead head; BYTE compressionMethod; ///< ¾ÐÃà ¹æ¹ý : 2 - deflate, 1 - bzip2(?), 0 - ¾ÐÃà ¾ÈÇÔ. BYTE unknown3[1]; ///< ??? UINT32 maybeCRC; ///< ¾Æ¸¶µµ crc INT64 compressedSize; INT64 uncompressedSize; CHAR* fileName; BYTE* extraField; _SDataDescriptor dataDescriptor; INT64 dwFileDataPos; ///< file data °¡ ÀúÀåµÈ À§Ä¡.. }; struct _SCentralDirectoryStructureHead { UINT32 dwUnknown; ///< Ç×»ó NULL ÀÌ´øµ¥.. UINT32 dwMaybeCRC; ///< ¾Æ¸¶µµ crc UINT32 dwCLZ03; ///< "CLZ0x03" - 0x035a4c43 ³¡À» Ç¥½ÃÇϴµí. /* SHORT versionMadeBy; SHORT versionNeededToExtract; _UGeneralPurposeBitFlag generalPurposeBitFlag; SHORT compressionMethod; SHORT lastModFileTime; SHORT lastModFileDate; UINT32 crc32; UINT32 compressedSize; UINT32 uncompressedSize; SHORT fileNameLength; SHORT extraFieldLength; SHORT fileCommentLength; SHORT diskNumberStart; SHORT internalFileAttributes; UINT32 externalFileAttributes; UINT32 relativeOffsetOfLocalHeader; */ }; struct SCentralDirectoryStructure { SCentralDirectoryStructure() { memset(this, 0, sizeof(*this)); } //~SCentralDirectoryStructure() { if(fileName) free(fileName); if(extraField) free(extraField);if(fileComment)free(fileComment); } _SCentralDirectoryStructureHead head; /* CHAR* fileName; BYTE* extraField; CHAR* fileComment; */ }; /* struct _SEndOfCentralDirectoryRecordHead { SHORT numberOfThisDisk; SHORT numberOfTheDiskWithTheStartOfTheCentralDirectory; SHORT centralDirectoryOnThisDisk; SHORT totalNumberOfEntriesInTheCentralDirectoryOnThisDisk; UINT32 sizeOfTheCentralDirectory; UINT32 offsetOfStartOfCentralDirectoryWithREspectoTotheStartingDiskNumber; SHORT zipFileCommentLength; }; */ /* struct SEndOfCentralDirectoryRecord { SEndOfCentralDirectoryRecord() { memset(this, 0, sizeof(*this)); } ~SEndOfCentralDirectoryRecord() { if(fileComment) free(fileComment); } _SEndOfCentralDirectoryRecordHead head; CHAR* fileComment; }; */ #ifdef _WIN32 # pragma pack(pop, UNALZ) ///< PACKING ¿ø»ó º¹±¸ #else # ifdef __LP64__ // 63bit ´Â 8byte ¸Â³ª ? À߸𸣰ڴÙ.. # pragma pack(8) # else # pragma pack(4) # endif #endif ///< PROGRESS CALLBACK FUNCTION - ¾ÐÃà ÇØÁ¦ ÁøÇà »óȲÀ» ¾Ë°í ½ÍÀ¸¸é ÀÌ°É ¾²¸é µÈ´Ù. typedef void (_UnAlzCallback)(const char* szMessage, INT64 nCurrent, INT64 nRange, void* param, BOOL* bHalt); class CUnAlz { public: CUnAlz(); ~CUnAlz(); BOOL Open(const char* szPathName); void Close(); BOOL SetCurrentFile(const char* szFileName); BOOL ExtractCurrentFile(const char* szDestPathName, const char* szDestFileName=NULL); BOOL ExtractCurrentFileToBuf(BYTE* pDestBuf, int nBufSize); BOOL ExtractAll(const char* szDestPathName); void SetCallback(_UnAlzCallback* pFunc, void* param=NULL); #ifdef _UNALZ_ICONV void SetDestCodepage(const char* szToCodepage) { strcpy(m_szToCodepage, szToCodepage); } #endif public : ///< WIN32 Àü¿ë ( UNICODE 󸮿ë ) #ifdef _WIN32 #ifndef __GNUWIN32__ #ifndef LPCWSTR typedef const wchar_t* LPCWSTR; #endif BOOL Open(LPCWSTR szPathName); BOOL SetCurrentFile(LPCWSTR szFileName); static BOOL IsFolder(LPCWSTR szPathName); #endif // __GNUWIN32__ #endif // _WIN32 public : typedef vector FileList; ///< ÆÄÀÏ ¸ñ·Ï. const FileList& GetFileList() { return m_fileList; }; ///< file ¸ñ·Ï ¸®ÅÏ FileList::iterator GetCurFileHeader() { return m_posCur; }; ///< ÇöÀç (SetCurrentFile() ·Î ¼¼ÆõÈ) ÆÄÀÏ Á¤º¸ // const SLocalFileHeader* GetCurFileHeader() { return m_posCur; }; ///< ÇöÀç (SetCurrentFile() ·Î ¼¼ÆõÈ) ÆÄÀÏ Á¤º¸ public : enum ERR ///< ¿¡·¯ ÄÚµå - Á¤¸® ÇÊ¿ä.. { ERR_NOERR, ERR_CANT_OPEN_FILE, ///< ÆÄÀÏ ¿­±â ½ÇÆÐ ERR_CANT_READ_SIG, ///< signature Àб⠽ÇÆÐ ERR_CANT_READ_FILE, ERR_AT_READ_HEADER, ERR_INVALID_FILENAME_LENGTH, ERR_INVALID_EXTRAFIELD_LENGTH, ERR_CANT_READ_CENTRAL_DIRECTORY_STRUCTURE_HEAD, ERR_INVALID_FILENAME_SIZE, ERR_INVALID_EXTRAFIELD_SIZE, ERR_INVALID_FILECOMMENT_SIZE, ERR_CANT_READ_HEADER, ERR_MEM_ALLOC_FAILED, ERR_FILE_READ_ERROR, ERR_INFLATE_FAILED, ERR_ICONV_CANT_OPEN, ERR_ICONV_INVALID_MULTISEQUENCE_OF_CHARACTERS, ERR_ICONV_INCOMPLETE_MULTIBYTE_SEQUENCE, ERR_ICONV_NOT_ENOUGH_SPACE_OF_BUFFER_TO_CONVERT, ERR_ICONV_ETC, }; ERR GetLastErr(){return m_nErr;} const char* GetLastErrStr(){return LastErrToStr(m_nErr);} const char* LastErrToStr(ERR nERR); enum SIGNATURE ///< zip file signature - little endian { SIG_ERROR = 0x00, SIG_EOF = 0x01, SIG_ALZ_FILE_HEADER = 0x015a4c41, ///< ALZ 0x01 SIG_LOCAL_FILE_HEADER = 0x015a4c42, ///< BLZ 0x01 SIG_CENTRAL_DIRECTORY_STRUCTURE = 0x015a4c43, ///< CLZ 0x01 SIG_ENDOF_CENTRAL_DIRECTORY_RECORD = 0x025a4c43, ///< CLZ 0x02 }; public : static BOOL DigPath(const char* szPathName); static BOOL IsFolder(LPCSTR szPathName); static const char* GetVersion() { return UNALZ_VERSION; } static const char* GetCopyright() { return UNALZ_COPYRIGHT; } private : SIGNATURE ReadSignature(); BOOL ReadAlzFileHeader(); BOOL ReadLocalFileheader(); BOOL ReadCentralDirectoryStructure(); BOOL ReadEndofCentralDirectoryRecord(); private : enum EXTRACT_TYPE ///< ¾ÐÃà ÇØÁ¦ ŸÀÔ. { ET_FILE, ///< FILE* ET_MEM, ///< memory buffer }; struct SExtractDest ///< ¾ÐÃà ÇØÁ¦ ´ë»ó. { SExtractDest() { memset(this, 0, sizeof(SExtractDest)); } EXTRACT_TYPE nType; ///< ´ë»óÀÌ ÆÄÀÏÀΰ¡ ¸Þ¸ð¸® Àΰ¡.. FILE* fp; ///< ET_FILE ÀÏ °æ¿ì ´ë»ó FILE* BYTE* buf; ///< ET_MEM ÀÏ °æ¿ì ´ë»ó Æ÷ÀÎÅÍ UINT32 bufsize; ///< ET_MEM ÀÏ °æ¿ì ´ë»ó ¹öÆÛÀÇ Å©±â UINT32 bufpos; ///< ET_MEM ÀÏ °æ¿ì ´ë»ó ¹öÆÛ¿¡ ¾²°í ÀÖ´Â À§Ä¡ }; int WriteToDest(SExtractDest* dest, BYTE* buf, int nSize); private : BOOL ExtractTo(SExtractDest* dest); //BOOL ExtractDeflate(FILE* fp, SLocalFileHeader& file); //BOOL ExtractBzip2_bak(FILE* fp, SLocalFileHeader& file); - ½ÇÆÐÇÑ(À߸øµÈ) ¹æ¹ý BOOL ExtractDeflate2(SExtractDest* dest, SLocalFileHeader& file); BOOL ExtractBzip2(SExtractDest* dest, SLocalFileHeader& file); BOOL ExtractRawfile(SExtractDest* dest, SLocalFileHeader& file); private : // bzip2 ÆÄÀÏ Ã³¸® ÇÔ¼ö.. typedef void MYBZFILE; MYBZFILE* BZ2_bzReadOpen(int* bzerror, CUnAlz* f, int verbosity, int _small, void* unused, int nUnused); int BZ2_bzread(MYBZFILE* b, void* buf, int len ); int BZ2_bzRead(int* bzerror, MYBZFILE* b, void* buf, int len); void BZ2_bzReadClose( int *bzerror, MYBZFILE *b ); private : // ºÐÇÒ ¾ÐÃà ÆÄÀÏ Ã³¸®¸¦ À§ÇÑ ·¡ÆÛ(lapper?) Ŭ·¡½º BOOL FOpen(const char* szPathName); void FClose(); INT64 FTell(); BOOL FEof(); BOOL FSeek(INT64 offset); BOOL FRead(void* buffer, UINT32 nBytesToRead, int* pTotRead=NULL); enum {MAX_FILES=1000}; ///< ó¸® °¡´ÉÇÑ ºÐÇÒ ¾ÐÃà ÆÄÀÏ ¼ö. enum {MULTIVOL_TAIL_SIZE=16,MULTIVOL_HEAD_SIZE=8}; ///< ºÐÇÒ ¾ÐÃà½Ã ²Ã¶ûÁö, Çì´õ Å©±â struct SFile ///< ºÐÇÒ ÆÄÀÏ Á¤º¸ { HANDLE fp; INT64 nFileSize; int nMultivolHeaderSize; int nMultivolTailSize; }; SFile m_files[MAX_FILES]; ///< ºÐÇÒ ÆÄÀÏ ÀúÀå array - ¹«½ÄÇÑ°¡? int m_nCurFile; ///< m_files ¿¡¼­ ÇöÀç ó¸®ÁßÀÎ ÆÄÀÏÀÇ À§Ä¡. int m_nFileCount; ///< ºÐÇÒ ÆÄÀÏ °¹¼ö.. INT64 m_nVirtualFilePos; ///< ¸ÖƼº¼·ý¿¡¼­ÀÇ °¡»óÀÇ À§Ä¡ INT64 m_nCurFilePos; ///< ÇöÀç ÆÄÀÏÀÇ ¹°¸®Àû À§Ä¡. BOOL m_bIsEOF; ///< ÆÄÀÏÀÇ ³¡±îÁö (ºÐÇÒ ÆÄÀÏ Æ÷ÇÔÇؼ­) ¿Ô³ª? private : FileList m_fileList; ///< ¾ÐÃàÆÄÀÏ ³»ÀÇ ÆÄÀÏ ¸ñ·Ï ERR m_nErr; FileList::iterator m_posCur; ///< ÇöÀç ÆÄÀÏ _UnAlzCallback* m_pFuncCallBack; void* m_pCallbackParam; BOOL m_bHalt; #ifdef _UNALZ_ICONV char m_szToCodepage[256]; ///< codepage char m_szFromCodepage[256]; ///< "CP949" #endif }; } using namespace UNALZ; #endif