tor  0.4.2.0-alpha-dev
Macros | Functions
util_string.c File Reference
#include "lib/string/util_string.h"
#include "lib/string/compat_ctype.h"
#include "lib/err/torerr.h"
#include "lib/ctime/di_ops.h"
#include "lib/defs/digest_sizes.h"
#include <string.h>
#include <stdlib.h>

Go to the source code of this file.

Macros

#define TOP_BITS(x)   ((uint8_t)(0xFF << (8 - (x))))
 
#define LOW_BITS(x)   ((uint8_t)(0xFF >> (8 - (x))))
 

Functions

const void * tor_memmem (const void *_haystack, size_t hlen, const void *_needle, size_t nlen)
 
const void * tor_memstr (const void *haystack, size_t hlen, const char *needle)
 
int fast_mem_is_zero (const char *mem, size_t len)
 
int tor_digest_is_zero (const char *digest)
 
int tor_digest256_is_zero (const char *digest)
 
void tor_strstrip (char *s, const char *strip)
 
void tor_strlower (char *s)
 
void tor_strupper (char *s)
 
int tor_strisprint (const char *s)
 
int tor_strisnonupper (const char *s)
 
int tor_strisspace (const char *s)
 
int strcmp_opt (const char *s1, const char *s2)
 
int strcmpstart (const char *s1, const char *s2)
 
int strcasecmpstart (const char *s1, const char *s2)
 
int fast_memcmpstart (const void *mem, size_t memlen, const char *prefix)
 
int strcmpend (const char *s1, const char *s2)
 
int strcasecmpend (const char *s1, const char *s2)
 
const char * eat_whitespace (const char *s)
 
const char * eat_whitespace_eos (const char *s, const char *eos)
 
const char * eat_whitespace_no_nl (const char *s)
 
const char * eat_whitespace_eos_no_nl (const char *s, const char *eos)
 
const char * find_whitespace (const char *s)
 
const char * find_whitespace_eos (const char *s, const char *eos)
 
const char * find_str_at_start_of_line (const char *haystack, const char *needle)
 
int string_is_C_identifier (const char *string)
 
static uint8_t bytes_in_char (uint8_t b)
 
static bool is_continuation_byte (uint8_t b)
 
static bool validate_char (const uint8_t *c, uint8_t len)
 
int string_is_utf8 (const char *str, size_t len)
 
int string_is_utf8_no_bom (const char *str, size_t len)
 

Detailed Description

Non-standard string functions used throughout Tor.

Definition in file util_string.c.

Macro Definition Documentation

◆ LOW_BITS

#define LOW_BITS (   x)    ((uint8_t)(0xFF >> (8 - (x))))

A byte with the lowest x bits set.

Definition at line 440 of file util_string.c.

◆ TOP_BITS

#define TOP_BITS (   x)    ((uint8_t)(0xFF << (8 - (x))))

A byte with the top x bits set.

Definition at line 438 of file util_string.c.

Function Documentation

◆ bytes_in_char()

static uint8_t bytes_in_char ( uint8_t  b)
static

Given the leading byte b, return the total number of bytes in the UTF-8 character. Returns 0 if it's an invalid leading byte.

Definition at line 446 of file util_string.c.

References TOP_BITS.

Referenced by string_is_utf8().

◆ eat_whitespace()

const char* eat_whitespace ( const char *  s)

Return a pointer to the first char of s that is not whitespace and not a comment, or to the terminating NUL if no such character exists.

Definition at line 268 of file util_string.c.

Referenced by MOCK_IMPL(), parse_log_severity_config(), port_cfg_line_extract_addrport(), rend_parse_client_keys(), router_parse_addr_policy_private(), tor_addr_port_lookup(), and tor_version_parse_platform().

◆ eat_whitespace_eos()

const char* eat_whitespace_eos ( const char *  s,
const char *  eos 
)

Return a pointer to the first char of s that is not whitespace and not a comment, or to the terminating NUL if no such character exists.

Definition at line 295 of file util_string.c.

Referenced by authority_cert_parse_from_string(), find_start_of_next_microdesc(), find_start_of_next_router_or_extrainfo(), and pem_decode().

◆ eat_whitespace_eos_no_nl()

const char* eat_whitespace_eos_no_nl ( const char *  s,
const char *  eos 
)

As eat_whitespace_no_nl, but stop at eos whether we have found a non-whitespace character or not.

Definition at line 333 of file util_string.c.

◆ eat_whitespace_no_nl()

const char* eat_whitespace_no_nl ( const char *  s)

Return a pointer to the first char of s that is not a space or a tab or a \r, or to the terminating NUL if no such character exists.

Definition at line 323 of file util_string.c.

Referenced by parse_http_command().

◆ fast_mem_is_zero()

int fast_mem_is_zero ( const char *  mem,
size_t  len 
)

Return true iff the 'len' bytes at 'mem' are all zero.

Definition at line 74 of file util_string.c.

References fast_memcmp.

Referenced by check_sockaddr(), construct_ntor_key_map(), crypto_read_tagged_contents_from_file(), microdesc_has_curve25519_onion_key(), and routerinfo_incompatible_with_extrainfo().

◆ fast_memcmpstart()

int fast_memcmpstart ( const void *  mem,
size_t  memlen,
const char *  prefix 
)

Compare the value of the string prefix with the start of the memlen-byte memory chunk at mem. Return as for strcmp.

[As fast_memcmp(mem, prefix, strlen(prefix)) but returns -1 if memlen is less than strlen(prefix).]

Definition at line 229 of file util_string.c.

References fast_memcmp.

◆ find_str_at_start_of_line()

const char* find_str_at_start_of_line ( const char *  haystack,
const char *  needle 
)

Return the first occurrence of needle in haystack that occurs at the start of a line (that is, at the beginning of haystack or immediately after a newline). Return NULL if no such string is found.

Definition at line 391 of file util_string.c.

◆ find_whitespace()

const char* find_whitespace ( const char *  s)

Return a pointer to the first char of s that is whitespace or #, or to the terminating NUL if no such character exists.

Definition at line 344 of file util_string.c.

Referenced by parse_http_command(), tor_version_parse_platform(), and version_from_platform().

◆ find_whitespace_eos()

const char* find_whitespace_eos ( const char *  s,
const char *  eos 
)

As find_whitespace, but stop at eos whether we have found a whitespace or not.

Definition at line 366 of file util_string.c.

◆ is_continuation_byte()

static bool is_continuation_byte ( uint8_t  b)
static

Returns true iff b is a UTF-8 continuation byte.

Definition at line 463 of file util_string.c.

References TOP_BITS.

Referenced by validate_char().

◆ strcasecmpend()

int strcasecmpend ( const char *  s1,
const char *  s2 
)

Compares the last strlen(s2) characters of s1 with s2. Returns as for strcasecmp.

Definition at line 255 of file util_string.c.

Referenced by address_is_in_virtual_range(), addressmap_address_should_automap(), hostname_in_track_host_exits(), and tor_addr_parse_PTR_name().

◆ strcasecmpstart()

int strcasecmpstart ( const char *  s1,
const char *  s2 
)

Compares the first strlen(s2) characters of s1 with s2. Returns as for strcasecmp.

Definition at line 216 of file util_string.c.

Referenced by circuit_purpose_from_string(), config_lines_dup_and_filter(), handle_control_hspost(), http_get_header(), and tor_version_parse().

◆ strcmp_opt()

int strcmp_opt ( const char *  s1,
const char *  s2 
)

As strcmp, except that either string may be NULL. The NULL string is considered to be before any non-NULL string.

Definition at line 188 of file util_string.c.

Referenced by bridge_resolve_conflicts(), connection_dir_list_by_purpose_and_resource(), connection_dir_list_by_purpose_resource_and_state(), opt_streq(), and proxy_needs_restart().

◆ strcmpend()

int strcmpend ( const char *  s1,
const char *  s2 
)

Compares the last strlen(s2) characters of s1 with s2. Returns as for strcmp.

Definition at line 242 of file util_string.c.

Referenced by crypto_read_tagged_contents_from_file(), and storage_dir_clean_tmpfiles().

◆ strcmpstart()

int strcmpstart ( const char *  s1,
const char *  s2 
)

◆ string_is_C_identifier()

int string_is_C_identifier ( const char *  string)

Returns true if string could be a C identifier. A C identifier must begin with a letter or an underscore and the rest of its characters can be letters, numbers or underscores. No length limit is imposed.

Definition at line 414 of file util_string.c.

◆ string_is_utf8()

int string_is_utf8 ( const char *  str,
size_t  len 
)

Returns true iff the first len bytes in str are a valid UTF-8 string.

Definition at line 507 of file util_string.c.

References bytes_in_char(), and validate_char().

Referenced by string_is_utf8_no_bom().

◆ string_is_utf8_no_bom()

int string_is_utf8_no_bom ( const char *  str,
size_t  len 
)

As string_is_utf8(), but returns false if the string begins with a UTF-8 byte order mark (BOM).

Definition at line 531 of file util_string.c.

References strcmpstart(), and string_is_utf8().

◆ tor_digest256_is_zero()

int tor_digest256_is_zero ( const char *  digest)

Return true iff the DIGEST256_LEN bytes in digest are all zero.

Definition at line 103 of file util_string.c.

References DIGEST256_LEN, and safe_mem_is_zero().

◆ tor_digest_is_zero()

int tor_digest_is_zero ( const char *  digest)

◆ tor_memmem()

const void* tor_memmem ( const void *  _haystack,
size_t  hlen,
const void *  _needle,
size_t  nlen 
)

Given hlen bytes at haystack and nlen bytes at needle, return a pointer to the first occurrence of the needle within the haystack, or NULL if there is no such occurrence.

This function is not timing-safe.

Requires that nlen be greater than zero.

Definition at line 29 of file util_string.c.

◆ tor_strisnonupper()

int tor_strisnonupper ( const char *  s)

Return 1 if no character in s is uppercase, else return 0.

Definition at line 162 of file util_string.c.

Referenced by assert_resolve_ok().

◆ tor_strisprint()

int tor_strisprint ( const char *  s)

Return 1 if every character in s is printable, else return 0.

Definition at line 149 of file util_string.c.

◆ tor_strisspace()

int tor_strisspace ( const char *  s)

Return true iff every character in s is whitespace space; else return false.

Definition at line 175 of file util_string.c.

◆ tor_strlower()

void tor_strlower ( char *  s)

Convert all alphabetic characters in the nul-terminated string s to lowercase.

Definition at line 127 of file util_string.c.

Referenced by clear_trackexithost_mappings().

◆ tor_strstrip()

void tor_strstrip ( char *  s,
const char *  strip 
)

Remove from the string s every character which appears in strip.

Definition at line 111 of file util_string.c.

◆ tor_strupper()

void tor_strupper ( char *  s)

Convert all alphabetic characters in the nul-terminated string s to lowercase.

Definition at line 138 of file util_string.c.

◆ validate_char()

static bool validate_char ( const uint8_t *  c,
uint8_t  len 
)
static

Returns true iff the len bytes in c are a valid UTF-8 character.

Definition at line 473 of file util_string.c.

References is_continuation_byte(), and LOW_BITS.

Referenced by string_is_utf8().