suricata
util-byte.c File Reference
#include "suricata-common.h"
#include "util-byte.h"
#include "util-unittest.h"
#include "util-debug.h"
#include "util-validate.h"
Include dependency graph for util-byte.c:

Go to the source code of this file.

Functions

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

Detailed Description

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

Byte utility functions

Definition in file util-byte.c.

Function Documentation

◆ ByteExtractString()

int ByteExtractString ( uint64_t *  res,
int  base,
uint16_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 185 of file util-byte.c.

References len, res, SC_ERR_INVALID_NUMERIC_VALUE, SCLogDebug, SCLogError, and str.

Referenced by ByteExtractStringUint16(), ByteExtractStringUint32(), ByteExtractStringUint64(), ByteExtractStringUint8(), StringParseU16RangeCheck(), StringParseU32RangeCheck(), StringParseU64RangeCheck(), StringParseU8RangeCheck(), StringParseUint16(), StringParseUint32(), StringParseUint64(), and StringParseUint8().

Here is the caller graph for this function:

◆ ByteExtractStringInt16()

int ByteExtractStringInt16 ( int16_t *  res,
int  base,
uint16_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 560 of file util-byte.c.

References ByteExtractStringSigned(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Here is the call graph for this function:

◆ ByteExtractStringInt32()

int ByteExtractStringInt32 ( int32_t *  res,
int  base,
uint16_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 536 of file util-byte.c.

References ByteExtractStringSigned(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Here is the call graph for this function:

◆ ByteExtractStringInt64()

int ByteExtractStringInt64 ( int64_t *  res,
int  base,
uint16_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 531 of file util-byte.c.

References ByteExtractStringSigned(), len, res, and str.

Here is the call graph for this function:

◆ ByteExtractStringInt8()

int ByteExtractStringInt8 ( int8_t *  res,
int  base,
uint16_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 584 of file util-byte.c.

References ByteExtractStringSigned(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Here is the call graph for this function:

◆ ByteExtractStringSigned()

int ByteExtractStringSigned ( int64_t *  res,
int  base,
uint16_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 485 of file util-byte.c.

References len, res, SC_ERR_ARG_LEN_LONG, SC_ERR_INVALID_NUMERIC_VALUE, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Referenced by ByteExtractStringInt16(), ByteExtractStringInt32(), ByteExtractStringInt64(), ByteExtractStringInt8(), StringParseI16RangeCheck(), StringParseI32RangeCheck(), StringParseI64RangeCheck(), StringParseI8RangeCheck(), StringParseInt16(), StringParseInt32(), StringParseInt64(), and StringParseInt8().

Here is the caller graph for this function:

◆ ByteExtractStringUint16()

int ByteExtractStringUint16 ( uint16_t *  res,
int  base,
uint16_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 262 of file util-byte.c.

References ByteExtractString(), len, res, SCLogDebug, and str.

Here is the call graph for this function:

◆ ByteExtractStringUint32()

int ByteExtractStringUint32 ( uint32_t *  res,
int  base,
uint16_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 239 of file util-byte.c.

References ByteExtractString(), len, res, SCLogDebug, and str.

Referenced by SCGetGroupID(), and SCGetUserID().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ByteExtractStringUint64()

int ByteExtractStringUint64 ( uint64_t *  res,
int  base,
uint16_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 234 of file util-byte.c.

References ByteExtractString(), len, res, and str.

Referenced by DetectByteExtractDoMatch(), DetectBytejumpDoMatch(), DetectByteMathDoMatch(), DetectBytetestDoMatch(), and LLVMFuzzerTestOneInput().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ByteExtractStringUint8()

int ByteExtractStringUint8 ( uint8_t *  res,
int  base,
uint16_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 285 of file util-byte.c.

References ByteExtractString(), len, res, SCLogDebug, and str.

Referenced by DetectProtoParse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ByteExtractUint16()

int 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 164 of file util-byte.c.

References len.

Referenced by DecodeCIPRequestMSPPDU(), and DecodeCIPResponseMSPPDU().

Here is the caller graph for this function:

◆ ByteExtractUint32()

int 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
Todo:
Need standard return values

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

References len.

◆ ByteExtractUint64()

int 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 122 of file util-byte.c.

References len.

Referenced by DetectByteExtractDoMatch(), DetectBytejumpDoMatch(), DetectByteMathDoMatch(), and DetectBytetestDoMatch().

Here is the caller graph for this function:

◆ ByteRegisterTests()

void ByteRegisterTests ( void  )

Definition at line 1019 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.

◆ BytesToStringBuffer()

void BytesToStringBuffer ( const uint8_t *  bytes,
size_t  nbytes,
char *  outstr,
size_t  outlen 
)

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
outstr[out]buffer to fill
outlensize of outstr. Must be at least 2 * nbytes + 1 in size

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

References DEBUG_VALIDATE_BUG_ON, dst, and strlcpy().

Here is the call graph for this function:

◆ StringParseI16RangeCheck()

int StringParseI16RangeCheck ( int16_t *  res,
int  base,
uint16_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 733 of file util-byte.c.

References ByteExtractStringSigned(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Here is the call graph for this function:

◆ StringParseI32RangeCheck()

int StringParseI32RangeCheck ( int32_t *  res,
int  base,
uint16_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 704 of file util-byte.c.

References ByteExtractStringSigned(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Here is the call graph for this function:

◆ StringParseI64RangeCheck()

int StringParseI64RangeCheck ( int64_t *  res,
int  base,
uint16_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 685 of file util-byte.c.

References ByteExtractStringSigned(), len, res, and str.

Here is the call graph for this function:

◆ StringParseI8RangeCheck()

int StringParseI8RangeCheck ( int8_t *  res,
int  base,
uint16_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 762 of file util-byte.c.

References ByteExtractStringSigned(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Here is the call graph for this function:

◆ StringParseInt16()

int StringParseInt16 ( int16_t *  res,
int  base,
uint16_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 637 of file util-byte.c.

References ByteExtractStringSigned(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Here is the call graph for this function:

◆ StringParseInt32()

int StringParseInt32 ( int32_t *  res,
int  base,
uint16_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 613 of file util-byte.c.

References ByteExtractStringSigned(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Here is the call graph for this function:

◆ StringParseInt64()

int StringParseInt64 ( int64_t *  res,
int  base,
uint16_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 608 of file util-byte.c.

References ByteExtractStringSigned(), len, res, and str.

Referenced by UtilCpuGetNumProcessorsConfigured().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StringParseInt8()

int StringParseInt8 ( int8_t *  res,
int  base,
uint16_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 661 of file util-byte.c.

References ByteExtractStringSigned(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Here is the call graph for this function:

◆ StringParseU16RangeCheck()

int StringParseU16RangeCheck ( uint16_t *  res,
int  base,
uint16_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 429 of file util-byte.c.

References ByteExtractString(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Here is the call graph for this function:

◆ StringParseU32RangeCheck()

int StringParseU32RangeCheck ( uint32_t *  res,
int  base,
uint16_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 401 of file util-byte.c.

References ByteExtractString(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Here is the call graph for this function:

◆ StringParseU64RangeCheck()

int StringParseU64RangeCheck ( uint64_t *  res,
int  base,
uint16_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 382 of file util-byte.c.

References ByteExtractString(), len, res, and str.

Here is the call graph for this function:

◆ StringParseU8RangeCheck()

int StringParseU8RangeCheck ( uint8_t *  res,
int  base,
uint16_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

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

References ByteExtractString(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Referenced by SCRadixAddKeyIPV4String(), and SCRadixAddKeyIPV6String().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StringParseUint16()

int StringParseUint16 ( uint16_t *  res,
int  base,
uint16_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

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

References ByteExtractString(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Referenced by IPFWRegisterQueue(), and PostConfLoadedSetup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StringParseUint32()

int StringParseUint32 ( uint32_t *  res,
int  base,
uint16_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 313 of file util-byte.c.

References ByteExtractString(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Referenced by FlowInitConfig(), FlowInitFlowProto(), and SCProfilingRulesGlobalInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StringParseUint64()

int StringParseUint64 ( uint64_t *  res,
int  base,
uint16_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 308 of file util-byte.c.

References ByteExtractString(), len, res, and str.

Here is the call graph for this function:

◆ StringParseUint8()

int StringParseUint8 ( uint8_t *  res,
int  base,
uint16_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

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

References ByteExtractString(), len, res, SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Referenced by IPv4AddressStringIsValid().

Here is the call graph for this function:
Here is the caller graph for this function: