]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blobdiff - ctypescrypto/bio.py
Python 3 support for modules pbkdf2 pkey ec x509
[oss/ctypescrypto.git] / ctypescrypto / bio.py
index 5040d0c1180487b55f3423492efc3be21fa39c5b..0c80263b08fb500f2e46a9ca13321127ff7d2dd2 100644 (file)
@@ -1,7 +1,7 @@
 """
 Interface to OpenSSL BIO library
 """
-from ctypescrypto import libcrypto
+from ctypescrypto import libcrypto,pyver, inttype, chartype
 from ctypes import c_char_p, c_void_p, c_int, string_at, c_long
 from ctypes import POINTER, byref, create_string_buffer
 class Membio(object):
@@ -9,27 +9,39 @@ class Membio(object):
     Provides interface to OpenSSL memory bios
     use str() or unicode() to get contents of writable bio
     use bio member to pass to libcrypto function
+    
     """
-    def __init__(self, data=None):
+    def __init__(self, data=None, clone=False):
         """
-        If data is specified, creates read-only BIO. If data is
+        If data is specified, creates read-only BIO. 
+        If clone is True, makes copy of data in the instance member
+        If data is
         None, creates writable BIO, contents of which can be retrieved
         by str() or unicode()
+        
         """
         if data is None:
             method = libcrypto.BIO_s_mem()
             self.bio = libcrypto.BIO_new(method)
         else:
-            self.bio = libcrypto.BIO_new_mem_buf(c_char_p(data), len(data))
-
+            if isinstance(data, chartype):
+                data = data.encode("utf-8")
+                clone = True
+            if clone :
+                self.data = data
+                self.bio = libcrypto.BIO_new_mem_buf(c_char_p(self.data), len(data))
+            else:
+                self.bio = libcrypto.BIO_new_mem_buf(c_char_p(data), len(data))
+                
     def __del__(self):
         """
         Cleans up memory used by bio
         """
-        libcrypto.BIO_free(self.bio)
-        del self.bio
+        if hasattr(self,'bio'):
+            libcrypto.BIO_free(self.bio)
+            del self.bio
 
-    def __str__(self):
+    def __bytes__(self):
         """
         Returns current contents of buffer as byte string
         """
@@ -42,8 +54,11 @@ class Membio(object):
         Attempts to interpret current contents of buffer as UTF-8 string
         and convert it to unicode
         """
-        return str(self).decode("utf-8")
-
+        return self.__bytes__().decode("utf-8")
+    if pyver == 2:
+        __str__ = __bytes__
+    else: 
+        __str__ = __unicode__
     def read(self, length=None):
         """
         Reads data from readble BIO. For test purposes.
@@ -51,7 +66,7 @@ class Membio(object):
         If not BIO is read until end of buffer
         """
         if not length is None:
-            if not isinstance(length, (int, long)):
+            if not isinstance(length, inttype) :
                 raise TypeError("length to read should be number")
             buf = create_string_buffer(length)
             readbytes = libcrypto.BIO_read(self.bio, buf, length)
@@ -61,11 +76,11 @@ class Membio(object):
             if readbytes == -1:
                 raise IOError
             if readbytes == 0:
-                return ""
+                return b""
             return buf.raw[:readbytes]
         else:
             buf = create_string_buffer(1024)
-            out = ""
+            out = b""
             readbytes = 1
             while readbytes > 0:
                 readbytes = libcrypto.BIO_read(self.bio, buf, 1024)
@@ -82,10 +97,14 @@ class Membio(object):
         """
         Writes data to writable bio. For test purposes
         """
-        if isinstance(data, unicode):
-            data = data.encode("utf-8")
+        if pyver == 2:
+             if isinstance(data, unicode):
+                data = data.encode("utf-8")
+             else:
+                data = str(data)
         else:
-            data = str(data)
+             if not isinstance(data, bytes): 
+                data=str(data).encode("utf-8")   
 
         written = libcrypto.BIO_write(self.bio, data, len(data))
         if written == -2: