Advertisement
Guest User

Untitled

a guest
Nov 24th, 2020
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string.h>
  5. #include<iomanip>
  6. #include<fstream>
  7.  
  8. #include<stack>
  9. #include<queue>
  10. #include<sstream>
  11. #include<list>
  12.  
  13. using namespace std;
  14.  
  15. namespace Test1{
  16.     using SingnaturesQueue = std::queue<char>;
  17.     using UCharQueue       = std::queue<unsigned char>;
  18.     using UCharStack       = std::stack<unsigned char>;
  19. }
  20.  
  21. namespace Test1{
  22.     unsigned long convertHexStringToULong( const std::string &str ){
  23.         char* endptr;
  24.         unsigned res = strtoul( str.c_str(), &endptr, 16 );
  25.     return res;
  26.     }
  27. }
  28.  
  29. namespace Test1{
  30.     std::string convertUCharToHexString( unsigned char val ){
  31.        stringstream ss;
  32.        ss << std::hex << static_cast<unsigned int>(val);
  33.     return ss.str();
  34.     }
  35. }
  36.  
  37. namespace Util{
  38.     class BinaryFileReader{
  39.         public:
  40.             BinaryFileReader( const std::string &filepath ) : filepath_(filepath),fp_(NULL){
  41.                 fp_ = fopen( filepath_.c_str(), "rb" );
  42.             }
  43.            
  44.             ~BinaryFileReader(){
  45.                 fclose( fp_ );
  46.             }
  47.            
  48.             bool isEnabled( void ) const{ return (fp_ == NULL ? false : true); }
  49.             bool isEof( void ) const{ return (feof( fp_ ) != 0 ? true : false); }
  50.             int read( void* ptr, size_t size, size_t n ){
  51.                return fread( ptr, size, n, fp_ );
  52.             }
  53.             std::string filepath( void ) const{ return filepath_; }
  54.         private:
  55.             std::string filepath_;
  56.             FILE*       fp_;
  57.     };
  58. }
  59.  
  60. namespace Util{
  61.     class BinaryFileWriter{
  62.         public:
  63.             BinaryFileWriter( const std::string &filepath ) : filepath_(filepath),fp_(NULL){
  64.                 fp_ = fopen( filepath_.c_str(), "wb" );
  65.             }
  66.            
  67.             ~BinaryFileWriter(){
  68.                 fclose( fp_ );
  69.             }
  70.            
  71.             bool isEnabled( void ) const{ return (fp_ == NULL ? false : true); }
  72.             bool isEof( void ) const{ return (feof( fp_ ) != 0 ? true : false); }
  73.            
  74.             int write( void* ptr, size_t size, size_t n ){
  75.                return fwrite( ptr, size, n, fp_ );
  76.             }
  77.             std::string filepath( void ) const{ return filepath_; }
  78.         private:
  79.             std::string filepath_;
  80.             FILE*       fp_;
  81.     };
  82. }
  83.  
  84.  
  85. namespace Test1{
  86.     class ICompressedItem{
  87.         public:
  88.             using TYPE = unsigned char;
  89.         public:
  90.             virtual ~ICompressedItem(){}
  91.             virtual std::string filename( void ) const = 0;
  92.             virtual std::list<ICompressedItem::TYPE> fileEntry( void ) const = 0;
  93.     };
  94.     using CompressedItemList = std::list<ICompressedItem*>;
  95. }
  96.  
  97. namespace Test1{
  98.     class IReader{
  99.         public:
  100.             virtual ~IReader(){}
  101.             virtual Test1::CompressedItemList read( void ) = 0;
  102.     };
  103. }
  104.  
  105. namespace Test1::ZipFiles{
  106.     class CompressedItem : public Test1::ICompressedItem{
  107.         public:
  108.             CompressedItem( const Test1::UCharQueue &all ) : all_(all){
  109.                 int versionNeeded2Extract = CompressedItem::extractIntDecimal( 2 );
  110.                 cout << "version needed to extract = " << versionNeeded2Extract << endl;
  111.                 int generalPurposeBitFlag = CompressedItem::extractIntDecimal( 2 );
  112.                 int compressionMethod     = CompressedItem::extractIntDecimal( 2 );
  113.                 cout << "compressionMethod = " << compressionMethod << endl;
  114.                 int lastModFileTime       = CompressedItem::extractIntDecimal( 2 );
  115.                 int lastModeFileDate      = CompressedItem::extractIntDecimal( 2 );
  116.                 int crc_32                = CompressedItem::extractIntDecimal( 4 );
  117.                 int compressedSize        = CompressedItem::extractIntDecimal( 4 );
  118.                 int uncompressedSize      = CompressedItem::extractIntDecimal( 4 );
  119.                 int fileNameLength        = CompressedItem::extractULongDecimal( 2 );
  120.                 int extraFieldLength      = CompressedItem::extractIntDecimal( 2 );
  121.                
  122.                 {
  123.                     for( int i = 0; i < fileNameLength; i++ ){
  124.                         filename_ += static_cast<char>(all_.front());
  125.                         all_.pop();
  126.                     }
  127.                 }
  128.                
  129.                 for( int i = 0; i < extraFieldLength; i++ ) all_.pop();
  130.                
  131.                 {
  132.                     Test1::UCharStack cs;
  133.                     while( !all_.empty() ){
  134.                         ICompressedItem::TYPE uc = all_.front();
  135.                         all_.pop();
  136.                         cs.push( uc );
  137.                     }
  138.                     while( !cs.empty() ){
  139.                         ICompressedItem::TYPE uc = cs.top();
  140.                         cs.pop();
  141.                         fileEntry_.push_back( uc );
  142.                     }
  143.                 }
  144.                 // 前に余計なシグネチャ(3バイト分)が付いているため、そぎ落とす
  145.                 for( int i = 0; i < 3; i++ ) fileEntry_.pop_front();
  146.             }
  147.             ~CompressedItem(){}
  148.            
  149.             std::string filename( void ) const{ return filename_; }
  150.             std::list<ICompressedItem::TYPE> fileEntry( void ) const{ return fileEntry_; }
  151.         protected:
  152.             int extractIntDecimal( int nbytes ){
  153.                Test1::UCharStack cs;
  154.                // データをnbytes分取り出す
  155.                for( int i = 0; i < nbytes; i++ ){
  156.                    unsigned char c = all_.front();
  157.                    all_.pop();
  158.                    cs.push( c );
  159.                }
  160.                
  161.                // 16進数に変換
  162.                std::string ss = "";
  163.                while( !cs.empty() ){
  164.                    unsigned char t = cs.top();
  165.                    cs.pop();
  166.                    ss += Test1::convertUCharToHexString( static_cast<unsigned char>(t) );
  167.                }
  168.                // 10進数の整数に変換
  169.                unsigned long res = Test1::convertHexStringToULong( ss );
  170.             return static_cast<int>(res);
  171.             }
  172.         protected:
  173.             unsigned long extractULongDecimal( int nbytes ){
  174.                Test1::UCharStack cs;
  175.                // データをnbytes分取り出す
  176.                for( int i = 0; i < nbytes; i++ ){
  177.                    unsigned char c = all_.front();
  178.                    all_.pop();
  179.                    cs.push( c );
  180.                }
  181.                
  182.                // 16進数に変換
  183.                std::string ss = "";
  184.                while( !cs.empty() ){
  185.                    unsigned char t = cs.top();
  186.                    cs.pop();
  187.                    ss += Test1::convertUCharToHexString( static_cast<unsigned char>(t) );
  188.                }
  189.                // 10進数の整数に変換
  190.             return Test1::convertHexStringToULong( ss );
  191.             }
  192.         private:
  193.             Test1::UCharQueue all_;
  194.         private:
  195.             std::string                      filename_;
  196.             std::list<ICompressedItem::TYPE> fileEntry_;
  197.     };
  198. }
  199.  
  200. namespace Test1::ZipFiles{
  201.     class Reader : public Test1::IReader{
  202.         public:
  203.             Reader( const std::string &compressedFilePath ) : file_(new Util::BinaryFileReader(compressedFilePath)),zipLocalHeaderSignature_(Reader::createZipLocalHeaderSignature()),centralDirectoryFileHeaderSignature_(Reader::createCentralDirectoryFileHeaderSignature()){
  204.                
  205.             }
  206.             ~Reader(){ delete file_; }
  207.             std::string filepath( void ) const{ return file_->filepath(); }
  208.             bool isEnabled( void ) const{ return file_->isEnabled(); }
  209.             virtual Test1::CompressedItemList read( void ){
  210.                 Test1::CompressedItemList items; // 結果を保持するリスト
  211.                 Test1::UCharQueue         all;   // 一時的に保持しておくキュー
  212.                 Test1::SingnaturesQueue   signature; // シグネチャとして保存してチェックするためのもの
  213.                 // バイナリデータが存在している間
  214.                 while( !file_->isEof() ){
  215.                     // シグネチャは4バイトまで
  216.                     if( signature.size() >= 4 ) signature.pop();
  217.                    
  218.                     // ファイルから1バイト分読み込む
  219.                     char c[1];
  220.                     file_->read( c, 1, 1 );
  221.                    
  222.                     // 読み込んだデータを16進数としてシグネチャキューに突っ込む
  223.                     signature.push( c[0] );
  224.                    
  225.                     if( signature == zipLocalHeaderSignature_ ){ // ZIPローカルヘッダのシグネチャと一致する場合
  226.                         // allを itemsに追加
  227.                         if( !all.empty() ) items.push_back( new Test1::ZipFiles::CompressedItem( all ) );
  228.                         // all を破棄
  229.                         all = Test1::UCharQueue();
  230.                     }else if( signature == centralDirectoryFileHeaderSignature_ ){ // セントラルディレクトリファイルヘッダのシグネチャと一致する場合
  231.                         // allを itemsに追加
  232.                         if( !all.empty() ) items.push_back( new Test1::ZipFiles::CompressedItem( all ) );
  233.                         // これ以上はないのでbreak
  234.                         break;
  235.                     }else{ // それ以外はそのままallに追加
  236.                         all.push( c[0] );
  237.                     }
  238.                 }
  239.             return items;
  240.             }
  241.         protected:
  242.             Test1::SingnaturesQueue createZipLocalHeaderSignature( void ){
  243.                 Test1::SingnaturesQueue res;
  244.                 // { "50", "4b", "3", "4" } = { 80, 75, 3, 4 }
  245.                 char tmp[] = { 80, 75, 3, 4 };
  246.                 for( int i = 0; i < 4; i++ ) res.push( tmp[i] );
  247.             return res;
  248.             }
  249.             Test1::SingnaturesQueue createCentralDirectoryFileHeaderSignature( void ){
  250.                 Test1::SingnaturesQueue res;
  251.                 // { "50", "4b", "1", "2" } = { 80, 75, 1, 2 }
  252.                 char tmp[] = { 80, 75, 1, 2 };
  253.                 for( int i = 0; i < 4; i++ ) res.push( tmp[i] );
  254.             return res;
  255.             }
  256.         private:
  257.             Util::BinaryFileReader* file_;
  258.             Test1::SingnaturesQueue zipLocalHeaderSignature_;
  259.             Test1::SingnaturesQueue centralDirectoryFileHeaderSignature_;
  260.     };
  261. }
  262.  
  263. int main( int argc, char* argv[] ){
  264.     if( argc < 2 ){
  265.         cout << "cmd: main FILE" << endl;
  266.         return -1;
  267.     }
  268.    
  269.     Test1::ZipFiles::Reader* reader = new Test1::ZipFiles::Reader( argv[1] );
  270.     if( !reader->isEnabled() ){
  271.         cout << "Cannnot read " << reader->filepath() << endl;
  272.         return -2;
  273.     }
  274.    
  275.     Test1::CompressedItemList itemList = reader->read();
  276.    
  277.     delete reader;
  278.     cout << "size = " << itemList.size() << endl;
  279.    
  280.     Test1::CompressedItemList::iterator it = itemList.begin();
  281.     {
  282.         Util::BinaryFileWriter fileWriter( (*it)->filename() );
  283.         if( !fileWriter.isEnabled() ){
  284.             cout << "Cannot open " << fileWriter.filepath() << endl;
  285.             return -2;
  286.         }
  287.        
  288.         std::list<Test1::ICompressedItem::TYPE> file = (*it)->fileEntry();
  289.         for( Test1::ICompressedItem::TYPE c : file ){
  290.             fileWriter.write( &c, 1, 1 );
  291.         }
  292.     }
  293. return 0;
  294. }
  295.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement