Ticket #239: ip_filter_no_boost-ip_filter_no_boost-fast.patch
| File ip_filter_no_boost-ip_filter_no_boost-fast.patch, 3.8 KB (added by yb, 2 years ago) |
|---|
-
src/core/ip_filter.cc
diff -ruN rtorrent-0.8.5-ip_filter_no_boost/src/core/ip_filter.cc rtorrent-0.8.5-ip_filter_no_boost-new/src/core/ip_filter.cc
old new 60 60 } 61 61 62 62 int IpFilter::add_from_file( const std::string& fileName, range_map* rs, str_list* files ) { 63 std::ifstream in( fileName.c_str() ); 64 std::string line; 63 FILE *f = fopen(fileName.c_str(),"r"); 65 64 int mergeCount = 0; 66 67 if( in.fail() || !in.is_open() ) 68 return -1; 69 70 while( in.good() ) { 71 std::getline( in, line ); 72 utils::trim( line ); 73 74 if( (line[0] == '#') || (line.length() == 0) || (line[0] == 0) ) 65 if (f==0) return -1; 66 char *line = (char *)malloc(64); 67 size_t sz=64; 68 int charsread = 0; 69 int linesread=0; 70 while( (charsread=getline(&line,&sz,f)) >=0 ) { 71 if( (line[0] == '#' ) || ( charsread <= 1 ) ) 75 72 continue; 76 77 IpRange* ir = IpRange::parse( line );73 74 IpRange* ir = IpRange::parse( line, charsread ); 78 75 if( !ir || !ir->get_from() || !ir->get_to() ) 79 76 continue; 80 77 81 78 mergeCount += merge_and_insert( rs, ir ); 82 79 } 80 free(line); 83 81 files->push_back( std::string(fileName) ); 84 in.close(); 85 82 fclose(f); 86 83 m_merges += mergeCount; 87 84 return mergeCount; 88 85 } -
src/core/ip_range.cc
diff -ruN rtorrent-0.8.5-ip_filter_no_boost/src/core/ip_range.cc rtorrent-0.8.5-ip_filter_no_boost-new/src/core/ip_range.cc
old new 48 48 return r; 49 49 } 50 50 51 //fast version 52 IpRange* IpRange::parse( const char *s, const int size ){ 53 static char description[256]; 54 static char ip1[24], ip2[24]; 55 int pos=0, post=0, enddesc=size-1; 56 while (enddesc>0 && s[enddesc]!=':') enddesc--; //find last ':' in the line 57 while((pos<enddesc) && (unsigned char)s[pos]<=' ') pos++; // strip from start 58 while ((pos<enddesc)){ 59 if (post<255) description[post++]=s[pos]; 60 pos++; 61 } 62 description[post]=0; 63 if (s[pos]==':') pos++; 64 post=0; 65 while ((pos<size) && s[pos]!='-'){ 66 if (post<23) ip1[post++]=s[pos]; 67 pos++; 68 } 69 ip1[post]=0; 70 if (s[pos]=='-'){ 71 pos++; 72 post=0; 73 while ((pos<size) && s[pos]>' '){ 74 if (post<23) ip2[post++]=s[pos]; 75 pos++; 76 } 77 ip2[post]=0; 78 } else ip2[0]=0; 79 80 IpAddress* from = IpAddress::parse(ip1); 81 IpAddress* to = IpAddress::parse(ip2); 82 83 if( !from ) { 84 std::cout << "!! from is invalid: description='" << description << ", ip1=" << ip1 << ", ip2=" << ip2 << std::endl; 85 return NULL; 86 } 87 if( !to ) { 88 std::cout << "!! to is invalid: description='" << description << ", ip1=" << ip1 << ", ip2=" << ip2 << std::endl; 89 return NULL; 90 } 91 92 IpRange* r = new IpRange(); 93 r->m_description = description; 94 r->m_from = from; 95 r->m_to = to; 96 97 if( (*to < *from) ) { 98 std::cout << "!! to < from: " << r->to_string() << std::endl; 99 delete r; 100 return NULL; 101 } 102 103 return r; 104 } 105 51 106 std::string IpRange::to_string() const { 52 107 std::stringstream result; 53 108 result << m_description << ": [" << m_from->to_string() << " - " << m_to->to_string() << ']'; -
src/core/ip_range.h
diff -ruN rtorrent-0.8.5-ip_filter_no_boost/src/core/ip_range.h rtorrent-0.8.5-ip_filter_no_boost-new/src/core/ip_range.h
old new 30 30 public: // static methods 31 31 typedef IpRange* ptr; 32 32 static IpRange* parse( const std::string& s ); 33 static IpRange* parse( const char *s, const int size ); 33 34 34 35 public: // dynamic methods 35 36 IpRange( IpRange& rng ) { copy(rng); }
