Index: trunk/src/VBox/Additions/darwin/Installer/DiskImage/Uninstall.tool
===================================================================
--- trunk/src/VBox/Additions/darwin/Installer/DiskImage/Uninstall.tool	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/Installer/DiskImage/Uninstall.tool	(revision 48251)
@@ -0,0 +1,105 @@
+#!/bin/sh
+#
+# VirtualBox Uninstaller Script.
+#
+# Copyright (C) 2007-2013 Oracle Corporation
+#
+# This file is part of VirtualBox Open Source Edition (OSE), as
+# available from http://www.virtualbox.org. This file is free software;
+# you can redistribute it and/or modify it under the terms of the GNU
+# General Public License (GPL) as published by the Free Software
+# Foundation, in version 2 as it comes in the "COPYING" file of the
+# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+#
+
+# Override any funny stuff from the user.
+export PATH="/bin:/usr/bin:/sbin:/usr/sbin:$PATH"
+
+#
+# Display a simple welcome message first.
+#
+echo ""
+echo "Welcome to the VirtualBox Guest Additions uninstall script."
+echo ""
+
+# Check if user interraction is required to start uninstall process.
+fUnattended=0
+if test "$#" != "0"; then
+    if test "$#" != "1" -o "$1" != "--unattended"; then
+            echo "Error: Unknown argument(s): $*"
+            echo ""
+            echo "Usage: $0 [--unattended]"
+            echo ""
+            echo "If the '--unattended' option is not given, you will be prompted"
+            echo "for a Yes/No before doing the actual uninstallation."
+            echo ""
+        exit 4;
+    fi
+    fUnattended="Yes"
+fi
+
+if test "$fUnattended" != "Yes"; then
+    echo "Do you wish to continue none the less (Yes/No)?"
+    read fUnattended
+    if test "$fUnattended" != "Yes"  -a  "$fUnattended" != "YES"  -a  "$fUnattended" != "yes"; then
+        echo "Aborting uninstall. (answer: '$fUnattended')".
+        exit 2;
+    fi
+    echo ""
+fi
+
+# Stop services
+echo "Checking running services..."
+unload()
+{
+    ITEM_ID=$1
+    ITEM_PATH=$2
+    FORCED_USER=$3
+
+    echo "Unloading $ITEM_ID"
+
+
+    loaded="NO"
+    test -n "$(sudo -u "$FORCED_USER" launchctl list | grep $ITEM_ID)" && loaded="YES"
+    if [ "$loaded" = "YES" ] ; then
+        sudo -p "Please enter $FORCED_USER's password (unloading $ITEM_ID):" sudo -u "$FORCED_USER" launchctl unload -F "$ITEM_PATH/$ITEM_ID.plist"
+    fi
+
+}
+
+unload "org.virtualbox.additions.vboxservice" "/Library/LaunchDaemons" "root"
+unload "org.virtualbox.additions.vboxclient" "/Library/LaunchAgents" `whoami`
+
+# Unload kernel extensions
+echo "Checking running kernel extensions..."
+items="VBoxGuest"
+for item in $items; do
+    kext_item="org.virtualbox.kext.$item"
+    loaded=`kextstat | grep $kext_item`
+    if [ ! -z "$loaded" ] ; then
+        echo "Unloading $item kernel extension"
+        sudo -p "Please enter %u's password (unloading $item):" kextunload -b $kext_item
+    fi
+done
+
+# Remove files and directories
+echo "Checking files and directories..."
+sudo -p "Please enter %u's password (removing files and directories):" rm -rf "/Library/Application Support/VirtualBox Guest Additions"
+sudo -p "Please enter %u's password (removing files and directories):" rm -rf "/Library/Extensions/VBoxGuest.kext"
+sudo -p "Please enter %u's password (removing files and directories):" rm -rf "/Library/LaunchAgents/org.virtualbox.additions.vboxclient.plist"
+sudo -p "Please enter %u's password (removing files and directories):" rm -rf "/Library/LaunchDaemons/org.virtualbox.additions.vboxservice.plist"
+
+# Cleaning up pkgutil database
+echo "Checking package database ..."
+items="kexts tools-and-services"
+for item in $items; do
+    pkg_item="org.virtualbox.pkg.additions.$item"
+    installed=`pkgutil --pkgs="$pkg_item"`
+    if [ ! -z "$installed" ] ; then
+        sudo -p "Please enter %u's password (removing $pkg_item):" pkgutil --forget "$pkg_item"
+    fi
+done
+
+echo "Done."
+exit 0;
Index: trunk/src/VBox/Additions/darwin/Installer/Makefile.kmk
===================================================================
--- trunk/src/VBox/Additions/darwin/Installer/Makefile.kmk	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/Installer/Makefile.kmk	(revision 48251)
@@ -0,0 +1,383 @@
+# $Id$
+## @file
+# Install misc stuff and create dist packages for Mac OS X Guest Additions.
+#
+
+#
+# Copyright (C) 2006-2013 Oracle Corporation
+#
+# This file is part of VirtualBox Open Source Edition (OSE), as
+# available from http://www.virtualbox.org. This file is free software;
+# you can redistribute it and/or modify it under the terms of the GNU
+# General Public License (GPL) as published by the Free Software
+# Foundation, in version 2 as it comes in the "COPYING" file of the
+# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+#
+
+SUB_DEPTH = ../../../../..
+include $(KBUILD_PATH)/subheader.kmk
+
+
+#
+# Globals
+#
+VBOX_PATH_DI_SRC := $(PATH_SUB_CURRENT)
+VBOX_DI_OUT_DIR  := $(PATH_TARGET)/additions/Installer
+BLDDIRS += $(VBOX_DI_OUT_DIR)
+
+ifdef VBOX_WITH_COMBINED_PACKAGE
+ ifeq ($(KBUILD_TARGET_ARCH),x86)
+  VBOX_PATH_DIST_32 = $(VBOX_PATH_DIST)/additions
+  VBOX_PATH_DIST_64 = $(PATH_OUT_BASE)/darwin.amd64/$(KBUILD_TYPE)/dist/additions
+ else
+  VBOX_PATH_DIST_64 = $(VBOX_PATH_DIST)/additions
+  VBOX_PATH_DIST_32 = $(PATH_OUT_BASE)/darwin.x86/$(KBUILD_TYPE)/dist
+ endif
+ VBOX_DI_FN_DEP_BOTH = $(VBOX_PATH_DIST_32)/$1 $(VBOX_PATH_DIST_64)/$2
+ VBOX_DI_FN_DEP_32   = $(VBOX_PATH_DIST_32)/$1
+ VBOX_DI_FN_DEP_64   = $(VBOX_PATH_DIST_64)/$1
+ VBOX_DI_LIPO = lipo
+else
+ VBOX_DI_FN_DEP_BOTH = $(VBOX_PATH_DIST)/additions/$1
+ ifeq ($(KBUILD_TARGET_ARCH),x86)
+  VBOX_DI_FN_DEP_32  = $(VBOX_PATH_DIST)/additions/$1
+  VBOX_DI_FN_DEP_64  =
+ else
+  VBOX_DI_FN_DEP_64  = $(VBOX_PATH_DIST)/additions/$1
+  VBOX_DI_FN_DEP_32  =
+ endif
+endif
+
+# Unset this to speed up things during makefile hacking.
+VBOX_DARWIN_INST_DEP_ON_MAKEFILE := $(MAKEFILE_CURRENT)
+
+# The location of the pkgbuild program.
+ifndef VBOX_PKGBUILD
+ VBOX_PKGBUILD := pkgbuild
+endif
+
+# The location of the productbuild program.
+ifndef VBOX_PRODUCTBUILD
+ VBOX_PRODUCTBUILD := productbuild
+endif
+
+# Where we do the packing (override this in LocalConfig.kmk if building on smbfs).
+ifndef VBOX_PATH_PACK_TMP
+ VBOX_PATH_PACK_TMP := $(VBOX_DI_OUT_DIR)
+endif
+
+
+#
+# The packing.
+#
+PACKING += $(VBOX_PATH_DIST)/VBoxDarwinAdditions.dmg
+#OTHER_CLEAN = TODO
+
+
+include $(FILE_KBUILD_SUB_FOOTER)
+
+
+#
+# We're running commands as root here, take some care and assertion
+# a sane environment.
+#
+ifeq ($(strip $(VBOX_PATH_DIST)),)
+ $(error VBOX_PATH_DIST=$(VBOX_PATH_DIST))
+endif
+ifeq ($(strip $(VBOX_PATH_DIST)),/)
+ $(error VBOX_PATH_DIST=$(VBOX_PATH_DIST))
+endif
+ifeq ($(strip $(VBOX_PATH_PACK_TMP)),)
+ $(error VBOX_PATH_PACK_TMP=$(VBOX_PATH_PACK_TMP))
+endif
+ifeq ($(strip $(VBOX_PATH_PACK_TMP)),/)
+ $(error VBOX_PATH_PACK_TMP=$(VBOX_PATH_PACK_TMP))
+endif
+
+#
+# The disk image.
+#
+$(VBOX_PATH_DIST)/VBoxDarwinAdditions.dmg: \
+		$(VBOX_PATH_PACK_TMP)/DiskImage/VBoxGuestAdditions.pkg \
+		$(VBOX_BRAND_DARWIN_DISKIMAGE_BG) \
+		$(VBOX_BRAND_DARWIN_DISKIMAGE_DS_STORE) \
+		$$(wildcard $(VBOX_PATH_DI_SRC)/DiskImage/*) \
+		$(VBOX_PATH_DI_SRC)/DiskImage/Uninstall.tool \
+		$(VBOX_DARWIN_INST_DEP_ON_MAKEFILE)
+	$(call MSG_TOOL,hdiutil,,,$@)
+	@# Cleanup any previously failed attempts and various trash.
+	sudo rm -Rf $(VBOX_PATH_PACK_TMP)/DiskImage.tmp
+	$(MKDIR) -p $(VBOX_PATH_PACK_TMP)/DiskImage.tmp/
+	sudo mv $(VBOX_PATH_PACK_TMP)/DiskImage/VBoxGuestAdditions.pkg $(VBOX_PATH_PACK_TMP)/DiskImage.tmp/
+	sudo rm -Rf \
+		$@ \
+		$(VBOX_PATH_PACK_TMP)/DiskImage/
+	sudo mv $(VBOX_PATH_PACK_TMP)/DiskImage.tmp $(VBOX_PATH_PACK_TMP)/DiskImage
+	@# Remove .dmg packages from old depend builds
+	$(QUIET)$(RM) -f $(wildcard $(VBOX_PATH_DIST)/VBoxDarwinAdditions-*-r*.dmg)
+	@# Populate the image with uninstaller, readme, picture, and .VolumeIcon.icns. (TODO)
+	$(INSTALL) $(VBOX_PATH_DI_SRC)/DiskImage/Uninstall.tool  $(VBOX_PATH_PACK_TMP)/DiskImage/
+ifeq (1,1)
+	@# Pedantic mode...
+	$(INSTALL) -m 644 $(VBOX_BRAND_DARWIN_DISKIMAGE_DS_STORE)           $(VBOX_PATH_PACK_TMP)/DiskImage/.DS_Store
+	$(MKDIR) $(VBOX_PATH_PACK_TMP)/DiskImage/.background
+	$(INSTALL) -m 644 $(VBOX_BRAND_DARWIN_DISKIMAGE_BG)                 $(VBOX_PATH_PACK_TMP)/DiskImage/.background/vbox_folder.tiff
+endif
+	@# Change the owners.
+	sudo chown -R root:admin $(VBOX_PATH_PACK_TMP)/DiskImage
+	@# Create the image.
+	sudo hdiutil create -format UDBZ -volname "VirtualBox Guest Additions" -srcfolder "$(VBOX_PATH_PACK_TMP)/DiskImage" "$@"
+	@# Change (back) the owner so it can be deleted by the user.
+	sudo chown "$(shell whoami)" "$@"
+	sudo chown -R "$(shell whoami)" $(VBOX_PATH_PACK_TMP)/DiskImage
+
+#
+# The meta-package.
+#
+
+$(VBOX_PATH_PACK_TMP)/DiskImage/VBoxGuestAdditions.pkg: \
+		$(VBOX_PATH_PACK_TMP)/Packages/VBoxGuestAdditionsKEXTs.pkg \
+		$(VBOX_PATH_PACK_TMP)/Packages/VBoxGuestAdditionsToolsAndServices.pkg \
+		$$(wildcard $(VBOX_PATH_DI_SRC)/VBoxGuestAdditions_mpkg/* \
+		            $(VBOX_PATH_DI_SRC)/VBoxGuestAdditions_mpkg/*.lproj/*) \
+		$(foreach f,$(VBOX_INSTALLER_ADD_LANGUAGES), $(VBOX_BRAND_$(f)_VIRTUALBOX_WELCOME_RTF)) \
+		$(VBOX_PATH_DI_SRC)/VBoxGuestAdditions_mpkg/Welcome.rtf \
+		$(VBOX_PATH_DI_SRC)/VBoxGuestAdditions_mpkg/Conclusion.rtf \
+		$(VBOX_DARWIN_INST_DEP_ON_MAKEFILE)
+	$(call MSG_TOOL,productbuild,,,$@)
+	@# Cleanup any previously failed attempts.
+	sudo rm -Rf \
+		$@ \
+		$(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.root \
+		$(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.desc \
+		$(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res
+	@# Correct directory permissions are important.
+	$(MKDIR) -p \
+		$(@D) \
+		$(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.desc \
+		$(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res \
+		$(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res/English.lproj
+	
+	@# Do keyword replacement in the package info and description files.
+	$(SED) \
+		-e 's/@VBOX_VERSION_STRING@/$(VBOX_VERSION_STRING)/g' \
+		-e 's/@VBOX_VERSION_MAJOR@/$(VBOX_VERSION_MAJOR)/g' \
+		-e 's/@VBOX_VERSION_MINOR@/$(VBOX_VERSION_MINOR)/g' \
+		-e 's/@VBOX_VERSION_BUILD@/$(VBOX_VERSION_BUILD)/g' \
+		-e 's/@VBOX_VENDOR@/$(VBOX_VENDOR)/g' \
+		-e 's/@VBOX_PRODUCT@/$(VBOX_PRODUCT)/g' \
+		-e 's/@VBOX_C_YEAR@/$(VBOX_C_YEAR)/g' \
+		--output $(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res/English.lproj/Welcome.rtf \
+		$(VBOX_PATH_DI_SRC)/VBoxGuestAdditions_mpkg/Welcome.rtf
+	@# Copy the resources.
+	$(INSTALL) -m 0644 $(VBOX_PATH_DI_SRC)/VBoxGuestAdditions_mpkg/Conclusion.rtf $(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res/English.lproj/Conclusion.rtf
+	
+	$(SED) \
+		-e 's/@VBOX_VENDOR@/$(VBOX_VENDOR)/g' \
+		-e 's/@VBOX_PRODUCT@/$(VBOX_PRODUCT)/g' \
+		-e 's/@VBOX_C_YEAR@/$(VBOX_C_YEAR)/g' \
+		--output $(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res/English.lproj/Localizable.strings \
+		$(VBOX_PATH_DI_SRC)/VBoxGuestAdditions_mpkg/Localizable.strings
+	$(INSTALL) -m 0644 $(VBOX_BRAND_DARWIN_INSTALLER_BG)                          $(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res/background.tif
+	
+	$(foreach f,$(VBOX_INSTALLER_ADD_LANGUAGES), \
+		$(MKDIR) -p \
+			$(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res/$(VBOX_INSTALLER_$(f)_DARWIN_TARGET).lproj$(NLTAB) \
+		$(SED) \
+			-e 's/@VBOX_VERSION_STRING@/$(VBOX_VERSION_STRING)/g' \
+			-e 's/@VBOX_VERSION_MAJOR@/$(VBOX_VERSION_MAJOR)/g' \
+			-e 's/@VBOX_VERSION_MINOR@/$(VBOX_VERSION_MINOR)/g' \
+			-e 's/@VBOX_VERSION_BUILD@/$(VBOX_VERSION_BUILD)/g' \
+			--output $(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res/$(VBOX_INSTALLER_$(f)_DARWIN_TARGET).lproj/Welcome.rtf \
+			$(VBOX_BRAND_$(f)_VIRTUALBOX_WELCOME_RTF)$(NLTAB) \
+		$(INSTALL) -m 0644 $(VBOX_BRAND_$(f)_VIRTUALBOX_CONCLUSION_RTF)           $(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res/$(VBOX_INSTALLER_$(f)_DARWIN_TARGET).lproj/Conclusion.rtf$(NLTAB) \
+		$(SED) \
+			-e 's/@VBOX_VENDOR@/$(VBOX_VENDOR)/g' \
+			-e 's/@VBOX_PRODUCT@/$(VBOX_PRODUCT)/g' \
+			-e 's/@VBOX_C_YEAR@/$(VBOX_C_YEAR)/g' \
+			--output $(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res/$(VBOX_INSTALLER_$(f)_DARWIN_TARGET).lproj/Localizable.strings \
+			$(VBOX_BRAND_$(f)_VIRTUALBOX_LOCALIZABLE_STRINGS)$(NLTAB) \
+	)
+	
+	@# Build the package.
+	$(VBOX_PRODUCTBUILD) \
+		--distribution $(VBOX_PATH_DI_SRC)/VBoxGuestAdditions_mpkg/distribution.dist \
+		--package-path $(VBOX_PATH_PACK_TMP)/Packages \
+		--resources $(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res \
+		--identifier org.VirtualBox.mpkg.GuestAdditions \
+		--version $(VBOX_VERSION_MAJOR).$(VBOX_VERSION_MINOR).$(VBOX_VERSION_BUILD) \
+		$(if $(VBOX_MACOSX_INSTALLER_SIGN),--sign "$(VBOX_MACOSX_INSTALLER_SIGN)",) \
+		$@ 
+	
+	@# Cleanup.
+	sudo rm -Rf \
+		$(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.root \
+		$(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.desc \
+		$(VBOX_PATH_PACK_TMP)/VBoxDarwinAdditions.dist.res
+
+VBoxDarwinAdditions.pkg:: $(VBOX_PATH_PACK_TMP)/DiskImage/VBoxDarwinAdditions.pkg
+
+#
+# The VirtualBox Kernel extensions.
+#
+VBOX_DI_KEXTS_UNIVERSAL = VBoxGuest
+VBOX_DI_KEXTS = $(VBOX_DI_KEXTS_UNIVERSAL)
+
+$(VBOX_PATH_PACK_TMP)/Packages/VBoxGuestAdditionsKEXTs.pkg: \
+		$(foreach kext,$(VBOX_DI_KEXTS_UNIVERSAL), $(call VBOX_DI_FN_DEP_BOTH,$(kext).kext/Contents/MacOS/$(kext))) \
+		$(foreach kext,$(VBOX_DI_KEXTS), $(VBOX_PATH_DIST)/additions/$(kext).kext/Contents/Info.plist) \
+		$$(wildcard $(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsKEXTs/* \
+		            $(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsKEXTs/*.lproj/*) \
+		$(foreach f,$(VBOX_INSTALLER_ADD_LANGUAGES), \
+			$(VBOX_BRAND_$(f)_VBOXKEXTS_DESCRIPTION_PLIST) \
+			$(VBOX_BRAND_$(f)_VBOXKEXTS_README_HTML) \
+			$(VBOX_BRAND_$(f)_VBOXKEXTS_INSTALLATIONCHECK_STRINGS)) \
+		$(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsKEXTs/postflight \
+		$(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsKEXTs/PkgBuildComponent.plist \
+		$(VBOX_DARWIN_INST_DEP_ON_MAKEFILE)
+	$(call MSG_TOOL,pkgbuild,,,$@)
+	@# Cleanup any previously failed attempts.
+	sudo rm -Rf \
+		$@ \
+		$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root \
+		$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.desc \
+		$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.res
+	@# Correct directory permissions are important.
+	$(MKDIR) -p \
+		$(@D) \
+		$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.desc \
+		$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.res \
+		$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.res/English.lproj
+	$(MKDIR) -p -m 1775 $(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library
+	$(MKDIR) -p -m 0755 \
+		$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library/Extensions \
+		$(foreach kext,$(VBOX_DI_KEXTS), \
+			$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library/Extensions/$(kext).kext \
+			$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library/Extensions/$(kext).kext/Contents \
+			$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library/Extensions/$(kext).kext/Contents/MacOS )
+	@# Copy the common files (Info.plist).
+	$(foreach kext,$(VBOX_DI_KEXTS), \
+		$(NLTAB)$(INSTALL) -m 0644 $(VBOX_PATH_DIST)/additions/$(kext).kext/Contents/Info.plist $(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library/Extensions/$(kext).kext/Contents/)
+	@# Copy the binaries and invoking lipo.
+ifdef VBOX_WITH_COMBINED_PACKAGE
+	$(foreach kext,$(VBOX_DI_KEXTS_UNIVERSAL), \
+		$(NLTAB)$(VBOX_DI_LIPO) -create \
+			$(VBOX_PATH_DIST_32)/additions/$(kext).kext/Contents/MacOS/$(kext) \
+			$(VBOX_PATH_DIST_64)/additions/$(kext).kext/Contents/MacOS/$(kext) \
+			-output $(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library/Extensions/$(kext).kext/Contents/MacOS/$(kext))
+else
+	$(foreach kext,$(VBOX_DI_KEXTS), \
+		$(NLTAB)$(INSTALL) -m 0755 $(VBOX_PATH_DIST)/additions/$(kext).kext/Contents/MacOS/$(kext) $(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library/Extensions/$(kext).kext/Contents/MacOS/)
+endif
+	@# Signed the kext bundles.
+ifdef VBOX_SIGNING_MODE
+	$(foreach kext,$(VBOX_DI_KEXTS), \
+		$(NLTAB)$(call VBOX_SIGN_BUNDLE_FN,$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library/Extensions/$(kext).kext,) )
+endif
+	@# Set the correct owners.
+	sudo chown    root:admin $(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library
+	sudo chown -R root:wheel $(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library/Extensions
+	
+	# Copy package internal files
+	$(INSTALL) $(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsKEXTs/PkgBuildComponent.plist $(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.desc/PkgBuildComponent.plist
+	
+	# Copy installer scripts
+	$(INSTALL) -m 0755 $(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsKEXTs/postflight $(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.res
+	
+	@# Build the package.
+	$(VBOX_PKGBUILD) \
+		--root   $(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root/Library/Extensions/ \
+		--component-plist $(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.desc/PkgBuildComponent.plist \
+		--script $(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.res \
+		--identifier org.virtualbox.pkg.additions.kexts \
+		--version $(VBOX_VERSION_MAJOR).$(VBOX_VERSION_MINOR).$(VBOX_VERSION_BUILD) \
+		--install-location /Library/Extensions/ \
+		--ownership preserve \
+		$(if $(VBOX_MACOSX_INSTALLER_SIGN),--sign "$(VBOX_MACOSX_INSTALLER_SIGN)",) \
+		$@
+	@# Cleanup
+	sudo rm -Rf \
+		$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.root \
+		$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.desc \
+		$(VBOX_PATH_PACK_TMP)/VBoxGuestAdditionsKEXTs.pkg.res
+
+#
+# The VirtualBox Guest Additions Tools & Services.
+#
+
+VBOX_GA_PKG=VBoxGuestAdditionsToolsAndServices.pkg
+VBOX_DI_VB_GA_BINARIES=VBoxClient VBoxControl VBoxService
+$(VBOX_PATH_PACK_TMP)/Packages/$(VBOX_GA_PKG): \
+		$(foreach f, $(VBOX_DI_VB_GA_BINARIES)\
+			,$(call VBOX_DI_FN_DEP_BOTH,$(f)) ) \
+		$(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsToolsAndServices/org.virtualbox.additions.vboxclient.plist \
+		$(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsToolsAndServices/org.virtualbox.additions.vboxservice.plist \
+		$(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsToolsAndServices/VBoxServiceWrapper \
+		$(VBOX_PATH_DI_SRC)/DiskImage/Uninstall.tool \
+		$(VBOX_DARWIN_INST_DEP_ON_MAKEFILE)
+	$(call MSG_TOOL,pkgbuild,,,$@)
+	@# Cleanup any previously failed attempts.
+	sudo rm -Rf \
+		$@ \
+		$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root \
+		$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).desc \
+		$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).res
+	@# Correct directory permissions are important.
+	$(MKDIR) -p \
+		$(@D) \
+		$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).desc \
+		$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).res \
+		$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).res/English.lproj
+	
+	@# Create directory structure within a package w/ proper permittions
+	$(MKDIR) -p -m 0775 \
+	    "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/Application Support/VirtualBox Guest Additions" \
+	    "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/LaunchAgents" \
+	    "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/LaunchDaemons"
+	
+	@# Install binaries
+ifdef VBOX_WITH_COMBINED_PACKAGE
+	$(foreach binary, $(VBOX_DI_VB_GA_BINARIES) \
+		,$(VBOX_DI_LIPO) -create \
+			$(VBOX_PATH_DIST_32)/additions/$(binary) \
+			$(VBOX_PATH_DIST_64)/additions/$(binary) \
+			-output "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/Application Support/VirtualBox Guest Additions/$(binary)"$(NLTAB))
+	$(foreach binary, $(VBOX_DI_VB_GA_BINARIES), \
+		$(NLTAB)$(INSTALL) -m 0755 $(VBOX_PATH_DIST_32)/additions/$(binary) "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/Application Support/VirtualBox Guest Additions/$(binary)-x86" \
+		$(NLTAB)$(INSTALL) -m 0755 $(VBOX_PATH_DIST_64)/additions/$(binary) "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/Application Support/VirtualBox Guest Additions/$(binary)-amd64" )
+else
+	$(foreach binary, $(VBOX_DI_VB_GA_BINARIES) \
+		,$(INSTALL) -m 0755 $(VBOX_PATH_DIST)/additions/$(binary)           "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/Application Support/VirtualBox Guest Additions/$(binary)"$(NLTAB))
+	$(foreach binary, $(VBOX_DI_VB_GA_BINARIES) \
+		,$(INSTALL) -m 0755 $(VBOX_PATH_DIST)/additions/$(binary)           "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/Application Support/VirtualBox Guest Additions/$(binary)-$(KBUILD_TARGET_ARCH)"$(NLTAB))
+endif
+	# Add Uninstall.tool
+	$(INSTALL) $(VBOX_PATH_DI_SRC)/DiskImage/Uninstall.tool  "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/Application Support/VirtualBox Guest Additions/" \
+	
+	@# Install launchd stuff
+	$(INSTALL) -m 0755 $(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsToolsAndServices/VBoxServiceWrapper \
+	                                                                         "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/Application Support/VirtualBox Guest Additions/"
+	$(INSTALL) -m 644 $(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsToolsAndServices/org.virtualbox.additions.vboxclient.plist \
+	                                                                         "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/LaunchAgents/"
+	$(INSTALL) -m 644 $(VBOX_PATH_DI_SRC)/VBoxGuestAdditionsToolsAndServices/org.virtualbox.additions.vboxservice.plist \
+	                                                                         "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/LaunchDaemons/"
+	
+	@# Correct ownership
+	sudo chown -R root:wheel "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/"
+	
+	@# Build the package.
+	$(VBOX_PKGBUILD) \
+		--root   "$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root/Library/" \
+		--script $(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).res \
+		--identifier org.virtualbox.pkg.additions.tools-and-services \
+		--version $(VBOX_VERSION_MAJOR).$(VBOX_VERSION_MINOR).$(VBOX_VERSION_BUILD) \
+		--install-location "/Library/" \
+		--ownership preserve \
+		$(if $(VBOX_MACOSX_INSTALLER_SIGN),--sign "$(VBOX_MACOSX_INSTALLER_SIGN)",) \
+		$@
+	@# Cleanup
+	sudo rm -Rf \
+		$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).root \
+		$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).desc \
+		$(VBOX_PATH_PACK_TMP)/$(VBOX_GA_PKG).res
Index: trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsKEXTs/PkgBuildComponent.plist
===================================================================
--- trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsKEXTs/PkgBuildComponent.plist	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsKEXTs/PkgBuildComponent.plist	(revision 48251)
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<array>
+    <dict>
+         <key>RootRelativeBundlePath</key>               <string>VBoxGuest.kext</string>
+         <key>BundleIsRelocatable</key>                  <false/>
+         <key>BundleIsVersionChecked</key>               <false/>
+         <key>BundleHasStrictIdentifier</key>            <false/>
+         <key>BundleOverwriteAction</key>                <string>upgrade</string>
+         <key>BundlePostInstallScriptPath</key>          <string>postflight</string>
+    </dict>
+</array>
+</plist>
+
Index: trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsKEXTs/postflight
===================================================================
--- trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsKEXTs/postflight	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsKEXTs/postflight	(revision 48251)
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+#
+# Copyright (C) 2007-2013 Oracle Corporation
+#
+# This file is part of VirtualBox Open Source Edition (OSE), as
+# available from http://www.virtualbox.org. This file is free software;
+# you can redistribute it and/or modify it under the terms of the GNU
+# General Public License (GPL) as published by the Free Software
+# Foundation, in version 2 as it comes in the "COPYING" file of the
+# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+#
+
+set -e
+
+# Setup environment.
+export PATH="/bin:/usr/bin:/sbin:/usr/sbin:$PATH"
+
+unload_service()
+{
+    ITEM_ID=$1
+    ITEM_PATH=$2
+    FORCED_USER=$3
+
+    loaded="NO"
+    test -n "$(sudo -u "$FORCED_USER" launchctl list | grep $ITEM_ID)" && loaded="YES"
+    if [ "$loaded" = "YES" ] ; then
+        echo "Unloading previously installed service: $ITEM_ID"
+        sudo -u "$FORCED_USER" launchctl unload -F "$ITEM_PATH/$ITEM_ID.plist"
+    fi
+}
+
+load_service()
+{
+    ITEM_ID=$1
+    ITEM_PATH=$2
+    FORCED_USER=$3
+
+    echo "Loading newly installed service: $ITEM_ID"
+    sudo -u "$FORCED_USER" launchctl load -F "$ITEM_PATH/$ITEM_ID.plist"
+}
+
+unload_service "org.virtualbox.additions.vboxservice" "/Library/LaunchDaemons" "root"
+unload_service "org.virtualbox.additions.vboxclient" "/Library/LaunchAgents" "${USER}"
+
+items="VBoxGuest"
+for item in $items; do
+    kext_item="org.virtualbox.kext.$item"
+
+    loaded="NO"
+    test -n "$(kextstat | grep $kext_item)" && loaded="YES"
+    if [ "$loaded" = "YES" ] ; then
+        echo "Unloading $item kernel extension..."
+        kextunload -b $kext_item
+    fi
+done
+echo "Loading newly installed kernel extensions."
+kextload "/Library/Extensions/VBoxGuest.kext"
+
+load_service "org.virtualbox.additions.vboxservice" "/Library/LaunchDaemons" "root"
+load_service "org.virtualbox.additions.vboxclient" "/Library/LaunchAgents" "${USER}"
+
+echo "Done."
+
+exit 0;
Index: trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsToolsAndServices/VBoxServiceWrapper
===================================================================
--- trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsToolsAndServices/VBoxServiceWrapper	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsToolsAndServices/VBoxServiceWrapper	(revision 48251)
@@ -0,0 +1,33 @@
+#!/bin/sh
+#
+# VBoxService wrapper script.
+#
+# Load required kernel extensions before start service (if necessary).
+#
+# Copyright (C) 2007-2013 Oracle Corporation
+#
+# This file is part of VirtualBox Open Source Edition (OSE), as
+# available from http://www.virtualbox.org. This file is free software;
+# you can redistribute it and/or modify it under the terms of the GNU
+# General Public License (GPL) as published by the Free Software
+# Foundation, in version 2 as it comes in the "COPYING" file of the
+# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+#
+
+export PATH="/bin:/usr/bin:/sbin:/usr/sbin:$PATH"
+
+echo "Check if kernel extensions loaded..."
+items="VBoxGuest"
+for item in $items; do
+    kext_item="org.virtualbox.kext.$item"
+    loaded=`kextstat | grep $kext_item`
+    if [ -z "$loaded" ] ; then
+        echo "Loading $item kernel extension..."
+        kextload /Library/Extensions/$item.kext
+    fi
+done
+
+"/Library/Application Support/VirtualBox Guest Additions/VBoxService" -f
+
+exit $?
Index: trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsToolsAndServices/org.virtualbox.additions.vboxclient.plist
===================================================================
--- trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsToolsAndServices/org.virtualbox.additions.vboxclient.plist	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsToolsAndServices/org.virtualbox.additions.vboxclient.plist	(revision 48251)
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+    <key>Disabled</key>  <false/>
+    <key>RunAtLoad</key> <true/>
+    <key>KeepAlive</key> <true/>
+    <key>Label</key>     <string>org.virtualbox.additions.vboxclient</string>
+
+    <key>ProgramArguments</key>
+        <array>
+            <string>/Library/Application Support/VirtualBox Guest Additions/VBoxClient</string>
+            <string>-f</string>
+        </array>
+</dict>
+</plist>
Index: trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsToolsAndServices/org.virtualbox.additions.vboxservice.plist
===================================================================
--- trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsToolsAndServices/org.virtualbox.additions.vboxservice.plist	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditionsToolsAndServices/org.virtualbox.additions.vboxservice.plist	(revision 48251)
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+    <key>Disabled</key>  <false/>
+    <key>RunAtLoad</key> <true/>
+    <key>KeepAlive</key> <true/>
+    <key>Label</key>     <string>org.virtualbox.additions.vboxservice</string>
+
+    <key>ProgramArguments</key>
+        <array>
+            <string>/Library/Application Support/VirtualBox Guest Additions/VBoxServiceWrapper</string>
+        </array>
+</dict>
+</plist>
Index: trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditions_mpkg/Localizable.strings
===================================================================
--- trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditions_mpkg/Localizable.strings	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditions_mpkg/Localizable.strings	(revision 48251)
@@ -0,0 +1,10 @@
+'VirtualBox_title' = '@VBOX_PRODUCT@';
+
+'choiceVBoxKEXTs_title' = 'Kernel Extensions';
+'choiceVBoxKEXTs_msg' = 'Installs the @VBOX_PRODUCT@ Guest Additions Kernel Extensions into /Library/Extensions.';
+
+'choiceVBoxToolsAndServices_title' = 'Tools and Services';
+'choiceVBoxToolsAndServices_msg' = 'Installs the @VBOX_PRODUCT@ Guest Additions Tools and Services to /Library/Application Support/VirtualBox Guest Additions.';
+
+'UNSUPPORTED_OS_TLE' = "Unsupported OS version detected!";
+'UNSUPPORTED_OS_MSG' = "The installer has detected an unsupported operation system. VirtualBox Guest Additions require Mac OS X 10.6 or later.";
Index: trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditions_mpkg/distribution.dist
===================================================================
--- trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditions_mpkg/distribution.dist	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/Installer/VBoxGuestAdditions_mpkg/distribution.dist	(revision 48251)
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+#
+# Copyright (C) 2008-2013 Oracle Corporation
+#
+# This file is part of VirtualBox Open Source Edition (OSE), as
+# available from http://www.virtualbox.org. This file is free software;
+# you can redistribute it and/or modify it under the terms of the GNU
+# General Public License (GPL) as published by the Free Software
+# Foundation, in version 2 as it comes in the "COPYING" file of the
+# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+#
+-->
+<installer-gui-script minSpecVersion="1.0">
+    <title>VirtualBox_title</title>
+    <options customize="allow" allow-external-scripts="yes" rootVolumeOnly="true" hostArchitectures="i386"/>
+    <installation-check script="checkPrerequisite()"></installation-check>
+    <domains enable_anywhere="false" enable_currentUserHome="false" enable_localSystem="true"/>
+    <script>
+    /* js:pkmk:start */
+    function checkPrerequisite()
+    {
+       try
+       {
+           test = system.version['ProductVersion'];
+           system.log("OS version detected: " + test);
+           result = (system.compareVersions(test, '10.6') >= 0);
+       } catch (e) { system.log(e); result = false; }
+
+       if (!result)
+       {
+           my.result.type = 'Fatal';
+           my.result.title = system.localizedString('UNSUPPORTED_OS_TLE');
+           my.result.message = system.localizedString('UNSUPPORTED_OS_MSG');
+           return result;
+       }
+
+       system.log("result:" + result);
+       return result;
+    }
+    /* js:pkmk:end */
+    </script>
+    <background file="background.tif" alignment="topleft" scaling="none"/>
+    <welcome file="Welcome.rtf" mime-type="text/rtf" uti="public.rtf"/>
+    <choices-outline>
+        <line choice="choiceVBoxToolsAndServices"></line>
+        <line choice="choiceVBoxKEXTs"></line>
+    </choices-outline>
+
+    <choice id="choiceVBoxToolsAndServices" title="choiceVBoxToolsAndServices_title" description="choiceVBoxToolsAndServices_msg" start_selected="true" start_enabled="false" start_visible="true">
+        <pkg-ref id="org.virtualbox.pkg.additions.tools-and-services"></pkg-ref>
+    </choice>
+    <choice id="choiceVBoxKEXTs"   title="choiceVBoxKEXTs_title"   description="choiceVBoxKEXTs_msg"   start_selected="true" start_enabled="false" start_visible="true">
+        <pkg-ref id="org.virtualbox.pkg.additions.kexts"></pkg-ref>
+    </choice>
+
+    <pkg-ref id="org.virtualbox.pkg.additions.tools-and-services" auth="Root">file:./Contents/Packages/VBoxGuestAdditionsToolsAndServices.pkg</pkg-ref>
+    <pkg-ref id="org.virtualbox.pkg.additions.kexts" auth="Root">file:./Contents/Packages/VBoxGuestAdditionsKEXTs.pkg</pkg-ref>
+
+</installer-gui-script>
Index: trunk/src/VBox/Additions/darwin/Makefile.kmk
===================================================================
--- trunk/src/VBox/Additions/darwin/Makefile.kmk	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/Makefile.kmk	(revision 48251)
@@ -0,0 +1,24 @@
+# $Id$
+## @file
+# Makefile for the Mac OS guest additions base directory.
+#
+
+#
+# Copyright (C) 2010-2013 Oracle Corporation
+#
+# This file is part of VirtualBox Open Source Edition (OSE), as
+# available from http://www.virtualbox.org. This file is free software;
+# you can redistribute it and/or modify it under the terms of the GNU
+# General Public License (GPL) as published by the Free Software
+# Foundation, in version 2 as it comes in the "COPYING" file of the
+# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+#
+
+SUB_DEPTH = ../../../..
+include $(KBUILD_PATH)/subheader.kmk
+
+include $(PATH_SUB_CURRENT)/VBoxClient/Makefile.kmk
+include $(PATH_SUB_CURRENT)/Installer/Makefile.kmk
+
+include $(FILE_KBUILD_SUB_FOOTER)
Index: trunk/src/VBox/Additions/darwin/VBoxClient/Makefile.kmk
===================================================================
--- trunk/src/VBox/Additions/darwin/VBoxClient/Makefile.kmk	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/VBoxClient/Makefile.kmk	(revision 48251)
@@ -0,0 +1,41 @@
+# $Id$
+## @file
+# Sub-Makefile for the VirtualBox Guest Addition Darwin Client.
+#
+
+#
+# Copyright (C) 2006-2013 Oracle Corporation
+#
+# This file is part of VirtualBox Open Source Edition (OSE), as
+# available from http://www.virtualbox.org. This file is free software;
+# you can redistribute it and/or modify it under the terms of the GNU
+# General Public License (GPL) as published by the Free Software
+# Foundation, in version 2 as it comes in the "COPYING" file of the
+# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+#
+
+SUB_DEPTH = ../../../../..
+include $(KBUILD_PATH)/subheader.kmk
+
+#
+# VBoxClient - shared clipboard support.
+#
+
+PROGRAMS += VBoxClient
+
+VBoxClient_TEMPLATE = NewVBoxGuestR3Exe
+VBoxClient_DEFS    += VBOX_WITH_HGCM
+
+VBoxClient_SOURCES  = \
+    VBoxClient.cpp \
+    VBoxClientClipboard.cpp \
+    VBoxClientClipboardHostToGuest.cpp \
+    VBoxClientClipboardGuestToHost.cpp \
+    $(PATH_ROOT)/src/VBox/GuestHost/SharedClipboard/clipboard-helper.cpp
+
+VBoxClient_LDFLAGS  = -framework IOKit -framework ApplicationServices
+VBoxClient_INST     = $(INST_ADDITIONS)
+
+include $(FILE_KBUILD_SUB_FOOTER)
+
Index: trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClient.cpp
===================================================================
--- trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClient.cpp	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClient.cpp	(revision 48251)
@@ -0,0 +1,297 @@
+/** $Id$ */
+/** @file
+ * VBoxClient - User specific services, Darwin.
+ */
+
+/*
+ * Copyright (C) 2007-2013 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ */
+
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include <stdio.h>
+#include <signal.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <VBox/log.h>
+#include <VBox/VBoxGuestLib.h>
+#include <iprt/stream.h>
+#include <iprt/initterm.h>
+#include <iprt/message.h>
+#include <iprt/process.h>
+#include <iprt/string.h>
+
+#include "VBoxClientInternal.h"
+
+/*******************************************************************************
+*   Glogal Variables                                                           *
+*******************************************************************************/
+
+static int                  g_cVerbosity = 0;
+static PRTLOGGER            g_pLogger = NULL;
+
+static VBOXCLIENTSERVICE    g_aServices[] =
+{
+    { g_ClipboardService },
+};
+
+
+/**
+ * Create default logger in order to print output to the specified file.
+ *
+ * @return  IPRT status code.
+ */
+static int vbclInitLogger(char *szLogFileName)
+{
+    int rc;
+
+    uint32_t                   fFlags         = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG;
+    static const char * const  s_apszGroups[] = VBOX_LOGGROUP_NAMES;
+    uint32_t                   fDestFlags     = RTLOGDEST_STDOUT;
+
+    rc = RTLogCreateEx(&g_pLogger,
+                       fFlags,                          /* Logger instance flags, a combination of the RTLOGFLAGS_* values */
+                       "all",                           /* The initial group settings */
+                       "VBOXCLIENT_RELEASE_LOG",        /* Base name for the environment variables for this instance */
+                       RT_ELEMENTS(s_apszGroups),       /* Number of groups in the array */
+                       s_apszGroups,                    /* Pointer to array of groups. This must stick around for the life of the logger instance */
+                       fDestFlags,                      /* The destination flags */
+                       NULL,                            /* Callback function for starting logging and for ending or starting a new file for log history rotation */
+                       szLogFileName ? 10 : 0,          /* Number of old log files to keep when performing log history rotation */
+                       szLogFileName ? 100 * _1M : 0,   /* Maximum size of log file when performing history rotation */
+                       szLogFileName ? RT_SEC_1DAY : 0, /* Maximum time interval per log file when performing history rotation */
+                       0,                               /* A buffer which is filled with an error message if something fails */
+                       0,                               /* The size of the error message buffer */
+                       szLogFileName                    /* Log filename format string */
+                       );
+
+    AssertReturn(RT_SUCCESS(rc), rc);
+
+    /* Register this logger as the release logger */
+    RTLogRelSetDefaultInstance(g_pLogger);
+
+    /* Explicitly flush the log in case of VBOXCLIENT_RELEASE_LOG=buffered. */
+    RTLogFlush(g_pLogger);
+
+    return rc;
+}
+
+
+/**
+ * Destroy logger.
+ */
+static void vbclTermLogger(char *szLogFileName)
+{
+    // Why SIGBUS here?
+    RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
+
+    if (szLogFileName)
+        RTStrFree(szLogFileName);
+}
+
+/**
+ * Displays a verbose message.
+ *
+ * @param   iLevel      Minimum log level required to display this message.
+ * @param   pszFormat   The message text.
+ * @param   ...         Format arguments.
+ */
+void VBoxClientVerbose(int iLevel, const char *pszFormat, ...)
+{
+    if (iLevel > g_cVerbosity)
+        return;
+
+    va_list args;
+    va_start(args, pszFormat);
+    char *psz = NULL;
+    RTStrAPrintfV(&psz, pszFormat, args);
+    va_end(args);
+
+    AssertPtr(psz);
+    LogRel(("%s", psz));
+
+    RTStrFree(psz);
+}
+
+/**
+ * Wait for signals in order to safely terminate process.
+ */
+static void vbclWait(void)
+{
+    sigset_t signalMask;
+    int      iSignal;
+
+    /* Register signals that we are waiting for */
+    sigemptyset(&signalMask);
+    sigaddset(&signalMask, SIGHUP);
+    sigaddset(&signalMask, SIGINT);
+    sigaddset(&signalMask, SIGQUIT);
+    sigaddset(&signalMask, SIGABRT);
+    sigaddset(&signalMask, SIGTERM);
+    pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
+
+    /* Ignoring return status */
+    sigwait(&signalMask, &iSignal);
+}
+
+/**
+ * Start registered services.
+ *
+ * @return  IPRT status code.
+ */
+static int vbclStartServices(void)
+{
+    int rc;
+    unsigned int iServiceId = 0;
+
+    VBoxClientVerbose(1, "Starting services...\n");
+    for (iServiceId = 0; iServiceId < RT_ELEMENTS(g_aServices); iServiceId++)
+    {
+        VBoxClientVerbose(1, "Starting service: %s\n", g_aServices[iServiceId].pszName);
+        rc = (g_aServices[iServiceId].pfnStart)();
+        if (RT_FAILURE(rc))
+        {
+            VBoxClientVerbose(1, "unable to start service: %s (%Rrc)\n", g_aServices[iServiceId].pszName, rc);
+            VBoxClientVerbose(1, "Rolling back..\n");
+
+            /* Stop running services */
+            do
+            {
+                VBoxClientVerbose(1, "Stopping service: %s\n", g_aServices[iServiceId].pszName);
+                int rcStop = (g_aServices[iServiceId].pfnStop)();
+                if (RT_FAILURE(rcStop))
+                    VBoxClientVerbose(1, "unable to stop service: %s (%Rrc)\n", g_aServices[iServiceId].pszName, rcStop);
+            } while (--iServiceId != 0);
+
+            break;
+        }
+    }
+
+    if (RT_SUCCESS(rc))
+        VBoxClientVerbose(1, "Services start completed.\n");
+
+    return rc;
+}
+
+/**
+ * Stop registered services.
+ *
+ * @return  IPRT status code.
+ */
+static void vbclStopServices(void)
+{
+    unsigned int iServiceId = 0;
+
+    VBoxClientVerbose(1, "Stopping services...\n");
+    for (iServiceId = 0; iServiceId < RT_ELEMENTS(g_aServices); iServiceId++)
+    {
+        VBoxClientVerbose(1, "Stopping service: %s\n", g_aServices[iServiceId].pszName);
+        int rc = (g_aServices[iServiceId].pfnStop)();
+        if (RT_FAILURE(rc))
+            VBoxClientVerbose(1, "unable to stop service: %s (%Rrc)\n", g_aServices[iServiceId].pszName, rc);
+    }
+    VBoxClientVerbose(1, "Services stop completed\n");
+}
+
+
+static void usage(char *sProgName)
+{
+    RTPrintf("usage: %s [-fvl]\n", sProgName);
+    RTPrintf("       -f\tRun in foreground (default: no)\n", sProgName);
+    RTPrintf("       -v\tIncrease verbosity level (default: no verbosity)\n", sProgName);
+    RTPrintf("       -l\tSpecify log file name (default: no log file)\n", sProgName);
+    exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+    int  rc;
+    int  c;
+
+    bool         fDemonize     = true;
+    static char *szLogFileName = NULL;
+
+    /* Parse command line */
+    while((c = getopt(argc, argv, "fvl:")) != -1)
+    {
+        switch(c)
+        {
+            case 'f':
+                fDemonize = false;
+                break;
+            case 'v':
+                g_cVerbosity++;
+                break;
+            case 'l':
+                szLogFileName = RTStrDup(optarg);
+                break;
+
+            default : usage(argv[0]);
+        }
+    }
+
+    /* No more arguments allowed */
+    if ((argc - optind) != 0)
+        usage(argv[0]);
+
+    if (fDemonize)
+    {
+        rc = RTProcDaemonizeUsingFork(true /* fNoChDir */, false /* fNoClose */, NULL);
+        if (RT_FAILURE(rc))
+        {
+            RTPrintf("failed to run into background\n");
+            return 1;
+        }
+    }
+
+    rc = RTR3InitExe(argc, &argv, 0);
+    if (RT_FAILURE(rc))
+    {
+        RTPrintf("RTR3InitExe() failed: (%Rrc)\n", rc);
+        return RTMsgInitFailure(rc);
+    }
+
+    rc = VbglR3Init();
+    if (RT_SUCCESS(rc))
+    {
+        rc = vbclInitLogger(szLogFileName);
+        if (RT_SUCCESS(rc))
+        {
+            rc = vbclStartServices();
+            if (RT_SUCCESS(rc))
+            {
+                vbclWait();
+                vbclStopServices();
+            }
+            else
+            {
+                RTPrintf("failed to start services: (%Rrc)\n", rc);
+            }
+
+            vbclTermLogger(szLogFileName);
+        }
+        else
+        {
+            RTPrintf("failed to start logger: (%Rrc)\n", rc);
+        }
+
+        VbglR3Term();
+    }
+    else
+    {
+        RTPrintf("failed to initialize guest library: (%Rrc)\n", rc);
+    }
+
+    return 0;
+}
Index: trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientClipboard.cpp
===================================================================
--- trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientClipboard.cpp	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientClipboard.cpp	(revision 48251)
@@ -0,0 +1,328 @@
+/** $Id$ */
+/** @file
+ * VBoxClient - Shared Slipboard Dispatcher, Darwin.
+ */
+
+/*
+ * Copyright (C) 2007-2013 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ */
+
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include <Carbon/Carbon.h>
+
+#include <iprt/asm.h>
+#include <iprt/stream.h>
+#include <iprt/thread.h>
+#include <iprt/critsect.h>
+#include <VBox/VBoxGuestLib.h>
+#include <VBox/HostServices/VBoxClipboardSvc.h>
+#include <VBox/GuestHost/clipboard-helper.h>
+#include "VBoxClientInternal.h"
+
+
+/*******************************************************************************
+*   Global Variables                                                           *
+*******************************************************************************/
+
+/** Host clipboard connection client ID */
+static uint32_t         g_u32ClientId;
+/* Guest clipboard reference */
+static PasteboardRef    g_PasteboardRef;
+/* Dispatcher tharead handle */
+RTTHREAD                g_DispatcherThread;
+/* Pasteboard polling tharead handle */
+RTTHREAD                g_GuestPasteboardThread;
+/* Flag that indicates whether or not dispatcher and Pasteboard polling threada should stop */
+static bool volatile    g_fShouldStop;
+/* Barrier for Pasteboard */
+static RTCRITSECT       g_critsect;
+
+
+/*******************************************************************************
+*   Local Macros                                                               *
+*******************************************************************************/
+
+#define VBOXCLIENT_SERVICE_NAME     "clipboard"
+
+
+/*******************************************************************************
+*   Local Function Prototypes                                                  *
+*******************************************************************************/
+static int vbclClipboardStop(void);
+
+
+/**
+ * Clipboard dispatcher function.
+ *
+ * Forwards cliproard content between host and guest.
+ *
+ * @param   ThreadSelf  Unused parameter.
+ * @param   pvUser      Unused parameter.
+ *
+ * @return  IPRT status code.
+ */
+static DECLCALLBACK(int) vbclClipboardDispatcher(RTTHREAD ThreadSelf, void *pvUser)
+{
+    bool fQuit = false;
+    NOREF(ThreadSelf);
+    NOREF(pvUser);
+
+    VBoxClientVerbose(2, "starting host clipboard polling thread\n");
+
+    /*
+     * Block all signals for this thread. Only the main thread will handle signals.
+     */
+    sigset_t signalMask;
+    sigfillset(&signalMask);
+    pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
+
+    while (!fQuit && !ASMAtomicReadBool(&g_fShouldStop))
+    {
+        int rc;
+        uint32_t Msg;
+        uint32_t fFormats;
+
+        VBoxClientVerbose(2, "waiting for new host request\n");
+
+        rc = VbglR3ClipboardGetHostMsg(g_u32ClientId, &Msg, &fFormats);
+        if (RT_SUCCESS(rc))
+        {
+            RTCritSectEnter(&g_critsect);
+            switch (Msg)
+            {
+                /* The host is terminating */
+                case VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT:
+                    VBoxClientVerbose(2, "host requested quit\n");
+                    fQuit = true;
+                    break;
+
+                /* The host needs data in the specified format */
+                case VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA:
+                    VBoxClientVerbose(2, "host requested guest's clipboard read\n");
+                    rc = vbclClipboardForwardToHost(g_u32ClientId, g_PasteboardRef, fFormats);
+                    AssertMsg(RT_SUCCESS(rc), ("paste to host failed\n"));
+                    break;
+
+                /* The host has announced available clipboard formats */
+                case VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS:
+                    VBoxClientVerbose(2, "host requested guest's clipboard write\n");
+                    rc = vbclClipboardForwardToGuest(g_u32ClientId, g_PasteboardRef, fFormats);
+                    AssertMsg(RT_SUCCESS(rc), ("paste to guest failed\n"));
+                    break;
+
+                default:
+                    VBoxClientVerbose(2, "received unknow command from host service\n");
+                    RTThreadSleep(1000);
+            }
+
+            RTCritSectLeave(&g_critsect);
+        }
+        else
+        {
+            RTThreadSleep(1000);
+        }
+    }
+
+    VBoxClientVerbose(2, "host clipboard polling thread stopped\n");
+
+    return VINF_SUCCESS;
+}
+
+
+/**
+ * Clipboard dispatcher function.
+ *
+ * Forwards cliproard content between host and guest.
+ *
+ * @param   ThreadSelf  Unused parameter.
+ * @param   pvUser      Unused parameter.
+ *
+ * @return  IPRT status code.
+ */
+static DECLCALLBACK(int) vbclGuestPasteboardPoll(RTTHREAD ThreadSelf, void *pvUser)
+{
+    /*
+     * Block all signals for this thread. Only the main thread will handle signals.
+     */
+    sigset_t signalMask;
+    sigfillset(&signalMask);
+    pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
+
+    VBoxClientVerbose(2, "starting guest clipboard polling thread\n");
+
+    while (!ASMAtomicReadBool(&g_fShouldStop))
+    {
+        PasteboardSyncFlags fSyncFlags;
+        uint32_t            fFormats;
+        int                 rc;
+
+        RTCritSectEnter(&g_critsect);
+
+        fSyncFlags = PasteboardSynchronize(g_PasteboardRef);
+        if (fSyncFlags & kPasteboardModified)
+        {
+            fFormats = vbclClipboardGetAvailableFormats(g_PasteboardRef);
+            rc = VbglR3ClipboardReportFormats(g_u32ClientId, fFormats);
+            if (RT_FAILURE(rc))
+            {
+                VBoxClientVerbose(2, "failed to report pasteboard update (%Rrc)\n", rc);
+            }
+            else
+            {
+                VBoxClientVerbose(2, "guest clipboard update reported: %d\n", (int)fFormats);
+            }
+        }
+
+        RTCritSectLeave(&g_critsect);
+
+        /* Check pasteboard every 200 ms */
+        RTThreadSleep(200);
+    }
+
+    VBoxClientVerbose(2, "guest clipboard polling thread stopped\n");
+
+    return VINF_SUCCESS;
+}
+
+
+/**
+ * Initialize host and guest clipboards, start clipboard dispatcher loop.
+ *
+ * @return  IPRT status code.
+ */
+static int vbclClipboardStart(void)
+{
+    int rc;
+
+    VBoxClientVerbose(2, "starting clipboard\n");
+
+    rc = RTCritSectInit(&g_critsect);
+    if (RT_FAILURE(rc))
+        return VERR_GENERAL_FAILURE;
+
+    rc = VbglR3ClipboardConnect(&g_u32ClientId);
+    if (RT_SUCCESS(rc))
+    {
+        rc = PasteboardCreate(kPasteboardClipboard, &g_PasteboardRef);
+        if (rc == noErr)
+        {
+            /* Start dispatcher loop */
+            ASMAtomicWriteBool(&g_fShouldStop, false);
+            rc = RTThreadCreate(&g_DispatcherThread,
+                                vbclClipboardDispatcher,
+                                (void *)NULL,
+                                0,
+                                RTTHREADTYPE_DEFAULT,
+                                RTTHREADFLAGS_WAITABLE,
+                                VBOXCLIENT_SERVICE_NAME);
+            if (RT_SUCCESS(rc))
+            {
+                /* Start dispatcher loop */
+                ASMAtomicWriteBool(&g_fShouldStop, false);
+                rc = RTThreadCreate(&g_GuestPasteboardThread,
+                                    vbclGuestPasteboardPoll,
+                                    (void *)NULL,
+                                    0,
+                                    RTTHREADTYPE_DEFAULT,
+                                    RTTHREADFLAGS_WAITABLE,
+                                    VBOXCLIENT_SERVICE_NAME);
+                if (RT_SUCCESS(rc))
+                    return VINF_SUCCESS;
+
+                /* Stop dispatcher thread */
+                ASMAtomicWriteBool(&g_fShouldStop, true);
+                RTThreadWait(g_DispatcherThread, 10 * 1000 /* Wait 10 seconds */, NULL);
+
+            }
+            VBoxClientVerbose(2, "unable create dispatcher thread\n");
+            CFRelease(g_PasteboardRef);
+            g_PasteboardRef = NULL;
+
+        }
+        else
+        {
+            rc = VERR_GENERAL_FAILURE;
+            VBoxClientVerbose(2, "unable access guest clipboard\n");
+        }
+
+        vbclClipboardStop();
+
+    }
+    else
+    {
+        VBoxClientVerbose(2, "unable to establish connection to clipboard service: %Rrc\n", rc);
+    }
+
+    RTCritSectDelete(&g_critsect);
+
+    return rc;
+}
+
+
+/**
+ * Release host and guest clipboards, stop clipboard dispatcher loop.
+ *
+ * @return  IPRT status code.
+ */
+static int vbclClipboardStop(void)
+{
+    int rc;
+
+    VBoxClientVerbose(2, "stopping clipboard\n");
+
+    AssertReturn(g_u32ClientId != 0, VERR_GENERAL_FAILURE);
+
+    VbglR3ClipboardReportFormats(g_u32ClientId, 0);
+
+    rc = VbglR3ClipboardDisconnect(g_u32ClientId);
+    if (RT_SUCCESS(rc))
+        g_u32ClientId = 0;
+    else
+        VBoxClientVerbose(2, "unable to close clipboard service connection: %Rrc\n", rc);
+
+    if (g_PasteboardRef)
+    {
+        CFRelease(g_PasteboardRef);
+        g_PasteboardRef = NULL;
+    }
+
+    /* Stop dispatcher thread */
+    ASMAtomicWriteBool(&g_fShouldStop, true);
+    rc = RTThreadWait(g_DispatcherThread, 10 * 1000 /* Wait 10 seconds */, NULL);
+    if (RT_FAILURE(rc))
+        VBoxClientVerbose(2, "failed to stop dispatcher thread");
+
+    /* Stop Pasteboard polling thread */
+    rc = RTThreadWait(g_GuestPasteboardThread, 10 * 1000 /* Wait 10 seconds */, NULL);
+    if (RT_FAILURE(rc))
+        VBoxClientVerbose(2, "failed to stop pasteboard polling thread");
+
+    RTCritSectDelete(&g_critsect);
+
+    return rc;
+}
+
+
+/* Clipboard service struct */
+VBOXCLIENTSERVICE g_ClipboardService =
+{
+    /* pszName */
+    VBOXCLIENT_SERVICE_NAME,
+
+    /* pfnStart */
+    vbclClipboardStart,
+
+    /* pfnStop */
+    vbclClipboardStop,
+};
Index: trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientClipboardGuestToHost.cpp
===================================================================
--- trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientClipboardGuestToHost.cpp	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientClipboardGuestToHost.cpp	(revision 48251)
@@ -0,0 +1,376 @@
+/** $Id$ */
+/** @file
+ * VBoxClient - Shared Clipboard Guest -> Host copying, Darwin.
+ */
+
+/*
+ * Copyright (C) 2007-2013 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ */
+
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include <Carbon/Carbon.h>
+#include <signal.h>
+#include <stdlib.h>
+
+#include <iprt/thread.h>
+#include <iprt/mem.h>
+#include <iprt/stream.h>
+#include <iprt/initterm.h>
+#include <iprt/message.h>
+#include <VBox/VBoxGuestLib.h>
+#include <VBox/HostServices/VBoxClipboardSvc.h>
+#include <VBox/GuestHost/clipboard-helper.h>
+#include "VBoxClientInternal.h"
+
+/**
+ * Walk through pasteboard items and report currently available item types.
+ *
+ * @param   pPasteboard         Reference to guest Pasteboard.
+ * @returns Available formats bit field.
+ */
+uint32_t vbclClipboardGetAvailableFormats(PasteboardRef pPasteboard)
+{
+    uint32_t  fFormats = 0;
+    ItemCount cItems   = 0;
+    ItemCount iItem;
+    OSStatus  rc;
+
+#define VBOXCL_ADD_FORMAT_IF_PRESENT(a_kDarwinFmt, a_fVBoxFmt) \
+    if (PasteboardCopyItemFlavorData(pPasteboard, iItemID, a_kDarwinFmt, &flavorData) == noErr) \
+    { \
+        fFormats |= (uint32_t)a_fVBoxFmt; \
+        CFRelease(flavorData); \
+    }
+
+    rc = PasteboardGetItemCount(pPasteboard, &cItems);
+    AssertReturn((rc == noErr) && (cItems > 0), fFormats);
+
+    for (iItem = 1; iItem <= cItems; iItem++)
+    {
+        PasteboardItemID iItemID;
+        CFDataRef        flavorData;
+
+        rc = PasteboardGetItemIdentifier(pPasteboard, iItem, &iItemID);
+        if (rc == noErr)
+        {
+            VBOXCL_ADD_FORMAT_IF_PRESENT(kUTTypeUTF16PlainText, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT);
+            VBOXCL_ADD_FORMAT_IF_PRESENT(kUTTypeUTF8PlainText,  VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT);
+            VBOXCL_ADD_FORMAT_IF_PRESENT(kUTTypeBMP,            VBOX_SHARED_CLIPBOARD_FMT_BITMAP     );
+            VBOXCL_ADD_FORMAT_IF_PRESENT(kUTTypeHTML,           VBOX_SHARED_CLIPBOARD_FMT_HTML       );
+
+#ifdef CLIPBOARD_DUMP_CONTENT_FORMATS
+            CFArrayRef  flavorTypeArray;
+            CFIndex     flavorCount;
+            CFStringRef flavorType;
+
+            rc = PasteboardCopyItemFlavors(pPasteboard, iItemID, &flavorTypeArray);
+            if (rc == noErr)
+            {
+                VBoxClientVerbose(3, "SCAN..\n");
+                flavorCount = CFArrayGetCount(flavorTypeArray);
+                VBoxClientVerbose(3, "SCAN (%d)..\n", (int)flavorCount);
+                for(CFIndex flavorIndex = 0; flavorIndex < flavorCount; flavorIndex++)
+                {
+                    VBoxClientVerbose(3, "SCAN #%d..\n", (int)flavorIndex);
+                    flavorType = (CFStringRef)CFArrayGetValueAtIndex(flavorTypeArray, flavorIndex);
+
+                    CFDataRef flavorData1;
+                    rc = PasteboardCopyItemFlavorData(pPasteboard, iItemID, flavorType, &flavorData1);
+                    if (rc == noErr)
+                    {
+                        VBoxClientVerbose(3, "Found: %s, size: %d\n", (char *)CFStringGetCStringPtr(flavorType, kCFStringEncodingMacRoman), (int)CFDataGetLength(flavorData1));
+                        CFRelease(flavorData1);
+                    }
+                }
+                VBoxClientVerbose(3, "SCAN COMPLETE\n");
+                CFRelease(flavorTypeArray);
+            }
+#endif /* CLIPBOARD_DUMP_CONTENT_FORMATS */
+        }
+    }
+
+#undef VBOXCL_ADD_FORMAT_IF_PRESENT
+
+    return fFormats;
+}
+
+
+/**
+ * Search for content of specified type in guest clipboard buffer and put
+ * it into newly allocated buffer.
+ *
+ * @param   pPasteboard     Guest PasteBoard reference.
+ * @param   fFormat         Data formats we are looking for.
+ * @param   ppvData         Where to return pointer to the received data. M
+ * @param   pcbData         Where to return the size of the data.
+ * @param   pcbAlloc        Where to return the size of the memory block
+ *                          *ppvData pointes to. (Usually greater than *cbData
+ *                           because the allocation is page aligned.)
+ * @returns IPRT status code.
+ */
+static int vbclClipboardReadGuestData(PasteboardRef pPasteboard, CFStringRef sFormat, void **ppvData, uint32_t *pcbData,
+                                      uint32_t *pcbAlloc)
+{
+    ItemCount cItems, iItem;
+    OSStatus  rc;
+
+    void     *pvData  = NULL;
+    uint32_t  cbData  = 0;
+    uint32_t  cbAlloc = 0;
+
+    AssertPtrReturn(ppvData, VERR_INVALID_POINTER);
+    AssertPtrReturn(pcbData, VERR_INVALID_POINTER);
+    AssertPtrReturn(pcbAlloc, VERR_INVALID_POINTER);
+
+    rc = PasteboardGetItemCount(pPasteboard, &cItems);
+    AssertReturn(rc == noErr, VERR_INVALID_PARAMETER);
+    AssertReturn(cItems > 0, VERR_INVALID_PARAMETER);
+
+    /* Walk through all the items in PasteBoard in order to find
+       that one that correcponds to requested data format. */
+    for (iItem = 1; iItem <= cItems; iItem++)
+    {
+        PasteboardItemID iItemID;
+        CFDataRef        flavorData;
+
+        /* Now, get the item's flavors that corresponds to requested type. */
+        rc = PasteboardGetItemIdentifier(pPasteboard, iItem, &iItemID);
+        AssertReturn(rc == noErr, VERR_INVALID_PARAMETER);
+        rc = PasteboardCopyItemFlavorData(pPasteboard, iItemID, sFormat, &flavorData);
+        if (rc == noErr)
+        {
+            void *flavorDataPtr = (void *)CFDataGetBytePtr(flavorData);
+            cbData = CFDataGetLength(flavorData);
+            if (flavorDataPtr && cbData > 0)
+            {
+                cbAlloc = RT_ALIGN_32(cbData, PAGE_SIZE);
+                pvData = RTMemPageAllocZ(cbAlloc);
+                if (pvData)
+                    memcpy(pvData, flavorDataPtr, cbData);
+            }
+
+            CFRelease(flavorData);
+
+            /* Found first matching item, no more search. */
+            break;
+        }
+
+    }
+
+    /* Found match */
+    if (pvData)
+    {
+        *ppvData  = pvData;
+        *pcbData  = cbData;
+        *pcbAlloc = cbAlloc;
+
+        return VINF_SUCCESS;
+    }
+
+    return VERR_INVALID_PARAMETER;
+}
+
+
+/**
+ * Release resources occupied by vbclClipboardReadGuestData().
+ */
+static void vbclClipboardReleaseGuestData(void **ppvData, uint32_t cbAlloc)
+{
+    AssertReturnVoid(ppvData);
+    RTMemPageFree(*ppvData, cbAlloc);
+    *ppvData = NULL;
+}
+
+/**
+ * Pass data to host.
+ */
+static int vbclClipboardHostPasteData(uint32_t u32ClientId, uint32_t u32Format, const void *pvData, uint32_t cbData)
+{
+    /* Allow empty buffers */
+    if (cbData == 0)
+        return VbglR3ClipboardWriteData(u32ClientId, u32Format, NULL, 0);
+
+    AssertReturn(pvData, VERR_INVALID_PARAMETER);
+    return VbglR3ClipboardWriteData(u32ClientId, u32Format, (void *)pvData, cbData); /** @todo r=bird: Why on earth does a write function like VbglR3ClipboardWriteData take a non-const parameter? */
+}
+
+/**
+ * Paste text data into host clipboard.
+ *
+ * @param   u32ClientId     Host clipboard connection.
+ * @param   pwszData        UTF-16 encoded string.
+ * @param   cbData          The length of the string, in bytes, probably
+ *                          including a terminating zero.
+ */
+static int vbclClipboardHostPasteText(uint32_t u32ClientId, PRTUTF16 pwszData, uint32_t cbData)
+{
+    AssertReturn(cbData > 0,  VERR_INVALID_PARAMETER);
+    AssertPtrReturn(pwszData, VERR_INVALID_POINTER);
+
+    size_t cwcActual; /* (includes a schwarzenegger character) */
+    int rc = vboxClipboardUtf16GetWinSize(pwszData, cbData / sizeof(RTUTF16), &cwcActual);
+    AssertReturn(RT_SUCCESS(rc), rc);
+
+    PRTUTF16 pwszWinTmp = (PRTUTF16)RTMemAlloc(cwcActual * sizeof(RTUTF16));
+    AssertReturn(pwszWinTmp, VERR_NO_MEMORY);
+
+    rc = vboxClipboardUtf16LinToWin(pwszData, cbData / sizeof(RTUTF16), pwszWinTmp, cwcActual);
+    if (RT_SUCCESS(rc))
+        rc = vbclClipboardHostPasteData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT,
+                                        pwszWinTmp, cwcActual * sizeof(RTUTF16));
+
+    RTMemFree(pwszWinTmp);
+
+    return rc;
+}
+
+
+/**
+ * Paste a bitmap onto the host clipboard.
+ *
+ * @param   u32ClientId     Host clipboard connection.
+ * @param   pvData          The bitmap data.
+ * @param   cbData          The size of the bitmap.
+ */
+static int vbclClipboardHostPasteBitmap(uint32_t u32ClientId, void *pvData, uint32_t cbData)
+{
+    const void   *pvDib;
+    size_t        cbDib;
+    int rc = vboxClipboardBmpGetDib(pvData, cbData, &pvDib, &cbDib);
+    AssertRCReturn(rc, rc);
+
+    rc = vbclClipboardHostPasteData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_BITMAP, pvDib, cbDib);
+
+    return rc;
+}
+
+
+/**
+ * Read guest's clipboard buffer and forward its content to host.
+ *
+ * @param   u32ClientId    Host clipboard connection.
+ * @param   pPasteboard    Guest PasteBoard reference.
+ * @param   fFormats       List of data formats (bit field) received from host.
+ *
+ * @returns IPRT status code.
+ */
+int vbclClipboardForwardToHost(uint32_t u32ClientId, PasteboardRef pPasteboard, uint32_t fFormats)
+{
+    int       rc = VINF_SUCCESS;
+
+    void     *pvData  = NULL;
+    uint32_t  cbData  = 0;
+    uint32_t  cbAlloc = 0;
+
+    VBoxClientVerbose(3, "vbclClipboardForwardToHost: %d\n", fFormats);
+
+    /* Walk across all item(s) formats */
+    uint32_t  fFormatsLeft = fFormats;
+    while (fFormatsLeft)
+    {
+        if (fFormatsLeft & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
+        {
+            VBoxClientVerbose(3, "requested VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT: %d\n", fFormats);
+
+            RTUTF16 *pUtf16Str = NULL;
+
+            /* First, try to get UTF16 encoded buffer */
+            rc = vbclClipboardReadGuestData(pPasteboard, kUTTypeUTF16PlainText, &pvData, &cbData, &cbAlloc);
+            if (RT_SUCCESS(rc))
+            {
+                rc = RTUtf16DupEx(&pUtf16Str, (PRTUTF16)pvData, 0);
+                if (RT_FAILURE(rc))
+                    pUtf16Str = NULL;
+            }
+            else /* Failed to get UTF16 buffer */
+            {
+                /* Then, try to get UTF8 encoded buffer */
+                rc = vbclClipboardReadGuestData(pPasteboard, kUTTypeUTF8PlainText, &pvData, &cbData, &cbAlloc);
+                if (RT_SUCCESS(rc))
+                {
+                    rc = RTStrToUtf16((const char *)pvData, &pUtf16Str);
+                    if (RT_FAILURE(rc))
+                        pUtf16Str = NULL;
+                }
+            }
+
+            /* Finally, we got UTF16 encoded buffer */
+            if (RT_SUCCESS(rc))
+            {
+                rc = vbclClipboardHostPasteText(u32ClientId, (PRTUTF16)pvData, cbData);
+
+                if (pUtf16Str)
+                {
+                    RTUtf16Free(pUtf16Str);
+                    pUtf16Str = NULL;
+                }
+
+                vbclClipboardReleaseGuestData(&pvData, cbAlloc);
+            }
+            else
+            {
+                /* No data found or error occurred: send empty buffer */
+                rc = vbclClipboardHostPasteData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT, NULL, 0);
+            }
+
+            fFormatsLeft &= ~(uint32_t)VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT;
+        }
+
+        else if (fFormatsLeft & VBOX_SHARED_CLIPBOARD_FMT_BITMAP)
+        {
+            VBoxClientVerbose(3, "requested VBOX_SHARED_CLIPBOARD_FMT_BITMAP: %d\n", fFormats);
+
+            rc = vbclClipboardReadGuestData(pPasteboard, kUTTypeBMP, &pvData, &cbData, &cbAlloc);
+            if (RT_SUCCESS(rc))
+            {
+                rc = vbclClipboardHostPasteBitmap(u32ClientId, pvData, cbData);
+                vbclClipboardReleaseGuestData(&pvData, cbAlloc);
+            }
+            else
+            {
+                /* No data found or error occurred: send empty buffer */
+                rc = vbclClipboardHostPasteData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_BITMAP, NULL, 0);
+            }
+
+            fFormatsLeft &= ~(uint32_t)VBOX_SHARED_CLIPBOARD_FMT_BITMAP;
+        }
+
+        else if (fFormatsLeft & VBOX_SHARED_CLIPBOARD_FMT_HTML)
+        {
+            VBoxClientVerbose(3, "requested VBOX_SHARED_CLIPBOARD_FMT_HTML: %d\n", fFormats);
+
+            rc = vbclClipboardReadGuestData(pPasteboard, kUTTypeHTML, &pvData, &cbData, &cbAlloc);
+            if (RT_SUCCESS(rc))
+            {
+                rc = vbclClipboardHostPasteData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_HTML, pvData, cbData);
+                vbclClipboardReleaseGuestData(&pvData, cbAlloc);
+            }
+            else
+            {
+                /* No data found or error occurred: send empty buffer */
+                rc = vbclClipboardHostPasteData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_HTML, NULL, 0);
+            }
+
+            fFormatsLeft &= ~(uint32_t)VBOX_SHARED_CLIPBOARD_FMT_HTML;
+        }
+
+        else
+        {
+            VBoxClientVerbose(3, "requested data in unsupported format: %#x\n", fFormatsLeft);
+            break;
+        }
+    }
+
+    return rc; /** @todo r=bird: If there are multiple formats available, which rc is returned here? Does it matter? */
+}
Index: trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientClipboardHostToGuest.cpp
===================================================================
--- trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientClipboardHostToGuest.cpp	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientClipboardHostToGuest.cpp	(revision 48251)
@@ -0,0 +1,304 @@
+/** $Id$ */
+/** @file
+ * VBoxClient - Shared Clipboard Host -> Guest copying, Darwin.
+ */
+
+/*
+ * Copyright (C) 2007-2013 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ */
+
+
+/*******************************************************************************
+*   Header Files                                                               *
+*******************************************************************************/
+#include <Carbon/Carbon.h>
+#include <signal.h>
+#include <stdlib.h>
+
+#include <iprt/thread.h>
+#include <iprt/mem.h>
+#include <iprt/stream.h>
+#include <iprt/initterm.h>
+#include <iprt/message.h>
+#include <VBox/VBoxGuestLib.h>
+#include <VBox/HostServices/VBoxClipboardSvc.h>
+#include <VBox/GuestHost/clipboard-helper.h>
+#include "VBoxClientInternal.h"
+
+/**
+ * Allocate memory for host buffer and receive it.
+ *
+ * @param   u32ClientId    Host connection.
+ * @param   fFormat        Buffer data format.
+ * @param   pData          Where to store received data.
+ * @param   cbDataSize     The size of the received data.
+ * @param   cbMemSize      The actual size of memory occupied by *pData.
+ *
+ * @returns IPRT status code.
+ */
+static int vbclClipboardReadHostData(uint32_t u32ClientId, uint32_t fFormat, void **pData, uint32_t *cbDataSize, uint32_t *cbMemSize)
+{
+    int rc;
+
+    AssertReturn(pData && cbDataSize && cbMemSize, VERR_INVALID_PARAMETER);
+
+    uint32_t  cbDataSizeInternal = _4K;
+    uint32_t  cbMemSizeInternal  = cbDataSizeInternal;
+    void     *pDataInternal      = RTMemPageAllocZ(cbDataSizeInternal);
+
+    if (!pDataInternal)
+        return VERR_NO_MEMORY;
+
+    rc = VbglR3ClipboardReadData(u32ClientId, fFormat, pDataInternal, cbMemSizeInternal, &cbDataSizeInternal);
+    if (rc == VINF_BUFFER_OVERFLOW)
+    {
+        /* Reallocate bigger buffer and receive all the data */
+        RTMemPageFree(pDataInternal, cbMemSizeInternal);
+        cbDataSizeInternal = cbMemSizeInternal = RT_ALIGN_32(cbDataSizeInternal, PAGE_SIZE);
+        pDataInternal = RTMemPageAllocZ(cbMemSizeInternal);
+        if (!pDataInternal)
+            return VERR_NO_MEMORY;
+
+        rc = VbglR3ClipboardReadData(u32ClientId, fFormat, pDataInternal, cbMemSizeInternal, &cbDataSizeInternal);
+    }
+
+    /* Error occurred of zero-sized buffer */
+    if (RT_FAILURE(rc))
+    {
+        RTMemPageFree(pDataInternal, cbMemSizeInternal);
+        return VERR_NO_MEMORY;
+    }
+
+    *pData      = pDataInternal;
+    *cbDataSize = cbDataSizeInternal;
+    *cbMemSize  = cbMemSizeInternal;
+
+    return rc;
+}
+
+/**
+ * Release memory occupied by host buffer.
+ *
+ * @param   pData          Pointer to memory occupied by host buffer.
+ * @param   cbMemSize      The actual size of memory occupied by *pData.
+ */
+static void vbclClipboardReleaseHostData(void **pData, uint32_t cbMemSize)
+{
+    AssertReturnVoid(pData && cbMemSize > 0);
+    RTMemPageFree(*pData, cbMemSize);
+}
+
+/**
+ * Paste buffer into guest clipboard.
+ *
+ * @param   pPasteboard    Guest PasteBoard reference.
+ * @param   pData          Data to be pasted.
+ * @param   cbDataSize     The size of *pData.
+ * @param   fFormat        Buffer data format.
+ * @param   fClear         Whether or not clear guest clipboard before insert data.
+ *
+ * @returns IPRT status code.
+ */
+static int vbclClipboardGuestPasteData(PasteboardRef pPasteboard, UInt8 *pData, CFIndex cbDataSize, CFStringRef sFormat, bool fClear)
+{
+    PasteboardItemID    itemId   = (PasteboardItemID)1;
+    CFDataRef           textData = NULL;
+    OSStatus            rc;
+
+    /* Ignoring sunchronization flags here */
+    PasteboardSynchronize(pPasteboard);
+
+    if (fClear)
+    {
+        rc = PasteboardClear(pPasteboard);
+        AssertReturn(rc == noErr, VERR_NOT_SUPPORTED);
+    }
+
+    /* Create a CData object which we could pass to the pasteboard */
+    if ((textData = CFDataCreate(kCFAllocatorDefault, pData, cbDataSize)))
+    {
+        /* Put the Utf-8 version to the pasteboard */
+        rc = PasteboardPutItemFlavor(pPasteboard, itemId, sFormat, textData, 0);
+        CFRelease(textData);
+        if (rc != noErr)
+        {
+            VBoxClientVerbose(3, "unable to put data into guest's clipboard: %d\n", rc);
+            return VERR_GENERAL_FAILURE;
+        }
+    }
+    else
+        return VERR_NO_MEMORY;
+
+    /* Synchronize updated content */
+    PasteboardSynchronize(pPasteboard);
+
+    return VINF_SUCCESS;
+}
+
+/**
+ * Paste text data into guest clipboard.
+ *
+ * @param   pPasteboard    Guest PasteBoard reference.
+ * @param   pData          Data to be pasted.
+ * @param   cbDataSize     Size of *pData.
+ */
+static int vbclClipboardGuestPasteText(PasteboardRef pPasteboard, void *pData, uint32_t cbDataSize)
+{
+    size_t   cbActualLen;
+    int      rc;
+    char    *pszUtf8Buf;
+    RTUTF16 *pDataInternal;
+
+    AssertReturn(pData, VERR_INVALID_PARAMETER);
+
+    /* Skip zero-sized buffer */
+    AssertReturn(cbDataSize > 0, VINF_SUCCESS);
+
+    /* If buffer content is Unicode text, then deliver
+       it in both formats UTF16 (original) and UTF8. */
+
+    /* Convert END-OF-LINE */
+    rc = vboxClipboardUtf16GetLinSize((RTUTF16 *)pData, cbDataSize / 2, &cbActualLen);
+    AssertReturn(RT_SUCCESS(rc), rc);
+    pDataInternal = (RTUTF16 *)RTMemAlloc(cbActualLen * 2);
+    AssertReturn(pDataInternal, VERR_NO_MEMORY);
+    rc = vboxClipboardUtf16WinToLin((RTUTF16 *)pData, cbDataSize / 2, pDataInternal, cbActualLen);
+
+    /* Do actual paste */
+    if (RT_SUCCESS(rc))
+    {
+        /* Paste UTF16 */
+        rc = vbclClipboardGuestPasteData(pPasteboard, (UInt8 *)pDataInternal, cbActualLen * 2, kUTTypeUTF16PlainText, true);
+        if (RT_SUCCESS(rc))
+        {
+            /* Paste UTF8 */
+            rc = RTUtf16ToUtf8((RTUTF16 *)pDataInternal, &pszUtf8Buf);
+            if (RT_SUCCESS(rc))
+            {
+                rc = vbclClipboardGuestPasteData(pPasteboard, (UInt8 *)pszUtf8Buf, strlen(pszUtf8Buf), kUTTypeUTF8PlainText, false);
+                RTStrFree(pszUtf8Buf);
+            }
+        }
+
+    }
+
+    RTMemFree(pDataInternal);
+
+    return rc;
+}
+
+/**
+ * Paste picture data into guest clipboard.
+ *
+ * @param   pPasteboard    Guest PasteBoard reference.
+ * @param   pData          Data to be pasted.
+ * @param   cbDataSize     The size of *pData.
+ *
+ * @returns IPRT status code.
+ */
+static int vbclClipboardGuestPastePicture(PasteboardRef pPasteboard, void *pData, uint32_t cbDataSize)
+{
+    int     rc;
+    void   *pBmp;
+    size_t  cbBmpSize;
+
+    AssertReturn(pData, VERR_INVALID_PARAMETER);
+    /* Skip zero-sized buffer */
+    AssertReturn(cbDataSize > 0, VINF_SUCCESS);
+
+    rc = vboxClipboardDibToBmp(pData, cbDataSize, &pBmp, &cbBmpSize);
+    AssertReturn(RT_SUCCESS(rc), rc);
+
+    rc = vbclClipboardGuestPasteData(pPasteboard, (UInt8 *)pBmp, cbBmpSize, kUTTypeBMP, true);
+    RTMemFree(pBmp);
+
+    return rc;
+}
+
+/**
+ * Read host's clipboard buffer and put its content to guest clipboard.
+ *
+ * @param   u32ClientId    Host connection.
+ * @param   pPasteboard    Guest PasteBoard reference.
+ * @param   fFormats       List of data formats (bit field) received from host.
+ *
+ * @returns IPRT status code.
+ */
+int vbclClipboardForwardToGuest(uint32_t u32ClientId, PasteboardRef pPasteboard, uint32_t fFormats)
+{
+    int       rc = VERR_INVALID_PARAMETER;
+    void     *pData;
+    uint32_t  cbDataSize, cbMemSize;
+    uint32_t  fFormatsInternal = fFormats;
+
+    /* Walk across all item(s) formats */
+    while (fFormatsInternal)
+    {
+        if (fFormatsInternal & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
+        {
+            VBoxClientVerbose(3, "found VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT: %d\n", fFormatsInternal);
+
+            rc = vbclClipboardReadHostData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT, &pData, &cbDataSize, &cbMemSize);
+            if (RT_SUCCESS(rc))
+            {
+                /* Store data in guest buffer */
+                rc = vbclClipboardGuestPasteText(pPasteboard, pData, cbDataSize);
+
+                /* Release occupied resources */
+                vbclClipboardReleaseHostData(&pData, cbMemSize);
+            }
+
+            fFormatsInternal &= ~((uint32_t)VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT);
+        }
+
+        else if (fFormatsInternal & VBOX_SHARED_CLIPBOARD_FMT_BITMAP)
+        {
+            VBoxClientVerbose(3, "found VBOX_SHARED_CLIPBOARD_FMT_BITMAP: %d\n", fFormatsInternal);
+
+            rc = vbclClipboardReadHostData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_BITMAP, &pData, &cbDataSize, &cbMemSize);
+            if (RT_SUCCESS(rc))
+            {
+                /* Store data in guest buffer */
+                rc = vbclClipboardGuestPastePicture(pPasteboard, pData, cbDataSize);
+
+                /* Release occupied resources */
+                vbclClipboardReleaseHostData(&pData, cbMemSize);
+            }
+
+            fFormatsInternal &= ~((uint32_t)VBOX_SHARED_CLIPBOARD_FMT_BITMAP);
+        }
+
+        else if (fFormatsInternal & VBOX_SHARED_CLIPBOARD_FMT_HTML)
+        {
+            VBoxClientVerbose(3, "found VBOX_SHARED_CLIPBOARD_FMT_HTML: %d\n", fFormatsInternal);
+
+            rc = vbclClipboardReadHostData(u32ClientId, VBOX_SHARED_CLIPBOARD_FMT_HTML, &pData, &cbDataSize, &cbMemSize);
+            if (RT_SUCCESS(rc))
+            {
+                /* Store data in guest buffer */
+                rc = vbclClipboardGuestPasteData(pPasteboard, (UInt8 *)pData, cbDataSize, kUTTypeHTML, true);
+
+                /* Release occupied resources */
+                vbclClipboardReleaseHostData(&pData, cbMemSize);
+            }
+
+            fFormatsInternal &= ~((uint32_t)VBOX_SHARED_CLIPBOARD_FMT_HTML);
+        }
+
+        else
+        {
+            VBoxClientVerbose(3, "received data in unsupported format: %d\n", fFormats);
+            break;
+        }
+    }
+
+    return rc;
+}
Index: trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientInternal.h
===================================================================
--- trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientInternal.h	(revision 48251)
+++ trunk/src/VBox/Additions/darwin/VBoxClient/VBoxClientInternal.h	(revision 48251)
@@ -0,0 +1,99 @@
+/** $Id$ */
+/** @file
+ * VBoxClient - common definitions, Darwin.
+ */
+
+/*
+ * Copyright (C) 2007-2013 Oracle Corporation
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ */
+
+#ifndef ___VBoxClientInternal_h
+#define ___VBoxClientInternal_h
+
+#include <VBox/VBoxGuestLib.h>
+#include <Carbon/Carbon.h>
+
+/* Service description */
+typedef struct
+{
+    /** The service name. */
+    const char *pszName;
+
+    /**
+     * Start service.
+     * @returns VBox status code.
+     */
+    DECLCALLBACKMEMBER(int, pfnStart)(void);
+
+    /**
+     * Stop service.
+     * @returns VBox status code.
+     */
+    DECLCALLBACKMEMBER(int, pfnStop)(void);
+
+} VBOXCLIENTSERVICE;
+
+
+/*
+ * Services
+ */
+
+RT_C_DECLS_BEGIN
+
+extern VBOXCLIENTSERVICE g_ClipboardService;
+
+RT_C_DECLS_END
+
+
+/*
+ * Functions
+ */
+
+/**
+ * Displays a verbose message.
+ *
+ * @param   iLevel      Minimum log level required to display this message.
+ * @param   pszFormat   The message text.
+ * @param   ...         Format arguments.
+ */
+extern void VBoxClientVerbose(int iLevel, const char *pszFormat, ...);
+
+/**
+ * Walk through pasteboard items and report currently available item types.
+ *
+ * @param   pPasteboard       Reference to guest Pasteboard.
+ # @returns Available formats bit field.
+ */
+extern uint32_t vbclClipboardGetAvailableFormats(PasteboardRef pPasteboard);
+
+/**
+ * Read host's clipboard buffer and put its content to guest clipboard.
+ *
+ * @param   u32ClientId    Host connection.
+ * @param   pPasteboard    Guest PasteBoard reference.
+ * @param   fFormats       List of data formats (bit field) received from host.
+ *
+ * @returns IPRT status code.
+ */
+extern int vbclClipboardForwardToGuest(uint32_t u32ClientId, PasteboardRef pPasteboard, uint32_t fFormats);
+
+/**
+ * Read guest's clipboard buffer and forward its content to host.
+ *
+ * @param   u32ClientId    Host clipboard connection.
+ * @param   pPasteboard    Guest PasteBoard reference.
+ * @param   fFormats       List of data formats (bit field) received from host.
+ *
+ * @returns IPRT status code.
+ */
+extern int vbclClipboardForwardToHost(uint32_t u32ClientId, PasteboardRef pPasteboard, uint32_t fFormats);
+
+#endif /* ___VBoxClientInternal_h */
