daily pastebin goal
56%
SHARE
TWEET

Raw memory continuous table cell detector

keverman Jun 21st, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // A program to discern continuous table cells in another program's memory
  2.  
  3. long long hex2Dec(std::string hexVal)
  4. {  
  5.     long long base = 1, decVal = 0;
  6.      
  7.     for(int i = hexVal.size() - 1; i >= 0; i--)
  8.     {  
  9.         if(hexVal[i] >= '0' && hexVal[i] <= '9') decVal += (hexVal[i] - 48) * base;
  10.         else                                     decVal += (hexVal[i] - 55) * base;
  11.        
  12.         base *= 16;
  13.     }
  14.      
  15.     return decVal;
  16. }
  17.  
  18. std::string dec2Hex(long long decVal)
  19. {
  20.     std::string hexVal;
  21.    
  22.     while(decVal > 0)
  23.     {
  24.         long long rem = decVal % 16;
  25.         decVal /= 16;
  26.        
  27.         if(rem > 9) hexVal += (char)(rem + 55);
  28.         else        hexVal += rem + 48;
  29.     }
  30.    
  31.     std::reverse(hexVal.begin(), hexVal.end());
  32.     return hexVal;
  33. }
  34.  
  35. struct memoryElement
  36. {
  37.     long long decVal, idx;
  38.     std::string hexVal;
  39.    
  40.     memoryElement() {}
  41.    
  42.     memoryElement(long long index, long long val)
  43.     {
  44.         idx = index;
  45.         decVal = val, hexVal = dec2Hex(val);
  46.     }
  47. };
  48.  
  49. struct memoryElementComparator
  50. {
  51.     bool operator()(const memoryElement &l, const memoryElement &r)
  52.     {
  53.         return l.decVal < r.decVal;
  54.     }
  55. };
  56.  
  57. int main()
  58. {
  59.     std::vector<std::vector<long long>> mem;
  60.    
  61.     std::string file;
  62.     std::cin >> file;
  63.    
  64.     std::ifstream in;
  65.     in.open(file.c_str());
  66.    
  67.     while(!in.eof())
  68.     {
  69.         char buf[4096];
  70.         std::string line;
  71.         in.getline(buf, 4096);
  72.         line = std::string(buf);
  73.         mem.resize(mem.size() + 1);
  74.        
  75.         size_t it = 0;
  76.        
  77.         while(it != line.size())
  78.         {
  79.             std::string memCell;
  80.            
  81.             while(it != line.size() && line[it] != ';')
  82.                 memCell += line[it++];
  83.            
  84.             if(it != line.size())
  85.                 it++;
  86.            
  87.             mem.back().push_back(hex2Dec(memCell));
  88.         }
  89.        
  90.         std::sort(mem.back().begin(), mem.back().end());
  91.     }
  92.    
  93.     in.close();
  94.  
  95.     std::vector<std::vector<long long>> paths(mem.front().size());
  96.     long long maxDif = 10000;
  97.    
  98.     for(unsigned int i = 0; i < mem.front().size(); i++)
  99.         paths[i].push_back(mem.front()[i]);
  100.    
  101.     for(unsigned int memColumn = 1; memColumn < mem.size() && paths.size() > 0; memColumn++)
  102.     {
  103.         unsigned int initialPathsSize = paths.size(), prevStart = 0;
  104.        
  105.         for(unsigned int path = 0; path < initialPathsSize; path++)
  106.         {
  107.             unsigned int cur = prevStart;
  108.            
  109.             while(cur < mem[memColumn].size() && mem[memColumn][cur] - paths[path].back() < -maxDif)
  110.                 cur++;
  111.            
  112.             if(cur == mem[memColumn].size())
  113.                 break;
  114.            
  115.             prevStart = cur;
  116.            
  117.             while(cur < mem[memColumn].size() && mem[memColumn][cur] - paths[path].back() < maxDif)
  118.             {
  119.                 paths.push_back(paths[path]);
  120.                 paths.back().push_back(mem[memColumn][cur++]);
  121.             }
  122.         }
  123.        
  124.         paths.erase(paths.begin(), paths.begin() + initialPathsSize);
  125.     }
  126.    
  127.     file = file.substr(0, file.find('.'));
  128.     file += ".out";
  129.    
  130.     std::vector<std::set<memoryElement, memoryElementComparator>> sortedPaths(paths.size());
  131.    
  132.     for(unsigned int i = 0; i < paths.size(); i++)
  133.         for(unsigned int j = 0; j < paths[i].size(); j++)
  134.             sortedPaths[i].insert(memoryElement(j + 1, paths[i][j]));
  135.        
  136.     std::ofstream out;
  137.     out.open(file.c_str());
  138.    
  139.     if(paths.size() > 0)
  140.     {
  141.         std::cout << "Following paths were found:\n";
  142.        
  143.         for(std::set<memoryElement, memoryElementComparator> path : sortedPaths)
  144.         {
  145.             std::string outputLine;
  146.            
  147.             for(memoryElement el : path)
  148.                 outputLine += el.hexVal + " [" + std::to_string(el.decVal) + "] [" + std::to_string(el.idx) + "] -> ";
  149.            
  150.             outputLine.erase(outputLine.size() - 4, outputLine.size() - 1);
  151.             outputLine += "\n";
  152.            
  153.             out << outputLine;
  154.             std::cout << outputLine;
  155.         }
  156.     }
  157.    
  158.     else
  159.     {
  160.         std::cout << "No paths were found\n" << std::endl;
  161.     }
  162.    
  163.     out.close();
  164. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top