diff --git a/src/net_util.h b/src/net_util.h index f9016c930d..8ddc4d99e9 100644 --- a/src/net_util.h +++ b/src/net_util.h @@ -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) { diff --git a/src/zeek.bif b/src/zeek.bif index 717e8a988a..11976e6a16 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -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(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(0.0); + } + + float f; + memcpy(&f, s->Bytes(), sizeof(float)); + return zeek::make_intrusive(ntohf(f)); + %} + ## Converts a string of bytes to a :zeek:type:`count`. ## ## s: A string of bytes containing the binary representation of the value. diff --git a/testing/btest/Baseline/bifs.bytestring_to_double/out b/testing/btest/Baseline/bifs.bytestring_to_double/out index b7fe7801fd..bf6c042226 100644 --- a/testing/btest/Baseline/bifs.bytestring_to_double/out +++ b/testing/btest/Baseline/bifs.bytestring_to_double/out @@ -8,3 +8,4 @@ inf -inf nan 4.94e-324 +0.0 diff --git a/testing/btest/Baseline/bifs.bytestring_to_float/out b/testing/btest/Baseline/bifs.bytestring_to_float/out new file mode 100644 index 0000000000..20c2130165 --- /dev/null +++ b/testing/btest/Baseline/bifs.bytestring_to_float/out @@ -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 diff --git a/testing/btest/bifs/bytestring_to_double.zeek b/testing/btest/bifs/bytestring_to_double.zeek index ef6890bd61..d008b4039d 100644 --- a/testing/btest/bifs/bytestring_to_double.zeek +++ b/testing/btest/bifs/bytestring_to_double.zeek @@ -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"); } diff --git a/testing/btest/bifs/bytestring_to_float.zeek b/testing/btest/bifs/bytestring_to_float.zeek new file mode 100644 index 0000000000..bdab37e668 --- /dev/null +++ b/testing/btest/bifs/bytestring_to_float.zeek @@ -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"); + }