Exemple #1
0
void hb_dict_set( hb_dict_t ** dict_ptr, const char * key, const char * value )
{
    hb_dict_t * dict = *dict_ptr;
    if( !dict )
    {
        hb_log( "hb_dict_set: NULL dictionary" );
        return;
    }
    if( !key || !strlen( key ) )
        return;
    hb_dict_entry_t * entry = hb_dict_get( dict, key );
    if( entry )
    {
        if( entry->value )
        {
            if( value && !strcmp( value, entry->value ) )
                return;
            else
            {
                free( entry->value );
                entry->value = NULL;
            }
        }
        if( value && strlen( value ) )
            entry->value = strdup( value );
    }
    else
    {
        if( dict->alloc <= dict->count )
        {
            hb_dict_entry_t * tmp = NULL;
            tmp = malloc( ( 2 * dict->alloc ) * sizeof( hb_dict_entry_t ) );
            if( !tmp )
            {
                hb_log( "ERROR: could not realloc hb_dict_t objects" );
                return;
            }
            if( dict->objects )
            {
                if( dict->count )
                    memcpy( tmp, dict->objects, dict->count * sizeof( hb_dict_entry_t ) );
                free( dict->objects );
            }
            dict->objects = tmp;
            dict->alloc *= 2;
        }
        dict->objects[dict->count].key = strdup( key );
        if( value && strlen( value ) )
            dict->objects[dict->count].value = strdup( value );
        else
            dict->objects[dict->count].value = NULL;
        dict->count++;
    }
}
Exemple #2
0
int hb_validate_filter_settings(int filter_id, const hb_dict_t * settings)
{
    hb_filter_object_t * filter;
    hb_dict_t          * settings_template;
    hb_dict_iter_t       iter;

    if (settings == NULL)
        return 0;

    // Verify that all keys in settings are in the filter settings template
    filter = hb_filter_get(filter_id);
    if (filter == NULL)
    {
        hb_log("hb_validate_filter_settings: Unrecognized filter (%d).\n",
               filter_id);
        return 1;
    }
    if (filter->settings_template == NULL)
    {
        // filter has no template to verify settings against
        return 0;
    }
    settings_template = hb_parse_filter_settings(filter->settings_template);
    if (settings_template == NULL)
    {
        hb_log("hb_validate_filter_settings: invalid template!");
        return 0;
    }

    for (iter = hb_dict_iter_init(settings);
         iter != HB_DICT_ITER_DONE;
         iter = hb_dict_iter_next(settings, iter))
    {
        const char * key;
        hb_value_t * val;

        key = hb_dict_iter_key(iter);

        // Check if key found in settings is also found in the template
        val = hb_dict_get(settings_template, key);
        if (val == NULL)
        {
            // Key is missing from template, indicate invalid settings
            hb_log("Invalid filter key (%s) for filter %s",
                    key, filter->name);
            return 1;
        }

        // If a string value is found, and it is non-empty,
        // it is a regex pattern for allowed values.
        const char * regex_pattern = hb_value_get_string(val);
        if (regex_pattern != NULL && regex_pattern[0] != 0)
        {
            char * param;
            param = hb_value_get_string_xform(hb_dict_get(settings, key));
            if (hb_validate_param_string(regex_pattern, param) != 0)
            {
                hb_log("Invalid filter value (%s) for key %s filter %s",
                        param, key, filter->name);
                free(param);
                return 1;
            }
            free(param);
        }
    }
    hb_value_free(&settings_template);

    return 0;
}