Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wap_push_sl_compiler.c File Reference
#include <ctype.h>
#include <inttypes.h>
#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/debugXML.h>
#include <libxml/encoding.h>
#include "xml_shared.h"
#include "wap_push_sl_compiler.h"
#include "xml_definitions.h"

Go to the source code of this file.

Data Structures

struct  sl_2table_t
 
struct  sl_3table_t
 

Macros

#define NUMBER_OF_ELEMENTS   sizeof(sl_elements)/sizeof(sl_elements[0])
 
#define NUMBER_OF_ATTRIBUTES   sizeof(sl_attributes)/sizeof(sl_attributes[0])
 
#define NUMBER_OF_URL_VALUES   sizeof(sl_url_values)/sizeof(sl_url_values[0])
 

Typedefs

typedef struct sl_2table_t sl_2table_t
 
typedef struct sl_3table_t sl_3table_t
 

Functions

static int parse_document (xmlDocPtr document, Octstr *charset, simple_binary_t **slbxml)
 
static int parse_node (xmlNodePtr node, simple_binary_t **slbxml)
 
static int parse_element (xmlNodePtr node, simple_binary_t **slbxml)
 
static int parse_attribute (xmlAttrPtr attr, simple_binary_t **slbxml)
 
static int url (int hex)
 
static int action (int hex)
 
static void parse_url_value (Octstr *value, simple_binary_t **slbxml)
 
int sl_compile (Octstr *sl_doc, Octstr *charset, Octstr **sl_binary)
 

Variables

static sl_2table_t sl_elements []
 
static sl_3table_t sl_attributes []
 
static sl_2table_t sl_url_values []
 

Macro Definition Documentation

#define NUMBER_OF_ATTRIBUTES   sizeof(sl_attributes)/sizeof(sl_attributes[0])

Definition at line 127 of file wap_push_sl_compiler.c.

Referenced by parse_attribute().

#define NUMBER_OF_ELEMENTS   sizeof(sl_elements)/sizeof(sl_elements[0])

Definition at line 109 of file wap_push_sl_compiler.c.

#define NUMBER_OF_URL_VALUES   sizeof(sl_url_values)/sizeof(sl_url_values[0])

Definition at line 141 of file wap_push_sl_compiler.c.

Referenced by parse_url_value().

Typedef Documentation

typedef struct sl_2table_t sl_2table_t

Definition at line 87 of file wap_push_sl_compiler.c.

typedef struct sl_3table_t sl_3table_t

Definition at line 99 of file wap_push_sl_compiler.c.

Function Documentation

static int action ( int  hex)
static

Definition at line 440 of file wap_push_sl_compiler.c.

Referenced by parse_attribute().

441 {
442  switch ((unsigned char) hex) {
443  case 0x05: case 0x06: /* action execute-low, action execute-high */
444  case 0x07: /* action cache */
445  return 1;
446  }
447  return 0;
448 }
static int parse_attribute ( xmlAttrPtr  attr,
simple_binary_t **  slbxml 
)
static

Definition at line 354 of file wap_push_sl_compiler.c.

References action(), create_octstr_from_node, debug(), error(), name, NUMBER_OF_ATTRIBUTES, octstr_compare(), octstr_create, octstr_delete(), octstr_destroy(), octstr_imm(), octstr_len(), octstr_ncompare(), output_char(), parse_inline_string(), parse_url_value(), sl_3table_t::token, url(), sl_3table_t::value_part, and warning().

Referenced by parse_element().

355 {
356  Octstr *name,
357  *value,
358  *valueos;
359  unsigned char sl_hex;
360  size_t i,
361  value_len;
362 
363  name = octstr_create((char *)attr->name);
364 
365  if (attr->children != NULL)
366  value = create_octstr_from_node((char *)attr->children);
367  else
368  value = NULL;
369 
370  if (value == NULL)
371  goto error;
372 
373  i = 0;
374  valueos = NULL;
375  while (i < NUMBER_OF_ATTRIBUTES) {
376  if (octstr_compare(name, octstr_imm(sl_attributes[i].name)) == 0) {
377  if (sl_attributes[i].value_part == NULL) {
378  debug("wap.push.sl.compiler", 0, "value part was NULL");
379  break;
380  } else {
381  value_len = octstr_len(valueos =
382  octstr_imm(sl_attributes[i].value_part));
383  if (octstr_ncompare(value, valueos, value_len) == 0) {
384  break;
385  }
386  }
387  }
388  ++i;
389  }
390 
391  if (i == NUMBER_OF_ATTRIBUTES) {
392  warning(0, "unknown attribute in SL source");
393  goto error;
394  }
395 
396  sl_hex = sl_attributes[i].token;
397  if (action(sl_hex)) {
398  output_char(sl_hex, slbxml);
399  } else if (url(sl_hex)) {
400  output_char(sl_hex, slbxml);
401  octstr_delete(value, 0, octstr_len(valueos));
402  parse_url_value(value, slbxml);
403  } else {
404  output_char(sl_hex, slbxml);
405  parse_inline_string(value, slbxml);
406  }
407 
408  octstr_destroy(name);
409  octstr_destroy(value);
410  return 0;
411 
412 error:
413  octstr_destroy(name);
414  octstr_destroy(value);
415  return -1;
416 }
void error(int err, const char *fmt,...)
Definition: log.c:612
static int action(int hex)
static sl_3table_t sl_attributes[]
#define NUMBER_OF_ATTRIBUTES
void parse_inline_string(Octstr *temp, simple_binary_t **binary)
Definition: xml_shared.c:339
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1525
unsigned char token
int octstr_ncompare(const Octstr *ostr1, const Octstr *ostr2, long n)
Definition: octstr.c:950
static int url(int hex)
char * name
Definition: smsc_cimd2.c:212
void warning(int err, const char *fmt,...)
Definition: log.c:624
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
static void parse_url_value(Octstr *value, simple_binary_t **slbxml)
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
void output_char(int byte, simple_binary_t **binary)
Definition: xml_shared.c:326
#define create_octstr_from_node(node)
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:869
static int parse_document ( xmlDocPtr  document,
Octstr charset,
simple_binary_t **  slbxml 
)
static

Definition at line 206 of file wap_push_sl_compiler.c.

References octstr_create, octstr_destroy(), parse_charset(), and parse_node().

Referenced by sl_compile().

208 {
209  xmlNodePtr node;
210 
211  (**slbxml).wbxml_version = 0x02; /* WBXML Version number 1.2 */
212  (**slbxml).public_id = 0x06; /* SL 1.0 Public ID */
213 
214  charset = octstr_create("UTF-8");
215  (**slbxml).charset = parse_charset(charset);
216  octstr_destroy(charset);
217 
218  node = xmlDocGetRootElement(document);
219  return parse_node(node, slbxml);
220 }
static int parse_node(xmlNodePtr node, simple_binary_t **slbxml)
int parse_charset(Octstr *os)
Definition: xml_shared.c:189
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
static int parse_element ( xmlNodePtr  node,
simple_binary_t **  slbxml 
)
static

Definition at line 297 of file wap_push_sl_compiler.c.

References element_check_content(), name, octstr_compare(), octstr_create, octstr_destroy(), octstr_duplicate, octstr_get_cstr, octstr_imm(), octstr_len(), output_char(), output_octet_string(), parse_attribute(), parse_end(), sl_2table_t::token, warning(), WBXML_CONTENT_BIT, and WBXML_LITERAL.

Referenced by parse_node().

298 {
299  Octstr *name,
300  *nameos;
301  unsigned char status_bits,
302  sl_hex;
303  int add_end_tag;
304  xmlAttrPtr attribute;
305 
306  name = octstr_create((char *)node->name);
307  if (octstr_len(name) == 0) {
308  octstr_destroy(name);
309  return -1;
310  }
311 
312  status_bits = 0x00;
313  sl_hex = 0x00;
314  add_end_tag = 0;
315 
316  if (octstr_compare(name, octstr_imm(sl_elements[0].name)) != 0) {
317  warning(0, "unknown tag %s in SL source", octstr_get_cstr(name));
318  sl_hex = WBXML_LITERAL;
319  if ((status_bits = element_check_content(node)) > 0) {
320  sl_hex = sl_hex | status_bits;
321  /* If this node has children, the end tag must be added after
322  them. */
323  if ((status_bits & WBXML_CONTENT_BIT) == WBXML_CONTENT_BIT)
324  add_end_tag = 1;
325  }
326  output_char(sl_hex, slbxml);
327  output_octet_string(nameos = octstr_duplicate(name), slbxml);
328  octstr_destroy(nameos);
329  } else {
330  sl_hex = sl_elements[0].token;
331  if ((status_bits = element_check_content(node)) > 0) {
332  sl_hex = sl_hex | status_bits;
333 
334  if ((status_bits & WBXML_CONTENT_BIT) == WBXML_CONTENT_BIT) {
335  add_end_tag = 1;
336  }
337  output_char(sl_hex, slbxml);
338  }
339  }
340 
341  if (node->properties != NULL) {
342  attribute = node->properties;
343  while (attribute != NULL) {
344  parse_attribute(attribute, slbxml);
345  attribute = attribute->next;
346  }
347  parse_end(slbxml);
348  }
349 
350  octstr_destroy(name);
351  return add_end_tag;
352 }
void output_octet_string(Octstr *os, simple_binary_t **sibxml)
Definition: xml_shared.c:349
unsigned char element_check_content(xmlNodePtr node)
Definition: xml_shared.c:242
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
#define WBXML_LITERAL
static sl_2table_t sl_elements[]
#define octstr_duplicate(ostr)
Definition: octstr.h:187
unsigned char token
char * name
Definition: smsc_cimd2.c:212
void warning(int err, const char *fmt,...)
Definition: log.c:624
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
void parse_end(simple_binary_t **binary)
Definition: xml_shared.c:321
static int parse_attribute(xmlAttrPtr attr, simple_binary_t **slbxml)
void output_char(int byte, simple_binary_t **binary)
Definition: xml_shared.c:326
#define WBXML_CONTENT_BIT
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:869
static int parse_node ( xmlNodePtr  node,
simple_binary_t **  slbxml 
)
static

Definition at line 231 of file wap_push_sl_compiler.c.

References error(), parse_element(), parse_end(), and warning().

Referenced by parse_document().

232 {
233  int status = 0;
234 
235  /* Call for the parser function of the node type. */
236  switch (node->type) {
237  case XML_ELEMENT_NODE:
238  status = parse_element(node, slbxml);
239  break;
240  case XML_TEXT_NODE:
241  case XML_COMMENT_NODE:
242  case XML_PI_NODE:
243  /* Text nodes, comments and PIs are ignored. */
244  break;
245  /*
246  * XML has also many other node types, these are not needed with
247  * SL. Therefore they are assumed to be an error.
248  */
249  default:
250  error(0, "SL COMPILER: Unknown XML node in the SL source.");
251  return -1;
252  break;
253  }
254 
255  /*
256  * If node is an element with content, it will need an end tag after it's
257  * children. The status for it is returned by parse_element.
258  */
259  switch (status) {
260  case 0:
261 
262  if (node->children != NULL)
263  if (parse_node(node->children, slbxml) == -1)
264  return -1;
265  break;
266  case 1:
267  if (node->children != NULL)
268  if (parse_node(node->children, slbxml) == -1)
269  return -1;
270  parse_end(slbxml);
271  break;
272 
273  case -1: /* Something went wrong in the parsing. */
274  return -1;
275  default:
276  warning(0,"SL compiler: undefined return value in a parse function.");
277  return -1;
278  break;
279  }
280 
281  if (node->next != NULL)
282  if (parse_node(node->next, slbxml) == -1)
283  return -1;
284 
285  return 0;
286 }
void error(int err, const char *fmt,...)
Definition: log.c:612
static int parse_element(xmlNodePtr node, simple_binary_t **slbxml)
static int parse_node(xmlNodePtr node, simple_binary_t **slbxml)
void warning(int err, const char *fmt,...)
Definition: log.c:624
void parse_end(simple_binary_t **binary)
Definition: xml_shared.c:321
static void parse_url_value ( Octstr value,
simple_binary_t **  slbxml 
)
static

Definition at line 457 of file wap_push_sl_compiler.c.

References name, NUMBER_OF_URL_VALUES, octstr_delete(), octstr_destroy(), octstr_duplicate, octstr_imm(), octstr_len(), octstr_search(), output_char(), parse_inline_string(), and token.

Referenced by parse_attribute().

458 {
459  size_t i;
460  long pos;
461  Octstr *urlos,
462  *first_part,
463  *last_part;
464  size_t first_part_len;
465 
466  i = 0;
467  first_part_len = 0;
468  first_part = NULL;
469  last_part = NULL;
470  while (i < NUMBER_OF_URL_VALUES) {
471  pos = octstr_search(value,
472  urlos = octstr_imm(sl_url_values[i].name), 0);
473  if (pos >= 0) {
474  first_part = octstr_duplicate(value);
475  octstr_delete(first_part, pos, octstr_len(first_part) - pos);
476  first_part_len = octstr_len(first_part);
477  parse_inline_string(first_part, slbxml);
478  output_char(sl_url_values[i].token, slbxml);
479  last_part = octstr_duplicate(value);
480  octstr_delete(last_part, 0, first_part_len + octstr_len(urlos));
481  parse_inline_string(last_part, slbxml);
482  octstr_destroy(first_part);
483  octstr_destroy(last_part);
484  break;
485  }
486  octstr_destroy(urlos);
487  ++i;
488  }
489 
490  if (pos < 0)
491  parse_inline_string(value, slbxml);
492 
493 }
long octstr_search(const Octstr *haystack, const Octstr *needle, long pos)
Definition: octstr.c:1068
void parse_inline_string(Octstr *temp, simple_binary_t **binary)
Definition: xml_shared.c:339
#define NUMBER_OF_URL_VALUES
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
int token
Definition: wslexer.c:157
static sl_2table_t sl_url_values[]
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1525
#define octstr_duplicate(ostr)
Definition: octstr.h:187
char * name
Definition: smsc_cimd2.c:212
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
void output_char(int byte, simple_binary_t **binary)
Definition: xml_shared.c:326
int sl_compile ( Octstr sl_doc,
Octstr charset,
Octstr **  sl_binary 
)

Definition at line 163 of file wap_push_sl_compiler.c.

References error(), octstr_create, octstr_destroy(), octstr_get_cstr, octstr_len(), octstr_strip_blanks(), parse_document(), set_charset(), simple_binary_create(), simple_binary_destroy(), simple_binary_output(), and size.

Referenced by convert_sl_to_slc(), and main().

164 {
165  simple_binary_t *slbxml;
166  int ret;
167  xmlDocPtr pDoc;
168  size_t size;
169  char *sl_c_text;
170 
171  *sl_binary = octstr_create("");
172  slbxml = simple_binary_create();
173 
174  octstr_strip_blanks(sl_doc);
175  set_charset(sl_doc, charset);
176  size = octstr_len(sl_doc);
177  sl_c_text = octstr_get_cstr(sl_doc);
178  pDoc = xmlParseMemory(sl_c_text, size);
179 
180  ret = 0;
181  if (pDoc) {
182  ret = parse_document(pDoc, charset, &slbxml);
183  simple_binary_output(*sl_binary, slbxml);
184  xmlFreeDoc(pDoc);
185  } else {
186  xmlFreeDoc(pDoc);
187  octstr_destroy(*sl_binary);
188  simple_binary_destroy(slbxml);
189  error(0, "SL: No document to parse. Probably an error in SL source");
190  return -1;
191  }
192 
193  simple_binary_destroy(slbxml);
194 
195  return ret;
196 }
void error(int err, const char *fmt,...)
Definition: log.c:612
int size
Definition: wsasm.c:84
static int parse_document(xmlDocPtr document, Octstr *charset, simple_binary_t **slbxml)
void octstr_strip_blanks(Octstr *text)
Definition: octstr.c:1344
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void simple_binary_destroy(simple_binary_t *binary)
Definition: xml_shared.c:298
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
void simple_binary_output(Octstr *os, simple_binary_t *binary)
Definition: xml_shared.c:311
void set_charset(Octstr *document, Octstr *charset)
Definition: xml_shared.c:111
simple_binary_t * simple_binary_create(void)
Definition: xml_shared.c:284
static int url ( int  hex)
static

Definition at line 423 of file wap_push_sl_compiler.c.

Referenced by parse_attribute().

424 {
425  switch ((unsigned char) hex) {
426  case 0x08: /* href */
427  case 0x09: case 0x0b: /* href http://, href https:// */
428  case 0x0a: case 0x0c: /* href http://www., href https://www. */
429  return 1;
430  }
431  return 0;
432 }

Variable Documentation

sl_3table_t sl_attributes[]
static
Initial value:
= {
{ "action", "execute-low", 0x05 },
{ "action", "execute-high", 0x06 },
{ "action", "cache", 0x07 },
{ "href", "http://", 0x09 },
{ "href", "http://www.", 0x0a },
{ "href", "https://", 0x0b },
{ "href", "https://www.", 0x0c },
{ "href", NULL, 0x08 }
}

Definition at line 116 of file wap_push_sl_compiler.c.

sl_2table_t sl_elements[]
static
Initial value:
= {
{ "sl", 0x05 }
}

Definition at line 105 of file wap_push_sl_compiler.c.

sl_2table_t sl_url_values[]
static
Initial value:
= {
{ ".com/", 0x85 },
{ ".edu/", 0x86 },
{ ".net/", 0x87 },
{ ".org/", 0x88 },
}

Definition at line 134 of file wap_push_sl_compiler.c.

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