libsyncml  0.5.4
sml_wbxml.c
1 /*
2  * libsyncml - A syncml protocol implementation
3  * Copyright (C) 2005 Armin Bauer <armin.bauer@opensync.org>
4  * Copyright (C) 2007-2009 Michael Bell <michael.bell@opensync.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include <libsyncml/syncml.h>
23 
24 #include <libsyncml/syncml_internals.h>
25 #include <libsyncml/sml_elements_internals.h>
26 #include <libsyncml/sml_command_internals.h>
27 #include <libsyncml/sml_session_internals.h>
28 #include "libsyncml/sml_error_internals.h"
29 
30 #ifdef ENABLE_WBXML
31 
32 #include "sml_xml_assm.h"
33 #include "sml_xml_assm_internals.h"
34 #include "sml_xml_parse.h"
35 #include "sml_xml_parse_internals.h"
36 #include "sml_wbxml.h"
37 #include "sml_wbxml_internals.h"
38 
39 SmlBool smlWbxmlConvertTo(WBXMLConvXML2WBXMLParams *params, const char *input, char **output, size_t *outputLen, SmlError** error)
40 {
41  smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p, %p)", __func__, params, input, output, outputLen, error);
42  smlAssert(input);
43  smlAssert(strlen(input));
44  smlAssert(output);
45  smlAssert(outputLen);
46  WBXMLError wberror;
47 
48  wberror = wbxml_conv_xml2wbxml((WB_UTINY*)input, (WB_UTINY**)output, (WB_ULONG*)outputLen, params);
49  if (wberror != WBXML_OK)
50  goto error;
51 
52  smlTrace(TRACE_EXIT, "%s", __func__);
53  return TRUE;
54 
55 error:
56  smlErrorSet(error, SML_ERROR_GENERIC, (char *)wbxml_errors_string(wberror));
57  smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
58  return FALSE;
59 }
60 
61 SmlBool smlWbxmlConvertFrom(WBXMLConvWBXML2XMLParams *params, const char *input, size_t inputLen, char **output, size_t *outputLen, SmlError** error)
62 {
63  smlTrace(TRACE_ENTRY, "%s(%p, %p, %i, %p, %p, %p)", __func__, params, input, inputLen, output, outputLen, error);
64  WBXMLError wberror;
65 
66  smlTrace(TRACE_INTERNAL, "WBXML2 VERSION: %s", WBXML_LIB_VERSION);
67  wberror = wbxml_conv_wbxml2xml_withlen((WB_UTINY*)input, inputLen, (WB_UTINY**)output, (WB_ULONG*)outputLen, params);
68  if (wberror != WBXML_OK)
69  goto error;
70 
71  smlTrace(TRACE_EXIT, "%s", __func__);
72  return TRUE;
73 
74 error:
75  smlErrorSet(error, SML_ERROR_GENERIC, (char *)wbxml_errors_string(wberror));
76  smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
77  return FALSE;
78 }
79 
80 static SmlBool smlWbxmlParserStart(SmlXmlParser *parser, const char *data, unsigned int size, SmlError **error)
81 {
82  smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, parser, data, size, error);
83  CHECK_ERROR_REF
84  smlAssert(parser);
85  smlAssert(data);
86  smlAssert(size);
87 
88  char *bin = smlPrintBinary(data, size);
89  smlTrace(TRACE_INTERNAL, "Wbxml input: %s", bin);
90  smlSafeCFree(&bin);
91  smlLog("received-%i.wbxml", data, size);
92 
93  /* Ignorable whitespaces must be preserved because sometimes
94  * a buggy mobile does not the its data objects correctly
95  * encapsulated into CDATA.
96  */
97  char *buffer = NULL;
98  size_t buffer_size = 0;
99  WBXMLConvWBXML2XMLParams params = {WBXML_ENCODER_XML_GEN_COMPACT, WBXML_LANG_UNKNOWN, 0, TRUE};
100  if (!smlWbxmlConvertFrom(&params, data, size, &buffer, &buffer_size, error))
101  goto error;
102  smlTrace(TRACE_INTERNAL, "converted XML message: %s", buffer);
103 
104  if (!smlXmlParserStart(parser, buffer, buffer_size, error))
105  goto error;
106  smlSafeCFree(&buffer);
107 
108  smlTrace(TRACE_EXIT, "%s", __func__);
109  return TRUE;
110 
111 error:
112  if (buffer)
113  smlSafeCFree(&buffer);
114  smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
115  return FALSE;
116 }
117 
118 SmlXmlParser *smlWbxmlParserNew(SmlParserFunctions *functions, SmlError **error)
119 {
120  smlTrace(TRACE_ENTRY, "%s(%p, %p)", __func__, functions, error);
121  CHECK_ERROR_REF
122  smlAssert(functions);
123 
124  SmlXmlParser *parser = smlXmlParserNew(functions, error);
125  if (!parser)
126  goto error;
127 
128  functions->start = (SmlParserStartFunction)smlWbxmlParserStart;
129 
130  smlTrace(TRACE_EXIT, "%s: %p", __func__, parser);
131  return parser;
132 
133 error:
134  smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
135  return NULL;
136 }
137 
138 SmlBool smlWbxmlAssemblerRun(SmlXmlAssembler *assm, char **data, unsigned int *size, SmlBool *end, SmlBool final, unsigned int maxsize, SmlError **error)
139 {
140  smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p, %i, %i, %p)", __func__, assm, data, size, end, final, maxsize, error);
141  CHECK_ERROR_REF
142  smlAssert(assm);
143  smlAssert(data);
144  smlAssert(size);
145 
146  char *buffer = NULL;
147  unsigned int buffer_size = 0;
148  if (!smlXmlAssemblerRun(assm, &buffer, &buffer_size, end, final, 0, error))
149  goto error;
150 
151  WBXMLConvXML2WBXMLParams params = {WBXML_VERSION_12, FALSE, FALSE, FALSE};
152  const char *opt = smlAssemblerGetOption(assm->assembler, "USE_STRTABLE");
153  if (opt && atoi(opt))
154  params.use_strtbl = TRUE;
155  if (!smlWbxmlConvertTo(&params, buffer, data, size, error))
156  goto error;
157  smlSafeCFree(&buffer);
158 
159  char *hex = smlPrintHex(*data, *size);
160  smlTrace(TRACE_INTERNAL, "Wbxml assembled: %s", hex);
161  smlSafeCFree(&hex);
162 
163  smlLog("sent-%i.wbxml", *data, *size);
164 
165  smlTrace(TRACE_EXIT, "%s", __func__);
166  return TRUE;
167 
168 error:
169  if (buffer)
170  smlSafeCFree(&buffer);
171  smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
172  return FALSE;
173 }
174 
175 unsigned int smlWbxmlAssemblerCheckSize(SmlXmlAssembler *assm, SmlBool headeronly, SmlError **error)
176 {
177  smlTrace(TRACE_ENTRY, "%s(%p, %i, %p)", __func__, assm, headeronly, error);
178  CHECK_ERROR_REF
179  smlAssert(assm);
180 
181  unsigned int size = 0;
182  char *data = NULL;
183  char *buffer = NULL;
184  unsigned int buffer_size = 0;
185  if (!smlXmlAssemblerRunFull(assm, &buffer, &buffer_size, NULL, TRUE, FALSE, 0, error))
186  goto error;
187 
188  WBXMLConvXML2WBXMLParams params = {WBXML_VERSION_12, FALSE, FALSE, FALSE};
189  const char *opt = smlAssemblerGetOption(assm->assembler, "USE_STRTABLE");
190  if (opt && atoi(opt))
191  params.use_strtbl = TRUE;
192  if (!smlWbxmlConvertTo(&params, buffer, &data, &size, error))
193  goto error_free_buffer;
194 
195  smlSafeCFree(&data);
196  smlSafeCFree(&buffer);
197 
198  smlTrace(TRACE_EXIT, "%s: %i", __func__, size);
199  return size;
200 
201 error_free_buffer:
202  smlSafeCFree(&buffer);
203 error:
204  smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
205  return 0;
206 }
207 
215 SmlXmlAssembler *smlWbxmlAssemblerNew(SmlAssembler *assembler, SmlAssemblerFunctions *functions, SmlError **error)
216 {
217  smlTrace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, assembler, functions, error);
218  CHECK_ERROR_REF
219 
220  SmlXmlAssembler *assm = smlXmlAssemblerNew(assembler, functions, error);
221  if (!assm)
222  goto error;
223 
224  functions->run = (SmlAssemblerRunFunction)smlWbxmlAssemblerRun;
225  functions->check_size = (SmlAssemblerCheckFunction)smlWbxmlAssemblerCheckSize;
226 
227  smlTrace(TRACE_EXIT, "%s: %p", __func__, assm);
228  return assm;
229 
230 error:
231  smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(error));
232  return NULL;
233 }
234 
235 #endif
SmlBool smlXmlParserStart(SmlXmlParser *parser, const char *data, unsigned int size, SmlError **error)
Start the parsing.
const char * smlErrorPrint(SmlError **error)
Returns the message of the error.
Definition: sml_error.c:299
const char * smlAssemblerGetOption(SmlAssembler *assm, const char *optionname)
Gets a option from the assembler.
Definition: sml_parse.c:914
char * smlPrintHex(const char *data, int len)
Used for printing binary data in just hex.
Definition: sml_support.c:271
char * smlPrintBinary(const char *data, int len)
Used for printing binary data.
Definition: sml_support.c:252
SmlXmlAssembler * smlXmlAssemblerNew(SmlAssembler *assembler, SmlAssemblerFunctions *functions, SmlError **error)
Creates a new XML assembler.
void smlTrace(SmlTraceType type, const char *message,...)
Used for tracing the application.
Definition: sml_support.c:120
void smlErrorSet(SmlError **error, SmlErrorType type, const char *format,...)
Sets the error.
Definition: sml_error.c:355
Represent an error.