All pastes #88601 Raw Edit

robertj

public text v1 · immutable
·published 2006-07-15 04:33 UTC
rendered paste body
#!/usr/bin/pythonfrom collections import dequeclass packetWrangler:     p = deque('')        def hasData(this):        if len(this.p) > 0: return True        else: return False    def feed(this, more_yummy_p):        for x in more_yummy_p: this.p.append(x)    def getInt(this):        val,x = this.__getint()        for i in range(x):this.p.popleft()        #print val        return val    def getUint(this):        val,x = this.__getuint()        for i in range(x):this.p.popleft()        #print val        return val    def getStr(this):        s = unicode("")        while(val != 0):            val,x = __getint()            s = s + unichr(val)            for i in range(x):this.p.popleft()        return s;    def __getint(this):        if this.p[0]==-128:            return this.p[1] | (this.p[1]<<8), 2        elif this.p[0] == -127:            return this.p[1] | (this.p[1] <<8) | (this.p[2] << 16), 3        else:            return this.p[0], 1    def __getuint(this):        n = this.p[0]        size = 1        if this.p[0] & 0x80:            n = n + (this.p[1] << 7) - 0x80            if(n & (1<<14)):                n = n + (this.p[2] << 14) - (1<<14)                size = 2            if(n & (1<<21)):                n = n + (this.p[3] << 21) - (1<<21)                size = 2            if(n & (1<<28)):                n |= 0xF0000000                size = 2        return n,sizeif __name__ == '__main__':    x = packetWrangler()    g = [-128, -128, 43, 11, 29]     x.feed(g)    while x.hasData():        print x.getUint()