]> gitweb.pimeys.fr Git - NK2015_Client_Python_Alpha.git/blob - rsa_source/rsa/varblock.py
on ajoute le module rsa car le client aussi en a besoin
[NK2015_Client_Python_Alpha.git] / rsa_source / rsa / varblock.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 '''VARBLOCK file support
17
18 The VARBLOCK file format is as follows, where || denotes byte concatenation:
19
20 FILE := VERSION || BLOCK || BLOCK ...
21
22 BLOCK := LENGTH || DATA
23
24 LENGTH := varint-encoded length of the subsequent data. Varint comes from
25 Google Protobuf, and encodes an integer into a variable number of bytes.
26 Each byte uses the 7 lowest bits to encode the value. The highest bit set
27 to 1 indicates the next byte is also part of the varint. The last byte will
28 have this bit set to 0.
29
30 This file format is called the VARBLOCK format, in line with the varint format
31 used to denote the block sizes.
32
33 '''
34
35 VARBLOCK_VERSION = 1
36
37 def read_varint(infile):
38 '''Reads a varint from the file.
39
40 When the first byte to be read indicates EOF, (0, 0) is returned. When an
41 EOF occurs when at least one byte has been read, an EOFError exception is
42 raised.
43
44 @param infile: the file-like object to read from. It should have a read()
45 method.
46 @returns (varint, length), the read varint and the number of read bytes.
47 '''
48
49 varint = 0
50 read_bytes = 0
51
52 while True:
53 char = infile.read(1)
54 if len(char) == 0:
55 if read_bytes == 0:
56 return (0, 0)
57 raise EOFError('EOF while reading varint, value is %i so far' %
58 varint)
59
60 byte = ord(char)
61 varint += (byte & 0x7F) << (7 * read_bytes)
62
63 read_bytes += 1
64
65 if not byte & 0x80:
66 return (varint, read_bytes)
67
68 def write_varint(outfile, value):
69 '''Writes a varint to a file.
70
71 @param outfile: the file-like object to write to. It should have a write()
72 method.
73 @returns the number of written bytes.
74 '''
75
76 # there is a big difference between 'write the value 0' (this case) and
77 # 'there is nothing left to write' (the false-case of the while loop)
78
79 if value == 0:
80 outfile.write('\x00')
81 return 1
82
83 written_bytes = 0
84 while value > 0:
85 to_write = value & 0x7f
86 value = value >> 7
87
88 if value > 0:
89 to_write |= 0x80
90
91 outfile.write(chr(to_write))
92 written_bytes += 1
93
94 return written_bytes
95
96
97 def yield_varblocks(infile):
98 '''Generator, yields each block in the input file.
99
100 @param infile: file to read, is expected to have the VARBLOCK format as
101 described in the module's docstring.
102 @yields the contents of each block.
103 '''
104
105 # Check the version number
106 first_char = infile.read(1)
107 if len(first_char) == 0:
108 raise EOFError('Unable to read VARBLOCK version number')
109
110 version = ord(first_char)
111 if version != VARBLOCK_VERSION:
112 raise ValueError('VARBLOCK version %i not supported' % version)
113
114 while True:
115 (block_size, read_bytes) = read_varint(infile)
116
117 # EOF at block boundary, that's fine.
118 if read_bytes == 0 and block_size == 0:
119 break
120
121 block = infile.read(block_size)
122
123 read_size = len(block)
124 if read_size != block_size:
125 raise EOFError('Block size is %i, but could read only %i bytes' %
126 (block_size, read_size))
127
128
129 yield block
130
131 def yield_fixedblocks(infile, blocksize):
132 '''Generator, yields each block of ``blocksize`` bytes in the input file.
133
134 :param infile: file to read and separate in blocks.
135 :returns: a generator that yields the contents of each block
136 '''
137
138 while True:
139 block = infile.read(blocksize)
140
141 read_bytes = len(block)
142 if read_bytes == 0:
143 break
144
145 yield block
146
147 if read_bytes < blocksize:
148 break
149