WBXML_DECLARE(WBXMLBuffer *) wbxml_buffer_create_real(const WB_UTINY *data, WB_ULONG len, WB_ULONG malloc_block) { WBXMLBuffer *buffer = NULL; buffer = (WBXMLBuffer *) wbxml_malloc(sizeof(WBXMLBuffer)); if (buffer == NULL) return NULL; buffer->malloc_block = malloc_block; buffer->is_static = FALSE; if ((len <= 0) || (data == NULL)) { buffer->malloced = 0; buffer->len = 0; buffer->data = NULL; } else { if (len + 1 > malloc_block + 1) buffer->malloced = len + 1 + malloc_block; else buffer->malloced = malloc_block + 1; buffer->data = (WB_UTINY *) wbxml_malloc(buffer->malloced * sizeof(WB_UTINY)); if (buffer->data == NULL) { wbxml_free(buffer); return NULL; } buffer->len = len; memcpy(buffer->data, data, len); buffer->data[len] = '\0'; } return buffer; }
/** * @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 Create a List Element * @param item Item of Element to create * @return The newly created Element, or NULL if not enough memory * @warning Do NOT use this function directly, use wbxml_list_create() macro instead */ static WBXMLListElt *wbxml_elt_create_real(void *item) { WBXMLListElt *elt = NULL; if ((elt = (WBXMLListElt *) wbxml_malloc(sizeof(WBXMLListElt))) == NULL) return NULL; elt->item = item; elt->next = NULL; return elt; }
WBXML_DECLARE(WBXMLList *) wbxml_list_create_real(void) { WBXMLList *list = NULL; if ((list = (WBXMLList *) wbxml_malloc(sizeof(WBXMLList))) == NULL) return NULL; list->head = NULL; list->tail = NULL; list->len = 0; return list; }
WBXML_DECLARE(WBXMLTree *) wbxml_tree_create(WBXMLLanguage lang, WBXMLCharsetMIBEnum orig_charset) { WBXMLTree *result = NULL; if ((result = (WBXMLTree *) wbxml_malloc(sizeof(WBXMLTree))) == NULL) return NULL; result->lang = wbxml_tables_get_table(lang); result->root = NULL; result->orig_charset = orig_charset; result->cur_code_page = 0; return result; }
WBXML_DECLARE(WBXMLBuffer *) wbxml_buffer_sta_create_real(const WB_UTINY *data, WB_ULONG len) { WBXMLBuffer *buffer = NULL; buffer = wbxml_malloc(sizeof(WBXMLBuffer)); if (buffer == NULL) { return NULL; } buffer->is_static = TRUE; buffer->data = (WB_UTINY *) data; buffer->len = len; return buffer; }
WBXML_DECLARE(WBXMLTreeNode *) wbxml_tree_node_create(WBXMLTreeNodeType type) { WBXMLTreeNode *result = NULL; if ((result = (WBXMLTreeNode *) wbxml_malloc(sizeof(WBXMLTreeNode))) == NULL) return NULL; result->type = type; result->name = NULL; result->attrs = NULL; result->content = NULL; result->tree = NULL; result->parent = NULL; result->children = NULL; result->next = NULL; result->prev = NULL; return result; }
WBXML_DECLARE(WBXMLError) wbxml_charset_conv(const WB_TINY *in_buf, WB_ULONG *io_bytes, WBXMLCharsetMIBEnum in_charset, WBXMLBuffer **out_buf, WBXMLCharsetMIBEnum out_charset) { /************************************************** * First, check for simple US-ASCII / UTF-8 cases */ /* Are we dealing with US-ASCII or UTF-8 ? */ if (((in_charset == WBXML_CHARSET_US_ASCII) || (in_charset == WBXML_CHARSET_UTF_8)) && ((out_charset == WBXML_CHARSET_US_ASCII) || (out_charset == WBXML_CHARSET_UTF_8))) { /* Create a static buffer */ if ((*out_buf = wbxml_buffer_sta_create_from_cstr(in_buf)) == NULL) { return WBXML_ERROR_NOT_ENOUGH_MEMORY; } /* US-ASCII and UTF-8 are NULL terminated */ *io_bytes -= WBXML_STRLEN(in_buf) + 1; return WBXML_OK; } /************************************** * Ok guys, we really have to convert */ #if defined( HAVE_ICONV ) { /********************** * The iconv way */ const WB_TINY * inbuf_pos = NULL; WB_TINY **__restrict__ inbuf_ref = NULL; const WB_TINY * charset_to = NULL; const WB_TINY * charset_from = NULL; WB_TINY * tmp_buf = NULL; WB_TINY * tmp_ptr = NULL; WB_ULONG tmp_buf_len = 0; WB_ULONG tmp_len_left = 0; WBXMLError ret = WBXML_OK; iconv_t cd = 0; WB_UTINY last_char = 0; /* Get Charsets names */ if (!wbxml_charset_get_name(in_charset, &charset_from)) { return WBXML_ERROR_CHARSET_UNKNOWN; } if (!wbxml_charset_get_name(out_charset, &charset_to)) { return WBXML_ERROR_CHARSET_UNKNOWN; } /* Init iconv */ if ((cd = iconv_open(charset_to, charset_from)) == (iconv_t)(-1)) { /* Init failed */ return WBXML_ERROR_CHARSET_CONV_INIT; } /* Allocate maximum result buffer (4 bytes unicode) */ tmp_len_left = tmp_buf_len = 4 * (sizeof(WB_TINY) * (*io_bytes)); if ((tmp_buf = (WB_TINY *) wbxml_malloc(tmp_buf_len)) == NULL) { iconv_close(cd); return WBXML_ERROR_NOT_ENOUGH_MEMORY; } tmp_ptr = tmp_buf; /* The input buffer is const but not the pointer itself. The original const *inbuf should not be modified for a potential later usage. */ inbuf_pos = in_buf; inbuf_ref = (WB_TINY **__restrict__) &inbuf_pos; /* Convert ! */ (void) iconv(cd, inbuf_ref, (size_t*)io_bytes, &tmp_buf, (size_t*)&tmp_len_left); /** @todo Check errno (but it doesn't seems to work on windows) */ if (tmp_buf_len > tmp_len_left) { /* Create result buffer */ if ((*out_buf = wbxml_buffer_create(tmp_ptr, tmp_buf_len - tmp_len_left, tmp_buf_len - tmp_len_left)) == NULL) { /* Not enough memory */ ret = WBXML_ERROR_NOT_ENOUGH_MEMORY; } /* Remove trailing NULL char */ wbxml_buffer_remove_trailing_zeros(*out_buf); } else { /* Not converted */ ret = WBXML_ERROR_CHARSET_CONV; } /* Shutdown iconv */ iconv_close(cd); /* Clean-up */ wbxml_free(tmp_ptr); return ret; } #else { /*************************************************** * Add your own charset conversion function here ! */ return WBXML_ERROR_NO_CHARSET_CONV; } #endif /* HAVE_ICONV */ }