Exemple #1
0
bin2hex_input_t bin2hex_parse_command_line( char const* const* cmdline )
{
    bin2hex_input_t input = {0};
    int arg, asize;

    error_context_push( "parsing command line", "" );
    for( arg = 1, asize = array_size( cmdline ); arg < asize; ++arg )
    {
        if( string_equal( cmdline[arg], "--columns" ) )
        {
            if( arg < ( asize - 1 ) )
                input.columns = string_to_int( cmdline[++arg] );
        }
        else if( string_equal( cmdline[arg], "--" ) )
            break; //Stop parsing cmdline options
        else if( ( string_length( cmdline[arg] ) > 2 ) && string_equal_substr( cmdline[arg], "--", 2 ) )
            continue; //Cmdline argument not parsed here
        else
        {
            array_push( input.input_files, string_clone( cmdline[arg] ) );
            array_push( input.output_files, string_format( "%s.hex", cmdline[arg] ) );
        }
    }
    error_context_pop();

    return input;
}
Exemple #2
0
hashify_input_t hashify_parse_command_line( const char* const* cmdline )
{
	hashify_input_t input = {0};
	int arg, asize;

	input.check_only = false;

	error_context_push( "parsing command line", "" );
	for( arg = 1, asize = array_size( cmdline ); arg < asize; ++arg )
	{
		if( string_equal( cmdline[arg], "--validate" ) )
		{
			input.check_only = true;
			continue;
		}
		else if( string_equal( cmdline[arg], "--generate-string" ) )
		{
			if( arg < asize - 1 )
			{
				++arg;
				array_push( input.strings, string_clone( cmdline[arg] ) );
			}
			continue;
		}
		else if( string_equal( cmdline[arg], "--" ) )
			break; //Stop parsing cmdline options
		else if( ( string_length( cmdline[arg] ) > 2 ) && string_equal_substr( cmdline[arg], "--", 2 ) )
			continue; //Cmdline argument not parsed here			

		array_push( input.files, string_clone( cmdline[arg] ) );
	}
	error_context_pop();

	return input;
}
Exemple #3
0
static NOINLINE const char* _expand_environment( hash_t key, char* var )
{
	if( key == HASH_EXECUTABLE_NAME )
		return environment_executable_name();
	else if( key == HASH_EXECUTABLE_DIRECTORY )
		return environment_executable_directory();
	else if( key == HASH_EXECUTABLE_PATH )
		return environment_executable_path();
	else if( key == HASH_INITIAL_WORKING_DIRECTORY )
		return environment_initial_working_directory();
	else if( key == HASH_CURRENT_WORKING_DIRECTORY )
		return environment_current_working_directory();
	else if( key == HASH_HOME_DIRECTORY )
		return environment_home_directory();
	else if( key == HASH_TEMPORARY_DIRECTORY )
		return environment_temporary_directory();
	else if( string_equal_substr( var, "variable[", 9 ) )  //variable[varname] - Environment variable named "varname"
	{
		const char* value;
		unsigned int end_pos = string_find( var, ']', 9 );
		if( end_pos != STRING_NPOS )
			var[end_pos] = 0;
		value = environment_variable( var );
		if( end_pos != STRING_NPOS )
			var[end_pos] = ']';
		return value;
	}
	return "";
}
Exemple #4
0
stream_t* stream_open( const char* path, unsigned int mode )
{
	unsigned int protocol_end;

	//Check if protocol was given
	protocol_end = string_find_string( path, "://", 0 );
	if( protocol_end != STRING_NPOS )
	{
		//TODO: Proper pluggable protocol handling
#if FOUNDATION_PLATFORM_ANDROID
		if( ( protocol_end == 5 ) && string_equal_substr( path, "asset", 5 ) )
			return asset_stream_open( path, mode );
		else
#endif
		if( ( protocol_end == 4 ) && string_equal_substr( path, "file", 4 ) )
			return fs_open_file( path, mode );
		else if( ( protocol_end == 4 ) && string_equal_substr( path, "stdout", 4 ) )
			return stream_open_stdout();
		else if( ( protocol_end == 4 ) && string_equal_substr( path, "stderr", 4 ) )
			return stream_open_stderr();
		else if( ( protocol_end == 4 ) && string_equal_substr( path, "stdin", 4 ) )
			return stream_open_stdin();
		else if( ( protocol_end != 3 ) || !string_equal_substr( path, "vfs", protocol_end ) )
		{
			log_errorf( 0, ERROR_INVALID_VALUE, "Invalid protocol: %s", path );
			return 0;
		}
	}

	//No protocol, assume virtual file system path
	//TODO: Virtual file system

	return fs_open_file( path, mode );
}
uint64_t system_hostid( void )
{
#if FOUNDATION_PLATFORM_ANDROID
	//Not implemented yet, see https://code.google.com/p/libjingle/source/browse/trunk/talk/base/ifaddrs-android.cc
	return 0;
#else
	struct ifaddrs* ifaddr;
	struct ifaddrs* ifa;
	uint64_t hostid = 0;

#if !FOUNDATION_PLATFORM_APPLE
	struct ifreq buffer;
	int sock = socket( PF_INET, SOCK_DGRAM, 0 );
#endif
	
	if( getifaddrs( &ifaddr ) == 0 )
	{
		for( ifa = ifaddr; ifa && !hostid; ifa = ifa->ifa_next )
		{
			if( string_equal_substr( ifa->ifa_name, "lo", 2 ) )
				continue;
			
#if FOUNDATION_PLATFORM_APPLE
			
			
#else
			memset( &buffer, 0, sizeof( buffer ) );
			string_copy( buffer.ifr_name, ifa->ifa_name, sizeof( buffer.ifr_name ) );

			hostid = _system_hostid_lookup( sock, &buffer );
#endif
		}
		freeifaddrs( ifaddr );
	}
#if !FOUNDATION_PLATFORM_APPLE
	else
	{
		memset( &buffer, 0, sizeof( buffer ) );
		strcpy( buffer.ifr_name, "eth0" );

		hostid = _system_hostid_lookup( sock, &buffer );
	}

	close( sock );
#endif
	
	return hostid;
#endif
}
uint64_t system_hostid( void )
{
	struct ifaddrs* ifaddr;
	struct ifaddrs* ifa;
	uint64_t hostid = 0;

#if !FOUNDATION_PLATFORM_APPLE
	struct ifreq buffer;
	int sock = socket( PF_INET, SOCK_DGRAM, 0 );
#endif
	
	if( getifaddrs( &ifaddr ) == 0 )
	{
		for( ifa = ifaddr; ifa && !hostid; ifa = ifa->ifa_next )
		{
			if( string_equal_substr( ifa->ifa_name, "lo", 2 ) )
				continue;
			
#if FOUNDATION_PLATFORM_APPLE
			
			
#else
			memset( &buffer, 0, sizeof( buffer ) );
			string_copy( buffer.ifr_name, ifa->ifa_name, sizeof( buffer.ifr_name ) );

			hostid = _system_hostid_lookup( sock, &buffer );
#endif
		}
		freeifaddrs( ifaddr );
	}
#if !FOUNDATION_PLATFORM_APPLE
	else
	{
		memset( &buffer, 0, sizeof( buffer ) );
		strcpy( buffer.ifr_name, "eth0" );

		hostid = _system_hostid_lookup( sock, &buffer );
	}

	close( sock );
#endif
	
	return hostid;
}
Exemple #7
0
char* path_subdirectory_name( const char* path, const char* root )
{
	char* subpath;
	char* testpath;
	char* testroot;
	char* pathofpath;
	unsigned int pathprotocol, rootprotocol;
	char* cpath = string_clone( path );
	char* croot = string_clone( root );

	cpath = path_clean( cpath, path_is_absolute( cpath ) );
	croot = path_clean( croot, path_is_absolute( croot ) );

	pathofpath = path_directory_name( cpath );

	testpath = pathofpath;
	pathprotocol = string_find_string( testpath, "://", 0 );
	if( pathprotocol != STRING_NPOS )
		testpath += pathprotocol + 2; // add two to treat as absolute path

	testroot = croot;
	rootprotocol = string_find_string( testroot, "://", 0 );
	if( rootprotocol != STRING_NPOS )
		testroot += rootprotocol + 2;

	if( ( rootprotocol != STRING_NPOS ) && ( ( pathprotocol == STRING_NPOS ) || ( pathprotocol != rootprotocol ) || !string_equal_substr( cpath, croot, rootprotocol ) ) )
		subpath = string_allocate( 0 );
	else if( !string_equal_substr( testpath, testroot, string_length( testroot ) ) )
		subpath = string_allocate( 0 );
	else
	{
		char* filename = path_file_name( cpath );

		subpath = string_substr( testpath, string_length( testroot ), STRING_NPOS );
		subpath = path_clean( path_append( subpath, filename ), false );

		string_deallocate( filename );
	}
	string_deallocate( pathofpath );
	string_deallocate( cpath );
	string_deallocate( croot );

	return subpath;
}
Exemple #8
0
stream_t* asset_stream_open( const char* path, unsigned int mode )
{
	if( !path || !path[0] )
		return 0;

	if( string_equal_substr( path, "asset://", 8 ) )
		path += 8;
	if( *path == '/' )
		++path;

	AAsset* assetobj = AAssetManager_open( android_app()->activity->assetManager, path, AASSET_MODE_RANDOM );
	if( !assetobj )
	{
		//warn_logf( WARNING_SYSTEM_CALL_FAIL, "Unable to open asset: asset://%s", path );
		return 0;
	}

	stream_asset_t* asset = memory_allocate_zero_context( HASH_STREAM, sizeof( stream_asset_t ), 8, MEMORY_PERSISTENT );
	stream_t* stream = (stream_t*)asset;

	_stream_initialize( stream, BUILD_DEFAULT_STREAM_BYTEORDER );

	stream->type = STREAMTYPE_ASSET;
	stream->sequential = 0;
	stream->reliable = 1;
	stream->inorder = 1;
	stream->swap = 0;
	stream->path = string_format( "asset://%s", path );
	stream->mode = ( mode & STREAM_BINARY ) | STREAM_IN;
	stream->vtable = &_asset_stream_vtable;

	asset->asset = assetobj;
	asset->position = 0;

	return stream;
}
Exemple #9
0
DECLARE_TEST( hash, stability )
{
	//TODO: Implement a proper test instead of this crap
	int i, j, k, len;
	hash_t lhash, rhash, rhashref;

	for( i = 0; i < 128; ++i )
	{
		uint32_t lhs[129], rhs[129];
		len = i + 1;

		for( k = 0; k < len; ++k )
			lhs[k] = random32();

		lhash = hash( lhs, len * sizeof( uint32_t ) );
		EXPECT_NE( lhash, 0U );

		for( j = 0; j < 64000; ++j )
		{
			for( k = 0; k < len; ++k )
				rhs[k] = random32();

			rhashref = hash( rhs, len * sizeof( uint32_t ) );
			rhash = hash( rhs, len * sizeof( uint32_t ) );

			EXPECT_EQ( rhashref, rhash );
			if( memcmp( lhs, rhs, len * sizeof( uint32_t ) ) )
				EXPECT_NE( lhash, rhash );
			EXPECT_NE( rhash, 0U );
		}
	}

	for( i = 4; i < 128; ++i )
	{
		char lhs[130], rhs[130];
		len = i + 1;

		lhs[0] = 'f'; lhs[1] = 'n'; lhs[2] = 'd'; lhs[3] = '_';
		rhs[0] = 'f'; rhs[1] = 'n'; rhs[2] = 'd'; rhs[3] = '_';

		for( k = 4; k < len; ++k )
			lhs[k] = random32_range( 32, 128 );
		lhs[len] = 0;

		lhash = hash( lhs, len );
		EXPECT_NE( lhash, 0U );

		for( j = 0; j < 128000; ++j )
		{
			for( k = 4; k < len; ++k )
				rhs[k] = random32_range( 32, 128 );
			rhs[len] = 0;

			rhashref = hash( rhs, len );
			rhash = hash( rhs, len );

			EXPECT_EQ( rhashref, rhash );
			if( !string_equal_substr( lhs, rhs, len ) )
				EXPECT_NE( lhash, rhash );
			EXPECT_NE( rhash, 0U );
		}
	}
	return 0;
}
Exemple #10
0
void config_load( const char* name, hash_t filter_section, bool built_in, bool overwrite )
{
	/*lint --e{838} Safety null assign all pointers for all preprocessor paths */
	/*lint --e{750} Unused macros in some paths */
#define NUM_SEARCH_PATHS 10
#define ANDROID_ASSET_PATH_INDEX 5
	char* sub_exe_path = 0;
	char* exe_parent_path = 0;
	char* exe_processed_path = 0;
	char* abs_exe_parent_path = 0;
	char* abs_exe_processed_path = 0;
	char* bundle_path = 0;
	char* home_dir = 0;
	char* cmdline_path = 0;
	char* cwd_config_path = 0;
	const char* paths[NUM_SEARCH_PATHS];
#if !FOUNDATION_PLATFORM_FAMILY_MOBILE
	const char* const* cmd_line;
	int icl, clsize;
#endif
	int start_path, i, j;

	const char buildsuffix[4][9] = { "/debug", "/release", "/profile", "/deploy" };
	const char platformsuffix[7][14] = { "/win32", "/win64", "/osx", "/ios", "/android", "/raspberrypi", "/unknown" };
	const char binsuffix[1][5] = { "/bin" };

	FOUNDATION_ASSERT( name );

	sub_exe_path = path_merge( environment_executable_directory(), "config" );
	exe_parent_path = path_merge( environment_executable_directory(), "../config" );
	abs_exe_parent_path = path_make_absolute( exe_parent_path );

	exe_processed_path = string_clone( environment_executable_directory() );
	for( i = 0; i < 4; ++i )
	{
		if( string_ends_with( exe_processed_path, buildsuffix[i] ) )
		{
			exe_processed_path[ string_length( exe_processed_path ) - string_length( buildsuffix[i] ) ] = 0;
			break;
		}
	}
	for( i = 0; i < 7; ++i )
	{
		if( string_ends_with( exe_processed_path, platformsuffix[i] ) )
		{
			exe_processed_path[ string_length( exe_processed_path ) - string_length( platformsuffix[i] ) ] = 0;
			break;
		}
	}
	for( i = 0; i < 1; ++i )
	{
		if( string_ends_with( exe_processed_path, binsuffix[i] ) )
		{
			exe_processed_path[ string_length( exe_processed_path ) - string_length( binsuffix[i] ) ] = 0;
			break;
		}
	}
	exe_processed_path = path_append( exe_processed_path, "config" );
	abs_exe_processed_path = path_make_absolute( exe_processed_path );
	
	paths[0] = environment_executable_directory();
	paths[1] = sub_exe_path;
	paths[2] = abs_exe_parent_path;
	paths[3] = abs_exe_processed_path;

#if FOUNDATION_PLATFORM_FAMILY_DESKTOP && !BUILD_DEPLOY
	paths[4] = environment_initial_working_directory();
#else
	paths[4] = 0;
#endif

#if FOUNDATION_PLATFORM_APPLE
	bundle_path = path_merge( environment_executable_directory(), "../Resources/config" );
	paths[5] = bundle_path;
#elif FOUNDATION_PLATFORM_ANDROID
	paths[5] = "/config";
#else
	paths[5] = 0;
#endif

#if FOUNDATION_PLATFORM_FAMILY_DESKTOP
	paths[6] = environment_current_working_directory();
#else
	paths[6] = 0;
#endif

	paths[7] = 0;
	paths[8] = 0;

	string_deallocate( exe_parent_path );
	string_deallocate( exe_processed_path );

#if FOUNDATION_PLATFORM_FAMILY_DESKTOP
	cwd_config_path = path_merge( environment_current_working_directory(), "config" );
	paths[7] = cwd_config_path;

	cmd_line = environment_command_line();
	/*lint -e{850} We modify loop var to skip extra arg */
	for( icl = 0, clsize = array_size( cmd_line ); icl < clsize; ++icl )
	{
		/*lint -e{613} array_size( cmd_line ) in loop condition does the null pointer guard */
		if( string_equal_substr( cmd_line[icl], "--configdir", 11 ) )
		{
			if( string_equal_substr( cmd_line[icl], "--configdir=", 12 ) )
			{
				paths[8] = cmdline_path = string_substr( cmd_line[icl], 12, STRING_NPOS );
			}
			else if( icl < ( clsize - 1 ) )
			{
				paths[8] = cmdline_path = string_clone( cmd_line[++icl] );
			}
		}
	}
#endif
	
	start_path = 0;
	if( !built_in )
	{
#if FOUNDATION_PLATFORM_WINDOWS
		home_dir = path_merge( environment_home_directory(), environment_application()->config_dir );
#elif FOUNDATION_PLATFORM_LINUX || FOUNDATION_PLATFORM_MACOSX
		home_dir = path_prepend( string_concat( ".", environment_application()->config_dir ), environment_home_directory() );
#endif
		if( home_dir )
			paths[9] = home_dir;
		start_path = 9;
	}
	else
	{
		paths[9] = 0;
	}

	for( i = start_path; i < NUM_SEARCH_PATHS; ++i )
	{
		char* filename;
		stream_t* istream;
		bool path_already_searched = false;

		if( !paths[i] )
			continue;

		for( j = start_path; j < i; ++j )
		{
			if( paths[j] && string_equal( paths[j], paths[i] ) )
			{
				path_already_searched = true;
				break;
			}
		}
		if( path_already_searched )
			continue;
		
		//TODO: Support loading configs from virtual file system (i.e in zip/other packages)
		filename = string_append( path_merge( paths[i], name ), ".ini" );
		istream = 0;
#if FOUNDATION_PLATFORM_ANDROID
		if( i == ANDROID_ASSET_PATH_INDEX )
			istream = asset_stream_open( filename, STREAM_IN );
		else
#endif
		istream = stream_open( filename, STREAM_IN );
		if( istream )
		{
			config_parse( istream, filter_section, overwrite );
			stream_deallocate( istream );
		}
		string_deallocate( filename );

		if( built_in )
		{
			const char* FOUNDATION_PLATFORM_name =
#if FOUNDATION_PLATFORM_WINDOWS
				"windows";
#elif FOUNDATION_PLATFORM_LINUX_RASPBERRYPI
				"raspberrypi";
#elif FOUNDATION_PLATFORM_LINUX
				"linux";
#elif FOUNDATION_PLATFORM_MACOSX
				"osx";
#elif FOUNDATION_PLATFORM_IOS
				"ios";
#elif FOUNDATION_PLATFORM_ANDROID
				"android";
#else
#  error Insert platform name
				"unknown";
#endif
			filename = string_append( path_append( path_merge( paths[i], FOUNDATION_PLATFORM_name ), name ), ".ini" );
#if FOUNDATION_PLATFORM_ANDROID
			if( i == ANDROID_ASSET_PATH_INDEX )
				istream = asset_stream_open( filename, STREAM_IN );
			else
#endif
			istream = stream_open( filename, STREAM_IN );
			if( istream )
			{
				config_parse( istream, filter_section, overwrite );
				stream_deallocate( istream );
			}
			string_deallocate( filename );
		}
	}

	string_deallocate( home_dir );
	string_deallocate( cmdline_path );
	string_deallocate( sub_exe_path );
	string_deallocate( abs_exe_processed_path );
	string_deallocate( abs_exe_parent_path );
	string_deallocate( bundle_path );
	string_deallocate( cwd_config_path );
}