X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=ctypescrypto%2Fbio.py;h=f8150f72cebc25db4b8604a33c88f6a1c64f727d;hb=39458a66c05b88ba2c08164d238691a59e2708c8;hp=642b96b72c140c5c74382bf01a1c5fd8823d6c55;hpb=8afcf4f352ef2f967adbe84837d4abc5e9b6ee0c;p=oss%2Fctypescrypto.git diff --git a/ctypescrypto/bio.py b/ctypescrypto/bio.py index 642b96b..f8150f7 100644 --- a/ctypescrypto/bio.py +++ b/ctypescrypto/bio.py @@ -3,12 +3,12 @@ from ctypes import c_char_p, c_void_p, c_int, string_at, c_long,POINTER,byref, c class Membio: """ Provides interface to OpenSSL memory bios - use str() to get contents of writable bio + 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 + None, creates writable BIO, contents of which can be retrieved by str() or unicode() """ if data is None: method=libcrypto.BIO_s_mem() @@ -16,13 +16,28 @@ class Membio: 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 type(length)!=type(0): raise TypeError("length to read should be number") @@ -50,13 +65,21 @@ class Membio: 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