Lucciefr
Lua code caving, injection and exploration framework
Macros | Functions
strutils.c File Reference

string utilities, low-level string operations More...

#include "strutils.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

Macros

#define DEFAULT_ASCII_MINLEN   2
 default value for minimum string length that is_ascii() and is_utf16() will use if you pass a negative min_len
 

Functions

bool strew (const char *str, const char *what)
 string ends with
 
bool wstrew (const wchar_t *str, const wchar_t *what)
 wide string ends with
 
bool strsw (const char *str, const char *what)
 string starts with
 
bool wstrsw_ic (const wchar_t *str, const wchar_t *what)
 wide string starts with
 
bool streq (const char *str, const char *what)
 string equals
 
bool streq_ic (const char *str, const char *what)
 case-insensitive string equals
 
const char * strstr_ic (const char *haystack, const char *needle)
 case-insensitive substring match
 
int rtrim_len (const char *s, size_t *len)
 A helper function for "right trim" (removal of trailing whitepace). More...
 
int ltrim_ofs (const char *s, size_t *len)
 A helper function for "left trim" (removal of leading whitepace). More...
 
int strcmp_safe (const char *a, const char *b)
 "Lua-safe" string comparison (strcmp wrapper), protects against NULL pointers
 
int strcasecmp_safe (const char *a, const char *b)
 "Lua-safe" string comparison, ignores case (strcasecmp wrapper)
 
char * repeat_char (const char c, int n)
 construct a string that repeats a char n times. More...
 
char * repeat_string (const char *str, int n)
 construct a string that repeats another string n times. More...
 
bool is_ascii (unsigned char *data, int min_len)
 test for valid strings (consisting only of ASCII chars) with a given minimum length More...
 
bool is_utf16 (wchar_t *data, int min_len)
 test for valid wide strings (consisting only of ASCII wide chars) More...
 
int hextoi (const char *str)
 A primitive version of "hex to integer", similar to strtol(str, NULL, 16). More...
 
int32_t hash_str_djb (const char *key, size_t length, size_t step)
 DJB string hash function. More...
 
int32_t hash_wstr_djb (const wchar_t *key, size_t length, size_t step)
 DJB hash function for wide strings.
 
int32_t hash_str_sdbm (const char *key, size_t length, size_t step)
 sdbm string hash function. More...
 
int32_t hash_wstr_sdbm (const wchar_t *key, size_t length, size_t step)
 sdbm hash function for wide strings
 
int32_t hash_str_elf (const char *key, size_t length, size_t step)
 ELF hash. More...
 
int32_t hash_wstr_elf (const wchar_t *key, size_t length, size_t step)
 ELF hash function for wide strings. More...
 
int32_t hash_str_mulAdd (int32_t init, int32_t factor, const char *key, size_t length, size_t step)
 multiplicative string hash functions, using "add" operation
 
int32_t hash_wstr_mulAdd (int32_t init, int32_t factor, const wchar_t *key, size_t length, size_t step)
 multiplicative wide string hash functions, using "add" operation
 
int32_t hash_str_mulXor (int32_t init, int32_t factor, const char *key, size_t length, size_t step)
 multiplicative string hash functions, using "xor" operation
 
int32_t hash_wstr_mulXor (int32_t init, int32_t factor, const wchar_t *key, size_t length, size_t step)
 multiplicative wide string hash functions, using "xor" operation
 
int32_t hash_str_xorMul (int32_t init, int32_t factor, const char *key, size_t length, size_t step)
 string hash functions, using (FNV-1a style) "xor before mul"
 
int32_t hash_wstr_xorMul (int32_t init, int32_t factor, const wchar_t *key, size_t length, size_t step)
 wide string hash functions, using (FNV-1a style) "xor before mul"
 

String formatting

General-purpose "print to string" routines.

vformatmsg() and formatmsg() are "printf-style" functions that return a formatted message string.

Note
The string gets allocated via realloc(), and you are responsible for calling free() on it later!

Their low-level counterparts vformatmsg_len() and formatmsg_len() return the resulting string length (excluding the terminating NUL) instead, and therefore require an additional char** result pointer.

#define DEFAULT_FORMATMSG_SIZE   512
 default allocation size for string formatting
 
int vformatmsg_len (char **msg, const char *fmt, va_list ap)
 Function to create a string according to format string fmt while using arguments from vararg list ap. More...
 
char * vformatmsg (const char *fmt, va_list ap)
 Return a string according to format string fmt, using arguments from vararg list ap. More...
 
int formatmsg_len (char **msg, const char *fmt,...)
 Vararg wrapper for vformatmsg_len(). More...
 
char * formatmsg (const char *fmt,...)
 Vararg wrapper for vformatmsg(). Returns formatted string.
 

Detailed Description

string utilities, low-level string operations

Note
*_ic functions are case-insensitive ("ignore case")

Function Documentation

int formatmsg_len ( char **  msg,
const char *  fmt,
  ... 
)

Vararg wrapper for vformatmsg_len().

Assigns to *msg, and returns resulting string length.

int32_t hash_str_djb ( const char *  key,
size_t  length,
size_t  step 
)
int32_t hash_str_elf ( const char *  key,
size_t  length,
size_t  step 
)

ELF hash.

The published hash algorithm used in the UNIX ELF format for object files.

It's basically a PJW hash variant (see e.g. [1], pp. 434-438)

int32_t hash_str_sdbm ( const char *  key,
size_t  length,
size_t  step 
)

sdbm string hash function.

see e.g. http://www.cse.yorku.ca/~oz/hash.html#sdbm

int32_t hash_wstr_elf ( const wchar_t *  key,
size_t  length,
size_t  step 
)

ELF hash function for wide strings.

See also
hash_str_elf()
int hextoi ( const char *  str)

A primitive version of "hex to integer", similar to strtol(str, NULL, 16).

(This function simply ignores any non-hexadecimal chars.)

bool is_ascii ( unsigned char *  data,
int  min_len 
)

test for valid strings (consisting only of ASCII chars) with a given minimum length

Parameters
data(string) data to test
min_lenminimum length required. if you pass min_len < 0, uses DEFAULT_ASCII_MINLEN instead
Returns
false if the string interpretation of data contains invalid chars or has insufficient length, true otherwise
bool is_utf16 ( wchar_t *  data,
int  min_len 
)

test for valid wide strings (consisting only of ASCII wide chars)

See also
is_ascii()
int ltrim_ofs ( const char *  s,
size_t *  len 
)

A helper function for "left trim" (removal of leading whitepace).

The function returns an offset into the string that would skip the leading chars. len is optional input specifying the string length - if NULL, strlen(s) is used.

See also
rtrim_len()
char* repeat_char ( const char  c,
int  n 
)

construct a string that repeats a char n times.

(The string gets calloc()ed, you are responsible for calling free() on the result later.)

char* repeat_string ( const char *  str,
int  n 
)

construct a string that repeats another string n times.

(The string gets calloc()ed, you are responsible for calling free() on the result later.)

int rtrim_len ( const char *  s,
size_t *  len 
)

A helper function for "right trim" (removal of trailing whitepace).

The function returns the new length of the string without the trailing chars. len is optional input specifying the string length - if NULL, strlen(s) is used.

See also
ltrim_ofs()
char* vformatmsg ( const char *  fmt,
va_list  ap 
)

Return a string according to format string fmt, using arguments from vararg list ap.

int vformatmsg_len ( char **  msg,
const char *  fmt,
va_list  ap 
)

Function to create a string according to format string fmt while using arguments from vararg list ap.

Assigns to *msg accordingly, and returns resulting string length.