/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/util/bencode.py

  • Committer: Vincent Ladeuil
  • Date: 2007-11-24 14:20:59 UTC
  • mto: (3928.1.1 bzr.integration)
  • mto: This revision was merged to the branch mainline in revision 3929.
  • Revision ID: v.ladeuil+lp@free.fr-20071124142059-2114qtsgfdv8g9p1
Ssl files needed for the test https server.

* bzrlib/tests/ssl_certs/create_ssls.py: 
Script to create the ssl keys and certificates.

* bzrlib/tests/ssl_certs/server.crt: 
Server certificate signed by the certificate authority.

* bzrlib/tests/ssl_certs/server.csr: 
Server certificate signing request.

* bzrlib/tests/ssl_certs/server_without_pass.key: 
Server key usable without password.

* bzrlib/tests/ssl_certs/server_with_pass.key: 
Server key.

* bzrlib/tests/ssl_certs/ca.key: 
Certificate authority private key.

* bzrlib/tests/ssl_certs/ca.crt: 
Certificate authority certificate.

* bzrlib/tests/ssl_certs/__init__.py: 
Provide access to ssl files (keys and certificates). 

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
# publish, distribute, sublicense, and/or sell copies of the Software,
10
10
# and to permit persons to whom the Software is furnished to do so,
11
11
# subject to the following conditions:
12
 
#
 
12
13
13
# The above copyright notice and this permission notice shall be
14
14
# included in all copies or substantial portions of the Software.
15
 
#
16
 
# Modifications copyright (C) 2008 Canonical Ltd
17
 
 
18
 
from __future__ import absolute_import
19
 
 
20
 
import sys
21
 
 
22
 
 
23
 
class BDecoder(object):
24
 
 
25
 
    def __init__(self, yield_tuples=False):
26
 
        """Constructor.
27
 
 
28
 
        :param yield_tuples: if true, decode "l" elements as tuples rather than
29
 
            lists.
30
 
        """
31
 
        self.yield_tuples = yield_tuples
32
 
        decode_func = {}
33
 
        decode_func[b'l'] = self.decode_list
34
 
        decode_func[b'd'] = self.decode_dict
35
 
        decode_func[b'i'] = self.decode_int
36
 
        decode_func[b'0'] = self.decode_string
37
 
        decode_func[b'1'] = self.decode_string
38
 
        decode_func[b'2'] = self.decode_string
39
 
        decode_func[b'3'] = self.decode_string
40
 
        decode_func[b'4'] = self.decode_string
41
 
        decode_func[b'5'] = self.decode_string
42
 
        decode_func[b'6'] = self.decode_string
43
 
        decode_func[b'7'] = self.decode_string
44
 
        decode_func[b'8'] = self.decode_string
45
 
        decode_func[b'9'] = self.decode_string
46
 
        self.decode_func = decode_func
47
 
 
48
 
    def decode_int(self, x, f):
49
 
        f += 1
50
 
        newf = x.index(b'e', f)
 
15
 
16
# The Software is provided "AS IS", without warranty of any kind,
 
17
# express or implied, including but not limited to the warranties of
 
18
# merchantability,  fitness for a particular purpose and
 
19
# noninfringement. In no event shall the  authors or copyright holders
 
20
# be liable for any claim, damages or other liability, whether in an
 
21
# action of contract, tort or otherwise, arising from, out of or in
 
22
# connection with the Software or the use or other dealings in the
 
23
# Software.
 
24
 
 
25
def decode_int(x, f):
 
26
    f += 1
 
27
    newf = x.index('e', f)
 
28
    try:
51
29
        n = int(x[f:newf])
52
 
        if x[f:f+2] == b'-0':
53
 
            raise ValueError
54
 
        elif x[f:f+1] == b'0' and newf != f+1:
55
 
            raise ValueError
56
 
        return (n, newf+1)
 
30
    except (OverflowError, ValueError):
 
31
        n = long(x[f:newf])
 
32
    if x[f] == '-':
 
33
        if x[f + 1] == '0':
 
34
            raise ValueError
 
35
    elif x[f] == '0' and newf != f+1:
 
36
        raise ValueError
 
37
    return (n, newf+1)
57
38
 
58
 
    def decode_string(self, x, f):
59
 
        colon = x.index(b':', f)
 
39
def decode_string(x, f):
 
40
    colon = x.index(':', f)
 
41
    try:
60
42
        n = int(x[f:colon])
61
 
        if x[f:f+1] == b'0' and colon != f+1:
62
 
            raise ValueError
63
 
        colon += 1
64
 
        return (x[colon:colon+n], colon+n)
65
 
 
66
 
    def decode_list(self, x, f):
67
 
        r, f = [], f+1
68
 
        while x[f:f+1] != b'e':
69
 
            v, f = self.decode_func[x[f:f+1]](x, f)
70
 
            r.append(v)
71
 
        if self.yield_tuples:
72
 
            r = tuple(r)
73
 
        return (r, f + 1)
74
 
 
75
 
    def decode_dict(self, x, f):
76
 
        r, f = {}, f+1
77
 
        lastkey = None
78
 
        while x[f:f+1] != b'e':
79
 
            k, f = self.decode_string(x, f)
80
 
            if lastkey is not None and lastkey >= k:
81
 
                raise ValueError
82
 
            lastkey = k
83
 
            r[k], f = self.decode_func[x[f:f+1]](x, f)
84
 
        return (r, f + 1)
85
 
 
86
 
    def bdecode(self, x):
87
 
        if not isinstance(x, bytes):
88
 
            raise TypeError
89
 
        try:
90
 
            r, l = self.decode_func[x[:1]](x, 0)
91
 
        except (IndexError, KeyError, OverflowError) as e:
92
 
            raise ValueError(str(e))
93
 
        if l != len(x):
94
 
            raise ValueError
95
 
        return r
96
 
 
97
 
 
98
 
_decoder = BDecoder()
99
 
bdecode = _decoder.bdecode
100
 
 
101
 
_tuple_decoder = BDecoder(True)
102
 
bdecode_as_tuple = _tuple_decoder.bdecode
103
 
 
 
43
    except (OverflowError, ValueError):
 
44
        n = long(x[f:colon])
 
45
    if x[f] == '0' and colon != f+1:
 
46
        raise ValueError
 
47
    colon += 1
 
48
    return (x[colon:colon+n], colon+n)
 
49
 
 
50
def decode_list(x, f):
 
51
    r, f = [], f+1
 
52
    while x[f] != 'e':
 
53
        v, f = decode_func[x[f]](x, f)
 
54
        r.append(v)
 
55
    return (r, f + 1)
 
56
 
 
57
def decode_dict(x, f):
 
58
    r, f = {}, f+1
 
59
    lastkey = None
 
60
    while x[f] != 'e':
 
61
        k, f = decode_string(x, f)
 
62
        if lastkey >= k:
 
63
            raise ValueError
 
64
        lastkey = k
 
65
        r[k], f = decode_func[x[f]](x, f)
 
66
    return (r, f + 1)
 
67
 
 
68
decode_func = {}
 
69
decode_func['l'] = decode_list
 
70
decode_func['d'] = decode_dict
 
71
decode_func['i'] = decode_int
 
72
decode_func['0'] = decode_string
 
73
decode_func['1'] = decode_string
 
74
decode_func['2'] = decode_string
 
75
decode_func['3'] = decode_string
 
76
decode_func['4'] = decode_string
 
77
decode_func['5'] = decode_string
 
78
decode_func['6'] = decode_string
 
79
decode_func['7'] = decode_string
 
80
decode_func['8'] = decode_string
 
81
decode_func['9'] = decode_string
 
82
 
 
83
def bdecode(x):
 
84
    try:
 
85
        r, l = decode_func[x[0]](x, 0)
 
86
    except (IndexError, KeyError):
 
87
        raise ValueError
 
88
    if l != len(x):
 
89
        raise ValueError
 
90
    return r
 
91
 
 
92
 
 
93
from types import StringType, IntType, LongType, DictType, ListType, TupleType
104
94
 
105
95
class Bencached(object):
106
96
    __slots__ = ['bencoded']
108
98
    def __init__(self, s):
109
99
        self.bencoded = s
110
100
 
111
 
 
112
 
def encode_bencached(x, r):
 
101
def encode_bencached(x,r):
113
102
    r.append(x.bencoded)
114
103
 
115
 
def encode_bool(x, r):
116
 
    encode_int(int(x), r)
117
 
 
118
104
def encode_int(x, r):
119
 
    r.extend((b'i', int_to_bytes(x), b'e'))
 
105
    r.extend(('i', str(x), 'e'))
120
106
 
121
107
def encode_string(x, r):
122
 
    r.extend((int_to_bytes(len(x)), b':', x))
 
108
    r.extend((str(len(x)), ':', x))
123
109
 
124
110
def encode_list(x, r):
125
 
    r.append(b'l')
 
111
    r.append('l')
126
112
    for i in x:
127
113
        encode_func[type(i)](i, r)
128
 
    r.append(b'e')
 
114
    r.append('e')
129
115
 
130
 
def encode_dict(x, r):
131
 
    r.append(b'd')
132
 
    ilist = sorted(x.items())
 
116
def encode_dict(x,r):
 
117
    r.append('d')
 
118
    ilist = x.items()
 
119
    ilist.sort()
133
120
    for k, v in ilist:
134
 
        r.extend((int_to_bytes(len(k)), b':', k))
 
121
        r.extend((str(len(k)), ':', k))
135
122
        encode_func[type(v)](v, r)
136
 
    r.append(b'e')
 
123
    r.append('e')
137
124
 
138
125
encode_func = {}
139
126
encode_func[type(Bencached(0))] = encode_bencached
140
 
encode_func[int] = encode_int
141
 
if sys.version_info < (3,):
142
 
    encode_func[long] = encode_int
143
 
    int_to_bytes = str
144
 
else:
145
 
    def int_to_bytes(n):
146
 
        return b'%d' % n
147
 
encode_func[bytes] = encode_string
148
 
encode_func[list] = encode_list
149
 
encode_func[tuple] = encode_list
150
 
encode_func[dict] = encode_dict
151
 
encode_func[bool] = encode_bool
 
127
encode_func[IntType] = encode_int
 
128
encode_func[LongType] = encode_int
 
129
encode_func[StringType] = encode_string
 
130
encode_func[ListType] = encode_list
 
131
encode_func[TupleType] = encode_list
 
132
encode_func[DictType] = encode_dict
152
133
 
153
 
from breezy._static_tuple_py import StaticTuple
154
 
encode_func[StaticTuple] = encode_list
155
134
try:
156
 
    from breezy._static_tuple_c import StaticTuple
 
135
    from types import BooleanType
 
136
    encode_func[BooleanType] = encode_int
157
137
except ImportError:
158
138
    pass
159
 
else:
160
 
    encode_func[StaticTuple] = encode_list
161
 
 
162
139
 
163
140
def bencode(x):
164
141
    r = []
165
142
    encode_func[type(x)](x, r)
166
 
    return b''.join(r)
 
143
    return ''.join(r)
167
144