home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / disasm / source / encoder_dasm.cpp next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  3.6 KB  |  138 lines

  1. #include <vector>
  2. #include "ruleset.h"
  3.  
  4. #define iterate_forward(type, obj, it) if(0);else for(type::const_iterator it = (obj).begin(), it##End = (obj).end(); it != it##End; ++it)
  5.  
  6. void dump_ia(std::vector<char>& dst, const tRuleSystem& rulesys) {
  7.     long decomp_bytes = 0;
  8.     long packed_bytes = 0;
  9.  
  10.     dst.resize(72, 0);
  11.     dst.push_back(rulesys.size());
  12.  
  13.     iterate_forward(tRuleSystem, rulesys, it) {
  14.         const ruleset& rs = *it;
  15.         std::vector<std::pair<uint8, uint8> > last_match[4];
  16.         std::string last_result[4];
  17.  
  18.         iterate_forward(std::list<rule>, rs.rules, itRule) {
  19.             const rule& r = *itRule;
  20.             std::vector<char>::size_type s;
  21.             int prematch, postmatch;
  22.             int i, x, ibest;
  23.             
  24.             int l = (int)r.match_stream.size();
  25.  
  26.             ibest = 0;
  27.             prematch = postmatch = 0;
  28.  
  29.             for(i=0; i<4; ++i) {
  30.                 int l2 = (int)last_match[i].size();
  31.                 if (l2 > l)
  32.                     l2 = l;
  33.                 int tprematch = std::mismatch(last_match[i].begin(), last_match[i].begin() + l2, r.match_stream.begin()).first - last_match[i].begin();
  34.                 int tpostmatch = std::mismatch(last_match[i].rbegin(), last_match[i].rbegin() + l2, r.match_stream.rbegin()).first - last_match[i].rbegin();
  35.  
  36.                 if (tprematch+tpostmatch > prematch+postmatch) {
  37.                     prematch = tprematch;
  38.                     postmatch = tpostmatch;
  39.                     ibest = i;
  40.                 }
  41.             }
  42.  
  43.             if (prematch > 7)
  44.                 prematch = 7;
  45.  
  46.             if (postmatch > 7)
  47.                 postmatch = 7;
  48.  
  49.             if (postmatch > l - prematch)
  50.                 postmatch = l - prematch;
  51.  
  52.             dst.push_back(ibest*64 + postmatch*8 + prematch);
  53.             dst.push_back(1+l - prematch - postmatch);
  54.  
  55.             for(x=prematch; x<l - postmatch; ++x) {
  56.                 dst.push_back(r.match_stream[x].first);
  57.                 dst.push_back(r.match_stream[x].second);
  58.             }
  59.  
  60.             decomp_bytes += l*2+1;
  61.  
  62.             std::rotate(last_match, last_match+3, last_match+4);
  63.             last_match[0] = r.match_stream;
  64.  
  65.             //////////////
  66.  
  67.             l = r.result.size();
  68.  
  69.             ibest = 0;
  70.             prematch = postmatch = 0;
  71.  
  72.             const char *cur = r.result.data();
  73.  
  74.             for(i=0; i<4; ++i) {
  75.                 const char *last = last_result[i].data();
  76.                 const int lastsize = (int)last_result[i].size();
  77.                 const int maxmatch = l < lastsize ? l : lastsize;
  78.  
  79.                 int tprematch = 0;
  80.                 int tpostmatch = 0;
  81.  
  82.                 while(tprematch < maxmatch && last[tprematch] == cur[tprematch])
  83.                     ++tprematch;
  84.  
  85.                 while(tpostmatch < maxmatch && last[lastsize-tpostmatch-1] == cur[l-tpostmatch-1])
  86.                     ++tpostmatch;
  87.  
  88.                 if (tprematch+tpostmatch > prematch+postmatch) {
  89.                     prematch = tprematch;
  90.                     postmatch = tpostmatch;
  91.                     ibest = i;
  92.                 }
  93.             }
  94.  
  95.             if (prematch > 7)
  96.                 prematch = 7;
  97.  
  98.             if (postmatch > 7)
  99.                 postmatch = 7;
  100.  
  101.             if (postmatch > l - prematch)
  102.                 postmatch = l - prematch;
  103.  
  104.             dst.push_back(ibest*64 + postmatch*8 + prematch);
  105.             dst.push_back(1+l - prematch - postmatch);
  106.             s = dst.size();
  107.             if (prematch+postmatch < l) {
  108.                 dst.resize(s + l - prematch - postmatch);
  109.                 std::copy(r.result.begin() + prematch, r.result.begin() + l - postmatch, &dst[s]);
  110.             }
  111.  
  112.             decomp_bytes += l+1;
  113.  
  114.             std::rotate(last_result, last_result+3, last_result+4);
  115.             last_result[0] = r.result;
  116.         }
  117.  
  118.         dst.push_back(0);
  119.         dst.push_back(0);
  120.  
  121.         decomp_bytes += 2;
  122.     }
  123.  
  124. #ifndef _M_AMD64
  125.     static const char header[64]="[02|02] VirtualDub disasm module (IA32:P4/Athlon V1.05)\r\n\x1A";
  126. #else
  127.     static const char header[64]="[02|02] VirtualDub disasm module (AMD64:EM64T/Athlon64 V1.0)\r\n\x1A";
  128. #endif
  129.  
  130.     memcpy(&dst[0], header, 64);
  131.  
  132.     packed_bytes = dst.size() - 72;
  133.     memcpy(&dst[64], &packed_bytes, 4);
  134.  
  135.     decomp_bytes += (rulesys.size()+1)*sizeof(void *);
  136.     memcpy(&dst[68], &decomp_bytes, 4);
  137. }
  138.