Fix quotes handling.
[midnight-commander.git] / lib / search / hex.c
blob2fdb71631b5936c5c68c9cb8dedeb247e7abd43c
1 /*
2 Search text engine.
3 HEX-style pattern matching
5 Copyright (C) 2009-2016
6 Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <config.h>
29 #include <stdio.h>
31 #include "lib/global.h"
32 #include "lib/strutil.h"
33 #include "lib/search.h"
34 #include "lib/strescape.h"
36 #include "internal.h"
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 typedef enum
44 MC_SEARCH_HEX_E_OK,
45 MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE,
46 MC_SEARCH_HEX_E_INVALID_CHARACTER,
47 MC_SEARCH_HEX_E_UNMATCHED_QUOTES
48 } mc_search_hex_parse_error_t;
50 /*** file scope type declarations ****************************************************************/
52 /*** file scope variables ************************************************************************/
54 /*** file scope functions ************************************************************************/
56 static GString *
57 mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_error_t * error_ptr,
58 int *error_pos_ptr)
60 GString *buff;
61 gchar *tmp_str, *tmp_str2;
62 gsize tmp_str_len;
63 gsize loop = 0;
64 mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
66 buff = g_string_sized_new (64);
67 tmp_str = g_strndup (astr->str, astr->len);
68 tmp_str2 = tmp_str;
70 /* remove 0x prefices */
71 while (TRUE)
73 tmp_str2 = strstr (tmp_str2, "0x");
74 if (tmp_str2 == NULL)
75 break;
77 *tmp_str2++ = ' ';
78 *tmp_str2++ = ' ';
81 tmp_str_len = strlen (tmp_str);
83 while (loop < tmp_str_len && error == MC_SEARCH_HEX_E_OK)
85 unsigned int val;
86 int ptr;
88 if (g_ascii_isspace (tmp_str[loop]))
90 /* Eat-up whitespace between tokens. */
91 while (g_ascii_isspace (tmp_str[loop]))
92 loop++;
94 /* cppcheck-suppress invalidscanf */
95 else if (sscanf (tmp_str + loop, "%x%n", &val, &ptr) == 1)
97 if (val > 255)
98 error = MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE;
99 else
101 g_string_append_printf (buff, "\\x%02X", val);
102 loop += ptr;
105 else if (tmp_str[loop] == '"')
107 gsize loop2;
109 loop2 = loop + 1;
111 while (loop2 < tmp_str_len)
113 if (tmp_str[loop2] == '"')
114 break;
115 if (tmp_str[loop2] == '\\' && loop2 + 1 < tmp_str_len)
116 loop2++;
117 g_string_append_c (buff, tmp_str[loop2]);
118 loop2++;
121 if (tmp_str[loop2] == '\0')
122 error = MC_SEARCH_HEX_E_UNMATCHED_QUOTES;
123 else
124 loop = loop2 + 1;
126 else
127 error = MC_SEARCH_HEX_E_INVALID_CHARACTER;
130 g_free (tmp_str);
132 if (error != MC_SEARCH_HEX_E_OK)
134 g_string_free (buff, TRUE);
135 if (error_ptr != NULL)
136 *error_ptr = error;
137 if (error_pos_ptr != NULL)
138 *error_pos_ptr = loop;
139 return NULL;
142 return buff;
145 /*** public functions ****************************************************************************/
147 void
148 mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_search,
149 mc_search_cond_t * mc_search_cond)
151 GString *tmp;
152 mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
153 int error_pos = 0;
155 tmp = mc_search__hex_translate_to_regex (mc_search_cond->str, &error, &error_pos);
156 if (tmp != NULL)
158 g_string_free (mc_search_cond->str, TRUE);
159 mc_search_cond->str = tmp;
160 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
162 else
164 const char *desc;
166 switch (error)
168 case MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE:
169 desc =
171 ("Number out of range (should be in byte range, 0 <= n <= 0xFF, expressed in hex)");
172 break;
173 case MC_SEARCH_HEX_E_INVALID_CHARACTER:
174 desc = _("Invalid character");
175 break;
176 case MC_SEARCH_HEX_E_UNMATCHED_QUOTES:
177 desc = _("Unmatched quotes character");
178 break;
179 default:
180 desc = "";
183 lc_mc_search->error = MC_SEARCH_E_INPUT;
184 lc_mc_search->error_str =
185 g_strdup_printf (_("Hex pattern error at position %d:\n%s."), error_pos + 1, desc);
189 /* --------------------------------------------------------------------------------------------- */
191 gboolean
192 mc_search__run_hex (mc_search_t * lc_mc_search, const void *user_data,
193 gsize start_search, gsize end_search, gsize * found_len)
195 return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
198 /* --------------------------------------------------------------------------------------------- */
200 GString *
201 mc_search_hex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
203 (void) lc_mc_search;
204 return g_string_new_len (replace_str->str, replace_str->len);
207 /* --------------------------------------------------------------------------------------------- */