Fix potential out-of-bounds read (coverity) (#844)
[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       else if( strcasecmp( fieldname, "tid") == 0)
107         query_param->tid = strdup( fieldval);
108
109       else if( strcasecmp( fieldname, "fsiz") == 0)
110         sscanf( fieldval, "%d,%d", &query_param->fx, &query_param->fy);
111       
112       else if( strcasecmp( fieldname, "roff") == 0)
113         sscanf( fieldval, "%d,%d", &query_param->rx, &query_param->ry);
114
115       else if( strcasecmp( fieldname, "rsiz") == 0)
116         sscanf( fieldval, "%d,%d", &query_param->rw, &query_param->rh);
117       
118       else if( strcasecmp( fieldname, "layers") == 0)
119         sscanf( fieldval, "%d", &query_param->layers);
120       
121       else if( strcasecmp( fieldname, "cid") == 0)
122         query_param->cid = strdup( fieldval);
123
124       else if( strcasecmp( fieldname, "cnew") == 0){
125         if( strncasecmp( fieldval, "http-tcp", 8) == 0)
126           query_param->cnew = tcp;
127         else if( strncasecmp( fieldval, "http", 4) == 0)
128           query_param->cnew = http;
129       }
130       
131       else if( strcasecmp( fieldname, "cclose") == 0)
132         parse_cclose( fieldval, query_param);
133       
134       else if( strcasecmp( fieldname, "metareq") == 0)
135         parse_metareq( fieldval, query_param);
136       
137       else if( strcasecmp( fieldname, "comps") == 0)
138         parse_comps( fieldval, query_param);
139       
140       else if( strcasecmp( fieldname, "type") == 0){
141         if( strncasecmp( fieldval, "jpp-stream", 10) == 0)
142           query_param->return_type = JPPstream;
143         else if( strncasecmp( fieldval, "jpt-stream", 10) == 0)
144           query_param->return_type = JPTstream;
145       }
146
147       else if( strcasecmp( fieldname, "len") == 0){
148         sscanf( fieldval, "%d", &query_param->len);
149         if( query_param->len == 2000) /* for kakadu client*/
150           strncpy( query_param->box_type[0], "ftyp", 4);
151       }
152     }
153   }
154   return query_param;
155 }
156
157 query_param_t * get_initquery(void)
158 {
159   query_param_t *query;
160   int i;
161
162   query = (query_param_t *)opj_malloc( sizeof(query_param_t));
163
164   query->target = NULL;
165   query->tid = NULL;
166   query->fx = -1;
167   query->fy = -1;
168   query->rx = -1;
169   query->ry = -1;
170   query->rw = -1;
171   query->rh = -1;
172   query->layers = -1;
173   query->lastcomp = -1;
174   query->comps = NULL;  
175   query->cid = NULL;
176   query->cnew = non;
177   query->cclose = NULL;
178   query->numOfcclose = 0;
179   memset( query->box_type, 0, MAX_NUMOFBOX*4);
180   memset( query->limit, 0, MAX_NUMOFBOX*sizeof(int));
181   for( i=0; i<MAX_NUMOFBOX; i++){
182     query->w[i] = OPJ_FALSE;
183     query->s[i] = OPJ_FALSE;
184     query->g[i] = OPJ_FALSE;
185     query->a[i] = OPJ_FALSE;
186     query->priority[i] = OPJ_FALSE;
187   }
188   query->root_bin = 0;
189   query->max_depth = -1;
190   query->metadata_only = OPJ_FALSE;
191   query->return_type = UNKNOWN;
192   query->len = -1;
193
194   return query;
195 }
196
197
198 char * get_fieldparam( const char *stringptr, char *fieldname, char *fieldval)
199 {
200   char *eqp, *andp, *nexfieldptr;
201
202   if((eqp = strchr( stringptr, '='))==NULL){
203     fprintf( stderr, "= not found\n");
204     strcpy( fieldname, "");
205     strcpy( fieldval, "");
206     return NULL;
207   }
208   if((andp = strchr( stringptr, '&'))==NULL){
209     andp = strchr( stringptr, '\0');
210     nexfieldptr = NULL;
211   }
212   else
213     nexfieldptr = andp+1;
214
215   assert( (size_t)(eqp-stringptr));
216   strncpy( fieldname, stringptr, (size_t)(eqp-stringptr));
217   fieldname[eqp-stringptr]='\0';
218   assert( andp-eqp-1 >= 0);
219   strncpy( fieldval, eqp+1, (size_t)(andp-eqp-1));
220   fieldval[andp-eqp-1]='\0';
221
222   return nexfieldptr;
223 }
224
225 void print_queryparam( query_param_t query_param)
226 {
227   int i;
228   char *cclose;
229
230   fprintf( logstream, "query parameters:\n");
231   
232   if( query_param.target)
233     fprintf( logstream, "\t target: %s\n", query_param.target);
234   
235   if( query_param.tid)
236     fprintf( logstream, "\t tid: %s\n", query_param.tid);
237   
238   fprintf( logstream, "\t fx,fy: %d, %d\n", query_param.fx, query_param.fy);
239   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);
240   fprintf( logstream, "\t layers: %d\n", query_param.layers);
241   fprintf( logstream, "\t components: ");
242   
243   if( query_param.lastcomp == -1)
244     fprintf( logstream, "ALL\n");
245   else{
246     for( i=0; i<=query_param.lastcomp; i++)
247       if( query_param.comps[i])
248         fprintf( logstream, "%d ", i);
249     fprintf( logstream, "\n");
250   }
251   fprintf( logstream, "\t cnew: %d\n", query_param.cnew);
252   
253   if( query_param.cid)
254     fprintf( logstream, "\t cid: %s\n", query_param.cid);
255   
256   if( query_param.cclose){
257     fprintf( logstream, "\t cclose: ");
258     
259     for( i=0, cclose=query_param.cclose; i<query_param.numOfcclose; i++){
260       fprintf( logstream, "%s ", cclose);
261       cclose += (strlen(cclose)+1);
262     }
263     fprintf(logstream, "\n");
264   }
265
266   fprintf( logstream, "\t req-box-prop\n");
267   for( i=0; i<MAX_NUMOFBOX && query_param.box_type[i][0]!=0; i++){
268     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]);
269   }
270   
271   fprintf( logstream, "\t root-bin:  %d\n", query_param.root_bin);
272   fprintf( logstream, "\t max-depth: %d\n", query_param.max_depth);
273   fprintf( logstream, "\t metadata-only: %d\n", query_param.metadata_only);
274   fprintf( logstream, "\t image return type: %d, [JPP-stream=0, JPT-stream=1, UNKNOWN=-1]\n", query_param.return_type);
275   fprintf( logstream, "\t len:  %d\n", query_param.len);
276 }
277
278 void parse_cclose( char *src, query_param_t *query_param)
279 {
280   size_t i;
281   size_t len;
282   
283   len = strlen( src);
284   query_param->cclose = strdup( src);
285   
286   for( i=0; i<len; i++)
287     if( query_param->cclose[i] == ','){
288       query_param->cclose[i] = '\0';
289       query_param->numOfcclose ++;
290     }
291   
292   query_param->numOfcclose ++;
293 }
294
295 void parse_req_box_prop( char *req_box_prop, int idx, query_param_t *query_param);
296
297 void parse_metareq( char *field, query_param_t *query_param)
298 {
299   char req_box_prop[20];
300   char *ptr, *src;
301   int numofboxreq = 0;
302   
303   memset( req_box_prop, 0, 20);
304
305   /* req-box-prop*/
306   ptr = strchr( field, '[');
307   ptr++;
308   src = ptr;
309   while( *ptr != ']'){
310     if( *ptr == ';'){
311       assert( ptr-src >= 0);
312       strncpy( req_box_prop, src, (size_t)(ptr-src));
313       parse_req_box_prop( req_box_prop, numofboxreq++, query_param);
314       ptr++;
315       src = ptr;
316       memset( req_box_prop, 0, 20);
317     }
318     ptr++;
319   }
320   assert(ptr-src>=0);
321   strncpy( req_box_prop, src, (size_t)(ptr-src));
322
323   parse_req_box_prop( req_box_prop, numofboxreq++, query_param);
324
325   if(( ptr = strchr( field, 'R')))
326     sscanf( ptr+1, "%d", &(query_param->root_bin));
327   
328   if(( ptr = strchr( field, 'D')))
329     sscanf( ptr+1, "%d", &(query_param->max_depth));
330
331   if(( ptr = strstr( field, "!!")))
332     query_param->metadata_only = OPJ_TRUE;
333 }
334
335 void parse_req_box_prop( char *req_box_prop, int idx, query_param_t *query_param)
336 {
337   char *ptr;
338   
339   if( *req_box_prop == '*')
340     query_param->box_type[idx][0]='*';
341   else
342     strncpy( query_param->box_type[idx], req_box_prop, 4);
343   
344   if(( ptr = strchr( req_box_prop, ':'))){
345     if( *(ptr+1)=='r')
346       query_param->limit[idx] = -1;
347     else
348       sscanf( ptr+1, "%d", &(query_param->limit[idx]));
349   }
350
351   if(( ptr = strchr( req_box_prop, '/'))){
352     ptr++;
353     while( *ptr=='w' || *ptr=='s' || *ptr=='g' || *ptr=='a'){
354       switch( *ptr){
355       case 'w': query_param->w[idx] = OPJ_TRUE; break;
356       case 's': query_param->s[idx] = OPJ_TRUE; break;
357       case 'g': query_param->g[idx] = OPJ_TRUE; break;
358       case 'a': query_param->a[idx] = OPJ_TRUE; break;
359       }
360       ptr++;
361     }
362   }
363   else{
364     query_param->g[idx] = OPJ_TRUE;
365     query_param->s[idx] = OPJ_TRUE;
366     query_param->w[idx] = OPJ_TRUE;
367   }
368
369   if((ptr = strchr( req_box_prop, '!')))
370     query_param->priority[idx] = OPJ_TRUE;
371   
372   idx++;   
373 }
374
375 void parse_comps( char *field, query_param_t *query_param)
376 {
377   int i,start,stop,aux = -1;
378   char *ptr1,*ptr2;
379
380   ptr1 = strchr( field, '-');
381   ptr2 = strchr( field, ',');
382
383   if( ptr1 && ptr2)
384     if( ptr1 > ptr2)
385       sscanf( field, "%d,%d-%d",&aux, &start, &stop);
386     else
387       sscanf( field, "%d-%d,%d", &start, &stop, &aux);
388   else
389     if(ptr1)
390       sscanf( field, "%d-%d", &start, &stop);
391     else if(ptr2){
392       sscanf( field, "%d,%d", &start, &stop);
393       aux = start;
394       start = stop;
395     }
396     else{
397       sscanf( field, "%d", &stop);
398       start = stop;
399     }
400   
401   query_param->lastcomp = stop > aux ? stop : aux;
402   query_param->comps = (OPJ_BOOL *)opj_calloc( 1, (OPJ_SIZE_T)(query_param->lastcomp+1)*sizeof(OPJ_BOOL));
403
404   for( i=start; i<=stop; i++)
405     query_param->comps[i]=OPJ_TRUE;
406   
407   if(aux!=-1)
408     query_param->comps[aux] = OPJ_TRUE;
409 }
410
411 void delete_query( query_param_t **query)
412 {
413   if( (*query)->target)
414     opj_free( (*query)->target);
415   
416   if( (*query)->tid)
417     opj_free( (*query)->tid);
418   
419   if( (*query)->comps)
420     opj_free((*query)->comps);
421   
422   if( (*query)->cid)
423     opj_free( (*query)->cid);
424
425   if( (*query)->cclose)
426     opj_free( (*query)->cclose);
427   
428   opj_free( *query);
429 }