4 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2014, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
7 * Copyright (c) 2011, Lucian Corlaciu, GSoC
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 #define strcasecmp _stricmp
36 #define strncasecmp _strnicmp
45 #include "query_parser.h"
46 #include "opj_stdint.h"
49 #include "fcgi_stdio.h"
50 #define logstream FCGI_stdout
52 #define FCGI_stdout stdout
53 #define FCGI_stderr stderr
54 #define logstream stderr
59 * Get initialized query parameters
61 * @return initial query parameters
63 query_param_t * get_initquery(void);
66 * get a pair of field name and value from the string starting fieldname=fieldval&... format
68 * @param[in] stringptr pointer to the beginning of the parsing string
69 * @param[out] fieldname string to copy the field name, if not found, NULL
70 * @param[out] fieldval string to copy the field value, if not found, NULL
71 * @return pointer to the next field string, if there is none, NULL
73 char * get_fieldparam(const char *stringptr, char *fieldname, char *fieldval);
75 void parse_cclose(char *src, query_param_t *query_param);
76 void parse_metareq(char *field, query_param_t *query_param);
78 /* parse the requested components (parses forms like:a; a,b; a-b; a-b,c; a,b-c)*/
79 void parse_comps(char *field, query_param_t *query_param);
82 /** maximum length of field name*/
83 #define MAX_LENOFFIELDNAME 10
85 /** maximum length of field value*/
86 #define MAX_LENOFFIELDVAL 128
88 query_param_t * parse_query(const char *query_string)
90 query_param_t *query_param;
92 char fieldname[MAX_LENOFFIELDNAME], fieldval[MAX_LENOFFIELDVAL];
94 query_param = get_initquery();
96 pquery = query_string;
98 while (pquery != NULL) {
100 pquery = get_fieldparam(pquery, fieldname, fieldval);
102 if (fieldname[0] != '\0') {
103 if (strcasecmp(fieldname, "target") == 0) {
104 query_param->target = strdup(fieldval);
107 else if (strcasecmp(fieldname, "tid") == 0) {
108 query_param->tid = strdup(fieldval);
111 else if (strcasecmp(fieldname, "fsiz") == 0) {
112 sscanf(fieldval, "%d,%d", &query_param->fx, &query_param->fy);
115 else if (strcasecmp(fieldname, "roff") == 0) {
116 sscanf(fieldval, "%d,%d", &query_param->rx, &query_param->ry);
119 else if (strcasecmp(fieldname, "rsiz") == 0) {
120 sscanf(fieldval, "%d,%d", &query_param->rw, &query_param->rh);
123 else if (strcasecmp(fieldname, "layers") == 0) {
124 sscanf(fieldval, "%d", &query_param->layers);
127 else if (strcasecmp(fieldname, "cid") == 0) {
128 query_param->cid = strdup(fieldval);
131 else if (strcasecmp(fieldname, "cnew") == 0) {
132 if (strncasecmp(fieldval, "http-tcp", 8) == 0) {
133 query_param->cnew = tcp;
134 } else if (strncasecmp(fieldval, "http", 4) == 0) {
135 query_param->cnew = http;
139 else if (strcasecmp(fieldname, "cclose") == 0) {
140 parse_cclose(fieldval, query_param);
143 else if (strcasecmp(fieldname, "metareq") == 0) {
144 parse_metareq(fieldval, query_param);
147 else if (strcasecmp(fieldname, "comps") == 0) {
148 parse_comps(fieldval, query_param);
151 else if (strcasecmp(fieldname, "type") == 0) {
152 if (strncasecmp(fieldval, "jpp-stream", 10) == 0) {
153 query_param->return_type = JPPstream;
154 } else if (strncasecmp(fieldval, "jpt-stream", 10) == 0) {
155 query_param->return_type = JPTstream;
159 else if (strcasecmp(fieldname, "len") == 0) {
160 sscanf(fieldval, "%d", &query_param->len);
161 if (query_param->len == 2000) { /* for kakadu client*/
162 strncpy(query_param->box_type[0], "ftyp", 4);
170 query_param_t * get_initquery(void)
172 query_param_t *query;
175 query = (query_param_t *)opj_malloc(sizeof(query_param_t));
177 query->target = NULL;
186 query->lastcomp = -1;
190 query->cclose = NULL;
191 query->numOfcclose = 0;
192 memset(query->box_type, 0, MAX_NUMOFBOX * 4);
193 memset(query->limit, 0, MAX_NUMOFBOX * sizeof(int));
194 for (i = 0; i < MAX_NUMOFBOX; i++) {
195 query->w[i] = OPJ_FALSE;
196 query->s[i] = OPJ_FALSE;
197 query->g[i] = OPJ_FALSE;
198 query->a[i] = OPJ_FALSE;
199 query->priority[i] = OPJ_FALSE;
202 query->max_depth = -1;
203 query->metadata_only = OPJ_FALSE;
204 query->return_type = UNKNOWN;
211 char * get_fieldparam(const char *stringptr, char *fieldname, char *fieldval)
213 char *eqp, *andp, *nexfieldptr;
215 if ((eqp = strchr(stringptr, '=')) == NULL) {
216 fprintf(stderr, "= not found\n");
217 strcpy(fieldname, "");
218 strcpy(fieldval, "");
221 if ((andp = strchr(stringptr, '&')) == NULL) {
222 andp = strchr(stringptr, '\0');
225 nexfieldptr = andp + 1;
228 assert((size_t)(eqp - stringptr));
229 strncpy(fieldname, stringptr, (size_t)(eqp - stringptr));
230 fieldname[eqp - stringptr] = '\0';
231 assert(andp - eqp - 1 >= 0);
232 strncpy(fieldval, eqp + 1, (size_t)(andp - eqp - 1));
233 fieldval[andp - eqp - 1] = '\0';
238 void print_queryparam(query_param_t query_param)
243 fprintf(logstream, "query parameters:\n");
245 if (query_param.target) {
246 fprintf(logstream, "\t target: %s\n", query_param.target);
249 if (query_param.tid) {
250 fprintf(logstream, "\t tid: %s\n", query_param.tid);
253 fprintf(logstream, "\t fx,fy: %d, %d\n", query_param.fx, query_param.fy);
254 fprintf(logstream, "\t rx,ry: %d, %d \t rw,rh: %d, %d\n", query_param.rx,
255 query_param.ry, query_param.rw, query_param.rh);
256 fprintf(logstream, "\t layers: %d\n", query_param.layers);
257 fprintf(logstream, "\t components: ");
259 if (query_param.lastcomp == -1) {
260 fprintf(logstream, "ALL\n");
262 for (i = 0; i <= query_param.lastcomp; i++)
263 if (query_param.comps[i]) {
264 fprintf(logstream, "%d ", i);
266 fprintf(logstream, "\n");
268 fprintf(logstream, "\t cnew: %d\n", query_param.cnew);
270 if (query_param.cid) {
271 fprintf(logstream, "\t cid: %s\n", query_param.cid);
274 if (query_param.cclose) {
275 fprintf(logstream, "\t cclose: ");
277 for (i = 0, cclose = query_param.cclose; i < query_param.numOfcclose; i++) {
278 fprintf(logstream, "%s ", cclose);
279 cclose += (strlen(cclose) + 1);
281 fprintf(logstream, "\n");
284 fprintf(logstream, "\t req-box-prop\n");
285 for (i = 0; i < MAX_NUMOFBOX && query_param.box_type[i][0] != 0; i++) {
287 "\t\t box_type: %.4s limit: %d w:%d s:%d g:%d a:%d priority:%d\n",
288 query_param.box_type[i], query_param.limit[i], query_param.w[i],
289 query_param.s[i], query_param.g[i], query_param.a[i], query_param.priority[i]);
292 fprintf(logstream, "\t root-bin: %d\n", query_param.root_bin);
293 fprintf(logstream, "\t max-depth: %d\n", query_param.max_depth);
294 fprintf(logstream, "\t metadata-only: %d\n", query_param.metadata_only);
296 "\t image return type: %d, [JPP-stream=0, JPT-stream=1, UNKNOWN=-1]\n",
297 query_param.return_type);
298 fprintf(logstream, "\t len: %d\n", query_param.len);
301 void parse_cclose(char *src, query_param_t *query_param)
307 query_param->cclose = strdup(src);
309 for (i = 0; i < len; i++)
310 if (query_param->cclose[i] == ',') {
311 query_param->cclose[i] = '\0';
312 query_param->numOfcclose ++;
315 query_param->numOfcclose ++;
318 void parse_req_box_prop(char *req_box_prop, int idx,
319 query_param_t *query_param);
321 void parse_metareq(char *field, query_param_t *query_param)
323 char req_box_prop[20];
327 memset(req_box_prop, 0, 20);
330 ptr = strchr(field, '[');
333 while (*ptr != ']') {
335 assert(ptr - src >= 0);
336 strncpy(req_box_prop, src, (size_t)(ptr - src));
337 parse_req_box_prop(req_box_prop, numofboxreq++, query_param);
340 memset(req_box_prop, 0, 20);
344 assert(ptr - src >= 0);
345 strncpy(req_box_prop, src, (size_t)(ptr - src));
347 parse_req_box_prop(req_box_prop, numofboxreq++, query_param);
349 if ((ptr = strchr(field, 'R'))) {
350 sscanf(ptr + 1, "%d", &(query_param->root_bin));
353 if ((ptr = strchr(field, 'D'))) {
354 sscanf(ptr + 1, "%d", &(query_param->max_depth));
357 if ((ptr = strstr(field, "!!"))) {
358 query_param->metadata_only = OPJ_TRUE;
362 void parse_req_box_prop(char *req_box_prop, int idx, query_param_t *query_param)
366 if (*req_box_prop == '*') {
367 query_param->box_type[idx][0] = '*';
369 strncpy(query_param->box_type[idx], req_box_prop, 4);
372 if ((ptr = strchr(req_box_prop, ':'))) {
373 if (*(ptr + 1) == 'r') {
374 query_param->limit[idx] = -1;
376 sscanf(ptr + 1, "%d", &(query_param->limit[idx]));
380 if ((ptr = strchr(req_box_prop, '/'))) {
382 while (*ptr == 'w' || *ptr == 's' || *ptr == 'g' || *ptr == 'a') {
385 query_param->w[idx] = OPJ_TRUE;
388 query_param->s[idx] = OPJ_TRUE;
391 query_param->g[idx] = OPJ_TRUE;
394 query_param->a[idx] = OPJ_TRUE;
400 query_param->g[idx] = OPJ_TRUE;
401 query_param->s[idx] = OPJ_TRUE;
402 query_param->w[idx] = OPJ_TRUE;
405 if ((ptr = strchr(req_box_prop, '!'))) {
406 query_param->priority[idx] = OPJ_TRUE;
412 void parse_comps(char *field, query_param_t *query_param)
414 int i, start, stop, aux = -1;
417 ptr1 = strchr(field, '-');
418 ptr2 = strchr(field, ',');
422 sscanf(field, "%d,%d-%d", &aux, &start, &stop);
424 sscanf(field, "%d-%d,%d", &start, &stop, &aux);
426 sscanf(field, "%d-%d", &start, &stop);
428 sscanf(field, "%d,%d", &start, &stop);
432 sscanf(field, "%d", &stop);
436 query_param->lastcomp = stop > aux ? stop : aux;
437 query_param->comps = (OPJ_BOOL *)opj_calloc(1,
438 (OPJ_SIZE_T)(query_param->lastcomp + 1) * sizeof(OPJ_BOOL));
440 for (i = start; i <= stop; i++) {
441 query_param->comps[i] = OPJ_TRUE;
445 query_param->comps[aux] = OPJ_TRUE;
449 void delete_query(query_param_t **query)
451 if ((*query)->target) {
452 opj_free((*query)->target);
456 opj_free((*query)->tid);
459 if ((*query)->comps) {
460 opj_free((*query)->comps);
464 opj_free((*query)->cid);
467 if ((*query)->cclose) {
468 opj_free((*query)->cclose);