codekingpro/portable-devtools
114k
1#!/usr/bin/env python32'''3@author: David Shaw, shawd@vmware.com4 5Inspired by EAS Inspector for Fiddler6https://easinspectorforfiddler.codeplex.com7 8----- The MIT License (MIT) -----9Filename: ASWBXMLByteQueue.py10Copyright (c) 2014, David P. Shaw11 12Permission is hereby granted, free of charge, to any person obtaining a copy13of this software and associated documentation files (the "Software"), to deal14in the Software without restriction, including without limitation the rights15to use, copy, modify, merge, publish, distribute, sublicense, and/or sell16copies of the Software, and to permit persons to whom the Software is17furnished to do so, subject to the following conditions:18 19The above copyright notice and this permission notice shall be included in20all copies or substantial portions of the Software.21 22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN28THE SOFTWARE.29'''30from queue import Queue31import logging32 33class ASWBXMLByteQueue(Queue):34 35 def __init__(self, wbxmlBytes):36 37 self.bytesDequeued = 038 self.bytesEnqueued = 039 40 Queue.__init__(self)41 42 for byte in wbxmlBytes:43 self.put(byte)44 self.bytesEnqueued += 145 46 47 logging.debug("Array byte count: %d, enqueued: %d" % (self.qsize(), self.bytesEnqueued))48 49 """50 Created to debug the dequeueing of bytes51 """52 def dequeueAndLog(self):53 singleByte = self.get()54 self.bytesDequeued += 155 logging.debug("Dequeued byte 0x{0:X} ({1} total)".format(singleByte, self.bytesDequeued))56 return singleByte57 58 """59 Return true if the continuation bit is set in the byte60 """61 def checkContinuationBit(self, byteval):62 continuationBitmask = 0x8063 return (continuationBitmask & byteval) != 064 65 def dequeueMultibyteInt(self):66 iReturn = 067 singleByte = 0xFF68 69 while True:70 iReturn <<= 771 if (self.qsize() == 0):72 break73 else:74 singleByte = self.dequeueAndLog()75 iReturn += int(singleByte & 0x7F)76 if not self.checkContinuationBit(singleByte):77 return iReturn78 79 def dequeueString(self, length=None):80 if ( length != None):81 currentByte = 0x0082 strReturn = ""83 for i in range(0, length):84 # TODO: Improve this handling. We are technically UTF-8, meaning85 # that characters could be more than one byte long. This will fail if we have86 # characters outside of the US-ASCII range87 if ( self.qsize() == 0 ):88 break89 currentByte = self.dequeueAndLog()90 strReturn += chr(currentByte)91 92 else:93 currentByte = 0x0094 strReturn = ""95 while True:96 currentByte = self.dequeueAndLog()97 if (currentByte != 0x00):98 strReturn += chr(currentByte)99 else:100 break101 102 return strReturn103 