library/src/blockclass/BP-BlockClass.cc
// Insert a base_href into the db (linked to result in base field)
Insert a base_href into the db (linked to result in base field)
Insert a base_href into the db ( linked to result in base field )
size_t Block::insertBaseHref(char * base_href, size_t result_id)
{
	if(!this->mysqlSession)
		return ERR_FAILURE;
	BP_BLOCK_MARIADB_ENTER_CRITICAL_SECTION;
	if(!base_href)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	if(!BP_StringIsReasonablePrintableString(base_href, bpstrlen(base_href), BP_FALSE, BP_FALSE))
		return ERR_FAILURE;
	size_t base_href_len = bpstrlen(base_href);
	if(!base_href_len)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	if(base_href_len < 6)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	if(base_href[0] == '.')
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	if(base_href[0] == '/')
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	if(base_href[0] == '#')
		return ERR_FAILURE;
	char * hex_buff = BP_BufferToHexString(base_href,strnlen(base_href, 1024));
	if(!hex_buff)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	BP_StringUtils * query_builder = new BP_StringUtils(NULL);
	query_builder->BuildString((char *) "INSERT INTO `result_base_uris` (`sha1`, `base_href`, `initial_result_id`) VALUES (");
	query_builder->BuildString((char *) "sha1(0x");
	query_builder->BuildString((char *) hex_buff);
	query_builder->BuildString((char *) "), ");
	query_builder->BuildString((char *) "0x");
	query_builder->BuildString((char *) hex_buff);
	char result_id_buff[64] = {0};
	sprintf((char *) &result_id_buff, ", %u", result_id);
	query_builder->BuildString((char *) &result_id_buff);
	query_builder->BuildString((char *) " ) ");
	query_builder->BuildString(" ON DUPLICATE KEY UPDATE hits = hits+1;");
	if(this->mysqlPrepareQuery(query_builder->build_str) == ERR_FAILURE)
	{
		if(hex_buff)
			bpfree(hex_buff);
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	}
	if(this->mysqlExecuteStatement() == ERR_FAILURE)
	{
		if(hex_buff)
			bpfree(hex_buff);
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	}
	if(hex_buff)
	{
		bpfree(hex_buff);
		hex_buff=NULL;
	}
	this->mysqlStoreResults();
	this->mysqlDestroyStatement();
	delete query_builder;
#if USE_MARIADB == 1
	my_ulonglong insert_or_update_id = mysql_insert_id(this->mysqlSession->conn);
	if(!insert_or_update_id)
		insert_or_update_id = this->lookupBaseHrefId(base_href);
	if(!insert_or_update_id)
	{
		printf("\n [!!] Error: Record was not inserted, updated, or exited in defined pattern.  Undefined behavior.");
	}
	BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION;
	return insert_or_update_id;
#else
	my_ulonglong insert_or_update_id = this->mysqlSession->stmt->insert_id;
	if(!insert_or_update_id)
		insert_or_update_id = this->lookupBaseHrefId(base_href);
	if(!insert_or_update_id)
	{
		printf("\n [!!] Error: Record was not inserted, updated, or exited in defined pattern.  Undefined behavior.");
	}
	BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION;
	return insert_or_update_id;
#endif
}
size_t Block :: insertBaseHref ( char * base_href , size_t result_id ) { if ( ! this -> mysqlSession ) return ERR_FAILURE ; BP_BLOCK_MARIADB_ENTER_CRITICAL_SECTION ; if ( ! base_href ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; if ( ! BP_StringIsReasonablePrintableString ( base_href , bpstrlen ( base_href ) , BP_FALSE , BP_FALSE ) ) return ERR_FAILURE ; size_t base_href_len = bpstrlen ( base_href ) ; if ( ! base_href_len ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; if ( base_href_len < 6 ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; if ( base_href [ 0 ] == ' ' ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; if ( base_href [ 0 ] == ' ' ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; if ( base_href [ 0 ] == ' ' ) return ERR_FAILURE ; char * hex_buff = BP_BufferToHexString ( base_href , strnlen ( base_href , 1024 ) ) ; if ( ! hex_buff ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; BP_StringUtils * query_builder = new BP_StringUtils ( NULL ) ; query_builder -> BuildString ( ( char * ) " " ) ; query_builder -> BuildString ( ( char * ) " " ) ; query_builder -> BuildString ( ( char * ) hex_buff ) ; query_builder -> BuildString ( ( char * ) " " ) ; query_builder -> BuildString ( ( char * ) " " ) ; query_builder -> BuildString ( ( char * ) hex_buff ) ; char result_id_buff [ 64 ] = { 0 } ; sprintf ( ( char * ) & result_id_buff , " " , result_id ) ; query_builder -> BuildString ( ( char * ) & result_id_buff ) ; query_builder -> BuildString ( ( char * ) " " ) ; query_builder -> BuildString ( " " ) ; if ( this -> mysqlPrepareQuery ( query_builder -> build_str ) == ERR_FAILURE ) { if ( hex_buff ) bpfree ( hex_buff ) ; BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; } if ( this -> mysqlExecuteStatement ( ) == ERR_FAILURE ) { if ( hex_buff ) bpfree ( hex_buff ) ; BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; } if ( hex_buff ) { bpfree ( hex_buff ) ; hex_buff = NULL ; } this -> mysqlStoreResults ( ) ; this -> mysqlDestroyStatement ( ) ; delete query_builder ; #if USE_MARIADB == 1 my_ulonglong insert_or_update_id = mysql_insert_id ( this -> mysqlSession -> conn ) ; if ( ! insert_or_update_id ) insert_or_update_id = this -> lookupBaseHrefId ( base_href ) ; if ( ! insert_or_update_id ) { printf ( " \n " ) ; } BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION ; return insert_or_update_id ; #else my_ulonglong insert_or_update_id = this -> mysqlSession -> stmt -> insert_id ; if ( ! insert_or_update_id ) insert_or_update_id = this -> lookupBaseHrefId ( base_href ) ; if ( ! insert_or_update_id ) { printf ( " \n " ) ; } BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION ; return insert_or_update_id ; #endif }
Insert a base_href into the db (linked to result in base field)
Insert a base_href into the db ( linked to result in base field )
// check to ensure session exists // enter critical section // href argument is required // ensure base is reasonable printable // calculate href len // ensure we have a base href // ensure the reference is reasonable // skip any relative links that could've possibly // gotten past any previous sanity checking // create hex buffer // run jit href check // build the string // add buffer // add last result id (or zero if new/unreferenced) // append result id buffer // close query set // add duplicate query constraint // prepare the query // execute the query // destroy hex buffer here // store result // destroy statement // fetch the result // this->mysqlFetchResult(); // destroy the query builder // check for insert or update id // if the record was updated instead of updated, the id must be looked up // add catch for undefined behavior // asm("int3"); // return the new insert id (should always be nonzero) // check for insert or update id // if the record was updated instead of updated, the id must be looked up // add catch for undefined behavior // asm("int3"); // return the new insert id (should always be nonzero)