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 |
43 | 43 | { |
44 | 44 | MC_SEARCH_HEX_E_OK, |
45 | 45 | 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 |
47 | 48 | } mc_search_hex_parse_error_t; |
48 | 49 | |
49 | 50 | /*** file scope type declarations ****************************************************************/ |
… |
… |
mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_err |
101 | 102 | loop += ptr; |
102 | 103 | } |
103 | 104 | } |
104 | | else if (*(tmp_str + loop) == '"') |
| 105 | else if (tmp_str[loop] == '"') |
105 | 106 | { |
106 | | gsize loop2 = 0; |
| 107 | gsize loop2; |
107 | 108 | |
108 | | loop++; |
109 | | while (loop + loop2 < tmp_str_len) |
| 109 | loop2 = loop + 1; |
| 110 | |
| 111 | while (loop2 < tmp_str_len) |
110 | 112 | { |
111 | | if (*(tmp_str + loop + loop2) == '"' && |
112 | | !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2)) |
| 113 | if (tmp_str[loop2] == '"') |
113 | 114 | break; |
| 115 | if (tmp_str[loop2] == '\\' && loop2 + 1 < tmp_str_len) |
| 116 | loop2++; |
| 117 | g_string_append_c (buff, tmp_str[loop2]); |
114 | 118 | loop2++; |
115 | 119 | } |
116 | 120 | |
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; |
119 | 125 | } |
120 | 126 | else |
121 | 127 | error = MC_SEARCH_HEX_E_INVALID_CHARACTER; |
… |
… |
mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_se |
146 | 152 | mc_search_hex_parse_error_t error; |
147 | 153 | int error_pos; |
148 | 154 | |
149 | | g_string_ascii_down (mc_search_cond->str); |
150 | 155 | tmp = mc_search__hex_translate_to_regex (mc_search_cond->str, &error, &error_pos); |
151 | 156 | if (tmp != NULL) |
152 | 157 | { |
… |
… |
mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_se |
168 | 173 | case MC_SEARCH_HEX_E_INVALID_CHARACTER: |
169 | 174 | desc = _("Invalid character"); |
170 | 175 | break; |
| 176 | case MC_SEARCH_HEX_E_UNMATCHED_QUOTES: |
| 177 | desc = _("Unmatched quotes character"); |
| 178 | break; |
171 | 179 | default: |
172 | 180 | desc = ""; |
173 | 181 | } |
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 |
85 | 85 | NULL, |
86 | 86 | MC_SEARCH_HEX_E_INVALID_CHARACTER |
87 | 87 | }, |
| 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 | }, |
88 | 145 | }; |
89 | 146 | /* *INDENT-ON* */ |
90 | 147 | |