void xml_parser_doc_to_memory(WsXmlDocH doc, char **buf, int *ptrSize, const char *encoding) { if (doc && buf && ptrSize) xmlDocDumpMemoryEnc(doc->parserDoc, (xmlChar **) buf, ptrSize, (!encoding) ? "UTF-8" : encoding); }
static gboolean app_uninstall(IRAPISession *session, const gchar *program, GError **error) { xmlChar *config = NULL; LPWSTR config_w = NULL; DWORD flags = CONFIG_PROCESS_DOCUMENT; LPWSTR reply_w = NULL; gchar *reply = NULL; HRESULT hr; xmlDocPtr doc = NULL; xmlNodePtr parent = NULL, node = NULL; gint config_size; doc = xmlNewDoc((xmlChar *)"1.0"); parent = xmlNewNode(NULL, (xmlChar *)"wap-provisioningdoc"); xmlDocSetRootElement(doc, parent); node = xmlNewNode(NULL, (xmlChar *)"characteristic"); xmlNewProp(node, (xmlChar *)"type", (xmlChar *)"UnInstall"); xmlAddChild(parent, node); parent = node; node = xmlNewNode(NULL, (xmlChar *)"characteristic"); xmlNewProp(node, (xmlChar *)"type", (xmlChar *)program); xmlAddChild(parent, node); parent = node; node = xmlNewNode(NULL, (xmlChar *)"parm"); xmlNewProp(node, (xmlChar *)"name", (xmlChar *)"uninstall"); xmlNewProp(node, (xmlChar *)"value", (xmlChar *)"1"); xmlAddChild(parent, node); xmlDocDumpMemoryEnc(doc, &config, &config_size, "utf-8"); xmlFreeDoc(doc); g_debug("%s: config doc: %s", G_STRFUNC, config); config_w = wstr_from_utf8((gchar *)config); g_free(config); hr = IRAPISession_CeProcessConfig(session, config_w, flags, &reply_w); wstr_free_string(config_w); if (SUCCEEDED(hr)) { wstr_free_string(reply_w); return TRUE; } reply = wstr_to_utf8(reply_w); g_debug("%s: reply doc: %s", G_STRFUNC, reply); g_set_error(error, SYNCE_APP_MAN_ERROR, SYNCE_APP_MAN_ERROR_RAPI, _("Failed to uninstall application: %s"), reply); g_free(reply); wstr_free_string(reply_w); return FALSE; }
std::string tiary::xml_make (const XMLNode *root) { /** * I have considered directly generating the XML text, which should * not be difficult to implement and obviously more efficient than * using libxml2. * * However, considering there are many details in XML that I may * easily forget, (say, always remember to replace special characters * with escape sequences starting with "&") * I decide to stick to libxml2. */ std::string ret; if (const XMLNodeTree *iroot = dynamic_cast <const XMLNodeTree *> (root)) { libxml2_init (); xmlDocPtr doc = xmlNewDoc (BAD_CAST "1.0"); // "1.0" - XML version xmlNodePtr oroot = xmlNewNode(0, BAD_CAST (iroot->name.c_str())); xmlDocSetRootElement(doc, oroot); // "(a,b) in stk" means "a's children should be copied as b's children" std::stack<std::pair<const XMLNodeTree *, xmlNodePtr>, std::vector<std::pair<const XMLNodeTree *, xmlNodePtr> > > stk; xmlNodePtr optr = oroot; // Current working output node const XMLNodeTree *iptr = iroot; // Current working input node for (;;) { // Shallow copy all children of current node for (XMLNode *child_ptr = iptr->children; child_ptr; child_ptr = child_ptr->next) { if (xmlNodePtr nptr = shallow_copy (child_ptr)) { xmlAddChild (optr, nptr); if (const XMLNodeTree *ip = dynamic_cast <const XMLNodeTree *> (child_ptr)) { stk.push (std::make_pair (ip, nptr)); } } } if (stk.empty ()) { break; } iptr = stk.top().first; optr = stk.top().second; stk.pop (); } xmlChar *str; int len; xmlDocDumpMemoryEnc (doc, &str, &len, "UTF-8"); xmlFreeDoc (doc); ret.assign ((const char*)str, len); xmlFree (str); } return ret; }
/** * nmp_add_new_node: add a new node * * @xml_in: input, pointer of xml text buffer * @len_in: iutput, xml text buffer length * @xml_out: output, pointer of buffer block, containing xml text * after deleting a node * @len_out: output, indicate buffer length of xml_out * @xpath: addressing parts of an XML document * @node_name: node name * @node_cont: node content * @return: succeed 0, else -1 */ int nmp_add_new_node( const char *xml_in, int len_in, xmlChar *xml_out, int *len_out, xmlChar *xpath, xmlChar *node_name, xmlChar *node_cont ) { ASSERT(xml_in != NULL && xml_out!= NULL && xpath != NULL); xmlDocPtr doc; xmlNodePtr node; xmlXPathObjectPtr app_result; int node_num;; xmlChar *xml_buffer; doc = xmlParseMemory(xml_in, len_in); //doc = xmlParseDoc(&xml_buff); if (doc == NULL ) { xml_error("Document not parsed successfully. \n"); return -1; } app_result = nmp_get_node(doc, (const xmlChar *)xpath); if (app_result == NULL) goto end; if (app_result) { xmlNodeSetPtr nodeset = app_result->nodesetval; node_num = nodeset->nodeNr - 1; node = xmlNewNode(NULL, BAD_CAST node_name); xmlNodeSetContent(node, (const xmlChar *)node_cont); xmlAddNextSibling ( nodeset->nodeTab[node_num],node); xmlSaveFormatFileEnc("-", doc, ECODE_UFT8, 1); xmlDocDumpMemoryEnc(doc, &xml_buffer, len_out,ECODE_UFT8); xml_debug("xml_buffer=%s\n", xml_buffer); memcpy(xml_out,xml_buffer,*len_out); xmlFree(xml_buffer); xmlXPathFreeObject(app_result); } end: xmlFreeDoc(doc); return 0; }
/** * nmp_modify_attr_value: modify attribute's value in XML document * * @xml_in: input, pointer of xml text buffer * @len_in: iutput, xml text buffer length * @xml_out: output, pointer of buffer block, containing xml text * after modifying attribute value * @len_out: output, indicate buffer length of xml_out * @xpath: addressing parts of an XML document * @attribute: XML element attribute * @attr_value: value of a specific attribute in XML document * @return: succeed 0, else -1 */ int nmp_modify_attr_value( const char *xml_in, int len_in, xmlChar *xml_out, int *len_out, xmlChar *xpath, xmlChar *attribute, xmlChar *attr_value ) { ASSERT(xml_out != NULL && attribute!=NULL && attr_value!= NULL); xmlDocPtr doc; xmlXPathObjectPtr app_result; int node_num;; xmlChar *xml_buffer; doc = xmlParseMemory(xml_in, len_in); //doc = xmlParseDoc(&xml_buff); if (doc == NULL ) { xml_error("Document not parsed successfully. \n"); return -1; } app_result = nmp_get_node(doc, (const xmlChar *)xpath); if (app_result == NULL) { goto end; } if (app_result) { xmlNodeSetPtr nodeset = app_result->nodesetval; //*value_num = nodeset->nodeNr; xml_debug(" nodeset->nodeNr=%d\n", nodeset->nodeNr); node_num = nodeset->nodeNr - 1; xmlSetProp(nodeset->nodeTab[0], BAD_CAST attribute, BAD_CAST attr_value); xmlSaveFormatFileEnc("-", doc, ECODE_UFT8, 1); xmlDocDumpMemoryEnc(doc, &xml_buffer, len_out,ECODE_UFT8); memcpy(xml_out, xml_buffer, *len_out); xmlFree(xml_buffer); xmlXPathFreeObject(app_result); } end: xmlFreeDoc(doc); return 0; }
char * epg_to_xmltv(const struct epg *epg) { xmlDocPtr doc = NULL; xmlDtdPtr dtd = NULL; xmlNodePtr root = NULL; xmlChar *s = NULL; error_if(epg == NULL, error, "Param Error"); LIBXML_TEST_VERSION; doc = xmlNewDoc((const xmlChar *)"1.0"); error_if(doc == NULL, error, "Error Creating xmlDoc"); root = xmlNewDocNode(doc, NULL, BAD_CAST "tv", NULL); error_if(root == NULL, error, "Error creating root node"); dtd = xmlCreateIntSubset(doc, BAD_CAST "tv", NULL, BAD_CAST "epg.dtd"); error_if(dtd == NULL, error, "Error adding DTD to xmlDoc"); xmlDocSetRootElement(doc, root); xmlNewProp(root, BAD_CAST "generator-info-name", BAD_CAST "tvz_epg - https://github.com/virgiliosanz/movistar_sv"); trace("%s", "Adding programmes"); _programmes_to_xml(root, epg->programmes); trace("%s", "Adding channels"); _channels_to_xml(root, epg->channels); trace("calling xmlDocDumpMemory chan: %zu prog: %zu", list_count(epg->channels), list_count(epg->programmes)); int size; xmlDocDumpMemoryEnc(doc, &s, &size, "UTF-8"); xmlCleanupParser(); return (char *)s; error: xmlCleanupParser(); if (s) free(s); return NULL; }
/** * nmp_del_node: delete a node from XML text * * @xml_in: input, pointer of xml text buffer * @len_in: iutput, xml text buffer length * @xml_out: output, pointer of buffer block, containing xml text * after deleting a node * @len_out: output, indicate buffer length of xml_out * @xpath: addressing parts of an XML document * @return: succeed 0, else -1 */ int nmp_del_node( const char *xml_in, int len_in, xmlChar *xml_out, int *len_out, xmlChar *xpath ) { ASSERT(xml_out != NULL); xmlDocPtr doc; xmlXPathObjectPtr app_result; int node_num;; xmlChar *xml_buffer; doc = xmlParseMemory(xml_in, len_in); //doc = xmlParseDoc(&xml_buff); if (doc == NULL ) { xml_error("Document not parsed successfully. \n"); return -1; } app_result = nmp_get_node(doc, (const xmlChar *)xpath); if (app_result == NULL) goto end; if (app_result) { xmlNodeSetPtr nodeset = app_result->nodesetval; node_num = nodeset->nodeNr - 1; xmlUnlinkNode(nodeset->nodeTab[node_num]); xmlFreeNode(nodeset->nodeTab[node_num]); xmlSaveFormatFileEnc("-", doc, ECODE_UFT8, 1); xmlDocDumpMemoryEnc(doc, &xml_buffer, len_out,ECODE_UFT8); memcpy(xml_out, xml_buffer, *len_out); xmlFree(xml_buffer); xmlXPathFreeObject(app_result); } end: xmlFreeDoc(doc); return 0; }
bool CSpmXml::SaveXML(QString& strXML, const QString& strCodec) { strXML.clear(); if (0 == m_pXMLDoc) return false; int len = 0; xmlChar* pDoc = 0; xmlDocDumpMemoryEnc(m_pXMLDoc, &pDoc, &len, strCodec.toLocal8Bit()); if ((0 == pDoc) || (0 == len)) return false; //strXML.fromLocal8Bit((const char*)pDoc); strXML = (const char*)pDoc; return true; }
string cXmlDoc::write(bool indent, const char* encoding) { if (!m_doc) throw eXmlInvalid("Tried to write bad document"); xmlChar* output; int outputlen; if (indent) { xmlDocDumpFormatMemoryEnc(m_doc.get(), &output, &outputlen, encoding, 1); } else { xmlDocDumpMemoryEnc(m_doc.get(), &output, &outputlen, encoding); } string ret; if (output) { ret = reinterpret_cast<char*>(output); xmlFree(output); } return ret; }
/** * nmp_create_xml_str: generate XML string according to sys_msg * * @xml_buf: output, memory address used to contain xml text * @sys_msg: input, struct, system message information * @return: succeed:lens of xml, else -1 */ int nmp_create_xml_str(NmpCmdType *self, char *xml_buff, int *buff_size, NmpMsgInfo *sys_msg) { ASSERT(self != NULL && xml_buff != NULL && buff_size != NULL && sys_msg != NULL); xmlDocPtr doc = NULL; xmlChar *xml_buffer = NULL; int size; doc = xmlNewDoc(BAD_CAST XML_VER); if(!doc) return -E_CREATEDOC; if ((size = nmp_create_xml(self, doc, sys_msg))) { goto end_create_xml; } xmlSaveFormatFileEnc( "-", doc, "UTF-8", 1); xmlDocDumpMemoryEnc(doc, &xml_buffer, &size, ECODE_UFT8); if(size > *buff_size) { *buff_size = size; size = -E_XML2LONG; if(xml_buffer) xmlFree(xml_buffer); goto end_create_xml; } memcpy(xml_buff, (char*)xml_buffer, size); if(xml_buffer) xmlFree(xml_buffer); end_create_xml: xmlFreeDoc(doc); return size; }
void wi_p7_message_serialize(wi_p7_message_t *p7_message, wi_p7_serialization_t serialization) { xmlDocPtr doc; xmlNsPtr ns; xmlNodePtr root_node, list_node, item_node; wi_p7_spec_field_t *field; wi_p7_spec_type_t *type; wi_runtime_instance_t *instance; wi_string_t *field_name, *field_value; unsigned char *buffer, *start; wi_uuid_t *uuid; wi_date_t *date; wi_p7_boolean_t p7_bool; wi_p7_int32_t p7_int32; wi_p7_uint32_t p7_uint32; wi_p7_int64_t p7_int64; wi_p7_uint64_t p7_uint64; wi_p7_double_t p7_double; wi_p7_oobdata_t p7_oobdata; wi_string_t *string; wi_data_t *data; wi_array_t *list; wi_uinteger_t i, count; uint32_t message_size, field_id, field_size; if(serialization == WI_P7_XML && !p7_message->xml_buffer) { doc = xmlNewDoc((xmlChar *) "1.0"); root_node = xmlNewNode(NULL, (xmlChar *) "message"); xmlDocSetRootElement(doc, root_node); ns = xmlNewNs(root_node, (xmlChar *) "http://www.zankasoftware.com/P7/Message", (xmlChar *) "p7"); xmlSetNs(root_node, ns); xmlSetProp(root_node, (xmlChar *) "name", (xmlChar *) wi_string_cstring(p7_message->name)); message_size = p7_message->binary_size - WI_P7_MESSAGE_BINARY_HEADER_SIZE; buffer = start = p7_message->binary_buffer + WI_P7_MESSAGE_BINARY_HEADER_SIZE; while((uint32_t) (buffer - start) < message_size) { field_id = wi_read_swap_big_to_host_int32(buffer, 0); buffer += sizeof(field_id); field = wi_p7_spec_field_with_id(p7_message->spec, field_id); if(!field) continue; field_size = wi_p7_spec_field_size(field); if(field_size == 0) { field_size = wi_read_swap_big_to_host_int32(buffer, 0); buffer += sizeof(field_size); } field_name = wi_p7_spec_field_name(field); field_value = NULL; type = wi_p7_spec_field_type(field); switch(wi_p7_spec_type_id(type)) { case WI_P7_BOOL: if(wi_p7_message_get_bool_for_name(p7_message, &p7_bool, field_name)) field_value = wi_string_with_format(WI_STR("%u"), p7_bool ? 1 : 0); break; case WI_P7_ENUM: string = wi_p7_message_enum_name_for_name(p7_message, field_name); if(string) field_value = string; break; case WI_P7_INT32: if(wi_p7_message_get_int32_for_name(p7_message, &p7_int32, field_name)) field_value = wi_string_with_format(WI_STR("%u"), p7_int32); break; case WI_P7_UINT32: if(wi_p7_message_get_uint32_for_name(p7_message, &p7_uint32, field_name)) field_value = wi_string_with_format(WI_STR("%u"), p7_uint32); break; case WI_P7_INT64: if(wi_p7_message_get_int64_for_name(p7_message, &p7_int64, field_name)) field_value = wi_string_with_format(WI_STR("%lld"), p7_int64); break; case WI_P7_UINT64: if(wi_p7_message_get_uint64_for_name(p7_message, &p7_uint64, field_name)) field_value = wi_string_with_format(WI_STR("%llu"), p7_uint64); break; case WI_P7_DOUBLE: if(wi_p7_message_get_double_for_name(p7_message, &p7_double, field_name)) field_value = wi_string_with_format(WI_STR("%f"), p7_double); break; case WI_P7_STRING: string = wi_p7_message_string_for_name(p7_message, field_name); if(string) field_value = string; break; case WI_P7_UUID: uuid = wi_p7_message_uuid_for_name(p7_message, field_name); if(uuid) field_value = wi_uuid_string(uuid); break; case WI_P7_DATE: date = wi_p7_message_date_for_name(p7_message, field_name); if(date) field_value = wi_string_with_format(WI_STR("%f"), wi_date_time_interval(date)); break; case WI_P7_DATA: data = wi_p7_message_data_for_name(p7_message, field_name); if(data) field_value = wi_data_base64(data); break; case WI_P7_OOBDATA: if(wi_p7_message_get_oobdata_for_name(p7_message, &p7_oobdata, field_name)) field_value = wi_string_with_format(WI_STR("%llu"), p7_oobdata); break; case WI_P7_LIST: list = wi_p7_message_list_for_name(p7_message, field_name); if(list) { list_node = wi_xml_node_child_with_name(root_node, field_name); if(!list_node) { list_node = xmlNewNode(ns, (xmlChar *) "field"); xmlSetProp(list_node, (xmlChar *) "name", (xmlChar *) wi_string_cstring(field_name)); xmlAddChild(root_node, list_node); } count = wi_array_count(list); for(i = 0; i < count; i++) { item_node = xmlNewNode(ns, (xmlChar *) "item"); instance = WI_ARRAY(list, i); if(wi_runtime_id(instance) == wi_string_runtime_id()) xmlNodeSetContent(item_node, (xmlChar *) wi_string_cstring(instance)); xmlAddChild(list_node, item_node); } } break; } if(field_value) { item_node = wi_xml_node_child_with_name(root_node, field_name); if(!item_node) { item_node = xmlNewNode(ns, (xmlChar *) "field"); xmlSetProp(item_node, (xmlChar *) "name", (xmlChar *) wi_string_cstring(field_name)); xmlAddChild(root_node, item_node); } xmlNodeSetContent(item_node, (xmlChar *) wi_string_cstring(field_value)); } buffer += field_size; } xmlDocDumpMemoryEnc(doc, &p7_message->xml_buffer, &p7_message->xml_length, "UTF-8"); xmlFreeDoc(doc); } }
/* *len_o 为字符串的长度 */ xml_handel xml_output_enc(const xml_body_ptr body, char **buf_o, int *len_o, const char *enc) { xmlDocPtr doc = NULL; xmlNodePtr n_body = NULL, n_table = NULL, n_item = NULL; xmlChar *buf; int len, i, j, k; xml_table_ptr table = NULL; xml_item_attr_ptr item_attr; xml_handel handle = NULL; assert(body); assert(buf_o); assert(len_o); handle = malloc(sizeof(struct xml_handel)); if ( NULL == handle ) { return NULL; } bzero(handle, sizeof(struct xml_handel)); /* 生成空的xml结构 */ doc = xmlNewDoc(NULL); n_body = xmlNewNode(NULL, CONST_CAST "Body"); xmlDocSetRootElement(doc, n_body); /* 加入body属性 */ s_xml_add_attr(n_body, "UnitCode", body->unit_code); s_xml_add_attr(n_body, "Code", body->code); s_xml_add_attr(n_body, "Description", body->desc); /* 加入table结点 */ for ( i = 0; i < body->t_cnt; i++ ) { n_table = xmlNewNode(NULL, CONST_CAST "Table"); table = body->t_list + i; /* 加入table属性 */ s_xml_add_attr(n_table, "Name", table->name); s_xml_add_attr(n_table, "Method", table->method); s_xml_add_attr(n_table, "Range", table->range); s_xml_add_attr(n_table, "CmdNo", table->cmd_no); s_xml_add_attr(n_table, "Description", table->desc); /* 加入item结点 */ for ( j = 0; j < table->it_cnt; j++ ) { n_item = xmlNewNode(NULL, CONST_CAST "Item"); item_attr = table->it_list[j].attr_list; /* 加入item属性 */ for ( k = 0; k < table->it_attr_cnt; k++ ) { s_xml_add_attr(n_item, item_attr[k].name, item_attr[k].value); } xmlAddChild(n_table, n_item); } xmlAddChild(n_body, n_table); } xmlDocDumpMemoryEnc(doc, &buf, &len, enc); handle->doc = doc; handle->buf = (char *)buf; *buf_o = strchr((char *)buf, '\n')+1; *len_o = len - (*buf_o - (char *)buf); return handle; }
static gboolean create_program_list(IRAPISession *session, GList **list, SynceAppManBusyFunc busy_func, gpointer busy_data, GError **error) { GList *prog_list = NULL; xmlDocPtr doc = NULL; xmlDocPtr reply_doc = NULL; xmlNodePtr parent = NULL; xmlNodePtr root_node = NULL, node = NULL, reply_node = NULL; xmlChar *doc_string = NULL; gint doc_size; LPWSTR config_w = NULL; LPWSTR reply_w = NULL; HRESULT result; gchar *reply = NULL; gchar *prop = NULL; doc = xmlNewDoc((xmlChar *)"1.0"); root_node = xmlNewNode(NULL, (xmlChar *)"wap-provisioningdoc"); xmlDocSetRootElement(doc, root_node); parent = root_node; node = xmlNewNode(NULL, (xmlChar *)"characteristic-query"); xmlNewProp(node, (xmlChar *)"recursive", (xmlChar *)"false"); xmlNewProp(node, (xmlChar *)"type", (xmlChar *)"UnInstall"); xmlAddChild(parent, node); xmlDocDumpMemoryEnc(doc, &doc_string, &doc_size, "utf-8"); g_debug("%s: CeProcessConfig request is \n%s", G_STRFUNC, doc_string); config_w = wstr_from_utf8((char *)doc_string); xmlFree(doc_string); xmlFreeDoc(doc); result = IRAPISession_CeProcessConfig(session, config_w, CONFIG_PROCESS_DOCUMENT, &reply_w); wstr_free_string(config_w); if (result != 0) { g_set_error(error, SYNCE_APP_MAN_ERROR, SYNCE_APP_MAN_ERROR_RAPI, _("Unable to obtain application list: %s"), synce_strerror(result)); return FALSE; } reply = wstr_to_utf8(reply_w); wstr_free_string(reply_w); reply_doc = xmlReadMemory(reply, strlen(reply), "reply.xml", NULL, 0); xmlDocDumpMemoryEnc(reply_doc, &doc_string, &doc_size, "utf-8"); g_debug("%s: CeProcessConfig response is \n%s", G_STRFUNC, doc_string); xmlFree(doc_string); reply_node = NULL; node = xmlDocGetRootElement(reply_doc); node = node->children; while(node) { if (node->type == XML_ELEMENT_NODE) { reply_node = node; } node = node->next; } if (!reply_node) { xmlFreeDoc(reply_doc); g_set_error(error, SYNCE_APP_MAN_ERROR, SYNCE_APP_MAN_ERROR_RAPI, _("Unable to obtain application list: Unexpected reply XML structure")); return FALSE; } node = reply_node->children; while (node) { prop = (gchar *)xmlGetProp(node, (xmlChar *)"type"); g_strstrip(prop); prog_list = g_list_append(prog_list, prop); node = node->next; } xmlFreeDoc(reply_doc); *list = prog_list; return TRUE; }
/* ############################################################################# * * Description encrypt and write our data to the given filename * Author Harry Brueckner * Date 2005-03-18 * Arguments char* filename - filename to write to * char** errormsg - pointer to the GpgMe error message, if any * PASSPHRASE_FN - passphrase callback function * SHOWERROR_FN - callback function for error messages * Return 1 on error, otherwise 0 */ int xmlDataFileWrite(char* filename, char** errormsg, PASSPHRASE_FN passphrase_cb, SHOWERROR_FN showerror_cb) { xmlNode* rootnode; xmlChar* xmlbuffer = NULL; int error = 0, fd, gpgsize = 0, size; char* buffer = NULL; char* gpgbuffer = NULL; char* tmpbuffer = NULL; /* we initialize the error message */ *errormsg = NULL; /* update the modification date of the root node */ rootnode = xmlDocGetRootElement(xmldoc); if (rootnode) { xmlSetModification(rootnode); } /* we create the memory buffer */ xmlDocDumpMemoryEnc(xmldoc, &xmlbuffer, &size, config -> encoding); buffer = (char*)xmlbuffer; if (buffer && !error && config -> encryptdata) { /* we have a buffer and must compress it */ error = zlibCompress(buffer, size, &gpgbuffer, &gpgsize, errormsg); if (error) { tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH); snprintf(tmpbuffer, STDBUFFERLENGTH, _("error (%s) compressing file '%s'."), *errormsg, filename); showerror_cb(_("compression error"), tmpbuffer); memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH); memFree(__FILE__, __LINE__, buffer, size); return 1; } xmlFree(buffer); buffer = gpgbuffer; size = gpgsize; } else if (buffer && !error && !config -> encryptdata) { /* we don't have to encrypt data, so nothing is compressed */ gpgbuffer = memAlloc(__FILE__, __LINE__, size); /* Flawfinder: ignore */ memcpy(gpgbuffer, buffer, size); xmlFree(buffer); buffer = gpgbuffer; } if (buffer && !error && config -> encryptdata) { error = gpgEncrypt(buffer, size, &gpgbuffer, &gpgsize, passphrase_cb, showerror_cb); if (error) { *errormsg = _("could not encrypt database file."); } memFree(__FILE__, __LINE__, buffer, size); buffer = gpgbuffer; size = gpgsize; } else { showerror_cb(_("warning"), _("the database file is written in unecrypted mode.")); } if (buffer && !error) { /* if we have a buffer we write the file */ createBackupfile(filename, showerror_cb); fd = fileLockOpen(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, &tmpbuffer); if (fd == -1) { /* error opening the file */ showerror_cb(_("file error"), tmpbuffer); memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH); *errormsg = strerror(errno); memFree(__FILE__, __LINE__, buffer, size); return 1; } if (write(fd, buffer, size) != size) { /* error writing the file */ tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH); snprintf(tmpbuffer, STDBUFFERLENGTH, _("error %d (%s) writing file '%s'."), errno, strerror(errno), filename); showerror_cb(_("file error"), tmpbuffer); memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH); *errormsg = strerror(errno); memFree(__FILE__, __LINE__, buffer, size); return 1; } lockf(fd, F_UNLCK, 0L); close(fd); } if (buffer && size) { memFree(__FILE__, __LINE__, buffer, size); } return error; }
int main(void) { initialize(); while (FCGI_Accept() >= 0) { query_name = getenv("SQL_NAME"); /* Environment variables */ env_node = xmlNewChild(root_node, NULL, BAD_CAST "ENV", NULL); xmlNewChild(env_node, NULL, BAD_CAST "SERVER_HOSTNAME", BAD_CAST getenv("SERVER_HOSTNAME")); xmlNewChild(env_node, NULL, BAD_CAST "SCRIPT_FILENAME", BAD_CAST getenv("SCRIPT_FILENAME")); xmlNewChild(env_node, NULL, BAD_CAST "QUERY_STRING", BAD_CAST qEncodeUrl(getenv("SCRIPT_FILENAME"))); xmlNewChild(env_node, NULL, BAD_CAST "REQUEST_METHOD", BAD_CAST getenv("REQUEST_METHOD")); xmlNewChild(env_node, NULL, BAD_CAST "CONTENT_TYPE", BAD_CAST getenv("CONTENT_TYPE")); xmlNewChild(env_node, NULL, BAD_CAST "CONTENT_LENGTH", BAD_CAST getenv("CONTENT_LENGTH")); /* Get variables */ get_node = xmlNewChild(root_node, NULL, BAD_CAST "GET", NULL); /* Parse queries. */ qget = qCgiRequestParseQueries(NULL, "GET"); qnum = qEntryGetNum(qget); /* Create children */ for(i=0; i<qnum; i++) { qname = (char *)qEntryGetName(qget); qvalue = (char *)qEntryGetValue(qget); xmlNewChild(get_node, NULL, BAD_CAST qname, BAD_CAST qvalue); } /* Post variables */ post_node = xmlNewChild(root_node, NULL, BAD_CAST "POST", NULL); /* Parse queries. */ qget = qCgiRequestParseQueries(NULL, "POST"); qnum = qEntryGetNum(qget); /* Create children */ for(i=0; i<qnum; i++) { qname = (char *)qEntryGetName(qget); qvalue = (char *)qEntryGetValue(qget); xmlNewChild(post_node, NULL, BAD_CAST qname, BAD_CAST qvalue); } /* Cookie variables */ cookie_node = xmlNewChild(root_node, NULL, BAD_CAST "COOKIE", NULL); /* Parse queries. */ qget = qCgiRequestParseCookies(NULL); qnum = qEntryGetNum(qget); /* Create children */ for(i=0; i<qnum; i++) { qname = (char *)qEntryGetName(qget); qvalue = (char *)qEntryGetValue(qget); xmlNewChild(cookie_node, NULL, BAD_CAST qname, BAD_CAST qvalue); } sql_node = xmlNewChild(root_node, NULL, BAD_CAST "SQL", NULL); myq = getenv("SQL_QUERY"); query_handler(query_name,myq); /* Output XML */ mylen = xmlStrlen(myxml); xmlDocDumpMemoryEnc(doc, &myxml, &mylen, "UTF-8"); printf("Content-type: application/xhtml+xml\r\n\r\n%s",myxml); /* Free everything under the root */ xmlUnlinkNode(env_node); xmlFreeNode(env_node); xmlUnlinkNode(get_node); xmlFreeNode(get_node); xmlUnlinkNode(post_node); xmlFreeNode(post_node); xmlUnlinkNode(cookie_node); xmlFreeNode(cookie_node); xmlUnlinkNode(sql_node); xmlFreeNode(sql_node); } xmlFreeDoc(doc); xmlCleanupParser(); return 0; }