GH-696: Add bytestring_to_float BIF

This commit is contained in:
Tim Wojtulewicz 2022-07-11 13:12:46 -07:00
parent 96a14b39fa
commit 7e56605d83
6 changed files with 101 additions and 1 deletions

View file

@ -261,6 +261,15 @@ inline double htond(double d)
return d;
}
inline float ntohf(float f)
{
return f;
}
inline float htonf(float f)
{
return f;
}
#ifndef HAVE_BYTEORDER_64
inline uint64_t ntohll(uint64_t i)
{
@ -299,6 +308,27 @@ inline double htond(double d)
return ntohd(d);
}
inline float ntohf(float f)
{
assert(sizeof(f) == 4);
float tmp;
char* src = (char*)&f;
char* dst = (char*)&tmp;
dst[0] = src[3];
dst[1] = src[2];
dst[2] = src[1];
dst[3] = src[0];
return tmp;
}
inline float htonf(float f)
{
return ntohf(f);
}
#ifndef HAVE_BYTEORDER_64
inline uint64_t ntohll(uint64_t i)
{

View file

@ -2831,13 +2831,16 @@ function to_port%(s: string%): port
return zeek::val_mgr->Port(port, TRANSPORT_UNKNOWN);
%}
## Converts a string of bytes (in network byte order) to a :zeek:type:`double`.
## Converts a string of bytes representing a double value (in network byte order)
## to a :zeek:type:`double`. This is similar to :zeek:id:`bytestring_to_float`
## but works on 8-byte strings.
##
## s: A string of bytes containing the binary representation of a double value.
##
## Returns: The double value contained in *s*, or 0 if the conversion
## failed.
##
## .. zeek:see:: bytestring_to_float
function bytestring_to_double%(s: string%): double
%{
if ( s->Len() != sizeof(double) )
@ -2852,6 +2855,29 @@ function bytestring_to_double%(s: string%): double
return zeek::make_intrusive<zeek::DoubleVal>(ntohd(d));
%}
## Converts a string of bytes representing a float value (in network byte order)
## to a :zeek:type:`double`. This is similar to :zeek:id:`bytestring_to_double`
## but works on 4-byte strings.
##
## s: A string of bytes containing the binary representation of a float value.
##
## Returns: The float value contained in *s*, or 0 if the conversion
## failed.
##
## .. zeek:see:: bytestring_to_double
function bytestring_to_float%(s: string%): double
%{
if ( s->Len() != sizeof(float) )
{
zeek::emit_builtin_error("bad conversion to float");
return zeek::make_intrusive<zeek::DoubleVal>(0.0);
}
float f;
memcpy(&f, s->Bytes(), sizeof(float));
return zeek::make_intrusive<zeek::DoubleVal>(ntohf(f));
%}
## Converts a string of bytes to a :zeek:type:`count`.
##
## s: A string of bytes containing the binary representation of the value.

View file

@ -8,3 +8,4 @@ inf
-inf
nan
4.94e-324
0.0

View file

@ -0,0 +1,11 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
3.139244209995776e+15
-3.139244209995776e+15
4.000000e-38
0.000000e+00
-0.000000e+00
inf
-inf
nan
1.40e-45
0.0

View file

@ -23,4 +23,7 @@ event zeek_init()
print bytestring_to_double(s7);
print bytestring_to_double(s8);
print fmt("%.2e", bytestring_to_double(s9));
# Error case, passing an incorrectly-sized string. This returns zero.
print bytestring_to_double("\x00\x00\x00\x00\x00\x00\x00");
}

View file

@ -0,0 +1,29 @@
#
# @TEST-EXEC: zeek -b %INPUT >out
# @TEST-EXEC: btest-diff out
event zeek_init()
{
local s1 = "\x59\x32\x72\x04"; # 3.14e15
local s2 = "\xd9\x32\x72\x04"; #-3.14e15
local s3 = "\x01\x59\xc7\xdd"; # 4e-38
local s4 = "\x00\x00\x00\x00"; # 0.0
local s5 = "\x80\x00\x00\x00"; #-0.0
local s6 = "\x7f\x80\x00\x00"; # Inf
local s7 = "\xff\x80\x00\x00"; #-Inf
local s8 = "\x7f\xc0\x00\x00"; # NaN
local s9 = "\x00\x00\x00\x01"; # subnormal
print bytestring_to_float(s1);
print bytestring_to_float(s2);
print fmt("%e", bytestring_to_float(s3));
print fmt("%e", bytestring_to_float(s4));
print fmt("%e", bytestring_to_float(s5));
print bytestring_to_float(s6);
print bytestring_to_float(s7);
print bytestring_to_float(s8);
print fmt("%.2e", bytestring_to_float(s9));
# Error case, passing an incorrectly-sized string. This returns zero.
print bytestring_to_float("\x00\x00\x00");
}