netmsgs  1.2.2
RoadNarrows Robotics Network Messaging Package
nmLibPack.c File Reference

Field value packing/unpacking definitions. More...

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <libgen.h>
#include <string.h>
#include <ctype.h>
#include <endian.h>
#include <ieee754.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/netmsgs.h"
#include "nmLibInternal.h"

Go to the source code of this file.

Macros

#define HOST_BIG_ENDIAN
 big-endian byte order
 
#define HOST_WORD_SIZE   32
 32-bit architecture
 
#define PACKNATIVE(val, buf)   packbig((byte_t *)&(val), sizeof(val), buf)
 Pack an element into the message buffer in native byte order. More...
 
#define UNPACKNATIVE(buf, pval)   unpackbig(buf, (byte_t *)pval, sizeof(*pval))
 Unpack an element from the message buffer in native byte order. More...
 
#define PACK64NATIVE(uMSB, uLSB, buf)   PACK64BIG(uMSB, uLSB, buf)
 Pack two 4-btye unsigned integers into the message buffer in native byte order. More...
 
#define UNPACK64NATIVE(buf, puMSB, puLSB)   UNPACK64BIG(buf, puMSB, puLSB)
 Unpack two 4-byte unsigned integers from the message buffer in native byte order. More...
 
#define PACKBIG(val, buf)   packbig((byte_t *)&(val), sizeof(val), buf)
 Pack an element into the message buffer in big-endian byte order. More...
 
#define UNPACKBIG(buf, pval)   unpackbig(buf, (byte_t *)pval, sizeof(*pval));
 Unpack an element from the message buffer in big-endian byte order. More...
 
#define PACKLITTLE(val, buf)   packlittle((byte_t *)&(val), sizeof(val), buf)
 Pack an element into the message buffer in little-endian byte order. More...
 
#define UNPACKLITTLE(buf, pval)   unpacklittle(buf, (byte_t *)pval, sizeof(*pval))
 Unpack an element from the message buffer in little-endian byte order. More...
 
#define PACK64BIG(uMSB, uLSB, buf)
 Pack two 4-btye unsigned integers into the message buffer in big-endian byte order. More...
 
#define UNPACK64BIG(buf, puMSB, puLSB)
 Unpack two 4-byte unsigned integers from the message buffer in big-endian byte order. More...
 
#define PACK64LITTLE(uMSB, uLSB, buf)
 Pack two 4-btye unsigned integers into the message buffer in little-endian byte order. More...
 
#define UNPACK64LITTLE(buf, puMSB, puLSB)
 Unpack two 4-byte unsigned integers from the message buffer in little-endian byte order. More...
 
#define IEEE754_F32_SIGN_SHIFT   31
 f32 sign shift
 
#define IEEE754_F32_SIGN_MASK   0x00000001
 f32 sign mask
 
#define IEEE754_F32_EXPONENT_SHIFT   23
 f32 exponent shift
 
#define IEEE754_F32_EXPONENT_MASK   0x000000ff
 f32 exponent mask
 
#define IEEE754_F32_MANTISSA_SHIFT   0
 f32 mantissa shift
 
#define IEEE754_F32_MANTISSA_MASK   0x007fffff
 f32 mantissa mask
 
#define IEEE754_F64_SIGN_SHIFT   31
 f64 sign shift
 
#define IEEE754_F64_SIGN_MASK   0x00000001
 f64 sign mask
 
#define IEEE754_F64_EXPONENT_SHIFT   20
 f64 exponent shift
 
#define IEEE754_F64_EXPONENT_MASK   0x000007ff
 f64 exponent mask
 
#define IEEE754_F64_MANTISSA0_SHIFT   0
 f64 msb mantissa shift
 
#define IEEE754_F64_MANTISSA0_MASK   0x000fffff
 f64 msb mantissa mask
 
#define IEEE754_F64_MANTISSA1_SHIFT   0
 f64 lsb mantissa shift
 
#define IEEE754_F64_MANTISSA1_MASK   0xffffffff
 f64 lsb mantissa mask
 
#define IEEE754_F32_F64_MANTISSA0_SHIFT   3
 msb mantissa shift
 
#define IEEE754_F32_F64_MANTISSA1_SHIFT   29
 lsb mantissa shift
 
#define IEEE754_F32_F64_MANTISSA1_MASK   0x00000007
 lsb mantissa mask
 
#define IEEE754_F32_EXP_MAX   127
 maximum power of 2 exponent
 
#define IEEE754_F32_EXP_MIN   (-126)
 minimum power of 2 exponent
 
#define IEEE754_F64_EXP_MAX   1023
 maximum power of 2 exponent
 
#define IEEE754_F64_EXP_MIN   (-1022)
 minimum power of 2 exponent
 
#define IEEE754_F32_POS_INF   HUGE_VALF
 f32 positive infinity
 
#define IEEE754_F32_NEG_INF   (-HUGE_VALF)
 f32 negative infinity
 
#define IEEE754_F64_POS_INF   HUGE_VAL
 f64 positive infinity
 
#define IEEE754_F64_NEG_INF   (-HUGE_VAL)
 f64 negative infinity
 
#define _NM_CHK_MEM(size, count)
 Check if memory is available. More...
 
#define _NM_CHK_ARCH(val, size)
 Check if host architecture supports basic element sizes. More...
 

Functions

static void packbig (byte_t *p, size_t size, byte_t buf[])
 Pack an element into the message buffer in big-endian byte order. More...
 
static void unpackbig (byte_t buf[], byte_t *p, size_t size)
 Unpack an element from the message buffer in big-endian byte order. More...
 
static void packlittle (byte_t *p, size_t size, byte_t buf[])
 Pack an element into the message buffer in little-endian byte order. More...
 
static void unpacklittle (byte_t buf[], byte_t *p, size_t size)
 Unpack an element from the message buffer in little-endian byte order. More...
 
static bool_t ieee754_f32_is_nan (union ieee754_float f)
 Test if 32-bit floating-point number is a NaN. More...
 
static bool_t ieee754_f64_is_nan (union ieee754_double d)
 Test if 64-bit floating-point number is a NaN. More...
 
static bool_t ieee754_f32_is_inf (union ieee754_float f)
 Test if 32-bit floating-point number is ± infinity. More...
 
static bool_t ieee754_f64_is_inf (union ieee754_double d)
 Test if 64-bit floating-point number is ± infinity. More...
 
static bool_t ieee754_f32_is_zero (union ieee754_float f)
 Test if 32-bit floating-point number is ± 0.0. More...
 
static bool_t ieee754_f64_is_zero (union ieee754_double d)
 Test if 64-bit floating-point number is ± 0.0. More...
 
int nmPackU8 (byte_t val, byte_t buf[], size_t bufSize, NMEndian_T eEndian)
 Pack an unsigned 8-bit byte into the message buffer. More...
 
int nmPackU16 (ushort_t val, byte_t buf[], size_t bufSize, NMEndian_T eEndian)
 Pack an unsigned 16-bit integer into the message buffer. More...
 
int nmPackU32 (uint_t val, byte_t buf[], size_t bufSize, NMEndian_T eEndian)
 Pack an unsigned 32-bit integer into the message buffer. More...
 
int nmPackU64 (ulonglong_t val, byte_t buf[], size_t bufSize, NMEndian_T eEndian)
 Pack an unsigned 64-bit integer into the message buffer. More...
 
int nmPackS64 (long long val, byte_t buf[], size_t bufSize, NMEndian_T eEndian)
 Pack an signed 64-bit integer into the message buffer. More...
 
int nmPackF32 (float val, byte_t buf[], size_t bufSize, NMEndian_T eEndian)
 Pack a 32-bit float into the message buffer. More...
 
int nmPackF64 (double val, byte_t buf[], size_t bufSize, NMEndian_T eEndian)
 Pack a 64-bit float into the message buffer. More...
 
int nmPackP32 (void *val, byte_t buf[], size_t bufSize, NMEndian_T eEndian)
 Pack a 32-bit pointer into the message buffer. More...
 
int nmPackP64 (void *val, byte_t buf[], size_t bufSize, NMEndian_T eEndian)
 Pack a 64-bit pointer into the message buffer. More...
 
int nmPackBuf (byte_t bufSrc[], size_t uCount, byte_t buf[], size_t bufSize, NMEndian_T eEndian)
 Pack a byte buffer into the message buffer. More...
 
int nmUnpackU8 (byte_t buf[], size_t bufSize, byte_t *pVal, NMEndian_T eEndian)
 Unpack an unsigned 8-bit byte from the message buffer. More...
 
int nmUnpackU16 (byte_t buf[], size_t bufSize, ushort_t *pVal, NMEndian_T eEndian)
 Unpack an unsigned 16-bit integer from the message buffer. More...
 
int nmUnpackU32 (byte_t buf[], size_t bufSize, uint_t *pVal, NMEndian_T eEndian)
 Unpack an unsigned 32-bit integer from the message buffer. More...
 
int nmUnpackU64 (byte_t buf[], size_t bufSize, ulonglong_t *pVal, NMEndian_T eEndian)
 Unpack an unsigned 64-bit integer from the message buffer. More...
 
int nmUnpackS64 (byte_t buf[], size_t bufSize, long long *pVal, NMEndian_T eEndian)
 Unpack an signed 64-bit integer from the message buffer. More...
 
int nmUnpackF32 (byte_t buf[], size_t bufSize, float *pVal, NMEndian_T eEndian)
 Unpack a 32-bit float from the message buffer. More...
 
int nmUnpackF64 (byte_t buf[], size_t bufSize, double *pVal, NMEndian_T eEndian)
 Unpack a 64-bit float from the message buffer. More...
 
int nmUnpackP32 (byte_t buf[], size_t bufSize, void *pVal, NMEndian_T eEndian)
 Unpack a 32-bit pointer from the message buffer. More...
 
int nmUnpackP64 (byte_t buf[], size_t bufSize, void *pVal, NMEndian_T eEndian)
 Unpack a 64-bit pointer from the message buffer. More...
 
int nmUnpackBuf (byte_t buf[], size_t bufSize, byte_t bufDst[], size_t uCount, NMEndian_T eEndian)
 Unpack a byte buffer from the message buffer. More...
 

Variables

static const union ieee754_float IEEE754_F32_NaN
 
static const union ieee754_double IEEE754_F64_NaN
 
static const union ieee754_float IEEE754_F32_Inf
 
static const union ieee754_double IEEE754_F64_Inf
 
static const union ieee754_float IEEE754_F32_Zero
 
static const union ieee754_double IEEE754_F64_Zero
 

Detailed Description

Field value packing/unpacking definitions.

LastChangedDate
2011-11-18 13:32:30 -0700 (Fri, 18 Nov 2011)
Rev
1578

The host may be either a big-endian or little-endian architecture. Currenly, any exotic architectures are not supported. It is assumed that 1, 2, and 4 byte (unsigned) integers and 4 byte floats are supported. For systems that do not support 8 byte (unsigned) integers or 8 byte doubles, promotion will be done in software. However, on unpacking, rounding or truncation will occur if the unpacked element exceeds the 4 byte limits.

Floats are expected to be in IEEE 754 format.

Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file nmLibPack.c.

Macro Definition Documentation

#define _NM_CHK_ARCH (   val,
  size 
)
Value:
do \
{ \
if( sizeof(val) != (size) ) \
{ \
} \
} \
while(0)
#define NM_ECODE_ARCH_NOTSUP
machine architecture not supported
Definition: netmsgs.h:74

Check if host architecture supports basic element sizes.

On check failure, returns from calling function with NM_ECODE_ARCH_NOTSUP.

Parameters
valElement to check
sizeRequired byte size.

Definition at line 724 of file nmLibPack.c.

Referenced by nmPackF32(), nmPackF64(), nmPackP64(), nmPackS64(), nmPackU64(), nmUnpackF32(), nmUnpackF64(), nmUnpackP64(), nmUnpackS64(), and nmUnpackU64().

#define _NM_CHK_MEM (   size,
  count 
)
Value:
do \
{ \
if( (size) < (count) ) \
{ \
return -NM_ECODE_NOMEM; \
} \
} \
while(0)
#define NM_ECODE_NOMEM
not enough buffer memory available
Definition: netmsgs.h:73

Check if memory is available.

On check failure, returns from calling function with NM_ECODE_NOMEM.

Parameters
sizeAvailable memory in bytes.
countRequired memory.

Definition at line 705 of file nmLibPack.c.

Referenced by nmPackBuf(), nmPackF32(), nmPackF64(), nmPackP32(), nmPackP64(), nmPackS64(), nmPackU16(), nmPackU32(), nmPackU64(), nmPackU8(), nmUnpackBuf(), nmUnpackF32(), nmUnpackF64(), nmUnpackP32(), nmUnpackP64(), nmUnpackS64(), nmUnpackU16(), nmUnpackU32(), nmUnpackU64(), and nmUnpackU8().

#define PACK64BIG (   uMSB,
  uLSB,
  buf 
)
Value:
do \
{ \
PACKBIG(uMSB, buf); \
PACKBIG(uLSB, &(buf)[4]); \
} while(0)
#define PACKBIG(val, buf)
Pack an element into the message buffer in big-endian byte order.
Definition: nmLibPack.c:397

Pack two 4-btye unsigned integers into the message buffer in big-endian byte order.

Safe 8-byte packing.

Note
The netmsgs package requires the host to have 4-byte integers available, but not 8-byte versions.
Parameters
[in]uMSBMost Significant Bytes.
[in]uLSBLeast Significant Bytes.
[out]bufOutput message buffer.

Definition at line 436 of file nmLibPack.c.

Referenced by nmPackF64(), nmPackS64(), and nmPackU64().

#define PACK64LITTLE (   uMSB,
  uLSB,
  buf 
)
Value:
do \
{ \
PACKLITTLE(uLSB, buf); \
PACKLITTLE(uMSB, &(buf)[4]); \
} while(0)
#define PACKLITTLE(val, buf)
Pack an element into the message buffer in little-endian byte order.
Definition: nmLibPack.c:413

Pack two 4-btye unsigned integers into the message buffer in little-endian byte order.

Safe 8-byte packing.

Note
The netmsgs package requires the host to have 4-byte integers available, but not 8-byte versions.
Parameters
[in]uMSBMost Significant Bytes.
[in]uLSBLeast Significant Bytes.
[out]bufOutput message buffer.

Definition at line 477 of file nmLibPack.c.

Referenced by nmPackF64(), nmPackS64(), and nmPackU64().

#define PACK64NATIVE (   uMSB,
  uLSB,
  buf 
)    PACK64BIG(uMSB, uLSB, buf)

Pack two 4-btye unsigned integers into the message buffer in native byte order.

Big-Endian Host Version

Parameters
[in]uMSBMost Significant Bytes.
[in]uLSBLeast Significant Bytes.
[out]bufOutput message buffer.

Definition at line 238 of file nmLibPack.c.

Referenced by nmPackF64(), nmPackP64(), nmPackS64(), and nmPackU64().

#define PACKBIG (   val,
  buf 
)    packbig((byte_t *)&(val), sizeof(val), buf)

Pack an element into the message buffer in big-endian byte order.

Parameters
[in]valType T element to be packed.
[out]bufOutput message buffer.

Definition at line 397 of file nmLibPack.c.

Referenced by nmPackF32(), nmPackS64(), nmPackU16(), nmPackU32(), and nmPackU64().

#define PACKLITTLE (   val,
  buf 
)    packlittle((byte_t *)&(val), sizeof(val), buf)

Pack an element into the message buffer in little-endian byte order.

Parameters
[in]valType T element to be packed.
[out]bufOutput message buffer.

Definition at line 413 of file nmLibPack.c.

Referenced by nmPackF32(), nmPackS64(), nmPackU16(), nmPackU32(), and nmPackU64().

#define PACKNATIVE (   val,
  buf 
)    packbig((byte_t *)&(val), sizeof(val), buf)

Pack an element into the message buffer in native byte order.

Big-Endian Host Version

Parameters
[in]valType T element to be packed.
[out]bufOutput message buffer.

Definition at line 216 of file nmLibPack.c.

Referenced by nmPackF32(), nmPackP32(), nmPackS64(), nmPackU16(), nmPackU32(), and nmPackU64().

#define UNPACK64BIG (   buf,
  puMSB,
  puLSB 
)
Value:
do \
{ \
UNPACKBIG(buf, puMSB); \
UNPACKBIG(&(buf)[4], puLSB); \
} while(0)
#define UNPACKBIG(buf, pval)
Unpack an element from the message buffer in big-endian byte order.
Definition: nmLibPack.c:405

Unpack two 4-byte unsigned integers from the message buffer in big-endian byte order.

Safe 8-byte packing.

Note
The netmsgs package requires the host to have 4-byte integers available, but not 8-byte versions.
Parameters
[in]bufOutput message buffer.
[out]puMSBPointer to Most Significant Bytes.
[out]puLSBPointer to Least Significant Bytes.

Definition at line 457 of file nmLibPack.c.

Referenced by nmUnpackF64(), nmUnpackS64(), and nmUnpackU64().

#define UNPACK64LITTLE (   buf,
  puMSB,
  puLSB 
)
Value:
do \
{ \
UNPACKLITTLE(buf, puLSB); \
UNPACKLITTLE(&(buf)[4], puMSB); \
} while(0)
#define UNPACKLITTLE(buf, pval)
Unpack an element from the message buffer in little-endian byte order.
Definition: nmLibPack.c:421

Unpack two 4-byte unsigned integers from the message buffer in little-endian byte order.

Safe 8-byte packing.

Note
The netmsgs package requires the host to have 4-byte integers available, but not 8-byte versions.
Parameters
[in]bufOutput message buffer.
[out]puMSBPointer to Most Significant Bytes.
[out]puLSBPointer to Least Significant Bytes.

Definition at line 498 of file nmLibPack.c.

Referenced by nmUnpackF64(), nmUnpackS64(), and nmUnpackU64().

#define UNPACK64NATIVE (   buf,
  puMSB,
  puLSB 
)    UNPACK64BIG(buf, puMSB, puLSB)

Unpack two 4-byte unsigned integers from the message buffer in native byte order.

Big-Endian Host Version

Parameters
[in]bufOutput message buffer.
[out]puMSBMost Significant Bytes.
[out]puLSBLeast Significant Bytes.

Definition at line 250 of file nmLibPack.c.

Referenced by nmUnpackF64(), nmUnpackP64(), nmUnpackS64(), and nmUnpackU64().

#define UNPACKBIG (   buf,
  pval 
)    unpackbig(buf, (byte_t *)pval, sizeof(*pval));

Unpack an element from the message buffer in big-endian byte order.

Parameters
[in]bufOutput message buffer.
[out]pvalType T* element to be unpacked (set).

Definition at line 405 of file nmLibPack.c.

Referenced by nmUnpackF32(), nmUnpackF64(), nmUnpackS64(), nmUnpackU16(), nmUnpackU32(), and nmUnpackU64().

#define UNPACKLITTLE (   buf,
  pval 
)    unpacklittle(buf, (byte_t *)pval, sizeof(*pval))

Unpack an element from the message buffer in little-endian byte order.

Parameters
[in]bufOutput message buffer.
[out]pvalType T* element to be unpacked (set).

Definition at line 421 of file nmLibPack.c.

Referenced by nmUnpackF32(), nmUnpackF64(), nmUnpackS64(), nmUnpackU16(), nmUnpackU32(), and nmUnpackU64().

#define UNPACKNATIVE (   buf,
  pval 
)    unpackbig(buf, (byte_t *)pval, sizeof(*pval))

Unpack an element from the message buffer in native byte order.

Big-Endian Host Version

Parameters
[in]bufOutput message buffer.
[out]pvalType T* element to be unpacked (set).

Definition at line 226 of file nmLibPack.c.

Referenced by nmUnpackF32(), nmUnpackF64(), nmUnpackP32(), nmUnpackS64(), nmUnpackU16(), nmUnpackU32(), and nmUnpackU64().

Function Documentation

static bool_t ieee754_f32_is_inf ( union ieee754_float  f)
inlinestatic

Test if 32-bit floating-point number is ± infinity.

Parameters
f32-bit float with bit overlay.
Returns
Returns true or false.

Definition at line 653 of file nmLibPack.c.

Referenced by nmPackF64().

654 {
655  return (f.ieee.exponent == IEEE754_F32_Inf.ieee.exponent)
656  && (f.ieee.mantissa == 0)? true: false;
657 }
static const union ieee754_float IEEE754_F32_Inf
Definition: nmLibPack.c:587
static bool_t ieee754_f32_is_nan ( union ieee754_float  f)
inlinestatic

Test if 32-bit floating-point number is a NaN.

Parameters
f32-bit float with bit overlay.
Returns
Returns true or false.

Definition at line 631 of file nmLibPack.c.

Referenced by nmPackF64().

632 {
633  return (f.ieee.exponent == IEEE754_F32_NaN.ieee.exponent)
634  && (f.ieee.mantissa != 0)? true: false;
635 }
static const union ieee754_float IEEE754_F32_NaN
Definition: nmLibPack.c:566
static bool_t ieee754_f32_is_zero ( union ieee754_float  f)
inlinestatic

Test if 32-bit floating-point number is ± 0.0.

Parameters
f32-bit float with bit overlay.
Returns
Returns true or false.

Definition at line 675 of file nmLibPack.c.

Referenced by nmPackF64().

676 {
677  return (f.ieee.exponent == IEEE754_F32_Zero.ieee.exponent)
678  && (f.ieee.mantissa == 0)? true: false;
679 }
static const union ieee754_float IEEE754_F32_Zero
Definition: nmLibPack.c:608
static bool_t ieee754_f64_is_inf ( union ieee754_double  d)
inlinestatic

Test if 64-bit floating-point number is ± infinity.

Parameters
d64-bit float with bit overlay.
Returns
Returns true or false.

Definition at line 664 of file nmLibPack.c.

Referenced by nmUnpackF64().

665 {
666  return (d.ieee.exponent == IEEE754_F64_Inf.ieee.exponent)
667  && ((d.ieee.mantissa0 == 0) || (d.ieee.mantissa1 == 0))? true: false;
668 }
static const union ieee754_double IEEE754_F64_Inf
Definition: nmLibPack.c:597
static bool_t ieee754_f64_is_nan ( union ieee754_double  d)
inlinestatic

Test if 64-bit floating-point number is a NaN.

Parameters
d64-bit float with bit overlay.
Returns
Returns true or false.

Definition at line 642 of file nmLibPack.c.

Referenced by nmUnpackF64().

643 {
644  return (d.ieee.exponent == IEEE754_F64_NaN.ieee.exponent)
645  && ((d.ieee.mantissa0 != 0) || (d.ieee.mantissa1 != 0))? true: false;
646 }
static const union ieee754_double IEEE754_F64_NaN
Definition: nmLibPack.c:576
static bool_t ieee754_f64_is_zero ( union ieee754_double  d)
inlinestatic

Test if 64-bit floating-point number is ± 0.0.

Parameters
d64-bit float with bit overlay.
Returns
Returns true or false.

Definition at line 686 of file nmLibPack.c.

Referenced by nmUnpackF64().

687 {
688  return (d.ieee.exponent == IEEE754_F64_Zero.ieee.exponent)
689  && ((d.ieee.mantissa0 == 0) || (d.ieee.mantissa1 == 0))? true: false;
690 }
static const union ieee754_double IEEE754_F64_Zero
Definition: nmLibPack.c:618
int nmPackBuf ( byte_t  bufSrc[],
size_t  uCount,
byte_t  buf[],
size_t  bufSize,
NMEndian_T  eEndian 
)

Pack a byte buffer into the message buffer.

The source and message buffer memory areas can overlap.

Note
The buffer is always packed in sequential order ignoring the byte order parameter.
Parameters
[in]bufSrcSource buffer.
uCountNumber of bytes to copy.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
eEndianPacking order. See NMEndian_T.
Returns
On success, returns the number of bytes packed. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1215 of file nmLibPack.c.

References _NM_CHK_MEM.

1220 {
1221  _NM_CHK_MEM(bufSize, uCount);
1222 
1223  // safe copy
1224  memmove(buf, bufSrc, uCount);
1225 
1226  return (int)uCount;
1227 }
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmPackF32 ( float  val,
byte_t  buf[],
size_t  bufSize,
NMEndian_T  eEndian 
)

Pack a 32-bit float into the message buffer.

The IEEE 754 32-bit format is used with both the big and little endian byte orders.

Note
On some machines, while integers were represented in little-endian form, floating point numbers were represented in big-endian form. Because there are many floating point formats, and a lack of a standard network representation, no standard for transferring floating point values has been made - Wikipedia.
Todo:
Detect non-IEEE 754 formats and convert to IEEE 754 prior to packing.
Parameters
[in]valValue to pack.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
eEndianPacking order. See NMEndian_T.
Returns
On success, returns the number of bytes packed. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 957 of file nmLibPack.c.

References _NM_CHK_ARCH, _NM_CHK_MEM, IEEE754_F32_EXPONENT_MASK, IEEE754_F32_EXPONENT_SHIFT, IEEE754_F32_MANTISSA_MASK, IEEE754_F32_MANTISSA_SHIFT, IEEE754_F32_SIGN_MASK, IEEE754_F32_SIGN_SHIFT, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_F32, PACKBIG, PACKLITTLE, and PACKNATIVE.

Referenced by nmPackFlatF32(), and nmPackITVF32().

958 {
959  static size_t fvalLen = NMFVAL_LEN_F32; // message field value length
960 
961  union ieee754_float f; // 4 byte floating-point number
962  uint_t uBits; // bits
963 
964  _NM_CHK_MEM(bufSize, fvalLen);
965  _NM_CHK_ARCH(val, fvalLen);
966 
967  f.f = val;
968 
969  uBits = (uint_t)((f.ieee.negative & IEEE754_F32_SIGN_MASK)
971  uBits |= (uint_t)((f.ieee.exponent & IEEE754_F32_EXPONENT_MASK)
973  uBits |= (uint_t)((f.ieee.mantissa & IEEE754_F32_MANTISSA_MASK)
975 
976  switch( eEndian )
977  {
978  case NMEndianLittle:
979  PACKLITTLE(uBits, buf);
980  break;
981  case NMEndianNative:
982  PACKNATIVE(f.f, buf);
983  break;
984  case NMEndianBig:
985  default:
986  PACKBIG(uBits, buf);
987  break;
988  }
989 
990  return (int)fvalLen;
991 }
#define _NM_CHK_ARCH(val, size)
Check if host architecture supports basic element sizes.
Definition: nmLibPack.c:724
#define IEEE754_F32_EXPONENT_SHIFT
f32 exponent shift
Definition: nmLibPack.c:518
Most Significant Byte first.
Definition: netmsgs.h:102
native processor order
Definition: netmsgs.h:104
#define PACKBIG(val, buf)
Pack an element into the message buffer in big-endian byte order.
Definition: nmLibPack.c:397
#define PACKLITTLE(val, buf)
Pack an element into the message buffer in little-endian byte order.
Definition: nmLibPack.c:413
#define NMFVAL_LEN_F32
32-bit floating-point number field val len
Definition: netmsgs.h:181
#define IEEE754_F32_SIGN_MASK
f32 sign mask
Definition: nmLibPack.c:517
#define PACKNATIVE(val, buf)
Pack an element into the message buffer in native byte order.
Definition: nmLibPack.c:216
#define IEEE754_F32_EXPONENT_MASK
f32 exponent mask
Definition: nmLibPack.c:519
#define IEEE754_F32_SIGN_SHIFT
f32 sign shift
Definition: nmLibPack.c:516
#define IEEE754_F32_MANTISSA_MASK
f32 mantissa mask
Definition: nmLibPack.c:521
Least Significant Byte first.
Definition: netmsgs.h:103
#define IEEE754_F32_MANTISSA_SHIFT
f32 mantissa shift
Definition: nmLibPack.c:520
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmPackF64 ( double  val,
byte_t  buf[],
size_t  bufSize,
NMEndian_T  eEndian 
)

Pack a 64-bit float into the message buffer.

The IEEE 754 64-bit format is used with both the big and little endian byte orders. If the machine architecture only supports 32-bit doubles, then a conversion to 64-bit representation is performed first.

Note
On some machines, while integers were represented in little-endian form, floating point numbers were represented in big-endian form. Because there are many floating point formats, and a lack of a standard network representation, no standard for transferring floating point values has been made - Wikipedia.
Todo:
Detect non-IEEE 754 formats and convert to IEEE 754 prior to packing.
Parameters
[in]valValue to pack.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
eEndianPacking order. See NMEndian_T.
Returns
On success, returns the number of bytes packed. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1013 of file nmLibPack.c.

References _NM_CHK_ARCH, _NM_CHK_MEM, IEEE754_F32_F64_MANTISSA0_SHIFT, IEEE754_F32_F64_MANTISSA1_MASK, IEEE754_F32_F64_MANTISSA1_SHIFT, ieee754_f32_is_inf(), ieee754_f32_is_nan(), ieee754_f32_is_zero(), IEEE754_F64_EXPONENT_MASK, IEEE754_F64_EXPONENT_SHIFT, IEEE754_F64_Inf, IEEE754_F64_MANTISSA0_MASK, IEEE754_F64_MANTISSA0_SHIFT, IEEE754_F64_MANTISSA1_MASK, IEEE754_F64_MANTISSA1_SHIFT, IEEE754_F64_NaN, IEEE754_F64_SIGN_MASK, IEEE754_F64_SIGN_SHIFT, IEEE754_F64_Zero, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_F32, NMFVAL_LEN_F64, PACK64BIG, PACK64LITTLE, and PACK64NATIVE.

Referenced by nmPackFlatF64(), and nmPackITVF64().

1014 {
1015  static size_t fvalLen = NMFVAL_LEN_F64; // message field value length
1016 
1017  union ieee754_float f; // 4-byte floating-point number
1018  union ieee754_double d; // 8-byte floating-point number
1019  uint_t uMSB; // Most Signifcant Bytes
1020  uint_t uLSB; // Least Signifcant Bytes
1021 
1022  _NM_CHK_MEM(bufSize, fvalLen);
1023 
1024  // 8-byte double
1025  if( sizeof(double) == fvalLen )
1026  {
1027  d.d = val;
1028  }
1029 
1030  // 4-byte double
1031  else
1032  {
1033  _NM_CHK_ARCH(val, (size_t)NMFVAL_LEN_F32);
1034 
1035  f.f = (float)val;
1036 
1037  // special infinity cases
1038  if( ieee754_f32_is_inf(f) )
1039  {
1040  d = IEEE754_F64_Inf;
1041  }
1042 
1043  // special zero cases
1044  else if( ieee754_f32_is_zero(f) )
1045  {
1046  d = IEEE754_F64_Zero;
1047  }
1048 
1049  // special Not-a-Number case
1050  else if( ieee754_f32_is_nan(f) )
1051  {
1052  d = IEEE754_F64_NaN;
1053  }
1054 
1055  // "normal" floating-point numbers
1056  else
1057  {
1058  d.ieee.exponent = (f.ieee.exponent + (uint_t)IEEE754_DOUBLE_BIAS
1059  - (uint_t)IEEE754_FLOAT_BIAS)
1061  d.ieee.mantissa0 = ((uint_t)(f.ieee.mantissa
1062  >> (uint_t)IEEE754_F32_F64_MANTISSA0_SHIFT))
1064  d.ieee.mantissa1 = (((uint_t)(f.ieee.mantissa))
1067  }
1068 
1069  // preserve the sign
1070  d.ieee.negative = f.ieee.negative;
1071  }
1072 
1073  //
1074  // Stuff the bits into two 4-byte unsigned integers.
1075  //
1076  uMSB = (uint_t)((d.ieee.negative & IEEE754_F64_SIGN_MASK)
1078  uMSB |= (uint_t)((d.ieee.exponent & IEEE754_F64_EXPONENT_MASK)
1080  uMSB |= (uint_t)((d.ieee.mantissa0 & IEEE754_F64_MANTISSA0_MASK)
1082  uLSB = (uint_t)((d.ieee.mantissa1 & IEEE754_F64_MANTISSA1_MASK)
1084 
1085  switch( eEndian )
1086  {
1087  case NMEndianLittle:
1088  PACK64LITTLE(uMSB, uLSB, buf);
1089  break;
1090  case NMEndianNative:
1091  PACK64NATIVE(uMSB, uLSB, buf);
1092  break;
1093  case NMEndianBig:
1094  default:
1095  PACK64BIG(uMSB, uLSB, buf);
1096  break;
1097  }
1098 
1099  return (int)fvalLen;
1100 }
static bool_t ieee754_f32_is_zero(union ieee754_float f)
Test if 32-bit floating-point number is &plusmn; 0.0.
Definition: nmLibPack.c:675
#define IEEE754_F32_F64_MANTISSA1_SHIFT
lsb mantissa shift
Definition: nmLibPack.c:544
#define IEEE754_F64_MANTISSA0_SHIFT
f64 msb mantissa shift
Definition: nmLibPack.c:535
static const union ieee754_double IEEE754_F64_NaN
Definition: nmLibPack.c:576
#define IEEE754_F64_SIGN_SHIFT
f64 sign shift
Definition: nmLibPack.c:531
#define IEEE754_F64_MANTISSA1_MASK
f64 lsb mantissa mask
Definition: nmLibPack.c:538
static const union ieee754_double IEEE754_F64_Inf
Definition: nmLibPack.c:597
#define _NM_CHK_ARCH(val, size)
Check if host architecture supports basic element sizes.
Definition: nmLibPack.c:724
Most Significant Byte first.
Definition: netmsgs.h:102
#define NMFVAL_LEN_F64
64-bit floating-point number field val len
Definition: netmsgs.h:182
#define PACK64BIG(uMSB, uLSB, buf)
Pack two 4-btye unsigned integers into the message buffer in big-endian byte order.
Definition: nmLibPack.c:436
#define IEEE754_F64_MANTISSA0_MASK
f64 msb mantissa mask
Definition: nmLibPack.c:536
native processor order
Definition: netmsgs.h:104
#define IEEE754_F64_EXPONENT_MASK
f64 exponent mask
Definition: nmLibPack.c:534
#define NMFVAL_LEN_F32
32-bit floating-point number field val len
Definition: netmsgs.h:181
#define IEEE754_F64_EXPONENT_SHIFT
f64 exponent shift
Definition: nmLibPack.c:533
#define PACK64LITTLE(uMSB, uLSB, buf)
Pack two 4-btye unsigned integers into the message buffer in little-endian byte order.
Definition: nmLibPack.c:477
static bool_t ieee754_f32_is_nan(union ieee754_float f)
Test if 32-bit floating-point number is a NaN.
Definition: nmLibPack.c:631
#define IEEE754_F32_F64_MANTISSA1_MASK
lsb mantissa mask
Definition: nmLibPack.c:545
static const union ieee754_double IEEE754_F64_Zero
Definition: nmLibPack.c:618
#define PACK64NATIVE(uMSB, uLSB, buf)
Pack two 4-btye unsigned integers into the message buffer in native byte order.
Definition: nmLibPack.c:238
static bool_t ieee754_f32_is_inf(union ieee754_float f)
Test if 32-bit floating-point number is &plusmn; infinity.
Definition: nmLibPack.c:653
Least Significant Byte first.
Definition: netmsgs.h:103
#define IEEE754_F64_MANTISSA1_SHIFT
f64 lsb mantissa shift
Definition: nmLibPack.c:537
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
#define IEEE754_F64_SIGN_MASK
f64 sign mask
Definition: nmLibPack.c:532
#define IEEE754_F32_F64_MANTISSA0_SHIFT
msb mantissa shift
Definition: nmLibPack.c:543
int nmPackP32 ( void *  val,
byte_t  buf[],
size_t  bufSize,
NMEndian_T  eEndian 
)

Pack a 32-bit pointer into the message buffer.

If the machine architecture has 64-bit pointers, then only the LSB 4 bytes are packed.

Note
Pointer is always packed in native byte order.
Parameters
[in]valValue to pack.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
eEndianPacking order. See NMEndian_T.
Returns
On success, returns the number of bytes packed. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1113 of file nmLibPack.c.

References _NM_CHK_MEM, NMFVAL_LEN_P32, NMFVAL_LEN_P64, NMLIB_WARNING, and PACKNATIVE.

Referenced by nmPackFlatP32(), nmPackITVP32(), and nmPackPointer().

1114 {
1115  static size_t fvalLen = NMFVAL_LEN_P32; // message field value length
1116  ulong_t luVal;
1117  uint_t uVal;
1118 
1119  _NM_CHK_MEM(bufSize, fvalLen);
1120 
1121  // long's are same size as void *'s. exceptions?
1122  luVal = *((ulong_t *)val);
1123 
1124  // 4-byte natvie pointer
1125  if( sizeof(void *) == fvalLen )
1126  {
1127  PACKNATIVE(luVal, buf);
1128  }
1129 
1130  // 8-byte native pointer - "convert"
1131  if( sizeof(void *) == NMFVAL_LEN_P64 )
1132  {
1133 #if HOST_WORD_SIZE == 64
1134  //if( luVal & 0xffffffff00000000 )
1135  if( (luVal) & 0xffffffff00000000 )
1136  {
1137  NMLIB_WARNING("%p: Packing 64-bit pointer in 32 bits lost non-zero "
1138  "significance.", (void *)luVal);
1139  }
1140 #endif // HOST_WORD_SIZE
1141  uVal = (uint_t)(luVal & 0xffffffff);
1142  PACKNATIVE(uVal, buf);
1143  }
1144 
1145 
1146  return (int)fvalLen;
1147 }
#define NMLIB_WARNING(fmt,...)
Log libnetmsgs warning.
Definition: nmLibInternal.h:80
#define PACKNATIVE(val, buf)
Pack an element into the message buffer in native byte order.
Definition: nmLibPack.c:216
#define NMFVAL_LEN_P32
32-bit pointer field value length
Definition: netmsgs.h:183
#define NMFVAL_LEN_P64
64-bit pointer field value length
Definition: netmsgs.h:184
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmPackP64 ( void *  val,
byte_t  buf[],
size_t  bufSize,
NMEndian_T  eEndian 
)

Pack a 64-bit pointer into the message buffer.

If the machine architecture only supports 32-bit pointers, then the 4 MSBs are padded with 0's.

Note
Pointer is always packed in native byte order.
Parameters
[in]valValue to pack.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
eEndianPacking order. See NMEndian_T.
Returns
On success, returns the number of bytes packed. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1160 of file nmLibPack.c.

References _NM_CHK_ARCH, _NM_CHK_MEM, NMFVAL_LEN_P32, NMFVAL_LEN_P64, and PACK64NATIVE.

Referenced by nmPackFlatP64(), nmPackITVP64(), and nmPackPointer().

1161 {
1162  static size_t fvalLen = NMFVAL_LEN_P64; // message field value length
1163 
1164  ulong_t luVal;
1165  uint_t uMSB; // Most Signifcant Bytes
1166  uint_t uLSB; // Least Signifcant Bytes
1167 
1168  _NM_CHK_MEM(bufSize, fvalLen);
1169 
1170  // long's are same size as void *'s. exceptions?
1171  luVal = *((ulong_t *)val);
1172 
1173  // 8-byte pointer
1174  if( sizeof(void *) == fvalLen )
1175  {
1176 #if HOST_WORD_SIZE == 64
1177  uMSB = (uint_t)(luVal >> 32);
1178  uLSB = (uint_t)(luVal & 0xffffffff);
1179 #else
1180  uMSB = 0;
1181  uLSB = (uint_t)luVal;
1182 #endif // HOST_WORD_SIZE
1183  }
1184 
1185  // 4-byte pointer
1186  else
1187  {
1188  _NM_CHK_ARCH(val, (size_t)NMFVAL_LEN_P32);
1189 
1190  uMSB = 0;
1191  uLSB = (uint_t)luVal;
1192  }
1193 
1194  PACK64NATIVE(uMSB, uLSB, buf);
1195 
1196  return (int)fvalLen;
1197 }
#define _NM_CHK_ARCH(val, size)
Check if host architecture supports basic element sizes.
Definition: nmLibPack.c:724
#define NMFVAL_LEN_P32
32-bit pointer field value length
Definition: netmsgs.h:183
#define PACK64NATIVE(uMSB, uLSB, buf)
Pack two 4-btye unsigned integers into the message buffer in native byte order.
Definition: nmLibPack.c:238
#define NMFVAL_LEN_P64
64-bit pointer field value length
Definition: netmsgs.h:184
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmPackS64 ( long long  val,
byte_t  buf[],
size_t  bufSize,
NMEndian_T  eEndian 
)

Pack an signed 64-bit integer into the message buffer.

If the machine architecture only supports 32-bit long longs, then the 4 MSBs are signed extended.

Parameters
[in]valValue to pack.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
eEndianPacking order. See NMEndian_T.
Returns
On success, returns the number of bytes packed. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 885 of file nmLibPack.c.

References _NM_CHK_ARCH, _NM_CHK_MEM, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_S32, NMFVAL_LEN_S64, PACK64BIG, PACK64LITTLE, PACK64NATIVE, PACKBIG, PACKLITTLE, and PACKNATIVE.

Referenced by nmPackFlatS64(), nmPackITVS64(), nmPackLong(), and nmPackLongLong().

886 {
887  static size_t fvalLen = NMFVAL_LEN_S64; // message field value length
888 
889  uint_t uMSB; // most significant bytes
890  uint_t uLSB; // least signifcant bytes
891 
892  _NM_CHK_MEM(bufSize, fvalLen);
893 
894  // 8-byte signed long long
895  if( sizeof(long long) == fvalLen )
896  {
897  switch( eEndian )
898  {
899  case NMEndianLittle:
900  PACKLITTLE(val, buf);
901  break;
902  case NMEndianNative:
903  PACKNATIVE(val, buf);
904  break;
905  case NMEndianBig:
906  default:
907  PACKBIG(val, buf);
908  break;
909  }
910  }
911 
912  // 4-byte signed long long
913  else
914  {
915  _NM_CHK_ARCH(val, (size_t)NMFVAL_LEN_S32);
916 
917  uMSB = val & 0x80000000? 0xffffffff: 0x00000000; // extend sign
918  uLSB = (uint_t)val;
919 
920  switch( eEndian )
921  {
922  case NMEndianLittle:
923  PACK64LITTLE(uMSB, uLSB, buf);
924  break;
925  case NMEndianNative:
926  PACK64NATIVE(uMSB, uLSB, buf);
927  break;
928  case NMEndianBig:
929  default:
930  PACK64BIG(uMSB, uLSB, buf);
931  break;
932  }
933  }
934 
935  return (int)fvalLen;
936 }
#define NMFVAL_LEN_S32
signed 32-bit field value length
Definition: netmsgs.h:178
#define _NM_CHK_ARCH(val, size)
Check if host architecture supports basic element sizes.
Definition: nmLibPack.c:724
Most Significant Byte first.
Definition: netmsgs.h:102
#define PACK64BIG(uMSB, uLSB, buf)
Pack two 4-btye unsigned integers into the message buffer in big-endian byte order.
Definition: nmLibPack.c:436
native processor order
Definition: netmsgs.h:104
#define PACKBIG(val, buf)
Pack an element into the message buffer in big-endian byte order.
Definition: nmLibPack.c:397
#define PACKLITTLE(val, buf)
Pack an element into the message buffer in little-endian byte order.
Definition: nmLibPack.c:413
#define PACKNATIVE(val, buf)
Pack an element into the message buffer in native byte order.
Definition: nmLibPack.c:216
#define PACK64LITTLE(uMSB, uLSB, buf)
Pack two 4-btye unsigned integers into the message buffer in little-endian byte order.
Definition: nmLibPack.c:477
#define PACK64NATIVE(uMSB, uLSB, buf)
Pack two 4-btye unsigned integers into the message buffer in native byte order.
Definition: nmLibPack.c:238
Least Significant Byte first.
Definition: netmsgs.h:103
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
#define NMFVAL_LEN_S64
signed 64-bit field value length
Definition: netmsgs.h:180
int nmPackU16 ( ushort_t  val,
byte_t  buf[],
size_t  bufSize,
NMEndian_T  eEndian 
)

Pack an unsigned 16-bit integer into the message buffer.

Parameters
[in]valValue to pack.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
eEndianPacking order. See NMEndian_T.
Returns
On success, returns the number of bytes packed. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 762 of file nmLibPack.c.

References _NM_CHK_MEM, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_U16, PACKBIG, PACKLITTLE, and PACKNATIVE.

Referenced by nmPackFlatU16(), nmPackITVMsg(), nmPackITVMsgDebug(), nmPackITVU16(), nmPackS16(), nmPackShort(), and nmPackUShort().

763 {
764  static size_t fvalLen = NMFVAL_LEN_U16; // message field value length
765 
766  _NM_CHK_MEM(bufSize, fvalLen);
767 
768  switch( eEndian )
769  {
770  case NMEndianLittle:
771  PACKLITTLE(val, buf);
772  break;
773  case NMEndianNative:
774  PACKNATIVE(val, buf);
775  break;
776  case NMEndianBig:
777  default:
778  PACKBIG(val, buf);
779  break;
780  }
781 
782  return (int)fvalLen;
783 }
Most Significant Byte first.
Definition: netmsgs.h:102
native processor order
Definition: netmsgs.h:104
#define PACKBIG(val, buf)
Pack an element into the message buffer in big-endian byte order.
Definition: nmLibPack.c:397
#define PACKLITTLE(val, buf)
Pack an element into the message buffer in little-endian byte order.
Definition: nmLibPack.c:413
#define PACKNATIVE(val, buf)
Pack an element into the message buffer in native byte order.
Definition: nmLibPack.c:216
#define NMFVAL_LEN_U16
unsigned 16-bit field value length
Definition: netmsgs.h:175
Least Significant Byte first.
Definition: netmsgs.h:103
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmPackU32 ( uint_t  val,
byte_t  buf[],
size_t  bufSize,
NMEndian_T  eEndian 
)

Pack an unsigned 32-bit integer into the message buffer.

Parameters
[in]valValue to pack.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
eEndianPacking order. See NMEndian_T.
Returns
On success, returns the number of bytes packed. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 791 of file nmLibPack.c.

References _NM_CHK_MEM, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_U32, PACKBIG, PACKLITTLE, and PACKNATIVE.

Referenced by nmPackFlatU32(), nmPackInt(), nmPackITVU32(), nmPackS32(), nmPackUInt(), and nmPackULong().

792 {
793  static size_t fvalLen = NMFVAL_LEN_U32; // message field value length
794 
795  _NM_CHK_MEM(bufSize, fvalLen);
796 
797  switch( eEndian )
798  {
799  case NMEndianLittle:
800  PACKLITTLE(val, buf);
801  break;
802  case NMEndianNative:
803  PACKNATIVE(val, buf);
804  break;
805  case NMEndianBig:
806  default:
807  PACKBIG(val, buf);
808  break;
809  }
810 
811  return (int)fvalLen;
812 }
Most Significant Byte first.
Definition: netmsgs.h:102
native processor order
Definition: netmsgs.h:104
#define PACKBIG(val, buf)
Pack an element into the message buffer in big-endian byte order.
Definition: nmLibPack.c:397
#define PACKLITTLE(val, buf)
Pack an element into the message buffer in little-endian byte order.
Definition: nmLibPack.c:413
#define PACKNATIVE(val, buf)
Pack an element into the message buffer in native byte order.
Definition: nmLibPack.c:216
#define NMFVAL_LEN_U32
unsigned 32-bit field value length
Definition: netmsgs.h:177
Least Significant Byte first.
Definition: netmsgs.h:103
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmPackU64 ( ulonglong_t  val,
byte_t  buf[],
size_t  bufSize,
NMEndian_T  eEndian 
)

Pack an unsigned 64-bit integer into the message buffer.

If the machine architecture only supports 32-bit long longs, then the 4 MSBs are padded with zero's.

Parameters
[in]valValue to pack.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
eEndianPacking order. See NMEndian_T.
Returns
On success, returns the number of bytes packed. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 823 of file nmLibPack.c.

References _NM_CHK_ARCH, _NM_CHK_MEM, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_U32, NMFVAL_LEN_U64, PACK64BIG, PACK64LITTLE, PACK64NATIVE, PACKBIG, PACKLITTLE, and PACKNATIVE.

Referenced by nmPackFlatU64(), nmPackITVU64(), nmPackULong(), and nmPackULongLong().

824 {
825  static size_t fvalLen = NMFVAL_LEN_U64; // message field value length
826 
827  uint_t uMSB; // most significant bytes
828  uint_t uLSB; // least signifcant bytes
829 
830  _NM_CHK_MEM(bufSize, fvalLen);
831 
832  // 8-byte unsigned long long
833  if( sizeof(ulonglong_t) == fvalLen )
834  {
835  switch( eEndian )
836  {
837  case NMEndianLittle:
838  PACKLITTLE(val, buf);
839  break;
840  case NMEndianNative:
841  PACKNATIVE(val, buf);
842  break;
843  case NMEndianBig:
844  default:
845  PACKBIG(val, buf);
846  break;
847  }
848  }
849 
850  // 4-byte unsigned long long
851  else
852  {
853  _NM_CHK_ARCH(val, (size_t)NMFVAL_LEN_U32);
854 
855  uMSB = 0;
856  uLSB = (uint_t)val;
857 
858  switch( eEndian )
859  {
860  case NMEndianLittle:
861  PACK64LITTLE(uMSB, uLSB, buf);
862  break;
863  case NMEndianNative:
864  PACK64NATIVE(uMSB, uLSB, buf);
865  break;
866  case NMEndianBig:
867  default:
868  PACK64BIG(uMSB, uLSB, buf);
869  break;
870  }
871  }
872 
873  return (int)fvalLen;
874 }
#define NMFVAL_LEN_U64
unsigned 64-bit field value length
Definition: netmsgs.h:179
#define _NM_CHK_ARCH(val, size)
Check if host architecture supports basic element sizes.
Definition: nmLibPack.c:724
Most Significant Byte first.
Definition: netmsgs.h:102
#define PACK64BIG(uMSB, uLSB, buf)
Pack two 4-btye unsigned integers into the message buffer in big-endian byte order.
Definition: nmLibPack.c:436
native processor order
Definition: netmsgs.h:104
#define PACKBIG(val, buf)
Pack an element into the message buffer in big-endian byte order.
Definition: nmLibPack.c:397
#define PACKLITTLE(val, buf)
Pack an element into the message buffer in little-endian byte order.
Definition: nmLibPack.c:413
#define PACKNATIVE(val, buf)
Pack an element into the message buffer in native byte order.
Definition: nmLibPack.c:216
#define PACK64LITTLE(uMSB, uLSB, buf)
Pack two 4-btye unsigned integers into the message buffer in little-endian byte order.
Definition: nmLibPack.c:477
#define PACK64NATIVE(uMSB, uLSB, buf)
Pack two 4-btye unsigned integers into the message buffer in native byte order.
Definition: nmLibPack.c:238
#define NMFVAL_LEN_U32
unsigned 32-bit field value length
Definition: netmsgs.h:177
Least Significant Byte first.
Definition: netmsgs.h:103
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmPackU8 ( byte_t  val,
byte_t  buf[],
size_t  bufSize,
NMEndian_T  eEndian 
)

Pack an unsigned 8-bit byte into the message buffer.

Parameters
[in]valValue to pack.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
eEndianPacking order. See NMEndian_T.
Returns
On success, returns the number of bytes packed. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 745 of file nmLibPack.c.

References _NM_CHK_MEM, and NMFVAL_LEN_U8.

Referenced by nmPackBool(), nmPackByte(), nmPackChar(), nmPackFlatU8(), nmPackITVU8(), nmPackS8(), and nmPackSChar().

746 {
747  static size_t fvalLen = NMFVAL_LEN_U8; // message field value length
748 
749  _NM_CHK_MEM(bufSize, fvalLen);
750 
751  buf[0] = val & 0xff;
752 
753  return (int)fvalLen;
754 }
#define NMFVAL_LEN_U8
unsigned 8-bit field value length
Definition: netmsgs.h:172
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmUnpackBuf ( byte_t  buf[],
size_t  bufSize,
byte_t  bufDst[],
size_t  uCount,
NMEndian_T  eEndian 
)

Unpack a byte buffer from the message buffer.

The source and message buffer memory areas can overlap.

Note
The buffer is always packed in sequential order ignoring the byte order parameter.
Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]bufDstDestination output buffer.
uCountNumber of bytes to copy.
eEndianUnpacking order. See NMEndian_T.
Returns
On success, returns the number of bytes unpacked. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1735 of file nmLibPack.c.

References _NM_CHK_MEM.

Referenced by nmPackPointer().

1740 {
1741  _NM_CHK_MEM(bufSize, uCount);
1742 
1743  // safe copy
1744  memmove(bufDst, buf, uCount);
1745 
1746  return (int)uCount;
1747 }
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmUnpackF32 ( byte_t  buf[],
size_t  bufSize,
float *  pVal,
NMEndian_T  eEndian 
)

Unpack a 32-bit float from the message buffer.

The IEEE 754 32-bit format is used with both the big and little endian byte orders.

Note
On some machines, while integers were represented in little-endian form, floating point numbers were represented in big-endian form. Because there are many floating point formats, and a lack of a standard network representation, no standard for transferring floating point values has been made - Wikipedia.
Todo:
Detect non-IEEE 754 formats and convert to IEEE 754 prior to packing.
Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]pValPointer to value to unpack (set).
eEndianUnpacking order. See NMEndian_T.
Returns
On success, returns the number of bytes unpacked. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1472 of file nmLibPack.c.

References _NM_CHK_ARCH, _NM_CHK_MEM, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_F32, UNPACKBIG, UNPACKLITTLE, and UNPACKNATIVE.

Referenced by nmPackPointer(), nmUnpackFlatF32(), and nmUnpackITVF32().

1473 {
1474  static size_t fvalLen = NMFVAL_LEN_F32; // message field value length
1475 
1476  _NM_CHK_MEM(bufSize, fvalLen);
1477  _NM_CHK_ARCH(*pVal, fvalLen);
1478 
1479  switch( eEndian )
1480  {
1481  case NMEndianLittle:
1482  UNPACKLITTLE(buf, pVal);
1483  break;
1484  case NMEndianNative:
1485  UNPACKNATIVE(buf, pVal);
1486  break;
1487  case NMEndianBig:
1488  default:
1489  UNPACKBIG(buf, pVal);
1490  break;
1491  }
1492 
1493 
1494  return (int)fvalLen;
1495 }
#define UNPACKNATIVE(buf, pval)
Unpack an element from the message buffer in native byte order.
Definition: nmLibPack.c:226
#define _NM_CHK_ARCH(val, size)
Check if host architecture supports basic element sizes.
Definition: nmLibPack.c:724
Most Significant Byte first.
Definition: netmsgs.h:102
native processor order
Definition: netmsgs.h:104
#define NMFVAL_LEN_F32
32-bit floating-point number field val len
Definition: netmsgs.h:181
#define UNPACKBIG(buf, pval)
Unpack an element from the message buffer in big-endian byte order.
Definition: nmLibPack.c:405
#define UNPACKLITTLE(buf, pval)
Unpack an element from the message buffer in little-endian byte order.
Definition: nmLibPack.c:421
Least Significant Byte first.
Definition: netmsgs.h:103
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmUnpackF64 ( byte_t  buf[],
size_t  bufSize,
double *  pVal,
NMEndian_T  eEndian 
)

Unpack a 64-bit float from the message buffer.

The IEEE 754 64-bit format is used with both the big and little endian byte orders. If the machine architecture only supports 32-bit doubles, then mantissa truncation will occur.

Note
On some machines, while integers were represented in little-endian form, floating point numbers were represented in big-endian form. Because there are many floating point formats, and a lack of a standard network representation, no standard for transferring floating point values has been made - Wikipedia.
Todo:
Detect non-IEEE 754 formats and convert to IEEE 754 prior to packing.
Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]pValPointer to value to unpack (set).
eEndianUnpacking order. See NMEndian_T.
Returns
On success, returns the number of bytes unpacked. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1517 of file nmLibPack.c.

References _NM_CHK_ARCH, _NM_CHK_MEM, IEEE754_F32_EXP_MAX, IEEE754_F32_EXP_MIN, IEEE754_F32_F64_MANTISSA0_SHIFT, IEEE754_F32_F64_MANTISSA1_MASK, IEEE754_F32_F64_MANTISSA1_SHIFT, IEEE754_F32_Inf, IEEE754_F32_MANTISSA_MASK, IEEE754_F32_NaN, IEEE754_F32_Zero, IEEE754_F64_EXPONENT_MASK, IEEE754_F64_EXPONENT_SHIFT, ieee754_f64_is_inf(), ieee754_f64_is_nan(), ieee754_f64_is_zero(), IEEE754_F64_MANTISSA0_MASK, IEEE754_F64_MANTISSA0_SHIFT, IEEE754_F64_MANTISSA1_MASK, IEEE754_F64_MANTISSA1_SHIFT, IEEE754_F64_SIGN_MASK, IEEE754_F64_SIGN_SHIFT, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_F32, NMFVAL_LEN_F64, NMLIB_WARNING, UNPACK64BIG, UNPACK64LITTLE, UNPACK64NATIVE, UNPACKBIG, UNPACKLITTLE, and UNPACKNATIVE.

Referenced by nmPackPointer(), nmUnpackFlatF64(), and nmUnpackITVF64().

1518 {
1519  static size_t fvalLen = NMFVAL_LEN_F64; // message field value length
1520 
1521  uint_t uMSB; // Most Signifcant Bytes
1522  uint_t uLSB; // Least Signifcant Bytes
1523  int exponent; // ieee754 unbiased exponent
1524  union ieee754_float f; // 4-byte floating-point number
1525  union ieee754_double d; // 8-byte floating-point number
1526 
1527  _NM_CHK_MEM(bufSize, fvalLen);
1528 
1529  // 8-byte doubles
1530  if( sizeof(double) == (size_t)NMFVAL_LEN_F64 )
1531  {
1532  switch( eEndian )
1533  {
1534  case NMEndianLittle:
1535  UNPACKLITTLE(buf, &d.d);
1536  break;
1537  case NMEndianNative:
1538  UNPACKNATIVE(buf, &d.d);
1539  break;
1540  case NMEndianBig:
1541  default:
1542  UNPACKBIG(buf, &d.d);
1543  break;
1544  }
1545 
1546  *pVal = d.d;
1547  }
1548 
1549  else // 4-byte doubles
1550  {
1551  _NM_CHK_ARCH(*pVal, (size_t)NMFVAL_LEN_F32);
1552 
1553  switch( eEndian )
1554  {
1555  case NMEndianLittle:
1556  UNPACK64LITTLE(buf, &uMSB, &uLSB);
1557  break;
1558  case NMEndianNative:
1559  UNPACK64NATIVE(buf, &uMSB, &uLSB);
1560  break;
1561  case NMEndianBig:
1562  default:
1563  UNPACK64BIG(buf, &uMSB, &uLSB);
1564  break;
1565  }
1566 
1567  d.ieee.negative = ((uMSB >> IEEE754_F64_SIGN_SHIFT)
1568  & IEEE754_F64_SIGN_MASK)? 1: 0;
1569  d.ieee.exponent = (uMSB >> IEEE754_F64_EXPONENT_SHIFT)
1571  d.ieee.mantissa0 = (uMSB >> IEEE754_F64_MANTISSA0_SHIFT)
1573  d.ieee.mantissa1 = (uLSB >> IEEE754_F64_MANTISSA1_SHIFT)
1575 
1576  // unbias the exponent
1577  exponent = (int)d.ieee.exponent - IEEE754_DOUBLE_BIAS;
1578 
1579  // special Not-a-Number case
1580  if( ieee754_f64_is_nan(d) )
1581  {
1582  f = IEEE754_F32_NaN;
1583  }
1584 
1585  // special infinity cases
1586  else if( ieee754_f64_is_inf(d) )
1587  {
1588  f = IEEE754_F32_Inf;
1589  }
1590 
1591  // exponent overflow
1592  else if( exponent > IEEE754_F32_EXP_MAX )
1593  {
1594  f = IEEE754_F32_Inf;
1595  NMLIB_WARNING("floating-point number overflow.");
1596  }
1597 
1598  // exponent overflow
1599  else if( exponent < IEEE754_F32_EXP_MIN )
1600  {
1601  f = IEEE754_F32_Zero;
1602  NMLIB_WARNING("floating-point number underflow.");
1603  }
1604 
1605  // special zero cases
1606  else if( ieee754_f64_is_zero(d) )
1607  {
1608  f = IEEE754_F32_Zero;
1609  }
1610 
1611  // "normal" floating-point numbers
1612  else
1613  {
1614  f.ieee.exponent = (byte_t)(exponent + IEEE754_FLOAT_BIAS);
1615  f.ieee.mantissa = ( (uint_t)((d.ieee.mantissa0
1618  | (uint_t)((d.ieee.mantissa1
1622  if( (d.ieee.mantissa1 & (uint_t)~IEEE754_F32_F64_MANTISSA1_MASK) != 0 )
1623  {
1624  NMLIB_WARNING("floating-point number mantissa truncation.");
1625  }
1626  }
1627 
1628  // preserve the sign
1629  f.ieee.negative = d.ieee.negative;
1630 
1631  *pVal = (double)f.f;
1632  }
1633 
1634  return (int)fvalLen;
1635 }
#define UNPACKNATIVE(buf, pval)
Unpack an element from the message buffer in native byte order.
Definition: nmLibPack.c:226
#define IEEE754_F32_F64_MANTISSA1_SHIFT
lsb mantissa shift
Definition: nmLibPack.c:544
#define IEEE754_F64_MANTISSA0_SHIFT
f64 msb mantissa shift
Definition: nmLibPack.c:535
#define IEEE754_F64_SIGN_SHIFT
f64 sign shift
Definition: nmLibPack.c:531
#define IEEE754_F32_EXP_MAX
maximum power of 2 exponent
Definition: nmLibPack.c:550
#define IEEE754_F64_MANTISSA1_MASK
f64 lsb mantissa mask
Definition: nmLibPack.c:538
#define _NM_CHK_ARCH(val, size)
Check if host architecture supports basic element sizes.
Definition: nmLibPack.c:724
Most Significant Byte first.
Definition: netmsgs.h:102
#define NMFVAL_LEN_F64
64-bit floating-point number field val len
Definition: netmsgs.h:182
#define IEEE754_F64_MANTISSA0_MASK
f64 msb mantissa mask
Definition: nmLibPack.c:536
native processor order
Definition: netmsgs.h:104
#define UNPACK64NATIVE(buf, puMSB, puLSB)
Unpack two 4-byte unsigned integers from the message buffer in native byte order. ...
Definition: nmLibPack.c:250
#define IEEE754_F64_EXPONENT_MASK
f64 exponent mask
Definition: nmLibPack.c:534
#define NMLIB_WARNING(fmt,...)
Log libnetmsgs warning.
Definition: nmLibInternal.h:80
#define NMFVAL_LEN_F32
32-bit floating-point number field val len
Definition: netmsgs.h:181
#define IEEE754_F64_EXPONENT_SHIFT
f64 exponent shift
Definition: nmLibPack.c:533
static bool_t ieee754_f64_is_inf(union ieee754_double d)
Test if 64-bit floating-point number is &plusmn; infinity.
Definition: nmLibPack.c:664
static const union ieee754_float IEEE754_F32_NaN
Definition: nmLibPack.c:566
#define IEEE754_F32_F64_MANTISSA1_MASK
lsb mantissa mask
Definition: nmLibPack.c:545
#define UNPACK64BIG(buf, puMSB, puLSB)
Unpack two 4-byte unsigned integers from the message buffer in big-endian byte order.
Definition: nmLibPack.c:457
#define UNPACK64LITTLE(buf, puMSB, puLSB)
Unpack two 4-byte unsigned integers from the message buffer in little-endian byte order...
Definition: nmLibPack.c:498
#define UNPACKBIG(buf, pval)
Unpack an element from the message buffer in big-endian byte order.
Definition: nmLibPack.c:405
#define IEEE754_F32_MANTISSA_MASK
f32 mantissa mask
Definition: nmLibPack.c:521
#define UNPACKLITTLE(buf, pval)
Unpack an element from the message buffer in little-endian byte order.
Definition: nmLibPack.c:421
Least Significant Byte first.
Definition: netmsgs.h:103
static bool_t ieee754_f64_is_zero(union ieee754_double d)
Test if 64-bit floating-point number is &plusmn; 0.0.
Definition: nmLibPack.c:686
#define IEEE754_F64_MANTISSA1_SHIFT
f64 lsb mantissa shift
Definition: nmLibPack.c:537
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
#define IEEE754_F64_SIGN_MASK
f64 sign mask
Definition: nmLibPack.c:532
static const union ieee754_float IEEE754_F32_Inf
Definition: nmLibPack.c:587
static bool_t ieee754_f64_is_nan(union ieee754_double d)
Test if 64-bit floating-point number is a NaN.
Definition: nmLibPack.c:642
static const union ieee754_float IEEE754_F32_Zero
Definition: nmLibPack.c:608
#define IEEE754_F32_F64_MANTISSA0_SHIFT
msb mantissa shift
Definition: nmLibPack.c:543
#define IEEE754_F32_EXP_MIN
minimum power of 2 exponent
Definition: nmLibPack.c:551
int nmUnpackP32 ( byte_t  buf[],
size_t  bufSize,
void *  pVal,
NMEndian_T  eEndian 
)

Unpack a 32-bit pointer from the message buffer.

If the machine architecture has 64-bit pointers, then only the LSB 4 bytes are packed.

Note
Pointer is always unpacked in native byte order.
Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]pValPointer to value to unpack (set).
eEndianUnpacking order. See NMEndian_T.
Returns
On success, returns the number of bytes unpacked. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1648 of file nmLibPack.c.

References _NM_CHK_MEM, NMFVAL_LEN_P32, and UNPACKNATIVE.

Referenced by nmPackPointer(), nmUnpackFlatP32(), nmUnpackITVP32(), and nmUnpackPointer().

1649 {
1650  static size_t fvalLen = NMFVAL_LEN_P32; // message field value length
1651 
1652  ulong_t *pluVal; // longs are same size as pointers on 32/64 bit machines
1653  uint_t uP32;
1654 
1655  _NM_CHK_MEM(bufSize, fvalLen);
1656 
1657  pluVal = (ulong_t *)pVal;
1658 
1659  UNPACKNATIVE(buf, &uP32);
1660 
1661  *pluVal = (ulong_t)uP32;
1662 
1663  return (int)fvalLen;
1664 
1665 }
#define UNPACKNATIVE(buf, pval)
Unpack an element from the message buffer in native byte order.
Definition: nmLibPack.c:226
#define NMFVAL_LEN_P32
32-bit pointer field value length
Definition: netmsgs.h:183
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmUnpackP64 ( byte_t  buf[],
size_t  bufSize,
void *  pVal,
NMEndian_T  eEndian 
)

Unpack a 64-bit pointer from the message buffer.

If the machine architecture only supports 32-bit pointers, then the 4 MSBs are padded with 0's.

Note
Pointer is always unpacked in native byte order.
Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]pValPointer to value to unpack (set).
eEndianUnpacking order. See NMEndian_T.
Returns
On success, returns the number of bytes unpacked. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1678 of file nmLibPack.c.

References _NM_CHK_ARCH, _NM_CHK_MEM, NMFVAL_LEN_P32, NMFVAL_LEN_P64, NMLIB_WARNING, and UNPACK64NATIVE.

Referenced by nmPackPointer(), nmUnpackFlatP64(), nmUnpackITVP64(), and nmUnpackPointer().

1679 {
1680  static size_t fvalLen = NMFVAL_LEN_P64; // message field value length
1681 
1682  uint_t uMSB; // Most Signifcant Bytes
1683  uint_t uLSB; // Least Signifcant Bytes
1684  ulong_t *pluVal; // longs are same size as pointers on 32/64 bit machines
1685 
1686  _NM_CHK_MEM(bufSize, fvalLen);
1687 
1688  pluVal = (ulong_t *)pVal;
1689 
1690  UNPACK64NATIVE(buf, &uMSB, &uLSB);
1691 
1692  // 8-byte pointer
1693  if( sizeof(void *) == fvalLen )
1694  {
1695 #if HOST_WORD_SIZE == 64
1696  *pluVal = (ulong_t)(((ulong_t)uMSB << 32) | (uLSB & 0xffffffff));
1697 #else
1698  *pluVal = (ulong_t)uLSB;
1699 #endif // HOST_WORD_SIZE
1700  }
1701 
1702  // 4-byte pointer
1703  else
1704  {
1705  _NM_CHK_ARCH(pVal, (size_t)NMFVAL_LEN_P32);
1706 
1707  if( uMSB != 0 )
1708  {
1709  NMLIB_WARNING("0x%08x%08x: Unpacking 64-bit pointer into 32 bits "
1710  "lost non-zero significance.\n",
1711  uMSB, uLSB);
1712  }
1713  *pluVal = (ulong_t)uLSB;
1714  }
1715 
1716  return (int)fvalLen;
1717 }
#define _NM_CHK_ARCH(val, size)
Check if host architecture supports basic element sizes.
Definition: nmLibPack.c:724
#define UNPACK64NATIVE(buf, puMSB, puLSB)
Unpack two 4-byte unsigned integers from the message buffer in native byte order. ...
Definition: nmLibPack.c:250
#define NMLIB_WARNING(fmt,...)
Log libnetmsgs warning.
Definition: nmLibInternal.h:80
#define NMFVAL_LEN_P32
32-bit pointer field value length
Definition: netmsgs.h:183
#define NMFVAL_LEN_P64
64-bit pointer field value length
Definition: netmsgs.h:184
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmUnpackS64 ( byte_t  buf[],
size_t  bufSize,
long long *  pVal,
NMEndian_T  eEndian 
)

Unpack an signed 64-bit integer from the message buffer.

If the machine architecture only supports 32-bit long longs, then the 4 MSBs are truncated.

Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]pValPointer to value to unpack (set).
eEndianUnpacking order. See NMEndian_T.
Returns
On success, returns the number of bytes unpacked. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1391 of file nmLibPack.c.

References _NM_CHK_ARCH, _NM_CHK_MEM, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_S32, NMFVAL_LEN_S64, NMLIB_WARNING, UNPACK64BIG, UNPACK64LITTLE, UNPACK64NATIVE, UNPACKBIG, UNPACKLITTLE, and UNPACKNATIVE.

Referenced by nmPackPointer(), nmUnpackFlatS64(), nmUnpackITVS64(), nmUnpackLong(), and nmUnpackLongLong().

1395 {
1396  static size_t fvalLen = NMFVAL_LEN_S64; // message field value length
1397 
1398  uint_t uMSB; // most significant bytes
1399  uint_t uLSB; // least signifcant bytes
1400 
1401  _NM_CHK_MEM(bufSize, fvalLen);
1402 
1403  // 8-byte unsigned long long
1404  if( sizeof(ulonglong_t) == fvalLen )
1405  {
1406  switch( eEndian )
1407  {
1408  case NMEndianLittle:
1409  UNPACKLITTLE(buf, pVal);
1410  break;
1411  case NMEndianNative:
1412  UNPACKNATIVE(buf, pVal);
1413  break;
1414  case NMEndianBig:
1415  default:
1416  UNPACKBIG(buf, pVal);
1417  break;
1418  }
1419  }
1420 
1421  // 4-byte unsigned long long
1422  else
1423  {
1424  _NM_CHK_ARCH(*pVal, (size_t)NMFVAL_LEN_S32);
1425 
1426  switch( eEndian )
1427  {
1428  case NMEndianLittle:
1429  UNPACK64LITTLE(buf, &uMSB, &uLSB);
1430  break;
1431  case NMEndianNative:
1432  UNPACK64NATIVE(buf, &uMSB, &uLSB);
1433  break;
1434  case NMEndianBig:
1435  default:
1436  UNPACK64BIG(buf, &uMSB, &uLSB);
1437  break;
1438  }
1439 
1440  if( (uMSB != 0) && (uMSB != 0xffffffff) )
1441  {
1442  NMLIB_WARNING("0x%08x%08x: Unpacking signed 64-bit integer into "
1443  "32 bits lost non-zero significance.",
1444  uMSB, uLSB);
1445  }
1446 
1447  *pVal = (long long)uLSB;
1448  }
1449 
1450  return (int)fvalLen;
1451 }
#define UNPACKNATIVE(buf, pval)
Unpack an element from the message buffer in native byte order.
Definition: nmLibPack.c:226
#define NMFVAL_LEN_S32
signed 32-bit field value length
Definition: netmsgs.h:178
#define _NM_CHK_ARCH(val, size)
Check if host architecture supports basic element sizes.
Definition: nmLibPack.c:724
Most Significant Byte first.
Definition: netmsgs.h:102
native processor order
Definition: netmsgs.h:104
#define UNPACK64NATIVE(buf, puMSB, puLSB)
Unpack two 4-byte unsigned integers from the message buffer in native byte order. ...
Definition: nmLibPack.c:250
#define NMLIB_WARNING(fmt,...)
Log libnetmsgs warning.
Definition: nmLibInternal.h:80
#define UNPACK64BIG(buf, puMSB, puLSB)
Unpack two 4-byte unsigned integers from the message buffer in big-endian byte order.
Definition: nmLibPack.c:457
#define UNPACK64LITTLE(buf, puMSB, puLSB)
Unpack two 4-byte unsigned integers from the message buffer in little-endian byte order...
Definition: nmLibPack.c:498
#define UNPACKBIG(buf, pval)
Unpack an element from the message buffer in big-endian byte order.
Definition: nmLibPack.c:405
#define UNPACKLITTLE(buf, pval)
Unpack an element from the message buffer in little-endian byte order.
Definition: nmLibPack.c:421
Least Significant Byte first.
Definition: netmsgs.h:103
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
#define NMFVAL_LEN_S64
signed 64-bit field value length
Definition: netmsgs.h:180
int nmUnpackU16 ( byte_t  buf[],
size_t  bufSize,
ushort_t *  pVal,
NMEndian_T  eEndian 
)

Unpack an unsigned 16-bit integer from the message buffer.

Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]pValPointer to value to unpack (set).
eEndianUnpacking order. See NMEndian_T.
Returns
On success, returns the number of bytes unpacked. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1256 of file nmLibPack.c.

References _NM_CHK_MEM, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_U16, UNPACKBIG, UNPACKLITTLE, and UNPACKNATIVE.

Referenced by nmGetITVMsgId(), nmPackPointer(), nmUnpackFlatU16(), nmUnpackITVMsg(), nmUnpackITVMsgDebug(), nmUnpackITVU16(), nmUnpackS16(), nmUnpackShort(), and nmUnpackUShort().

1260 {
1261  static size_t fvalLen = NMFVAL_LEN_U16; // message field value length
1262 
1263  _NM_CHK_MEM(bufSize, fvalLen);
1264 
1265  switch( eEndian )
1266  {
1267  case NMEndianLittle:
1268  UNPACKLITTLE(buf, pVal);
1269  break;
1270  case NMEndianNative:
1271  UNPACKNATIVE(buf, pVal);
1272  break;
1273  case NMEndianBig:
1274  default:
1275  UNPACKBIG(buf, pVal);
1276  break;
1277  }
1278 
1279  return (int)fvalLen;
1280 }
#define UNPACKNATIVE(buf, pval)
Unpack an element from the message buffer in native byte order.
Definition: nmLibPack.c:226
Most Significant Byte first.
Definition: netmsgs.h:102
native processor order
Definition: netmsgs.h:104
#define NMFVAL_LEN_U16
unsigned 16-bit field value length
Definition: netmsgs.h:175
#define UNPACKBIG(buf, pval)
Unpack an element from the message buffer in big-endian byte order.
Definition: nmLibPack.c:405
#define UNPACKLITTLE(buf, pval)
Unpack an element from the message buffer in little-endian byte order.
Definition: nmLibPack.c:421
Least Significant Byte first.
Definition: netmsgs.h:103
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmUnpackU32 ( byte_t  buf[],
size_t  bufSize,
uint_t *  pVal,
NMEndian_T  eEndian 
)

Unpack an unsigned 32-bit integer from the message buffer.

Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]pValPointer to value to unpack (set).
eEndianUnpacking order. See NMEndian_T.
Returns
On success, returns the number of bytes unpacked. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1288 of file nmLibPack.c.

References _NM_CHK_MEM, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_U32, UNPACKBIG, UNPACKLITTLE, and UNPACKNATIVE.

Referenced by nmPackPointer(), nmUnpackFlatU32(), nmUnpackInt(), nmUnpackITVU32(), nmUnpackS32(), nmUnpackUInt(), and nmUnpackULong().

1289 {
1290  static size_t fvalLen = NMFVAL_LEN_U32; // message field value length
1291 
1292  _NM_CHK_MEM(bufSize, fvalLen);
1293 
1294  switch( eEndian )
1295  {
1296  case NMEndianLittle:
1297  UNPACKLITTLE(buf, pVal);
1298  break;
1299  case NMEndianNative:
1300  UNPACKNATIVE(buf, pVal);
1301  break;
1302  case NMEndianBig:
1303  default:
1304  UNPACKBIG(buf, pVal);
1305  break;
1306  }
1307 
1308  return (int)fvalLen;
1309 }
#define UNPACKNATIVE(buf, pval)
Unpack an element from the message buffer in native byte order.
Definition: nmLibPack.c:226
Most Significant Byte first.
Definition: netmsgs.h:102
native processor order
Definition: netmsgs.h:104
#define NMFVAL_LEN_U32
unsigned 32-bit field value length
Definition: netmsgs.h:177
#define UNPACKBIG(buf, pval)
Unpack an element from the message buffer in big-endian byte order.
Definition: nmLibPack.c:405
#define UNPACKLITTLE(buf, pval)
Unpack an element from the message buffer in little-endian byte order.
Definition: nmLibPack.c:421
Least Significant Byte first.
Definition: netmsgs.h:103
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmUnpackU64 ( byte_t  buf[],
size_t  bufSize,
ulonglong_t *  pVal,
NMEndian_T  eEndian 
)

Unpack an unsigned 64-bit integer from the message buffer.

If the machine architecture only supports 32-bit unsigned long longs, then the 4 MSBs are truncated.

Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]pValPointer to value to unpack (set).
eEndianUnpacking order. See NMEndian_T.
Returns
On success, returns the number of bytes unpacked. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1320 of file nmLibPack.c.

References _NM_CHK_ARCH, _NM_CHK_MEM, NMEndianBig, NMEndianLittle, NMEndianNative, NMFVAL_LEN_U32, NMFVAL_LEN_U64, NMLIB_WARNING, UNPACK64BIG, UNPACK64LITTLE, UNPACK64NATIVE, UNPACKBIG, UNPACKLITTLE, and UNPACKNATIVE.

Referenced by nmPackPointer(), nmUnpackFlatU64(), nmUnpackITVU64(), nmUnpackULong(), and nmUnpackULongLong().

1324 {
1325  static size_t fvalLen = NMFVAL_LEN_U64; // message field value length
1326 
1327  uint_t uMSB; // most significant bytes
1328  uint_t uLSB; // least signifcant bytes
1329 
1330  _NM_CHK_MEM(bufSize, fvalLen);
1331 
1332  // 8-byte unsigned long long
1333  if( sizeof(ulonglong_t) == fvalLen )
1334  {
1335  switch( eEndian )
1336  {
1337  case NMEndianLittle:
1338  UNPACKLITTLE(buf, pVal);
1339  break;
1340  case NMEndianNative:
1341  UNPACKNATIVE(buf, pVal);
1342  break;
1343  case NMEndianBig:
1344  default:
1345  UNPACKBIG(buf, pVal);
1346  break;
1347  }
1348  }
1349 
1350  // 4-byte unsigned long long
1351  else
1352  {
1353  _NM_CHK_ARCH(*pVal, (size_t)NMFVAL_LEN_U32);
1354 
1355  switch( eEndian )
1356  {
1357  case NMEndianLittle:
1358  UNPACK64LITTLE(buf, &uMSB, &uLSB);
1359  break;
1360  case NMEndianNative:
1361  UNPACK64NATIVE(buf, &uMSB, &uLSB);
1362  break;
1363  case NMEndianBig:
1364  default:
1365  UNPACK64BIG(buf, &uMSB, &uLSB);
1366  break;
1367  }
1368 
1369  if( uMSB != 0 )
1370  {
1371  NMLIB_WARNING("0x%08x%08x: Unpacking unsigned 64-bit integer into "
1372  "32 bits lost non-zero significance.",
1373  uMSB, uLSB);
1374  }
1375 
1376  *pVal = (ulonglong_t)uLSB;
1377  }
1378 
1379  return (int)fvalLen;
1380 }
#define NMFVAL_LEN_U64
unsigned 64-bit field value length
Definition: netmsgs.h:179
#define UNPACKNATIVE(buf, pval)
Unpack an element from the message buffer in native byte order.
Definition: nmLibPack.c:226
#define _NM_CHK_ARCH(val, size)
Check if host architecture supports basic element sizes.
Definition: nmLibPack.c:724
Most Significant Byte first.
Definition: netmsgs.h:102
native processor order
Definition: netmsgs.h:104
#define UNPACK64NATIVE(buf, puMSB, puLSB)
Unpack two 4-byte unsigned integers from the message buffer in native byte order. ...
Definition: nmLibPack.c:250
#define NMLIB_WARNING(fmt,...)
Log libnetmsgs warning.
Definition: nmLibInternal.h:80
#define UNPACK64BIG(buf, puMSB, puLSB)
Unpack two 4-byte unsigned integers from the message buffer in big-endian byte order.
Definition: nmLibPack.c:457
#define UNPACK64LITTLE(buf, puMSB, puLSB)
Unpack two 4-byte unsigned integers from the message buffer in little-endian byte order...
Definition: nmLibPack.c:498
#define NMFVAL_LEN_U32
unsigned 32-bit field value length
Definition: netmsgs.h:177
#define UNPACKBIG(buf, pval)
Unpack an element from the message buffer in big-endian byte order.
Definition: nmLibPack.c:405
#define UNPACKLITTLE(buf, pval)
Unpack an element from the message buffer in little-endian byte order.
Definition: nmLibPack.c:421
Least Significant Byte first.
Definition: netmsgs.h:103
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
int nmUnpackU8 ( byte_t  buf[],
size_t  bufSize,
byte_t *  pVal,
NMEndian_T  eEndian 
)

Unpack an unsigned 8-bit byte from the message buffer.

Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]pValPointer to value to unpack (set).
eEndianUnpacking order. See NMEndian_T.
Returns
On success, returns the number of bytes unpacked. On error, the appropriate negated netmsgs error code ( < 0) is returned.

Definition at line 1239 of file nmLibPack.c.

References _NM_CHK_MEM, and NMFVAL_LEN_U8.

Referenced by nmPackPointer(), nmUnpackBool(), nmUnpackByte(), nmUnpackChar(), nmUnpackFlatU8(), nmUnpackITVU8(), nmUnpackS8(), and nmUnpackSChar().

1240 {
1241  static size_t fvalLen = NMFVAL_LEN_U8; // message field value length
1242 
1243  _NM_CHK_MEM(bufSize, fvalLen);
1244 
1245  *pVal = buf[0];
1246 
1247  return (int)fvalLen;
1248 }
#define NMFVAL_LEN_U8
unsigned 8-bit field value length
Definition: netmsgs.h:172
#define _NM_CHK_MEM(size, count)
Check if memory is available.
Definition: nmLibPack.c:705
static void packbig ( byte_t *  p,
size_t  size,
byte_t  buf[] 
)
inlinestatic

Pack an element into the message buffer in big-endian byte order.

Big-Endian Host Version

Parameters
[in]pByte pointer to typecasted element to be packed.
sizeByte size of element to pack.
[out]bufOutput message buffer.

Definition at line 135 of file nmLibPack.c.

136 {
137  size_t n = 0;
138 
139  // forward copy
140  while( n < size )
141  {
142  buf[n++] = *p++;
143  }
144 }
static void packlittle ( byte_t *  p,
size_t  size,
byte_t  buf[] 
)
inlinestatic

Pack an element into the message buffer in little-endian byte order.

Big-Endian Host Version

Parameters
[in]pByte pointer to typecasted element to be packed.
sizeByte size of element to pack.
[out]bufOutput message buffer.

Definition at line 175 of file nmLibPack.c.

176 {
177  size_t n = 0;
178  size_t k = size - 1;
179 
180  // reverse copy
181  while( n < size )
182  {
183  buf[n++] = p[k--];
184  }
185 }
static void unpackbig ( byte_t  buf[],
byte_t *  p,
size_t  size 
)
inlinestatic

Unpack an element from the message buffer in big-endian byte order.

Big-Endian Host Version

Parameters
[in]bufOutput message buffer.
[out]pByte pointer to typecasted element to be unpacked (set).
sizeByte size of element to unpack.

Definition at line 155 of file nmLibPack.c.

156 {
157  size_t n = 0;
158 
159  // forward copy
160  while( n < size )
161  {
162  *p++ = buf[n++];
163  }
164 }
static void unpacklittle ( byte_t  buf[],
byte_t *  p,
size_t  size 
)
inlinestatic

Unpack an element from the message buffer in little-endian byte order.

Big-Endian Host Version

Parameters
[in]bufOutput message buffer.
[out]pByte pointer to typecasted element to be unpacked (set).
sizeByte size of element to unpack.

Definition at line 196 of file nmLibPack.c.

197 {
198  size_t n = 0;
199  size_t k = size - 1;
200 
201  // reverse copy
202  while( n < size )
203  {
204  p[k--] = buf[n++];
205  }
206 }

Variable Documentation

const union ieee754_float IEEE754_F32_Inf
static
Initial value:
=
{
.ieee.negative = 0,
.ieee.exponent = 0x000000ff ,
.ieee.mantissa = 0
}

± Infinity bit pattern for 32-bit floating-point numbers.

Definition at line 587 of file nmLibPack.c.

Referenced by nmUnpackF64().

const union ieee754_float IEEE754_F32_NaN
static
Initial value:
=
{
.ieee.negative = 0,
.ieee.exponent = 0x000000ff ,
.ieee.mantissa = 0x400000,
}

Not-a-Number bit pattern for 32-bit floating-point numbers.

Definition at line 566 of file nmLibPack.c.

Referenced by nmUnpackF64().

const union ieee754_float IEEE754_F32_Zero
static
Initial value:
=
{
.ieee.negative = 0,
.ieee.exponent = 0,
.ieee.mantissa = 0
}

± 0 bit pattern for 32-bit floating-point numbers.

Definition at line 608 of file nmLibPack.c.

Referenced by nmUnpackF64().

const union ieee754_double IEEE754_F64_Inf
static
Initial value:
=
{
.ieee.negative = 0,
.ieee.exponent = 0x000007ff ,
.ieee.mantissa0 = 0,
.ieee.mantissa1 = 0
}

± Infinity bit pattern for 64-bit floating-point numbers.

Definition at line 597 of file nmLibPack.c.

Referenced by nmPackF64().

const union ieee754_double IEEE754_F64_NaN
static
Initial value:
=
{
.ieee.negative = 0,
.ieee.exponent = 0x000007ff ,
.ieee.mantissa0 = 0x80000,
.ieee.mantissa1 = 0
}

Not-a-Number bit pattern for 64-bit floating-point numbers.

Definition at line 576 of file nmLibPack.c.

Referenced by nmPackF64().

const union ieee754_double IEEE754_F64_Zero
static
Initial value:
=
{
.ieee.negative = 0,
.ieee.exponent = 0,
.ieee.mantissa0 = 0,
.ieee.mantissa1 = 0
}

± 0 bit pattern for 64-bit floating-point numbers.

Definition at line 618 of file nmLibPack.c.

Referenced by nmPackF64().