예제 #1
0
파일: y_string.c 프로젝트: a-lex-leon/y-lib
// --------------------------------------------------------------------------------------------------
// Clean a parameter by removing any embedded \x00 (null) characters from it.
// This would only happen if you have used to web_reg_save_param() and the result contains
// a null-character. 
// Any such characters are replaced with ' '. (space)
// Since this changes existing parameters be careful what types of parameters 
// you use this on.But when no null-character is found, the result is unaltered.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//        example usage:
//            char *test;
//            web_reg_save_param("TestParam", "LB=\"name=LastName\" Value=\"", "RB=\"", LAST);
//            web_submit_data(...);                    // will fail, obiously.                                    
//            lr_message(lr_eval_string("TestParam: {TestParam}\n"));            
//            y_cleanse_parameter("TestParam");
//            test=y_get_parameter("TestParam");
//            lr_message("\nTest: >%s<\n", test);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void y_cleanse_parameter(const char* paramName)
{
   char *result;
   unsigned long resultLen;
   size_t resultStrlen;
   size_t param_size = strlen(paramName) +3; // parameter name + "{}" + '\0' (end of string)
   char *tmp = y_mem_alloc( param_size );

   //lr_log_message("y_cleanse_parameter(%s)", paramName );

   // Get the contents of the parameter using lr_eval_string_ext() - we can't use the
   // regular version if we expect to find \x00 in there.
   snprintf( tmp, param_size, "{%s}", paramName );
   lr_eval_string_ext(tmp, strlen(tmp), &result, &resultLen, 0, 0, -1);
   free(tmp);

   // replace NULL bytes (\x00) in the input with something else..
   for( resultStrlen = strlen(result);
        resultStrlen < resultLen;
        resultStrlen = strlen(result))
   {
       result[resultStrlen] = ' ';
   }

   // Put the result back into the original parameter.
   lr_save_string(result, paramName);
   lr_eval_string_ext_free(&result);
}
예제 #2
0
파일: y_string.c 프로젝트: linpelvis/y-lib
/*!
\brief Get the content of a parameter without embedded null bytes (\0 characters) from the named parameter, if any.
In some cases we want to fetch the content of a parameter but the parameter contains embedded NULL characters which make further processing harder. 
This will fetch a parameter but "cleanse" it from such contamination, leaving the rest of the data unaltered before returning it.

\warning The return value of this function needs to be freed using lr_eval_string_ext_free().

\param [in] param_name The parameter to cleanse of nulls.
\param [in] replacement A character that replaces any embedded nulls found.
\returns The resulting parameter content.

\b Example:
\code
{
   char buffer[11] = { '\0', 'b', '\0', 'r','o', '\0', 'k', 'e', 'n', '\0', '\0' };
   char *tmp;
   lr_save_var(buffer, 11, 0, "broken");
   tmp = y_get_cleansed_parameter("broken", '!');
   lr_log_message("Result: %s", tmp); // Prints "Result: !b!ro!ken!!".
   free(tmp);
}
\endcode
*/
char* y_get_cleansed_parameter(const char* param_name, char replacement)
{
   char* result;
   unsigned long result_size;
   size_t param_eval_size = strlen(param_name) +3; // parameter name + "{}" + '\0' (end of string)
   char* param_eval_string = y_mem_alloc(param_eval_size);
   //lr_log_message("y_cleanse_parameter(%s)", param_name );

   // Get the contents of the parameter using lr_eval_string_ext() - we can't use the
   // regular version if we expect to find NULL in there.
   snprintf( param_eval_string, param_eval_size, "{%s}", param_name );
   lr_eval_string_ext(param_eval_string, param_eval_size-1, &result, &result_size, 0, 0, -1);
   if( strcmp(param_eval_string, result) == 0 )
   {
       lr_error_message("y_get_cleansed_parameter: Parameter %s does not exist.", param_name);
       lr_abort();
   }
   free(param_eval_string);

   //lr_log_message("Cleansing param %s, result starts with '%-*.*s' and contains %d bytes.", param_name, result_size, result_size, result, result_size);
   if (result_size) {
      char *ptr;
	  for(ptr = result; result_size--; ptr++)
	     if (*ptr == 0) *ptr = replacement;	  
	  
	  /*
      // Now replace NULL bytes (NULL) in the input with something else..
      for( result_strlen = strlen(result); result_strlen < result_size; result_strlen = strlen(result))
      {
         result[result_strlen] = replacement;
         //lr_log_message("Cleansing param %s, result now '%-*.*s' and contains %d bytes.", param_name, result_size, result_size, result, result_size);
      }*/
   }
   return result;
}
예제 #3
0
파일: y_string.c 프로젝트: a-lex-leon/y-lib
// --------------------------------------------------------------------------------------------------
// Given a parameter name, obtain the string required to fetch the contents of that parameter through
// lr_eval_string().
// 
// Note: the return argument will need to be freed.
// 
// @author Floris Kraak
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//        example usage:
// 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
char* y_get_parameter_eval_string(const char *param_name)
{
    size_t size = strlen(param_name) +3; // parameter name + "{}" + '\0' (end of string)
    char *result = y_mem_alloc( size ); 

    snprintf(result, size, "{%s}", param_name );
    return result;
}
예제 #4
0
파일: y_string.c 프로젝트: a-lex-leon/y-lib
// --------------------------------------------------------------------------------------------------
// y_split a string in two based on a seperating string.
// @author Floris Kraak
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//        example usage:
//            lr_save_string("WackoYackoDotWarner", "Test");
//            lr_message(lr_eval_string("Original: {Test}\n"));    // {Test}    = WackoYackoDotWarner
//            y_split("Test", "Yacko", "Left", "Right");           // Use "Yacko" as the separator
//            lr_message(lr_eval_string("Original: {Test}\n"));    // {Test}    = WackoYackoDotWarner
//            lr_message(lr_eval_string("Left    : {Left}\n"));    // {Left}    = Wacko
//            lr_message(lr_eval_string("Right   : {Right}\n"));   // {Right}   = DotWarner
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
y_split( const char *originalParameter, const char *separator, const char *leftParameter, const char *rightParameter)
{
    char *item = y_get_parameter(originalParameter);
    int len = strlen(item);

    if( len < strlen(separator) )
    {
        // Since the separator doesn't even fit in the item, searching is pointless.
        // Store the original in the left hand parameter, the right hand is empty.
        lr_save_string(originalParameter, leftParameter);
        lr_save_string("", rightParameter);
        return;

    }
    else
    {

        // Left hand side
        // If the separator isn't found the full original string gets stored here.
        // Don't forget the 0 byte at the end though..
        char *left = y_mem_alloc(len+1);

        // Right hand side
        // If the separator gets found in position 1 the remainder of the
        // original string gets stored here (and left gets a zero-length string).
        char *right = y_mem_alloc(len-strlen(separator)+1);

        // Start off with zero-length strings. We can safely assume
        // both variables contain garbage when freshly allocated.
        left[0] = '\0';
        right[0] = '\0';

        // This is where the magic happens.
        y_split_str(item, separator, left, right);

        // Store the results in parameters.
        lr_save_string(left, leftParameter);
        free(left);
        lr_save_string(right, rightParameter);
        free(right);
    }
}
예제 #5
0
파일: y_string.c 프로젝트: a-lex-leon/y-lib
char* y_get_parameter_in_malloc_string(const char *src_param)
{
    char *result;
    char *src = y_get_parameter(src_param);
    //lr_log_message("Copying source data: %s", src);

    result = y_mem_alloc( strlen(src) +1);
    strcpy(result, src);

    return result;
    //lr_log_message("Copied result: %s", result);
}
예제 #6
0
파일: y_string.c 프로젝트: a-lex-leon/y-lib
// --------------------------------------------------------------------------------------------------
// Split the string into 2 parts using the search string. Save the left part into the resultParameter.
//
// @author Floris Kraak
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//        example usage:
//            lr_save_string("AstrixObelixIdefix", "Test");
//            lr_message(lr_eval_string("Original: {Test}\n"));    // {Test}=AstrixObelixIdefix
//            y_left( "Test", "Obelix", "Test2" );
//            lr_message(lr_eval_string("New Param: {Test2}\n"));    // {Test2}=Astrix
//
//    note: the previous name of this function was: head() and y_head()
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
y_left( const char *originalParameter, const char *search, const char *resultParameter )
{
   char *buffer;
   char *original = y_get_parameter(originalParameter);
   char *posPtr = (char *)strstr(original, search);
   int pos = (int)(posPtr - original);

   //lr_log_message("y_head: original=%s, search=%s, resultParam=%s", original, search, resultParam);

   if( posPtr == NULL )
   {
      lr_save_string(original, resultParameter);
      return;
   }
   //lr_log_message("pos = %d", pos);
   
   // Copy the original to a temporary buffer
   buffer = y_mem_alloc(strlen(original)+1);
   strcpy(buffer, original);
   buffer[pos] = '\0';                             // make the cut
   lr_save_string(buffer, resultParameter);        // save the result
   free(buffer);
}
예제 #7
0
// --------------------------------------------------------------------------------------------------
// Usage: y_random_string_buffer("parameterNameWhichGetsTheRandomValue", minimumlength, maximumlength);
//
// ex. randomString("randomFeedback", 100, 200); will fill a parameter with between 100 and 200 characters.
// to use the random value, use the parameter name provided.
// To make it look like real sentences, this function inserts spaces at random.
// The "words" will be minimal 1 character long, and max. 8 characters.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//        example usage:     
//            y_random_string_buffer("par1", 10,10);   // creates a string of exactly 10 characters and saves it into string {par1}
//            y_random_string_buffer("par1", 5,10);    // creates a string of min 5 and max 10 char and saves it into string {par1}    
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
y_random_string_buffer(const char *parameter, int minimumLength, int maximumLength)
{
   const char characterSet[] = { 
                     'a','b','c','d','e','f','g','h','i','j','k','l','m',
                     'n','o','p','q','r','s','t','u','v','w','x','y','z',
                     'A','B','C','D','E','F','G','H','I','J','K','L','M',
                     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
                     /*
                     '1','2','3','4','5','6','7','8','9','0','?','!','-',
                     ',','.',';',
                     '`','~','@','#','$','%','^','&','*','(',')',
                     '=','_','+','[',']','{','}','|',':','/',
                     '<','>',  };
                     */

   char *buffer;
   int charSetSize = 52; // length of the above array
   int length = 0;
   int max = 0;

   char randomNumber;
   int lettersInWord;

   // error checks - lots of code that saves us headaches later
   if( minimumLength < 1 ) {
      lr_error_message( "minimumLength smaller than 0 (%d)", minimumLength );
   }
   else if( maximumLength < 1 ) {
      lr_error_message( "maximumLength smaller than 0 (%d)", maximumLength );
   }
   else if( maximumLength > (1024 * 1024) ) {
      lr_error_message( "maximumLength too big (%d)", maximumLength );
   }
   else if( maximumLength < minimumLength ) {
      lr_error_message( "minimumLength (%d) bigger than maximumLength (%d)", minimumLength, maximumLength );
   }
   else if(maximumLength > minimumLength) {
      // Not an error
      max = (y_rand() % (maximumLength-minimumLength)) + minimumLength;
   }
   else if(maximumLength == minimumLength) {
      // Not an error either
      max = maximumLength;
   }
   else {
      lr_error_message("This can never happen. If we reach this point it's a bug.");
   }

   // if we got an error
   if( max == 0 )
   {
      lr_set_transaction_status(LR_FAIL);
      // Not sure this is the right exit code that we want to use here, but ok.
      lr_exit(LR_EXIT_ITERATION_AND_CONTINUE, LR_FAIL);
   }
   
   // get memory for the buffer
   buffer = (char *)y_mem_alloc( max +1 );
   // note: if this fails y_mem_alloc() aborts the script, so no error handling needed.

   while( length < max )
   {
      lettersInWord = ((y_rand() % 8) + 2);

      while( lettersInWord-- && (length < (max)) )
      {
         randomNumber = (char) (y_rand() % charSetSize);
         buffer[length++] = characterSet[randomNumber];
      }

      if(length!=max)
      {
          buffer[length++] = ' ';
      }
   }

   buffer[length++] = '\0';

   lr_save_string(buffer, parameter);
   free(buffer);

   return 0;
}
예제 #8
0
파일: y_string.c 프로젝트: a-lex-leon/y-lib
// --------------------------------------------------------------------------------------------------
// Search and replace.
// Find 'search' in 'parameter' and replace it with 'replace'.
// Replaces the originally passed-in parameter with the new one.
//
// Note: This one has a built-in search/y_replace limit when
// search > y_replace. It won't do it more than 'limit' times.
//
// @author Floris Kraak
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//        example usage:     
//             lr_save_string("test123", "par1");
//          y_replace("par1", "1", "ing1");        // {par1} now has the value testing123
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
y_replace( const char *parameter, const char *search, const char *replace)
{
   int slen, rlen, plen;      // lengte van search, replace, en basis string
   int i = 0;                 // aantal replacements
   int limit = 1000;          // replacement limiet als replace > search

   char *c;                   // punt waar change moet beginnen
   char *cend;                // einde van de change = c+slen
   char *last;                // de \0 slotbyte van input string
   char *buffer;              // buffer voor bewerkingen
   char *string;              // originele string waar we in zoeken

   if ((search == NULL)     || (strlen(search) <1)) 
       return;
   if (!search || !replace)      return;   // ongeldige search of_replace
   if (!strcmp(search, replace)) return;   // search == replace: geen wijziging

   slen = strlen (search);          // de lengte van search
   rlen = strlen (replace);         // de lengte van replace

   string = y_get_parameter(parameter); // <-- memory allocated by loadrunner, too small if replace > search
   plen = strlen(string);

   //lr_log_message("y_replace(%s, %s, %s) - slen %d, rlen %d, plen %d", parameter, search, replace, slen, rlen, plen);
    
   if ( rlen > slen)
   {
      // Reserve memory for -limit- replacements.
      size_t size = plen + ((rlen-slen) * limit);
      buffer = y_mem_alloc( size );
      snprintf( buffer, size, "%s", string );
   }
   else
   {
      // No need to reserve memory
      buffer = string;
   }

   last = buffer + strlen(buffer) +1;      // het einde van de string, de null byte
   c = buffer;                             // initialiseer c - buffer moet intact blijven

   while (c = (char*) strstr(c, search))   // doorzoek de search string vanaf waar je gebleven bent
   {
      //lr_output_message("c:%s", c);
 
      i++;
      if (slen != rlen)                    // zijn search en replace van verschillende lengte?
      {
         if( i >= limit )
         {
            lr_log_message("Unable to handle more than %d search-and-replaces, apologies for the inconvenience.", limit);
            break;
         }
         cend = c+slen;                        // cend is de plaats van waaraf de string moet opschuiven
         //lr_output_message("memmove(dest=%d, src=%d, size=%d)", (long)(c+rlen), (long)(cend), (long)(last-cend) );
         memmove (c+rlen , cend, last-cend);   // verplaats de rest van de string - als de geheugenberekening niet klopt gaat dit stuk!
         //lr_output_message("memmove completed, result = %s", c);
         last = last - slen + rlen;            // en bereken het nieuwe eindpunt
      }
      memmove(c, replace, rlen);  // execute the replacement
      c += rlen;                  // start the next search at the point where the replacement ended
   }
   lr_save_string(buffer, parameter);

   if( rlen > slen )
   {
      free(buffer);
   }
}
예제 #9
0
파일: y_string.c 프로젝트: linpelvis/y-lib
/*!
This function uses a given set of characters to create words, separated by spaces.
The words are minimal \e minWordLength characters long, and maximum \e minWordLength characters.
The total length of the line is minimal \e minimumLength and maimum \e maximumLength long.

\b Example:
\code
// Generates a string of minimal 3 and max 20 characters, 
// with words of minimal 1 and maximal 3 charactes.
// Chooses only characters a, c, d or d.
y_random_string_buffer_core("uitvoer", 3,20, 1, 3, "abcd");

// Generates some sort of mock morse-code of exactly 30 characters.
// with words of minimal 1 and maximal 3 charactes.
// Chooses only characters a, c, d or d.
y_random_string_buffer_core("uitvoer", 3,20, 1, 3, "abcd"); // could result in "ccc db dac c"
\endcode

@param[out] parameter Name of the LR-parameter in which the result is stored
@param[in] minimumLength Minumum length of the string
@param[in] maximumLength Maximum length of the string
@param[in] minWordLength Minimum length of the words within the string
@param[in] maxWordLength Minimum length of the words within the string
@param[in] characterSet The string is build from this string of characters
\return void
\author Floris Kraak / Raymond de Jongh

\sa y_random_number_buffer
\sa y_random_string_buffer_curses
\sa y_random_string_buffer
\sa y_random_string_buffer_hex
*/
void y_random_string_buffer_core(const char *parameter, int minimumLength, int maximumLength, 
                                 int minWordLength, int maxWordLength, char *characterSet)
{
   char *buffer;
   int charSetSize; // length of the characterSet
   int length = 0;
   int max = -1;

   char randomNumber;
   int lettersInWord;

   charSetSize=strlen(characterSet);

   //lr_message("minimumLength %d -- maximumLength %d -- minWordLength %d -- maxWordLength %d", 
   //      minimumLength, maximumLength, minWordLength, maxWordLength);

   // error checks - lots of code that saves us headaches later
   if( minimumLength < 0 ) {
      lr_error_message( "minimumLength less than 0 (%d)", minimumLength );
   }
   else if( maximumLength < 1 ) {
      lr_error_message( "maximumLength less than 1 (%d)", maximumLength );
   }
   else if( maximumLength > (1024 * 1024) ) {
      lr_error_message( "maximumLength too big (%d)", maximumLength );
   }
   else if( maximumLength < minimumLength ) {
      lr_error_message( "minimumLength (%d) bigger than maximumLength (%d)", minimumLength, maximumLength );
   }
   else if(maximumLength > minimumLength) {
      // Not an error
      max = y_rand_between(minimumLength, maximumLength);
      lr_log_message("Max: %d", max);
   }
   else if(maximumLength == minimumLength) {
      // Not an error either
      max = maximumLength;
   }
   else {
      lr_error_message("This can never happen. If we reach this point it's a bug.");
   }

   // if we got an error
   if( max < 0 )
   {
      lr_set_transaction_status(LR_FAIL);
      lr_exit(LR_EXIT_ITERATION_AND_CONTINUE, LR_FAIL);
   }

   // get memory for the buffer
   buffer = (char *)y_mem_alloc( max +1 );
   // note: if this fails y_mem_alloc() aborts the script, so no error handling needed.

   while( length < max )
   {
//      lr_message("Length: %d   max: %d", length, max);
//      lettersInWord = ((y_rand() % 8) + 2);
      if( maxWordLength == 0 )
      {
         lettersInWord = maximumLength;
      }
      else
      {
         lettersInWord = y_rand_between(minWordLength, maxWordLength);
         if( lettersInWord < 0 )
         {
            lr_error_message( "y_rand_between() returned an errorcode (%d)", lettersInWord );
            lr_set_transaction_status(LR_FAIL);
            lr_exit(LR_EXIT_ITERATION_AND_CONTINUE, LR_FAIL);
         }
      }

      while( lettersInWord-- && (length < (max)) )
      {
         randomNumber = (char) (y_rand() % charSetSize);
         buffer[length++] = characterSet[randomNumber];
      }

      if( maxWordLength != 0 )
      {
         if( length < max -1 )
         {
            buffer[length++] = ' ';
         }
      }
   }
   buffer[max] = '\0';

   lr_save_string(buffer, parameter);
   free(buffer);
}