hex.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Search text engine.
  3. HEX-style pattern matching
  4. Copyright (C) 2009, 2011
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Slava Zanko <slavazanko@gmail.com>, 2009.
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software: you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <stdio.h>
  22. #include "lib/global.h"
  23. #include "lib/strutil.h"
  24. #include "lib/search.h"
  25. #include "lib/strescape.h"
  26. #include "internal.h"
  27. /*** global variables ****************************************************************************/
  28. /*** file scope macro definitions ****************************************************************/
  29. /*** file scope type declarations ****************************************************************/
  30. /*** file scope variables ************************************************************************/
  31. /*** file scope functions ************************************************************************/
  32. static GString *
  33. mc_search__hex_translate_to_regex (const GString * astr)
  34. {
  35. GString *buff;
  36. gchar *tmp_str, *tmp_str2;
  37. gsize tmp_str_len;
  38. gsize loop = 0;
  39. buff = g_string_sized_new (64);
  40. tmp_str = g_strndup (astr->str, astr->len);
  41. tmp_str2 = tmp_str;
  42. /* remove 0x prefices */
  43. while (TRUE)
  44. {
  45. tmp_str2 = strstr (tmp_str2, "0x");
  46. if (tmp_str2 == NULL)
  47. break;
  48. *tmp_str2++ = ' ';
  49. *tmp_str2++ = ' ';
  50. }
  51. g_strchug (tmp_str); /* trim leadind whitespaces */
  52. tmp_str_len = strlen (tmp_str);
  53. while (loop < tmp_str_len)
  54. {
  55. int val, ptr;
  56. /* cppcheck-suppress invalidscanf */
  57. if (sscanf (tmp_str + loop, "%x%n", &val, &ptr))
  58. {
  59. if (val < -128 || val > 255)
  60. loop++;
  61. else
  62. {
  63. g_string_append_printf (buff, "\\x%02X", (unsigned char) val);
  64. loop += ptr;
  65. }
  66. }
  67. else if (*(tmp_str + loop) == '"')
  68. {
  69. gsize loop2 = 0;
  70. loop++;
  71. while (loop + loop2 < tmp_str_len)
  72. {
  73. if (*(tmp_str + loop + loop2) == '"' &&
  74. !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2))
  75. break;
  76. loop2++;
  77. }
  78. g_string_append_len (buff, tmp_str + loop, loop2 - 1);
  79. loop += loop2;
  80. }
  81. else
  82. loop++;
  83. }
  84. g_free (tmp_str);
  85. return buff;
  86. }
  87. /*** public functions ****************************************************************************/
  88. void
  89. mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_search,
  90. mc_search_cond_t * mc_search_cond)
  91. {
  92. GString *tmp;
  93. g_string_ascii_down (mc_search_cond->str);
  94. tmp = mc_search__hex_translate_to_regex (mc_search_cond->str);
  95. g_string_free (mc_search_cond->str, TRUE);
  96. mc_search_cond->str = tmp;
  97. mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. gboolean
  101. mc_search__run_hex (mc_search_t * lc_mc_search, const void *user_data,
  102. gsize start_search, gsize end_search, gsize * found_len)
  103. {
  104. return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
  105. }
  106. /* --------------------------------------------------------------------------------------------- */
  107. GString *
  108. mc_search_hex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  109. {
  110. (void) lc_mc_search;
  111. return g_string_new_len (replace_str->str, replace_str->len);
  112. }
  113. /* --------------------------------------------------------------------------------------------- */