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

  1. /*
  2.     arcgz.cpp
  3.         gzip archive input/output 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 "arcgz.h"
  34. #include "util.h"
  35. #include "zlib.h"
  36. #include <stdlib.h>
  37. #include <fstream>
  38.  
  39. #include <io.h>    // open
  40. #include <fcntl.h>
  41. #include <sys/stat.h>    // _S_IREAD
  42. #include "rpm.h"
  43.  
  44. /* gzip flag byte */
  45. const CTarArcFile_GZip::GZIP_FLAG_ASCII_FLAG   =0x01; /* bit 0 set: file probably ascii text */
  46. const CTarArcFile_GZip::GZIP_FLAG_CONTINUATION =0x02; /* bit 1 set: continuation of multi-part gzip file */
  47. const CTarArcFile_GZip::GZIP_FLAG_EXTRA_FIELD  =0x04; /* bit 2 set: extra field present */
  48. const CTarArcFile_GZip::GZIP_FLAG_ORIG_NAME    =0x08; /* bit 3 set: original file name present */
  49. const CTarArcFile_GZip::GZIP_FLAG_COMMENT      =0x10; /* bit 4 set: file comment present */
  50. const CTarArcFile_GZip::GZIP_FLAG_ENCRYPTED    =0x20; /* bit 5 set: file is encrypted */
  51. const CTarArcFile_GZip::GZIP_FLAG_RESERVED     =0xC0; /* bit 6,7:   reserved */
  52. const CTarArcFile_GZip::GZIP_METHOD_DEFLATED   =8;
  53.  
  54.  
  55. CTarArcFile_GZip::CTarArcFile_GZip()
  56. {
  57.     m_gzFile = NULL;
  58. }
  59. CTarArcFile_GZip::~CTarArcFile_GZip()
  60. {
  61.     close();
  62. }
  63.  
  64. bool CTarArcFile_GZip::open(const char *arcfile, const char *mode)
  65. {
  66.     m_arcfile = arcfile;
  67.     gzFile f = NULL;
  68.     int fd = -1;
  69.     int rpmlen = 0;
  70.     if(strchr(mode,'r')){
  71.         rpmlen = rpm_getheadersize(arcfile);
  72.         if(rpmlen == -1){rpmlen = 0;}
  73.         fd = _open(arcfile, _O_BINARY|_O_RDONLY, 0);
  74.         if(fd == -1){return false;}
  75.         lseek(fd, rpmlen, SEEK_CUR);
  76.     }else{
  77.         fd = _open(arcfile, _O_BINARY|_O_CREAT|_O_RDWR|_O_TRUNC, _S_IREAD | _S_IWRITE);
  78.     }
  79.  
  80.     f = gzdopen(fd, mode);
  81.     if(f==NULL){ return false;}
  82.     m_gzFile = f;
  83.  
  84.     if(strchr(mode,'r')){
  85.         /* retrieve GZIP header information(filename, time,...) */
  86.         ifstream fs_r;
  87.         fs_r.open(arcfile, ios::in|ios::binary);
  88.         int c;
  89.         if(fs_r.fail()){return false;}
  90.         fs_r.seekg(rpmlen, ios_base::cur);        /* skip rpm header */
  91.         if(fs_r.get()!=0x1f || fs_r.get()!=0x8b){return false;}
  92.         if((c=fs_r.get())==EOF){return false;}
  93.         m_gzip_compress_method = c;
  94.         if((c=fs_r.get())==EOF){return false;}
  95.         int flags = m_gzip_flags = c;
  96.         if((flags & GZIP_FLAG_ENCRYPTED)||(flags & GZIP_FLAG_CONTINUATION)||(flags & GZIP_FLAG_RESERVED)){return true;}
  97.         time_t stamp;
  98.         stamp = fs_r.get();
  99.         stamp |= fs_r.get()<<8;
  100.         stamp |= fs_r.get()<<16;
  101.         stamp |= fs_r.get()<<24;
  102.         m_mtime = m_gzip_time_stamp = stamp;
  103.         m_gzip_ext_flag = fs_r.get();
  104.         m_gzip_os_type = fs_r.get();
  105.         if(flags & GZIP_FLAG_CONTINUATION){
  106.             m_gzip_part = fs_r.get();
  107.         }
  108.         if(flags & GZIP_FLAG_EXTRA_FIELD){
  109.             int len = fs_r.get();
  110.             while(len<10000 && (len--)>0){
  111.                 fs_r.get();
  112.             }
  113.         }
  114.         if(flags & GZIP_FLAG_ORIG_NAME){
  115.             string fname;
  116.             while((c=fs_r.get())!=EOF && c!='\0'){
  117.                 fname += c;
  118.             }
  119.             m_orig_filename = m_gzip_orig_name = fname;
  120.         }
  121.         if(flags & GZIP_FLAG_COMMENT){
  122.             string comment;
  123.             while((c=fs_r.get())!=EOF && c!='\0'){
  124.                 comment += c;
  125.             }
  126.             m_gzip_comment = comment;
  127.         }
  128.         fs_r.seekg(-4, ios_base::end);
  129.         size_t size;
  130.         size = fs_r.get();
  131.         size |= fs_r.get()<<8;
  132.         size |= fs_r.get()<<16;
  133.         size |= fs_r.get()<<24;
  134.         m_orig_filesize = size;
  135.     }
  136.     return true;
  137.     //return (f != NULL);
  138. }
  139. int CTarArcFile_GZip::read(void *buf, int size)
  140. {
  141.     return gzread(m_gzFile, buf, size);
  142. }
  143. int CTarArcFile_GZip::write(void *buf, int size)
  144. {
  145.     return gzwrite(m_gzFile, buf, size);
  146. }
  147. int CTarArcFile_GZip::seek(int offset, int origin)
  148. {
  149.     return gzseek(m_gzFile, offset, origin);
  150. }
  151. void CTarArcFile_GZip::close()
  152. {
  153.     if(m_gzFile){
  154.         int ret = gzclose(m_gzFile);
  155.         m_gzFile = NULL;
  156.     }
  157. }
  158.  
  159. string CTarArcFile_GZip::get_orig_filename(){
  160.     if(! m_orig_filename.empty()){return m_orig_filename;}
  161.     string fname = get_filename(m_arcfile.c_str());
  162.     if(fname.length()>3 && stricmp(fname.substr(fname.length()-3).c_str(),".gz") == 0){
  163.         return fname.substr(0, fname.length()-3);
  164.     }
  165.     return fname + "_extracted";
  166. }
  167.