char *fmakeword(FILE *f, char stop, int *cl) { int wsize; char *word; int ll; wsize = 102400; ll=0; word = (char *) msSmallMalloc(sizeof(char) * (wsize + 1)); while(1) { word[ll] = (char)fgetc(f); if(ll==wsize) { word[ll+1] = '\0'; wsize+=102400; word = (char *)msSmallRealloc(word,sizeof(char)*(wsize+1)); } --(*cl); if((word[ll] == stop) || (feof(f)) || (!(*cl))) { if(word[ll] != stop) ll++; word[ll] = '\0'; word = (char *) msSmallRealloc(word, ll+1); return word; } ++ll; } }
void KmlRenderer::addLineStyleToList(strokeStyleObj *style) { /*actually this is not necessary. kml only uses the last LineStyle so we should not bother keeping them all*/ int i =0; for (i=0; i<numLineStyle; i++) { if (style->width == LineStyle[i].width && LineStyle[i].color->alpha == style->color->alpha && LineStyle[i].color->red == style->color->red && LineStyle[i].color->green == style->color->green && LineStyle[i].color->blue == style->color->blue) break; } if (i == numLineStyle) { numLineStyle++; if (LineStyle == NULL) LineStyle = (strokeStyleObj *)msSmallMalloc(sizeof(strokeStyleObj)); else LineStyle = (strokeStyleObj *)msSmallRealloc(LineStyle, sizeof(strokeStyleObj)*numLineStyle); memcpy(&LineStyle[numLineStyle-1], style, sizeof(strokeStyleObj)); } }
template<class VertexSource> int renderPolygonHatches(imageObj *img,VertexSource &clipper, colorObj *color) { if(img->format->renderer == MS_RENDER_WITH_AGG) { AGG2Renderer *r = AGG_RENDERER(img); r->m_rasterizer_aa_gamma.reset(); r->m_rasterizer_aa_gamma.filling_rule(mapserver::fill_non_zero); r->m_rasterizer_aa_gamma.add_path(clipper); r->m_renderer_scanline.color(aggColor(color)); mapserver::render_scanlines(r->m_rasterizer_aa_gamma, r->sl_poly, r->m_renderer_scanline); } else { shapeObj shape; msInitShape(&shape); int allocated = 20; lineObj line; shape.line = &line; shape.numlines = 1; shape.line[0].point = (pointObj*)msSmallCalloc(allocated,sizeof(pointObj)); shape.line[0].numpoints = 0; double x=0,y=0; unsigned int cmd; clipper.rewind(0); while((cmd = clipper.vertex(&x,&y)) != mapserver::path_cmd_stop) { switch(cmd) { case mapserver::path_cmd_line_to: if(shape.line[0].numpoints == allocated) { allocated *= 2; shape.line[0].point = (pointObj*)msSmallRealloc(shape.line[0].point, allocated*sizeof(pointObj)); } shape.line[0].point[shape.line[0].numpoints].x = x; shape.line[0].point[shape.line[0].numpoints].y = y; shape.line[0].numpoints++; break; case mapserver::path_cmd_move_to: shape.line[0].point[0].x = x; shape.line[0].point[0].y = y; shape.line[0].numpoints = 1; break; case mapserver::path_cmd_end_poly|mapserver::path_flags_close: if(shape.line[0].numpoints > 2) { if(UNLIKELY(MS_FAILURE == MS_IMAGE_RENDERER(img)->renderPolygon(img,&shape,color))) { free(shape.line[0].point); return MS_FAILURE; } } break; default: assert(0); //WTF? } } free(shape.line[0].point); } return MS_SUCCESS; }
/* * Allocate more memory to the table if necessary. */ int growTable(lookupTable *data) { int i; data->table = (shapeData*) msSmallRealloc(data->table,sizeof(*data->table)*data->size*2); data->size = data->size*2; for(i=data->counter; i<data->size; i++) { data->table[i].datavalues = NULL; data->table[i].itemvalue = NULL; data->table[i].utfvalue = 0; data->table[i].serialid = 0; } return MS_SUCCESS; }
/********************************************************************** * msHTTPWriteFct() * * CURL_OPTWRITEFUNCTION, called to write blocks of data to the file we * are downloading. Should return the number of bytes that were taken * care of. If that amount differs from the amount passed to it * it'll signal an error to the library and it will abort the transfer * and produce a CURLE_WRITE_ERROR. * **********************************************************************/ static size_t msHTTPWriteFct(void *buffer, size_t size, size_t nmemb, void *reqInfo) { httpRequestObj *psReq; psReq = (httpRequestObj *)reqInfo; if (psReq->debug) { msDebug("msHTTPWriteFct(id=%d, %d bytes)\n", psReq->nLayerId, size*nmemb); } /* Case where we are writing to a disk file. */ if( psReq->fp != NULL ) { return fwrite(buffer, size, nmemb, psReq->fp); } /* Case where we build up the result in memory */ else { if( psReq->result_data == NULL ) { psReq->result_buf_size = size*nmemb + 10000; psReq->result_data = (char *) msSmallMalloc( psReq->result_buf_size ); } else if( psReq->result_size + nmemb * size > psReq->result_buf_size ) { psReq->result_buf_size = psReq->result_size + nmemb*size + 10000; psReq->result_data = (char *) msSmallRealloc( psReq->result_data, psReq->result_buf_size ); } if( psReq->result_data == NULL ) { msSetError(MS_HTTPERR, "Unable to grow HTTP result buffer to size %d.", "msHTTPWriteFct()", psReq->result_buf_size ); psReq->result_buf_size = 0; psReq->result_size = 0; return -1; } memcpy( psReq->result_data + psReq->result_size, buffer, size*nmemb ); psReq->result_size += size*nmemb; return size*nmemb; } }
/* Append the given printf-style formatted string to the pString 'ps'. * This is much cleaner (and faster) than the technique this file * used to use! */ static void im_iprintf(pString *ps, const char *fmt, ...) { int n, remaining; va_list ap; do { remaining = *(ps->alloc_size) - ps->string_len; va_start(ap, fmt); #if defined(HAVE_VSNPRINTF) n = vsnprintf((*(ps->string)) + ps->string_len, remaining, fmt, ap); #else /* If vsnprintf() is not available then require a minimum * of 512 bytes of free space to prevent a buffer overflow * This is not fully bulletproof but should help, see bug 1613 */ if (remaining < 512) n = -1; else n = vsprintf((*(ps->string)) + ps->string_len, fmt, ap); #endif va_end(ap); /* if that worked, we're done! */ if (-1<n && n<remaining) { ps->string_len += n; return; } else { /* double allocated string size */ *(ps->alloc_size) *= 2;/* these keeps realloc linear.*/ if (*(ps->alloc_size) < 1024) /* careful: initial size can be 0 */ *(ps->alloc_size)=1024; if (n>-1 && *(ps->alloc_size) <= (n + ps->string_len)) /* ensure at least as much as what is needed */ *(ps->alloc_size) = n+ps->string_len+1; *(ps->string) = (char *) msSmallRealloc (*(ps->string), *(ps->alloc_size)); /* if realloc doesn't work, we're screwed! */ } } while (1); /* go back and try again. */ }
int readPostBody( cgiRequestObj *request, char **data ) { size_t data_max, data_len; int chunk_size; msIO_needBinaryStdin(); /* -------------------------------------------------------------------- */ /* If the length is provided, read in one gulp. */ /* -------------------------------------------------------------------- */ if( getenv("CONTENT_LENGTH") != NULL ) { data_max = (size_t) atoi(getenv("CONTENT_LENGTH")); /* Test for suspicious CONTENT_LENGTH (negative value or SIZE_MAX) */ if( data_max >= SIZE_MAX ) { msIO_setHeader("Content-Type","text/html"); msIO_sendHeaders(); msIO_printf("Suspicious Content-Length.\n"); return MS_FAILURE; } *data = (char *) malloc(data_max+1); if( *data == NULL ) { msIO_setHeader("Content-Type","text/html"); msIO_sendHeaders(); msIO_printf("malloc() failed, Content-Length: %u unreasonably large?\n", data_max ); return MS_FAILURE; } if( (int) msIO_fread(*data, 1, data_max, stdin) < data_max ) { msIO_setHeader("Content-Type","text/html"); msIO_sendHeaders(); msIO_printf("POST body is short\n"); return MS_FAILURE; } (*data)[data_max] = '\0'; return MS_SUCCESS; } /* -------------------------------------------------------------------- */ /* Otherwise read in chunks to the end. */ /* -------------------------------------------------------------------- */ #define DATA_ALLOC_SIZE 10000 data_max = DATA_ALLOC_SIZE; data_len = 0; *data = (char *) msSmallMalloc(data_max+1); while( (chunk_size = msIO_fread( *data + data_len, 1, data_max-data_len, stdin )) > 0 ) { data_len += chunk_size; if( data_len == data_max ) { /* Realloc buffer, making sure we check for possible size_t overflow */ if ( data_max > SIZE_MAX - (DATA_ALLOC_SIZE+1) ) { msIO_setHeader("Content-Type","text/html"); msIO_sendHeaders(); msIO_printf("Possible size_t overflow, cannot reallocate input buffer, POST body too large?\n" ); return MS_FAILURE; } data_max = data_max + DATA_ALLOC_SIZE; *data = (char *) msSmallRealloc(*data, data_max+1); } } (*data)[data_len] = '\0'; return MS_SUCCESS; }
int loadParams(cgiRequestObj *request, char* (*getenv2)(const char*, void* thread_context), char *raw_post_data, ms_uint32 raw_post_data_length, void* thread_context) { register int x,m=0; char *s, *queryString = NULL, *httpCookie = NULL; int debuglevel; int maxParams = MS_DEFAULT_CGI_PARAMS; if (getenv2==NULL) getenv2 = &msGetEnv; if(getenv2("REQUEST_METHOD", thread_context)==NULL) { msIO_printf("This script can only be used to decode form results and \n"); msIO_printf("should be initiated as a CGI process via a httpd server.\n"); return -1; } debuglevel = (int)msGetGlobalDebugLevel(); if(strcmp(getenv2("REQUEST_METHOD", thread_context),"POST") == 0) { /* we've got a post from a form */ char *post_data; int data_len; request->type = MS_POST_REQUEST; s = getenv2("CONTENT_TYPE", thread_context); if (s != NULL) request->contenttype = msStrdup(s); /* we've to set default Content-Type which is * application/octet-stream according to * W3 RFC 2626 section 7.2.1 */ else request->contenttype = msStrdup("application/octet-stream"); if (raw_post_data) { post_data = msStrdup(raw_post_data); data_len = raw_post_data_length; } else { if(MS_SUCCESS != readPostBody( request, &post_data )) return -1; data_len = strlen(post_data); } /* if the content_type is application/x-www-form-urlencoded, we have to parse it like the QUERY_STRING variable */ if(strncmp(request->contenttype, "application/x-www-form-urlencoded", strlen("application/x-www-form-urlencoded")) == 0) { while( data_len > 0 && isspace(post_data[data_len-1]) ) post_data[--data_len] = '\0'; while( post_data[0] ) { if(m >= maxParams) { maxParams *= 2; request->ParamNames = (char **) msSmallRealloc(request->ParamNames,sizeof(char *) * maxParams); request->ParamValues = (char **) msSmallRealloc(request->ParamValues,sizeof(char *) * maxParams); } request->ParamValues[m] = makeword(post_data,'&'); plustospace(request->ParamValues[m]); unescape_url(request->ParamValues[m]); request->ParamNames[m] = makeword(request->ParamValues[m],'='); m++; } free( post_data ); } else request->postrequest = post_data; /* check the QUERY_STRING even in the post request since it can contain information. Eg a wfs request with */ s = getenv2("QUERY_STRING", thread_context); if(s) { if (debuglevel >= MS_DEBUGLEVEL_DEBUG) msDebug("loadParams() QUERY_STRING: %s\n", s); queryString = msStrdup(s); for(x=0; queryString[0] != '\0'; x++) { if(m >= maxParams) { maxParams *= 2; request->ParamNames = (char **) msSmallRealloc(request->ParamNames,sizeof(char *) * maxParams); request->ParamValues = (char **) msSmallRealloc(request->ParamValues,sizeof(char *) * maxParams); } request->ParamValues[m] = makeword(queryString,'&'); plustospace(request->ParamValues[m]); unescape_url(request->ParamValues[m]); request->ParamNames[m] = makeword(request->ParamValues[m],'='); m++; } } } else { if(strcmp(getenv2("REQUEST_METHOD", thread_context),"GET") == 0) { /* we've got a get request */ request->type = MS_GET_REQUEST; s = getenv2("QUERY_STRING", thread_context); if(s == NULL) { msIO_setHeader("Content-Type","text/html"); msIO_sendHeaders(); msIO_printf("No query information to decode. QUERY_STRING not set.\n"); return -1; } if (debuglevel >= MS_DEBUGLEVEL_DEBUG) msDebug("loadParams() QUERY_STRING: %s\n", s); if(strlen(s)==0) { msIO_setHeader("Content-Type","text/html"); msIO_sendHeaders(); msIO_printf("No query information to decode. QUERY_STRING is set, but empty.\n"); return -1; } /* don't modify the string returned by getenv2 */ queryString = msStrdup(s); for(x=0; queryString[0] != '\0'; x++) { if(m >= maxParams) { maxParams *= 2; request->ParamNames = (char **) msSmallRealloc(request->ParamNames,sizeof(char *) * maxParams); request->ParamValues = (char **) msSmallRealloc(request->ParamValues,sizeof(char *) * maxParams); } request->ParamValues[m] = makeword(queryString,'&'); plustospace(request->ParamValues[m]); unescape_url(request->ParamValues[m]); request->ParamNames[m] = makeword(request->ParamValues[m],'='); m++; } } else { msIO_setHeader("Content-Type","text/html"); msIO_sendHeaders(); msIO_printf("This script should be referenced with a METHOD of GET or METHOD of POST.\n"); return -1; } } /* check for any available cookies */ s = getenv2("HTTP_COOKIE", thread_context); if(s != NULL) { httpCookie = msStrdup(s); request->httpcookiedata = msStrdup(s); for(x=0; httpCookie[0] != '\0'; x++) { if(m >= maxParams) { maxParams *= 2; request->ParamNames = (char **) msSmallRealloc(request->ParamNames,sizeof(char *) * maxParams); request->ParamValues = (char **) msSmallRealloc(request->ParamValues,sizeof(char *) * maxParams); } request->ParamValues[m] = makeword(httpCookie,';'); plustospace(request->ParamValues[m]); unescape_url(request->ParamValues[m]); request->ParamNames[m] = makeword_skip(request->ParamValues[m],'=',' '); m++; } } if (queryString) free(queryString); if (httpCookie) free(httpCookie); return(m); }