Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
xml_shared.c File Reference
#include <ctype.h>
#include "xml_shared.h"
#include "xml_definitions.h"
#include <string.h>

Go to the source code of this file.

Data Structures

struct  charset_t
 

Functions

void set_charset (Octstr *document, Octstr *charset)
 
Octstrfind_charset_encoding (Octstr *document)
 
int only_blanks (const char *text)
 
int parse_charset (Octstr *os)
 
unsigned char element_check_content (xmlNodePtr node)
 
Listwml_charsets (void)
 
simple_binary_tsimple_binary_create (void)
 
void simple_binary_destroy (simple_binary_t *binary)
 
void simple_binary_output (Octstr *os, simple_binary_t *binary)
 
void parse_end (simple_binary_t **binary)
 
void output_char (int byte, simple_binary_t **binary)
 
void parse_octet_string (Octstr *os, simple_binary_t **binary)
 
void parse_inline_string (Octstr *temp, simple_binary_t **binary)
 
void output_octet_string (Octstr *os, simple_binary_t **sibxml)
 

Variables

charset_t character_sets []
 

Function Documentation

unsigned char element_check_content ( xmlNodePtr  node)

Definition at line 242 of file xml_shared.c.

References only_blanks(), WBXML_ATTR_BIT, and WBXML_CONTENT_BIT.

Referenced by parse_element(), and parse_ota_syncsettings().

243 {
244  unsigned char status_bits = 0x00;
245 
246  if ((node->children != NULL) &&
247  !((node->children->next == NULL) &&
248  (node->children->type == XML_TEXT_NODE) &&
249  (only_blanks((char *)node->children->content))))
250  status_bits = WBXML_CONTENT_BIT;
251 
252  if (node->properties != NULL)
253  status_bits = status_bits | WBXML_ATTR_BIT;
254 
255  return status_bits;
256 }
int only_blanks(const char *text)
Definition: xml_shared.c:171
#define WBXML_CONTENT_BIT
#define WBXML_ATTR_BIT
Octstr* find_charset_encoding ( Octstr document)

Definition at line 145 of file xml_shared.c.

References octstr_copy, octstr_destroy(), octstr_imm(), octstr_len(), octstr_search(), and octstr_strip_blanks().

Referenced by normalize_charset().

146 {
147  long gt = 0, enc = 0;
148  Octstr *encoding = NULL, *temp = NULL;
149 
150  enc = octstr_search(document, octstr_imm(" encoding="), 0);
151  gt = octstr_search(document, octstr_imm("?>"), 0);
152 
153  /* in case there is no encoding argument, assume always UTF-8 */
154  if (enc < 0 || enc + 10 > gt)
155  return NULL;
156 
157  temp = octstr_copy(document, enc + 10, gt - (enc + 10));
158  octstr_strip_blanks(temp);
159  encoding = octstr_copy(temp, 1, octstr_len(temp) - 2);
160  octstr_destroy(temp);
161 
162  return encoding;
163 }
long octstr_search(const Octstr *haystack, const Octstr *needle, long pos)
Definition: octstr.c:1068
void octstr_strip_blanks(Octstr *text)
Definition: octstr.c:1344
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
int only_blanks ( const char *  text)

Definition at line 171 of file xml_shared.c.

Referenced by element_check_content(), and parse_ota_syncsettings().

172 {
173  int blank = 1;
174  int j=0;
175  int len = strlen(text);
176 
177  while ((j<len) && blank) {
178  blank = blank && isspace((int)text[j]);
179  j++;
180  }
181 
182  return blank;
183 }
char * text
Definition: smsc_cimd2.c:921
void output_char ( int  byte,
simple_binary_t **  binary 
)

Definition at line 326 of file xml_shared.c.

References octstr_append_char().

Referenced by parse_attribute(), parse_element(), parse_end(), parse_ota_syncsettings(), and parse_url_value().

327 {
328  octstr_append_char((**binary).binary, byte);
329 }
void octstr_append_char(Octstr *ostr, int ch)
Definition: octstr.c:1515
void output_octet_string ( Octstr os,
simple_binary_t **  sibxml 
)

Definition at line 349 of file xml_shared.c.

References octstr_insert(), and octstr_len().

Referenced by parse_attribute(), parse_element(), and parse_octet_string().

350 {
351  octstr_insert((*sibxml)->binary, os, octstr_len((*sibxml)->binary));
352 }
void octstr_insert(Octstr *ostr1, const Octstr *ostr2, long pos)
Definition: octstr.c:1301
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
int parse_charset ( Octstr os)

Definition at line 189 of file xml_shared.c.

References charset, charset_t::charset, gw_assert(), charset_t::MIBenum, number, octstr_convert_range(), octstr_copy, octstr_destroy(), octstr_duplicate, octstr_len(), octstr_search_char(), octstr_str_compare(), and octstr_truncate().

Referenced by parse_document().

190 {
191  Octstr *charset = NULL;
192  Octstr *number = NULL;
193  int i, j, cut = 0, ret = 0;
194 
195  gw_assert(os != NULL);
196  charset = octstr_duplicate(os);
197 
198  /* The charset might be in lower case, so... */
199  octstr_convert_range(charset, 0, octstr_len(charset), toupper);
200 
201  /*
202  * The character set is handled in two parts to make things easier.
203  * The cutting.
204  */
205  if ((cut = octstr_search_char(charset, '_', 0)) > 0) {
206  number = octstr_copy(charset, cut + 1, (octstr_len(charset) - (cut + 1)));
207  octstr_truncate(charset, cut);
208  }
209  else if ((cut = octstr_search_char(charset, '-', 0)) > 0) {
210  number = octstr_copy(charset, cut + 1, (octstr_len(charset) - (cut + 1)));
211  octstr_truncate(charset, cut);
212  }
213 
214  /* And table search. */
215  for (i = 0; character_sets[i].charset != NULL; i++)
216  if (octstr_str_compare(charset, character_sets[i].charset) == 0) {
217  for (j = i; octstr_str_compare(charset,
218  character_sets[j].charset) == 0; j++)
219  if (octstr_str_compare(number, character_sets[j].nro) == 0) {
220  ret = character_sets[j].MIBenum;
221  break;
222  }
223  break;
224  }
225 
226  /* UTF-8 is the default value */
227  if (character_sets[i].charset == NULL)
228  ret = character_sets[i-1].MIBenum;
229 
230  octstr_destroy(number);
231  octstr_destroy(charset);
232 
233  return ret;
234 }
int number
Definition: smsc_cimd2.c:213
void octstr_convert_range(Octstr *ostr, long pos, long len, octstr_func_t map)
Definition: octstr.c:834
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1010
Octstr * charset
Definition: test_ota.c:68
#define octstr_duplicate(ostr)
Definition: octstr.h:187
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
gw_assert(wtls_machine->packet_to_send!=NULL)
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
charset_t character_sets[]
Definition: xml_shared.c:77
char * charset
Definition: xml_shared.c:72
int octstr_str_compare(const Octstr *ostr, const char *str)
Definition: octstr.c:971
void octstr_truncate(Octstr *ostr, int new_len)
Definition: octstr.c:1325
unsigned int MIBenum
Definition: xml_shared.c:74
void parse_end ( simple_binary_t **  binary)

Definition at line 321 of file xml_shared.c.

References output_char(), and WBXML_END.

Referenced by parse_element(), and parse_node().

322 {
323  output_char(WBXML_END, binary);
324 }
#define WBXML_END
void output_char(int byte, simple_binary_t **binary)
Definition: xml_shared.c:326
void parse_inline_string ( Octstr temp,
simple_binary_t **  binary 
)

Definition at line 339 of file xml_shared.c.

References octstr_destroy(), octstr_format(), octstr_format_append(), octstr_insert(), parse_octet_string(), WBXML_STR_END, and WBXML_STR_I.

Referenced by parse_attribute(), parse_ota_syncsettings(), parse_text(), and parse_url_value().

340 {
341  Octstr *startos;
342 
343  octstr_insert(temp, startos = octstr_format("%c", WBXML_STR_I), 0);
344  octstr_destroy(startos);
345  octstr_format_append(temp, "%c", WBXML_STR_END);
346  parse_octet_string(temp, binary);
347 }
void parse_octet_string(Octstr *os, simple_binary_t **binary)
Definition: xml_shared.c:331
#define WBXML_STR_END
#define WBXML_STR_I
void octstr_insert(Octstr *ostr1, const Octstr *ostr2, long pos)
Definition: octstr.c:1301
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
Definition: octstr.c:118
void octstr_format_append(Octstr *os, const char *fmt,...)
Definition: octstr.c:2505
void parse_octet_string ( Octstr os,
simple_binary_t **  binary 
)

Definition at line 331 of file xml_shared.c.

References output_octet_string().

Referenced by parse_cdata(), and parse_inline_string().

332 {
333  output_octet_string(os, binary);
334 }
void output_octet_string(Octstr *os, simple_binary_t **sibxml)
Definition: xml_shared.c:349
void set_charset ( Octstr document,
Octstr charset 
)

Definition at line 111 of file xml_shared.c.

References charset_to_utf8(), octstr_append_data(), octstr_copy, octstr_create, octstr_delete(), octstr_destroy(), octstr_get_cstr, octstr_len(), octstr_search(), octstr_search_char(), and text.

Referenced by ota_compile(), si_compile(), and sl_compile().

112 {
113  long gt = 0, enc = 0;
114  Octstr *encoding = NULL, *text = NULL, *temp = NULL;
115 
116  if (octstr_len(charset) == 0)
117  return;
118 
119  encoding = octstr_create(" encoding");
120  enc = octstr_search(document, encoding, 0);
121  gt = octstr_search_char(document, '>', 0);
122 
123  if (enc < 0 || enc > gt) {
124  gt++;
125  text = octstr_copy(document, gt, octstr_len(document) - gt);
126  if (charset_to_utf8(text, &temp, charset) >= 0) {
127  octstr_delete(document, gt, octstr_len(document) - gt);
128  octstr_append_data(document, octstr_get_cstr(temp),
129  octstr_len(temp));
130  }
131 
132  octstr_destroy(temp);
133  octstr_destroy(text);
134  }
135 
136  octstr_destroy(encoding);
137 }
void octstr_append_data(Octstr *ostr, const char *data, long len)
Definition: octstr.c:1495
int charset_to_utf8(Octstr *from, Octstr **to, Octstr *charset_from)
Definition: charset.c:526
long octstr_search(const Octstr *haystack, const Octstr *needle, long pos)
Definition: octstr.c:1068
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1010
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1525
char * text
Definition: smsc_cimd2.c:921
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
simple_binary_t* simple_binary_create ( void  )

Definition at line 284 of file xml_shared.c.

References simple_binary_t::binary, binary, simple_binary_t::charset, octstr_create, simple_binary_t::public_id, and simple_binary_t::wbxml_version.

Referenced by ota_compile(), si_compile(), and sl_compile().

285 {
287 
288  binary = gw_malloc(sizeof(simple_binary_t));
289 
290  binary->wbxml_version = 0x00;
291  binary->public_id = 0x00;
292  binary->charset = 0x00;
293  binary->binary = octstr_create("");
294 
295  return binary;
296 }
Octstr * binary
Definition: xml_shared.h:88
unsigned long charset
Definition: xml_shared.h:87
#define octstr_create(cstr)
Definition: octstr.h:125
unsigned char wbxml_version
Definition: xml_shared.h:85
unsigned char public_id
Definition: xml_shared.h:86
void simple_binary_destroy ( simple_binary_t binary)

Definition at line 298 of file xml_shared.c.

References simple_binary_t::binary, and octstr_destroy().

Referenced by ota_compile(), si_compile(), and sl_compile().

299 {
300  if (binary == NULL)
301  return;
302 
303  octstr_destroy(binary->binary);
304  gw_free(binary);
305 }
Octstr * binary
Definition: xml_shared.h:88
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
void simple_binary_output ( Octstr os,
simple_binary_t binary 
)

Definition at line 311 of file xml_shared.c.

References simple_binary_t::binary, simple_binary_t::charset, gw_assert(), octstr_append_uintvar(), octstr_format_append(), octstr_len(), simple_binary_t::public_id, and simple_binary_t::wbxml_version.

Referenced by ota_compile(), si_compile(), and sl_compile().

312 {
313  gw_assert(octstr_len(os) == 0);
314  octstr_format_append(os, "%c", binary->wbxml_version);
315  octstr_format_append(os, "%c", binary->public_id);
316  octstr_append_uintvar(os, binary->charset);
317  octstr_format_append(os, "%c", 0x00);
318  octstr_format_append(os, "%S", binary->binary);
319 }
Octstr * binary
Definition: xml_shared.h:88
unsigned long charset
Definition: xml_shared.h:87
unsigned char wbxml_version
Definition: xml_shared.h:85
unsigned char public_id
Definition: xml_shared.h:86
gw_assert(wtls_machine->packet_to_send!=NULL)
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
void octstr_append_uintvar(Octstr *ostr, unsigned long value)
Definition: octstr.c:1929
void octstr_format_append(Octstr *os, const char *fmt,...)
Definition: octstr.c:2505
List* wml_charsets ( void  )

Definition at line 262 of file xml_shared.c.

References charset, charset_t::charset, gwlist_append(), gwlist_create, octstr_append(), octstr_append_char(), octstr_create, and octstr_imm().

Referenced by wap_appl_init().

263 {
264  int i;
265  List *result;
266  Octstr *charset;
267 
268  result = gwlist_create();
269  for (i = 0; character_sets[i].charset != NULL; i++) {
270  charset = octstr_create(character_sets[i].charset);
271  octstr_append_char(charset, '-');
272  octstr_append(charset, octstr_imm(character_sets[i].nro));
273  gwlist_append(result, charset);
274  }
275 
276  return result;
277 }
void gwlist_append(List *list, void *item)
Definition: list.c:179
void octstr_append(Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:1502
void octstr_append_char(Octstr *ostr, int ch)
Definition: octstr.c:1515
Octstr * charset
Definition: test_ota.c:68
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
charset_t character_sets[]
Definition: xml_shared.c:77
char * charset
Definition: xml_shared.c:72
#define gwlist_create()
Definition: list.h:136
Definition: list.c:102

Variable Documentation

charset_t character_sets[]
Initial value:
= {
{ "ISO", "8859-1", 4 },
{ "ISO", "8859-2", 5 },
{ "ISO", "8859-3", 6 },
{ "ISO", "8859-4", 7 },
{ "ISO", "8859-5", 8 },
{ "ISO", "8859-6", 9 },
{ "ISO", "8859-7", 10 },
{ "ISO", "8859-8", 11 },
{ "ISO", "8859-9", 12 },
{ "WINDOWS", "1250", 2250 },
{ "WINDOWS", "1251", 2251 },
{ "WINDOWS", "1252", 2252 },
{ "WINDOWS", "1253", 2253 },
{ "WINDOWS", "1254", 2254 },
{ "WINDOWS", "1255", 2255 },
{ "WINDOWS", "1256", 2256 },
{ "WINDOWS", "1257", 2257 },
{ "WINDOWS", "1258", 2258 },
{ "UTF", "8", 106 },
{ NULL }
}

Definition at line 77 of file xml_shared.c.

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.