root/lib/search/hex.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_search__hex_translate_to_regex
  2. mc_search__cond_struct_new_init_hex
  3. mc_search__run_hex
  4. mc_search_hex_prepare_replace_str

   1 /*
   2    Search text engine.
   3    HEX-style pattern matching
   4 
   5    Copyright (C) 2009-2024
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Slava Zanko <slavazanko@gmail.com>, 2009.
  10 
  11    This file is part of the Midnight Commander.
  12 
  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.
  17 
  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.
  22 
  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/>.
  25  */
  26 
  27 #include <config.h>
  28 
  29 #include <stdio.h>
  30 
  31 #include "lib/global.h"
  32 #include "lib/strutil.h"
  33 #include "lib/search.h"
  34 
  35 #include "internal.h"
  36 
  37 /*** global variables ****************************************************************************/
  38 
  39 /*** file scope macro definitions ****************************************************************/
  40 
  41 typedef enum
  42 {
  43     MC_SEARCH_HEX_E_OK,
  44     MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE,
  45     MC_SEARCH_HEX_E_INVALID_CHARACTER,
  46     MC_SEARCH_HEX_E_UNMATCHED_QUOTES
  47 } mc_search_hex_parse_error_t;
  48 
  49 /*** file scope type declarations ****************************************************************/
  50 
  51 /*** forward declarations (file scope functions) *************************************************/
  52 
  53 /*** file scope variables ************************************************************************/
  54 
  55 /* --------------------------------------------------------------------------------------------- */
  56 /*** file scope functions ************************************************************************/
  57 /* --------------------------------------------------------------------------------------------- */
  58 
  59 static GString *
  60 mc_search__hex_translate_to_regex (const GString *astr, mc_search_hex_parse_error_t *error_ptr,
     /* [previous][next][first][last][top][bottom][index][help]  */
  61                                    int *error_pos_ptr)
  62 {
  63     GString *buff;
  64     const char *str;
  65     gsize str_len;
  66     gsize loop = 0;
  67     mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
  68 
  69     buff = g_string_sized_new (64);
  70     str = astr->str;
  71     str_len = astr->len;
  72 
  73     while (loop < str_len && error == MC_SEARCH_HEX_E_OK)
  74     {
  75         unsigned int val;
  76         int ptr;
  77 
  78         if (g_ascii_isspace (str[loop]))
  79         {
  80             /* Eat-up whitespace between tokens. */
  81             while (g_ascii_isspace (str[loop]))
  82                 loop++;
  83         }
  84         /* cppcheck-suppress invalidscanf */
  85         else if (sscanf (str + loop, "%x%n", &val, &ptr) == 1)
  86         {
  87             if (val > 255)
  88                 error = MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE;
  89             else
  90             {
  91                 g_string_append_printf (buff, "\\x%02X", val);
  92                 loop += ptr;
  93             }
  94         }
  95         else if (str[loop] == '"')
  96         {
  97             gsize loop2;
  98 
  99             loop2 = loop + 1;
 100 
 101             while (loop2 < str_len)
 102             {
 103                 if (str[loop2] == '"')
 104                     break;
 105                 if (str[loop2] == '\\' && loop2 + 1 < str_len)
 106                     loop2++;
 107                 g_string_append_c (buff, str[loop2]);
 108                 loop2++;
 109             }
 110 
 111             if (str[loop2] == '\0')
 112                 error = MC_SEARCH_HEX_E_UNMATCHED_QUOTES;
 113             else
 114                 loop = loop2 + 1;
 115         }
 116         else
 117             error = MC_SEARCH_HEX_E_INVALID_CHARACTER;
 118     }
 119 
 120     if (error != MC_SEARCH_HEX_E_OK)
 121     {
 122         g_string_free (buff, TRUE);
 123         if (error_ptr != NULL)
 124             *error_ptr = error;
 125         if (error_pos_ptr != NULL)
 126             *error_pos_ptr = loop;
 127         return NULL;
 128     }
 129 
 130     return buff;
 131 }
 132 
 133 /* --------------------------------------------------------------------------------------------- */
 134 /*** public functions ****************************************************************************/
 135 /* --------------------------------------------------------------------------------------------- */
 136 
 137 void
 138 mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t *lc_mc_search,
     /* [previous][next][first][last][top][bottom][index][help]  */
 139                                      mc_search_cond_t *mc_search_cond)
 140 {
 141     GString *tmp;
 142     mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
 143     int error_pos = 0;
 144 
 145     /*
 146      * We may be searching in binary data, which is often invalid UTF-8.
 147      *
 148      * We have to create a non UTF-8 regex (that is, G_REGEX_RAW) or else, as
 149      * the data is invalid UTF-8, both GLib's PCRE and our
 150      * mc_search__g_regex_match_full_safe() are going to fail us. The former by
 151      * not finding all bytes, the latter by overwriting the supposedly invalid
 152      * UTF-8 with NULs.
 153      *
 154      * To do this, we specify "ASCII" as the charset.
 155      *
 156      * In fact, we can specify any charset other than "UTF-8": any such charset
 157      * will trigger G_REGEX_RAW (see [1]). The output of [2] will be the same
 158      * for all charsets because it skips the \xXX symbols
 159      * mc_search__hex_translate_to_regex() outputs.
 160      *
 161      * But "ASCII" is the best choice because a hex pattern may contain a
 162      * quoted string: this way we know [2] will ignore any characters outside
 163      * ASCII letters range (these ignored chars will be copied verbatim to the
 164      * output and will match as-is; in other words, in a case-sensitive manner;
 165      * If the user is interested in case-insensitive searches of international
 166      * text, he shouldn't be using hex search in the first place.)
 167      *
 168      * Switching out of UTF-8 has another advantage:
 169      *
 170      * When doing case-insensitive searches, GLib treats \xXX symbols as normal
 171      * letters and therefore matches both "a" and "A" for the hex pattern
 172      * "0x61". When we switch out of UTF-8, we're switching to using [2], which
 173      * doesn't have this issue.
 174      *
 175      * [1] mc_search__cond_struct_new_init_regex
 176      * [2] mc_search__cond_struct_new_regex_ci_str
 177      */
 178     if (str_isutf8 (charset))
 179         charset = "ASCII";
 180 
 181     tmp = mc_search__hex_translate_to_regex (mc_search_cond->str, &error, &error_pos);
 182     if (tmp != NULL)
 183     {
 184         g_string_free (mc_search_cond->str, TRUE);
 185         mc_search_cond->str = tmp;
 186         mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
 187     }
 188     else
 189     {
 190         const char *desc;
 191 
 192         switch (error)
 193         {
 194         case MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE:
 195             desc =
 196                 _
 197                 ("Number out of range (should be in byte range, 0 <= n <= 0xFF, expressed in hex)");
 198             break;
 199         case MC_SEARCH_HEX_E_INVALID_CHARACTER:
 200             desc = _("Invalid character");
 201             break;
 202         case MC_SEARCH_HEX_E_UNMATCHED_QUOTES:
 203             desc = _("Unmatched quotes character");
 204             break;
 205         default:
 206             desc = "";
 207         }
 208 
 209         lc_mc_search->error = MC_SEARCH_E_INPUT;
 210         lc_mc_search->error_str =
 211             g_strdup_printf (_("Hex pattern error at position %d:\n%s."), error_pos + 1, desc);
 212     }
 213 }
 214 
 215 /* --------------------------------------------------------------------------------------------- */
 216 
 217 gboolean
 218 mc_search__run_hex (mc_search_t *lc_mc_search, const void *user_data,
     /* [previous][next][first][last][top][bottom][index][help]  */
 219                     gsize start_search, gsize end_search, gsize *found_len)
 220 {
 221     return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
 222 }
 223 
 224 /* --------------------------------------------------------------------------------------------- */
 225 
 226 GString *
 227 mc_search_hex_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 228 {
 229     (void) lc_mc_search;
 230 
 231     return mc_g_string_dup (replace_str);
 232 }
 233 
 234 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */