FreeBSD/contrib/elftoolchain/libelftc/libelftc_dem_gnu3.c
/*
 * Decode floating point representation to string
 * Return new allocated string or NULL
 *
 * Todo
 * Replace these functions to macro.
 */
Decode floating point representation to string
Return new allocated string or NULL


Decode floating point representation to string Return new allocated string or NULL
static char *
decode_fp_to_double(const char *p, size_t len)
{
	double f;
	size_t rtn_len, limit, i;
	int byte;
	char *rtn;
	if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(double))
		return (NULL);
	memset(&f, 0, sizeof(double));
	for (i = 0; i < len / 2; ++i) {
		byte = hex_to_dec(p[len - i * 2 - 1]) +
		    hex_to_dec(p[len - i * 2 - 2]) * 16;
		if (byte < 0 || byte > 255)
			return (NULL);
#if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN
		((unsigned char *)&f)[i] = (unsigned char)(byte);
#else 
		((unsigned char *)&f)[sizeof(double) - i - 1] =
		    (unsigned char)(byte);
#endif 
	}
	rtn_len = 64;
	limit = 0;
again:
	if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL)
		return (NULL);
	if (snprintf(rtn, rtn_len, "%fld", f) >= (int)rtn_len) {
		free(rtn);
		if (limit++ > FLOAT_SPRINTF_TRY_LIMIT)
			return (NULL);
		rtn_len *= BUFFER_GROWFACTOR;
		goto again;
	}
	return rtn;
}
static char * decode_fp_to_double ( const char * p , size_t len ) { double f ; size_t rtn_len , limit , i ; int byte ; char * rtn ; if ( p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof ( double ) ) return ( NULL ) ; memset ( & f , 0 , sizeof ( double ) ) ; for ( i = 0 ; i < len / 2 ; ++ i ) { byte = hex_to_dec ( p [ len - i * 2 - 1 ] ) + hex_to_dec ( p [ len - i * 2 - 2 ] ) * 16 ; if ( byte < 0 || byte > 255 ) return ( NULL ) ; #if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN ( ( unsigned char * ) & f ) [ i ] = ( unsigned char ) ( byte ) ; #else ( ( unsigned char * ) & f ) [ sizeof ( double ) - i - 1 ] = ( unsigned char ) ( byte ) ; #endif } rtn_len = 64 ; limit = 0 ; again : if ( ( rtn = malloc ( sizeof ( char ) * rtn_len ) ) == NULL ) return ( NULL ) ; if ( snprintf ( rtn , rtn_len , " " , f ) >= ( int ) rtn_len ) { free ( rtn ) ; if ( limit ++ > FLOAT_SPRINTF_TRY_LIMIT ) return ( NULL ) ; rtn_len *= BUFFER_GROWFACTOR ; goto again ; } return rtn ; }
Decode floating point representation to string
 Return new allocated string or NULL
Decode floating point representation to string Return new allocated string or NULL
/* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */