library/src/utility/BP-StringUtilities.cc
// creates a string containing pseudorandom (not cryptographically secure) ASCII data
creates a string containing pseudorandom (not cryptographically secure) ASCII data
creates a string containing pseudorandom ( not cryptographically secure ) ASCII data
char * BP_GenRandomASCIICharString(size_t len, BP_GEN_RANDOM_ASCII_STRING_TYPE type, P_BP_LINKL tq)
{
	if(!len)
		return NULL;
	char *random_str = NULL;
	if(!tq)
		random_str = (char *) bpcalloc(len+1, 1);
	else
		random_str = (char *) tq_calloc(tq, len+1, 1, BPLN);
	if(!random_str)
		return NULL;
	size_t n = 0;
	char curr_char = 0x00;
	BP_SeedPNRG();
	for(; n < len; n++)
	{
		switch(type)
		{
			case BP_GEN_RANDOM_ASCII_STRING_TYPE_UNSET:
				curr_char = rand() % 94 + 33;
				random_str[n] = curr_char;
				break;
			case BP_GEN_RANDOM_ASCII_STRING_TYPE_ALPHA_CAPS_OR_LOWER:
				do
				{
					curr_char = rand();
				} while( !(curr_char >= 'A' && curr_char <= 'Z') && !(curr_char >= 'a' && curr_char <= 'z' ) );
				random_str[n] = curr_char;
				break;
			case BP_GEN_RANDOM_ASCII_STRING_TYPE_ALPHA_CAPS_ONLY:
				do
				{
					curr_char = rand();
				} while( !(curr_char >= 'A' && curr_char <= 'Z'));
				random_str[n] = curr_char;
				break;
			case BP_GEN_RANDOM_ASCII_STRING_TYPE_ALPHA_NUMERIC_CAPS_OR_LOWER:
				do
				{
					curr_char = rand();
				} while( !(curr_char >= 'A' && curr_char <= 'Z') && !(curr_char >= 'a' && curr_char <= 'z' ) && !(curr_char >= '0' && curr_char <= '9'));
				random_str[n] = curr_char;
				break;
			case BP_GEN_RANDOM_ASCII_STRING_TYPE_ALPHA_NUMERIC_CAPS_ONLY:
				do
				{
					curr_char = rand();
				} while( !(curr_char >= 'A' && curr_char <= 'Z') && !(curr_char >= '0' && curr_char <= '9'));
				random_str[n] = curr_char;
				break;
			case BP_GEN_RANDOM_ASCII_STRING_TYPE_NUMERIC_ONLY:
				do
				{
					curr_char = rand();
				} while( !(curr_char >= '0' && curr_char <= '9'));
				random_str[n] = curr_char;
				break;
			case BP_GEN_RANDOM_ASCII_STRING_TYPE_SYMBOLS_ONLY:
				do
				{
					curr_char = rand() % 94 + 33;
				} while( (curr_char >= 'A' && curr_char <= 'Z') || (curr_char >= 'a' && curr_char <= 'z' ) || (curr_char >= '0' && curr_char <= '9'));
				random_str[n] = curr_char;
				break;
			default:
				printf("[!] Error, unable to generate random string due to bad type variable.");
				if(!tq)
					bpfree(random_str);
				else
					tq_free(tq, random_str);
				return NULL;
				break;
		}
	}
	return random_str;
}
char * BP_GenRandomASCIICharString ( size_t len , BP_GEN_RANDOM_ASCII_STRING_TYPE type , P_BP_LINKL tq ) { if ( ! len ) return NULL ; char * random_str = NULL ; if ( ! tq ) random_str = ( char * ) bpcalloc ( len + 1 , 1 ) ; else random_str = ( char * ) tq_calloc ( tq , len + 1 , 1 , BPLN ) ; if ( ! random_str ) return NULL ; size_t n = 0 ; char curr_char = 0x00 ; BP_SeedPNRG ( ) ; for ( ; n < len ; n ++ ) { switch ( type ) { case BP_GEN_RANDOM_ASCII_STRING_TYPE_UNSET : curr_char = rand ( ) % 94 + 33 ; random_str [ n ] = curr_char ; break ; case BP_GEN_RANDOM_ASCII_STRING_TYPE_ALPHA_CAPS_OR_LOWER : do { curr_char = rand ( ) ; } while ( ! ( curr_char >= ' ' && curr_char <= ' ' ) && ! ( curr_char >= ' ' && curr_char <= ' ' ) ) ; random_str [ n ] = curr_char ; break ; case BP_GEN_RANDOM_ASCII_STRING_TYPE_ALPHA_CAPS_ONLY : do { curr_char = rand ( ) ; } while ( ! ( curr_char >= ' ' && curr_char <= ' ' ) ) ; random_str [ n ] = curr_char ; break ; case BP_GEN_RANDOM_ASCII_STRING_TYPE_ALPHA_NUMERIC_CAPS_OR_LOWER : do { curr_char = rand ( ) ; } while ( ! ( curr_char >= ' ' && curr_char <= ' ' ) && ! ( curr_char >= ' ' && curr_char <= ' ' ) && ! ( curr_char >= ' ' && curr_char <= ' ' ) ) ; random_str [ n ] = curr_char ; break ; case BP_GEN_RANDOM_ASCII_STRING_TYPE_ALPHA_NUMERIC_CAPS_ONLY : do { curr_char = rand ( ) ; } while ( ! ( curr_char >= ' ' && curr_char <= ' ' ) && ! ( curr_char >= ' ' && curr_char <= ' ' ) ) ; random_str [ n ] = curr_char ; break ; case BP_GEN_RANDOM_ASCII_STRING_TYPE_NUMERIC_ONLY : do { curr_char = rand ( ) ; } while ( ! ( curr_char >= ' ' && curr_char <= ' ' ) ) ; random_str [ n ] = curr_char ; break ; case BP_GEN_RANDOM_ASCII_STRING_TYPE_SYMBOLS_ONLY : do { curr_char = rand ( ) % 94 + 33 ; } while ( ( curr_char >= ' ' && curr_char <= ' ' ) || ( curr_char >= ' ' && curr_char <= ' ' ) || ( curr_char >= ' ' && curr_char <= ' ' ) ) ; random_str [ n ] = curr_char ; break ; default : printf ( " " ) ; if ( ! tq ) bpfree ( random_str ) ; else tq_free ( tq , random_str ) ; return NULL ; break ; } } return random_str ; }
creates a string containing pseudorandom (not cryptographically secure) ASCII data
creates a string containing pseudorandom ( not cryptographically secure ) ASCII data
// run length check // allocate space for the ascii string // check to ensure data was allocated // generate the random string // seed random with loose seed (not secure data) // srand((unsigned int) NEW_RANDOM_ID); // no filter, generate any printable ascii chars // generate a char. // generate only alphabetic strings caps or lower chars // simple loop to generate a random that fits correctly // generate a char. // set the character after its validated // caps-only filter for alphanueric generation // simple loop to generate a random that fits correctly // generate a char. // set the character after its validated // filter for generating alphanum caps or lower data // simple loop to generate a random that fits correctly // generate a char. // set the character after its validated // filter for generating alphanumeric caps only data // simple loop to generate a random that fits correctly // generate a char. // set the character after its validated // filter to generate only numbers only string data // simple loop to generate a random that fits correctly // generate a char. // set the character after its validated // filter to only generate random printable ascii symbols >!@# etc // simple loop to generate a random that fits correctly // generate a char. // set the character after its validated // print error message and exit on unrecognized type // return the random string