libidav/davqlexec.c

Fri, 29 May 2015 13:12:25 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 29 May 2015 13:12:25 +0200
changeset 122
9a016d5fa9e7
parent 104
6fb4d24d9df9
child 123
806c4dccf2ae
permissions
-rw-r--r--

secured malloc / calloc calls

104
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2015 Olaf Wintermann. All rights reserved.
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <ucx/utils.h>
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "davqlexec.h"
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 DavQLResult* dav_statement_exec(DavSession *sn, DavQLStatement *st, ...) {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 va_list ap;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 va_start(ap, st);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 DavQLResult *result = dav_statement_execv(sn, st, ap);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 va_end(ap);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 return result;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 DavQLResult* dav_statement_execv(DavSession *sn, DavQLStatement *st, va_list ap) {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 DavQLResult *result = dav_session_malloc(sn, sizeof(DavQLResult));
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 result->result = NULL;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 result->status = 1;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 // make sure the statement was successfully parsed
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 if(st->type == DAVQL_ERROR) {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 return result;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 // get path string
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 davqlerror_t error;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 UcxBuffer *path = dav_path_string(st->path, ap, &error);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 if(st->type == DAVQL_GET) {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 dav_exec_get(sn, st, path->space, ap);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 } else {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 // TODO
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 return result;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 UcxBuffer* dav_path_string(sstr_t pathfmt, va_list ap, davqlerror_t *error) {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 UcxBuffer *path = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 int placeholder = 0;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 for(int i=0;i<pathfmt.length;i++) {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 char c = pathfmt.ptr[i];
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 if(placeholder) {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 if(c == '%') {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 // no placeholder, %% transposes to %
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 ucx_buffer_putc(path, c);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 } else {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 // detect placeholder type and insert arg
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 int err = 0;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 switch(c) {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 case 's': {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 char *arg = va_arg(ap, char*);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 ucx_buffer_puts(path, arg);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 break;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 case 'd': {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 int arg = va_arg(ap, int);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 ucx_bprintf(path, "%d", arg);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 break;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 case 'u': {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 unsigned int arg = va_arg(ap, unsigned int);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 ucx_bprintf(path, "%u", arg);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 break;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 case 't': {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 // time arguments doesn't make any sense in a path
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 *error = DAVQL_UNSUPPORTED_FORMATCHAR;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 err = 1;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 break;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 default: {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 *error = DAVQL_UNKNOWN_FORMATCHAR;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 err = 1;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 if(err) {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 ucx_buffer_free(path);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 return NULL;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 placeholder = 0;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 } else {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 if(c == '%') {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 placeholder = 1;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 } else {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 ucx_buffer_putc(path, c);
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 ucx_buffer_putc(path, '\0');
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 *error = DAVQL_OK;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 return path;
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 void dav_exec_get(DavSession *sn, DavQLStatement *st, char* path, va_list ap) {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 // execute a davql get statement
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 // TODO: get property list
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 }
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 int dav_eval_lexpr(DavResource *res, DavQLExpression *lexpr) {
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136
6fb4d24d9df9 added davqlexec source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 }

mercurial