add documentations
[openjpeg.git] / applications / jpip / opj_server / query_parser.c
1 /*
2  * $Id: query_parser.c 53 2011-05-09 16:55:39Z kaori $
3  *
4  * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2011, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31
32 #ifdef _WIN32
33 #include <windows.h>
34 #define strcasecmp  _stricmp
35 #else
36 #include <strings.h>
37 #endif
38
39 #include <stdio.h>
40 #include <string.h>
41 #include "query_parser.h"
42
43 #ifdef SERVER
44 #include "fcgi_stdio.h"
45 #define logstream FCGI_stdout
46 #else
47 #define FCGI_stdout stdout
48 #define FCGI_stderr stderr
49 #define logstream stderr
50 #endif //SERVER
51
52
53 /**
54  * initialize query parameters
55  *
56  * @param[in,out] query_param  query parameters
57  */
58 void init_queryparam( query_param_t *query_param);
59
60 /*
61  * get a pair of field name and value from the string starting fieldname=fieldval&... format
62  *
63  * @param[in] stringptr pointer to the beginning of the parsing string
64  * @param[out] fieldname string to copy the field name, if not found, NULL
65  * @param[out] fieldval string to copy the field value, if not found, NULL
66  * @return pointer to the next field string, if there is none, NULL
67  */
68 char * get_fieldparam( char *stringptr, char *fieldname, char *fieldval);
69
70 /**
71  * parse string to string array
72  *
73  * @param[in]  src src string
74  * @param[out] cclose parsed string array
75  */
76 void str2cclose( char *src, char cclose[][MAX_LENOFCID]);
77
78 void parse_metareq( char *field, query_param_t *query_param);
79
80 //! maximum length of field name
81 #define MAX_LENOFFIELDNAME 10
82
83 //! maximum length of field value
84 #define MAX_LENOFFIELDVAL 128
85
86 void parse_query( char *query_string, query_param_t *query_param)
87 {
88   char *pquery, fieldname[MAX_LENOFFIELDNAME], fieldval[MAX_LENOFFIELDVAL];
89
90   init_queryparam( query_param);
91   
92   pquery = query_string;
93
94   while( pquery!=NULL) {
95     
96     pquery = get_fieldparam( pquery, fieldname, fieldval);
97
98     if( fieldname[0] != '\0'){
99       if( strcasecmp( fieldname, "target") == 0)
100         strcpy( query_param->target,fieldval);
101       
102       else if( strcasecmp( fieldname, "fsiz") == 0)
103         sscanf( fieldval, "%d,%d", &query_param->fx, &query_param->fy);
104       
105       else if( strcasecmp( fieldname, "roff") == 0)
106         sscanf( fieldval, "%d,%d", &query_param->rx, &query_param->ry);
107
108       else if( strcasecmp( fieldname, "rsiz") == 0)
109         sscanf( fieldval, "%d,%d", &query_param->rw, &query_param->rh);
110       
111       else if( strcasecmp( fieldname, "cid") == 0)
112         strcpy( query_param->cid, fieldval);
113
114       else if( strcasecmp( fieldname, "cnew") == 0)
115         query_param->cnew = true;
116       
117       else if( strcasecmp( fieldname, "cclose") == 0)
118         str2cclose( fieldval, query_param->cclose);
119       
120       else if( strcasecmp( fieldname, "metareq") == 0)
121         parse_metareq( fieldval, query_param);
122     }
123   }
124 }
125
126 void init_queryparam( query_param_t *query_param)
127 {
128   int i;
129
130   query_param->target[0]='\0';
131   query_param->fx=-1;
132   query_param->fy=-1;
133   query_param->rx=-1;
134   query_param->ry=-1;
135   query_param->rw=-1;
136   query_param->rh=-1;
137   query_param->cid[0]='\0';
138   query_param->cnew=false;
139   memset( query_param->cclose, 0, MAX_NUMOFCCLOSE*MAX_LENOFCID);
140   memset( query_param->box_type, 0, MAX_NUMOFBOX*4);
141   memset( query_param->limit, 0, MAX_NUMOFBOX*sizeof(int));
142   for( i=0; i<MAX_NUMOFBOX; i++){
143     query_param->w[i] = false;
144     query_param->s[i] = false;
145     query_param->g[i] = false;
146     query_param->a[i] = false;
147     query_param->priority[i] = false;
148   }
149   query_param->root_bin = 0;
150   query_param->max_depth = -1;
151   query_param->metadata_only = false;
152 }
153
154
155 char * get_fieldparam( char *stringptr, char *fieldname, char *fieldval)
156 {
157   char *eqp, *andp, *nexfieldptr;
158
159   if((eqp = strchr( stringptr, '='))==NULL){
160     fprintf( stderr, "= not found\n");
161     strcpy( fieldname, "");
162     strcpy( fieldval, "");
163     return NULL;
164   }
165   if((andp = strchr( stringptr, '&'))==NULL){
166     andp = strchr( stringptr, '\0');
167     nexfieldptr = NULL;
168   }
169   else
170     nexfieldptr = andp+1;
171
172   strncpy( fieldname, stringptr, eqp-stringptr);
173   fieldname[eqp-stringptr]='\0';
174   strncpy( fieldval, eqp+1, andp-eqp-1);
175   fieldval[andp-eqp-1]='\0';
176
177   return nexfieldptr;
178 }
179
180 void print_queryparam( query_param_t query_param)
181 {
182   int i;
183
184   fprintf( logstream, "query parameters:\n");
185   fprintf( logstream, "\t target: %s\n", query_param.target);
186   fprintf( logstream, "\t fx,fy: %d, %d\n", query_param.fx, query_param.fy);
187   fprintf( logstream, "\t rx,ry: %d, %d \t rw,rh: %d, %d\n", query_param.rx, query_param.ry, query_param.rw, query_param.rh);
188   fprintf( logstream, "\t cnew: %d\n", query_param.cnew);
189   fprintf( logstream, "\t cid: %s\n", query_param.cid);
190   
191   fprintf( logstream, "\t cclose: ");
192   for( i=0; query_param.cclose[i][0]!=0 && i<MAX_NUMOFCCLOSE; i++)
193     fprintf( logstream, "%s ", query_param.cclose[i]);
194   fprintf(logstream, "\n");
195   
196   fprintf( logstream, "\t req-box-prop\n");
197   for( i=0; query_param.box_type[i][0]!=0 && i<MAX_NUMOFBOX; i++){
198     fprintf( logstream, "\t\t box_type: %.4s limit: %d w:%d s:%d g:%d a:%d priority:%d\n", query_param.box_type[i], query_param.limit[i], query_param.w[i], query_param.s[i], query_param.g[i], query_param.a[i], query_param.priority[i]);
199   }
200   
201   fprintf( logstream, "\t root-bin:  %d\n", query_param.root_bin);
202   fprintf( logstream, "\t max-depth: %d\n", query_param.max_depth);
203   fprintf( logstream, "\t metadata-only: %d\n", query_param.metadata_only);
204 }
205
206 void str2cclose( char *src, char cclose[][MAX_LENOFCID])
207 {
208   int i, u, v;
209
210   size_t len = strlen( src);
211   
212   for( i=0, u=0, v=0; i<len; i++){
213     if( src[i]==','){
214       u++;
215       v=0;
216     }
217     else
218       cclose[u][v++] = src[i];
219   }
220 }
221
222 void parse_req_box_prop( char *req_box_prop, int idx, query_param_t *query_param);
223
224 void parse_metareq( char *field, query_param_t *query_param)
225 {
226   char req_box_prop[20];
227   char *ptr, *src;
228   int numofboxreq = 0;
229   
230   memset( req_box_prop, 0, 20);
231
232   // req-box-prop
233   ptr = strchr( field, '[');
234   ptr++;
235   src = ptr;
236   while( *ptr != ']'){
237     if( *ptr == ';'){
238       strncpy( req_box_prop, src, ptr-src);
239       parse_req_box_prop( req_box_prop, numofboxreq++, query_param);
240       ptr++;
241       src = ptr;
242       memset( req_box_prop, 0, 20);
243     }
244     ptr++;
245   }
246   strncpy( req_box_prop, src, ptr-src);
247
248   parse_req_box_prop( req_box_prop, numofboxreq++, query_param);
249
250   if(( ptr = strchr( field, 'R')))
251     sscanf( ptr+1, "%d", &(query_param->root_bin));
252   
253   if(( ptr = strchr( field, 'D')))
254     sscanf( ptr+1, "%d", &(query_param->max_depth));
255
256   if(( ptr = strstr( field, "!!")))
257     query_param->metadata_only = true;
258 }
259
260 void parse_req_box_prop( char *req_box_prop, int idx, query_param_t *query_param)
261 {
262   char *ptr;
263   
264   if( *req_box_prop == '*')
265     query_param->box_type[idx][0]='*';
266   else
267     strncpy( query_param->box_type[idx], req_box_prop, 4);
268
269   if(( ptr = strchr( req_box_prop, ':'))){
270     if( *(ptr+1)=='r')
271       query_param->limit[idx] = -1;
272     else
273       sscanf( ptr+1, "%d", &(query_param->limit[idx]));
274   }
275
276   if(( ptr = strchr( req_box_prop, '/'))){
277     ptr++;
278     while( *ptr=='w' || *ptr=='s' || *ptr=='g' || *ptr=='a'){
279       switch( *ptr){
280       case 'w': query_param->w[idx] = true; break;
281       case 's': query_param->s[idx] = true; break;
282       case 'g': query_param->g[idx] = true; break;
283       case 'a': query_param->a[idx] = true; break;
284       }
285       ptr++;
286     }
287   }
288   else{
289     query_param->g[idx] = true;
290     query_param->s[idx] = true;
291     query_param->w[idx] = true;
292   }
293
294   if((ptr = strchr( req_box_prop, '!')))
295     query_param->priority[idx] = true;
296   
297   idx++;   
298 }