Reformat whole codebase with astyle.options (#128)
[openjpeg.git] / src / lib / openjpip / query_parser.c
1 /*
2  * $Id$
3  *
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
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31
32
33 #ifdef _WIN32
34 #include <windows.h>
35 #define strcasecmp  _stricmp
36 #define strncasecmp _strnicmp
37 #else
38 #include <strings.h>
39 #endif
40
41 #include <stdio.h>
42 #include <assert.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include "query_parser.h"
46 #include "opj_stdint.h"
47
48 #ifdef SERVER
49 #include "fcgi_stdio.h"
50 #define logstream FCGI_stdout
51 #else
52 #define FCGI_stdout stdout
53 #define FCGI_stderr stderr
54 #define logstream stderr
55 #endif /*SERVER*/
56
57
58 /**
59  * Get initialized query parameters
60  *
61  * @return initial query parameters
62  */
63 query_param_t * get_initquery(void);
64
65 /*
66  * get a pair of field name and value from the string starting fieldname=fieldval&... format
67  *
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
72  */
73 char * get_fieldparam(const char *stringptr, char *fieldname, char *fieldval);
74
75 void parse_cclose(char *src, query_param_t *query_param);
76 void parse_metareq(char *field, query_param_t *query_param);
77
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);
80
81
82 /** maximum length of field name*/
83 #define MAX_LENOFFIELDNAME 10
84
85 /** maximum length of field value*/
86 #define MAX_LENOFFIELDVAL 128
87
88 query_param_t * parse_query(const char *query_string)
89 {
90     query_param_t *query_param;
91     const char *pquery;
92     char fieldname[MAX_LENOFFIELDNAME], fieldval[MAX_LENOFFIELDVAL];
93
94     query_param = get_initquery();
95
96     pquery = query_string;
97
98     while (pquery != NULL) {
99
100         pquery = get_fieldparam(pquery, fieldname, fieldval);
101
102         if (fieldname[0] != '\0') {
103             if (strcasecmp(fieldname, "target") == 0) {
104                 query_param->target = strdup(fieldval);
105             }
106
107             else if (strcasecmp(fieldname, "tid") == 0) {
108                 query_param->tid = strdup(fieldval);
109             }
110
111             else if (strcasecmp(fieldname, "fsiz") == 0) {
112                 sscanf(fieldval, "%d,%d", &query_param->fx, &query_param->fy);
113             }
114
115             else if (strcasecmp(fieldname, "roff") == 0) {
116                 sscanf(fieldval, "%d,%d", &query_param->rx, &query_param->ry);
117             }
118
119             else if (strcasecmp(fieldname, "rsiz") == 0) {
120                 sscanf(fieldval, "%d,%d", &query_param->rw, &query_param->rh);
121             }
122
123             else if (strcasecmp(fieldname, "layers") == 0) {
124                 sscanf(fieldval, "%d", &query_param->layers);
125             }
126
127             else if (strcasecmp(fieldname, "cid") == 0) {
128                 query_param->cid = strdup(fieldval);
129             }
130
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;
136                 }
137             }
138
139             else if (strcasecmp(fieldname, "cclose") == 0) {
140                 parse_cclose(fieldval, query_param);
141             }
142
143             else if (strcasecmp(fieldname, "metareq") == 0) {
144                 parse_metareq(fieldval, query_param);
145             }
146
147             else if (strcasecmp(fieldname, "comps") == 0) {
148                 parse_comps(fieldval, query_param);
149             }
150
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;
156                 }
157             }
158
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);
163                 }
164             }
165         }
166     }
167     return query_param;
168 }
169
170 query_param_t * get_initquery(void)
171 {
172     query_param_t *query;
173     int i;
174
175     query = (query_param_t *)opj_malloc(sizeof(query_param_t));
176
177     query->target = NULL;
178     query->tid = NULL;
179     query->fx = -1;
180     query->fy = -1;
181     query->rx = -1;
182     query->ry = -1;
183     query->rw = -1;
184     query->rh = -1;
185     query->layers = -1;
186     query->lastcomp = -1;
187     query->comps = NULL;
188     query->cid = NULL;
189     query->cnew = non;
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;
200     }
201     query->root_bin = 0;
202     query->max_depth = -1;
203     query->metadata_only = OPJ_FALSE;
204     query->return_type = UNKNOWN;
205     query->len = -1;
206
207     return query;
208 }
209
210
211 char * get_fieldparam(const char *stringptr, char *fieldname, char *fieldval)
212 {
213     char *eqp, *andp, *nexfieldptr;
214
215     if ((eqp = strchr(stringptr, '=')) == NULL) {
216         fprintf(stderr, "= not found\n");
217         strcpy(fieldname, "");
218         strcpy(fieldval, "");
219         return NULL;
220     }
221     if ((andp = strchr(stringptr, '&')) == NULL) {
222         andp = strchr(stringptr, '\0');
223         nexfieldptr = NULL;
224     } else {
225         nexfieldptr = andp + 1;
226     }
227
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';
234
235     return nexfieldptr;
236 }
237
238 void print_queryparam(query_param_t query_param)
239 {
240     int i;
241     char *cclose;
242
243     fprintf(logstream, "query parameters:\n");
244
245     if (query_param.target) {
246         fprintf(logstream, "\t target: %s\n", query_param.target);
247     }
248
249     if (query_param.tid) {
250         fprintf(logstream, "\t tid: %s\n", query_param.tid);
251     }
252
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: ");
258
259     if (query_param.lastcomp == -1) {
260         fprintf(logstream, "ALL\n");
261     } else {
262         for (i = 0; i <= query_param.lastcomp; i++)
263             if (query_param.comps[i]) {
264                 fprintf(logstream, "%d ", i);
265             }
266         fprintf(logstream, "\n");
267     }
268     fprintf(logstream, "\t cnew: %d\n", query_param.cnew);
269
270     if (query_param.cid) {
271         fprintf(logstream, "\t cid: %s\n", query_param.cid);
272     }
273
274     if (query_param.cclose) {
275         fprintf(logstream, "\t cclose: ");
276
277         for (i = 0, cclose = query_param.cclose; i < query_param.numOfcclose; i++) {
278             fprintf(logstream, "%s ", cclose);
279             cclose += (strlen(cclose) + 1);
280         }
281         fprintf(logstream, "\n");
282     }
283
284     fprintf(logstream, "\t req-box-prop\n");
285     for (i = 0; i < MAX_NUMOFBOX && query_param.box_type[i][0] != 0; i++) {
286         fprintf(logstream,
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]);
290     }
291
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);
295     fprintf(logstream,
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);
299 }
300
301 void parse_cclose(char *src, query_param_t *query_param)
302 {
303     size_t i;
304     size_t len;
305
306     len = strlen(src);
307     query_param->cclose = strdup(src);
308
309     for (i = 0; i < len; i++)
310         if (query_param->cclose[i] == ',') {
311             query_param->cclose[i] = '\0';
312             query_param->numOfcclose ++;
313         }
314
315     query_param->numOfcclose ++;
316 }
317
318 void parse_req_box_prop(char *req_box_prop, int idx,
319                         query_param_t *query_param);
320
321 void parse_metareq(char *field, query_param_t *query_param)
322 {
323     char req_box_prop[20];
324     char *ptr, *src;
325     int numofboxreq = 0;
326
327     memset(req_box_prop, 0, 20);
328
329     /* req-box-prop*/
330     ptr = strchr(field, '[');
331     ptr++;
332     src = ptr;
333     while (*ptr != ']') {
334         if (*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);
338             ptr++;
339             src = ptr;
340             memset(req_box_prop, 0, 20);
341         }
342         ptr++;
343     }
344     assert(ptr - src >= 0);
345     strncpy(req_box_prop, src, (size_t)(ptr - src));
346
347     parse_req_box_prop(req_box_prop, numofboxreq++, query_param);
348
349     if ((ptr = strchr(field, 'R'))) {
350         sscanf(ptr + 1, "%d", &(query_param->root_bin));
351     }
352
353     if ((ptr = strchr(field, 'D'))) {
354         sscanf(ptr + 1, "%d", &(query_param->max_depth));
355     }
356
357     if ((ptr = strstr(field, "!!"))) {
358         query_param->metadata_only = OPJ_TRUE;
359     }
360 }
361
362 void parse_req_box_prop(char *req_box_prop, int idx, query_param_t *query_param)
363 {
364     char *ptr;
365
366     if (*req_box_prop == '*') {
367         query_param->box_type[idx][0] = '*';
368     } else {
369         strncpy(query_param->box_type[idx], req_box_prop, 4);
370     }
371
372     if ((ptr = strchr(req_box_prop, ':'))) {
373         if (*(ptr + 1) == 'r') {
374             query_param->limit[idx] = -1;
375         } else {
376             sscanf(ptr + 1, "%d", &(query_param->limit[idx]));
377         }
378     }
379
380     if ((ptr = strchr(req_box_prop, '/'))) {
381         ptr++;
382         while (*ptr == 'w' || *ptr == 's' || *ptr == 'g' || *ptr == 'a') {
383             switch (*ptr) {
384             case 'w':
385                 query_param->w[idx] = OPJ_TRUE;
386                 break;
387             case 's':
388                 query_param->s[idx] = OPJ_TRUE;
389                 break;
390             case 'g':
391                 query_param->g[idx] = OPJ_TRUE;
392                 break;
393             case 'a':
394                 query_param->a[idx] = OPJ_TRUE;
395                 break;
396             }
397             ptr++;
398         }
399     } else {
400         query_param->g[idx] = OPJ_TRUE;
401         query_param->s[idx] = OPJ_TRUE;
402         query_param->w[idx] = OPJ_TRUE;
403     }
404
405     if ((ptr = strchr(req_box_prop, '!'))) {
406         query_param->priority[idx] = OPJ_TRUE;
407     }
408
409     idx++;
410 }
411
412 void parse_comps(char *field, query_param_t *query_param)
413 {
414     int i, start, stop, aux = -1;
415     char *ptr1, *ptr2;
416
417     ptr1 = strchr(field, '-');
418     ptr2 = strchr(field, ',');
419
420     if (ptr1 && ptr2)
421         if (ptr1 > ptr2) {
422             sscanf(field, "%d,%d-%d", &aux, &start, &stop);
423         } else {
424             sscanf(field, "%d-%d,%d", &start, &stop, &aux);
425         } else if (ptr1) {
426         sscanf(field, "%d-%d", &start, &stop);
427     } else if (ptr2) {
428         sscanf(field, "%d,%d", &start, &stop);
429         aux = start;
430         start = stop;
431     } else {
432         sscanf(field, "%d", &stop);
433         start = stop;
434     }
435
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));
439
440     for (i = start; i <= stop; i++) {
441         query_param->comps[i] = OPJ_TRUE;
442     }
443
444     if (aux != -1) {
445         query_param->comps[aux] = OPJ_TRUE;
446     }
447 }
448
449 void delete_query(query_param_t **query)
450 {
451     if ((*query)->target) {
452         opj_free((*query)->target);
453     }
454
455     if ((*query)->tid) {
456         opj_free((*query)->tid);
457     }
458
459     if ((*query)->comps) {
460         opj_free((*query)->comps);
461     }
462
463     if ((*query)->cid) {
464         opj_free((*query)->cid);
465     }
466
467     if ((*query)->cclose) {
468         opj_free((*query)->cclose);
469     }
470
471     opj_free(*query);
472 }