hex.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "src/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 (gchar * str, gsize * len)
  36. {
  37. GString *buff = g_string_new ("");
  38. gchar *tmp_str = g_strndup (str, *len);
  39. gchar *tmp_str2;
  40. gsize loop = 0;
  41. int val, ptr;
  42. g_strchug (tmp_str); /* trim leadind whitespaces */
  43. while (loop < *len) {
  44. if (sscanf (tmp_str + loop, "%i%n", &val, &ptr)) {
  45. if (val < -128 || val > 255) {
  46. loop++;
  47. continue;
  48. }
  49. tmp_str2 = g_strdup_printf ("\\x%02X", (unsigned char) val);
  50. g_string_append (buff, tmp_str2);
  51. g_free (tmp_str2);
  52. loop += ptr;
  53. continue;
  54. }
  55. if (*(tmp_str + loop) == '"') {
  56. gsize loop2 = 0;
  57. loop++;
  58. while (loop + loop2 < *len) {
  59. if (*(tmp_str + loop + loop2) == '"' &&
  60. !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2))
  61. break;
  62. loop2++;
  63. }
  64. g_string_append_len (buff, tmp_str + loop, loop2 - 1);
  65. loop += loop2;
  66. continue;
  67. }
  68. loop++;
  69. }
  70. g_free (tmp_str);
  71. *len = buff->len;
  72. return buff;
  73. }
  74. /*** public functions ****************************************************************************/
  75. void
  76. mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_search,
  77. mc_search_cond_t * mc_search_cond)
  78. {
  79. GString *tmp =
  80. mc_search__hex_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->len);
  81. g_string_free (mc_search_cond->str, TRUE);
  82. mc_search_cond->str = tmp;
  83. mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
  84. }
  85. /* --------------------------------------------------------------------------------------------- */
  86. gboolean
  87. mc_search__run_hex (mc_search_t * lc_mc_search, const void *user_data,
  88. gsize start_search, gsize end_search, gsize * found_len)
  89. {
  90. return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
  91. }
  92. /* --------------------------------------------------------------------------------------------- */
  93. GString *
  94. mc_search_hex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  95. {
  96. (void) lc_mc_search;
  97. return g_string_new_len (replace_str->str, replace_str->len);
  98. }
  99. /* --------------------------------------------------------------------------------------------- */