Ejemplo n.º 1
0
DWORD WINAPI Thd_Match(PVOID lParam)
  {
  UNREFERENCED_PARAMETER(lParam);
  XLIB_TRY
    {
    const DWORD start_time = GetTickCount();
    mlog << "\r\n\r\n------------------------匹配工作开始------------------------\r\n";
    if(g_file_sig)
      {
      ifstream file(g_filename.c_str(), ios::in | ios::binary);

      if(!file)
        {
        file.close();
        mlog << "无法打开或读取指定的特征码文件:" << g_filename;
        return 0;
        }

      file.seekg(0, ios::end);
      const unsigned int filelen = file.tellg();
      if(filelen == 0)
        {
        file.close();
        mlog << "指定的特征码文件:" << g_filename << "内容为空";
        return 0;
        }
      file.seekg(0, ios::beg);
      g_signature.clear();
      g_signature.reserve(filelen);
      file.read((char*)g_signature.end()._Ptr, filelen);
      g_signature.append(g_signature.end()._Ptr, filelen);
      file.close();

      if(g_create_atom)
        {
        const DWORD atom_start_time = GetTickCount();
        line sig(g_signature);
        const line atoms(signaturematcher::create_atom_by_script(sig));
        if(!atoms.empty())
          {
          string atomname(g_filename);
          const size_t pos = atomname.find_last_of('.');
          if(pos != string::npos)
            {
            atomname.erase(pos);
            }
          atomname.append(".atom");

          ofstream of(atomname.c_str(), ios::out | ios::binary);
          of.write((const char*)atoms.c_str(), atoms.size());
          of.close();
          mlog << "成功解析并写入中间原子文件("
            << (int)(GetTickCount() - atom_start_time)
            << "ms):"<< atomname << "\r\n";
          }
        }
      }

    if(g_blk.start() == nullptr)
      {
      pe tmppe(GetModuleHandle(nullptr));
      g_blk = tmppe.GetImage();
      }

    g_blks.clear();
    g_blks.push_back(g_blk);

    g_report.clear();

    signaturematcher::analy_script(g_signature, match_routine, nullptr);

    if(g_file_sig && !g_report.empty())
      {
      const DWORD h_start_time = GetTickCount();
      xmsg s64, s32, s16, s8, sp;
      const size_t max_name_len = 46;
      for(auto rep : g_report)
        {
        signaturematcher::REPORT_VALUE& rv = rep.second.values;
        xmsg* lpmsg = nullptr;
        xmsg var;
        switch(rv.t)
          {
          case 'q': lpmsg = &s64; var << rv.q;   break;
          case 'd': lpmsg = &s32; var << rv.d;  break;
          case 'w': lpmsg = &s16; var << rv.w;  break;
          case 'b': lpmsg = &s8; var << rv.b;  break;
          case 'p': lpmsg = &sp; var << rv.p;  break;
          default: lpmsg = &s64; var << rv.q;  break;
          }

        if(!(*lpmsg).empty()) (*lpmsg) << "\r\n";
        (*lpmsg) << "  " << rep.first;
        if(rep.first.size() < max_name_len)
          {
          (*lpmsg).append(max_name_len - rep.first.size(),' ');
          }
        (*lpmsg) << "  =  0x" << var << ",";
        if(!rep.second.note.empty())  (*lpmsg) << "  //" << rep.second.note;
        }

      xmsg hmsg;
      hmsg << "#pragma once\r\n\r\n";
      if(g_cx11)
        {
        if(!s64.empty()) hmsg << "enum : unsigned __int64\r\n  {\r\n" << s64 << "\r\n  };\r\n\r\n";
        if(!sp.empty()) hmsg << "enum : size_t\r\n  {\r\n" << sp << "\r\n  };\r\n\r\n";
        if(!s32.empty()) hmsg << "enum : unsigned __int32\r\n  {\r\n" << s32 << "\r\n  };\r\n\r\n";
        if(!s16.empty()) hmsg << "enum : unsigned __int16\r\n  {\r\n" << s16 << "\r\n  };\r\n\r\n";
        if(!s8.empty()) hmsg << "enum : unsigned __int8\r\n  {\r\n" << s8 << "\r\n  };";
        }
      else
        {
        hmsg << "enum\r\n  {";
        if(!s64.empty()) hmsg << "\r\n" << s64;
        if(!sp.empty()) hmsg << "\r\n" << sp;
        if(!s32.empty()) hmsg << "\r\n" << s32;
        if(!s16.empty()) hmsg << "\r\n" << s16;
        if(!s8.empty()) hmsg << "\r\n" << s8;
        hmsg << "\r\n  };";
        }

      string hname(g_filename);
      const size_t pos = hname.find_last_of('.');
      if(pos != string::npos)
        {
        hname.erase(pos);
        }
      hname.append(".h");

      ofstream of(hname.c_str(), ios::out | ios::binary);
      of.write((const char*)hmsg.c_str(), hmsg.size());
      of.close();
      mlog << "成功写入H文件("
        << (int)(GetTickCount() - h_start_time)
        << "ms):" << hname;

      }

    const DWORD end_time = GetTickCount();
    
    mlog << "\r\n------------------------匹配工作结束------------------------\t"
      << (int)(end_time - start_time) << "ms";
    return 0;
    }
  XLIB_CATCH
    {
    ;
    }
  mlog << "----------------匹配工作出现未知异常,失败!----------------";
  return 0;
  }
Ejemplo n.º 2
0
__HOST____DEVICE__
scalar line<point2D, const point2D&>::nearestDist
(
    const line<point2D, const point2D&>& e,
    point2D& thisPt,
    point2D& edgePt
) const
{
    vector2D u = end()-start();
    vector2D v = e.end()-e.start();
    vector2D w = start()-e.start();

    scalar d = u.perp(v);

    if (Foam::mag(d) > VSMALL)
    {
        scalar s = v.perp(w) / d;

        if (s <= SMALL)
        {
            thisPt = start();
        }
        else if (s >= (1-SMALL))
        {
            thisPt = end();
        }
        else
        {
            thisPt = start()+s*u;
        }


        scalar t = u.perp(w) / d;

        if (t <= SMALL)
        {
            edgePt = e.start();
        }
        else if (t >= (1-SMALL))
        {
            edgePt = e.end();
        }
        else
        {
            edgePt = e.start()+t*v;
        }
    }
    else
    {
        // Parallel lines. Find overlap of both lines by projecting onto
        // direction vector (now equal for both lines).

        scalar edge0 = e.start() & u;
        scalar edge1 = e.end() & u;
        bool edgeOrder = edge0 < edge1;

        scalar minEdge = (edgeOrder ? edge0 : edge1);
        scalar maxEdge = (edgeOrder ? edge1 : edge0);
        const point2D& minEdgePt = (edgeOrder ? e.start() : e.end());
        const point2D& maxEdgePt = (edgeOrder ? e.end() : e.start());

        scalar this0 = start() & u;
        scalar this1 = end() & u;
        bool thisOrder = this0 < this1;

        scalar minThis = min(this0, this1);
        scalar maxThis = max(this1, this0);
        const point2D& minThisPt = (thisOrder ? start() : end());
        const point2D& maxThisPt = (thisOrder ? end() : start());

        if (maxEdge < minThis)
        {
            // edge completely below *this
            edgePt = maxEdgePt;
            thisPt = minThisPt;
        }
        else if (maxEdge < maxThis)
        {
            // maxEdge inside interval of *this
            edgePt = maxEdgePt;
            thisPt = nearestDist(edgePt).rawPoint();
        }
        else
        {
            // maxEdge outside. Check if minEdge inside.
            if (minEdge < minThis)
            {
                // Edge completely envelops this. Take any this point and
                // determine nearest on edge.
                thisPt = minThisPt;
                edgePt = e.nearestDist(thisPt).rawPoint();
            }
            else if (minEdge < maxThis)
            {
                // minEdge inside this interval.
                edgePt = minEdgePt;
                thisPt = nearestDist(edgePt).rawPoint();
            }
            else
            {
                // minEdge outside this interval
                edgePt = minEdgePt;
                thisPt = maxThisPt;
            }
        }
    }

    return Foam::mag(thisPt - edgePt);
}