/** * @brief Format a Log Message * @param buf [out] Resulting formated buffer * @param type [in] Type of Message * @param fmt [int] Message to format */ static void format_log_message(WB_TINY *buf, WB_UTINY type, const WB_TINY *fmt) { WB_TINY *p, prefix[WBXML_LOG_FORMAT_SIZE]; p = prefix; sprintf(p, "%s> ", get_type(type)); if (WBXML_STRLEN(prefix) + WBXML_STRLEN(fmt) > WBXML_LOG_FORMAT_SIZE / 2) { sprintf(buf, "(LOG MESSAGE TOO LONG !)\n"); return; } sprintf(buf, "%s%s\n", prefix, fmt); }
WBXML_DECLARE(WB_BOOL) wbxml_buffer_insert_cstr(WBXMLBuffer *to, WB_UTINY *str, WB_ULONG pos) { if ((to != NULL) && (str != NULL) && !to->is_static) return insert_data(to, pos, str, WBXML_STRLEN(str)); return FALSE; }
WBXML_DECLARE(WBXMLError) wbxml_tree_node_add_xml_attr(const WBXMLLangEntry *lang_table, WBXMLTreeNode *node, const WB_UTINY *name, const WB_UTINY *value) { WBXMLAttribute *attr = NULL; const WBXMLAttrEntry *attr_entry = NULL; /* Create list if needed */ if (node->attrs == NULL) { if ((node->attrs = wbxml_list_create()) == NULL) { return WBXML_ERROR_NOT_ENOUGH_MEMORY; } } /* Create Attribute */ if ((attr = wbxml_attribute_create()) == NULL) return WBXML_ERROR_NOT_ENOUGH_MEMORY; /* Set Attribute Name */ if ((attr_entry = wbxml_tables_get_attr_from_xml(lang_table, (WB_UTINY *)name, (WB_UTINY *)value, NULL)) != NULL) attr->name = wbxml_attribute_name_create_token(attr_entry); else attr->name = wbxml_attribute_name_create_literal((WB_UTINY *)name); if (attr->name == NULL) { wbxml_attribute_destroy(attr); return WBXML_ERROR_NOT_ENOUGH_MEMORY; } /* Set Attribute Value */ attr->value = wbxml_buffer_create_real(value, WBXML_STRLEN(value), WBXML_STRLEN(value)); if (attr->value == NULL) { wbxml_attribute_destroy(attr); return WBXML_ERROR_NOT_ENOUGH_MEMORY; } /* Add attribute to list */ if (!wbxml_list_append(node->attrs, attr)) { wbxml_attribute_destroy(attr); return WBXML_ERROR_NOT_ENOUGH_MEMORY; } return WBXML_OK; }
WBXML_DECLARE(WBXMLError) wbxml_charset_conv_term(const WB_TINY *in_buf, WB_ULONG *io_bytes, WBXMLCharsetMIBEnum in_charset, WBXMLBuffer **out_buf, WBXMLCharsetMIBEnum out_charset) { WB_ULONG buf_len = 0; WB_ULONG new_len = 0; WB_ULONG term_len = 0; WBXMLError ret = WBXML_OK; /* Find length of input buffer */ switch (in_charset) { case WBXML_CHARSET_ISO_10646_UCS_2 : case WBXML_CHARSET_UTF_16 : /* Terminated by two NULL char ("\0\0") */ term_len = 2; if (!search_null_block(in_buf, *io_bytes, 2, &buf_len)) { return WBXML_ERROR_CHARSET_STR_LEN; } /* Add termination bytes length */ buf_len += term_len; break; default : /* Terminated by a simple NULL char ('\0') */ term_len = 1; buf_len = WBXML_STRLEN(in_buf) + term_len; break; } /* Check length found */ if (buf_len > *io_bytes) { return WBXML_ERROR_CHARSET_STR_LEN; } /* Use a temporary length var (because it is decreased) */ new_len = buf_len; /* Convert ! */ ret = wbxml_charset_conv(in_buf, &new_len, in_charset, out_buf, out_charset); /* Set input buffer length */ *io_bytes = buf_len; return ret; }
WBXML_DECLARE(WB_BOOL) wbxml_buffer_append_cstr_real(WBXMLBuffer *buffer, const WB_UTINY *data) { if ((buffer == NULL) || buffer->is_static) { return FALSE; } if (data == NULL) return TRUE; return wbxml_buffer_append_data(buffer, data, WBXML_STRLEN(data)); }
WBXML_DECLARE(WB_BOOL) wbxml_buffer_search_cstr(WBXMLBuffer *to, WB_UTINY *search, WB_ULONG pos, WB_ULONG *result) { WB_UTINY first = 0; if ((to == NULL) || (search == NULL)) return FALSE; if (result != NULL) *result = 0; /* Always "find" an empty string */ if (WBXML_STRLEN(search) == 0) return TRUE; /* Check if 'search' is greater than 'to' */ if (WBXML_STRLEN(search) > to->len) return FALSE; /* We are searching for one char */ if (WBXML_STRLEN(search) == 1) return wbxml_buffer_search_char(to, search[0], pos, result); /* For each occurrence of search's first character in to, then check if the rest of needle follows. * Stop if there are no more occurrences, or if the rest of 'search' can't possibly fit in 'to'. */ first = search[0]; while ((wbxml_buffer_search_char(to, first, pos, &pos)) && (to->len - pos >= WBXML_STRLEN(search))) { if (memcmp(to->data + pos, search, WBXML_STRLEN(search)) == 0) { if (result != NULL) *result = pos; return TRUE; } pos++; } return FALSE; }
WBXML_DECLARE(WB_LONG) wbxml_buffer_compare_cstr(WBXMLBuffer *buff, const WB_TINY *str) { WB_LONG ret = 0, len = 0; if ((buff == NULL) || (str == NULL)) { if ((buff == NULL) && (str == NULL)) return 0; if (buff == NULL) return -1; else return 1; } if (buff->len < WBXML_STRLEN(str)) len = buff->len; else len = WBXML_STRLEN(str); if (len == 0) { if (buff->len == 0 && WBXML_STRLEN(str) > 0) return -1; if (buff->len > 0 && WBXML_STRLEN(str) == 0) return 1; return 0; } if ((ret = memcmp(buff->data, str, len)) == 0) { if (buff->len < WBXML_STRLEN(str)) ret = -1; else { if (buff->len > WBXML_STRLEN(str)) ret = 1; } } return ret; }
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 */ }
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; }