1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
|
###############################################################################
# CODEC.py
#
# Low-level WBXML codecs functions
#
# From original code by Jonny Lamb - updated for sync-engine use 27/1/2008 by
# Dr J A Gow.
#
###############################################################################
WBXML_SWITCH_PAGE = 0x00
WBXML_END = 0x01
WBXML_ENTITY = 0x02
WBXML_STR_I = 0x03
WBXML_LITERAL = 0x04
WBXML_EXT_I_0 = 0x40
WBXML_EXT_I_1 = 0x41
WBXML_EXT_I_2 = 0x42
WBXML_PI = 0x43
WBXML_LITERAL_C = 0x44
WBXML_EXT_T_0 = 0x80
WBXML_EXT_T_1 = 0x81
WBXML_EXT_T_2 = 0x82
WBXML_STR_T = 0x83
WBXML_LITERAL_A = 0x84
WBXML_EXT_0 = 0xC1
WBXML_EXT_1 = 0xC2
WBXML_OPAQUE = 0xC3
WBXML_LITERAL_AC = 0xC4
EN_TYPE = 1
EN_TAG = 2
EN_CONTENT = 3
EN_FLAGS = 4
EN_ATTRIBUTES = 5
EN_TYPE_STARTTAG = 1
EN_TYPE_ENDTAG = 2
EN_TYPE_CONTENT = 3
EN_FLAGS_CONTENT = 1
EN_FLAGS_ATTRIBUTES = 2
WBXML_DEBUG = True
###############################################################################
# WBXMLDecoder
#
# EXPORTED
#
# This object is a generic WBXML decoder object - provide it with the binary
# string to decode from and the DTD - and the XML can be extracted by calling
# the appropriate functions
#
###############################################################################
class WBXMLDecoder(object):
# Decoder state
dtd = None
input = None
version = None
publicid = None
publicstringid = None
charsetid = None
string_table = None
tagcp = 0
attrcp = 0
unget_buffer = None
log_stack = []
def __init__(self, input, dtd):
self.input = input
self.dtd = dtd[0]
self.version = self.GetByte()
self.publicid = self.GetMBUInt()
if self.publicid == 0:
self.publicstringid = self.GetMBUInt()
self.charsetid = self.GetMBUInt()
self.string_table = self.GetStringTable()
#
# GetElement
#
# Pull down the element at this point in the WBXML stream
#
def GetElement(self):
element = self.GetToken()
if element.has_key(EN_TYPE):
if (element[EN_TYPE] == EN_TYPE_STARTTAG):
return element
elif (element[EN_TYPE] == EN_TYPE_ENDTAG):
return element
elif (element[EN_TYPE] == EN_TYPE_CONTENT):
while True:
next = self.GetToken()
if next == False:
break
elif next[EN_TYPE] == EN_CONTENT:
element[EN_CONTENT] += next[EN_CONTENT]
else:
self.UngetElement(next)
break;
return element
return False
#
# Peek
#
# Return the next element without changing the position in the
# input byte stream.
#
def Peek(self):
element = self.GetElement()
self.UngetElement(element)
return element
#
# GetElementStartTag
#
# Return the start tag for a given tag - or return false if no match
#
def GetElementStartTag(self, tag):
element = self.GetToken()
if element[EN_TYPE] == EN_TYPE_STARTTAG and element[EN_TAG] == tag:
return element
else:
self.UngetElement(element)
return False
#
# GetElementEndTag
#
# Return the end tag.
#
def GetElementEndTag(self):
element = self.GetToken()
if element[EN_TYPE] == EN_TYPE_ENDTAG:
return element
else:
self.UngetElement(element)
return False
#
# GetElementContent
#
# Return the content of an element
#
def GetElementContent(self):
element = self.GetToken()
if element[EN_TYPE] == EN_TYPE_CONTENT:
return element
else:
self.UngetElement(element)
return False
#
# GetToken
#
# Return the next token in the stream
#
def GetToken(self):
if self.unget_buffer:
element = self.unget_buffer
self.unget_buffer = False
return element
el = self._GetToken()
return el
#
# _GetToken
#
# INTERNAL
#
# Low level call to retrieve a token from the wbxml stream
#
def _GetToken(self):
element = {}
while True:
byte = self.GetByte()
if byte == None:
break
if byte == WBXML_SWITCH_PAGE:
self.tagcp = self.GetByte()
continue
elif byte == WBXML_END:
element[EN_TYPE] = EN_TYPE_ENDTAG
return element
elif byte == WBXML_ENTITY:
entity = self.GetMBUInt()
element[EN_TYPE] = EN_TYPE_CONTENT
element[EN_CONTENT] = self.EntityToCharset(entity)
return element
elif byte == WBXML_STR_I:
element[EN_TYPE] = EN_TYPE_CONTENT
element[EN_CONTENT] = self.GetTermStr()
return element
elif byte == WBXML_LITERAL:
element[EN_TYPE] = EN_TYPE_STARTTAG
element[EN_TAG] = self.GetStringTableEntry(self.GetMBUInt())
element[EN_FLAGS] = 0
return element
elif byte == WBXML_EXT_I_0 or \
byte == WBXML_EXT_I_1 or \
byte == WBXML_EXT_I_2:
self.GetTermStr()
continue
elif byte == WBXML_PI:
self.GetAttributes()
continue
elif byte == WBXML_LITERAL_C:
element[EN_TYPE] = EN_TYPE_STARTTAG
element[EN_TAG] = self.GetStringTableEntry(self.GetMBUInt())
element[EN_FLAGS] = EN_FLAGS_CONTENT
return element
elif byte == WBXML_EXT_T_0 or \
byte == WBXML_EXT_T_1 or \
byte == WBXML_EXT_T_2:
self.GetMBUInt()
continue
elif byte == WBXML_STR_T:
element[EN_TYPE] = EN_TYPE_CONTENT
element[EN_CONTENT] = self.GetStringTableEntry(self.GetMBUInt())
return element
elif byte == WBXML_LITERAL_A:
element[EN_TYPE] = EN_TYPE_STARTTAG
element[EN_TAG] = self.GetStringTableEntry(self.GetMBUInt())
element[EN_ATTRIBUTES] = self.GetAttributes()
element[EN_FLAGS] = EN_FLAGS_ATTRIBUTES
return element
elif byte == WBXML_EXT_0 or \
byte == WBXML_EXT_1:
continue
elif byte == WBXML_OPAQUE:
length = self.GetMBUInt()
element[EN_TYPE] = EN_TYPE_CONTENT
element[EN_CONTENT] = self.GetOpaque(length)
return element
elif byte == WBXML_LITERAL_AC:
element[EN_TYPE] = EN_TYPE_STARTTAG
element[EN_TAG] = self.GetStringTableEntry(self.GetMBUInt())
element[EN_ATTRIBUTES] = self.GetAttributes()
element[EN_FLAGS] = EN_FLAGS_ATTRIBUTES | EN_FLAGS_CONTENT
return element
else:
element[EN_TYPE] = EN_TYPE_STARTTAG
element[EN_TAG] = self.GetMapping(self.tagcp, byte & 0x3F)
if byte & 0x80:
flag1 = EN_FLAGS_ATTRIBUTES
else:
flag1 = 0
if byte & 0x40:
flag2 = EN_FLAGS_CONTENT
else:
flag2 = 0
element[EN_FLAGS] = flag1 | flag2
if byte & 0x80:
element[EN_ATTRIBUTES] = self.GetAttributes()
return element
return element
#
# UngetElement
#
# Put it back if we do not use it
#
def UngetElement(self, element):
if self.unget_buffer:
pass
self.unget_buffer = element
#
# GetAttributes
#
# Retrieve a list of attributes for a given tag
#
def GetAttributes(self):
attributes = []
attr = ''
while True:
byte = self.GetByte()
if len(byte) == 0:
break
if byte == WBXML_SWITCH_PAGE:
self.attrcp = self.GetByte()
break
elif byte == WBXML_END:
if attr != '':
attributes.append(self.SplitAttribute(attr))
return attributes
elif byte == WBXML_ENTITY:
entity = self.GetMBUInt()
attr += self.EntityToCharset(entity)
# return element
elif byte == WBXML_STR_I:
attr += self.GetTermStr()
# return element
elif byte == WBXML_LITERAL:
if attr != '':
attributes.append(self.SplitAttribute(attr))
attr = self.GetStringTableEntry(self.GetMBUInt())
# return element
elif byte == WBXML_EXT_I_0 or \
byte == WBXML_EXT_I_1 or \
byte == WBXML_EXT_I_2:
self.GetTermStr()
continue
elif byte == WBXML_PI or \
byte == WBXML_LITERAL_C:
return False
elif byte == WBXML_EXT_T_0 or \
byte == WBXML_EXT_T_1 or \
byte == WBXML_EXT_T_2:
self.GetMBUInt()
continue
elif byte == WBXML_STR_T:
attr += self.GetStringTableEntry(self.GetMBUInt())
return element
elif byte == WBXML_LITERAL_A:
return False
elif byte == WBXML_EXT_0 or \
byte == WBXML_EXT_1 or \
byte == WBXML_EXT_2:
continue
elif byte == WBXML_OPAQUE:
length = self.GetMBUInt()
attr += self.GetOpaque(length)
return element
elif byte == WBXML_LITERAL_AC:
return False
else:
if byte < 128:
if attr != '':
attributes.append(self.SplitAttribute(attr))
attr = ''
attr += self.GetMapping(self.attrcp, byte)
break
return attributes
#
# SplitAttribute
#
# Split attribute into name and content
#
def SplitAttribute(self, attr):
pos = attr.find(chr(61))
if pos:
attribute = (attr[0:pos], attr[(pos+1):])
else:
attribute = (attr,None)
return attribute
#
# GetTermStr
#
# Return a string up until the next null
#
def GetTermStr(self):
str = ''
while True:
input = self.GetByte()
if input == 0:
break
else:
str += chr(input)
return str
#
# GetOpaque
#
# Return up to len bytes from the input
#
def GetOpaque(self, len):
return self.input.read(len)
#
# GetByte
#
# Retrieve a byte from the input stream
#
def GetByte(self):
ch = self.input.read(1)
if len(ch) > 0:
return ord(ch)
else:
return None
#
# GetMBUInt
#
#
def GetMBUInt(self):
uint = 0
while True:
byte = self.GetByte()
uint |= byte & 0x7f
if byte & 0x80:
uint = uint << 7
else:
break
return uint
#
# GetStringTable
#
# Read and return the string table
#
def GetStringTable(self):
string_table = ''
length = self.GetMBUInt()
if length > 0:
string_table = self.input.read(length)
return string_table
#
# GetMapping
#
# Interrogate the DTD for tag and namespace code pairs
#
def GetMapping(self, cp, id):
if not self.dtd['codes'][cp] or not self.dtd['codes'][cp][id]:
return False
else:
if self.dtd['namespaces'][cp]:
return (self.dtd['namespaces'][cp],self.dtd['codes'][cp][id])
else:
return (None,self.dtd['codes'][cp][id])
###############################################################################
# WBXMLEncoder
#
# EXPORTED
#
# This object is a generic WBXML encoder object - parsing XML externally and
# calling the appropriate methods will build up a WBXML representation in the
# internal string
#
###############################################################################
class WBXMLEncoder(object):
# Encoder globals
_dtd = None
_out = None
_tagcp = None
_attrcp = None
log_stack = []
#
# Initialization
#
def __init__(self, output, dtd):
self._out = output
self._dtd = dtd[1]
self._tagcp = 0
self._attrcp = 0
self._stack = []
#
# StartWBXML
#
# Write the initial WBXML header
#
def StartWBXML(self):
# Content-Type: application/vnd.ms-sync.wbxml'
self.OutByte(0x03)
self.OutMBUInt(0x01)
self.OutMBUInt(106)
self.OutMBUInt(0x00)
#
# StartTag
#
# Call to create a new tag. Pass in a tuple of (ns,name) for tag.
#
def StartTag(self, tag, attributes=False, nocontent=False):
stackelem = {}
if not nocontent:
stackelem['tag'] = tag
stackelem['attributes'] = attributes
stackelem['nocontent'] = nocontent
stackelem['sent'] = False
self._stack.append(stackelem)
else:
self._OutputStack()
self._StartTag(tag, attributes, nocontent)
#
# EndTag
#
# Called at end of tag (only one with content)
#
def EndTag(self):
stackelem = self._stack.pop()
if stackelem['sent']:
self._EndTag()
#
# Content
#
# Called to output tag content
def Content(self, content):
content = content.replace('\0', '')
if 'x' + content == 'x':
return
self._OutputStack()
self._Content(content)
#
# _OutputStack
#
# INTERNAL
#
# Spool all stacked tags to the output file.
#
def _OutputStack(self):
for i in range(len(self._stack)):
if not self._stack[i]['sent']:
self._StartTag(self._stack[i]['tag'], self._stack[i]['attributes'], self._stack[i]['nocontent'])
self._stack[i]['sent'] = True
#
# _StartTag
#
# INTERNAL
#
# Set up a new tag and handle the DTD mappings
def _StartTag(self, tag, attributes = False, nocontent = False):
mapping = self.GetMapping(tag)
if self._tagcp != mapping['cp']:
self.OutSwitchPage(mapping['cp'])
self._tagcp = mapping['cp']
code = mapping['code']
if attributes and len(attributes) > 0:
code |= 0x80
if not nocontent:
code |= 0x40
self.OutByte(code)
if code & 0x80:
self.OutAttributes(attributes)
#
# _Content
#
# INTERNAL
#
# Send tag content to the file
#
def _Content(self, content):
self.OutByte(WBXML_STR_I)
self.OutTermStr(content)
#
# _EndTag
#
# INTERNAL
#
# Send end tag data to the file
#
def _EndTag(self):
self.OutByte(WBXML_END)
#
# OutByte
#
# Send a byte to the output stream
def OutByte(self, byte):
self._out.write(chr(byte))
#
# OutMBUInt
#
# Send a multi byte value to the output stream
#
def OutMBUInt(self, uint):
while True:
byte = uint & 0x7f
uint = uint >> 7
if uint == 0:
self.OutByte(byte)
break
else:
self.OutByte(byte | 0x80)
#
# OutTermStr
#
# Send a null terminated string to the output stream
def OutTermStr(self, content):
self._out.write(content)
self._out.write(chr(0))
#
# OutAttributes
#
# Send attributes to the stream (needs work)
#
def OutAttributes(self):
self.OutByte(WBXML_END)
#
# OutSwitchPage
#
# Send a switch page command to the stream
def OutSwitchPage(self, page):
self.OutByte(WBXML_SWITCH_PAGE)
self.OutByte(page)
#
# GetMapping
#
# Return a mapping between a tag and a DTD code pair
# 'tag' is a tuple of (namespace,tagname)
#
def GetMapping(self, tag):
mapping = {}
ns,name = tag
if ns:
cp = self._dtd['namespaces'][ns]
else:
cp = 0
code = self._dtd['codes'][cp][name]
mapping['cp'] = cp
mapping['code'] = code
return mapping
|