Ejemplo n.º 1
0
 bool book(int start, int end) {
     auto p = make_pair(start, end);
     if(isAlreadyBookingTwice(p))
         return false;
     auto it = intervals.insert(make_pair(p, end));
     if(it != intervals.begin()) {
         auto prevIt = prev(it);
         if(prevIt->second > start) {
             auto newOverlap = make_pair(start, min(prevIt->second, end));
             overlaps.insert(newOverlap);
         }
         it->second = max(prevIt->second, end);
     }
     auto nextIt = next(it);
     while(nextIt != intervals.end() && nextIt->first.first < end) {
         auto newOverlap = make_pair(nextIt->first.first,
                                     min(nextIt->first.second, end));
         overlaps.insert(newOverlap);
         nextIt->second = max(nextIt->second, end);
         nextIt = next(nextIt);
     }
     return true;
 }
Ejemplo n.º 2
0
bool CMesh::RayCast(VECTOR4D &RayOrigin, VECTOR4D &RayDir, multimap<float, INTERSECTIONINFO>& Faces) {
  unsigned long nFaces = (unsigned long)m_Indices.size() / 3;
  unsigned long nBaseIndex = 0;
  unsigned long nIntersectedFaces = 0;

  for (unsigned long iFace = 0; iFace < nFaces; iFace++) {
    VECTOR4D &V0 = m_Vertices[m_Indices[nBaseIndex + 0]].Position;
    VECTOR4D &V1 = m_Vertices[m_Indices[nBaseIndex + 1]].Position;
    VECTOR4D &V2 = m_Vertices[m_Indices[nBaseIndex + 2]].Position;
    VECTOR4D Intersection;

    if (RayCastOnTriangle(V0, V1, V2, RayOrigin, RayDir, Intersection)) {
      float dist = Magnity(Intersection - RayOrigin);
      INTERSECTIONINFO II;
      II.Face = iFace;
      II.LocalPosition = Intersection;
      Faces.insert(make_pair(dist, II));
      nIntersectedFaces++;
    }
    nBaseIndex += 3;
  }
  return nIntersectedFaces > 0;
}
Ejemplo n.º 3
0
	void preprocess() {
		for (int i = 0; i < raw_heads.size(); i++) {
			raw_head_to_body.insert(pair<int,int>(toInt((Lit) raw_heads[i]), i));
		}

		for (int i = 0; i < raw_heads.size(); i++) {
			if (raw_posb[i].size() == 1 && raw_negb[i].size() == 0) {
				if (raw_head_to_body.count(toInt((Lit) raw_posb[i][0])) == 1) {
					int r = raw_head_to_body.find(toInt((Lit) raw_posb[i][0]))->second;
					raw_bl[i] = raw_posb[i][0];
					raw_posb[r].copyTo(raw_posb[i]);
					raw_negb[r].copyTo(raw_negb[i]);
					raw_heads[r] = bv_false;
				}
			}
		}

		for (int i = 0; i < raw_heads.size(); i++) {
			if (raw_heads[i] == bv_false) continue;
			getId(raw_heads[i]);
		}

	}
Ejemplo n.º 4
0
void evaluation_thread(const std::string& proxy_ip, const std::string& proxy_port)
{
	qtk::time::qStopwatch sw;

	sw.start();
	ip::tcp::iostream stream;
	stream.connect(proxy_ip, proxy_port);
	stream << "GET http://www.google.com/intl/en_ALL/images/logo.gif HTTP/1.1\r\n"
	   << "HOST www.google.com\r\n" << "\r\n"
	   << std::flush; 

	std::string response_line;
	std::getline(stream, response_line);
	double time = sw.lap();

	if (response_line.find("200") != std::string::npos)
	{
		boost::mutex::scoped_lock lock(final_proxy_list_mutex);
		final_proxy_list.insert(make_pair(time, proxy_ip + ":" + proxy_port));
	}

	stream.close();
}
Ejemplo n.º 5
0
int main()
{
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    scanf("%d",&n);
    int rs=-1,d;
    for(int i=0;i<n;++i)
    {
        scanf("%d",&a[i]); 
        for(d=1;d<=100;++d)
        {
            ret=s.equal_range(a[i]-d);
            for(it=ret.first; it!=ret.second;++it)
            {   
                f[i][d]=max(f[i][d],f[(*it).second][d]+1);    
            }                  
            if(f[i][d]>rs) rs=f[i][d];
        }
        s.insert(pair<int,int>(a[i],i));
        
    }
    printf("%d",rs+1);
}
Ejemplo n.º 6
0
bool CMesh::RayCast(VECTOR4D &RayOrigin, VECTOR4D &RayDir, multimap<float, INTERSECTIONINFO> &Faces)
{
    unsigned long nFaces = m_Indexes.size() / 3;
    unsigned long nBaseIndex = 0;
    unsigned long nIntersectedFaces = 0;
    for (unsigned long iFace = 0; iFace < nFaces; ++iFace, nBaseIndex+=3)
    {
        VECTOR4D &V0 = m_Vertexes[m_Indexes[nBaseIndex]].Position;
        VECTOR4D &V1 = m_Vertexes[m_Indexes[nBaseIndex + 1]].Position;
        VECTOR4D &V2 = m_Vertexes[m_Indexes[nBaseIndex + 2]].Position;
        VECTOR4D Intersection;
        if (RayCastOnTriangle(V0, V1, V2, RayOrigin, RayDir, Intersection))
        {
            // Distancia entre el origen y esa interseccion
            float dist = Magnity(Intersection - RayOrigin);
            INTERSECTIONINFO II;
            II.Face = iFace;
            II.LocalPosition = Intersection;
            Faces.insert(make_pair(dist, II));
            ++nIntersectedFaces;
        }
    }
    return nIntersectedFaces != 0;
}
Ejemplo n.º 7
0
void CCacheThread::CasValueProcess(const multimap<unsigned int, string> &mapRequestValue, multimap<unsigned int, string> &mapStoredValue)
{
	CVS_XLOG(XLOG_DEBUG, "CCacheThread::%s\n", __FUNCTION__);
	multimap<unsigned int, string>::iterator itr;
	multimap<unsigned int, string>::const_iterator citr;
	for (citr=mapRequestValue.begin(); citr!=mapRequestValue.end(); ++citr)
	{
		itr = mapStoredValue.find(citr->first);
		if(itr==mapStoredValue.end()) 
		{
			mapStoredValue.insert(make_pair(citr->first, citr->second));
		}
		else
		{
			unsigned int dwStoreValue = atoi(itr->second.c_str());
			unsigned int dwNeedAddValue = atoi(citr->second.c_str());
			dwStoreValue += dwNeedAddValue;

			char szTemp[32] = {0};
			snprintf(szTemp, sizeof(szTemp), "%d", dwStoreValue);
			itr->second = szTemp;
		}
	}
}
Ejemplo n.º 8
0
void LiveFeed::MergeFeed(int uid, vector<vector<LiveFeedItem*> >& merge_feeds, multimap<Ice::Long, int>& order_by_time) const{  
  vector<int> update_times;
  map<pair<int, Ice::Long>, int> merge_index;
  for(int i=0; i<feeds_.size(); i++){
    LiveFeedItem* litem = const_cast<LiveFeedItem*>(&feeds_.at(i));
    map<pair<int, Ice::Long>, int>::iterator it = merge_index.find(make_pair<int, Ice::Long>(litem->VerAndSmallType(), litem->merge()));
    if(it != merge_index.end()){
      if(litem->MiniMergeType() == FeedMergeReplace ||
      (litem->MiniMergeType() == FeedMergeAppend && merge_feeds[it->second].size()>=5)){
        continue;  
      }
      merge_feeds[it->second].push_back(litem);
      //update_times[it->second] = litem->time_;
    }else{
        merge_feeds.push_back(vector<LiveFeedItem*>());
        merge_feeds.back().push_back(litem);
        update_times.push_back(litem->time_);
        merge_index.insert(make_pair<pair<int, Ice::Long>, int>(make_pair<int, Ice::Long>(litem->VerAndSmallType(), litem->merge()), merge_feeds.size()-1));
    }
  }
  for(size_t i=0; i<update_times.size(); i++){
    order_by_time.insert(make_pair<Ice::Long, int>(update_times.at(i), i));
  }
}
Ejemplo n.º 9
0
void setmap(multimap<int, char*> &mmap, int *array)
{
	int size_dict = sizeof(dict)/sizeof(dict[1]);
	int k, j, length, p, mul;
	for( k = 0; k<size_dict; k++)
	{
		length = strlen(dict[k]);
		p = 0;
		mul = 1;
		for(j = 0; j<length; j++)
		{
			while(dict[k][j] != freq[p])
			{
				++p;
			}
			
			mul = mul*array[p];
			p = 0;		
		}
		//cout<<mul<<"\n";
		//cout<<dict[k]<<"\n";
		mmap.insert(make_pair(mul,dict[k]));
	}
}
Ejemplo n.º 10
0
/*
multimap<size_t,data> MapUser; //size_t无符号整数
std::multimap<size_t,data>::iterator itu;
multimap<size_t,AdData>MapAd;
using namespace std;
*/
void Read_Data(multimap<size_t,data> &MapUser,multimap<size_t,AdData> &MapAd,string strFileAddress)
{
    // ----------------------open file-----------------------------
    
    FILE *fp;
    const char* address = strFileAddress.c_str();
    fp = fopen(address,"r");
    if (fp == NULL) {
        cout << "can not open file" << endl;
    }
    
    // ----------------------read by line -----------------------------
    int size = 1024;
    char s[1024];
    int count = 0;
    while(fgets(s,size,fp) != NULL)
    {
        //fputs (s, stdout);
        
        int i = 0;
        char *ptr = NULL;
        char *ss = s;
        vector<char*>p;
        ptr = strtok(ss, "\t");
        
        
        while(ptr != NULL){
            //test.push_back(p);
            p.push_back(ptr);
            //cout << i << "=" << p[i] << endl;
            i++;
            ss = NULL;
            ptr = strtok(ss, "\t");
            
        }
        //cout <<0 << "=" << p[1]<< endl;
  
    // ----------------------insert the data -----------------------------
        
        unsigned long int temp = 0;
        data tempdata;
        AdData adtemp;
        vector<data> UserBase;
        vector<AdData>AdBase;
        size_t UserIndex  = 0;
        size_t AdIndex = 0;

    
        
        temp = atoi(p[11]);
        tempdata.Impression = atoi(p[1]);
        tempdata.AdID=atoi(p[3]);
        tempdata.Click=atoi(p[0]);
        tempdata.Depth=atoi(p[5]);
        
        tempdata.Position=atoi(p[6]);
        tempdata.QueryID=atoi(p[7]);
        
        
        tempdata.AdvertiserID = atoi(p[4]);
        tempdata.DescriptionID = atoi(p[10]);
        tempdata.KeywordID = atoi(p[8]);
        tempdata.TitleID = atoi(p[9]);
        tempdata.DisplayURL = p[2];
        
        
        
        adtemp.UserID=temp;
        adtemp.Click = atoi(p[0]);
        adtemp.Impression = atoi(p[1]);
        
        AdBase.push_back(adtemp);
        MapAd.insert(std::multimap<size_t,AdData>::value_type(tempdata.AdID,AdBase[AdIndex]));
        AdIndex++;
        
        UserBase.push_back(tempdata);
        MapUser.insert(std::multimap<size_t,data>::value_type(temp,UserBase[UserIndex]));
        UserIndex++;
        p.clear();
        
       // delete *p;
        
        //cout << "Read from file: " << s << endl;
        
        i = 0;
        count++;
        if(count % 1000000 ==0){
            cout << count << endl;
        }
        
    }

    //cout << test[11] << endl;
    //for (int j = 0; j < 1000; j++){
    //  cout << data.AdID[j] << endl;
    //}
    
    //cout<< UserBase[2].AdID<<endl;
    
    
    fclose(fp);
}
Ejemplo n.º 11
0
/**
 * return public keys or hashes from scriptpubkey, for 'standard' transaction types.
 */
bool solver(const cscript& scriptpubkey, txnouttype& typeret, vector<vector<unsigned char> >& vsolutionsret)
{
    // templates
    static multimap<txnouttype, cscript> mtemplates;
    if (mtemplates.empty())
    {
        // standard tx, sender provides pubkey, receiver adds signature
        mtemplates.insert(make_pair(tx_pubkey, cscript() << op_pubkey << op_checksig));

        // moorecoin address tx, sender provides hash of pubkey, receiver provides signature and pubkey
        mtemplates.insert(make_pair(tx_pubkeyhash, cscript() << op_dup << op_hash160 << op_pubkeyhash << op_equalverify << op_checksig));

        // sender provides n pubkeys, receivers provides m signatures
        mtemplates.insert(make_pair(tx_multisig, cscript() << op_smallinteger << op_pubkeys << op_smallinteger << op_checkmultisig));

        // empty, provably prunable, data-carrying output
        if (getboolarg("-datacarrier", true))
            mtemplates.insert(make_pair(tx_null_data, cscript() << op_return << op_smalldata));
        mtemplates.insert(make_pair(tx_null_data, cscript() << op_return));
    }

    // shortcut for pay-to-script-hash, which are more constrained than the other types:
    // it is always op_hash160 20 [20 byte hash] op_equal
    if (scriptpubkey.ispaytoscripthash())
    {
        typeret = tx_scripthash;
        vector<unsigned char> hashbytes(scriptpubkey.begin()+2, scriptpubkey.begin()+22);
        vsolutionsret.push_back(hashbytes);
        return true;
    }

    // scan templates
    const cscript& script1 = scriptpubkey;
    boost_foreach(const pairtype(txnouttype, cscript)& tplate, mtemplates)
    {
        const cscript& script2 = tplate.second;
        vsolutionsret.clear();

        opcodetype opcode1, opcode2;
        vector<unsigned char> vch1, vch2;

        // compare
        cscript::const_iterator pc1 = script1.begin();
        cscript::const_iterator pc2 = script2.begin();
        while (true)
        {
            if (pc1 == script1.end() && pc2 == script2.end())
            {
                // found a match
                typeret = tplate.first;
                if (typeret == tx_multisig)
                {
                    // additional checks for tx_multisig:
                    unsigned char m = vsolutionsret.front()[0];
                    unsigned char n = vsolutionsret.back()[0];
                    if (m < 1 || n < 1 || m > n || vsolutionsret.size()-2 != n)
                        return false;
                }
                return true;
            }
            if (!script1.getop(pc1, opcode1, vch1))
                break;
            if (!script2.getop(pc2, opcode2, vch2))
                break;

            // template matching opcodes:
            if (opcode2 == op_pubkeys)
            {
                while (vch1.size() >= 33 && vch1.size() <= 65)
                {
                    vsolutionsret.push_back(vch1);
                    if (!script1.getop(pc1, opcode1, vch1))
                        break;
                }
                if (!script2.getop(pc2, opcode2, vch2))
                    break;
                // normal situation is to fall through
                // to other if/else statements
            }

            if (opcode2 == op_pubkey)
            {
                if (vch1.size() < 33 || vch1.size() > 65)
                    break;
                vsolutionsret.push_back(vch1);
            }
            else if (opcode2 == op_pubkeyhash)
            {
                if (vch1.size() != sizeof(uint160))
                    break;
                vsolutionsret.push_back(vch1);
            }
            else if (opcode2 == op_smallinteger)
            {   // single-byte small integer pushed onto vsolutions
                if (opcode1 == op_0 ||
                    (opcode1 >= op_1 && opcode1 <= op_16))
                {
                    char n = (char)cscript::decodeop_n(opcode1);
                    vsolutionsret.push_back(valtype(1, n));
                }
                else
                    break;
            }
            else if (opcode2 == op_smalldata)
            {
                // small pushdata, <= nmaxdatacarrierbytes
                if (vch1.size() > nmaxdatacarrierbytes)
                    break;
            }
            else if (opcode1 != opcode2 || vch1 != vch2)
            {
                // others must match exactly
                break;
            }
        }
    }

    vsolutionsret.clear();
    typeret = tx_nonstandard;
    return false;
}
Ejemplo n.º 12
0
	void addWeight(const K& key, const C& cost)
	{
		auto const weight = w_.calculate(t_.getTimestamp(), cost);
		keyWeightMap_.insert( make_pair(key, weight) );
		weightKeyMap_.insert( make_pair(weight, key) );
	}
Ejemplo n.º 13
0
void parseOptsIntoMultiMap(vector<OptStruct>& opts,multimap<string,string>& optmap){
	for(vector<OptStruct>::iterator i=opts.begin();i!=opts.end();i++)
		optmap.insert(multimap<string,string>::value_type(i->opname,i->opvalue));
}
Ejemplo n.º 14
0
void enabler_inputst::load_keybindings(const string &file) {
  cout << "Loading bindings from " << file << endl;
  interfacefile = file;
  ifstream s(file.c_str());
  if (!s.good()) {
    MessageBox(NULL, (file + " not found, or I/O error encountered").c_str(), 0, 0);
    abort();
  }

  list<string> lines;
  while (s.good()) {
    string line;
    getline(s, line);
    lines.push_back(line);
  }

  static const string bind("[BIND:*:*]");
  static const string sym("[SYM:*:*]");
  static const string key("[KEY:*]");
  static const string button("[BUTTON:*:*]");

  list<string>::iterator line = lines.begin();
  vector<string> match;

  while (line != lines.end()) {
    if (parse_line(*line, bind, match)) {
      map<string,InterfaceKey>::iterator it = bindingNames.right.find(match[1]);
      if (it != bindingNames.right.end()) {
        InterfaceKey binding = it->second;
        // Parse repeat data
        if (match[2] == "REPEAT_FAST")
          repeatmap[(InterfaceKey)binding] = REPEAT_FAST;
        else if (match[2] == "REPEAT_SLOW")
          repeatmap[(InterfaceKey)binding] = REPEAT_SLOW;
        else if (match[2] == "REPEAT_NOT")
          repeatmap[(InterfaceKey)binding] = REPEAT_NOT;
        else {
          repeatmap[(InterfaceKey)binding] = REPEAT_NOT;
          cout << "Broken repeat request: " << match[2] << endl;
        }
        ++line;
        // Add symbols/keys/buttons
        while (line != lines.end()) {
          EventMatch matcher;
          // SDL Keys
          if (parse_line(*line, sym, match)) {
            map<string,SDLKey>::iterator it = sdlNames.right.find(match[2]);
            if (it != sdlNames.right.end()) {
              matcher.mod  = atoi(string(match[1]).c_str());
              matcher.type = type_key;
              matcher.key  = it->second;
              keymap.insert(pair<EventMatch,InterfaceKey>(matcher, (InterfaceKey)binding));
              update_keydisplay(binding, display(matcher));
            } else {
              cout << "Unknown SDLKey: " << match[2] << endl;
            }
            ++line;           
          } // Unicode
          else if (parse_line(*line, key, match)) {
            matcher.type = type_unicode;
            matcher.unicode = decode_utf8(match[1]);
            matcher.mod = KMOD_NONE;
            if (matcher.unicode) {
              keymap.insert(make_pair(matcher, (InterfaceKey)binding));
              if (matcher.unicode < 256) {
                // This unicode key is part of the latin-1 mapped portion of unicode, so we can
                // actually display it. Nice.
                char c[2] = {char(matcher.unicode), 0};
                update_keydisplay(binding, display(matcher));
              }
            } else {
              cout << "Broken unicode: " << *line << endl;
            }
            ++line;
          } // Mouse buttons
          else if (parse_line(*line, button, match)) {
            matcher.type = type_button;
            string str = match[2];
            matcher.button = atoi(str.c_str());
            if (matcher.button) {
              matcher.mod  = atoi(string(match[1]).c_str());
              keymap.insert(pair<EventMatch,InterfaceKey>(matcher, (InterfaceKey)binding));
              update_keydisplay(binding, display(matcher));
            } else {
              cout << "Broken button (should be [BUTTON:#:#]): " << *line << endl;
            }
            ++line;
          } else {
            break;
          }
        }
      } else {
        cout << "Unknown binding: " << match[1] << endl;
		++line;
      }
    } else {
      // Retry with next line
      ++line;
    }
  }
}
void inser(char ch,int num)
{

  mymm.insert(pair<char,int>(ch,num));
}
Ejemplo n.º 16
0
//函数:下载文件
void CCrawl::DownroadFile(CPSEFile *pse_file_ptr,CLinkForPSEFile *link_for_pse_file_ptr,
		CUrl iurl,int nGsock)
{
	char *downroaded_file = NULL,//网页体信息
		*file_header = NULL,//网页头信息
		*location = NULL;//网页重定向

	int file_length = 0;//网页体真实的字节长度
	string str_url_location = "";//保存网页的重定向超链接

	//之后请求的网页和之前请求的网页位于同一个主机上,我们可以利用之前
	//的套接字文件描述符进行通信,这样我们可以节约带宽,节省时间????为何???????????????????????????????
	int nsock = nGsock;//将之前的套接字文件描述符赋值给nsock
	cout<<"PID = "<<GetCurrentThreadId()<<" nsock = "<<nsock<<endl;

	CHttp ihttp;
	//真正的抓取网页的函数,有了URL,搜集系统可以根据URL的标识抓取其对应的网页
	file_length = ihttp.Fetch(iurl.origin_url,&downroaded_file,&file_header,&location,&nsock);

	int location_count = 0;//标识url重定向的次数,如果重定向了3次,我们就不要抓取它对应的网页

	while(file_length == -300)//表明此时重定向了
	{
		//转到其他的地址
		if( strlen(location)> URL_LEN -1 || location_count == 3 ||strlen(location) == 0)
		{
			if(location)
			{
				free(location);
				location = 0;
			}
			file_length = -1;
			break;
		}

		//将获取到的重定向的URL给str_url_location,为下次抓取网页做准备
		str_url_location = location;
		if(location)
		{
			free(location);
			location = 0;
		}

		//因为重定向的URL可能是相对路径,所以我们必须将它转化为绝对路径
		//跟CPage类中提取超链接信息一样
		string::size_type index1 = CStrFunction::FindCase(str_url_location,"http");
		if(index1 != 0)//没有找到http协议
		{
			char c1 = iurl.origin_url.at(iurl.origin_url.length()-1);
			char c2 = str_url_location.at(0);

			if(c2 == '/')//重定向的url一定是相对url
				str_url_location = "http://" + iurl.host_name + str_url_location;
			else if(c1 != '/' && c2 != '/')
			{
				string::size_type index;
				index = iurl.origin_url.rfind('/');
				if(index != string::npos)
				{
					if(index > 6)
					{
						str_url_location = iurl.origin_url.substr(0,index+1) + str_url_location;
					}
					else
						str_url_location = iurl.origin_url + "/" + str_url_location;
				}
				else
				{
					file_length = -1;
					break;
				}
			}
			else
			{
				if(c1 == '/')
					str_url_location = iurl.origin_url + str_url_location;
				else
					str_url_location = iurl.origin_url + "/" + str_url_location;
			}

		}

		CPage ipage;
		if(ipage.IsFilterLink(str_url_location))
		{
			file_length = -1;
			break;
		}

		cout<<"PID= "<<GetCurrentThreadId()<<" sock= "<<nGsock<<endl;
		file_length = ihttp.Fetch(str_url_location,&downroaded_file,&file_header,&location,&nGsock);
		location_count++;


	}//while循环结束

	nGsock = nsock;//将得到的套接字文件描述符给之前的套接字文件描述符,为下次重用做准备

	if(file_length == -1)//其他的各种错误,错误在CHttp的Fetch函数中
	{
		cout<<"error in the page of"<<iurl.origin_url<<endl;
		
		if(file_header)
		{
			free(file_header);
			file_header = NULL;
		}

		if(downroaded_file)
		{
			free(downroaded_file);
			downroaded_file = NULL;
		}

		cout<<"unreach host : "<<iurl.host_name<<endl;
		return ;
	}

	if(file_length == -2)//在ip阻塞范围内
	{
		if(file_header)
		{
			free(file_header);
			file_header = NULL;
		}

		if(downroaded_file)
		{
			free(downroaded_file);
			downroaded_file = NULL;
		}

		SaveUnreachHost(iurl.host_name);
		cout<<"out of block host : "<<iurl.host_name<<endl;
		return ;
	}

	if(file_length == -3)//错误的ip地址
	{
		if(file_header)
		{
			free(file_header);
			file_header = NULL;
		}

		if(downroaded_file)
		{
			free(downroaded_file);
			downroaded_file = NULL;
		}

		cout<<"invalid host : "<<iurl.host_name<<endl;
		return ;
	}

	if(file_length == -4)//图片类型的网页
	{
		if(file_header)
		{
			free(file_header);
			file_header = NULL;
		}

		if(downroaded_file)
		{
			free(downroaded_file);
			downroaded_file = NULL;
		}

		if(ofs_link_for_history_file)
		{
			
		}
		cout<<"image host : "<<iurl.host_name<<endl;
		return ;
	}
	
	//处理正常的网页,只要网页头或网页体的信息有一个为空,我们就认为网页不正常
	if(!file_header || !downroaded_file)
	{
		if(file_header)
		{
			free(file_header);
			file_header = NULL;
		}

		if(downroaded_file)
		{
			free(downroaded_file);
			downroaded_file = NULL;
		}

		closesocket(nGsock);
		nGsock = -1;
		cout<<"not the nomal host"<<endl;
		return ;
	}

	//将抓取的网页信息放入到CPage类中
	CPage ipage(iurl.origin_url,str_url_location,file_header,downroaded_file,file_length);

	if(file_header)
	{
		free(file_header);
		file_header = NULL;
	}

	if(downroaded_file)
	{
		free(downroaded_file);
		downroaded_file = NULL;
	}
	
	//解析网页头信息
	ipage.ParseHeaderInfo(ipage.header);

	if(ipage.connection_state == false)
	{
		closesocket(nGsock);
		nGsock = -1;
	}

	//过滤掉不是我们想要的网页体信息,注意?????????????????????????????????忽略了图片形式的网页
	if(ipage.content_type != "text/html"
		&&ipage.content_type != "text/plain"
		&&ipage.content_type != "text/xml"
		&&ipage.content_type != "application/msword"
		&&ipage.content_type != "applicaiton/pdf"
		&&ipage.content_type != "text/rtf"
		&&ipage.content_type != "applicaiton/postscript"
		&&ipage.content_type != "applicaiton/vnd.ms-excel"
		&&ipage.content_type != "application/vnd.ms-powerpoint")
	{
		cout<<"unwant host type"<<iurl.host_name<<endl;
		return ;
	}

	//解压缩
	//如果是gzip编码,要解压缩,然后提取超链接信息
/*	char unzip_content_buffer[1024000];
	int unzip_length = 0;

	if(ipage.content_encoding == "gzip" && ipage.content_type == "text/html")
	{
		gzFile zip;
		string ofs_gzip_name;
		ofs_gzip_name = CStrFunction::itos(GetCurrentThreadId()) + ".gz";

		//以二进制截断的方式打开文件
		//ios::trunc 如果文件存在,则将文件长度截断为0,并清除文件的内容,如果文件不存在,则创建该文件
		ofstream ofs_downroad_file(ofs_gzip_name.c_str(),ios::trunc|ios::binary);

		cout<<"file length : "<<endl;
		ofs_downroad_file.write(ipage.body_content.c_str(),ipage.body_content_length);
		ofs_downroad_file.close();

		zip = gzopen(ofs_gzip_name.c_str(),"rb");

		if(zip == NULL)
		{
			cout<<"open zip file "<<ofs_gzip_name.c_str()<<" error ."<<endl;
			exit(-1);

		}

		//解压缩过程,将解压缩后的网页体信息放入到缓冲区域unzip_content_buffer
		unzip_length = gzread(zip,unzip_content_buffer,1024000);
		if(unzip_length == -1)
		{
			cout<<"read zip file "<<ofs_gzip_name.c_str()<<" error ."<<endl;
			exit(-1);
		}

		unzip_content_buffer[unzip_length] = 0;
		gzclose(zip);
	}//解压缩过程结束

	*/
	CMD5 imd5;
	string str_digest;

	//判断该URL是否在set_visited_url_md5中,在返回;不在加到其中,并保存
	imd5.GenerateMd5((unsigned char *)iurl.origin_url.c_str(),iurl.origin_url.length());//生成md5码
	str_digest = imd5.ToString();

	WaitForSingleObject(mutex_visited_url_md5,INFINITE);

	if(set_visited_url_md5.find(str_digest) != set_visited_url_md5.end())//表明已经抓取过
	{
		cout<<"the url :"<<iurl.origin_url.c_str()<<" have crawled !"<<endl;

		ReleaseMutex(mutex_visited_url_md5);
		return ;
	}

	//不在set_visited_url_md5中,现在必须插入set_visited_url_md5中
	//因为该URL现在已经访问过了
	set_visited_url_md5.insert(str_digest);
	SaveVisitedUrlMd5(str_digest);
	ReleaseMutex(mutex_visited_url_md5);

	//判断该网页体是否已经访问过,访问过返回,没有访问过加到set_visited_page_md5集合中
	imd5.GenerateMd5((unsigned char *)ipage.body_content.c_str(),ipage.body_content.length());
	str_digest = imd5.ToString();

	WaitForSingleObject(mutex_visited_page_md5,INFINITE);
	//网页体MD5同URL的关系插入到容器replicas中
	replicas.insert(pair<string,string>(str_digest,iurl.origin_url));

	if(set_visited_page_md5.find(str_digest) != set_visited_page_md5.end())//表明出现了镜像文件
	{
		cout<<"the page  have crawled !"<<endl;

		ReleaseMutex(mutex_visited_page_md5);
		return ;
	}

	//不在set_visited_page_md5中,现在必须插入set_visited_page_md5中
	//因为该URL现在已经访问过了
	set_visited_page_md5.insert(str_digest);
	SaveVisitedPageMd5(str_digest);
	ReleaseMutex(mutex_visited_page_md5);
	

	//将抓取到的网页以PSE格式放到原始网页库中
	SavePseRawData(pse_file_ptr,&iurl,&ipage);

	if(ipage.location.length()<1)
	{
		SaveVisitedUrl(iurl.origin_url);
	}
	else
		SaveVisitedUrl(ipage.location);

	if(ipage.content_type != "text/html")//只可以在text/html中发现超链接
		return ;

//=====================================保存单个网页的所有连接信息

	if (ipage.ParseHyperLinks() == false)
	{
		return;
	}
	
	SaveLinkForPSE( &ipage);
	SaveLinkForHistory( &ipage);

	map<string,string>::iterator it = ipage.map_link_for_pse.begin();
	string str;
	for( ; it!= ipage.map_link_for_pse.end(); ++it )
	{
		str = (*it).first;
		AddUrl( str.c_str() );

	}

//========================================

	return ;
}
Ejemplo n.º 17
0
void* read_module_symbols(void* input)
{
    // Get module name and offset from input parameter.
    pair<string, uint64_t>* mod_info =
        reinterpret_cast<pair<string,uint64_t>*>(input);
    const string& module = mod_info->first;
    uint64_t addr = mod_info->second;

    // Determine the full path to the module based on the base image path.
    // Assumes they are in the same subdirectory.
    string module_path = g_imageName.substr(0, g_imageName.rfind('/') + 1) +
                         module;

    // Create the 'objdump' command for finding all the symbols and start as
    // a sub-process.
    string command = string(g_crossPrefix) + string("objdump --syms -C ") +
                     module_path;
    FILE* pipe = popen(command.c_str(), "r");
    if (NULL == pipe) return NULL;

    // Local symbol map (to reduce contention on the global symbol map).
    //   No need to use the overhead of a map because we don't care about
    //   order at this point.
    vector<pair<uint64_t, string> > l_symbols;

    // Parse each line of the 'objdump' output.
    char line[1024];
    do
    {
        if (NULL == fgets(line, 1024, pipe)) break;

        size_t linelen = strlen(line);

        // Skip absolute values (ex. constants) and undefined symbols.
        if (strstr(line, "*ABS*") || strstr(line, "*UND*")) continue;
        // Skip section symbols (marked by 'd' in the 22nd column).
        if (linelen > 22 && 'd' == line[22]) continue;

        // First part of an objdump line is the symbol address, parse that.
        uint64_t line_address;
        if (1 != sscanf(line, "%16lx", &line_address)) continue;
        line_address += addr;

        // Determine if the symbol is a function and if it is in the .rodata
        // section.  Symbols in the .rodata section have a slightly longer
        // line than those in the .text/.data sections (by 2 characters).
        bool is_function = (linelen > 23 && 'F' == line[23]);
        size_t rodata = (NULL != strstr(line, ".rodata")) ? 2 : 0;

        // Parse the symbol size.
        uint64_t symbol_size;
        if (linelen > 32+rodata &&
            1 != sscanf(&line[32+rodata], "%lx", &symbol_size)) continue;

        // Parse the function name.
        assert(linelen > 48+rodata);
        string function = &line[48+rodata];
        function.resize(function.length() - 1); // remove the newline.

        // Function have two addresses: TOC entry and code address.  Objdump
        // gives the TOC entry, so we need to read the file itself to determine
        // the code address.  The first part of the TOC entry is the code
        // address.
        uint64_t code_addr = 0;
        if (is_function)
        {
            // Module is in the extended image, read from it.
            if (line_address > g_extImageOffset)
            {
                // Read code address.
                assert((line_address - g_extImageOffset) < g_extImageFileSize);
                memcpy(&code_addr,
                       &g_extImageFile[line_address - g_extImageOffset], 8);
            }
            // Module is in the base image.
            else
            {
                // Read code address.
                assert(line_address < g_imageFileSize);
                memcpy(&code_addr, &g_imageFile[line_address], 8);
            }
            // Fix up the endianness.
            code_addr = be64toh(code_addr);

            std::swap(code_addr, line_address);
        }

        // Print all of this into a new line and add to the symbol map.
        sprintf(line, "%c,%08lx,%08lx,%08lx,%s\n",
            is_function ? 'F' : 'V',
            line_address, code_addr, symbol_size,
            function.c_str());

        l_symbols.push_back(make_pair(line_address, line));

    } while(1);

    // Close subprocess (done).
    pclose(pipe);

    // Copy our local symbol list all at once into the global symbol list.
    pthread_mutex_lock(&g_symbolMutex);
    g_symbols.insert(l_symbols.begin(), l_symbols.end());
    pthread_mutex_unlock(&g_symbolMutex);

    return NULL;
}
Ejemplo n.º 18
0
int main() {
    
    int n;
    
    while(cin >> n >> ws) {
        
        // reset.
        vexp.clear();
        vvar.clear();
        
        vals.clear();
        expr.clear();
        
        vreq.assign(n, set<string>());
        vidx.clear();
        
        q.clear();
        
        broken = false;
        ans = 0;
        result.clear();
        

        // input and calculation.
        for(int i = 0; i < n; ++i) {
            
            //cout << "i = " << i << endl;
            
            string s;
            getline(cin, s);
            int p = s.find(' ');
            
            vvar.push_back(s.substr(0, p));
            vexp.push_back(s.substr(p + 3));
            
            //cout << "vvar = |" << vvar[i] << "|" << endl;
            //cout << "vexp = |" << vexp[i] << "|" << endl;
            
            
            // scratch all the reference variables.
            // add sentienl to split.
            istringstream iss(vexp.back());
            string v;
            
            while(iss >> v) {
                
                // if variable gotcha.
                if(isalpha(v[0])) {
                    
                    // initialize variable list
                    vals[v] = 0;
                    expr[v] = "";
                    
                    // add requirement link
                    vreq[i].insert(v);
                    
                    // add index link, AVOID repeatly link.
                    if(vidx[v].size() == 0 || vidx[v].back() != i) { 
                        vidx[v].push_back(i);
                    }
                }
                
            }

            // if the value is clear.
            if(vreq[i].size() == 0) {
                
                i64 val = eval(vexp.back());
                
                q.insert(make_pair(val, i));
                
            }
            
        }
        
        // proccess
        while(!q.empty()) {
            
            // pop from priority queue.
            i64 val = q.begin()->first;
            int i = q.begin()->second;
            q.erase(q.begin());
            
            // get var.
            string v = vvar[i];
            
            if(vals[v] == 0) {
            
                result.push_back(i);
                
                vals[v] = val;
                expr[v] = vexp[i];
                
                for(int j = 0; j < vidx[v].size(); ++j) {
                    
                    int k = vidx[v][j];
                    
                    // kick the link.
                    vreq[k].erase(v);
                    
                    // if all requirement was calculated.
                    if(vreq[k].empty()) {
                
                        i64 val = eval(vexp[k]);
                        
                        if(val != 0) q.insert(make_pair(val, k));
                        
                    }
                }
                
            }
            
        }
        
        
        // reduce..
        if(result.size() < vals.size()) broken = true;
        
        for(map<string, i64>::iterator it = vals.begin(); it != vals.end(); ++it) {
            
            // if overflow
            if(ans + it->second < ans) {
                broken = true;
                break;
            }
            
            ans += it->second;
            
        }
        
        if(broken) {
            cout << "stupid expressions!" << endl;
        }
        else {
            cout << ans << endl;
            for(int i = 0; i < result.size(); ++i) {
                int j = result[i];
                cout << vvar[j] << " = " << vexp[j] << endl;
            }
        }
        
    }
    
    return 0;
    
}
Ejemplo n.º 19
0
/**
 * Return public keys or hashes from scriptPubKey, for 'standard' transaction types.
 */
bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, vector<vector<unsigned char> >& vSolutionsRet)
{
    // Templates
    static multimap<txnouttype, CScript> mTemplates;
    if (mTemplates.empty())
    {
        // Standard tx, sender provides pubkey, receiver adds signature
        mTemplates.insert(make_pair(TX_PUBKEY, CScript() << OP_PUBKEY << OP_CHECKSIG));

        // Bitcoin address tx, sender provides hash of pubkey, receiver provides signature and pubkey
        mTemplates.insert(make_pair(TX_PUBKEYHASH, CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG));

        // Sender provides N pubkeys, receivers provides M signatures
        mTemplates.insert(make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));

        // Empty, provably prunable, data-carrying output
        if (GetBoolArg("-datacarrier", true))
            mTemplates.insert(make_pair(TX_NULL_DATA, CScript() << OP_RETURN << OP_SMALLDATA));
        mTemplates.insert(make_pair(TX_NULL_DATA, CScript() << OP_RETURN));
    }

    // Shortcut for pay-to-script-hash, which are more constrained than the other types:
    // it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
    if (scriptPubKey.IsPayToScriptHash())
    {
        typeRet = TX_SCRIPTHASH;
        vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
        vSolutionsRet.push_back(hashBytes);
        return true;
    }

    if (IsCryptoConditionsEnabled()) {
        // Shortcut for pay-to-crypto-condition
        CScript ccSubScript = CScript();
        std::vector<std::vector<unsigned char>> vParams;
        if (scriptPubKey.IsPayToCryptoCondition(&ccSubScript, vParams))
        {
            if (scriptPubKey.MayAcceptCryptoCondition())
            {
                typeRet = TX_CRYPTOCONDITION;
                vector<unsigned char> hashBytes; uint160 x; int32_t i; uint8_t hash20[20],*ptr;;
                x = Hash160(ccSubScript);
                memcpy(hash20,&x,20);
                hashBytes.resize(20);
                ptr = hashBytes.data();
                for (i=0; i<20; i++)
                    ptr[i] = hash20[i];
                vSolutionsRet.push_back(hashBytes);
                if (vParams.size())
                {
                    COptCCParams cp = COptCCParams(vParams[0]);
                    if (cp.IsValid())
                    {
                        for (auto k : cp.vKeys)
                        {
                            vSolutionsRet.push_back(std::vector<unsigned char>(k.begin(), k.end()));
                        }
                    }
                }
                return true;
            }
            return false;
        }
    }

    // Scan templates
    const CScript& script1 = scriptPubKey;
    BOOST_FOREACH(const PAIRTYPE(txnouttype, CScript)& tplate, mTemplates)
    {
        const CScript& script2 = tplate.second;
        vSolutionsRet.clear();

        opcodetype opcode1, opcode2;
        vector<unsigned char> vch1, vch2;

        // Compare
        CScript::const_iterator pc1 = script1.begin();
        CScript::const_iterator pc2 = script2.begin();
        while (true)
        {
            if (pc1 == script1.end() && pc2 == script2.end())
            {
                // Found a match
                typeRet = tplate.first;
                if (typeRet == TX_MULTISIG)
                {
                    // Additional checks for TX_MULTISIG:
                    unsigned char m = vSolutionsRet.front()[0];
                    unsigned char n = vSolutionsRet.back()[0];
                    if (m < 1 || n < 1 || m > n || vSolutionsRet.size()-2 != n)
                        return false;
                }
                return true;
            }
            if (!script1.GetOp(pc1, opcode1, vch1))
                break;
            if (!script2.GetOp(pc2, opcode2, vch2))
                break;

            // Template matching opcodes:
            if (opcode2 == OP_PUBKEYS)
            {
                while (vch1.size() >= 33 && vch1.size() <= 65)
                {
                    vSolutionsRet.push_back(vch1);
                    if (!script1.GetOp(pc1, opcode1, vch1))
                        break;
                }
                if (!script2.GetOp(pc2, opcode2, vch2))
                    break;
                // Normal situation is to fall through
                // to other if/else statements
            }

            if (opcode2 == OP_PUBKEY)
            {
                if (vch1.size() < 33 || vch1.size() > 65)
                    break;
                vSolutionsRet.push_back(vch1);
            }
            else if (opcode2 == OP_PUBKEYHASH)
            {
                if (vch1.size() != sizeof(uint160))
                    break;
                vSolutionsRet.push_back(vch1);
            }
            else if (opcode2 == OP_SMALLINTEGER)
            {   // Single-byte small integer pushed onto vSolutions
                if (opcode1 == OP_0 ||
                    (opcode1 >= OP_1 && opcode1 <= OP_16))
                {
                    char n = (char)CScript::DecodeOP_N(opcode1);
                    vSolutionsRet.push_back(valtype(1, n));
                }
                else
                    break;
            }
            else if (opcode2 == OP_SMALLDATA)
            {
                // small pushdata, <= nMaxDatacarrierBytes
                if (vch1.size() > nMaxDatacarrierBytes)
                {
                    //fprintf(stderr,"size.%d > nMaxDatacarrier.%d\n",(int32_t)vch1.size(),(int32_t)nMaxDatacarrierBytes);
                    break;
                }
            }
            else if (opcode1 != opcode2 || vch1 != vch2)
            {
                // Others must match exactly
                break;
            }
        }
    }

    vSolutionsRet.clear();
    typeRet = TX_NONSTANDARD;
    return false;
}
Ejemplo n.º 20
0
void add_new_book(multimap<string, string> &books, const string &name, const string &author){
	books.insert({name, author});
}
Ejemplo n.º 21
0
bool
XMLIndata::parseContent(const DOMNode* content,
                        multimap<MC2String, ItemIdentifier*>& contents )
{
   if ( XMLString::equals( content->getNodeName(), "content" ) ) {
      
      // Get attributes
      DOMNamedNodeMap* attributes = content->getAttributes();

      
      // Note that map_ident attribute is #REQUIRED.
      DOMNode* attribute = 
         attributes->getNamedItem( X( "map_ident" ) );
      MC2_ASSERT( attribute != NULL );
      char* tmpStr = XMLUtility::transcodefromucs(
            attribute->getNodeValue() );
      MC2String mapIdent( tmpStr );
      delete [] tmpStr;

      // Get children
      DOMNode* child = content->getFirstChild();

      while ( child != NULL ) {
         switch ( child->getNodeType() ) {
            case DOMNode::ELEMENT_NODE :
               // See if the element is a known type
               if ( XMLString::equals( child->getNodeName(),
                                       "item_ident" ) )
               {
                  
                  // Parse item ident
                  ItemIdentifier* itemIdent = new ItemIdentifier();
                  parseItemIdent( child, *itemIdent );

                  contents.insert( make_pair( mapIdent, itemIdent ) );
                  
               } else if ( XMLString::equals( child->getNodeName(),
                                              "whole_map" ) )
               {
                  
                  // Content is all of the specified map.
                  contents.insert( make_pair( mapIdent, 
                                              (ItemIdentifier*) NULL ) );
               
               } else {
                  mc2log << warn 
                         << "XMLIndata::parseContent:"
                         << " odd Element in content element: " 
                         << child->getNodeName() << endl;
               }
               break;
            case DOMNode::COMMENT_NODE :
               // Ignore comments
               break;
            default:
               mc2log << warn 
                      << "XMLIndata::parseContent odd "
                      << "node type in content element: " 
                      << child->getNodeName() 
                      << " type " << child->getNodeType() << endl;
               break;
         }
         child = child->getNextSibling();
      }
      
      return true; 
      
   } else {
      // Not a content node.
      mc2log << warn 
             << "XMLIndata::parseContent:"
             << " not a content node."
             << content->getNodeName() << endl;
      return false;
   }
}
Ejemplo n.º 22
0
	Solution(vector<int> nums) {
		for (int i = 0; i < nums.size(); ++i) {
			mmnums.insert(make_pair(nums[i], i));
		}
	}
Ejemplo n.º 23
0
void TelephoneDirectory::add_entry(string name, int number)
{
   database[name] = number;
   inverse_database.insert(make_pair(number, name));
}
Ejemplo n.º 24
0
void add_child(const string &family, const string &child){
	families.insert({family, child});
}
Ejemplo n.º 25
0
	void addInstance(double dInput, const double* pOutputs)
	{
		m_instances.insert(make_pair(dInput, pOutputs));
	}
Ejemplo n.º 26
0
bool ProcessConfigFile (const string& rktCONFIG, multimap<string, string>& rtMAP)
{

  string     tOptionName, tOptionValue;
  ifstream   tConfigFile;
  char*      pcBuffer = new char[200];

  tConfigFile.open (rktCONFIG.c_str());
  if ( !tConfigFile )
  {
    return false;
  }

  // Reads configuration file line by line
  while ( !tConfigFile.eof() )
  {
    Byte   J;
    bool   gAppend;
    
    tOptionName  = "";
    tOptionValue = "";
    gAppend      = false;
    
    tConfigFile.getline (pcBuffer, 200);

    // Checks for an empty line
    if ( strlen (pcBuffer) == 0 )
    {
      continue;
    }
    
    // Checks for a comment
    if ( pcBuffer[0] == '#' )
    {
      continue;
    }
    
    // Gets option name (until the first occurrence of character '=')
    for (J = 0; ( ( J < strlen (pcBuffer) ) && ( pcBuffer[J] != '=' ) ) ;J++)
    {
      tOptionName += pcBuffer[J];
    }

    // Checks if the line contains no '=' character
    if ( J == strlen (pcBuffer) )
    {
      continue;
    }
    
    // Checks if we are adding a new value for that option, or substituting previous ones
    if ( ( J > 0 ) && ( pcBuffer[J - 1] == '+' ) )
    {
      tOptionName = tOptionName.substr (0, J - 1);
      gAppend     = true;
    }
         
    // Gets option value (until the end of the line)
    for (J++; ( J < strlen (pcBuffer) ) ;J++)
    {
      tOptionValue += pcBuffer[J];
    }

    if ( !gAppend )
    {
      // Removes every previous option with the same name
      multimap<string, string>::iterator   iter;
      
      while ( (iter = rtMAP.find (tOptionName)) != rtMAP.end() )
      {
        rtMAP.erase (iter);
      }
    }

    if( tOptionValue[0] != '/' )
    {
      tOptionValue = _tLocalPath + tOptionValue;
    }

    // Inserts a new option entry in the map
    rtMAP.insert (pair<const string, string> (tOptionName, tOptionValue));
  }

  tConfigFile.close();
  delete pcBuffer;
  
  return true;
  
}  /* ProcessConfigFile() */
Ejemplo n.º 27
0
int recover_block_from_disk_data(const char* ns_addr, const char* ns_slave_addr, BlockManager& block_manager,
    VUINT64& tmp_fail_block_list, VUINT64& no_need_recover_block_list, VUINT64& success_block_list,
    multimap<uint64_t, uint64_t>& fail_block_file_list, VUINT64& fail_block_list)
{
  int ret = TFS_SUCCESS;
  VUINT64 blocks;
  vector<FileInfoV2> finfos;
  multimap<uint64_t, uint64_t> tmp_fail_block_file_list;
  uint64_t ns_id = Func::get_host_ip(ns_addr);

  //get all blocks from ds disk
  block_manager.get_all_block_ids(blocks);//肯定返回成功

  vector<BlockMeta> blocks_meta;
  ret = ToolUtil::get_all_blocks_meta(ns_id, blocks, blocks_meta, false);// no need get check block
  TBSYS_LOG(DEBUG , "all logic blocks count: %zd, data block count: %zd", blocks.size(), blocks_meta.size());
  if (TFS_SUCCESS == ret)
  {
    int32_t bret = TFS_SUCCESS;
    for (uint32_t block_index = 0; block_index < blocks_meta.size(); ++block_index)
    {
      BlockMeta blk_meta = blocks_meta.at(block_index);
      uint64_t block_id = blk_meta.block_id_;
      if (blk_meta.size_ > 0 || INVALID_FAMILY_ID != blk_meta.family_info_.family_id_)
      {
        TBSYS_LOG(DEBUG , "blockid: %" PRI64_PREFIX "u  no need recover, ds_size:%d, family_id: %" PRI64_PREFIX "u",
            block_id, blk_meta.size_, blk_meta.family_info_.family_id_);
        no_need_recover_block_list.push_back(block_id);//只要还有副本或者副本丢失但有编组(不考虑退化读恢复)都不用本工具恢复
      }
      else
      {
        bret = rm_no_replicate_block_from_ns(ns_addr, block_id);//now T_NEWBLK will can not remove empty ds_list's block
        if (TFS_SUCCESS != bret)
        {
          fail_block_list.push_back(block_id);
          TBSYS_LOG(WARN , "remove block %" PRI64_PREFIX "u from ns: %s failed, ret: %d", block_id, ns_addr, bret);
        }
        else
        {
          IndexHeaderV2 header;
          finfos.clear();
          bret = block_manager.traverse(header, finfos, block_id, block_id);
          if (TFS_SUCCESS != bret)
          {
            TBSYS_LOG(WARN , "block %" PRI64_PREFIX "u get local file infos failed, ret: %d", block_id, bret);
            tmp_fail_block_list.push_back(block_id);//只有本磁盘读取block的文件index错误,才需要整个block尝试从辅集群恢复
          }
          else
          {
            bool all_success = true;
            int32_t copy_file_succ_count = 0;
            tmp_fail_block_file_list.clear();
            for (uint32_t file_index = 0; file_index < finfos.size(); ++file_index)
            {
              // skip deleted file
              if ((finfos.at(file_index).status_ & FILE_STATUS_DELETE) != 0)
                continue;

              uint64_t file_id = finfos.at(file_index).id_;
              bret = copy_file(block_manager, block_id, finfos.at(file_index));
              if (TFS_SUCCESS == bret)
              {
                TBSYS_LOG(DEBUG, "recover block_id: %" PRI64_PREFIX "u, file_id: %" PRI64_PREFIX "u successful!", block_id, file_id);
              }
              else
              {// 如果磁盘中该文件数据已经损坏(如crc出错),则从对等集群(ns_slave_addr)拷贝数据复制
                bret = copy_file_from_slave_cluster(ns_slave_addr, ns_addr, block_id, finfos.at(file_index));
                if (TFS_SUCCESS == bret)
                {
                  TBSYS_LOG(DEBUG, "recover block_id: %" PRI64_PREFIX "u, file_id: %" PRI64_PREFIX "u successful from slave cluster!", block_id, file_id);
                }
                else
                {
                  TBSYS_LOG(WARN, "recover block_id: %" PRI64_PREFIX "u, file_id: %" PRI64_PREFIX "u failed from slave cluster, ret: %d!", block_id, file_id, bret);
                  all_success = false;
                  tmp_fail_block_file_list.insert(pair<uint64_t, uint64_t>(block_id, file_id));
                }
              }
              if (TFS_SUCCESS == bret)
              {
                ++copy_file_succ_count;
              }
            }

            if (all_success)
            {
              success_block_list.push_back(block_id);
              if (0 == copy_file_succ_count)
              {
                TBSYS_LOG(DEBUG, "recover block_id: %" PRI64_PREFIX "u need to do nothing,"
                    " because the count of files who need to recover is ZERO except DELETE status files!", block_id);
              }
            }
            else if (0 == copy_file_succ_count)// all file(exclude DELETE) fail
            {
              TBSYS_LOG(WARN, "recover block_id: %" PRI64_PREFIX "u's files failed, copy_file_succ_count is Zero !", block_id);
              fail_block_list.push_back(block_id);// for print fail block to out log file at end
            }
            else
            {
              fail_block_file_list.insert(tmp_fail_block_file_list.begin(), tmp_fail_block_file_list.end());
            }
          }
        }
      }
    }//end for blocks loop
  }
  else
  {
    TBSYS_LOG(WARN, "get blockis ds_list error, ret:%d", ret);
  }
  TBSYS_LOG(INFO, "success_block_list size: %zd, tmp_fail_block_list size: %zd, fail_block_list size: %zd",
      success_block_list.size(), tmp_fail_block_list.size(), fail_block_list.size());
  return ret;
}
Ejemplo n.º 28
0
static void __handle_mmap_event(event_t * event)
{
	static bool kptr_restrict_warning_displayed_already = false;
	string image_basename = op_basename(event->mmap.filename);
	struct operf_mmap * mapping = NULL;
	multimap<string, struct operf_mmap *>::iterator it;
	pair<multimap<string, struct operf_mmap *>::iterator,
	     multimap<string, struct operf_mmap *>::iterator> range;

	range = all_images_map.equal_range(image_basename);
	for (it = range.first; it != range.second; it++) {
		if (((strcmp((*it).second->filename, image_basename.c_str())) == 0)
				&& ((*it).second->start_addr == event->mmap.start)) {
			mapping = (*it).second;
			break;
		}
	}
	if (!mapping) {
		mapping = new struct operf_mmap;
		memset(mapping, 0, sizeof(struct operf_mmap));
		mapping->start_addr = event->mmap.start;
	        strcpy(mapping->filename, event->mmap.filename);
		/* Mappings starting with "/" are for either a file or shared memory object.
		 * From the kernel's perf_events subsystem, anon maps have labels like:
		 *     [heap], [stack], [vdso], //anon
		 */
		if (mapping->filename[0] == '[') {
			mapping->is_anon_mapping = true;
		} else if ((strncmp(mapping->filename, "//anon",
		                    strlen("//anon")) == 0)) {
			mapping->is_anon_mapping = true;
			strcpy(mapping->filename, "anon");
		}
		mapping->end_addr = (event->mmap.len == 0ULL)? 0ULL : mapping->start_addr + event->mmap.len - 1;
		mapping->pgoff = event->mmap.pgoff;

		if (cverb << vconvert) {
			cout << "PERF_RECORD_MMAP for process " << hex << event->mmap.pid << "/"
			     << event->mmap.tid << ": " << event->mmap.filename << endl;
			cout << "\tstart_addr: " << hex << mapping->start_addr
			     << "; end addr: " << mapping->end_addr << endl;
		}

		if (event->header.misc & PERF_RECORD_MISC_USER)
			all_images_map.insert(pair<string, struct operf_mmap *>(image_basename, mapping));
	}

	if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
		if (!strncmp(mapping->filename, operf_get_vmlinux_name(),
		            strlen(mapping->filename))) {
			/* The kernel_mmap is just a convenience variable
			 * for use when mapping samples to kernel space, since
			 * most of the kernel samples will be attributable to
			 * the vmlinux file versus kernel modules.
			 */
			kernel_mmap = mapping;
		} else {
			if ((kptr_restrict == 1) && !no_vmlinux && (my_uid != 0)) {
				if (!kptr_restrict_warning_displayed_already) {
					kptr_restrict_warning_displayed_already = true;
					cerr << endl << "< < < WARNING > > >" << endl;
					cerr << "Samples for vmlinux kernel will be recorded, but kernel module profiling"
					     << endl << "is not possible with current system config." << endl;
					cerr << "Set /proc/sys/kernel/kptr_restrict to 0 to see samples for kernel modules."
					     << endl << "< < < < < > > > > >" << endl << endl;
				}
			} else {
				operf_create_module(mapping->filename,
				                    mapping->start_addr,
				                    mapping->end_addr);
				kernel_modules[mapping->start_addr] = mapping;
			}
		}
	} else {
		map<pid_t, operf_process_info *>::iterator it;
		it = process_map.find(event->mmap.pid);
		if (it == process_map.end()) {
			/* Create a new proc info object, but mark it invalid since we have
			 * not yet received a COMM event for this PID. This MMAP event may
			 * be on behalf of a process created as a result of a fork/exec.
			 * The order of delivery of events is not guaranteed so we may see
			 * this MMAP event before getting the COMM event for that process.
			 * If this is the case here, we just pass NULL for appname arg.
			 * It will get fixed up later when the COMM event occurs.
			 */
			const char * appname_arg;
			bool is_complete_appname;
			if (app_name && (app_PID == event->mmap.pid)) {
				appname_arg = app_name;
				is_complete_appname = true;
			} else {
				appname_arg = NULL;
				is_complete_appname = false;
			}

			operf_process_info * proc = new operf_process_info(event->mmap.pid, appname_arg,
			                                                   is_complete_appname, false);
			process_map[event->mmap.pid] = proc;
			proc->process_mapping(mapping, false);
		} else {
			it->second->process_mapping(mapping, false);
		}
		if (cverb << vconvert)
			cout << "Process mapping for " << event->mmap.filename << " on behalf of "
			<< event->mmap.pid << endl;
	}
}
Ejemplo n.º 29
0
/**
 * Return public keys or hashes from scriptPubKey, for 'standard' transaction types.
 */
bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, vector<vector<unsigned char> >& vSolutionsRet)
{
    // Templates
    static multimap<txnouttype, CScript> mTemplates;
    if (mTemplates.empty())
    {
        // Standard tx, sender provides pubkey, receiver adds signature
        mTemplates.insert(make_pair(TX_PUBKEY, CScript() << OP_PUBKEY << OP_CHECKSIG));

        // Uniqredit address tx, sender provides hash of pubkey, receiver provides signature and pubkey
        mTemplates.insert(make_pair(TX_PUBKEYHASH, CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG));

        // Sender provides N pubkeys, receivers provides M signatures
        mTemplates.insert(make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));
    }

    vSolutionsRet.clear();

    // Shortcut for pay-to-script-hash, which are more constrained than the other types:
    // it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
    if (scriptPubKey.IsPayToScriptHash())
    {
        typeRet = TX_SCRIPTHASH;
        vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
        vSolutionsRet.push_back(hashBytes);
        return true;
    }

    // Provably prunable, data-carrying output
    //
    // So long as script passes the IsUnspendable() test and all but the first
    // byte passes the IsPushOnly() test we don't care what exactly is in the
    // script.
    if (scriptPubKey.size() >= 1 && scriptPubKey[0] == OP_RETURN && scriptPubKey.IsPushOnly(scriptPubKey.begin()+1)) {
        typeRet = TX_NULL_DATA;
        return true;
    }

    // Scan templates
    const CScript& script1 = scriptPubKey;
    BOOST_FOREACH(const PAIRTYPE(txnouttype, CScript)& tplate, mTemplates)
    {
        const CScript& script2 = tplate.second;
        vSolutionsRet.clear();

        opcodetype opcode1, opcode2;
        vector<unsigned char> vch1, vch2;

        // Compare
        CScript::const_iterator pc1 = script1.begin();
        CScript::const_iterator pc2 = script2.begin();
        while (true)
        {
            if (pc1 == script1.end() && pc2 == script2.end())
            {
                // Found a match
                typeRet = tplate.first;
                if (typeRet == TX_MULTISIG)
                {
                    // Additional checks for TX_MULTISIG:
                    unsigned char m = vSolutionsRet.front()[0];
                    unsigned char n = vSolutionsRet.back()[0];
                    if (m < 1 || n < 1 || m > n || vSolutionsRet.size()-2 != n)
                        return false;
                }
                return true;
            }
            if (!script1.GetOp(pc1, opcode1, vch1))
                break;
            if (!script2.GetOp(pc2, opcode2, vch2))
                break;

            // Template matching opcodes:
            if (opcode2 == OP_PUBKEYS)
            {
                while (vch1.size() >= 33 && vch1.size() <= 65)
                {
                    vSolutionsRet.push_back(vch1);
                    if (!script1.GetOp(pc1, opcode1, vch1))
                        break;
                }
                if (!script2.GetOp(pc2, opcode2, vch2))
                    break;
                // Normal situation is to fall through
                // to other if/else statements
            }

            if (opcode2 == OP_PUBKEY)
            {
                if (vch1.size() < 33 || vch1.size() > 65)
                    break;
                vSolutionsRet.push_back(vch1);
            }
            else if (opcode2 == OP_PUBKEYHASH)
            {
                if (vch1.size() != sizeof(uint160))
                    break;
                vSolutionsRet.push_back(vch1);
            }
            else if (opcode2 == OP_SMALLINTEGER)
            {   // Single-byte small integer pushed onto vSolutions
                if (opcode1 == OP_0 ||
                    (opcode1 >= OP_1 && opcode1 <= OP_16))
                {
                    char n = (char)CScript::DecodeOP_N(opcode1);
                    vSolutionsRet.push_back(valtype(1, n));
                }
                else
                    break;
            }
            else if (opcode1 != opcode2 || vch1 != vch2)
            {
                // Others must match exactly
                break;
            }
        }
    }

    vSolutionsRet.clear();
    typeRet = TX_NONSTANDARD;
    return false;
}
Ejemplo n.º 30
0
//将url放入map_urls到容器中
void CCrawl::AddUrl(const char * url)
{
	string str_url = url;
	if(str_url.empty() || str_url.length() < 8)
	{
		cout<<"the url is empty or too short"<<endl;
		return ;
	}

	CPage ipage;
	if(ipage.NormalizeUrl(str_url) == false)
		return ;

	CUrl iurl;

	//图片类型的网页,存放到历史网页链接库中
	if(iurl.IsImageUrl(str_url))
	{
		if(ofs_link_for_history_file)
		{
			WaitForSingleObject(mutex_link_for_history_file,INFINITE);

			ofs_link_for_history_file<<str_url<<endl;

			ReleaseMutex(mutex_link_for_history_file);
		}

		return ;
	}

	if(iurl.ParseUrl(str_url) == false)
	{
		cout<<"parse url error in AddUrl"<<endl;
		return ;
	}

	if(iurl.IsValidHost(iurl.host_name.c_str()) == false)
	{
		cout<<"not the valid host in AddUrl"<<endl;
		return ;
	}

	if(iurl.IsForeignHost(iurl.host_name.c_str()) )
	{
		cout<<"foreign host in AddUrl"<<endl;
		return ;
	}

	//如果是阻塞的ip地址,剔除掉
	unsigned long inaddr = 0;
	char *ip = NULL;

	inaddr =(unsigned long) inet_addr(iurl.host_name.c_str());

	if(inaddr != INADDR_NONE)
	{
		ip = new char[iurl.host_name.size() + 1];
		memset(ip,0,iurl.host_name.size() + 1);
		memcpy(ip,iurl.host_name.c_str(),iurl.host_name.size());
		if(!iurl.IsValidIp(ip))
		{
			delete []ip;
			ip = NULL;
			return ;
		}

		delete []ip;
		ip = NULL;
	}


	CStrFunction::StrToLower(iurl.host_name,iurl.host_name.size());

	CMD5 imd5;
	imd5.GenerateMd5((unsigned char *)str_url.c_str(),str_url.size());

	string str_digest = imd5.ToString();

	if(set_visited_url_md5.find(str_digest) != set_visited_url_md5.end())
	{
		return ;
	}

	if(set_unvisited_url_md5.find(str_digest) != set_unvisited_url_md5.end())
	{
		return ;
	}
	else
	{
		WaitForSingleObject(mutex_unvisited_url_md5,INFINITE);
		set_unvisited_url_md5.insert(str_digest);
		ReleaseMutex(mutex_unvisited_url_md5);
	}

	//确保同一个线程在一个网站上爬取
	int cnt = 0;

	for(;;)
	{
		if(1)//???????
		{
			WaitForSingleObject(mutex_visited_url_md5,INFINITE);
			map_urls.insert(val_type(iurl.host_name,str_url));
			ReleaseMutex(mutex_visited_url_md5);
			break;
		}
		else
		{
			cnt++;
			if(cnt%100 == 0)
				cout<<"~";
			

			if(cnt == 5000)
			{
				cout<<"remove it"<<endl;
			}

			Sleep(4000);
		}
	}

}