Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

wap_push_si_compiler.c File Reference

#include <ctype.h>
#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/debugXML.h>
#include <libxml/encoding.h>
#include "shared.h"
#include "xml_shared.h"
#include "wap_push_si_compiler.h"
#include "xml_definitions.h"

Include dependency graph for wap_push_si_compiler.c:

Include dependency graph

Go to the source code of this file.

Data Structures

struct  si_2table_t
struct  si_3table_t

Defines

#define NUMBER_OF_ELEMENTS   sizeof(si_elements)/sizeof(si_elements[0])
#define NUMBER_OF_ATTRIBUTES   sizeof(si_attributes)/sizeof(si_attributes[0])
#define NUMBER_OF_URL_VALUES   sizeof(si_URL_values)/sizeof(si_URL_values[0])

Typedefs

typedef si_2table_t si_2table_t
typedef si_3table_t si_3table_t

Functions

int parse_document (xmlDocPtr document, Octstr *charset, simple_binary_t **si_binary)
int parse_node (xmlNodePtr node, simple_binary_t **sibxml)
int parse_element (xmlNodePtr node, simple_binary_t **sibxml)
int parse_text (xmlNodePtr node, simple_binary_t **sibxml)
int parse_cdata (xmlNodePtr node, simple_binary_t **sibxml)
int parse_attribute (xmlAttrPtr attr, simple_binary_t **sibxml)
int url (int hex)
int action (int hex)
int date (int hex)
Octstrtokenize_date (Octstr *date)
void octstr_drop_trailing_zeros (Octstr **date_token)
void flag_date_length (Octstr **token)
void parse_url_value (Octstr *value, simple_binary_t **sibxml)
int si_compile (Octstr *si_doc, Octstr *charset, Octstr **si_binary)

Variables

si_2table_t si_elements []
si_3table_t si_attributes []
si_2table_t si_URL_values []


Define Documentation

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

Definition at line 136 of file wap_push_si_compiler.c.

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

Definition at line 112 of file wap_push_si_compiler.c.

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

Definition at line 149 of file wap_push_si_compiler.c.


Typedef Documentation

typedef struct si_2table_t si_2table_t
 

Definition at line 87 of file wap_push_si_compiler.c.

typedef struct si_3table_t si_3table_t
 

Definition at line 99 of file wap_push_si_compiler.c.


Function Documentation

int action int  hex  )  [static]
 

Definition at line 433 of file wap_push_si_compiler.c.

00434 {
00435     switch ((unsigned char) hex) {
00436     case 0x05: case 0x06: /* action signal-none, action signal-low */
00437     case 0x07: case 0x08: /* action signal-medium, action signal-high */
00438     case 0x09:            /* action delete */
00439     return 1;
00440     }
00441     return 0;
00442 }

int date int  hex  )  [static]
 

Definition at line 450 of file wap_push_si_compiler.c.

Referenced by parse_attribute(), and tokenize_date().

00451 {
00452     switch ((unsigned char) hex) {
00453     case 0x0a: case 0x10: /* created, si-expires */
00454     return 1;
00455     }
00456     return 0;
00457 }

void flag_date_length Octstr **  token  )  [static]
 

Definition at line 512 of file wap_push_si_compiler.c.

References octstr_destroy(), octstr_format(), octstr_insert(), octstr_len(), and token.

Referenced by tokenize_date().

00513 {
00514     Octstr *lenos;
00515 
00516     lenos = octstr_format("%c", octstr_len(*token) - 1);
00517     octstr_insert(*token, lenos, 1);
00518 
00519     octstr_destroy(lenos);
00520 }

Here is the call graph for this function:

void octstr_drop_trailing_zeros Octstr **  date_token  )  [static]
 

Definition at line 502 of file wap_push_si_compiler.c.

References octstr_delete(), octstr_get_char(), and octstr_len().

Referenced by tokenize_date().

00503 {
00504     while (1) {
00505         if (octstr_get_char(*date_token, octstr_len(*date_token) - 1) == '\0')
00506             octstr_delete(*date_token, octstr_len(*date_token) - 1, 1);
00507         else
00508             return;
00509     }
00510 }

Here is the call graph for this function:

int parse_attribute xmlAttrPtr  attr,
simple_binary_t **  sibxml
[static]
 

Definition at line 341 of file wap_push_si_compiler.c.

References action(), attr, create_octstr_from_node, date(), error(), name, octstr_compare(), octstr_create, octstr_delete(), octstr_destroy(), octstr_imm(), octstr_len(), octstr_ncompare(), output_char(), output_octet_string(), parse_inline_string(), parse_url_value(), si_attributes, si_3table_t::token, tokenize_date(), url(), and si_3table_t::value_part.

00342 {
00343     Octstr *name,
00344            *value,
00345            *valueos,
00346            *tokenized_date;
00347     unsigned char si_hex;
00348     size_t i,
00349            value_len;
00350 
00351     name = octstr_create((char *)attr->name);
00352 
00353     if (attr->children != NULL)
00354     value = create_octstr_from_node((char *)attr->children);
00355     else 
00356     value = NULL;
00357 
00358     if (value == NULL)
00359         goto error;
00360 
00361     i = 0;
00362     valueos = NULL;
00363     while (i < NUMBER_OF_ATTRIBUTES) {
00364         if (octstr_compare(name, octstr_imm(si_attributes[i].name)) == 0) {
00365         if (si_attributes[i].value_part == NULL) {
00366             break; 
00367             } else {
00368                 value_len = octstr_len(valueos = 
00369                     octstr_imm(si_attributes[i].value_part));
00370             if (octstr_ncompare(value, valueos, value_len) == 0) {
00371             break;
00372                 }
00373             }
00374         }
00375        ++i;
00376     }
00377 
00378     if (i == NUMBER_OF_ATTRIBUTES)
00379         goto error;
00380 
00381     tokenized_date = NULL;
00382     si_hex = si_attributes[i].token;
00383     if (action(si_hex)) {
00384         output_char(si_hex, sibxml);
00385     } else if (url(si_hex)) {
00386         output_char(si_hex, sibxml);
00387         octstr_delete(value, 0, octstr_len(valueos));
00388         parse_url_value(value, sibxml);
00389     } else if (date(si_hex)) {
00390         if ((tokenized_date = tokenize_date(value)) == NULL)
00391             goto error;
00392         output_char(si_hex, sibxml);
00393         output_octet_string(tokenized_date, sibxml);
00394     } else {
00395         output_char(si_hex, sibxml);
00396         parse_inline_string(value, sibxml);
00397     }  
00398 
00399     octstr_destroy(tokenized_date);
00400     octstr_destroy(name);
00401     octstr_destroy(value);
00402     return 0;
00403 
00404 error:
00405     octstr_destroy(name);
00406     octstr_destroy(value);
00407     return -1;
00408 }

Here is the call graph for this function:

int parse_cdata xmlNodePtr  node,
simple_binary_t **  sibxml
[static]
 

Definition at line 595 of file wap_push_si_compiler.c.

References create_octstr_from_node, octstr_destroy(), and parse_octet_string().

00596 {
00597     int ret = 0;
00598     Octstr *temp;
00599 
00600     temp = create_octstr_from_node((char *)node);
00601     parse_octet_string(temp, sibxml);
00602     octstr_destroy(temp);
00603 
00604     return ret;
00605 }

Here is the call graph for this function:

int parse_document xmlDocPtr  document,
Octstr charset,
simple_binary_t **  si_binary
[static]
 

Definition at line 220 of file wap_push_si_compiler.c.

References charset, octstr_create, octstr_destroy(), parse_charset(), parse_node(), simple_binary_t::public_id, and simple_binary_t::wbxml_version.

00222 {
00223     xmlNodePtr node;
00224 
00225     (*sibxml)->wbxml_version = 0x02; /* WBXML Version number 1.2  */
00226     (*sibxml)->public_id = 0x05; /* SI 1.0 Public ID */
00227     
00228     charset = octstr_create("UTF-8");
00229     (*sibxml)->charset = parse_charset(charset);
00230     octstr_destroy(charset);
00231 
00232     node = xmlDocGetRootElement(document);
00233     return parse_node(node, sibxml);
00234 }

Here is the call graph for this function:

int parse_element xmlNodePtr  node,
simple_binary_t **  sibxml
[static]
 

Definition at line 244 of file wap_push_si_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(), si_elements, si_2table_t::token, and warning().

00245 {
00246     Octstr *name,
00247            *outos;
00248     size_t i;
00249     unsigned char status_bits,
00250              si_hex;
00251     int add_end_tag;
00252     xmlAttrPtr attribute;
00253 
00254     name = octstr_create((char *)node->name);
00255     outos = NULL;
00256     if (octstr_len(name) == 0) {
00257         octstr_destroy(name);
00258         return -1;
00259     }
00260 
00261     i = 0;
00262     while (i < NUMBER_OF_ELEMENTS) {
00263         if (octstr_compare(name, octstr_imm(si_elements[i].name)) == 0)
00264             break;
00265         ++i;
00266     }
00267 
00268     status_bits = 0x00;
00269     si_hex = 0x00;
00270     add_end_tag = 0;
00271 
00272     if (i != NUMBER_OF_ELEMENTS) {
00273         si_hex = si_elements[i].token;
00274         if ((status_bits = element_check_content(node)) > 0) {
00275         si_hex = si_hex | status_bits;
00276         
00277         if ((status_bits & WBXML_CONTENT_BIT) == WBXML_CONTENT_BIT)
00278             add_end_tag = 1;
00279         }
00280         output_char(si_hex, sibxml);
00281     } else {
00282         warning(0, "unknown tag %s in SI source", octstr_get_cstr(name));
00283         si_hex = WBXML_LITERAL;
00284         if ((status_bits = element_check_content(node)) > 0) {
00285         si_hex = si_hex | status_bits;
00286         /* If this node has children, the end tag must be added after 
00287            them. */
00288         if ((status_bits & WBXML_CONTENT_BIT) == WBXML_CONTENT_BIT)
00289         add_end_tag = 1;
00290     }
00291     output_char(si_hex, sibxml);
00292         output_octet_string(outos = octstr_duplicate(name), sibxml);
00293     }
00294 
00295     if (node->properties != NULL) {
00296     attribute = node->properties;
00297     while (attribute != NULL) {
00298         parse_attribute(attribute, sibxml);
00299         attribute = attribute->next;
00300     }
00301     parse_end(sibxml);
00302     }
00303 
00304     octstr_destroy(outos);
00305     octstr_destroy(name);
00306     return add_end_tag;
00307 }

Here is the call graph for this function:

int parse_node xmlNodePtr  node,
simple_binary_t **  sibxml
[static]
 

Definition at line 529 of file wap_push_si_compiler.c.

References error(), parse_cdata(), parse_element(), parse_end(), parse_node(), parse_text(), and warning().

00530 {
00531     int status = 0;
00532     
00533     /* Call for the parser function of the node type. */
00534     switch (node->type) {
00535     case XML_ELEMENT_NODE:
00536     status = parse_element(node, sibxml);
00537     break;
00538     case XML_TEXT_NODE:
00539     status = parse_text(node, sibxml);
00540     break;
00541     case XML_CDATA_SECTION_NODE:
00542     status = parse_cdata(node, sibxml);
00543     break;
00544     case XML_COMMENT_NODE:
00545     case XML_PI_NODE:
00546     /* Comments and PIs are ignored. */
00547     break;
00548     /*
00549      * XML has also many other node types, these are not needed with 
00550      * SI. Therefore they are assumed to be an error.
00551      */
00552     default:
00553     error(0, "SI compiler: Unknown XML node in the SI source.");
00554     return -1;
00555     break;
00556     }
00557 
00558     /* 
00559      * If node is an element with content, it will need an end tag after it's
00560      * children. The status for it is returned by parse_element.
00561      */
00562     switch (status) {
00563     case 0:
00564 
00565     if (node->children != NULL)
00566         if (parse_node(node->children, sibxml) == -1)
00567         return -1;
00568     break;
00569     case 1:
00570     if (node->children != NULL)
00571         if (parse_node(node->children, sibxml) == -1)
00572         return -1;
00573     parse_end(sibxml);
00574     break;
00575 
00576     case -1: /* Something went wrong in the parsing. */
00577     return -1;
00578     default:
00579     warning(0,"SI compiler: undefined return value in a parse function.");
00580     return -1;
00581     break;
00582     }
00583 
00584     if (node->next != NULL)
00585     if (parse_node(node->next, sibxml) == -1)
00586         return -1;
00587 
00588     return 0;
00589 }

Here is the call graph for this function:

int parse_text xmlNodePtr  node,
simple_binary_t **  sibxml
[static]
 

Definition at line 315 of file wap_push_si_compiler.c.

References create_octstr_from_node, octstr_destroy(), octstr_len(), octstr_shrink_blanks(), octstr_strip_blanks(), and parse_inline_string().

00316 {
00317     Octstr *temp;
00318 
00319     temp = create_octstr_from_node((char *)node);
00320 
00321     octstr_shrink_blanks(temp);
00322     octstr_strip_blanks(temp);
00323 
00324     if (octstr_len(temp) == 0) {
00325         octstr_destroy(temp);
00326         return 0;
00327     }
00328 
00329     parse_inline_string(temp, sibxml);    
00330     octstr_destroy(temp);
00331 
00332     return 0;
00333 }

Here is the call graph for this function:

void parse_url_value Octstr value,
simple_binary_t **  sibxml
[static]
 

Definition at line 614 of file wap_push_si_compiler.c.

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

00615 {
00616     size_t i;
00617     long pos;
00618     Octstr *urlos,
00619            *first_part,
00620        *last_part;
00621     size_t first_part_len;
00622 
00623     i = 0;
00624     first_part_len = 0;
00625     first_part = NULL;
00626     last_part = NULL;
00627     while (i < NUMBER_OF_URL_VALUES) {
00628         pos = octstr_search(value, 
00629             urlos = octstr_imm(si_URL_values[i].name), 0);
00630         if (pos >= 0) {
00631         first_part = octstr_duplicate(value);
00632             octstr_delete(first_part, pos, octstr_len(first_part) - pos);
00633             first_part_len = octstr_len(first_part);
00634             parse_inline_string(first_part, sibxml);
00635             output_char(si_URL_values[i].token, sibxml);
00636             last_part = octstr_duplicate(value);
00637             octstr_delete(last_part, 0, first_part_len + octstr_len(urlos));
00638             parse_inline_string(last_part, sibxml);
00639         octstr_destroy(first_part);
00640             octstr_destroy(last_part);
00641             break;
00642         }
00643         octstr_destroy(urlos);
00644         ++i;
00645     }
00646 
00647     if (pos < 0) 
00648     parse_inline_string(value, sibxml);
00649         
00650 }

Here is the call graph for this function:

int si_compile Octstr si_doc,
Octstr charset,
Octstr **  si_binary
 

Definition at line 177 of file wap_push_si_compiler.c.

References charset, 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_si_to_sic(), and main().

00178 {
00179     simple_binary_t *sibxml;
00180     int ret;
00181     xmlDocPtr pDoc;
00182     size_t size;
00183     char *si_c_text;
00184 
00185     *si_binary = octstr_create(""); 
00186     sibxml = simple_binary_create();
00187 
00188     octstr_strip_blanks(si_doc);
00189     set_charset(si_doc, charset);
00190     size = octstr_len(si_doc);
00191     si_c_text = octstr_get_cstr(si_doc);
00192     pDoc = xmlParseMemory(si_c_text, size);
00193 
00194     ret = 0;
00195     if (pDoc) {
00196         ret = parse_document(pDoc, charset, &sibxml);
00197         simple_binary_output(*si_binary, sibxml);
00198         xmlFreeDoc(pDoc);
00199     } else {
00200         xmlFreeDoc(pDoc);
00201         octstr_destroy(*si_binary);
00202         simple_binary_destroy(sibxml);
00203         error(0, "SI: No document to parse. Probably an error in SI source");
00204         return -1;
00205     }
00206 
00207     simple_binary_destroy(sibxml);
00208 
00209     return ret;
00210 }

Here is the call graph for this function:

Octstr * tokenize_date Octstr date  )  [static]
 

Definition at line 464 of file wap_push_si_compiler.c.

References date(), flag_date_length(), octstr_append_char(), octstr_create, octstr_destroy(), octstr_drop_trailing_zeros(), octstr_get_char(), octstr_len(), octstr_set_bits(), parse_date(), and WBXML_OPAQUE.

Referenced by parse_attribute().

00465 {
00466     Octstr *date_token;
00467     long j;
00468     size_t i,
00469            date_len;
00470     unsigned char c;
00471 
00472     if (!parse_date(date)) {
00473         return NULL;
00474     }
00475 
00476     date_token = octstr_create("");
00477     octstr_append_char(date_token, WBXML_OPAQUE);
00478 
00479     i = 0;
00480     j = 0;
00481     date_len = octstr_len(date);
00482     while (i < date_len) {
00483         c = octstr_get_char(date, i);
00484         if (c != 'T' && c != 'Z' && c != '-' && c != ':') {
00485             if (isdigit(c)) {
00486                 octstr_set_bits(date_token, 4*j + 8, 4, c & 0x0f);
00487                 ++j;
00488             } else {
00489                 octstr_destroy(date_token);
00490                 return NULL;
00491             }
00492         }  
00493         ++i; 
00494     }
00495 
00496     octstr_drop_trailing_zeros(&date_token);
00497     flag_date_length(&date_token);
00498 
00499     return date_token;
00500 }

Here is the call graph for this function:

int url int  hex  )  [static]
 

Definition at line 416 of file wap_push_si_compiler.c.

00417 {
00418     switch ((unsigned char) hex) {
00419     case 0x0b:            /* href */
00420     case 0x0c: case 0x0e: /* href http://, href https:// */
00421     case 0x0d: case 0x0f: /* href http://www., href https://www. */
00422     return 1;
00423     }
00424     return 0;
00425 }


Variable Documentation

si_3table_t si_attributes[] [static]
 

Initial value:

 {
    { "action", "signal-none", 0x05 },
    { "action", "signal-low", 0x06 },
    { "action", "signal-medium", 0x07 },
    { "action", "signal-high", 0x08 },
    { "action", "delete", 0x09 },
    { "created", NULL, 0x0a },
    { "href", "https://www.", 0x0f },
    { "href", "http://www.", 0x0d },
    { "href", "https://", 0x0e },
    { "href", "http://", 0x0c },
    { "href", NULL, 0x0b },
    { "si-expires", NULL, 0x10 },
    { "si-id", NULL, 0x11 },
    { "class", NULL, 0x12 }
}

Definition at line 119 of file wap_push_si_compiler.c.

Referenced by parse_attribute().

si_2table_t si_elements[] [static]
 

Initial value:

 {
    { "si", 0x05 },
    { "indication", 0x06 },
    { "info", 0x07 },
    { "item", 0x08 }
}

Definition at line 105 of file wap_push_si_compiler.c.

Referenced by parse_element().

si_2table_t si_URL_values[] [static]
 

Initial value:

 {
  { ".com/", 0x85 },
  { ".edu/", 0x86 },
  { ".net/", 0x87 },
  { ".org/", 0x88 }
}

Definition at line 142 of file wap_push_si_compiler.c.

Referenced by parse_url_value().

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