Tor  0.4.7.0-alpha-dev
Macros | Functions
util_string.c File Reference

Non-standard string functions used throughout Tor. More...

#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)
 
void tor_strreplacechar (char *s, char find, char replacement)
 
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 449 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 447 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 455 of file util_string.c.

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 277 of file util_string.c.

Referenced by parse_log_severity_config(), port_cfg_line_extract_addrport(), router_parse_addr_policy_item_from_string(), 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 304 of file util_string.c.

Referenced by authority_cert_parse_from_string(), find_start_of_next_microdesc(), find_start_of_next_router_or_extrainfo(), microdescs_parse_from_string(), 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 342 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 332 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 
)

◆ 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 238 of file util_string.c.

◆ 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 400 of file util_string.c.

Referenced by get_total_system_memory_impl().

◆ 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 353 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 375 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 472 of file util_string.c.

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

Referenced by address_is_in_virtual_range(), addressmap_address_should_automap(), hostname_in_track_host_exits(), tor_addr_hostname_is_local(), 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 225 of file util_string.c.

Referenced by circuit_purpose_from_string(), config_lines_dup_and_filter(), handle_control_hspost(), http_get_header(), parse_dirauth_dirport(), 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 197 of file util_string.c.

Referenced by bridge_resolve_conflicts(), compare_by_valid_after_(), 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 251 of file util_string.c.

Referenced by auth_key_filename_is_valid(), client_filename_is_valid(), crypto_read_tagged_contents_from_file(), get_onion_public_key(), 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 423 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 516 of file util_string.c.

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 557 of file util_string.c.

◆ 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.

◆ 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 171 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 158 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 184 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_strreplacechar()

void tor_strreplacechar ( char *  s,
char  find,
char  replacement 
)

Replaces old with replacement in s

Definition at line 148 of file util_string.c.

◆ 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 482 of file util_string.c.

Referenced by string_is_utf8().