Ticket #3694: 3694-0005-Hex-patterns-fix-quotes-handling.patch

File 3694-0005-Hex-patterns-fix-quotes-handling.patch, 4.7 KB (added by mooffie, 2 years ago)
  • lib/search/hex.c

    From 7de6cb1aaf35008c606c27304ec446498e8447c2 Mon Sep 17 00:00:00 2001
    From: Mooffie <mooffie@gmail.com>
    Date: Sun, 25 Sep 2016 20:57:12 +0300
    Subject: [PATCH 5/8] Hex patterns: fix quotes handling.
    
    Note: considering that this feature hasn't worked, we may consider removing it
    entirely or partially (e.g., escaping) in order to simplify the code, as nobody
    has grown used to it. It seems, based on the "hex mode" mentioned in the manual
    page, that in the past there was no "normal" search in hex mode, and quoted
    strings were the only easy way to look for text. This is no longer the case
    nowadays.
    
    Note: the characters in the quoted string are copied out as-is to the regexp.
    No regexp-quoting is currently done. We may want to revisit this issue when we
    work on ticket #3695.
    ---
     lib/search/hex.c                          | 28 +++++++++------
     tests/lib/search/hex_translate_to_regex.c | 57 +++++++++++++++++++++++++++++++
     2 files changed, 75 insertions(+), 10 deletions(-)
    
    diff --git a/lib/search/hex.c b/lib/search/hex.c
    index 9395543..1180016 100644
    a b typedef enum 
    4343{ 
    4444    MC_SEARCH_HEX_E_OK, 
    4545    MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE, 
    46     MC_SEARCH_HEX_E_INVALID_CHARACTER 
     46    MC_SEARCH_HEX_E_INVALID_CHARACTER, 
     47    MC_SEARCH_HEX_E_UNMATCHED_QUOTES 
    4748} mc_search_hex_parse_error_t; 
    4849 
    4950/*** file scope type declarations ****************************************************************/ 
    mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_err 
    101102                loop += ptr; 
    102103            } 
    103104        } 
    104         else if (*(tmp_str + loop) == '"') 
     105        else if (tmp_str[loop] == '"') 
    105106        { 
    106             gsize loop2 = 0; 
     107            gsize loop2; 
    107108 
    108             loop++; 
    109             while (loop + loop2 < tmp_str_len) 
     109            loop2 = loop + 1; 
     110 
     111            while (loop2 < tmp_str_len) 
    110112            { 
    111                 if (*(tmp_str + loop + loop2) == '"' && 
    112                     !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2)) 
     113                if (tmp_str[loop2] == '"') 
    113114                    break; 
     115                if (tmp_str[loop2] == '\\' && loop2 + 1 < tmp_str_len) 
     116                    loop2++; 
     117                g_string_append_c (buff, tmp_str[loop2]); 
    114118                loop2++; 
    115119            } 
    116120 
    117             g_string_append_len (buff, tmp_str + loop, loop2 - 1); 
    118             loop += loop2; 
     121            if (tmp_str[loop2] == '\0') 
     122                error = MC_SEARCH_HEX_E_UNMATCHED_QUOTES; 
     123            else 
     124                loop = loop2 + 1; 
    119125        } 
    120126        else 
    121127            error = MC_SEARCH_HEX_E_INVALID_CHARACTER; 
    mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_se 
    146152    mc_search_hex_parse_error_t error; 
    147153    int error_pos; 
    148154 
    149     g_string_ascii_down (mc_search_cond->str); 
    150155    tmp = mc_search__hex_translate_to_regex (mc_search_cond->str, &error, &error_pos); 
    151156    if (tmp != NULL) 
    152157    { 
    mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_se 
    168173        case MC_SEARCH_HEX_E_INVALID_CHARACTER: 
    169174            desc = _("Invalid character"); 
    170175            break; 
     176        case MC_SEARCH_HEX_E_UNMATCHED_QUOTES: 
     177            desc = _("Unmatched quotes character"); 
     178            break; 
    171179        default: 
    172180            desc = ""; 
    173181        } 
  • tests/lib/search/hex_translate_to_regex.c

    diff --git a/tests/lib/search/hex_translate_to_regex.c b/tests/lib/search/hex_translate_to_regex.c
    index 4b580ae..258dc25 100644
    a b static const struct test_hex_translate_to_regex_ds 
    8585        NULL, 
    8686        MC_SEARCH_HEX_E_INVALID_CHARACTER 
    8787    }, 
     88    /* 
     89     * Quotes. 
     90     */ 
     91    { 
     92        " \"abc\" ", 
     93        "abc", 
     94        MC_SEARCH_HEX_E_OK 
     95    }, 
     96    { 
     97        /* Preserve upper/lower case */ 
     98        "\"aBc\"", 
     99        "aBc", 
     100        MC_SEARCH_HEX_E_OK 
     101    }, 
     102    { 
     103        " 12\"abc\"34 ", 
     104        "\\x12abc\\x34", 
     105        MC_SEARCH_HEX_E_OK 
     106    }, 
     107    { 
     108        "\"a\"\"b\"", 
     109        "ab", 
     110        MC_SEARCH_HEX_E_OK 
     111    }, 
     112    /* Empty quotes */ 
     113    { 
     114        "\"\"", 
     115        "", 
     116        MC_SEARCH_HEX_E_OK 
     117    }, 
     118    { 
     119        "12 \"\"", 
     120        "\\x12", 
     121        MC_SEARCH_HEX_E_OK 
     122    }, 
     123    /* Error: Unmatched quotes */ 
     124    { 
     125        "\"a", 
     126        NULL, 
     127        MC_SEARCH_HEX_E_UNMATCHED_QUOTES 
     128    }, 
     129    { 
     130        "\"", 
     131        NULL, 
     132        MC_SEARCH_HEX_E_UNMATCHED_QUOTES 
     133    }, 
     134    /* Escaped quotes */ 
     135    { 
     136        "\"a\\\"b\"", 
     137        "a\"b", 
     138        MC_SEARCH_HEX_E_OK 
     139    }, 
     140    { 
     141        "\"a\\\\b\"", 
     142        "a\\b", 
     143        MC_SEARCH_HEX_E_OK 
     144    }, 
    88145}; 
    89146/* *INDENT-ON* */ 
    90147