00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 #include <ctype.h>
00066 #include <libxml/xmlmemory.h>
00067 #include <libxml/tree.h>
00068 #include <libxml/debugXML.h>
00069 #include <libxml/encoding.h>
00070
00071 #include "xml_shared.h"
00072 #include "wap_push_sl_compiler.h"
00073
00074
00075
00076
00077
00078
00079
00080
00081 struct sl_2table_t {
00082 char *name;
00083 unsigned char token;
00084 };
00085
00086 typedef struct sl_2table_t sl_2table_t;
00087
00088
00089
00090
00091
00092 struct sl_3table_t {
00093 char *name;
00094 char *value_part;
00095 unsigned char token;
00096 };
00097
00098 typedef struct sl_3table_t sl_3table_t;
00099
00100
00101
00102
00103
00104 static sl_2table_t sl_elements[] = {
00105 { "sl", 0x05 }
00106 };
00107
00108 #define NUMBER_OF_ELEMENTS sizeof(sl_elements)/sizeof(sl_elements[0])
00109
00110
00111
00112
00113
00114
00115 static sl_3table_t sl_attributes[] = {
00116 { "action", "execute-low", 0x05 },
00117 { "action", "execute-high", 0x06 },
00118 { "action", "cache", 0x07 },
00119 { "href", "http://", 0x09 },
00120 { "href", "http://www.", 0x0a },
00121 { "href", "https://", 0x0b },
00122 { "href", "https://www.", 0x0c },
00123 { "href", NULL, 0x08 }
00124 };
00125
00126 #define NUMBER_OF_ATTRIBUTES sizeof(sl_attributes)/sizeof(sl_attributes[0])
00127
00128
00129
00130
00131
00132
00133 static sl_2table_t sl_url_values[] = {
00134 { ".com/", 0x85 },
00135 { ".edu/", 0x86 },
00136 { ".net/", 0x87 },
00137 { ".org/", 0x88 },
00138 };
00139
00140 #define NUMBER_OF_URL_VALUES sizeof(sl_url_values)/sizeof(sl_url_values[0])
00141
00142 #include "xml_definitions.h"
00143
00144
00145
00146
00147
00148 static int parse_document(xmlDocPtr document, Octstr *charset,
00149 simple_binary_t **slbxml);
00150 static int parse_node(xmlNodePtr node, simple_binary_t **slbxml);
00151 static int parse_element(xmlNodePtr node, simple_binary_t **slbxml);
00152 static int parse_attribute(xmlAttrPtr attr, simple_binary_t **slbxml);
00153 static int url(int hex);
00154 static int action(int hex);
00155 static void parse_url_value(Octstr *value, simple_binary_t **slbxml);
00156
00157
00158
00159
00160
00161
00162 int sl_compile(Octstr *sl_doc, Octstr *charset, Octstr **sl_binary)
00163 {
00164 simple_binary_t *slbxml;
00165 int ret;
00166 xmlDocPtr pDoc;
00167 size_t size;
00168 char *sl_c_text;
00169
00170 *sl_binary = octstr_create("");
00171 slbxml = simple_binary_create();
00172
00173 octstr_strip_blanks(sl_doc);
00174 set_charset(sl_doc, charset);
00175 size = octstr_len(sl_doc);
00176 sl_c_text = octstr_get_cstr(sl_doc);
00177 pDoc = xmlParseMemory(sl_c_text, size);
00178
00179 ret = 0;
00180 if (pDoc) {
00181 ret = parse_document(pDoc, charset, &slbxml);
00182 simple_binary_output(*sl_binary, slbxml);
00183 xmlFreeDoc(pDoc);
00184 } else {
00185 xmlFreeDoc(pDoc);
00186 octstr_destroy(*sl_binary);
00187 simple_binary_destroy(slbxml);
00188 error(0, "SL: No document to parse. Probably an error in SL source");
00189 return -1;
00190 }
00191
00192 simple_binary_destroy(slbxml);
00193
00194 return ret;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 static int parse_document(xmlDocPtr document, Octstr *charset,
00206 simple_binary_t **slbxml)
00207 {
00208 xmlNodePtr node;
00209
00210 (**slbxml).wbxml_version = 0x02;
00211 (**slbxml).public_id = 0x06;
00212
00213 charset = octstr_create("UTF-8");
00214 (**slbxml).charset = parse_charset(charset);
00215 octstr_destroy(charset);
00216
00217 node = xmlDocGetRootElement(document);
00218 return parse_node(node, slbxml);
00219 }
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 static int parse_node(xmlNodePtr node, simple_binary_t **slbxml)
00231 {
00232 int status = 0;
00233
00234
00235 switch (node->type) {
00236 case XML_ELEMENT_NODE:
00237 status = parse_element(node, slbxml);
00238 break;
00239 case XML_TEXT_NODE:
00240 case XML_COMMENT_NODE:
00241 case XML_PI_NODE:
00242
00243 break;
00244
00245
00246
00247
00248 default:
00249 error(0, "SL COMPILER: Unknown XML node in the SL source.");
00250 return -1;
00251 break;
00252 }
00253
00254
00255
00256
00257
00258 switch (status) {
00259 case 0:
00260
00261 if (node->children != NULL)
00262 if (parse_node(node->children, slbxml) == -1)
00263 return -1;
00264 break;
00265 case 1:
00266 if (node->children != NULL)
00267 if (parse_node(node->children, slbxml) == -1)
00268 return -1;
00269 parse_end(slbxml);
00270 break;
00271
00272 case -1:
00273 return -1;
00274 default:
00275 warning(0,"SL compiler: undefined return value in a parse function.");
00276 return -1;
00277 break;
00278 }
00279
00280 if (node->next != NULL)
00281 if (parse_node(node->next, slbxml) == -1)
00282 return -1;
00283
00284 return 0;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 static int parse_element(xmlNodePtr node, simple_binary_t **slbxml)
00297 {
00298 Octstr *name,
00299 *nameos;
00300 unsigned char status_bits,
00301 sl_hex;
00302 int add_end_tag;
00303 xmlAttrPtr attribute;
00304
00305 name = octstr_create((char *)node->name);
00306 if (octstr_len(name) == 0) {
00307 octstr_destroy(name);
00308 return -1;
00309 }
00310
00311 status_bits = 0x00;
00312 sl_hex = 0x00;
00313 add_end_tag = 0;
00314
00315 if (octstr_compare(name, octstr_imm(sl_elements[0].name)) != 0) {
00316 warning(0, "unknown tag %s in SL source", octstr_get_cstr(name));
00317 sl_hex = WBXML_LITERAL;
00318 if ((status_bits = element_check_content(node)) > 0) {
00319 sl_hex = sl_hex | status_bits;
00320
00321
00322 if ((status_bits & WBXML_CONTENT_BIT) == WBXML_CONTENT_BIT)
00323 add_end_tag = 1;
00324 }
00325 output_char(sl_hex, slbxml);
00326 output_octet_string(nameos = octstr_duplicate(name), slbxml);
00327 octstr_destroy(nameos);
00328 } else {
00329 sl_hex = sl_elements[0].token;
00330 if ((status_bits = element_check_content(node)) > 0) {
00331 sl_hex = sl_hex | status_bits;
00332
00333 if ((status_bits & WBXML_CONTENT_BIT) == WBXML_CONTENT_BIT) {
00334 add_end_tag = 1;
00335 }
00336 output_char(sl_hex, slbxml);
00337 }
00338 }
00339
00340 if (node->properties != NULL) {
00341 attribute = node->properties;
00342 while (attribute != NULL) {
00343 parse_attribute(attribute, slbxml);
00344 attribute = attribute->next;
00345 }
00346 parse_end(slbxml);
00347 }
00348
00349 octstr_destroy(name);
00350 return add_end_tag;
00351 }
00352
00353 static int parse_attribute(xmlAttrPtr attr, simple_binary_t **slbxml)
00354 {
00355 Octstr *name,
00356 *value,
00357 *valueos;
00358 unsigned char sl_hex;
00359 size_t i,
00360 value_len;
00361
00362 name = octstr_create((char *)attr->name);
00363
00364 if (attr->children != NULL)
00365 value = create_octstr_from_node((char *)attr->children);
00366 else
00367 value = NULL;
00368
00369 if (value == NULL)
00370 goto error;
00371
00372 i = 0;
00373 valueos = NULL;
00374 while (i < NUMBER_OF_ATTRIBUTES) {
00375 if (octstr_compare(name, octstr_imm(sl_attributes[i].name)) == 0) {
00376 if (sl_attributes[i].value_part == NULL) {
00377 debug("wap.push.sl.compiler", 0, "value part was NULL");
00378 break;
00379 } else {
00380 value_len = octstr_len(valueos =
00381 octstr_imm(sl_attributes[i].value_part));
00382 if (octstr_ncompare(value, valueos, value_len) == 0) {
00383 break;
00384 }
00385 }
00386 }
00387 ++i;
00388 }
00389
00390 if (i == NUMBER_OF_ATTRIBUTES) {
00391 warning(0, "unknown attribute in SL source");
00392 goto error;
00393 }
00394
00395 sl_hex = sl_attributes[i].token;
00396 if (action(sl_hex)) {
00397 output_char(sl_hex, slbxml);
00398 } else if (url(sl_hex)) {
00399 output_char(sl_hex, slbxml);
00400 octstr_delete(value, 0, octstr_len(valueos));
00401 parse_url_value(value, slbxml);
00402 } else {
00403 output_char(sl_hex, slbxml);
00404 parse_inline_string(value, slbxml);
00405 }
00406
00407 octstr_destroy(name);
00408 octstr_destroy(value);
00409 return 0;
00410
00411 error:
00412 octstr_destroy(name);
00413 octstr_destroy(value);
00414 return -1;
00415 }
00416
00417
00418
00419
00420
00421
00422 static int url(int hex)
00423 {
00424 switch ((unsigned char) hex) {
00425 case 0x08:
00426 case 0x09: case 0x0b:
00427 case 0x0a: case 0x0c:
00428 return 1;
00429 }
00430 return 0;
00431 }
00432
00433
00434
00435
00436
00437
00438
00439 static int action(int hex)
00440 {
00441 switch ((unsigned char) hex) {
00442 case 0x05: case 0x06:
00443 case 0x07:
00444 return 1;
00445 }
00446 return 0;
00447 }
00448
00449
00450
00451
00452
00453
00454
00455
00456 static void parse_url_value(Octstr *value, simple_binary_t **slbxml)
00457 {
00458 size_t i;
00459 long pos;
00460 Octstr *urlos,
00461 *first_part,
00462 *last_part;
00463 size_t first_part_len;
00464
00465 i = 0;
00466 first_part_len = 0;
00467 first_part = NULL;
00468 last_part = NULL;
00469 while (i < NUMBER_OF_URL_VALUES) {
00470 pos = octstr_search(value,
00471 urlos = octstr_imm(sl_url_values[i].name), 0);
00472 if (pos >= 0) {
00473 first_part = octstr_duplicate(value);
00474 octstr_delete(first_part, pos, octstr_len(first_part) - pos);
00475 first_part_len = octstr_len(first_part);
00476 parse_inline_string(first_part, slbxml);
00477 output_char(sl_url_values[i].token, slbxml);
00478 last_part = octstr_duplicate(value);
00479 octstr_delete(last_part, 0, first_part_len + octstr_len(urlos));
00480 parse_inline_string(last_part, slbxml);
00481 octstr_destroy(first_part);
00482 octstr_destroy(last_part);
00483 break;
00484 }
00485 octstr_destroy(urlos);
00486 ++i;
00487 }
00488
00489 if (pos < 0)
00490 parse_inline_string(value, slbxml);
00491
00492 }
00493
00494
00495
00496
00497
00498
00499
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.