int BIO_write(BIO *b, const char *in, int inl)
	{
	int i;
	long (*cb)();

	if (b == NULL)
		return(0);

	cb=b->callback;
	if ((b->method == NULL) || (b->method->bwrite == NULL))
		{
		BIOerr(BIO_F_BIO_WRITE,BIO_R_UNSUPPORTED_METHOD);
		return(-2);
		}

	if ((cb != NULL) &&
		((i=(int)cb(b,BIO_CB_WRITE,in,inl,0L,1L)) <= 0))
			return(i);

	if (!b->init)
		{
		BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITIALIZED);
		return(-2);
		}

	i=b->method->bwrite(b,in,inl);

	if (i > 0) b->num_write+=(unsigned long)i;

	/* This is evil and not thread safe.  If the BIO has been freed,
	 * we must not call the callback.  The only way to be able to
	 * determine this is the reference count which is now invalid since
	 * the memory has been free()ed.
	 */
	if (b->references <= 0) abort();
	if (cb != NULL) /* && (b->references >= 1)) */
		i=(int)cb(b,BIO_CB_WRITE|BIO_CB_RETURN,in,inl,
			0L,(long)i);
	return(i);
	}