1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
1 |
/*
|
2 |
* Drizzle Client & Protocol Library
|
|
3 |
*
|
|
4 |
* Copyright (C) 2008 Eric Day (eday@oddments.org)
|
|
5 |
* All rights reserved.
|
|
6 |
*
|
|
1971.2.1
by kalebral at gmail
update files that did not have license or had incorrect license structure |
7 |
* Redistribution and use in source and binary forms, with or without
|
8 |
* modification, are permitted provided that the following conditions are
|
|
9 |
* met:
|
|
10 |
*
|
|
11 |
* * Redistributions of source code must retain the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer.
|
|
13 |
*
|
|
14 |
* * Redistributions in binary form must reproduce the above
|
|
15 |
* copyright notice, this list of conditions and the following disclaimer
|
|
16 |
* in the documentation and/or other materials provided with the
|
|
17 |
* distribution.
|
|
18 |
*
|
|
19 |
* * The names of its contributors may not be used to endorse or
|
|
20 |
* promote products derived from this software without specific prior
|
|
21 |
* written permission.
|
|
22 |
*
|
|
23 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
24 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
25 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
26 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
27 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
28 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
29 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
30 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
31 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
32 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
33 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
34 |
*
|
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
35 |
*/
|
36 |
||
37 |
/**
|
|
38 |
* @file
|
|
39 |
* @brief Query definitions
|
|
40 |
*/
|
|
41 |
||
2449.1.4
by Brian Aker
Complete update of libdrizzle |
42 |
#include <libdrizzle-1.0/common.h> |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
43 |
|
44 |
drizzle_result_st *drizzle_query(drizzle_con_st *con, drizzle_result_st *result, |
|
45 |
const char *query, size_t size, |
|
46 |
drizzle_return_t *ret_ptr) |
|
47 |
{
|
|
48 |
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY, |
|
49 |
(uint8_t *)query, size, size, ret_ptr); |
|
50 |
}
|
|
51 |
||
52 |
drizzle_result_st *drizzle_query_str(drizzle_con_st *con, |
|
53 |
drizzle_result_st *result, |
|
54 |
const char *query, |
|
55 |
drizzle_return_t *ret_ptr) |
|
56 |
{
|
|
57 |
size_t size; |
|
58 |
||
59 |
size= strlen(query); |
|
60 |
||
61 |
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY, |
|
62 |
(uint8_t *)query, size, size, ret_ptr); |
|
63 |
}
|
|
64 |
||
65 |
drizzle_result_st *drizzle_query_inc(drizzle_con_st *con, |
|
66 |
drizzle_result_st *result, |
|
67 |
const char *query, size_t size, |
|
68 |
size_t total, drizzle_return_t *ret_ptr) |
|
69 |
{
|
|
70 |
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY, |
|
71 |
(uint8_t *)query, size, total, ret_ptr); |
|
72 |
}
|
|
73 |
||
74 |
drizzle_query_st *drizzle_query_add(drizzle_st *drizzle, |
|
75 |
drizzle_query_st *query, |
|
76 |
drizzle_con_st *con, |
|
77 |
drizzle_result_st *result, |
|
78 |
const char *query_string, size_t size, |
|
79 |
drizzle_query_options_t options, |
|
80 |
void *context) |
|
81 |
{
|
|
82 |
query= drizzle_query_create(drizzle, query); |
|
83 |
if (query == NULL) |
|
84 |
return NULL; |
|
85 |
||
86 |
drizzle_query_set_con(query, con); |
|
87 |
drizzle_query_set_result(query, result); |
|
88 |
drizzle_query_set_string(query, query_string, size); |
|
89 |
drizzle_query_add_options(query, options); |
|
90 |
drizzle_query_set_context(query, context); |
|
91 |
||
92 |
return query; |
|
93 |
}
|
|
94 |
||
95 |
drizzle_query_st *drizzle_query_create(drizzle_st *drizzle, |
|
96 |
drizzle_query_st *query) |
|
97 |
{
|
|
98 |
if (query == NULL) |
|
99 |
{
|
|
1992.6.7
by Monty Taylor
Revert -Wc++-compat change. |
100 |
query= malloc(sizeof(drizzle_query_st)); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
101 |
if (query == NULL) |
102 |
{
|
|
103 |
drizzle_set_error(drizzle, "drizzle_query_create", "malloc"); |
|
104 |
return NULL; |
|
105 |
}
|
|
106 |
||
107 |
memset(query, 0, sizeof(drizzle_query_st)); |
|
108 |
query->options|= DRIZZLE_CON_ALLOCATED; |
|
109 |
}
|
|
110 |
else
|
|
111 |
memset(query, 0, sizeof(drizzle_query_st)); |
|
112 |
||
113 |
query->drizzle= drizzle; |
|
114 |
||
115 |
if (drizzle->query_list) |
|
116 |
drizzle->query_list->prev= query; |
|
117 |
query->next= drizzle->query_list; |
|
118 |
drizzle->query_list= query; |
|
119 |
drizzle->query_count++; |
|
120 |
drizzle->query_new++; |
|
121 |
||
122 |
return query; |
|
123 |
}
|
|
124 |
||
125 |
void drizzle_query_free(drizzle_query_st *query) |
|
126 |
{
|
|
127 |
if (query->context != NULL && query->context_free_fn != NULL) |
|
128 |
query->context_free_fn(query, query->context); |
|
129 |
||
130 |
if (query->drizzle->query_list == query) |
|
131 |
query->drizzle->query_list= query->next; |
|
132 |
if (query->prev) |
|
133 |
query->prev->next= query->next; |
|
134 |
if (query->next) |
|
135 |
query->next->prev= query->prev; |
|
136 |
query->drizzle->query_count--; |
|
137 |
||
138 |
if (query->options & DRIZZLE_QUERY_ALLOCATED) |
|
139 |
free(query); |
|
140 |
}
|
|
141 |
||
142 |
void drizzle_query_free_all(drizzle_st *drizzle) |
|
143 |
{
|
|
144 |
while (drizzle->query_list != NULL) |
|
145 |
drizzle_query_free(drizzle->query_list); |
|
146 |
}
|
|
147 |
||
148 |
drizzle_con_st *drizzle_query_con(drizzle_query_st *query) |
|
149 |
{
|
|
150 |
return query->con; |
|
151 |
}
|
|
152 |
||
153 |
void drizzle_query_set_con(drizzle_query_st *query, drizzle_con_st *con) |
|
154 |
{
|
|
155 |
query->con= con; |
|
156 |
}
|
|
157 |
||
158 |
drizzle_result_st *drizzle_query_result(drizzle_query_st *query) |
|
159 |
{
|
|
160 |
return query->result; |
|
161 |
}
|
|
162 |
||
163 |
void drizzle_query_set_result(drizzle_query_st *query, |
|
164 |
drizzle_result_st *result) |
|
165 |
{
|
|
166 |
query->result= result; |
|
167 |
}
|
|
168 |
||
169 |
char *drizzle_query_string(drizzle_query_st *query, size_t *size) |
|
170 |
{
|
|
171 |
*size= query->size; |
|
172 |
return (char *)(query->string); |
|
173 |
}
|
|
174 |
||
175 |
void drizzle_query_set_string(drizzle_query_st *query, const char *string, |
|
176 |
size_t size) |
|
177 |
{
|
|
178 |
query->string= string; |
|
179 |
query->size= size; |
|
180 |
}
|
|
181 |
||
1992.6.7
by Monty Taylor
Revert -Wc++-compat change. |
182 |
drizzle_query_options_t drizzle_query_options(drizzle_query_st *query) |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
183 |
{
|
184 |
return query->options; |
|
185 |
}
|
|
186 |
||
187 |
void drizzle_query_set_options(drizzle_query_st *query, |
|
1992.6.7
by Monty Taylor
Revert -Wc++-compat change. |
188 |
drizzle_query_options_t options) |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
189 |
{
|
190 |
query->options= options; |
|
191 |
}
|
|
192 |
||
193 |
void drizzle_query_add_options(drizzle_query_st *query, |
|
1992.6.7
by Monty Taylor
Revert -Wc++-compat change. |
194 |
drizzle_query_options_t options) |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
195 |
{
|
196 |
query->options|= options; |
|
197 |
}
|
|
198 |
||
199 |
void drizzle_query_remove_options(drizzle_query_st *query, |
|
1992.6.7
by Monty Taylor
Revert -Wc++-compat change. |
200 |
drizzle_query_options_t options) |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
201 |
{
|
202 |
query->options&= ~options; |
|
203 |
}
|
|
204 |
||
205 |
void *drizzle_query_context(drizzle_query_st *query) |
|
206 |
{
|
|
207 |
return query->context; |
|
208 |
}
|
|
209 |
||
210 |
void drizzle_query_set_context(drizzle_query_st *query, void *context) |
|
211 |
{
|
|
212 |
query->context= context; |
|
213 |
}
|
|
214 |
||
215 |
void drizzle_query_set_context_free_fn(drizzle_query_st *query, |
|
216 |
drizzle_query_context_free_fn *function) |
|
217 |
{
|
|
218 |
query->context_free_fn= function; |
|
219 |
}
|
|
220 |
||
221 |
static void drizzle_query_run_state(drizzle_query_st* query, |
|
222 |
drizzle_return_t* ret_ptr) |
|
223 |
{
|
|
224 |
switch (query->state) |
|
225 |
{
|
|
226 |
case DRIZZLE_QUERY_STATE_INIT: |
|
227 |
query->state= DRIZZLE_QUERY_STATE_QUERY; |
|
228 |
case DRIZZLE_QUERY_STATE_QUERY: |
|
229 |
query->result= drizzle_query(query->con, query->result, query->string, |
|
230 |
query->size, ret_ptr); |
|
231 |
if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT) |
|
232 |
{
|
|
233 |
return; |
|
234 |
}
|
|
235 |
else if (*ret_ptr != DRIZZLE_RETURN_OK) |
|
236 |
{
|
|
237 |
query->state= DRIZZLE_QUERY_STATE_DONE; |
|
238 |
return; |
|
239 |
}
|
|
240 |
||
241 |
query->state= DRIZZLE_QUERY_STATE_RESULT; |
|
242 |
||
243 |
case DRIZZLE_QUERY_STATE_RESULT: |
|
244 |
*ret_ptr= drizzle_result_buffer(query->result); |
|
245 |
if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT) |
|
246 |
{
|
|
247 |
return; |
|
248 |
}
|
|
249 |
||
250 |
query->state= DRIZZLE_QUERY_STATE_DONE; |
|
251 |
return; |
|
252 |
||
253 |
default: |
|
254 |
case DRIZZLE_QUERY_STATE_DONE: |
|
255 |
return; |
|
256 |
}
|
|
257 |
}
|
|
258 |
||
259 |
drizzle_query_st *drizzle_query_run(drizzle_st *drizzle, |
|
260 |
drizzle_return_t *ret_ptr) |
|
261 |
{
|
|
1992.6.7
by Monty Taylor
Revert -Wc++-compat change. |
262 |
drizzle_options_t options; |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
263 |
drizzle_query_st *query; |
264 |
drizzle_con_st *con; |
|
265 |
||
266 |
if (drizzle->query_new == 0 && drizzle->query_running == 0) |
|
267 |
{
|
|
268 |
*ret_ptr= DRIZZLE_RETURN_OK; |
|
269 |
return NULL; |
|
270 |
}
|
|
271 |
||
272 |
options= drizzle->options; |
|
273 |
drizzle->options|= DRIZZLE_NON_BLOCKING; |
|
274 |
||
275 |
/* Check to see if any queries need to be started. */
|
|
276 |
if (drizzle->query_new > 0) |
|
277 |
{
|
|
278 |
for (query= drizzle->query_list; query != NULL; query= query->next) |
|
279 |
{
|
|
280 |
if (query->state != DRIZZLE_QUERY_STATE_INIT) |
|
281 |
continue; |
|
282 |
||
283 |
drizzle->query_new--; |
|
284 |
drizzle->query_running++; |
|
285 |
assert(query->con->query == NULL); |
|
286 |
query->con->query= query; |
|
287 |
||
288 |
drizzle_query_run_state(query, ret_ptr); |
|
289 |
if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT) |
|
290 |
{
|
|
291 |
assert(query->state == DRIZZLE_QUERY_STATE_DONE); |
|
292 |
drizzle->query_running--; |
|
293 |
drizzle->options= options; |
|
294 |
query->con->query= NULL; |
|
295 |
if (*ret_ptr == DRIZZLE_RETURN_ERROR_CODE || *ret_ptr == DRIZZLE_RETURN_OK) |
|
296 |
{
|
|
297 |
return query; |
|
298 |
}
|
|
299 |
return NULL; |
|
300 |
}
|
|
301 |
}
|
|
302 |
assert(drizzle->query_new == 0); |
|
303 |
}
|
|
304 |
||
305 |
while (1) |
|
306 |
{
|
|
307 |
/* Loop through each active connection. */
|
|
308 |
while ((con= drizzle_con_ready(drizzle)) != NULL) |
|
309 |
{
|
|
310 |
query= con->query; |
|
311 |
drizzle_query_run_state(query, ret_ptr); |
|
312 |
if (query->state == DRIZZLE_QUERY_STATE_DONE) |
|
313 |
{
|
|
314 |
drizzle->query_running--; |
|
315 |
drizzle->options= options; |
|
316 |
con->query= NULL; |
|
317 |
return query; |
|
318 |
}
|
|
319 |
assert(*ret_ptr == DRIZZLE_RETURN_IO_WAIT); |
|
320 |
}
|
|
321 |
||
322 |
if (options & DRIZZLE_NON_BLOCKING) |
|
323 |
{
|
|
324 |
*ret_ptr= DRIZZLE_RETURN_IO_WAIT; |
|
325 |
return NULL; |
|
326 |
}
|
|
327 |
||
328 |
*ret_ptr= drizzle_con_wait(drizzle); |
|
329 |
if (*ret_ptr != DRIZZLE_RETURN_OK) |
|
330 |
{
|
|
331 |
drizzle->options= options; |
|
332 |
return NULL; |
|
333 |
}
|
|
334 |
}
|
|
335 |
}
|
|
336 |
||
337 |
drizzle_return_t drizzle_query_run_all(drizzle_st *drizzle) |
|
338 |
{
|
|
339 |
drizzle_return_t ret; |
|
340 |
||
341 |
while (drizzle->query_new > 0 || drizzle->query_running > 0) |
|
342 |
{
|
|
343 |
(void)drizzle_query_run(drizzle, &ret); |
|
344 |
if (ret != DRIZZLE_RETURN_OK && ret != DRIZZLE_RETURN_ERROR_CODE) |
|
345 |
return ret; |
|
346 |
}
|
|
347 |
||
348 |
return DRIZZLE_RETURN_OK; |
|
349 |
}
|
|
350 |
||
2235.1.2
by Andrew Hutchings
Add drizzle_safe_escape_string() function |
351 |
ssize_t drizzle_safe_escape_string(char *to, size_t max_to_size, const char *from, size_t from_size) |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
352 |
{
|
2235.1.2
by Andrew Hutchings
Add drizzle_safe_escape_string() function |
353 |
ssize_t to_size= 0; |
1857.5.1
by Andrew Hutchings
Add unit tests for libdrizzle utility functions |
354 |
char newchar; |
2210.2.1
by Andrew Hutchings
Fix libdrizzle byte count handling for drizzle_escape_string and add unittest |
355 |
const char *end; |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
356 |
|
2210.2.1
by Andrew Hutchings
Fix libdrizzle byte count handling for drizzle_escape_string and add unittest |
357 |
for (end= from + from_size; from < end; from++) |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
358 |
{
|
1857.5.1
by Andrew Hutchings
Add unit tests for libdrizzle utility functions |
359 |
newchar= 0; |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
360 |
/* All multi-byte UTF8 characters have the high bit set for all bytes. */
|
361 |
if (!(*from & 0x80)) |
|
362 |
{
|
|
363 |
switch (*from) |
|
364 |
{
|
|
365 |
case 0: |
|
1857.5.1
by Andrew Hutchings
Add unit tests for libdrizzle utility functions |
366 |
newchar= '0'; |
367 |
break; |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
368 |
case '\n': |
1857.5.1
by Andrew Hutchings
Add unit tests for libdrizzle utility functions |
369 |
newchar= 'n'; |
370 |
break; |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
371 |
case '\r': |
1857.5.1
by Andrew Hutchings
Add unit tests for libdrizzle utility functions |
372 |
newchar= 'r'; |
373 |
break; |
|
374 |
case '\032': |
|
375 |
newchar= 'Z'; |
|
376 |
break; |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
377 |
case '\\': |
2210.2.1
by Andrew Hutchings
Fix libdrizzle byte count handling for drizzle_escape_string and add unittest |
378 |
newchar= '\\'; |
379 |
break; |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
380 |
case '\'': |
2210.2.1
by Andrew Hutchings
Fix libdrizzle byte count handling for drizzle_escape_string and add unittest |
381 |
newchar= '\''; |
382 |
break; |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
383 |
case '"': |
2210.2.1
by Andrew Hutchings
Fix libdrizzle byte count handling for drizzle_escape_string and add unittest |
384 |
newchar= '"'; |
385 |
break; |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
386 |
default: |
387 |
break; |
|
388 |
}
|
|
389 |
}
|
|
2210.2.1
by Andrew Hutchings
Fix libdrizzle byte count handling for drizzle_escape_string and add unittest |
390 |
if (newchar != '\0') |
1857.5.1
by Andrew Hutchings
Add unit tests for libdrizzle utility functions |
391 |
{
|
2235.1.2
by Andrew Hutchings
Add drizzle_safe_escape_string() function |
392 |
if ((size_t)to_size + 2 > max_to_size) |
393 |
return -1; |
|
394 |
||
1857.5.1
by Andrew Hutchings
Add unit tests for libdrizzle utility functions |
395 |
*to++= '\\'; |
396 |
*to++= newchar; |
|
397 |
to_size++; |
|
398 |
}
|
|
2210.2.1
by Andrew Hutchings
Fix libdrizzle byte count handling for drizzle_escape_string and add unittest |
399 |
else
|
2235.1.2
by Andrew Hutchings
Add drizzle_safe_escape_string() function |
400 |
{
|
401 |
if ((size_t)to_size + 1 > max_to_size) |
|
402 |
return -1; |
|
403 |
||
2210.2.1
by Andrew Hutchings
Fix libdrizzle byte count handling for drizzle_escape_string and add unittest |
404 |
*to++= *from; |
405 |
}
|
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
406 |
to_size++; |
407 |
}
|
|
408 |
||
409 |
*to= 0; |
|
410 |
||
411 |
return to_size; |
|
412 |
}
|
|
413 |
||
2235.1.2
by Andrew Hutchings
Add drizzle_safe_escape_string() function |
414 |
size_t drizzle_escape_string(char *to, const char *from, size_t from_size) |
415 |
{
|
|
416 |
return (size_t) drizzle_safe_escape_string(to, (from_size * 2), from, from_size); |
|
417 |
}
|
|
418 |
||
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
419 |
size_t drizzle_hex_string(char *to, const char *from, size_t from_size) |
420 |
{
|
|
421 |
static const char hex_map[]= "0123456789ABCDEF"; |
|
422 |
const char *from_end; |
|
423 |
||
424 |
for (from_end= from + from_size; from != from_end; from++) |
|
425 |
{
|
|
426 |
*to++= hex_map[((unsigned char) *from) >> 4]; |
|
427 |
*to++= hex_map[((unsigned char) *from) & 0xF]; |
|
428 |
}
|
|
429 |
||
430 |
*to= 0; |
|
431 |
||
432 |
return from_size * 2; |
|
433 |
}
|
|
434 |
||
435 |
void drizzle_mysql_password_hash(char *to, const char *from, size_t from_size) |
|
436 |
{
|
|
437 |
SHA1_CTX ctx; |
|
438 |
uint8_t hash_tmp1[SHA1_DIGEST_LENGTH]; |
|
439 |
uint8_t hash_tmp2[SHA1_DIGEST_LENGTH]; |
|
440 |
||
441 |
SHA1Init(&ctx); |
|
442 |
SHA1Update(&ctx, (const uint8_t*)from, from_size); |
|
443 |
SHA1Final(hash_tmp1, &ctx); |
|
444 |
||
445 |
SHA1Init(&ctx); |
|
446 |
SHA1Update(&ctx, hash_tmp1, SHA1_DIGEST_LENGTH); |
|
447 |
SHA1Final(hash_tmp2, &ctx); |
|
448 |
||
449 |
(void)drizzle_hex_string(to, (char*)hash_tmp2, SHA1_DIGEST_LENGTH); |
|
450 |
}
|