This source file includes following definitions.
- mc_search__hex_translate_to_regex
- mc_search__cond_struct_new_init_hex
- mc_search__run_hex
- mc_search_hex_prepare_replace_str
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 #include <config.h>
28
29 #include <stdio.h>
30
31 #include "lib/global.h"
32 #include "lib/strutil.h"
33 #include "lib/search.h"
34
35 #include "internal.h"
36
37
38
39
40
41 typedef enum
42 {
43 MC_SEARCH_HEX_E_OK,
44 MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE,
45 MC_SEARCH_HEX_E_INVALID_CHARACTER,
46 MC_SEARCH_HEX_E_UNMATCHED_QUOTES
47 } mc_search_hex_parse_error_t;
48
49
50
51
52
53
54
55
56
57
58
59 static GString *
60 mc_search__hex_translate_to_regex (const GString *astr, mc_search_hex_parse_error_t *error_ptr,
61 int *error_pos_ptr)
62 {
63 GString *buff;
64 const char *str;
65 gsize str_len;
66 gsize loop = 0;
67 mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
68
69 buff = g_string_sized_new (64);
70 str = astr->str;
71 str_len = astr->len;
72
73 while (loop < str_len && error == MC_SEARCH_HEX_E_OK)
74 {
75 unsigned int val;
76 int ptr;
77
78 if (g_ascii_isspace (str[loop]))
79 {
80
81 while (g_ascii_isspace (str[loop]))
82 loop++;
83 }
84 else if (sscanf (str + loop, "%x%n", &val, &ptr) == 1)
85 {
86 if (val > 255)
87 error = MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE;
88 else
89 {
90 g_string_append_printf (buff, "\\x%02X", val);
91 loop += ptr;
92 }
93 }
94 else if (str[loop] == '"')
95 {
96 gsize loop2;
97
98 loop2 = loop + 1;
99
100 while (loop2 < str_len)
101 {
102 if (str[loop2] == '"')
103 break;
104 if (str[loop2] == '\\' && loop2 + 1 < str_len)
105 loop2++;
106 g_string_append_c (buff, str[loop2]);
107 loop2++;
108 }
109
110 if (str[loop2] == '\0')
111 error = MC_SEARCH_HEX_E_UNMATCHED_QUOTES;
112 else
113 loop = loop2 + 1;
114 }
115 else
116 error = MC_SEARCH_HEX_E_INVALID_CHARACTER;
117 }
118
119 if (error != MC_SEARCH_HEX_E_OK)
120 {
121 g_string_free (buff, TRUE);
122 if (error_ptr != NULL)
123 *error_ptr = error;
124 if (error_pos_ptr != NULL)
125 *error_pos_ptr = loop;
126 return NULL;
127 }
128
129 return buff;
130 }
131
132
133
134
135
136 void
137 mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t *lc_mc_search,
138 mc_search_cond_t *mc_search_cond)
139 {
140 GString *tmp;
141 mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
142 int error_pos = 0;
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 if (str_isutf8 (charset))
178 charset = "ASCII";
179
180 tmp = mc_search__hex_translate_to_regex (mc_search_cond->str, &error, &error_pos);
181 if (tmp != NULL)
182 {
183 g_string_free (mc_search_cond->str, TRUE);
184 mc_search_cond->str = tmp;
185 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
186 }
187 else
188 {
189 const char *desc;
190
191 switch (error)
192 {
193 case MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE:
194 desc = _ (
195 "Number out of range (should be in byte range, 0 <= n <= 0xFF, expressed in hex)");
196 break;
197 case MC_SEARCH_HEX_E_INVALID_CHARACTER:
198 desc = _ ("Invalid character");
199 break;
200 case MC_SEARCH_HEX_E_UNMATCHED_QUOTES:
201 desc = _ ("Unmatched quotes character");
202 break;
203 default:
204 desc = "";
205 }
206
207 lc_mc_search->error = MC_SEARCH_E_INPUT;
208 lc_mc_search->error_str =
209 g_strdup_printf (_ ("Hex pattern error at position %d:\n%s."), error_pos + 1, desc);
210 }
211 }
212
213
214
215 gboolean
216 mc_search__run_hex (mc_search_t *lc_mc_search, const void *user_data, off_t start_search,
217 off_t end_search, gsize *found_len)
218 {
219 return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
220 }
221
222
223
224 GString *
225 mc_search_hex_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace_str)
226 {
227 (void) lc_mc_search;
228
229 return mc_g_string_dup (replace_str);
230 }
231
232