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_tracedec.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  2.8 KB  |  105 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_tracedec(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.             int prematch, postmatch;
  21.             int i, x, ibest;
  22.             
  23.             int l = (int)r.match_stream.size();
  24.  
  25.             ibest = 0;
  26.             prematch = postmatch = 0;
  27.  
  28.             for(i=0; i<4; ++i) {
  29.                 int l2 = (int)last_match[i].size();
  30.                 if (l2 > l)
  31.                     l2 = l;
  32.                 int tprematch = std::mismatch(last_match[i].begin(), last_match[i].begin() + l2, r.match_stream.begin()).first - last_match[i].begin();
  33.                 int tpostmatch = std::mismatch(last_match[i].rbegin(), last_match[i].rbegin() + l2, r.match_stream.rbegin()).first - last_match[i].rbegin();
  34.  
  35.                 if (tprematch+tpostmatch > prematch+postmatch) {
  36.                     prematch = tprematch;
  37.                     postmatch = tpostmatch;
  38.                     ibest = i;
  39.                 }
  40.             }
  41.  
  42.             if (prematch > 7)
  43.                 prematch = 7;
  44.  
  45.             if (postmatch > 7)
  46.                 postmatch = 7;
  47.  
  48.             if (postmatch > l - prematch)
  49.                 postmatch = l - prematch;
  50.  
  51.             dst.push_back(ibest*64 + postmatch*8 + prematch);
  52.             dst.push_back(1+l - prematch - postmatch);
  53.  
  54.             for(x=prematch; x<l - postmatch; ++x) {
  55.                 dst.push_back(r.match_stream[x].first);
  56.                 dst.push_back(r.match_stream[x].second);
  57.             }
  58.  
  59.             decomp_bytes += l*2+1;
  60.  
  61.             std::rotate(last_match, last_match+3, last_match+4);
  62.             last_match[0] = r.match_stream;
  63.  
  64.             uint8 flags = 0;
  65.             if (r.is_66)        flags = 0x80;
  66.             else if (r.is_67)    flags = 0x81;
  67.             else if (r.is_f2)    flags = 0x82;
  68.             else if (r.is_f3)    flags = 0x83;
  69.             else {
  70.                 if (r.is_call)        flags |= 0x01;
  71.                 if (r.is_jcc)        flags |= 0x02;
  72.                 if (r.is_jump)        flags |= 0x04;
  73.                 if (r.is_return)    flags |= 0x08;
  74.                 if (r.is_invalid)    flags |= 0x10;
  75.                 if (r.is_imm8)        flags |= 0x20;
  76.                 if (r.is_imm16)        flags |= 0x40;
  77.                 if (r.is_imm32)        flags |= 0x60;
  78.             }
  79.  
  80.             dst.push_back(flags);
  81.             
  82.             ++decomp_bytes;
  83.         }
  84.  
  85.         dst.push_back(0);
  86.         dst.push_back(0);
  87.  
  88.         decomp_bytes += 2;
  89.     }
  90.  
  91. #ifndef _M_AMD64
  92.     static const char header[64]="[02|02] VirtualDub tracedec module (IA32:P4/Athlon V1.05)\r\n\x1A";
  93. #else
  94.     static const char header[64]="[02|02] VirtualDub tracedec module (AMD64:EM64T/A64 V1.0)\r\n\x1A";
  95. #endif
  96.  
  97.     memcpy(&dst[0], header, 64);
  98.  
  99.     packed_bytes = dst.size() - 72;
  100.     memcpy(&dst[64], &packed_bytes, 4);
  101.  
  102.     decomp_bytes += (rulesys.size()+1)*sizeof(void *);
  103.     memcpy(&dst[68], &decomp_bytes, 4);
  104. }
  105.