From 1dc2654b54ace196973a53e46c0b0c1241125976 Mon Sep 17 00:00:00 2001 From: Antonin Descampe Date: Sun, 8 May 2011 10:49:06 +0000 Subject: [PATCH] OpenJPIP: fixed several bugs in opj_server (removal of strsep function, duplication of query string) + some changes to compile opj_server under windows (replacement of strcasecmp(), bzero()). --- CHANGES | 3 + applications/jpip/opj_server/query_parser.c | 90 ++++++++++++++++----- 2 files changed, 71 insertions(+), 22 deletions(-) diff --git a/CHANGES b/CHANGES index d524eccd..130090a3 100644 --- a/CHANGES +++ b/CHANGES @@ -5,6 +5,9 @@ What's New for OpenJPEG ! : changed + : added +May 8, 2011 +* [antonin] OpenJPIP: fixed several bugs in opj_server (removal of strsep function, duplication of query string) + some changes to compile opj_server under windows (replacement of strcasecmp(), bzero()). + April 16, 2011 * [antonin] fixed a bug in mqc.c (see https://groups.google.com/d/topic/openjpeg/kQ1PbZUd19k/discussion, credit to Peter Wimmer) * [antonin] fixed wrong path in Doxyfile.dox diff --git a/applications/jpip/opj_server/query_parser.c b/applications/jpip/opj_server/query_parser.c index 23e6a09d..879c42b7 100644 --- a/applications/jpip/opj_server/query_parser.c +++ b/applications/jpip/opj_server/query_parser.c @@ -27,12 +27,18 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - +#ifndef _WIN32 #include +#endif #include #include #include "query_parser.h" +#ifdef _WIN32 +#include +#define strcasecmp _stricmp +#endif + #ifdef SERVER #include "fcgi_stdio.h" #define logstream FCGI_stdout @@ -50,6 +56,16 @@ */ void init_queryparam( query_param_t *query_param); +/* + * get a pair of field name and value from the string starting fieldname=fieldval&... format + * + * @param[in] stringptr pointer to the beginning of the parsing string + * @param[out] fieldname string to copy the field name, if not found, NULL + * @param[out] fieldval string to copy the field value, if not found, NULL + * @return pointer to the next field string, if there is none, NULL + */ +char * get_fieldparam( char *stringptr, char *fieldname, char *fieldval); + /** * parse string to string array * @@ -60,10 +76,15 @@ void str2cclose( char *src, char cclose[][MAX_LENOFCID]); void parse_metareq( char *field, query_param_t *query_param); +//! maximum length of field name +#define MAX_LENOFFIELDNAME 10 + +//! maximum length of field value +#define MAX_LENOFFIELDVAL 128 void parse_query( char *query_string, query_param_t *query_param) { - char *pquery, *field, *fieldname; + char *pquery, fieldname[MAX_LENOFFIELDNAME], fieldval[MAX_LENOFFIELDVAL]; init_queryparam( query_param); @@ -71,43 +92,40 @@ void parse_query( char *query_string, query_param_t *query_param) while( pquery!=NULL) { - field = strsep(&pquery, "&\n"); - //field = strtok_r( query, "&", pquery); - - fieldname = strsep(&field, "="); - //fieldname = strtok( field, "="); + pquery = get_fieldparam( pquery, fieldname, fieldval); - if( field != NULL){ + if( fieldname[0] != '\0'){ if( strcasecmp( fieldname, "target") == 0) - strcpy( query_param->target,field); + strcpy( query_param->target,fieldval); else if( strcasecmp( fieldname, "fsiz") == 0) - sscanf( field, "%d,%d", &query_param->fx, &query_param->fy); + sscanf( fieldval, "%d,%d", &query_param->fx, &query_param->fy); else if( strcasecmp( fieldname, "roff") == 0) - sscanf( field, "%d,%d", &query_param->rx, &query_param->ry); + sscanf( fieldval, "%d,%d", &query_param->rx, &query_param->ry); else if( strcasecmp( fieldname, "rsiz") == 0) - sscanf( field, "%d,%d", &query_param->rw, &query_param->rh); + sscanf( fieldval, "%d,%d", &query_param->rw, &query_param->rh); else if( strcasecmp( fieldname, "cid") == 0) - strcpy( query_param->cid, field); + strcpy( query_param->cid, fieldval); else if( strcasecmp( fieldname, "cnew") == 0) query_param->cnew = true; else if( strcasecmp( fieldname, "cclose") == 0) - str2cclose( field, query_param->cclose); + str2cclose( fieldval, query_param->cclose); else if( strcasecmp( fieldname, "metareq") == 0) - parse_metareq( field, query_param); + parse_metareq( fieldval, query_param); } } } - void init_queryparam( query_param_t *query_param) { + int i; + query_param->target[0]='\0'; query_param->fx=-1; query_param->fy=-1; @@ -120,7 +138,7 @@ void init_queryparam( query_param_t *query_param) memset( query_param->cclose, 0, MAX_NUMOFCCLOSE*MAX_LENOFCID); memset( query_param->box_type, 0, MAX_NUMOFBOX*4); memset( query_param->limit, 0, MAX_NUMOFBOX*sizeof(int)); - for( int i=0; iw[i] = false; query_param->s[i] = false; query_param->g[i] = false; @@ -133,8 +151,35 @@ void init_queryparam( query_param_t *query_param) } +char * get_fieldparam( char *stringptr, char *fieldname, char *fieldval) +{ + char *eqp, *andp, *nexfieldptr; + + if((eqp = strchr( stringptr, '='))==NULL){ + fprintf( stderr, "= not found\n"); + strcpy( fieldname, ""); + strcpy( fieldval, ""); + return NULL; + } + if((andp = strchr( stringptr, '&'))==NULL){ + andp = strchr( stringptr, '\0'); + nexfieldptr = NULL; + } + else + nexfieldptr = andp+1; + + strncpy( fieldname, stringptr, eqp-stringptr); + fieldname[eqp-stringptr]='\0'; + strncpy( fieldval, eqp+1, andp-eqp-1); + fieldval[andp-eqp-1]='\0'; + + return nexfieldptr; +} + void print_queryparam( query_param_t query_param) { + int i; + fprintf( logstream, "query parameters:\n"); fprintf( logstream, "\t target: %s\n", query_param.target); fprintf( logstream, "\t fx,fy: %d, %d\n", query_param.fx, query_param.fy); @@ -143,12 +188,12 @@ void print_queryparam( query_param_t query_param) fprintf( logstream, "\t cid: %s\n", query_param.cid); fprintf( logstream, "\t cclose: "); - for( int i=0; query_param.cclose[i][0]!=0 && i