Tor  0.4.7.0-alpha-dev
Functions
kvline.c File Reference

Manipulating lines of key-value pairs. More...

#include "orconfig.h"
#include "lib/container/smartlist.h"
#include "lib/encoding/confline.h"
#include "lib/encoding/cstring.h"
#include "lib/encoding/kvline.h"
#include "lib/encoding/qstring.h"
#include "lib/malloc/malloc.h"
#include "lib/string/compat_ctype.h"
#include "lib/string/printf.h"
#include "lib/string/util_string.h"
#include "lib/log/escape.h"
#include "lib/log/util_bug.h"
#include <stdbool.h>
#include <stddef.h>
#include <string.h>

Go to the source code of this file.

Functions

static bool needs_escape (const char *s, bool as_keyless_val)
 
static bool line_has_no_key (const config_line_t *line)
 
static bool line_has_no_val (const config_line_t *line)
 
static bool kvline_can_encode_lines (const config_line_t *line, unsigned flags)
 
char * kvline_encode (const config_line_t *line, unsigned flags)
 
config_line_tkvline_parse (const char *line, unsigned flags)
 

Detailed Description

Manipulating lines of key-value pairs.

Definition in file kvline.c.

Function Documentation

◆ kvline_can_encode_lines()

static bool kvline_can_encode_lines ( const config_line_t line,
unsigned  flags 
)
static

Return true iff the all the lines in line can be encoded using flags.

Definition at line 79 of file kvline.c.

◆ kvline_encode()

char* kvline_encode ( const config_line_t line,
unsigned  flags 
)

Encode a linked list of lines in line as a series of 'Key=Value' pairs, using the provided flags to encode it. Return a newly allocated string on success, or NULL on failure.

If KV_QUOTED is set in flags, then all values that contain spaces or unusual characters are escaped and quoted. Otherwise, such values are not allowed. Mutually exclusive with KV_RAW.

If KV_OMIT_KEYS is set in flags, then pairs with empty keys are allowed, and are encoded as 'Value'. Otherwise, such pairs are not allowed.

If KV_OMIT_VALS is set in flags, then an empty value is encoded as 'Key', not as 'Key=' or 'Key=""'. Mutually exclusive with KV_OMIT_KEYS.

If KV_RAW is set in flags, then don't apply any quoting to the value, and assume that the caller has adequately quoted it. (The control protocol has some quirks that make this necessary.) Mutually exclusive with KV_QUOTED.

KV_QUOTED_QSTRING is not supported.

Definition at line 126 of file kvline.c.

Referenced by control_write_reply_line().

◆ kvline_parse()

config_line_t* kvline_parse ( const char *  line,
unsigned  flags 
)

Decode a line containing a series of space-separated 'Key=Value' pairs, using the provided flags to decode it. Return a newly allocated list of pairs on success, or NULL on failure.

If KV_QUOTED is set in flags, then (double-)quoted values are allowed and handled as C strings. Otherwise, such values are not allowed.

If KV_OMIT_KEYS is set in flags, then values without keys are allowed. Otherwise, such values are not allowed.

If KV_OMIT_VALS is set in flags, then keys without values are allowed. Otherwise, such keys are not allowed. Mutually exclusive with KV_OMIT_KEYS.

If KV_QUOTED_QSTRING is set in flags, then double-quoted values are allowed and handled as QuotedStrings per qstring.c. Do not add new users of this flag.

KV_RAW is not supported.

Definition at line 199 of file kvline.c.

◆ line_has_no_key()

static bool line_has_no_key ( const config_line_t line)
static

Return true iff the key in line is not set.

Definition at line 60 of file kvline.c.

Referenced by kvline_can_encode_lines().

◆ line_has_no_val()

static bool line_has_no_val ( const config_line_t line)
static

Return true iff the value in line is not set.

Definition at line 69 of file kvline.c.

◆ needs_escape()

static bool needs_escape ( const char *  s,
bool  as_keyless_val 
)
static

Return true iff we need to quote and escape the string s to encode it.

kvline_can_encode_lines() also uses this (with as_keyless_val true) to check whether a key would require quoting.

Definition at line 39 of file kvline.c.