summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rsa/blocks.py91
-rw-r--r--tests/test_blocks.py58
2 files changed, 149 insertions, 0 deletions
diff --git a/rsa/blocks.py b/rsa/blocks.py
new file mode 100644
index 0000000..f1072b8
--- /dev/null
+++ b/rsa/blocks.py
@@ -0,0 +1,91 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+'''Large file support
+
+ - break a file into smaller blocks, and encrypt them, and store the
+ encrypted blocks in another file.
+
+ - take such an encrypted files, decrypt its blocks, and reconstruct the
+ original file.
+
+The encrypted file format is as follows, where || denotes byte concatenation:
+
+ FILE := VERSION || BLOCK || BLOCK ...
+
+ BLOCK := LENGTH || DATA
+
+ LENGTH := varint-encoded length of the subsequent data. Varint comes from
+ Google Protobuf, and encodes an integer into a variable number of bytes.
+ Each byte uses the 7 lowest bits to encode the value. The highest bit set
+ to 1 indicates the next byte is also part of the varint. The last byte will
+ have this bit set to 0.
+
+'''
+
+def read_varint(infile):
+ '''Reads a varint from the file.
+
+ @param infile: the file-like object to read from. It should have a read()
+ method.
+ @returns (varint, length), the read varint and the number of read bytes.
+ '''
+
+ varint = 0
+ read_bytes = 0
+
+ while True:
+ char = infile.read(1)
+ if len(char) == 0:
+ raise EOFError('EOF while reading varint')
+
+ byte = ord(char)
+ varint += (byte & 0x7F) << (7 * read_bytes)
+
+ read_bytes += 1
+
+ if not byte & 0x80:
+ return (varint, read_bytes)
+
+
+def write_varint(outfile, value):
+ '''Writes a varint to a file.
+
+ @param outfile: the file-like object to write to. It should have a write()
+ method.
+ @returns the number of written bytes.
+ '''
+
+ # there is a big difference between 'write the value 0' (this case) and
+ # 'there is nothing left to write' (the false-case of the while loop)
+
+ if value == 0:
+ outfile.write('\x00')
+ return 1
+
+ written_bytes = 0
+ while value > 0:
+ to_write = value & 0x7f
+ value = value >> 7
+
+ if value > 0:
+ to_write |= 0x80
+
+ outfile.write(chr(to_write))
+ written_bytes += 1
+
+ return written_bytes
+
+
diff --git a/tests/test_blocks.py b/tests/test_blocks.py
new file mode 100644
index 0000000..be7b640
--- /dev/null
+++ b/tests/test_blocks.py
@@ -0,0 +1,58 @@
+'''Tests block operations.'''
+
+from StringIO import StringIO
+import unittest
+
+from rsa import blocks
+
+class VarintTest(unittest.TestCase):
+
+ def test_read_varint(self):
+
+ encoded = '\xac\x02crummy'
+ infile = StringIO(encoded)
+
+ (decoded, read) = blocks.read_varint(infile)
+
+ # Test the returned values
+ self.assertEqual(300, decoded)
+ self.assertEqual(2, read)
+
+ # The rest of the file should be untouched
+ self.assertEqual('crummy', infile.read())
+
+ def test_read_zero(self):
+
+ encoded = '\x00crummy'
+ infile = StringIO(encoded)
+
+ (decoded, read) = blocks.read_varint(infile)
+
+ # Test the returned values
+ self.assertEqual(0, decoded)
+ self.assertEqual(1, read)
+
+ # The rest of the file should be untouched
+ self.assertEqual('crummy', infile.read())
+
+ def test_write_varint(self):
+
+ expected = '\xac\x02'
+ outfile = StringIO()
+
+ written = blocks.write_varint(outfile, 300)
+
+ # Test the returned values
+ self.assertEqual(expected, outfile.getvalue())
+ self.assertEqual(2, written)
+
+
+ def test_write_zero(self):
+
+ outfile = StringIO()
+ written = blocks.write_varint(outfile, 0)
+
+ # Test the returned values
+ self.assertEqual('\x00', outfile.getvalue())
+ self.assertEqual(1, written)
+