Ejemplo n.º 1
0
    def test_encode_decimal(self, space, api):
        with rffi.scoped_unicode2wcharp(u' (12, 35 ABC)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                res = api.PyUnicode_EncodeDecimal(u, 13, buf.raw, None)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == ' (12, 35 ABC)'

        with rffi.scoped_unicode2wcharp(u' (12, \u1234\u1235)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                res = api.PyUnicode_EncodeDecimal(u, 9, buf.raw, None)
        assert res == -1
        api.PyErr_Clear()

        with rffi.scoped_unicode2wcharp(u' (12, \u1234\u1235)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                with rffi.scoped_str2charp("replace") as errors:
                    res = api.PyUnicode_EncodeDecimal(u, 9, buf.raw, errors)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == " (12, ??)"

        with rffi.scoped_unicode2wcharp(u'12\u1234') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                with rffi.scoped_str2charp("xmlcharrefreplace") as errors:
                    res = api.PyUnicode_EncodeDecimal(u, 3, buf.raw, errors)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == "12ሴ"
Ejemplo n.º 2
0
    def test_encode_decimal(self, space, api):
        with rffi.scoped_unicode2wcharp(u' (12, 35 ABC)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                res = api.PyUnicode_EncodeDecimal(u, 13, buf.raw, None)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == ' (12, 35 ABC)'

        with rffi.scoped_unicode2wcharp(u' (12, \u1234\u1235)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                res = api.PyUnicode_EncodeDecimal(u, 9, buf.raw, None)
        assert res == -1
        api.PyErr_Clear()

        with rffi.scoped_unicode2wcharp(u' (12, \u1234\u1235)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                with rffi.scoped_str2charp("replace") as errors:
                    res = api.PyUnicode_EncodeDecimal(u, 9, buf.raw,
                                                      errors)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == " (12, ??)"

        with rffi.scoped_unicode2wcharp(u'12\u1234') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                with rffi.scoped_str2charp("xmlcharrefreplace") as errors:
                    res = api.PyUnicode_EncodeDecimal(u, 3, buf.raw,
                                                      errors)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == "12ሴ"
Ejemplo n.º 3
0
    def test_encode_decimal(self, space):
        with rffi.scoped_unicode2wcharp(u' (12, 35 ABC)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                res = PyUnicode_EncodeDecimal(space, u, 13, buf.raw, None)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == ' (12, 35 ABC)'

        with rffi.scoped_unicode2wcharp(u' (12, \u1234\u1235)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                with pytest.raises(OperationError):
                    PyUnicode_EncodeDecimal(space, u, 9, buf.raw, None)

        with rffi.scoped_unicode2wcharp(u' (12, \u1234\u1235)') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                with rffi.scoped_str2charp("replace") as errors:
                    res = PyUnicode_EncodeDecimal(space, u, 9, buf.raw, errors)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == " (12, ??)"

        with rffi.scoped_unicode2wcharp(u'12\u1234') as u:
            with rffi.scoped_alloc_buffer(20) as buf:
                with rffi.scoped_str2charp("xmlcharrefreplace") as errors:
                    res = PyUnicode_EncodeDecimal(space, u, 3, buf.raw, errors)
                s = rffi.charp2str(buf.raw)
        assert res == 0
        assert s == "12ሴ"
Ejemplo n.º 4
0
def freshKeypair():
    """
    Make a fresh keypair.
    """

    publicSize = intmask(cryptoBoxPublickeybytes())
    secretSize = intmask(cryptoBoxSecretkeybytes())

    with rffi.scoped_alloc_buffer(publicSize) as public:
        with rffi.scoped_alloc_buffer(secretSize) as secret:
            rv = cryptoBoxKeypair(public.raw, secret.raw)
            if rv:
                raise SodiumError("crypto_box_keypair: %d" % rv)
            return public.str(publicSize), secret.str(secretSize)
Ejemplo n.º 5
0
 def READ(self, size):
     from rpython.rlib._rsocket_rffi import socketrecv, geterrno
     with rffi.scoped_alloc_buffer(size) as buf:
         length = socketrecv(self.fd, buf.raw, buf.size, 0)
         if length < 0:
             raise WindowsError(geterrno(), "recv")
         return buf.str(length)
Ejemplo n.º 6
0
 def recvfrom(self, buffersize, flags=0):
     """Like recv(buffersize, flags) but also return the sender's
     address."""
     read_bytes = -1
     timeout = self._select(False)
     if timeout == 1:
         raise SocketTimeout
     elif timeout == 0:
         with rffi.scoped_alloc_buffer(buffersize) as buf:
             address, addr_p, addrlen_p = self._addrbuf()
             try:
                 read_bytes = _c.recvfrom(self.fd, buf.raw, buffersize, flags,
                                          addr_p, addrlen_p)
                 addrlen = rffi.cast(lltype.Signed, addrlen_p[0])
             finally:
                 lltype.free(addrlen_p, flavor='raw')
                 address.unlock()
             if read_bytes >= 0:
                 if addrlen:
                     address.addrlen = addrlen
                 else:
                     address = None
                 data = buf.str(read_bytes)
                 return (data, address)
     raise self.error_handler()
Ejemplo n.º 7
0
 def recvfrom(self, buffersize, flags=0):
     """Like recv(buffersize, flags) but also return the sender's
     address."""
     read_bytes = -1
     timeout = self._select(False)
     if timeout == 1:
         raise SocketTimeout
     elif timeout == 0:
         with rffi.scoped_alloc_buffer(buffersize) as buf:
             address, addr_p, addrlen_p = self._addrbuf()
             try:
                 read_bytes = _c.recvfrom(self.fd, buf.raw, buffersize, flags,
                                          addr_p, addrlen_p)
                 addrlen = rffi.cast(lltype.Signed, addrlen_p[0])
             finally:
                 lltype.free(addrlen_p, flavor='raw')
                 address.unlock()
             if read_bytes >= 0:
                 if addrlen:
                     address.addrlen = addrlen
                 else:
                     address = None
                 data = buf.str(read_bytes)
                 return (data, address)
     raise self.error_handler()
Ejemplo n.º 8
0
 def READ(self, size):
     from rpython.rlib._rsocket_rffi import socketrecv, geterrno
     with rffi.scoped_alloc_buffer(size) as buf:
         length = socketrecv(self.fd, buf.raw, buf.size, 0)
         if length < 0:
             raise WindowsError(geterrno(), "recv")
         return buf.str(length)
Ejemplo n.º 9
0
 def read(self, size=-1):
     # XXX CPython uses a more delicate logic here
     self._check_closed()
     ll_file = self._ll_file
     if size == 0:
         return ""
     elif size < 0:
         # read the entire contents
         buf = lltype.malloc(rffi.CCHARP.TO, BASE_BUF_SIZE, flavor='raw')
         try:
             s = StringBuilder()
             while True:
                 returned_size = self._fread(buf, BASE_BUF_SIZE, ll_file)
                 returned_size = intmask(returned_size)  # is between 0 and BASE_BUF_SIZE
                 if returned_size == 0:
                     if c_feof(ll_file):
                         # ok, finished
                         return s.build()
                     raise _error(ll_file)
                 s.append_charpsize(buf, returned_size)
         finally:
             lltype.free(buf, flavor='raw')
     else:  # size > 0
         with rffi.scoped_alloc_buffer(size) as buf:
             returned_size = self._fread(buf.raw, size, ll_file)
             returned_size = intmask(returned_size)  # is between 0 and size
             if returned_size == 0:
                 if not c_feof(ll_file):
                     raise _error(ll_file)
             s = buf.str(returned_size)
             assert s is not None
         return s
Ejemplo n.º 10
0
 def read(self, size=-1):
     # XXX CPython uses a more delicate logic here
     self._check_closed()
     ll_file = self._ll_file
     if size == 0:
         return ""
     elif size < 0:
         # read the entire contents
         buf = lltype.malloc(rffi.CCHARP.TO, BASE_BUF_SIZE, flavor='raw')
         try:
             s = StringBuilder()
             while True:
                 returned_size = self._fread(buf, BASE_BUF_SIZE, ll_file)
                 returned_size = intmask(returned_size)  # is between 0 and BASE_BUF_SIZE
                 if returned_size == 0:
                     if c_feof(ll_file):
                         # ok, finished
                         return s.build()
                     raise _error(ll_file)
                 s.append_charpsize(buf, returned_size)
         finally:
             lltype.free(buf, flavor='raw')
     else:  # size > 0
         with rffi.scoped_alloc_buffer(size) as buf:
             returned_size = self._fread(buf.raw, size, ll_file)
             returned_size = intmask(returned_size)  # is between 0 and size
             if returned_size == 0:
                 if not c_feof(ll_file):
                     raise _error(ll_file)
             s = buf.str(returned_size)
             assert s is not None
         return s
Ejemplo n.º 11
0
def utf8_encode_code_page(cp, s, errors, errorhandler):
    """Encode a utf8 string s using code page cp and the given
    errors/errorhandler.
    Returns a encoded byte string
    """

    name = _code_page_name(cp)
    lgt = len(s)

    if lgt == 0:
        return ''
    flags = _encode_code_page_flags(cp, errors)
    if cp in (rwin32.CP_UTF8, rwin32.CP_UTF7):
        used_default_p = lltype.nullptr(BOOLP.TO)
    else:
        used_default_p = lltype.malloc(BOOLP.TO, 1, flavor='raw')
    # Encode one codpoint at a time to allow the errorhandlers to do
    # their thing
    chars = lltype.malloc(rffi.CWCHARP.TO, 2, flavor='raw')
    res = StringBuilder(lgt)
    try:
        with rffi.scoped_alloc_buffer(4) as buf:
            pos = 0
            # TODO: update s if obj != s is returned from an errorhandler
            for uni in Utf8StringIterator(s):
                if used_default_p:
                    used_default_p[0] = rffi.cast(rwin32.BOOL, False)
                if uni < 0x10000:
                    chars[0] = rffi.cast(lltype.UniChar, uni)
                    charsize = 1
                else:
                    chars[0] = Py_UNICODE_HIGH_SURROGATE(uni)
                    chars[1] = Py_UNICODE_LOW_SURROGATE(uni)
                    charsize = 2
                    # first get the size of the result
                outsize = WideCharToMultiByte(cp, flags, chars, charsize,
                                              buf.raw, 4, None, used_default_p)

                if outsize > 0:
                    if not (used_default_p and used_default_p[0]):
                        r = buf.str(outsize)
                        assert r is not None
                        res.append(r)
                        pos += 1
                        continue
                elif rwin32.GetLastError_saved(
                ) != rwin32.ERROR_NO_UNICODE_TRANSLATION:
                    raise rwin32.lastSavedWindowsError()
                # If we used a default char, then we failed!
                r, pos, retype, obj = errorhandler(errors, name,
                                                   "invalid character", s, pos,
                                                   pos + 1)
                res.append(r)
                pos += 1
    finally:
        lltype.free(chars, flavor='raw')
        if used_default_p:
            lltype.free(used_default_p, flavor='raw')
    return res.build()
Ejemplo n.º 12
0
 def receive(self):
     msgsize = self.msgsize
     with rffi.scoped_alloc_buffer(msgsize) as msg_ptr:
         with lltype.scoped_alloc(rffi.UINTP.TO, 1) as msg_prio:
             size = check_call(
                 mq_receive(self.mqd, msg_ptr.raw, msgsize, msg_prio),
                 "mq_receive")
             return msg_ptr.str(size), msg_prio[0]
Ejemplo n.º 13
0
 def receive(self):
     msgsize = self.msgsize
     with rffi.scoped_alloc_buffer(msgsize) as msg_ptr:
         with lltype.scoped_alloc(rffi.UINTP.TO, 1) as msg_prio:
             size = check_call(mq_receive(self.mqd, msg_ptr.raw, msgsize,
                                          msg_prio),
                               "mq_receive")
             return msg_ptr.str(size), msg_prio[0]
Ejemplo n.º 14
0
 def _digest(self, space):
     with lltype.scoped_alloc(ropenssl.EVP_MD_CTX.TO) as ctx:
         with self.lock:
             ropenssl.EVP_MD_CTX_copy(ctx, self.ctx)
         digest_size = self.digest_size
         with rffi.scoped_alloc_buffer(digest_size) as buf:
             ropenssl.EVP_DigestFinal(ctx, buf.raw, None)
             ropenssl.EVP_MD_CTX_cleanup(ctx)
             return buf.str(digest_size)
Ejemplo n.º 15
0
 def _digest(self, space):
     with lltype.scoped_alloc(ropenssl.EVP_MD_CTX.TO) as ctx:
         with self.lock:
             ropenssl.EVP_MD_CTX_copy(ctx, self.ctx)
         digest_size = self.digest_size
         with rffi.scoped_alloc_buffer(digest_size) as buf:
             ropenssl.EVP_DigestFinal(ctx, buf.raw, None)
             ropenssl.EVP_MD_CTX_cleanup(ctx)
             return buf.str(digest_size)
Ejemplo n.º 16
0
 def descr__unicode__(self, space):
     h_str = support.emjs_to_string(self.handle)
     _check_error(space, h_str)
     # The buffer must hold utf8 version of the string, which
     # might take up to four bytes per character.
     bufsize = support.emjs_length(h_str) * 4
     _check_error(space, bufsize)
     with rffi.scoped_alloc_buffer(bufsize) as buf:
         n = support.emjs_read_strn(h_str, buf.raw, buf.size)
         return space.wrap(buf.str(n).decode("utf-8"))
Ejemplo n.º 17
0
 def descr__unicode__(self, space):
     h_str = support.emjs_to_string(self.handle)
     _check_error(space, h_str)
     # The buffer must hold utf8 version of the string, which
     # might take up to four bytes per character.
     bufsize = support.emjs_length(h_str) * 4
     _check_error(space, bufsize)
     with rffi.scoped_alloc_buffer(bufsize) as buf:
         n = support.emjs_read_strn(h_str, buf.raw, buf.size)
         return space.wrap(buf.str(n).decode("utf-8"))
Ejemplo n.º 18
0
def freshNonce():
    """
    Make a fresh nonce.
    """

    nonceSize = intmask(cryptoBoxNoncebytes())

    with rffi.scoped_alloc_buffer(nonceSize) as nonce:
        randombytesBuf(nonce.raw, nonceSize)
        return nonce.str(nonceSize)
Ejemplo n.º 19
0
def copy_bio_ssl(bio, w_dst, dstlen, loglevel):
    nbytes = ropenssl.libssl_BIO_ctrl_pending(bio)
    if loglevel:
        print("copy_bio_ssl: %s bytes pending; buffer size %s" %
              (nbytes, dstlen))
    if nbytes > dstlen:  # more bytes to copy than available
        return -1
    with rffi.scoped_alloc_buffer(nbytes) as buf:
        r = ropenssl.libssl_BIO_read(bio, buf.raw, dstlen)
        for idx in range(r):
            w_dst.setchar(idx, buf.raw[idx])
        return r
Ejemplo n.º 20
0
def copy_bio_ssl(bio, w_dst, dstlen, loglevel):
    nbytes = ropenssl.libssl_BIO_ctrl_pending(bio)
    if loglevel:
        print ("copy_bio_ssl: %s bytes pending; buffer size %s" %
               (nbytes, dstlen))
    if nbytes > dstlen:  # more bytes to copy than available
        return -1
    with rffi.scoped_alloc_buffer(nbytes) as buf:
        r = ropenssl.libssl_BIO_read(bio, buf.raw, dstlen)
        for idx in range(r):
            w_dst.setchar(idx, buf.raw[idx])
        return r
Ejemplo n.º 21
0
def get_profile_path(space):
    vmp = _get_vmprof()
    if not vmp.is_enabled:
        return None

    with rffi.scoped_alloc_buffer(4096) as buf:
        length = vmp.cintf.vmprof_get_profile_path(buf.raw, buf.size)
        if length == -1:
            return ""
        return buf.str(length)

    return None
Ejemplo n.º 22
0
 def descr__repr__(self, space):
     bufsize = support.emjs_length(self.handle)
     _check_error(space, bufsize)
     truncated = False
     if bufsize > 17:
         bufsize = 17
         truncated = True
     with rffi.scoped_alloc_buffer(bufsize) as buf:
         n = support.emjs_read_strn(self.handle, buf.raw, buf.size)
         strval = buf.str(n)
     if truncated:
         strval = strval + "..."
     return space.wrap("<js.String '%s'>" % (strval, ))
Ejemplo n.º 23
0
def regenerateKey(secretKey):
    """
    Regenerate a public key from a secret key.
    """

    publicSize = intmask(cryptoBoxPublickeybytes())

    with rffi.scoped_alloc_buffer(publicSize) as public:
        with rffi.scoped_str2charp(secretKey) as rawSecret:
            rv = cryptoScalarmultBase(public.raw, rawSecret)
            if rv:
                raise SodiumError("crypto_scalarmult_base: %d" % rv)
            return public.str(publicSize)
Ejemplo n.º 24
0
 def descr__repr__(self, space):
     bufsize = support.emjs_length(self.handle)
     _check_error(space, bufsize)
     truncated = False
     if bufsize > 17:
         bufsize = 17
         truncated = True
     with rffi.scoped_alloc_buffer(bufsize) as buf:
         n = support.emjs_read_strn(self.handle, buf.raw, buf.size)
         strval = buf.str(n)
     if truncated:
         strval = strval + "..."
     return space.wrap("<js.String '%s'>" % (strval,))
Ejemplo n.º 25
0
    def read(self, space, num_bytes=1024):
        """read([len]) -> string

        Read up to len bytes from the SSL socket."""
        count = libssl_SSL_pending(self.ssl)
        if not count:
            sockstate = checkwait(space, self.w_socket, False)
            if sockstate == SOCKET_HAS_TIMED_OUT:
                raise ssl_error(space, "The read operation timed out")
            elif sockstate == SOCKET_TOO_LARGE_FOR_SELECT:
                raise ssl_error(space,
                                "Underlying socket too large for select().")
            elif sockstate == SOCKET_HAS_BEEN_CLOSED:
                if libssl_SSL_get_shutdown(self.ssl) == SSL_RECEIVED_SHUTDOWN:
                    return space.wrap('')
                raise ssl_error(
                    space, "Socket closed without SSL shutdown handshake")

        with rffi.scoped_alloc_buffer(num_bytes) as buf:
            while True:
                err = 0

                count = libssl_SSL_read(self.ssl, buf.raw, num_bytes)
                err = libssl_SSL_get_error(self.ssl, count)

                if err == SSL_ERROR_WANT_READ:
                    sockstate = checkwait(space, self.w_socket, False)
                elif err == SSL_ERROR_WANT_WRITE:
                    sockstate = checkwait(space, self.w_socket, True)
                elif (err == SSL_ERROR_ZERO_RETURN and libssl_SSL_get_shutdown(
                        self.ssl) == SSL_RECEIVED_SHUTDOWN):
                    return space.wrap("")
                else:
                    sockstate = SOCKET_OPERATION_OK

                if sockstate == SOCKET_HAS_TIMED_OUT:
                    raise ssl_error(space, "The read operation timed out")
                elif sockstate == SOCKET_IS_NONBLOCKING:
                    break

                if err == SSL_ERROR_WANT_READ or err == SSL_ERROR_WANT_WRITE:
                    continue
                else:
                    break

            if count <= 0:
                raise _ssl_seterror(space, self, count)

            result = buf.str(count)

        return space.wrap(result)
Ejemplo n.º 26
0
    def read(self, space, num_bytes=1024):
        """read([len]) -> string

        Read up to len bytes from the SSL socket."""
        count = libssl_SSL_pending(self.ssl)
        if not count:
            sockstate = checkwait(space, self.w_socket, False)
            if sockstate == SOCKET_HAS_TIMED_OUT:
                raise ssl_error(space, "The read operation timed out")
            elif sockstate == SOCKET_TOO_LARGE_FOR_SELECT:
                raise ssl_error(space,
                                "Underlying socket too large for select().")
            elif sockstate == SOCKET_HAS_BEEN_CLOSED:
                if libssl_SSL_get_shutdown(self.ssl) == SSL_RECEIVED_SHUTDOWN:
                    return space.wrap('')
                raise ssl_error(space,
                                "Socket closed without SSL shutdown handshake")

        with rffi.scoped_alloc_buffer(num_bytes) as buf:
            while True:
                err = 0

                count = libssl_SSL_read(self.ssl, buf.raw, num_bytes)
                err = libssl_SSL_get_error(self.ssl, count)

                if err == SSL_ERROR_WANT_READ:
                    sockstate = checkwait(space, self.w_socket, False)
                elif err == SSL_ERROR_WANT_WRITE:
                    sockstate = checkwait(space, self.w_socket, True)
                elif (err == SSL_ERROR_ZERO_RETURN and
                   libssl_SSL_get_shutdown(self.ssl) == SSL_RECEIVED_SHUTDOWN):
                    return space.wrap("")
                else:
                    sockstate = SOCKET_OPERATION_OK

                if sockstate == SOCKET_HAS_TIMED_OUT:
                    raise ssl_error(space, "The read operation timed out")
                elif sockstate == SOCKET_IS_NONBLOCKING:
                    break

                if err == SSL_ERROR_WANT_READ or err == SSL_ERROR_WANT_WRITE:
                    continue
                else:
                    break

            if count <= 0:
                raise _ssl_seterror(space, self, count)

            result = buf.str(count)

        return space.wrap(result)
Ejemplo n.º 27
0
 def _digest(self, space):
     ctx = ropenssl.EVP_MD_CTX_new()
     if ctx is None:
         raise MemoryError
     try:
         with self.lock:
             if not ropenssl.EVP_MD_CTX_copy(ctx, self.ctx):
                 raise ValueError
         digest_size = self.digest_size
         with rffi.scoped_alloc_buffer(digest_size) as buf:
             ropenssl.EVP_DigestFinal(ctx, buf.raw, None)
             return buf.str(digest_size)
     finally:
         ropenssl.EVP_MD_CTX_free(ctx)
Ejemplo n.º 28
0
 def _digest(self, space):
     ctx = ropenssl.EVP_MD_CTX_new()
     if ctx is None:
         raise MemoryError
     try:
         with self.lock:
             if not ropenssl.EVP_MD_CTX_copy(ctx, self.ctx):
                 raise ValueError
         digest_size = self.digest_size
         with rffi.scoped_alloc_buffer(digest_size) as buf:
             ropenssl.EVP_DigestFinal(ctx, buf.raw, None)
             return buf.str(digest_size)
     finally:
         ropenssl.EVP_MD_CTX_free(ctx)
Ejemplo n.º 29
0
def boxSeal(message, nonce, public, secret):
    cipherSize = len(message) + intmask(cryptoBoxMacbytes())

    with rffi.scoped_alloc_buffer(cipherSize) as cipher:
        with rffi.scoped_str2charp(message) as rawMessage:
            with rffi.scoped_str2charp(nonce) as rawNonce:
                with rffi.scoped_str2charp(public) as rawPublic:
                    with rffi.scoped_str2charp(secret) as rawSecret:
                        rv = cryptoBoxEasy(cipher.raw, rawMessage,
                                           len(message), rawNonce, rawPublic,
                                           rawSecret)
                        if rv:
                            raise SodiumError("crypto_box_easy: %d" % rv)
                        return cipher.str(cipherSize)
Ejemplo n.º 30
0
    def utf8_encode_mbcs(s, errors, errorhandler, force_replace=True):
        # TODO: do the encoding without decoding utf8 -> unicode
        uni = s.decode('utf8')
        lgt = len(uni)
        if not force_replace and errors not in ('strict', 'replace'):
            msg = "mbcs encoding does not support errors='%s'" % errors
            errorhandler('strict', 'mbcs', msg, s, 0, 0)

        if lgt == 0:
            return ''

        if force_replace or errors == 'replace':
            flags = 0
            used_default_p = lltype.nullptr(BOOLP.TO)
        else:
            # strict
            flags = rwin32.WC_NO_BEST_FIT_CHARS
            used_default_p = lltype.malloc(BOOLP.TO, 1, flavor='raw')
            used_default_p[0] = rffi.cast(rwin32.BOOL, False)

        try:
            with rffi.scoped_nonmoving_unicodebuffer(uni) as dataptr:
                # first get the size of the result
                mbcssize = WideCharToMultiByte(CP_ACP, flags, dataptr, lgt,
                                               None, 0, None, used_default_p)
                if mbcssize == 0:
                    raise rwin32.lastSavedWindowsError()
                # If we used a default char, then we failed!
                if (used_default_p
                        and rffi.cast(lltype.Bool, used_default_p[0])):
                    errorhandler('strict', 'mbcs', "invalid character", s, 0,
                                 0)

                with rffi.scoped_alloc_buffer(mbcssize) as buf:
                    # do the conversion
                    if WideCharToMultiByte(CP_ACP, flags, dataptr, lgt,
                                           buf.raw, mbcssize, None,
                                           used_default_p) == 0:
                        raise rwin32.lastSavedWindowsError()
                    if (used_default_p
                            and rffi.cast(lltype.Bool, used_default_p[0])):
                        errorhandler('strict', 'mbcs', "invalid character", s,
                                     0, 0)
                    result = buf.str(mbcssize)
                    assert result is not None
                    return result
        finally:
            if used_default_p:
                lltype.free(used_default_p, flavor='raw')
Ejemplo n.º 31
0
def boxUnseal(cipher, nonce, public, secret):
    messageSize = len(cipher) - intmask(cryptoBoxMacbytes())
    assert messageSize >= 0

    with rffi.scoped_alloc_buffer(messageSize) as message:
        with rffi.scoped_str2charp(cipher) as rawCipher:
            with rffi.scoped_str2charp(nonce) as rawNonce:
                with rffi.scoped_str2charp(public) as rawPublic:
                    with rffi.scoped_str2charp(secret) as rawSecret:
                        rv = cryptoBoxOpenEasy(message.raw, rawCipher,
                                               len(cipher), rawNonce,
                                               rawPublic, rawSecret)
                        if rv:
                            raise SodiumError("crypto_box_open_easy: %d" % rv)
                        return message.str(messageSize)
Ejemplo n.º 32
0
 def recv(self, buffersize, flags=0):
     """Receive up to buffersize bytes from the socket.  For the optional
     flags argument, see the Unix manual.  When no data is available, block
     until at least one byte is available or until the remote end is closed.
     When the remote end is closed and all data is read, return the empty
     string."""
     timeout = self._select(False)
     if timeout == 1:
         raise SocketTimeout
     elif timeout == 0:
         with rffi.scoped_alloc_buffer(buffersize) as buf:
             read_bytes = _c.socketrecv(self.fd, rffi.cast(rffi.VOIDP, buf.raw), buffersize, flags)
             if read_bytes >= 0:
                 return buf.str(read_bytes)
     raise self.error_handler()
Ejemplo n.º 33
0
 def recv(self, buffersize, flags=0):
     """Receive up to buffersize bytes from the socket.  For the optional
     flags argument, see the Unix manual.  When no data is available, block
     until at least one byte is available or until the remote end is closed.
     When the remote end is closed and all data is read, return the empty
     string."""
     timeout = self._select(False)
     if timeout == 1:
         raise SocketTimeout
     elif timeout == 0:
         with rffi.scoped_alloc_buffer(buffersize) as buf:
             read_bytes = _c.socketrecv(self.fd,
                                        rffi.cast(rffi.VOIDP, buf.raw),
                                        buffersize, flags)
             if read_bytes >= 0:
                 return buf.str(read_bytes)
     raise self.error_handler()
Ejemplo n.º 34
0
 def pbkdf2_hmac(space, name, password, salt, rounds, w_dklen):
     digest = ropenssl.EVP_get_digestbyname(name)
     if not digest:
         raise oefmt(space.w_ValueError, "unknown hash function")
     if space.is_w(w_dklen, space.w_None):
         dklen = ropenssl.EVP_MD_size(digest)
     else:
         dklen = space.int_w(w_dklen)
     if dklen < 1:
         raise oefmt(space.w_ValueError,
                     "key length must be greater than 0.")
     with rffi.scoped_alloc_buffer(dklen) as buf:
         r = ropenssl.PKCS5_PBKDF2_HMAC(password, len(password), salt,
                                        len(salt), rounds, digest, dklen,
                                        buf.raw)
         if not r:
             raise ValueError
         return space.newbytes(buf.str(dklen))
Ejemplo n.º 35
0
 def pbkdf2_hmac(space, name, password, salt, rounds, w_dklen):
     digest = ropenssl.EVP_get_digestbyname(name)
     if not digest:
         raise oefmt(space.w_ValueError, "unknown hash function")
     if space.is_w(w_dklen, space.w_None):
         dklen = ropenssl.EVP_MD_size(digest)
     else:
         dklen = space.int_w(w_dklen)
     if dklen < 1:
         raise oefmt(space.w_ValueError,
                     "key length must be greater than 0.")
     with rffi.scoped_alloc_buffer(dklen) as buf:
         r = ropenssl.PKCS5_PBKDF2_HMAC(
             password, len(password), salt, len(salt), rounds, digest,
             dklen, buf.raw)
         if not r:
             raise ValueError
         return space.wrap(buf.str(dklen))
Ejemplo n.º 36
0
def PyRun_File(space, fp, filename, start, w_globals, w_locals):
    """This is a simplified interface to PyRun_FileExFlags() below, leaving
    closeit set to 0 and flags set to NULL."""
    BUF_SIZE = 8192
    source = ""
    filename = rffi.charp2str(filename)
    with rffi.scoped_alloc_buffer(BUF_SIZE) as buf:
        while True:
            try:
                count = fread(buf.raw, 1, BUF_SIZE, fp)
            except OSError:
                PyErr_SetFromErrno(space, space.w_IOError)
                return
            count = rffi.cast(lltype.Signed, count)
            source += rffi.charpsize2str(buf.raw, count)
            if count < BUF_SIZE:
                if feof(fp):
                    break
                PyErr_SetFromErrno(space, space.w_IOError)
    return run_string(space, source, filename, start, w_globals, w_locals)
Ejemplo n.º 37
0
def _unibuf_to_utf8(dataptr, insize):
    """Encode the widechar unicode buffer u to utf8
    Should never error, since the buffer comes from a call to
    MultiByteToWideChar
    """
    flags = 0
    cp = rwin32.CP_UTF8
    used_default_p = lltype.nullptr(BOOLP.TO)
    # first get the size of the result
    outsize = WideCharToMultiByte(cp, flags, dataptr, insize, None, 0, None,
                                  used_default_p)
    if outsize == 0:
        raise rwin32.lastSavedWindowsError()
    with rffi.scoped_alloc_buffer(outsize) as buf:
        # do the conversion
        if WideCharToMultiByte(cp, flags, dataptr, insize, buf.raw, outsize,
                               None, used_default_p) == 0:
            raise rwin32.lastSavedWindowsError()
        result = buf.str(outsize)
        assert result is not None
        return result
Ejemplo n.º 38
0
def primitiveDecrypt(interp, s_frame, w_rcvr, w_handle, src, start, srclen, w_dst):
    _debug_in_interpreter()
    if (not isinstance(w_handle, W_SSLHandle) or
            w_handle.state != SSL_CONNECTED):
        return interp.space.wrap_int(SSL_INVALID_STATE)
    assert start >= 0 and srclen >= 0
    nbytes = ropenssl.libssl_BIO_write(w_handle.readbio, src[start:start+srclen], srclen)
    if nbytes != srclen:
        return interp.space.wrap_int(SSL_GENERIC_ERROR)
    dstlen = w_dst.size()
    with rffi.scoped_alloc_buffer(dstlen) as buf:
        nbytes = ropenssl.libssl_SSL_read(w_handle.ssl, buf.raw, dstlen)
        if nbytes <= 0:
            err = ropenssl.libssl_SSL_get_error(w_handle.ssl, nbytes)
            if (err != ropenssl.SSL_ERROR_WANT_READ and
                err != ropenssl.SSL_ERROR_ZERO_RETURN):
                return interp.space.wrap_int(SSL_GENERIC_ERROR)
            else:
                nbytes = 0
        for idx in range(nbytes):
            w_dst.setchar(idx, buf.raw[idx])
    return interp.space.wrap_int(nbytes)
Ejemplo n.º 39
0
 def _getrandom(n, result, signal_checker):
     if not getrandom_works.status:
         return n
     while n > 0:
         with rffi.scoped_alloc_buffer(n) as buf:
             got = syscall(SYS_getrandom, buf.raw, n, GRND_NONBLOCK)
             if got >= 0:
                 s = buf.str(got)
                 result.append(s)
                 n -= len(s)
                 continue
         err = get_saved_errno()
         if (err == errno.ENOSYS or err == errno.EPERM or
                 err == errno.EAGAIN):   # see CPython 3.5
             getrandom_works.status = False
             return n
         if err == errno.EINTR:
             if signal_checker is not None:
                 signal_checker()
             continue
         handle_posix_error("getrandom", got)
         raise AssertionError("unreachable")
     return n
Ejemplo n.º 40
0
def primitiveDecrypt(interp, s_frame, w_rcvr, w_handle, src, start, srclen,
                     w_dst):
    _debug_in_interpreter()
    if (not isinstance(w_handle, W_SSLHandle)
            or w_handle.state != SSL_CONNECTED):
        return interp.space.wrap_int(SSL_INVALID_STATE)
    assert start >= 0 and srclen >= 0
    nbytes = ropenssl.libssl_BIO_write(w_handle.readbio,
                                       src[start:start + srclen], srclen)
    if nbytes != srclen:
        return interp.space.wrap_int(SSL_GENERIC_ERROR)
    dstlen = w_dst.size()
    with rffi.scoped_alloc_buffer(dstlen) as buf:
        nbytes = ropenssl.libssl_SSL_read(w_handle.ssl, buf.raw, dstlen)
        if nbytes <= 0:
            err = ropenssl.libssl_SSL_get_error(w_handle.ssl, nbytes)
            if (err != ropenssl.SSL_ERROR_WANT_READ
                    and err != ropenssl.SSL_ERROR_ZERO_RETURN):
                return interp.space.wrap_int(SSL_GENERIC_ERROR)
            else:
                nbytes = 0
        for idx in range(nbytes):
            w_dst.setchar(idx, buf.raw[idx])
    return interp.space.wrap_int(nbytes)
Ejemplo n.º 41
0
 def urandom(context, n):
     with rffi.scoped_alloc_buffer(n) as buf:
         if libssl_RAND_pseudo_bytes(self.raw, n) < 0:
             raise ValueError("RAND_pseudo_bytes")
         return buf.str(n)
Ejemplo n.º 42
0
    def readline(self, size=-1):
        self._check_closed()
        if size == 0:
            return ""
        elif size < 0 and not self._univ_newline:
            with rffi.scoped_alloc_buffer(BASE_LINE_SIZE) as buf:
                c = self._readline1(buf.raw)
                if c >= 0:
                    return buf.str(c)

                # this is the rare case: the line is longer than BASE_LINE_SIZE
                s = StringBuilder()
                while True:
                    s.append_charpsize(buf.raw, BASE_LINE_SIZE - 1)
                    c = self._readline1(buf.raw)
                    if c >= 0:
                        break
                s.append_charpsize(buf.raw, c)
            return s.build()
        else:  # size > 0 or self._univ_newline
            ll_file = self._ll_file
            c = 0
            s = StringBuilder()
            if self._univ_newline:
                newlinetypes = self._newlinetypes
                skipnextlf = self._skipnextlf
                while size < 0 or s.getlength() < size:
                    c = c_getc(ll_file)
                    if c == EOF:
                        break
                    if skipnextlf:
                        skipnextlf = False
                        if c == ord('\n'):
                            newlinetypes |= NEWLINE_CRLF
                            c = c_getc(ll_file)
                            if c == EOF:
                                break
                        else:
                            newlinetypes |= NEWLINE_CR
                    if c == ord('\r'):
                        skipnextlf = True
                        c = ord('\n')
                    elif c == ord('\n'):
                        newlinetypes |= NEWLINE_LF
                    s.append(chr(c))
                    if c == ord('\n'):
                        break
                if c == EOF:
                    if skipnextlf:
                        newlinetypes |= NEWLINE_CR
                self._newlinetypes = newlinetypes
                self._skipnextlf = skipnextlf
            else:
                while s.getlength() < size:
                    c = c_getc(ll_file)
                    if c == EOF:
                        break
                    s.append(chr(c))
                    if c == ord('\n'):
                        break
            if c == EOF:
                if c_ferror(ll_file):
                    raise _error(ll_file)
            return s.build()
Ejemplo n.º 43
0
 def urandom(context, n):
     with rffi.scoped_alloc_buffer(n) as buf:
         if libssl_RAND_pseudo_bytes(self.raw, n) < 0:
             raise ValueError("RAND_pseudo_bytes")
         return buf.str(n)
Ejemplo n.º 44
0
def IP6Name(sockaddr):
    size = 46
    with rffi.scoped_alloc_buffer(size) as buf:
        check("ip6_name", ip6_name(sockaddr, buf.raw, size))
        return buf.str(size).split('\x00', 1)[0]
Ejemplo n.º 45
0
def _init_timezone(space):
    timezone = daylight = altzone = 0
    tzname = ["", ""]

    if _WIN:
        c_tzset()
        timezone = c_get_timezone()
        altzone = timezone - 3600
        daylight = c_get_daylight()
        with rffi.scoped_alloc_buffer(100) as buf:
            s = c_get_tzname(100, 0, buf.raw)
            tzname[0] = buf.str(s)
            s = c_get_tzname(100, 1, buf.raw)
            tzname[1] = buf.str(s)

    if _POSIX:
        if _CYGWIN:
            YEAR = (365 * 24 + 6) * 3600

            # about January 11th
            t = (((c_time(lltype.nullptr(rffi.TIME_TP.TO))) / YEAR) * YEAR +
                 10 * 24 * 3600)
            # we cannot have reference to stack variable, put it on the heap
            t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
            t_ref[0] = rffi.cast(rffi.TIME_T, t)
            p = c_localtime(t_ref)
            q = c_gmtime(t_ref)
            janzone = (p.c_tm_hour + 24 * p.c_tm_mday) - (q.c_tm_hour +
                                                          24 * q.c_tm_mday)
            if janzone < -12:
                janname = "   "
            elif janzone > 14:
                janname = "   "
            else:
                janname = _time_zones[janzone - 12]
            janzone = janzone * 3600
            # about July 11th
            tt = t + YEAR / 2
            t_ref[0] = rffi.cast(rffi.TIME_T, tt)
            p = c_localtime(t_ref)
            q = c_gmtime(t_ref)
            julyzone = (p.c_tm_hour + 24 * p.c_tm_mday) - (q.c_tm_hour +
                                                           24 * q.c_tm_mday)
            if julyzone < -12:
                julyname = "   "
            elif julyzone > 14:
                julyname = "   "
            else:
                julyname = _time_zones[julyzone - 12]
            julyzone = julyzone * 3600
            lltype.free(t_ref, flavor='raw')

            if janzone < julyzone:
                # DST is reversed in the southern hemisphere
                timezone = julyzone
                altzone = janzone
                daylight = int(janzone != julyzone)
                tzname = [julyname, janname]
            else:
                timezone = janzone
                altzone = julyzone
                daylight = int(janzone != julyzone)
                tzname = [janname, julyname]

        else:
            YEAR = (365 * 24 + 6) * 3600

            t = (((c_time(lltype.nullptr(rffi.TIME_TP.TO))) / YEAR) * YEAR)
            # we cannot have reference to stack variable, put it on the heap
            t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
            t_ref[0] = rffi.cast(rffi.TIME_T, t)
            p = c_localtime(t_ref)
            janzone = -p.c_tm_gmtoff
            tm_zone = rffi.charp2str(p.c_tm_zone)
            janname = ["   ", tm_zone][bool(tm_zone)]
            tt = t + YEAR / 2
            t_ref[0] = rffi.cast(rffi.TIME_T, tt)
            p = c_localtime(t_ref)
            lltype.free(t_ref, flavor='raw')
            tm_zone = rffi.charp2str(p.c_tm_zone)
            julyzone = -p.c_tm_gmtoff
            julyname = ["   ", tm_zone][bool(tm_zone)]

            if janzone < julyzone:
                # DST is reversed in the southern hemisphere
                timezone = julyzone
                altzone = janzone
                daylight = int(janzone != julyzone)
                tzname = [julyname, janname]
            else:
                timezone = janzone
                altzone = julyzone
                daylight = int(janzone != julyzone)
                tzname = [janname, julyname]

    _set_module_object(space, "timezone", space.newint(timezone))
    _set_module_object(space, 'daylight', space.newint(daylight))
    tzname_w = [space.newtext(tzname[0]), space.newtext(tzname[1])]
    _set_module_object(space, 'tzname', space.newtuple(tzname_w))
    _set_module_object(space, 'altzone', space.newint(altzone))
Ejemplo n.º 46
0
 def f():
     with rffi.scoped_alloc_buffer(42) as p:
         p.raw[0] = 'X'
         s = p.str(1)
     return ord(s[0])
Ejemplo n.º 47
0
def primitiveConnect(interp, s_frame, w_rcvr, w_handle, src, start, srclen, w_dst):
    _debug_in_interpreter()
    if (not isinstance(w_handle, W_SSLHandle) or
            w_handle.state != SSL_UNUSED and
            w_handle.state != SSL_CONNECTING):
        return interp.space.wrap_int(SSL_INVALID_STATE)
    w_handle.log("primitiveConnect: handle %s", w_handle.hash)
    if w_handle.state == SSL_UNUSED:
        w_handle.state = SSL_CONNECTING
        w_handle.log("primitiveConnect: Setting up SSL")
        if not w_handle.setup():
            return interp.space.wrap_int(SSL_GENERIC_ERROR)
        w_handle.log("primitiveConnect: Setting connect state")
        ropenssl.libssl_SSL_set_connect_state(w_handle.ssl)
    assert start >= 0 and srclen >= 0
    w_handle.log("primitiveConnect: BIO_write %s bytes", srclen)
    n = ropenssl.libssl_BIO_write(w_handle.readbio, src[start:start+srclen], srclen)
    if n < srclen:
        w_handle.log("primitiveConnect: BIO too small for input")
        return interp.space.wrap_int(SSL_GENERIC_ERROR)
    if n < 0:
        w_handle.log("primitiveConnect: BIO_write failed", srclen)
        return interp.space.wrap_int(SSL_GENERIC_ERROR)
    if w_handle.servername:
        w_handle.log("primitiveConnect: Using server name %s", w_handle.servername)
        ropenssl.libssl_SSL_set_tlsext_host_name(w_handle.ssl, w_handle.servername)
    w_handle.log("primitiveConnect: SSL_connect")
    result = ropenssl.libssl_SSL_connect(w_handle.ssl)
    if result <= 0:
        err = ropenssl.libssl_SSL_get_error(w_handle.ssl, result)
        if err != ropenssl.SSL_ERROR_WANT_READ:
            w_handle.log("primitiveConnect: SSL_connect failed with %s", err)
            ropenssl.libssl_pypy_ERR_print_errors_stdout()
            return interp.space.wrap_int(-1)
        w_handle.log("primitiveConnect: copy_bio_ssl")
        c = copy_bio_ssl(w_handle.writebio, w_dst, w_dst.bytesize(),
                         w_handle.loglevel)
        return interp.space.wrap_int(c)
    # We are connected. Verify the cert.
    w_handle.state = SSL_CONNECTED
    w_handle.log("primitiveConnect: SSL_get_peer_certificate")
    cert = ropenssl.libssl_SSL_get_peer_certificate(w_handle.ssl)
    w_handle.log("primitiveConnect: cert = %s", cert)
    if cert:
        x509_name = ropenssl.libssl_X509_get_subject_name(cert)
        with rffi.scoped_alloc_buffer(256) as buf:
            ropenssl.libssl_X509_NAME_get_text_by_NID(
                x509_name, NID_commonName, buf.raw, 256)
            w_handle.peername = rffi.charp2str(buf.raw)
        w_handle.log("primitiveConnect: peerName = %s", w_handle.peername)
        ropenssl.libssl_X509_free(cert)

        # Check the result of verification
        result = ropenssl.libssl_SSL_get_verify_result(w_handle.ssl)
        w_handle.log("primitiveConnect: SSL_get_verify_result = %s (%s)", (result, result == X509_V_OK))
        if result == X509_V_OK:
            w_handle.certflags = SSL_OK
        else:
            w_handle.certflags = SSL_OTHER_ISSUE
    else:
        w_handle.certflags = SSL_NO_CERTIFICATE
    return interp.space.wrap_int(0)
Ejemplo n.º 48
0
Archivo: ruv.py Proyecto: dckc/typhon
def IP6Name(sockaddr):
    size = 46
    with rffi.scoped_alloc_buffer(size) as buf:
        check("ip6_name", ip6_name(sockaddr, buf.raw, size))
        return buf.str(size).split('\x00', 1)[0]
Ejemplo n.º 49
0
def IP4Name(sockaddr):
    size = 16
    with rffi.scoped_alloc_buffer(size) as buf:
        check("ip4_name", ip4_name(sockaddr, buf.raw, size))
        return buf.str(size).split("\x00", 1)[0]
Ejemplo n.º 50
0
def multiprocessing_recv(space, handle, buffersize):
    with rffi.scoped_alloc_buffer(buffersize) as buf:
        read_bytes = socketrecv(handle, buf.raw, buffersize, 0)
        if read_bytes >= 0:
            return space.newtext(buf.str(read_bytes))
    raise getWindowsError(space)