Exemple #1
0
int get_status_servico(int ambiente, char *URL, int cuf, 
		EVP_PKEY *key, X509 *cert, char *msg){ 
	char *response, *status;
	int cStat;
	xmlDocPtr doc;
	char *xml = gen_cons_status(ambiente, cuf);
	response = send_sefaz(SEFAZ_NFE_STATUS_SERVICO, URL, ambiente, cuf, 
		xml, key, cert);
	if(response == NULL){
		strcpy(msg, "Sem resposta do SEFAZ, tente novamente");
		return -ESEFAZ;
	}
	doc = xmlReadMemory(response, strlen(response), "noname.xml", NULL, 0);
	status = get_xml_element(doc, "nfe:cStat");
	if(status == NULL){
		return -ESEFAZ;	
	}
	cStat = atoi(status);
	char *motivo = get_xml_element(doc, "nfe:xMotivo");
	if(motivo == NULL){
		return -ESEFAZ;	
	}
	strcpy(msg, motivo);
	xmlFree(motivo);
	xmlFree(status);
	return cStat;
}
Exemple #2
0
int cons_lote(LOTE *lote, char *URL, int ambiente, int cuf, EVP_PKEY *key, 
		X509 *cert, char *msg){
	char *response, *status;
	int cStat;
	xmlDocPtr doc;
	char *xml = gen_cons_nfe(lote, ambiente);
	response = send_sefaz(SEFAZ_NFE_RET_AUTORIZACAO, URL, ambiente, cuf, 
		xml, key, cert);
	if(response == NULL){
		strcpy(msg, "Sem resposta do SEFAZ, tente novamente");
		return -ESEFAZ;
	}
	doc = xmlReadMemory(response, strlen(response), "noname.xml", NULL, 0);
	status = get_xml_element(doc, "nfe:cStat");
	if(status == NULL){
		return -ESEFAZ;	
	}
	cStat = atoi(status);
	char *motivo = get_xml_element(doc, "nfe:xMotivo");
	if(motivo == NULL){
		return -ESEFAZ;	
	}
	msg = strcpy(msg, motivo);
	if(cStat == 104)
		sefaz_response_protocolos(lote, doc, msg);
	xmlFree(motivo);
	xmlFree(status);

	return cStat;
}
Exemple #3
0
static int sefaz_response_eventos(LOTE_EVENTO *lote, xmlDocPtr doc, char *msg){
	int i;
	LOTE_EVENTO_ITEM *it = lote->eventos;
	for (i = 0; i < lote->qtd; i++){
		char *status, *motivo;
		int cStat;
		EVENTO *e = it->evento;
		NFE *n = e->nfe;
		char *xp = malloc(sizeof(char) * 100);
		sprintf(xp, "nfe:retEvento/nfe:infEvento[nfe:chNFe='%s']/nfe:cStat", 
			n->idnfe->chave);
		status = get_xml_element(doc, xp);
		if(status == NULL){
			xmlFree(status);
			free(xp);
			return -ESEFAZ;	
		}
		cStat = atoi(status);
		xmlFree(status);
		e->cStat = cStat;

		sprintf(xp, "nfe:retEvento/nfe:infEvento[nfe:chNFe='%s']/nfe:xMotivo", 
			n->idnfe->chave);
		motivo = get_xml_element(doc, xp);
		strcat(msg, "\n");
		strcat(msg, n->idnfe->chave);
		strcat(msg, ": ");
		strcat(msg, motivo);
		e->xmot = strdup(motivo);
		xmlFree(motivo);
		if(cStat == 135 || cStat == 136){
			char *nProt;
			sprintf(xp, "nfe:retEvento/nfe:infEvento[nfe:chNFe='%s']/nfe:nProt", 
				n->idnfe->chave);
			nProt = get_xml_element(doc, xp);
			e->protocolo = strdup(nProt);
			sprintf(xp, "//nfe:retEvento/nfe:infEvento[nfe:chNFe='%s']/..", 
				n->idnfe->chave);
			char *xml_prot = get_xml_subtree(doc, xp);
			e->xml_response = strdup(xml_prot);
			n->canceled = 1;
			xmlFree(nProt);
			xmlFree(xml_prot);
		}
		it = it->next;
		free(xp);
	}
	return 0;
}
static int expect_element_end (
    xmlTextReaderPtr reader,
    char *exp_name)
{
    xmlChar *name;
    /* maybe we are already on the end element ... lets see */
    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT){
         xmlChar *temp;
         xmlChar *temp2;            
         temp = xmlTextReaderName(reader);
         temp2 = (xmlChar*)sprintf_alloc("/%s", temp);
         name = xmlStrdup(temp2);
         xmlFree(temp);
         free(temp2);            
    } else {     
         name = get_xml_element(reader);
    }

    if (name == NULL)
        return -1;    
    if (xmlStrcasecmp(name+1,(xmlChar *)exp_name) != 0 || name[0] != '/'){
        rrd_set_error("line %d: expected </%s> end element but found <%s>",
                      xmlTextReaderGetParserLineNumber(reader),exp_name,name);
        xmlFree(name);            
        return -1;            
    }
    xmlFree(name);    
    return 0;    
} /* expect_element_end */
Exemple #5
0
static int sefaz_response_protocolos(LOTE *lote, xmlDocPtr doc, char *msg){
	int i;
	LOTE_ITEM *it = lote->nfes;
	for (i = 0; i < lote->qtd; i++){
		char *status, *motivo;
		int cStat;
		NFE *n = it->nfe;
		char *xp = malloc(sizeof(char) * 100);
		sprintf(xp, "nfe:protNFe/nfe:infProt[nfe:chNFe='%s']/nfe:cStat", 
			n->idnfe->chave);
		status = get_xml_element(doc, xp);
		if(status == NULL){
			xmlFree(status);
			free(xp);
			return -ESEFAZ;	
		}
		cStat = atoi(status);
		xmlFree(status);
		n->protocolo->cod_status = cStat;

		sprintf(xp, "nfe:protNFe/nfe:infProt[nfe:chNFe='%s']/nfe:xMotivo", 
			n->idnfe->chave);
		motivo = get_xml_element(doc, xp);
		n->protocolo->xmot = strdup(motivo);
		strcat(msg, "\n");
		strcat(msg, n->idnfe->chave);
		strcat(msg, ": ");
		strcat(msg, motivo);
		xmlFree(motivo);
		if(cStat == 100){
			char *nProt;
			sprintf(xp, "nfe:protNFe/nfe:infProt[nfe:chNFe='%s']/nfe:nProt", 
				n->idnfe->chave);
			nProt = get_xml_element(doc, xp);
			n->protocolo->numero = strdup(nProt);
			sprintf(xp, "//nfe:protNFe/nfe:infProt[nfe:chNFe='%s']/..", 
				n->idnfe->chave);
			char *xml_prot = get_xml_subtree(doc, xp);
			n->protocolo->xml = strdup(xml_prot);
			xmlFree(nProt);
			xmlFree(xml_prot);
		}
		it = it->next;
		free(xp);
	}
	return 0;
}
/*
 * Parse root nodes
 */
static int parse_tag_rrd(
    xmlTextReaderPtr reader,
    rrd_t *rrd)
{
    int       status;
    xmlChar *element;
    
    status = 0;
    while ((element = get_xml_element(reader)) != NULL ){
        if (xmlStrcasecmp(element, (const xmlChar *) "version") == 0)
            status = get_xml_string(reader,
                                          rrd->stat_head->version,
                                          sizeof(rrd->stat_head->version));
        else if (xmlStrcasecmp(element, (const xmlChar *) "step") == 0)
            status = get_xml_ulong(reader,
                                        &rrd->stat_head->pdp_step);
        else if (xmlStrcasecmp(element, (const xmlChar *) "lastupdate") == 0) {
                status = get_xml_time_t(reader, &rrd->live_head->last_up);
        }
        else if (xmlStrcasecmp(element, (const xmlChar *) "ds") == 0){            
            xmlFree(element);
            status = parse_tag_ds(reader, rrd);
            /* as we come back the </ds> tag is already gone */
            if (status == 0)
                continue;
            else
                return status;
        }        
        else if (xmlStrcasecmp(element, (const xmlChar *) "rra") == 0){            
            xmlFree(element);
            status = parse_tag_rra(reader, rrd);
            if (status == 0)
                continue;
            else
                return status;
        }
        else if (xmlStrcasecmp(element, (const xmlChar *) "/rrd") == 0) {
            xmlFree(element);
            return status;
        }
        else {
            rrd_set_error("parse_tag_rrd: Unknown tag: %s", element);
            status = -1;
        }

        if (status != 0){
            xmlFree(element);
            break;
        }        
        status = expect_element_end(reader,(char *)element);
        xmlFree(element);        
        if (status != 0)
            break;        
    }
    return (status);
}                       /* int parse_tag_rrd */
Exemple #7
0
int send_lote(LOTE *lote, char *URL, int ambiente, int cuf, EVP_PKEY *key, 
		X509 *cert, char *msg){
	char *response, *status;
	int cStat, rc;
	xmlDocPtr doc;
	char *xml = gen_lote_xml(lote, key, cert);
	response = send_sefaz(SEFAZ_NFE_AUTORIZACAO, URL, ambiente, cuf, 
		xml, key, cert);
	if(response == NULL){
		strcpy(msg, "Sem resposta do SEFAZ, tente novamente");
		return -ESEFAZ;
	}
	doc = xmlReadMemory(response, strlen(response), "noname.xml", NULL, 0);
	status = get_xml_element(doc, "nfe:cStat");
	if(status == NULL){
		return -ESEFAZ;	
	}
	cStat = atoi(status);
	char *motivo = get_xml_element(doc, "nfe:xMotivo");
	if(motivo == NULL){
		return -ESEFAZ;	
	}
	strcpy(msg, motivo);
	char *nRec = get_xml_element(doc, "nfe:nRec");
	fprintf(stdout, "Lote: %s\n", nRec);
	lote->recibo = strdup(nRec);
	lote->xml_response = strdup(response);
	xmlFree(motivo);
	xmlFree(status);
	xmlFree(nRec);

	if(rc){
		sprintf(msg, "Erro ao salvar lote\nNúmero de recibo: %s",
			nRec);
		return -ESQL;
	}
	return cStat;
}
Exemple #8
0
int send_lote_evento(LOTE_EVENTO *lote, char *URL, int ambiente, int cuf, 
		EVP_PKEY *key, X509 *cert, char *msg){
	char *response, *status;
	int cStat, rc = 0;
	xmlDocPtr doc;
	char *xml = gen_lote_evento_xml(lote, key, cert);
	response = send_sefaz(SEFAZ_RECEPCAO_EVENTO, URL, ambiente, cuf, 
		xml, key, cert);
	if(response == NULL){
		strcpy(msg, "Sem resposta do SEFAZ, tente novamente");
		return -ESEFAZ;
	}
	lote->xml_response = response;
	doc = xmlReadMemory(response, strlen(response), "noname.xml", NULL, 0);
	status = get_xml_element(doc, "nfe:cStat");
	if(status == NULL){
		return -ESEFAZ;	
	}
	cStat = atoi(status);
	char *motivo = get_xml_element(doc, "nfe:xMotivo");
	if(motivo == NULL){
		return -ESEFAZ;	
	}
	strcpy(msg, motivo);

	if(cStat == 128){
		rc = sefaz_response_eventos(lote, doc, msg);
	}
	xmlFree(motivo);
	xmlFree(status);

	if(rc){
		strcpy(msg, "Erro ao enviar eventos");
		return -ESQL;
	}
	return cStat;
}
static int expect_element (
    xmlTextReaderPtr reader,
    char *exp_name)
{
    xmlChar *name;
    name = get_xml_element(reader);
    if (!name)
        return -1;    
    if (xmlStrcasecmp(name,(xmlChar *)exp_name) != 0){
        rrd_set_error("line %d: expected <%s> element but found <%s>",
                      xmlTextReaderGetParserLineNumber(reader),name,exp_name);
        xmlFree(name);            
        return -1;            
    }
    xmlFree(name);    
    return 0;    
} /* expect_element */
static int parse_tag_ds(
    xmlTextReaderPtr reader,
    rrd_t *rrd)
{
    int       status;
    xmlChar  *element;
    
    ds_def_t *cur_ds_def;
    pdp_prep_t *cur_pdp_prep;

    /*
     * If there are DS definitions after RRA definitions the number of values,
     * cdp_prep areas and so on will be calculated wrong. Thus, enforce a
     * specific order in this case.
     */
    if (rrd->stat_head->rra_cnt > 0) {
        rrd_set_error("parse_tag_ds: All data source definitions MUST "
                      "precede the RRA definitions!");
        return (-1);
    }

    /* Allocate space for the new DS definition */
    {                   /* {{{ */
        ds_def_t *temp;

        temp = (ds_def_t *) realloc(rrd->ds_def,
                                    sizeof(ds_def_t) *
                                    (rrd->stat_head->ds_cnt + 1));
        if (temp == NULL) {
            rrd_set_error("parse_tag_ds: malloc failed.");
            return (-1);
        }
        rrd->ds_def = temp;
        cur_ds_def = rrd->ds_def + rrd->stat_head->ds_cnt;
        memset(cur_ds_def, '\0', sizeof(ds_def_t));
    }                   /* }}} */

    /* Allocate pdp_prep space for the new DS definition */
    {                   /* {{{ */
        pdp_prep_t *temp;

        temp = (pdp_prep_t *) realloc(rrd->pdp_prep,
                                      sizeof(pdp_prep_t) *
                                      (rrd->stat_head->ds_cnt + 1));
        if (temp == NULL) {
            rrd_set_error("parse_tag_ds: malloc failed.");
            return (-1);
        }
        rrd->pdp_prep = temp;
        cur_pdp_prep = rrd->pdp_prep + rrd->stat_head->ds_cnt;
        memset(cur_pdp_prep, '\0', sizeof(pdp_prep_t));
    }                   /* }}} */

    /* All allocations successful, let's increment the number of DSes. */
    rrd->stat_head->ds_cnt++;

    status = 0;
    while ((element = get_xml_element(reader)) != NULL){
        if (xmlStrcasecmp(element, (const xmlChar *) "name") == 0){
            status = get_xml_string(reader,cur_ds_def->ds_nam,sizeof(cur_ds_def->ds_nam));
        }
        else if (xmlStrcasecmp(element, (const xmlChar *) "type") == 0)
            status = parse_tag_ds_type(reader, cur_ds_def);
        else if (xmlStrcasecmp(element,
                           (const xmlChar *) "minimal_heartbeat") == 0)
            status = get_xml_ulong(reader,
                                        &cur_ds_def->par[DS_mrhb_cnt].
                                       u_cnt);
        else if (xmlStrcasecmp(element, (const xmlChar *) "min") == 0)
            status = get_xml_double(reader,
                                          &cur_ds_def->par[DS_min_val].u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "max") == 0)
            status = get_xml_double(reader,
                                          &cur_ds_def->par[DS_max_val].u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "cdef") == 0)
            status = parse_tag_ds_cdef(reader, rrd);
        else if (xmlStrcasecmp(element, (const xmlChar *) "last_ds") == 0)
            status = get_xml_string(reader,
                                          cur_pdp_prep->last_ds,
                                          sizeof(cur_pdp_prep->last_ds));
        else if (xmlStrcasecmp(element, (const xmlChar *) "value") == 0)
            status = get_xml_double(reader,
                                          &cur_pdp_prep->scratch[PDP_val].
                                          u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "unknown_sec") == 0)
            status = get_xml_ulong(reader,
                                        &cur_pdp_prep->
                                       scratch[PDP_unkn_sec_cnt].u_cnt);
        else if (xmlStrcasecmp(element, (const xmlChar *) "/ds") == 0) {
            xmlFree(element);            
            break;
        }        
        else {
            rrd_set_error("parse_tag_ds: Unknown tag: %s", element);
            status = -1;
        }        
        if (status != 0) {            
            xmlFree(element);        
            break;
        }
        status = expect_element_end(reader,(char *)element);
        xmlFree(element);        
        if (status != 0)
            break;        
    }
    
    return (status);
}                       /* int parse_tag_ds */
static int parse_tag_rra(
    xmlTextReaderPtr reader,
    rrd_t *rrd)
{
    int       status;
    xmlChar *element;
    
    rra_def_t *cur_rra_def;
    cdp_prep_t *cur_cdp_prep;
    rra_ptr_t *cur_rra_ptr;

    /* Allocate more rra_def space for this RRA */
    {                   /* {{{ */
        rra_def_t *temp;

        temp = (rra_def_t *) realloc(rrd->rra_def,
                                     sizeof(rra_def_t) *
                                     (rrd->stat_head->rra_cnt + 1));
        if (temp == NULL) {
            rrd_set_error("parse_tag_rra: realloc failed.");
            return (-1);
        }
        rrd->rra_def = temp;
        cur_rra_def = rrd->rra_def + rrd->stat_head->rra_cnt;
        memset(cur_rra_def, '\0', sizeof(rra_def_t));
    }                   /* }}} */

    /* allocate cdp_prep_t */
    {                   /* {{{ */
        cdp_prep_t *temp;

        temp = (cdp_prep_t *) realloc(rrd->cdp_prep, sizeof(cdp_prep_t)
                                      * rrd->stat_head->ds_cnt
                                      * (rrd->stat_head->rra_cnt + 1));
        if (temp == NULL) {
            rrd_set_error("parse_tag_rra: realloc failed.");
            return (-1);
        }
        rrd->cdp_prep = temp;
        cur_cdp_prep = rrd->cdp_prep
            + (rrd->stat_head->ds_cnt * rrd->stat_head->rra_cnt);
        memset(cur_cdp_prep, '\0',
               sizeof(cdp_prep_t) * rrd->stat_head->ds_cnt);
    }                   /* }}} */

    /* allocate rra_ptr_t */
    {                   /* {{{ */
        rra_ptr_t *temp;

        temp = (rra_ptr_t *) realloc(rrd->rra_ptr,
                                     sizeof(rra_ptr_t) *
                                     (rrd->stat_head->rra_cnt + 1));
        if (temp == NULL) {
            rrd_set_error("parse_tag_rra: realloc failed.");
            return (-1);
        }
        rrd->rra_ptr = temp;
        cur_rra_ptr = rrd->rra_ptr + rrd->stat_head->rra_cnt;
        memset(cur_rra_ptr, '\0', sizeof(rra_ptr_t));
    }                   /* }}} */

    /* All space successfully allocated, increment number of RRAs. */
    rrd->stat_head->rra_cnt++;
    
    status = 0;
    while ((element = get_xml_element(reader)) != NULL){
        if (xmlStrcasecmp(element, (const xmlChar *) "cf") == 0)
            status = parse_tag_rra_cf(reader, cur_rra_def);
        else if (xmlStrcasecmp(element, (const xmlChar *) "pdp_per_row") == 0)
            status = get_xml_ulong(reader,
                                        &cur_rra_def->pdp_cnt);
        else if (atoi(rrd->stat_head->version) == 1
                 && xmlStrcasecmp(element, (const xmlChar *) "xff") == 0)
            status = get_xml_double(reader,
                                          (double *) &cur_rra_def->
                                          par[RRA_cdp_xff_val].u_val);
        else if (atoi(rrd->stat_head->version) >= 2
                 && xmlStrcasecmp(element, (const xmlChar *) "params") == 0){            
            xmlFree(element);
            status = parse_tag_rra_params(reader, cur_rra_def);
            if (status == 0)
                continue;
            else
                return status;
        }
        else if (xmlStrcasecmp(element, (const xmlChar *) "cdp_prep") == 0){
            xmlFree(element);
            status = parse_tag_rra_cdp_prep(reader, rrd, cur_cdp_prep);
            if (status == 0)
                continue;
            else
                return status;
        }        
        else if (xmlStrcasecmp(element, (const xmlChar *) "database") == 0){            
            xmlFree(element);
            status = parse_tag_rra_database(reader, rrd);
            if (status == 0)
                continue;
            else
                return status;
        }
        else if (xmlStrcasecmp(element,(const xmlChar *) "/rra") == 0){
            xmlFree(element);
            return status;
        }  /* }}} */        
       else {
            rrd_set_error("line %d: parse_tag_rra: Unknown tag: %s",
                          xmlTextReaderGetParserLineNumber(reader), element);
            status = -1;            
        }
        if (status != 0) {
            xmlFree(element);
            return status;
        }        
        status = expect_element_end(reader,(char *)element);
        xmlFree(element);
        if (status != 0) {
            return status;
        }        
    }    
    return (status);
}                       /* int parse_tag_rra */
/*
 * Parse the <params> block within an RRA definition
 */
static int parse_tag_rra_params(
    xmlTextReaderPtr reader,
    rra_def_t *rra_def)
{
    xmlChar *element;
    int       status;

    status = -1;
    while ((element = get_xml_element(reader)) != NULL){
        /*
         * Parameters for CF_HWPREDICT
         */
        if (xmlStrcasecmp(element, (const xmlChar *) "hw_alpha") == 0)
            status = get_xml_double(reader,
                                          &rra_def->par[RRA_hw_alpha].u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "hw_beta") == 0)
            status = get_xml_double(reader,
                                          &rra_def->par[RRA_hw_beta].u_val);
        else if (xmlStrcasecmp(element,
                           (const xmlChar *) "dependent_rra_idx") == 0)
            status = get_xml_ulong(reader,
                                        &rra_def->
                                       par[RRA_dependent_rra_idx].u_cnt);
        /*
         * Parameters for CF_SEASONAL and CF_DEVSEASONAL
         */
        else if (xmlStrcasecmp(element, (const xmlChar *) "seasonal_gamma") ==
                 0)
            status =
                get_xml_double(reader,
                                     &rra_def->par[RRA_seasonal_gamma].u_val);
        else if (xmlStrcasecmp
                 (element, (const xmlChar *) "seasonal_smooth_idx") == 0)
            status =
                get_xml_ulong(reader,
                                   &rra_def->
                                  par[RRA_seasonal_smooth_idx].u_cnt);
        else if (xmlStrcasecmp(element, (const xmlChar *) "smoothing_window")
                 == 0)
            status =
                get_xml_double(reader,
                                     &rra_def->
                                     par[RRA_seasonal_smoothing_window].
                                     u_val);
        /* else if (dependent_rra_idx) ...; */
        /*
         * Parameters for CF_FAILURES
         */
        else if (xmlStrcasecmp(element, (const xmlChar *) "delta_pos") == 0)
            status = get_xml_double(reader,
                                          &rra_def->par[RRA_delta_pos].u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "delta_neg") == 0)
            status = get_xml_double(reader,
                                          &rra_def->par[RRA_delta_neg].u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "window_len") == 0)
            status = get_xml_ulong(reader,
                                        &rra_def->par[RRA_window_len].
                                       u_cnt);
        else if (xmlStrcasecmp(element, (const xmlChar *) "failure_threshold")
                 == 0)
            status =
                get_xml_ulong(reader,
                                   &rra_def->
                                  par[RRA_failure_threshold].u_cnt);
        /*
         * Parameters for CF_AVERAGE, CF_MAXIMUM, CF_MINIMUM, and CF_LAST
         */
        else if (xmlStrcasecmp(element, (const xmlChar *) "xff") == 0)
            status = get_xml_double(reader,
                                          &rra_def->par[RRA_cdp_xff_val].
                                          u_val);
        /*
         * Compatibility code for 1.0.49
         */
        else if (xmlStrcasecmp(element, (const xmlChar *) "value") == 0) {  /* {{{ */
            unsigned int i = 0;

            for (i=0;i<ARRAY_LENGTH(rra_def->par);i++){
                if ((i == RRA_dependent_rra_idx)
                    || (i == RRA_seasonal_smooth_idx)
                    || (i == RRA_failure_threshold))
                    status = get_xml_ulong(reader,
                                                &rra_def->par[i].
                                               u_cnt);
                else
                    status = get_xml_double(reader,
                                                  &rra_def->par[i].u_val);

                if (status != 0)
                    break;
                if ( i-1 < ARRAY_LENGTH(rra_def->par)){
                    status = expect_element(reader,"/value");
                    if (status == 0){
                        status  = expect_element(reader,"value");
                    }
                }
                if (status != 0){
                    break;                    
                }
            }
        }  /* }}} */        
        else if (xmlStrcasecmp(element,(const xmlChar *) "/params") == 0){
            xmlFree(element);            
            return status;
        }  /* }}} */        
        else {
            rrd_set_error("line %d: parse_tag_rra_params: Unknown tag: %s",
                          xmlTextReaderGetParserLineNumber(reader),element);
            status = -1;
        }
        status = expect_element_end(reader,(char *)element);
        xmlFree(element);        
        if (status != 0)
            break;
    }
    return (status);
}                       /* int parse_tag_rra_params */
static int parse_tag_rra_cdp_prep_ds(
    xmlTextReaderPtr reader,
    rrd_t *rrd,
    cdp_prep_t *cdp_prep)
{
    int       status;
    xmlChar *element;
    memset(cdp_prep, '\0', sizeof(cdp_prep_t));

    status = -1;
    
    if (atoi(rrd->stat_head->version) == 1) {
        cdp_prep->scratch[CDP_primary_val].u_val = 0.0;
        cdp_prep->scratch[CDP_secondary_val].u_val = 0.0;
    }

    while ((element = get_xml_element(reader)) != NULL){
        if (xmlStrcasecmp(element, (const xmlChar *) "primary_value") == 0)
            status =
                get_xml_double(reader,&cdp_prep->scratch[CDP_primary_val].u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "secondary_value") == 0)
            status =
                get_xml_double(reader,&cdp_prep->scratch[CDP_secondary_val].u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "intercept") == 0)
            status = get_xml_double(reader,
                                          &cdp_prep->
                                          scratch[CDP_hw_intercept].u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "last_intercept") ==
                 0)
            status =
                get_xml_double(reader,
                                     &cdp_prep->
                                     scratch[CDP_hw_last_intercept].u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "slope") == 0)
            status = get_xml_double(reader,
                                    &cdp_prep->scratch[CDP_hw_slope].
                                    u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "last_slope") == 0)
            status = get_xml_double(reader,
                                    &cdp_prep->
                                    scratch[CDP_hw_last_slope].u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "nan_count") == 0)
            status = get_xml_ulong(reader,
                                   &cdp_prep->
                                   scratch[CDP_null_count].u_cnt);
        else if (xmlStrcasecmp(element, (const xmlChar *) "last_nan_count") ==
                 0)
            status =
                get_xml_ulong(reader,
                              &cdp_prep->
                              scratch[CDP_last_null_count].u_cnt);
        else if (xmlStrcasecmp(element, (const xmlChar *) "seasonal") == 0)
            status = get_xml_double(reader,
                                    &cdp_prep->scratch[CDP_hw_seasonal].
                                    u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "last_seasonal") ==
                 0)
            status =
                get_xml_double(reader,
                                     &cdp_prep->scratch[CDP_hw_last_seasonal].
                                     u_val);
        else if (xmlStrcasecmp(element, (const xmlChar *) "init_flag") == 0)
            status = get_xml_ulong(reader,
                                        &cdp_prep->
                                       scratch[CDP_init_seasonal].u_cnt);
        else if (xmlStrcasecmp(element, (const xmlChar *) "history") == 0)
            status = parse_tag_rra_cdp_prep_ds_history(reader, cdp_prep);
        else if (xmlStrcasecmp(element, (const xmlChar *) "value") == 0)
            status = get_xml_double(reader,
                                    &cdp_prep->scratch[CDP_val].u_val);
        else if (xmlStrcasecmp(element,
                           (const xmlChar *) "unknown_datapoints") == 0)
            status = get_xml_ulong(reader,
                                        &cdp_prep->
                                       scratch[CDP_unkn_pdp_cnt].u_cnt);
        else if (xmlStrcasecmp(element,
                               (const xmlChar *) "/ds") == 0){
            xmlFree(element);            
            break;
        }        
        else {
            rrd_set_error("parse_tag_rra_cdp_prep: Unknown tag: %s",
                          element);
            status = -1;
            xmlFree(element);            
            break;            
        }
        if (status != 0){
            xmlFree(element);
            break;
        }
        status = expect_element_end(reader,(char *)element);
        xmlFree(element);        
        if (status != 0)
            break;
    }    
    return (status);
}                       /* int parse_tag_rra_cdp_prep_ds */
static int parse_tag_rra_database(
    xmlTextReaderPtr reader,
    rrd_t *rrd )
{
    rra_def_t *cur_rra_def;
    rra_ptr_t *cur_rra_ptr;
    unsigned int total_row_cnt;
    int       status;
    int       i;
    xmlChar *element;
    unsigned int start_row_cnt;
    int       ds_cnt;
    
    ds_cnt = rrd->stat_head->ds_cnt;
    
    total_row_cnt = 0;
    for (i = 0; i < (((int) rrd->stat_head->rra_cnt) - 1); i++)
        total_row_cnt += rrd->rra_def[i].row_cnt;

    cur_rra_def = rrd->rra_def + i;
    cur_rra_ptr = rrd->rra_ptr + i;
    start_row_cnt = total_row_cnt;
    
    status = 0;
    while ((element = get_xml_element(reader)) != NULL){        
        if (xmlStrcasecmp(element,(const xmlChar *)"row") == 0){
           rrd_value_t *temp;
           rrd_value_t *cur_rrd_value;
           unsigned int total_values_count = rrd->stat_head->ds_cnt
               * (total_row_cnt + 1);

            /* Allocate space for the new values.. */
            temp = (rrd_value_t *) realloc(rrd->rrd_value,
                                           sizeof(rrd_value_t) *
                                           total_values_count);
            if (temp == NULL) {
                rrd_set_error("parse_tag_rra_database: realloc failed.");
                status = -1;
               break;
            }
            rrd->rrd_value = temp;
            cur_rrd_value = rrd->rrd_value
                + (rrd->stat_head->ds_cnt * total_row_cnt);
            memset(cur_rrd_value, '\0',
                   sizeof(rrd_value_t) * rrd->stat_head->ds_cnt);
            total_row_cnt++;
            cur_rra_def->row_cnt++;

            status =
                parse_tag_rra_database_row(reader, rrd, cur_rrd_value);
            if (status == 0)
                status =  expect_element(reader,"/row");
        } /* if (xmlStrcasecmp(element,"row")) */
        else {
            if ( xmlStrcasecmp(element,(const xmlChar *)"/database") == 0){
                xmlFree(element);                
                break;
            }
            else {
                rrd_set_error("line %d: found unexpected tag: %s",
                              xmlTextReaderGetParserLineNumber(reader),element);
                status = -1;
            }
        }
        xmlFree(element);        
        if (status != 0)
            break;        
    }
    
    /* Set the RRA pointer to a random location */
    cur_rra_ptr->cur_row = rrd_random() % cur_rra_def->row_cnt;
    
    /*
     * rotate rows to match cur_row...
     * 
     * this will require some extra temp. memory. We can do this rather 
     * brainlessly, because we have done all kinds of realloc before, 
     * so we messed around with memory a lot already.
     */
    
    /*
        
     What we want:
     
     +-start_row_cnt
     |           +-cur_rra_def->row_cnt
     |           |
     |a---------n012-------------------------|
    
   (cur_rra_def->row_cnt slots of ds_cnt width)
   
     What we have 
      
     |   
     |012-------------------------a---------n|
     
     Do this by:
     copy away 0..(a-1) to a temp buffer
     move a..n to start of buffer
     copy temp buffer to position after where we moved n to
     */
    
    int a = cur_rra_def->row_cnt - cur_rra_ptr->cur_row - 1;
    
    rrd_value_t *temp = malloc(ds_cnt * sizeof(rrd_value_t) * a);
    if (temp == NULL) {
        rrd_set_error("parse_tag_rra: malloc failed.");
        return -1;
    }

    rrd_value_t *start = rrd->rrd_value + start_row_cnt * ds_cnt;
    /* */            
    memcpy(temp, start,
            a * ds_cnt * sizeof(rrd_value_t));
    
    memmove(start,
            start + a * ds_cnt,
            (cur_rra_ptr->cur_row + 1) * ds_cnt * sizeof(rrd_value_t));
            
    memcpy(start + (cur_rra_ptr->cur_row + 1) * ds_cnt,
           temp,
           a * ds_cnt * sizeof(rrd_value_t));
            
    free(temp);

    return (status);
}                       /* int parse_tag_rra_database */
Exemple #15
0
static int parse_tag_rra_database(
    xmlTextReaderPtr reader,
    rrd_t *rrd )
{
    rra_def_t *cur_rra_def;
    unsigned int total_row_cnt;
    int       status;
    int       i;
    xmlChar *element;

    total_row_cnt = 0;
    for (i = 0; i < (((int) rrd->stat_head->rra_cnt) - 1); i++)
        total_row_cnt += rrd->rra_def[i].row_cnt;

    cur_rra_def = rrd->rra_def + i;

    status = 0;
    while ((element = get_xml_element(reader)) != NULL){        
        if (xmlStrcasecmp(element,(const xmlChar *)"row") == 0){
           rrd_value_t *temp;
           rrd_value_t *cur_rrd_value;
           unsigned int total_values_count = rrd->stat_head->ds_cnt
               * (total_row_cnt + 1);

            /* Allocate space for the new values.. */
            temp = (rrd_value_t *) realloc(rrd->rrd_value,
                                           sizeof(rrd_value_t) *
                                           total_values_count);
            if (temp == NULL) {
                rrd_set_error("parse_tag_rra_database: realloc failed.");
                status = -1;
               break;
            }
            rrd->rrd_value = temp;
            cur_rrd_value = rrd->rrd_value
                + (rrd->stat_head->ds_cnt * total_row_cnt);
            memset(cur_rrd_value, '\0',
                   sizeof(rrd_value_t) * rrd->stat_head->ds_cnt);
            total_row_cnt++;
            cur_rra_def->row_cnt++;

            status =
                parse_tag_rra_database_row(reader, rrd, cur_rrd_value);
            if (status == 0)
                status =  expect_element(reader,"/row");
        } /* if (xmlStrcasecmp(element,"row")) */
        else {
            if ( xmlStrcasecmp(element,(const xmlChar *)"/database") == 0){
                xmlFree(element);                
                break;
            }
            else {
                rrd_set_error("line %d: found unexpected tag: %s",
                              xmlTextReaderGetParserLineNumber(reader),element);
                status = -1;
            }
        }
        xmlFree(element);        
        if (status != 0)
            break;        
    }
    return (status);
}                       /* int parse_tag_rra_database */