]>
gitweb.pimeys.fr Git - NK2015_Client_Python_Alpha.git/blob - rsa_source/rsa/varblock.py
1 # -*- coding: utf-8 -*-
3 # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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
18 The VARBLOCK file format is as follows, where || denotes byte concatenation:
20 FILE := VERSION || BLOCK || BLOCK ...
22 BLOCK := LENGTH || DATA
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.
30 This file format is called the VARBLOCK format, in line with the varint format
31 used to denote the block sizes.
37 def read_varint(infile
):
38 '''Reads a varint from the file.
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
44 @param infile: the file-like object to read from. It should have a read()
46 @returns (varint, length), the read varint and the number of read bytes.
57 raise EOFError('EOF while reading varint, value is %i so far' %
61 varint
+= (byte
& 0x7F) << (7 * read_bytes
)
66 return (varint
, read_bytes
)
68 def write_varint(outfile
, value
):
69 '''Writes a varint to a file.
71 @param outfile: the file-like object to write to. It should have a write()
73 @returns the number of written bytes.
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)
85 to_write
= value
& 0x7f
91 outfile
.write(chr(to_write
))
97 def yield_varblocks(infile
):
98 '''Generator, yields each block in the input file.
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.
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')
110 version
= ord(first_char
)
111 if version
!= VARBLOCK_VERSION
:
112 raise ValueError('VARBLOCK version %i not supported' % version
)
115 (block_size
, read_bytes
) = read_varint(infile
)
117 # EOF at block boundary, that's fine.
118 if read_bytes
== 0 and block_size
== 0:
121 block
= infile
.read(block_size
)
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
))
131 def yield_fixedblocks(infile
, blocksize
):
132 '''Generator, yields each block of ``blocksize`` bytes in the input file.
134 :param infile: file to read and separate in blocks.
135 :returns: a generator that yields the contents of each block
139 block
= infile
.read(blocksize
)
141 read_bytes
= len(block
)
147 if read_bytes
< blocksize
: