#include "DABase.h"
#include "DAInternal.h"
#include <fcntl.h>
#include <paths.h>
#include <pwd.h>
#include <sysexits.h>
#include <unistd.h>
#include <vproc.h>
#include <sys/attr.h>
#include <sys/stat.h>
#include <CommonCrypto/CommonDigest.h>
#include <CoreFoundation/CFBundlePriv.h>
#include <IOKit/hidsystem/IOHIDLib.h>
static vproc_transaction_t __vproc_transaction = NULL;
static size_t __vproc_transaction_count = 0;
__private_extern__ int ___chattr( const char * path, ___attr_t attr, ___attr_t noattr )
{
struct __chattrbuf
{
uint32_t size;
uint8_t reserved0032[8];
___attr_t attr;
uint8_t reserved0112[22];
};
typedef struct __chattrbuf __chattrbuf;
struct attrlist attributes;
__chattrbuf buffer;
int status;
attributes.bitmapcount = ATTR_BIT_MAP_COUNT;
attributes.commonattr = ATTR_CMN_FNDRINFO;
attributes.dirattr = 0;
attributes.fileattr = 0;
attributes.forkattr = 0;
attributes.volattr = 0;
status = getattrlist( path, &attributes, &buffer, sizeof( buffer ), 0 );
if ( status == 0 )
{
buffer.attr = ( buffer.attr & ~noattr ) | attr;
status = setattrlist( path, &attributes, ( ( uint8_t * ) &buffer ) + sizeof( buffer.size ), sizeof( buffer ) - sizeof( buffer.size ), 0 );
}
return status;
}
__private_extern__ int ___initgroups( uid_t uid, gid_t basegid )
{
struct passwd * user;
user = getpwuid( uid );
if ( user )
{
return initgroups( user->pw_name, basegid );
}
return -1;
}
__private_extern__ int ___isautofs( const char * path )
{
struct statfs * mountList;
int mountListCount;
int mountListIndex;
mountListCount = getmntinfo( &mountList, MNT_NOWAIT );
for ( mountListIndex = 0; mountListIndex < mountListCount; mountListIndex++ )
{
if ( strcmp( mountList[mountListIndex].f_fstypename, "autofs" ) == 0 )
{
if ( strncmp( mountList[mountListIndex].f_mntonname, path, strlen( mountList[mountListIndex].f_mntonname ) ) == 0 )
{
return 1;
}
}
}
return 0;
}
__private_extern__ int ___mkdir( const char * path, mode_t mode )
{
int status;
status = -1;
if ( path )
{
char * template;
asprintf( &template, "%s.XXXXXX", path );
if ( template )
{
status = mkdtemp( template ) ? 0 : -1;
if ( status == 0 )
{
status = chmod( template, mode );
if ( status == 0 )
{
status = rename( template, path );
}
if ( status )
{
rmdir( template );
}
}
free( template );
}
}
return status;
}
__private_extern__ void ___vproc_transaction_begin( void )
{
if ( __vproc_transaction_count == 0 )
{
__vproc_transaction = vproc_transaction_begin( NULL );
}
__vproc_transaction_count++;
}
__private_extern__ void ___vproc_transaction_end( void )
{
__vproc_transaction_count--;
if ( __vproc_transaction_count == 0 )
{
vproc_transaction_end( NULL, __vproc_transaction );
__vproc_transaction = NULL;
}
}
__private_extern__ void ___CFArrayIntersect( CFMutableArrayRef array1, CFArrayRef array2 )
{
CFIndex count;
CFIndex index;
count = CFArrayGetCount( array1 );
for ( index = count - 1; index > -1; index-- )
{
const void * value;
value = CFArrayGetValueAtIndex( array1, index );
if ( ___CFArrayContainsValue( array2, value ) == FALSE )
{
CFArrayRemoveValueAtIndex( array1, index );
}
}
}
__private_extern__ CFStringRef ___CFBundleCopyLocalizedStringInDirectory( CFURLRef bundleURL, CFStringRef key, CFStringRef value, CFStringRef table )
{
CFBundleRef bundle;
CFStringRef string = NULL;
bundle = CFBundleCreate( kCFAllocatorDefault, bundleURL );
if ( bundle )
{
_CFBundleSetStringsFilesShared( bundle, FALSE );
string = CFBundleCopyLocalizedString( bundle, key, value, table );
CFRelease( bundle );
}
return string;
}
__private_extern__ CFURLRef ___CFBundleCopyResourceURLInDirectory( CFURLRef bundleURL, CFStringRef resourcePath )
{
CFURLRef resourceURL = NULL;
CFURLRef resourcePathURL = NULL;
resourcePathURL = CFURLCreateWithFileSystemPathRelativeToBase( kCFAllocatorDefault, resourcePath, kCFURLPOSIXPathStyle, FALSE, NULL );
if ( resourcePathURL )
{
CFStringRef resourceName;
resourceName = CFURLCopyLastPathComponent( resourcePathURL );
if ( resourceName )
{
CFURLRef resourceSubDirNameURL;
resourceSubDirNameURL = CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault, resourcePathURL );
if ( resourceSubDirNameURL )
{
CFStringRef resourceSubDirName;
resourceSubDirName = CFURLCopyFileSystemPath( resourceSubDirNameURL, kCFURLPOSIXPathStyle );
if ( resourceSubDirName )
{
resourceURL = CFBundleCopyResourceURLInDirectory( bundleURL, resourceName, NULL, resourceSubDirName );
CFRelease( resourceSubDirName );
}
CFRelease( resourceSubDirNameURL );
}
CFRelease( resourceName );
}
CFRelease( resourcePathURL );
}
return resourceURL;
}
__private_extern__ CFDataRef ___CFDataCreateFromString( CFAllocatorRef allocator, CFStringRef string )
{
CFMutableDataRef data;
data = CFDataCreateMutable( allocator, 0 );
if ( data )
{
CFIndex index;
CFIndex length;
length = CFStringGetLength( string );
for ( index = 0; index + 1 < length; index += 2 )
{
UInt8 byte;
UniChar character1;
UniChar character2;
character1 = CFStringGetCharacterAtIndex( string, index );
character2 = CFStringGetCharacterAtIndex( string, index + 1 );
if ( isxdigit( character1 ) == 0 ) break;
if ( isxdigit( character2 ) == 0 ) break;
character1 = tolower( character1 ) - '0';
character2 = tolower( character2 ) - '0';
byte = ( ( ( character1 > 9 ) ? ( character1 + '0' - 'a' + 10 ) : character1 ) << 4 ) |
( ( ( character2 > 9 ) ? ( character2 + '0' - 'a' + 10 ) : character2 ) );
CFDataAppendBytes( data, &byte, 1 );
}
for ( ; index < length; index++ )
{
UniChar character;
character = CFStringGetCharacterAtIndex( string, index );
if ( isspace( character ) == 0 ) break;
}
if ( index < length )
{
CFDataSetLength( data, 0 );
}
if ( CFDataGetLength( data ) == 0 )
{
CFRelease( data );
data = NULL;
}
}
return data;
}
__private_extern__ CFDictionaryRef ___CFDictionaryCreateFromXMLString( CFAllocatorRef allocator, CFStringRef string )
{
CFDataRef data;
CFDictionaryRef dictionary = NULL;
data = CFStringCreateExternalRepresentation( kCFAllocatorDefault, string, kCFStringEncodingUTF8, 0 );
if ( data )
{
CFTypeRef object;
object = CFPropertyListCreateFromXMLData( kCFAllocatorDefault, data, kCFPropertyListImmutable, NULL );
if ( object )
{
if ( CFGetTypeID( object ) == CFDictionaryGetTypeID( ) )
{
dictionary = CFRetain( object );
}
CFRelease( object );
}
CFRelease( data );
}
return dictionary;
}
__private_extern__ CFTypeRef ___CFDictionaryGetAnyValue( CFDictionaryRef dictionary )
{
CFIndex count;
CFTypeRef value = NULL;
count = CFDictionaryGetCount( dictionary );
if ( count )
{
CFTypeRef * values;
values = malloc( count * sizeof( CFDictionaryRef ) );
if ( values )
{
CFDictionaryGetKeysAndValues( dictionary, NULL, values );
value = values[0];
free( values );
}
}
return value;
}
__private_extern__ char * ___CFStringCreateCStringWithFormatAndArguments( const char * format, va_list arguments )
{
char * buffer = NULL;
if ( format )
{
CFStringRef formatAsString;
formatAsString = CFStringCreateWithCString( kCFAllocatorDefault, format, kCFStringEncodingUTF8 );
if ( formatAsString )
{
CFStringRef bufferAsString;
bufferAsString = CFStringCreateWithFormatAndArguments( kCFAllocatorDefault, NULL, formatAsString, arguments );
if ( bufferAsString )
{
buffer = ___CFStringCopyCString( bufferAsString );
CFRelease( bufferAsString );
}
CFRelease( formatAsString );
}
}
return buffer;
}
__private_extern__ Boolean ___CFStringGetCString( CFStringRef string, char * buffer, CFIndex length )
{
length--;
CFStringGetBytes( string, CFRangeMake( 0, CFStringGetLength( string ) ), kCFStringEncodingUTF8, 0, FALSE, ( void * ) buffer, length, &length );
buffer[length] = 0;
return length ? TRUE : FALSE;
}
__private_extern__ void ___CFStringInsertFormat( CFMutableStringRef string, CFIndex index, CFStringRef format, ... )
{
va_list arguments;
va_start( arguments, format );
___CFStringInsertFormatAndArguments( string, index, format, arguments );
va_end( arguments );
}
__private_extern__ void ___CFStringInsertFormatAndArguments( CFMutableStringRef string, CFIndex index, CFStringRef format, va_list arguments )
{
CFStringRef insert;
insert = CFStringCreateWithFormatAndArguments( kCFAllocatorDefault, NULL, format, arguments );
if ( insert )
{
CFStringInsert( string, index, insert );
CFRelease( insert );
}
}
__private_extern__ void ___CFStringPad( CFMutableStringRef string, CFStringRef pad, CFIndex length, CFIndex index )
{
if ( length > CFStringGetLength( string ) )
{
CFStringPad( string, pad, length, index );
}
}
__private_extern__ CFUUIDRef ___CFUUIDCreateFromName( CFAllocatorRef allocator, CFUUIDRef space, CFDataRef name )
{
CC_MD5_CTX md5c;
CFUUIDBytes uuid;
assert( sizeof( uuid ) == CC_MD5_DIGEST_LENGTH );
uuid = CFUUIDGetUUIDBytes( space );
CC_MD5_Init( &md5c );
CC_MD5_Update( &md5c, &uuid, sizeof( uuid ) );
CC_MD5_Update( &md5c, CFDataGetBytePtr( name ), CFDataGetLength( name ) );
CC_MD5_Final( ( void * ) &uuid, &md5c );
uuid.byte6 = 0x30 | ( uuid.byte6 & 0x0F );
uuid.byte8 = 0x80 | ( uuid.byte8 & 0x3F );
return CFUUIDCreateFromUUIDBytes( allocator, uuid );
}
__private_extern__ CFUUIDRef ___CFUUIDCreateFromString( CFAllocatorRef allocator, CFStringRef string )
{
UInt32 index;
UInt32 length;
length = CFStringGetLength( string );
for ( index = 0; index < length; index++ )
{
UniChar character;
character = CFStringGetCharacterAtIndex( string, index );
if ( index < 36 )
{
if ( index == 8 || index == 13 || index == 18 || index == 23 )
{
if ( character != '-' ) break;
}
else
{
if ( isxdigit( character ) == 0 ) break;
}
}
else
{
if ( isspace( character ) == 0 ) break;
}
}
return ( index < length ) ? NULL : CFUUIDCreateFromString( allocator, string );
}
__private_extern__ CFStringRef ___CFURLCopyRawDeviceFileSystemPath( CFURLRef url, CFURLPathStyle pathStyle )
{
CFStringRef path;
path = CFURLCopyFileSystemPath( url, pathStyle );
if ( path )
{
CFStringRef node;
node = CFURLCopyLastPathComponent( url );
if ( node )
{
CFMutableStringRef string;
string = CFStringCreateMutableCopy( CFGetAllocator( url ), 0, path );
if ( string )
{
CFIndex index;
index = CFStringGetLength( path ) - CFStringGetLength( node );
CFStringInsert( string, index, CFSTR( "r" ) );
CFRelease( path );
path = string;
}
CFRelease( node );
}
}
return path;
}
__private_extern__ void ___DADisplayUpdateActivity( void )
{
static io_connect_t port = MACH_PORT_NULL;
static struct timeval time = { 0 };
if ( port == MACH_PORT_NULL )
{
io_service_t service;
service = IOServiceGetMatchingService( kIOMasterPortDefault, IOServiceMatching( kIOHIDSystemClass ) );
if ( service )
{
IOServiceOpen( service, mach_task_self( ), kIOHIDParamConnectType, &port );
IOObjectRelease( service );
}
}
if ( port )
{
struct timeval now;
gettimeofday( &now, NULL );
if ( time.tv_sec != now.tv_sec )
{
NXEventData data;
IOGPoint location;
time.tv_sec = now.tv_sec;
IOHIDPostEvent( port, NX_NULLEVENT, location, &data, 0, 0, 0 );
}
}
}
__private_extern__ kern_return_t ___IORegistryEntryGetPath( io_registry_entry_t entry, const io_name_t plane, ___io_path_t path )
{
IOReturn status;
status = IORegistryEntryGetPath( entry, plane, path );
if ( status == kIOReturnBadArgument )
{
io_registry_entry_t parent;
status = IORegistryEntryGetParentEntry( entry, plane, &parent );
if ( status == kIOReturnSuccess )
{
status = ___IORegistryEntryGetPath( parent, plane, path );
if ( status == kIOReturnSuccess )
{
io_name_t name;
status = IORegistryEntryGetNameInPlane( entry, plane, name );
if ( status == kIOReturnSuccess )
{
io_name_t location;
status = IORegistryEntryGetLocationInPlane( entry, plane, location );
if ( status == kIOReturnSuccess )
{
if ( strlen( path ) + strlen( "/" ) + strlen( name ) + strlen( "@" ) + strlen( location ) < sizeof( ___io_path_t ) )
{
strcat( path, "/" );
strcat( path, name );
strcat( path, "@" );
strcat( path, location );
}
else
{
status = kIOReturnBadArgument;
}
}
else
{
if ( strlen( path ) + strlen( "/" ) + strlen( name ) < sizeof( ___io_path_t ) )
{
strcat( path, "/" );
strcat( path, name );
status = kIOReturnSuccess;
}
else
{
status = kIOReturnBadArgument;
}
}
}
}
IOObjectRelease( parent );
}
}
return status;
}