PageRenderTime 32ms CodeModel.GetById 17ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/gdata/tlslite/utils/codec.py

http://radioappz.googlecode.com/
Python | 94 lines | 76 code | 16 blank | 2 comment | 18 complexity | 77ee994fc41b3367dc7d5dc39c4c6648 MD5 | raw file
 1"""Classes for reading/writing binary data (such as TLS records)."""
 2
 3from compat import *
 4
 5class Writer:
 6    def __init__(self, length=0):
 7        #If length is zero, then this is just a "trial run" to determine length
 8        self.index = 0
 9        self.bytes = createByteArrayZeros(length)
10
11    def add(self, x, length):
12        if self.bytes:
13            newIndex = self.index+length-1
14            while newIndex >= self.index:
15                self.bytes[newIndex] = x & 0xFF
16                x >>= 8
17                newIndex -= 1
18        self.index += length
19
20    def addFixSeq(self, seq, length):
21        if self.bytes:
22            for e in seq:
23                self.add(e, length)
24        else:
25            self.index += len(seq)*length
26
27    def addVarSeq(self, seq, length, lengthLength):
28        if self.bytes:
29            self.add(len(seq)*length, lengthLength)
30            for e in seq:
31                self.add(e, length)
32        else:
33            self.index += lengthLength + (len(seq)*length)
34
35
36class Parser:
37    def __init__(self, bytes):
38        self.bytes = bytes
39        self.index = 0
40
41    def get(self, length):
42        if self.index + length > len(self.bytes):
43            raise SyntaxError()
44        x = 0
45        for count in range(length):
46            x <<= 8
47            x |= self.bytes[self.index]
48            self.index += 1
49        return x
50
51    def getFixBytes(self, lengthBytes):
52        bytes = self.bytes[self.index : self.index+lengthBytes]
53        self.index += lengthBytes
54        return bytes
55
56    def getVarBytes(self, lengthLength):
57        lengthBytes = self.get(lengthLength)
58        return self.getFixBytes(lengthBytes)
59
60    def getFixList(self, length, lengthList):
61        l = [0] * lengthList
62        for x in range(lengthList):
63            l[x] = self.get(length)
64        return l
65
66    def getVarList(self, length, lengthLength):
67        lengthList = self.get(lengthLength)
68        if lengthList % length != 0:
69            raise SyntaxError()
70        lengthList = int(lengthList/length)
71        l = [0] * lengthList
72        for x in range(lengthList):
73            l[x] = self.get(length)
74        return l
75
76    def startLengthCheck(self, lengthLength):
77        self.lengthCheck = self.get(lengthLength)
78        self.indexCheck = self.index
79
80    def setLengthCheck(self, length):
81        self.lengthCheck = length
82        self.indexCheck = self.index
83
84    def stopLengthCheck(self):
85        if (self.index - self.indexCheck) != self.lengthCheck:
86            raise SyntaxError()
87
88    def atLengthCheck(self):
89        if (self.index - self.indexCheck) < self.lengthCheck:
90            return False
91        elif (self.index - self.indexCheck) == self.lengthCheck:
92            return True
93        else:
94            raise SyntaxError()