rpc_C_Initialize (CK_X_FUNCTION_LIST *self,
                  p11_rpc_message *msg)
{
	CK_X_Initialize func;
	CK_C_INITIALIZE_ARGS init_args;
	CK_BYTE_PTR handshake;
	CK_ULONG n_handshake;
	CK_BYTE reserved_present = 0;
	CK_BYTE_PTR reserved = NULL;
	CK_ULONG n_reserved;
	CK_RV ret = CKR_OK;

	p11_debug ("C_Initialize: enter");

	assert (msg != NULL);
	assert (self != NULL);

	ret = proto_read_byte_array (msg, &handshake, &n_handshake);
	if (ret == CKR_OK) {

		/* Check to make sure the header matches */
		if (n_handshake != P11_RPC_HANDSHAKE_LEN ||
		    memcmp (handshake, P11_RPC_HANDSHAKE, n_handshake) != 0) {
			p11_message ("invalid handshake received from connecting module");
			ret = CKR_GENERAL_ERROR;
		}
	}

	if (ret == CKR_OK) {
		if (!p11_rpc_message_read_byte (msg, &reserved_present))
			ret = PARSE_ERROR;
	}

	if (ret == CKR_OK) {
		ret = proto_read_byte_array (msg, &reserved, &n_reserved);

		assert (p11_rpc_message_is_verified (msg));
	}

	if (ret == CKR_OK) {
		memset (&init_args, 0, sizeof (init_args));
		init_args.flags = CKF_OS_LOCKING_OK;
		init_args.pReserved = reserved_present ? reserved : NULL;

		func = self->C_Initialize;
		assert (func != NULL);
		ret = (func) (self, &init_args);

		/* Empty response */
		if (ret == CKR_OK)
			ret = call_ready (msg);
	}

	p11_debug ("ret: %d", (int)ret);
	return ret;
}
rpc_C_Initialize (CK_X_FUNCTION_LIST *self,
                  p11_rpc_message *msg)
{
	CK_X_Initialize func;
	CK_C_INITIALIZE_ARGS init_args;
	CK_BYTE_PTR handshake;
	CK_ULONG n_handshake;
	CK_BYTE reserved_present = 0;
	CK_BYTE_PTR reserved = NULL;
	CK_ULONG n_reserved;
	CK_RV ret = CKR_OK;

	p11_debug ("C_Initialize: enter");

	assert (msg != NULL);
	assert (self != NULL);

	ret = proto_read_byte_array (msg, &handshake, &n_handshake);
	if (ret == CKR_OK) {

		 
		if (n_handshake != P11_RPC_HANDSHAKE_LEN ||
		    memcmp (handshake, P11_RPC_HANDSHAKE, n_handshake) != 0) {
			p11_message ("invalid handshake received from connecting module");
			ret = CKR_GENERAL_ERROR;
		}
	}

	if (ret == CKR_OK) {
		if (!p11_rpc_message_read_byte (msg, &reserved_present))
			ret = PARSE_ERROR;
	}

	if (ret == CKR_OK) {
		ret = proto_read_byte_array (msg, &reserved, &n_reserved);

		assert (p11_rpc_message_is_verified (msg));
	}

	if (ret == CKR_OK) {
		memset (&init_args, 0, sizeof (init_args));
		init_args.flags = CKF_OS_LOCKING_OK;
		init_args.pReserved = reserved_present ? reserved : NULL;

		func = self->C_Initialize;
		assert (func != NULL);
		ret = (func) (self, &init_args);

		 
		if (ret == CKR_OK)
			ret = call_ready (msg);
	}

	p11_debug ("ret: %d", (int)ret);
	return ret;
}