Ejemplo n.º 1
0
double get_float(void *buffer)
{
	unsigned char *buf = buffer;
	unsigned long frac = get_long(buf);

	int 	expr	= get_sshort(buf + 4),
		sign 	= get_byte(buf + 6);

        /* if (sign) f = frac; else f = -frac; return ldexp(f, exp); */
        return ldexp(sign ? (double) frac : -(double) frac, expr);
}
Ejemplo n.º 2
0
bool valtostr(const value_t * val, char *str, size_t n)
{
    char buf[128];
    int max_bytes = 0;
    bool print_as_unsigned = false;

#define FLAG_MACRO(bytes, string) (val->flags.u##bytes##b && val->flags.s##bytes##b) ? (string " ") : (val->flags.u##bytes##b) ? (string "u ") : (val->flags.s##bytes##b) ? (string "s ") : ""

    /* set the flags */
    snprintf(buf, sizeof(buf), "[%s%s%s%s%s%s%s]",
             FLAG_MACRO(64, "I64"),
             FLAG_MACRO(32, "I32"),
             FLAG_MACRO(16, "I16"),
             FLAG_MACRO(8,  "I8"),
             val->flags.f64b ? "F64 " : "",
             val->flags.f32b ? "F32 " : "",
             (val->flags.ineq_reverse && !val->flags.ineq_forwards) ? "(reversed inequality) " : "");

    if (val->flags.u64b) {
        max_bytes = 8;
        print_as_unsigned =  true;
    }
    else if (val->flags.s64b) {
        max_bytes = 8;
        print_as_unsigned = false;
    }
    else if (val->flags.u32b) {
        max_bytes = 4;
        print_as_unsigned =  true;
    }
    else if (val->flags.s32b) {
        max_bytes = 4;
        print_as_unsigned = false;
    }
    else if (val->flags.u16b) {
        max_bytes = 2;
        print_as_unsigned =  true;
    }
    else if (val->flags.s16b) {
        max_bytes = 2;
        print_as_unsigned = false;
    }
    else if (val->flags.u8b ) {
        max_bytes = 1;
        print_as_unsigned =  true;
    }
    else if (val->flags.s8b ) {
        max_bytes = 1;
        print_as_unsigned = false;
    }

    /* find the right format, considering different integer size implementations */
    if (max_bytes == sizeof(long long)) snprintf(str, n, print_as_unsigned ? "%llu, %s" : "%lld, %s", print_as_unsigned ? get_ulonglong(val) : get_slonglong(val), buf);
    else if (max_bytes == sizeof(long))      snprintf(str, n, print_as_unsigned ? "%lu, %s"  : "%ld, %s" , print_as_unsigned ? get_ulong(val) : get_slong(val), buf);
    else if (max_bytes == sizeof(int))       snprintf(str, n, print_as_unsigned ? "%u, %s"   : "%d, %s"  , print_as_unsigned ? get_uint(val) : get_sint(val), buf);
    else if (max_bytes == sizeof(short))     snprintf(str, n, print_as_unsigned ? "%hu, %s"  : "%hd, %s" , print_as_unsigned ? get_ushort(val) : get_sshort(val), buf);
    else if (max_bytes == sizeof(char))      snprintf(str, n, print_as_unsigned ? "%hhu, %s" : "%hhd, %s", print_as_unsigned ? get_uchar(val) : get_schar(val), buf);
    else if (val->flags.f64b) snprintf(str, n, "%lf, %s", get_f64b(val), buf);
    else if (val->flags.f32b) snprintf(str, n, "%f, %s", get_f32b(val), buf);
    else {
        snprintf(str, n, "%#llx?, %s", get_slonglong(val), buf);
        return false;
    }

    return true;
}