X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=ctypescrypto%2Fbio.py;fp=ctypescrypto%2Fbio.py;h=2c54212befd62372e7fb2fd54aa0cd47a96b5090;hb=954b6dc9e3312f8d8b49f20f8466e6d2a8342f35;hp=b1700b9a869ca80dd1eba7dba1c13117782202b0;hpb=3ddc0864d900227434283bfc5956e0c4342a6b01;p=oss%2Fctypescrypto.git diff --git a/ctypescrypto/bio.py b/ctypescrypto/bio.py index b1700b9..2c54212 100644 --- a/ctypescrypto/bio.py +++ b/ctypescrypto/bio.py @@ -4,85 +4,85 @@ Interface to OpenSSL BIO library from ctypescrypto import libcrypto from ctypes import c_char_p, c_void_p, c_int, string_at, c_long,POINTER,byref, create_string_buffer 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): - """ If data is specified, creates read-only BIO. 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)) - def __del__(self): - """ - Cleans up memory used by bio - """ - libcrypto.BIO_free(self.bio) - del(self.bio) - def __str__(self): - """ - Returns current contents of buffer as byte string - """ - p=c_char_p(None) - l=libcrypto.BIO_ctrl(self.bio,3,0,byref(p)) - return string_at(p,l) - def __unicode__(self): - """ - Attempts to interpret current contents of buffer as UTF-8 string and convert it to unicode - """ - return str(self).decode("utf-8") - def read(self,length=None): - """ - Reads data from readble BIO. For test purposes. - @param length - if specifed, limits amount of data read. If not BIO is read until end of buffer - """ - if not length is None: - if not isinstance(length,(int,long)): - raise TypeError("length to read should be number") - buf=create_string_buffer(length) - readbytes=libcrypto.BIO_read(self.bio,buf,length) - if readbytes==-2: - raise NotImplementedError("Function is not supported by this BIO") - if readbytes==-1: - raise IOError - if readbytes==0: - return "" - return buf.raw[:readbytes] - else: - buf=create_string_buffer(1024) - out="" - r=1 - while r>0: - r=libcrypto.BIO_read(self.bio,buf,1024) - if r==-2: - raise NotImplementedError("Function is not supported by this BIO") - if r==-1: - raise IOError - if (r>0): - out+=buf.raw[:r] - return out + """ + 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): + """ If data is specified, creates read-only BIO. 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)) + def __del__(self): + """ + Cleans up memory used by bio + """ + libcrypto.BIO_free(self.bio) + del(self.bio) + def __str__(self): + """ + Returns current contents of buffer as byte string + """ + p=c_char_p(None) + l=libcrypto.BIO_ctrl(self.bio,3,0,byref(p)) + return string_at(p,l) + def __unicode__(self): + """ + Attempts to interpret current contents of buffer as UTF-8 string and convert it to unicode + """ + return str(self).decode("utf-8") + def read(self,length=None): + """ + Reads data from readble BIO. For test purposes. + @param length - if specifed, limits amount of data read. If not BIO is read until end of buffer + """ + if not length is None: + if not isinstance(length,(int,long)): + raise TypeError("length to read should be number") + buf=create_string_buffer(length) + readbytes=libcrypto.BIO_read(self.bio,buf,length) + if readbytes==-2: + raise NotImplementedError("Function is not supported by this BIO") + if readbytes==-1: + raise IOError + if readbytes==0: + return "" + return buf.raw[:readbytes] + else: + buf=create_string_buffer(1024) + out="" + r=1 + while r>0: + r=libcrypto.BIO_read(self.bio,buf,1024) + if r==-2: + raise NotImplementedError("Function is not supported by this BIO") + if r==-1: + raise IOError + if (r>0): + out+=buf.raw[:r] + return out - def write(self,data): - """ - Writes data to writable bio. For test purposes - """ - if isinstance(data,unicode): - data=data.encode("utf-8") - r=libcrypto.BIO_write(self.bio,data,len(data)) - if r==-2: - raise NotImplementedError("Function not supported by this BIO") - if r