home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2003 May / VPR0305.ISO / OLS / TAR32223 / tar32223.lzh / tar32_2 / src / arcfile.cpp < prev    next >
C/C++ Source or Header  |  2003-01-17  |  4KB  |  128 lines

  1. /*
  2.     arcfile.cpp
  3.         archive file input/output virtual class.
  4.         by Yoshioka Tsuneo(QWF00133@nifty.ne.jp)
  5. */
  6. /*    
  7.     このファイルの利用条件:
  8.         このソースファイルの利用制限は一切ありません。
  9.         ソースの一部、全部を商用、非商用など目的に
  10.         かかわりなく他のプログラムで自由に使用できます。
  11.         パブリック・ドメイン・ソフトウェアと同様に扱えます。
  12.     
  13.     プログラマ向けの要望(制限ではありません):
  14.         ソース中に改善すべき点があればお知らせください。
  15.         ソースコード中にバグを見つけた場合は報告してください。
  16.         直した部分などありましたら教えてください。
  17.         断片的な情報でも結構です。
  18.         このファイルを利用した場合はなるべく教えてください。
  19. */
  20. /*
  21.     LICENSE of this file:
  22.         There is no restriction for using this file.
  23.         You can use this file in your software for any purpose.
  24.         In other words, you can use this file as Public Domain Software.
  25.  
  26.     RECOMMENDATION for Programmer(not restriction):
  27.         If you find points to improve code, please report me.
  28.         If you find bug in source code, please report me.
  29.         If you fixed bug, please teach me.
  30.         I want any trivial information.
  31.         If you use this file, please report me.
  32. */
  33. #include "arcfile.h"
  34.  
  35. #include "normal.h" // CTarArcFile_Normal
  36. #include "arcgz.h"    // CTarArcFile_GZip
  37. #include "arcbz2.h"    // CTarArcFile_BZip2
  38. #include "arcz.h"    // CTarArcFile_Compress
  39. #include "tar32api.h"
  40.  
  41. #include <string.h>
  42. #include <assert.h>
  43.  
  44. #include <algorithm>
  45. using namespace std;
  46.  
  47. int ITarArcFile::seek(int offset, int origin)
  48. {
  49.     if(origin != SEEK_CUR){
  50.         return -1;
  51.     }
  52.     while(offset > 0){
  53.         // char buf[4096];
  54.         char buf[1000];
  55.         int size = min(offset,(int)sizeof(buf));
  56.         int n = read(buf,size);
  57.         if(n != size){return -1;}
  58.         offset -= n;
  59.     }
  60.     return 0;
  61. }
  62. /*static*/
  63. ITarArcFile *ITarArcFile::s_open(const char *arcfile, const char *mode, int type)
  64. {
  65.     ITarArcFile *pfile = NULL;
  66.     int ret = 0;
  67.  
  68.     if(strchr(mode,'r') != 0 && type == ARCHIVETYPE_AUTO){
  69.         type = s_get_archive_type(arcfile);
  70.     }
  71.  
  72.     switch(type){
  73.     case ARCHIVETYPE_AUTO:
  74.     case ARCHIVETYPE_NORMAL:
  75.     case ARCHIVETYPE_TAR:
  76.     case ARCHIVETYPE_CPIO:
  77.     case ARCHIVETYPE_AR:
  78.         pfile = new CTarArcFile_Normal;
  79.         break;
  80.     case ARCHIVETYPE_GZ:
  81.     case ARCHIVETYPE_TARGZ:
  82.     case ARCHIVETYPE_CPIOGZ:
  83.     case ARCHIVETYPE_ARGZ:
  84.         pfile = new CTarArcFile_GZip;
  85.         break;
  86.     case ARCHIVETYPE_Z:
  87.     case ARCHIVETYPE_TARZ:
  88.     case ARCHIVETYPE_CPIOZ:
  89.     case ARCHIVETYPE_ARZ:
  90.         pfile = new CTarArcFile_Compress;
  91.         break;
  92.     case ARCHIVETYPE_BZ2:
  93.     case ARCHIVETYPE_TARBZ2:
  94.     case ARCHIVETYPE_CPIOBZ2:
  95.     case ARCHIVETYPE_ARBZ2:
  96.         pfile = new CTarArcFile_BZip2;
  97.         break;
  98.     default:
  99.         return NULL;
  100.     }
  101.     ret = pfile->open(arcfile, mode);
  102.     if(!ret){delete pfile;return NULL;}
  103.     return pfile;
  104. }
  105. int ITarArcFile::s_get_archive_type(const char *arcfile)
  106. {
  107.     FILE *fp = fopen(arcfile, "rb");
  108.     if(fp==NULL){return -1;} // どんぞ:追加
  109.     unsigned char buf[100]; memset(buf, 0, sizeof(buf));
  110.     int n = fread(buf, 1, sizeof(buf), fp);
  111.     fclose(fp);
  112.  
  113.     if(buf[0] == 0x1f && buf[1] == 0x8b){
  114.         return ARCHIVETYPE_GZ;
  115.     }else if(buf[0] == 0xed && buf[1] == 0xab && buf[2] == 0xee && buf[3] == 0xdb){
  116.         return ARCHIVETYPE_GZ; /* RPM */
  117.     }else if(buf[0] == 'B' && buf[1] == 'Z' && buf[2] == 'h'
  118.         && buf[4]==0x31 && buf[5]==0x41 && buf[6]==0x59 && buf[7]==0x26 && buf[8]==0x53 && buf[9]==0x59){
  119.         return ARCHIVETYPE_BZ2;
  120.     }else if(buf[0] == (unsigned char)'\037' && buf[1] == (unsigned char)'\235'){
  121.         return ARCHIVETYPE_Z;
  122.     }else{
  123.         return ARCHIVETYPE_NORMAL;
  124.     }
  125.     return -1;
  126. }
  127.  
  128.