2012-07-11 04:41:59 +00:00
|
|
|
/*
|
|
|
|
Serval string buffer helper functions.
|
2012-07-30 05:20:08 +00:00
|
|
|
Copyright (C) 2012 Serval Project Inc.
|
2012-07-11 04:41:59 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __STRBUF_HELPERS_H__
|
|
|
|
#define __STRBUF_HELPERS_H__
|
|
|
|
|
2016-10-19 05:57:20 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-09-18 07:06:28 +00:00
|
|
|
// For socklen_t
|
|
|
|
#ifdef WIN32
|
|
|
|
# include "win32/win32.h"
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_SYS_SOCKET_H
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2012-07-11 04:41:59 +00:00
|
|
|
#include "strbuf.h"
|
2014-05-02 06:15:01 +00:00
|
|
|
#include "os.h"
|
2012-07-11 04:41:59 +00:00
|
|
|
|
2012-07-27 08:18:05 +00:00
|
|
|
/* Append a representation of the given chars in a given buffer (including nul
|
|
|
|
* chars) in printable format, ie, with non-printable characters expanded to \n
|
|
|
|
* \r \t \0 \\ \xHH.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_toprint_len(strbuf sb, const char *buf, size_t len);
|
|
|
|
|
|
|
|
/* Equivalent to strbuf_toprint_len(sb, str, strlen(str)).
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_toprint(strbuf sb, const char *str);
|
|
|
|
|
|
|
|
/* Same as strbuf_toprint_len, but also delimits the appended printable text
|
|
|
|
* with a given quote character and escapes that quotation char with a
|
|
|
|
* backslash within the text.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-09-28 08:16:40 +00:00
|
|
|
strbuf strbuf_toprint_quoted_len(strbuf sb, const char quotes[2], const char *buf, size_t len);
|
2012-07-27 08:18:05 +00:00
|
|
|
|
|
|
|
/* Equivalent to strbuf_toprint_quoted_len(sb, str, strlen(str)).
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-09-28 08:16:40 +00:00
|
|
|
strbuf strbuf_toprint_quoted(strbuf sb, const char quotes[2], const char *str);
|
2012-07-27 08:18:05 +00:00
|
|
|
|
2015-06-22 08:34:41 +00:00
|
|
|
/* Append a symbolic representation of a struct __sourceloc.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
struct __sourceloc;
|
|
|
|
strbuf strbuf_append_sourceloc(strbuf sb, struct __sourceloc);
|
|
|
|
|
2012-12-03 23:17:39 +00:00
|
|
|
/* Join Unix file path segments together with separator characters '/' to form
|
|
|
|
* a complete path. Any segment that starts with '/' is taken as the start of
|
|
|
|
* an absolute path, and all prior segments are discarded.
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_path_join(strbuf sb, ...);
|
|
|
|
|
2012-07-11 04:41:59 +00:00
|
|
|
/* Append a symbolic representation of the poll(2) event flags.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_poll_events(strbuf sb, short events);
|
2013-10-16 13:31:32 +00:00
|
|
|
#define alloca_poll_events(ev) strbuf_str(strbuf_append_poll_events(strbuf_alloca(200), (ev)))
|
2012-07-11 04:41:59 +00:00
|
|
|
|
2012-07-30 05:20:08 +00:00
|
|
|
/* Append a nul-terminated string as a single-quoted shell word which, if
|
|
|
|
* expanded in a shell command line, would evaluate to the original string.
|
|
|
|
* Eg:
|
|
|
|
* "abc" -> "'abc'"
|
|
|
|
* "" -> "''"
|
|
|
|
* "O'Toole" -> "'O'\''Toole'"
|
|
|
|
*
|
2012-07-26 09:00:24 +00:00
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_shell_quote(strbuf sb, const char *word);
|
|
|
|
|
2012-07-30 05:20:08 +00:00
|
|
|
/* Append a nul-terminated string as a shell word, quoted if it contains shell
|
|
|
|
* metacharacters or spaces. In other words, is acts like
|
|
|
|
* str_append_shell_quote() but only if needed. Eg:
|
|
|
|
* "abc" -> "abc"
|
|
|
|
* "a b c " -> "'a b c '"
|
|
|
|
* "$abc" -> "'$abc'"
|
|
|
|
* "" -> "''"
|
|
|
|
*
|
2012-07-24 06:09:36 +00:00
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_shell_quotemeta(strbuf sb, const char *word);
|
|
|
|
|
2012-12-03 23:17:39 +00:00
|
|
|
/* Append an array of nul-terminated strings as a space-separated sequence of
|
|
|
|
* quoted strings. Any NULL entry in argv[] is printed as unquoted "NULL".
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_argv(strbuf sb, int argc, const char *const *argv);
|
2016-10-12 06:38:26 +00:00
|
|
|
#define alloca_argv(argc, argv) strbuf_str(strbuf_append_argv(strbuf_alloca(strbuf_count(strbuf_append_argv(strbuf_alloca(0), (argc), (argv)))), (argc), (argv)))
|
2012-12-03 23:17:39 +00:00
|
|
|
|
2012-07-18 09:46:30 +00:00
|
|
|
/* Append a textual description of a process exit status as produced by wait(2)
|
|
|
|
* and waitpid(2).
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_exit_status(strbuf sb, int status);
|
|
|
|
|
2014-05-08 01:49:43 +00:00
|
|
|
/* Append a textual description of a signal as used by kill(2) and signal(2).
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_signal_name(strbuf sb, int signal);
|
|
|
|
#define alloca_signal_name(sig) strbuf_str(strbuf_append_signal_name(strbuf_alloca(80), (sig)))
|
|
|
|
|
2013-09-18 07:06:28 +00:00
|
|
|
/* Append a textual description of a socket domain code (AF_...).
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_socket_domain(strbuf sb, int domain);
|
|
|
|
#define alloca_socket_domain(domain) strbuf_str(strbuf_append_socket_domain(strbuf_alloca(15), domain))
|
|
|
|
|
|
|
|
/* Append a textual description of a socket type code (SOCK_...).
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_socket_type(strbuf sb, int type);
|
|
|
|
#define alloca_socket_type(type) strbuf_str(strbuf_append_socket_type(strbuf_alloca(15), type))
|
|
|
|
|
2013-10-28 01:38:57 +00:00
|
|
|
/* Append a textual description of a struct in_addr (in network order) as IPv4
|
|
|
|
* quartet "N.N.N.N".
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
struct in_addr;
|
|
|
|
strbuf strbuf_append_in_addr(strbuf sb, const struct in_addr *addr);
|
|
|
|
#define alloca_in_addr(addr) strbuf_str(strbuf_append_in_addr(strbuf_alloca(16), (const struct in_addr *)(addr)))
|
|
|
|
|
2013-11-26 03:11:46 +00:00
|
|
|
/* Append a textual description of a struct sockaddr.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-10-23 07:37:56 +00:00
|
|
|
struct sockaddr;
|
2013-09-18 07:06:28 +00:00
|
|
|
strbuf strbuf_append_sockaddr(strbuf sb, const struct sockaddr *addr, socklen_t addrlen);
|
|
|
|
#define alloca_sockaddr(addr, addrlen) strbuf_str(strbuf_append_sockaddr(strbuf_alloca(200), (const struct sockaddr *)(addr), (addrlen)))
|
2012-11-20 03:13:51 +00:00
|
|
|
|
2013-11-25 01:45:20 +00:00
|
|
|
struct socket_address;
|
|
|
|
strbuf strbuf_append_socket_address(strbuf sb, const struct socket_address *addr);
|
|
|
|
#define alloca_socket_address(addr) strbuf_str(strbuf_append_socket_address(strbuf_alloca(200), (addr)))
|
|
|
|
|
2014-04-16 09:38:42 +00:00
|
|
|
struct fragmented_data;
|
|
|
|
strbuf strbuf_append_fragmented_data(strbuf sb, const struct fragmented_data *data);
|
|
|
|
#define alloca_fragmented_data(data) strbuf_str(strbuf_append_fragmented_data(strbuf_alloca(200), (data)))
|
|
|
|
|
2013-04-04 07:10:58 +00:00
|
|
|
/* Append a strftime(3) string.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
struct tm;
|
|
|
|
strbuf strbuf_append_strftime(strbuf sb, const char *format, const struct tm *tm);
|
2014-04-16 09:42:01 +00:00
|
|
|
#define alloca_strftime(fmt,tm) strbuf_str(strbuf_append_strftime(strbuf_alloca(40), (fmt), (tm)))
|
2013-04-04 07:10:58 +00:00
|
|
|
|
2013-10-11 17:24:18 +00:00
|
|
|
/* Append a representation of a struct iovec[] array.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
struct iovec;
|
|
|
|
strbuf strbuf_append_iovec(strbuf sb, const struct iovec *iov, int iovcnt);
|
|
|
|
#define alloca_iovec(iov,cnt) strbuf_str(strbuf_append_iovec(strbuf_alloca(200), (iov), (cnt)))
|
|
|
|
|
2014-05-02 06:15:01 +00:00
|
|
|
/* Append a representation of a time_t value (second resolution).
|
2014-04-29 05:34:20 +00:00
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_time_t(strbuf sb, time_t);
|
|
|
|
#define alloca_time_t(t) strbuf_str(strbuf_append_time_t(strbuf_alloca(40), (t)))
|
|
|
|
|
2014-05-02 06:15:01 +00:00
|
|
|
/* Append a representation of a time_ms_t value (millisecond resolution).
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_time_ms_t(strbuf sb, time_ms_t);
|
|
|
|
#define alloca_time_ms_t(t) strbuf_str(strbuf_append_time_ms_t(strbuf_alloca(45), (t)))
|
|
|
|
|
|
|
|
/* Append a representation of a struct timeval (microsecond resolution).
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
struct timeval;
|
|
|
|
strbuf strbuf_append_timeval(strbuf sb, const struct timeval *tv);
|
|
|
|
#define alloca_timeval(tv) strbuf_str(strbuf_append_timeval(strbuf_alloca(50), (tv)))
|
|
|
|
|
|
|
|
/* Append a representation of a struct timespec (nanosecond resolution).
|
2014-04-29 05:34:20 +00:00
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
struct timespec;
|
|
|
|
strbuf strbuf_append_timespec(strbuf sb, const struct timespec *tv);
|
2014-05-02 06:15:01 +00:00
|
|
|
#define alloca_timespec(tv) strbuf_str(strbuf_append_timespec(strbuf_alloca(55), (tv)))
|
2014-04-29 05:34:20 +00:00
|
|
|
|
|
|
|
/* Append a representation of a struct file_meta.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
struct file_meta;
|
|
|
|
strbuf strbuf_append_file_meta(strbuf sb, const struct file_meta *metap);
|
|
|
|
#define alloca_file_meta(metap) strbuf_str(strbuf_append_file_meta(strbuf_alloca(80), (metap)))
|
|
|
|
|
2018-03-21 07:31:33 +00:00
|
|
|
/* Append a representation of routing reachable flags.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_reachable_flags(strbuf sb, int flags);
|
|
|
|
#define alloca_reachable_flags(flags) strbuf_str(strbuf_append_reachable_flags(strbuf_alloca(80), (flags)))
|
|
|
|
|
2013-10-28 11:57:27 +00:00
|
|
|
/* Append a string using HTTP quoted-string format: delimited by double quotes (") and
|
|
|
|
* internal double quotes and backslash escaped by leading backslash.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_quoted_string(strbuf sb, const char *str);
|
|
|
|
|
2017-10-19 23:25:21 +00:00
|
|
|
/* Append a string using HTTP token|quoted-string format: if it contains only
|
|
|
|
* token characters, then unmodified, otherwise as a quoted-string
|
|
|
|
* (strbuf_append_quoted_string).
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_append_token_or_quoted_string(strbuf sb, const char *str);
|
|
|
|
|
2014-02-07 05:29:34 +00:00
|
|
|
/* Escape HTML entities.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_html_escape(strbuf sb, const char *, size_t);
|
|
|
|
|
2013-11-07 13:09:53 +00:00
|
|
|
/* Append various JSON elements.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_json_null(strbuf sb);
|
2014-01-20 05:13:37 +00:00
|
|
|
strbuf strbuf_json_boolean(strbuf sb, int boolean);
|
2018-03-26 00:54:39 +00:00
|
|
|
strbuf strbuf_json_integer(strbuf sb, int64_t integer);
|
2014-02-05 03:58:15 +00:00
|
|
|
strbuf strbuf_json_string(strbuf sb, const char *str); // str can be NULL
|
|
|
|
strbuf strbuf_json_string_len(strbuf sb, const char *str, size_t strlen); // str cannot be NULL
|
2013-11-07 13:09:53 +00:00
|
|
|
strbuf strbuf_json_hex(strbuf sb, const unsigned char *buf, size_t len);
|
2015-08-31 09:33:40 +00:00
|
|
|
struct json_key_value {
|
|
|
|
const char *key;
|
|
|
|
struct json_atom *value;
|
|
|
|
};
|
2014-02-07 05:29:34 +00:00
|
|
|
struct json_atom {
|
2015-08-31 09:33:40 +00:00
|
|
|
enum json_atomic_type {
|
|
|
|
JSON_NULL,
|
|
|
|
JSON_BOOLEAN, // u.boolean
|
|
|
|
JSON_INTEGER, // u.integer
|
|
|
|
JSON_STRING_NULTERM, // u.string.content (nul terminated)
|
|
|
|
JSON_STRING_LENGTH, // u.string.content[0 .. u.string.length-1]
|
|
|
|
JSON_OBJECT, // u.object.itemv[0 .. u.object.itemc].key .value
|
|
|
|
JSON_ARRAY, // u.array.itemv[0 .. u.object.itemc]
|
|
|
|
} type;
|
2014-02-07 05:29:34 +00:00
|
|
|
union {
|
|
|
|
int boolean;
|
|
|
|
int64_t integer;
|
|
|
|
struct {
|
|
|
|
const char *content;
|
|
|
|
size_t length;
|
|
|
|
} string;
|
2015-08-31 09:33:40 +00:00
|
|
|
struct {
|
|
|
|
size_t itemc;
|
|
|
|
struct json_key_value *itemv;
|
|
|
|
} object;
|
|
|
|
struct {
|
|
|
|
size_t itemc;
|
|
|
|
struct json_atom **itemv;
|
|
|
|
} array;
|
2014-02-07 05:29:34 +00:00
|
|
|
} u;
|
|
|
|
};
|
|
|
|
strbuf strbuf_json_atom(strbuf sb, const struct json_atom *);
|
|
|
|
strbuf strbuf_json_atom_as_html(strbuf sb, const struct json_atom *);
|
2015-08-31 09:33:40 +00:00
|
|
|
strbuf strbuf_json_atom_as_text(strbuf sb, const struct json_atom *, const char *eol);
|
2013-11-07 13:09:53 +00:00
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
/* Append a representation of a struct http_range[] array.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
struct http_range;
|
|
|
|
strbuf strbuf_append_http_ranges(strbuf sb, const struct http_range *ranges, unsigned nels);
|
|
|
|
#define alloca_http_ranges(ra) strbuf_str(strbuf_append_http_ranges(strbuf_alloca(25*NELS(ra)), (ra), NELS(ra)))
|
|
|
|
|
2013-10-28 11:57:27 +00:00
|
|
|
/* Append a representation of a struct mime_content_type in HTTP header format.
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
struct mime_content_type;
|
|
|
|
strbuf strbuf_append_mime_content_type(strbuf, const struct mime_content_type *);
|
|
|
|
#define alloca_mime_content_type(ct) strbuf_str(strbuf_append_mime_content_type(strbuf_alloca(500), (ct)))
|
|
|
|
|
|
|
|
/* Append a representation of a struct mime_content_disposition, in HTTP header format.
|
2013-10-26 15:07:07 +00:00
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
struct mime_content_disposition;
|
|
|
|
strbuf strbuf_append_mime_content_disposition(strbuf, const struct mime_content_disposition *);
|
|
|
|
#define alloca_mime_content_disposition(cd) strbuf_str(strbuf_append_mime_content_disposition(strbuf_alloca(500), (cd)))
|
|
|
|
|
2012-07-11 04:41:59 +00:00
|
|
|
#endif //__STRBUF_HELPERS_H__
|