static inline int cr_sqlite3_bind_text(sqlite3_stmt *stmt, int i, const char *orig_content, int len, void(*desctructor)(void *)) { int ret; int free_content = 0; unsigned char *content; if (!orig_content) { content = (unsigned char *) orig_content; } else if (xmlCheckUTF8((const unsigned char *) orig_content) && !hascontrollchars((const unsigned char *) orig_content)) { content = (unsigned char *) orig_content; } else { desctructor = SQLITE_TRANSIENT; size_t llen = strlen((const char *) orig_content); content = malloc(sizeof(unsigned char)*llen*2 + 1); cr_latin1_to_utf8((const unsigned char *) orig_content, content); free_content = 1; } ret = sqlite3_bind_text(stmt, i, (char *) content, len, desctructor); if (free_content) free(content); return ret; }
xmlNodePtr cr_xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, const xmlChar *name, const xmlChar *orig_content) { int free_content = 0; xmlChar *content; xmlNodePtr child; if (!orig_content) { content = BAD_CAST ""; } else if (xmlCheckUTF8(orig_content)) { content = (xmlChar *) orig_content; } else { size_t len = strlen((const char *) orig_content); content = malloc(sizeof(xmlChar)*len*2 + 1); cr_latin1_to_utf8(orig_content, content); free_content = 1; } child = xmlNewTextChild(parent, ns, name, content); if (free_content) free(content); return child; }
xmlAttrPtr cr_xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *orig_content) { int free_content = 0; xmlChar *content; xmlAttrPtr attr; if (!orig_content) { content = BAD_CAST ""; } else if (xmlCheckUTF8(orig_content)) { content = (xmlChar *) orig_content; } else { size_t len = strlen((const char *) orig_content); content = malloc(sizeof(xmlChar)*len*2 + 1); cr_latin1_to_utf8(orig_content, content); free_content = 1; } attr = xmlNewProp(node, name, content); if (free_content) free(content); return attr; }
/* simple name=tag stat create/update */ void stats_event(const char *source, const char *name, const char *value) { stats_event_t event; if (value && xmlCheckUTF8 ((unsigned char *)value) == 0) { WARN3 ("seen non-UTF8 data (%s), probably incorrect metadata (%s, %s)", source?source:"global", name, value); return; } build_event (&event, source, name, (char *)value); process_event (&event); }
/* simple name=tag stat create/update */ void stats_event(const char *source, const char *name, const char *value) { stats_event_t *event; if (value && xmlCheckUTF8 ((unsigned char *)value) == 0) { ICECAST_LOG_WARN("seen non-UTF8 data, probably incorrect metadata (%s, %s)", name, value); return; } event = build_event(source, name, value); if (event) queue_global_event(event); }
/** * Check if a string is an UTF-8. * If it is, a new string, copy of @in, is returned. * If not, a conversion is made from ISO Latin 1 to UTF-8 and the new string is * returned. * That's because an enconding detection is not performed and the former has * a large use in web. * This routine is intended to make use of the same variable whether it was * converted or not. * * Input * @in: a pointer to an array of ISO Latin 1 chars * Output * - * Returns: * NULL in case of error * A new string if success * * Notes: Returned string may be freed by the caller if != NULL * * use: * char *str_in, *str_out; * str_out = fixCgiStr (str_in); */ xmlChar *fixCgiStr (xmlChar *in) { int utf8; xmlChar *out = NULL; if (in == NULL) return NULL; utf8 = xmlCheckUTF8 (in); if (!utf8) { if (convertISO88591toUTF8(in, &out) < 0) return NULL; return (out); } return (xmlStrdup(in)); }
/** * exsltStrDecodeUriFunction: * @ctxt: an XPath parser context * @nargs: the number of arguments * * reverses URI-Escaping of a string */ static void exsltStrDecodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) { int str_len = 0; xmlChar *str = NULL, *ret = NULL, *tmp; if ((nargs < 1) || (nargs > 2)) { xmlXPathSetArityError(ctxt); return; } if (nargs >= 2) { /* check for UTF-8 if encoding was explicitly given; we don't support anything else yet */ tmp = xmlXPathPopString(ctxt); if (xmlUTF8Strlen(tmp) != 5 || xmlStrcmp((const xmlChar *)"UTF-8",tmp)) { xmlXPathReturnEmptyString(ctxt); xmlFree(tmp); return; } xmlFree(tmp); } str = xmlXPathPopString(ctxt); str_len = xmlUTF8Strlen(str); if (str_len == 0) { xmlXPathReturnEmptyString(ctxt); xmlFree(str); return; } ret = (xmlChar *) xmlURIUnescapeString((const char *)str,0,NULL); if (!xmlCheckUTF8(ret)) { /* FIXME: instead of throwing away the whole URI, we should only discard the invalid sequence(s). How to do that? */ xmlXPathReturnEmptyString(ctxt); xmlFree(str); xmlFree(ret); return; } xmlXPathReturnString(ctxt, ret); if (str != NULL) xmlFree(str); }
void NormalizeString (DPL::Optional<DPL::String>& txt) { if (!!txt) { std::string tmp = DPL::ToUTF8String(*txt); const xmlChar* str = reinterpret_cast<const xmlChar*>(tmp.c_str()); if (!xmlCheckUTF8(str)) { LogError("Not valid UTF8"); return; } int i = 0; xmlChar* c; while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) { if (!IsSpace(c)) { break; } ++i; } xmlChar* tmpnew = xmlUTF8Strndup(c, xmlUTF8Strlen(c) + 1); bool first = false; xmlChar* s = tmpnew; while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) { if (IsSpace(c)) { first = true; ++i; } else { if (c[0] == 0x0) { break; } if (first) { xmlChar space[6] = { 0x20 }; CopyChar(s, space); s += xmlUTF8Size(s); first = false; } CopyChar(s, c); s += xmlUTF8Size(s); ++i; } } s[0] = 0x0; txt = DPL::FromUTF8String(reinterpret_cast<char*>(tmpnew)); xmlFree(tmpnew); } }
void stats_listener_to_xml (client_t *listener, xmlNodePtr parent) { const char *useragent; char buf[30]; xmlNodePtr node = xmlNewChild (parent, NULL, XMLSTR("listener"), NULL); snprintf (buf, sizeof (buf), "%lu", listener->connection.id); xmlSetProp (node, XMLSTR("id"), XMLSTR(buf)); xmlNewChild (node, NULL, XMLSTR("IP"), XMLSTR(listener->connection.ip)); useragent = httpp_getvar (listener->parser, "user-agent"); if (useragent && xmlCheckUTF8((unsigned char *)useragent)) { xmlChar *str = xmlEncodeEntitiesReentrant (parent->doc, XMLSTR(useragent)); xmlNewChild (node, NULL, XMLSTR("UserAgent"), str); xmlFree (str); } if ((listener->flags & (CLIENT_ACTIVE|CLIENT_IN_FSERVE)) == CLIENT_ACTIVE) { source_t *source = listener->shared_data; snprintf (buf, sizeof (buf), "%"PRIu64, source->client->queue_pos - listener->queue_pos); } else snprintf (buf, sizeof (buf), "0"); xmlNewChild (node, NULL, XMLSTR("lag"), XMLSTR(buf)); if (listener->worker) { snprintf (buf, sizeof (buf), "%lu", (unsigned long)(listener->worker->current_time.tv_sec - listener->connection.con_time)); xmlNewChild (node, NULL, XMLSTR("Connected"), XMLSTR(buf)); } if (listener->username) { xmlChar *str = xmlEncodeEntitiesReentrant (parent->doc, XMLSTR(listener->username)); xmlNewChild (node, NULL, XMLSTR("username"), str); xmlFree (str); } }
int main (void) { /* general declarations */ char *pid, *formName; char xmlFilePath [FILE_NAME_MAX]; char tempFile[] = "xml/pacientesTemp.xml"; int rc, utf8; boolean found_patient, found_form; /* date and time declarations */ /*time_t rawtime; struct tm *timeinfo; int day, month, year, hour, min;*/ /* libcgi declarations */ formvars *first_input, *input; /* libxml2 declarations */ xmlChar *strUTF; xmlDocPtr doc; xmlNodePtr root_element, cur_node, new_form, new_node, right_patient; /* * this initialize the library and check potential ABI mismatches * between the version it was compiled for and the actual shared * library used. */ LIBXML_TEST_VERSION /****************************************************************************** * GET CURRENT DATE AND TIME * ******************************************************************************/ /* time(&rawtime); timeinfo = localtime(&rawtime); day = timeinfo->tm_mday; month = timeinfo->tm_mon +1; year = timeinfo->tm_year +1900; hour = timeinfo->tm_hour; min = timeinfo->tm_min; *//* date and time are being entered manually */ cgi_init(); /****************************************************************************** * READ CONTENT STRING FROM SERVER. * * PROCESS DATA. * * CREATE A LINKED LIST. * ******************************************************************************/ first_input = cgi_process_form(); formName = cgi_param("form"); pid = cgi_param("pid"); if ((!pid) || (!formName)) { printError("ID do paciente e nome do formulário não foram enviados"); usualFreeMemory(NULL); exit(0); } /****************************************************************************** * OPENING AND PARSING AN XML FILE TO A TREE * ******************************************************************************/ //strcpy (healthCenter, "Guadalupe"); //snprintf (xmlFilePath, FILE_NAME_MAX, "xml/pacientes%s.xml", healthCenter); strcpy (xmlFilePath, "xml/pacientesGuadalupe.xml"); doc = xmlReadFile(xmlFilePath, NULL, 256); /* 256 = remove blank nodes */ if (doc == NULL) { printError("Failed to parse doc"); usualFreeMemory(NULL); exit(0); } /****************************************************************************** * SEARCH PATIENT * ******************************************************************************/ root_element = xmlDocGetRootElement(doc); cur_node = root_element->children; /* Get the node <paciente> if file isn't empty */ if (!cur_node) //test!!!! printf!! pode-se tb na remocao exigir a remocao do arquivo se o paciente excluido for o unico do arquivo found_patient = false; else { /* looping through patients looking for the right patient */ for (found_patient = false; ((!found_patient) && (cur_node)); cur_node = cur_node->next) { cur_node = cur_node->children; /* <triagem> */ cur_node = cur_node->children; /* <numeroGeral> ? */ while ((!xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) && (cur_node)) cur_node = cur_node->next; if (xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) { if (xmlStrEqual(cur_node->children->content, BAD_CAST pid)) { //MANDAR IMPRIMIR o CONTEUDO VAZIO DE UM NUMERO GERAL PARA TESTE! found_patient = true; right_patient = cur_node->parent->parent; } else { cur_node = cur_node->parent; /* <triagem> */ cur_node = cur_node->parent; /* <paciente> */ } } else { printError("Erro de formação do XML. Tem paciente sem número geral."); usualFreeMemory(doc); exit(0); } } } /****************************************************************************** * CHECK IF PATIENT WAS FOUND * * IF TRUE, CONVERT FORM NAME INPUT TO UTF-8 IF NECESSARY * ******************************************************************************/ if (found_patient) { utf8 = xmlCheckUTF8 (BAD_CAST formName); if (!utf8) { rc = convertISO88591toUTF8 ((unsigned char *)formName, &strUTF); if (rc < 0) { printError("Erro convertendo valor para UTF-8"); usualFreeMemory(doc); free(strUTF); exit(0); } formName = (char *)strUTF; } /****************************************************************************** * CHECK IF FORM EXISTS * ******************************************************************************/ cur_node = right_patient->children; found_form = false; while ((cur_node) && (!found_form)) { if ((xmlStrEqual(BAD_CAST formName, cur_node->name)) && (strcmp(formName, "followUp"))) found_form = true; cur_node = cur_node->next; } if (found_form) { printError("O paciente já possui este formulário"); usualFreeMemory(doc); free(strUTF); exit(0); } /****************************************************************************** * ADD NEW FORM * ******************************************************************************/ new_form = xmlNewNode (NULL, BAD_CAST formName); free(strUTF);//frees formName for (input = first_input; input; input = input->next) { while ((!strcmp(input->name,"form")) || (!strcmp(input->name,"pid"))) input = input->next; /* Validate tag name input against UTF-8 */ utf8 = xmlCheckUTF8 (BAD_CAST input->name); if (!utf8) { rc = convertISO88591toUTF8 ((unsigned char *)input->name, &strUTF); /** the code above is equiv to the code below inlen = strlen(input->name); lenUTF = 2 * inlen; strUTF = BAD_CAST malloc ((size_t) (lenUTF + 1)); rc = isolat1ToUTF8 (strUTF, &lenUTF, input->name, &inlen); **/ if (rc < 0) { printError("Erro convertendo valor para UTF-8"); usualFreeMemory(doc); free(strUTF); exit(0); } new_node = xmlNewNode (NULL, strUTF); free(strUTF); } else new_node = xmlNewNode (NULL, BAD_CAST input->name); /* Validate tag value input against UTF-8 */ utf8 = xmlCheckUTF8 (BAD_CAST input->value); if (!utf8) { rc = convertISO88591toUTF8 ((unsigned char *)input->value, &strUTF); if (rc < 0) { printError("Erro convertendo valor para UTF-8"); usualFreeMemory(doc); free(strUTF); exit(0); } xmlNodeAddContent (new_node, strUTF); free(strUTF); } else xmlNodeAddContent (new_node, BAD_CAST input->value); xmlAddChild (new_form, new_node); } xmlAddChild (right_patient, new_form); /****************************************************************************** * DUMPING DOCUMENT TO FILE * ******************************************************************************/ if ((xmlSaveFormatFileEnc(tempFile, doc, "ISO-8859-1", 1)) < 0) { remove(tempFile); printError("Erro ao salvar arquivo"); usualFreeMemory(doc); exit(0); } remove(xmlFilePath); if (rename(tempFile, xmlFilePath)) { printError("Erro ao renomear o arquivo atualizado"); usualFreeMemory(doc); exit(0); } } else { printError("O paciente não tem registro no sistema"); usualFreeMemory(doc); exit(0); } /****************************************************************************** * FREE MEMORY AND EXIT * ******************************************************************************/ usualFreeMemory(doc); printError("Fomulário adicionado com sucesso"); return (0); }