suricata
util-byte.h File Reference
#include <stdint.h>
#include <byteswap.h>
Include dependency graph for util-byte.h:

Go to the source code of this file.

Macros

#define BYTE_BIG_ENDIAN   0
 
#define BYTE_LITTLE_ENDIAN   1
 
#define SCByteSwap16(x)   bswap_16(x)
 
#define SCByteSwap32(x)   bswap_32(x)
 
#define SCByteSwap64(x)   bswap_64(x)
 

Functions

char * BytesToString (const uint8_t *bytes, size_t nbytes)
 Turn byte array into string. More...
 
int WARN_UNUSED ByteExtractUint64 (uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
 
int WARN_UNUSED ByteExtractUint32 (uint32_t *res, int e, uint16_t len, const uint8_t *bytes)
 
int WARN_UNUSED ByteExtractUint16 (uint16_t *res, int e, uint16_t len, const uint8_t *bytes)
 
int WARN_UNUSED ByteExtractString (uint64_t *res, int base, size_t len, const char *str, bool strict)
 
int WARN_UNUSED ByteExtractStringUint64 (uint64_t *res, int base, size_t len, const char *str)
 
int WARN_UNUSED ByteExtractStringUint32 (uint32_t *res, int base, size_t len, const char *str)
 
int WARN_UNUSED ByteExtractStringUint16 (uint16_t *res, int base, size_t len, const char *str)
 
int WARN_UNUSED ByteExtractStringUint8 (uint8_t *res, int base, size_t len, const char *str)
 
int WARN_UNUSED ByteExtractStringSigned (int64_t *res, int base, size_t len, const char *str, bool strict)
 
int WARN_UNUSED ByteExtractStringInt64 (int64_t *res, int base, size_t len, const char *str)
 
int WARN_UNUSED ByteExtractStringInt32 (int32_t *res, int base, size_t len, const char *str)
 
int WARN_UNUSED ByteExtractStringInt16 (int16_t *res, int base, size_t len, const char *str)
 
int WARN_UNUSED ByteExtractStringInt8 (int8_t *res, int base, size_t len, const char *str)
 
int StringParseUint64 (uint64_t *res, int base, size_t len, const char *str)
 
int StringParseUint32 (uint32_t *res, int base, size_t len, const char *str)
 
int StringParseUint16 (uint16_t *res, int base, size_t len, const char *str)
 
int StringParseUint8 (uint8_t *res, int base, size_t len, const char *str)
 
int StringParseInt64 (int64_t *res, int base, size_t len, const char *str)
 
int StringParseInt32 (int32_t *res, int base, size_t len, const char *str)
 
int StringParseInt16 (int16_t *res, int base, size_t len, const char *str)
 
int StringParseInt8 (int8_t *res, int base, size_t len, const char *str)
 
int WARN_UNUSED StringParseU64RangeCheck (uint64_t *res, int base, size_t len, const char *str, uint64_t min, uint64_t max)
 
int WARN_UNUSED StringParseU32RangeCheck (uint32_t *res, int base, size_t len, const char *str, uint32_t min, uint32_t max)
 
int WARN_UNUSED StringParseU16RangeCheck (uint16_t *res, int base, size_t len, const char *str, uint16_t min, uint16_t max)
 
int WARN_UNUSED StringParseU8RangeCheck (uint8_t *res, int base, size_t len, const char *str, uint8_t min, uint8_t max)
 
int WARN_UNUSED StringParseI64RangeCheck (int64_t *res, int base, size_t len, const char *str, int64_t min, int64_t max)
 
int WARN_UNUSED StringParseI32RangeCheck (int32_t *res, int base, size_t len, const char *str, int32_t min, int32_t max)
 
int WARN_UNUSED StringParseI16RangeCheck (int16_t *res, int base, size_t len, const char *str, int16_t min, int16_t max)
 
int WARN_UNUSED StringParseI8RangeCheck (int8_t *res, int base, size_t len, const char *str, int8_t min, int8_t max)
 
void ByteRegisterTests (void)
 
int HexToRaw (const uint8_t *in, size_t ins, uint8_t *out, size_t outs)
 

Detailed Description

Author
Brian Rectanus brect.nosp@m.anu@.nosp@m.gmail.nosp@m..com

Definition in file util-byte.h.

Macro Definition Documentation

◆ BYTE_BIG_ENDIAN

#define BYTE_BIG_ENDIAN   0

Definition at line 30 of file util-byte.h.

◆ BYTE_LITTLE_ENDIAN

#define BYTE_LITTLE_ENDIAN   1

Definition at line 31 of file util-byte.h.

◆ SCByteSwap16

#define SCByteSwap16 (   x)    bswap_16(x)

Wrappers for OS dependent byte swapping functions

Definition at line 70 of file util-byte.h.

◆ SCByteSwap32

#define SCByteSwap32 (   x)    bswap_32(x)

Definition at line 71 of file util-byte.h.

◆ SCByteSwap64

#define SCByteSwap64 (   x)    bswap_64(x)

Definition at line 72 of file util-byte.h.

Function Documentation

◆ ByteExtractString()

int WARN_UNUSED ByteExtractString ( uint64_t *  res,
int  base,
size_t  len,
const char *  str,
bool  strict 
)

Extract unsigned integer value from a string.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
boolEnable strict check for parsers
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 141 of file util-byte.c.

◆ ByteExtractStringInt16()

int WARN_UNUSED ByteExtractStringInt16 ( int16_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract signed integer value from a string as uint16_t.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 532 of file util-byte.c.

◆ ByteExtractStringInt32()

int WARN_UNUSED ByteExtractStringInt32 ( int32_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract signed integer value from a string as uint32_t.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 507 of file util-byte.c.

◆ ByteExtractStringInt64()

int WARN_UNUSED ByteExtractStringInt64 ( int64_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract signed integer value from a string as uint64_t.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 502 of file util-byte.c.

◆ ByteExtractStringInt8()

int WARN_UNUSED ByteExtractStringInt8 ( int8_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract signed integer value from a string as uint8_t.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 557 of file util-byte.c.

◆ ByteExtractStringSigned()

int WARN_UNUSED ByteExtractStringSigned ( int64_t *  res,
int  base,
size_t  len,
const char *  str,
bool  strict 
)

Extract signed integer value from a string.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
boolEnable strict check for parsers
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 456 of file util-byte.c.

◆ ByteExtractStringUint16()

int WARN_UNUSED ByteExtractStringUint16 ( uint16_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract unsigned integer value from a string as uint16_t.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 218 of file util-byte.c.

◆ ByteExtractStringUint32()

int WARN_UNUSED ByteExtractStringUint32 ( uint32_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract unsigned integer value from a string as uint32_t.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 195 of file util-byte.c.

Referenced by SCGetGroupID(), and SCGetUserID().

Here is the caller graph for this function:

◆ ByteExtractStringUint64()

int WARN_UNUSED ByteExtractStringUint64 ( uint64_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract unsigned integer value from a string as uint64_t.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 190 of file util-byte.c.

Referenced by DetectByteExtractDoMatch().

Here is the caller graph for this function:

◆ ByteExtractStringUint8()

int WARN_UNUSED ByteExtractStringUint8 ( uint8_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract unsigned integer value from a string as uint8_t.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 241 of file util-byte.c.

◆ ByteExtractUint16()

int WARN_UNUSED ByteExtractUint16 ( uint16_t *  res,
int  e,
uint16_t  len,
const uint8_t *  bytes 
)

Extract bytes from a byte string and convert to a unint16_t.

Parameters
resStores result
eEndianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
lenNumber of bytes to extract (8 max)
bytesData to extract from
Returns
n Number of bytes extracted on success
-1 On error
Todo:
Need standard return values

Definition at line 120 of file util-byte.c.

References len.

◆ ByteExtractUint32()

int WARN_UNUSED ByteExtractUint32 ( uint32_t *  res,
int  e,
uint16_t  len,
const uint8_t *  bytes 
)

Extract bytes from a byte string and convert to a uint32_t.

Parameters
resStores result
eEndianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
lenNumber of bytes to extract (8 max)
bytesData to extract from
Returns
n Number of bytes extracted on success
-1 On error
Return values
Greaterthan 0 if successful, 0 or negative on failure.
Todo:
Need standard return values

Definition at line 99 of file util-byte.c.

References len.

◆ ByteExtractUint64()

int WARN_UNUSED ByteExtractUint64 ( uint64_t *  res,
int  e,
uint16_t  len,
const uint8_t *  bytes 
)

Extract bytes from a byte string and convert to a unint64_t.

Parameters
resStores result
eEndianness (BYTE_BIG_ENDIAN or BYTE_LITTLE_ENDIAN)
lenNumber of bytes to extract (8 max)
bytesData to extract from
Returns
n Number of bytes extracted on success
-1 On error
Todo:
Need standard return values

Definition at line 75 of file util-byte.c.

References len.

Referenced by DetectByteExtractDoMatch().

Here is the caller graph for this function:

◆ ByteRegisterTests()

void ByteRegisterTests ( void  )

Definition at line 1029 of file util-byte.c.

References UtRegisterTest().

Here is the call graph for this function:

◆ BytesToString()

char* BytesToString ( const uint8_t *  bytes,
size_t  nbytes 
)

Turn byte array into string.

All non-printables are copied over, except for '\0', which is turned into literal \0 in the string.

Parameters
bytesbyte array
nbytesnumber of bytes
Returns
string nul-terminated string or NULL on error

Definition at line 41 of file util-byte.c.

References dst, and SCCalloc.

◆ HexToRaw()

int HexToRaw ( const uint8_t *  in,
size_t  ins,
uint8_t *  out,
size_t  outs 
)

Definition at line 771 of file util-byte.c.

References SCLogError.

◆ StringParseI16RangeCheck()

int WARN_UNUSED StringParseI16RangeCheck ( int16_t *  res,
int  base,
size_t  len,
const char *  str,
int16_t  min,
int16_t  max 
)

Extract signed integer value from a string as int16_t strictly within the range.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 711 of file util-byte.c.

◆ StringParseI32RangeCheck()

int WARN_UNUSED StringParseI32RangeCheck ( int32_t *  res,
int  base,
size_t  len,
const char *  str,
int32_t  min,
int32_t  max 
)

Extract signed integer value from a string as int32_t strictly within the range.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 681 of file util-byte.c.

◆ StringParseI64RangeCheck()

int WARN_UNUSED StringParseI64RangeCheck ( int64_t *  res,
int  base,
size_t  len,
const char *  str,
int64_t  min,
int64_t  max 
)

Extract signed integer value from a string as int64_t strictly within the range.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 662 of file util-byte.c.

◆ StringParseI8RangeCheck()

int WARN_UNUSED StringParseI8RangeCheck ( int8_t *  res,
int  base,
size_t  len,
const char *  str,
int8_t  min,
int8_t  max 
)

Extract signed integer value from a string as int8_t strictly within the range.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 741 of file util-byte.c.

◆ StringParseInt16()

int StringParseInt16 ( int16_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract signed integer value from a string as int16_t strictly.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 612 of file util-byte.c.

◆ StringParseInt32()

int StringParseInt32 ( int32_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract signed integer value from a string as int32_t strictly.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 587 of file util-byte.c.

Referenced by SCKernelVersionIsAtLeast().

Here is the caller graph for this function:

◆ StringParseInt64()

int StringParseInt64 ( int64_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract signed integer value from a string as int64_t strictly.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 582 of file util-byte.c.

Referenced by UtilCpuGetNumProcessorsConfigured().

Here is the caller graph for this function:

◆ StringParseInt8()

int StringParseInt8 ( int8_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract signed integer value from a string as int8_t strictly.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 637 of file util-byte.c.

◆ StringParseU16RangeCheck()

int WARN_UNUSED StringParseU16RangeCheck ( uint16_t *  res,
int  base,
size_t  len,
const char *  str,
uint16_t  min,
uint16_t  max 
)

Extract unsigned integer value from a string as uint16_t strictly within the range.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 395 of file util-byte.c.

◆ StringParseU32RangeCheck()

int WARN_UNUSED StringParseU32RangeCheck ( uint32_t *  res,
int  base,
size_t  len,
const char *  str,
uint32_t  min,
uint32_t  max 
)

Extract unsigned integer value from a string as uint32_t strictly within the range.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 366 of file util-byte.c.

◆ StringParseU64RangeCheck()

int WARN_UNUSED StringParseU64RangeCheck ( uint64_t *  res,
int  base,
size_t  len,
const char *  str,
uint64_t  min,
uint64_t  max 
)

Extract unsigned integer value from a string as uint64_t strictly within the range.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 347 of file util-byte.c.

◆ StringParseU8RangeCheck()

int WARN_UNUSED StringParseU8RangeCheck ( uint8_t *  res,
int  base,
size_t  len,
const char *  str,
uint8_t  min,
uint8_t  max 
)

Extract unsigned integer value from a string as uint8_t strictly within the range.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error
Return values
Greaterthan 0 if successful, 0 or negative on failure.

Definition at line 427 of file util-byte.c.

Referenced by SCRadix4AddKeyIPV4String(), and SCRadix6AddKeyIPV6String().

Here is the caller graph for this function:

◆ StringParseUint16()

int StringParseUint16 ( uint16_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract unsigned integer value from a string as uint16_t strictly.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error
Return values
Greaterthan 0 if successful, 0 or negative on failure.

Definition at line 296 of file util-byte.c.

Referenced by IPFWRegisterQueue(), and PostConfLoadedSetup().

Here is the caller graph for this function:

◆ StringParseUint32()

int StringParseUint32 ( uint32_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract unsigned integer value from a string as uint32_t strictly.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 269 of file util-byte.c.

Referenced by BuildCpusetWithCallback(), FlowInitConfig(), FlowInitFlowProto(), HttpRangeContainersInit(), and SCConfLogOpenGeneric().

Here is the caller graph for this function:

◆ StringParseUint64()

int StringParseUint64 ( uint64_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract unsigned integer value from a string as uint64_t strictly.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error

Definition at line 264 of file util-byte.c.

◆ StringParseUint8()

int StringParseUint8 ( uint8_t *  res,
int  base,
size_t  len,
const char *  str 
)

Extract unsigned integer value from a string as uint8_t strictly.

Parameters
resStores result
baseBase of the number to extract
lenNumber of bytes to extract (23 max or 0 for unbounded)
strString to extract from
Returns
n Number of bytes extracted on success
-1 On error
Return values
Greaterthan 0 if successful, 0 or negative on failure.

Definition at line 323 of file util-byte.c.

Referenced by IPv4AddressStringIsValid().

Here is the caller graph for this function: