1/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 Copyright(C) 2013-2017 Kouhei Sutou <kou@clear-code.com>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#include <mrn_mysql.h>
21#include <mrn_mysql_compat.h>
22#include <mrn_path_mapper.hpp>
23#include <mrn_windows.hpp>
24#include <mrn_macro.hpp>
25#include <mrn_variables.hpp>
26#include <mrn_context_pool.hpp>
27
28MRN_BEGIN_DECLS
29
30extern mrn::ContextPool *mrn_context_pool;
31
32struct EscapeInfo
33{
34 grn_ctx *ctx;
35 bool script_mode;
36 grn_obj target_characters;
37 grn_obj escaped_value;
38};
39
40MRN_API my_bool mroonga_escape_init(UDF_INIT *init, UDF_ARGS *args,
41 char *message)
42{
43 EscapeInfo *info = NULL;
44 bool script_mode = false;
45
46 init->ptr = NULL;
47 if (!(1 <= args->arg_count && args->arg_count <= 2)) {
48 snprintf(message,
49 MYSQL_ERRMSG_SIZE,
50 "mroonga_escape(): Incorrect number of arguments: %u for 1..2",
51 args->arg_count);
52 goto error;
53 }
54
55 if (args->attribute_lengths[0] == strlen("script") &&
56 strncmp(args->attributes[0], "script", strlen("script")) == 0) {
57 switch (args->arg_type[0]) {
58 case ROW_RESULT:
59 snprintf(message,
60 MYSQL_ERRMSG_SIZE,
61 "mroonga_escape(): "
62 "The 1st script argument must be "
63 "string, integer or floating point: <row>");
64 goto error;
65 break;
66 default:
67 break;
68 }
69 script_mode = true;
70 } else {
71 if (args->arg_type[0] != STRING_RESULT) {
72 strcpy(message,
73 "mroonga_escape(): The 1st query argument must be string");
74 goto error;
75 }
76 }
77 if (args->arg_count == 2) {
78 if (args->arg_type[1] != STRING_RESULT) {
79 strcpy(message,
80 "mroonga_escape(): "
81 "The 2st argument must be escape target characters as string");
82 goto error;
83 }
84 }
85
86 init->maybe_null = 1;
87
88 info = static_cast<EscapeInfo *>(mrn_my_malloc(sizeof(EscapeInfo),
89 MYF(MY_WME | MY_ZEROFILL)));
90 if (!info) {
91 strcpy(message, "mroonga_escape(): out of memory");
92 goto error;
93 }
94
95 info->ctx = mrn_context_pool->pull();
96 info->script_mode = script_mode;
97 GRN_TEXT_INIT(&(info->target_characters), 0);
98 GRN_TEXT_INIT(&(info->escaped_value), 0);
99
100 init->ptr = reinterpret_cast<char *>(info);
101
102 return FALSE;
103
104error:
105 if (info) {
106 mrn_context_pool->release(info->ctx);
107 my_free(info);
108 }
109 return TRUE;
110}
111
112static void escape(EscapeInfo *info, UDF_ARGS *args)
113{
114 grn_ctx *ctx = info->ctx;
115
116 GRN_BULK_REWIND(&(info->escaped_value));
117 if (info->script_mode) {
118 switch (args->arg_type[0]) {
119 case STRING_RESULT:
120 {
121 char *value = args->args[0];
122 unsigned long value_length = args->lengths[0];
123 GRN_TEXT_PUTC(ctx, &(info->escaped_value), '"');
124 if (args->arg_count == 2) {
125 grn_obj special_characters;
126 GRN_TEXT_INIT(&special_characters, 0);
127 GRN_TEXT_PUT(ctx,
128 &special_characters,
129 args->args[1],
130 args->lengths[1]);
131 GRN_TEXT_PUTC(ctx, &special_characters, '\0');
132 grn_expr_syntax_escape(ctx,
133 value,
134 value_length,
135 GRN_TEXT_VALUE(&special_characters),
136 '\\',
137 &(info->escaped_value));
138 GRN_OBJ_FIN(ctx, &special_characters);
139 } else {
140 const char *special_characters = "\"\\";
141 grn_expr_syntax_escape(ctx,
142 value,
143 value_length,
144 special_characters,
145 '\\',
146 &(info->escaped_value));
147 }
148 GRN_TEXT_PUTC(ctx, &(info->escaped_value), '"');
149 }
150 break;
151 case REAL_RESULT:
152 {
153 double value = *reinterpret_cast<double *>(args->args[0]);
154 grn_text_ftoa(ctx, &(info->escaped_value), value);
155 }
156 break;
157 case INT_RESULT:
158 {
159 longlong value = *reinterpret_cast<longlong *>(args->args[0]);
160 grn_text_lltoa(ctx, &(info->escaped_value), value);
161 }
162 break;
163 case DECIMAL_RESULT:
164 {
165 grn_obj value_raw;
166 GRN_TEXT_INIT(&value_raw, GRN_OBJ_DO_SHALLOW_COPY);
167 GRN_TEXT_SET(ctx, &value_raw, args->args[0], args->lengths[0]);
168 grn_obj value;
169 GRN_FLOAT_INIT(&value, 0);
170 if (grn_obj_cast(ctx, &value_raw, &value, GRN_FALSE) == GRN_SUCCESS) {
171 grn_text_ftoa(ctx, &(info->escaped_value), GRN_FLOAT_VALUE(&value));
172 } else {
173 GRN_TEXT_PUT(ctx,
174 &(info->escaped_value),
175 args->args[0],
176 args->lengths[0]);
177 }
178 GRN_OBJ_FIN(ctx, &value);
179 GRN_OBJ_FIN(ctx, &value_raw);
180 }
181 break;
182 default:
183 break;
184 }
185 } else {
186 char *query = args->args[0];
187 unsigned long query_length = args->lengths[0];
188 if (args->arg_count == 2) {
189 char *target_characters = args->args[1];
190 unsigned long target_characters_length = args->lengths[1];
191 GRN_TEXT_PUT(ctx, &(info->target_characters),
192 target_characters,
193 target_characters_length);
194 GRN_TEXT_PUTC(ctx, &(info->target_characters), '\0');
195 grn_expr_syntax_escape(ctx, query, query_length,
196 GRN_TEXT_VALUE(&(info->target_characters)),
197 GRN_QUERY_ESCAPE,
198 &(info->escaped_value));
199 } else {
200 grn_expr_syntax_escape_query(ctx, query, query_length,
201 &(info->escaped_value));
202 }
203 }
204}
205
206MRN_API char *mroonga_escape(UDF_INIT *init, UDF_ARGS *args, char *result,
207 unsigned long *length, char *is_null, char *error)
208{
209 EscapeInfo *info = reinterpret_cast<EscapeInfo *>(init->ptr);
210 grn_ctx *ctx = info->ctx;
211
212 if (!args->args[0]) {
213 *is_null = 1;
214 return NULL;
215 }
216
217 *is_null = 0;
218
219 escape(info, args);
220
221 if (ctx->rc) {
222 my_message(ER_ERROR_ON_WRITE, ctx->errbuf, MYF(0));
223 goto error;
224 }
225
226 *length = GRN_TEXT_LEN(&(info->escaped_value));
227 return GRN_TEXT_VALUE(&(info->escaped_value));
228
229error:
230 *error = 1;
231 return NULL;
232}
233
234MRN_API void mroonga_escape_deinit(UDF_INIT *init)
235{
236 EscapeInfo *info = reinterpret_cast<EscapeInfo *>(init->ptr);
237 if (info) {
238 grn_obj_unlink(info->ctx, &(info->target_characters));
239 grn_obj_unlink(info->ctx, &(info->escaped_value));
240 mrn_context_pool->release(info->ctx);
241 my_free(info);
242 }
243}
244
245MRN_END_DECLS
246