hex.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "lib/charsets.h"
  27. #include "internal.h"
  28. /*** global variables ****************************************************************************/
  29. /*** file scope macro definitions ****************************************************************/
  30. /*** file scope type declarations ****************************************************************/
  31. /*** file scope variables ************************************************************************/
  32. /*** file scope functions ************************************************************************/
  33. static GString *
  34. mc_search__hex_translate_to_regex (const GString * astr)
  35. {
  36. GString *buff;
  37. gchar *tmp_str;
  38. gsize tmp_str_len;
  39. gsize loop = 0;
  40. buff = g_string_sized_new (64);
  41. tmp_str = g_strndup (astr->str, astr->len);
  42. g_strchug (tmp_str); /* trim leadind whitespaces */
  43. tmp_str_len = strlen (tmp_str);
  44. while (loop < tmp_str_len)
  45. {
  46. int val, ptr;
  47. if (sscanf (tmp_str + loop, "%i%n", &val, &ptr))
  48. {
  49. if (val < -128 || val > 255)
  50. loop++;
  51. else
  52. {
  53. g_string_append_printf (buff, "\\x%02X", (unsigned char) val);
  54. loop += ptr;
  55. }
  56. }
  57. else if (*(tmp_str + loop) == '"')
  58. {
  59. gsize loop2 = 0;
  60. loop++;
  61. while (loop + loop2 < tmp_str_len)
  62. {
  63. if (*(tmp_str + loop + loop2) == '"' &&
  64. !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2))
  65. break;
  66. loop2++;
  67. }
  68. g_string_append_len (buff, tmp_str + loop, loop2 - 1);
  69. loop += loop2;
  70. }
  71. else
  72. loop++;
  73. }
  74. g_free (tmp_str);
  75. return buff;
  76. }
  77. /*** public functions ****************************************************************************/
  78. void
  79. mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_search,
  80. mc_search_cond_t * mc_search_cond)
  81. {
  82. GString *tmp;
  83. tmp = mc_search__hex_translate_to_regex (mc_search_cond->str);
  84. g_string_free (mc_search_cond->str, TRUE);
  85. mc_search_cond->str = tmp;
  86. mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. gboolean
  90. mc_search__run_hex (mc_search_t * lc_mc_search, const void *user_data,
  91. gsize start_search, gsize end_search, gsize * found_len)
  92. {
  93. return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
  94. }
  95. /* --------------------------------------------------------------------------------------------- */
  96. GString *
  97. mc_search_hex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  98. {
  99. (void) lc_mc_search;
  100. return g_string_new_len (replace_str->str, replace_str->len);
  101. }
  102. /* --------------------------------------------------------------------------------------------- */