2009-05-05 23:28:27 +04:00
|
|
|
/*
|
|
|
|
Search text engine.
|
|
|
|
HEX-style pattern matching
|
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 2009-2024
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2009-05-05 23:28:27 +04:00
|
|
|
|
|
|
|
Written by:
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2009.
|
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
2009-05-05 23:28:27 +04:00
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
2011-10-15 14:56:47 +04:00
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
2009-05-05 23:28:27 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2009-05-05 23:28:27 +04:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-05-05 23:28:27 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2009-05-06 11:27:06 +04:00
|
|
|
#include <stdio.h>
|
2009-05-05 23:28:27 +04:00
|
|
|
|
2010-01-20 18:11:52 +03:00
|
|
|
#include "lib/global.h"
|
2010-01-21 15:17:26 +03:00
|
|
|
#include "lib/strutil.h"
|
2010-01-21 16:06:15 +03:00
|
|
|
#include "lib/search.h"
|
2010-01-21 15:17:26 +03:00
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
|
2009-05-05 23:28:27 +04:00
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
2016-09-25 14:09:43 +03:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
MC_SEARCH_HEX_E_OK,
|
2016-09-25 18:30:45 +03:00
|
|
|
MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE,
|
2016-09-25 20:57:12 +03:00
|
|
|
MC_SEARCH_HEX_E_INVALID_CHARACTER,
|
|
|
|
MC_SEARCH_HEX_E_UNMATCHED_QUOTES
|
2016-09-25 14:09:43 +03:00
|
|
|
} mc_search_hex_parse_error_t;
|
|
|
|
|
2009-05-05 23:28:27 +04:00
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2009-05-05 23:28:27 +04:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-05 23:28:27 +04:00
|
|
|
/*** file scope functions ************************************************************************/
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-05 23:28:27 +04:00
|
|
|
|
|
|
|
static GString *
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_search__hex_translate_to_regex (const GString *astr, mc_search_hex_parse_error_t *error_ptr,
|
2016-09-25 14:09:43 +03:00
|
|
|
int *error_pos_ptr)
|
2009-05-05 23:28:27 +04:00
|
|
|
{
|
2010-10-21 17:18:18 +04:00
|
|
|
GString *buff;
|
2016-09-26 00:55:41 +03:00
|
|
|
const char *str;
|
|
|
|
gsize str_len;
|
2009-05-06 11:27:06 +04:00
|
|
|
gsize loop = 0;
|
2016-09-25 14:09:43 +03:00
|
|
|
mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
|
2009-05-06 11:27:06 +04:00
|
|
|
|
2010-10-21 17:18:18 +04:00
|
|
|
buff = g_string_sized_new (64);
|
2016-09-26 00:55:41 +03:00
|
|
|
str = astr->str;
|
|
|
|
str_len = astr->len;
|
2009-05-06 11:27:06 +04:00
|
|
|
|
2016-09-26 00:55:41 +03:00
|
|
|
while (loop < str_len && error == MC_SEARCH_HEX_E_OK)
|
2010-10-21 17:33:32 +04:00
|
|
|
{
|
2016-09-17 15:04:35 +03:00
|
|
|
unsigned int val;
|
|
|
|
int ptr;
|
2011-07-19 12:43:00 +04:00
|
|
|
|
2016-09-26 00:55:41 +03:00
|
|
|
if (g_ascii_isspace (str[loop]))
|
2016-09-25 18:30:45 +03:00
|
|
|
{
|
|
|
|
/* Eat-up whitespace between tokens. */
|
2016-09-26 00:55:41 +03:00
|
|
|
while (g_ascii_isspace (str[loop]))
|
2016-09-25 18:30:45 +03:00
|
|
|
loop++;
|
|
|
|
}
|
2013-10-10 16:21:26 +04:00
|
|
|
/* cppcheck-suppress invalidscanf */
|
2016-09-26 00:55:41 +03:00
|
|
|
else if (sscanf (str + loop, "%x%n", &val, &ptr) == 1)
|
2010-10-21 17:33:32 +04:00
|
|
|
{
|
2016-09-17 15:04:35 +03:00
|
|
|
if (val > 255)
|
2016-09-25 14:09:43 +03:00
|
|
|
error = MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE;
|
2011-07-19 12:43:00 +04:00
|
|
|
else
|
|
|
|
{
|
2016-09-17 15:04:35 +03:00
|
|
|
g_string_append_printf (buff, "\\x%02X", val);
|
2011-07-19 12:43:00 +04:00
|
|
|
loop += ptr;
|
2009-05-06 11:27:06 +04:00
|
|
|
}
|
|
|
|
}
|
2016-09-26 00:55:41 +03:00
|
|
|
else if (str[loop] == '"')
|
2010-10-21 17:33:32 +04:00
|
|
|
{
|
2016-09-25 20:57:12 +03:00
|
|
|
gsize loop2;
|
2010-10-21 17:33:32 +04:00
|
|
|
|
2016-09-25 20:57:12 +03:00
|
|
|
loop2 = loop + 1;
|
|
|
|
|
2016-09-26 00:55:41 +03:00
|
|
|
while (loop2 < str_len)
|
2010-10-21 17:33:32 +04:00
|
|
|
{
|
2016-09-26 00:55:41 +03:00
|
|
|
if (str[loop2] == '"')
|
2009-06-08 14:54:35 +04:00
|
|
|
break;
|
2016-09-26 00:55:41 +03:00
|
|
|
if (str[loop2] == '\\' && loop2 + 1 < str_len)
|
2016-09-25 20:57:12 +03:00
|
|
|
loop2++;
|
2016-09-26 00:55:41 +03:00
|
|
|
g_string_append_c (buff, str[loop2]);
|
2009-06-08 14:54:35 +04:00
|
|
|
loop2++;
|
|
|
|
}
|
2010-10-21 17:33:32 +04:00
|
|
|
|
2016-09-26 00:55:41 +03:00
|
|
|
if (str[loop2] == '\0')
|
2016-09-25 20:57:12 +03:00
|
|
|
error = MC_SEARCH_HEX_E_UNMATCHED_QUOTES;
|
|
|
|
else
|
|
|
|
loop = loop2 + 1;
|
2009-05-06 11:27:06 +04:00
|
|
|
}
|
2010-10-21 17:33:32 +04:00
|
|
|
else
|
2016-09-25 18:30:45 +03:00
|
|
|
error = MC_SEARCH_HEX_E_INVALID_CHARACTER;
|
2009-05-06 11:27:06 +04:00
|
|
|
}
|
2010-02-17 16:47:28 +03:00
|
|
|
|
2016-09-25 14:09:43 +03:00
|
|
|
if (error != MC_SEARCH_HEX_E_OK)
|
|
|
|
{
|
|
|
|
g_string_free (buff, TRUE);
|
|
|
|
if (error_ptr != NULL)
|
|
|
|
*error_ptr = error;
|
|
|
|
if (error_pos_ptr != NULL)
|
|
|
|
*error_pos_ptr = loop;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-05 23:28:27 +04:00
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-05 23:28:27 +04:00
|
|
|
/*** public functions ****************************************************************************/
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-05 23:28:27 +04:00
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t *lc_mc_search,
|
|
|
|
mc_search_cond_t *mc_search_cond)
|
2009-05-05 23:28:27 +04:00
|
|
|
{
|
2010-10-21 17:07:06 +04:00
|
|
|
GString *tmp;
|
2016-09-25 14:09:43 +03:00
|
|
|
mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
|
|
|
|
int error_pos = 0;
|
2009-05-05 23:28:27 +04:00
|
|
|
|
2016-09-26 16:17:08 +03:00
|
|
|
/*
|
|
|
|
* We may be searching in binary data, which is often invalid UTF-8.
|
|
|
|
*
|
|
|
|
* We have to create a non UTF-8 regex (that is, G_REGEX_RAW) or else, as
|
|
|
|
* the data is invalid UTF-8, both GLib's PCRE and our
|
|
|
|
* mc_search__g_regex_match_full_safe() are going to fail us. The former by
|
|
|
|
* not finding all bytes, the latter by overwriting the supposedly invalid
|
|
|
|
* UTF-8 with NULs.
|
|
|
|
*
|
|
|
|
* To do this, we specify "ASCII" as the charset.
|
|
|
|
*
|
|
|
|
* In fact, we can specify any charset other than "UTF-8": any such charset
|
|
|
|
* will trigger G_REGEX_RAW (see [1]). The output of [2] will be the same
|
|
|
|
* for all charsets because it skips the \xXX symbols
|
|
|
|
* mc_search__hex_translate_to_regex() outputs.
|
|
|
|
*
|
|
|
|
* But "ASCII" is the best choice because a hex pattern may contain a
|
|
|
|
* quoted string: this way we know [2] will ignore any characters outside
|
|
|
|
* ASCII letters range (these ignored chars will be copied verbatim to the
|
|
|
|
* output and will match as-is; in other words, in a case-sensitive manner;
|
|
|
|
* If the user is interested in case-insensitive searches of international
|
|
|
|
* text, he shouldn't be using hex search in the first place.)
|
|
|
|
*
|
|
|
|
* Switching out of UTF-8 has another advantage:
|
|
|
|
*
|
|
|
|
* When doing case-insensitive searches, GLib treats \xXX symbols as normal
|
|
|
|
* letters and therefore matches both "a" and "A" for the hex pattern
|
|
|
|
* "0x61". When we switch out of UTF-8, we're switching to using [2], which
|
|
|
|
* doesn't have this issue.
|
|
|
|
*
|
|
|
|
* [1] mc_search__cond_struct_new_init_regex
|
|
|
|
* [2] mc_search__cond_struct_new_regex_ci_str
|
|
|
|
*/
|
|
|
|
if (str_isutf8 (charset))
|
|
|
|
charset = "ASCII";
|
|
|
|
|
2016-09-25 14:09:43 +03:00
|
|
|
tmp = mc_search__hex_translate_to_regex (mc_search_cond->str, &error, &error_pos);
|
|
|
|
if (tmp != NULL)
|
|
|
|
{
|
|
|
|
g_string_free (mc_search_cond->str, TRUE);
|
|
|
|
mc_search_cond->str = tmp;
|
|
|
|
mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *desc;
|
|
|
|
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE:
|
|
|
|
desc =
|
|
|
|
_
|
|
|
|
("Number out of range (should be in byte range, 0 <= n <= 0xFF, expressed in hex)");
|
|
|
|
break;
|
2016-09-25 18:30:45 +03:00
|
|
|
case MC_SEARCH_HEX_E_INVALID_CHARACTER:
|
|
|
|
desc = _("Invalid character");
|
2016-09-25 20:57:12 +03:00
|
|
|
break;
|
|
|
|
case MC_SEARCH_HEX_E_UNMATCHED_QUOTES:
|
|
|
|
desc = _("Unmatched quotes character");
|
2016-09-25 18:30:45 +03:00
|
|
|
break;
|
2016-09-25 14:09:43 +03:00
|
|
|
default:
|
|
|
|
desc = "";
|
|
|
|
}
|
2009-05-05 23:28:27 +04:00
|
|
|
|
2016-09-25 14:09:43 +03:00
|
|
|
lc_mc_search->error = MC_SEARCH_E_INPUT;
|
|
|
|
lc_mc_search->error_str =
|
|
|
|
g_strdup_printf (_("Hex pattern error at position %d:\n%s."), error_pos + 1, desc);
|
|
|
|
}
|
2009-05-05 23:28:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_search__run_hex (mc_search_t *lc_mc_search, const void *user_data,
|
|
|
|
gsize start_search, gsize end_search, gsize *found_len)
|
2009-05-05 23:28:27 +04:00
|
|
|
{
|
2009-10-30 04:12:04 +03:00
|
|
|
return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
|
2009-05-05 23:28:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
GString *
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_search_hex_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace_str)
|
2009-05-05 23:28:27 +04:00
|
|
|
{
|
2009-10-30 04:12:04 +03:00
|
|
|
(void) lc_mc_search;
|
2021-02-28 18:31:30 +03:00
|
|
|
|
|
|
|
return mc_g_string_dup (replace_str);
|
2009-05-05 23:28:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|