hex.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. if (sscanf (tmp_str + loop, "%x%n", &val, &ptr))
  57. {
  58. if (val < -128 || val > 255)
  59. loop++;
  60. else
  61. {
  62. g_string_append_printf (buff, "\\x%02X", (unsigned char) val);
  63. loop += ptr;
  64. }
  65. }
  66. else if (*(tmp_str + loop) == '"')
  67. {
  68. gsize loop2 = 0;
  69. loop++;
  70. while (loop + loop2 < tmp_str_len)
  71. {
  72. if (*(tmp_str + loop + loop2) == '"' &&
  73. !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2))
  74. break;
  75. loop2++;
  76. }
  77. g_string_append_len (buff, tmp_str + loop, loop2 - 1);
  78. loop += loop2;
  79. }
  80. else
  81. loop++;
  82. }
  83. g_free (tmp_str);
  84. return buff;
  85. }
  86. /*** public functions ****************************************************************************/
  87. void
  88. mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_search,
  89. mc_search_cond_t * mc_search_cond)
  90. {
  91. GString *tmp;
  92. g_string_ascii_down (mc_search_cond->str);
  93. tmp = mc_search__hex_translate_to_regex (mc_search_cond->str);
  94. g_string_free (mc_search_cond->str, TRUE);
  95. mc_search_cond->str = tmp;
  96. mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
  97. }
  98. /* --------------------------------------------------------------------------------------------- */
  99. gboolean
  100. mc_search__run_hex (mc_search_t * lc_mc_search, const void *user_data,
  101. gsize start_search, gsize end_search, gsize * found_len)
  102. {
  103. return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
  104. }
  105. /* --------------------------------------------------------------------------------------------- */
  106. GString *
  107. mc_search_hex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  108. {
  109. (void) lc_mc_search;
  110. return g_string_new_len (replace_str->str, replace_str->len);
  111. }
  112. /* --------------------------------------------------------------------------------------------- */