mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-27 22:03:31 +01:00
Move confline.c to lib/encoding: it is about encoding key-value pairs
Also, move "unescape_string()" to encoding too, since it's about encoding data as C strings.
This commit is contained in:
parent
696f6f1569
commit
b9b44bf000
@ -36,7 +36,6 @@ LIBOR_A_SRC = \
|
||||
src/common/compat.c \
|
||||
src/common/compat_threads.c \
|
||||
src/common/compat_time.c \
|
||||
src/common/confline.c \
|
||||
src/common/conffile.c \
|
||||
src/common/memarea.c \
|
||||
src/common/util.c \
|
||||
@ -81,7 +80,6 @@ COMMONHEADERS = \
|
||||
src/common/compat_threads.h \
|
||||
src/common/compat_time.h \
|
||||
src/common/conffile.h \
|
||||
src/common/confline.h \
|
||||
src/common/handles.h \
|
||||
src/common/memarea.h \
|
||||
src/common/procmon.h \
|
||||
|
@ -453,125 +453,6 @@ tv_to_msec(const struct timeval *tv)
|
||||
* Filesystem operations.
|
||||
*/
|
||||
|
||||
#define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
|
||||
|
||||
/** Given a c-style double-quoted escaped string in <b>s</b>, extract and
|
||||
* decode its contents into a newly allocated string. On success, assign this
|
||||
* string to *<b>result</b>, assign its length to <b>size_out</b> (if
|
||||
* provided), and return a pointer to the position in <b>s</b> immediately
|
||||
* after the string. On failure, return NULL.
|
||||
*/
|
||||
const char *
|
||||
unescape_string(const char *s, char **result, size_t *size_out)
|
||||
{
|
||||
const char *cp;
|
||||
char *out;
|
||||
if (s[0] != '\"')
|
||||
return NULL;
|
||||
cp = s+1;
|
||||
while (1) {
|
||||
switch (*cp) {
|
||||
case '\0':
|
||||
case '\n':
|
||||
return NULL;
|
||||
case '\"':
|
||||
goto end_of_loop;
|
||||
case '\\':
|
||||
if (cp[1] == 'x' || cp[1] == 'X') {
|
||||
if (!(TOR_ISXDIGIT(cp[2]) && TOR_ISXDIGIT(cp[3])))
|
||||
return NULL;
|
||||
cp += 4;
|
||||
} else if (TOR_ISODIGIT(cp[1])) {
|
||||
cp += 2;
|
||||
if (TOR_ISODIGIT(*cp)) ++cp;
|
||||
if (TOR_ISODIGIT(*cp)) ++cp;
|
||||
} else if (cp[1] == 'n' || cp[1] == 'r' || cp[1] == 't' || cp[1] == '"'
|
||||
|| cp[1] == '\\' || cp[1] == '\'') {
|
||||
cp += 2;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
++cp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
end_of_loop:
|
||||
out = *result = tor_malloc(cp-s + 1);
|
||||
cp = s+1;
|
||||
while (1) {
|
||||
switch (*cp)
|
||||
{
|
||||
case '\"':
|
||||
*out = '\0';
|
||||
if (size_out) *size_out = out - *result;
|
||||
return cp+1;
|
||||
|
||||
/* LCOV_EXCL_START -- we caught this in parse_config_from_line. */
|
||||
case '\0':
|
||||
tor_fragile_assert();
|
||||
tor_free(*result);
|
||||
return NULL;
|
||||
/* LCOV_EXCL_STOP */
|
||||
case '\\':
|
||||
switch (cp[1])
|
||||
{
|
||||
case 'n': *out++ = '\n'; cp += 2; break;
|
||||
case 'r': *out++ = '\r'; cp += 2; break;
|
||||
case 't': *out++ = '\t'; cp += 2; break;
|
||||
case 'x': case 'X':
|
||||
{
|
||||
int x1, x2;
|
||||
|
||||
x1 = hex_decode_digit(cp[2]);
|
||||
x2 = hex_decode_digit(cp[3]);
|
||||
if (x1 == -1 || x2 == -1) {
|
||||
/* LCOV_EXCL_START */
|
||||
/* we caught this above in the initial loop. */
|
||||
tor_assert_nonfatal_unreached();
|
||||
tor_free(*result);
|
||||
return NULL;
|
||||
/* LCOV_EXCL_STOP */
|
||||
}
|
||||
|
||||
*out++ = ((x1<<4) + x2);
|
||||
cp += 4;
|
||||
}
|
||||
break;
|
||||
case '0': case '1': case '2': case '3': case '4': case '5':
|
||||
case '6': case '7':
|
||||
{
|
||||
int n = cp[1]-'0';
|
||||
cp += 2;
|
||||
if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
|
||||
if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
|
||||
if (n > 255) { tor_free(*result); return NULL; }
|
||||
*out++ = (char)n;
|
||||
}
|
||||
break;
|
||||
case '\'':
|
||||
case '\"':
|
||||
case '\\':
|
||||
case '\?':
|
||||
*out++ = cp[1];
|
||||
cp += 2;
|
||||
break;
|
||||
|
||||
/* LCOV_EXCL_START */
|
||||
default:
|
||||
/* we caught this above in the initial loop. */
|
||||
tor_assert_nonfatal_unreached();
|
||||
tor_free(*result); return NULL;
|
||||
/* LCOV_EXCL_STOP */
|
||||
}
|
||||
break;
|
||||
default:
|
||||
*out++ = *cp++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* =====
|
||||
* Process helpers
|
||||
* ===== */
|
||||
|
@ -39,6 +39,7 @@
|
||||
#include "lib/fs/files.h"
|
||||
#include "lib/fs/path.h"
|
||||
#include "lib/encoding/time_fmt.h"
|
||||
#include "lib/encoding/cstring.h"
|
||||
|
||||
void tor_log_mallinfo(int severity);
|
||||
|
||||
@ -102,8 +103,6 @@ const char *stream_status_to_string(enum stream_status stream_status);
|
||||
|
||||
enum stream_status get_string_from_pipe(int fd, char *buf, size_t count);
|
||||
|
||||
const char *unescape_string(const char *s, char **result, size_t *size_out);
|
||||
|
||||
/* Process helpers */
|
||||
void start_daemon(void);
|
||||
void finish_daemon(const char *desired_cwd);
|
||||
|
@ -4,11 +4,16 @@
|
||||
* Copyright (c) 2007-2018, The Tor Project, Inc. */
|
||||
/* See LICENSE for licensing information */
|
||||
|
||||
#include "common/compat.h"
|
||||
#include "common/confline.h"
|
||||
#include "lib/encoding/confline.h"
|
||||
#include "lib/encoding/cstring.h"
|
||||
#include "lib/log/torlog.h"
|
||||
#include "common/util.h"
|
||||
#include "lib/container/smartlist.h"
|
||||
#include "lib/log/util_bug.h"
|
||||
#include "lib/malloc/util_malloc.h"
|
||||
#include "lib/string/compat_ctype.h"
|
||||
#include "lib/string/compat_string.h"
|
||||
#include "lib/string/util_string.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/** Helper: allocate a new configuration option mapping 'key' to 'val',
|
||||
* append it to *<b>lst</b>. */
|
||||
@ -74,7 +79,7 @@ config_line_find(const config_line_t *lines,
|
||||
int
|
||||
config_get_lines_aux(const char *string, config_line_t **result, int extended,
|
||||
int allow_include, int *has_include,
|
||||
smartlist_t *opened_lst, int recursion_level,
|
||||
struct smartlist_t *opened_lst, int recursion_level,
|
||||
config_line_t **last,
|
||||
include_handler_fn handle_include)
|
||||
{
|
132
src/lib/encoding/cstring.c
Normal file
132
src/lib/encoding/cstring.c
Normal file
@ -0,0 +1,132 @@
|
||||
/* Copyright (c) 2001 Matej Pfajfar.
|
||||
* Copyright (c) 2001-2004, Roger Dingledine.
|
||||
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
||||
* Copyright (c) 2007-2018, The Tor Project, Inc. */
|
||||
/* See LICENSE for licensing information */
|
||||
|
||||
#include "lib/encoding/cstring.h"
|
||||
#include "lib/log/torlog.h"
|
||||
#include "lib/log/util_bug.h"
|
||||
#include "lib/malloc/util_malloc.h"
|
||||
#include "lib/string/compat_ctype.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define TOR_ISODIGIT(c) ('0' <= (c) && (c) <= '7')
|
||||
|
||||
/** Given a c-style double-quoted escaped string in <b>s</b>, extract and
|
||||
* decode its contents into a newly allocated string. On success, assign this
|
||||
* string to *<b>result</b>, assign its length to <b>size_out</b> (if
|
||||
* provided), and return a pointer to the position in <b>s</b> immediately
|
||||
* after the string. On failure, return NULL.
|
||||
*/
|
||||
const char *
|
||||
unescape_string(const char *s, char **result, size_t *size_out)
|
||||
{
|
||||
const char *cp;
|
||||
char *out;
|
||||
if (s[0] != '\"')
|
||||
return NULL;
|
||||
cp = s+1;
|
||||
while (1) {
|
||||
switch (*cp) {
|
||||
case '\0':
|
||||
case '\n':
|
||||
return NULL;
|
||||
case '\"':
|
||||
goto end_of_loop;
|
||||
case '\\':
|
||||
if (cp[1] == 'x' || cp[1] == 'X') {
|
||||
if (!(TOR_ISXDIGIT(cp[2]) && TOR_ISXDIGIT(cp[3])))
|
||||
return NULL;
|
||||
cp += 4;
|
||||
} else if (TOR_ISODIGIT(cp[1])) {
|
||||
cp += 2;
|
||||
if (TOR_ISODIGIT(*cp)) ++cp;
|
||||
if (TOR_ISODIGIT(*cp)) ++cp;
|
||||
} else if (cp[1] == 'n' || cp[1] == 'r' || cp[1] == 't' || cp[1] == '"'
|
||||
|| cp[1] == '\\' || cp[1] == '\'') {
|
||||
cp += 2;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
++cp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
end_of_loop:
|
||||
out = *result = tor_malloc(cp-s + 1);
|
||||
cp = s+1;
|
||||
while (1) {
|
||||
switch (*cp)
|
||||
{
|
||||
case '\"':
|
||||
*out = '\0';
|
||||
if (size_out) *size_out = out - *result;
|
||||
return cp+1;
|
||||
|
||||
/* LCOV_EXCL_START -- we caught this in parse_config_from_line. */
|
||||
case '\0':
|
||||
tor_fragile_assert();
|
||||
tor_free(*result);
|
||||
return NULL;
|
||||
/* LCOV_EXCL_STOP */
|
||||
case '\\':
|
||||
switch (cp[1])
|
||||
{
|
||||
case 'n': *out++ = '\n'; cp += 2; break;
|
||||
case 'r': *out++ = '\r'; cp += 2; break;
|
||||
case 't': *out++ = '\t'; cp += 2; break;
|
||||
case 'x': case 'X':
|
||||
{
|
||||
int x1, x2;
|
||||
|
||||
x1 = hex_decode_digit(cp[2]);
|
||||
x2 = hex_decode_digit(cp[3]);
|
||||
if (x1 == -1 || x2 == -1) {
|
||||
/* LCOV_EXCL_START */
|
||||
/* we caught this above in the initial loop. */
|
||||
tor_assert_nonfatal_unreached();
|
||||
tor_free(*result);
|
||||
return NULL;
|
||||
/* LCOV_EXCL_STOP */
|
||||
}
|
||||
|
||||
*out++ = ((x1<<4) + x2);
|
||||
cp += 4;
|
||||
}
|
||||
break;
|
||||
case '0': case '1': case '2': case '3': case '4': case '5':
|
||||
case '6': case '7':
|
||||
{
|
||||
int n = cp[1]-'0';
|
||||
cp += 2;
|
||||
if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
|
||||
if (TOR_ISODIGIT(*cp)) { n = n*8 + *cp-'0'; cp++; }
|
||||
if (n > 255) { tor_free(*result); return NULL; }
|
||||
*out++ = (char)n;
|
||||
}
|
||||
break;
|
||||
case '\'':
|
||||
case '\"':
|
||||
case '\\':
|
||||
case '\?':
|
||||
*out++ = cp[1];
|
||||
cp += 2;
|
||||
break;
|
||||
|
||||
/* LCOV_EXCL_START */
|
||||
default:
|
||||
/* we caught this above in the initial loop. */
|
||||
tor_assert_nonfatal_unreached();
|
||||
tor_free(*result); return NULL;
|
||||
/* LCOV_EXCL_STOP */
|
||||
}
|
||||
break;
|
||||
default:
|
||||
*out++ = *cp++;
|
||||
}
|
||||
}
|
||||
}
|
13
src/lib/encoding/cstring.h
Normal file
13
src/lib/encoding/cstring.h
Normal file
@ -0,0 +1,13 @@
|
||||
/* Copyright (c) 2001 Matej Pfajfar.
|
||||
* Copyright (c) 2001-2004, Roger Dingledine.
|
||||
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
||||
* Copyright (c) 2007-2018, The Tor Project, Inc. */
|
||||
/* See LICENSE for licensing information */
|
||||
|
||||
#ifndef TOR_CSTRING_H
|
||||
#define TOR_CSTRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
const char *unescape_string(const char *s, char **result, size_t *size_out);
|
||||
|
||||
#endif /* !defined(TOR_CSTRING_H) */
|
@ -6,6 +6,8 @@ endif
|
||||
|
||||
src_lib_libtor_encoding_a_SOURCES = \
|
||||
src/lib/encoding/binascii.c \
|
||||
src/lib/encoding/confline.c \
|
||||
src/lib/encoding/cstring.c \
|
||||
src/lib/encoding/time_fmt.c
|
||||
|
||||
src_lib_libtor_encoding_testing_a_SOURCES = \
|
||||
@ -15,4 +17,6 @@ src_lib_libtor_encoding_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
|
||||
|
||||
noinst_HEADERS += \
|
||||
src/lib/encoding/binascii.h \
|
||||
src/lib/encoding/confline.h \
|
||||
src/lib/encoding/cstring.h \
|
||||
src/lib/encoding/time_fmt.h
|
||||
|
Loading…
Reference in New Issue
Block a user