Advertisement
Guest User

Untitled

a guest
Oct 8th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <tchar.h>
  4. #include <windows.h>
  5. #include <windowsx.h>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. #define F_FS 1
  11. #define F_V 2
  12. #define F_WAPI 3
  13. #define F_MM 4
  14.  
  15. class myfiles{
  16.     private:
  17.         int now_type;
  18.         char* filename;
  19.        
  20.         fstream fs;
  21.         FILE * fl;
  22.         HANDLE hFile;
  23.         HANDLE hMapping;
  24.         LPVOID dataPtr;
  25.         DWORD dwFileSize;
  26.        
  27.     public:
  28.         myfiles(int type, char* filename){
  29.             this->now_type = type;
  30.             this->filename = filename;
  31.         }
  32.  
  33.         ~myfiles(){
  34.             switch(now_type){
  35.                 /*case F_FS:
  36.                     fs.close();
  37.                     break;
  38.                 case F_V:
  39.                     fclose(fl);
  40.                     break;*/
  41.                 case F_WAPI:
  42.                     CloseHandle(hFile);
  43.                     break;
  44.                 case F_MM:
  45.                     UnmapViewOfFile(dataPtr);
  46.                     CloseHandle(hMapping);
  47.                     SetEndOfFile(hFile);
  48.                     CloseHandle(hFile);
  49.             }
  50.         }
  51.  
  52.         bool read(char * &result, int &lenght){
  53.             switch(now_type){
  54.                 case F_FS:
  55.                     return read_fstreamFiles(result, lenght);
  56.                 case F_V:
  57.                     return read_variablesFiles(result, lenght);
  58.                 case F_WAPI:
  59.                     return read_winApiFiles(result, lenght);
  60.                 case F_MM:
  61.                     return read_memoryMappedFiles(result, lenght);
  62.  
  63.             }
  64.         }
  65.  
  66.         bool write(char * result, int lenght){
  67.             switch(now_type){
  68.                 case F_FS:
  69.                     return write_fstreamFiles(result, lenght);
  70.                 case F_V:
  71.                     return write_variablesFiles(result, lenght);
  72.                 case F_WAPI:
  73.                     return write_winApiFiles(result, lenght);
  74.                 case F_MM:
  75.                     return write_memoryMappedFiles(result, lenght);
  76.             }
  77.         }
  78.  
  79.     private:
  80.         bool read_fstreamFiles(char * &result, int &lenght){
  81.             fs.open(filename, ios::in|ios::binary);
  82.             fs.seekp(0, ios::end);
  83.             lenght = fs.tellp();
  84.             fs.seekp(0, ios::beg);  // back to start   
  85.             result = new char[lenght+1];
  86.             fs.read(result, lenght);
  87.             result[lenght] = '\0';
  88.             fs.close();
  89.             return true;
  90.         }
  91.  
  92.         bool read_variablesFiles(char * &result, int &lenght){
  93.             fl = fopen(filename, "rb");
  94.             fseek(fl,0,SEEK_END);
  95.             lenght = ftell(fl);
  96.             fseek(fl,0,SEEK_SET); // back to start
  97.             result = new char[lenght+1];
  98.             fread(result, 1, lenght, fl);
  99.             result[lenght] = '\0';
  100.             fclose(fl);
  101.             return true;
  102.         }
  103.  
  104.         bool read_winApiFiles(char * &result, int &lenght){
  105.             hFile = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  106.             if(hFile == INVALID_HANDLE_VALUE)
  107.                 return false;
  108.  
  109.             dwFileSize = GetFileSize(hFile, nullptr);
  110.             if(dwFileSize == INVALID_FILE_SIZE) {      
  111.                 CloseHandle(hFile);
  112.                 return false;
  113.             }
  114.  
  115.             result = new char[dwFileSize];
  116.             lenght = dwFileSize;
  117.             DWORD dwCount;
  118.             ReadFile(hFile, result, dwFileSize, &dwCount, NULL);
  119.  
  120.             if(dwCount != dwFileSize)
  121.                 return false;
  122.    
  123.             return true;
  124.         }
  125.  
  126.         bool read_memoryMappedFiles(char * &result, int &lenght){
  127.             hFile = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  128.             if(hFile == INVALID_HANDLE_VALUE)
  129.                 return false;
  130.  
  131.             dwFileSize = GetFileSize(hFile, nullptr);
  132.             if(dwFileSize == INVALID_FILE_SIZE || dwFileSize == 0) {       
  133.                 CloseHandle(hFile);
  134.                 return false;
  135.             }
  136.  
  137.             hMapping = CreateFileMapping(hFile, nullptr, PAGE_READWRITE, 0, 0, nullptr);
  138.             if(hMapping == nullptr) { // yes, NULL, not INVALID_HANDLE_VALUE, see MSDN
  139.                 CloseHandle(hFile);
  140.                 return false;
  141.             }
  142.  
  143.             dataPtr = MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, dwFileSize);
  144.             if(dataPtr == nullptr) {
  145.                 CloseHandle(hMapping);
  146.                 CloseHandle(hFile);
  147.                 return false;
  148.             }
  149.  
  150.             lenght = dwFileSize;
  151.             result = new char[lenght+1];
  152.             result = strncpy(result, (char*)dataPtr, lenght);
  153.             result[lenght] = '\0';
  154.  
  155.             return true;
  156.         }
  157.  
  158.         bool write_fstreamFiles(char * result, int lenght){
  159.             fs.open(filename, ios::out|ios::binary|ios::trunc);
  160.             fs.write(result, lenght);
  161.             fs.close();
  162.             return true;
  163.         }
  164.  
  165.         bool write_variablesFiles(char * result, int lenght){
  166.             fl = fopen(filename, "wb");
  167.             fwrite(result, 1, lenght, fl);
  168.             return true;
  169.         }
  170.  
  171.         bool write_winApiFiles(char * result, int lenght){
  172.             DWORD dwPtr = SetFilePointer (hFile, 0, NULL, FILE_BEGIN) ;
  173.             if (dwPtr == INVALID_SET_FILE_POINTER)
  174.                 return false;
  175.  
  176.             DWORD lpNumberOfBytesWritten;
  177.             WriteFile(hFile, result, lenght, &lpNumberOfBytesWritten, NULL);
  178.  
  179.             if(lpNumberOfBytesWritten != lenght)
  180.                 return false;
  181.  
  182.             if(!SetEndOfFile(hFile))
  183.                 return false;
  184.  
  185.             return true;
  186.         }
  187.  
  188.         bool write_memoryMappedFiles(char * result, int lenght){
  189.             if(lenght > dwFileSize){/* remapping to increase the size*/
  190.                 UnmapViewOfFile(dataPtr);
  191.                 if(hMapping && hMapping != nullptr)
  192.                     CloseHandle(hMapping);
  193.  
  194.                 hMapping = CreateFileMapping(hFile, nullptr, PAGE_READWRITE|SEC_COMMIT, 0, lenght, nullptr);
  195.                 if(hMapping == nullptr) {
  196.                     CloseHandle(hFile);
  197.                     return false;
  198.                 }
  199.  
  200.                 dataPtr = MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, lenght);
  201.                 if(dataPtr == nullptr) {
  202.                     CloseHandle(hMapping);
  203.                     CloseHandle(hFile);
  204.                     return false;
  205.                 }
  206.             }
  207.  
  208.             strcpy((char*)dataPtr, result);
  209.             SetFilePointer(hFile, lenght, NULL, FILE_BEGIN);
  210.  
  211.             return true;
  212.         }
  213. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement