int main(int argc, char ** argv) {
  const int N = 1024 * 32;
  int alignoffset = 0;
  if(argc>1)  {
    alignoffset = atoi(argv[1]);
    printf("alignment offset = %d \n", alignoffset);
  }
  char *origbuffer = malloc(N + alignoffset);
  char *origtmpbuffer = malloc(N + alignoffset);
  char *buffer = origbuffer + alignoffset;
  char *tmpbuffer = origtmpbuffer + alignoffset;
  printf("pointer alignment = %d bytes \n", 1<< __builtin_ctzll((uintptr_t)(const void *)(buffer)));

  int repeat = 100;
  size_t howmanywhite = fillwithtext(buffer, N);

  BEST_TIME_NOCHECK_NOPRE(memcpy(tmpbuffer,buffer,N),
                   repeat, N);
  printf("\n");
  BEST_TIME(despace(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME(neon_despace(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME(neon_despace_branchless(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME(neontbl_despace(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  free(origbuffer);
  free(origtmpbuffer);
}
Example #2
0
			std::set<std::string> getAcceptRanges() const
			{
				std::set<std::string> V;
				if ( fields.find("accept-ranges") == fields.end() )
					return V;
				std::string const v = fields.find("accept-ranges")->second;
				std::deque<std::string> tokens = libmaus2::util::stringFunctions::tokenize(v,std::string(","));
				for ( uint64_t i = 0; i < tokens.size(); ++i )
				{
					std::string s = despace(tolower(tokens[i]));
					if ( s.size() )
						V.insert(s);
				}
				
				return V;
			}
Example #3
0
std::string TS_Util::fixExpr(std::string expr)
{
	std::string result = expr;
	result = despace(result);
	result = replaceEx(result, "++", cstr(OP_INC));
	result = replaceEx(result, "--", cstr(OP_DEC));
	result = replaceEx(result, "+=", cstr(OP_ADD_SET));
	result = replaceEx(result, "-=", cstr(OP_SUB_SET));
	result = replaceEx(result, "*=", cstr(OP_MUL_SET));
	result = replaceEx(result, "/=", cstr(OP_DIV_SET));
	result = replaceEx(result, "^=", cstr(OP_POW_SET));
	result = replaceEx(result, ">=", cstr(OP_GE));
	result = replaceEx(result, "<=", cstr(OP_LE));
	result = replaceEx(result, "!=", cstr(OP_NEQ));
	result = replaceEx(result, "==", cstr(OP_EQ));
	result = replaceEx(result, "=", cstr(OP_SET));
	result = replaceEx(result, "&&", "&");
	result = replaceEx(result, "||", "|");
	result = replaceEx(result, "(xor)", "#");
	result = replaceEx(result, "(nand)", cstr(OP_NAND));
	result = replaceEx(result, "(nor)", cstr(OP_NOR));
	result = replaceEx(result, "(nxor)", cstr(OP_NXOR));
	result = replaceEx(result, "(band)", cstr(OP_BAND));
	result = replaceEx(result, "(bor)", cstr(OP_BOR));
	result = replaceEx(result, "<<", "\\");
	result = replaceEx(result, ">>", "\\-");
	result = replaceEx(result, "(max)", cstr(OP_MAX));
	result = replaceEx(result, "(min)", cstr(OP_MIN));
	result = replaceEx(result, "(imp)", cstr(OP_IMP));
	if (result != "(pi)")
		result = replaceEx(result, "(pi)", PI_STR);
	result = replaceEx(result, "(e)", "exp(1)");
	result = replaceEx(result, "()", "(0)");
	result = replaceEx(result, ")(", ")*(");
	return result;
}
Example #4
0
			void init(
				std::string method, 
				std::string addreq, 
				std::string host, 
				std::string path, unsigned int port = 80, bool ssl = false
			)
			{
				bool headercomplete = false;
				
				std::set<InitParameters> seen;

				while ( ! headercomplete )
				{
					InitParameters const initparams(method,addreq,host,path,port,ssl);
					
					if ( seen.find(initparams) != seen.end() )
					{
						libmaus2::exception::LibMausException lme;
						lme.getStream() << "HttpHeader: redirect loop detected, method=" << method << " host=" << host << " path=" << path << " port=" << port << " ssl=" << ssl << std::endl;
						lme.finish();
						throw lme;	
					}
					
					seen.insert(initparams);
				
					fields.clear();
					
					CS.reset();
					OS.reset();
					SIS.reset();
					GTLSIOS.reset();
					SIOS = 0;
					
					bool const hasproxy =
						(ssl && hasHttpsProxy())
						||
						((!ssl) && hasHttpProxy());
						
					if ( hasproxy )
					{
						HttpAbsoluteUrl proxyurl =
							ssl ? HttpAbsoluteUrl(getenv("https_proxy")) : HttpAbsoluteUrl(getenv("http_proxy"));
						
						// std::cerr << "[D] using proxy " << proxyurl << std::endl;
							
						if ( proxyurl.ssl )
						{
							#if defined(LIBMAUS2_HAVE_GNUTLS)
							libmaus2::network::GnuTLSSocket::unique_ptr_type tGTLSIOS(new libmaus2::network::GnuTLSSocket(proxyurl.host,proxyurl.port,"/etc/ssl/certs/ca-certificates.crt","/etc/ssl/certs",true));
							GTLSIOS = UNIQUE_PTR_MOVE(tGTLSIOS);
							SIOS = GTLSIOS.get();
							#else	
							libmaus2::network::OpenSSLSocket::unique_ptr_type tOS(new libmaus2::network::OpenSSLSocket(proxyurl.host,proxyurl.port,0,"/etc/ssl/certs",true));
							OS = UNIQUE_PTR_MOVE(tOS);						
							SIOS = OS.get();
							#endif

							libmaus2::network::SocketInputStream::unique_ptr_type tSIS(new libmaus2::network::SocketInputStream(*SIOS,64*1024));
							SIS = UNIQUE_PTR_MOVE(tSIS);
						}
						else
						{
							libmaus2::network::ClientSocket::unique_ptr_type tCS(new libmaus2::network::ClientSocket(proxyurl.port,proxyurl.host.c_str()));
							CS = UNIQUE_PTR_MOVE(tCS);

							SIOS = CS.get();
						
							libmaus2::network::SocketInputStream::unique_ptr_type tSIS(new libmaus2::network::SocketInputStream(*CS,64*1024));
							SIS = UNIQUE_PTR_MOVE(tSIS);
						}						
					}
					else
					{
						if ( ssl )
						{
							#if defined(LIBMAUS2_HAVE_GNUTLS)
							libmaus2::network::GnuTLSSocket::unique_ptr_type tGTLSIOS(new libmaus2::network::GnuTLSSocket(host,port,"/etc/ssl/certs/ca-certificates.crt","/etc/ssl/certs",true));
							GTLSIOS = UNIQUE_PTR_MOVE(tGTLSIOS);
							
							SIOS = GTLSIOS.get();
							#else
							libmaus2::network::OpenSSLSocket::unique_ptr_type tOS(new libmaus2::network::OpenSSLSocket(host,port,0,"/etc/ssl/certs",true));
							OS = UNIQUE_PTR_MOVE(tOS);
						
							SIOS = OS.get();
							#endif

							libmaus2::network::SocketInputStream::unique_ptr_type tSIS(new libmaus2::network::SocketInputStream(*SIOS,64*1024));
							SIS = UNIQUE_PTR_MOVE(tSIS);
						}
						else
						{
							libmaus2::network::ClientSocket::unique_ptr_type tCS(new libmaus2::network::ClientSocket(port,host.c_str()));
							CS = UNIQUE_PTR_MOVE(tCS);

							SIOS = CS.get();
						
							libmaus2::network::SocketInputStream::unique_ptr_type tSIS(new libmaus2::network::SocketInputStream(*CS,64*1024));
							SIS = UNIQUE_PTR_MOVE(tSIS);
						}
					}
					
					std::ostringstream reqastr;

					if ( hasproxy )
					{
						reqastr << method << " " 
							<< (ssl ? "https" : "http")
							<< "://"
							<< host
							<< ":"
							<< port
							<< path 
							<< " HTTP/1.1\r\n";
						reqastr << "Connection: close\r\n";
					}
					else
					{
						reqastr << method << " " << path << " HTTP/1.1\r\n";
						reqastr << "Host: " << host << "\r\n";
					}

					reqastr << addreq;
					reqastr << "\r\n";

					// send request
					std::string const reqa = reqastr.str();
					SIOS->write(reqa.c_str(),reqa.size());

					libmaus2::autoarray::AutoArray<char> c(128,false);
					char last4[4] = {0,0,0,0};
					bool done = false;
					
					std::ostringstream headstr;
					
					while ( !done )
					{
						SIS->readsome(c.begin(),c.size());
						ssize_t r = SIS->gcount();
						
						// buffer is empty, try to fill it
						if ( ! r )
						{
							SIS->read(c.begin(),1);
							r = SIS->gcount();
							
							if ( ! r )
							{
								libmaus2::exception::LibMausException lme;
								lme.getStream() << "HttpHeader: unexpected EOF/error while reading header" << std::endl;
								lme.finish();
								throw lme;	
							}
							else
							{
								SIS->unget();
								continue;
							}
						}
						
						for ( ssize_t i = 0; (!done) && i < r; ++i )
						{
							headstr.put(c[i]);
							
							last4[0] = last4[1];
							last4[1] = last4[2];
							last4[2] = last4[3];
							last4[3] = c[i];
						
							if ( 
								last4[0] == '\r' && 
								last4[1] == '\n' && 
								last4[2] == '\r' && 
								last4[3] == '\n'
							)
							{
								for ( ssize_t j = i+1; j < r; ++j )
									SIS->unget();

								// SIS->clear();
								done = true;
							}
						}			
					}
					
					std::istringstream linestr(headstr.str());
					std::vector<std::string> lines;
					while ( linestr )
					{
						std::string line;
						std::getline(linestr,line);
						
						while ( line.size() && isspace(line[line.size()-1]) )
						{
							line = line.substr(0,line.size()-1);
						}
						
						if ( line.size() )
							lines.push_back(line);
					}
					
					if ( ! lines.size() )
					{
						libmaus2::exception::LibMausException lme;
						lme.getStream() << "HttpHeader: unable to get header" << std::endl;
						lme.finish();
						throw lme;
					}
					
					statusline = lines[0];
					std::vector<std::string> statustokens;
					for ( uint64_t i = 0; i < statusline.size(); )
					{
						while ( i < statusline.size() && isspace(statusline[i]) )
							++i;

						uint64_t j = i;					
						while ( j < statusline.size() && (!isspace(statusline[j])) )
							++j;
							
						std::string const token = statusline.substr(i,j-i);
						
						i = j;
						
						statustokens.push_back(token);
					}
					
					if ( statustokens.size() < 2 )
					{			
						libmaus2::exception::LibMausException lme;
						lme.getStream() << "HttpHeader: status line " << statusline << " invalid" << std::endl;
						lme.finish();
						throw lme;
					}
				
					std::string const replyformat = statustokens[0];	
					std::istringstream statuscodeistr(statustokens[1]);
					uint64_t statuscode;
					statuscodeistr >> statuscode;
					
					if ( ! statuscodeistr )
					{
						libmaus2::exception::LibMausException lme;
						lme.getStream() << "HttpHeader: invalid status code " << statustokens[1] << std::endl;
						lme.finish();
						throw lme;			
					}
					
					if (
						replyformat != "HTTP/1.0"
						&&
						replyformat != "HTTP/1.1"
					)
					{
						libmaus2::exception::LibMausException lme;
						lme.getStream() << "HttpHeader: unknown reply format " << replyformat << std::endl;
						lme.finish();
						throw lme;						
					}
					
					// std::cerr << "status code " << statuscode << std::endl;
					
					for ( uint64_t i = 1; i < lines.size(); ++i )
					{
						std::string const line = lines[i];
						
						uint64_t col = 0;
						while ( col < line.size() && line[col] != ':' )
							++col;
						
						if ( ! col || col == line.size() )
						{
							libmaus2::exception::LibMausException lme;
							lme.getStream() << "HttpHeader: invalid key value pair " << line << std::endl;
							lme.finish();
							throw lme;						
						}
						
						std::string const key = tolower(despace(line.substr(0,col)));
						std::string const val = despace(line.substr(col+1));
						
						fields[key] = val;
					}
					
					switch ( statuscode )
					{
						case 301:
						case 302:
						case 307:
						case 308:
						{				
							std::map<std::string,std::string>::const_iterator it = fields.end();
							
							if ( it == fields.end() )
								it = fields.find("Location");
							if ( it == fields.end() )
								it = fields.find("location");
							
							if ( it == fields.end() )
							{	
								libmaus2::exception::LibMausException lme;
								lme.getStream() << "HttpHeader: redirect status code " << statuscode << " but no location given" << std::endl;
								lme.finish();
								throw lme;						
							}
							
							std::string location = it->second;

							// std::cerr << "redirecting " << statuscode << " to " << location << std::endl;
							
							if ( 
								::libmaus2::network::HttpAbsoluteUrl::isHttpAbsoluteUrl(location) 
								||
								::libmaus2::network::HttpAbsoluteUrl::isHttpsAbsoluteUrl(location) 
							)
							{
								::libmaus2::network::HttpAbsoluteUrl url(location);
								host = url.host;
								port = url.port;
								path = url.path;
								ssl = url.ssl;
							}
							else if ( ::libmaus2::network::HttpAbsoluteUrl::isAbsoluteUrl(location) )
							{
								libmaus2::exception::LibMausException lme;
								lme.getStream() << "HttpHeader: unsupported protocol in location " << location << std::endl;
								lme.finish();
								throw lme;						
							}
							else
							{
								path = location;
							}
							
							break;
						}
						default:
						{
							headercomplete = true;
							break;
						}
					}	
				}	
				
				#if 0
				std::cerr << statusline << std::endl;
				for ( std::map<std::string,std::string>::const_iterator ita = fields.begin(); ita != fields.end(); ++ita )
				{
					std::cerr << ita->first << ": " << ita->second << std::endl;
				}
				#endif
				
				url.host = host;
				url.port = port;
				url.path = path;
				url.ssl = ssl;		
			}
Example #5
0
int main(int argc, char **argv) {
#ifdef __SSSE3__
  gen_table_1mb();
#endif
  const int N = 1024;
  int alignoffset = 0;
  if (argc > 1) {
    alignoffset = atoi(argv[1]);
    printf("alignment offset = %d \n", alignoffset);
  }
  char *origbuffer = malloc(N + alignoffset);
  char *origtmpbuffer = malloc(N + alignoffset);
  char *buffer = origbuffer + alignoffset;
  char *tmpbuffer = origtmpbuffer + alignoffset;
  printf("pointer alignment = %d bytes \n",
         1 << __builtin_ctzll((uintptr_t)(const void *)(buffer)));

  int repeat = 100;
  size_t howmanywhite;

  BEST_TIME_NOCHECK(memcpy(tmpbuffer, buffer, N),
                    howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(countspaces(buffer, N), howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(countspaces32(buffer, N), howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(despace(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(despace32(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(faster_despace(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(faster_despace32(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(despace64(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(despace_to(buffer, N, tmpbuffer), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(despace_branchless(tmpbuffer, buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(despace_cmov(tmpbuffer, buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(despace_table(tmpbuffer, buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
#ifdef __SSSE3__
  BEST_TIME_CHECK(despace_ssse3_cumsum(tmpbuffer, buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(despace_ssse3_lut_1kb(tmpbuffer, buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(despace_ssse3_lut_1mb(tmpbuffer, buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
#endif
#ifdef __AVX2__
  BEST_TIME_CHECK(avx2_countspaces(buffer, N), howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(despace_avx2_vpermd(tmpbuffer, buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(avx2_despace(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(avx2_despace_branchless(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(avx2_despace_branchless_u2(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);

#endif
#ifdef __SSE4_1__
  BEST_TIME_CHECK(sse4_despace(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(sse4_despace_branchless(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(sse4_despace_branchless32(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(sse4_despace_branchless_u2(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(sse4_despace_branchless_u4(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(sse4_despace_trail(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
#endif
#ifdef __SSE4_2__

  BEST_TIME_CHECK(sse42_despace_branchless(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(sse42_despace_branchless_lookup(buffer, N), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
  BEST_TIME_CHECK(sse42_despace_to(buffer, N, tmpbuffer), N - howmanywhite,
                  howmanywhite = fillwithtext(buffer, N), repeat, N);
#endif
  free(origbuffer);
  free(origtmpbuffer);
}