home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 5036 / source.7z / x_file.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2012-01-01  |  14.9 KB  |  521 lines

  1. #include "xentax.h"
  2.  
  3. void GetModulePathname(LPSTR pathname, DWORD maxlen)
  4. {
  5.  // get filename
  6.  char filename[MAX_PATH];
  7.  GetModuleFileNameA(NULL, filename, MAX_PATH);
  8.  
  9.  // make path from filename
  10.  char c_drive[1024];
  11.  char c_path[1024];
  12.  _splitpath(filename, c_drive, c_path, nullptr, nullptr);
  13.  string temp(c_drive);
  14.  temp += c_path;
  15.  
  16.  // set pathname
  17.  strcpy_s(&pathname[0], maxlen, temp.c_str());
  18. }
  19.  
  20. std::string GetShortFilename(const std::string& filename)
  21. {
  22.  // validate
  23.  if(!filename.length()) return std::string();
  24.  
  25.  // extract components
  26.  char c_param1[MAX_PATH];
  27.  char c_param2[MAX_PATH];
  28.  char c_param3[MAX_PATH];
  29.  char c_param4[MAX_PATH];
  30.  
  31.  // extract components
  32.  c_param1[0] = '\0';
  33.  c_param2[0] = '\0';
  34.  c_param3[0] = '\0';
  35.  c_param4[0] = '\0';
  36.  _splitpath(filename.c_str(), c_param1, c_param2, c_param3, c_param4); 
  37.  
  38.  // return name + extension
  39.  std::string retval(c_param3);
  40.  retval += c_param4;
  41.  return retval;
  42. }
  43.  
  44. std::string GetShortFilenameWithoutExtension(const std::string& filename)
  45. {
  46.  // validate
  47.  if(!filename.length()) return std::string();
  48.  
  49.  // extract components
  50.  char c_param1[MAX_PATH];
  51.  char c_param2[MAX_PATH];
  52.  char c_param3[MAX_PATH];
  53.  char c_param4[MAX_PATH];
  54.  
  55.  // extract components
  56.  c_param1[0] = '\0';
  57.  c_param2[0] = '\0';
  58.  c_param3[0] = '\0';
  59.  c_param4[0] = '\0';
  60.  _splitpath(filename.c_str(), c_param1, c_param2, c_param3, c_param4); 
  61.  
  62.  // return name + extension
  63.  std::string retval(c_param3);
  64.  return retval;
  65. }
  66.  
  67. std::string GetPathnameFromFilename(const std::string& filename)
  68. {
  69.  // validate
  70.  if(!filename.length()) return std::string();
  71.  
  72.  // extract components
  73.  char c_param1[MAX_PATH];
  74.  char c_param2[MAX_PATH];
  75.  char c_param3[MAX_PATH];
  76.  char c_param4[MAX_PATH];
  77.  
  78.  // extract components
  79.  c_param1[0] = '\0';
  80.  c_param2[0] = '\0';
  81.  c_param3[0] = '\0';
  82.  c_param4[0] = '\0';
  83.  _splitpath(filename.c_str(), c_param1, c_param2, c_param3, c_param4); 
  84.  
  85.  // return name + extension
  86.  std::string retval(c_param1);
  87.  retval += c_param2;
  88.  return retval;
  89. }
  90.  
  91. std::string GetExtensionFromFilename(const std::string& filename)
  92. {
  93.  // validate
  94.  if(!filename.length()) return std::string();
  95.  
  96.  // extract components
  97.  char c_param1[MAX_PATH];
  98.  char c_param2[MAX_PATH];
  99.  char c_param3[MAX_PATH];
  100.  char c_param4[MAX_PATH];
  101.  
  102.  // extract components
  103.  c_param1[0] = '\0';
  104.  c_param2[0] = '\0';
  105.  c_param3[0] = '\0';
  106.  c_param4[0] = '\0';
  107.  _splitpath(filename.c_str(), c_param1, c_param2, c_param3, c_param4); 
  108.  
  109.  // return name + extension
  110.  strlwr(c_param4);
  111.  return std::string(c_param4);
  112. }
  113.  
  114. bool BuildFilenameList(std::deque<std::string>& namelist, const char* fext)
  115. {
  116.  // get path of application
  117.  char c_rootname[MAX_PATH];
  118.  GetModuleFileNameA(0, c_rootname, MAX_PATH);
  119.  
  120.  // split the pathname
  121.  char c_drive[1024];
  122.  char c_dir[1024];
  123.  _splitpath(c_rootname, c_drive, c_dir, nullptr, nullptr);
  124.  strlwr(c_drive);
  125.  strlwr(c_dir);
  126.  
  127.  // build pathname
  128.  std::string s_rootname(c_drive);
  129.  s_rootname += std::string(c_dir);
  130.  
  131.  // build directory list
  132.  std::deque<std::string> templist;
  133.  templist.insert(templist.end(), s_rootname);
  134.  
  135.  size_t i = 0;
  136.  for(;;)
  137.     {
  138.      // get wildcard
  139.      find_file file;
  140.      std::string path(templist[i]);
  141.      path += "*.*";
  142.      file.find(path.c_str());
  143.      if(!file) return error("Could not build wildcard path.");
  144.  
  145.      // insert directories
  146.      if(file.is_directory() && !file.is_dots() && !file.is_system()) {
  147.         std::string path(templist[i]);
  148.         path += std::string(file.filename());
  149.         path += std::string("\\");
  150.         templist.insert(templist.end(), path);
  151.        }
  152.  
  153.      while(file.next()) {
  154.         if(file.is_directory() && !file.is_dots() && !file.is_system()) {
  155.            std::string path(templist[i]);
  156.            path += std::string(file.filename());
  157.            path += std::string("\\");
  158.            templist.insert(templist.end(), path);
  159.           }
  160.        }
  161.  
  162.      file.close();
  163.  
  164.      // update index
  165.      if(++i == templist.size())
  166.         break;
  167.     }
  168.  
  169.  // transfer directories with EXT files
  170.  std::deque<std::string> rootlist;
  171.  for(size_t i = 0; i < templist.size(); i++)
  172.     {
  173.      find_file file;
  174.      bool added = false;
  175.      std::string path(templist[i]);
  176.      path += "*";
  177.      path += ".";
  178.      path += fext;
  179.      if(file.find(path.c_str()) && !file.is_directory()) {
  180.         rootlist.insert(rootlist.end(), templist[i]);
  181.         added = true;
  182.        }
  183.     }
  184.  
  185.  // erase the temporary list
  186.  templist.erase(templist.begin(), templist.end());
  187.  if(rootlist.empty()) return error("No files found in current directory or subdirectories.");
  188.  
  189.  // build namelist
  190.  namelist.clear();
  191.  for(size_t i = 0; i < rootlist.size(); i++)
  192.     {
  193.      // clear templist
  194.      find_file file;
  195.      templist.erase(templist.begin(), templist.end());
  196.  
  197.      // add EXT files
  198.      std::string path(rootlist[i]);
  199.      path += "*";
  200.      path += ".";
  201.      path += fext;
  202.      if(file.find(path.c_str())) {
  203.         std::string temp(rootlist[i]);
  204.         temp += file.filename();
  205.         templist.insert(templist.end(), temp);
  206.        }
  207.      while(file.next()) {
  208.         std::string temp(rootlist[i]);
  209.         temp += file.filename();
  210.         templist.insert(templist.end(), temp);
  211.        }
  212.  
  213.      // sort temporary list and append to the namelist
  214.      if(templist.size()) {
  215.         std::sort(templist.begin(), templist.end());
  216.         namelist.insert(namelist.end(), templist.begin(), templist.end());
  217.        }
  218.     }
  219.  
  220.  // must have something in namelist
  221.  templist.erase(templist.begin(), templist.end());
  222.  if(!namelist.size()) return error("No files found in root directory or subdirectories.");
  223.  
  224.  return true;
  225. }
  226.  
  227. bool BuildFilenameList(std::deque<std::string>& namelist, const char* fext, const char* rootname)
  228. {
  229.  // get path of application
  230.  char c_rootname[MAX_PATH];
  231.  memmove(c_rootname, rootname, strlen(rootname) + 1);
  232.  
  233.  // add "." to extension
  234.  std::string extension;
  235.  if(fext[0] != '.') extension += ".";
  236.  extension += fext;
  237.  
  238.  // split the pathname
  239.  char c_drive[1024];
  240.  char c_dir[1024];
  241.  _splitpath(c_rootname, c_drive, c_dir, nullptr, nullptr);
  242.  strlwr(c_drive);
  243.  strlwr(c_dir);
  244.  
  245.  // build pathname
  246.  std::string s_rootname(c_drive);
  247.  s_rootname += std::string(c_dir);
  248.  
  249.  // build directory list
  250.  std::deque<std::string> templist;
  251.  templist.insert(templist.end(), s_rootname);
  252.  
  253.  size_t i = 0;
  254.  for(;;)
  255.     {
  256.      // get wildcard
  257.      find_file file;
  258.      std::string path(templist[i]);
  259.      path += "*.*";
  260.      file.find(path.c_str());
  261.      if(!file) return error("Could not build wildcard path.");
  262.  
  263.      // insert directories
  264.      if(file.is_directory() && !file.is_dots() && !file.is_system()) {
  265.         std::string path(templist[i]);
  266.         path += std::string(file.filename());
  267.         path += std::string("\\");
  268.         templist.insert(templist.end(), path);
  269.        }
  270.  
  271.      while(file.next()) {
  272.         if(file.is_directory() && !file.is_dots() && !file.is_system()) {
  273.            std::string path(templist[i]);
  274.            path += std::string(file.filename());
  275.            path += std::string("\\");
  276.            templist.insert(templist.end(), path);
  277.           }
  278.        }
  279.  
  280.      file.close();
  281.  
  282.      // update index
  283.      if(++i == templist.size())
  284.         break;
  285.     }
  286.  
  287.  // transfer directories with EXT files
  288.  std::deque<std::string> rootlist;
  289.  for(size_t i = 0; i < templist.size(); i++)
  290.     {
  291.      find_file file;
  292.      bool added = false;
  293.      std::string path(templist[i]);
  294.      path += "*";
  295.      path += extension;
  296.      if(file.find(path.c_str()) && !file.is_directory()) {
  297.         rootlist.insert(rootlist.end(), templist[i]);
  298.         added = true;
  299.        }
  300.     }
  301.  
  302.  // erase the temporary list
  303.  templist.erase(templist.begin(), templist.end());
  304.  if(rootlist.empty()) return error("No files found in current directory or subdirectories.");
  305.  
  306.  // build namelist
  307.  namelist.clear();
  308.  for(size_t i = 0; i < rootlist.size(); i++)
  309.     {
  310.      // clear templist
  311.      find_file file;
  312.      templist.erase(templist.begin(), templist.end());
  313.  
  314.      // add files
  315.      std::string path(rootlist[i]);
  316.      path += "*";
  317.      path += extension;
  318.      if(file.find(path.c_str())) {
  319.         std::string temp(rootlist[i]);
  320.         temp += file.filename();
  321.         if(!file.is_directory())
  322.            if(!strcmpi(extension.c_str(), GetExtensionFromFilename(temp).c_str()))
  323.               templist.insert(templist.end(), temp);
  324.        }
  325.      while(file.next()) {
  326.         std::string temp(rootlist[i]);
  327.         temp += file.filename();
  328.         if(!file.is_directory())
  329.            if(!strcmpi(extension.c_str(), GetExtensionFromFilename(temp).c_str()))
  330.               templist.insert(templist.end(), temp);
  331.        }
  332.  
  333.      // sort temporary list and append to the namelist
  334.      if(templist.size()) {
  335.         std::sort(templist.begin(), templist.end());
  336.         namelist.insert(namelist.end(), templist.begin(), templist.end());
  337.        }
  338.     }
  339.  
  340.  // must have something in namelist
  341.  templist.erase(templist.begin(), templist.end());
  342.  if(!namelist.size()) return error("No files found in root directory or subdirectories.");
  343.  
  344.  return true;
  345. }
  346.  
  347. bool SearchFileForSignature(std::ifstream& ifile, const char* signature, size_t sigsize, std::deque<uint64>& offsets)
  348. {
  349.  // validation
  350.  if(!ifile.is_open()) return false;
  351.  if(!signature || !sigsize) return false;
  352.  if(offsets.size()) offsets.clear();
  353.  
  354.  // get filesize
  355.  ifile.seekg(0, ios::end);
  356.  uint64 filesize = ifile.tellg();
  357.  ifile.seekg(0, ios::beg);
  358.  
  359.  // read buffer properties
  360.  const uint32 maxbuffersize = 32*1024*1024;
  361.  uint32 buffersize = (filesize < (uint64)maxbuffersize ? (uint32)filesize : maxbuffersize);
  362.  boost::shared_array<char> buffer(new char[buffersize]);
  363.  
  364.  // boyer-moore-horspool properties
  365.  boost::shared_array<unsigned char> table(new unsigned char[256]);
  366.  for(uint32 i = 0; i < 256; i++) table[i] = sigsize;
  367.  uint32 p_len = sigsize;
  368.  const unsigned char* p_str = reinterpret_cast<const unsigned char*>(signature);
  369.  for(uint32 i = 0; i < p_len - 1; i++) table[p_str[i]] = p_len - i - 1;
  370.  
  371.  // search properties
  372.  uint64 bytes_read = 0;
  373.  uint64 bytes_left = filesize;
  374.  uint64 start = 0;
  375.  
  376.  // search
  377.  while(bytes_read < filesize)
  378.       {
  379.        // search partial buffer
  380.        if(bytes_left < buffersize)
  381.          {
  382.           // read data
  383.           ifile.read(buffer.get(), bytes_left);
  384.           if(ifile.fail()) return false;
  385.  
  386.           // resume search?
  387.  
  388.           // boyer-moore-horspool properties
  389.           uint32 s_len = (uint32)bytes_left;
  390.           const unsigned char* s_str = reinterpret_cast<const unsigned char*>(buffer.get());          
  391.  
  392.           // boyer-moore-horspool search
  393.           size_t j = 0;
  394.           while(j <= (s_len - p_len)) {
  395.                 unsigned char c = s_str[j + p_len - 1];
  396.                 if(p_str[p_len - 1] == c && memcmp(p_str, s_str + j, p_len - 1) == 0) offsets.push_back(bytes_read + j);
  397.                 j += table[c];
  398.                }
  399.  
  400.           // adjust search properties
  401.           bytes_read = bytes_read + bytes_left;
  402.           bytes_left = 0;
  403.          }
  404.        // search full buffer
  405.        else
  406.          {
  407.           // read data
  408.           ifile.read(buffer.get(), buffersize);
  409.           if(ifile.fail()) return false;
  410.  
  411.           // resume search?
  412.  
  413.           // boyer-moore-horspool properties
  414.           size_t s_len = buffersize;
  415.           const unsigned char* s_str = reinterpret_cast<const unsigned char*>(buffer.get());          
  416.  
  417.           // boyer-moore-horspool search
  418.           size_t j = 0;
  419.           while(j <= (s_len - p_len)) {
  420.                 unsigned char c = s_str[j + p_len - 1];
  421.                 if(p_str[p_len - 1] == c && memcmp(p_str, s_str + j, p_len - 1) == 0) offsets.push_back(bytes_read + j);
  422.                 j += table[c];
  423.                }
  424.  
  425.           // adjust search properties
  426.           bytes_read = bytes_read + buffersize;
  427.           bytes_left = bytes_left - buffersize;
  428.          }
  429.       }
  430.  
  431.  return true;
  432. }
  433.  
  434. bool SearchFileForSignature(std::ifstream& ifile, const char* signature, size_t sigsize, size_t alignment, std::deque<uint64>& offsets)
  435. {
  436.  // validation
  437.  if(!ifile.is_open()) return false;
  438.  if(!signature || !sigsize) return false;
  439.  if(offsets.size()) offsets.clear();
  440.  
  441.  // get filesize
  442.  ifile.seekg(0, ios::end);
  443.  uint64 filesize = ifile.tellg();
  444.  ifile.seekg(0, ios::beg);
  445.  
  446.  // read buffer properties
  447.  const uint32 maxbuffersize = 32*1024*1024;
  448.  uint32 buffersize = (filesize < (uint64)maxbuffersize ? (uint32)filesize : maxbuffersize);
  449.  boost::shared_array<char> buffer(new char[buffersize]);
  450.  
  451.  // boyer-moore-horspool properties
  452.  boost::shared_array<unsigned char> table(new unsigned char[256]);
  453.  for(uint32 i = 0; i < 256; i++) table[i] = sigsize;
  454.  uint32 p_len = sigsize;
  455.  const unsigned char* p_str = reinterpret_cast<const unsigned char*>(signature);
  456.  for(uint32 i = 0; i < p_len - 1; i++) table[p_str[i]] = p_len - i - 1;
  457.  
  458.  // search properties
  459.  uint64 bytes_read = 0;
  460.  uint64 bytes_left = filesize;
  461.  uint64 start = 0;
  462.  
  463.  // search
  464.  while(bytes_read < filesize)
  465.       {
  466.        // search partial buffer
  467.        if(bytes_left < buffersize)
  468.          {
  469.           // read data
  470.           ifile.read(buffer.get(), bytes_left);
  471.           if(ifile.fail()) return false;
  472.  
  473.           // resume search?
  474.  
  475.           // boyer-moore-horspool properties
  476.           uint32 s_len = (uint32)bytes_left;
  477.           const unsigned char* s_str = reinterpret_cast<const unsigned char*>(buffer.get());          
  478.  
  479.           // boyer-moore-horspool search
  480.           size_t j = 0;
  481.           while(j <= (s_len - p_len)) {
  482.                 unsigned char c = s_str[j + p_len - 1];
  483.                 if(p_str[p_len - 1] == c && memcmp(p_str, s_str + j, p_len - 1) == 0)
  484.                    if(!((bytes_read + j) % alignment)) offsets.push_back(bytes_read + j);
  485.                 j += table[c];
  486.                }
  487.  
  488.           // adjust search properties
  489.           bytes_read = bytes_read + bytes_left;
  490.           bytes_left = 0;
  491.          }
  492.        // search full buffer
  493.        else
  494.          {
  495.           // read data
  496.           ifile.read(buffer.get(), buffersize);
  497.           if(ifile.fail()) return false;
  498.  
  499.           // resume search?
  500.  
  501.           // boyer-moore-horspool properties
  502.           size_t s_len = buffersize;
  503.           const unsigned char* s_str = reinterpret_cast<const unsigned char*>(buffer.get());          
  504.  
  505.           // boyer-moore-horspool search
  506.           size_t j = 0;
  507.           while(j <= (s_len - p_len)) {
  508.                 unsigned char c = s_str[j + p_len - 1];
  509.                 if(p_str[p_len - 1] == c && memcmp(p_str, s_str + j, p_len - 1) == 0)
  510.                   if(!((bytes_read + j) % alignment)) offsets.push_back(bytes_read + j);
  511.                 j += table[c];
  512.                }
  513.  
  514.           // adjust search properties
  515.           bytes_read = bytes_read + buffersize;
  516.           bytes_left = bytes_left - buffersize;
  517.          }
  518.       }
  519.  
  520.  return true;
  521. }