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)
 
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 ByteExtractStringSigned (int64_t *res, int base, uint16_t len, const char *str)
 
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)
 
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

int ByteExtractString ( uint64_t *  res,
int  base,
uint16_t  len,
const char *  str 
)

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
Returns
n Number of bytes extracted on success
-1 On error

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

References len, SCLogDebug, and str.

Referenced by ByteExtractStringUint16(), ByteExtractStringUint32(), ByteExtractStringUint64(), and ByteExtractStringUint8().

Here is the caller graph for this function:

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

References ByteExtractStringSigned(), SC_ERR_NUMERIC_VALUE_ERANGE, and SCLogError.

Referenced by ByteExtractStringInt8().

Here is the call graph for this function:

Here is the caller graph for this function:

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

References ByteExtractStringSigned(), SC_ERR_NUMERIC_VALUE_ERANGE, and SCLogError.

Referenced by ByteExtractStringInt8(), DetectAsn1Register(), DetectBytejumpDoMatch(), DetectBytetestDoMatch(), DetectDistanceRegister(), and DetectWithinRegister().

Here is the call graph for this function:

Here is the caller graph for this function:

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

References ByteExtractStringSigned().

Referenced by ByteExtractStringInt8().

Here is the call graph for this function:

Here is the caller graph for this function:

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

References BYTE_BIG_ENDIAN, BYTE_LITTLE_ENDIAN, ByteExtractStringInt16(), ByteExtractStringInt32(), ByteExtractStringInt64(), ByteExtractStringInt8(), ByteExtractStringSigned(), ByteExtractStringUint16(), ByteExtractStringUint32(), ByteExtractStringUint64(), ByteExtractStringUint8(), ByteExtractUint16(), ByteExtractUint32(), ByteExtractUint64(), SC_ERR_NUMERIC_VALUE_ERANGE, SCLogError, and str.

Referenced by ByteExtractStringInt8().

Here is the call graph for this function:

Here is the caller graph for this function:

int ByteExtractStringSigned ( int64_t *  res,
int  base,
uint16_t  len,
const char *  str 
)

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
Returns
n Number of bytes extracted on success
-1 On error

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

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

Referenced by ByteExtractStringInt16(), ByteExtractStringInt32(), ByteExtractStringInt64(), ByteExtractStringInt8(), and DetectTosRegister().

Here is the caller graph for this function:

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

References ByteExtractString(), and SCLogDebug.

Referenced by ByteExtractStringInt8(), DetectDatarepBufferMatch(), DetectDepthRegister(), DetectDsizeRegister(), DetectEngineCtxFree(), DetectEngineReloadTenantBlocking(), DetectFragOffsetRegister(), DetectIcmpIdRegister(), DetectIcmpSeqRegister(), DetectIsdataatRegister(), DetectOffsetRegister(), DetectUrilenRegister(), DetectWindowRegister(), IPFWRegisterQueue(), OutputJsonInitCtx(), and PostRunDeinit().

Here is the call graph for this function:

Here is the caller graph for this function:

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

References ByteExtractString(), and SCLogDebug.

Referenced by ByteExtractStringInt8(), DefragInitConfig(), DetectAckRegister(), DetectAsn1Register(), DetectBase64DecodeDoMatch(), DetectBytejumpDoMatch(), DetectBytetestDoMatch(), DetectDetectionFilterRegister(), DetectEngineMultiTenantSetup(), DetectEngineReloadTenantBlocking(), DetectRpcRegister(), DetectSeqRegister(), DetectTagRegister(), DetectThresholdRegister(), FlowInitConfig(), FlowInitFlowProto(), HostInitConfig(), IPPairInitConfig(), SCConfLogOpenGeneric(), SCProfilingRulesGlobalInit(), SCThresholdConfInitContext(), StreamTcpAppLayerIsDisabled(), and Unified2AlertInitCtx().

Here is the call graph for this function:

Here is the caller graph for this function:

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)
lenNumber of bytes to extract (23 max)
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().

Referenced by ByteExtractStringInt8(), DetectByteExtractDoMatch(), DetectBytejumpDoMatch(), DetectBytetestDoMatch(), and OutputJsonInitCtx().

Here is the call graph for this function:

Here is the caller graph for this function:

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

References ByteExtractString(), and SCLogDebug.

Referenced by ByteExtractStringInt8(), DetectIPProtoRegister(), and DetectProtoParse().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Referenced by ByteExtractStringInt8(), DecodeCIPRequestMSPPDU(), and DecodeCIPResponseMSPPDU().

Here is the caller graph for this function:

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

Extract bytes from a byte string and convert to a unint32_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.

Referenced by ByteExtractStringInt8().

Here is the caller graph for this function:

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.

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

Here is the caller graph for this function:

void ByteRegisterTests ( void  )

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

References UtRegisterTest().

Here is the call graph for this function:

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.

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().

Referenced by JsonBuildFileInfoRecord(), and JsonFTPDataAddMetadata().

Here is the call graph for this function:

Here is the caller graph for this function: