/** * @brief Add space for at least 'size' octets * @param buffer The buffer * @param size The size to add * @return TRUE is space successfully reserved, FALSE is size was negative, buffer was NULL or if not enough memory */ static WB_BOOL grow_buff(WBXMLBuffer *buffer, WB_ULONG size) { // unsigned char *Srcbuff; if ((buffer == NULL) || buffer->is_static || ((int)size < 0)) return FALSE; /* Make room for the invisible terminating NUL */ size++; if ((buffer->len + size) > buffer->malloced) { if ((buffer->malloced + buffer->malloc_block) < (buffer->len + size)) buffer->malloced = buffer->len + size + buffer->malloc_block; else buffer->malloced = buffer->malloced + buffer->malloc_block; #if 1 //CSH1024. 2008 Tmp Fix 1 buffer->data = wbxml_realloc(buffer->data, buffer->malloced); #else Srcbuff = buffer->data; buffer->data = wbxml_malloc(buffer->malloced); memcpy(buffer->data, Srcbuff, buffer->len); wbxml_free(Srcbuff); #endif if (buffer->data == NULL) return FALSE; } return TRUE; }
/** * @brief Add space for at least 'size' octets * @param buffer The buffer * @param size The size to add * @return TRUE is space successfully reserved, FALSE is size was negative, buffer was NULL or if not enough memory */ static WB_BOOL grow_buff(WBXMLBuffer *buffer, WB_ULONG size) { if ((buffer == NULL) || buffer->is_static) return FALSE; /* Make room for the invisible terminating NUL */ size++; if ((buffer->len + size) > buffer->malloced) { if ((buffer->malloced + buffer->malloc_block) < (buffer->len + size)) buffer->malloced = buffer->len + size + buffer->malloc_block; else buffer->malloced = buffer->malloced + buffer->malloc_block; buffer->data = wbxml_realloc(buffer->data, buffer->malloced); if (buffer->data == NULL) return FALSE; } return TRUE; }
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, ¶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; }