コード例 #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 プロジェクト: a-lex-leon/y-lib
// --------------------------------------------------------------------------------------------------
// Semi-efficiënt parameter copy using lr_eval_string_ext() with appropriate freeing of memory.
// @author Floris Kraak
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//        example usage:
//                lr_save_string("text", "param_a");
//                y_copy_param("param_a", "param_b");   // Results in an exact copy of the content of param_a being stored in param_b.
//                lr_log_message(lr_eval_string("param_b: {param_b}")); // Prints "param_b: text".
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void y_copy_param(char* source_param, char* dest_param)
{
    unsigned long size;
    char* buffer;
    char* source = y_get_parameter_eval_string(source_param); // Puts the parameter name into parameter seperators { }. Resulting ptr must be freed.
    lr_eval_string_ext(source, strlen(source), &buffer, &size, 0, 0, -1); // Evaluates the result and copies the data into buffer.
    free(source);                              // Free the intermediate parameter name.
    lr_save_var(buffer, size, 0, dest_param);  // Save the result.
    lr_eval_string_ext_free(&buffer);          // Free the buffer.
}
コード例 #3
0
ファイル: y_string.c プロジェクト: linpelvis/y-lib
/*!
\brief Clean a parameter by replacing any embedded NULL (null) characters with a replacement character.

This would normally only happen if you have used to web_reg_save_param() and the result contains one or more null-character(s).
Any such characters are replaced with replacement_char and the result is stored in the original parameter.
When no null-character is found, the result is unaltered.

\warning Since the return value is allocated with malloc(), it will need to be freed using free() at some point.

\param [in] param_name The parameter to cleanse of nulls.
\param [in] replacement A character that replaces any embedded nulls found.
\warning Since this changes existing parameters be careful what types of parameters you use this on.

\b Example:
\code
{
   char buffer[11] = { '\0', 'b', '\0', 'r','o', '\0', 'k', 'e', 'n', '\0', '\0' };
   lr_save_var(buffer, 11, 0, "broken");
   y_cleanse_parameter_ext("broken", '!'); // will save "!b!ro!ken!!" into the "broken" parameter.
}
\endcode
*/
void y_cleanse_parameter_ext(const char* param_name, char replacement)
{
    if( param_name && strlen(param_name) )
    {
        char* result = y_get_cleansed_parameter(param_name, replacement);
        lr_save_string(result, param_name);
        lr_eval_string_ext_free(&result);
    }
    else
    {
        lr_error_message("Empty or NULL parameter name passed to y_cleanse_parameter_ext(): %s", param_name);
        lr_abort();
    }
}