#include "CPolicyBase.h"
#include <PasswordServer/CPolicyBase.h>
#pragma mark -
#pragma mark Public Methods
#pragma mark -
CPolicyBase::CPolicyBase()
{
mPolicyDict = NULL;
}
CPolicyBase::CPolicyBase( CFDictionaryRef inPolicyDict )
{
mPolicyDict = NULL;
}
CPolicyBase::CPolicyBase( const char *xmlDataStr )
{
mPolicyDict = NULL;
}
CPolicyBase::~CPolicyBase()
{
if ( mPolicyDict != NULL )
{
CFRelease( mPolicyDict );
mPolicyDict = NULL;
}
}
char *
CPolicyBase::GetPolicyAsXMLData( void )
{
CFDataRef xmlData = NULL;
const UInt8 *sourcePtr;
char *returnString = NULL;
long length;
if ( mPolicyDict == NULL )
return NULL;
xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, (CFPropertyListRef)mPolicyDict );
if ( xmlData == NULL )
return NULL;
sourcePtr = CFDataGetBytePtr( xmlData );
length = CFDataGetLength( xmlData );
if ( sourcePtr != NULL && length > 0 )
{
returnString = (char *) malloc( length + 1 );
if ( returnString != NULL )
{
memcpy( returnString, sourcePtr, length );
returnString[length] = '\0';
}
}
CFRelease( xmlData );
return returnString;
}
bool pwsf_ConvertCFDateToBSDTime( CFDateRef inDateRef, struct tm *outBSDDate )
{
CFGregorianDate gregorianDate;
CFAbsoluteTime theCFDate = 0;
if ( outBSDDate == NULL || inDateRef == NULL )
return false;
theCFDate = CFDateGetAbsoluteTime( inDateRef );
gregorianDate = CFAbsoluteTimeGetGregorianDate( theCFDate, NULL );
outBSDDate->tm_sec = (int)gregorianDate.second;
outBSDDate->tm_min = gregorianDate.minute;
outBSDDate->tm_hour = gregorianDate.hour;
outBSDDate->tm_mday = gregorianDate.day;
outBSDDate->tm_mon = gregorianDate.month - 1;
outBSDDate->tm_year = gregorianDate.year - 1900;
outBSDDate->tm_wday = 0;
outBSDDate->tm_yday = 0;
outBSDDate->tm_isdst = 0;
outBSDDate->tm_gmtoff = 0;
outBSDDate->tm_zone = NULL;
return true;
}
bool
CPolicyBase::ConvertCFDateToBSDTime( CFDateRef inDateRef, struct tm *outBSDDate )
{
return pwsf_ConvertCFDateToBSDTime( inDateRef, outBSDDate );
}
bool pwsf_ConvertBSDTimeToCFDate( struct tm *inBSDDate, CFDateRef *outDateRef )
{
CFGregorianDate gregorianDate;
if ( inBSDDate == NULL || outDateRef == NULL )
return false;
gregorianDate.second = inBSDDate->tm_sec;
gregorianDate.minute = inBSDDate->tm_min;
gregorianDate.hour = inBSDDate->tm_hour;
gregorianDate.day = inBSDDate->tm_mday;
gregorianDate.month = inBSDDate->tm_mon + 1;
gregorianDate.year = inBSDDate->tm_year + 1900;
*outDateRef = CFDateCreate( kCFAllocatorDefault, CFGregorianDateGetAbsoluteTime(gregorianDate, NULL) );
return true;
}
bool
CPolicyBase::ConvertBSDTimeToCFDate( struct tm *inBSDDate, CFDateRef *outDateRef )
{
return pwsf_ConvertBSDTimeToCFDate( inBSDDate, outDateRef );
}
#pragma mark -
#pragma mark Protected Methods
#pragma mark -
bool
CPolicyBase::GetBooleanForKey( CFStringRef inKey, bool *outValue )
{
CFTypeRef valueRef = NULL;
bool result = false;
int anIntValue = 0;
if ( CFDictionaryGetValueIfPresent( mPolicyDict, inKey, (const void **)&valueRef ) )
{
if ( CFGetTypeID(valueRef) == CFBooleanGetTypeID() )
{
*outValue = CFBooleanGetValue( (CFBooleanRef)valueRef );
result = true;
}
else
if ( CFGetTypeID(valueRef) == CFNumberGetTypeID() )
{
if ( CFNumberGetValue( (CFNumberRef)valueRef, kCFNumberIntType, &anIntValue) )
{
*outValue = (anIntValue != 0);
result = true;
}
}
}
return result;
}