/** * Makes a copy of the specified string. The memory is allocated using malloc. * Reports a resource error if there is not enough available memory. * * @param context the parsing context * @param src the source string to be copied * @return copy of the string, or NULL if memory allocation failed */ static char *parser_strdup(ploader_context_t *plcontext, const char *src) { char *dup; if ((dup = strdup(src)) == NULL) { resource_error(plcontext); } return dup; }
/** * Allocates memory using malloc. Reports a resource error if there is not * enough available memory. * * @param context the parsing context * @param size the number of bytes to allocate * @return pointer to the allocated memory, or NULL if memory allocation failed */ static void *parser_malloc(ploader_context_t *plcontext, size_t size) { void *ptr; if ((ptr = malloc(size)) == NULL) { resource_error(plcontext); } return ptr; }
static int create_pipes(p_options *info) { int i; for(i=0; i<3; i++) { p_stream *s = &info->streams[i]; if ( s->term ) { if ( pipe(s->fd) ) { assert(errno = EMFILE); return resource_error("open_files"); } } } return TRUE; }
/** * Processes the character data while parsing. * * @param userData the parsing context * @param str the string data * @param len the string length */ static void CP_XMLCALL character_data_handler( void *userData, const XML_Char *str, int len) { ploader_context_t *plcontext = userData; // Ignore leading whitespace if (plcontext->value == NULL) { int i; for (i = 0; i < len; i++) { if (str[i] != ' ' && str[i] != '\n' && str[i] != '\r' && str[i] != '\t') { break; } } str += i; len -= i; if (len == 0) { return; } } // Allocate more memory for the character data if needed if (plcontext->value_length + len >= plcontext->value_size) { size_t ns; char *nv; ns = plcontext->value_size; while (plcontext->value_length + len >= ns) { if (ns == 0) { ns = CP_CFG_ELEMENT_VALUE_INITSIZE; } else { ns = 2 * ns; } } if ((nv = realloc(plcontext->value, ns * sizeof(char))) != NULL) { plcontext->value = nv; plcontext->value_size = ns; } else { resource_error(plcontext); return; } } // Copy character data strncpy(plcontext->value + plcontext->value_length, str, len * sizeof(char)); plcontext->value_length += len; }
static foreign_t snowball(term_t lang, term_t in, term_t out) { struct sb_stemmer *stemmer = NULL; char *s; size_t len, olen; const sb_symbol *stemmed; if ( !get_lang_stemmer(lang, &stemmer) ) return FALSE; if ( !PL_get_nchars(in, &len, &s, CVT_ATOM|CVT_STRING|CVT_LIST|REP_UTF8|CVT_EXCEPTION) ) return FALSE; if ( !(stemmed = sb_stemmer_stem(stemmer, (const sb_symbol*)s, (int)len)) ) return resource_error("memory"); olen = sb_stemmer_length(stemmer); return PL_unify_chars(out, PL_ATOM|REP_UTF8, olen, (const char*)stemmed); }
static int get_lang_stemmer(term_t t, struct sb_stemmer **stemmer) { stem_cache *cache = get_cache(); atom_t lang; int i; if ( !PL_get_atom(t, &lang) ) return type_error("atom", t); for(i=0; i<CACHE_SIZE; i++) { if ( cache->stemmers[i].language == lang ) { *stemmer = cache->stemmers[i].stemmer; return TRUE; } } for(i=0; i<CACHE_SIZE; i++) { if ( !cache->stemmers[i].stemmer ) { struct sb_stemmer *st; if ( !(st= sb_stemmer_new(PL_atom_chars(lang), NULL)) ) { if ( errno == ENOMEM ) return resource_error("memory"); else return domain_error("snowball_algorithm", t); } cache->stemmers[i].language = lang; cache->stemmers[i].stemmer = st; PL_register_atom(cache->stemmers[i].language); *stemmer = cache->stemmers[i].stemmer; return TRUE; } } assert(0); /* TBD: clean cache */ return FALSE; }
void image_dct(INT32 args) { rgbd_group *area,*val; struct object *o; struct image *img; INT32 x,y,u,v; double xsz2,ysz2,enh,xp,yp,dx,dy; double *costbl; rgb_group *pix; if (!THIS->img) Pike_error("Called Image.Image object is not initialized\n"); #ifdef DCT_DEBUG fprintf(stderr,"%lu bytes, %lu bytes\n", DO_NOT_WARN((unsigned long)(sizeof(rgbd_group)*THIS->xsize*THIS->ysize)), DO_NOT_WARN((unsigned long)(sizeof(rgb_group)*THIS->xsize*THIS->ysize+1))); #endif area=xalloc(sizeof(rgbd_group)*THIS->xsize*THIS->ysize+1); if (!(costbl=malloc(sizeof(double)*THIS->xsize+1))) { free(area); resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); } o=clone_object(image_program,0); img=(struct image*)(o->storage); *img=*THIS; if (args>=2 && sp[-args].type==T_INT && sp[1-args].type==T_INT) { img->xsize=MAXIMUM(1,sp[-args].u.integer); img->ysize=MAXIMUM(1,sp[1-args].u.integer); } else { free(area); free(costbl); free_object(o); bad_arg_error("image->dct",sp-args,args,0,"",sp-args, "Bad arguments to image->dct()\n"); } if (!(img->img=(rgb_group*)malloc(sizeof(rgb_group)* img->xsize*img->ysize+1))) { free(area); free(costbl); free_object(o); resource_error(NULL,0,0,"memory",0,"Out of memory.\n"); } xsz2=THIS->xsize*2.0; ysz2=THIS->ysize*2.0; enh=(8.0/THIS->xsize)*(8.0/THIS->ysize); for (u=0; u<THIS->xsize; u++) { double d,z0; rgbd_group sum; for (v=0; v<THIS->ysize; v++) { d=(u?1:c0)*(v?1:c0)/4.0; sum.r=sum.g=sum.b=0; pix=THIS->img; for (x=0; x<THIS->xsize; x++) costbl[x]=cos( (2*x+1)*u*pi/xsz2 ); for (y=0; y<THIS->ysize; y++) { z0=cos( (2*y+1)*v*pi/ysz2 ); for (x=0; x<THIS->xsize; x++) { double z; z = costbl[x] * z0; sum.r += (float)(pix->r*z); sum.g += (float)(pix->g*z); sum.b += (float)(pix->b*z); pix++; } } sum.r *= (float)d; sum.g *= (float)d; sum.b *= (float)d; area[u+v*THIS->xsize]=sum; } #ifdef DCT_DEBUG fprintf(stderr,"."); fflush(stderr); #endif } #ifdef DCT_DEBUG fprintf(stderr,"\n"); #endif dx=((double)(THIS->xsize-1))/(img->xsize); dy=((double)(THIS->ysize-1))/(img->ysize); pix=img->img; for (y=0,yp=0; y<img->ysize; y++,yp+=dy) { double z0; rgbd_group sum; for (x=0,xp=0; x<img->xsize; x++,xp+=dx) { sum.r=sum.g=sum.b=0; val=area; for (u=0; u<THIS->xsize; u++) costbl[u]=cos( (2*xp+1)*u*pi/xsz2 ); for (v=0; v<THIS->ysize; v++) { z0=cos( (2*yp+1)*v*pi/ysz2 )*(v?1:c0)/4.0; for (u=0; u<THIS->xsize; u++) { double z; z = (u?1:c0) * costbl[u] * z0; sum.r += DO_NOT_WARN((float)(val->r*z)); sum.g += DO_NOT_WARN((float)(val->g*z)); sum.b += DO_NOT_WARN((float)(val->b*z)); val++; } } sum.r *= (float)enh; sum.g *= (float)enh; sum.b *= (float)enh; pix->r=testrange((DOUBLE_TO_INT(sum.r+0.5))); pix->g=testrange((DOUBLE_TO_INT(sum.g+0.5))); pix->b=testrange((DOUBLE_TO_INT(sum.b+0.5))); pix++; } #ifdef DCT_DEBUG fprintf(stderr,"."); fflush(stderr); #endif } free(area); free(costbl); pop_n_elems(args); push_object(o); }
/** * Processes the start of element events while parsing. * * @param userData the parsing context * @param name the element name * @param atts the element attributes */ static void CP_XMLCALL start_element_handler( void *userData, const XML_Char *name, const XML_Char **atts) { static const XML_Char * const req_plugin_atts[] = { "id", NULL }; static const XML_Char * const opt_plugin_atts[] = { "name", "version", "provider-name", NULL }; static const XML_Char * const req_bwcompatibility_atts[] = { NULL }; static const XML_Char * const opt_bwcompatibility_atts[] = { "abi", "api", NULL }; static const XML_Char * const req_cpluff_atts[] = { "version", NULL }; static const XML_Char * const opt_cpluff_atts[] = { NULL }; static const XML_Char * const req_import_atts[] = { "plugin", NULL }; static const XML_Char * const opt_import_atts[] = { "version", "optional", NULL }; static const XML_Char * const req_runtime_atts[] = { "library", NULL }; static const XML_Char * const opt_runtime_atts[] = { "funcs", NULL }; static const XML_Char * const req_ext_point_atts[] = { "id", NULL }; static const XML_Char * const opt_ext_point_atts[] = { "name", "schema", NULL }; static const XML_Char * const req_extension_atts[] = { "point", NULL }; //static const XML_Char * const opt_extension_atts[] = { "id", "name", NULL }; ploader_context_t *plcontext = userData; unsigned int i; // Process element start switch (plcontext->state) { case PARSER_BEGIN: if (!strcmp(name, "plugin")) { plcontext->state = PARSER_PLUGIN; if (!check_attributes(plcontext, name, atts, req_plugin_atts, opt_plugin_atts)) { break; } for (i = 0; atts[i] != NULL; i += 2) { if (!strcmp(atts[i], "name")) { plcontext->plugin->name = parser_strdup(plcontext, atts[i+1]); } else if (!strcmp(atts[i], "id")) { plcontext->plugin->identifier = parser_strdup(plcontext, atts[i+1]); } else if (!strcmp(atts[i], "version")) { plcontext->plugin->version = parser_strdup(plcontext, atts[i+1]); } else if (!strcmp(atts[i], "provider-name")) { plcontext->plugin->provider_name = parser_strdup(plcontext, atts[i+1]); } else if(!strcmp(atts[i],"url")){ plcontext->plugin->url = parser_strdup(plcontext, atts[i+1]); } else if(!strcmp(atts[i],"resourcetype")){ plcontext->plugin->resourcetype = parser_strdup(plcontext, atts[i+1]); } } } else { unexpected_element(plcontext, name); } break; case PARSER_PLUGIN: if (!strcmp(name, "backwards-compatibility")) { if (check_attributes(plcontext, name, atts, req_bwcompatibility_atts, opt_bwcompatibility_atts)) { for (i = 0; atts[i] != NULL; i += 2) { if (!strcmp(atts[i], "abi")) { plcontext->plugin->abi_bw_compatibility = parser_strdup(plcontext, atts[i+1]); } else if (!strcmp(atts[i], "api")) { plcontext->plugin->api_bw_compatibility = parser_strdup(plcontext, atts[i+1]); } } } } else if (!strcmp(name, "requires")) { plcontext->state = PARSER_REQUIRES; } else if (!strcmp(name, "runtime")) { if (check_attributes(plcontext, name, atts, req_runtime_atts, opt_runtime_atts)) { for (i = 0; atts[i] != NULL; i += 2) { if (!strcmp(atts[i], "library")) { plcontext->plugin->runtime_lib_name = parser_strdup(plcontext, atts[i+1]); } else if (!strcmp(atts[i], "funcs")) { plcontext->plugin->runtime_funcs_symbol = parser_strdup(plcontext, atts[i+1]); } } } } else if (!strcmp(name, "extension-point")) { if (check_attributes(plcontext, name, atts, req_ext_point_atts, opt_ext_point_atts)) { cp_ext_point_t *ext_point; // Allocate space for extension points, if necessary if (plcontext->plugin->num_ext_points == plcontext->ext_points_size) { cp_ext_point_t *nep; size_t ns; if (plcontext->ext_points_size == 0) { ns = 4; } else { ns = plcontext->ext_points_size * 2; } if ((nep = realloc(plcontext->plugin->ext_points, ns * sizeof(cp_ext_point_t))) == NULL) { resource_error(plcontext); break; } plcontext->plugin->ext_points = nep; plcontext->ext_points_size = ns; } // Parse extension point specification ext_point = plcontext->plugin->ext_points + plcontext->plugin->num_ext_points; memset(ext_point, 0, sizeof(cp_ext_point_t)); ext_point->plugin = plcontext->plugin; ext_point->name = NULL; ext_point->local_id = NULL; ext_point->identifier = NULL; ext_point->schema_path = NULL; for (i = 0; atts[i] != NULL; i += 2) { if (!strcmp(atts[i], "name")) { ext_point->name = parser_strdup(plcontext, atts[i+1]); } else if (!strcmp(atts[i], "id")) { ext_point->local_id = parser_strdup(plcontext, atts[i+1]); ext_point->identifier = parser_strscat(plcontext, plcontext->plugin->identifier, ".", atts[i+1], NULL); } else if (!strcmp(atts[i], "schema")) { ext_point->schema_path = parser_strdup(plcontext, atts[i+1]); } } plcontext->plugin->num_ext_points++; } } else if (!(strcmp(name, "extension"))) { plcontext->state = PARSER_EXTENSION; plcontext->depth = 0; if (check_req_attributes( plcontext, name, atts, req_extension_atts)) { cp_extension_t *extension; // Allocate space for extensions, if necessary if (plcontext->plugin->num_extensions == plcontext->extensions_size) { cp_extension_t *ne; size_t ns; if (plcontext->extensions_size == 0) { ns = 16; } else { ns = plcontext->extensions_size * 2; } if ((ne = realloc(plcontext->plugin->extensions, ns * sizeof(cp_extension_t))) == NULL) { resource_error(plcontext); break; } plcontext->plugin->extensions = ne; plcontext->extensions_size = ns; } // Parse extension attributes extension = plcontext->plugin->extensions + plcontext->plugin->num_extensions; memset(extension, 0, sizeof(cp_extension_t)); extension->plugin = plcontext->plugin; extension->name = NULL; extension->local_id = NULL; extension->identifier = NULL; extension->ext_point_id = NULL; extension->configuration = NULL; for (i = 0; atts[i] != NULL; i += 2) { if (!strcmp(atts[i], "point")) { extension->ext_point_id = parser_strdup(plcontext, atts[i+1]); } else if (!strcmp(atts[i], "id")) { extension->local_id = parser_strdup(plcontext, atts[i+1]); extension->identifier = parser_strscat(plcontext, plcontext->plugin->identifier, ".", atts[i+1], NULL); } else if (!strcmp(atts[i], "name")) { extension->name = parser_strdup(plcontext, atts[i+1]); } } plcontext->plugin->num_extensions++; // Initialize configuration parsing if ((extension->configuration = plcontext->configuration = parser_malloc(plcontext, sizeof(cp_cfg_element_t))) != NULL) { init_cfg_element(plcontext, plcontext->configuration, name, atts, NULL); } XML_SetCharacterDataHandler(plcontext->parser, character_data_handler); } } else { unexpected_element(plcontext, name); } break; case PARSER_REQUIRES: if (!strcmp(name, "c-pluff")) { if (check_attributes(plcontext, name, atts, req_cpluff_atts, opt_cpluff_atts)) { for (i = 0; atts[i] != NULL; i += 2) { if (!strcmp(atts[i], "version")) { plcontext->plugin->req_cpluff_version = parser_strdup(plcontext, atts[i+1]); } } } } else if (!strcmp(name, "import")) { if (check_attributes(plcontext, name, atts, req_import_atts, opt_import_atts)) { cp_plugin_import_t *import = NULL; // Allocate space for imports, if necessary if (plcontext->plugin->num_imports == plcontext->imports_size) { cp_plugin_import_t *ni; size_t ns; if (plcontext->imports_size == 0) { ns = 16; } else { ns = plcontext->imports_size * 2; } if ((ni = realloc(plcontext->plugin->imports, ns * sizeof(cp_plugin_import_t))) == NULL) { resource_error(plcontext); break; } plcontext->plugin->imports = ni; plcontext->imports_size = ns; } // Parse import specification import = plcontext->plugin->imports + plcontext->plugin->num_imports; memset(import, 0, sizeof(cp_plugin_import_t)); import->plugin_id = NULL; import->version = NULL; for (i = 0; atts[i] != NULL; i += 2) { if (!strcmp(atts[i], "plugin")) { import->plugin_id = parser_strdup(plcontext, atts[i+1]); } else if (!strcmp(atts[i], "version")) { import->version = parser_strdup(plcontext, atts[i+1]); } else if (!strcmp(atts[i], "optional")) { if (!strcmp(atts[i+1], "true") || !strcmp(atts[i+1], "1")) { import->optional = 1; } else if (strcmp(atts[i+1], "false") && strcmp(atts[i+1], "0")) { descriptor_errorf(plcontext, 0, _("unknown boolean value: %s"), atts[i+1]); } } } plcontext->plugin->num_imports++; } } else { unexpected_element(plcontext, name); } break; case PARSER_EXTENSION: plcontext->depth++; if (plcontext->configuration != NULL && plcontext->skippedCEs == 0) { cp_cfg_element_t *ce; // Allocate more space for children, if necessary if (plcontext->configuration->num_children == plcontext->configuration->index) { cp_cfg_element_t *nce; size_t ns; if (plcontext->configuration->index == 0) { ns = 16; } else { ns = plcontext->configuration->index * 2; } if ((nce = realloc(plcontext->configuration->children, ns * sizeof(cp_cfg_element_t))) == NULL) { plcontext->skippedCEs++; resource_error(plcontext); break; } plcontext->configuration->children = nce; plcontext->configuration->index = ns; } // Save possible value if (plcontext->value != NULL) { plcontext->value[plcontext->value_length] = '\0'; plcontext->configuration->value = plcontext->value; } ce = plcontext->configuration->children + plcontext->configuration->num_children; init_cfg_element(plcontext, ce, name, atts, plcontext->configuration); plcontext->configuration->num_children++; plcontext->configuration = ce; } break; case PARSER_UNKNOWN: plcontext->depth++; break; default: unexpected_element(plcontext, name); break; } }