Exemple #1
1
VALUE
xml_to_wbxml(VALUE self, VALUE xml_source)
{
	WBXMLError	ret = WBXML_OK;
	WBXMLConvXML2WBXMLParams params;
	WB_UTINY*	xml = NULL;		/* Must be null terminated */
	WB_UTINY*	wbxml = NULL;
	WB_ULONG	wbxml_len = 0;
	VALUE		value;

	Check_SafeStr(xml_source);
	if (TYPE(xml_source) != T_STRING)
		rb_raise(rb_eTypeError, "parameter to xml_to_wbxml must be a string");
	xml = (WB_UTINY*)STR2CSTR(xml_source);

	params.wbxml_version = WBXML_VERSION_13;
	params.use_strtbl = TRUE;
	params.keep_ignorable_ws = FALSE;

	ret = wbxml_conv_xml2wbxml(xml, &wbxml, &wbxml_len, &params);
	if (ret != WBXML_OK)
		rb_raise(rb_eRuntimeError, (const char*)wbxml_errors_string(ret));

	value = rb_str_new((const char*)wbxml, wbxml_len);

	if (wbxml != NULL)
		wbxml_free(wbxml);
	return value;
}
Exemple #2
0
VALUE
wbxml_to_xml(VALUE self, VALUE wbxml_source)
{
	WBXMLError	ret = WBXML_OK;
	WBXMLConvWBXML2XMLParams	params;
	WB_UTINY*	wbxml = NULL;
	WB_ULONG	wbxml_len = 0;
	WB_UTINY*	xml = NULL;		/* Must be null terminated */
	VALUE		value;

	Check_SafeStr(wbxml_source);
	if (TYPE(wbxml_source) != T_STRING)
		rb_raise(rb_eTypeError, "parameter to wbxml_to_xml must be a string");
	wbxml = (WB_UTINY*)STR2CSTR(wbxml_source);
	wbxml_len = RSTRING_LEN(wbxml_source);

	params.gen_type = WBXML_ENCODER_XML_GEN_INDENT;
	params.lang = WBXML_LANG_UNKNOWN;	/* Don't force the language */
	params.indent = 2;
	params.keep_ignorable_ws = FALSE;

	ret = wbxml_conv_wbxml2xml(wbxml, wbxml_len, &xml, &params);
	if (ret != WBXML_OK)
		rb_raise(rb_eRuntimeError, (const char*)wbxml_errors_string(ret));

	value = rb_str_new((const char*)xml, strlen(xml));

	if (xml != NULL)
		wbxml_free(xml);
	return value;
}
Exemple #3
0
static VALUE
mWBXML2_convert(VALUE self, VALUE input, void *conv, mWBXML2_run_conv run_conv) {
  WBXMLError code = WBXML_OK;
  WB_UTINY *output_buf_ptr = NULL;
  WB_ULONG output_len = 0;
  VALUE retval = Qnil;
  if (WBXML_OK != (code = run_conv(conv, (WB_UTINY*)StringValuePtr(input), 
				   RSTRING_LENINT(input),
				   &output_buf_ptr,
				   &output_len))) {
    rb_raise(rb_eRuntimeError, "conversion error: %s.", wbxml_errors_string(code));
  }
  retval = rb_str_new((char*)output_buf_ptr, output_len);
  free(output_buf_ptr);
  return retval;
}
Exemple #4
0
WBXML_DECLARE(WBXMLError) wbxml_tree_from_wbxml(WB_UTINY *wbxml, WB_ULONG wbxml_len, WBXMLLanguage lang, WBXMLTree **tree)
{
    WBXMLParser *wbxml_parser = NULL;
    WB_LONG error_index = 0;
    WBXMLTreeClbCtx wbxml_tree_clb_ctx;
    WBXMLError ret = WBXML_OK;
    WBXMLContentHandler wbxml_tree_content_handler = 
        {
            wbxml_tree_clb_wbxml_start_document,
            wbxml_tree_clb_wbxml_end_document,
            wbxml_tree_clb_wbxml_start_element,
            wbxml_tree_clb_wbxml_end_element,
            wbxml_tree_clb_wbxml_characters,
            wbxml_tree_clb_wbxml_pi
        };

    if (tree != NULL)
        *tree = NULL;

    /* Create WBXML Parser */
    if((wbxml_parser = wbxml_parser_create()) == NULL) {
        WBXML_ERROR((WBXML_PARSER, "Can't create WBXML Parser"));
        return WBXML_ERROR_NOT_ENOUGH_MEMORY;
    }

    /* Init context */
    wbxml_tree_clb_ctx.error = WBXML_OK;
    wbxml_tree_clb_ctx.current = NULL;
    if ((wbxml_tree_clb_ctx.tree = wbxml_tree_create(WBXML_LANG_UNKNOWN, WBXML_CHARSET_UNKNOWN)) == NULL) {
        wbxml_parser_destroy(wbxml_parser);
        WBXML_ERROR((WBXML_PARSER, "Can't create WBXML Tree"));
        return WBXML_ERROR_NOT_ENOUGH_MEMORY;
    }
    
    /* Set Handlers Callbacks */
    wbxml_parser_set_user_data(wbxml_parser, &wbxml_tree_clb_ctx);
    wbxml_parser_set_content_handler(wbxml_parser, &wbxml_tree_content_handler);

    /* Give the user the possibility to force Document Language */
    if (lang != WBXML_LANG_UNKNOWN)
        wbxml_parser_set_language(wbxml_parser, lang);

    /** @todo Use wbxml_parser_set_meta_charset() */

    /* Parse the WBXML document to WBXML Tree */
    ret = wbxml_parser_parse(wbxml_parser, wbxml, wbxml_len);
    if ((ret != WBXML_OK) || (wbxml_tree_clb_ctx.error != WBXML_OK)) 
    {
        error_index = wbxml_parser_get_current_byte_index(wbxml_parser);
        WBXML_ERROR((WBXML_PARSER, "WBXML Parser failed at %ld - token: %x (%s)", 
                                   error_index,
                                   wbxml[error_index],
                                   ret != WBXML_OK ? wbxml_errors_string(ret) : wbxml_errors_string(wbxml_tree_clb_ctx.error)));
        
        wbxml_tree_destroy(wbxml_tree_clb_ctx.tree);
    }
    else {
        *tree = wbxml_tree_clb_ctx.tree;
    }

    /* Clean-up */
    wbxml_parser_destroy(wbxml_parser);

    if (ret != WBXML_OK)
        return ret;
    else
        return wbxml_tree_clb_ctx.error;
}
Exemple #5
0
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;
}
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, &params);
    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;
}