hex.c 4.2 KB

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