static WBXMLVersion get_version(const WB_TINY *lang) { if (WBXML_STRCMP(lang, "1.0") == 0) return WBXML_VERSION_10; if (WBXML_STRCMP(lang, "1.1") == 0) return WBXML_VERSION_11; if (WBXML_STRCMP(lang, "1.2") == 0) return WBXML_VERSION_12; if (WBXML_STRCMP(lang, "1.3") == 0) return WBXML_VERSION_13; return WBXML_VERSION_UNKNOWN; }
WBXML_DECLARE(WBXMLTreeNode *) wbxml_tree_node_elt_get_from_name(WBXMLTreeNode *node, const char *name, WB_BOOL recurs) { WBXMLTreeNode *current_node = NULL; WB_BOOL node_found = FALSE; if ((node == NULL) || (name == NULL)) return NULL; /** @todo Handle 'recurs' TRUE */ /* Let's go through the tree */ current_node = node; while (current_node != NULL) { /* Is this the Node we searched ? */ if ((current_node->type == WBXML_TREE_ELEMENT_NODE) && (WBXML_STRCMP(wbxml_tag_get_xml_name(current_node->name), name) == 0)) { node_found = TRUE; break; } else { /* Go to next Sibbling Node */ current_node = current_node->next; } } if (node_found) return current_node; return NULL; }
const WBXMLLangEntry * wbxml_tables_search_table(const WBXMLLangEntry *main_table, const WB_UTINY *public_id, const WB_UTINY *system_id, const WB_UTINY *root) { WB_ULONG index; if (main_table == NULL) return NULL; /* Search by XML Public ID */ if (public_id != NULL) { index = 0; while (main_table[index].publicID != NULL) { if (main_table[index].publicID->xmlPublicID && WBXML_STRCASECMP((const WB_TINY *)main_table[index].publicID->xmlPublicID, (const WB_TINY *)public_id) == 0) return &main_table[index]; index++; } } /* Search by XML System ID */ if (system_id != NULL) { index = 0; while (main_table[index].publicID != NULL) { if (main_table[index].publicID->xmlDTD && WBXML_STRCMP((const WB_TINY *)main_table[index].publicID->xmlDTD, (const WB_TINY *)system_id) == 0) return &main_table[index]; index++; } } /* Search by XML Root Element */ if (root != NULL) { index = 0; while (main_table[index].publicID != NULL) { if (main_table[index].publicID->xmlRootElt && WBXML_STRCMP((const WB_TINY *)main_table[index].publicID->xmlRootElt, (const WB_TINY *)root) == 0) return &main_table[index]; index++; } } return NULL; }
const WBXMLExtValueEntry * wbxml_tables_get_ext_from_xml(const WBXMLLangEntry *lang_table, WB_UTINY *xml_value) { WB_ULONG i = 0; if ((lang_table == NULL) || (lang_table->extValueTable == NULL) || (xml_value == NULL)) return NULL; while (lang_table->extValueTable[i].xmlName != NULL) { if (WBXML_STRCMP((const WB_TINY *)lang_table->extValueTable[i].xmlName, (const WB_TINY *)xml_value) == 0) return &(lang_table->extValueTable[i]); i++; } return NULL; }
const WBXMLTagEntry * wbxml_tables_get_tag_from_xml(const WBXMLLangEntry *lang_table, const WB_UTINY *xml_name) { WB_ULONG i = 0; if ((lang_table == NULL) || (lang_table->tagTable == NULL) || (xml_name == NULL)) return NULL; while (lang_table->tagTable[i].xmlName != NULL) { if (WBXML_STRCMP((const WB_TINY *)lang_table->tagTable[i].xmlName, (const WB_TINY *)xml_name) == 0) return &(lang_table->tagTable[i]); i++; } return NULL; }
WBXML_DECLARE(WBXMLTreeNode *) wbxml_tree_node_elt_get_from_name(WBXMLTreeNode *node, const char *name, WB_BOOL recurs) { WBXMLTreeNode *current_node = NULL; WBXMLTreeNode *recurs_node = NULL; if ((node == NULL) || (name == NULL)) return NULL; /* Let's go through the tree */ current_node = node; while (current_node != NULL) { /* Is this a normal node? */ if (current_node->type == WBXML_TREE_ELEMENT_NODE) { /* Is this the Node we searched ? */ if (WBXML_STRCMP(wbxml_tag_get_xml_name(current_node->name), name) == 0) { return current_node; } /* Sould we start a recursive search? */ if (recurs && current_node->children) { recurs_node = wbxml_tree_node_elt_get_from_name(current_node->children, name, TRUE); /* Is this the Node we searched ? */ if (recurs_node) { return recurs_node; } } } /* Go to next Sibbling Node */ current_node = current_node->next; } /* A node with the specified name could not be found. */ return NULL; }
WBXML_DECLARE(WBXMLSyncMLDataType) wbxml_tree_node_get_syncml_data_type(WBXMLTreeNode *node) { WBXMLTreeNode *tmp_node = NULL; if (node == NULL) return WBXML_SYNCML_DATA_TYPE_NORMAL; /* Are we in a <Data> ? */ if ((node->type == WBXML_TREE_ELEMENT_NODE) && (node->name != NULL) && (WBXML_STRCMP(wbxml_tag_get_xml_name(node->name), "Data") == 0)) { /* Go to Parent element (or Parent of Parent) and search for <Meta> then <Type> */ if (((node->parent != NULL) && (node->parent->children != NULL) && ((tmp_node = wbxml_tree_node_elt_get_from_name(node->parent->children, "Meta", FALSE)) != NULL) && ((tmp_node = wbxml_tree_node_elt_get_from_name(tmp_node->children, "Type", FALSE)) != NULL)) || (((node->parent != NULL) && (node->parent->parent != NULL) && (node->parent->parent->children != NULL) && ((tmp_node = wbxml_tree_node_elt_get_from_name(node->parent->parent->children, "Meta", FALSE)) != NULL)) && ((tmp_node = wbxml_tree_node_elt_get_from_name(tmp_node->children, "Type", FALSE)) != NULL))) { /* Check <Type> value */ if ((tmp_node->children != NULL) && (tmp_node->children->type == WBXML_TREE_TEXT_NODE)) { /* This function is used by wbxml and xml callbacks. * So content types must be handled for both situations. */ /* application/vnd.syncml-devinf+wbxml */ if (wbxml_buffer_compare_cstr(tmp_node->children->content, "application/vnd.syncml-devinf+wbxml") == 0) { return WBXML_SYNCML_DATA_TYPE_WBXML; } /* application/vnd.syncml-devinf+xml */ if (wbxml_buffer_compare_cstr(tmp_node->children->content, "application/vnd.syncml-devinf+xml") == 0) { return WBXML_SYNCML_DATA_TYPE_NORMAL; } /* application/vnd.syncml.dmtnds+wbxml */ if (wbxml_buffer_compare_cstr(tmp_node->children->content, "application/vnd.syncml.dmtnds+wbxml") == 0) { return WBXML_SYNCML_DATA_TYPE_WBXML; } /* application/vnd.syncml.dmtnds+xml */ if (wbxml_buffer_compare_cstr(tmp_node->children->content, "application/vnd.syncml.dmtnds+xml") == 0) { return WBXML_SYNCML_DATA_TYPE_NORMAL; } /* text/clear */ if (wbxml_buffer_compare_cstr(tmp_node->children->content, "text/clear") == 0) { return WBXML_SYNCML_DATA_TYPE_CLEAR; } /* text/directory;profile=vCard */ if (wbxml_buffer_compare_cstr(tmp_node->children->content, "text/directory;profile=vCard") == 0) { return WBXML_SYNCML_DATA_TYPE_DIRECTORY_VCARD; } /* text/x-vcard */ if (wbxml_buffer_compare_cstr(tmp_node->children->content, "text/x-vcard") == 0) { return WBXML_SYNCML_DATA_TYPE_VCARD; } /* text/x-vcalendar */ if (wbxml_buffer_compare_cstr(tmp_node->children->content, "text/x-vcalendar") == 0) { return WBXML_SYNCML_DATA_TYPE_VCALENDAR; } } } /** * Hack: we assume that any <Data> inside a <Replace> or <Add> Item is a vObject (vCard / vCal / ...). * * This is because when parsing a <Data> content we really need to put a CDATA, event if we don't really * know the content-type. For example when receiving the end of a splitted vObject with Samsung D600, we receive this: * * <Replace> * <CmdID>162</CmdID> * <Item> * <Source> * <LocURI>./690</LocURI> * </Source> * <Data>EF;CELL:0661809055 * TEL;HOME:0299783886 * X-IRMC-LUID:690 * END:VCARD</Data> * </Item> * </Replace> * * There is no <Meta> info to find the content-type of the <Data>. */ if ( (node->parent != NULL) && (node->parent->parent != NULL) && (node->parent->parent->name != NULL) && ((WBXML_STRCMP(wbxml_tag_get_xml_name(node->parent->parent->name), "Add") == 0) || (WBXML_STRCMP(wbxml_tag_get_xml_name(node->parent->parent->name), "Replace") == 0)) ) { return WBXML_SYNCML_DATA_TYPE_VOBJECT; } } return WBXML_SYNCML_DATA_TYPE_NORMAL; }
void wbxml_tree_clb_xml_end_element(void *ctx, const XML_Char *localName) { WBXMLTreeClbCtx *tree_ctx = (WBXMLTreeClbCtx *) ctx; WBXMLBuffer *content = NULL; WBXMLTreeNode *node = NULL; WBXMLError ret = WBXML_OK; WBXML_DEBUG((WBXML_PARSER, "Expat element end callback ('%s')", localName)); /* If the node is flagged as binary node * then the data is base64 encoded in the XML document * and the data must be decoded in one step. * Examples: Microsoft ActiveSync tags ConversationId or MIME */ node = tree_ctx->current; if (node && node->type == WBXML_TREE_ELEMENT_NODE && node->name->type == WBXML_VALUE_TOKEN && node->name->u.token->options & WBXML_TAG_OPTION_BINARY) { if (node->content == NULL) { WBXML_DEBUG((WBXML_PARSER, " Binary tag: No content => no conversion!")); } else { WBXML_DEBUG((WBXML_PARSER, " Binary tag: Convert base64 data")); ret = wbxml_buffer_decode_base64(node->content); if (ret != WBXML_OK) { WBXML_DEBUG((WBXML_PARSER, " Binary tag: Base64 decoder failed!")); tree_ctx->error = ret; } else { /* Add the buffer as a regular string node (since libwbxml doesn't * offer a way to specify an opaque data node). The WBXML * encoder is responsible for generating correct opaque data for * nodes like this. */ if (wbxml_tree_add_text(tree_ctx->tree, tree_ctx->current, (const WB_UTINY*)wbxml_buffer_get_cstr(node->content), wbxml_buffer_len(node->content)) == NULL) { WBXML_DEBUG((WBXML_PARSER, " Binary tag: Cannot add base64 decoded node!")); tree_ctx->error = WBXML_ERROR_INTERNAL; } } /* safe cleanup */ content = node->content; node->content = NULL; wbxml_buffer_destroy(content); } } if (tree_ctx->expat_utf16) { /** @todo Convert from UTF-16 to UTF-8 */ } /* Check for Error */ if (tree_ctx->error != WBXML_OK) return; /* Are we skipping a whole node ? */ if (tree_ctx->skip_lvl > 0) { if (tree_ctx->skip_lvl == 1) { /* End of skipped node */ #if defined( WBXML_SUPPORT_SYNCML ) if (WBXML_STRCMP(localName, "syncml:devinf:DevInf") == 0 || WBXML_STRCMP(localName, "syncml:dmddf1.2:MgmtTree") == 0) { /* definitions first ... or some compilers don't like it */ WBXMLBuffer *embed_doc = NULL; WBXMLTree *tree = NULL; const WBXMLLangEntry *lang; /* Get embedded DevInf or DM DDF Document */ embed_doc = wbxml_buffer_create(tree_ctx->input_buff + tree_ctx->skip_start, XML_GetCurrentByteIndex(tree_ctx->xml_parser) - tree_ctx->skip_start, XML_GetCurrentByteIndex(tree_ctx->xml_parser) - tree_ctx->skip_start + 10); if (embed_doc == NULL) { tree_ctx->error = WBXML_ERROR_NOT_ENOUGH_MEMORY; wbxml_buffer_destroy(embed_doc); return; } if (tree_ctx->expat_utf16) { /** @todo Convert from UTF-16 to UTF-8 */ } /* Check Buffer Creation and add the closing tag */ if ((WBXML_STRCMP(localName, "syncml:devinf:DevInf") == 0 && (!wbxml_buffer_append_cstr(embed_doc, "</DevInf>"))) || (WBXML_STRCMP(localName, "syncml:dmddf1.2:MgmtTree") == 0 && (!wbxml_buffer_append_cstr(embed_doc, "</MgmtTree>")))) { tree_ctx->error = WBXML_ERROR_NOT_ENOUGH_MEMORY; wbxml_buffer_destroy(embed_doc); return; } /* Add doctype to give the XML parser a chance */ if (WBXML_STRCMP(localName, "syncml:dmddf1.2:MgmtTree") == 0 && tree_ctx->tree->lang->langID != WBXML_LANG_SYNCML_SYNCML12) { tree_ctx->error = WBXML_ERROR_UNKNOWN_XML_LANGUAGE; wbxml_buffer_destroy(embed_doc); return; } switch(tree_ctx->tree->lang->langID) { case WBXML_LANG_SYNCML_SYNCML10: lang = wbxml_tables_get_table(WBXML_LANG_SYNCML_DEVINF10); break; case WBXML_LANG_SYNCML_SYNCML11: lang = wbxml_tables_get_table(WBXML_LANG_SYNCML_DEVINF11); break; case WBXML_LANG_SYNCML_SYNCML12: if (WBXML_STRCMP(localName, "syncml:dmddf1.2:MgmtTree") == 0) { lang = wbxml_tables_get_table(WBXML_LANG_SYNCML_DMDDF12); } else { lang = wbxml_tables_get_table(WBXML_LANG_SYNCML_DEVINF12); } break; default: tree_ctx->error = WBXML_ERROR_UNKNOWN_XML_LANGUAGE; wbxml_buffer_destroy(embed_doc); return; } assert (lang!= NULL); if (lang == NULL) { tree_ctx->error = WBXML_ERROR_UNKNOWN_XML_LANGUAGE; wbxml_buffer_destroy(embed_doc); return; } /* DOCTYPE in reverse order */ if (!wbxml_buffer_insert_cstr(embed_doc,(WB_UTINY *) "\">\n", 0) || /* > */ !wbxml_buffer_insert_cstr(embed_doc, (WB_UTINY *) lang->publicID->xmlDTD, 0) || /* DTD */ !wbxml_buffer_insert_cstr(embed_doc, (WB_UTINY *) "\" \"", 0) || /* DTD */ !wbxml_buffer_insert_cstr(embed_doc, (WB_UTINY *) lang->publicID->xmlPublicID, 0) || /* Public ID */ !wbxml_buffer_insert_cstr(embed_doc, (WB_UTINY *) " PUBLIC \"", 0) || /* PUBLIC " */ !wbxml_buffer_insert_cstr(embed_doc, (WB_UTINY *) lang->publicID->xmlRootElt, 0) || /* Root Element */ !wbxml_buffer_insert_cstr(embed_doc, (WB_UTINY *) "<!DOCTYPE ", 0)) /* <!DOCTYPE */ { tree_ctx->error = WBXML_ERROR_ENCODER_APPEND_DATA; wbxml_buffer_destroy(embed_doc); return; } WBXML_DEBUG((WBXML_PARSER, "\t Embedded Doc : '%s'", wbxml_buffer_get_cstr(embed_doc))); /* Parse 'DevInf' Document */ if ((ret = wbxml_tree_from_xml(wbxml_buffer_get_cstr(embed_doc), wbxml_buffer_len(embed_doc), &tree)) != WBXML_OK) { tree_ctx->error = ret; wbxml_buffer_destroy(embed_doc); return; } /* Add Tree Node */ tree_ctx->current = wbxml_tree_add_tree(tree_ctx->tree, tree_ctx->current, tree); if (tree_ctx->current == NULL) { tree_ctx->error = WBXML_ERROR_INTERNAL; wbxml_tree_destroy(tree); wbxml_buffer_destroy(embed_doc); return; } /* Clean-up */ wbxml_buffer_destroy(embed_doc); tree_ctx->skip_lvl = 0; } #endif /* WBXML_SUPPORT_SYNCML */ } else { tree_ctx->skip_lvl--; return; } } if (tree_ctx->current == NULL) { tree_ctx->error = WBXML_ERROR_INTERNAL; return; } if (tree_ctx->current->parent == NULL) { /* This must be the Root Element */ if (tree_ctx->current != tree_ctx->tree->root) { tree_ctx->error = WBXML_ERROR_INTERNAL; } } else { #if defined ( WBXML_SUPPORT_SYNCML ) /* Have we added a missing CDATA section ? * If so, we assume that now that we have reached an end of Element, * the CDATA section ended, and so we go back to parent. */ if ((tree_ctx->current != NULL) && (tree_ctx->current->type == WBXML_TREE_CDATA_NODE)) tree_ctx->current = tree_ctx->current->parent; #endif /* WBXML_SUPPORT_SYNCML */ /* Go back one step upper in the tree */ tree_ctx->current = tree_ctx->current->parent; } }
void wbxml_tree_clb_xml_start_element(void *ctx, const XML_Char *localName, const XML_Char **attrs) { WBXMLTreeClbCtx *tree_ctx = (WBXMLTreeClbCtx *) ctx; const WBXMLLangEntry *lang_table = NULL; WBXML_DEBUG((WBXML_PARSER, "Expat element start callback ('%s')", localName)); if (tree_ctx->expat_utf16) { /** @todo Convert from UTF-16 to UTF-8 */ } /* Check for Error */ if (tree_ctx->error != WBXML_OK) return; /* Are we skipping a whole node ? */ if (tree_ctx->skip_lvl > 0) { tree_ctx->skip_lvl++; return; } if (tree_ctx->current == NULL) { /* This is the Root Element */ if (tree_ctx->tree->lang == NULL) { /* Language Table not already found: Search again */ lang_table = wbxml_tables_search_table(wbxml_tables_get_main(), NULL, NULL, (const WB_UTINY *) localName); if (lang_table == NULL) { /* Damn, this is an unknown language for us... */ tree_ctx->error = WBXML_ERROR_UNKNOWN_XML_LANGUAGE; return; } else { /* Well, we hope this was the Language we are searching for.. let's try with it :| */ tree_ctx->tree->lang = lang_table; } } } #if defined( WBXML_SUPPORT_SYNCML ) /* If this is an embedded (not root) document, skip it * Actually SyncML DevInf and DM DDF are known as such * potentially embedded documents. */ if (( (WBXML_STRCMP(localName, "syncml:devinf:DevInf") == 0) || (WBXML_STRCMP(localName, "syncml:dmddf1.2:MgmtTree") == 0) )&& (tree_ctx->current != NULL)) { tree_ctx->skip_start = XML_GetCurrentByteIndex(tree_ctx->xml_parser); /* Skip this node */ tree_ctx->skip_lvl++; return; } #endif /* WBXML_SUPPORT_SYNCML */ /* Add Element Node */ tree_ctx->current = wbxml_tree_add_xml_elt_with_attrs(tree_ctx->tree, tree_ctx->current, (WB_UTINY *) localName, (const WB_UTINY**) attrs); if (tree_ctx->current == NULL) { tree_ctx->error = WBXML_ERROR_NOT_ENOUGH_MEMORY; } }
const WBXMLAttrEntry * wbxml_tables_get_attr_from_xml(const WBXMLLangEntry *lang_table, WB_UTINY *xml_name, WB_UTINY *xml_value, WB_UTINY **value_left) { WB_ULONG i = 0; WB_ULONG found_index = 0, found_comp = 0; WB_BOOL found = FALSE; if ((lang_table == NULL) || (lang_table->attrTable == NULL) || (xml_name == NULL)) return NULL; if (value_left != NULL) *value_left = xml_value; /* Iterate in Attribute Table */ while (lang_table->attrTable[i].xmlName != NULL) { /* Search for Attribute Name */ if (WBXML_STRCMP((const WB_TINY *)lang_table->attrTable[i].xmlName, (const WB_TINY *)xml_name) == 0) { if (lang_table->attrTable[i].xmlValue == NULL) { /* This is the token with a NULL Attribute Value */ if (xml_value == NULL) { /* Well, we got it */ return &(lang_table->attrTable[i]); } else { if (!found) { /* We haven't found yet a better Attribute Token */ found = TRUE; found_index = i; } /* Else: We already have found a better Attribute Token, so let's forget this one */ } } else { /* Check the Attribute Value */ if (xml_value != NULL) { if (WBXML_STRCMP((const WB_TINY *)lang_table->attrTable[i].xmlValue, (const WB_TINY *)xml_value) == 0) { /* We have found the EXACT Attribute Name / Value pair we are searching, well done boy */ if (value_left != NULL) *value_left = NULL; return &(lang_table->attrTable[i]); } else { if ((WBXML_STRLEN((const WB_TINY *)lang_table->attrTable[i].xmlValue) < WBXML_STRLEN((const WB_TINY *)xml_value)) && (found_comp < WBXML_STRLEN(lang_table->attrTable[i].xmlValue)) && (WBXML_STRNCMP((const WB_TINY *)lang_table->attrTable[i].xmlValue, (const WB_TINY *)xml_value, WBXML_STRLEN((const WB_TINY *)lang_table->attrTable[i].xmlValue)) == 0)) { /* We have found a better Attribute Value */ found = TRUE; found_index = i; found_comp = WBXML_STRLEN(lang_table->attrTable[i].xmlValue); } } } /* Else: We are searching for the Attribute Token with a NULL Attribute Value associated, so forget this one */ } } i++; } /* Attribute Name / Value pair not found, but an entry with this Attribute Name, * and (maybe) start of this Attribute Value was found */ if (found) { if (value_left != NULL) *value_left = xml_value + found_comp; return &(lang_table->attrTable[found_index]); } /* Attribute Name NOT found */ return NULL; }
WB_LONG main(WB_LONG argc, WB_TINY **argv) { WB_UTINY *wbxml = NULL, *output = NULL, *xml = NULL; FILE *input_file = NULL, *output_file = NULL; WB_ULONG wbxml_len = 0; WB_LONG count = 0, xml_len = 0, total = 0; WB_TINY opt; WBXMLError ret = WBXML_OK; WB_UTINY input_buffer[INPUT_BUFFER_SIZE + 1]; WBXMLConvXML2WBXML *conv = NULL; ret = wbxml_conv_xml2wbxml_create(&conv); if (ret != WBXML_OK) { fprintf(stderr, "xml2wbxml failed: %s\n", wbxml_errors_string(ret)); goto clean_up; } while ((opt = (WB_TINY) wbxml_getopt(argc, argv, "nkah?o:v:")) != EOF) { switch (opt) { case 'v': wbxml_conv_xml2wbxml_set_version(conv, get_version((const WB_TINY*)optarg)); break; case 'n': wbxml_conv_xml2wbxml_disable_string_table(conv); break; case 'k': wbxml_conv_xml2wbxml_enable_preserve_whitespaces(conv); break; case 'a': wbxml_conv_xml2wbxml_disable_public_id(conv); break; case 'o': output = (WB_UTINY*) optarg; break; case 'h': case '?': default: help(); return 0; } } if (optind >= argc) { fprintf(stderr, "Missing arguments\n"); help(); return 0; } #ifdef WBXML_USE_LEAKTRACKER lt_init_mem(); lt_log_open_file("xml2wbxml.log"); lt_log(0, "\n***************************\n Converting file: %s", argv[optind]); #endif /********************************** * Read the XML Document */ if (WBXML_STRCMP(argv[optind], "-") == 0) { input_file = stdin; } else { /* Open XML document */ input_file = fopen(argv[optind], "r"); if (input_file == NULL) { printf("Failed to open %s\n", argv[optind]); goto clean_up; } } /* Read XML document */ while(!feof(input_file)) { count = fread(input_buffer, sizeof(WB_UTINY), INPUT_BUFFER_SIZE, input_file); if (ferror(input_file)) { fprintf(stderr, "Error while reading from file %s\n", argv[1]); if (input_file != stdin) fclose(input_file); if (xml != NULL) #ifdef WBXML_USE_LEAKTRACKER wbxml_free(xml); #else free(xml); #endif goto clean_up; } total += count; #ifdef WBXML_USE_LEAKTRACKER xml = wbxml_realloc(xml, total + 1); #else xml = realloc(xml, total + 1); #endif if (xml == NULL) { fprintf(stderr, "Not enought memory\n"); if (input_file != stdin) fclose(input_file); goto clean_up; } memcpy(xml + xml_len, input_buffer, count); xml_len += count; } if (input_file != stdin) fclose(input_file); xml[xml_len] = '\0'; /* Convert XML document */ ret = wbxml_conv_xml2wbxml_run(conv, xml, xml_len, &wbxml, &wbxml_len); if (ret != WBXML_OK) { fprintf(stderr, "xml2wbxml failed: %s\n", wbxml_errors_string(ret)); } else { fprintf(stderr, "xml2wbxml succeded\n"); if (output != NULL) { if (WBXML_STRCMP(output, "-") == 0) { output_file = stdout; } else { /* Open Output File */ output_file = fopen((const WB_TINY*) output, "wb"); } if (output_file == NULL) { fprintf(stderr, "Failed to open output file: %s\n", output); } else { /* Write to Output File */ if (fwrite(wbxml, sizeof(WB_UTINY), wbxml_len, output_file) < wbxml_len) fprintf(stderr, "Error while writing to file: %s\n", output); /* else fprintf(stderr, "Written %u bytes to file: %s\n", wbxml_len, output); */ if (output_file != stdout) fclose(output_file); } } /* Clean-up */ if (wbxml != NULL) #ifdef WBXML_USE_LEAKTRACKER wbxml_free(wbxml); #else free(wbxml); #endif } if (xml != NULL) #ifdef WBXML_USE_LEAKTRACKER wbxml_free(xml); #else free(xml); #endif clean_up: if (conv != NULL) wbxml_conv_xml2wbxml_destroy(conv); #ifdef WBXML_USE_LEAKTRACKER lt_check_leaks(); lt_shutdown_mem(); lt_log_close_file(); #endif return ret; }
static WBXMLLanguage get_lang(const WB_TINY *lang) { #if defined( WBXML_SUPPORT_WML ) if (WBXML_STRCMP(lang, "WML10") == 0) return WBXML_LANG_WML10; if (WBXML_STRCMP(lang, "WML11") == 0) return WBXML_LANG_WML11; if (WBXML_STRCMP(lang, "WML12") == 0) return WBXML_LANG_WML12; if (WBXML_STRCMP(lang, "WML13") == 0) return WBXML_LANG_WML13; #endif /* WBXML_SUPPORT_WML */ #if defined( WBXML_SUPPORT_WTA ) if (WBXML_STRCMP(lang, "WTA10") == 0) return WBXML_LANG_WTA10; if (WBXML_STRCMP(lang, "WTAWML12") == 0) return WBXML_LANG_WTAWML12; if (WBXML_STRCMP(lang, "CHANNEL11") == 0) return WBXML_LANG_CHANNEL11; if (WBXML_STRCMP(lang, "CHANNEL12") == 0) return WBXML_LANG_CHANNEL12; #endif /* WBXML_SUPPORT_WTA */ #if defined( WBXML_SUPPORT_SI ) if (WBXML_STRCMP(lang, "SI10") == 0) return WBXML_LANG_SI10; #endif /* WBXML_SUPPORT_SI */ #if defined( WBXML_SUPPORT_SL ) if (WBXML_STRCMP(lang, "SL10") == 0) return WBXML_LANG_SL10; #endif /* WBXML_SUPPORT_SL */ #if defined( WBXML_SUPPORT_CO ) if (WBXML_STRCMP(lang, "CO10") == 0) return WBXML_LANG_CO10; #endif /* WBXML_SUPPORT_CO */ #if defined( WBXML_SUPPORT_PROV ) if (WBXML_STRCMP(lang, "PROV10") == 0) return WBXML_LANG_PROV10; #endif /* WBXML_SUPPORT_PROV */ #if defined( WBXML_SUPPORT_EMN ) if (WBXML_STRCMP(lang, "EMN10") == 0) return WBXML_LANG_EMN10; #endif /* WBXML_SUPPORT_EMN */ #if defined( WBXML_SUPPORT_DRMREL ) if (WBXML_STRCMP(lang, "DRMREL10") == 0) return WBXML_LANG_DRMREL10; #endif /* WBXML_SUPPORT_DRMREL */ #if defined( WBXML_SUPPORT_OTA_SETTINGS ) if (WBXML_STRCMP(lang, "OTA") == 0) return WBXML_LANG_OTA_SETTINGS; #endif /* WBXML_SUPPORT_OTA_SETTINGS */ #if defined( WBXML_SUPPORT_SYNCML ) if (WBXML_STRCMP(lang, "SYNCML10") == 0) return WBXML_LANG_SYNCML_SYNCML10; if (WBXML_STRCMP(lang, "DEVINF10") == 0) return WBXML_LANG_SYNCML_DEVINF10; if (WBXML_STRCMP(lang, "SYNCML11") == 0) return WBXML_LANG_SYNCML_SYNCML11; if (WBXML_STRCMP(lang, "DEVINF11") == 0) return WBXML_LANG_SYNCML_DEVINF11; if (WBXML_STRCMP(lang, "METINF11") == 0) return WBXML_LANG_SYNCML_METINF11; if (WBXML_STRCMP(lang, "SYNCML12") == 0) return WBXML_LANG_SYNCML_SYNCML12; if (WBXML_STRCMP(lang, "DEVINF12") == 0) return WBXML_LANG_SYNCML_DEVINF12; if (WBXML_STRCMP(lang, "METINF12") == 0) return WBXML_LANG_SYNCML_METINF12; #endif /* WBXML_SUPPORT_SYNCML */ #if defined( WBXML_SUPPORT_WV ) if (WBXML_STRCMP(lang, "CSP11") == 0) return WBXML_LANG_WV_CSP11; if (WBXML_STRCMP(lang, "CSP12") == 0) return WBXML_LANG_WV_CSP12; #endif /* WBXML_SUPPORT_WV */ #if defined( WBXML_SUPPORT_AIRSYNC ) if (WBXML_STRCMP(lang, "AIRSYNC") == 0) return WBXML_LANG_AIRSYNC; #endif /* WBXML_SUPPORT_AIRSYNC */ return WBXML_LANG_UNKNOWN; }
WB_LONG main(WB_LONG argc, WB_TINY **argv) { WB_UTINY *wbxml = NULL, *output = NULL, *xml = NULL; FILE *input_file = NULL, *output_file = NULL; WB_LONG count = 0, wbxml_len = 0, total = 0; WB_ULONG xml_len = 0; WB_TINY opt; WBXMLError ret = WBXML_OK; WB_UTINY input_buffer[INPUT_BUFFER_SIZE + 1]; WBXMLGenXMLParams params; /* Init Default Parameters */ params.lang = WBXML_LANG_UNKNOWN; params.gen_type = WBXML_GEN_XML_INDENT; params.indent = 1; params.keep_ignorable_ws = FALSE; while ((opt = (WB_TINY) getopt(argc, argv, "kh?o:m:i:l:")) != EOF) { switch (opt) { case 'k': params.keep_ignorable_ws = TRUE; break; case 'i': params.indent = (WB_UTINY) atoi((const WB_TINY*)optarg); break; case 'l': params.lang = get_lang((const WB_TINY*)optarg); break; case 'm': switch (atoi((const WB_TINY*)optarg)) { case 0: params.gen_type = WBXML_GEN_XML_COMPACT; break; case 1: params.gen_type = WBXML_GEN_XML_INDENT; break; case 2: params.gen_type = WBXML_GEN_XML_CANONICAL; break; default: params.gen_type = WBXML_GEN_XML_INDENT; } break; case 'o': output = (WB_UTINY*) optarg; break; case 'h': case '?': default: help(); return 0; } } if (optind >= argc) { fprintf(stderr, "Missing arguments\n"); help(); return 0; } #ifdef WBXML_USE_LEAKTRACKER lt_init_mem(); lt_log_open_file("wbxml2xml.log"); lt_log(0, "\n***************************\n Converting file: %s", argv[optind]); #endif /********************************** * Read the WBXML Document */ if (WBXML_STRCMP(argv[optind], "-") == 0) { input_file = stdin; } else { /* Open WBXML document */ input_file = fopen(argv[optind], "rb"); if (input_file == NULL) { fprintf(stderr, "Failed to open %s\n", argv[optind]); goto clean_up; } } /* Read WBXML document */ while(!feof(input_file)) { count = fread(input_buffer, sizeof(WB_UTINY), INPUT_BUFFER_SIZE, input_file); if (ferror(input_file)) { fprintf(stderr, "Error while reading from file %s\n", argv[optind]); if (input_file != stdin) fclose(input_file); if (wbxml != NULL) wbxml_free(wbxml); goto clean_up; } total += count; wbxml = wbxml_realloc(wbxml, total); if (wbxml == NULL) { fprintf(stderr, "Not enought memory\n"); if (input_file != stdin) fclose(input_file); if (wbxml != NULL) wbxml_free(wbxml); goto clean_up; } memcpy(wbxml + wbxml_len, input_buffer, count); wbxml_len += count; } if (input_file != stdin) fclose(input_file); /* Convert WBXML document */ ret = wbxml_conv_wbxml2xml_withlen(wbxml, wbxml_len, &xml, &xml_len, ¶ms); if (ret != WBXML_OK) { fprintf(stderr, "wbxml2xml failed: %s\n", wbxml_errors_string(ret)); } else { /* fprintf(stderr, "wbxml2xml succeded: \n%s\n", xml); */ fprintf(stderr, "wbxml2xml succeded\n"); if (output != NULL) { if (WBXML_STRCMP(output, "-") == 0) { output_file = stdout; } else { /* Open Output File */ output_file = fopen((const WB_TINY*) output, "w"); } if (output_file == NULL) { fprintf(stderr, "Failed to open output file: %s\n", output); } /* Write to Output File */ if (fwrite(xml, sizeof(WB_UTINY), xml_len, output_file) < xml_len) fprintf(stderr, "Error while writing to file: %s\n", output); /* else fprintf(stderr, "Written %u bytes to file: %s\n", xml_len, output); */ if (output_file != stdout) fclose(output_file); } /* Clean-up */ wbxml_free(xml); } wbxml_free(wbxml); clean_up: #ifdef WBXML_USE_LEAKTRACKER lt_check_leaks(); lt_shutdown_mem(); lt_log_close_file(); #endif return 0; }