00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <libsyncml/syncml.h>
00023
00024 #include <libsyncml/syncml_internals.h>
00025 #include <libsyncml/sml_elements_internals.h>
00026 #include <libsyncml/sml_command_internals.h>
00027 #include <libsyncml/sml_session_internals.h>
00028 #include "libsyncml/sml_error_internals.h"
00029
00030 #ifdef ENABLE_WBXML
00031
00032 #include "sml_xml_assm.h"
00033 #include "sml_xml_assm_internals.h"
00034 #include "sml_xml_parse.h"
00035 #include "sml_xml_parse_internals.h"
00036 #include "sml_wbxml.h"
00037 #include "sml_wbxml_internals.h"
00038
00039 SmlBool smlWbxmlConvertTo(WBXMLConvXML2WBXMLParams *params, const char *input, char **output, size_t *outputLen, SmlError** error)
00040 {
00041 smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p, %p)", __func__, params, input, output, outputLen, error);
00042 smlAssert(input);
00043 smlAssert(strlen(input));
00044 smlAssert(output);
00045 smlAssert(outputLen);
00046 WBXMLError wberror;
00047
00048 wberror = wbxml_conv_xml2wbxml((WB_UTINY*)input, (WB_UTINY**)output, (WB_ULONG*)outputLen, params);
00049 if (wberror != WBXML_OK)
00050 goto error;
00051
00052 smlTrace(TRACE_EXIT, "%s", __func__);
00053 return TRUE;
00054
00055 error:
00056 smlErrorSet(error, SML_ERROR_GENERIC, (char *)wbxml_errors_string(wberror));
00057 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00058 return FALSE;
00059 }
00060
00061 SmlBool smlWbxmlConvertFrom(WBXMLConvWBXML2XMLParams *params, const char *input, size_t inputLen, char **output, size_t *outputLen, SmlError** error)
00062 {
00063 smlTrace(TRACE_ENTRY, "%s(%p, %p, %i, %p, %p, %p)", __func__, params, input, inputLen, output, outputLen, error);
00064 WBXMLError wberror;
00065
00066 smlTrace(TRACE_INTERNAL, "WBXML2 VERSION: %s", WBXML_LIB_VERSION);
00067 wberror = wbxml_conv_wbxml2xml_withlen((WB_UTINY*)input, inputLen, (WB_UTINY**)output, (WB_ULONG*)outputLen, params);
00068 if (wberror != WBXML_OK)
00069 goto error;
00070
00071 smlTrace(TRACE_EXIT, "%s", __func__);
00072 return TRUE;
00073
00074 error:
00075 smlErrorSet(error, SML_ERROR_GENERIC, (char *)wbxml_errors_string(wberror));
00076 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00077 return FALSE;
00078 }
00079
00080 static SmlBool smlWbxmlParserStart(SmlXmlParser *parser, const char *data, unsigned int size, SmlError **error)
00081 {
00082 smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, parser, data, size, error);
00083 CHECK_ERROR_REF
00084 smlAssert(parser);
00085 smlAssert(data);
00086 smlAssert(size);
00087
00088 char *bin = smlPrintBinary(data, size);
00089 smlTrace(TRACE_INTERNAL, "Wbxml input: %s", bin);
00090 smlSafeCFree(&bin);
00091 smlLog("received-%i.wbxml", data, size);
00092
00093
00094
00095
00096
00097 char *buffer = NULL;
00098 size_t buffer_size = 0;
00099 WBXMLConvWBXML2XMLParams params = {WBXML_ENCODER_XML_GEN_COMPACT, WBXML_LANG_UNKNOWN, 0, TRUE};
00100 if (!smlWbxmlConvertFrom(¶ms, data, size, &buffer, &buffer_size, error))
00101 goto error;
00102 smlTrace(TRACE_INTERNAL, "converted XML message: %s", buffer);
00103
00104 if (!smlXmlParserStart(parser, buffer, buffer_size, error))
00105 goto error;
00106 smlSafeCFree(&buffer);
00107
00108 smlTrace(TRACE_EXIT, "%s", __func__);
00109 return TRUE;
00110
00111 error:
00112 if (buffer)
00113 smlSafeCFree(&buffer);
00114 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00115 return FALSE;
00116 }
00117
00118 SmlXmlParser *smlWbxmlParserNew(SmlParserFunctions *functions, SmlError **error)
00119 {
00120 smlTrace(TRACE_ENTRY, "%s(%p, %p)", __func__, functions, error);
00121 CHECK_ERROR_REF
00122 smlAssert(functions);
00123
00124 SmlXmlParser *parser = smlXmlParserNew(functions, error);
00125 if (!parser)
00126 goto error;
00127
00128 functions->start = (SmlParserStartFunction)smlWbxmlParserStart;
00129
00130 smlTrace(TRACE_EXIT, "%s: %p", __func__, parser);
00131 return parser;
00132
00133 error:
00134 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00135 return NULL;
00136 }
00137
00138 SmlBool smlWbxmlAssemblerRun(SmlXmlAssembler *assm, char **data, unsigned int *size, SmlBool *end, SmlBool final, unsigned int maxsize, SmlError **error)
00139 {
00140 smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p, %i, %i, %p)", __func__, assm, data, size, end, final, maxsize, error);
00141 CHECK_ERROR_REF
00142 smlAssert(assm);
00143 smlAssert(data);
00144 smlAssert(size);
00145
00146 char *buffer = NULL;
00147 unsigned int buffer_size = 0;
00148 if (!smlXmlAssemblerRun(assm, &buffer, &buffer_size, end, final, 0, error))
00149 goto error;
00150
00151 WBXMLConvXML2WBXMLParams params = {WBXML_VERSION_12, FALSE, FALSE, FALSE};
00152 const char *opt = smlAssemblerGetOption(assm->assembler, "USE_STRTABLE");
00153 if (opt && atoi(opt))
00154 params.use_strtbl = TRUE;
00155 if (!smlWbxmlConvertTo(¶ms, buffer, data, size, error))
00156 goto error;
00157 smlSafeCFree(&buffer);
00158
00159 char *hex = smlPrintHex(*data, *size);
00160 smlTrace(TRACE_INTERNAL, "Wbxml assembled: %s", hex);
00161 smlSafeCFree(&hex);
00162
00163 smlLog("sent-%i.wbxml", *data, *size);
00164
00165 smlTrace(TRACE_EXIT, "%s", __func__);
00166 return TRUE;
00167
00168 error:
00169 if (buffer)
00170 smlSafeCFree(&buffer);
00171 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00172 return FALSE;
00173 }
00174
00175 unsigned int smlWbxmlAssemblerCheckSize(SmlXmlAssembler *assm, SmlBool headeronly, SmlError **error)
00176 {
00177 smlTrace(TRACE_ENTRY, "%s(%p, %i, %p)", __func__, assm, headeronly, error);
00178 CHECK_ERROR_REF
00179 smlAssert(assm);
00180
00181 unsigned int size = 0;
00182 char *data = NULL;
00183 char *buffer = NULL;
00184 unsigned int buffer_size = 0;
00185 if (!smlXmlAssemblerRunFull(assm, &buffer, &buffer_size, NULL, TRUE, FALSE, 0, error))
00186 goto error;
00187
00188 WBXMLConvXML2WBXMLParams params = {WBXML_VERSION_12, FALSE, FALSE, FALSE};
00189 const char *opt = smlAssemblerGetOption(assm->assembler, "USE_STRTABLE");
00190 if (opt && atoi(opt))
00191 params.use_strtbl = TRUE;
00192 if (!smlWbxmlConvertTo(¶ms, buffer, &data, &size, error))
00193 goto error_free_buffer;
00194
00195 smlSafeCFree(&data);
00196 smlSafeCFree(&buffer);
00197
00198 smlTrace(TRACE_EXIT, "%s: %i", __func__, size);
00199 return size;
00200
00201 error_free_buffer:
00202 smlSafeCFree(&buffer);
00203 error:
00204 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00205 return 0;
00206 }
00207
00215 SmlXmlAssembler *smlWbxmlAssemblerNew(SmlAssembler *assembler, SmlAssemblerFunctions *functions, SmlError **error)
00216 {
00217 smlTrace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, assembler, functions, error);
00218 CHECK_ERROR_REF
00219
00220 SmlXmlAssembler *assm = smlXmlAssemblerNew(assembler, functions, error);
00221 if (!assm)
00222 goto error;
00223
00224 functions->run = (SmlAssemblerRunFunction)smlWbxmlAssemblerRun;
00225 functions->check_size = (SmlAssemblerCheckFunction)smlWbxmlAssemblerCheckSize;
00226
00227 smlTrace(TRACE_EXIT, "%s: %p", __func__, assm);
00228 return assm;
00229
00230 error:
00231 smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
00232 return NULL;
00233 }
00234
00235 #endif