Artifact Content

Not logged in

Artifact 6f288846948232f7920dd1a4ba3c3368554bef0e


     1  // Archiver.h
     2  //-- CArchiver -- common interface in 'Noah' for arhiving routine --
     3  
     4  #ifndef AFX_ARCHIVER_H__359A2ED3_2F97_480E_BC94_24834EBA6498__INCLUDED_
     5  #define AFX_ARCHIVER_H__359A2ED3_2F97_480E_BC94_24834EBA6498__INCLUDED_
     6  
     7  enum {
     8  	aCheck = 1, aMelt = 2, aList = 4, aMeltEach = 8, aCompress = 16, aArchive = 32, aSfx = 64,
     9  };
    10  enum {
    11  	aUnknown=0, aSingleFile, aSingleDir, aMulti
    12  };
    13  
    14  struct arcname {
    15  	arcname( const kiPath& b,const char *s,const char *l )
    16  		: basedir(b),sname(s),lname(l) {}
    17  	const kiPath& basedir;
    18  	const char*   lname;
    19  	const char*   sname;
    20  };
    21  
    22  struct arcfile {
    23  	INDIVIDUALINFO inf;
    24  	union {
    25  		bool selected;
    26  		bool isfile;
    27  	};
    28  };
    29  
    30  #define aflArray kiArray<arcfile>
    31  #define wfdArray kiArray<WIN32_FIND_DATA>
    32  
    33  class CArchiver
    34  {
    35  public: //--< attribute >--
    36  
    37  	int ability();
    38  	int cancompressby( const char* ext, const char* mhd, bool sfx );
    39  
    40  	const char*     mlt_ext();
    41  	const kiStr&    cmp_ext();
    42  	const StrArray& cmp_mhd_list();
    43  	const int       cmp_mhd_default();
    44  	bool            ver( kiStr& str );
    45  	static bool GetVersionInfoStr( char* name, char* buf, size_t cbBuf );
    46  
    47  public: //--< action >--
    48  
    49  	bool check( const kiPath& aname );
    50  	int  contents( const kiPath& aname, kiPath& dname );
    51  	int  melt( const arcname& aname, const kiPath& ddir, const aflArray* files=NULL );
    52  	bool list( const arcname& aname, aflArray& files );
    53  	int  compress( const kiPath& base, const wfdArray& files, const kiPath& ddir, int method, bool sfx );
    54  	kiStr arctype_name(const char* an) const { return v_name(an); }
    55  
    56  protected: //--< for child >--
    57  
    58  	CArchiver( const char* mext );
    59  	void set_cmp_ext( const char* ext );
    60  	void add_cmp_mhd( const char* mhd, bool def=false );
    61  
    62  	virtual int  v_load(){return 0;}
    63  	virtual bool v_ver( kiStr& str ){return false;}
    64  	virtual bool v_check( const kiPath& aname ){return false;}
    65  	virtual int  v_contents( const kiPath& aname, kiPath& dname ){return aUnknown;}
    66  	virtual int  v_melt( const arcname& aname, const kiPath& ddir, const aflArray* files ){return false;}
    67  	virtual bool v_list( const arcname& aname, aflArray& files ){return false;}
    68  	virtual int  v_compress( const kiPath& base, const wfdArray& files, const kiPath& ddir, int method, bool sfx ){return false;}
    69  	virtual kiStr v_name(const char*) const { return ""; }
    70  
    71  private: //--< private >--
    72  
    73  	friend class CNoahArchiverManager;
    74  	bool extCheck( const char* ext );
    75  	kiStr m_MyExtList, m_MyCmpExt;
    76  	StrArray m_Mhd;
    77  	int m_MhdDef,m_Able;
    78  	bool not_loaded;
    79  
    80  public: //--< dummy >--
    81  
    82  	virtual ~CArchiver(){}
    83  };
    84  
    85  inline int CArchiver::ability()
    86  {
    87  	if( not_loaded )
    88  		m_Able=v_load(), not_loaded=false;
    89  	return m_Able;
    90  }
    91  
    92  inline int CArchiver::cancompressby( const char* ext, const char* mhd, bool sfx )
    93  {
    94  	if( not_loaded )
    95  		m_Able=v_load(), not_loaded=false;
    96  	if( (sfx && !(m_Able&aSfx)) || !(m_Able&aCompress) || !m_MyCmpExt.isSame(ext) )
    97  		return -1; // no
    98  	for( unsigned int i=0; i!=m_Mhd.len(); i++ )
    99  		if( m_Mhd[i] == mhd )
   100  			return (int)i;
   101  	return -2; // only - 'type name' matched
   102  }
   103  
   104  inline bool CArchiver::check( const kiPath& aname )
   105  {
   106  	if( not_loaded )
   107  		m_Able=v_load(), not_loaded=false;
   108  	return (m_Able&aCheck)?v_check(aname):false;
   109  }
   110  
   111  inline int CArchiver::contents( const kiPath& aname, kiPath& dname )
   112  {
   113  	if( not_loaded )
   114  		m_Able=v_load(), not_loaded=false;
   115  	return (m_Able&aList)?v_contents(aname,dname):false;
   116  }
   117  
   118  inline int CArchiver::melt( const arcname& aname, const kiPath& ddir, const aflArray* files )
   119  {
   120  	if( not_loaded )
   121  		m_Able=v_load(), not_loaded=false;
   122  	return (m_Able&aMelt)?v_melt(aname,ddir,files):0xffff;
   123  }
   124  
   125  inline bool CArchiver::list( const arcname& aname, aflArray& files )
   126  {
   127  	if( not_loaded )
   128  		m_Able=v_load(), not_loaded=false;
   129  	return (m_Able&aList)?v_list(aname,files):false;
   130  }
   131  
   132  inline int CArchiver::compress( const kiPath& base, const wfdArray& files, const kiPath& ddir, int method, bool sfx )
   133  {
   134  	if( not_loaded )
   135  		m_Able=v_load(), not_loaded=false;
   136  	return (m_Able&aCompress)?v_compress(base,files,ddir,method,sfx):0xffff;
   137  }
   138  
   139  inline bool CArchiver::ver( kiStr& str )
   140  {
   141  	if( not_loaded )
   142  		m_Able=v_load(), not_loaded=false;
   143  	return v_ver(str);
   144  }
   145  
   146  inline const char* CArchiver::mlt_ext()
   147  {
   148  	return m_MyExtList;
   149  }
   150  
   151  inline const kiStr& CArchiver::cmp_ext()
   152  {
   153  	if( not_loaded )
   154  		m_Able=v_load(), not_loaded=false;
   155  	return m_MyCmpExt;
   156  }
   157  
   158  inline const StrArray& CArchiver::cmp_mhd_list()
   159  {
   160  	if( not_loaded )
   161  		m_Able=v_load(), not_loaded=false;
   162  	return m_Mhd;
   163  }
   164  
   165  inline const int CArchiver::cmp_mhd_default()
   166  {
   167  	if( not_loaded )
   168  		m_Able=v_load(), not_loaded=false;
   169  	return m_MhdDef;
   170  }
   171  
   172  inline CArchiver::CArchiver( const char* extlist )
   173  		: m_MyExtList( extlist ), m_Mhd(3), m_MhdDef(0), not_loaded(true)
   174  {
   175  	m_MyExtList.lower();
   176  }
   177  
   178  inline void CArchiver::set_cmp_ext( const char* ext )
   179  {
   180  	m_MyCmpExt = ext;
   181  }
   182  
   183  inline void CArchiver::add_cmp_mhd( const char* method, bool Default )
   184  {
   185  	m_Mhd.add(method);
   186  	if( Default )
   187  		m_MhdDef = m_Mhd.len() - 1;
   188  }
   189  
   190  inline bool CArchiver::extCheck( const char* ext )
   191  {
   192  	const char *x=m_MyExtList,*y;
   193  	int ln = ki_strlen(ext);
   194  	while( *x )
   195  	{
   196  		for( y=x+1; *y && *y!='.'; y++ );
   197  		if( *y=='\0' ) break;
   198  
   199  		if( y-x == ln )
   200  		{
   201  			while( x!=y )
   202  			{
   203  				if( *x!=ext[ln+(x-y)] )
   204  					break;
   205  				x++;
   206  			}
   207  			if( x==y )
   208  				return true;
   209  		}
   210  		x=y+1;
   211  	}
   212  	return false;
   213  }
   214  
   215  // 渡されたパス文字列が、絶対パスや".."を含んでいればtrue
   216  static bool containsDangerPath( const char* path )
   217  {
   218  	// 絶対パス
   219  	if( path[0]=='\\' || path[0]=='/' || path[0]!='\0' && path[1]==':' )
   220  		return true;
   221  
   222  	// ".."
   223  	for( const char* p=path; *p; )
   224  	{
   225  		const char* q = p;
   226  		while( *q!='\0' && *q!='\\' && *q!='/' )
   227  			q = ::CharNext(q);
   228  
   229  		if( p+2 <= q )
   230  		{
   231  			const char* r;
   232  			for( r=p; r!=q; ++r )
   233  				if( *r != '.' )
   234  					break;
   235  			if( r == q ) // all dot
   236  				return true;
   237  		}
   238  		p = (*q ? ::CharNext(q) : q);
   239  	}
   240  	return false;
   241  }
   242  
   243  /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
   244  // 統合アーカイバDLLレイヤ第二層 ver2。ついでにexeも適当に動く
   245  
   246  class CArcModule
   247  {
   248  public:
   249  
   250  	// 実行コマンド名を指定して作成
   251  	//  ・ファイル名として探して見つからなかった場合
   252  	//    シェルの内部コマンドと仮定して一応保持しておく
   253  	//  ・拡張子が exe か com なら実行ファイルとして扱う。
   254  	//  ・それ以外ならアーカイバDLLとして扱う。
   255  	CArcModule( const char* name, bool us=false );
   256  	virtual ~CArcModule();
   257  	bool exist();
   258  	bool isdll();
   259  
   260  	kiStr name() const { return kiPath::name(m_name); }
   261  
   262  	// 実行
   263  	int cmd( const char* cmd, bool mini=false );
   264  
   265  	// 書庫チェック
   266  	bool chk( const char* aname );
   267  	int  arctype( const char* aname ) const;
   268  
   269  	// バージョン情報文字列を返す
   270  	void ver( kiStr& str );
   271  
   272  	// 書庫の中身をリストアップ
   273  	bool lst_dll( const arcname& aname, aflArray& files, const char* wild="*" );
   274  	int  cnt( const kiPath& aname, kiPath& dname, const char* wild="*" );
   275  	bool lst_exe( const char* lstcmd, aflArray& files,
   276  		const char* BL, int BSL, const char* EL, int SL, int dx );
   277  
   278  	// DLL強制アンロード( BugTrap for UnZip32.dll )
   279  	void unload() { m_dll->unload(); }
   280  	// DLLオーナー窓設定( BugTrap for Unrar.dll )
   281  	void own( HWND wnd ) { m_dll->setOwner( wnd ); }
   282  	void fre() { m_dll->clearOwner(); }
   283  
   284  private:
   285  	enum { NOTEXIST, DLL, DLLGCA, DLLBGA, EXE, EXEUS, SHLCMD=0 } m_type;
   286  	kiArcDLLRaw* m_dll;
   287  	char         m_name[MAX_PATH];
   288  	const char*  m_wild;
   289  };
   290  
   291  inline bool CArcModule::chk( const char* aname )
   292  	{ return m_dll ? FALSE!=m_dll->check( aname, m_type==DLLGCA?24:0 ) : false; }
   293  
   294  inline int CArcModule::arctype( const char* aname ) const
   295  	{ return m_dll ?
   296  		m_type==DLLBGA ? m_dll->check(aname,0) : m_dll->getArcType(aname)
   297  	  : 0; }
   298  
   299  inline bool CArcModule::exist()
   300  	{ return m_type!=NOTEXIST; }
   301  
   302  inline bool CArcModule::isdll()
   303  	{ return m_type==DLL || m_type==DLLGCA; }
   304  
   305  
   306  
   307  #endif